tuner-core.c 35 KB

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