rtl2832.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Realtek RTL2832 DVB-T demodulator driver
  4. *
  5. * Copyright (C) 2012 Thomas Mair <thomas.mair86@gmail.com>
  6. * Copyright (C) 2012-2014 Antti Palosaari <crope@iki.fi>
  7. */
  8. #include "rtl2832_priv.h"
  9. #define REG_MASK(b) (BIT(b + 1) - 1)
  10. static const struct rtl2832_reg_entry registers[] = {
  11. [DVBT_SOFT_RST] = {0x101, 2, 2},
  12. [DVBT_IIC_REPEAT] = {0x101, 3, 3},
  13. [DVBT_TR_WAIT_MIN_8K] = {0x188, 11, 2},
  14. [DVBT_RSD_BER_FAIL_VAL] = {0x18f, 15, 0},
  15. [DVBT_EN_BK_TRK] = {0x1a6, 7, 7},
  16. [DVBT_AD_EN_REG] = {0x008, 7, 7},
  17. [DVBT_AD_EN_REG1] = {0x008, 6, 6},
  18. [DVBT_EN_BBIN] = {0x1b1, 0, 0},
  19. [DVBT_MGD_THD0] = {0x195, 7, 0},
  20. [DVBT_MGD_THD1] = {0x196, 7, 0},
  21. [DVBT_MGD_THD2] = {0x197, 7, 0},
  22. [DVBT_MGD_THD3] = {0x198, 7, 0},
  23. [DVBT_MGD_THD4] = {0x199, 7, 0},
  24. [DVBT_MGD_THD5] = {0x19a, 7, 0},
  25. [DVBT_MGD_THD6] = {0x19b, 7, 0},
  26. [DVBT_MGD_THD7] = {0x19c, 7, 0},
  27. [DVBT_EN_CACQ_NOTCH] = {0x161, 4, 4},
  28. [DVBT_AD_AV_REF] = {0x009, 6, 0},
  29. [DVBT_REG_PI] = {0x00a, 2, 0},
  30. [DVBT_PIP_ON] = {0x021, 3, 3},
  31. [DVBT_SCALE1_B92] = {0x292, 7, 0},
  32. [DVBT_SCALE1_B93] = {0x293, 7, 0},
  33. [DVBT_SCALE1_BA7] = {0x2a7, 7, 0},
  34. [DVBT_SCALE1_BA9] = {0x2a9, 7, 0},
  35. [DVBT_SCALE1_BAA] = {0x2aa, 7, 0},
  36. [DVBT_SCALE1_BAB] = {0x2ab, 7, 0},
  37. [DVBT_SCALE1_BAC] = {0x2ac, 7, 0},
  38. [DVBT_SCALE1_BB0] = {0x2b0, 7, 0},
  39. [DVBT_SCALE1_BB1] = {0x2b1, 7, 0},
  40. [DVBT_KB_P1] = {0x164, 3, 1},
  41. [DVBT_KB_P2] = {0x164, 6, 4},
  42. [DVBT_KB_P3] = {0x165, 2, 0},
  43. [DVBT_OPT_ADC_IQ] = {0x006, 5, 4},
  44. [DVBT_AD_AVI] = {0x009, 1, 0},
  45. [DVBT_AD_AVQ] = {0x009, 3, 2},
  46. [DVBT_K1_CR_STEP12] = {0x2ad, 9, 4},
  47. [DVBT_TRK_KS_P2] = {0x16f, 2, 0},
  48. [DVBT_TRK_KS_I2] = {0x170, 5, 3},
  49. [DVBT_TR_THD_SET2] = {0x172, 3, 0},
  50. [DVBT_TRK_KC_P2] = {0x173, 5, 3},
  51. [DVBT_TRK_KC_I2] = {0x175, 2, 0},
  52. [DVBT_CR_THD_SET2] = {0x176, 7, 6},
  53. [DVBT_PSET_IFFREQ] = {0x119, 21, 0},
  54. [DVBT_SPEC_INV] = {0x115, 0, 0},
  55. [DVBT_RSAMP_RATIO] = {0x19f, 27, 2},
  56. [DVBT_CFREQ_OFF_RATIO] = {0x19d, 23, 4},
  57. [DVBT_FSM_STAGE] = {0x351, 6, 3},
  58. [DVBT_RX_CONSTEL] = {0x33c, 3, 2},
  59. [DVBT_RX_HIER] = {0x33c, 6, 4},
  60. [DVBT_RX_C_RATE_LP] = {0x33d, 2, 0},
  61. [DVBT_RX_C_RATE_HP] = {0x33d, 5, 3},
  62. [DVBT_GI_IDX] = {0x351, 1, 0},
  63. [DVBT_FFT_MODE_IDX] = {0x351, 2, 2},
  64. [DVBT_RSD_BER_EST] = {0x34e, 15, 0},
  65. [DVBT_CE_EST_EVM] = {0x40c, 15, 0},
  66. [DVBT_RF_AGC_VAL] = {0x35b, 13, 0},
  67. [DVBT_IF_AGC_VAL] = {0x359, 13, 0},
  68. [DVBT_DAGC_VAL] = {0x305, 7, 0},
  69. [DVBT_SFREQ_OFF] = {0x318, 13, 0},
  70. [DVBT_CFREQ_OFF] = {0x35f, 17, 0},
  71. [DVBT_POLAR_RF_AGC] = {0x00e, 1, 1},
  72. [DVBT_POLAR_IF_AGC] = {0x00e, 0, 0},
  73. [DVBT_AAGC_HOLD] = {0x104, 5, 5},
  74. [DVBT_EN_RF_AGC] = {0x104, 6, 6},
  75. [DVBT_EN_IF_AGC] = {0x104, 7, 7},
  76. [DVBT_IF_AGC_MIN] = {0x108, 7, 0},
  77. [DVBT_IF_AGC_MAX] = {0x109, 7, 0},
  78. [DVBT_RF_AGC_MIN] = {0x10a, 7, 0},
  79. [DVBT_RF_AGC_MAX] = {0x10b, 7, 0},
  80. [DVBT_IF_AGC_MAN] = {0x10c, 6, 6},
  81. [DVBT_IF_AGC_MAN_VAL] = {0x10c, 13, 0},
  82. [DVBT_RF_AGC_MAN] = {0x10e, 6, 6},
  83. [DVBT_RF_AGC_MAN_VAL] = {0x10e, 13, 0},
  84. [DVBT_DAGC_TRG_VAL] = {0x112, 7, 0},
  85. [DVBT_AGC_TARG_VAL_0] = {0x102, 0, 0},
  86. [DVBT_AGC_TARG_VAL_8_1] = {0x103, 7, 0},
  87. [DVBT_AAGC_LOOP_GAIN] = {0x1c7, 5, 1},
  88. [DVBT_LOOP_GAIN2_3_0] = {0x104, 4, 1},
  89. [DVBT_LOOP_GAIN2_4] = {0x105, 7, 7},
  90. [DVBT_LOOP_GAIN3] = {0x1c8, 4, 0},
  91. [DVBT_VTOP1] = {0x106, 5, 0},
  92. [DVBT_VTOP2] = {0x1c9, 5, 0},
  93. [DVBT_VTOP3] = {0x1ca, 5, 0},
  94. [DVBT_KRF1] = {0x1cb, 7, 0},
  95. [DVBT_KRF2] = {0x107, 7, 0},
  96. [DVBT_KRF3] = {0x1cd, 7, 0},
  97. [DVBT_KRF4] = {0x1ce, 7, 0},
  98. [DVBT_EN_GI_PGA] = {0x1e5, 0, 0},
  99. [DVBT_THD_LOCK_UP] = {0x1d9, 8, 0},
  100. [DVBT_THD_LOCK_DW] = {0x1db, 8, 0},
  101. [DVBT_THD_UP1] = {0x1dd, 7, 0},
  102. [DVBT_THD_DW1] = {0x1de, 7, 0},
  103. [DVBT_INTER_CNT_LEN] = {0x1d8, 3, 0},
  104. [DVBT_GI_PGA_STATE] = {0x1e6, 3, 3},
  105. [DVBT_EN_AGC_PGA] = {0x1d7, 0, 0},
  106. [DVBT_CKOUTPAR] = {0x17b, 5, 5},
  107. [DVBT_CKOUT_PWR] = {0x17b, 6, 6},
  108. [DVBT_SYNC_DUR] = {0x17b, 7, 7},
  109. [DVBT_ERR_DUR] = {0x17c, 0, 0},
  110. [DVBT_SYNC_LVL] = {0x17c, 1, 1},
  111. [DVBT_ERR_LVL] = {0x17c, 2, 2},
  112. [DVBT_VAL_LVL] = {0x17c, 3, 3},
  113. [DVBT_SERIAL] = {0x17c, 4, 4},
  114. [DVBT_SER_LSB] = {0x17c, 5, 5},
  115. [DVBT_CDIV_PH0] = {0x17d, 3, 0},
  116. [DVBT_CDIV_PH1] = {0x17d, 7, 4},
  117. [DVBT_MPEG_IO_OPT_2_2] = {0x006, 7, 7},
  118. [DVBT_MPEG_IO_OPT_1_0] = {0x007, 7, 6},
  119. [DVBT_CKOUTPAR_PIP] = {0x0b7, 4, 4},
  120. [DVBT_CKOUT_PWR_PIP] = {0x0b7, 3, 3},
  121. [DVBT_SYNC_LVL_PIP] = {0x0b7, 2, 2},
  122. [DVBT_ERR_LVL_PIP] = {0x0b7, 1, 1},
  123. [DVBT_VAL_LVL_PIP] = {0x0b7, 0, 0},
  124. [DVBT_CKOUTPAR_PID] = {0x0b9, 4, 4},
  125. [DVBT_CKOUT_PWR_PID] = {0x0b9, 3, 3},
  126. [DVBT_SYNC_LVL_PID] = {0x0b9, 2, 2},
  127. [DVBT_ERR_LVL_PID] = {0x0b9, 1, 1},
  128. [DVBT_VAL_LVL_PID] = {0x0b9, 0, 0},
  129. [DVBT_SM_PASS] = {0x193, 11, 0},
  130. [DVBT_AD7_SETTING] = {0x011, 15, 0},
  131. [DVBT_RSSI_R] = {0x301, 6, 0},
  132. [DVBT_ACI_DET_IND] = {0x312, 0, 0},
  133. [DVBT_REG_MON] = {0x00d, 1, 0},
  134. [DVBT_REG_MONSEL] = {0x00d, 2, 2},
  135. [DVBT_REG_GPE] = {0x00d, 7, 7},
  136. [DVBT_REG_GPO] = {0x010, 0, 0},
  137. [DVBT_REG_4MSEL] = {0x013, 0, 0},
  138. };
  139. static int rtl2832_rd_demod_reg(struct rtl2832_dev *dev, int reg, u32 *val)
  140. {
  141. struct i2c_client *client = dev->client;
  142. int ret, i;
  143. u16 reg_start_addr;
  144. u8 msb, lsb, reading[4], len;
  145. u32 reading_tmp, mask;
  146. reg_start_addr = registers[reg].start_address;
  147. msb = registers[reg].msb;
  148. lsb = registers[reg].lsb;
  149. len = (msb >> 3) + 1;
  150. mask = REG_MASK(msb - lsb);
  151. ret = regmap_bulk_read(dev->regmap, reg_start_addr, reading, len);
  152. if (ret)
  153. goto err;
  154. reading_tmp = 0;
  155. for (i = 0; i < len; i++)
  156. reading_tmp |= reading[i] << ((len - 1 - i) * 8);
  157. *val = (reading_tmp >> lsb) & mask;
  158. return 0;
  159. err:
  160. dev_dbg(&client->dev, "failed=%d\n", ret);
  161. return ret;
  162. }
  163. static int rtl2832_wr_demod_reg(struct rtl2832_dev *dev, int reg, u32 val)
  164. {
  165. struct i2c_client *client = dev->client;
  166. int ret, i;
  167. u16 reg_start_addr;
  168. u8 msb, lsb, reading[4], writing[4], len;
  169. u32 reading_tmp, writing_tmp, mask;
  170. reg_start_addr = registers[reg].start_address;
  171. msb = registers[reg].msb;
  172. lsb = registers[reg].lsb;
  173. len = (msb >> 3) + 1;
  174. mask = REG_MASK(msb - lsb);
  175. ret = regmap_bulk_read(dev->regmap, reg_start_addr, reading, len);
  176. if (ret)
  177. goto err;
  178. reading_tmp = 0;
  179. for (i = 0; i < len; i++)
  180. reading_tmp |= reading[i] << ((len - 1 - i) * 8);
  181. writing_tmp = reading_tmp & ~(mask << lsb);
  182. writing_tmp |= ((val & mask) << lsb);
  183. for (i = 0; i < len; i++)
  184. writing[i] = (writing_tmp >> ((len - 1 - i) * 8)) & 0xff;
  185. ret = regmap_bulk_write(dev->regmap, reg_start_addr, writing, len);
  186. if (ret)
  187. goto err;
  188. return 0;
  189. err:
  190. dev_dbg(&client->dev, "failed=%d\n", ret);
  191. return ret;
  192. }
  193. static int rtl2832_set_if(struct dvb_frontend *fe, u32 if_freq)
  194. {
  195. struct rtl2832_dev *dev = fe->demodulator_priv;
  196. struct i2c_client *client = dev->client;
  197. int ret;
  198. u64 pset_iffreq;
  199. u8 en_bbin = (if_freq == 0 ? 0x1 : 0x0);
  200. /*
  201. * PSET_IFFREQ = - floor((IfFreqHz % CrystalFreqHz) * pow(2, 22)
  202. * / CrystalFreqHz)
  203. */
  204. pset_iffreq = if_freq % dev->pdata->clk;
  205. pset_iffreq *= 0x400000;
  206. pset_iffreq = div_u64(pset_iffreq, dev->pdata->clk);
  207. pset_iffreq = -pset_iffreq;
  208. pset_iffreq = pset_iffreq & 0x3fffff;
  209. dev_dbg(&client->dev, "if_frequency=%d pset_iffreq=%08x\n",
  210. if_freq, (unsigned)pset_iffreq);
  211. ret = rtl2832_wr_demod_reg(dev, DVBT_EN_BBIN, en_bbin);
  212. if (ret)
  213. goto err;
  214. ret = rtl2832_wr_demod_reg(dev, DVBT_PSET_IFFREQ, pset_iffreq);
  215. if (ret)
  216. goto err;
  217. return 0;
  218. err:
  219. dev_dbg(&client->dev, "failed=%d\n", ret);
  220. return ret;
  221. }
  222. static int rtl2832_init(struct dvb_frontend *fe)
  223. {
  224. struct rtl2832_dev *dev = fe->demodulator_priv;
  225. struct i2c_client *client = dev->client;
  226. struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
  227. const struct rtl2832_reg_value *init;
  228. int i, ret, len;
  229. /* initialization values for the demodulator registers */
  230. struct rtl2832_reg_value rtl2832_initial_regs[] = {
  231. {DVBT_AD_EN_REG, 0x1},
  232. {DVBT_AD_EN_REG1, 0x1},
  233. {DVBT_RSD_BER_FAIL_VAL, 0x2800},
  234. {DVBT_MGD_THD0, 0x10},
  235. {DVBT_MGD_THD1, 0x20},
  236. {DVBT_MGD_THD2, 0x20},
  237. {DVBT_MGD_THD3, 0x40},
  238. {DVBT_MGD_THD4, 0x22},
  239. {DVBT_MGD_THD5, 0x32},
  240. {DVBT_MGD_THD6, 0x37},
  241. {DVBT_MGD_THD7, 0x39},
  242. {DVBT_EN_BK_TRK, 0x0},
  243. {DVBT_EN_CACQ_NOTCH, 0x0},
  244. {DVBT_AD_AV_REF, 0x2a},
  245. {DVBT_REG_PI, 0x6},
  246. {DVBT_PIP_ON, 0x0},
  247. {DVBT_CDIV_PH0, 0x8},
  248. {DVBT_CDIV_PH1, 0x8},
  249. {DVBT_SCALE1_B92, 0x4},
  250. {DVBT_SCALE1_B93, 0xb0},
  251. {DVBT_SCALE1_BA7, 0x78},
  252. {DVBT_SCALE1_BA9, 0x28},
  253. {DVBT_SCALE1_BAA, 0x59},
  254. {DVBT_SCALE1_BAB, 0x83},
  255. {DVBT_SCALE1_BAC, 0xd4},
  256. {DVBT_SCALE1_BB0, 0x65},
  257. {DVBT_SCALE1_BB1, 0x43},
  258. {DVBT_KB_P1, 0x1},
  259. {DVBT_KB_P2, 0x4},
  260. {DVBT_KB_P3, 0x7},
  261. {DVBT_K1_CR_STEP12, 0xa},
  262. {DVBT_REG_GPE, 0x1},
  263. {DVBT_SERIAL, 0x0},
  264. {DVBT_CDIV_PH0, 0x9},
  265. {DVBT_CDIV_PH1, 0x9},
  266. {DVBT_MPEG_IO_OPT_2_2, 0x0},
  267. {DVBT_MPEG_IO_OPT_1_0, 0x0},
  268. {DVBT_TRK_KS_P2, 0x4},
  269. {DVBT_TRK_KS_I2, 0x7},
  270. {DVBT_TR_THD_SET2, 0x6},
  271. {DVBT_TRK_KC_I2, 0x5},
  272. {DVBT_CR_THD_SET2, 0x1},
  273. };
  274. dev_dbg(&client->dev, "\n");
  275. ret = rtl2832_wr_demod_reg(dev, DVBT_SOFT_RST, 0x0);
  276. if (ret)
  277. goto err;
  278. for (i = 0; i < ARRAY_SIZE(rtl2832_initial_regs); i++) {
  279. ret = rtl2832_wr_demod_reg(dev, rtl2832_initial_regs[i].reg,
  280. rtl2832_initial_regs[i].value);
  281. if (ret)
  282. goto err;
  283. }
  284. /* load tuner specific settings */
  285. dev_dbg(&client->dev, "load settings for tuner=%02x\n",
  286. dev->pdata->tuner);
  287. switch (dev->pdata->tuner) {
  288. case RTL2832_TUNER_FC2580:
  289. len = ARRAY_SIZE(rtl2832_tuner_init_fc2580);
  290. init = rtl2832_tuner_init_fc2580;
  291. break;
  292. case RTL2832_TUNER_FC0012:
  293. case RTL2832_TUNER_FC0013:
  294. len = ARRAY_SIZE(rtl2832_tuner_init_fc0012);
  295. init = rtl2832_tuner_init_fc0012;
  296. break;
  297. case RTL2832_TUNER_TUA9001:
  298. len = ARRAY_SIZE(rtl2832_tuner_init_tua9001);
  299. init = rtl2832_tuner_init_tua9001;
  300. break;
  301. case RTL2832_TUNER_E4000:
  302. len = ARRAY_SIZE(rtl2832_tuner_init_e4000);
  303. init = rtl2832_tuner_init_e4000;
  304. break;
  305. case RTL2832_TUNER_R820T:
  306. case RTL2832_TUNER_R828D:
  307. len = ARRAY_SIZE(rtl2832_tuner_init_r820t);
  308. init = rtl2832_tuner_init_r820t;
  309. break;
  310. case RTL2832_TUNER_SI2157:
  311. len = ARRAY_SIZE(rtl2832_tuner_init_si2157);
  312. init = rtl2832_tuner_init_si2157;
  313. break;
  314. default:
  315. ret = -EINVAL;
  316. goto err;
  317. }
  318. for (i = 0; i < len; i++) {
  319. ret = rtl2832_wr_demod_reg(dev, init[i].reg, init[i].value);
  320. if (ret)
  321. goto err;
  322. }
  323. /* init stats here in order signal app which stats are supported */
  324. c->strength.len = 1;
  325. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  326. c->cnr.len = 1;
  327. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  328. c->post_bit_error.len = 1;
  329. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  330. c->post_bit_count.len = 1;
  331. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  332. dev->sleeping = false;
  333. return 0;
  334. err:
  335. dev_dbg(&client->dev, "failed=%d\n", ret);
  336. return ret;
  337. }
  338. static int rtl2832_sleep(struct dvb_frontend *fe)
  339. {
  340. struct rtl2832_dev *dev = fe->demodulator_priv;
  341. struct i2c_client *client = dev->client;
  342. int ret;
  343. dev_dbg(&client->dev, "\n");
  344. dev->sleeping = true;
  345. dev->fe_status = 0;
  346. ret = rtl2832_wr_demod_reg(dev, DVBT_SOFT_RST, 0x1);
  347. if (ret)
  348. goto err;
  349. return 0;
  350. err:
  351. dev_dbg(&client->dev, "failed=%d\n", ret);
  352. return ret;
  353. }
  354. static int rtl2832_get_tune_settings(struct dvb_frontend *fe,
  355. struct dvb_frontend_tune_settings *s)
  356. {
  357. struct rtl2832_dev *dev = fe->demodulator_priv;
  358. struct i2c_client *client = dev->client;
  359. dev_dbg(&client->dev, "\n");
  360. s->min_delay_ms = 1000;
  361. s->step_size = fe->ops.info.frequency_stepsize_hz * 2;
  362. s->max_drift = (fe->ops.info.frequency_stepsize_hz * 2) + 1;
  363. return 0;
  364. }
  365. static int rtl2832_set_frontend(struct dvb_frontend *fe)
  366. {
  367. struct rtl2832_dev *dev = fe->demodulator_priv;
  368. struct i2c_client *client = dev->client;
  369. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  370. int ret, i, j;
  371. u64 bw_mode, num, num2;
  372. u32 resamp_ratio, cfreq_off_ratio;
  373. static u8 bw_params[3][32] = {
  374. /* 6 MHz bandwidth */
  375. {
  376. 0xf5, 0xff, 0x15, 0x38, 0x5d, 0x6d, 0x52, 0x07, 0xfa, 0x2f,
  377. 0x53, 0xf5, 0x3f, 0xca, 0x0b, 0x91, 0xea, 0x30, 0x63, 0xb2,
  378. 0x13, 0xda, 0x0b, 0xc4, 0x18, 0x7e, 0x16, 0x66, 0x08, 0x67,
  379. 0x19, 0xe0,
  380. },
  381. /* 7 MHz bandwidth */
  382. {
  383. 0xe7, 0xcc, 0xb5, 0xba, 0xe8, 0x2f, 0x67, 0x61, 0x00, 0xaf,
  384. 0x86, 0xf2, 0xbf, 0x59, 0x04, 0x11, 0xb6, 0x33, 0xa4, 0x30,
  385. 0x15, 0x10, 0x0a, 0x42, 0x18, 0xf8, 0x17, 0xd9, 0x07, 0x22,
  386. 0x19, 0x10,
  387. },
  388. /* 8 MHz bandwidth */
  389. {
  390. 0x09, 0xf6, 0xd2, 0xa7, 0x9a, 0xc9, 0x27, 0x77, 0x06, 0xbf,
  391. 0xec, 0xf4, 0x4f, 0x0b, 0xfc, 0x01, 0x63, 0x35, 0x54, 0xa7,
  392. 0x16, 0x66, 0x08, 0xb4, 0x19, 0x6e, 0x19, 0x65, 0x05, 0xc8,
  393. 0x19, 0xe0,
  394. },
  395. };
  396. dev_dbg(&client->dev, "frequency=%u bandwidth_hz=%u inversion=%u\n",
  397. c->frequency, c->bandwidth_hz, c->inversion);
  398. /* program tuner */
  399. if (fe->ops.tuner_ops.set_params)
  400. fe->ops.tuner_ops.set_params(fe);
  401. /* If the frontend has get_if_frequency(), use it */
  402. if (fe->ops.tuner_ops.get_if_frequency) {
  403. u32 if_freq;
  404. ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
  405. if (ret)
  406. goto err;
  407. ret = rtl2832_set_if(fe, if_freq);
  408. if (ret)
  409. goto err;
  410. }
  411. switch (c->bandwidth_hz) {
  412. case 6000000:
  413. i = 0;
  414. bw_mode = 48000000;
  415. break;
  416. case 7000000:
  417. i = 1;
  418. bw_mode = 56000000;
  419. break;
  420. case 8000000:
  421. i = 2;
  422. bw_mode = 64000000;
  423. break;
  424. default:
  425. dev_err(&client->dev, "invalid bandwidth_hz %u\n",
  426. c->bandwidth_hz);
  427. ret = -EINVAL;
  428. goto err;
  429. }
  430. for (j = 0; j < sizeof(bw_params[0]); j++) {
  431. ret = regmap_bulk_write(dev->regmap,
  432. 0x11c + j, &bw_params[i][j], 1);
  433. if (ret)
  434. goto err;
  435. }
  436. /* calculate and set resample ratio
  437. * RSAMP_RATIO = floor(CrystalFreqHz * 7 * pow(2, 22)
  438. * / ConstWithBandwidthMode)
  439. */
  440. num = dev->pdata->clk * 7ULL;
  441. num *= 0x400000;
  442. num = div_u64(num, bw_mode);
  443. resamp_ratio = num & 0x3ffffff;
  444. ret = rtl2832_wr_demod_reg(dev, DVBT_RSAMP_RATIO, resamp_ratio);
  445. if (ret)
  446. goto err;
  447. /* calculate and set cfreq off ratio
  448. * CFREQ_OFF_RATIO = - floor(ConstWithBandwidthMode * pow(2, 20)
  449. * / (CrystalFreqHz * 7))
  450. */
  451. num = bw_mode << 20;
  452. num2 = dev->pdata->clk * 7ULL;
  453. num = div_u64(num, num2);
  454. num = -num;
  455. cfreq_off_ratio = num & 0xfffff;
  456. ret = rtl2832_wr_demod_reg(dev, DVBT_CFREQ_OFF_RATIO, cfreq_off_ratio);
  457. if (ret)
  458. goto err;
  459. /* soft reset */
  460. ret = rtl2832_wr_demod_reg(dev, DVBT_SOFT_RST, 0x1);
  461. if (ret)
  462. goto err;
  463. ret = rtl2832_wr_demod_reg(dev, DVBT_SOFT_RST, 0x0);
  464. if (ret)
  465. goto err;
  466. return 0;
  467. err:
  468. dev_dbg(&client->dev, "failed=%d\n", ret);
  469. return ret;
  470. }
  471. static int rtl2832_get_frontend(struct dvb_frontend *fe,
  472. struct dtv_frontend_properties *c)
  473. {
  474. struct rtl2832_dev *dev = fe->demodulator_priv;
  475. struct i2c_client *client = dev->client;
  476. int ret;
  477. u8 buf[3];
  478. if (dev->sleeping)
  479. return 0;
  480. ret = regmap_bulk_read(dev->regmap, 0x33c, buf, 2);
  481. if (ret)
  482. goto err;
  483. ret = regmap_bulk_read(dev->regmap, 0x351, &buf[2], 1);
  484. if (ret)
  485. goto err;
  486. dev_dbg(&client->dev, "TPS=%*ph\n", 3, buf);
  487. switch ((buf[0] >> 2) & 3) {
  488. case 0:
  489. c->modulation = QPSK;
  490. break;
  491. case 1:
  492. c->modulation = QAM_16;
  493. break;
  494. case 2:
  495. c->modulation = QAM_64;
  496. break;
  497. }
  498. switch ((buf[2] >> 2) & 1) {
  499. case 0:
  500. c->transmission_mode = TRANSMISSION_MODE_2K;
  501. break;
  502. case 1:
  503. c->transmission_mode = TRANSMISSION_MODE_8K;
  504. }
  505. switch ((buf[2] >> 0) & 3) {
  506. case 0:
  507. c->guard_interval = GUARD_INTERVAL_1_32;
  508. break;
  509. case 1:
  510. c->guard_interval = GUARD_INTERVAL_1_16;
  511. break;
  512. case 2:
  513. c->guard_interval = GUARD_INTERVAL_1_8;
  514. break;
  515. case 3:
  516. c->guard_interval = GUARD_INTERVAL_1_4;
  517. break;
  518. }
  519. switch ((buf[0] >> 4) & 7) {
  520. case 0:
  521. c->hierarchy = HIERARCHY_NONE;
  522. break;
  523. case 1:
  524. c->hierarchy = HIERARCHY_1;
  525. break;
  526. case 2:
  527. c->hierarchy = HIERARCHY_2;
  528. break;
  529. case 3:
  530. c->hierarchy = HIERARCHY_4;
  531. break;
  532. }
  533. switch ((buf[1] >> 3) & 7) {
  534. case 0:
  535. c->code_rate_HP = FEC_1_2;
  536. break;
  537. case 1:
  538. c->code_rate_HP = FEC_2_3;
  539. break;
  540. case 2:
  541. c->code_rate_HP = FEC_3_4;
  542. break;
  543. case 3:
  544. c->code_rate_HP = FEC_5_6;
  545. break;
  546. case 4:
  547. c->code_rate_HP = FEC_7_8;
  548. break;
  549. }
  550. switch ((buf[1] >> 0) & 7) {
  551. case 0:
  552. c->code_rate_LP = FEC_1_2;
  553. break;
  554. case 1:
  555. c->code_rate_LP = FEC_2_3;
  556. break;
  557. case 2:
  558. c->code_rate_LP = FEC_3_4;
  559. break;
  560. case 3:
  561. c->code_rate_LP = FEC_5_6;
  562. break;
  563. case 4:
  564. c->code_rate_LP = FEC_7_8;
  565. break;
  566. }
  567. return 0;
  568. err:
  569. dev_dbg(&client->dev, "failed=%d\n", ret);
  570. return ret;
  571. }
  572. static int rtl2832_read_status(struct dvb_frontend *fe, enum fe_status *status)
  573. {
  574. struct rtl2832_dev *dev = fe->demodulator_priv;
  575. struct i2c_client *client = dev->client;
  576. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  577. int ret;
  578. u32 uninitialized_var(tmp);
  579. u8 u8tmp, buf[2];
  580. u16 u16tmp;
  581. dev_dbg(&client->dev, "\n");
  582. *status = 0;
  583. if (dev->sleeping)
  584. return 0;
  585. ret = rtl2832_rd_demod_reg(dev, DVBT_FSM_STAGE, &tmp);
  586. if (ret)
  587. goto err;
  588. if (tmp == 11) {
  589. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  590. FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  591. } else if (tmp == 10) {
  592. *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
  593. FE_HAS_VITERBI;
  594. }
  595. dev->fe_status = *status;
  596. /* signal strength */
  597. if (dev->fe_status & FE_HAS_SIGNAL) {
  598. /* read digital AGC */
  599. ret = regmap_bulk_read(dev->regmap, 0x305, &u8tmp, 1);
  600. if (ret)
  601. goto err;
  602. dev_dbg(&client->dev, "digital agc=%02x", u8tmp);
  603. u8tmp = ~u8tmp;
  604. u16tmp = u8tmp << 8 | u8tmp << 0;
  605. c->strength.stat[0].scale = FE_SCALE_RELATIVE;
  606. c->strength.stat[0].uvalue = u16tmp;
  607. } else {
  608. c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  609. }
  610. /* CNR */
  611. if (dev->fe_status & FE_HAS_VITERBI) {
  612. unsigned hierarchy, constellation;
  613. #define CONSTELLATION_NUM 3
  614. #define HIERARCHY_NUM 4
  615. static const u32 constant[CONSTELLATION_NUM][HIERARCHY_NUM] = {
  616. {85387325, 85387325, 85387325, 85387325},
  617. {86676178, 86676178, 87167949, 87795660},
  618. {87659938, 87659938, 87885178, 88241743},
  619. };
  620. ret = regmap_bulk_read(dev->regmap, 0x33c, &u8tmp, 1);
  621. if (ret)
  622. goto err;
  623. constellation = (u8tmp >> 2) & 0x03; /* [3:2] */
  624. if (constellation > CONSTELLATION_NUM - 1)
  625. goto err;
  626. hierarchy = (u8tmp >> 4) & 0x07; /* [6:4] */
  627. if (hierarchy > HIERARCHY_NUM - 1)
  628. goto err;
  629. ret = regmap_bulk_read(dev->regmap, 0x40c, buf, 2);
  630. if (ret)
  631. goto err;
  632. u16tmp = buf[0] << 8 | buf[1] << 0;
  633. if (u16tmp)
  634. tmp = (constant[constellation][hierarchy] -
  635. intlog10(u16tmp)) / ((1 << 24) / 10000);
  636. else
  637. tmp = 0;
  638. dev_dbg(&client->dev, "cnr raw=%u\n", u16tmp);
  639. c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
  640. c->cnr.stat[0].svalue = tmp;
  641. } else {
  642. c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  643. }
  644. /* BER */
  645. if (dev->fe_status & FE_HAS_LOCK) {
  646. ret = regmap_bulk_read(dev->regmap, 0x34e, buf, 2);
  647. if (ret)
  648. goto err;
  649. u16tmp = buf[0] << 8 | buf[1] << 0;
  650. dev->post_bit_error += u16tmp;
  651. dev->post_bit_count += 1000000;
  652. dev_dbg(&client->dev, "ber errors=%u total=1000000\n", u16tmp);
  653. c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
  654. c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
  655. c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
  656. c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
  657. } else {
  658. c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  659. c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
  660. }
  661. return 0;
  662. err:
  663. dev_dbg(&client->dev, "failed=%d\n", ret);
  664. return ret;
  665. }
  666. static int rtl2832_read_snr(struct dvb_frontend *fe, u16 *snr)
  667. {
  668. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  669. /* report SNR in resolution of 0.1 dB */
  670. if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
  671. *snr = div_s64(c->cnr.stat[0].svalue, 100);
  672. else
  673. *snr = 0;
  674. return 0;
  675. }
  676. static int rtl2832_read_ber(struct dvb_frontend *fe, u32 *ber)
  677. {
  678. struct rtl2832_dev *dev = fe->demodulator_priv;
  679. *ber = (dev->post_bit_error - dev->post_bit_error_prev);
  680. dev->post_bit_error_prev = dev->post_bit_error;
  681. return 0;
  682. }
  683. /*
  684. * I2C gate/mux/repeater logic
  685. * There is delay mechanism to avoid unneeded I2C gate open / close. Gate close
  686. * is delayed here a little bit in order to see if there is sequence of I2C
  687. * messages sent to same I2C bus.
  688. */
  689. static void rtl2832_i2c_gate_work(struct work_struct *work)
  690. {
  691. struct rtl2832_dev *dev = container_of(work, struct rtl2832_dev, i2c_gate_work.work);
  692. struct i2c_client *client = dev->client;
  693. int ret;
  694. /* close gate */
  695. ret = regmap_update_bits(dev->regmap, 0x101, 0x08, 0x00);
  696. if (ret)
  697. goto err;
  698. return;
  699. err:
  700. dev_dbg(&client->dev, "failed=%d\n", ret);
  701. }
  702. static int rtl2832_select(struct i2c_mux_core *muxc, u32 chan_id)
  703. {
  704. struct rtl2832_dev *dev = i2c_mux_priv(muxc);
  705. struct i2c_client *client = dev->client;
  706. int ret;
  707. /* terminate possible gate closing */
  708. cancel_delayed_work(&dev->i2c_gate_work);
  709. /* open gate */
  710. ret = regmap_update_bits(dev->regmap, 0x101, 0x08, 0x08);
  711. if (ret)
  712. goto err;
  713. return 0;
  714. err:
  715. dev_dbg(&client->dev, "failed=%d\n", ret);
  716. return ret;
  717. }
  718. static int rtl2832_deselect(struct i2c_mux_core *muxc, u32 chan_id)
  719. {
  720. struct rtl2832_dev *dev = i2c_mux_priv(muxc);
  721. schedule_delayed_work(&dev->i2c_gate_work, usecs_to_jiffies(100));
  722. return 0;
  723. }
  724. static const struct dvb_frontend_ops rtl2832_ops = {
  725. .delsys = { SYS_DVBT },
  726. .info = {
  727. .name = "Realtek RTL2832 (DVB-T)",
  728. .frequency_min_hz = 174 * MHz,
  729. .frequency_max_hz = 862 * MHz,
  730. .frequency_stepsize_hz = 166667,
  731. .caps = FE_CAN_FEC_1_2 |
  732. FE_CAN_FEC_2_3 |
  733. FE_CAN_FEC_3_4 |
  734. FE_CAN_FEC_5_6 |
  735. FE_CAN_FEC_7_8 |
  736. FE_CAN_FEC_AUTO |
  737. FE_CAN_QPSK |
  738. FE_CAN_QAM_16 |
  739. FE_CAN_QAM_64 |
  740. FE_CAN_QAM_AUTO |
  741. FE_CAN_TRANSMISSION_MODE_AUTO |
  742. FE_CAN_GUARD_INTERVAL_AUTO |
  743. FE_CAN_HIERARCHY_AUTO |
  744. FE_CAN_RECOVER |
  745. FE_CAN_MUTE_TS
  746. },
  747. .init = rtl2832_init,
  748. .sleep = rtl2832_sleep,
  749. .get_tune_settings = rtl2832_get_tune_settings,
  750. .set_frontend = rtl2832_set_frontend,
  751. .get_frontend = rtl2832_get_frontend,
  752. .read_status = rtl2832_read_status,
  753. .read_snr = rtl2832_read_snr,
  754. .read_ber = rtl2832_read_ber,
  755. };
  756. static bool rtl2832_volatile_reg(struct device *dev, unsigned int reg)
  757. {
  758. switch (reg) {
  759. case 0x305:
  760. case 0x33c:
  761. case 0x34e:
  762. case 0x351:
  763. case 0x40c ... 0x40d:
  764. return true;
  765. default:
  766. break;
  767. }
  768. return false;
  769. }
  770. static struct dvb_frontend *rtl2832_get_dvb_frontend(struct i2c_client *client)
  771. {
  772. struct rtl2832_dev *dev = i2c_get_clientdata(client);
  773. dev_dbg(&client->dev, "\n");
  774. return &dev->fe;
  775. }
  776. static struct i2c_adapter *rtl2832_get_i2c_adapter(struct i2c_client *client)
  777. {
  778. struct rtl2832_dev *dev = i2c_get_clientdata(client);
  779. dev_dbg(&client->dev, "\n");
  780. return dev->muxc->adapter[0];
  781. }
  782. static int rtl2832_slave_ts_ctrl(struct i2c_client *client, bool enable)
  783. {
  784. struct rtl2832_dev *dev = i2c_get_clientdata(client);
  785. int ret;
  786. dev_dbg(&client->dev, "enable=%d\n", enable);
  787. if (enable) {
  788. ret = rtl2832_wr_demod_reg(dev, DVBT_SOFT_RST, 0x0);
  789. if (ret)
  790. goto err;
  791. ret = regmap_bulk_write(dev->regmap, 0x10c, "\x5f\xff", 2);
  792. if (ret)
  793. goto err;
  794. ret = rtl2832_wr_demod_reg(dev, DVBT_PIP_ON, 0x1);
  795. if (ret)
  796. goto err;
  797. ret = regmap_bulk_write(dev->regmap, 0x0bc, "\x18", 1);
  798. if (ret)
  799. goto err;
  800. ret = regmap_bulk_write(dev->regmap, 0x192, "\x7f\xf7\xff", 3);
  801. if (ret)
  802. goto err;
  803. } else {
  804. ret = regmap_bulk_write(dev->regmap, 0x192, "\x00\x0f\xff", 3);
  805. if (ret)
  806. goto err;
  807. ret = regmap_bulk_write(dev->regmap, 0x0bc, "\x08", 1);
  808. if (ret)
  809. goto err;
  810. ret = rtl2832_wr_demod_reg(dev, DVBT_PIP_ON, 0x0);
  811. if (ret)
  812. goto err;
  813. ret = regmap_bulk_write(dev->regmap, 0x10c, "\x00\x00", 2);
  814. if (ret)
  815. goto err;
  816. ret = rtl2832_wr_demod_reg(dev, DVBT_SOFT_RST, 0x1);
  817. if (ret)
  818. goto err;
  819. }
  820. dev->slave_ts = enable;
  821. return 0;
  822. err:
  823. dev_dbg(&client->dev, "failed=%d\n", ret);
  824. return ret;
  825. }
  826. static int rtl2832_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
  827. {
  828. struct rtl2832_dev *dev = fe->demodulator_priv;
  829. struct i2c_client *client = dev->client;
  830. int ret;
  831. u8 u8tmp;
  832. dev_dbg(&client->dev, "onoff=%d, slave_ts=%d\n", onoff, dev->slave_ts);
  833. /* enable / disable PID filter */
  834. if (onoff)
  835. u8tmp = 0x80;
  836. else
  837. u8tmp = 0x00;
  838. if (dev->slave_ts)
  839. ret = regmap_update_bits(dev->regmap, 0x021, 0xc0, u8tmp);
  840. else
  841. ret = regmap_update_bits(dev->regmap, 0x061, 0xc0, u8tmp);
  842. if (ret)
  843. goto err;
  844. return 0;
  845. err:
  846. dev_dbg(&client->dev, "failed=%d\n", ret);
  847. return ret;
  848. }
  849. static int rtl2832_pid_filter(struct dvb_frontend *fe, u8 index, u16 pid,
  850. int onoff)
  851. {
  852. struct rtl2832_dev *dev = fe->demodulator_priv;
  853. struct i2c_client *client = dev->client;
  854. int ret;
  855. u8 buf[4];
  856. dev_dbg(&client->dev, "index=%d pid=%04x onoff=%d slave_ts=%d\n",
  857. index, pid, onoff, dev->slave_ts);
  858. /* skip invalid PIDs (0x2000) */
  859. if (pid > 0x1fff || index > 32)
  860. return 0;
  861. if (onoff)
  862. set_bit(index, &dev->filters);
  863. else
  864. clear_bit(index, &dev->filters);
  865. /* enable / disable PIDs */
  866. buf[0] = (dev->filters >> 0) & 0xff;
  867. buf[1] = (dev->filters >> 8) & 0xff;
  868. buf[2] = (dev->filters >> 16) & 0xff;
  869. buf[3] = (dev->filters >> 24) & 0xff;
  870. if (dev->slave_ts)
  871. ret = regmap_bulk_write(dev->regmap, 0x022, buf, 4);
  872. else
  873. ret = regmap_bulk_write(dev->regmap, 0x062, buf, 4);
  874. if (ret)
  875. goto err;
  876. /* add PID */
  877. buf[0] = (pid >> 8) & 0xff;
  878. buf[1] = (pid >> 0) & 0xff;
  879. if (dev->slave_ts)
  880. ret = regmap_bulk_write(dev->regmap, 0x026 + 2 * index, buf, 2);
  881. else
  882. ret = regmap_bulk_write(dev->regmap, 0x066 + 2 * index, buf, 2);
  883. if (ret)
  884. goto err;
  885. return 0;
  886. err:
  887. dev_dbg(&client->dev, "failed=%d\n", ret);
  888. return ret;
  889. }
  890. static int rtl2832_probe(struct i2c_client *client,
  891. const struct i2c_device_id *id)
  892. {
  893. struct rtl2832_platform_data *pdata = client->dev.platform_data;
  894. struct i2c_adapter *i2c = client->adapter;
  895. struct rtl2832_dev *dev;
  896. int ret;
  897. u8 tmp;
  898. static const struct regmap_range_cfg regmap_range_cfg[] = {
  899. {
  900. .selector_reg = 0x00,
  901. .selector_mask = 0xff,
  902. .selector_shift = 0,
  903. .window_start = 0,
  904. .window_len = 0x100,
  905. .range_min = 0 * 0x100,
  906. .range_max = 5 * 0x100,
  907. },
  908. };
  909. dev_dbg(&client->dev, "\n");
  910. /* allocate memory for the internal state */
  911. dev = kzalloc(sizeof(struct rtl2832_dev), GFP_KERNEL);
  912. if (dev == NULL) {
  913. ret = -ENOMEM;
  914. goto err;
  915. }
  916. /* setup the state */
  917. i2c_set_clientdata(client, dev);
  918. dev->client = client;
  919. dev->pdata = client->dev.platform_data;
  920. dev->sleeping = true;
  921. INIT_DELAYED_WORK(&dev->i2c_gate_work, rtl2832_i2c_gate_work);
  922. /* create regmap */
  923. dev->regmap_config.reg_bits = 8,
  924. dev->regmap_config.val_bits = 8,
  925. dev->regmap_config.volatile_reg = rtl2832_volatile_reg,
  926. dev->regmap_config.max_register = 5 * 0x100,
  927. dev->regmap_config.ranges = regmap_range_cfg,
  928. dev->regmap_config.num_ranges = ARRAY_SIZE(regmap_range_cfg),
  929. dev->regmap_config.cache_type = REGCACHE_NONE,
  930. dev->regmap = regmap_init_i2c(client, &dev->regmap_config);
  931. if (IS_ERR(dev->regmap)) {
  932. ret = PTR_ERR(dev->regmap);
  933. goto err_kfree;
  934. }
  935. /* check if the demod is there */
  936. ret = regmap_bulk_read(dev->regmap, 0x000, &tmp, 1);
  937. if (ret)
  938. goto err_regmap_exit;
  939. /* create muxed i2c adapter for demod tuner bus */
  940. dev->muxc = i2c_mux_alloc(i2c, &i2c->dev, 1, 0, I2C_MUX_LOCKED,
  941. rtl2832_select, rtl2832_deselect);
  942. if (!dev->muxc) {
  943. ret = -ENOMEM;
  944. goto err_regmap_exit;
  945. }
  946. dev->muxc->priv = dev;
  947. ret = i2c_mux_add_adapter(dev->muxc, 0, 0, 0);
  948. if (ret)
  949. goto err_regmap_exit;
  950. /* create dvb_frontend */
  951. memcpy(&dev->fe.ops, &rtl2832_ops, sizeof(struct dvb_frontend_ops));
  952. dev->fe.demodulator_priv = dev;
  953. /* setup callbacks */
  954. pdata->get_dvb_frontend = rtl2832_get_dvb_frontend;
  955. pdata->get_i2c_adapter = rtl2832_get_i2c_adapter;
  956. pdata->slave_ts_ctrl = rtl2832_slave_ts_ctrl;
  957. pdata->pid_filter = rtl2832_pid_filter;
  958. pdata->pid_filter_ctrl = rtl2832_pid_filter_ctrl;
  959. pdata->regmap = dev->regmap;
  960. dev_info(&client->dev, "Realtek RTL2832 successfully attached\n");
  961. return 0;
  962. err_regmap_exit:
  963. regmap_exit(dev->regmap);
  964. err_kfree:
  965. kfree(dev);
  966. err:
  967. dev_dbg(&client->dev, "failed=%d\n", ret);
  968. return ret;
  969. }
  970. static int rtl2832_remove(struct i2c_client *client)
  971. {
  972. struct rtl2832_dev *dev = i2c_get_clientdata(client);
  973. dev_dbg(&client->dev, "\n");
  974. cancel_delayed_work_sync(&dev->i2c_gate_work);
  975. i2c_mux_del_adapters(dev->muxc);
  976. regmap_exit(dev->regmap);
  977. kfree(dev);
  978. return 0;
  979. }
  980. static const struct i2c_device_id rtl2832_id_table[] = {
  981. {"rtl2832", 0},
  982. {}
  983. };
  984. MODULE_DEVICE_TABLE(i2c, rtl2832_id_table);
  985. static struct i2c_driver rtl2832_driver = {
  986. .driver = {
  987. .name = "rtl2832",
  988. .suppress_bind_attrs = true,
  989. },
  990. .probe = rtl2832_probe,
  991. .remove = rtl2832_remove,
  992. .id_table = rtl2832_id_table,
  993. };
  994. module_i2c_driver(rtl2832_driver);
  995. MODULE_AUTHOR("Thomas Mair <mair.thomas86@gmail.com>");
  996. MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
  997. MODULE_DESCRIPTION("Realtek RTL2832 DVB-T demodulator driver");
  998. MODULE_LICENSE("GPL");