tda9887.c 19 KB

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