saa717x.c 36 KB

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