tda10048.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 "dvb_frontend.h"
  23. #include "dvb_math.h"
  24. #include "tda10048.h"
  25. #define TDA10048_DEFAULT_FIRMWARE "dvb-fe-tda10048-1.0.fw"
  26. #define TDA10048_DEFAULT_FIRMWARE_SIZE 24878
  27. /* Register name definitions */
  28. #define TDA10048_IDENTITY 0x00
  29. #define TDA10048_VERSION 0x01
  30. #define TDA10048_DSP_CODE_CPT 0x0C
  31. #define TDA10048_DSP_CODE_IN 0x0E
  32. #define TDA10048_IN_CONF1 0x10
  33. #define TDA10048_IN_CONF2 0x11
  34. #define TDA10048_IN_CONF3 0x12
  35. #define TDA10048_OUT_CONF1 0x14
  36. #define TDA10048_OUT_CONF2 0x15
  37. #define TDA10048_OUT_CONF3 0x16
  38. #define TDA10048_AUTO 0x18
  39. #define TDA10048_SYNC_STATUS 0x1A
  40. #define TDA10048_CONF_C4_1 0x1E
  41. #define TDA10048_CONF_C4_2 0x1F
  42. #define TDA10048_CODE_IN_RAM 0x20
  43. #define TDA10048_CHANNEL_INFO_1_R 0x22
  44. #define TDA10048_CHANNEL_INFO_2_R 0x23
  45. #define TDA10048_CHANNEL_INFO1 0x24
  46. #define TDA10048_CHANNEL_INFO2 0x25
  47. #define TDA10048_TIME_ERROR_R 0x26
  48. #define TDA10048_TIME_ERROR 0x27
  49. #define TDA10048_FREQ_ERROR_LSB_R 0x28
  50. #define TDA10048_FREQ_ERROR_MSB_R 0x29
  51. #define TDA10048_FREQ_ERROR_LSB 0x2A
  52. #define TDA10048_FREQ_ERROR_MSB 0x2B
  53. #define TDA10048_IT_SEL 0x30
  54. #define TDA10048_IT_STAT 0x32
  55. #define TDA10048_DSP_AD_LSB 0x3C
  56. #define TDA10048_DSP_AD_MSB 0x3D
  57. #define TDA10048_DSP_REF_LSB 0x3E
  58. #define TDA10048_DSP_REF_MSB 0x3F
  59. #define TDA10048_CONF_TRISTATE1 0x44
  60. #define TDA10048_CONF_TRISTATE2 0x45
  61. #define TDA10048_CONF_POLARITY 0x46
  62. #define TDA10048_GPIO_SP_DS0 0x48
  63. #define TDA10048_GPIO_SP_DS1 0x49
  64. #define TDA10048_GPIO_SP_DS2 0x4A
  65. #define TDA10048_GPIO_SP_DS3 0x4B
  66. #define TDA10048_GPIO_OUT_SEL 0x4C
  67. #define TDA10048_GPIO_SELECT 0x4D
  68. #define TDA10048_IC_MODE 0x4E
  69. #define TDA10048_CONF_XO 0x50
  70. #define TDA10048_CONF_PLL1 0x51
  71. #define TDA10048_CONF_PLL2 0x52
  72. #define TDA10048_CONF_PLL3 0x53
  73. #define TDA10048_CONF_ADC 0x54
  74. #define TDA10048_CONF_ADC_2 0x55
  75. #define TDA10048_CONF_C1_1 0x60
  76. #define TDA10048_CONF_C1_3 0x62
  77. #define TDA10048_AGC_CONF 0x70
  78. #define TDA10048_AGC_THRESHOLD_LSB 0x72
  79. #define TDA10048_AGC_THRESHOLD_MSB 0x73
  80. #define TDA10048_AGC_RENORM 0x74
  81. #define TDA10048_AGC_GAINS 0x76
  82. #define TDA10048_AGC_TUN_MIN 0x78
  83. #define TDA10048_AGC_TUN_MAX 0x79
  84. #define TDA10048_AGC_IF_MIN 0x7A
  85. #define TDA10048_AGC_IF_MAX 0x7B
  86. #define TDA10048_AGC_TUN_LEVEL 0x7E
  87. #define TDA10048_AGC_IF_LEVEL 0x7F
  88. #define TDA10048_DIG_AGC_LEVEL 0x81
  89. #define TDA10048_FREQ_PHY2_LSB 0x86
  90. #define TDA10048_FREQ_PHY2_MSB 0x87
  91. #define TDA10048_TIME_INVWREF_LSB 0x88
  92. #define TDA10048_TIME_INVWREF_MSB 0x89
  93. #define TDA10048_TIME_WREF_LSB 0x8A
  94. #define TDA10048_TIME_WREF_MID1 0x8B
  95. #define TDA10048_TIME_WREF_MID2 0x8C
  96. #define TDA10048_TIME_WREF_MSB 0x8D
  97. #define TDA10048_NP_OUT 0xA2
  98. #define TDA10048_CELL_ID_LSB 0xA4
  99. #define TDA10048_CELL_ID_MSB 0xA5
  100. #define TDA10048_EXTTPS_ODD 0xAA
  101. #define TDA10048_EXTTPS_EVEN 0xAB
  102. #define TDA10048_TPS_LENGTH 0xAC
  103. #define TDA10048_FREE_REG_1 0xB2
  104. #define TDA10048_FREE_REG_2 0xB3
  105. #define TDA10048_CONF_C3_1 0xC0
  106. #define TDA10048_CYBER_CTRL 0xC2
  107. #define TDA10048_CBER_NMAX_LSB 0xC4
  108. #define TDA10048_CBER_NMAX_MSB 0xC5
  109. #define TDA10048_CBER_LSB 0xC6
  110. #define TDA10048_CBER_MSB 0xC7
  111. #define TDA10048_VBER_LSB 0xC8
  112. #define TDA10048_VBER_MID 0xC9
  113. #define TDA10048_VBER_MSB 0xCA
  114. #define TDA10048_CYBER_LUT 0xCC
  115. #define TDA10048_UNCOR_CTRL 0xCD
  116. #define TDA10048_UNCOR_CPT_LSB 0xCE
  117. #define TDA10048_UNCOR_CPT_MSB 0xCF
  118. #define TDA10048_SOFT_IT_C3 0xD6
  119. #define TDA10048_CONF_TS2 0xE0
  120. #define TDA10048_CONF_TS1 0xE1
  121. static unsigned int debug;
  122. #define dprintk(level, fmt, arg...)\
  123. do { if (debug >= level)\
  124. printk(KERN_DEBUG "tda10048: " fmt, ## arg);\
  125. } while (0)
  126. struct tda10048_state {
  127. struct i2c_adapter *i2c;
  128. /* configuration settings */
  129. const struct tda10048_config *config;
  130. struct dvb_frontend frontend;
  131. int fwloaded;
  132. };
  133. static struct init_tab {
  134. u8 reg;
  135. u16 data;
  136. } init_tab[] = {
  137. { TDA10048_CONF_PLL1, 0x08 },
  138. { TDA10048_CONF_ADC_2, 0x00 },
  139. { TDA10048_CONF_C4_1, 0x00 },
  140. { TDA10048_CONF_PLL1, 0x0f },
  141. { TDA10048_CONF_PLL2, 0x0a },
  142. { TDA10048_CONF_PLL3, 0x43 },
  143. { TDA10048_FREQ_PHY2_LSB, 0x02 },
  144. { TDA10048_FREQ_PHY2_MSB, 0x0a },
  145. { TDA10048_TIME_WREF_LSB, 0xbd },
  146. { TDA10048_TIME_WREF_MID1, 0xe4 },
  147. { TDA10048_TIME_WREF_MID2, 0xa8 },
  148. { TDA10048_TIME_WREF_MSB, 0x02 },
  149. { TDA10048_TIME_INVWREF_LSB, 0x04 },
  150. { TDA10048_TIME_INVWREF_MSB, 0x06 },
  151. { TDA10048_CONF_C4_1, 0x00 },
  152. { TDA10048_CONF_C1_1, 0xa8 },
  153. { TDA10048_AGC_CONF, 0x16 },
  154. { TDA10048_CONF_C1_3, 0x0b },
  155. { TDA10048_AGC_TUN_MIN, 0x00 },
  156. { TDA10048_AGC_TUN_MAX, 0xff },
  157. { TDA10048_AGC_IF_MIN, 0x00 },
  158. { TDA10048_AGC_IF_MAX, 0xff },
  159. { TDA10048_AGC_THRESHOLD_MSB, 0x00 },
  160. { TDA10048_AGC_THRESHOLD_LSB, 0x70 },
  161. { TDA10048_CYBER_CTRL, 0x38 },
  162. { TDA10048_AGC_GAINS, 0x12 },
  163. { TDA10048_CONF_XO, 0x00 },
  164. { TDA10048_CONF_TS1, 0x07 },
  165. { TDA10048_IC_MODE, 0x00 },
  166. { TDA10048_CONF_TS2, 0xc0 },
  167. { TDA10048_CONF_TRISTATE1, 0x21 },
  168. { TDA10048_CONF_TRISTATE2, 0x00 },
  169. { TDA10048_CONF_POLARITY, 0x00 },
  170. { TDA10048_CONF_C4_2, 0x04 },
  171. { TDA10048_CONF_ADC, 0x60 },
  172. { TDA10048_CONF_ADC_2, 0x10 },
  173. { TDA10048_CONF_ADC, 0x60 },
  174. { TDA10048_CONF_ADC_2, 0x00 },
  175. { TDA10048_CONF_C1_1, 0xa8 },
  176. { TDA10048_UNCOR_CTRL, 0x00 },
  177. { TDA10048_CONF_C4_2, 0x04 },
  178. };
  179. static int tda10048_writereg(struct tda10048_state *state, u8 reg, u8 data)
  180. {
  181. int ret;
  182. u8 buf[] = { reg, data };
  183. struct i2c_msg msg = {
  184. .addr = state->config->demod_address,
  185. .flags = 0, .buf = buf, .len = 2 };
  186. dprintk(2, "%s(reg = 0x%02x, data = 0x%02x)\n", __func__, reg, data);
  187. ret = i2c_transfer(state->i2c, &msg, 1);
  188. if (ret != 1)
  189. printk("%s: writereg error (ret == %i)\n", __func__, ret);
  190. return (ret != 1) ? -1 : 0;
  191. }
  192. static u8 tda10048_readreg(struct tda10048_state *state, u8 reg)
  193. {
  194. int ret;
  195. u8 b0[] = { reg };
  196. u8 b1[] = { 0 };
  197. struct i2c_msg msg[] = {
  198. { .addr = state->config->demod_address,
  199. .flags = 0, .buf = b0, .len = 1 },
  200. { .addr = state->config->demod_address,
  201. .flags = I2C_M_RD, .buf = b1, .len = 1 } };
  202. dprintk(2, "%s(reg = 0x%02x)\n", __func__, reg);
  203. ret = i2c_transfer(state->i2c, msg, 2);
  204. if (ret != 2)
  205. printk(KERN_ERR "%s: readreg error (ret == %i)\n",
  206. __func__, ret);
  207. return b1[0];
  208. }
  209. static int tda10048_writeregbulk(struct tda10048_state *state, u8 reg,
  210. const u8 *data, u16 len)
  211. {
  212. int ret = -EREMOTEIO;
  213. struct i2c_msg msg;
  214. u8 *buf;
  215. dprintk(2, "%s(%d, ?, len = %d)\n", __func__, reg, len);
  216. buf = kmalloc(len + 1, GFP_KERNEL);
  217. if (buf == NULL) {
  218. ret = -ENOMEM;
  219. goto error;
  220. }
  221. *buf = reg;
  222. memcpy(buf + 1, data, len);
  223. msg.addr = state->config->demod_address;
  224. msg.flags = 0;
  225. msg.buf = buf;
  226. msg.len = len + 1;
  227. dprintk(2, "%s(): write len = %d\n",
  228. __func__, msg.len);
  229. ret = i2c_transfer(state->i2c, &msg, 1);
  230. if (ret != 1) {
  231. printk(KERN_ERR "%s(): writereg error err %i\n",
  232. __func__, ret);
  233. ret = -EREMOTEIO;
  234. }
  235. error:
  236. kfree(buf);
  237. return ret;
  238. }
  239. static int tda10048_firmware_upload(struct dvb_frontend *fe)
  240. {
  241. struct tda10048_state *state = fe->demodulator_priv;
  242. const struct firmware *fw;
  243. int ret;
  244. int pos = 0;
  245. int cnt;
  246. u8 wlen = state->config->fwbulkwritelen;
  247. if ((wlen != TDA10048_BULKWRITE_200) && (wlen != TDA10048_BULKWRITE_50))
  248. wlen = TDA10048_BULKWRITE_200;
  249. /* request the firmware, this will block and timeout */
  250. printk(KERN_INFO "%s: waiting for firmware upload (%s)...\n",
  251. __func__,
  252. TDA10048_DEFAULT_FIRMWARE);
  253. ret = request_firmware(&fw, TDA10048_DEFAULT_FIRMWARE,
  254. &state->i2c->dev);
  255. if (ret) {
  256. printk(KERN_ERR "%s: Upload failed. (file not found?)\n",
  257. __func__);
  258. return -EIO;
  259. } else {
  260. printk(KERN_INFO "%s: firmware read %Zu bytes.\n",
  261. __func__,
  262. fw->size);
  263. ret = 0;
  264. }
  265. if (fw->size != TDA10048_DEFAULT_FIRMWARE_SIZE) {
  266. printk(KERN_ERR "%s: firmware incorrect size\n", __func__);
  267. ret = -EIO;
  268. } else {
  269. printk(KERN_INFO "%s: firmware uploading\n", __func__);
  270. /* Soft reset */
  271. tda10048_writereg(state, TDA10048_CONF_TRISTATE1,
  272. tda10048_readreg(state, TDA10048_CONF_TRISTATE1)
  273. & 0xfe);
  274. tda10048_writereg(state, TDA10048_CONF_TRISTATE1,
  275. tda10048_readreg(state, TDA10048_CONF_TRISTATE1)
  276. | 0x01);
  277. /* Put the demod into host download mode */
  278. tda10048_writereg(state, TDA10048_CONF_C4_1,
  279. tda10048_readreg(state, TDA10048_CONF_C4_1) & 0xf9);
  280. /* Boot the DSP */
  281. tda10048_writereg(state, TDA10048_CONF_C4_1,
  282. tda10048_readreg(state, TDA10048_CONF_C4_1) | 0x08);
  283. /* Prepare for download */
  284. tda10048_writereg(state, TDA10048_DSP_CODE_CPT, 0);
  285. /* Download the firmware payload */
  286. while (pos < fw->size) {
  287. if ((fw->size - pos) > wlen)
  288. cnt = wlen;
  289. else
  290. cnt = fw->size - pos;
  291. tda10048_writeregbulk(state, TDA10048_DSP_CODE_IN,
  292. &fw->data[pos], cnt);
  293. pos += cnt;
  294. }
  295. ret = -EIO;
  296. /* Wait up to 250ms for the DSP to boot */
  297. for (cnt = 0; cnt < 250 ; cnt += 10) {
  298. msleep(10);
  299. if (tda10048_readreg(state, TDA10048_SYNC_STATUS)
  300. & 0x40) {
  301. ret = 0;
  302. break;
  303. }
  304. }
  305. }
  306. release_firmware(fw);
  307. if (ret == 0) {
  308. printk(KERN_INFO "%s: firmware uploaded\n", __func__);
  309. state->fwloaded = 1;
  310. } else
  311. printk(KERN_ERR "%s: firmware upload failed\n", __func__);
  312. return ret;
  313. }
  314. static int tda10048_set_inversion(struct dvb_frontend *fe, int inversion)
  315. {
  316. struct tda10048_state *state = fe->demodulator_priv;
  317. dprintk(1, "%s(%d)\n", __func__, inversion);
  318. if (inversion == TDA10048_INVERSION_ON)
  319. tda10048_writereg(state, TDA10048_CONF_C1_1,
  320. tda10048_readreg(state, TDA10048_CONF_C1_1) | 0x20);
  321. else
  322. tda10048_writereg(state, TDA10048_CONF_C1_1,
  323. tda10048_readreg(state, TDA10048_CONF_C1_1) & 0xdf);
  324. return 0;
  325. }
  326. /* Retrieve the demod settings */
  327. static int tda10048_get_tps(struct tda10048_state *state,
  328. struct dvb_ofdm_parameters *p)
  329. {
  330. u8 val;
  331. /* Make sure the TPS regs are valid */
  332. if (!(tda10048_readreg(state, TDA10048_AUTO) & 0x01))
  333. return -EAGAIN;
  334. val = tda10048_readreg(state, TDA10048_OUT_CONF2);
  335. switch ((val & 0x60) >> 5) {
  336. case 0:
  337. p->constellation = QPSK;
  338. break;
  339. case 1:
  340. p->constellation = QAM_16;
  341. break;
  342. case 2:
  343. p->constellation = QAM_64;
  344. break;
  345. }
  346. switch ((val & 0x18) >> 3) {
  347. case 0:
  348. p->hierarchy_information = HIERARCHY_NONE;
  349. break;
  350. case 1:
  351. p->hierarchy_information = HIERARCHY_1;
  352. break;
  353. case 2:
  354. p->hierarchy_information = HIERARCHY_2;
  355. break;
  356. case 3:
  357. p->hierarchy_information = HIERARCHY_4;
  358. break;
  359. }
  360. switch (val & 0x07) {
  361. case 0:
  362. p->code_rate_HP = FEC_1_2;
  363. break;
  364. case 1:
  365. p->code_rate_HP = FEC_2_3;
  366. break;
  367. case 2:
  368. p->code_rate_HP = FEC_3_4;
  369. break;
  370. case 3:
  371. p->code_rate_HP = FEC_5_6;
  372. break;
  373. case 4:
  374. p->code_rate_HP = FEC_7_8;
  375. break;
  376. }
  377. val = tda10048_readreg(state, TDA10048_OUT_CONF3);
  378. switch (val & 0x07) {
  379. case 0:
  380. p->code_rate_LP = FEC_1_2;
  381. break;
  382. case 1:
  383. p->code_rate_LP = FEC_2_3;
  384. break;
  385. case 2:
  386. p->code_rate_LP = FEC_3_4;
  387. break;
  388. case 3:
  389. p->code_rate_LP = FEC_5_6;
  390. break;
  391. case 4:
  392. p->code_rate_LP = FEC_7_8;
  393. break;
  394. }
  395. val = tda10048_readreg(state, TDA10048_OUT_CONF1);
  396. switch ((val & 0x0c) >> 2) {
  397. case 0:
  398. p->guard_interval = GUARD_INTERVAL_1_32;
  399. break;
  400. case 1:
  401. p->guard_interval = GUARD_INTERVAL_1_16;
  402. break;
  403. case 2:
  404. p->guard_interval = GUARD_INTERVAL_1_8;
  405. break;
  406. case 3:
  407. p->guard_interval = GUARD_INTERVAL_1_4;
  408. break;
  409. }
  410. switch (val & 0x02) {
  411. case 0:
  412. p->transmission_mode = TRANSMISSION_MODE_2K;
  413. break;
  414. case 1:
  415. p->transmission_mode = TRANSMISSION_MODE_8K;
  416. break;
  417. }
  418. return 0;
  419. }
  420. static int tda10048_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  421. {
  422. struct tda10048_state *state = fe->demodulator_priv;
  423. dprintk(1, "%s(%d)\n", __func__, enable);
  424. if (enable)
  425. return tda10048_writereg(state, TDA10048_CONF_C4_1,
  426. tda10048_readreg(state, TDA10048_CONF_C4_1) | 0x02);
  427. else
  428. return tda10048_writereg(state, TDA10048_CONF_C4_1,
  429. tda10048_readreg(state, TDA10048_CONF_C4_1) & 0xfd);
  430. }
  431. static int tda10048_output_mode(struct dvb_frontend *fe, int serial)
  432. {
  433. struct tda10048_state *state = fe->demodulator_priv;
  434. dprintk(1, "%s(%d)\n", __func__, serial);
  435. /* Ensure pins are out of tri-state */
  436. tda10048_writereg(state, TDA10048_CONF_TRISTATE1, 0x21);
  437. tda10048_writereg(state, TDA10048_CONF_TRISTATE2, 0x00);
  438. if (serial) {
  439. tda10048_writereg(state, TDA10048_IC_MODE, 0x80 | 0x20);
  440. tda10048_writereg(state, TDA10048_CONF_TS2, 0xc0);
  441. } else {
  442. tda10048_writereg(state, TDA10048_IC_MODE, 0x00);
  443. tda10048_writereg(state, TDA10048_CONF_TS2, 0x01);
  444. }
  445. return 0;
  446. }
  447. /* Talk to the demod, set the FEC, GUARD, QAM settings etc */
  448. /* TODO: Support manual tuning with specific params */
  449. static int tda10048_set_frontend(struct dvb_frontend *fe,
  450. struct dvb_frontend_parameters *p)
  451. {
  452. struct tda10048_state *state = fe->demodulator_priv;
  453. dprintk(1, "%s(frequency=%d)\n", __func__, p->frequency);
  454. if (fe->ops.tuner_ops.set_params) {
  455. if (fe->ops.i2c_gate_ctrl)
  456. fe->ops.i2c_gate_ctrl(fe, 1);
  457. fe->ops.tuner_ops.set_params(fe, p);
  458. if (fe->ops.i2c_gate_ctrl)
  459. fe->ops.i2c_gate_ctrl(fe, 0);
  460. }
  461. /* Enable demod TPS auto detection and begin acquisition */
  462. tda10048_writereg(state, TDA10048_AUTO, 0x57);
  463. return 0;
  464. }
  465. /* Establish sane defaults and load firmware. */
  466. static int tda10048_init(struct dvb_frontend *fe)
  467. {
  468. struct tda10048_state *state = fe->demodulator_priv;
  469. int ret = 0, i;
  470. dprintk(1, "%s()\n", __func__);
  471. /* Apply register defaults */
  472. for (i = 0; i < ARRAY_SIZE(init_tab); i++)
  473. tda10048_writereg(state, init_tab[i].reg, init_tab[i].data);
  474. if (state->fwloaded == 0)
  475. ret = tda10048_firmware_upload(fe);
  476. /* Set either serial or parallel */
  477. tda10048_output_mode(fe, state->config->output_mode);
  478. /* set inversion */
  479. tda10048_set_inversion(fe, state->config->inversion);
  480. /* Ensure we leave the gate closed */
  481. tda10048_i2c_gate_ctrl(fe, 0);
  482. return ret;
  483. }
  484. static int tda10048_read_status(struct dvb_frontend *fe, fe_status_t *status)
  485. {
  486. struct tda10048_state *state = fe->demodulator_priv;
  487. u8 reg;
  488. *status = 0;
  489. reg = tda10048_readreg(state, TDA10048_SYNC_STATUS);
  490. dprintk(1, "%s() status =0x%02x\n", __func__, reg);
  491. if (reg & 0x02)
  492. *status |= FE_HAS_CARRIER;
  493. if (reg & 0x04)
  494. *status |= FE_HAS_SIGNAL;
  495. if (reg & 0x08) {
  496. *status |= FE_HAS_LOCK;
  497. *status |= FE_HAS_VITERBI;
  498. *status |= FE_HAS_SYNC;
  499. }
  500. return 0;
  501. }
  502. static int tda10048_read_ber(struct dvb_frontend *fe, u32 *ber)
  503. {
  504. struct tda10048_state *state = fe->demodulator_priv;
  505. dprintk(1, "%s()\n", __func__);
  506. /* TODO: A reset may be required here */
  507. *ber = tda10048_readreg(state, TDA10048_CBER_MSB) << 8 |
  508. tda10048_readreg(state, TDA10048_CBER_LSB);
  509. return 0;
  510. }
  511. static int tda10048_read_signal_strength(struct dvb_frontend *fe,
  512. u16 *signal_strength)
  513. {
  514. struct tda10048_state *state = fe->demodulator_priv;
  515. u8 v;
  516. dprintk(1, "%s()\n", __func__);
  517. *signal_strength = 65535;
  518. v = tda10048_readreg(state, TDA10048_NP_OUT);
  519. if (v > 0)
  520. *signal_strength -= (v << 8) | v;
  521. return 0;
  522. }
  523. /* SNR lookup table */
  524. static struct snr_tab {
  525. u8 val;
  526. u8 data;
  527. } snr_tab[] = {
  528. { 0, 0 },
  529. { 1, 246 },
  530. { 2, 215 },
  531. { 3, 198 },
  532. { 4, 185 },
  533. { 5, 176 },
  534. { 6, 168 },
  535. { 7, 161 },
  536. { 8, 155 },
  537. { 9, 150 },
  538. { 10, 146 },
  539. { 11, 141 },
  540. { 12, 138 },
  541. { 13, 134 },
  542. { 14, 131 },
  543. { 15, 128 },
  544. { 16, 125 },
  545. { 17, 122 },
  546. { 18, 120 },
  547. { 19, 118 },
  548. { 20, 115 },
  549. { 21, 113 },
  550. { 22, 111 },
  551. { 23, 109 },
  552. { 24, 107 },
  553. { 25, 106 },
  554. { 26, 104 },
  555. { 27, 102 },
  556. { 28, 101 },
  557. { 29, 99 },
  558. { 30, 98 },
  559. { 31, 96 },
  560. { 32, 95 },
  561. { 33, 94 },
  562. { 34, 92 },
  563. { 35, 91 },
  564. { 36, 90 },
  565. { 37, 89 },
  566. { 38, 88 },
  567. { 39, 86 },
  568. { 40, 85 },
  569. { 41, 84 },
  570. { 42, 83 },
  571. { 43, 82 },
  572. { 44, 81 },
  573. { 45, 80 },
  574. { 46, 79 },
  575. { 47, 78 },
  576. { 48, 77 },
  577. { 49, 76 },
  578. { 50, 76 },
  579. { 51, 75 },
  580. { 52, 74 },
  581. { 53, 73 },
  582. { 54, 72 },
  583. { 56, 71 },
  584. { 57, 70 },
  585. { 58, 69 },
  586. { 60, 68 },
  587. { 61, 67 },
  588. { 63, 66 },
  589. { 64, 65 },
  590. { 66, 64 },
  591. { 67, 63 },
  592. { 68, 62 },
  593. { 69, 62 },
  594. { 70, 61 },
  595. { 72, 60 },
  596. { 74, 59 },
  597. { 75, 58 },
  598. { 77, 57 },
  599. { 79, 56 },
  600. { 81, 55 },
  601. { 83, 54 },
  602. { 85, 53 },
  603. { 87, 52 },
  604. { 89, 51 },
  605. { 91, 50 },
  606. { 93, 49 },
  607. { 95, 48 },
  608. { 97, 47 },
  609. { 100, 46 },
  610. { 102, 45 },
  611. { 104, 44 },
  612. { 107, 43 },
  613. { 109, 42 },
  614. { 112, 41 },
  615. { 114, 40 },
  616. { 117, 39 },
  617. { 120, 38 },
  618. { 123, 37 },
  619. { 125, 36 },
  620. { 128, 35 },
  621. { 131, 34 },
  622. { 134, 33 },
  623. { 138, 32 },
  624. { 141, 31 },
  625. { 144, 30 },
  626. { 147, 29 },
  627. { 151, 28 },
  628. { 154, 27 },
  629. { 158, 26 },
  630. { 162, 25 },
  631. { 165, 24 },
  632. { 169, 23 },
  633. { 173, 22 },
  634. { 177, 21 },
  635. { 181, 20 },
  636. { 186, 19 },
  637. { 190, 18 },
  638. { 194, 17 },
  639. { 199, 16 },
  640. { 204, 15 },
  641. { 208, 14 },
  642. { 213, 13 },
  643. { 218, 12 },
  644. { 223, 11 },
  645. { 229, 10 },
  646. { 234, 9 },
  647. { 239, 8 },
  648. { 245, 7 },
  649. { 251, 6 },
  650. { 255, 5 },
  651. };
  652. static int tda10048_read_snr(struct dvb_frontend *fe, u16 *snr)
  653. {
  654. struct tda10048_state *state = fe->demodulator_priv;
  655. u8 v;
  656. int i, ret = -EINVAL;
  657. dprintk(1, "%s()\n", __func__);
  658. v = tda10048_readreg(state, TDA10048_NP_OUT);
  659. for (i = 0; i < ARRAY_SIZE(snr_tab); i++) {
  660. if (v <= snr_tab[i].val) {
  661. *snr = snr_tab[i].data;
  662. ret = 0;
  663. break;
  664. }
  665. }
  666. return ret;
  667. }
  668. static int tda10048_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  669. {
  670. struct tda10048_state *state = fe->demodulator_priv;
  671. dprintk(1, "%s()\n", __func__);
  672. *ucblocks = tda10048_readreg(state, TDA10048_UNCOR_CPT_MSB) << 8 |
  673. tda10048_readreg(state, TDA10048_UNCOR_CPT_LSB);
  674. return 0;
  675. }
  676. static int tda10048_get_frontend(struct dvb_frontend *fe,
  677. struct dvb_frontend_parameters *p)
  678. {
  679. struct tda10048_state *state = fe->demodulator_priv;
  680. dprintk(1, "%s()\n", __func__);
  681. p->inversion = tda10048_readreg(state, TDA10048_CONF_C1_1)
  682. & 0x20 ? INVERSION_ON : INVERSION_OFF;
  683. return tda10048_get_tps(state, &p->u.ofdm);
  684. }
  685. static int tda10048_get_tune_settings(struct dvb_frontend *fe,
  686. struct dvb_frontend_tune_settings *tune)
  687. {
  688. tune->min_delay_ms = 1000;
  689. return 0;
  690. }
  691. static void tda10048_release(struct dvb_frontend *fe)
  692. {
  693. struct tda10048_state *state = fe->demodulator_priv;
  694. dprintk(1, "%s()\n", __func__);
  695. kfree(state);
  696. }
  697. static struct dvb_frontend_ops tda10048_ops;
  698. struct dvb_frontend *tda10048_attach(const struct tda10048_config *config,
  699. struct i2c_adapter *i2c)
  700. {
  701. struct tda10048_state *state = NULL;
  702. dprintk(1, "%s()\n", __func__);
  703. /* allocate memory for the internal state */
  704. state = kmalloc(sizeof(struct tda10048_state), GFP_KERNEL);
  705. if (state == NULL)
  706. goto error;
  707. /* setup the state */
  708. state->config = config;
  709. state->i2c = i2c;
  710. state->fwloaded = 0;
  711. /* check if the demod is present */
  712. if (tda10048_readreg(state, TDA10048_IDENTITY) != 0x048)
  713. goto error;
  714. /* create dvb_frontend */
  715. memcpy(&state->frontend.ops, &tda10048_ops,
  716. sizeof(struct dvb_frontend_ops));
  717. state->frontend.demodulator_priv = state;
  718. /* Leave the gate closed */
  719. tda10048_i2c_gate_ctrl(&state->frontend, 0);
  720. return &state->frontend;
  721. error:
  722. kfree(state);
  723. return NULL;
  724. }
  725. EXPORT_SYMBOL(tda10048_attach);
  726. static struct dvb_frontend_ops tda10048_ops = {
  727. .info = {
  728. .name = "NXP TDA10048HN DVB-T",
  729. .type = FE_OFDM,
  730. .frequency_min = 177000000,
  731. .frequency_max = 858000000,
  732. .frequency_stepsize = 166666,
  733. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  734. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  735. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  736. FE_CAN_HIERARCHY_AUTO | FE_CAN_GUARD_INTERVAL_AUTO |
  737. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_RECOVER
  738. },
  739. .release = tda10048_release,
  740. .init = tda10048_init,
  741. .i2c_gate_ctrl = tda10048_i2c_gate_ctrl,
  742. .set_frontend = tda10048_set_frontend,
  743. .get_frontend = tda10048_get_frontend,
  744. .get_tune_settings = tda10048_get_tune_settings,
  745. .read_status = tda10048_read_status,
  746. .read_ber = tda10048_read_ber,
  747. .read_signal_strength = tda10048_read_signal_strength,
  748. .read_snr = tda10048_read_snr,
  749. .read_ucblocks = tda10048_read_ucblocks,
  750. };
  751. module_param(debug, int, 0644);
  752. MODULE_PARM_DESC(debug, "Enable verbose debug messages");
  753. MODULE_DESCRIPTION("NXP TDA10048HN DVB-T Demodulator driver");
  754. MODULE_AUTHOR("Steven Toth");
  755. MODULE_LICENSE("GPL");