tuner-core.c 19 KB

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