rtl2830.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929
  1. /*
  2. * Realtek RTL2830 DVB-T demodulator driver
  3. *
  4. * Copyright (C) 2011 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. */
  17. #include "rtl2830_priv.h"
  18. /* Our regmap is bypassing I2C adapter lock, thus we do it! */
  19. static int rtl2830_bulk_write(struct i2c_client *client, unsigned int reg,
  20. const void *val, size_t val_count)
  21. {
  22. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  23. int ret;
  24. i2c_lock_adapter(client->adapter);
  25. ret = regmap_bulk_write(dev->regmap, reg, val, val_count);
  26. i2c_unlock_adapter(client->adapter);
  27. return ret;
  28. }
  29. static int rtl2830_update_bits(struct i2c_client *client, unsigned int reg,
  30. unsigned int mask, unsigned int val)
  31. {
  32. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  33. int ret;
  34. i2c_lock_adapter(client->adapter);
  35. ret = regmap_update_bits(dev->regmap, reg, mask, val);
  36. i2c_unlock_adapter(client->adapter);
  37. return ret;
  38. }
  39. static int rtl2830_bulk_read(struct i2c_client *client, unsigned int reg,
  40. void *val, size_t val_count)
  41. {
  42. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  43. int ret;
  44. i2c_lock_adapter(client->adapter);
  45. ret = regmap_bulk_read(dev->regmap, reg, val, val_count);
  46. i2c_unlock_adapter(client->adapter);
  47. return ret;
  48. }
  49. static int rtl2830_init(struct dvb_frontend *fe)
  50. {
  51. struct i2c_client *client = fe->demodulator_priv;
  52. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  53. struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
  54. int ret, i;
  55. struct rtl2830_reg_val_mask tab[] = {
  56. {0x00d, 0x01, 0x03},
  57. {0x00d, 0x10, 0x10},
  58. {0x104, 0x00, 0x1e},
  59. {0x105, 0x80, 0x80},
  60. {0x110, 0x02, 0x03},
  61. {0x110, 0x08, 0x0c},
  62. {0x17b, 0x00, 0x40},
  63. {0x17d, 0x05, 0x0f},
  64. {0x17d, 0x50, 0xf0},
  65. {0x18c, 0x08, 0x0f},
  66. {0x18d, 0x00, 0xc0},
  67. {0x188, 0x05, 0x0f},
  68. {0x189, 0x00, 0xfc},
  69. {0x2d5, 0x02, 0x02},
  70. {0x2f1, 0x02, 0x06},
  71. {0x2f1, 0x20, 0xf8},
  72. {0x16d, 0x00, 0x01},
  73. {0x1a6, 0x00, 0x80},
  74. {0x106, dev->pdata->vtop, 0x3f},
  75. {0x107, dev->pdata->krf, 0x3f},
  76. {0x112, 0x28, 0xff},
  77. {0x103, dev->pdata->agc_targ_val, 0xff},
  78. {0x00a, 0x02, 0x07},
  79. {0x140, 0x0c, 0x3c},
  80. {0x140, 0x40, 0xc0},
  81. {0x15b, 0x05, 0x07},
  82. {0x15b, 0x28, 0x38},
  83. {0x15c, 0x05, 0x07},
  84. {0x15c, 0x28, 0x38},
  85. {0x115, dev->pdata->spec_inv, 0x01},
  86. {0x16f, 0x01, 0x07},
  87. {0x170, 0x18, 0x38},
  88. {0x172, 0x0f, 0x0f},
  89. {0x173, 0x08, 0x38},
  90. {0x175, 0x01, 0x07},
  91. {0x176, 0x00, 0xc0},
  92. };
  93. for (i = 0; i < ARRAY_SIZE(tab); i++) {
  94. ret = rtl2830_update_bits(client, tab[i].reg, tab[i].mask,
  95. tab[i].val);
  96. if (ret)
  97. goto err;
  98. }
  99. ret = rtl2830_bulk_write(client, 0x18f, "\x28\x00", 2);
  100. if (ret)
  101. goto err;
  102. ret = rtl2830_bulk_write(client, 0x195,
  103. "\x04\x06\x0a\x12\x0a\x12\x1e\x28", 8);
  104. if (ret)
  105. goto err;
  106. /* TODO: spec init */
  107. /* soft reset */
  108. ret = rtl2830_update_bits(client, 0x101, 0x04, 0x04);
  109. if (ret)
  110. goto err;
  111. ret = rtl2830_update_bits(client, 0x101, 0x04, 0x00);
  112. if (ret)
  113. goto err;
  114. /* init stats here in order signal app which stats are supported */
  115. c->strength.len = 1;
  116. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  117. c->cnr.len = 1;
  118. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  119. c->post_bit_error.len = 1;
  120. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  121. c->post_bit_count.len = 1;
  122. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  123. /* start statistics polling */
  124. schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
  125. dev->sleeping = false;
  126. return ret;
  127. err:
  128. dev_dbg(&client->dev, "failed=%d\n", ret);
  129. return ret;
  130. }
  131. static int rtl2830_sleep(struct dvb_frontend *fe)
  132. {
  133. struct i2c_client *client = fe->demodulator_priv;
  134. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  135. dev->sleeping = true;
  136. /* stop statistics polling */
  137. cancel_delayed_work_sync(&dev->stat_work);
  138. dev->fe_status = 0;
  139. return 0;
  140. }
  141. static int rtl2830_get_tune_settings(struct dvb_frontend *fe,
  142. struct dvb_frontend_tune_settings *s)
  143. {
  144. s->min_delay_ms = 500;
  145. s->step_size = fe->ops.info.frequency_stepsize * 2;
  146. s->max_drift = (fe->ops.info.frequency_stepsize * 2) + 1;
  147. return 0;
  148. }
  149. static int rtl2830_set_frontend(struct dvb_frontend *fe)
  150. {
  151. struct i2c_client *client = fe->demodulator_priv;
  152. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  153. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  154. int ret, i;
  155. u64 num;
  156. u8 buf[3], u8tmp;
  157. u32 if_ctl, if_frequency;
  158. static const u8 bw_params1[3][34] = {
  159. {
  160. 0x1f, 0xf0, 0x1f, 0xf0, 0x1f, 0xfa, 0x00, 0x17, 0x00, 0x41,
  161. 0x00, 0x64, 0x00, 0x67, 0x00, 0x38, 0x1f, 0xde, 0x1f, 0x7a,
  162. 0x1f, 0x47, 0x1f, 0x7c, 0x00, 0x30, 0x01, 0x4b, 0x02, 0x82,
  163. 0x03, 0x73, 0x03, 0xcf, /* 6 MHz */
  164. }, {
  165. 0x1f, 0xfa, 0x1f, 0xda, 0x1f, 0xc1, 0x1f, 0xb3, 0x1f, 0xca,
  166. 0x00, 0x07, 0x00, 0x4d, 0x00, 0x6d, 0x00, 0x40, 0x1f, 0xca,
  167. 0x1f, 0x4d, 0x1f, 0x2a, 0x1f, 0xb2, 0x00, 0xec, 0x02, 0x7e,
  168. 0x03, 0xd0, 0x04, 0x53, /* 7 MHz */
  169. }, {
  170. 0x00, 0x10, 0x00, 0x0e, 0x1f, 0xf7, 0x1f, 0xc9, 0x1f, 0xa0,
  171. 0x1f, 0xa6, 0x1f, 0xec, 0x00, 0x4e, 0x00, 0x7d, 0x00, 0x3a,
  172. 0x1f, 0x98, 0x1f, 0x10, 0x1f, 0x40, 0x00, 0x75, 0x02, 0x5f,
  173. 0x04, 0x24, 0x04, 0xdb, /* 8 MHz */
  174. },
  175. };
  176. static const u8 bw_params2[3][6] = {
  177. {0xc3, 0x0c, 0x44, 0x33, 0x33, 0x30}, /* 6 MHz */
  178. {0xb8, 0xe3, 0x93, 0x99, 0x99, 0x98}, /* 7 MHz */
  179. {0xae, 0xba, 0xf3, 0x26, 0x66, 0x64}, /* 8 MHz */
  180. };
  181. dev_dbg(&client->dev, "frequency=%u bandwidth_hz=%u inversion=%u\n",
  182. c->frequency, c->bandwidth_hz, c->inversion);
  183. /* program tuner */
  184. if (fe->ops.tuner_ops.set_params)
  185. fe->ops.tuner_ops.set_params(fe);
  186. switch (c->bandwidth_hz) {
  187. case 6000000:
  188. i = 0;
  189. break;
  190. case 7000000:
  191. i = 1;
  192. break;
  193. case 8000000:
  194. i = 2;
  195. break;
  196. default:
  197. dev_err(&client->dev, "invalid bandwidth_hz %u\n",
  198. c->bandwidth_hz);
  199. return -EINVAL;
  200. }
  201. ret = rtl2830_update_bits(client, 0x008, 0x06, i << 1);
  202. if (ret)
  203. goto err;
  204. /* program if frequency */
  205. if (fe->ops.tuner_ops.get_if_frequency)
  206. ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
  207. else
  208. ret = -EINVAL;
  209. if (ret)
  210. goto err;
  211. num = if_frequency % dev->pdata->clk;
  212. num *= 0x400000;
  213. num = div_u64(num, dev->pdata->clk);
  214. num = -num;
  215. if_ctl = num & 0x3fffff;
  216. dev_dbg(&client->dev, "if_frequency=%d if_ctl=%08x\n",
  217. if_frequency, if_ctl);
  218. buf[0] = (if_ctl >> 16) & 0x3f;
  219. buf[1] = (if_ctl >> 8) & 0xff;
  220. buf[2] = (if_ctl >> 0) & 0xff;
  221. ret = rtl2830_bulk_read(client, 0x119, &u8tmp, 1);
  222. if (ret)
  223. goto err;
  224. buf[0] |= u8tmp & 0xc0; /* [7:6] */
  225. ret = rtl2830_bulk_write(client, 0x119, buf, 3);
  226. if (ret)
  227. goto err;
  228. /* 1/2 split I2C write */
  229. ret = rtl2830_bulk_write(client, 0x11c, &bw_params1[i][0], 17);
  230. if (ret)
  231. goto err;
  232. /* 2/2 split I2C write */
  233. ret = rtl2830_bulk_write(client, 0x12d, &bw_params1[i][17], 17);
  234. if (ret)
  235. goto err;
  236. ret = rtl2830_bulk_write(client, 0x19d, bw_params2[i], 6);
  237. if (ret)
  238. goto err;
  239. return ret;
  240. err:
  241. dev_dbg(&client->dev, "failed=%d\n", ret);
  242. return ret;
  243. }
  244. static int rtl2830_get_frontend(struct dvb_frontend *fe)
  245. {
  246. struct i2c_client *client = fe->demodulator_priv;
  247. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  248. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  249. int ret;
  250. u8 buf[3];
  251. if (dev->sleeping)
  252. return 0;
  253. ret = rtl2830_bulk_read(client, 0x33c, buf, 2);
  254. if (ret)
  255. goto err;
  256. ret = rtl2830_bulk_read(client, 0x351, &buf[2], 1);
  257. if (ret)
  258. goto err;
  259. dev_dbg(&client->dev, "TPS=%*ph\n", 3, buf);
  260. switch ((buf[0] >> 2) & 3) {
  261. case 0:
  262. c->modulation = QPSK;
  263. break;
  264. case 1:
  265. c->modulation = QAM_16;
  266. break;
  267. case 2:
  268. c->modulation = QAM_64;
  269. break;
  270. }
  271. switch ((buf[2] >> 2) & 1) {
  272. case 0:
  273. c->transmission_mode = TRANSMISSION_MODE_2K;
  274. break;
  275. case 1:
  276. c->transmission_mode = TRANSMISSION_MODE_8K;
  277. }
  278. switch ((buf[2] >> 0) & 3) {
  279. case 0:
  280. c->guard_interval = GUARD_INTERVAL_1_32;
  281. break;
  282. case 1:
  283. c->guard_interval = GUARD_INTERVAL_1_16;
  284. break;
  285. case 2:
  286. c->guard_interval = GUARD_INTERVAL_1_8;
  287. break;
  288. case 3:
  289. c->guard_interval = GUARD_INTERVAL_1_4;
  290. break;
  291. }
  292. switch ((buf[0] >> 4) & 7) {
  293. case 0:
  294. c->hierarchy = HIERARCHY_NONE;
  295. break;
  296. case 1:
  297. c->hierarchy = HIERARCHY_1;
  298. break;
  299. case 2:
  300. c->hierarchy = HIERARCHY_2;
  301. break;
  302. case 3:
  303. c->hierarchy = HIERARCHY_4;
  304. break;
  305. }
  306. switch ((buf[1] >> 3) & 7) {
  307. case 0:
  308. c->code_rate_HP = FEC_1_2;
  309. break;
  310. case 1:
  311. c->code_rate_HP = FEC_2_3;
  312. break;
  313. case 2:
  314. c->code_rate_HP = FEC_3_4;
  315. break;
  316. case 3:
  317. c->code_rate_HP = FEC_5_6;
  318. break;
  319. case 4:
  320. c->code_rate_HP = FEC_7_8;
  321. break;
  322. }
  323. switch ((buf[1] >> 0) & 7) {
  324. case 0:
  325. c->code_rate_LP = FEC_1_2;
  326. break;
  327. case 1:
  328. c->code_rate_LP = FEC_2_3;
  329. break;
  330. case 2:
  331. c->code_rate_LP = FEC_3_4;
  332. break;
  333. case 3:
  334. c->code_rate_LP = FEC_5_6;
  335. break;
  336. case 4:
  337. c->code_rate_LP = FEC_7_8;
  338. break;
  339. }
  340. return 0;
  341. err:
  342. dev_dbg(&client->dev, "failed=%d\n", ret);
  343. return ret;
  344. }
  345. static int rtl2830_read_status(struct dvb_frontend *fe, enum fe_status *status)
  346. {
  347. struct i2c_client *client = fe->demodulator_priv;
  348. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  349. int ret;
  350. u8 u8tmp;
  351. *status = 0;
  352. if (dev->sleeping)
  353. return 0;
  354. ret = rtl2830_bulk_read(client, 0x351, &u8tmp, 1);
  355. if (ret)
  356. goto err;
  357. u8tmp = (u8tmp >> 3) & 0x0f; /* [6:3] */
  358. if (u8tmp == 11) {
  359. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  360. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  361. } else if (u8tmp == 10) {
  362. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  363. FE_HAS_VITERBI;
  364. }
  365. dev->fe_status = *status;
  366. return ret;
  367. err:
  368. dev_dbg(&client->dev, "failed=%d\n", ret);
  369. return ret;
  370. }
  371. static int rtl2830_read_snr(struct dvb_frontend *fe, u16 *snr)
  372. {
  373. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  374. if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
  375. *snr = div_s64(c->cnr.stat[0].svalue, 100);
  376. else
  377. *snr = 0;
  378. return 0;
  379. }
  380. static int rtl2830_read_ber(struct dvb_frontend *fe, u32 *ber)
  381. {
  382. struct i2c_client *client = fe->demodulator_priv;
  383. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  384. *ber = (dev->post_bit_error - dev->post_bit_error_prev);
  385. dev->post_bit_error_prev = dev->post_bit_error;
  386. return 0;
  387. }
  388. static int rtl2830_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  389. {
  390. *ucblocks = 0;
  391. return 0;
  392. }
  393. static int rtl2830_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  394. {
  395. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  396. if (c->strength.stat[0].scale == FE_SCALE_RELATIVE)
  397. *strength = c->strength.stat[0].uvalue;
  398. else
  399. *strength = 0;
  400. return 0;
  401. }
  402. static struct dvb_frontend_ops rtl2830_ops = {
  403. .delsys = {SYS_DVBT},
  404. .info = {
  405. .name = "Realtek RTL2830 (DVB-T)",
  406. .caps = FE_CAN_FEC_1_2 |
  407. FE_CAN_FEC_2_3 |
  408. FE_CAN_FEC_3_4 |
  409. FE_CAN_FEC_5_6 |
  410. FE_CAN_FEC_7_8 |
  411. FE_CAN_FEC_AUTO |
  412. FE_CAN_QPSK |
  413. FE_CAN_QAM_16 |
  414. FE_CAN_QAM_64 |
  415. FE_CAN_QAM_AUTO |
  416. FE_CAN_TRANSMISSION_MODE_AUTO |
  417. FE_CAN_GUARD_INTERVAL_AUTO |
  418. FE_CAN_HIERARCHY_AUTO |
  419. FE_CAN_RECOVER |
  420. FE_CAN_MUTE_TS
  421. },
  422. .init = rtl2830_init,
  423. .sleep = rtl2830_sleep,
  424. .get_tune_settings = rtl2830_get_tune_settings,
  425. .set_frontend = rtl2830_set_frontend,
  426. .get_frontend = rtl2830_get_frontend,
  427. .read_status = rtl2830_read_status,
  428. .read_snr = rtl2830_read_snr,
  429. .read_ber = rtl2830_read_ber,
  430. .read_ucblocks = rtl2830_read_ucblocks,
  431. .read_signal_strength = rtl2830_read_signal_strength,
  432. };
  433. static void rtl2830_stat_work(struct work_struct *work)
  434. {
  435. struct rtl2830_dev *dev = container_of(work, struct rtl2830_dev, stat_work.work);
  436. struct i2c_client *client = dev->client;
  437. struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
  438. int ret, tmp;
  439. u8 u8tmp, buf[2];
  440. u16 u16tmp;
  441. dev_dbg(&client->dev, "\n");
  442. /* signal strength */
  443. if (dev->fe_status & FE_HAS_SIGNAL) {
  444. struct {signed int x:14; } s;
  445. /* read IF AGC */
  446. ret = rtl2830_bulk_read(client, 0x359, buf, 2);
  447. if (ret)
  448. goto err;
  449. u16tmp = buf[0] << 8 | buf[1] << 0;
  450. u16tmp &= 0x3fff; /* [13:0] */
  451. tmp = s.x = u16tmp; /* 14-bit bin to 2 complement */
  452. u16tmp = clamp_val(-4 * tmp + 32767, 0x0000, 0xffff);
  453. dev_dbg(&client->dev, "IF AGC=%d\n", tmp);
  454. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  455. c->strength.stat[0].uvalue = u16tmp;
  456. } else {
  457. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  458. }
  459. /* CNR */
  460. if (dev->fe_status & FE_HAS_VITERBI) {
  461. unsigned hierarchy, constellation;
  462. #define CONSTELLATION_NUM 3
  463. #define HIERARCHY_NUM 4
  464. static const u32 constant[CONSTELLATION_NUM][HIERARCHY_NUM] = {
  465. {70705899, 70705899, 70705899, 70705899},
  466. {82433173, 82433173, 87483115, 94445660},
  467. {92888734, 92888734, 95487525, 99770748},
  468. };
  469. ret = rtl2830_bulk_read(client, 0x33c, &u8tmp, 1);
  470. if (ret)
  471. goto err;
  472. constellation = (u8tmp >> 2) & 0x03; /* [3:2] */
  473. if (constellation > CONSTELLATION_NUM - 1)
  474. goto err_schedule_delayed_work;
  475. hierarchy = (u8tmp >> 4) & 0x07; /* [6:4] */
  476. if (hierarchy > HIERARCHY_NUM - 1)
  477. goto err_schedule_delayed_work;
  478. ret = rtl2830_bulk_read(client, 0x40c, buf, 2);
  479. if (ret)
  480. goto err;
  481. u16tmp = buf[0] << 8 | buf[1] << 0;
  482. if (u16tmp)
  483. tmp = (constant[constellation][hierarchy] -
  484. intlog10(u16tmp)) / ((1 << 24) / 10000);
  485. else
  486. tmp = 0;
  487. dev_dbg(&client->dev, "CNR raw=%u\n", u16tmp);
  488. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  489. c->cnr.stat[0].svalue = tmp;
  490. } else {
  491. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  492. }
  493. /* BER */
  494. if (dev->fe_status & FE_HAS_LOCK) {
  495. ret = rtl2830_bulk_read(client, 0x34e, buf, 2);
  496. if (ret)
  497. goto err;
  498. u16tmp = buf[0] << 8 | buf[1] << 0;
  499. dev->post_bit_error += u16tmp;
  500. dev->post_bit_count += 1000000;
  501. dev_dbg(&client->dev, "BER errors=%u total=1000000\n", u16tmp);
  502. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  503. c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
  504. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  505. c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
  506. } else {
  507. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  508. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  509. }
  510. err_schedule_delayed_work:
  511. schedule_delayed_work(&dev->stat_work, msecs_to_jiffies(2000));
  512. return;
  513. err:
  514. dev_dbg(&client->dev, "failed=%d\n", ret);
  515. }
  516. static int rtl2830_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
  517. {
  518. struct i2c_client *client = fe->demodulator_priv;
  519. int ret;
  520. u8 u8tmp;
  521. dev_dbg(&client->dev, "onoff=%d\n", onoff);
  522. /* enable / disable PID filter */
  523. if (onoff)
  524. u8tmp = 0x80;
  525. else
  526. u8tmp = 0x00;
  527. ret = rtl2830_update_bits(client, 0x061, 0x80, u8tmp);
  528. if (ret)
  529. goto err;
  530. return 0;
  531. err:
  532. dev_dbg(&client->dev, "failed=%d\n", ret);
  533. return ret;
  534. }
  535. static int rtl2830_pid_filter(struct dvb_frontend *fe, u8 index, u16 pid, int onoff)
  536. {
  537. struct i2c_client *client = fe->demodulator_priv;
  538. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  539. int ret;
  540. u8 buf[4];
  541. dev_dbg(&client->dev, "index=%d pid=%04x onoff=%d\n",
  542. index, pid, onoff);
  543. /* skip invalid PIDs (0x2000) */
  544. if (pid > 0x1fff || index > 32)
  545. return 0;
  546. if (onoff)
  547. set_bit(index, &dev->filters);
  548. else
  549. clear_bit(index, &dev->filters);
  550. /* enable / disable PIDs */
  551. buf[0] = (dev->filters >> 0) & 0xff;
  552. buf[1] = (dev->filters >> 8) & 0xff;
  553. buf[2] = (dev->filters >> 16) & 0xff;
  554. buf[3] = (dev->filters >> 24) & 0xff;
  555. ret = rtl2830_bulk_write(client, 0x062, buf, 4);
  556. if (ret)
  557. goto err;
  558. /* add PID */
  559. buf[0] = (pid >> 8) & 0xff;
  560. buf[1] = (pid >> 0) & 0xff;
  561. ret = rtl2830_bulk_write(client, 0x066 + 2 * index, buf, 2);
  562. if (ret)
  563. goto err;
  564. return 0;
  565. err:
  566. dev_dbg(&client->dev, "failed=%d\n", ret);
  567. return ret;
  568. }
  569. /*
  570. * I2C gate/mux/repeater logic
  571. * We must use unlocked __i2c_transfer() here (through regmap) because of I2C
  572. * adapter lock is already taken by tuner driver.
  573. * Gate is closed automatically after single I2C transfer.
  574. */
  575. static int rtl2830_select(struct i2c_adapter *adap, void *mux_priv, u32 chan_id)
  576. {
  577. struct i2c_client *client = mux_priv;
  578. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  579. int ret;
  580. dev_dbg(&client->dev, "\n");
  581. /* open I2C repeater for 1 transfer, closes automatically */
  582. /* XXX: regmap_update_bits() does not lock I2C adapter */
  583. ret = regmap_update_bits(dev->regmap, 0x101, 0x08, 0x08);
  584. if (ret)
  585. goto err;
  586. return 0;
  587. err:
  588. dev_dbg(&client->dev, "failed=%d\n", ret);
  589. return ret;
  590. }
  591. static struct dvb_frontend *rtl2830_get_dvb_frontend(struct i2c_client *client)
  592. {
  593. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  594. dev_dbg(&client->dev, "\n");
  595. return &dev->fe;
  596. }
  597. static struct i2c_adapter *rtl2830_get_i2c_adapter(struct i2c_client *client)
  598. {
  599. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  600. dev_dbg(&client->dev, "\n");
  601. return dev->adapter;
  602. }
  603. /*
  604. * We implement own I2C access routines for regmap in order to get manual access
  605. * to I2C adapter lock, which is needed for I2C mux adapter.
  606. */
  607. static int rtl2830_regmap_read(void *context, const void *reg_buf,
  608. size_t reg_size, void *val_buf, size_t val_size)
  609. {
  610. struct i2c_client *client = context;
  611. int ret;
  612. struct i2c_msg msg[2] = {
  613. {
  614. .addr = client->addr,
  615. .flags = 0,
  616. .len = reg_size,
  617. .buf = (u8 *)reg_buf,
  618. }, {
  619. .addr = client->addr,
  620. .flags = I2C_M_RD,
  621. .len = val_size,
  622. .buf = val_buf,
  623. }
  624. };
  625. ret = __i2c_transfer(client->adapter, msg, 2);
  626. if (ret != 2) {
  627. dev_warn(&client->dev, "i2c reg read failed %d\n", ret);
  628. if (ret >= 0)
  629. ret = -EREMOTEIO;
  630. return ret;
  631. }
  632. return 0;
  633. }
  634. static int rtl2830_regmap_write(void *context, const void *data, size_t count)
  635. {
  636. struct i2c_client *client = context;
  637. int ret;
  638. struct i2c_msg msg[1] = {
  639. {
  640. .addr = client->addr,
  641. .flags = 0,
  642. .len = count,
  643. .buf = (u8 *)data,
  644. }
  645. };
  646. ret = __i2c_transfer(client->adapter, msg, 1);
  647. if (ret != 1) {
  648. dev_warn(&client->dev, "i2c reg write failed %d\n", ret);
  649. if (ret >= 0)
  650. ret = -EREMOTEIO;
  651. return ret;
  652. }
  653. return 0;
  654. }
  655. static int rtl2830_regmap_gather_write(void *context, const void *reg,
  656. size_t reg_len, const void *val,
  657. size_t val_len)
  658. {
  659. struct i2c_client *client = context;
  660. int ret;
  661. u8 buf[256];
  662. struct i2c_msg msg[1] = {
  663. {
  664. .addr = client->addr,
  665. .flags = 0,
  666. .len = 1 + val_len,
  667. .buf = buf,
  668. }
  669. };
  670. buf[0] = *(u8 const *)reg;
  671. memcpy(&buf[1], val, val_len);
  672. ret = __i2c_transfer(client->adapter, msg, 1);
  673. if (ret != 1) {
  674. dev_warn(&client->dev, "i2c reg write failed %d\n", ret);
  675. if (ret >= 0)
  676. ret = -EREMOTEIO;
  677. return ret;
  678. }
  679. return 0;
  680. }
  681. static int rtl2830_probe(struct i2c_client *client,
  682. const struct i2c_device_id *id)
  683. {
  684. struct rtl2830_platform_data *pdata = client->dev.platform_data;
  685. struct rtl2830_dev *dev;
  686. int ret;
  687. u8 u8tmp;
  688. static const struct regmap_bus regmap_bus = {
  689. .read = rtl2830_regmap_read,
  690. .write = rtl2830_regmap_write,
  691. .gather_write = rtl2830_regmap_gather_write,
  692. .val_format_endian_default = REGMAP_ENDIAN_NATIVE,
  693. };
  694. static const struct regmap_range_cfg regmap_range_cfg[] = {
  695. {
  696. .selector_reg = 0x00,
  697. .selector_mask = 0xff,
  698. .selector_shift = 0,
  699. .window_start = 0,
  700. .window_len = 0x100,
  701. .range_min = 0 * 0x100,
  702. .range_max = 5 * 0x100,
  703. },
  704. };
  705. static const struct regmap_config regmap_config = {
  706. .reg_bits = 8,
  707. .val_bits = 8,
  708. .max_register = 5 * 0x100,
  709. .ranges = regmap_range_cfg,
  710. .num_ranges = ARRAY_SIZE(regmap_range_cfg),
  711. };
  712. dev_dbg(&client->dev, "\n");
  713. if (pdata == NULL) {
  714. ret = -EINVAL;
  715. goto err;
  716. }
  717. /* allocate memory for the internal state */
  718. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  719. if (dev == NULL) {
  720. ret = -ENOMEM;
  721. goto err;
  722. }
  723. /* setup the state */
  724. i2c_set_clientdata(client, dev);
  725. dev->client = client;
  726. dev->pdata = client->dev.platform_data;
  727. dev->sleeping = true;
  728. INIT_DELAYED_WORK(&dev->stat_work, rtl2830_stat_work);
  729. dev->regmap = regmap_init(&client->dev, &regmap_bus, client,
  730. &regmap_config);
  731. if (IS_ERR(dev->regmap)) {
  732. ret = PTR_ERR(dev->regmap);
  733. goto err_kfree;
  734. }
  735. /* check if the demod is there */
  736. ret = rtl2830_bulk_read(client, 0x000, &u8tmp, 1);
  737. if (ret)
  738. goto err_regmap_exit;
  739. /* create muxed i2c adapter for tuner */
  740. dev->adapter = i2c_add_mux_adapter(client->adapter, &client->dev,
  741. client, 0, 0, 0, rtl2830_select, NULL);
  742. if (dev->adapter == NULL) {
  743. ret = -ENODEV;
  744. goto err_regmap_exit;
  745. }
  746. /* create dvb frontend */
  747. memcpy(&dev->fe.ops, &rtl2830_ops, sizeof(dev->fe.ops));
  748. dev->fe.demodulator_priv = client;
  749. /* setup callbacks */
  750. pdata->get_dvb_frontend = rtl2830_get_dvb_frontend;
  751. pdata->get_i2c_adapter = rtl2830_get_i2c_adapter;
  752. pdata->pid_filter = rtl2830_pid_filter;
  753. pdata->pid_filter_ctrl = rtl2830_pid_filter_ctrl;
  754. dev_info(&client->dev, "Realtek RTL2830 successfully attached\n");
  755. return 0;
  756. err_regmap_exit:
  757. regmap_exit(dev->regmap);
  758. err_kfree:
  759. kfree(dev);
  760. err:
  761. dev_dbg(&client->dev, "failed=%d\n", ret);
  762. return ret;
  763. }
  764. static int rtl2830_remove(struct i2c_client *client)
  765. {
  766. struct rtl2830_dev *dev = i2c_get_clientdata(client);
  767. dev_dbg(&client->dev, "\n");
  768. i2c_del_mux_adapter(dev->adapter);
  769. regmap_exit(dev->regmap);
  770. kfree(dev);
  771. return 0;
  772. }
  773. static const struct i2c_device_id rtl2830_id_table[] = {
  774. {"rtl2830", 0},
  775. {}
  776. };
  777. MODULE_DEVICE_TABLE(i2c, rtl2830_id_table);
  778. static struct i2c_driver rtl2830_driver = {
  779. .driver = {
  780. .name = "rtl2830",
  781. },
  782. .probe = rtl2830_probe,
  783. .remove = rtl2830_remove,
  784. .id_table = rtl2830_id_table,
  785. };
  786. module_i2c_driver(rtl2830_driver);
  787. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  788. MODULE_DESCRIPTION("Realtek RTL2830 DVB-T demodulator driver");
  789. MODULE_LICENSE("GPL");