tuner-core.c 36 KB

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