tuner-core.c 19 KB

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