tda10048.c 29 KB

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