tda9887.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  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 = ( 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. cDeemphasis75 ),
  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->mode == 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 "Unsupported tvnorm entry - audio muted\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. case '-':
  462. /* default parameter, do nothing */
  463. break;
  464. default:
  465. printk(PREFIX "pal= argument not recognised\n");
  466. break;
  467. }
  468. }
  469. if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
  470. switch (secam[0]) {
  471. case 'd':
  472. case 'D':
  473. case 'k':
  474. case 'K':
  475. dprintk(PREFIX "insmod fixup: SECAM => SECAM-DK\n");
  476. t->std = V4L2_STD_SECAM_DK;
  477. break;
  478. case 'l':
  479. case 'L':
  480. dprintk(PREFIX "insmod fixup: SECAM => SECAM-L\n");
  481. t->std = V4L2_STD_SECAM_L;
  482. break;
  483. case '-':
  484. /* default parameter, do nothing */
  485. break;
  486. default:
  487. printk(PREFIX "secam= argument not recognised\n");
  488. break;
  489. }
  490. }
  491. return 0;
  492. }
  493. static int tda9887_status(struct tda9887 *t)
  494. {
  495. unsigned char buf[1];
  496. int rc;
  497. memset(buf,0,sizeof(buf));
  498. if (1 != (rc = i2c_master_recv(&t->client,buf,1)))
  499. printk(PREFIX "i2c i/o error: rc == %d (should be 1)\n",rc);
  500. dump_read_message(buf);
  501. return 0;
  502. }
  503. static int tda9887_configure(struct tda9887 *t)
  504. {
  505. unsigned char buf[4];
  506. int rc;
  507. memset(buf,0,sizeof(buf));
  508. tda9887_set_tvnorm(t,buf);
  509. buf[1] |= cOutputPort1Inactive;
  510. buf[1] |= cOutputPort2Inactive;
  511. if (UNSET != t->pinnacle_id) {
  512. tda9887_set_pinnacle(t,buf);
  513. }
  514. tda9887_set_config(t,buf);
  515. tda9887_set_insmod(t,buf);
  516. if (t->mode == T_STANDBY) {
  517. buf[1] |= cForcedMuteAudioON;
  518. }
  519. dprintk(PREFIX "writing: b=0x%02x c=0x%02x e=0x%02x\n",
  520. buf[1],buf[2],buf[3]);
  521. if (debug > 1)
  522. dump_write_message(buf);
  523. if (4 != (rc = i2c_master_send(&t->client,buf,4)))
  524. printk(PREFIX "i2c i/o error: rc == %d (should be 4)\n",rc);
  525. if (debug > 2) {
  526. msleep_interruptible(1000);
  527. tda9887_status(t);
  528. }
  529. return 0;
  530. }
  531. /* ---------------------------------------------------------------------- */
  532. static int tda9887_attach(struct i2c_adapter *adap, int addr, int kind)
  533. {
  534. struct tda9887 *t;
  535. client_template.adapter = adap;
  536. client_template.addr = addr;
  537. printk(PREFIX "chip found @ 0x%x\n", addr<<1);
  538. if (NULL == (t = kmalloc(sizeof(*t), GFP_KERNEL)))
  539. return -ENOMEM;
  540. memset(t,0,sizeof(*t));
  541. t->client = client_template;
  542. t->std = 0;
  543. t->pinnacle_id = UNSET;
  544. t->radio_mode = V4L2_TUNER_MODE_STEREO;
  545. i2c_set_clientdata(&t->client, t);
  546. i2c_attach_client(&t->client);
  547. return 0;
  548. }
  549. static int tda9887_probe(struct i2c_adapter *adap)
  550. {
  551. #ifdef I2C_CLASS_TV_ANALOG
  552. if (adap->class & I2C_CLASS_TV_ANALOG)
  553. return i2c_probe(adap, &addr_data, tda9887_attach);
  554. #else
  555. switch (adap->id) {
  556. case I2C_HW_B_BT848:
  557. case I2C_HW_B_RIVA:
  558. case I2C_HW_SAA7134:
  559. return i2c_probe(adap, &addr_data, tda9887_attach);
  560. break;
  561. }
  562. #endif
  563. return 0;
  564. }
  565. static int tda9887_detach(struct i2c_client *client)
  566. {
  567. struct tda9887 *t = i2c_get_clientdata(client);
  568. i2c_detach_client(client);
  569. kfree(t);
  570. return 0;
  571. }
  572. #define SWITCH_V4L2 if (!t->using_v4l2 && debug) \
  573. printk(PREFIX "switching to v4l2\n"); \
  574. t->using_v4l2 = 1;
  575. #define CHECK_V4L2 if (t->using_v4l2) { if (debug) \
  576. printk(PREFIX "ignore v4l1 call\n"); \
  577. return 0; }
  578. static int
  579. tda9887_command(struct i2c_client *client, unsigned int cmd, void *arg)
  580. {
  581. struct tda9887 *t = i2c_get_clientdata(client);
  582. switch (cmd) {
  583. /* --- configuration --- */
  584. case AUDC_SET_RADIO:
  585. {
  586. t->mode = T_RADIO;
  587. tda9887_configure(t);
  588. break;
  589. }
  590. case TUNER_SET_STANDBY:
  591. {
  592. t->mode = T_STANDBY;
  593. tda9887_configure(t);
  594. break;
  595. }
  596. case AUDC_CONFIG_PINNACLE:
  597. {
  598. int *i = arg;
  599. t->pinnacle_id = *i;
  600. tda9887_configure(t);
  601. break;
  602. }
  603. case TDA9887_SET_CONFIG:
  604. {
  605. int *i = arg;
  606. t->config = *i;
  607. tda9887_configure(t);
  608. break;
  609. }
  610. /* --- v4l ioctls --- */
  611. /* take care: bttv does userspace copying, we'll get a
  612. kernel pointer here... */
  613. case VIDIOCSCHAN:
  614. {
  615. static const v4l2_std_id map[] = {
  616. [ VIDEO_MODE_PAL ] = V4L2_STD_PAL,
  617. [ VIDEO_MODE_NTSC ] = V4L2_STD_NTSC_M,
  618. [ VIDEO_MODE_SECAM ] = V4L2_STD_SECAM,
  619. [ 4 /* bttv */ ] = V4L2_STD_PAL_M,
  620. [ 5 /* bttv */ ] = V4L2_STD_PAL_N,
  621. [ 6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
  622. };
  623. struct video_channel *vc = arg;
  624. CHECK_V4L2;
  625. t->mode = T_ANALOG_TV;
  626. if (vc->norm < ARRAY_SIZE(map))
  627. t->std = map[vc->norm];
  628. tda9887_fixup_std(t);
  629. tda9887_configure(t);
  630. break;
  631. }
  632. case VIDIOC_S_STD:
  633. {
  634. v4l2_std_id *id = arg;
  635. SWITCH_V4L2;
  636. t->mode = T_ANALOG_TV;
  637. t->std = *id;
  638. tda9887_fixup_std(t);
  639. tda9887_configure(t);
  640. break;
  641. }
  642. case VIDIOC_S_FREQUENCY:
  643. {
  644. struct v4l2_frequency *f = arg;
  645. SWITCH_V4L2;
  646. if (V4L2_TUNER_ANALOG_TV == f->type) {
  647. if (t->mode == T_ANALOG_TV)
  648. return 0;
  649. t->mode = T_ANALOG_TV;
  650. }
  651. if (V4L2_TUNER_RADIO == f->type) {
  652. if (t->mode == T_RADIO)
  653. return 0;
  654. t->mode = T_RADIO;
  655. }
  656. tda9887_configure(t);
  657. break;
  658. }
  659. case VIDIOC_G_TUNER:
  660. {
  661. static int AFC_BITS_2_kHz[] = {
  662. -12500, -37500, -62500, -97500,
  663. -112500, -137500, -162500, -187500,
  664. 187500, 162500, 137500, 112500,
  665. 97500 , 62500, 37500 , 12500
  666. };
  667. struct v4l2_tuner* tuner = arg;
  668. if (t->mode == T_RADIO) {
  669. __u8 reg = 0;
  670. tuner->afc=0;
  671. if (1 == i2c_master_recv(&t->client,&reg,1))
  672. tuner->afc = AFC_BITS_2_kHz[(reg>>1)&0x0f];
  673. }
  674. break;
  675. }
  676. case VIDIOC_S_TUNER:
  677. {
  678. struct v4l2_tuner* tuner = arg;
  679. if (t->mode == T_RADIO) {
  680. t->radio_mode = tuner->audmode;
  681. tda9887_configure (t);
  682. }
  683. break;
  684. }
  685. default:
  686. /* nothing */
  687. break;
  688. }
  689. return 0;
  690. }
  691. static int tda9887_suspend(struct device * dev, pm_message_t state, u32 level)
  692. {
  693. dprintk("tda9887: suspend\n");
  694. return 0;
  695. }
  696. static int tda9887_resume(struct device * dev, u32 level)
  697. {
  698. struct i2c_client *c = container_of(dev, struct i2c_client, dev);
  699. struct tda9887 *t = i2c_get_clientdata(c);
  700. dprintk("tda9887: resume\n");
  701. tda9887_configure(t);
  702. return 0;
  703. }
  704. /* ----------------------------------------------------------------------- */
  705. static struct i2c_driver driver = {
  706. .owner = THIS_MODULE,
  707. .name = "i2c tda9887 driver",
  708. .id = -1, /* FIXME */
  709. .flags = I2C_DF_NOTIFY,
  710. .attach_adapter = tda9887_probe,
  711. .detach_client = tda9887_detach,
  712. .command = tda9887_command,
  713. .driver = {
  714. .suspend = tda9887_suspend,
  715. .resume = tda9887_resume,
  716. },
  717. };
  718. static struct i2c_client client_template =
  719. {
  720. .name = "tda9887",
  721. .flags = I2C_CLIENT_ALLOW_USE,
  722. .driver = &driver,
  723. };
  724. static int __init tda9887_init_module(void)
  725. {
  726. return i2c_add_driver(&driver);
  727. }
  728. static void __exit tda9887_cleanup_module(void)
  729. {
  730. i2c_del_driver(&driver);
  731. }
  732. module_init(tda9887_init_module);
  733. module_exit(tda9887_cleanup_module);
  734. /*
  735. * Overrides for Emacs so that we follow Linus's tabbing style.
  736. * ---------------------------------------------------------------------------
  737. * Local variables:
  738. * c-basic-offset: 8
  739. * End:
  740. */