tda9887.c 20 KB

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