tda9887.c 19 KB

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