tuner-core.c 35 KB

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