tda10048.c 19 KB

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