tuner-core.c 20 KB

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