e4000.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Elonics E4000 silicon tuner driver
  4. *
  5. * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
  6. */
  7. #include "e4000_priv.h"
  8. static int e4000_init(struct e4000_dev *dev)
  9. {
  10. struct i2c_client *client = dev->client;
  11. int ret;
  12. dev_dbg(&client->dev, "\n");
  13. /* reset */
  14. ret = regmap_write(dev->regmap, 0x00, 0x01);
  15. if (ret)
  16. goto err;
  17. /* disable output clock */
  18. ret = regmap_write(dev->regmap, 0x06, 0x00);
  19. if (ret)
  20. goto err;
  21. ret = regmap_write(dev->regmap, 0x7a, 0x96);
  22. if (ret)
  23. goto err;
  24. /* configure gains */
  25. ret = regmap_bulk_write(dev->regmap, 0x7e, "\x01\xfe", 2);
  26. if (ret)
  27. goto err;
  28. ret = regmap_write(dev->regmap, 0x82, 0x00);
  29. if (ret)
  30. goto err;
  31. ret = regmap_write(dev->regmap, 0x24, 0x05);
  32. if (ret)
  33. goto err;
  34. ret = regmap_bulk_write(dev->regmap, 0x87, "\x20\x01", 2);
  35. if (ret)
  36. goto err;
  37. ret = regmap_bulk_write(dev->regmap, 0x9f, "\x7f\x07", 2);
  38. if (ret)
  39. goto err;
  40. /* DC offset control */
  41. ret = regmap_write(dev->regmap, 0x2d, 0x1f);
  42. if (ret)
  43. goto err;
  44. ret = regmap_bulk_write(dev->regmap, 0x70, "\x01\x01", 2);
  45. if (ret)
  46. goto err;
  47. /* gain control */
  48. ret = regmap_write(dev->regmap, 0x1a, 0x17);
  49. if (ret)
  50. goto err;
  51. ret = regmap_write(dev->regmap, 0x1f, 0x1a);
  52. if (ret)
  53. goto err;
  54. dev->active = true;
  55. return 0;
  56. err:
  57. dev_dbg(&client->dev, "failed=%d\n", ret);
  58. return ret;
  59. }
  60. static int e4000_sleep(struct e4000_dev *dev)
  61. {
  62. struct i2c_client *client = dev->client;
  63. int ret;
  64. dev_dbg(&client->dev, "\n");
  65. dev->active = false;
  66. ret = regmap_write(dev->regmap, 0x00, 0x00);
  67. if (ret)
  68. goto err;
  69. return 0;
  70. err:
  71. dev_dbg(&client->dev, "failed=%d\n", ret);
  72. return ret;
  73. }
  74. static int e4000_set_params(struct e4000_dev *dev)
  75. {
  76. struct i2c_client *client = dev->client;
  77. int ret, i;
  78. unsigned int div_n, k, k_cw, div_out;
  79. u64 f_vco;
  80. u8 buf[5], i_data[4], q_data[4];
  81. if (!dev->active) {
  82. dev_dbg(&client->dev, "tuner is sleeping\n");
  83. return 0;
  84. }
  85. /* gain control manual */
  86. ret = regmap_write(dev->regmap, 0x1a, 0x00);
  87. if (ret)
  88. goto err;
  89. /*
  90. * Fractional-N synthesizer
  91. *
  92. * +----------------------------+
  93. * v |
  94. * Fref +----+ +-------+ +------+ +---+
  95. * ------> | PD | --> | VCO | ------> | /N.F | <-- | K |
  96. * +----+ +-------+ +------+ +---+
  97. * |
  98. * |
  99. * v
  100. * +-------+ Fout
  101. * | /Rout | ------>
  102. * +-------+
  103. */
  104. for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) {
  105. if (dev->f_frequency <= e4000_pll_lut[i].freq)
  106. break;
  107. }
  108. if (i == ARRAY_SIZE(e4000_pll_lut)) {
  109. ret = -EINVAL;
  110. goto err;
  111. }
  112. #define F_REF dev->clk
  113. div_out = e4000_pll_lut[i].div_out;
  114. f_vco = (u64) dev->f_frequency * div_out;
  115. /* calculate PLL integer and fractional control word */
  116. div_n = div_u64_rem(f_vco, F_REF, &k);
  117. k_cw = div_u64((u64) k * 0x10000, F_REF);
  118. dev_dbg(&client->dev,
  119. "frequency=%u bandwidth=%u f_vco=%llu F_REF=%u div_n=%u k=%u k_cw=%04x div_out=%u\n",
  120. dev->f_frequency, dev->f_bandwidth, f_vco, F_REF, div_n, k,
  121. k_cw, div_out);
  122. buf[0] = div_n;
  123. buf[1] = (k_cw >> 0) & 0xff;
  124. buf[2] = (k_cw >> 8) & 0xff;
  125. buf[3] = 0x00;
  126. buf[4] = e4000_pll_lut[i].div_out_reg;
  127. ret = regmap_bulk_write(dev->regmap, 0x09, buf, 5);
  128. if (ret)
  129. goto err;
  130. /* LNA filter (RF filter) */
  131. for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) {
  132. if (dev->f_frequency <= e400_lna_filter_lut[i].freq)
  133. break;
  134. }
  135. if (i == ARRAY_SIZE(e400_lna_filter_lut)) {
  136. ret = -EINVAL;
  137. goto err;
  138. }
  139. ret = regmap_write(dev->regmap, 0x10, e400_lna_filter_lut[i].val);
  140. if (ret)
  141. goto err;
  142. /* IF filters */
  143. for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) {
  144. if (dev->f_bandwidth <= e4000_if_filter_lut[i].freq)
  145. break;
  146. }
  147. if (i == ARRAY_SIZE(e4000_if_filter_lut)) {
  148. ret = -EINVAL;
  149. goto err;
  150. }
  151. buf[0] = e4000_if_filter_lut[i].reg11_val;
  152. buf[1] = e4000_if_filter_lut[i].reg12_val;
  153. ret = regmap_bulk_write(dev->regmap, 0x11, buf, 2);
  154. if (ret)
  155. goto err;
  156. /* frequency band */
  157. for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) {
  158. if (dev->f_frequency <= e4000_band_lut[i].freq)
  159. break;
  160. }
  161. if (i == ARRAY_SIZE(e4000_band_lut)) {
  162. ret = -EINVAL;
  163. goto err;
  164. }
  165. ret = regmap_write(dev->regmap, 0x07, e4000_band_lut[i].reg07_val);
  166. if (ret)
  167. goto err;
  168. ret = regmap_write(dev->regmap, 0x78, e4000_band_lut[i].reg78_val);
  169. if (ret)
  170. goto err;
  171. /* DC offset */
  172. for (i = 0; i < 4; i++) {
  173. if (i == 0)
  174. ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7e\x24", 3);
  175. else if (i == 1)
  176. ret = regmap_bulk_write(dev->regmap, 0x15, "\x00\x7f", 2);
  177. else if (i == 2)
  178. ret = regmap_bulk_write(dev->regmap, 0x15, "\x01", 1);
  179. else
  180. ret = regmap_bulk_write(dev->regmap, 0x16, "\x7e", 1);
  181. if (ret)
  182. goto err;
  183. ret = regmap_write(dev->regmap, 0x29, 0x01);
  184. if (ret)
  185. goto err;
  186. ret = regmap_bulk_read(dev->regmap, 0x2a, buf, 3);
  187. if (ret)
  188. goto err;
  189. i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f);
  190. q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f);
  191. }
  192. swap(q_data[2], q_data[3]);
  193. swap(i_data[2], i_data[3]);
  194. ret = regmap_bulk_write(dev->regmap, 0x50, q_data, 4);
  195. if (ret)
  196. goto err;
  197. ret = regmap_bulk_write(dev->regmap, 0x60, i_data, 4);
  198. if (ret)
  199. goto err;
  200. /* gain control auto */
  201. ret = regmap_write(dev->regmap, 0x1a, 0x17);
  202. if (ret)
  203. goto err;
  204. return 0;
  205. err:
  206. dev_dbg(&client->dev, "failed=%d\n", ret);
  207. return ret;
  208. }
  209. /*
  210. * V4L2 API
  211. */
  212. #if IS_ENABLED(CONFIG_VIDEO_V4L2)
  213. static const struct v4l2_frequency_band bands[] = {
  214. {
  215. .type = V4L2_TUNER_RF,
  216. .index = 0,
  217. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  218. .rangelow = 59000000,
  219. .rangehigh = 1105000000,
  220. },
  221. {
  222. .type = V4L2_TUNER_RF,
  223. .index = 1,
  224. .capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS,
  225. .rangelow = 1249000000,
  226. .rangehigh = 2208000000UL,
  227. },
  228. };
  229. static inline struct e4000_dev *e4000_subdev_to_dev(struct v4l2_subdev *sd)
  230. {
  231. return container_of(sd, struct e4000_dev, sd);
  232. }
  233. static int e4000_standby(struct v4l2_subdev *sd)
  234. {
  235. struct e4000_dev *dev = e4000_subdev_to_dev(sd);
  236. int ret;
  237. ret = e4000_sleep(dev);
  238. if (ret)
  239. return ret;
  240. return e4000_set_params(dev);
  241. }
  242. static int e4000_g_tuner(struct v4l2_subdev *sd, struct v4l2_tuner *v)
  243. {
  244. struct e4000_dev *dev = e4000_subdev_to_dev(sd);
  245. struct i2c_client *client = dev->client;
  246. dev_dbg(&client->dev, "index=%d\n", v->index);
  247. strscpy(v->name, "Elonics E4000", sizeof(v->name));
  248. v->type = V4L2_TUNER_RF;
  249. v->capability = V4L2_TUNER_CAP_1HZ | V4L2_TUNER_CAP_FREQ_BANDS;
  250. v->rangelow = bands[0].rangelow;
  251. v->rangehigh = bands[1].rangehigh;
  252. return 0;
  253. }
  254. static int e4000_s_tuner(struct v4l2_subdev *sd, const struct v4l2_tuner *v)
  255. {
  256. struct e4000_dev *dev = e4000_subdev_to_dev(sd);
  257. struct i2c_client *client = dev->client;
  258. dev_dbg(&client->dev, "index=%d\n", v->index);
  259. return 0;
  260. }
  261. static int e4000_g_frequency(struct v4l2_subdev *sd, struct v4l2_frequency *f)
  262. {
  263. struct e4000_dev *dev = e4000_subdev_to_dev(sd);
  264. struct i2c_client *client = dev->client;
  265. dev_dbg(&client->dev, "tuner=%d\n", f->tuner);
  266. f->frequency = dev->f_frequency;
  267. return 0;
  268. }
  269. static int e4000_s_frequency(struct v4l2_subdev *sd,
  270. const struct v4l2_frequency *f)
  271. {
  272. struct e4000_dev *dev = e4000_subdev_to_dev(sd);
  273. struct i2c_client *client = dev->client;
  274. dev_dbg(&client->dev, "tuner=%d type=%d frequency=%u\n",
  275. f->tuner, f->type, f->frequency);
  276. dev->f_frequency = clamp_t(unsigned int, f->frequency,
  277. bands[0].rangelow, bands[1].rangehigh);
  278. return e4000_set_params(dev);
  279. }
  280. static int e4000_enum_freq_bands(struct v4l2_subdev *sd,
  281. struct v4l2_frequency_band *band)
  282. {
  283. struct e4000_dev *dev = e4000_subdev_to_dev(sd);
  284. struct i2c_client *client = dev->client;
  285. dev_dbg(&client->dev, "tuner=%d type=%d index=%d\n",
  286. band->tuner, band->type, band->index);
  287. if (band->index >= ARRAY_SIZE(bands))
  288. return -EINVAL;
  289. band->capability = bands[band->index].capability;
  290. band->rangelow = bands[band->index].rangelow;
  291. band->rangehigh = bands[band->index].rangehigh;
  292. return 0;
  293. }
  294. static const struct v4l2_subdev_tuner_ops e4000_subdev_tuner_ops = {
  295. .standby = e4000_standby,
  296. .g_tuner = e4000_g_tuner,
  297. .s_tuner = e4000_s_tuner,
  298. .g_frequency = e4000_g_frequency,
  299. .s_frequency = e4000_s_frequency,
  300. .enum_freq_bands = e4000_enum_freq_bands,
  301. };
  302. static const struct v4l2_subdev_ops e4000_subdev_ops = {
  303. .tuner = &e4000_subdev_tuner_ops,
  304. };
  305. static int e4000_set_lna_gain(struct dvb_frontend *fe)
  306. {
  307. struct e4000_dev *dev = fe->tuner_priv;
  308. struct i2c_client *client = dev->client;
  309. int ret;
  310. u8 u8tmp;
  311. dev_dbg(&client->dev, "lna auto=%d->%d val=%d->%d\n",
  312. dev->lna_gain_auto->cur.val, dev->lna_gain_auto->val,
  313. dev->lna_gain->cur.val, dev->lna_gain->val);
  314. if (dev->lna_gain_auto->val && dev->if_gain_auto->cur.val)
  315. u8tmp = 0x17;
  316. else if (dev->lna_gain_auto->val)
  317. u8tmp = 0x19;
  318. else if (dev->if_gain_auto->cur.val)
  319. u8tmp = 0x16;
  320. else
  321. u8tmp = 0x10;
  322. ret = regmap_write(dev->regmap, 0x1a, u8tmp);
  323. if (ret)
  324. goto err;
  325. if (dev->lna_gain_auto->val == false) {
  326. ret = regmap_write(dev->regmap, 0x14, dev->lna_gain->val);
  327. if (ret)
  328. goto err;
  329. }
  330. return 0;
  331. err:
  332. dev_dbg(&client->dev, "failed=%d\n", ret);
  333. return ret;
  334. }
  335. static int e4000_set_mixer_gain(struct dvb_frontend *fe)
  336. {
  337. struct e4000_dev *dev = fe->tuner_priv;
  338. struct i2c_client *client = dev->client;
  339. int ret;
  340. u8 u8tmp;
  341. dev_dbg(&client->dev, "mixer auto=%d->%d val=%d->%d\n",
  342. dev->mixer_gain_auto->cur.val, dev->mixer_gain_auto->val,
  343. dev->mixer_gain->cur.val, dev->mixer_gain->val);
  344. if (dev->mixer_gain_auto->val)
  345. u8tmp = 0x15;
  346. else
  347. u8tmp = 0x14;
  348. ret = regmap_write(dev->regmap, 0x20, u8tmp);
  349. if (ret)
  350. goto err;
  351. if (dev->mixer_gain_auto->val == false) {
  352. ret = regmap_write(dev->regmap, 0x15, dev->mixer_gain->val);
  353. if (ret)
  354. goto err;
  355. }
  356. return 0;
  357. err:
  358. dev_dbg(&client->dev, "failed=%d\n", ret);
  359. return ret;
  360. }
  361. static int e4000_set_if_gain(struct dvb_frontend *fe)
  362. {
  363. struct e4000_dev *dev = fe->tuner_priv;
  364. struct i2c_client *client = dev->client;
  365. int ret;
  366. u8 buf[2];
  367. u8 u8tmp;
  368. dev_dbg(&client->dev, "if auto=%d->%d val=%d->%d\n",
  369. dev->if_gain_auto->cur.val, dev->if_gain_auto->val,
  370. dev->if_gain->cur.val, dev->if_gain->val);
  371. if (dev->if_gain_auto->val && dev->lna_gain_auto->cur.val)
  372. u8tmp = 0x17;
  373. else if (dev->lna_gain_auto->cur.val)
  374. u8tmp = 0x19;
  375. else if (dev->if_gain_auto->val)
  376. u8tmp = 0x16;
  377. else
  378. u8tmp = 0x10;
  379. ret = regmap_write(dev->regmap, 0x1a, u8tmp);
  380. if (ret)
  381. goto err;
  382. if (dev->if_gain_auto->val == false) {
  383. buf[0] = e4000_if_gain_lut[dev->if_gain->val].reg16_val;
  384. buf[1] = e4000_if_gain_lut[dev->if_gain->val].reg17_val;
  385. ret = regmap_bulk_write(dev->regmap, 0x16, buf, 2);
  386. if (ret)
  387. goto err;
  388. }
  389. return 0;
  390. err:
  391. dev_dbg(&client->dev, "failed=%d\n", ret);
  392. return ret;
  393. }
  394. static int e4000_pll_lock(struct dvb_frontend *fe)
  395. {
  396. struct e4000_dev *dev = fe->tuner_priv;
  397. struct i2c_client *client = dev->client;
  398. int ret;
  399. unsigned int uitmp;
  400. ret = regmap_read(dev->regmap, 0x07, &uitmp);
  401. if (ret)
  402. goto err;
  403. dev->pll_lock->val = (uitmp & 0x01);
  404. return 0;
  405. err:
  406. dev_dbg(&client->dev, "failed=%d\n", ret);
  407. return ret;
  408. }
  409. static int e4000_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  410. {
  411. struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl);
  412. struct i2c_client *client = dev->client;
  413. int ret;
  414. if (!dev->active)
  415. return 0;
  416. switch (ctrl->id) {
  417. case V4L2_CID_RF_TUNER_PLL_LOCK:
  418. ret = e4000_pll_lock(dev->fe);
  419. break;
  420. default:
  421. dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n",
  422. ctrl->id, ctrl->name);
  423. ret = -EINVAL;
  424. }
  425. return ret;
  426. }
  427. static int e4000_s_ctrl(struct v4l2_ctrl *ctrl)
  428. {
  429. struct e4000_dev *dev = container_of(ctrl->handler, struct e4000_dev, hdl);
  430. struct i2c_client *client = dev->client;
  431. int ret;
  432. if (!dev->active)
  433. return 0;
  434. switch (ctrl->id) {
  435. case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
  436. case V4L2_CID_RF_TUNER_BANDWIDTH:
  437. /*
  438. * TODO: Auto logic does not work 100% correctly as tuner driver
  439. * do not have information to calculate maximum suitable
  440. * bandwidth. Calculating it is responsible of master driver.
  441. */
  442. dev->f_bandwidth = dev->bandwidth->val;
  443. ret = e4000_set_params(dev);
  444. break;
  445. case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
  446. case V4L2_CID_RF_TUNER_LNA_GAIN:
  447. ret = e4000_set_lna_gain(dev->fe);
  448. break;
  449. case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
  450. case V4L2_CID_RF_TUNER_MIXER_GAIN:
  451. ret = e4000_set_mixer_gain(dev->fe);
  452. break;
  453. case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
  454. case V4L2_CID_RF_TUNER_IF_GAIN:
  455. ret = e4000_set_if_gain(dev->fe);
  456. break;
  457. default:
  458. dev_dbg(&client->dev, "unknown ctrl: id=%d name=%s\n",
  459. ctrl->id, ctrl->name);
  460. ret = -EINVAL;
  461. }
  462. return ret;
  463. }
  464. static const struct v4l2_ctrl_ops e4000_ctrl_ops = {
  465. .g_volatile_ctrl = e4000_g_volatile_ctrl,
  466. .s_ctrl = e4000_s_ctrl,
  467. };
  468. #endif
  469. /*
  470. * DVB API
  471. */
  472. static int e4000_dvb_set_params(struct dvb_frontend *fe)
  473. {
  474. struct e4000_dev *dev = fe->tuner_priv;
  475. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  476. dev->f_frequency = c->frequency;
  477. dev->f_bandwidth = c->bandwidth_hz;
  478. return e4000_set_params(dev);
  479. }
  480. static int e4000_dvb_init(struct dvb_frontend *fe)
  481. {
  482. return e4000_init(fe->tuner_priv);
  483. }
  484. static int e4000_dvb_sleep(struct dvb_frontend *fe)
  485. {
  486. return e4000_sleep(fe->tuner_priv);
  487. }
  488. static int e4000_dvb_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  489. {
  490. *frequency = 0; /* Zero-IF */
  491. return 0;
  492. }
  493. static const struct dvb_tuner_ops e4000_dvb_tuner_ops = {
  494. .info = {
  495. .name = "Elonics E4000",
  496. .frequency_min_hz = 174 * MHz,
  497. .frequency_max_hz = 862 * MHz,
  498. },
  499. .init = e4000_dvb_init,
  500. .sleep = e4000_dvb_sleep,
  501. .set_params = e4000_dvb_set_params,
  502. .get_if_frequency = e4000_dvb_get_if_frequency,
  503. };
  504. static int e4000_probe(struct i2c_client *client,
  505. const struct i2c_device_id *id)
  506. {
  507. struct e4000_dev *dev;
  508. struct e4000_config *cfg = client->dev.platform_data;
  509. struct dvb_frontend *fe = cfg->fe;
  510. int ret;
  511. unsigned int uitmp;
  512. static const struct regmap_config regmap_config = {
  513. .reg_bits = 8,
  514. .val_bits = 8,
  515. };
  516. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  517. if (!dev) {
  518. ret = -ENOMEM;
  519. goto err;
  520. }
  521. dev->clk = cfg->clock;
  522. dev->client = client;
  523. dev->fe = cfg->fe;
  524. dev->regmap = devm_regmap_init_i2c(client, &regmap_config);
  525. if (IS_ERR(dev->regmap)) {
  526. ret = PTR_ERR(dev->regmap);
  527. goto err_kfree;
  528. }
  529. /* check if the tuner is there */
  530. ret = regmap_read(dev->regmap, 0x02, &uitmp);
  531. if (ret)
  532. goto err_kfree;
  533. dev_dbg(&client->dev, "chip id=%02x\n", uitmp);
  534. if (uitmp != 0x40) {
  535. ret = -ENODEV;
  536. goto err_kfree;
  537. }
  538. /* put sleep as chip seems to be in normal mode by default */
  539. ret = regmap_write(dev->regmap, 0x00, 0x00);
  540. if (ret)
  541. goto err_kfree;
  542. #if IS_ENABLED(CONFIG_VIDEO_V4L2)
  543. /* Register controls */
  544. v4l2_ctrl_handler_init(&dev->hdl, 9);
  545. dev->bandwidth_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
  546. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
  547. dev->bandwidth = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
  548. V4L2_CID_RF_TUNER_BANDWIDTH, 4300000, 11000000, 100000, 4300000);
  549. v4l2_ctrl_auto_cluster(2, &dev->bandwidth_auto, 0, false);
  550. dev->lna_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
  551. V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 1);
  552. dev->lna_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
  553. V4L2_CID_RF_TUNER_LNA_GAIN, 0, 15, 1, 10);
  554. v4l2_ctrl_auto_cluster(2, &dev->lna_gain_auto, 0, false);
  555. dev->mixer_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
  556. V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 1);
  557. dev->mixer_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
  558. V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
  559. v4l2_ctrl_auto_cluster(2, &dev->mixer_gain_auto, 0, false);
  560. dev->if_gain_auto = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
  561. V4L2_CID_RF_TUNER_IF_GAIN_AUTO, 0, 1, 1, 1);
  562. dev->if_gain = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
  563. V4L2_CID_RF_TUNER_IF_GAIN, 0, 54, 1, 0);
  564. v4l2_ctrl_auto_cluster(2, &dev->if_gain_auto, 0, false);
  565. dev->pll_lock = v4l2_ctrl_new_std(&dev->hdl, &e4000_ctrl_ops,
  566. V4L2_CID_RF_TUNER_PLL_LOCK, 0, 1, 1, 0);
  567. if (dev->hdl.error) {
  568. ret = dev->hdl.error;
  569. dev_err(&client->dev, "Could not initialize controls\n");
  570. v4l2_ctrl_handler_free(&dev->hdl);
  571. goto err_kfree;
  572. }
  573. dev->sd.ctrl_handler = &dev->hdl;
  574. dev->f_frequency = bands[0].rangelow;
  575. dev->f_bandwidth = dev->bandwidth->val;
  576. v4l2_i2c_subdev_init(&dev->sd, client, &e4000_subdev_ops);
  577. #endif
  578. fe->tuner_priv = dev;
  579. memcpy(&fe->ops.tuner_ops, &e4000_dvb_tuner_ops,
  580. sizeof(fe->ops.tuner_ops));
  581. v4l2_set_subdevdata(&dev->sd, client);
  582. i2c_set_clientdata(client, &dev->sd);
  583. dev_info(&client->dev, "Elonics E4000 successfully identified\n");
  584. return 0;
  585. err_kfree:
  586. kfree(dev);
  587. err:
  588. dev_dbg(&client->dev, "failed=%d\n", ret);
  589. return ret;
  590. }
  591. static int e4000_remove(struct i2c_client *client)
  592. {
  593. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  594. struct e4000_dev *dev = container_of(sd, struct e4000_dev, sd);
  595. dev_dbg(&client->dev, "\n");
  596. #if IS_ENABLED(CONFIG_VIDEO_V4L2)
  597. v4l2_ctrl_handler_free(&dev->hdl);
  598. #endif
  599. kfree(dev);
  600. return 0;
  601. }
  602. static const struct i2c_device_id e4000_id_table[] = {
  603. {"e4000", 0},
  604. {}
  605. };
  606. MODULE_DEVICE_TABLE(i2c, e4000_id_table);
  607. static struct i2c_driver e4000_driver = {
  608. .driver = {
  609. .name = "e4000",
  610. .suppress_bind_attrs = true,
  611. },
  612. .probe = e4000_probe,
  613. .remove = e4000_remove,
  614. .id_table = e4000_id_table,
  615. };
  616. module_i2c_driver(e4000_driver);
  617. MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
  618. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  619. MODULE_LICENSE("GPL");