tda10048.c 28 KB

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