e4000.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. /*
  2. * Elonics E4000 silicon tuner driver
  3. *
  4. * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include "e4000_priv.h"
  21. #include <linux/math64.h>
  22. static int e4000_init(struct dvb_frontend *fe)
  23. {
  24. struct e4000 *s = fe->tuner_priv;
  25. int ret;
  26. dev_dbg(&s->client->dev, "\n");
  27. /* dummy I2C to ensure I2C wakes up */
  28. ret = regmap_write(s->regmap, 0x02, 0x40);
  29. /* reset */
  30. ret = regmap_write(s->regmap, 0x00, 0x01);
  31. if (ret)
  32. goto err;
  33. /* disable output clock */
  34. ret = regmap_write(s->regmap, 0x06, 0x00);
  35. if (ret)
  36. goto err;
  37. ret = regmap_write(s->regmap, 0x7a, 0x96);
  38. if (ret)
  39. goto err;
  40. /* configure gains */
  41. ret = regmap_bulk_write(s->regmap, 0x7e, "\x01\xfe", 2);
  42. if (ret)
  43. goto err;
  44. ret = regmap_write(s->regmap, 0x82, 0x00);
  45. if (ret)
  46. goto err;
  47. ret = regmap_write(s->regmap, 0x24, 0x05);
  48. if (ret)
  49. goto err;
  50. ret = regmap_bulk_write(s->regmap, 0x87, "\x20\x01", 2);
  51. if (ret)
  52. goto err;
  53. ret = regmap_bulk_write(s->regmap, 0x9f, "\x7f\x07", 2);
  54. if (ret)
  55. goto err;
  56. /* DC offset control */
  57. ret = regmap_write(s->regmap, 0x2d, 0x1f);
  58. if (ret)
  59. goto err;
  60. ret = regmap_bulk_write(s->regmap, 0x70, "\x01\x01", 2);
  61. if (ret)
  62. goto err;
  63. /* gain control */
  64. ret = regmap_write(s->regmap, 0x1a, 0x17);
  65. if (ret)
  66. goto err;
  67. ret = regmap_write(s->regmap, 0x1f, 0x1a);
  68. if (ret)
  69. goto err;
  70. s->active = true;
  71. err:
  72. if (ret)
  73. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  74. return ret;
  75. }
  76. static int e4000_sleep(struct dvb_frontend *fe)
  77. {
  78. struct e4000 *s = fe->tuner_priv;
  79. int ret;
  80. dev_dbg(&s->client->dev, "\n");
  81. s->active = false;
  82. ret = regmap_write(s->regmap, 0x00, 0x00);
  83. if (ret)
  84. goto err;
  85. err:
  86. if (ret)
  87. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  88. return ret;
  89. }
  90. static int e4000_set_params(struct dvb_frontend *fe)
  91. {
  92. struct e4000 *s = fe->tuner_priv;
  93. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  94. int ret, i, sigma_delta;
  95. unsigned int pll_n, pll_f;
  96. u64 f_vco;
  97. u8 buf[5], i_data[4], q_data[4];
  98. dev_dbg(&s->client->dev,
  99. "delivery_system=%d frequency=%u bandwidth_hz=%u\n",
  100. c->delivery_system, c->frequency, c->bandwidth_hz);
  101. /* gain control manual */
  102. ret = regmap_write(s->regmap, 0x1a, 0x00);
  103. if (ret)
  104. goto err;
  105. /* PLL */
  106. for (i = 0; i < ARRAY_SIZE(e4000_pll_lut); i++) {
  107. if (c->frequency <= e4000_pll_lut[i].freq)
  108. break;
  109. }
  110. if (i == ARRAY_SIZE(e4000_pll_lut)) {
  111. ret = -EINVAL;
  112. goto err;
  113. }
  114. f_vco = 1ull * c->frequency * e4000_pll_lut[i].mul;
  115. pll_n = div_u64_rem(f_vco, s->clock, &pll_f);
  116. sigma_delta = div_u64(0x10000ULL * pll_f, s->clock);
  117. buf[0] = pll_n;
  118. buf[1] = (sigma_delta >> 0) & 0xff;
  119. buf[2] = (sigma_delta >> 8) & 0xff;
  120. buf[3] = 0x00;
  121. buf[4] = e4000_pll_lut[i].div;
  122. dev_dbg(&s->client->dev, "f_vco=%llu pll div=%d sigma_delta=%04x\n",
  123. f_vco, buf[0], sigma_delta);
  124. ret = regmap_bulk_write(s->regmap, 0x09, buf, 5);
  125. if (ret)
  126. goto err;
  127. /* LNA filter (RF filter) */
  128. for (i = 0; i < ARRAY_SIZE(e400_lna_filter_lut); i++) {
  129. if (c->frequency <= e400_lna_filter_lut[i].freq)
  130. break;
  131. }
  132. if (i == ARRAY_SIZE(e400_lna_filter_lut)) {
  133. ret = -EINVAL;
  134. goto err;
  135. }
  136. ret = regmap_write(s->regmap, 0x10, e400_lna_filter_lut[i].val);
  137. if (ret)
  138. goto err;
  139. /* IF filters */
  140. for (i = 0; i < ARRAY_SIZE(e4000_if_filter_lut); i++) {
  141. if (c->bandwidth_hz <= e4000_if_filter_lut[i].freq)
  142. break;
  143. }
  144. if (i == ARRAY_SIZE(e4000_if_filter_lut)) {
  145. ret = -EINVAL;
  146. goto err;
  147. }
  148. buf[0] = e4000_if_filter_lut[i].reg11_val;
  149. buf[1] = e4000_if_filter_lut[i].reg12_val;
  150. ret = regmap_bulk_write(s->regmap, 0x11, buf, 2);
  151. if (ret)
  152. goto err;
  153. /* frequency band */
  154. for (i = 0; i < ARRAY_SIZE(e4000_band_lut); i++) {
  155. if (c->frequency <= e4000_band_lut[i].freq)
  156. break;
  157. }
  158. if (i == ARRAY_SIZE(e4000_band_lut)) {
  159. ret = -EINVAL;
  160. goto err;
  161. }
  162. ret = regmap_write(s->regmap, 0x07, e4000_band_lut[i].reg07_val);
  163. if (ret)
  164. goto err;
  165. ret = regmap_write(s->regmap, 0x78, e4000_band_lut[i].reg78_val);
  166. if (ret)
  167. goto err;
  168. /* DC offset */
  169. for (i = 0; i < 4; i++) {
  170. if (i == 0)
  171. ret = regmap_bulk_write(s->regmap, 0x15, "\x00\x7e\x24", 3);
  172. else if (i == 1)
  173. ret = regmap_bulk_write(s->regmap, 0x15, "\x00\x7f", 2);
  174. else if (i == 2)
  175. ret = regmap_bulk_write(s->regmap, 0x15, "\x01", 1);
  176. else
  177. ret = regmap_bulk_write(s->regmap, 0x16, "\x7e", 1);
  178. if (ret)
  179. goto err;
  180. ret = regmap_write(s->regmap, 0x29, 0x01);
  181. if (ret)
  182. goto err;
  183. ret = regmap_bulk_read(s->regmap, 0x2a, buf, 3);
  184. if (ret)
  185. goto err;
  186. i_data[i] = (((buf[2] >> 0) & 0x3) << 6) | (buf[0] & 0x3f);
  187. q_data[i] = (((buf[2] >> 4) & 0x3) << 6) | (buf[1] & 0x3f);
  188. }
  189. swap(q_data[2], q_data[3]);
  190. swap(i_data[2], i_data[3]);
  191. ret = regmap_bulk_write(s->regmap, 0x50, q_data, 4);
  192. if (ret)
  193. goto err;
  194. ret = regmap_bulk_write(s->regmap, 0x60, i_data, 4);
  195. if (ret)
  196. goto err;
  197. /* gain control auto */
  198. ret = regmap_write(s->regmap, 0x1a, 0x17);
  199. if (ret)
  200. goto err;
  201. err:
  202. if (ret)
  203. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  204. return ret;
  205. }
  206. static int e4000_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
  207. {
  208. struct e4000 *s = fe->tuner_priv;
  209. dev_dbg(&s->client->dev, "\n");
  210. *frequency = 0; /* Zero-IF */
  211. return 0;
  212. }
  213. #if IS_ENABLED(CONFIG_VIDEO_V4L2)
  214. static int e4000_set_lna_gain(struct dvb_frontend *fe)
  215. {
  216. struct e4000 *s = fe->tuner_priv;
  217. int ret;
  218. u8 u8tmp;
  219. dev_dbg(&s->client->dev, "lna auto=%d->%d val=%d->%d\n",
  220. s->lna_gain_auto->cur.val, s->lna_gain_auto->val,
  221. s->lna_gain->cur.val, s->lna_gain->val);
  222. if (s->lna_gain_auto->val && s->if_gain_auto->cur.val)
  223. u8tmp = 0x17;
  224. else if (s->lna_gain_auto->val)
  225. u8tmp = 0x19;
  226. else if (s->if_gain_auto->cur.val)
  227. u8tmp = 0x16;
  228. else
  229. u8tmp = 0x10;
  230. ret = regmap_write(s->regmap, 0x1a, u8tmp);
  231. if (ret)
  232. goto err;
  233. if (s->lna_gain_auto->val == false) {
  234. ret = regmap_write(s->regmap, 0x14, s->lna_gain->val);
  235. if (ret)
  236. goto err;
  237. }
  238. err:
  239. if (ret)
  240. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  241. return ret;
  242. }
  243. static int e4000_set_mixer_gain(struct dvb_frontend *fe)
  244. {
  245. struct e4000 *s = fe->tuner_priv;
  246. int ret;
  247. u8 u8tmp;
  248. dev_dbg(&s->client->dev, "mixer auto=%d->%d val=%d->%d\n",
  249. s->mixer_gain_auto->cur.val, s->mixer_gain_auto->val,
  250. s->mixer_gain->cur.val, s->mixer_gain->val);
  251. if (s->mixer_gain_auto->val)
  252. u8tmp = 0x15;
  253. else
  254. u8tmp = 0x14;
  255. ret = regmap_write(s->regmap, 0x20, u8tmp);
  256. if (ret)
  257. goto err;
  258. if (s->mixer_gain_auto->val == false) {
  259. ret = regmap_write(s->regmap, 0x15, s->mixer_gain->val);
  260. if (ret)
  261. goto err;
  262. }
  263. err:
  264. if (ret)
  265. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  266. return ret;
  267. }
  268. static int e4000_set_if_gain(struct dvb_frontend *fe)
  269. {
  270. struct e4000 *s = fe->tuner_priv;
  271. int ret;
  272. u8 buf[2];
  273. u8 u8tmp;
  274. dev_dbg(&s->client->dev, "if auto=%d->%d val=%d->%d\n",
  275. s->if_gain_auto->cur.val, s->if_gain_auto->val,
  276. s->if_gain->cur.val, s->if_gain->val);
  277. if (s->if_gain_auto->val && s->lna_gain_auto->cur.val)
  278. u8tmp = 0x17;
  279. else if (s->lna_gain_auto->cur.val)
  280. u8tmp = 0x19;
  281. else if (s->if_gain_auto->val)
  282. u8tmp = 0x16;
  283. else
  284. u8tmp = 0x10;
  285. ret = regmap_write(s->regmap, 0x1a, u8tmp);
  286. if (ret)
  287. goto err;
  288. if (s->if_gain_auto->val == false) {
  289. buf[0] = e4000_if_gain_lut[s->if_gain->val].reg16_val;
  290. buf[1] = e4000_if_gain_lut[s->if_gain->val].reg17_val;
  291. ret = regmap_bulk_write(s->regmap, 0x16, buf, 2);
  292. if (ret)
  293. goto err;
  294. }
  295. err:
  296. if (ret)
  297. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  298. return ret;
  299. }
  300. static int e4000_pll_lock(struct dvb_frontend *fe)
  301. {
  302. struct e4000 *s = fe->tuner_priv;
  303. int ret;
  304. unsigned int utmp;
  305. ret = regmap_read(s->regmap, 0x07, &utmp);
  306. if (ret)
  307. goto err;
  308. s->pll_lock->val = (utmp & 0x01);
  309. err:
  310. if (ret)
  311. dev_dbg(&s->client->dev, "failed=%d\n", ret);
  312. return ret;
  313. }
  314. static int e4000_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
  315. {
  316. struct e4000 *s = container_of(ctrl->handler, struct e4000, hdl);
  317. int ret;
  318. if (!s->active)
  319. return 0;
  320. switch (ctrl->id) {
  321. case V4L2_CID_RF_TUNER_PLL_LOCK:
  322. ret = e4000_pll_lock(s->fe);
  323. break;
  324. default:
  325. dev_dbg(&s->client->dev, "unknown ctrl: id=%d name=%s\n",
  326. ctrl->id, ctrl->name);
  327. ret = -EINVAL;
  328. }
  329. return ret;
  330. }
  331. static int e4000_s_ctrl(struct v4l2_ctrl *ctrl)
  332. {
  333. struct e4000 *s = container_of(ctrl->handler, struct e4000, hdl);
  334. struct dvb_frontend *fe = s->fe;
  335. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  336. int ret;
  337. if (!s->active)
  338. return 0;
  339. switch (ctrl->id) {
  340. case V4L2_CID_RF_TUNER_BANDWIDTH_AUTO:
  341. case V4L2_CID_RF_TUNER_BANDWIDTH:
  342. c->bandwidth_hz = s->bandwidth->val;
  343. ret = e4000_set_params(s->fe);
  344. break;
  345. case V4L2_CID_RF_TUNER_LNA_GAIN_AUTO:
  346. case V4L2_CID_RF_TUNER_LNA_GAIN:
  347. ret = e4000_set_lna_gain(s->fe);
  348. break;
  349. case V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO:
  350. case V4L2_CID_RF_TUNER_MIXER_GAIN:
  351. ret = e4000_set_mixer_gain(s->fe);
  352. break;
  353. case V4L2_CID_RF_TUNER_IF_GAIN_AUTO:
  354. case V4L2_CID_RF_TUNER_IF_GAIN:
  355. ret = e4000_set_if_gain(s->fe);
  356. break;
  357. default:
  358. dev_dbg(&s->client->dev, "unknown ctrl: id=%d name=%s\n",
  359. ctrl->id, ctrl->name);
  360. ret = -EINVAL;
  361. }
  362. return ret;
  363. }
  364. static const struct v4l2_ctrl_ops e4000_ctrl_ops = {
  365. .g_volatile_ctrl = e4000_g_volatile_ctrl,
  366. .s_ctrl = e4000_s_ctrl,
  367. };
  368. #endif
  369. static const struct dvb_tuner_ops e4000_tuner_ops = {
  370. .info = {
  371. .name = "Elonics E4000",
  372. .frequency_min = 174000000,
  373. .frequency_max = 862000000,
  374. },
  375. .init = e4000_init,
  376. .sleep = e4000_sleep,
  377. .set_params = e4000_set_params,
  378. .get_if_frequency = e4000_get_if_frequency,
  379. };
  380. /*
  381. * Use V4L2 subdev to carry V4L2 control handler, even we don't implement
  382. * subdev itself, just to avoid reinventing the wheel.
  383. */
  384. static int e4000_probe(struct i2c_client *client,
  385. const struct i2c_device_id *id)
  386. {
  387. struct e4000_config *cfg = client->dev.platform_data;
  388. struct dvb_frontend *fe = cfg->fe;
  389. struct e4000 *s;
  390. int ret;
  391. unsigned int utmp;
  392. static const struct regmap_config regmap_config = {
  393. .reg_bits = 8,
  394. .val_bits = 8,
  395. .max_register = 0xff,
  396. };
  397. s = kzalloc(sizeof(struct e4000), GFP_KERNEL);
  398. if (!s) {
  399. ret = -ENOMEM;
  400. dev_err(&client->dev, "kzalloc() failed\n");
  401. goto err;
  402. }
  403. s->clock = cfg->clock;
  404. s->client = client;
  405. s->fe = cfg->fe;
  406. s->regmap = devm_regmap_init_i2c(client, &regmap_config);
  407. if (IS_ERR(s->regmap)) {
  408. ret = PTR_ERR(s->regmap);
  409. goto err;
  410. }
  411. /* check if the tuner is there */
  412. ret = regmap_read(s->regmap, 0x02, &utmp);
  413. if (ret)
  414. goto err;
  415. dev_dbg(&s->client->dev, "chip id=%02x\n", utmp);
  416. if (utmp != 0x40) {
  417. ret = -ENODEV;
  418. goto err;
  419. }
  420. /* put sleep as chip seems to be in normal mode by default */
  421. ret = regmap_write(s->regmap, 0x00, 0x00);
  422. if (ret)
  423. goto err;
  424. #if IS_ENABLED(CONFIG_VIDEO_V4L2)
  425. /* Register controls */
  426. v4l2_ctrl_handler_init(&s->hdl, 9);
  427. s->bandwidth_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
  428. V4L2_CID_RF_TUNER_BANDWIDTH_AUTO, 0, 1, 1, 1);
  429. s->bandwidth = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
  430. V4L2_CID_RF_TUNER_BANDWIDTH, 4300000, 11000000, 100000, 4300000);
  431. v4l2_ctrl_auto_cluster(2, &s->bandwidth_auto, 0, false);
  432. s->lna_gain_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
  433. V4L2_CID_RF_TUNER_LNA_GAIN_AUTO, 0, 1, 1, 1);
  434. s->lna_gain = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
  435. V4L2_CID_RF_TUNER_LNA_GAIN, 0, 15, 1, 10);
  436. v4l2_ctrl_auto_cluster(2, &s->lna_gain_auto, 0, false);
  437. s->mixer_gain_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
  438. V4L2_CID_RF_TUNER_MIXER_GAIN_AUTO, 0, 1, 1, 1);
  439. s->mixer_gain = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
  440. V4L2_CID_RF_TUNER_MIXER_GAIN, 0, 1, 1, 1);
  441. v4l2_ctrl_auto_cluster(2, &s->mixer_gain_auto, 0, false);
  442. s->if_gain_auto = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
  443. V4L2_CID_RF_TUNER_IF_GAIN_AUTO, 0, 1, 1, 1);
  444. s->if_gain = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
  445. V4L2_CID_RF_TUNER_IF_GAIN, 0, 54, 1, 0);
  446. v4l2_ctrl_auto_cluster(2, &s->if_gain_auto, 0, false);
  447. s->pll_lock = v4l2_ctrl_new_std(&s->hdl, &e4000_ctrl_ops,
  448. V4L2_CID_RF_TUNER_PLL_LOCK, 0, 1, 1, 0);
  449. if (s->hdl.error) {
  450. ret = s->hdl.error;
  451. dev_err(&s->client->dev, "Could not initialize controls\n");
  452. v4l2_ctrl_handler_free(&s->hdl);
  453. goto err;
  454. }
  455. s->sd.ctrl_handler = &s->hdl;
  456. #endif
  457. dev_info(&s->client->dev, "Elonics E4000 successfully identified\n");
  458. fe->tuner_priv = s;
  459. memcpy(&fe->ops.tuner_ops, &e4000_tuner_ops,
  460. sizeof(struct dvb_tuner_ops));
  461. v4l2_set_subdevdata(&s->sd, client);
  462. i2c_set_clientdata(client, &s->sd);
  463. return 0;
  464. err:
  465. if (ret) {
  466. dev_dbg(&client->dev, "failed=%d\n", ret);
  467. kfree(s);
  468. }
  469. return ret;
  470. }
  471. static int e4000_remove(struct i2c_client *client)
  472. {
  473. struct v4l2_subdev *sd = i2c_get_clientdata(client);
  474. struct e4000 *s = container_of(sd, struct e4000, sd);
  475. struct dvb_frontend *fe = s->fe;
  476. dev_dbg(&client->dev, "\n");
  477. #if IS_ENABLED(CONFIG_VIDEO_V4L2)
  478. v4l2_ctrl_handler_free(&s->hdl);
  479. #endif
  480. memset(&fe->ops.tuner_ops, 0, sizeof(struct dvb_tuner_ops));
  481. fe->tuner_priv = NULL;
  482. kfree(s);
  483. return 0;
  484. }
  485. static const struct i2c_device_id e4000_id[] = {
  486. {"e4000", 0},
  487. {}
  488. };
  489. MODULE_DEVICE_TABLE(i2c, e4000_id);
  490. static struct i2c_driver e4000_driver = {
  491. .driver = {
  492. .owner = THIS_MODULE,
  493. .name = "e4000",
  494. },
  495. .probe = e4000_probe,
  496. .remove = e4000_remove,
  497. .id_table = e4000_id,
  498. };
  499. module_i2c_driver(e4000_driver);
  500. MODULE_DESCRIPTION("Elonics E4000 silicon tuner driver");
  501. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  502. MODULE_LICENSE("GPL");