tuner-core.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426
  1. /*
  2. * i2c tv tuner chip device driver
  3. * core core, i.e. kernel interfaces, registering and so on
  4. *
  5. * Copyright(c) by Ralph Metzler, Gerd Knorr, Gunther Mayer
  6. *
  7. * Copyright(c) 2005-2011 by Mauro Carvalho Chehab
  8. * - Added support for a separate Radio tuner
  9. * - Major rework and cleanups at the code
  10. *
  11. * This driver supports many devices and the idea is to let the driver
  12. * detect which device is present. So rather than listing all supported
  13. * devices here, we pretend to support a single, fake device type that will
  14. * handle both radio and analog TV tuning.
  15. */
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/string.h>
  19. #include <linux/timer.h>
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/slab.h>
  23. #include <linux/poll.h>
  24. #include <linux/i2c.h>
  25. #include <linux/types.h>
  26. #include <linux/init.h>
  27. #include <linux/videodev2.h>
  28. #include <media/tuner.h>
  29. #include <media/tuner-types.h>
  30. #include <media/v4l2-device.h>
  31. #include <media/v4l2-ioctl.h>
  32. #include "mt20xx.h"
  33. #include "tda8290.h"
  34. #include "tea5761.h"
  35. #include "tea5767.h"
  36. #include "tuner-xc2028.h"
  37. #include "tuner-simple.h"
  38. #include "tda9887.h"
  39. #include "xc5000.h"
  40. #include "tda18271.h"
  41. #include "xc4000.h"
  42. #define UNSET (-1U)
  43. /*
  44. * Driver modprobe parameters
  45. */
  46. /* insmod options used at init time => read/only */
  47. static unsigned int addr;
  48. static unsigned int no_autodetect;
  49. static unsigned int show_i2c;
  50. module_param(addr, int, 0444);
  51. module_param(no_autodetect, int, 0444);
  52. module_param(show_i2c, int, 0444);
  53. /* insmod options used at runtime => read/write */
  54. static int tuner_debug;
  55. static unsigned int tv_range[2] = { 44, 958 };
  56. static unsigned int radio_range[2] = { 65, 108 };
  57. static char pal[] = "--";
  58. static char secam[] = "--";
  59. static char ntsc[] = "-";
  60. module_param_named(debug, tuner_debug, int, 0644);
  61. module_param_array(tv_range, int, NULL, 0644);
  62. module_param_array(radio_range, int, NULL, 0644);
  63. module_param_string(pal, pal, sizeof(pal), 0644);
  64. module_param_string(secam, secam, sizeof(secam), 0644);
  65. module_param_string(ntsc, ntsc, sizeof(ntsc), 0644);
  66. /*
  67. * Static vars
  68. */
  69. static LIST_HEAD(tuner_list);
  70. static const struct v4l2_subdev_ops tuner_ops;
  71. /*
  72. * Debug macros
  73. */
  74. #undef pr_fmt
  75. #define pr_fmt(fmt) KBUILD_MODNAME ": %d-%04x: " fmt, \
  76. i2c_adapter_id(t->i2c->adapter), t->i2c->addr
  77. #define dprintk(fmt, arg...) do { \
  78. if (tuner_debug) \
  79. printk(KERN_DEBUG pr_fmt("%s: " fmt), __func__, ##arg); \
  80. } while (0)
  81. /*
  82. * Internal enums/struct used inside the driver
  83. */
  84. /**
  85. * enum tuner_pad_index - tuner pad index for MEDIA_ENT_F_TUNER
  86. *
  87. * @TUNER_PAD_RF_INPUT:
  88. * Radiofrequency (RF) sink pad, usually linked to a RF connector entity.
  89. * @TUNER_PAD_OUTPUT:
  90. * tuner video output source pad. Contains the video chrominance
  91. * and luminance or the hole bandwidth of the signal converted to
  92. * an Intermediate Frequency (IF) or to baseband (on zero-IF tuners).
  93. * @TUNER_PAD_AUD_OUT:
  94. * Tuner audio output source pad. Tuners used to decode analog TV
  95. * signals have an extra pad for audio output. Old tuners use an
  96. * analog stage with a saw filter for the audio IF frequency. The
  97. * output of the pad is, in this case, the audio IF, with should be
  98. * decoded either by the bridge chipset (that's the case of cx2388x
  99. * chipsets) or may require an external IF sound processor, like
  100. * msp34xx. On modern silicon tuners, the audio IF decoder is usually
  101. * incorporated at the tuner. On such case, the output of this pad
  102. * is an audio sampled data.
  103. * @TUNER_NUM_PADS:
  104. * Number of pads of the tuner.
  105. */
  106. enum tuner_pad_index {
  107. TUNER_PAD_RF_INPUT,
  108. TUNER_PAD_OUTPUT,
  109. TUNER_PAD_AUD_OUT,
  110. TUNER_NUM_PADS
  111. };
  112. /**
  113. * enum if_vid_dec_pad_index - video IF-PLL pad index
  114. * for MEDIA_ENT_F_IF_VID_DECODER
  115. *
  116. * @IF_VID_DEC_PAD_IF_INPUT:
  117. * video Intermediate Frequency (IF) sink pad
  118. * @IF_VID_DEC_PAD_OUT:
  119. * IF-PLL video output source pad. Contains the video chrominance
  120. * and luminance IF signals.
  121. * @IF_VID_DEC_PAD_NUM_PADS:
  122. * Number of pads of the video IF-PLL.
  123. */
  124. enum if_vid_dec_pad_index {
  125. IF_VID_DEC_PAD_IF_INPUT,
  126. IF_VID_DEC_PAD_OUT,
  127. IF_VID_DEC_PAD_NUM_PADS
  128. };
  129. struct tuner {
  130. /* device */
  131. struct dvb_frontend fe;
  132. struct i2c_client *i2c;
  133. struct v4l2_subdev sd;
  134. struct list_head list;
  135. /* keep track of the current settings */
  136. v4l2_std_id std;
  137. unsigned int tv_freq;
  138. unsigned int radio_freq;
  139. unsigned int audmode;
  140. enum v4l2_tuner_type mode;
  141. unsigned int mode_mask; /* Combination of allowable modes */
  142. bool standby; /* Standby mode */
  143. unsigned int type; /* chip type id */
  144. void *config;
  145. const char *name;
  146. #if defined(CONFIG_MEDIA_CONTROLLER)
  147. struct media_pad pad[TUNER_NUM_PADS];
  148. #endif
  149. };
  150. /*
  151. * Function prototypes
  152. */
  153. static void set_tv_freq(struct i2c_client *c, unsigned int freq);
  154. static void set_radio_freq(struct i2c_client *c, unsigned int freq);
  155. /*
  156. * tuner attach/detach logic
  157. */
  158. /* This macro allows us to probe dynamically, avoiding static links */
  159. #ifdef CONFIG_MEDIA_ATTACH
  160. #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
  161. int __r = -EINVAL; \
  162. typeof(&FUNCTION) __a = symbol_request(FUNCTION); \
  163. if (__a) { \
  164. __r = (int) __a(ARGS); \
  165. symbol_put(FUNCTION); \
  166. } else { \
  167. printk(KERN_ERR "TUNER: Unable to find " \
  168. "symbol "#FUNCTION"()\n"); \
  169. } \
  170. __r; \
  171. })
  172. static void tuner_detach(struct dvb_frontend *fe)
  173. {
  174. if (fe->ops.tuner_ops.release) {
  175. fe->ops.tuner_ops.release(fe);
  176. symbol_put_addr(fe->ops.tuner_ops.release);
  177. }
  178. if (fe->ops.analog_ops.release) {
  179. fe->ops.analog_ops.release(fe);
  180. symbol_put_addr(fe->ops.analog_ops.release);
  181. }
  182. }
  183. #else
  184. #define tuner_symbol_probe(FUNCTION, ARGS...) ({ \
  185. FUNCTION(ARGS); \
  186. })
  187. static void tuner_detach(struct dvb_frontend *fe)
  188. {
  189. if (fe->ops.tuner_ops.release)
  190. fe->ops.tuner_ops.release(fe);
  191. if (fe->ops.analog_ops.release)
  192. fe->ops.analog_ops.release(fe);
  193. }
  194. #endif
  195. static inline struct tuner *to_tuner(struct v4l2_subdev *sd)
  196. {
  197. return container_of(sd, struct tuner, sd);
  198. }
  199. /*
  200. * struct analog_demod_ops callbacks
  201. */
  202. static void fe_set_params(struct dvb_frontend *fe,
  203. struct analog_parameters *params)
  204. {
  205. struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
  206. struct tuner *t = fe->analog_demod_priv;
  207. if (NULL == fe_tuner_ops->set_analog_params) {
  208. pr_warn("Tuner frontend module has no way to set freq\n");
  209. return;
  210. }
  211. fe_tuner_ops->set_analog_params(fe, params);
  212. }
  213. static void fe_standby(struct dvb_frontend *fe)
  214. {
  215. struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
  216. if (fe_tuner_ops->sleep)
  217. fe_tuner_ops->sleep(fe);
  218. }
  219. static int fe_set_config(struct dvb_frontend *fe, void *priv_cfg)
  220. {
  221. struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
  222. struct tuner *t = fe->analog_demod_priv;
  223. if (fe_tuner_ops->set_config)
  224. return fe_tuner_ops->set_config(fe, priv_cfg);
  225. pr_warn("Tuner frontend module has no way to set config\n");
  226. return 0;
  227. }
  228. static void tuner_status(struct dvb_frontend *fe);
  229. static const struct analog_demod_ops tuner_analog_ops = {
  230. .set_params = fe_set_params,
  231. .standby = fe_standby,
  232. .set_config = fe_set_config,
  233. .tuner_status = tuner_status
  234. };
  235. /*
  236. * Functions to select between radio and TV and tuner probe/remove functions
  237. */
  238. /**
  239. * set_type - Sets the tuner type for a given device
  240. *
  241. * @c: i2c_client descriptor
  242. * @type: type of the tuner (e. g. tuner number)
  243. * @new_mode_mask: Indicates if tuner supports TV and/or Radio
  244. * @new_config: an optional parameter used by a few tuners to adjust
  245. * internal parameters, like LNA mode
  246. * @tuner_callback: an optional function to be called when switching
  247. * to analog mode
  248. *
  249. * This function applies the tuner config to tuner specified
  250. * by tun_setup structure. It contains several per-tuner initialization "magic"
  251. */
  252. static void set_type(struct i2c_client *c, unsigned int type,
  253. unsigned int new_mode_mask, void *new_config,
  254. int (*tuner_callback) (void *dev, int component, int cmd, int arg))
  255. {
  256. struct tuner *t = to_tuner(i2c_get_clientdata(c));
  257. struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
  258. struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
  259. unsigned char buffer[4];
  260. int tune_now = 1;
  261. if (type == UNSET || type == TUNER_ABSENT) {
  262. dprintk("tuner 0x%02x: Tuner type absent\n", c->addr);
  263. return;
  264. }
  265. t->type = type;
  266. t->config = new_config;
  267. if (tuner_callback != NULL) {
  268. dprintk("defining GPIO callback\n");
  269. t->fe.callback = tuner_callback;
  270. }
  271. /* discard private data, in case set_type() was previously called */
  272. tuner_detach(&t->fe);
  273. t->fe.analog_demod_priv = NULL;
  274. switch (t->type) {
  275. case TUNER_MT2032:
  276. if (!dvb_attach(microtune_attach,
  277. &t->fe, t->i2c->adapter, t->i2c->addr))
  278. goto attach_failed;
  279. break;
  280. case TUNER_PHILIPS_TDA8290:
  281. {
  282. if (!dvb_attach(tda829x_attach, &t->fe, t->i2c->adapter,
  283. t->i2c->addr, t->config))
  284. goto attach_failed;
  285. break;
  286. }
  287. case TUNER_TEA5767:
  288. if (!dvb_attach(tea5767_attach, &t->fe,
  289. t->i2c->adapter, t->i2c->addr))
  290. goto attach_failed;
  291. t->mode_mask = T_RADIO;
  292. break;
  293. case TUNER_TEA5761:
  294. if (!dvb_attach(tea5761_attach, &t->fe,
  295. t->i2c->adapter, t->i2c->addr))
  296. goto attach_failed;
  297. t->mode_mask = T_RADIO;
  298. break;
  299. case TUNER_PHILIPS_FMD1216ME_MK3:
  300. case TUNER_PHILIPS_FMD1216MEX_MK3:
  301. buffer[0] = 0x0b;
  302. buffer[1] = 0xdc;
  303. buffer[2] = 0x9c;
  304. buffer[3] = 0x60;
  305. i2c_master_send(c, buffer, 4);
  306. mdelay(1);
  307. buffer[2] = 0x86;
  308. buffer[3] = 0x54;
  309. i2c_master_send(c, buffer, 4);
  310. if (!dvb_attach(simple_tuner_attach, &t->fe,
  311. t->i2c->adapter, t->i2c->addr, t->type))
  312. goto attach_failed;
  313. break;
  314. case TUNER_PHILIPS_TD1316:
  315. buffer[0] = 0x0b;
  316. buffer[1] = 0xdc;
  317. buffer[2] = 0x86;
  318. buffer[3] = 0xa4;
  319. i2c_master_send(c, buffer, 4);
  320. if (!dvb_attach(simple_tuner_attach, &t->fe,
  321. t->i2c->adapter, t->i2c->addr, t->type))
  322. goto attach_failed;
  323. break;
  324. case TUNER_XC2028:
  325. {
  326. struct xc2028_config cfg = {
  327. .i2c_adap = t->i2c->adapter,
  328. .i2c_addr = t->i2c->addr,
  329. };
  330. if (!dvb_attach(xc2028_attach, &t->fe, &cfg))
  331. goto attach_failed;
  332. tune_now = 0;
  333. break;
  334. }
  335. case TUNER_TDA9887:
  336. if (!dvb_attach(tda9887_attach,
  337. &t->fe, t->i2c->adapter, t->i2c->addr))
  338. goto attach_failed;
  339. break;
  340. case TUNER_XC5000:
  341. {
  342. struct xc5000_config xc5000_cfg = {
  343. .i2c_address = t->i2c->addr,
  344. /* if_khz will be set at dvb_attach() */
  345. .if_khz = 0,
  346. };
  347. if (!dvb_attach(xc5000_attach,
  348. &t->fe, t->i2c->adapter, &xc5000_cfg))
  349. goto attach_failed;
  350. tune_now = 0;
  351. break;
  352. }
  353. case TUNER_XC5000C:
  354. {
  355. struct xc5000_config xc5000c_cfg = {
  356. .i2c_address = t->i2c->addr,
  357. /* if_khz will be set at dvb_attach() */
  358. .if_khz = 0,
  359. .chip_id = XC5000C,
  360. };
  361. if (!dvb_attach(xc5000_attach,
  362. &t->fe, t->i2c->adapter, &xc5000c_cfg))
  363. goto attach_failed;
  364. tune_now = 0;
  365. break;
  366. }
  367. case TUNER_NXP_TDA18271:
  368. {
  369. struct tda18271_config cfg = {
  370. .small_i2c = TDA18271_03_BYTE_CHUNK_INIT,
  371. };
  372. if (!dvb_attach(tda18271_attach, &t->fe, t->i2c->addr,
  373. t->i2c->adapter, &cfg))
  374. goto attach_failed;
  375. tune_now = 0;
  376. break;
  377. }
  378. case TUNER_XC4000:
  379. {
  380. struct xc4000_config xc4000_cfg = {
  381. .i2c_address = t->i2c->addr,
  382. /* FIXME: the correct parameters will be set */
  383. /* only when the digital dvb_attach() occurs */
  384. .default_pm = 0,
  385. .dvb_amplitude = 0,
  386. .set_smoothedcvbs = 0,
  387. .if_khz = 0
  388. };
  389. if (!dvb_attach(xc4000_attach,
  390. &t->fe, t->i2c->adapter, &xc4000_cfg))
  391. goto attach_failed;
  392. tune_now = 0;
  393. break;
  394. }
  395. default:
  396. if (!dvb_attach(simple_tuner_attach, &t->fe,
  397. t->i2c->adapter, t->i2c->addr, t->type))
  398. goto attach_failed;
  399. break;
  400. }
  401. if ((NULL == analog_ops->set_params) &&
  402. (fe_tuner_ops->set_analog_params)) {
  403. t->name = fe_tuner_ops->info.name;
  404. t->fe.analog_demod_priv = t;
  405. memcpy(analog_ops, &tuner_analog_ops,
  406. sizeof(struct analog_demod_ops));
  407. if (fe_tuner_ops->get_rf_strength)
  408. analog_ops->has_signal = fe_tuner_ops->get_rf_strength;
  409. if (fe_tuner_ops->get_afc)
  410. analog_ops->get_afc = fe_tuner_ops->get_afc;
  411. } else {
  412. t->name = analog_ops->info.name;
  413. }
  414. #ifdef CONFIG_MEDIA_CONTROLLER
  415. t->sd.entity.name = t->name;
  416. #endif
  417. dprintk("type set to %s\n", t->name);
  418. t->mode_mask = new_mode_mask;
  419. /* Some tuners require more initialization setup before use,
  420. such as firmware download or device calibration.
  421. trying to set a frequency here will just fail
  422. FIXME: better to move set_freq to the tuner code. This is needed
  423. on analog tuners for PLL to properly work
  424. */
  425. if (tune_now) {
  426. if (V4L2_TUNER_RADIO == t->mode)
  427. set_radio_freq(c, t->radio_freq);
  428. else
  429. set_tv_freq(c, t->tv_freq);
  430. }
  431. dprintk("%s %s I2C addr 0x%02x with type %d used for 0x%02x\n",
  432. c->adapter->name, c->dev.driver->name, c->addr << 1, type,
  433. t->mode_mask);
  434. return;
  435. attach_failed:
  436. dprintk("Tuner attach for type = %d failed.\n", t->type);
  437. t->type = TUNER_ABSENT;
  438. return;
  439. }
  440. /**
  441. * tuner_s_type_addr - Sets the tuner type for a device
  442. *
  443. * @sd: subdev descriptor
  444. * @tun_setup: type to be associated to a given tuner i2c address
  445. *
  446. * This function applies the tuner config to tuner specified
  447. * by tun_setup structure.
  448. * If tuner I2C address is UNSET, then it will only set the device
  449. * if the tuner supports the mode specified in the call.
  450. * If the address is specified, the change will be applied only if
  451. * tuner I2C address matches.
  452. * The call can change the tuner number and the tuner mode.
  453. */
  454. static int tuner_s_type_addr(struct v4l2_subdev *sd,
  455. struct tuner_setup *tun_setup)
  456. {
  457. struct tuner *t = to_tuner(sd);
  458. struct i2c_client *c = v4l2_get_subdevdata(sd);
  459. dprintk("Calling set_type_addr for type=%d, addr=0x%02x, mode=0x%02x, config=%p\n",
  460. tun_setup->type,
  461. tun_setup->addr,
  462. tun_setup->mode_mask,
  463. tun_setup->config);
  464. if ((t->type == UNSET && ((tun_setup->addr == ADDR_UNSET) &&
  465. (t->mode_mask & tun_setup->mode_mask))) ||
  466. (tun_setup->addr == c->addr)) {
  467. set_type(c, tun_setup->type, tun_setup->mode_mask,
  468. tun_setup->config, tun_setup->tuner_callback);
  469. } else
  470. dprintk("set addr discarded for type %i, mask %x. Asked to change tuner at addr 0x%02x, with mask %x\n",
  471. t->type, t->mode_mask,
  472. tun_setup->addr, tun_setup->mode_mask);
  473. return 0;
  474. }
  475. /**
  476. * tuner_s_config - Sets tuner configuration
  477. *
  478. * @sd: subdev descriptor
  479. * @cfg: tuner configuration
  480. *
  481. * Calls tuner set_config() private function to set some tuner-internal
  482. * parameters
  483. */
  484. static int tuner_s_config(struct v4l2_subdev *sd,
  485. const struct v4l2_priv_tun_config *cfg)
  486. {
  487. struct tuner *t = to_tuner(sd);
  488. struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
  489. if (t->type != cfg->tuner)
  490. return 0;
  491. if (analog_ops->set_config) {
  492. analog_ops->set_config(&t->fe, cfg->priv);
  493. return 0;
  494. }
  495. dprintk("Tuner frontend module has no way to set config\n");
  496. return 0;
  497. }
  498. /**
  499. * tuner_lookup - Seek for tuner adapters
  500. *
  501. * @adap: i2c_adapter struct
  502. * @radio: pointer to be filled if the adapter is radio
  503. * @tv: pointer to be filled if the adapter is TV
  504. *
  505. * Search for existing radio and/or TV tuners on the given I2C adapter,
  506. * discarding demod-only adapters (tda9887).
  507. *
  508. * Note that when this function is called from tuner_probe you can be
  509. * certain no other devices will be added/deleted at the same time, I2C
  510. * core protects against that.
  511. */
  512. static void tuner_lookup(struct i2c_adapter *adap,
  513. struct tuner **radio, struct tuner **tv)
  514. {
  515. struct tuner *pos;
  516. *radio = NULL;
  517. *tv = NULL;
  518. list_for_each_entry(pos, &tuner_list, list) {
  519. int mode_mask;
  520. if (pos->i2c->adapter != adap ||
  521. strcmp(pos->i2c->dev.driver->name, "tuner"))
  522. continue;
  523. mode_mask = pos->mode_mask;
  524. if (*radio == NULL && mode_mask == T_RADIO)
  525. *radio = pos;
  526. /* Note: currently TDA9887 is the only demod-only
  527. device. If other devices appear then we need to
  528. make this test more general. */
  529. else if (*tv == NULL && pos->type != TUNER_TDA9887 &&
  530. (pos->mode_mask & T_ANALOG_TV))
  531. *tv = pos;
  532. }
  533. }
  534. /**
  535. *tuner_probe - Probes the existing tuners on an I2C bus
  536. *
  537. * @client: i2c_client descriptor
  538. * @id: not used
  539. *
  540. * This routine probes for tuners at the expected I2C addresses. On most
  541. * cases, if a device answers to a given I2C address, it assumes that the
  542. * device is a tuner. On a few cases, however, an additional logic is needed
  543. * to double check if the device is really a tuner, or to identify the tuner
  544. * type, like on tea5767/5761 devices.
  545. *
  546. * During client attach, set_type is called by adapter's attach_inform callback.
  547. * set_type must then be completed by tuner_probe.
  548. */
  549. static int tuner_probe(struct i2c_client *client,
  550. const struct i2c_device_id *id)
  551. {
  552. struct tuner *t;
  553. struct tuner *radio;
  554. struct tuner *tv;
  555. #ifdef CONFIG_MEDIA_CONTROLLER
  556. int ret;
  557. #endif
  558. t = kzalloc(sizeof(struct tuner), GFP_KERNEL);
  559. if (NULL == t)
  560. return -ENOMEM;
  561. v4l2_i2c_subdev_init(&t->sd, client, &tuner_ops);
  562. t->i2c = client;
  563. t->name = "(tuner unset)";
  564. t->type = UNSET;
  565. t->audmode = V4L2_TUNER_MODE_STEREO;
  566. t->standby = true;
  567. t->radio_freq = 87.5 * 16000; /* Initial freq range */
  568. t->tv_freq = 400 * 16; /* Sets freq to VHF High - needed for some PLL's to properly start */
  569. if (show_i2c) {
  570. unsigned char buffer[16];
  571. int rc;
  572. memset(buffer, 0, sizeof(buffer));
  573. rc = i2c_master_recv(client, buffer, sizeof(buffer));
  574. if (rc >= 0)
  575. pr_info("I2C RECV = %*ph\n", rc, buffer);
  576. }
  577. /* autodetection code based on the i2c addr */
  578. if (!no_autodetect) {
  579. switch (client->addr) {
  580. case 0x10:
  581. if (tuner_symbol_probe(tea5761_autodetection,
  582. t->i2c->adapter,
  583. t->i2c->addr) >= 0) {
  584. t->type = TUNER_TEA5761;
  585. t->mode_mask = T_RADIO;
  586. tuner_lookup(t->i2c->adapter, &radio, &tv);
  587. if (tv)
  588. tv->mode_mask &= ~T_RADIO;
  589. goto register_client;
  590. }
  591. kfree(t);
  592. return -ENODEV;
  593. case 0x42:
  594. case 0x43:
  595. case 0x4a:
  596. case 0x4b:
  597. /* If chip is not tda8290, don't register.
  598. since it can be tda9887*/
  599. if (tuner_symbol_probe(tda829x_probe, t->i2c->adapter,
  600. t->i2c->addr) >= 0) {
  601. dprintk("tda829x detected\n");
  602. } else {
  603. /* Default is being tda9887 */
  604. t->type = TUNER_TDA9887;
  605. t->mode_mask = T_RADIO | T_ANALOG_TV;
  606. goto register_client;
  607. }
  608. break;
  609. case 0x60:
  610. if (tuner_symbol_probe(tea5767_autodetection,
  611. t->i2c->adapter, t->i2c->addr)
  612. >= 0) {
  613. t->type = TUNER_TEA5767;
  614. t->mode_mask = T_RADIO;
  615. /* Sets freq to FM range */
  616. tuner_lookup(t->i2c->adapter, &radio, &tv);
  617. if (tv)
  618. tv->mode_mask &= ~T_RADIO;
  619. goto register_client;
  620. }
  621. break;
  622. }
  623. }
  624. /* Initializes only the first TV tuner on this adapter. Why only the
  625. first? Because there are some devices (notably the ones with TI
  626. tuners) that have more than one i2c address for the *same* device.
  627. Experience shows that, except for just one case, the first
  628. address is the right one. The exception is a Russian tuner
  629. (ACORP_Y878F). So, the desired behavior is just to enable the
  630. first found TV tuner. */
  631. tuner_lookup(t->i2c->adapter, &radio, &tv);
  632. if (tv == NULL) {
  633. t->mode_mask = T_ANALOG_TV;
  634. if (radio == NULL)
  635. t->mode_mask |= T_RADIO;
  636. dprintk("Setting mode_mask to 0x%02x\n", t->mode_mask);
  637. }
  638. /* Should be just before return */
  639. register_client:
  640. #if defined(CONFIG_MEDIA_CONTROLLER)
  641. t->sd.entity.name = t->name;
  642. /*
  643. * Handle the special case where the tuner has actually
  644. * two stages: the PLL to tune into a frequency and the
  645. * IF-PLL demodulator (tda988x).
  646. */
  647. if (t->type == TUNER_TDA9887) {
  648. t->pad[IF_VID_DEC_PAD_IF_INPUT].flags = MEDIA_PAD_FL_SINK;
  649. t->pad[IF_VID_DEC_PAD_IF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
  650. t->pad[IF_VID_DEC_PAD_OUT].flags = MEDIA_PAD_FL_SOURCE;
  651. t->pad[IF_VID_DEC_PAD_OUT].sig_type = PAD_SIGNAL_ANALOG;
  652. ret = media_entity_pads_init(&t->sd.entity,
  653. IF_VID_DEC_PAD_NUM_PADS,
  654. &t->pad[0]);
  655. t->sd.entity.function = MEDIA_ENT_F_IF_VID_DECODER;
  656. } else {
  657. t->pad[TUNER_PAD_RF_INPUT].flags = MEDIA_PAD_FL_SINK;
  658. t->pad[TUNER_PAD_RF_INPUT].sig_type = PAD_SIGNAL_ANALOG;
  659. t->pad[TUNER_PAD_OUTPUT].flags = MEDIA_PAD_FL_SOURCE;
  660. t->pad[TUNER_PAD_OUTPUT].sig_type = PAD_SIGNAL_ANALOG;
  661. t->pad[TUNER_PAD_AUD_OUT].flags = MEDIA_PAD_FL_SOURCE;
  662. t->pad[TUNER_PAD_AUD_OUT].sig_type = PAD_SIGNAL_AUDIO;
  663. ret = media_entity_pads_init(&t->sd.entity, TUNER_NUM_PADS,
  664. &t->pad[0]);
  665. t->sd.entity.function = MEDIA_ENT_F_TUNER;
  666. }
  667. if (ret < 0) {
  668. pr_err("failed to initialize media entity!\n");
  669. kfree(t);
  670. return ret;
  671. }
  672. #endif
  673. /* Sets a default mode */
  674. if (t->mode_mask & T_ANALOG_TV)
  675. t->mode = V4L2_TUNER_ANALOG_TV;
  676. else
  677. t->mode = V4L2_TUNER_RADIO;
  678. set_type(client, t->type, t->mode_mask, t->config, t->fe.callback);
  679. list_add_tail(&t->list, &tuner_list);
  680. pr_info("Tuner %d found with type(s)%s%s.\n",
  681. t->type,
  682. t->mode_mask & T_RADIO ? " Radio" : "",
  683. t->mode_mask & T_ANALOG_TV ? " TV" : "");
  684. return 0;
  685. }
  686. /**
  687. * tuner_remove - detaches a tuner
  688. *
  689. * @client: i2c_client descriptor
  690. */
  691. static int tuner_remove(struct i2c_client *client)
  692. {
  693. struct tuner *t = to_tuner(i2c_get_clientdata(client));
  694. v4l2_device_unregister_subdev(&t->sd);
  695. tuner_detach(&t->fe);
  696. t->fe.analog_demod_priv = NULL;
  697. list_del(&t->list);
  698. kfree(t);
  699. return 0;
  700. }
  701. /*
  702. * Functions to switch between Radio and TV
  703. *
  704. * A few cards have a separate I2C tuner for radio. Those routines
  705. * take care of switching between TV/Radio mode, filtering only the
  706. * commands that apply to the Radio or TV tuner.
  707. */
  708. /**
  709. * check_mode - Verify if tuner supports the requested mode
  710. * @t: a pointer to the module's internal struct_tuner
  711. * @mode: mode of the tuner, as defined by &enum v4l2_tuner_type.
  712. *
  713. * This function checks if the tuner is capable of tuning analog TV,
  714. * digital TV or radio, depending on what the caller wants. If the
  715. * tuner can't support that mode, it returns -EINVAL. Otherwise, it
  716. * returns 0.
  717. * This function is needed for boards that have a separate tuner for
  718. * radio (like devices with tea5767).
  719. *
  720. * NOTE: mt20xx uses V4L2_TUNER_DIGITAL_TV and calls set_tv_freq to
  721. * select a TV frequency. So, t_mode = T_ANALOG_TV could actually
  722. * be used to represent a Digital TV too.
  723. */
  724. static inline int check_mode(struct tuner *t, enum v4l2_tuner_type mode)
  725. {
  726. int t_mode;
  727. if (mode == V4L2_TUNER_RADIO)
  728. t_mode = T_RADIO;
  729. else
  730. t_mode = T_ANALOG_TV;
  731. if ((t_mode & t->mode_mask) == 0)
  732. return -EINVAL;
  733. return 0;
  734. }
  735. /**
  736. * set_mode - Switch tuner to other mode.
  737. * @t: a pointer to the module's internal struct_tuner
  738. * @mode: enum v4l2_type (radio or TV)
  739. *
  740. * If tuner doesn't support the needed mode (radio or TV), prints a
  741. * debug message and returns -EINVAL, changing its state to standby.
  742. * Otherwise, changes the mode and returns 0.
  743. */
  744. static int set_mode(struct tuner *t, enum v4l2_tuner_type mode)
  745. {
  746. struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
  747. if (mode != t->mode) {
  748. if (check_mode(t, mode) == -EINVAL) {
  749. dprintk("Tuner doesn't support mode %d. Putting tuner to sleep\n",
  750. mode);
  751. t->standby = true;
  752. if (analog_ops->standby)
  753. analog_ops->standby(&t->fe);
  754. return -EINVAL;
  755. }
  756. t->mode = mode;
  757. dprintk("Changing to mode %d\n", mode);
  758. }
  759. return 0;
  760. }
  761. /**
  762. * set_freq - Set the tuner to the desired frequency.
  763. * @t: a pointer to the module's internal struct_tuner
  764. * @freq: frequency to set (0 means to use the current frequency)
  765. */
  766. static void set_freq(struct tuner *t, unsigned int freq)
  767. {
  768. struct i2c_client *client = v4l2_get_subdevdata(&t->sd);
  769. if (t->mode == V4L2_TUNER_RADIO) {
  770. if (!freq)
  771. freq = t->radio_freq;
  772. set_radio_freq(client, freq);
  773. } else {
  774. if (!freq)
  775. freq = t->tv_freq;
  776. set_tv_freq(client, freq);
  777. }
  778. }
  779. /*
  780. * Functions that are specific for TV mode
  781. */
  782. /**
  783. * set_tv_freq - Set tuner frequency, freq in Units of 62.5 kHz = 1/16MHz
  784. *
  785. * @c: i2c_client descriptor
  786. * @freq: frequency
  787. */
  788. static void set_tv_freq(struct i2c_client *c, unsigned int freq)
  789. {
  790. struct tuner *t = to_tuner(i2c_get_clientdata(c));
  791. struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
  792. struct analog_parameters params = {
  793. .mode = t->mode,
  794. .audmode = t->audmode,
  795. .std = t->std
  796. };
  797. if (t->type == UNSET) {
  798. pr_warn("tuner type not set\n");
  799. return;
  800. }
  801. if (NULL == analog_ops->set_params) {
  802. pr_warn("Tuner has no way to set tv freq\n");
  803. return;
  804. }
  805. if (freq < tv_range[0] * 16 || freq > tv_range[1] * 16) {
  806. dprintk("TV freq (%d.%02d) out of range (%d-%d)\n",
  807. freq / 16, freq % 16 * 100 / 16, tv_range[0],
  808. tv_range[1]);
  809. /* V4L2 spec: if the freq is not possible then the closest
  810. possible value should be selected */
  811. if (freq < tv_range[0] * 16)
  812. freq = tv_range[0] * 16;
  813. else
  814. freq = tv_range[1] * 16;
  815. }
  816. params.frequency = freq;
  817. dprintk("tv freq set to %d.%02d\n",
  818. freq / 16, freq % 16 * 100 / 16);
  819. t->tv_freq = freq;
  820. t->standby = false;
  821. analog_ops->set_params(&t->fe, &params);
  822. }
  823. /**
  824. * tuner_fixup_std - force a given video standard variant
  825. *
  826. * @t: tuner internal struct
  827. * @std: TV standard
  828. *
  829. * A few devices or drivers have problem to detect some standard variations.
  830. * On other operational systems, the drivers generally have a per-country
  831. * code, and some logic to apply per-country hacks. V4L2 API doesn't provide
  832. * such hacks. Instead, it relies on a proper video standard selection from
  833. * the userspace application. However, as some apps are buggy, not allowing
  834. * to distinguish all video standard variations, a modprobe parameter can
  835. * be used to force a video standard match.
  836. */
  837. static v4l2_std_id tuner_fixup_std(struct tuner *t, v4l2_std_id std)
  838. {
  839. if (pal[0] != '-' && (std & V4L2_STD_PAL) == V4L2_STD_PAL) {
  840. switch (pal[0]) {
  841. case '6':
  842. return V4L2_STD_PAL_60;
  843. case 'b':
  844. case 'B':
  845. case 'g':
  846. case 'G':
  847. return V4L2_STD_PAL_BG;
  848. case 'i':
  849. case 'I':
  850. return V4L2_STD_PAL_I;
  851. case 'd':
  852. case 'D':
  853. case 'k':
  854. case 'K':
  855. return V4L2_STD_PAL_DK;
  856. case 'M':
  857. case 'm':
  858. return V4L2_STD_PAL_M;
  859. case 'N':
  860. case 'n':
  861. if (pal[1] == 'c' || pal[1] == 'C')
  862. return V4L2_STD_PAL_Nc;
  863. return V4L2_STD_PAL_N;
  864. default:
  865. pr_warn("pal= argument not recognised\n");
  866. break;
  867. }
  868. }
  869. if (secam[0] != '-' && (std & V4L2_STD_SECAM) == V4L2_STD_SECAM) {
  870. switch (secam[0]) {
  871. case 'b':
  872. case 'B':
  873. case 'g':
  874. case 'G':
  875. case 'h':
  876. case 'H':
  877. return V4L2_STD_SECAM_B |
  878. V4L2_STD_SECAM_G |
  879. V4L2_STD_SECAM_H;
  880. case 'd':
  881. case 'D':
  882. case 'k':
  883. case 'K':
  884. return V4L2_STD_SECAM_DK;
  885. case 'l':
  886. case 'L':
  887. if ((secam[1] == 'C') || (secam[1] == 'c'))
  888. return V4L2_STD_SECAM_LC;
  889. return V4L2_STD_SECAM_L;
  890. default:
  891. pr_warn("secam= argument not recognised\n");
  892. break;
  893. }
  894. }
  895. if (ntsc[0] != '-' && (std & V4L2_STD_NTSC) == V4L2_STD_NTSC) {
  896. switch (ntsc[0]) {
  897. case 'm':
  898. case 'M':
  899. return V4L2_STD_NTSC_M;
  900. case 'j':
  901. case 'J':
  902. return V4L2_STD_NTSC_M_JP;
  903. case 'k':
  904. case 'K':
  905. return V4L2_STD_NTSC_M_KR;
  906. default:
  907. pr_info("ntsc= argument not recognised\n");
  908. break;
  909. }
  910. }
  911. return std;
  912. }
  913. /*
  914. * Functions that are specific for Radio mode
  915. */
  916. /**
  917. * set_radio_freq - Set tuner frequency, freq in Units of 62.5 Hz = 1/16kHz
  918. *
  919. * @c: i2c_client descriptor
  920. * @freq: frequency
  921. */
  922. static void set_radio_freq(struct i2c_client *c, unsigned int freq)
  923. {
  924. struct tuner *t = to_tuner(i2c_get_clientdata(c));
  925. struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
  926. struct analog_parameters params = {
  927. .mode = t->mode,
  928. .audmode = t->audmode,
  929. .std = t->std
  930. };
  931. if (t->type == UNSET) {
  932. pr_warn("tuner type not set\n");
  933. return;
  934. }
  935. if (NULL == analog_ops->set_params) {
  936. pr_warn("tuner has no way to set radio frequency\n");
  937. return;
  938. }
  939. if (freq < radio_range[0] * 16000 || freq > radio_range[1] * 16000) {
  940. dprintk("radio freq (%d.%02d) out of range (%d-%d)\n",
  941. freq / 16000, freq % 16000 * 100 / 16000,
  942. radio_range[0], radio_range[1]);
  943. /* V4L2 spec: if the freq is not possible then the closest
  944. possible value should be selected */
  945. if (freq < radio_range[0] * 16000)
  946. freq = radio_range[0] * 16000;
  947. else
  948. freq = radio_range[1] * 16000;
  949. }
  950. params.frequency = freq;
  951. dprintk("radio freq set to %d.%02d\n",
  952. freq / 16000, freq % 16000 * 100 / 16000);
  953. t->radio_freq = freq;
  954. t->standby = false;
  955. analog_ops->set_params(&t->fe, &params);
  956. /*
  957. * The tuner driver might decide to change the audmode if it only
  958. * supports stereo, so update t->audmode.
  959. */
  960. t->audmode = params.audmode;
  961. }
  962. /*
  963. * Debug function for reporting tuner status to userspace
  964. */
  965. /**
  966. * tuner_status - Dumps the current tuner status at dmesg
  967. * @fe: pointer to struct dvb_frontend
  968. *
  969. * This callback is used only for driver debug purposes, answering to
  970. * VIDIOC_LOG_STATUS. No changes should happen on this call.
  971. */
  972. static void tuner_status(struct dvb_frontend *fe)
  973. {
  974. struct tuner *t = fe->analog_demod_priv;
  975. unsigned long freq, freq_fraction;
  976. struct dvb_tuner_ops *fe_tuner_ops = &fe->ops.tuner_ops;
  977. struct analog_demod_ops *analog_ops = &fe->ops.analog_ops;
  978. const char *p;
  979. switch (t->mode) {
  980. case V4L2_TUNER_RADIO:
  981. p = "radio";
  982. break;
  983. case V4L2_TUNER_DIGITAL_TV: /* Used by mt20xx */
  984. p = "digital TV";
  985. break;
  986. case V4L2_TUNER_ANALOG_TV:
  987. default:
  988. p = "analog TV";
  989. break;
  990. }
  991. if (t->mode == V4L2_TUNER_RADIO) {
  992. freq = t->radio_freq / 16000;
  993. freq_fraction = (t->radio_freq % 16000) * 100 / 16000;
  994. } else {
  995. freq = t->tv_freq / 16;
  996. freq_fraction = (t->tv_freq % 16) * 100 / 16;
  997. }
  998. pr_info("Tuner mode: %s%s\n", p,
  999. t->standby ? " on standby mode" : "");
  1000. pr_info("Frequency: %lu.%02lu MHz\n", freq, freq_fraction);
  1001. pr_info("Standard: 0x%08lx\n", (unsigned long)t->std);
  1002. if (t->mode != V4L2_TUNER_RADIO)
  1003. return;
  1004. if (fe_tuner_ops->get_status) {
  1005. u32 tuner_status;
  1006. fe_tuner_ops->get_status(&t->fe, &tuner_status);
  1007. if (tuner_status & TUNER_STATUS_LOCKED)
  1008. pr_info("Tuner is locked.\n");
  1009. if (tuner_status & TUNER_STATUS_STEREO)
  1010. pr_info("Stereo: yes\n");
  1011. }
  1012. if (analog_ops->has_signal) {
  1013. u16 signal;
  1014. if (!analog_ops->has_signal(fe, &signal))
  1015. pr_info("Signal strength: %hu\n", signal);
  1016. }
  1017. }
  1018. /*
  1019. * Function to splicitly change mode to radio. Probably not needed anymore
  1020. */
  1021. static int tuner_s_radio(struct v4l2_subdev *sd)
  1022. {
  1023. struct tuner *t = to_tuner(sd);
  1024. if (set_mode(t, V4L2_TUNER_RADIO) == 0)
  1025. set_freq(t, 0);
  1026. return 0;
  1027. }
  1028. /*
  1029. * Tuner callbacks to handle userspace ioctl's
  1030. */
  1031. /**
  1032. * tuner_standby - places the tuner in standby mode
  1033. * @sd: pointer to struct v4l2_subdev
  1034. */
  1035. static int tuner_standby(struct v4l2_subdev *sd)
  1036. {
  1037. struct tuner *t = to_tuner(sd);
  1038. struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
  1039. dprintk("Putting tuner to sleep\n");
  1040. t->standby = true;
  1041. if (analog_ops->standby)
  1042. analog_ops->standby(&t->fe);
  1043. return 0;
  1044. }
  1045. static int tuner_s_std(struct v4l2_subdev *sd, v4l2_std_id std)
  1046. {
  1047. struct tuner *t = to_tuner(sd);
  1048. if (set_mode(t, V4L2_TUNER_ANALOG_TV))
  1049. return 0;
  1050. t->std = tuner_fixup_std(t, std);
  1051. if (t->std != std)
  1052. dprintk("Fixup standard %llx to %llx\n", std, t->std);
  1053. set_freq(t, 0);
  1054. return 0;
  1055. }
  1056. static int tuner_s_frequency(struct v4l2_subdev *sd, const struct v4l2_frequency *f)
  1057. {
  1058. struct tuner *t = to_tuner(sd);
  1059. if (set_mode(t, f->type) == 0)
  1060. set_freq(t, f->frequency);
  1061. return 0;
  1062. }
  1063. /**
  1064. * tuner_g_frequency - Get the tuned frequency for the tuner
  1065. * @sd: pointer to struct v4l2_subdev
  1066. * @f: pointer to struct v4l2_frequency
  1067. *
  1068. * At return, the structure f will be filled with tuner frequency
  1069. * if the tuner matches the f->type.
  1070. * Note: f->type should be initialized before calling it.
  1071. * This is done by either video_ioctl2 or by the bridge driver.
  1072. */
  1073. static int tuner_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  1074. {
  1075. struct tuner *t = to_tuner(sd);
  1076. struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
  1077. if (check_mode(t, f->type) == -EINVAL)
  1078. return 0;
  1079. if (f->type == t->mode && fe_tuner_ops->get_frequency && !t->standby) {
  1080. u32 abs_freq;
  1081. fe_tuner_ops->get_frequency(&t->fe, &abs_freq);
  1082. f->frequency = (V4L2_TUNER_RADIO == t->mode) ?
  1083. DIV_ROUND_CLOSEST(abs_freq * 2, 125) :
  1084. DIV_ROUND_CLOSEST(abs_freq, 62500);
  1085. } else {
  1086. f->frequency = (V4L2_TUNER_RADIO == f->type) ?
  1087. t->radio_freq : t->tv_freq;
  1088. }
  1089. return 0;
  1090. }
  1091. /**
  1092. * tuner_g_tuner - Fill in tuner information
  1093. * @sd: pointer to struct v4l2_subdev
  1094. * @vt: pointer to struct v4l2_tuner
  1095. *
  1096. * At return, the structure vt will be filled with tuner information
  1097. * if the tuner matches vt->type.
  1098. * Note: vt->type should be initialized before calling it.
  1099. * This is done by either video_ioctl2 or by the bridge driver.
  1100. */
  1101. static int tuner_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *vt)
  1102. {
  1103. struct tuner *t = to_tuner(sd);
  1104. struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
  1105. struct dvb_tuner_ops *fe_tuner_ops = &t->fe.ops.tuner_ops;
  1106. if (check_mode(t, vt->type) == -EINVAL)
  1107. return 0;
  1108. if (vt->type == t->mode && analog_ops->get_afc)
  1109. analog_ops->get_afc(&t->fe, &vt->afc);
  1110. if (vt->type == t->mode && analog_ops->has_signal) {
  1111. u16 signal = (u16)vt->signal;
  1112. if (!analog_ops->has_signal(&t->fe, &signal))
  1113. vt->signal = signal;
  1114. }
  1115. if (vt->type != V4L2_TUNER_RADIO) {
  1116. vt->capability |= V4L2_TUNER_CAP_NORM;
  1117. vt->rangelow = tv_range[0] * 16;
  1118. vt->rangehigh = tv_range[1] * 16;
  1119. return 0;
  1120. }
  1121. /* radio mode */
  1122. if (vt->type == t->mode) {
  1123. vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO;
  1124. if (fe_tuner_ops->get_status) {
  1125. u32 tuner_status;
  1126. fe_tuner_ops->get_status(&t->fe, &tuner_status);
  1127. vt->rxsubchans =
  1128. (tuner_status & TUNER_STATUS_STEREO) ?
  1129. V4L2_TUNER_SUB_STEREO :
  1130. V4L2_TUNER_SUB_MONO;
  1131. }
  1132. vt->audmode = t->audmode;
  1133. }
  1134. vt->capability |= V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO;
  1135. vt->rangelow = radio_range[0] * 16000;
  1136. vt->rangehigh = radio_range[1] * 16000;
  1137. return 0;
  1138. }
  1139. /**
  1140. * tuner_s_tuner - Set the tuner's audio mode
  1141. * @sd: pointer to struct v4l2_subdev
  1142. * @vt: pointer to struct v4l2_tuner
  1143. *
  1144. * Sets the audio mode if the tuner matches vt->type.
  1145. * Note: vt->type should be initialized before calling it.
  1146. * This is done by either video_ioctl2 or by the bridge driver.
  1147. */
  1148. static int tuner_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *vt)
  1149. {
  1150. struct tuner *t = to_tuner(sd);
  1151. if (set_mode(t, vt->type))
  1152. return 0;
  1153. if (t->mode == V4L2_TUNER_RADIO) {
  1154. t->audmode = vt->audmode;
  1155. /*
  1156. * For radio audmode can only be mono or stereo. Map any
  1157. * other values to stereo. The actual tuner driver that is
  1158. * called in set_radio_freq can decide to limit the audmode to
  1159. * mono if only mono is supported.
  1160. */
  1161. if (t->audmode != V4L2_TUNER_MODE_MONO &&
  1162. t->audmode != V4L2_TUNER_MODE_STEREO)
  1163. t->audmode = V4L2_TUNER_MODE_STEREO;
  1164. }
  1165. set_freq(t, 0);
  1166. return 0;
  1167. }
  1168. static int tuner_log_status(struct v4l2_subdev *sd)
  1169. {
  1170. struct tuner *t = to_tuner(sd);
  1171. struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
  1172. if (analog_ops->tuner_status)
  1173. analog_ops->tuner_status(&t->fe);
  1174. return 0;
  1175. }
  1176. #ifdef CONFIG_PM_SLEEP
  1177. static int tuner_suspend(struct device *dev)
  1178. {
  1179. struct i2c_client *c = to_i2c_client(dev);
  1180. struct tuner *t = to_tuner(i2c_get_clientdata(c));
  1181. struct analog_demod_ops *analog_ops = &t->fe.ops.analog_ops;
  1182. dprintk("suspend\n");
  1183. if (t->fe.ops.tuner_ops.suspend)
  1184. t->fe.ops.tuner_ops.suspend(&t->fe);
  1185. else if (!t->standby && analog_ops->standby)
  1186. analog_ops->standby(&t->fe);
  1187. return 0;
  1188. }
  1189. static int tuner_resume(struct device *dev)
  1190. {
  1191. struct i2c_client *c = to_i2c_client(dev);
  1192. struct tuner *t = to_tuner(i2c_get_clientdata(c));
  1193. dprintk("resume\n");
  1194. if (t->fe.ops.tuner_ops.resume)
  1195. t->fe.ops.tuner_ops.resume(&t->fe);
  1196. else if (!t->standby)
  1197. if (set_mode(t, t->mode) == 0)
  1198. set_freq(t, 0);
  1199. return 0;
  1200. }
  1201. #endif
  1202. static int tuner_command(struct i2c_client *client, unsigned cmd, void *arg)
  1203. {
  1204. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  1205. /* TUNER_SET_CONFIG is still called by tuner-simple.c, so we have
  1206. to handle it here.
  1207. There must be a better way of doing this... */
  1208. switch (cmd) {
  1209. case TUNER_SET_CONFIG:
  1210. return tuner_s_config(sd, arg);
  1211. }
  1212. return -ENOIOCTLCMD;
  1213. }
  1214. /*
  1215. * Callback structs
  1216. */
  1217. static const struct v4l2_subdev_core_ops tuner_core_ops = {
  1218. .log_status = tuner_log_status,
  1219. };
  1220. static const struct v4l2_subdev_tuner_ops tuner_tuner_ops = {
  1221. .standby = tuner_standby,
  1222. .s_radio = tuner_s_radio,
  1223. .g_tuner = tuner_g_tuner,
  1224. .s_tuner = tuner_s_tuner,
  1225. .s_frequency = tuner_s_frequency,
  1226. .g_frequency = tuner_g_frequency,
  1227. .s_type_addr = tuner_s_type_addr,
  1228. .s_config = tuner_s_config,
  1229. };
  1230. static const struct v4l2_subdev_video_ops tuner_video_ops = {
  1231. .s_std = tuner_s_std,
  1232. };
  1233. static const struct v4l2_subdev_ops tuner_ops = {
  1234. .core = &tuner_core_ops,
  1235. .tuner = &tuner_tuner_ops,
  1236. .video = &tuner_video_ops,
  1237. };
  1238. /*
  1239. * I2C structs and module init functions
  1240. */
  1241. static const struct dev_pm_ops tuner_pm_ops = {
  1242. SET_SYSTEM_SLEEP_PM_OPS(tuner_suspend, tuner_resume)
  1243. };
  1244. static const struct i2c_device_id tuner_id[] = {
  1245. { "tuner", }, /* autodetect */
  1246. { }
  1247. };
  1248. MODULE_DEVICE_TABLE(i2c, tuner_id);
  1249. static struct i2c_driver tuner_driver = {
  1250. .driver = {
  1251. .name = "tuner",
  1252. .pm = &tuner_pm_ops,
  1253. },
  1254. .probe = tuner_probe,
  1255. .remove = tuner_remove,
  1256. .command = tuner_command,
  1257. .id_table = tuner_id,
  1258. };
  1259. module_i2c_driver(tuner_driver);
  1260. MODULE_DESCRIPTION("device driver for various TV and TV+FM radio tuners");
  1261. MODULE_AUTHOR("Ralph Metzler, Gerd Knorr, Gunther Mayer");
  1262. MODULE_LICENSE("GPL");