tuner-core.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. /*
  2. *
  3. * i2c tv tuner chip device driver
  4. * core core, i.e. kernel interfaces, registering and so on
  5. */
  6. #include <linux/module.h>
  7. #include <linux/moduleparam.h>
  8. #include <linux/kernel.h>
  9. #include <linux/sched.h>
  10. #include <linux/string.h>
  11. #include <linux/timer.h>
  12. #include <linux/delay.h>
  13. #include <linux/errno.h>
  14. #include <linux/slab.h>
  15. #include <linux/poll.h>
  16. #include <linux/i2c.h>
  17. #include <linux/types.h>
  18. #include <linux/videodev.h>
  19. #include <linux/init.h>
  20. #include <media/tuner.h>
  21. #include <media/v4l2-common.h>
  22. #include <media/audiochip.h>
  23. #define UNSET (-1U)
  24. /* standard i2c insmod options */
  25. static unsigned short normal_i2c[] = {
  26. 0x42, 0x43, 0x4a, 0x4b, /* tda8290 */
  27. 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
  28. 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f,
  29. I2C_CLIENT_END
  30. };
  31. I2C_CLIENT_INSMOD;
  32. /* insmod options used at init time => read/only */
  33. static unsigned int addr = 0;
  34. static unsigned int no_autodetect = 0;
  35. static unsigned int show_i2c = 0;
  36. /* insmod options used at runtime => read/write */
  37. static unsigned int tuner_debug_old = 0;
  38. int tuner_debug = 0;
  39. static unsigned int tv_range[2] = { 44, 958 };
  40. static unsigned int radio_range[2] = { 65, 108 };
  41. static char pal[] = "--";
  42. static char secam[] = "--";
  43. static char ntsc[] = "-";
  44. module_param(addr, int, 0444);
  45. module_param(no_autodetect, int, 0444);
  46. module_param(show_i2c, int, 0444);
  47. /* Note: tuner_debug is deprecated and will be removed in 2.6.17 */
  48. module_param_named(tuner_debug,tuner_debug_old, int, 0444);
  49. module_param_named(debug,tuner_debug, int, 0644);
  50. module_param_string(pal, pal, sizeof(pal), 0644);
  51. module_param_string(secam, secam, sizeof(secam), 0644);
  52. module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
  53. module_param_array(tv_range, int, NULL, 0644);
  54. module_param_array(radio_range, int, NULL, 0644);
  55. MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
  56. MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
  57. MODULE_LICENSE("GPL");
  58. static struct i2c_driver driver;
  59. static struct i2c_client client_template;
  60. /* ---------------------------------------------------------------------- */
  61. /* Set tuner frequency, freq in Units of 62.5kHz = 1/16MHz */
  62. static void set_tv_freq(struct i2c_client *c, unsigned int freq)
  63. {
  64. struct tuner *t = i2c_get_clientdata(c);
  65. if (t->type == UNSET) {
  66. tuner_warn ("tuner type not set\n");
  67. return;
  68. }
  69. if (NULL == t->set_tv_freq) {
  70. tuner_warn ("Tuner has no way to set tv freq\n");
  71. return;
  72. }
  73. if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
  74. tuner_dbg ("TV freq (%d.%02d) out of range (%d-%d)\n",
  75. freq / 16, freq % 16 * 100 / 16, tv_range[0],
  76. tv_range[1]);
  77. /* V4L2 spec: if the freq is not possible then the closest
  78. possible value should be selected */
  79. if (freq < tv_range[0] * 16)
  80. freq = tv_range[0] * 16;
  81. else
  82. freq = tv_range[1] * 16;
  83. }
  84. t->set_tv_freq(c, freq);
  85. }
  86. static void set_radio_freq(struct i2c_client *c, unsigned int freq)
  87. {
  88. struct tuner *t = i2c_get_clientdata(c);
  89. if (t->type == UNSET) {
  90. tuner_warn ("tuner type not set\n");
  91. return;
  92. }
  93. if (NULL == t->set_radio_freq) {
  94. tuner_warn ("tuner has no way to set radio frequency\n");
  95. return;
  96. }
  97. if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
  98. tuner_dbg ("radio freq (%d.%02d) out of range (%d-%d)\n",
  99. freq / 16000, freq % 16000 * 100 / 16000,
  100. radio_range[0], radio_range[1]);
  101. /* V4L2 spec: if the freq is not possible then the closest
  102. possible value should be selected */
  103. if (freq < radio_range[0] * 16000)
  104. freq = radio_range[0] * 16000;
  105. else
  106. freq = radio_range[1] * 16000;
  107. }
  108. t->set_radio_freq(c, freq);
  109. }
  110. static void set_freq(struct i2c_client *c, unsigned long freq)
  111. {
  112. struct tuner *t = i2c_get_clientdata(c);
  113. switch (t->mode) {
  114. case V4L2_TUNER_RADIO:
  115. tuner_dbg("radio freq set to %lu.%02lu\n",
  116. freq / 16000, freq % 16000 * 100 / 16000);
  117. set_radio_freq(c, freq);
  118. t->radio_freq = freq;
  119. break;
  120. case V4L2_TUNER_ANALOG_TV:
  121. case V4L2_TUNER_DIGITAL_TV:
  122. tuner_dbg("tv freq set to %lu.%02lu\n",
  123. freq / 16, freq % 16 * 100 / 16);
  124. set_tv_freq(c, freq);
  125. t->tv_freq = freq;
  126. break;
  127. }
  128. }
  129. static void set_type(struct i2c_client *c, unsigned int type,
  130. unsigned int new_mode_mask)
  131. {
  132. struct tuner *t = i2c_get_clientdata(c);
  133. unsigned char buffer[4];
  134. if (type == UNSET || type == TUNER_ABSENT) {
  135. tuner_dbg ("tuner 0x%02x: Tuner type absent\n",c->addr);
  136. return;
  137. }
  138. if (type >= tuner_count) {
  139. tuner_warn ("tuner 0x%02x: Tuner count greater than %d\n",c->addr,tuner_count);
  140. return;
  141. }
  142. /* This code detects calls by card attach_inform */
  143. if (NULL == t->i2c.dev.driver) {
  144. tuner_dbg ("tuner 0x%02x: called during i2c_client register by adapter's attach_inform\n", c->addr);
  145. t->type=type;
  146. return;
  147. }
  148. t->type = type;
  149. switch (t->type) {
  150. case TUNER_MT2032:
  151. microtune_init(c);
  152. break;
  153. case TUNER_PHILIPS_TDA8290:
  154. tda8290_init(c);
  155. break;
  156. case TUNER_TEA5767:
  157. if (tea5767_tuner_init(c) == EINVAL) {
  158. t->type = TUNER_ABSENT;
  159. t->mode_mask = T_UNINITIALIZED;
  160. return;
  161. }
  162. t->mode_mask = T_RADIO;
  163. break;
  164. case TUNER_PHILIPS_FMD1216ME_MK3:
  165. buffer[0] = 0x0b;
  166. buffer[1] = 0xdc;
  167. buffer[2] = 0x9c;
  168. buffer[3] = 0x60;
  169. i2c_master_send(c, buffer, 4);
  170. mdelay(1);
  171. buffer[2] = 0x86;
  172. buffer[3] = 0x54;
  173. i2c_master_send(c, buffer, 4);
  174. default_tuner_init(c);
  175. break;
  176. case TUNER_LG_TDVS_H062F:
  177. /* Set the Auxiliary Byte. */
  178. buffer[2] &= ~0x20;
  179. buffer[2] |= 0x18;
  180. buffer[3] = 0x20;
  181. i2c_master_send(c, buffer, 4);
  182. default_tuner_init(c);
  183. break;
  184. case TUNER_PHILIPS_TD1316:
  185. buffer[0] = 0x0b;
  186. buffer[1] = 0xdc;
  187. buffer[2] = 0x86;
  188. buffer[3] = 0xa4;
  189. i2c_master_send(c,buffer,4);
  190. default_tuner_init(c);
  191. break;
  192. #ifdef CONFIG_XC3028
  193. case TUNER_XCEIVE_XC3028:
  194. xc3028_init(c);
  195. break;
  196. #endif
  197. default:
  198. default_tuner_init(c);
  199. break;
  200. }
  201. if (t->mode_mask == T_UNINITIALIZED)
  202. t->mode_mask = new_mode_mask;
  203. set_freq(c, (V4L2_TUNER_RADIO == t->mode) ? t->radio_freq : t->tv_freq);
  204. tuner_dbg("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
  205. c->adapter->name, c->driver->driver.name, c->addr << 1, type,
  206. t->mode_mask);
  207. }
  208. /*
  209. * This function apply tuner config to tuner specified
  210. * by tun_setup structure. I addr is unset, then admin status
  211. * and tun addr status is more precise then current status,
  212. * it's applied. Otherwise status and type are applied only to
  213. * tuner with exactly the same addr.
  214. */
  215. static void set_addr(struct i2c_client *c, struct tuner_setup *tun_setup)
  216. {
  217. struct tuner *t = i2c_get_clientdata(c);
  218. if ( t->type == UNSET && ((tun_setup->addr == ADDR_UNSET &&
  219. (t->mode_mask & tun_setup->mode_mask)) ||
  220. tun_setup->addr == c->addr)) {
  221. set_type(c, tun_setup->type, tun_setup->mode_mask);
  222. }
  223. }
  224. static inline int check_mode(struct tuner *t, char *cmd)
  225. {
  226. if ((1 << t->mode & t->mode_mask) == 0) {
  227. return EINVAL;
  228. }
  229. switch (t->mode) {
  230. case V4L2_TUNER_RADIO:
  231. tuner_dbg("Cmd %s accepted for radio\n", cmd);
  232. break;
  233. case V4L2_TUNER_ANALOG_TV:
  234. tuner_dbg("Cmd %s accepted for analog TV\n", cmd);
  235. break;
  236. case V4L2_TUNER_DIGITAL_TV:
  237. tuner_dbg("Cmd %s accepted for digital TV\n", cmd);
  238. break;
  239. }
  240. return 0;
  241. }
  242. /* get more precise norm info from insmod option */
  243. static int tuner_fixup_std(struct tuner *t)
  244. {
  245. if ((t->std & V4L2_STD_PAL) == V4L2_STD_PAL) {
  246. switch (pal[0]) {
  247. case 'b':
  248. case 'B':
  249. case 'g':
  250. case 'G':
  251. tuner_dbg ("insmod fixup: PAL => PAL-BG\n");
  252. t->std = V4L2_STD_PAL_BG;
  253. break;
  254. case 'i':
  255. case 'I':
  256. tuner_dbg ("insmod fixup: PAL => PAL-I\n");
  257. t->std = V4L2_STD_PAL_I;
  258. break;
  259. case 'd':
  260. case 'D':
  261. case 'k':
  262. case 'K':
  263. tuner_dbg ("insmod fixup: PAL => PAL-DK\n");
  264. t->std = V4L2_STD_PAL_DK;
  265. break;
  266. case 'M':
  267. case 'm':
  268. tuner_dbg ("insmod fixup: PAL => PAL-M\n");
  269. t->std = V4L2_STD_PAL_M;
  270. break;
  271. case 'N':
  272. case 'n':
  273. if (pal[1] == 'c' || pal[1] == 'C') {
  274. tuner_dbg("insmod fixup: PAL => PAL-Nc\n");
  275. t->std = V4L2_STD_PAL_Nc;
  276. } else {
  277. tuner_dbg ("insmod fixup: PAL => PAL-N\n");
  278. t->std = V4L2_STD_PAL_N;
  279. }
  280. break;
  281. case '-':
  282. /* default parameter, do nothing */
  283. break;
  284. default:
  285. tuner_warn ("pal= argument not recognised\n");
  286. break;
  287. }
  288. }
  289. if ((t->std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
  290. switch (secam[0]) {
  291. case 'b':
  292. case 'B':
  293. case 'g':
  294. case 'G':
  295. case 'h':
  296. case 'H':
  297. tuner_dbg("insmod fixup: SECAM => SECAM-BGH\n");
  298. t->std = V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H;
  299. break;
  300. case 'd':
  301. case 'D':
  302. case 'k':
  303. case 'K':
  304. tuner_dbg ("insmod fixup: SECAM => SECAM-DK\n");
  305. t->std = V4L2_STD_SECAM_DK;
  306. break;
  307. case 'l':
  308. case 'L':
  309. if ((secam[1]=='C')||(secam[1]=='c')) {
  310. tuner_dbg ("insmod fixup: SECAM => SECAM-L'\n");
  311. t->std = V4L2_STD_SECAM_LC;
  312. } else {
  313. tuner_dbg ("insmod fixup: SECAM => SECAM-L\n");
  314. t->std = V4L2_STD_SECAM_L;
  315. }
  316. break;
  317. case '-':
  318. /* default parameter, do nothing */
  319. break;
  320. default:
  321. tuner_warn ("secam= argument not recognised\n");
  322. break;
  323. }
  324. }
  325. if ((t->std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
  326. switch (ntsc[0]) {
  327. case 'm':
  328. case 'M':
  329. tuner_dbg("insmod fixup: NTSC => NTSC-M\n");
  330. t->std = V4L2_STD_NTSC_M;
  331. break;
  332. case 'j':
  333. case 'J':
  334. tuner_dbg("insmod fixup: NTSC => NTSC_M_JP\n");
  335. t->std = V4L2_STD_NTSC_M_JP;
  336. break;
  337. case 'k':
  338. case 'K':
  339. tuner_dbg("insmod fixup: NTSC => NTSC_M_KR\n");
  340. t->std = V4L2_STD_NTSC_M_KR;
  341. break;
  342. case '-':
  343. /* default parameter, do nothing */
  344. break;
  345. default:
  346. tuner_info("ntsc= argument not recognised\n");
  347. break;
  348. }
  349. }
  350. return 0;
  351. }
  352. static void tuner_status(struct i2c_client *client)
  353. {
  354. struct tuner *t = i2c_get_clientdata(client);
  355. unsigned long freq, freq_fraction;
  356. const char *p;
  357. switch (t->mode) {
  358. case V4L2_TUNER_RADIO: p = "radio"; break;
  359. case V4L2_TUNER_ANALOG_TV: p = "analog TV"; break;
  360. case V4L2_TUNER_DIGITAL_TV: p = "digital TV"; break;
  361. default: p = "undefined"; break;
  362. }
  363. if (t->mode == V4L2_TUNER_RADIO) {
  364. freq = t->radio_freq / 16000;
  365. freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
  366. } else {
  367. freq = t->tv_freq / 16;
  368. freq_fraction = (t->tv_freq % 16) * 100 / 16;
  369. }
  370. tuner_info("Tuner mode: %s\n", p);
  371. tuner_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
  372. tuner_info("Standard: 0x%08llx\n", t->std);
  373. if (t->mode != V4L2_TUNER_RADIO)
  374. return;
  375. if (t->has_signal) {
  376. tuner_info("Signal strength: %d\n", t->has_signal(client));
  377. }
  378. if (t->is_stereo) {
  379. tuner_info("Stereo: %s\n", t->is_stereo(client) ? "yes" : "no");
  380. }
  381. }
  382. /* ---------------------------------------------------------------------- */
  383. /* static var Used only in tuner_attach and tuner_probe */
  384. static unsigned default_mode_mask;
  385. /* During client attach, set_type is called by adapter's attach_inform callback.
  386. set_type must then be completed by tuner_attach.
  387. */
  388. static int tuner_attach(struct i2c_adapter *adap, int addr, int kind)
  389. {
  390. struct tuner *t;
  391. client_template.adapter = adap;
  392. client_template.addr = addr;
  393. t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
  394. if (NULL == t)
  395. return -ENOMEM;
  396. memcpy(&t->i2c, &client_template, sizeof(struct i2c_client));
  397. i2c_set_clientdata(&t->i2c, t);
  398. t->type = UNSET;
  399. t->radio_if2 = 10700 * 1000; /* 10.7MHz - FM radio */
  400. t->audmode = V4L2_TUNER_MODE_STEREO;
  401. t->mode_mask = T_UNINITIALIZED;
  402. if (tuner_debug_old) {
  403. tuner_debug = tuner_debug_old;
  404. printk(KERN_ERR "tuner: tuner_debug is deprecated and will be removed in 2.6.17.\n");
  405. printk(KERN_ERR "tuner: use the debug option instead.\n");
  406. }
  407. if (show_i2c) {
  408. unsigned char buffer[16];
  409. int i,rc;
  410. memset(buffer, 0, sizeof(buffer));
  411. rc = i2c_master_recv(&t->i2c, buffer, sizeof(buffer));
  412. tuner_info("I2C RECV = ");
  413. for (i=0;i<rc;i++)
  414. printk("%02x ",buffer[i]);
  415. printk("\n");
  416. }
  417. /* autodetection code based on the i2c addr */
  418. if (!no_autodetect) {
  419. switch (addr) {
  420. case 0x42:
  421. case 0x43:
  422. case 0x4a:
  423. case 0x4b:
  424. /* If chip is not tda8290, don't register.
  425. since it can be tda9887*/
  426. if (tda8290_probe(&t->i2c) != 0) {
  427. tuner_dbg("chip at addr %x is not a tda8290\n", addr);
  428. kfree(t);
  429. return 0;
  430. }
  431. break;
  432. case 0x60:
  433. if (tea5767_autodetection(&t->i2c) != EINVAL) {
  434. t->type = TUNER_TEA5767;
  435. t->mode_mask = T_RADIO;
  436. t->mode = T_STANDBY;
  437. t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
  438. default_mode_mask &= ~T_RADIO;
  439. goto register_client;
  440. }
  441. break;
  442. }
  443. }
  444. /* Initializes only the first adapter found */
  445. if (default_mode_mask != T_UNINITIALIZED) {
  446. tuner_dbg ("Setting mode_mask to 0x%02x\n", default_mode_mask);
  447. t->mode_mask = default_mode_mask;
  448. t->tv_freq = 400 * 16; /* Sets freq to VHF High */
  449. t->radio_freq = 87.5 * 16000; /* Sets freq to FM range */
  450. default_mode_mask = T_UNINITIALIZED;
  451. }
  452. /* Should be just before return */
  453. register_client:
  454. tuner_info("chip found @ 0x%x (%s)\n", addr << 1, adap->name);
  455. i2c_attach_client (&t->i2c);
  456. set_type (&t->i2c,t->type, t->mode_mask);
  457. return 0;
  458. }
  459. static int tuner_probe(struct i2c_adapter *adap)
  460. {
  461. if (0 != addr) {
  462. normal_i2c[0] = addr;
  463. normal_i2c[1] = I2C_CLIENT_END;
  464. }
  465. default_mode_mask = T_RADIO | T_ANALOG_TV | T_DIGITAL_TV;
  466. if (adap->class & I2C_CLASS_TV_ANALOG)
  467. return i2c_probe(adap, &addr_data, tuner_attach);
  468. return 0;
  469. }
  470. static int tuner_detach(struct i2c_client *client)
  471. {
  472. struct tuner *t = i2c_get_clientdata(client);
  473. int err;
  474. err = i2c_detach_client(&t->i2c);
  475. if (err) {
  476. tuner_warn
  477. ("Client deregistration failed, client not detached.\n");
  478. return err;
  479. }
  480. kfree(t);
  481. return 0;
  482. }
  483. /*
  484. * Switch tuner to other mode. If tuner support both tv and radio,
  485. * set another frequency to some value (This is needed for some pal
  486. * tuners to avoid locking). Otherwise, just put second tuner in
  487. * standby mode.
  488. */
  489. static inline int set_mode(struct i2c_client *client, struct tuner *t, int mode, char *cmd)
  490. {
  491. if (mode == t->mode)
  492. return 0;
  493. t->mode = mode;
  494. if (check_mode(t, cmd) == EINVAL) {
  495. t->mode = T_STANDBY;
  496. if (t->standby)
  497. t->standby (client);
  498. return EINVAL;
  499. }
  500. return 0;
  501. }
  502. #define switch_v4l2() if (!t->using_v4l2) \
  503. tuner_dbg("switching to v4l2\n"); \
  504. t->using_v4l2 = 1;
  505. static inline int check_v4l2(struct tuner *t)
  506. {
  507. if (t->using_v4l2) {
  508. tuner_dbg ("ignore v4l1 call\n");
  509. return EINVAL;
  510. }
  511. return 0;
  512. }
  513. static int tuner_command(struct i2c_client *client, unsigned int cmd, void *arg)
  514. {
  515. struct tuner *t = i2c_get_clientdata(client);
  516. if (tuner_debug>1)
  517. v4l_i2c_print_ioctl(&(t->i2c),cmd);
  518. switch (cmd) {
  519. /* --- configuration --- */
  520. case TUNER_SET_TYPE_ADDR:
  521. tuner_dbg ("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x\n",
  522. ((struct tuner_setup *)arg)->type,
  523. ((struct tuner_setup *)arg)->addr,
  524. ((struct tuner_setup *)arg)->mode_mask);
  525. set_addr(client, (struct tuner_setup *)arg);
  526. break;
  527. case AUDC_SET_RADIO:
  528. if (set_mode(client, t, V4L2_TUNER_RADIO, "AUDC_SET_RADIO")
  529. == EINVAL)
  530. return 0;
  531. if (t->radio_freq)
  532. set_freq(client, t->radio_freq);
  533. break;
  534. case TUNER_SET_STANDBY:
  535. if (check_mode(t, "TUNER_SET_STANDBY") == EINVAL)
  536. return 0;
  537. if (t->standby)
  538. t->standby (client);
  539. break;
  540. case VIDIOCSAUDIO:
  541. if (check_mode(t, "VIDIOCSAUDIO") == EINVAL)
  542. return 0;
  543. if (check_v4l2(t) == EINVAL)
  544. return 0;
  545. /* Should be implemented, since bttv calls it */
  546. tuner_dbg("VIDIOCSAUDIO not implemented.\n");
  547. break;
  548. /* --- v4l ioctls --- */
  549. /* take care: bttv does userspace copying, we'll get a
  550. kernel pointer here... */
  551. case VIDIOCSCHAN:
  552. {
  553. static const v4l2_std_id map[] = {
  554. [VIDEO_MODE_PAL] = V4L2_STD_PAL,
  555. [VIDEO_MODE_NTSC] = V4L2_STD_NTSC_M,
  556. [VIDEO_MODE_SECAM] = V4L2_STD_SECAM,
  557. [4 /* bttv */ ] = V4L2_STD_PAL_M,
  558. [5 /* bttv */ ] = V4L2_STD_PAL_N,
  559. [6 /* bttv */ ] = V4L2_STD_NTSC_M_JP,
  560. };
  561. struct video_channel *vc = arg;
  562. if (check_v4l2(t) == EINVAL)
  563. return 0;
  564. if (set_mode(client,t,V4L2_TUNER_ANALOG_TV, "VIDIOCSCHAN")==EINVAL)
  565. return 0;
  566. if (vc->norm < ARRAY_SIZE(map))
  567. t->std = map[vc->norm];
  568. tuner_fixup_std(t);
  569. if (t->tv_freq)
  570. set_tv_freq(client, t->tv_freq);
  571. return 0;
  572. }
  573. case VIDIOCSFREQ:
  574. {
  575. unsigned long *v = arg;
  576. if (check_mode(t, "VIDIOCSFREQ") == EINVAL)
  577. return 0;
  578. if (check_v4l2(t) == EINVAL)
  579. return 0;
  580. set_freq(client, *v);
  581. return 0;
  582. }
  583. case VIDIOCGTUNER:
  584. {
  585. struct video_tuner *vt = arg;
  586. if (check_mode(t, "VIDIOCGTUNER") == EINVAL)
  587. return 0;
  588. if (check_v4l2(t) == EINVAL)
  589. return 0;
  590. if (V4L2_TUNER_RADIO == t->mode) {
  591. if (t->has_signal)
  592. vt->signal = t->has_signal(client);
  593. if (t->is_stereo) {
  594. if (t->is_stereo(client))
  595. vt->flags |=
  596. VIDEO_TUNER_STEREO_ON;
  597. else
  598. vt->flags &=
  599. ~VIDEO_TUNER_STEREO_ON;
  600. }
  601. vt->flags |= VIDEO_TUNER_LOW; /* Allow freqs at 62.5 Hz */
  602. vt->rangelow = radio_range[0] * 16000;
  603. vt->rangehigh = radio_range[1] * 16000;
  604. } else {
  605. vt->rangelow = tv_range[0] * 16;
  606. vt->rangehigh = tv_range[1] * 16;
  607. }
  608. return 0;
  609. }
  610. case VIDIOCGAUDIO:
  611. {
  612. struct video_audio *va = arg;
  613. if (check_mode(t, "VIDIOCGAUDIO") == EINVAL)
  614. return 0;
  615. if (check_v4l2(t) == EINVAL)
  616. return 0;
  617. if (V4L2_TUNER_RADIO == t->mode && t->is_stereo)
  618. va->mode = t->is_stereo(client)
  619. ? VIDEO_SOUND_STEREO : VIDEO_SOUND_MONO;
  620. return 0;
  621. }
  622. case VIDIOC_S_STD:
  623. {
  624. v4l2_std_id *id = arg;
  625. if (set_mode (client, t, V4L2_TUNER_ANALOG_TV, "VIDIOC_S_STD")
  626. == EINVAL)
  627. return 0;
  628. switch_v4l2();
  629. t->std = *id;
  630. tuner_fixup_std(t);
  631. if (t->tv_freq)
  632. set_freq(client, t->tv_freq);
  633. break;
  634. }
  635. case VIDIOC_S_FREQUENCY:
  636. {
  637. struct v4l2_frequency *f = arg;
  638. switch_v4l2();
  639. if (V4L2_TUNER_RADIO == f->type &&
  640. V4L2_TUNER_RADIO != t->mode) {
  641. if (set_mode (client, t, f->type, "VIDIOC_S_FREQUENCY")
  642. == EINVAL)
  643. return 0;
  644. }
  645. set_freq(client,f->frequency);
  646. break;
  647. }
  648. case VIDIOC_G_FREQUENCY:
  649. {
  650. struct v4l2_frequency *f = arg;
  651. if (check_mode(t, "VIDIOC_G_FREQUENCY") == EINVAL)
  652. return 0;
  653. switch_v4l2();
  654. f->type = t->mode;
  655. f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
  656. t->radio_freq : t->tv_freq;
  657. break;
  658. }
  659. case VIDIOC_G_TUNER:
  660. {
  661. struct v4l2_tuner *tuner = arg;
  662. if (check_mode(t, "VIDIOC_G_TUNER") == EINVAL)
  663. return 0;
  664. switch_v4l2();
  665. tuner->type = t->mode;
  666. if (t->mode != V4L2_TUNER_RADIO) {
  667. tuner->rangelow = tv_range[0] * 16;
  668. tuner->rangehigh = tv_range[1] * 16;
  669. break;
  670. }
  671. /* radio mode */
  672. if (t->has_signal)
  673. tuner->signal = t->has_signal(client);
  674. tuner->rxsubchans =
  675. V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  676. if (t->is_stereo) {
  677. tuner->rxsubchans = t->is_stereo(client) ?
  678. V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
  679. }
  680. tuner->capability |=
  681. V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  682. tuner->audmode = t->audmode;
  683. tuner->rangelow = radio_range[0] * 16000;
  684. tuner->rangehigh = radio_range[1] * 16000;
  685. break;
  686. }
  687. case VIDIOC_S_TUNER:
  688. {
  689. struct v4l2_tuner *tuner = arg;
  690. if (check_mode(t, "VIDIOC_S_TUNER") == EINVAL)
  691. return 0;
  692. switch_v4l2();
  693. /* do nothing unless we're a radio tuner */
  694. if (t->mode != V4L2_TUNER_RADIO)
  695. break;
  696. t->audmode = tuner->audmode;
  697. set_radio_freq(client, t->radio_freq);
  698. break;
  699. }
  700. case VIDIOC_LOG_STATUS:
  701. tuner_status(client);
  702. break;
  703. }
  704. return 0;
  705. }
  706. static int tuner_suspend(struct device *dev, pm_message_t state)
  707. {
  708. struct i2c_client *c = container_of (dev, struct i2c_client, dev);
  709. struct tuner *t = i2c_get_clientdata (c);
  710. tuner_dbg ("suspend\n");
  711. /* FIXME: power down ??? */
  712. return 0;
  713. }
  714. static int tuner_resume(struct device *dev)
  715. {
  716. struct i2c_client *c = container_of (dev, struct i2c_client, dev);
  717. struct tuner *t = i2c_get_clientdata (c);
  718. tuner_dbg ("resume\n");
  719. if (V4L2_TUNER_RADIO == t->mode) {
  720. if (t->radio_freq)
  721. set_freq(c, t->radio_freq);
  722. } else {
  723. if (t->tv_freq)
  724. set_freq(c, t->tv_freq);
  725. }
  726. return 0;
  727. }
  728. /* ----------------------------------------------------------------------- */
  729. static struct i2c_driver driver = {
  730. .id = I2C_DRIVERID_TUNER,
  731. .attach_adapter = tuner_probe,
  732. .detach_client = tuner_detach,
  733. .command = tuner_command,
  734. .driver = {
  735. .name = "tuner",
  736. .suspend = tuner_suspend,
  737. .resume = tuner_resume,
  738. },
  739. };
  740. static struct i2c_client client_template = {
  741. .name = "(tuner unset)",
  742. .driver = &driver,
  743. };
  744. static int __init tuner_init_module(void)
  745. {
  746. return i2c_add_driver(&driver);
  747. }
  748. static void __exit tuner_cleanup_module(void)
  749. {
  750. i2c_del_driver(&driver);
  751. }
  752. module_init(tuner_init_module);
  753. module_exit(tuner_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. */