tda10048.c 28 KB

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