tuner-core.c 34 KB

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