tda9887.c 20 KB

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