tuner-core.c 37 KB

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