saa717x.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362
  1. /*
  2. * saa717x - Philips SAA717xHL video decoder driver
  3. *
  4. * Based on the saa7115 driver
  5. *
  6. * Changes by Ohta Kyuma <alpha292@bremen.or.jp>
  7. * - Apply to SAA717x,NEC uPD64031,uPD64083. (1/31/2004)
  8. *
  9. * Changes by T.Adachi (tadachi@tadachi-net.com)
  10. * - support audio, video scaler etc, and checked the initialize sequence.
  11. *
  12. * Cleaned up by Hans Verkuil <hverkuil@xs4all.nl>
  13. *
  14. * Note: this is a reversed engineered driver based on captures from
  15. * the I2C bus under Windows. This chip is very similar to the saa7134,
  16. * though. Unfortunately, this driver is currently only working for NTSC.
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/slab.h>
  31. #include <linux/sched.h>
  32. #include <linux/videodev2.h>
  33. #include <linux/i2c.h>
  34. #include <media/v4l2-device.h>
  35. #include <media/v4l2-ctrls.h>
  36. MODULE_DESCRIPTION("Philips SAA717x audio/video decoder driver");
  37. MODULE_AUTHOR("K. Ohta, T. Adachi, Hans Verkuil");
  38. MODULE_LICENSE("GPL");
  39. static int debug;
  40. module_param(debug, int, 0644);
  41. MODULE_PARM_DESC(debug, "Debug level (0-1)");
  42. /*
  43. * Generic i2c probe
  44. * concerning the addresses: i2c wants 7 bit (without the r/w bit), so '>>1'
  45. */
  46. struct saa717x_state {
  47. struct v4l2_subdev sd;
  48. struct v4l2_ctrl_handler hdl;
  49. v4l2_std_id std;
  50. int input;
  51. int enable;
  52. int radio;
  53. int playback;
  54. int audio;
  55. int tuner_audio_mode;
  56. int audio_main_mute;
  57. int audio_main_vol_r;
  58. int audio_main_vol_l;
  59. u16 audio_main_bass;
  60. u16 audio_main_treble;
  61. u16 audio_main_volume;
  62. u16 audio_main_balance;
  63. int audio_input;
  64. };
  65. static inline struct saa717x_state *to_state(struct v4l2_subdev *sd)
  66. {
  67. return container_of(sd, struct saa717x_state, sd);
  68. }
  69. static inline struct v4l2_subdev *to_sd(struct v4l2_ctrl *ctrl)
  70. {
  71. return &container_of(ctrl->handler, struct saa717x_state, hdl)->sd;
  72. }
  73. /* ----------------------------------------------------------------------- */
  74. /* for audio mode */
  75. #define TUNER_AUDIO_MONO 0 /* LL */
  76. #define TUNER_AUDIO_STEREO 1 /* LR */
  77. #define TUNER_AUDIO_LANG1 2 /* LL */
  78. #define TUNER_AUDIO_LANG2 3 /* RR */
  79. #define SAA717X_NTSC_WIDTH (704)
  80. #define SAA717X_NTSC_HEIGHT (480)
  81. /* ----------------------------------------------------------------------- */
  82. static int saa717x_write(struct v4l2_subdev *sd, u32 reg, u32 value)
  83. {
  84. struct i2c_client *client = v4l2_get_subdevdata(sd);
  85. struct i2c_adapter *adap = client->adapter;
  86. int fw_addr = reg == 0x454 || (reg >= 0x464 && reg <= 0x478) || reg == 0x480 || reg == 0x488;
  87. unsigned char mm1[6];
  88. struct i2c_msg msg;
  89. msg.flags = 0;
  90. msg.addr = client->addr;
  91. mm1[0] = (reg >> 8) & 0xff;
  92. mm1[1] = reg & 0xff;
  93. if (fw_addr) {
  94. mm1[4] = (value >> 16) & 0xff;
  95. mm1[3] = (value >> 8) & 0xff;
  96. mm1[2] = value & 0xff;
  97. } else {
  98. mm1[2] = value & 0xff;
  99. }
  100. msg.len = fw_addr ? 5 : 3; /* Long Registers have *only* three bytes! */
  101. msg.buf = mm1;
  102. v4l2_dbg(2, debug, sd, "wrote: reg 0x%03x=%08x\n", reg, value);
  103. return i2c_transfer(adap, &msg, 1) == 1;
  104. }
  105. static void saa717x_write_regs(struct v4l2_subdev *sd, u32 *data)
  106. {
  107. while (data[0] || data[1]) {
  108. saa717x_write(sd, data[0], data[1]);
  109. data += 2;
  110. }
  111. }
  112. static u32 saa717x_read(struct v4l2_subdev *sd, u32 reg)
  113. {
  114. struct i2c_client *client = v4l2_get_subdevdata(sd);
  115. struct i2c_adapter *adap = client->adapter;
  116. int fw_addr = (reg >= 0x404 && reg <= 0x4b8) || reg == 0x528;
  117. unsigned char mm1[2];
  118. unsigned char mm2[4] = { 0, 0, 0, 0 };
  119. struct i2c_msg msgs[2];
  120. u32 value;
  121. msgs[0].flags = 0;
  122. msgs[1].flags = I2C_M_RD;
  123. msgs[0].addr = msgs[1].addr = client->addr;
  124. mm1[0] = (reg >> 8) & 0xff;
  125. mm1[1] = reg & 0xff;
  126. msgs[0].len = 2;
  127. msgs[0].buf = mm1;
  128. msgs[1].len = fw_addr ? 3 : 1; /* Multibyte Registers contains *only* 3 bytes */
  129. msgs[1].buf = mm2;
  130. i2c_transfer(adap, msgs, 2);
  131. if (fw_addr)
  132. value = (mm2[2] << 16) | (mm2[1] << 8) | mm2[0];
  133. else
  134. value = mm2[0];
  135. v4l2_dbg(2, debug, sd, "read: reg 0x%03x=0x%08x\n", reg, value);
  136. return value;
  137. }
  138. /* ----------------------------------------------------------------------- */
  139. static u32 reg_init_initialize[] =
  140. {
  141. /* from linux driver */
  142. 0x101, 0x008, /* Increment delay */
  143. 0x103, 0x000, /* Analog input control 2 */
  144. 0x104, 0x090, /* Analog input control 3 */
  145. 0x105, 0x090, /* Analog input control 4 */
  146. 0x106, 0x0eb, /* Horizontal sync start */
  147. 0x107, 0x0e0, /* Horizontal sync stop */
  148. 0x109, 0x055, /* Luminance control */
  149. 0x10f, 0x02a, /* Chroma gain control */
  150. 0x110, 0x000, /* Chroma control 2 */
  151. 0x114, 0x045, /* analog/ADC */
  152. 0x118, 0x040, /* RAW data gain */
  153. 0x119, 0x080, /* RAW data offset */
  154. 0x044, 0x000, /* VBI horizontal input window start (L) TASK A */
  155. 0x045, 0x000, /* VBI horizontal input window start (H) TASK A */
  156. 0x046, 0x0cf, /* VBI horizontal input window stop (L) TASK A */
  157. 0x047, 0x002, /* VBI horizontal input window stop (H) TASK A */
  158. 0x049, 0x000, /* VBI vertical input window start (H) TASK A */
  159. 0x04c, 0x0d0, /* VBI horizontal output length (L) TASK A */
  160. 0x04d, 0x002, /* VBI horizontal output length (H) TASK A */
  161. 0x064, 0x080, /* Lumina brightness TASK A */
  162. 0x065, 0x040, /* Luminance contrast TASK A */
  163. 0x066, 0x040, /* Chroma saturation TASK A */
  164. /* 067H: Reserved */
  165. 0x068, 0x000, /* VBI horizontal scaling increment (L) TASK A */
  166. 0x069, 0x004, /* VBI horizontal scaling increment (H) TASK A */
  167. 0x06a, 0x000, /* VBI phase offset TASK A */
  168. 0x06e, 0x000, /* Horizontal phase offset Luma TASK A */
  169. 0x06f, 0x000, /* Horizontal phase offset Chroma TASK A */
  170. 0x072, 0x000, /* Vertical filter mode TASK A */
  171. 0x084, 0x000, /* VBI horizontal input window start (L) TAKS B */
  172. 0x085, 0x000, /* VBI horizontal input window start (H) TAKS B */
  173. 0x086, 0x0cf, /* VBI horizontal input window stop (L) TAKS B */
  174. 0x087, 0x002, /* VBI horizontal input window stop (H) TAKS B */
  175. 0x089, 0x000, /* VBI vertical input window start (H) TAKS B */
  176. 0x08c, 0x0d0, /* VBI horizontal output length (L) TASK B */
  177. 0x08d, 0x002, /* VBI horizontal output length (H) TASK B */
  178. 0x0a4, 0x080, /* Lumina brightness TASK B */
  179. 0x0a5, 0x040, /* Luminance contrast TASK B */
  180. 0x0a6, 0x040, /* Chroma saturation TASK B */
  181. /* 0A7H reserved */
  182. 0x0a8, 0x000, /* VBI horizontal scaling increment (L) TASK B */
  183. 0x0a9, 0x004, /* VBI horizontal scaling increment (H) TASK B */
  184. 0x0aa, 0x000, /* VBI phase offset TASK B */
  185. 0x0ae, 0x000, /* Horizontal phase offset Luma TASK B */
  186. 0x0af, 0x000, /*Horizontal phase offset Chroma TASK B */
  187. 0x0b2, 0x000, /* Vertical filter mode TASK B */
  188. 0x00c, 0x000, /* Start point GREEN path */
  189. 0x00d, 0x000, /* Start point BLUE path */
  190. 0x00e, 0x000, /* Start point RED path */
  191. 0x010, 0x010, /* GREEN path gamma curve --- */
  192. 0x011, 0x020,
  193. 0x012, 0x030,
  194. 0x013, 0x040,
  195. 0x014, 0x050,
  196. 0x015, 0x060,
  197. 0x016, 0x070,
  198. 0x017, 0x080,
  199. 0x018, 0x090,
  200. 0x019, 0x0a0,
  201. 0x01a, 0x0b0,
  202. 0x01b, 0x0c0,
  203. 0x01c, 0x0d0,
  204. 0x01d, 0x0e0,
  205. 0x01e, 0x0f0,
  206. 0x01f, 0x0ff, /* --- GREEN path gamma curve */
  207. 0x020, 0x010, /* BLUE path gamma curve --- */
  208. 0x021, 0x020,
  209. 0x022, 0x030,
  210. 0x023, 0x040,
  211. 0x024, 0x050,
  212. 0x025, 0x060,
  213. 0x026, 0x070,
  214. 0x027, 0x080,
  215. 0x028, 0x090,
  216. 0x029, 0x0a0,
  217. 0x02a, 0x0b0,
  218. 0x02b, 0x0c0,
  219. 0x02c, 0x0d0,
  220. 0x02d, 0x0e0,
  221. 0x02e, 0x0f0,
  222. 0x02f, 0x0ff, /* --- BLUE path gamma curve */
  223. 0x030, 0x010, /* RED path gamma curve --- */
  224. 0x031, 0x020,
  225. 0x032, 0x030,
  226. 0x033, 0x040,
  227. 0x034, 0x050,
  228. 0x035, 0x060,
  229. 0x036, 0x070,
  230. 0x037, 0x080,
  231. 0x038, 0x090,
  232. 0x039, 0x0a0,
  233. 0x03a, 0x0b0,
  234. 0x03b, 0x0c0,
  235. 0x03c, 0x0d0,
  236. 0x03d, 0x0e0,
  237. 0x03e, 0x0f0,
  238. 0x03f, 0x0ff, /* --- RED path gamma curve */
  239. 0x109, 0x085, /* Luminance control */
  240. /**** from app start ****/
  241. 0x584, 0x000, /* AGC gain control */
  242. 0x585, 0x000, /* Program count */
  243. 0x586, 0x003, /* Status reset */
  244. 0x588, 0x0ff, /* Number of audio samples (L) */
  245. 0x589, 0x00f, /* Number of audio samples (M) */
  246. 0x58a, 0x000, /* Number of audio samples (H) */
  247. 0x58b, 0x000, /* Audio select */
  248. 0x58c, 0x010, /* Audio channel assign1 */
  249. 0x58d, 0x032, /* Audio channel assign2 */
  250. 0x58e, 0x054, /* Audio channel assign3 */
  251. 0x58f, 0x023, /* Audio format */
  252. 0x590, 0x000, /* SIF control */
  253. 0x595, 0x000, /* ?? */
  254. 0x596, 0x000, /* ?? */
  255. 0x597, 0x000, /* ?? */
  256. 0x464, 0x00, /* Digital input crossbar1 */
  257. 0x46c, 0xbbbb10, /* Digital output selection1-3 */
  258. 0x470, 0x101010, /* Digital output selection4-6 */
  259. 0x478, 0x00, /* Sound feature control */
  260. 0x474, 0x18, /* Softmute control */
  261. 0x454, 0x0425b9, /* Sound Easy programming(reset) */
  262. 0x454, 0x042539, /* Sound Easy programming(reset) */
  263. /**** common setting( of DVD play, including scaler commands) ****/
  264. 0x042, 0x003, /* Data path configuration for VBI (TASK A) */
  265. 0x082, 0x003, /* Data path configuration for VBI (TASK B) */
  266. 0x108, 0x0f8, /* Sync control */
  267. 0x2a9, 0x0fd, /* ??? */
  268. 0x102, 0x089, /* select video input "mode 9" */
  269. 0x111, 0x000, /* Mode/delay control */
  270. 0x10e, 0x00a, /* Chroma control 1 */
  271. 0x594, 0x002, /* SIF, analog I/O select */
  272. 0x454, 0x0425b9, /* Sound */
  273. 0x454, 0x042539,
  274. 0x111, 0x000,
  275. 0x10e, 0x00a,
  276. 0x464, 0x000,
  277. 0x300, 0x000,
  278. 0x301, 0x006,
  279. 0x302, 0x000,
  280. 0x303, 0x006,
  281. 0x308, 0x040,
  282. 0x309, 0x000,
  283. 0x30a, 0x000,
  284. 0x30b, 0x000,
  285. 0x000, 0x002,
  286. 0x001, 0x000,
  287. 0x002, 0x000,
  288. 0x003, 0x000,
  289. 0x004, 0x033,
  290. 0x040, 0x01d,
  291. 0x041, 0x001,
  292. 0x042, 0x004,
  293. 0x043, 0x000,
  294. 0x080, 0x01e,
  295. 0x081, 0x001,
  296. 0x082, 0x004,
  297. 0x083, 0x000,
  298. 0x190, 0x018,
  299. 0x115, 0x000,
  300. 0x116, 0x012,
  301. 0x117, 0x018,
  302. 0x04a, 0x011,
  303. 0x08a, 0x011,
  304. 0x04b, 0x000,
  305. 0x08b, 0x000,
  306. 0x048, 0x000,
  307. 0x088, 0x000,
  308. 0x04e, 0x012,
  309. 0x08e, 0x012,
  310. 0x058, 0x012,
  311. 0x098, 0x012,
  312. 0x059, 0x000,
  313. 0x099, 0x000,
  314. 0x05a, 0x003,
  315. 0x09a, 0x003,
  316. 0x05b, 0x001,
  317. 0x09b, 0x001,
  318. 0x054, 0x008,
  319. 0x094, 0x008,
  320. 0x055, 0x000,
  321. 0x095, 0x000,
  322. 0x056, 0x0c7,
  323. 0x096, 0x0c7,
  324. 0x057, 0x002,
  325. 0x097, 0x002,
  326. 0x0ff, 0x0ff,
  327. 0x060, 0x001,
  328. 0x0a0, 0x001,
  329. 0x061, 0x000,
  330. 0x0a1, 0x000,
  331. 0x062, 0x000,
  332. 0x0a2, 0x000,
  333. 0x063, 0x000,
  334. 0x0a3, 0x000,
  335. 0x070, 0x000,
  336. 0x0b0, 0x000,
  337. 0x071, 0x004,
  338. 0x0b1, 0x004,
  339. 0x06c, 0x0e9,
  340. 0x0ac, 0x0e9,
  341. 0x06d, 0x003,
  342. 0x0ad, 0x003,
  343. 0x05c, 0x0d0,
  344. 0x09c, 0x0d0,
  345. 0x05d, 0x002,
  346. 0x09d, 0x002,
  347. 0x05e, 0x0f2,
  348. 0x09e, 0x0f2,
  349. 0x05f, 0x000,
  350. 0x09f, 0x000,
  351. 0x074, 0x000,
  352. 0x0b4, 0x000,
  353. 0x075, 0x000,
  354. 0x0b5, 0x000,
  355. 0x076, 0x000,
  356. 0x0b6, 0x000,
  357. 0x077, 0x000,
  358. 0x0b7, 0x000,
  359. 0x195, 0x008,
  360. 0x0ff, 0x0ff,
  361. 0x108, 0x0f8,
  362. 0x111, 0x000,
  363. 0x10e, 0x00a,
  364. 0x2a9, 0x0fd,
  365. 0x464, 0x001,
  366. 0x454, 0x042135,
  367. 0x598, 0x0e7,
  368. 0x599, 0x07d,
  369. 0x59a, 0x018,
  370. 0x59c, 0x066,
  371. 0x59d, 0x090,
  372. 0x59e, 0x001,
  373. 0x584, 0x000,
  374. 0x585, 0x000,
  375. 0x586, 0x003,
  376. 0x588, 0x0ff,
  377. 0x589, 0x00f,
  378. 0x58a, 0x000,
  379. 0x58b, 0x000,
  380. 0x58c, 0x010,
  381. 0x58d, 0x032,
  382. 0x58e, 0x054,
  383. 0x58f, 0x023,
  384. 0x590, 0x000,
  385. 0x595, 0x000,
  386. 0x596, 0x000,
  387. 0x597, 0x000,
  388. 0x464, 0x000,
  389. 0x46c, 0xbbbb10,
  390. 0x470, 0x101010,
  391. 0x478, 0x000,
  392. 0x474, 0x018,
  393. 0x454, 0x042135,
  394. 0x598, 0x0e7,
  395. 0x599, 0x07d,
  396. 0x59a, 0x018,
  397. 0x59c, 0x066,
  398. 0x59d, 0x090,
  399. 0x59e, 0x001,
  400. 0x584, 0x000,
  401. 0x585, 0x000,
  402. 0x586, 0x003,
  403. 0x588, 0x0ff,
  404. 0x589, 0x00f,
  405. 0x58a, 0x000,
  406. 0x58b, 0x000,
  407. 0x58c, 0x010,
  408. 0x58d, 0x032,
  409. 0x58e, 0x054,
  410. 0x58f, 0x023,
  411. 0x590, 0x000,
  412. 0x595, 0x000,
  413. 0x596, 0x000,
  414. 0x597, 0x000,
  415. 0x464, 0x000,
  416. 0x46c, 0xbbbb10,
  417. 0x470, 0x101010,
  418. 0x478, 0x000,
  419. 0x474, 0x018,
  420. 0x454, 0x042135,
  421. 0x598, 0x0e7,
  422. 0x599, 0x07d,
  423. 0x59a, 0x018,
  424. 0x59c, 0x066,
  425. 0x59d, 0x090,
  426. 0x59e, 0x001,
  427. 0x584, 0x000,
  428. 0x585, 0x000,
  429. 0x586, 0x003,
  430. 0x588, 0x0ff,
  431. 0x589, 0x00f,
  432. 0x58a, 0x000,
  433. 0x58b, 0x000,
  434. 0x58c, 0x010,
  435. 0x58d, 0x032,
  436. 0x58e, 0x054,
  437. 0x58f, 0x023,
  438. 0x590, 0x000,
  439. 0x595, 0x000,
  440. 0x596, 0x000,
  441. 0x597, 0x000,
  442. 0x464, 0x000,
  443. 0x46c, 0xbbbb10,
  444. 0x470, 0x101010,
  445. 0x478, 0x000,
  446. 0x474, 0x018,
  447. 0x454, 0x042135,
  448. 0x193, 0x000,
  449. 0x300, 0x000,
  450. 0x301, 0x006,
  451. 0x302, 0x000,
  452. 0x303, 0x006,
  453. 0x308, 0x040,
  454. 0x309, 0x000,
  455. 0x30a, 0x000,
  456. 0x30b, 0x000,
  457. 0x000, 0x002,
  458. 0x001, 0x000,
  459. 0x002, 0x000,
  460. 0x003, 0x000,
  461. 0x004, 0x033,
  462. 0x040, 0x01d,
  463. 0x041, 0x001,
  464. 0x042, 0x004,
  465. 0x043, 0x000,
  466. 0x080, 0x01e,
  467. 0x081, 0x001,
  468. 0x082, 0x004,
  469. 0x083, 0x000,
  470. 0x190, 0x018,
  471. 0x115, 0x000,
  472. 0x116, 0x012,
  473. 0x117, 0x018,
  474. 0x04a, 0x011,
  475. 0x08a, 0x011,
  476. 0x04b, 0x000,
  477. 0x08b, 0x000,
  478. 0x048, 0x000,
  479. 0x088, 0x000,
  480. 0x04e, 0x012,
  481. 0x08e, 0x012,
  482. 0x058, 0x012,
  483. 0x098, 0x012,
  484. 0x059, 0x000,
  485. 0x099, 0x000,
  486. 0x05a, 0x003,
  487. 0x09a, 0x003,
  488. 0x05b, 0x001,
  489. 0x09b, 0x001,
  490. 0x054, 0x008,
  491. 0x094, 0x008,
  492. 0x055, 0x000,
  493. 0x095, 0x000,
  494. 0x056, 0x0c7,
  495. 0x096, 0x0c7,
  496. 0x057, 0x002,
  497. 0x097, 0x002,
  498. 0x060, 0x001,
  499. 0x0a0, 0x001,
  500. 0x061, 0x000,
  501. 0x0a1, 0x000,
  502. 0x062, 0x000,
  503. 0x0a2, 0x000,
  504. 0x063, 0x000,
  505. 0x0a3, 0x000,
  506. 0x070, 0x000,
  507. 0x0b0, 0x000,
  508. 0x071, 0x004,
  509. 0x0b1, 0x004,
  510. 0x06c, 0x0e9,
  511. 0x0ac, 0x0e9,
  512. 0x06d, 0x003,
  513. 0x0ad, 0x003,
  514. 0x05c, 0x0d0,
  515. 0x09c, 0x0d0,
  516. 0x05d, 0x002,
  517. 0x09d, 0x002,
  518. 0x05e, 0x0f2,
  519. 0x09e, 0x0f2,
  520. 0x05f, 0x000,
  521. 0x09f, 0x000,
  522. 0x074, 0x000,
  523. 0x0b4, 0x000,
  524. 0x075, 0x000,
  525. 0x0b5, 0x000,
  526. 0x076, 0x000,
  527. 0x0b6, 0x000,
  528. 0x077, 0x000,
  529. 0x0b7, 0x000,
  530. 0x195, 0x008,
  531. 0x598, 0x0e7,
  532. 0x599, 0x07d,
  533. 0x59a, 0x018,
  534. 0x59c, 0x066,
  535. 0x59d, 0x090,
  536. 0x59e, 0x001,
  537. 0x584, 0x000,
  538. 0x585, 0x000,
  539. 0x586, 0x003,
  540. 0x588, 0x0ff,
  541. 0x589, 0x00f,
  542. 0x58a, 0x000,
  543. 0x58b, 0x000,
  544. 0x58c, 0x010,
  545. 0x58d, 0x032,
  546. 0x58e, 0x054,
  547. 0x58f, 0x023,
  548. 0x590, 0x000,
  549. 0x595, 0x000,
  550. 0x596, 0x000,
  551. 0x597, 0x000,
  552. 0x464, 0x000,
  553. 0x46c, 0xbbbb10,
  554. 0x470, 0x101010,
  555. 0x478, 0x000,
  556. 0x474, 0x018,
  557. 0x454, 0x042135,
  558. 0x193, 0x0a6,
  559. 0x108, 0x0f8,
  560. 0x042, 0x003,
  561. 0x082, 0x003,
  562. 0x454, 0x0425b9,
  563. 0x454, 0x042539,
  564. 0x193, 0x000,
  565. 0x193, 0x0a6,
  566. 0x464, 0x000,
  567. 0, 0
  568. };
  569. /* Tuner */
  570. static u32 reg_init_tuner_input[] = {
  571. 0x108, 0x0f8, /* Sync control */
  572. 0x111, 0x000, /* Mode/delay control */
  573. 0x10e, 0x00a, /* Chroma control 1 */
  574. 0, 0
  575. };
  576. /* Composite */
  577. static u32 reg_init_composite_input[] = {
  578. 0x108, 0x0e8, /* Sync control */
  579. 0x111, 0x000, /* Mode/delay control */
  580. 0x10e, 0x04a, /* Chroma control 1 */
  581. 0, 0
  582. };
  583. /* S-Video */
  584. static u32 reg_init_svideo_input[] = {
  585. 0x108, 0x0e8, /* Sync control */
  586. 0x111, 0x000, /* Mode/delay control */
  587. 0x10e, 0x04a, /* Chroma control 1 */
  588. 0, 0
  589. };
  590. static u32 reg_set_audio_template[4][2] =
  591. {
  592. { /* for MONO
  593. tadachi 6/29 DMA audio output select?
  594. Register 0x46c
  595. 7-4: DMA2, 3-0: DMA1 ch. DMA4, DMA3 DMA2, DMA1
  596. 0: MAIN left, 1: MAIN right
  597. 2: AUX1 left, 3: AUX1 right
  598. 4: AUX2 left, 5: AUX2 right
  599. 6: DPL left, 7: DPL right
  600. 8: DPL center, 9: DPL surround
  601. A: monitor output, B: digital sense */
  602. 0xbbbb00,
  603. /* tadachi 6/29 DAC and I2S output select?
  604. Register 0x470
  605. 7-4:DAC right ch. 3-0:DAC left ch.
  606. I2S1 right,left I2S2 right,left */
  607. 0x00,
  608. },
  609. { /* for STEREO */
  610. 0xbbbb10, 0x101010,
  611. },
  612. { /* for LANG1 */
  613. 0xbbbb00, 0x00,
  614. },
  615. { /* for LANG2/SAP */
  616. 0xbbbb11, 0x111111,
  617. }
  618. };
  619. /* Get detected audio flags (from saa7134 driver) */
  620. static void get_inf_dev_status(struct v4l2_subdev *sd,
  621. int *dual_flag, int *stereo_flag)
  622. {
  623. u32 reg_data3;
  624. static char *stdres[0x20] = {
  625. [0x00] = "no standard detected",
  626. [0x01] = "B/G (in progress)",
  627. [0x02] = "D/K (in progress)",
  628. [0x03] = "M (in progress)",
  629. [0x04] = "B/G A2",
  630. [0x05] = "B/G NICAM",
  631. [0x06] = "D/K A2 (1)",
  632. [0x07] = "D/K A2 (2)",
  633. [0x08] = "D/K A2 (3)",
  634. [0x09] = "D/K NICAM",
  635. [0x0a] = "L NICAM",
  636. [0x0b] = "I NICAM",
  637. [0x0c] = "M Korea",
  638. [0x0d] = "M BTSC ",
  639. [0x0e] = "M EIAJ",
  640. [0x0f] = "FM radio / IF 10.7 / 50 deemp",
  641. [0x10] = "FM radio / IF 10.7 / 75 deemp",
  642. [0x11] = "FM radio / IF sel / 50 deemp",
  643. [0x12] = "FM radio / IF sel / 75 deemp",
  644. [0x13 ... 0x1e] = "unknown",
  645. [0x1f] = "??? [in progress]",
  646. };
  647. *dual_flag = *stereo_flag = 0;
  648. /* (demdec status: 0x528) */
  649. /* read current status */
  650. reg_data3 = saa717x_read(sd, 0x0528);
  651. v4l2_dbg(1, debug, sd, "tvaudio thread status: 0x%x [%s%s%s]\n",
  652. reg_data3, stdres[reg_data3 & 0x1f],
  653. (reg_data3 & 0x000020) ? ",stereo" : "",
  654. (reg_data3 & 0x000040) ? ",dual" : "");
  655. v4l2_dbg(1, debug, sd, "detailed status: "
  656. "%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s#%s\n",
  657. (reg_data3 & 0x000080) ? " A2/EIAJ pilot tone " : "",
  658. (reg_data3 & 0x000100) ? " A2/EIAJ dual " : "",
  659. (reg_data3 & 0x000200) ? " A2/EIAJ stereo " : "",
  660. (reg_data3 & 0x000400) ? " A2/EIAJ noise mute " : "",
  661. (reg_data3 & 0x000800) ? " BTSC/FM radio pilot " : "",
  662. (reg_data3 & 0x001000) ? " SAP carrier " : "",
  663. (reg_data3 & 0x002000) ? " BTSC stereo noise mute " : "",
  664. (reg_data3 & 0x004000) ? " SAP noise mute " : "",
  665. (reg_data3 & 0x008000) ? " VDSP " : "",
  666. (reg_data3 & 0x010000) ? " NICST " : "",
  667. (reg_data3 & 0x020000) ? " NICDU " : "",
  668. (reg_data3 & 0x040000) ? " NICAM muted " : "",
  669. (reg_data3 & 0x080000) ? " NICAM reserve sound " : "",
  670. (reg_data3 & 0x100000) ? " init done " : "");
  671. if (reg_data3 & 0x000220) {
  672. v4l2_dbg(1, debug, sd, "ST!!!\n");
  673. *stereo_flag = 1;
  674. }
  675. if (reg_data3 & 0x000140) {
  676. v4l2_dbg(1, debug, sd, "DUAL!!!\n");
  677. *dual_flag = 1;
  678. }
  679. }
  680. /* regs write to set audio mode */
  681. static void set_audio_mode(struct v4l2_subdev *sd, int audio_mode)
  682. {
  683. v4l2_dbg(1, debug, sd, "writing registers to set audio mode by set %d\n",
  684. audio_mode);
  685. saa717x_write(sd, 0x46c, reg_set_audio_template[audio_mode][0]);
  686. saa717x_write(sd, 0x470, reg_set_audio_template[audio_mode][1]);
  687. }
  688. /* write regs to set audio volume, bass and treble */
  689. static int set_audio_regs(struct v4l2_subdev *sd,
  690. struct saa717x_state *decoder)
  691. {
  692. u8 mute = 0xac; /* -84 dB */
  693. u32 val;
  694. unsigned int work_l, work_r;
  695. /* set SIF analog I/O select */
  696. saa717x_write(sd, 0x0594, decoder->audio_input);
  697. v4l2_dbg(1, debug, sd, "set audio input %d\n",
  698. decoder->audio_input);
  699. /* normalize ( 65535 to 0 -> 24 to -40 (not -84)) */
  700. work_l = (min(65536 - decoder->audio_main_balance, 32768) * decoder->audio_main_volume) / 32768;
  701. work_r = (min(decoder->audio_main_balance, (u16)32768) * decoder->audio_main_volume) / 32768;
  702. decoder->audio_main_vol_l = (long)work_l * (24 - (-40)) / 65535 - 40;
  703. decoder->audio_main_vol_r = (long)work_r * (24 - (-40)) / 65535 - 40;
  704. /* set main volume */
  705. /* main volume L[7-0],R[7-0],0x00 24=24dB,-83dB, -84(mute) */
  706. /* def:0dB->6dB(MPG600GR) */
  707. /* if mute is on, set mute */
  708. if (decoder->audio_main_mute) {
  709. val = mute | (mute << 8);
  710. } else {
  711. val = (u8)decoder->audio_main_vol_l |
  712. ((u8)decoder->audio_main_vol_r << 8);
  713. }
  714. saa717x_write(sd, 0x480, val);
  715. /* set bass and treble */
  716. val = decoder->audio_main_bass & 0x1f;
  717. val |= (decoder->audio_main_treble & 0x1f) << 5;
  718. saa717x_write(sd, 0x488, val);
  719. return 0;
  720. }
  721. /********** scaling staff ***********/
  722. static void set_h_prescale(struct v4l2_subdev *sd,
  723. int task, int prescale)
  724. {
  725. static const struct {
  726. int xpsc;
  727. int xacl;
  728. int xc2_1;
  729. int xdcg;
  730. int vpfy;
  731. } vals[] = {
  732. /* XPSC XACL XC2_1 XDCG VPFY */
  733. { 1, 0, 0, 0, 0 },
  734. { 2, 2, 1, 2, 2 },
  735. { 3, 4, 1, 3, 2 },
  736. { 4, 8, 1, 4, 2 },
  737. { 5, 8, 1, 4, 2 },
  738. { 6, 8, 1, 4, 3 },
  739. { 7, 8, 1, 4, 3 },
  740. { 8, 15, 0, 4, 3 },
  741. { 9, 15, 0, 4, 3 },
  742. { 10, 16, 1, 5, 3 },
  743. };
  744. static const int count = ARRAY_SIZE(vals);
  745. int i, task_shift;
  746. task_shift = task * 0x40;
  747. for (i = 0; i < count; i++)
  748. if (vals[i].xpsc == prescale)
  749. break;
  750. if (i == count)
  751. return;
  752. /* horizonal prescaling */
  753. saa717x_write(sd, 0x60 + task_shift, vals[i].xpsc);
  754. /* accumulation length */
  755. saa717x_write(sd, 0x61 + task_shift, vals[i].xacl);
  756. /* level control */
  757. saa717x_write(sd, 0x62 + task_shift,
  758. (vals[i].xc2_1 << 3) | vals[i].xdcg);
  759. /*FIR prefilter control */
  760. saa717x_write(sd, 0x63 + task_shift,
  761. (vals[i].vpfy << 2) | vals[i].vpfy);
  762. }
  763. /********** scaling staff ***********/
  764. static void set_v_scale(struct v4l2_subdev *sd, int task, int yscale)
  765. {
  766. int task_shift;
  767. task_shift = task * 0x40;
  768. /* Vertical scaling ratio (LOW) */
  769. saa717x_write(sd, 0x70 + task_shift, yscale & 0xff);
  770. /* Vertical scaling ratio (HI) */
  771. saa717x_write(sd, 0x71 + task_shift, yscale >> 8);
  772. }
  773. static int saa717x_s_ctrl(struct v4l2_ctrl *ctrl)
  774. {
  775. struct v4l2_subdev *sd = to_sd(ctrl);
  776. struct saa717x_state *state = to_state(sd);
  777. switch (ctrl->id) {
  778. case V4L2_CID_BRIGHTNESS:
  779. saa717x_write(sd, 0x10a, ctrl->val);
  780. return 0;
  781. case V4L2_CID_CONTRAST:
  782. saa717x_write(sd, 0x10b, ctrl->val);
  783. return 0;
  784. case V4L2_CID_SATURATION:
  785. saa717x_write(sd, 0x10c, ctrl->val);
  786. return 0;
  787. case V4L2_CID_HUE:
  788. saa717x_write(sd, 0x10d, ctrl->val);
  789. return 0;
  790. case V4L2_CID_AUDIO_MUTE:
  791. state->audio_main_mute = ctrl->val;
  792. break;
  793. case V4L2_CID_AUDIO_VOLUME:
  794. state->audio_main_volume = ctrl->val;
  795. break;
  796. case V4L2_CID_AUDIO_BALANCE:
  797. state->audio_main_balance = ctrl->val;
  798. break;
  799. case V4L2_CID_AUDIO_TREBLE:
  800. state->audio_main_treble = ctrl->val;
  801. break;
  802. case V4L2_CID_AUDIO_BASS:
  803. state->audio_main_bass = ctrl->val;
  804. break;
  805. default:
  806. return 0;
  807. }
  808. set_audio_regs(sd, state);
  809. return 0;
  810. }
  811. static int saa717x_s_video_routing(struct v4l2_subdev *sd,
  812. u32 input, u32 output, u32 config)
  813. {
  814. struct saa717x_state *decoder = to_state(sd);
  815. int is_tuner = input & 0x80; /* tuner input flag */
  816. input &= 0x7f;
  817. v4l2_dbg(1, debug, sd, "decoder set input (%d)\n", input);
  818. /* inputs from 0-9 are available*/
  819. /* saa717x have mode0-mode9 but mode5 is reserved. */
  820. if (input > 9 || input == 5)
  821. return -EINVAL;
  822. if (decoder->input != input) {
  823. int input_line = input;
  824. decoder->input = input_line;
  825. v4l2_dbg(1, debug, sd, "now setting %s input %d\n",
  826. input_line >= 6 ? "S-Video" : "Composite",
  827. input_line);
  828. /* select mode */
  829. saa717x_write(sd, 0x102,
  830. (saa717x_read(sd, 0x102) & 0xf0) |
  831. input_line);
  832. /* bypass chrominance trap for modes 6..9 */
  833. saa717x_write(sd, 0x109,
  834. (saa717x_read(sd, 0x109) & 0x7f) |
  835. (input_line < 6 ? 0x0 : 0x80));
  836. /* change audio_mode */
  837. if (is_tuner) {
  838. /* tuner */
  839. set_audio_mode(sd, decoder->tuner_audio_mode);
  840. } else {
  841. /* Force to STEREO mode if Composite or
  842. * S-Video were chosen */
  843. set_audio_mode(sd, TUNER_AUDIO_STEREO);
  844. }
  845. /* change initialize procedure (Composite/S-Video) */
  846. if (is_tuner)
  847. saa717x_write_regs(sd, reg_init_tuner_input);
  848. else if (input_line >= 6)
  849. saa717x_write_regs(sd, reg_init_svideo_input);
  850. else
  851. saa717x_write_regs(sd, reg_init_composite_input);
  852. }
  853. return 0;
  854. }
  855. #ifdef CONFIG_VIDEO_ADV_DEBUG
  856. static int saa717x_g_register(struct v4l2_subdev *sd, struct v4l2_dbg_register *reg)
  857. {
  858. reg->val = saa717x_read(sd, reg->reg);
  859. reg->size = 1;
  860. return 0;
  861. }
  862. static int saa717x_s_register(struct v4l2_subdev *sd, const struct v4l2_dbg_register *reg)
  863. {
  864. u16 addr = reg->reg & 0xffff;
  865. u8 val = reg->val & 0xff;
  866. saa717x_write(sd, addr, val);
  867. return 0;
  868. }
  869. #endif
  870. static int saa717x_set_fmt(struct v4l2_subdev *sd,
  871. struct v4l2_subdev_pad_config *cfg,
  872. struct v4l2_subdev_format *format)
  873. {
  874. struct v4l2_mbus_framefmt *fmt = &format->format;
  875. int prescale, h_scale, v_scale;
  876. v4l2_dbg(1, debug, sd, "decoder set size\n");
  877. if (format->pad || fmt->code != MEDIA_BUS_FMT_FIXED)
  878. return -EINVAL;
  879. /* FIXME need better bounds checking here */
  880. if (fmt->width < 1 || fmt->width > 1440)
  881. return -EINVAL;
  882. if (fmt->height < 1 || fmt->height > 960)
  883. return -EINVAL;
  884. fmt->field = V4L2_FIELD_INTERLACED;
  885. fmt->colorspace = V4L2_COLORSPACE_SMPTE170M;
  886. if (format->which == V4L2_SUBDEV_FORMAT_TRY)
  887. return 0;
  888. /* scaling setting */
  889. /* NTSC and interlace only */
  890. prescale = SAA717X_NTSC_WIDTH / fmt->width;
  891. if (prescale == 0)
  892. prescale = 1;
  893. h_scale = 1024 * SAA717X_NTSC_WIDTH / prescale / fmt->width;
  894. /* interlace */
  895. v_scale = 512 * 2 * SAA717X_NTSC_HEIGHT / fmt->height;
  896. /* Horizontal prescaling etc */
  897. set_h_prescale(sd, 0, prescale);
  898. set_h_prescale(sd, 1, prescale);
  899. /* Horizontal scaling increment */
  900. /* TASK A */
  901. saa717x_write(sd, 0x6C, (u8)(h_scale & 0xFF));
  902. saa717x_write(sd, 0x6D, (u8)((h_scale >> 8) & 0xFF));
  903. /* TASK B */
  904. saa717x_write(sd, 0xAC, (u8)(h_scale & 0xFF));
  905. saa717x_write(sd, 0xAD, (u8)((h_scale >> 8) & 0xFF));
  906. /* Vertical prescaling etc */
  907. set_v_scale(sd, 0, v_scale);
  908. set_v_scale(sd, 1, v_scale);
  909. /* set video output size */
  910. /* video number of pixels at output */
  911. /* TASK A */
  912. saa717x_write(sd, 0x5C, (u8)(fmt->width & 0xFF));
  913. saa717x_write(sd, 0x5D, (u8)((fmt->width >> 8) & 0xFF));
  914. /* TASK B */
  915. saa717x_write(sd, 0x9C, (u8)(fmt->width & 0xFF));
  916. saa717x_write(sd, 0x9D, (u8)((fmt->width >> 8) & 0xFF));
  917. /* video number of lines at output */
  918. /* TASK A */
  919. saa717x_write(sd, 0x5E, (u8)(fmt->height & 0xFF));
  920. saa717x_write(sd, 0x5F, (u8)((fmt->height >> 8) & 0xFF));
  921. /* TASK B */
  922. saa717x_write(sd, 0x9E, (u8)(fmt->height & 0xFF));
  923. saa717x_write(sd, 0x9F, (u8)((fmt->height >> 8) & 0xFF));
  924. return 0;
  925. }
  926. static int saa717x_s_radio(struct v4l2_subdev *sd)
  927. {
  928. struct saa717x_state *decoder = to_state(sd);
  929. decoder->radio = 1;
  930. return 0;
  931. }
  932. static int saa717x_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  933. {
  934. struct saa717x_state *decoder = to_state(sd);
  935. v4l2_dbg(1, debug, sd, "decoder set norm ");
  936. v4l2_dbg(1, debug, sd, "(not yet implemented)\n");
  937. decoder->radio = 0;
  938. decoder->std = std;
  939. return 0;
  940. }
  941. static int saa717x_s_audio_routing(struct v4l2_subdev *sd,
  942. u32 input, u32 output, u32 config)
  943. {
  944. struct saa717x_state *decoder = to_state(sd);
  945. if (input < 3) { /* FIXME! --tadachi */
  946. decoder->audio_input = input;
  947. v4l2_dbg(1, debug, sd,
  948. "set decoder audio input to %d\n",
  949. decoder->audio_input);
  950. set_audio_regs(sd, decoder);
  951. return 0;
  952. }
  953. return -ERANGE;
  954. }
  955. static int saa717x_s_stream(struct v4l2_subdev *sd, int enable)
  956. {
  957. struct saa717x_state *decoder = to_state(sd);
  958. v4l2_dbg(1, debug, sd, "decoder %s output\n",
  959. enable ? "enable" : "disable");
  960. decoder->enable = enable;
  961. saa717x_write(sd, 0x193, enable ? 0xa6 : 0x26);
  962. return 0;
  963. }
  964. /* change audio mode */
  965. static int saa717x_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
  966. {
  967. struct saa717x_state *decoder = to_state(sd);
  968. int audio_mode;
  969. char *mes[4] = {
  970. "MONO", "STEREO", "LANG1", "LANG2/SAP"
  971. };
  972. audio_mode = TUNER_AUDIO_STEREO;
  973. switch (vt->audmode) {
  974. case V4L2_TUNER_MODE_MONO:
  975. audio_mode = TUNER_AUDIO_MONO;
  976. break;
  977. case V4L2_TUNER_MODE_STEREO:
  978. audio_mode = TUNER_AUDIO_STEREO;
  979. break;
  980. case V4L2_TUNER_MODE_LANG2:
  981. audio_mode = TUNER_AUDIO_LANG2;
  982. break;
  983. case V4L2_TUNER_MODE_LANG1:
  984. audio_mode = TUNER_AUDIO_LANG1;
  985. break;
  986. }
  987. v4l2_dbg(1, debug, sd, "change audio mode to %s\n",
  988. mes[audio_mode]);
  989. decoder->tuner_audio_mode = audio_mode;
  990. /* The registers are not changed here. */
  991. /* See DECODER_ENABLE_OUTPUT section. */
  992. set_audio_mode(sd, decoder->tuner_audio_mode);
  993. return 0;
  994. }
  995. static int saa717x_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  996. {
  997. struct saa717x_state *decoder = to_state(sd);
  998. int dual_f, stereo_f;
  999. if (decoder->radio)
  1000. return 0;
  1001. get_inf_dev_status(sd, &dual_f, &stereo_f);
  1002. v4l2_dbg(1, debug, sd, "DETECT==st:%d dual:%d\n",
  1003. stereo_f, dual_f);
  1004. /* mono */
  1005. if ((dual_f == 0) && (stereo_f == 0)) {
  1006. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  1007. v4l2_dbg(1, debug, sd, "DETECT==MONO\n");
  1008. }
  1009. /* stereo */
  1010. if (stereo_f == 1) {
  1011. if (vt->audmode == V4L2_TUNER_MODE_STEREO ||
  1012. vt->audmode == V4L2_TUNER_MODE_LANG1) {
  1013. vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
  1014. v4l2_dbg(1, debug, sd, "DETECT==ST(ST)\n");
  1015. } else {
  1016. vt->rxsubchans = V4L2_TUNER_SUB_MONO;
  1017. v4l2_dbg(1, debug, sd, "DETECT==ST(MONO)\n");
  1018. }
  1019. }
  1020. /* dual */
  1021. if (dual_f == 1) {
  1022. if (vt->audmode == V4L2_TUNER_MODE_LANG2) {
  1023. vt->rxsubchans = V4L2_TUNER_SUB_LANG2 | V4L2_TUNER_SUB_MONO;
  1024. v4l2_dbg(1, debug, sd, "DETECT==DUAL1\n");
  1025. } else {
  1026. vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_MONO;
  1027. v4l2_dbg(1, debug, sd, "DETECT==DUAL2\n");
  1028. }
  1029. }
  1030. return 0;
  1031. }
  1032. static int saa717x_log_status(struct v4l2_subdev *sd)
  1033. {
  1034. struct saa717x_state *state = to_state(sd);
  1035. v4l2_ctrl_handler_log_status(&state->hdl, sd->name);
  1036. return 0;
  1037. }
  1038. /* ----------------------------------------------------------------------- */
  1039. static const struct v4l2_ctrl_ops saa717x_ctrl_ops = {
  1040. .s_ctrl = saa717x_s_ctrl,
  1041. };
  1042. static const struct v4l2_subdev_core_ops saa717x_core_ops = {
  1043. #ifdef CONFIG_VIDEO_ADV_DEBUG
  1044. .g_register = saa717x_g_register,
  1045. .s_register = saa717x_s_register,
  1046. #endif
  1047. .log_status = saa717x_log_status,
  1048. };
  1049. static const struct v4l2_subdev_tuner_ops saa717x_tuner_ops = {
  1050. .g_tuner = saa717x_g_tuner,
  1051. .s_tuner = saa717x_s_tuner,
  1052. .s_radio = saa717x_s_radio,
  1053. };
  1054. static const struct v4l2_subdev_video_ops saa717x_video_ops = {
  1055. .s_std = saa717x_s_std,
  1056. .s_routing = saa717x_s_video_routing,
  1057. .s_stream = saa717x_s_stream,
  1058. };
  1059. static const struct v4l2_subdev_audio_ops saa717x_audio_ops = {
  1060. .s_routing = saa717x_s_audio_routing,
  1061. };
  1062. static const struct v4l2_subdev_pad_ops saa717x_pad_ops = {
  1063. .set_fmt = saa717x_set_fmt,
  1064. };
  1065. static const struct v4l2_subdev_ops saa717x_ops = {
  1066. .core = &saa717x_core_ops,
  1067. .tuner = &saa717x_tuner_ops,
  1068. .audio = &saa717x_audio_ops,
  1069. .video = &saa717x_video_ops,
  1070. .pad = &saa717x_pad_ops,
  1071. };
  1072. /* ----------------------------------------------------------------------- */
  1073. /* i2c implementation */
  1074. /* ----------------------------------------------------------------------- */
  1075. static int saa717x_probe(struct i2c_client *client,
  1076. const struct i2c_device_id *did)
  1077. {
  1078. struct saa717x_state *decoder;
  1079. struct v4l2_ctrl_handler *hdl;
  1080. struct v4l2_subdev *sd;
  1081. u8 id = 0;
  1082. char *p = "";
  1083. /* Check if the adapter supports the needed features */
  1084. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  1085. return -EIO;
  1086. decoder = devm_kzalloc(&client->dev, sizeof(*decoder), GFP_KERNEL);
  1087. if (decoder == NULL)
  1088. return -ENOMEM;
  1089. sd = &decoder->sd;
  1090. v4l2_i2c_subdev_init(sd, client, &saa717x_ops);
  1091. if (saa717x_write(sd, 0x5a4, 0xfe) &&
  1092. saa717x_write(sd, 0x5a5, 0x0f) &&
  1093. saa717x_write(sd, 0x5a6, 0x00) &&
  1094. saa717x_write(sd, 0x5a7, 0x01))
  1095. id = saa717x_read(sd, 0x5a0);
  1096. if (id != 0xc2 && id != 0x32 && id != 0xf2 && id != 0x6c) {
  1097. v4l2_dbg(1, debug, sd, "saa717x not found (id=%02x)\n", id);
  1098. return -ENODEV;
  1099. }
  1100. if (id == 0xc2)
  1101. p = "saa7173";
  1102. else if (id == 0x32)
  1103. p = "saa7174A";
  1104. else if (id == 0x6c)
  1105. p = "saa7174HL";
  1106. else
  1107. p = "saa7171";
  1108. v4l2_info(sd, "%s found @ 0x%x (%s)\n", p,
  1109. client->addr << 1, client->adapter->name);
  1110. hdl = &decoder->hdl;
  1111. v4l2_ctrl_handler_init(hdl, 9);
  1112. /* add in ascending ID order */
  1113. v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
  1114. V4L2_CID_BRIGHTNESS, 0, 255, 1, 128);
  1115. v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
  1116. V4L2_CID_CONTRAST, 0, 255, 1, 68);
  1117. v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
  1118. V4L2_CID_SATURATION, 0, 255, 1, 64);
  1119. v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
  1120. V4L2_CID_HUE, -128, 127, 1, 0);
  1121. v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
  1122. V4L2_CID_AUDIO_VOLUME, 0, 65535, 65535 / 100, 42000);
  1123. v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
  1124. V4L2_CID_AUDIO_BALANCE, 0, 65535, 65535 / 100, 32768);
  1125. v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
  1126. V4L2_CID_AUDIO_BASS, -16, 15, 1, 0);
  1127. v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
  1128. V4L2_CID_AUDIO_TREBLE, -16, 15, 1, 0);
  1129. v4l2_ctrl_new_std(hdl, &saa717x_ctrl_ops,
  1130. V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
  1131. sd->ctrl_handler = hdl;
  1132. if (hdl->error) {
  1133. int err = hdl->error;
  1134. v4l2_ctrl_handler_free(hdl);
  1135. return err;
  1136. }
  1137. decoder->std = V4L2_STD_NTSC;
  1138. decoder->input = -1;
  1139. decoder->enable = 1;
  1140. /* FIXME!! */
  1141. decoder->playback = 0; /* initially capture mode used */
  1142. decoder->audio = 1; /* DECODER_AUDIO_48_KHZ */
  1143. decoder->audio_input = 2; /* FIXME!! */
  1144. decoder->tuner_audio_mode = TUNER_AUDIO_STEREO;
  1145. /* set volume, bass and treble */
  1146. decoder->audio_main_vol_l = 6;
  1147. decoder->audio_main_vol_r = 6;
  1148. v4l2_dbg(1, debug, sd, "writing init values\n");
  1149. /* FIXME!! */
  1150. saa717x_write_regs(sd, reg_init_initialize);
  1151. v4l2_ctrl_handler_setup(hdl);
  1152. set_current_state(TASK_INTERRUPTIBLE);
  1153. schedule_timeout(2*HZ);
  1154. return 0;
  1155. }
  1156. static int saa717x_remove(struct i2c_client *client)
  1157. {
  1158. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1159. v4l2_device_unregister_subdev(sd);
  1160. v4l2_ctrl_handler_free(sd->ctrl_handler);
  1161. return 0;
  1162. }
  1163. /* ----------------------------------------------------------------------- */
  1164. static const struct i2c_device_id saa717x_id[] = {
  1165. { "saa717x", 0 },
  1166. { }
  1167. };
  1168. MODULE_DEVICE_TABLE(i2c, saa717x_id);
  1169. static struct i2c_driver saa717x_driver = {
  1170. .driver = {
  1171. .name = "saa717x",
  1172. },
  1173. .probe = saa717x_probe,
  1174. .remove = saa717x_remove,
  1175. .id_table = saa717x_id,
  1176. };
  1177. module_i2c_driver(saa717x_driver);