tda9887.c 20 KB

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