tda9887.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  1. #include <linux/module.h>
  2. #include <linux/moduleparam.h>
  3. #include <linux/kernel.h>
  4. #include <linux/i2c.h>
  5. #include <linux/types.h>
  6. #include <linux/videodev.h>
  7. #include <linux/init.h>
  8. #include <linux/errno.h>
  9. #include <linux/slab.h>
  10. #include <linux/delay.h>
  11. #include <media/audiochip.h>
  12. #include <media/tuner.h>
  13. #include <media/id.h>
  14. /* Chips:
  15. TDA9885 (PAL, NTSC)
  16. TDA9886 (PAL, SECAM, NTSC)
  17. TDA9887 (PAL, SECAM, NTSC, FM Radio)
  18. found on:
  19. - Pinnacle PCTV (Jul.2002 Version with MT2032, bttv)
  20. TDA9887 (world), TDA9885 (USA)
  21. Note: OP2 of tda988x must be set to 1, else MT2032 is disabled!
  22. - KNC One TV-Station RDS (saa7134)
  23. - Hauppauge PVR-150/500 (possibly more)
  24. */
  25. /* Addresses to scan */
  26. static unsigned short normal_i2c[] = {
  27. 0x84 >>1,
  28. 0x86 >>1,
  29. 0x96 >>1,
  30. I2C_CLIENT_END,
  31. };
  32. I2C_CLIENT_INSMOD;
  33. /* insmod options */
  34. static unsigned int debug = 0;
  35. module_param(debug, int, 0644);
  36. MODULE_LICENSE("GPL");
  37. /* ---------------------------------------------------------------------- */
  38. #define UNSET (-1U)
  39. #define PREFIX "tda9885/6/7: "
  40. #define dprintk if (debug) printk
  41. struct tda9887 {
  42. struct i2c_client client;
  43. v4l2_std_id std;
  44. enum tuner_mode mode;
  45. unsigned int config;
  46. unsigned int pinnacle_id;
  47. unsigned int using_v4l2;
  48. unsigned int radio_mode;
  49. };
  50. struct tvnorm {
  51. v4l2_std_id std;
  52. char *name;
  53. unsigned char b;
  54. unsigned char c;
  55. unsigned char e;
  56. };
  57. static struct i2c_driver driver;
  58. static struct i2c_client client_template;
  59. /* ---------------------------------------------------------------------- */
  60. //
  61. // TDA defines
  62. //
  63. //// first reg (b)
  64. #define cVideoTrapBypassOFF 0x00 // bit b0
  65. #define cVideoTrapBypassON 0x01 // bit b0
  66. #define cAutoMuteFmInactive 0x00 // bit b1
  67. #define cAutoMuteFmActive 0x02 // bit b1
  68. #define cIntercarrier 0x00 // bit b2
  69. #define cQSS 0x04 // bit b2
  70. #define cPositiveAmTV 0x00 // bit b3:4
  71. #define cFmRadio 0x08 // bit b3:4
  72. #define cNegativeFmTV 0x10 // bit b3:4
  73. #define cForcedMuteAudioON 0x20 // bit b5
  74. #define cForcedMuteAudioOFF 0x00 // bit b5
  75. #define cOutputPort1Active 0x00 // bit b6
  76. #define cOutputPort1Inactive 0x40 // bit b6
  77. #define cOutputPort2Active 0x00 // bit b7
  78. #define cOutputPort2Inactive 0x80 // bit b7
  79. //// second reg (c)
  80. #define cDeemphasisOFF 0x00 // bit c5
  81. #define cDeemphasisON 0x20 // bit c5
  82. #define cDeemphasis75 0x00 // bit c6
  83. #define cDeemphasis50 0x40 // bit c6
  84. #define cAudioGain0 0x00 // bit c7
  85. #define cAudioGain6 0x80 // bit c7
  86. //// third reg (e)
  87. #define cAudioIF_4_5 0x00 // bit e0:1
  88. #define cAudioIF_5_5 0x01 // bit e0:1
  89. #define cAudioIF_6_0 0x02 // bit e0:1
  90. #define cAudioIF_6_5 0x03 // bit e0:1
  91. #define cVideoIF_58_75 0x00 // bit e2:4
  92. #define cVideoIF_45_75 0x04 // bit e2:4
  93. #define cVideoIF_38_90 0x08 // bit e2:4
  94. #define cVideoIF_38_00 0x0C // bit e2:4
  95. #define cVideoIF_33_90 0x10 // bit e2:4
  96. #define cVideoIF_33_40 0x14 // bit e2:4
  97. #define cRadioIF_45_75 0x18 // bit e2:4
  98. #define cRadioIF_38_90 0x1C // bit e2:4
  99. #define cTunerGainNormal 0x00 // bit e5
  100. #define cTunerGainLow 0x20 // bit e5
  101. #define cGating_18 0x00 // bit e6
  102. #define cGating_36 0x40 // bit e6
  103. #define cAgcOutON 0x80 // bit e7
  104. #define cAgcOutOFF 0x00 // bit e7
  105. /* ---------------------------------------------------------------------- */
  106. static struct tvnorm tvnorms[] = {
  107. {
  108. .std = V4L2_STD_PAL_BG,
  109. .name = "PAL-BG",
  110. .b = ( cNegativeFmTV |
  111. cQSS ),
  112. .c = ( cDeemphasisON |
  113. cDeemphasis50 ),
  114. .e = ( cAudioIF_5_5 |
  115. cVideoIF_38_90 ),
  116. },{
  117. .std = V4L2_STD_PAL_I,
  118. .name = "PAL-I",
  119. .b = ( cNegativeFmTV |
  120. cQSS ),
  121. .c = ( cDeemphasisON |
  122. cDeemphasis50 ),
  123. .e = ( cAudioIF_6_0 |
  124. cVideoIF_38_90 ),
  125. },{
  126. .std = V4L2_STD_PAL_DK,
  127. .name = "PAL-DK",
  128. .b = ( cNegativeFmTV |
  129. cQSS ),
  130. .c = ( cDeemphasisON |
  131. cDeemphasis50 ),
  132. .e = ( cAudioIF_6_5 |
  133. cVideoIF_38_00 ),
  134. },{
  135. .std = V4L2_STD_PAL_M | V4L2_STD_PAL_N,
  136. .name = "PAL-M/N",
  137. .b = ( cNegativeFmTV |
  138. cQSS ),
  139. .c = ( cDeemphasisON |
  140. cDeemphasis75 ),
  141. .e = ( cAudioIF_4_5 |
  142. cVideoIF_45_75 ),
  143. },{
  144. .std = V4L2_STD_SECAM_L,
  145. .name = "SECAM-L",
  146. .b = ( cPositiveAmTV |
  147. cQSS ),
  148. .e = ( cGaiting_36 |
  149. cAudioIF_6_5 |
  150. cVideoIF_38_90 ),
  151. },{
  152. .std = V4L2_STD_SECAM_DK,
  153. .name = "SECAM-DK",
  154. .b = ( cNegativeFmTV |
  155. cQSS ),
  156. .c = ( cDeemphasisON |
  157. cDeemphasis50 ),
  158. .e = ( cAudioIF_6_5 |
  159. cVideoIF_38_00 ),
  160. },{
  161. .std = V4L2_STD_NTSC_M,
  162. .name = "NTSC-M",
  163. .b = ( cNegativeFmTV |
  164. cQSS ),
  165. .c = ( cDeemphasisON |
  166. cDeemphasis75 ),
  167. .e = ( cGating_36 |
  168. cAudioIF_4_5 |
  169. cVideoIF_45_75 ),
  170. },{
  171. .std = V4L2_STD_NTSC_M_JP,
  172. .name = "NTSC-JP",
  173. .b = ( cNegativeFmTV |
  174. cQSS ),
  175. .c = ( cDeemphasisON |
  176. cDeemphasis50 ),
  177. .e = ( cGating_36 |
  178. cAudioIF_4_5 |
  179. cVideoIF_58_75 ),
  180. }
  181. };
  182. static struct tvnorm radio_stereo = {
  183. .name = "Radio Stereo",
  184. .b = ( cFmRadio |
  185. cQSS ),
  186. .c = ( cDeemphasisOFF |
  187. cAudioGain6 ),
  188. .e = ( cAudioIF_5_5 |
  189. cRadioIF_38_90 ),
  190. };
  191. static struct tvnorm radio_mono = {
  192. .name = "Radio Mono",
  193. .b = ( cFmRadio |
  194. cQSS ),
  195. .c = ( cDeemphasisON |
  196. cDeemphasis50),
  197. .e = ( cAudioIF_5_5 |
  198. cRadioIF_38_90 ),
  199. };
  200. /* ---------------------------------------------------------------------- */
  201. static void dump_read_message(unsigned char *buf)
  202. {
  203. static char *afc[16] = {
  204. "- 12.5 kHz",
  205. "- 37.5 kHz",
  206. "- 62.5 kHz",
  207. "- 87.5 kHz",
  208. "-112.5 kHz",
  209. "-137.5 kHz",
  210. "-162.5 kHz",
  211. "-187.5 kHz [min]",
  212. "+187.5 kHz [max]",
  213. "+162.5 kHz",
  214. "+137.5 kHz",
  215. "+112.5 kHz",
  216. "+ 87.5 kHz",
  217. "+ 62.5 kHz",
  218. "+ 37.5 kHz",
  219. "+ 12.5 kHz",
  220. };
  221. printk(PREFIX "read: 0x%2x\n", buf[0]);
  222. printk(" after power on : %s\n", (buf[0] & 0x01) ? "yes" : "no");
  223. printk(" afc : %s\n", afc[(buf[0] >> 1) & 0x0f]);
  224. printk(" fmif level : %s\n", (buf[0] & 0x20) ? "high" : "low");
  225. printk(" afc window : %s\n", (buf[0] & 0x40) ? "in" : "out");
  226. printk(" vfi level : %s\n", (buf[0] & 0x80) ? "high" : "low");
  227. }
  228. static void dump_write_message(unsigned char *buf)
  229. {
  230. static char *sound[4] = {
  231. "AM/TV",
  232. "FM/radio",
  233. "FM/TV",
  234. "FM/radio"
  235. };
  236. static char *adjust[32] = {
  237. "-16", "-15", "-14", "-13", "-12", "-11", "-10", "-9",
  238. "-8", "-7", "-6", "-5", "-4", "-3", "-2", "-1",
  239. "0", "+1", "+2", "+3", "+4", "+5", "+6", "+7",
  240. "+8", "+9", "+10", "+11", "+12", "+13", "+14", "+15"
  241. };
  242. static char *deemph[4] = {
  243. "no", "no", "75", "50"
  244. };
  245. static char *carrier[4] = {
  246. "4.5 MHz",
  247. "5.5 MHz",
  248. "6.0 MHz",
  249. "6.5 MHz / AM"
  250. };
  251. static char *vif[8] = {
  252. "58.75 MHz",
  253. "45.75 MHz",
  254. "38.9 MHz",
  255. "38.0 MHz",
  256. "33.9 MHz",
  257. "33.4 MHz",
  258. "45.75 MHz + pin13",
  259. "38.9 MHz + pin13",
  260. };
  261. static char *rif[4] = {
  262. "44 MHz",
  263. "52 MHz",
  264. "52 MHz",
  265. "44 MHz",
  266. };
  267. printk(PREFIX "write: byte B 0x%02x\n",buf[1]);
  268. printk(" B0 video mode : %s\n",
  269. (buf[1] & 0x01) ? "video trap" : "sound trap");
  270. printk(" B1 auto mute fm : %s\n",
  271. (buf[1] & 0x02) ? "yes" : "no");
  272. printk(" B2 carrier mode : %s\n",
  273. (buf[1] & 0x04) ? "QSS" : "Intercarrier");
  274. printk(" B3-4 tv sound/radio : %s\n",
  275. sound[(buf[1] & 0x18) >> 3]);
  276. printk(" B5 force mute audio: %s\n",
  277. (buf[1] & 0x20) ? "yes" : "no");
  278. printk(" B6 output port 1 : %s\n",
  279. (buf[1] & 0x40) ? "high (inactive)" : "low (active)");
  280. printk(" B7 output port 2 : %s\n",
  281. (buf[1] & 0x80) ? "high (inactive)" : "low (active)");
  282. printk(PREFIX "write: byte C 0x%02x\n",buf[2]);
  283. printk(" C0-4 top adjustment : %s dB\n", adjust[buf[2] & 0x1f]);
  284. printk(" C5-6 de-emphasis : %s\n", deemph[(buf[2] & 0x60) >> 5]);
  285. printk(" C7 audio gain : %s\n",
  286. (buf[2] & 0x80) ? "-6" : "0");
  287. printk(PREFIX "write: byte E 0x%02x\n",buf[3]);
  288. printk(" E0-1 sound carrier : %s\n",
  289. carrier[(buf[3] & 0x03)]);
  290. printk(" E6 l pll gaiting : %s\n",
  291. (buf[3] & 0x40) ? "36" : "13");
  292. if (buf[1] & 0x08) {
  293. /* radio */
  294. printk(" E2-4 video if : %s\n",
  295. rif[(buf[3] & 0x0c) >> 2]);
  296. printk(" E7 vif agc output : %s\n",
  297. (buf[3] & 0x80)
  298. ? ((buf[3] & 0x10) ? "fm-agc radio" : "sif-agc radio")
  299. : "fm radio carrier afc");
  300. } else {
  301. /* video */
  302. printk(" E2-4 video if : %s\n",
  303. vif[(buf[3] & 0x1c) >> 2]);
  304. printk(" E5 tuner gain : %s\n",
  305. (buf[3] & 0x80)
  306. ? ((buf[3] & 0x20) ? "external" : "normal")
  307. : ((buf[3] & 0x20) ? "minimum" : "normal"));
  308. printk(" E7 vif agc output : %s\n",
  309. (buf[3] & 0x80)
  310. ? ((buf[3] & 0x20)
  311. ? "pin3 port, pin22 vif agc out"
  312. : "pin22 port, pin3 vif acg ext in")
  313. : "pin3+pin22 port");
  314. }
  315. printk("--\n");
  316. }
  317. /* ---------------------------------------------------------------------- */
  318. static int tda9887_set_tvnorm(struct tda9887 *t, char *buf)
  319. {
  320. struct tvnorm *norm = NULL;
  321. int i;
  322. if (t->mode == T_RADIO) {
  323. if (t->radio_mode == V4L2_TUNER_MODE_MONO)
  324. norm = &radio_mono;
  325. else
  326. norm = &radio_stereo;
  327. } else {
  328. for (i = 0; i < ARRAY_SIZE(tvnorms); i++) {
  329. if (tvnorms[i].std & t->std) {
  330. norm = tvnorms+i;
  331. break;
  332. }
  333. }
  334. }
  335. if (NULL == norm) {
  336. dprintk(PREFIX "Unsupported tvnorm entry - audio muted\n");
  337. return -1;
  338. }
  339. dprintk(PREFIX "configure for: %s\n",norm->name);
  340. buf[1] = norm->b;
  341. buf[2] = norm->c;
  342. buf[3] = norm->e;
  343. return 0;
  344. }
  345. static unsigned int port1 = UNSET;
  346. static unsigned int port2 = UNSET;
  347. static unsigned int qss = UNSET;
  348. static unsigned int adjust = 0x10;
  349. module_param(port1, int, 0644);
  350. module_param(port2, int, 0644);
  351. module_param(qss, int, 0644);
  352. module_param(adjust, int, 0644);
  353. static int tda9887_set_insmod(struct tda9887 *t, char *buf)
  354. {
  355. if (UNSET != port1) {
  356. if (port1)
  357. buf[1] |= cOutputPort1Inactive;
  358. else
  359. buf[1] &= ~cOutputPort1Inactive;
  360. }
  361. if (UNSET != port2) {
  362. if (port2)
  363. buf[1] |= cOutputPort2Inactive;
  364. else
  365. buf[1] &= ~cOutputPort2Inactive;
  366. }
  367. if (UNSET != qss) {
  368. if (qss)
  369. buf[1] |= cQSS;
  370. else
  371. buf[1] &= ~cQSS;
  372. }
  373. if (adjust >= 0x00 && adjust < 0x20)
  374. buf[2] |= adjust;
  375. return 0;
  376. }
  377. static int tda9887_set_config(struct tda9887 *t, char *buf)
  378. {
  379. if (t->config & TDA9887_PORT1_ACTIVE)
  380. buf[1] &= ~cOutputPort1Inactive;
  381. if (t->config & TDA9887_PORT1_INACTIVE)
  382. buf[1] |= cOutputPort1Inactive;
  383. if (t->config & TDA9887_PORT2_ACTIVE)
  384. buf[1] &= ~cOutputPort2Inactive;
  385. if (t->config & TDA9887_PORT2_INACTIVE)
  386. buf[1] |= cOutputPort2Inactive;
  387. if (t->config & TDA9887_QSS)
  388. buf[1] |= cQSS;
  389. if (t->config & TDA9887_INTERCARRIER)
  390. buf[1] &= ~cQSS;
  391. if (t->config & TDA9887_AUTOMUTE)
  392. buf[1] |= cAutoMuteFmActive;
  393. if (t->config & TDA9887_DEEMPHASIS_MASK) {
  394. buf[2] &= ~0x60;
  395. switch (t->config & TDA9887_DEEMPHASIS_MASK) {
  396. case TDA9887_DEEMPHASIS_NONE:
  397. buf[2] |= cDeemphasisOFF;
  398. break;
  399. case TDA9887_DEEMPHASIS_50:
  400. buf[2] |= cDeemphasisON | cDeemphasis50;
  401. break;
  402. case TDA9887_DEEMPHASIS_75:
  403. buf[2] |= cDeemphasisON | cDeemphasis75;
  404. break;
  405. }
  406. }
  407. return 0;
  408. }
  409. /* ---------------------------------------------------------------------- */
  410. static int tda9887_set_pinnacle(struct tda9887 *t, char *buf)
  411. {
  412. unsigned int bCarrierMode = UNSET;
  413. if (t->std & V4L2_STD_625_50) {
  414. if ((1 == t->pinnacle_id) || (7 == t->pinnacle_id)) {
  415. bCarrierMode = cIntercarrier;
  416. } else {
  417. bCarrierMode = cQSS;
  418. }
  419. }
  420. if (t->std & V4L2_STD_525_60) {
  421. if ((5 == t->pinnacle_id) || (6 == t->pinnacle_id)) {
  422. bCarrierMode = cIntercarrier;
  423. } else {
  424. bCarrierMode = cQSS;
  425. }
  426. }
  427. if (bCarrierMode != UNSET) {
  428. buf[1] &= ~0x04;
  429. buf[1] |= bCarrierMode;
  430. }
  431. return 0;
  432. }
  433. /* ---------------------------------------------------------------------- */
  434. static char pal[] = "-";
  435. module_param_string(pal, pal, sizeof(pal), 0644);
  436. static char secam[] = "-";
  437. module_param_string(secam, secam, sizeof(secam), 0644);
  438. static int tda9887_fixup_std(struct tda9887 *t)
  439. {
  440. /* get more precise norm info from insmod option */
  441. if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
  442. switch (pal[0]) {
  443. case 'b':
  444. case 'B':
  445. case 'g':
  446. case 'G':
  447. dprintk(PREFIX "insmod fixup: PAL => PAL-BG\n");
  448. t->std = V4L2_STD_PAL_BG;
  449. break;
  450. case 'i':
  451. case 'I':
  452. dprintk(PREFIX "insmod fixup: PAL => PAL-I\n");
  453. t->std = V4L2_STD_PAL_I;
  454. break;
  455. case 'd':
  456. case 'D':
  457. case 'k':
  458. case 'K':
  459. dprintk(PREFIX "insmod fixup: PAL => PAL-DK\n");
  460. t->std = V4L2_STD_PAL_DK;
  461. break;
  462. case '-':
  463. /* default parameter, do nothing */
  464. break;
  465. default:
  466. printk(PREFIX "pal= argument not recognised\n");
  467. break;
  468. }
  469. }
  470. if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
  471. switch (secam[0]) {
  472. case 'd':
  473. case 'D':
  474. case 'k':
  475. case 'K':
  476. dprintk(PREFIX "insmod fixup: SECAM => SECAM-DK\n");
  477. t->std = V4L2_STD_SECAM_DK;
  478. break;
  479. case 'l':
  480. case 'L':
  481. dprintk(PREFIX "insmod fixup: SECAM => SECAM-L\n");
  482. t->std = V4L2_STD_SECAM_L;
  483. break;
  484. case '-':
  485. /* default parameter, do nothing */
  486. break;
  487. default:
  488. printk(PREFIX "secam= argument not recognised\n");
  489. break;
  490. }
  491. }
  492. return 0;
  493. }
  494. static int tda9887_status(struct tda9887 *t)
  495. {
  496. unsigned char buf[1];
  497. int rc;
  498. memset(buf,0,sizeof(buf));
  499. if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
  500. printk(PREFIX "i2c i/o error: rc == %d (should be 1)\n",rc);
  501. dump_read_message(buf);
  502. return 0;
  503. }
  504. static int tda9887_configure(struct tda9887 *t)
  505. {
  506. unsigned char buf[4];
  507. int rc;
  508. memset(buf,0,sizeof(buf));
  509. tda9887_set_tvnorm(t,buf);
  510. buf[1] |= cOutputPort1Inactive;
  511. buf[1] |= cOutputPort2Inactive;
  512. if (UNSET != t->pinnacle_id) {
  513. tda9887_set_pinnacle(t,buf);
  514. }
  515. tda9887_set_config(t,buf);
  516. tda9887_set_insmod(t,buf);
  517. if (t->mode == T_STANDBY) {
  518. buf[1] |= cForcedMuteAudioON;
  519. }
  520. dprintk(PREFIX "writing: b=0x%02x c=0x%02x e=0x%02x\n",
  521. buf[1],buf[2],buf[3]);
  522. if (debug > 1)
  523. dump_write_message(buf);
  524. if (4 != (rc = i2c_master_send(&t->client,buf,4)))
  525. printk(PREFIX "i2c i/o error: rc == %d (should be 4)\n",rc);
  526. if (debug > 2) {
  527. msleep_interruptible(1000);
  528. tda9887_status(t);
  529. }
  530. return 0;
  531. }
  532. /* ---------------------------------------------------------------------- */
  533. static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
  534. {
  535. struct tda9887 *t;
  536. client_template.adapter = adap;
  537. client_template.addr = addr;
  538. printk(PREFIX "chip found @ 0x%x\n", addr<<1);
  539. if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
  540. return -ENOMEM;
  541. memset(t,0,sizeof(*t));
  542. t->client = client_template;
  543. t->std = 0;
  544. t->pinnacle_id = UNSET;
  545. t->radio_mode = V4L2_TUNER_MODE_STEREO;
  546. i2c_set_clientdata(&t->client, t);
  547. i2c_attach_client(&t->client);
  548. return 0;
  549. }
  550. static int tda9887_probe(struct i2c_adapter *adap)
  551. {
  552. #ifdef I2C_CLASS_TV_ANALOG
  553. if (adap->class & I2C_CLASS_TV_ANALOG)
  554. return i2c_probe(adap, &addr_data, tda9887_attach);
  555. #else
  556. switch (adap->id) {
  557. case I2C_HW_B_BT848:
  558. case I2C_HW_B_RIVA:
  559. case I2C_HW_SAA7134:
  560. return i2c_probe(adap, &addr_data, tda9887_attach);
  561. break;
  562. }
  563. #endif
  564. return 0;
  565. }
  566. static int tda9887_detach(struct i2c_client *client)
  567. {
  568. struct tda9887 *t = i2c_get_clientdata(client);
  569. i2c_detach_client(client);
  570. kfree(t);
  571. return 0;
  572. }
  573. #define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
  574. printk(PREFIX "switching to v4l2\n"); \
  575. t->using_v4l2 = 1;
  576. #define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
  577. printk(PREFIX "ignore v4l1 call\n"); \
  578. return 0; }
  579. static int
  580. tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
  581. {
  582. struct tda9887 *t = i2c_get_clientdata(client);
  583. switch (cmd) {
  584. /* --- configuration --- */
  585. case AUDC_SET_RADIO:
  586. {
  587. t->mode = T_RADIO;
  588. tda9887_configure(t);
  589. break;
  590. }
  591. case TUNER_SET_STANDBY:
  592. {
  593. t->mode = T_STANDBY;
  594. tda9887_configure(t);
  595. break;
  596. }
  597. case AUDC_CONFIG_PINNACLE:
  598. {
  599. int *i = arg;
  600. t->pinnacle_id = *i;
  601. tda9887_configure(t);
  602. break;
  603. }
  604. case TDA9887_SET_CONFIG:
  605. {
  606. int *i = arg;
  607. t->config = *i;
  608. tda9887_configure(t);
  609. break;
  610. }
  611. /* --- v4l ioctls --- */
  612. /* take care: bttv does userspace copying, we'll get a
  613. kernel pointer here... */
  614. case VIDIOCSCHAN:
  615. {
  616. static const v4l2_std_id map[] = {
  617. [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
  618. [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
  619. [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
  620. [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
  621. [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
  622. [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
  623. };
  624. struct video_channel *vc = arg;
  625. CHECK_V4L2;
  626. t->mode = T_ANALOG_TV;
  627. if (vc->norm < ARRAY_SIZE(map))
  628. t->std = map[vc->norm];
  629. tda9887_fixup_std(t);
  630. tda9887_configure(t);
  631. break;
  632. }
  633. case VIDIOC_S_STD:
  634. {
  635. v4l2_std_id *id = arg;
  636. SWITCH_V4L2;
  637. t->mode = T_ANALOG_TV;
  638. t->std = *id;
  639. tda9887_fixup_std(t);
  640. tda9887_configure(t);
  641. break;
  642. }
  643. case VIDIOC_S_FREQUENCY:
  644. {
  645. struct v4l2_frequency *f = arg;
  646. SWITCH_V4L2;
  647. if (V4L2_TUNER_ANALOG_TV == f->type) {
  648. if (t->mode == T_ANALOG_TV)
  649. return 0;
  650. t->mode = T_ANALOG_TV;
  651. }
  652. if (V4L2_TUNER_RADIO == f->type) {
  653. if (t->mode == T_RADIO)
  654. return 0;
  655. t->mode = T_RADIO;
  656. }
  657. tda9887_configure(t);
  658. break;
  659. }
  660. case VIDIOC_G_TUNER:
  661. {
  662. static int AFC_BITS_2_kHz[] = {
  663. -12500, -37500, -62500, -97500,
  664. -112500, -137500, -162500, -187500,
  665. 187500, 162500, 137500, 112500,
  666. 97500 , 62500, 37500 , 12500
  667. };
  668. struct v4l2_tuner* tuner = arg;
  669. if (t->mode == T_RADIO) {
  670. __u8 reg = 0;
  671. tuner->afc=0;
  672. if (1 == i2c_master_recv(&t->client,&reg,1))
  673. tuner->afc = AFC_BITS_2_kHz[(reg>>1)&0x0f];
  674. }
  675. break;
  676. }
  677. case VIDIOC_S_TUNER:
  678. {
  679. struct v4l2_tuner* tuner = arg;
  680. if (t->mode == T_RADIO) {
  681. t->radio_mode = tuner->audmode;
  682. tda9887_configure (t);
  683. }
  684. break;
  685. }
  686. default:
  687. /* nothing */
  688. break;
  689. }
  690. return 0;
  691. }
  692. static int tda9887_suspend(struct device * dev, pm_message_t state)
  693. {
  694. dprintk("tda9887: suspend\n");
  695. return 0;
  696. }
  697. static int tda9887_resume(struct device * dev)
  698. {
  699. struct i2c_client *c = container_of(dev, struct i2c_client, dev);
  700. struct tda9887 *t = i2c_get_clientdata(c);
  701. dprintk("tda9887: resume\n");
  702. tda9887_configure(t);
  703. return 0;
  704. }
  705. /* ----------------------------------------------------------------------- */
  706. static struct i2c_driver driver = {
  707. .owner = THIS_MODULE,
  708. .name = "i2c tda9887 driver",
  709. .id = -1, /* FIXME */
  710. .flags = I2C_DF_NOTIFY,
  711. .attach_adapter = tda9887_probe,
  712. .detach_client = tda9887_detach,
  713. .command = tda9887_command,
  714. .driver = {
  715. .suspend = tda9887_suspend,
  716. .resume = tda9887_resume,
  717. },
  718. };
  719. static struct i2c_client client_template =
  720. {
  721. .name = "tda9887",
  722. .flags = I2C_CLIENT_ALLOW_USE,
  723. .driver = &driver,
  724. };
  725. static int __init tda9887_init_module(void)
  726. {
  727. return i2c_add_driver(&driver);
  728. }
  729. static void __exit tda9887_cleanup_module(void)
  730. {
  731. i2c_del_driver(&driver);
  732. }
  733. module_init(tda9887_init_module);
  734. module_exit(tda9887_cleanup_module);
  735. /*
  736. * Overrides for Emacs so that we follow Linus's tabbing style.
  737. * ---------------------------------------------------------------------------
  738. * Local variables:
  739. * c-basic-offset: 8
  740. * End:
  741. */