tda10048.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  1. /*
  2. NXP TDA10048HN DVB OFDM demodulator driver
  3. Copyright (C) 2008 Steven Toth <stoth@linuxtv.org>
  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 <asm/div64.h>
  23. #include "dvb_frontend.h"
  24. #include "dvb_math.h"
  25. #include "tda10048.h"
  26. #define TDA10048_DEFAULT_FIRMWARE "dvb-fe-tda10048-1.0.fw"
  27. #define TDA10048_DEFAULT_FIRMWARE_SIZE 24878
  28. /* Register name definitions */
  29. #define TDA10048_IDENTITY 0x00
  30. #define TDA10048_VERSION 0x01
  31. #define TDA10048_DSP_CODE_CPT 0x0C
  32. #define TDA10048_DSP_CODE_IN 0x0E
  33. #define TDA10048_IN_CONF1 0x10
  34. #define TDA10048_IN_CONF2 0x11
  35. #define TDA10048_IN_CONF3 0x12
  36. #define TDA10048_OUT_CONF1 0x14
  37. #define TDA10048_OUT_CONF2 0x15
  38. #define TDA10048_OUT_CONF3 0x16
  39. #define TDA10048_AUTO 0x18
  40. #define TDA10048_SYNC_STATUS 0x1A
  41. #define TDA10048_CONF_C4_1 0x1E
  42. #define TDA10048_CONF_C4_2 0x1F
  43. #define TDA10048_CODE_IN_RAM 0x20
  44. #define TDA10048_CHANNEL_INFO_1_R 0x22
  45. #define TDA10048_CHANNEL_INFO_2_R 0x23
  46. #define TDA10048_CHANNEL_INFO1 0x24
  47. #define TDA10048_CHANNEL_INFO2 0x25
  48. #define TDA10048_TIME_ERROR_R 0x26
  49. #define TDA10048_TIME_ERROR 0x27
  50. #define TDA10048_FREQ_ERROR_LSB_R 0x28
  51. #define TDA10048_FREQ_ERROR_MSB_R 0x29
  52. #define TDA10048_FREQ_ERROR_LSB 0x2A
  53. #define TDA10048_FREQ_ERROR_MSB 0x2B
  54. #define TDA10048_IT_SEL 0x30
  55. #define TDA10048_IT_STAT 0x32
  56. #define TDA10048_DSP_AD_LSB 0x3C
  57. #define TDA10048_DSP_AD_MSB 0x3D
  58. #define TDA10048_DSP_REF_LSB 0x3E
  59. #define TDA10048_DSP_REF_MSB 0x3F
  60. #define TDA10048_CONF_TRISTATE1 0x44
  61. #define TDA10048_CONF_TRISTATE2 0x45
  62. #define TDA10048_CONF_POLARITY 0x46
  63. #define TDA10048_GPIO_SP_DS0 0x48
  64. #define TDA10048_GPIO_SP_DS1 0x49
  65. #define TDA10048_GPIO_SP_DS2 0x4A
  66. #define TDA10048_GPIO_SP_DS3 0x4B
  67. #define TDA10048_GPIO_OUT_SEL 0x4C
  68. #define TDA10048_GPIO_SELECT 0x4D
  69. #define TDA10048_IC_MODE 0x4E
  70. #define TDA10048_CONF_XO 0x50
  71. #define TDA10048_CONF_PLL1 0x51
  72. #define TDA10048_CONF_PLL2 0x52
  73. #define TDA10048_CONF_PLL3 0x53
  74. #define TDA10048_CONF_ADC 0x54
  75. #define TDA10048_CONF_ADC_2 0x55
  76. #define TDA10048_CONF_C1_1 0x60
  77. #define TDA10048_CONF_C1_3 0x62
  78. #define TDA10048_AGC_CONF 0x70
  79. #define TDA10048_AGC_THRESHOLD_LSB 0x72
  80. #define TDA10048_AGC_THRESHOLD_MSB 0x73
  81. #define TDA10048_AGC_RENORM 0x74
  82. #define TDA10048_AGC_GAINS 0x76
  83. #define TDA10048_AGC_TUN_MIN 0x78
  84. #define TDA10048_AGC_TUN_MAX 0x79
  85. #define TDA10048_AGC_IF_MIN 0x7A
  86. #define TDA10048_AGC_IF_MAX 0x7B
  87. #define TDA10048_AGC_TUN_LEVEL 0x7E
  88. #define TDA10048_AGC_IF_LEVEL 0x7F
  89. #define TDA10048_DIG_AGC_LEVEL 0x81
  90. #define TDA10048_FREQ_PHY2_LSB 0x86
  91. #define TDA10048_FREQ_PHY2_MSB 0x87
  92. #define TDA10048_TIME_INVWREF_LSB 0x88
  93. #define TDA10048_TIME_INVWREF_MSB 0x89
  94. #define TDA10048_TIME_WREF_LSB 0x8A
  95. #define TDA10048_TIME_WREF_MID1 0x8B
  96. #define TDA10048_TIME_WREF_MID2 0x8C
  97. #define TDA10048_TIME_WREF_MSB 0x8D
  98. #define TDA10048_NP_OUT 0xA2
  99. #define TDA10048_CELL_ID_LSB 0xA4
  100. #define TDA10048_CELL_ID_MSB 0xA5
  101. #define TDA10048_EXTTPS_ODD 0xAA
  102. #define TDA10048_EXTTPS_EVEN 0xAB
  103. #define TDA10048_TPS_LENGTH 0xAC
  104. #define TDA10048_FREE_REG_1 0xB2
  105. #define TDA10048_FREE_REG_2 0xB3
  106. #define TDA10048_CONF_C3_1 0xC0
  107. #define TDA10048_CYBER_CTRL 0xC2
  108. #define TDA10048_CBER_NMAX_LSB 0xC4
  109. #define TDA10048_CBER_NMAX_MSB 0xC5
  110. #define TDA10048_CBER_LSB 0xC6
  111. #define TDA10048_CBER_MSB 0xC7
  112. #define TDA10048_VBER_LSB 0xC8
  113. #define TDA10048_VBER_MID 0xC9
  114. #define TDA10048_VBER_MSB 0xCA
  115. #define TDA10048_CYBER_LUT 0xCC
  116. #define TDA10048_UNCOR_CTRL 0xCD
  117. #define TDA10048_UNCOR_CPT_LSB 0xCE
  118. #define TDA10048_UNCOR_CPT_MSB 0xCF
  119. #define TDA10048_SOFT_IT_C3 0xD6
  120. #define TDA10048_CONF_TS2 0xE0
  121. #define TDA10048_CONF_TS1 0xE1
  122. static unsigned int debug;
  123. #define dprintk(level, fmt, arg...)\
  124. do { if (debug >= level)\
  125. printk(KERN_DEBUG "tda10048: " fmt, ## arg);\
  126. } while (0)
  127. struct tda10048_state {
  128. struct i2c_adapter *i2c;
  129. /* configuration settings */
  130. const struct tda10048_config *config;
  131. struct dvb_frontend frontend;
  132. int fwloaded;
  133. u32 freq_if_hz;
  134. u32 xtal_hz;
  135. u32 pll_mfactor;
  136. u32 pll_nfactor;
  137. u32 pll_pfactor;
  138. u32 sample_freq;
  139. enum fe_bandwidth bandwidth;
  140. };
  141. static struct init_tab {
  142. u8 reg;
  143. u16 data;
  144. } init_tab[] = {
  145. { TDA10048_CONF_PLL1, 0x08 },
  146. { TDA10048_CONF_ADC_2, 0x00 },
  147. { TDA10048_CONF_C4_1, 0x00 },
  148. { TDA10048_CONF_PLL1, 0x0f },
  149. { TDA10048_CONF_PLL2, 0x0a },
  150. { TDA10048_CONF_PLL3, 0x43 },
  151. { TDA10048_FREQ_PHY2_LSB, 0x02 },
  152. { TDA10048_FREQ_PHY2_MSB, 0x0a },
  153. { TDA10048_TIME_WREF_LSB, 0xbd },
  154. { TDA10048_TIME_WREF_MID1, 0xe4 },
  155. { TDA10048_TIME_WREF_MID2, 0xa8 },
  156. { TDA10048_TIME_WREF_MSB, 0x02 },
  157. { TDA10048_TIME_INVWREF_LSB, 0x04 },
  158. { TDA10048_TIME_INVWREF_MSB, 0x06 },
  159. { TDA10048_CONF_C4_1, 0x00 },
  160. { TDA10048_CONF_C1_1, 0xa8 },
  161. { TDA10048_AGC_CONF, 0x16 },
  162. { TDA10048_CONF_C1_3, 0x0b },
  163. { TDA10048_AGC_TUN_MIN, 0x00 },
  164. { TDA10048_AGC_TUN_MAX, 0xff },
  165. { TDA10048_AGC_IF_MIN, 0x00 },
  166. { TDA10048_AGC_IF_MAX, 0xff },
  167. { TDA10048_AGC_THRESHOLD_MSB, 0x00 },
  168. { TDA10048_AGC_THRESHOLD_LSB, 0x70 },
  169. { TDA10048_CYBER_CTRL, 0x38 },
  170. { TDA10048_AGC_GAINS, 0x12 },
  171. { TDA10048_CONF_XO, 0x00 },
  172. { TDA10048_CONF_TS1, 0x07 },
  173. { TDA10048_IC_MODE, 0x00 },
  174. { TDA10048_CONF_TS2, 0xc0 },
  175. { TDA10048_CONF_TRISTATE1, 0x21 },
  176. { TDA10048_CONF_TRISTATE2, 0x00 },
  177. { TDA10048_CONF_POLARITY, 0x00 },
  178. { TDA10048_CONF_C4_2, 0x04 },
  179. { TDA10048_CONF_ADC, 0x60 },
  180. { TDA10048_CONF_ADC_2, 0x10 },
  181. { TDA10048_CONF_ADC, 0x60 },
  182. { TDA10048_CONF_ADC_2, 0x00 },
  183. { TDA10048_CONF_C1_1, 0xa8 },
  184. { TDA10048_UNCOR_CTRL, 0x00 },
  185. { TDA10048_CONF_C4_2, 0x04 },
  186. };
  187. static int tda10048_writereg(struct tda10048_state *state, u8 reg, u8 data)
  188. {
  189. int ret;
  190. u8 buf[] = { reg, data };
  191. struct i2c_msg msg = {
  192. .addr = state->config->demod_address,
  193. .flags = 0, .buf = buf, .len = 2 };
  194. dprintk(2, "%s(reg = 0x%02x, data = 0x%02x)\n", __func__, reg, data);
  195. ret = i2c_transfer(state->i2c, &msg, 1);
  196. if (ret != 1)
  197. printk("%s: writereg error (ret == %i)\n", __func__, ret);
  198. return (ret != 1) ? -1 : 0;
  199. }
  200. static u8 tda10048_readreg(struct tda10048_state *state, u8 reg)
  201. {
  202. int ret;
  203. u8 b0[] = { reg };
  204. u8 b1[] = { 0 };
  205. struct i2c_msg msg[] = {
  206. { .addr = state->config->demod_address,
  207. .flags = 0, .buf = b0, .len = 1 },
  208. { .addr = state->config->demod_address,
  209. .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  210. dprintk(2, "%s(reg = 0x%02x)\n", __func__, reg);
  211. ret = i2c_transfer(state->i2c, msg, 2);
  212. if (ret != 2)
  213. printk(KERN_ERR "%s: readreg error (ret == %i)\n",
  214. __func__, ret);
  215. return b1[0];
  216. }
  217. static int tda10048_writeregbulk(struct tda10048_state *state, u8 reg,
  218. const u8 *data, u16 len)
  219. {
  220. int ret = -EREMOTEIO;
  221. struct i2c_msg msg;
  222. u8 *buf;
  223. dprintk(2, "%s(%d, ?, len = %d)\n", __func__, reg, len);
  224. buf = kmalloc(len + 1, GFP_KERNEL);
  225. if (buf == NULL) {
  226. ret = -ENOMEM;
  227. goto error;
  228. }
  229. *buf = reg;
  230. memcpy(buf + 1, data, len);
  231. msg.addr = state->config->demod_address;
  232. msg.flags = 0;
  233. msg.buf = buf;
  234. msg.len = len + 1;
  235. dprintk(2, "%s(): write len = %d\n",
  236. __func__, msg.len);
  237. ret = i2c_transfer(state->i2c, &msg, 1);
  238. if (ret != 1) {
  239. printk(KERN_ERR "%s(): writereg error err %i\n",
  240. __func__, ret);
  241. ret = -EREMOTEIO;
  242. }
  243. error:
  244. kfree(buf);
  245. return ret;
  246. }
  247. static int tda10048_set_phy2(struct dvb_frontend *fe, u32 sample_freq_hz,
  248. u32 if_hz)
  249. {
  250. struct tda10048_state *state = fe->demodulator_priv;
  251. u64 t;
  252. dprintk(1, "%s()\n", __func__);
  253. if (sample_freq_hz == 0)
  254. return -EINVAL;
  255. if (if_hz < (sample_freq_hz / 2)) {
  256. /* PHY2 = (if2/fs) * 2^15 */
  257. t = if_hz;
  258. t *= 10;
  259. t *= 32768;
  260. do_div(t, sample_freq_hz);
  261. t += 5;
  262. do_div(t, 10);
  263. } else {
  264. /* PHY2 = ((IF1-fs)/fs) * 2^15 */
  265. t = sample_freq_hz - if_hz;
  266. t *= 10;
  267. t *= 32768;
  268. do_div(t, sample_freq_hz);
  269. t += 5;
  270. do_div(t, 10);
  271. t = ~t + 1;
  272. }
  273. tda10048_writereg(state, TDA10048_FREQ_PHY2_LSB, (u8)t);
  274. tda10048_writereg(state, TDA10048_FREQ_PHY2_MSB, (u8)(t >> 8));
  275. return 0;
  276. }
  277. static int tda10048_set_wref(struct dvb_frontend *fe, u32 sample_freq_hz,
  278. u32 bw)
  279. {
  280. struct tda10048_state *state = fe->demodulator_priv;
  281. u64 t, z;
  282. u32 b = 8000000;
  283. dprintk(1, "%s()\n", __func__);
  284. if (sample_freq_hz == 0)
  285. return -EINVAL;
  286. if (bw == BANDWIDTH_6_MHZ)
  287. b = 6000000;
  288. else
  289. if (bw == BANDWIDTH_7_MHZ)
  290. b = 7000000;
  291. /* WREF = (B / (7 * fs)) * 2^31 */
  292. t = b * 10;
  293. /* avoid warning: this decimal constant is unsigned only in ISO C90 */
  294. /* t *= 2147483648 on 32bit platforms */
  295. t *= (2048 * 1024);
  296. t *= 1024;
  297. z = 7 * sample_freq_hz;
  298. do_div(t, z);
  299. t += 5;
  300. do_div(t, 10);
  301. tda10048_writereg(state, TDA10048_TIME_WREF_LSB, (u8)t);
  302. tda10048_writereg(state, TDA10048_TIME_WREF_MID1, (u8)(t >> 8));
  303. tda10048_writereg(state, TDA10048_TIME_WREF_MID2, (u8)(t >> 16));
  304. tda10048_writereg(state, TDA10048_TIME_WREF_MSB, (u8)(t >> 24));
  305. return 0;
  306. }
  307. static int tda10048_set_invwref(struct dvb_frontend *fe, u32 sample_freq_hz,
  308. u32 bw)
  309. {
  310. struct tda10048_state *state = fe->demodulator_priv;
  311. u64 t;
  312. u32 b = 8000000;
  313. dprintk(1, "%s()\n", __func__);
  314. if (sample_freq_hz == 0)
  315. return -EINVAL;
  316. if (bw == BANDWIDTH_6_MHZ)
  317. b = 6000000;
  318. else
  319. if (bw == BANDWIDTH_7_MHZ)
  320. b = 7000000;
  321. /* INVWREF = ((7 * fs) / B) * 2^5 */
  322. t = sample_freq_hz;
  323. t *= 7;
  324. t *= 32;
  325. t *= 10;
  326. do_div(t, b);
  327. t += 5;
  328. do_div(t, 10);
  329. tda10048_writereg(state, TDA10048_TIME_INVWREF_LSB, (u8)t);
  330. tda10048_writereg(state, TDA10048_TIME_INVWREF_MSB, (u8)(t >> 8));
  331. return 0;
  332. }
  333. static int tda10048_set_bandwidth(struct dvb_frontend *fe,
  334. enum fe_bandwidth bw)
  335. {
  336. struct tda10048_state *state = fe->demodulator_priv;
  337. dprintk(1, "%s(bw=%d)\n", __func__, bw);
  338. /* Bandwidth setting may need to be adjusted */
  339. switch (bw) {
  340. case BANDWIDTH_6_MHZ:
  341. case BANDWIDTH_7_MHZ:
  342. case BANDWIDTH_8_MHZ:
  343. tda10048_set_wref(fe, state->sample_freq, bw);
  344. tda10048_set_invwref(fe, state->sample_freq, bw);
  345. break;
  346. default:
  347. printk(KERN_ERR "%s() invalid bandwidth\n", __func__);
  348. return -EINVAL;
  349. }
  350. state->bandwidth = bw;
  351. return 0;
  352. }
  353. static int tda10048_set_pll(struct dvb_frontend *fe)
  354. {
  355. struct tda10048_state *state = fe->demodulator_priv;
  356. int ret = 0;
  357. dprintk(1, "%s()\n", __func__);
  358. if ((state->config->clk_freq_khz == TDA10048_CLK_4000) &&
  359. (state->config->if_freq_khz == TDA10048_IF_36130)) {
  360. state->freq_if_hz = TDA10048_IF_36130 * 1000;
  361. state->xtal_hz = TDA10048_CLK_4000 * 1000;
  362. state->pll_mfactor = 10;
  363. state->pll_nfactor = 0;
  364. state->pll_pfactor = 0;
  365. } else
  366. if ((state->config->clk_freq_khz == TDA10048_CLK_16000) &&
  367. (state->config->if_freq_khz == TDA10048_IF_4300)) {
  368. state->freq_if_hz = TDA10048_IF_4300 * 1000;
  369. state->xtal_hz = TDA10048_CLK_16000 * 1000;
  370. state->pll_mfactor = 10;
  371. state->pll_nfactor = 3;
  372. state->pll_pfactor = 0;
  373. } else
  374. if ((state->config->clk_freq_khz == TDA10048_CLK_16000) &&
  375. (state->config->if_freq_khz == TDA10048_IF_4000)) {
  376. state->freq_if_hz = TDA10048_IF_4000 * 1000;
  377. state->xtal_hz = TDA10048_CLK_16000 * 1000;
  378. state->pll_mfactor = 10;
  379. state->pll_nfactor = 3;
  380. state->pll_pfactor = 0;
  381. } else
  382. if ((state->config->clk_freq_khz == TDA10048_CLK_16000) &&
  383. (state->config->if_freq_khz == TDA10048_IF_36130)) {
  384. state->freq_if_hz = TDA10048_IF_36130 * 1000;
  385. state->xtal_hz = TDA10048_CLK_16000 * 1000;
  386. state->pll_mfactor = 10;
  387. state->pll_nfactor = 3;
  388. state->pll_pfactor = 0;
  389. } else {
  390. printk(KERN_ERR "%s() Incorrect attach settings\n", __func__);
  391. ret = -EINVAL;
  392. }
  393. dprintk(1, "- freq_if_hz = %d\n", state->freq_if_hz);
  394. dprintk(1, "- xtal_hz = %d\n", state->xtal_hz);
  395. dprintk(1, "- pll_mfactor = %d\n", state->pll_mfactor);
  396. dprintk(1, "- pll_nfactor = %d\n", state->pll_nfactor);
  397. dprintk(1, "- pll_pfactor = %d\n", state->pll_pfactor);
  398. /* Calculate the sample frequency */
  399. state->sample_freq = state->xtal_hz * (state->pll_mfactor + 45);
  400. state->sample_freq /= (state->pll_nfactor + 1);
  401. state->sample_freq /= (state->pll_pfactor + 4);
  402. dprintk(1, "- sample_freq = %d\n", state->sample_freq);
  403. tda10048_set_phy2(fe, state->sample_freq,
  404. state->config->if_freq_khz * 1000);
  405. tda10048_set_wref(fe, state->sample_freq, state->bandwidth);
  406. tda10048_set_invwref(fe, state->sample_freq, state->bandwidth);
  407. return ret;
  408. }
  409. static int tda10048_firmware_upload(struct dvb_frontend *fe)
  410. {
  411. struct tda10048_state *state = fe->demodulator_priv;
  412. const struct firmware *fw;
  413. int ret;
  414. int pos = 0;
  415. int cnt;
  416. u8 wlen = state->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 dvb_ofdm_parameters *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->constellation = QPSK;
  508. break;
  509. case 1:
  510. p->constellation = QAM_16;
  511. break;
  512. case 2:
  513. p->constellation = QAM_64;
  514. break;
  515. }
  516. switch ((val & 0x18) >> 3) {
  517. case 0:
  518. p->hierarchy_information = HIERARCHY_NONE;
  519. break;
  520. case 1:
  521. p->hierarchy_information = HIERARCHY_1;
  522. break;
  523. case 2:
  524. p->hierarchy_information = HIERARCHY_2;
  525. break;
  526. case 3:
  527. p->hierarchy_information = 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 & 0x02) {
  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. dprintk(1, "%s(%d)\n", __func__, enable);
  594. if (state->config->disable_gate_access)
  595. return 0;
  596. if (enable)
  597. return tda10048_writereg(state, TDA10048_CONF_C4_1,
  598. tda10048_readreg(state, TDA10048_CONF_C4_1) | 0x02);
  599. else
  600. return tda10048_writereg(state, TDA10048_CONF_C4_1,
  601. tda10048_readreg(state, TDA10048_CONF_C4_1) & 0xfd);
  602. }
  603. static int tda10048_output_mode(struct dvb_frontend *fe, int serial)
  604. {
  605. struct tda10048_state *state = fe->demodulator_priv;
  606. dprintk(1, "%s(%d)\n", __func__, serial);
  607. /* Ensure pins are out of tri-state */
  608. tda10048_writereg(state, TDA10048_CONF_TRISTATE1, 0x21);
  609. tda10048_writereg(state, TDA10048_CONF_TRISTATE2, 0x00);
  610. if (serial) {
  611. tda10048_writereg(state, TDA10048_IC_MODE, 0x80 | 0x20);
  612. tda10048_writereg(state, TDA10048_CONF_TS2, 0xc0);
  613. } else {
  614. tda10048_writereg(state, TDA10048_IC_MODE, 0x00);
  615. tda10048_writereg(state, TDA10048_CONF_TS2, 0x01);
  616. }
  617. return 0;
  618. }
  619. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  620. /* TODO: Support manual tuning with specific params */
  621. static int tda10048_set_frontend(struct dvb_frontend *fe,
  622. struct dvb_frontend_parameters *p)
  623. {
  624. struct tda10048_state *state = fe->demodulator_priv;
  625. dprintk(1, "%s(frequency=%d)\n", __func__, p->frequency);
  626. if (p->u.ofdm.bandwidth != state->bandwidth)
  627. tda10048_set_bandwidth(fe, p->u.ofdm.bandwidth);
  628. if (fe->ops.tuner_ops.set_params) {
  629. if (fe->ops.i2c_gate_ctrl)
  630. fe->ops.i2c_gate_ctrl(fe, 1);
  631. fe->ops.tuner_ops.set_params(fe, p);
  632. if (fe->ops.i2c_gate_ctrl)
  633. fe->ops.i2c_gate_ctrl(fe, 0);
  634. }
  635. /* Enable demod TPS auto detection and begin acquisition */
  636. tda10048_writereg(state, TDA10048_AUTO, 0x57);
  637. return 0;
  638. }
  639. /* Establish sane defaults and load firmware. */
  640. static int tda10048_init(struct dvb_frontend *fe)
  641. {
  642. struct tda10048_state *state = fe->demodulator_priv;
  643. int ret = 0, i;
  644. dprintk(1, "%s()\n", __func__);
  645. /* Apply register defaults */
  646. for (i = 0; i < ARRAY_SIZE(init_tab); i++)
  647. tda10048_writereg(state, init_tab[i].reg, init_tab[i].data);
  648. if (state->fwloaded == 0)
  649. ret = tda10048_firmware_upload(fe);
  650. /* Set either serial or parallel */
  651. tda10048_output_mode(fe, state->config->output_mode);
  652. /* Set inversion */
  653. tda10048_set_inversion(fe, state->config->inversion);
  654. /* Establish default PLL values */
  655. tda10048_set_pll(fe);
  656. /* Establish default bandwidth */
  657. tda10048_set_bandwidth(fe, BANDWIDTH_8_MHZ);
  658. /* Ensure we leave the gate closed */
  659. tda10048_i2c_gate_ctrl(fe, 0);
  660. return ret;
  661. }
  662. static int tda10048_read_status(struct dvb_frontend *fe, fe_status_t *status)
  663. {
  664. struct tda10048_state *state = fe->demodulator_priv;
  665. u8 reg;
  666. *status = 0;
  667. reg = tda10048_readreg(state, TDA10048_SYNC_STATUS);
  668. dprintk(1, "%s() status =0x%02x\n", __func__, reg);
  669. if (reg & 0x02)
  670. *status |= FE_HAS_CARRIER;
  671. if (reg & 0x04)
  672. *status |= FE_HAS_SIGNAL;
  673. if (reg & 0x08) {
  674. *status |= FE_HAS_LOCK;
  675. *status |= FE_HAS_VITERBI;
  676. *status |= FE_HAS_SYNC;
  677. }
  678. return 0;
  679. }
  680. static int tda10048_read_ber(struct dvb_frontend *fe, u32 *ber)
  681. {
  682. struct tda10048_state *state = fe->demodulator_priv;
  683. dprintk(1, "%s()\n", __func__);
  684. /* TODO: A reset may be required here */
  685. *ber = tda10048_readreg(state, TDA10048_CBER_MSB) << 8 |
  686. tda10048_readreg(state, TDA10048_CBER_LSB);
  687. return 0;
  688. }
  689. static int tda10048_read_signal_strength(struct dvb_frontend *fe,
  690. u16 *signal_strength)
  691. {
  692. struct tda10048_state *state = fe->demodulator_priv;
  693. u8 v;
  694. dprintk(1, "%s()\n", __func__);
  695. *signal_strength = 65535;
  696. v = tda10048_readreg(state, TDA10048_NP_OUT);
  697. if (v > 0)
  698. *signal_strength -= (v << 8) | v;
  699. return 0;
  700. }
  701. /* SNR lookup table */
  702. static struct snr_tab {
  703. u8 val;
  704. u8 data;
  705. } snr_tab[] = {
  706. { 0, 0 },
  707. { 1, 246 },
  708. { 2, 215 },
  709. { 3, 198 },
  710. { 4, 185 },
  711. { 5, 176 },
  712. { 6, 168 },
  713. { 7, 161 },
  714. { 8, 155 },
  715. { 9, 150 },
  716. { 10, 146 },
  717. { 11, 141 },
  718. { 12, 138 },
  719. { 13, 134 },
  720. { 14, 131 },
  721. { 15, 128 },
  722. { 16, 125 },
  723. { 17, 122 },
  724. { 18, 120 },
  725. { 19, 118 },
  726. { 20, 115 },
  727. { 21, 113 },
  728. { 22, 111 },
  729. { 23, 109 },
  730. { 24, 107 },
  731. { 25, 106 },
  732. { 26, 104 },
  733. { 27, 102 },
  734. { 28, 101 },
  735. { 29, 99 },
  736. { 30, 98 },
  737. { 31, 96 },
  738. { 32, 95 },
  739. { 33, 94 },
  740. { 34, 92 },
  741. { 35, 91 },
  742. { 36, 90 },
  743. { 37, 89 },
  744. { 38, 88 },
  745. { 39, 86 },
  746. { 40, 85 },
  747. { 41, 84 },
  748. { 42, 83 },
  749. { 43, 82 },
  750. { 44, 81 },
  751. { 45, 80 },
  752. { 46, 79 },
  753. { 47, 78 },
  754. { 48, 77 },
  755. { 49, 76 },
  756. { 50, 76 },
  757. { 51, 75 },
  758. { 52, 74 },
  759. { 53, 73 },
  760. { 54, 72 },
  761. { 56, 71 },
  762. { 57, 70 },
  763. { 58, 69 },
  764. { 60, 68 },
  765. { 61, 67 },
  766. { 63, 66 },
  767. { 64, 65 },
  768. { 66, 64 },
  769. { 67, 63 },
  770. { 68, 62 },
  771. { 69, 62 },
  772. { 70, 61 },
  773. { 72, 60 },
  774. { 74, 59 },
  775. { 75, 58 },
  776. { 77, 57 },
  777. { 79, 56 },
  778. { 81, 55 },
  779. { 83, 54 },
  780. { 85, 53 },
  781. { 87, 52 },
  782. { 89, 51 },
  783. { 91, 50 },
  784. { 93, 49 },
  785. { 95, 48 },
  786. { 97, 47 },
  787. { 100, 46 },
  788. { 102, 45 },
  789. { 104, 44 },
  790. { 107, 43 },
  791. { 109, 42 },
  792. { 112, 41 },
  793. { 114, 40 },
  794. { 117, 39 },
  795. { 120, 38 },
  796. { 123, 37 },
  797. { 125, 36 },
  798. { 128, 35 },
  799. { 131, 34 },
  800. { 134, 33 },
  801. { 138, 32 },
  802. { 141, 31 },
  803. { 144, 30 },
  804. { 147, 29 },
  805. { 151, 28 },
  806. { 154, 27 },
  807. { 158, 26 },
  808. { 162, 25 },
  809. { 165, 24 },
  810. { 169, 23 },
  811. { 173, 22 },
  812. { 177, 21 },
  813. { 181, 20 },
  814. { 186, 19 },
  815. { 190, 18 },
  816. { 194, 17 },
  817. { 199, 16 },
  818. { 204, 15 },
  819. { 208, 14 },
  820. { 213, 13 },
  821. { 218, 12 },
  822. { 223, 11 },
  823. { 229, 10 },
  824. { 234, 9 },
  825. { 239, 8 },
  826. { 245, 7 },
  827. { 251, 6 },
  828. { 255, 5 },
  829. };
  830. static int tda10048_read_snr(struct dvb_frontend *fe, u16 *snr)
  831. {
  832. struct tda10048_state *state = fe->demodulator_priv;
  833. u8 v;
  834. int i, ret = -EINVAL;
  835. dprintk(1, "%s()\n", __func__);
  836. v = tda10048_readreg(state, TDA10048_NP_OUT);
  837. for (i = 0; i < ARRAY_SIZE(snr_tab); i++) {
  838. if (v <= snr_tab[i].val) {
  839. *snr = snr_tab[i].data;
  840. ret = 0;
  841. break;
  842. }
  843. }
  844. return ret;
  845. }
  846. static int tda10048_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  847. {
  848. struct tda10048_state *state = fe->demodulator_priv;
  849. dprintk(1, "%s()\n", __func__);
  850. *ucblocks = tda10048_readreg(state, TDA10048_UNCOR_CPT_MSB) << 8 |
  851. tda10048_readreg(state, TDA10048_UNCOR_CPT_LSB);
  852. return 0;
  853. }
  854. static int tda10048_get_frontend(struct dvb_frontend *fe,
  855. struct dvb_frontend_parameters *p)
  856. {
  857. struct tda10048_state *state = fe->demodulator_priv;
  858. dprintk(1, "%s()\n", __func__);
  859. p->inversion = tda10048_readreg(state, TDA10048_CONF_C1_1)
  860. & 0x20 ? INVERSION_ON : INVERSION_OFF;
  861. return tda10048_get_tps(state, &p->u.ofdm);
  862. }
  863. static int tda10048_get_tune_settings(struct dvb_frontend *fe,
  864. struct dvb_frontend_tune_settings *tune)
  865. {
  866. tune->min_delay_ms = 1000;
  867. return 0;
  868. }
  869. static void tda10048_release(struct dvb_frontend *fe)
  870. {
  871. struct tda10048_state *state = fe->demodulator_priv;
  872. dprintk(1, "%s()\n", __func__);
  873. kfree(state);
  874. }
  875. static struct dvb_frontend_ops tda10048_ops;
  876. struct dvb_frontend *tda10048_attach(const struct tda10048_config *config,
  877. struct i2c_adapter *i2c)
  878. {
  879. struct tda10048_state *state = NULL;
  880. dprintk(1, "%s()\n", __func__);
  881. /* allocate memory for the internal state */
  882. state = kmalloc(sizeof(struct tda10048_state), GFP_KERNEL);
  883. if (state == NULL)
  884. goto error;
  885. /* setup the state */
  886. state->config = config;
  887. state->i2c = i2c;
  888. state->fwloaded = 0;
  889. state->bandwidth = BANDWIDTH_8_MHZ;
  890. /* check if the demod is present */
  891. if (tda10048_readreg(state, TDA10048_IDENTITY) != 0x048)
  892. goto error;
  893. /* create dvb_frontend */
  894. memcpy(&state->frontend.ops, &tda10048_ops,
  895. sizeof(struct dvb_frontend_ops));
  896. state->frontend.demodulator_priv = state;
  897. /* Set the xtal and freq defaults */
  898. if (tda10048_set_pll(&state->frontend) != 0)
  899. goto error;
  900. /* Leave the gate closed */
  901. tda10048_i2c_gate_ctrl(&state->frontend, 0);
  902. return &state->frontend;
  903. error:
  904. kfree(state);
  905. return NULL;
  906. }
  907. EXPORT_SYMBOL(tda10048_attach);
  908. static struct dvb_frontend_ops tda10048_ops = {
  909. .info = {
  910. .name = "NXP TDA10048HN DVB-T",
  911. .type = FE_OFDM,
  912. .frequency_min = 177000000,
  913. .frequency_max = 858000000,
  914. .frequency_stepsize = 166666,
  915. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  916. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  917. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  918. FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  919. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
  920. },
  921. .release = tda10048_release,
  922. .init = tda10048_init,
  923. .i2c_gate_ctrl = tda10048_i2c_gate_ctrl,
  924. .set_frontend = tda10048_set_frontend,
  925. .get_frontend = tda10048_get_frontend,
  926. .get_tune_settings = tda10048_get_tune_settings,
  927. .read_status = tda10048_read_status,
  928. .read_ber = tda10048_read_ber,
  929. .read_signal_strength = tda10048_read_signal_strength,
  930. .read_snr = tda10048_read_snr,
  931. .read_ucblocks = tda10048_read_ucblocks,
  932. };
  933. module_param(debug, int, 0644);
  934. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  935. MODULE_DESCRIPTION("NXP TDA10048HN DVB-T Demodulator driver");
  936. MODULE_AUTHOR("Steven Toth");
  937. MODULE_LICENSE("GPL");