tda10048.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. NXP TDA10048HN DVB OFDM demodulator driver
  4. Copyright (C) 2009 Steven Toth <stoth@kernellabs.com>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/string.h>
  10. #include <linux/slab.h>
  11. #include <linux/delay.h>
  12. #include <linux/math64.h>
  13. #include <asm/div64.h>
  14. #include <media/dvb_frontend.h>
  15. #include <linux/int_log.h>
  16. #include "tda10048.h"
  17. #define TDA10048_DEFAULT_FIRMWARE "dvb-fe-tda10048-1.0.fw"
  18. #define TDA10048_DEFAULT_FIRMWARE_SIZE 24878
  19. /* Register name definitions */
  20. #define TDA10048_IDENTITY 0x00
  21. #define TDA10048_VERSION 0x01
  22. #define TDA10048_DSP_CODE_CPT 0x0C
  23. #define TDA10048_DSP_CODE_IN 0x0E
  24. #define TDA10048_IN_CONF1 0x10
  25. #define TDA10048_IN_CONF2 0x11
  26. #define TDA10048_IN_CONF3 0x12
  27. #define TDA10048_OUT_CONF1 0x14
  28. #define TDA10048_OUT_CONF2 0x15
  29. #define TDA10048_OUT_CONF3 0x16
  30. #define TDA10048_AUTO 0x18
  31. #define TDA10048_SYNC_STATUS 0x1A
  32. #define TDA10048_CONF_C4_1 0x1E
  33. #define TDA10048_CONF_C4_2 0x1F
  34. #define TDA10048_CODE_IN_RAM 0x20
  35. #define TDA10048_CHANNEL_INFO1_R 0x22
  36. #define TDA10048_CHANNEL_INFO2_R 0x23
  37. #define TDA10048_CHANNEL_INFO1 0x24
  38. #define TDA10048_CHANNEL_INFO2 0x25
  39. #define TDA10048_TIME_ERROR_R 0x26
  40. #define TDA10048_TIME_ERROR 0x27
  41. #define TDA10048_FREQ_ERROR_LSB_R 0x28
  42. #define TDA10048_FREQ_ERROR_MSB_R 0x29
  43. #define TDA10048_FREQ_ERROR_LSB 0x2A
  44. #define TDA10048_FREQ_ERROR_MSB 0x2B
  45. #define TDA10048_IT_SEL 0x30
  46. #define TDA10048_IT_STAT 0x32
  47. #define TDA10048_DSP_AD_LSB 0x3C
  48. #define TDA10048_DSP_AD_MSB 0x3D
  49. #define TDA10048_DSP_REG_LSB 0x3E
  50. #define TDA10048_DSP_REG_MSB 0x3F
  51. #define TDA10048_CONF_TRISTATE1 0x44
  52. #define TDA10048_CONF_TRISTATE2 0x45
  53. #define TDA10048_CONF_POLARITY 0x46
  54. #define TDA10048_GPIO_SP_DS0 0x48
  55. #define TDA10048_GPIO_SP_DS1 0x49
  56. #define TDA10048_GPIO_SP_DS2 0x4A
  57. #define TDA10048_GPIO_SP_DS3 0x4B
  58. #define TDA10048_GPIO_OUT_SEL 0x4C
  59. #define TDA10048_GPIO_SELECT 0x4D
  60. #define TDA10048_IC_MODE 0x4E
  61. #define TDA10048_CONF_XO 0x50
  62. #define TDA10048_CONF_PLL1 0x51
  63. #define TDA10048_CONF_PLL2 0x52
  64. #define TDA10048_CONF_PLL3 0x53
  65. #define TDA10048_CONF_ADC 0x54
  66. #define TDA10048_CONF_ADC_2 0x55
  67. #define TDA10048_CONF_C1_1 0x60
  68. #define TDA10048_CONF_C1_3 0x62
  69. #define TDA10048_AGC_CONF 0x70
  70. #define TDA10048_AGC_THRESHOLD_LSB 0x72
  71. #define TDA10048_AGC_THRESHOLD_MSB 0x73
  72. #define TDA10048_AGC_RENORM 0x74
  73. #define TDA10048_AGC_GAINS 0x76
  74. #define TDA10048_AGC_TUN_MIN 0x78
  75. #define TDA10048_AGC_TUN_MAX 0x79
  76. #define TDA10048_AGC_IF_MIN 0x7A
  77. #define TDA10048_AGC_IF_MAX 0x7B
  78. #define TDA10048_AGC_TUN_LEVEL 0x7E
  79. #define TDA10048_AGC_IF_LEVEL 0x7F
  80. #define TDA10048_DIG_AGC_LEVEL 0x81
  81. #define TDA10048_FREQ_PHY2_LSB 0x86
  82. #define TDA10048_FREQ_PHY2_MSB 0x87
  83. #define TDA10048_TIME_INVWREF_LSB 0x88
  84. #define TDA10048_TIME_INVWREF_MSB 0x89
  85. #define TDA10048_TIME_WREF_LSB 0x8A
  86. #define TDA10048_TIME_WREF_MID1 0x8B
  87. #define TDA10048_TIME_WREF_MID2 0x8C
  88. #define TDA10048_TIME_WREF_MSB 0x8D
  89. #define TDA10048_NP_OUT 0xA2
  90. #define TDA10048_CELL_ID_LSB 0xA4
  91. #define TDA10048_CELL_ID_MSB 0xA5
  92. #define TDA10048_EXTTPS_ODD 0xAA
  93. #define TDA10048_EXTTPS_EVEN 0xAB
  94. #define TDA10048_TPS_LENGTH 0xAC
  95. #define TDA10048_FREE_REG_1 0xB2
  96. #define TDA10048_FREE_REG_2 0xB3
  97. #define TDA10048_CONF_C3_1 0xC0
  98. #define TDA10048_CVBER_CTRL 0xC2
  99. #define TDA10048_CBER_NMAX_LSB 0xC4
  100. #define TDA10048_CBER_NMAX_MSB 0xC5
  101. #define TDA10048_CBER_LSB 0xC6
  102. #define TDA10048_CBER_MSB 0xC7
  103. #define TDA10048_VBER_LSB 0xC8
  104. #define TDA10048_VBER_MID 0xC9
  105. #define TDA10048_VBER_MSB 0xCA
  106. #define TDA10048_CVBER_LUT 0xCC
  107. #define TDA10048_UNCOR_CTRL 0xCD
  108. #define TDA10048_UNCOR_CPT_LSB 0xCE
  109. #define TDA10048_UNCOR_CPT_MSB 0xCF
  110. #define TDA10048_SOFT_IT_C3 0xD6
  111. #define TDA10048_CONF_TS2 0xE0
  112. #define TDA10048_CONF_TS1 0xE1
  113. static unsigned int debug;
  114. #define dprintk(level, fmt, arg...)\
  115. do { if (debug >= level)\
  116. printk(KERN_DEBUG "tda10048: " fmt, ## arg);\
  117. } while (0)
  118. struct tda10048_state {
  119. struct i2c_adapter *i2c;
  120. /* We'll cache and update the attach config settings */
  121. struct tda10048_config config;
  122. struct dvb_frontend frontend;
  123. int fwloaded;
  124. u32 freq_if_hz;
  125. u32 xtal_hz;
  126. u32 pll_mfactor;
  127. u32 pll_nfactor;
  128. u32 pll_pfactor;
  129. u32 sample_freq;
  130. u32 bandwidth;
  131. };
  132. static struct init_tab {
  133. u8 reg;
  134. u16 data;
  135. } init_tab[] = {
  136. { TDA10048_CONF_PLL1, 0x08 },
  137. { TDA10048_CONF_ADC_2, 0x00 },
  138. { TDA10048_CONF_C4_1, 0x00 },
  139. { TDA10048_CONF_PLL1, 0x0f },
  140. { TDA10048_CONF_PLL2, 0x0a },
  141. { TDA10048_CONF_PLL3, 0x43 },
  142. { TDA10048_FREQ_PHY2_LSB, 0x02 },
  143. { TDA10048_FREQ_PHY2_MSB, 0x0a },
  144. { TDA10048_TIME_WREF_LSB, 0xbd },
  145. { TDA10048_TIME_WREF_MID1, 0xe4 },
  146. { TDA10048_TIME_WREF_MID2, 0xa8 },
  147. { TDA10048_TIME_WREF_MSB, 0x02 },
  148. { TDA10048_TIME_INVWREF_LSB, 0x04 },
  149. { TDA10048_TIME_INVWREF_MSB, 0x06 },
  150. { TDA10048_CONF_C4_1, 0x00 },
  151. { TDA10048_CONF_C1_1, 0xa8 },
  152. { TDA10048_AGC_CONF, 0x16 },
  153. { TDA10048_CONF_C1_3, 0x0b },
  154. { TDA10048_AGC_TUN_MIN, 0x00 },
  155. { TDA10048_AGC_TUN_MAX, 0xff },
  156. { TDA10048_AGC_IF_MIN, 0x00 },
  157. { TDA10048_AGC_IF_MAX, 0xff },
  158. { TDA10048_AGC_THRESHOLD_MSB, 0x00 },
  159. { TDA10048_AGC_THRESHOLD_LSB, 0x70 },
  160. { TDA10048_CVBER_CTRL, 0x38 },
  161. { TDA10048_AGC_GAINS, 0x12 },
  162. { TDA10048_CONF_XO, 0x00 },
  163. { TDA10048_CONF_TS1, 0x07 },
  164. { TDA10048_IC_MODE, 0x00 },
  165. { TDA10048_CONF_TS2, 0xc0 },
  166. { TDA10048_CONF_TRISTATE1, 0x21 },
  167. { TDA10048_CONF_TRISTATE2, 0x00 },
  168. { TDA10048_CONF_POLARITY, 0x00 },
  169. { TDA10048_CONF_C4_2, 0x04 },
  170. { TDA10048_CONF_ADC, 0x60 },
  171. { TDA10048_CONF_ADC_2, 0x10 },
  172. { TDA10048_CONF_ADC, 0x60 },
  173. { TDA10048_CONF_ADC_2, 0x00 },
  174. { TDA10048_CONF_C1_1, 0xa8 },
  175. { TDA10048_UNCOR_CTRL, 0x00 },
  176. { TDA10048_CONF_C4_2, 0x04 },
  177. };
  178. static struct pll_tab {
  179. u32 clk_freq_khz;
  180. u32 if_freq_khz;
  181. } pll_tab[] = {
  182. { TDA10048_CLK_4000, TDA10048_IF_36130 },
  183. { TDA10048_CLK_16000, TDA10048_IF_3300 },
  184. { TDA10048_CLK_16000, TDA10048_IF_3500 },
  185. { TDA10048_CLK_16000, TDA10048_IF_3800 },
  186. { TDA10048_CLK_16000, TDA10048_IF_4000 },
  187. { TDA10048_CLK_16000, TDA10048_IF_4300 },
  188. { TDA10048_CLK_16000, TDA10048_IF_4500 },
  189. { TDA10048_CLK_16000, TDA10048_IF_5000 },
  190. { TDA10048_CLK_16000, TDA10048_IF_36130 },
  191. };
  192. static int tda10048_writereg(struct tda10048_state *state, u8 reg, u8 data)
  193. {
  194. struct tda10048_config *config = &state->config;
  195. int ret;
  196. u8 buf[] = { reg, data };
  197. struct i2c_msg msg = {
  198. .addr = config->demod_address,
  199. .flags = 0, .buf = buf, .len = 2 };
  200. dprintk(2, "%s(reg = 0x%02x, data = 0x%02x)\n", __func__, reg, data);
  201. ret = i2c_transfer(state->i2c, &msg, 1);
  202. if (ret != 1)
  203. printk("%s: writereg error (ret == %i)\n", __func__, ret);
  204. return (ret != 1) ? -1 : 0;
  205. }
  206. static u8 tda10048_readreg(struct tda10048_state *state, u8 reg)
  207. {
  208. struct tda10048_config *config = &state->config;
  209. int ret;
  210. u8 b0[] = { reg };
  211. u8 b1[] = { 0 };
  212. struct i2c_msg msg[] = {
  213. { .addr = config->demod_address,
  214. .flags = 0, .buf = b0, .len = 1 },
  215. { .addr = config->demod_address,
  216. .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  217. dprintk(2, "%s(reg = 0x%02x)\n", __func__, reg);
  218. ret = i2c_transfer(state->i2c, msg, 2);
  219. if (ret != 2)
  220. printk(KERN_ERR "%s: readreg error (ret == %i)\n",
  221. __func__, ret);
  222. return b1[0];
  223. }
  224. static int tda10048_writeregbulk(struct tda10048_state *state, u8 reg,
  225. const u8 *data, u16 len)
  226. {
  227. struct tda10048_config *config = &state->config;
  228. int ret = -EREMOTEIO;
  229. struct i2c_msg msg;
  230. u8 *buf;
  231. dprintk(2, "%s(%d, ?, len = %d)\n", __func__, reg, len);
  232. buf = kmalloc(len + 1, GFP_KERNEL);
  233. if (buf == NULL) {
  234. ret = -ENOMEM;
  235. goto error;
  236. }
  237. *buf = reg;
  238. memcpy(buf + 1, data, len);
  239. msg.addr = config->demod_address;
  240. msg.flags = 0;
  241. msg.buf = buf;
  242. msg.len = len + 1;
  243. dprintk(2, "%s(): write len = %d\n",
  244. __func__, msg.len);
  245. ret = i2c_transfer(state->i2c, &msg, 1);
  246. if (ret != 1) {
  247. printk(KERN_ERR "%s(): writereg error err %i\n",
  248. __func__, ret);
  249. ret = -EREMOTEIO;
  250. }
  251. error:
  252. kfree(buf);
  253. return ret;
  254. }
  255. static int tda10048_set_phy2(struct dvb_frontend *fe, u32 sample_freq_hz,
  256. u32 if_hz)
  257. {
  258. struct tda10048_state *state = fe->demodulator_priv;
  259. u64 t;
  260. dprintk(1, "%s()\n", __func__);
  261. if (sample_freq_hz == 0)
  262. return -EINVAL;
  263. if (if_hz < (sample_freq_hz / 2)) {
  264. /* PHY2 = (if2/fs) * 2^15 */
  265. t = if_hz;
  266. t *= 10;
  267. t *= 32768;
  268. do_div(t, sample_freq_hz);
  269. t += 5;
  270. do_div(t, 10);
  271. } else {
  272. /* PHY2 = ((IF1-fs)/fs) * 2^15 */
  273. t = sample_freq_hz - if_hz;
  274. t *= 10;
  275. t *= 32768;
  276. do_div(t, sample_freq_hz);
  277. t += 5;
  278. do_div(t, 10);
  279. t = ~t + 1;
  280. }
  281. tda10048_writereg(state, TDA10048_FREQ_PHY2_LSB, (u8)t);
  282. tda10048_writereg(state, TDA10048_FREQ_PHY2_MSB, (u8)(t >> 8));
  283. return 0;
  284. }
  285. static int tda10048_set_wref(struct dvb_frontend *fe, u32 sample_freq_hz,
  286. u32 bw)
  287. {
  288. struct tda10048_state *state = fe->demodulator_priv;
  289. u64 t;
  290. u32 z;
  291. dprintk(1, "%s()\n", __func__);
  292. if (sample_freq_hz == 0)
  293. return -EINVAL;
  294. /* WREF = (B / (7 * fs)) * 2^31 */
  295. t = bw * 10;
  296. /* avoid warning: this decimal constant is unsigned only in ISO C90 */
  297. /* t *= 2147483648 on 32bit platforms */
  298. t *= (2048 * 1024);
  299. t *= 1024;
  300. /*
  301. * Sample frequency is typically 55 MHz, with a theoretical maximum of
  302. * 69 MHz. With a 32 bit z we have enough accuracy for up to 613 MHz.
  303. */
  304. z = 7 * sample_freq_hz;
  305. do_div(t, z);
  306. t += 5;
  307. do_div(t, 10);
  308. tda10048_writereg(state, TDA10048_TIME_WREF_LSB, (u8)t);
  309. tda10048_writereg(state, TDA10048_TIME_WREF_MID1, (u8)(t >> 8));
  310. tda10048_writereg(state, TDA10048_TIME_WREF_MID2, (u8)(t >> 16));
  311. tda10048_writereg(state, TDA10048_TIME_WREF_MSB, (u8)(t >> 24));
  312. return 0;
  313. }
  314. static int tda10048_set_invwref(struct dvb_frontend *fe, u32 sample_freq_hz,
  315. u32 bw)
  316. {
  317. struct tda10048_state *state = fe->demodulator_priv;
  318. u64 t;
  319. dprintk(1, "%s()\n", __func__);
  320. if (sample_freq_hz == 0)
  321. return -EINVAL;
  322. /* INVWREF = ((7 * fs) / B) * 2^5 */
  323. t = sample_freq_hz;
  324. t *= 7;
  325. t *= 32;
  326. t *= 10;
  327. do_div(t, bw);
  328. t += 5;
  329. do_div(t, 10);
  330. tda10048_writereg(state, TDA10048_TIME_INVWREF_LSB, (u8)t);
  331. tda10048_writereg(state, TDA10048_TIME_INVWREF_MSB, (u8)(t >> 8));
  332. return 0;
  333. }
  334. static int tda10048_set_bandwidth(struct dvb_frontend *fe,
  335. u32 bw)
  336. {
  337. struct tda10048_state *state = fe->demodulator_priv;
  338. dprintk(1, "%s(bw=%d)\n", __func__, bw);
  339. /* Bandwidth setting may need to be adjusted */
  340. switch (bw) {
  341. case 6000000:
  342. case 7000000:
  343. case 8000000:
  344. tda10048_set_wref(fe, state->sample_freq, bw);
  345. tda10048_set_invwref(fe, state->sample_freq, bw);
  346. break;
  347. default:
  348. printk(KERN_ERR "%s() invalid bandwidth\n", __func__);
  349. return -EINVAL;
  350. }
  351. state->bandwidth = bw;
  352. return 0;
  353. }
  354. static int tda10048_set_if(struct dvb_frontend *fe, u32 bw)
  355. {
  356. struct tda10048_state *state = fe->demodulator_priv;
  357. struct tda10048_config *config = &state->config;
  358. int i;
  359. u32 if_freq_khz;
  360. u64 sample_freq;
  361. dprintk(1, "%s(bw = %d)\n", __func__, bw);
  362. /* based on target bandwidth and clk we calculate pll factors */
  363. switch (bw) {
  364. case 6000000:
  365. if_freq_khz = config->dtv6_if_freq_khz;
  366. break;
  367. case 7000000:
  368. if_freq_khz = config->dtv7_if_freq_khz;
  369. break;
  370. case 8000000:
  371. if_freq_khz = config->dtv8_if_freq_khz;
  372. break;
  373. default:
  374. printk(KERN_ERR "%s() no default\n", __func__);
  375. return -EINVAL;
  376. }
  377. for (i = 0; i < ARRAY_SIZE(pll_tab); i++) {
  378. if ((pll_tab[i].clk_freq_khz == config->clk_freq_khz) &&
  379. (pll_tab[i].if_freq_khz == if_freq_khz)) {
  380. state->freq_if_hz = pll_tab[i].if_freq_khz * 1000;
  381. state->xtal_hz = pll_tab[i].clk_freq_khz * 1000;
  382. break;
  383. }
  384. }
  385. if (i == ARRAY_SIZE(pll_tab)) {
  386. printk(KERN_ERR "%s() Incorrect attach settings\n",
  387. __func__);
  388. return -EINVAL;
  389. }
  390. dprintk(1, "- freq_if_hz = %d\n", state->freq_if_hz);
  391. dprintk(1, "- xtal_hz = %d\n", state->xtal_hz);
  392. dprintk(1, "- pll_mfactor = %d\n", state->pll_mfactor);
  393. dprintk(1, "- pll_nfactor = %d\n", state->pll_nfactor);
  394. dprintk(1, "- pll_pfactor = %d\n", state->pll_pfactor);
  395. /* Calculate the sample frequency */
  396. sample_freq = state->xtal_hz;
  397. sample_freq *= state->pll_mfactor + 45;
  398. do_div(sample_freq, state->pll_nfactor + 1);
  399. do_div(sample_freq, state->pll_pfactor + 4);
  400. state->sample_freq = sample_freq;
  401. dprintk(1, "- sample_freq = %d\n", state->sample_freq);
  402. /* Update the I/F */
  403. tda10048_set_phy2(fe, state->sample_freq, state->freq_if_hz);
  404. return 0;
  405. }
  406. static int tda10048_firmware_upload(struct dvb_frontend *fe)
  407. {
  408. struct tda10048_state *state = fe->demodulator_priv;
  409. struct tda10048_config *config = &state->config;
  410. const struct firmware *fw;
  411. int ret;
  412. int pos = 0;
  413. int cnt;
  414. u8 wlen = config->fwbulkwritelen;
  415. if ((wlen != TDA10048_BULKWRITE_200) && (wlen != TDA10048_BULKWRITE_50))
  416. wlen = TDA10048_BULKWRITE_200;
  417. /* request the firmware, this will block and timeout */
  418. printk(KERN_INFO "%s: waiting for firmware upload (%s)...\n",
  419. __func__,
  420. TDA10048_DEFAULT_FIRMWARE);
  421. ret = request_firmware(&fw, TDA10048_DEFAULT_FIRMWARE,
  422. state->i2c->dev.parent);
  423. if (ret) {
  424. printk(KERN_ERR "%s: Upload failed. (file not found?)\n",
  425. __func__);
  426. return -EIO;
  427. } else {
  428. printk(KERN_INFO "%s: firmware read %zu bytes.\n",
  429. __func__,
  430. fw->size);
  431. ret = 0;
  432. }
  433. if (fw->size != TDA10048_DEFAULT_FIRMWARE_SIZE) {
  434. printk(KERN_ERR "%s: firmware incorrect size\n", __func__);
  435. ret = -EIO;
  436. } else {
  437. printk(KERN_INFO "%s: firmware uploading\n", __func__);
  438. /* Soft reset */
  439. tda10048_writereg(state, TDA10048_CONF_TRISTATE1,
  440. tda10048_readreg(state, TDA10048_CONF_TRISTATE1)
  441. & 0xfe);
  442. tda10048_writereg(state, TDA10048_CONF_TRISTATE1,
  443. tda10048_readreg(state, TDA10048_CONF_TRISTATE1)
  444. | 0x01);
  445. /* Put the demod into host download mode */
  446. tda10048_writereg(state, TDA10048_CONF_C4_1,
  447. tda10048_readreg(state, TDA10048_CONF_C4_1) & 0xf9);
  448. /* Boot the DSP */
  449. tda10048_writereg(state, TDA10048_CONF_C4_1,
  450. tda10048_readreg(state, TDA10048_CONF_C4_1) | 0x08);
  451. /* Prepare for download */
  452. tda10048_writereg(state, TDA10048_DSP_CODE_CPT, 0);
  453. /* Download the firmware payload */
  454. while (pos < fw->size) {
  455. if ((fw->size - pos) > wlen)
  456. cnt = wlen;
  457. else
  458. cnt = fw->size - pos;
  459. tda10048_writeregbulk(state, TDA10048_DSP_CODE_IN,
  460. &fw->data[pos], cnt);
  461. pos += cnt;
  462. }
  463. ret = -EIO;
  464. /* Wait up to 250ms for the DSP to boot */
  465. for (cnt = 0; cnt < 250 ; cnt += 10) {
  466. msleep(10);
  467. if (tda10048_readreg(state, TDA10048_SYNC_STATUS)
  468. & 0x40) {
  469. ret = 0;
  470. break;
  471. }
  472. }
  473. }
  474. release_firmware(fw);
  475. if (ret == 0) {
  476. printk(KERN_INFO "%s: firmware uploaded\n", __func__);
  477. state->fwloaded = 1;
  478. } else
  479. printk(KERN_ERR "%s: firmware upload failed\n", __func__);
  480. return ret;
  481. }
  482. static int tda10048_set_inversion(struct dvb_frontend *fe, int inversion)
  483. {
  484. struct tda10048_state *state = fe->demodulator_priv;
  485. dprintk(1, "%s(%d)\n", __func__, inversion);
  486. if (inversion == TDA10048_INVERSION_ON)
  487. tda10048_writereg(state, TDA10048_CONF_C1_1,
  488. tda10048_readreg(state, TDA10048_CONF_C1_1) | 0x20);
  489. else
  490. tda10048_writereg(state, TDA10048_CONF_C1_1,
  491. tda10048_readreg(state, TDA10048_CONF_C1_1) & 0xdf);
  492. return 0;
  493. }
  494. /* Retrieve the demod settings */
  495. static int tda10048_get_tps(struct tda10048_state *state,
  496. struct dtv_frontend_properties *p)
  497. {
  498. u8 val;
  499. /* Make sure the TPS regs are valid */
  500. if (!(tda10048_readreg(state, TDA10048_AUTO) & 0x01))
  501. return -EAGAIN;
  502. val = tda10048_readreg(state, TDA10048_OUT_CONF2);
  503. switch ((val & 0x60) >> 5) {
  504. case 0:
  505. p->modulation = QPSK;
  506. break;
  507. case 1:
  508. p->modulation = QAM_16;
  509. break;
  510. case 2:
  511. p->modulation = QAM_64;
  512. break;
  513. }
  514. switch ((val & 0x18) >> 3) {
  515. case 0:
  516. p->hierarchy = HIERARCHY_NONE;
  517. break;
  518. case 1:
  519. p->hierarchy = HIERARCHY_1;
  520. break;
  521. case 2:
  522. p->hierarchy = HIERARCHY_2;
  523. break;
  524. case 3:
  525. p->hierarchy = HIERARCHY_4;
  526. break;
  527. }
  528. switch (val & 0x07) {
  529. case 0:
  530. p->code_rate_HP = FEC_1_2;
  531. break;
  532. case 1:
  533. p->code_rate_HP = FEC_2_3;
  534. break;
  535. case 2:
  536. p->code_rate_HP = FEC_3_4;
  537. break;
  538. case 3:
  539. p->code_rate_HP = FEC_5_6;
  540. break;
  541. case 4:
  542. p->code_rate_HP = FEC_7_8;
  543. break;
  544. }
  545. val = tda10048_readreg(state, TDA10048_OUT_CONF3);
  546. switch (val & 0x07) {
  547. case 0:
  548. p->code_rate_LP = FEC_1_2;
  549. break;
  550. case 1:
  551. p->code_rate_LP = FEC_2_3;
  552. break;
  553. case 2:
  554. p->code_rate_LP = FEC_3_4;
  555. break;
  556. case 3:
  557. p->code_rate_LP = FEC_5_6;
  558. break;
  559. case 4:
  560. p->code_rate_LP = FEC_7_8;
  561. break;
  562. }
  563. val = tda10048_readreg(state, TDA10048_OUT_CONF1);
  564. switch ((val & 0x0c) >> 2) {
  565. case 0:
  566. p->guard_interval = GUARD_INTERVAL_1_32;
  567. break;
  568. case 1:
  569. p->guard_interval = GUARD_INTERVAL_1_16;
  570. break;
  571. case 2:
  572. p->guard_interval = GUARD_INTERVAL_1_8;
  573. break;
  574. case 3:
  575. p->guard_interval = GUARD_INTERVAL_1_4;
  576. break;
  577. }
  578. switch (val & 0x03) {
  579. case 0:
  580. p->transmission_mode = TRANSMISSION_MODE_2K;
  581. break;
  582. case 1:
  583. p->transmission_mode = TRANSMISSION_MODE_8K;
  584. break;
  585. }
  586. return 0;
  587. }
  588. static int tda10048_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  589. {
  590. struct tda10048_state *state = fe->demodulator_priv;
  591. struct tda10048_config *config = &state->config;
  592. dprintk(1, "%s(%d)\n", __func__, enable);
  593. if (config->disable_gate_access)
  594. return 0;
  595. if (enable)
  596. return tda10048_writereg(state, TDA10048_CONF_C4_1,
  597. tda10048_readreg(state, TDA10048_CONF_C4_1) | 0x02);
  598. else
  599. return tda10048_writereg(state, TDA10048_CONF_C4_1,
  600. tda10048_readreg(state, TDA10048_CONF_C4_1) & 0xfd);
  601. }
  602. static int tda10048_output_mode(struct dvb_frontend *fe, int serial)
  603. {
  604. struct tda10048_state *state = fe->demodulator_priv;
  605. dprintk(1, "%s(%d)\n", __func__, serial);
  606. /* Ensure pins are out of tri-state */
  607. tda10048_writereg(state, TDA10048_CONF_TRISTATE1, 0x21);
  608. tda10048_writereg(state, TDA10048_CONF_TRISTATE2, 0x00);
  609. if (serial) {
  610. tda10048_writereg(state, TDA10048_IC_MODE, 0x80 | 0x20);
  611. tda10048_writereg(state, TDA10048_CONF_TS2, 0xc0);
  612. } else {
  613. tda10048_writereg(state, TDA10048_IC_MODE, 0x00);
  614. tda10048_writereg(state, TDA10048_CONF_TS2, 0x01);
  615. }
  616. return 0;
  617. }
  618. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  619. /* TODO: Support manual tuning with specific params */
  620. static int tda10048_set_frontend(struct dvb_frontend *fe)
  621. {
  622. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  623. struct tda10048_state *state = fe->demodulator_priv;
  624. dprintk(1, "%s(frequency=%d)\n", __func__, p->frequency);
  625. /* Update the I/F pll's if the bandwidth changes */
  626. if (p->bandwidth_hz != state->bandwidth) {
  627. tda10048_set_if(fe, p->bandwidth_hz);
  628. tda10048_set_bandwidth(fe, p->bandwidth_hz);
  629. }
  630. if (fe->ops.tuner_ops.set_params) {
  631. if (fe->ops.i2c_gate_ctrl)
  632. fe->ops.i2c_gate_ctrl(fe, 1);
  633. fe->ops.tuner_ops.set_params(fe);
  634. if (fe->ops.i2c_gate_ctrl)
  635. fe->ops.i2c_gate_ctrl(fe, 0);
  636. }
  637. /* Enable demod TPS auto detection and begin acquisition */
  638. tda10048_writereg(state, TDA10048_AUTO, 0x57);
  639. /* trigger cber and vber acquisition */
  640. tda10048_writereg(state, TDA10048_CVBER_CTRL, 0x3B);
  641. return 0;
  642. }
  643. /* Establish sane defaults and load firmware. */
  644. static int tda10048_init(struct dvb_frontend *fe)
  645. {
  646. struct tda10048_state *state = fe->demodulator_priv;
  647. struct tda10048_config *config = &state->config;
  648. int ret = 0, i;
  649. dprintk(1, "%s()\n", __func__);
  650. /* PLL */
  651. init_tab[4].data = (u8)(state->pll_mfactor);
  652. init_tab[5].data = (u8)(state->pll_nfactor) | 0x40;
  653. /* Apply register defaults */
  654. for (i = 0; i < ARRAY_SIZE(init_tab); i++)
  655. tda10048_writereg(state, init_tab[i].reg, init_tab[i].data);
  656. if (state->fwloaded == 0)
  657. ret = tda10048_firmware_upload(fe);
  658. /* Set either serial or parallel */
  659. tda10048_output_mode(fe, config->output_mode);
  660. /* Set inversion */
  661. tda10048_set_inversion(fe, config->inversion);
  662. /* Establish default RF values */
  663. tda10048_set_if(fe, 8000000);
  664. tda10048_set_bandwidth(fe, 8000000);
  665. /* Ensure we leave the gate closed */
  666. tda10048_i2c_gate_ctrl(fe, 0);
  667. return ret;
  668. }
  669. static int tda10048_read_status(struct dvb_frontend *fe, enum fe_status *status)
  670. {
  671. struct tda10048_state *state = fe->demodulator_priv;
  672. u8 reg;
  673. *status = 0;
  674. reg = tda10048_readreg(state, TDA10048_SYNC_STATUS);
  675. dprintk(1, "%s() status =0x%02x\n", __func__, reg);
  676. if (reg & 0x02)
  677. *status |= FE_HAS_CARRIER;
  678. if (reg & 0x04)
  679. *status |= FE_HAS_SIGNAL;
  680. if (reg & 0x08) {
  681. *status |= FE_HAS_LOCK;
  682. *status |= FE_HAS_VITERBI;
  683. *status |= FE_HAS_SYNC;
  684. }
  685. return 0;
  686. }
  687. static int tda10048_read_ber(struct dvb_frontend *fe, u32 *ber)
  688. {
  689. struct tda10048_state *state = fe->demodulator_priv;
  690. static u32 cber_current;
  691. u32 cber_nmax;
  692. u64 cber_tmp;
  693. dprintk(1, "%s()\n", __func__);
  694. /* update cber on interrupt */
  695. if (tda10048_readreg(state, TDA10048_SOFT_IT_C3) & 0x01) {
  696. cber_tmp = tda10048_readreg(state, TDA10048_CBER_MSB) << 8 |
  697. tda10048_readreg(state, TDA10048_CBER_LSB);
  698. cber_nmax = tda10048_readreg(state, TDA10048_CBER_NMAX_MSB) << 8 |
  699. tda10048_readreg(state, TDA10048_CBER_NMAX_LSB);
  700. cber_tmp *= 100000000;
  701. cber_tmp *= 2;
  702. cber_tmp = div_u64(cber_tmp, (cber_nmax * 32) + 1);
  703. cber_current = (u32)cber_tmp;
  704. /* retrigger cber acquisition */
  705. tda10048_writereg(state, TDA10048_CVBER_CTRL, 0x39);
  706. }
  707. /* actual cber is (*ber)/1e8 */
  708. *ber = cber_current;
  709. return 0;
  710. }
  711. static int tda10048_read_signal_strength(struct dvb_frontend *fe,
  712. u16 *signal_strength)
  713. {
  714. struct tda10048_state *state = fe->demodulator_priv;
  715. u8 v;
  716. dprintk(1, "%s()\n", __func__);
  717. *signal_strength = 65535;
  718. v = tda10048_readreg(state, TDA10048_NP_OUT);
  719. if (v > 0)
  720. *signal_strength -= (v << 8) | v;
  721. return 0;
  722. }
  723. /* SNR lookup table */
  724. static struct snr_tab {
  725. u8 val;
  726. u8 data;
  727. } snr_tab[] = {
  728. { 0, 0 },
  729. { 1, 246 },
  730. { 2, 215 },
  731. { 3, 198 },
  732. { 4, 185 },
  733. { 5, 176 },
  734. { 6, 168 },
  735. { 7, 161 },
  736. { 8, 155 },
  737. { 9, 150 },
  738. { 10, 146 },
  739. { 11, 141 },
  740. { 12, 138 },
  741. { 13, 134 },
  742. { 14, 131 },
  743. { 15, 128 },
  744. { 16, 125 },
  745. { 17, 122 },
  746. { 18, 120 },
  747. { 19, 118 },
  748. { 20, 115 },
  749. { 21, 113 },
  750. { 22, 111 },
  751. { 23, 109 },
  752. { 24, 107 },
  753. { 25, 106 },
  754. { 26, 104 },
  755. { 27, 102 },
  756. { 28, 101 },
  757. { 29, 99 },
  758. { 30, 98 },
  759. { 31, 96 },
  760. { 32, 95 },
  761. { 33, 94 },
  762. { 34, 92 },
  763. { 35, 91 },
  764. { 36, 90 },
  765. { 37, 89 },
  766. { 38, 88 },
  767. { 39, 86 },
  768. { 40, 85 },
  769. { 41, 84 },
  770. { 42, 83 },
  771. { 43, 82 },
  772. { 44, 81 },
  773. { 45, 80 },
  774. { 46, 79 },
  775. { 47, 78 },
  776. { 48, 77 },
  777. { 49, 76 },
  778. { 50, 76 },
  779. { 51, 75 },
  780. { 52, 74 },
  781. { 53, 73 },
  782. { 54, 72 },
  783. { 56, 71 },
  784. { 57, 70 },
  785. { 58, 69 },
  786. { 60, 68 },
  787. { 61, 67 },
  788. { 63, 66 },
  789. { 64, 65 },
  790. { 66, 64 },
  791. { 67, 63 },
  792. { 68, 62 },
  793. { 69, 62 },
  794. { 70, 61 },
  795. { 72, 60 },
  796. { 74, 59 },
  797. { 75, 58 },
  798. { 77, 57 },
  799. { 79, 56 },
  800. { 81, 55 },
  801. { 83, 54 },
  802. { 85, 53 },
  803. { 87, 52 },
  804. { 89, 51 },
  805. { 91, 50 },
  806. { 93, 49 },
  807. { 95, 48 },
  808. { 97, 47 },
  809. { 100, 46 },
  810. { 102, 45 },
  811. { 104, 44 },
  812. { 107, 43 },
  813. { 109, 42 },
  814. { 112, 41 },
  815. { 114, 40 },
  816. { 117, 39 },
  817. { 120, 38 },
  818. { 123, 37 },
  819. { 125, 36 },
  820. { 128, 35 },
  821. { 131, 34 },
  822. { 134, 33 },
  823. { 138, 32 },
  824. { 141, 31 },
  825. { 144, 30 },
  826. { 147, 29 },
  827. { 151, 28 },
  828. { 154, 27 },
  829. { 158, 26 },
  830. { 162, 25 },
  831. { 165, 24 },
  832. { 169, 23 },
  833. { 173, 22 },
  834. { 177, 21 },
  835. { 181, 20 },
  836. { 186, 19 },
  837. { 190, 18 },
  838. { 194, 17 },
  839. { 199, 16 },
  840. { 204, 15 },
  841. { 208, 14 },
  842. { 213, 13 },
  843. { 218, 12 },
  844. { 223, 11 },
  845. { 229, 10 },
  846. { 234, 9 },
  847. { 239, 8 },
  848. { 245, 7 },
  849. { 251, 6 },
  850. { 255, 5 },
  851. };
  852. static int tda10048_read_snr(struct dvb_frontend *fe, u16 *snr)
  853. {
  854. struct tda10048_state *state = fe->demodulator_priv;
  855. u8 v;
  856. int i, ret = -EINVAL;
  857. dprintk(1, "%s()\n", __func__);
  858. v = tda10048_readreg(state, TDA10048_NP_OUT);
  859. for (i = 0; i < ARRAY_SIZE(snr_tab); i++) {
  860. if (v <= snr_tab[i].val) {
  861. *snr = snr_tab[i].data;
  862. ret = 0;
  863. break;
  864. }
  865. }
  866. return ret;
  867. }
  868. static int tda10048_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  869. {
  870. struct tda10048_state *state = fe->demodulator_priv;
  871. dprintk(1, "%s()\n", __func__);
  872. *ucblocks = tda10048_readreg(state, TDA10048_UNCOR_CPT_MSB) << 8 |
  873. tda10048_readreg(state, TDA10048_UNCOR_CPT_LSB);
  874. /* clear the uncorrected TS packets counter when saturated */
  875. if (*ucblocks == 0xFFFF)
  876. tda10048_writereg(state, TDA10048_UNCOR_CTRL, 0x80);
  877. return 0;
  878. }
  879. static int tda10048_get_frontend(struct dvb_frontend *fe,
  880. struct dtv_frontend_properties *p)
  881. {
  882. struct tda10048_state *state = fe->demodulator_priv;
  883. dprintk(1, "%s()\n", __func__);
  884. p->inversion = tda10048_readreg(state, TDA10048_CONF_C1_1)
  885. & 0x20 ? INVERSION_ON : INVERSION_OFF;
  886. return tda10048_get_tps(state, p);
  887. }
  888. static int tda10048_get_tune_settings(struct dvb_frontend *fe,
  889. struct dvb_frontend_tune_settings *tune)
  890. {
  891. tune->min_delay_ms = 1000;
  892. return 0;
  893. }
  894. static void tda10048_release(struct dvb_frontend *fe)
  895. {
  896. struct tda10048_state *state = fe->demodulator_priv;
  897. dprintk(1, "%s()\n", __func__);
  898. kfree(state);
  899. }
  900. static void tda10048_establish_defaults(struct dvb_frontend *fe)
  901. {
  902. struct tda10048_state *state = fe->demodulator_priv;
  903. struct tda10048_config *config = &state->config;
  904. /* Validate/default the config */
  905. if (config->dtv6_if_freq_khz == 0) {
  906. config->dtv6_if_freq_khz = TDA10048_IF_4300;
  907. printk(KERN_WARNING "%s() tda10048_config.dtv6_if_freq_khz is not set (defaulting to %d)\n",
  908. __func__,
  909. config->dtv6_if_freq_khz);
  910. }
  911. if (config->dtv7_if_freq_khz == 0) {
  912. config->dtv7_if_freq_khz = TDA10048_IF_4300;
  913. printk(KERN_WARNING "%s() tda10048_config.dtv7_if_freq_khz is not set (defaulting to %d)\n",
  914. __func__,
  915. config->dtv7_if_freq_khz);
  916. }
  917. if (config->dtv8_if_freq_khz == 0) {
  918. config->dtv8_if_freq_khz = TDA10048_IF_4300;
  919. printk(KERN_WARNING "%s() tda10048_config.dtv8_if_freq_khz is not set (defaulting to %d)\n",
  920. __func__,
  921. config->dtv8_if_freq_khz);
  922. }
  923. if (config->clk_freq_khz == 0) {
  924. config->clk_freq_khz = TDA10048_CLK_16000;
  925. printk(KERN_WARNING "%s() tda10048_config.clk_freq_khz is not set (defaulting to %d)\n",
  926. __func__,
  927. config->clk_freq_khz);
  928. }
  929. }
  930. static const struct dvb_frontend_ops tda10048_ops;
  931. struct dvb_frontend *tda10048_attach(const struct tda10048_config *config,
  932. struct i2c_adapter *i2c)
  933. {
  934. struct tda10048_state *state = NULL;
  935. dprintk(1, "%s()\n", __func__);
  936. /* allocate memory for the internal state */
  937. state = kzalloc_obj(struct tda10048_state);
  938. if (state == NULL)
  939. goto error;
  940. /* setup the state and clone the config */
  941. memcpy(&state->config, config, sizeof(*config));
  942. state->i2c = i2c;
  943. state->fwloaded = config->no_firmware;
  944. state->bandwidth = 8000000;
  945. /* check if the demod is present */
  946. if (tda10048_readreg(state, TDA10048_IDENTITY) != 0x048)
  947. goto error;
  948. /* create dvb_frontend */
  949. memcpy(&state->frontend.ops, &tda10048_ops,
  950. sizeof(struct dvb_frontend_ops));
  951. state->frontend.demodulator_priv = state;
  952. /* set pll */
  953. if (config->set_pll) {
  954. state->pll_mfactor = config->pll_m;
  955. state->pll_nfactor = config->pll_n;
  956. state->pll_pfactor = config->pll_p;
  957. } else {
  958. state->pll_mfactor = 10;
  959. state->pll_nfactor = 3;
  960. state->pll_pfactor = 0;
  961. }
  962. /* Establish any defaults the user didn't pass */
  963. tda10048_establish_defaults(&state->frontend);
  964. /* Set the xtal and freq defaults */
  965. if (tda10048_set_if(&state->frontend, 8000000) != 0)
  966. goto error;
  967. /* Default bandwidth */
  968. if (tda10048_set_bandwidth(&state->frontend, 8000000) != 0)
  969. goto error;
  970. /* Leave the gate closed */
  971. tda10048_i2c_gate_ctrl(&state->frontend, 0);
  972. return &state->frontend;
  973. error:
  974. kfree(state);
  975. return NULL;
  976. }
  977. EXPORT_SYMBOL_GPL(tda10048_attach);
  978. static const struct dvb_frontend_ops tda10048_ops = {
  979. .delsys = { SYS_DVBT },
  980. .info = {
  981. .name = "NXP TDA10048HN DVB-T",
  982. .frequency_min_hz = 177 * MHz,
  983. .frequency_max_hz = 858 * MHz,
  984. .frequency_stepsize_hz = 166666,
  985. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  986. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  987. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  988. FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  989. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
  990. },
  991. .release = tda10048_release,
  992. .init = tda10048_init,
  993. .i2c_gate_ctrl = tda10048_i2c_gate_ctrl,
  994. .set_frontend = tda10048_set_frontend,
  995. .get_frontend = tda10048_get_frontend,
  996. .get_tune_settings = tda10048_get_tune_settings,
  997. .read_status = tda10048_read_status,
  998. .read_ber = tda10048_read_ber,
  999. .read_signal_strength = tda10048_read_signal_strength,
  1000. .read_snr = tda10048_read_snr,
  1001. .read_ucblocks = tda10048_read_ucblocks,
  1002. };
  1003. module_param(debug, int, 0644);
  1004. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  1005. MODULE_DESCRIPTION("NXP TDA10048HN DVB-T Demodulator driver");
  1006. MODULE_AUTHOR("Steven Toth");
  1007. MODULE_LICENSE("GPL");