saa717x.c 36 KB

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