rtl2830.c 20 KB

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