tda1004x.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. Driver for Philips tda1004xh OFDM Demodulator
  4. (c) 2003, 2004 Andrew de Quincey & Robert Schlabbach
  5. */
  6. /*
  7. * This driver needs external firmware. Please use the commands
  8. * "<kerneldir>/scripts/get_dvb_firmware tda10045",
  9. * "<kerneldir>/scripts/get_dvb_firmware tda10046" to
  10. * download/extract them, and then copy them to /usr/lib/hotplug/firmware
  11. * or /lib/firmware (depending on configuration of firmware hotplug).
  12. */
  13. #define TDA10045_DEFAULT_FIRMWARE "dvb-fe-tda10045.fw"
  14. #define TDA10046_DEFAULT_FIRMWARE "dvb-fe-tda10046.fw"
  15. #include <linux/init.h>
  16. #include <linux/module.h>
  17. #include <linux/device.h>
  18. #include <linux/jiffies.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <media/dvb_frontend.h>
  22. #include "tda1004x.h"
  23. static int debug;
  24. #define dprintk(args...) \
  25. do { \
  26. if (debug) printk(KERN_DEBUG "tda1004x: " args); \
  27. } while (0)
  28. #define TDA1004X_CHIPID 0x00
  29. #define TDA1004X_AUTO 0x01
  30. #define TDA1004X_IN_CONF1 0x02
  31. #define TDA1004X_IN_CONF2 0x03
  32. #define TDA1004X_OUT_CONF1 0x04
  33. #define TDA1004X_OUT_CONF2 0x05
  34. #define TDA1004X_STATUS_CD 0x06
  35. #define TDA1004X_CONFC4 0x07
  36. #define TDA1004X_DSSPARE2 0x0C
  37. #define TDA10045H_CODE_IN 0x0D
  38. #define TDA10045H_FWPAGE 0x0E
  39. #define TDA1004X_SCAN_CPT 0x10
  40. #define TDA1004X_DSP_CMD 0x11
  41. #define TDA1004X_DSP_ARG 0x12
  42. #define TDA1004X_DSP_DATA1 0x13
  43. #define TDA1004X_DSP_DATA2 0x14
  44. #define TDA1004X_CONFADC1 0x15
  45. #define TDA1004X_CONFC1 0x16
  46. #define TDA10045H_S_AGC 0x1a
  47. #define TDA10046H_AGC_TUN_LEVEL 0x1a
  48. #define TDA1004X_SNR 0x1c
  49. #define TDA1004X_CONF_TS1 0x1e
  50. #define TDA1004X_CONF_TS2 0x1f
  51. #define TDA1004X_CBER_RESET 0x20
  52. #define TDA1004X_CBER_MSB 0x21
  53. #define TDA1004X_CBER_LSB 0x22
  54. #define TDA1004X_CVBER_LUT 0x23
  55. #define TDA1004X_VBER_MSB 0x24
  56. #define TDA1004X_VBER_MID 0x25
  57. #define TDA1004X_VBER_LSB 0x26
  58. #define TDA1004X_UNCOR 0x27
  59. #define TDA10045H_CONFPLL_P 0x2D
  60. #define TDA10045H_CONFPLL_M_MSB 0x2E
  61. #define TDA10045H_CONFPLL_M_LSB 0x2F
  62. #define TDA10045H_CONFPLL_N 0x30
  63. #define TDA10046H_CONFPLL1 0x2D
  64. #define TDA10046H_CONFPLL2 0x2F
  65. #define TDA10046H_CONFPLL3 0x30
  66. #define TDA10046H_TIME_WREF1 0x31
  67. #define TDA10046H_TIME_WREF2 0x32
  68. #define TDA10046H_TIME_WREF3 0x33
  69. #define TDA10046H_TIME_WREF4 0x34
  70. #define TDA10046H_TIME_WREF5 0x35
  71. #define TDA10045H_UNSURW_MSB 0x31
  72. #define TDA10045H_UNSURW_LSB 0x32
  73. #define TDA10045H_WREF_MSB 0x33
  74. #define TDA10045H_WREF_MID 0x34
  75. #define TDA10045H_WREF_LSB 0x35
  76. #define TDA10045H_MUXOUT 0x36
  77. #define TDA1004X_CONFADC2 0x37
  78. #define TDA10045H_IOFFSET 0x38
  79. #define TDA10046H_CONF_TRISTATE1 0x3B
  80. #define TDA10046H_CONF_TRISTATE2 0x3C
  81. #define TDA10046H_CONF_POLARITY 0x3D
  82. #define TDA10046H_FREQ_OFFSET 0x3E
  83. #define TDA10046H_GPIO_OUT_SEL 0x41
  84. #define TDA10046H_GPIO_SELECT 0x42
  85. #define TDA10046H_AGC_CONF 0x43
  86. #define TDA10046H_AGC_THR 0x44
  87. #define TDA10046H_AGC_RENORM 0x45
  88. #define TDA10046H_AGC_GAINS 0x46
  89. #define TDA10046H_AGC_TUN_MIN 0x47
  90. #define TDA10046H_AGC_TUN_MAX 0x48
  91. #define TDA10046H_AGC_IF_MIN 0x49
  92. #define TDA10046H_AGC_IF_MAX 0x4A
  93. #define TDA10046H_FREQ_PHY2_MSB 0x4D
  94. #define TDA10046H_FREQ_PHY2_LSB 0x4E
  95. #define TDA10046H_CVBER_CTRL 0x4F
  96. #define TDA10046H_AGC_IF_LEVEL 0x52
  97. #define TDA10046H_CODE_CPT 0x57
  98. #define TDA10046H_CODE_IN 0x58
  99. static int tda1004x_write_byteI(struct tda1004x_state *state, int reg, int data)
  100. {
  101. int ret;
  102. u8 buf[] = { reg, data };
  103. struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
  104. dprintk("%s: reg=0x%x, data=0x%x\n", __func__, reg, data);
  105. msg.addr = state->config->demod_address;
  106. ret = i2c_transfer(state->i2c, &msg, 1);
  107. if (ret != 1)
  108. dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
  109. __func__, reg, data, ret);
  110. dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __func__,
  111. reg, data, ret);
  112. return (ret != 1) ? -1 : 0;
  113. }
  114. static int tda1004x_read_byte(struct tda1004x_state *state, int reg)
  115. {
  116. int ret;
  117. u8 b0[] = { reg };
  118. u8 b1[] = { 0 };
  119. struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
  120. { .flags = I2C_M_RD, .buf = b1, .len = 1 }};
  121. dprintk("%s: reg=0x%x\n", __func__, reg);
  122. msg[0].addr = state->config->demod_address;
  123. msg[1].addr = state->config->demod_address;
  124. ret = i2c_transfer(state->i2c, msg, 2);
  125. if (ret != 2) {
  126. dprintk("%s: error reg=0x%x, ret=%i\n", __func__, reg,
  127. ret);
  128. return -EINVAL;
  129. }
  130. dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __func__,
  131. reg, b1[0], ret);
  132. return b1[0];
  133. }
  134. static int tda1004x_write_mask(struct tda1004x_state *state, int reg, int mask, int data)
  135. {
  136. int val;
  137. dprintk("%s: reg=0x%x, mask=0x%x, data=0x%x\n", __func__, reg,
  138. mask, data);
  139. // read a byte and check
  140. val = tda1004x_read_byte(state, reg);
  141. if (val < 0)
  142. return val;
  143. // mask if off
  144. val = val & ~mask;
  145. val |= data & 0xff;
  146. // write it out again
  147. return tda1004x_write_byteI(state, reg, val);
  148. }
  149. static int tda1004x_write_buf(struct tda1004x_state *state, int reg, unsigned char *buf, int len)
  150. {
  151. int i;
  152. int result;
  153. dprintk("%s: reg=0x%x, len=0x%x\n", __func__, reg, len);
  154. result = 0;
  155. for (i = 0; i < len; i++) {
  156. result = tda1004x_write_byteI(state, reg + i, buf[i]);
  157. if (result != 0)
  158. break;
  159. }
  160. return result;
  161. }
  162. static int tda1004x_enable_tuner_i2c(struct tda1004x_state *state)
  163. {
  164. int result;
  165. dprintk("%s\n", __func__);
  166. result = tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 2);
  167. msleep(20);
  168. return result;
  169. }
  170. static int tda1004x_disable_tuner_i2c(struct tda1004x_state *state)
  171. {
  172. dprintk("%s\n", __func__);
  173. return tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 0);
  174. }
  175. static int tda10045h_set_bandwidth(struct tda1004x_state *state,
  176. u32 bandwidth)
  177. {
  178. static u8 bandwidth_6mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x60, 0x1e, 0xa7, 0x45, 0x4f };
  179. static u8 bandwidth_7mhz[] = { 0x02, 0x00, 0x37, 0x00, 0x4a, 0x2f, 0x6d, 0x76, 0xdb };
  180. static u8 bandwidth_8mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x48, 0x17, 0x89, 0xc7, 0x14 };
  181. switch (bandwidth) {
  182. case 6000000:
  183. tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_6mhz, sizeof(bandwidth_6mhz));
  184. break;
  185. case 7000000:
  186. tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_7mhz, sizeof(bandwidth_7mhz));
  187. break;
  188. case 8000000:
  189. tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_8mhz, sizeof(bandwidth_8mhz));
  190. break;
  191. default:
  192. return -EINVAL;
  193. }
  194. tda1004x_write_byteI(state, TDA10045H_IOFFSET, 0);
  195. return 0;
  196. }
  197. static int tda10046h_set_bandwidth(struct tda1004x_state *state,
  198. u32 bandwidth)
  199. {
  200. static u8 bandwidth_6mhz_53M[] = { 0x7b, 0x2e, 0x11, 0xf0, 0xd2 };
  201. static u8 bandwidth_7mhz_53M[] = { 0x6a, 0x02, 0x6a, 0x43, 0x9f };
  202. static u8 bandwidth_8mhz_53M[] = { 0x5c, 0x32, 0xc2, 0x96, 0x6d };
  203. static u8 bandwidth_6mhz_48M[] = { 0x70, 0x02, 0x49, 0x24, 0x92 };
  204. static u8 bandwidth_7mhz_48M[] = { 0x60, 0x02, 0xaa, 0xaa, 0xab };
  205. static u8 bandwidth_8mhz_48M[] = { 0x54, 0x03, 0x0c, 0x30, 0xc3 };
  206. int tda10046_clk53m;
  207. if ((state->config->if_freq == TDA10046_FREQ_045) ||
  208. (state->config->if_freq == TDA10046_FREQ_052))
  209. tda10046_clk53m = 0;
  210. else
  211. tda10046_clk53m = 1;
  212. switch (bandwidth) {
  213. case 6000000:
  214. if (tda10046_clk53m)
  215. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_53M,
  216. sizeof(bandwidth_6mhz_53M));
  217. else
  218. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_48M,
  219. sizeof(bandwidth_6mhz_48M));
  220. if (state->config->if_freq == TDA10046_FREQ_045) {
  221. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0a);
  222. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xab);
  223. }
  224. break;
  225. case 7000000:
  226. if (tda10046_clk53m)
  227. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_53M,
  228. sizeof(bandwidth_7mhz_53M));
  229. else
  230. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_48M,
  231. sizeof(bandwidth_7mhz_48M));
  232. if (state->config->if_freq == TDA10046_FREQ_045) {
  233. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
  234. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
  235. }
  236. break;
  237. case 8000000:
  238. if (tda10046_clk53m)
  239. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_53M,
  240. sizeof(bandwidth_8mhz_53M));
  241. else
  242. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_48M,
  243. sizeof(bandwidth_8mhz_48M));
  244. if (state->config->if_freq == TDA10046_FREQ_045) {
  245. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
  246. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x55);
  247. }
  248. break;
  249. default:
  250. return -EINVAL;
  251. }
  252. return 0;
  253. }
  254. static int tda1004x_do_upload(struct tda1004x_state *state,
  255. const unsigned char *mem, unsigned int len,
  256. u8 dspCodeCounterReg, u8 dspCodeInReg)
  257. {
  258. u8 buf[65];
  259. struct i2c_msg fw_msg = { .flags = 0, .buf = buf, .len = 0 };
  260. int tx_size;
  261. int pos = 0;
  262. /* clear code counter */
  263. tda1004x_write_byteI(state, dspCodeCounterReg, 0);
  264. fw_msg.addr = state->config->demod_address;
  265. i2c_lock_bus(state->i2c, I2C_LOCK_SEGMENT);
  266. buf[0] = dspCodeInReg;
  267. while (pos != len) {
  268. // work out how much to send this time
  269. tx_size = len - pos;
  270. if (tx_size > 0x10)
  271. tx_size = 0x10;
  272. // send the chunk
  273. memcpy(buf + 1, mem + pos, tx_size);
  274. fw_msg.len = tx_size + 1;
  275. if (__i2c_transfer(state->i2c, &fw_msg, 1) != 1) {
  276. printk(KERN_ERR "tda1004x: Error during firmware upload\n");
  277. i2c_unlock_bus(state->i2c, I2C_LOCK_SEGMENT);
  278. return -EIO;
  279. }
  280. pos += tx_size;
  281. dprintk("%s: fw_pos=0x%x\n", __func__, pos);
  282. }
  283. i2c_unlock_bus(state->i2c, I2C_LOCK_SEGMENT);
  284. /* give the DSP a chance to settle 03/10/05 Hac */
  285. msleep(100);
  286. return 0;
  287. }
  288. static int tda1004x_check_upload_ok(struct tda1004x_state *state)
  289. {
  290. u8 data1, data2;
  291. unsigned long timeout;
  292. if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
  293. timeout = jiffies + 2 * HZ;
  294. while(!(tda1004x_read_byte(state, TDA1004X_STATUS_CD) & 0x20)) {
  295. if (time_after(jiffies, timeout)) {
  296. printk(KERN_ERR "tda1004x: timeout waiting for DSP ready\n");
  297. break;
  298. }
  299. msleep(1);
  300. }
  301. } else
  302. msleep(100);
  303. // check upload was OK
  304. tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0); // we want to read from the DSP
  305. tda1004x_write_byteI(state, TDA1004X_DSP_CMD, 0x67);
  306. data1 = tda1004x_read_byte(state, TDA1004X_DSP_DATA1);
  307. data2 = tda1004x_read_byte(state, TDA1004X_DSP_DATA2);
  308. if (data1 != 0x67 || data2 < 0x20 || data2 > 0x2e) {
  309. printk(KERN_INFO "tda1004x: found firmware revision %x -- invalid\n", data2);
  310. return -EIO;
  311. }
  312. printk(KERN_INFO "tda1004x: found firmware revision %x -- ok\n", data2);
  313. return 0;
  314. }
  315. static int tda10045_fwupload(struct dvb_frontend* fe)
  316. {
  317. struct tda1004x_state* state = fe->demodulator_priv;
  318. int ret;
  319. const struct firmware *fw;
  320. /* don't re-upload unless necessary */
  321. if (tda1004x_check_upload_ok(state) == 0)
  322. return 0;
  323. /* request the firmware, this will block until someone uploads it */
  324. printk(KERN_INFO "tda1004x: waiting for firmware upload (%s)...\n", TDA10045_DEFAULT_FIRMWARE);
  325. ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
  326. if (ret) {
  327. printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
  328. return ret;
  329. }
  330. /* reset chip */
  331. tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0);
  332. tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
  333. tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
  334. msleep(10);
  335. /* set parameters */
  336. tda10045h_set_bandwidth(state, 8000000);
  337. ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10045H_FWPAGE, TDA10045H_CODE_IN);
  338. release_firmware(fw);
  339. if (ret)
  340. return ret;
  341. printk(KERN_INFO "tda1004x: firmware upload complete\n");
  342. /* wait for DSP to initialise */
  343. /* DSPREADY doesn't seem to work on the TDA10045H */
  344. msleep(100);
  345. return tda1004x_check_upload_ok(state);
  346. }
  347. static void tda10046_init_plls(struct dvb_frontend* fe)
  348. {
  349. struct tda1004x_state* state = fe->demodulator_priv;
  350. int tda10046_clk53m;
  351. if ((state->config->if_freq == TDA10046_FREQ_045) ||
  352. (state->config->if_freq == TDA10046_FREQ_052))
  353. tda10046_clk53m = 0;
  354. else
  355. tda10046_clk53m = 1;
  356. tda1004x_write_byteI(state, TDA10046H_CONFPLL1, 0xf0);
  357. if(tda10046_clk53m) {
  358. printk(KERN_INFO "tda1004x: setting up plls for 53MHz sampling clock\n");
  359. tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x08); // PLL M = 8
  360. } else {
  361. printk(KERN_INFO "tda1004x: setting up plls for 48MHz sampling clock\n");
  362. tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x03); // PLL M = 3
  363. }
  364. if (state->config->xtal_freq == TDA10046_XTAL_4M ) {
  365. dprintk("%s: setting up PLLs for a 4 MHz Xtal\n", __func__);
  366. tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 0); // PLL P = N = 0
  367. } else {
  368. dprintk("%s: setting up PLLs for a 16 MHz Xtal\n", __func__);
  369. tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 3); // PLL P = 0, N = 3
  370. }
  371. if(tda10046_clk53m)
  372. tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x67);
  373. else
  374. tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x72);
  375. /* Note clock frequency is handled implicitly */
  376. switch (state->config->if_freq) {
  377. case TDA10046_FREQ_045:
  378. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
  379. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
  380. break;
  381. case TDA10046_FREQ_052:
  382. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
  383. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xc7);
  384. break;
  385. case TDA10046_FREQ_3617:
  386. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
  387. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x59);
  388. break;
  389. case TDA10046_FREQ_3613:
  390. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
  391. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x3f);
  392. break;
  393. }
  394. tda10046h_set_bandwidth(state, 8000000); /* default bandwidth 8 MHz */
  395. /* let the PLLs settle */
  396. msleep(120);
  397. }
  398. static int tda10046_fwupload(struct dvb_frontend* fe)
  399. {
  400. struct tda1004x_state* state = fe->demodulator_priv;
  401. int ret, confc4;
  402. const struct firmware *fw;
  403. /* reset + wake up chip */
  404. if (state->config->xtal_freq == TDA10046_XTAL_4M) {
  405. confc4 = 0;
  406. } else {
  407. dprintk("%s: 16MHz Xtal, reducing I2C speed\n", __func__);
  408. confc4 = 0x80;
  409. }
  410. tda1004x_write_byteI(state, TDA1004X_CONFC4, confc4);
  411. tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 1, 0);
  412. /* set GPIO 1 and 3 */
  413. if (state->config->gpio_config != TDA10046_GPTRI) {
  414. tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE2, 0x33);
  415. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f, state->config->gpio_config &0x0f);
  416. }
  417. /* let the clocks recover from sleep */
  418. msleep(10);
  419. /* The PLLs need to be reprogrammed after sleep */
  420. tda10046_init_plls(fe);
  421. tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0);
  422. /* don't re-upload unless necessary */
  423. if (tda1004x_check_upload_ok(state) == 0)
  424. return 0;
  425. /*
  426. For i2c normal work, we need to slow down the bus speed.
  427. However, the slow down breaks the eeprom firmware load.
  428. So, use normal speed for eeprom booting and then restore the
  429. i2c speed after that. Tested with MSI TV @nyware A/D board,
  430. that comes with firmware version 29 inside their eeprom.
  431. It should also be noticed that no other I2C transfer should
  432. be in course while booting from eeprom, otherwise, tda10046
  433. goes into an instable state. So, proper locking are needed
  434. at the i2c bus master.
  435. */
  436. printk(KERN_INFO "tda1004x: trying to boot from eeprom\n");
  437. tda1004x_write_byteI(state, TDA1004X_CONFC4, 4);
  438. msleep(300);
  439. tda1004x_write_byteI(state, TDA1004X_CONFC4, confc4);
  440. /* Checks if eeprom firmware went without troubles */
  441. if (tda1004x_check_upload_ok(state) == 0)
  442. return 0;
  443. /* eeprom firmware didn't work. Load one manually. */
  444. if (state->config->request_firmware != NULL) {
  445. /* request the firmware, this will block until someone uploads it */
  446. printk(KERN_INFO "tda1004x: waiting for firmware upload...\n");
  447. ret = state->config->request_firmware(fe, &fw, TDA10046_DEFAULT_FIRMWARE);
  448. if (ret) {
  449. /* remain compatible to old bug: try to load with tda10045 image name */
  450. ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
  451. if (ret) {
  452. printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
  453. return ret;
  454. } else {
  455. printk(KERN_INFO "tda1004x: please rename the firmware file to %s\n",
  456. TDA10046_DEFAULT_FIRMWARE);
  457. }
  458. }
  459. } else {
  460. printk(KERN_ERR "tda1004x: no request function defined, can't upload from file\n");
  461. return -EIO;
  462. }
  463. tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8); // going to boot from HOST
  464. ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10046H_CODE_CPT, TDA10046H_CODE_IN);
  465. release_firmware(fw);
  466. return tda1004x_check_upload_ok(state);
  467. }
  468. static int tda1004x_encode_fec(int fec)
  469. {
  470. // convert known FEC values
  471. switch (fec) {
  472. case FEC_1_2:
  473. return 0;
  474. case FEC_2_3:
  475. return 1;
  476. case FEC_3_4:
  477. return 2;
  478. case FEC_5_6:
  479. return 3;
  480. case FEC_7_8:
  481. return 4;
  482. }
  483. // unsupported
  484. return -EINVAL;
  485. }
  486. static int tda1004x_decode_fec(int tdafec)
  487. {
  488. // convert known FEC values
  489. switch (tdafec) {
  490. case 0:
  491. return FEC_1_2;
  492. case 1:
  493. return FEC_2_3;
  494. case 2:
  495. return FEC_3_4;
  496. case 3:
  497. return FEC_5_6;
  498. case 4:
  499. return FEC_7_8;
  500. }
  501. // unsupported
  502. return -1;
  503. }
  504. static int tda1004x_write(struct dvb_frontend* fe, const u8 buf[], int len)
  505. {
  506. struct tda1004x_state* state = fe->demodulator_priv;
  507. if (len != 2)
  508. return -EINVAL;
  509. return tda1004x_write_byteI(state, buf[0], buf[1]);
  510. }
  511. static int tda10045_init(struct dvb_frontend* fe)
  512. {
  513. struct tda1004x_state* state = fe->demodulator_priv;
  514. dprintk("%s\n", __func__);
  515. if (tda10045_fwupload(fe)) {
  516. printk("tda1004x: firmware upload failed\n");
  517. return -EIO;
  518. }
  519. tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0); // wake up the ADC
  520. // tda setup
  521. tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
  522. tda1004x_write_mask(state, TDA1004X_AUTO, 8, 0); // select HP stream
  523. tda1004x_write_mask(state, TDA1004X_CONFC1, 0x40, 0); // set polarity of VAGC signal
  524. tda1004x_write_mask(state, TDA1004X_CONFC1, 0x80, 0x80); // enable pulse killer
  525. tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10); // enable auto offset
  526. tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0x0); // no frequency offset
  527. tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 0); // setup MPEG2 TS interface
  528. tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0); // setup MPEG2 TS interface
  529. tda1004x_write_mask(state, TDA1004X_VBER_MSB, 0xe0, 0xa0); // 10^6 VBER measurement bits
  530. tda1004x_write_mask(state, TDA1004X_CONFC1, 0x10, 0); // VAGC polarity
  531. tda1004x_write_byteI(state, TDA1004X_CONFADC1, 0x2e);
  532. tda1004x_write_mask(state, 0x1f, 0x01, state->config->invert_oclk);
  533. return 0;
  534. }
  535. static int tda10046_init(struct dvb_frontend* fe)
  536. {
  537. struct tda1004x_state* state = fe->demodulator_priv;
  538. dprintk("%s\n", __func__);
  539. if (tda10046_fwupload(fe)) {
  540. printk("tda1004x: firmware upload failed\n");
  541. return -EIO;
  542. }
  543. // tda setup
  544. tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
  545. tda1004x_write_byteI(state, TDA1004X_AUTO, 0x87); // 100 ppm crystal, select HP stream
  546. tda1004x_write_byteI(state, TDA1004X_CONFC1, 0x88); // enable pulse killer
  547. switch (state->config->agc_config) {
  548. case TDA10046_AGC_DEFAULT:
  549. tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x00); // AGC setup
  550. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
  551. break;
  552. case TDA10046_AGC_IFO_AUTO_NEG:
  553. tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
  554. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
  555. break;
  556. case TDA10046_AGC_IFO_AUTO_POS:
  557. tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
  558. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x00); // set AGC polarities
  559. break;
  560. case TDA10046_AGC_TDA827X:
  561. tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x02); // AGC setup
  562. tda1004x_write_byteI(state, TDA10046H_AGC_THR, 0x70); // AGC Threshold
  563. tda1004x_write_byteI(state, TDA10046H_AGC_RENORM, 0x08); // Gain Renormalize
  564. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
  565. break;
  566. }
  567. if (state->config->ts_mode == 0) {
  568. tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x40);
  569. tda1004x_write_mask(state, 0x3a, 0x80, state->config->invert_oclk << 7);
  570. } else {
  571. tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x80);
  572. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x10,
  573. state->config->invert_oclk << 4);
  574. }
  575. tda1004x_write_byteI(state, TDA1004X_CONFADC2, 0x38);
  576. tda1004x_write_mask (state, TDA10046H_CONF_TRISTATE1, 0x3e, 0x38); // Turn IF AGC output on
  577. tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MIN, 0); // }
  578. tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MAX, 0xff); // } AGC min/max values
  579. tda1004x_write_byteI(state, TDA10046H_AGC_IF_MIN, 0); // }
  580. tda1004x_write_byteI(state, TDA10046H_AGC_IF_MAX, 0xff); // }
  581. tda1004x_write_byteI(state, TDA10046H_AGC_GAINS, 0x12); // IF gain 2, TUN gain 1
  582. tda1004x_write_byteI(state, TDA10046H_CVBER_CTRL, 0x1a); // 10^6 VBER measurement bits
  583. tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 7); // MPEG2 interface config
  584. tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0xc0); // MPEG2 interface config
  585. // tda1004x_write_mask(state, 0x50, 0x80, 0x80); // handle out of guard echoes
  586. return 0;
  587. }
  588. static int tda1004x_set_fe(struct dvb_frontend *fe)
  589. {
  590. struct dtv_frontend_properties *fe_params = &fe->dtv_property_cache;
  591. struct tda1004x_state* state = fe->demodulator_priv;
  592. int tmp;
  593. int inversion;
  594. dprintk("%s\n", __func__);
  595. if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
  596. // setup auto offset
  597. tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10);
  598. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x80, 0);
  599. tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0);
  600. // disable agc_conf[2]
  601. tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 0);
  602. }
  603. // set frequency
  604. if (fe->ops.tuner_ops.set_params) {
  605. fe->ops.tuner_ops.set_params(fe);
  606. if (fe->ops.i2c_gate_ctrl)
  607. fe->ops.i2c_gate_ctrl(fe, 0);
  608. }
  609. // Hardcoded to use auto as much as possible on the TDA10045 as it
  610. // is very unreliable if AUTO mode is _not_ used.
  611. if (state->demod_type == TDA1004X_DEMOD_TDA10045) {
  612. fe_params->code_rate_HP = FEC_AUTO;
  613. fe_params->guard_interval = GUARD_INTERVAL_AUTO;
  614. fe_params->transmission_mode = TRANSMISSION_MODE_AUTO;
  615. }
  616. // Set standard params.. or put them to auto
  617. if ((fe_params->code_rate_HP == FEC_AUTO) ||
  618. (fe_params->code_rate_LP == FEC_AUTO) ||
  619. (fe_params->modulation == QAM_AUTO) ||
  620. (fe_params->hierarchy == HIERARCHY_AUTO)) {
  621. tda1004x_write_mask(state, TDA1004X_AUTO, 1, 1); // enable auto
  622. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x03, 0); /* turn off modulation bits */
  623. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0); // turn off hierarchy bits
  624. tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x3f, 0); // turn off FEC bits
  625. } else {
  626. tda1004x_write_mask(state, TDA1004X_AUTO, 1, 0); // disable auto
  627. // set HP FEC
  628. tmp = tda1004x_encode_fec(fe_params->code_rate_HP);
  629. if (tmp < 0)
  630. return tmp;
  631. tda1004x_write_mask(state, TDA1004X_IN_CONF2, 7, tmp);
  632. // set LP FEC
  633. tmp = tda1004x_encode_fec(fe_params->code_rate_LP);
  634. if (tmp < 0)
  635. return tmp;
  636. tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x38, tmp << 3);
  637. /* set modulation */
  638. switch (fe_params->modulation) {
  639. case QPSK:
  640. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 0);
  641. break;
  642. case QAM_16:
  643. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 1);
  644. break;
  645. case QAM_64:
  646. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 2);
  647. break;
  648. default:
  649. return -EINVAL;
  650. }
  651. // set hierarchy
  652. switch (fe_params->hierarchy) {
  653. case HIERARCHY_NONE:
  654. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0 << 5);
  655. break;
  656. case HIERARCHY_1:
  657. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 1 << 5);
  658. break;
  659. case HIERARCHY_2:
  660. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 2 << 5);
  661. break;
  662. case HIERARCHY_4:
  663. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 3 << 5);
  664. break;
  665. default:
  666. return -EINVAL;
  667. }
  668. }
  669. // set bandwidth
  670. switch (state->demod_type) {
  671. case TDA1004X_DEMOD_TDA10045:
  672. tda10045h_set_bandwidth(state, fe_params->bandwidth_hz);
  673. break;
  674. case TDA1004X_DEMOD_TDA10046:
  675. tda10046h_set_bandwidth(state, fe_params->bandwidth_hz);
  676. break;
  677. }
  678. // set inversion
  679. inversion = fe_params->inversion;
  680. if (state->config->invert)
  681. inversion = inversion ? INVERSION_OFF : INVERSION_ON;
  682. switch (inversion) {
  683. case INVERSION_OFF:
  684. tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0);
  685. break;
  686. case INVERSION_ON:
  687. tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0x20);
  688. break;
  689. default:
  690. return -EINVAL;
  691. }
  692. // set guard interval
  693. switch (fe_params->guard_interval) {
  694. case GUARD_INTERVAL_1_32:
  695. tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
  696. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
  697. break;
  698. case GUARD_INTERVAL_1_16:
  699. tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
  700. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 1 << 2);
  701. break;
  702. case GUARD_INTERVAL_1_8:
  703. tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
  704. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 2 << 2);
  705. break;
  706. case GUARD_INTERVAL_1_4:
  707. tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
  708. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 3 << 2);
  709. break;
  710. case GUARD_INTERVAL_AUTO:
  711. tda1004x_write_mask(state, TDA1004X_AUTO, 2, 2);
  712. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
  713. break;
  714. default:
  715. return -EINVAL;
  716. }
  717. // set transmission mode
  718. switch (fe_params->transmission_mode) {
  719. case TRANSMISSION_MODE_2K:
  720. tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
  721. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0 << 4);
  722. break;
  723. case TRANSMISSION_MODE_8K:
  724. tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
  725. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 1 << 4);
  726. break;
  727. case TRANSMISSION_MODE_AUTO:
  728. tda1004x_write_mask(state, TDA1004X_AUTO, 4, 4);
  729. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0);
  730. break;
  731. default:
  732. return -EINVAL;
  733. }
  734. // start the lock
  735. switch (state->demod_type) {
  736. case TDA1004X_DEMOD_TDA10045:
  737. tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
  738. tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
  739. break;
  740. case TDA1004X_DEMOD_TDA10046:
  741. tda1004x_write_mask(state, TDA1004X_AUTO, 0x40, 0x40);
  742. msleep(1);
  743. tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 1);
  744. break;
  745. }
  746. msleep(10);
  747. return 0;
  748. }
  749. static int tda1004x_get_fe(struct dvb_frontend *fe,
  750. struct dtv_frontend_properties *fe_params)
  751. {
  752. struct tda1004x_state* state = fe->demodulator_priv;
  753. int status;
  754. dprintk("%s\n", __func__);
  755. status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
  756. if (status == -1)
  757. return -EIO;
  758. /* Only update the properties cache if device is locked */
  759. if (!(status & 8))
  760. return 0;
  761. // inversion status
  762. fe_params->inversion = INVERSION_OFF;
  763. if (tda1004x_read_byte(state, TDA1004X_CONFC1) & 0x20)
  764. fe_params->inversion = INVERSION_ON;
  765. if (state->config->invert)
  766. fe_params->inversion = fe_params->inversion ? INVERSION_OFF : INVERSION_ON;
  767. // bandwidth
  768. switch (state->demod_type) {
  769. case TDA1004X_DEMOD_TDA10045:
  770. switch (tda1004x_read_byte(state, TDA10045H_WREF_LSB)) {
  771. case 0x14:
  772. fe_params->bandwidth_hz = 8000000;
  773. break;
  774. case 0xdb:
  775. fe_params->bandwidth_hz = 7000000;
  776. break;
  777. case 0x4f:
  778. fe_params->bandwidth_hz = 6000000;
  779. break;
  780. }
  781. break;
  782. case TDA1004X_DEMOD_TDA10046:
  783. switch (tda1004x_read_byte(state, TDA10046H_TIME_WREF1)) {
  784. case 0x5c:
  785. case 0x54:
  786. fe_params->bandwidth_hz = 8000000;
  787. break;
  788. case 0x6a:
  789. case 0x60:
  790. fe_params->bandwidth_hz = 7000000;
  791. break;
  792. case 0x7b:
  793. case 0x70:
  794. fe_params->bandwidth_hz = 6000000;
  795. break;
  796. }
  797. break;
  798. }
  799. // FEC
  800. fe_params->code_rate_HP =
  801. tda1004x_decode_fec(tda1004x_read_byte(state, TDA1004X_OUT_CONF2) & 7);
  802. fe_params->code_rate_LP =
  803. tda1004x_decode_fec((tda1004x_read_byte(state, TDA1004X_OUT_CONF2) >> 3) & 7);
  804. /* modulation */
  805. switch (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 3) {
  806. case 0:
  807. fe_params->modulation = QPSK;
  808. break;
  809. case 1:
  810. fe_params->modulation = QAM_16;
  811. break;
  812. case 2:
  813. fe_params->modulation = QAM_64;
  814. break;
  815. }
  816. // transmission mode
  817. fe_params->transmission_mode = TRANSMISSION_MODE_2K;
  818. if (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x10)
  819. fe_params->transmission_mode = TRANSMISSION_MODE_8K;
  820. // guard interval
  821. switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x0c) >> 2) {
  822. case 0:
  823. fe_params->guard_interval = GUARD_INTERVAL_1_32;
  824. break;
  825. case 1:
  826. fe_params->guard_interval = GUARD_INTERVAL_1_16;
  827. break;
  828. case 2:
  829. fe_params->guard_interval = GUARD_INTERVAL_1_8;
  830. break;
  831. case 3:
  832. fe_params->guard_interval = GUARD_INTERVAL_1_4;
  833. break;
  834. }
  835. // hierarchy
  836. switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x60) >> 5) {
  837. case 0:
  838. fe_params->hierarchy = HIERARCHY_NONE;
  839. break;
  840. case 1:
  841. fe_params->hierarchy = HIERARCHY_1;
  842. break;
  843. case 2:
  844. fe_params->hierarchy = HIERARCHY_2;
  845. break;
  846. case 3:
  847. fe_params->hierarchy = HIERARCHY_4;
  848. break;
  849. }
  850. return 0;
  851. }
  852. static int tda1004x_read_status(struct dvb_frontend *fe,
  853. enum fe_status *fe_status)
  854. {
  855. struct tda1004x_state* state = fe->demodulator_priv;
  856. int status;
  857. int cber;
  858. int vber;
  859. dprintk("%s\n", __func__);
  860. // read status
  861. status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
  862. if (status == -1)
  863. return -EIO;
  864. // decode
  865. *fe_status = 0;
  866. if (status & 4)
  867. *fe_status |= FE_HAS_SIGNAL;
  868. if (status & 2)
  869. *fe_status |= FE_HAS_CARRIER;
  870. if (status & 8)
  871. *fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  872. // if we don't already have VITERBI (i.e. not LOCKED), see if the viterbi
  873. // is getting anything valid
  874. if (!(*fe_status & FE_HAS_VITERBI)) {
  875. // read the CBER
  876. cber = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
  877. if (cber == -1)
  878. return -EIO;
  879. status = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
  880. if (status == -1)
  881. return -EIO;
  882. cber |= (status << 8);
  883. // The address 0x20 should be read to cope with a TDA10046 bug
  884. tda1004x_read_byte(state, TDA1004X_CBER_RESET);
  885. if (cber != 65535)
  886. *fe_status |= FE_HAS_VITERBI;
  887. }
  888. // if we DO have some valid VITERBI output, but don't already have SYNC
  889. // bytes (i.e. not LOCKED), see if the RS decoder is getting anything valid.
  890. if ((*fe_status & FE_HAS_VITERBI) && (!(*fe_status & FE_HAS_SYNC))) {
  891. // read the VBER
  892. vber = tda1004x_read_byte(state, TDA1004X_VBER_LSB);
  893. if (vber == -1)
  894. return -EIO;
  895. status = tda1004x_read_byte(state, TDA1004X_VBER_MID);
  896. if (status == -1)
  897. return -EIO;
  898. vber |= (status << 8);
  899. status = tda1004x_read_byte(state, TDA1004X_VBER_MSB);
  900. if (status == -1)
  901. return -EIO;
  902. vber |= (status & 0x0f) << 16;
  903. // The CVBER_LUT should be read to cope with TDA10046 hardware bug
  904. tda1004x_read_byte(state, TDA1004X_CVBER_LUT);
  905. // if RS has passed some valid TS packets, then we must be
  906. // getting some SYNC bytes
  907. if (vber < 16632)
  908. *fe_status |= FE_HAS_SYNC;
  909. }
  910. // success
  911. dprintk("%s: fe_status=0x%x\n", __func__, *fe_status);
  912. return 0;
  913. }
  914. static int tda1004x_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
  915. {
  916. struct tda1004x_state* state = fe->demodulator_priv;
  917. int tmp;
  918. int reg = 0;
  919. dprintk("%s\n", __func__);
  920. // determine the register to use
  921. switch (state->demod_type) {
  922. case TDA1004X_DEMOD_TDA10045:
  923. reg = TDA10045H_S_AGC;
  924. break;
  925. case TDA1004X_DEMOD_TDA10046:
  926. reg = TDA10046H_AGC_IF_LEVEL;
  927. break;
  928. }
  929. // read it
  930. tmp = tda1004x_read_byte(state, reg);
  931. if (tmp < 0)
  932. return -EIO;
  933. *signal = (tmp << 8) | tmp;
  934. dprintk("%s: signal=0x%x\n", __func__, *signal);
  935. return 0;
  936. }
  937. static int tda1004x_read_snr(struct dvb_frontend* fe, u16 * snr)
  938. {
  939. struct tda1004x_state* state = fe->demodulator_priv;
  940. int tmp;
  941. dprintk("%s\n", __func__);
  942. // read it
  943. tmp = tda1004x_read_byte(state, TDA1004X_SNR);
  944. if (tmp < 0)
  945. return -EIO;
  946. tmp = 255 - tmp;
  947. *snr = ((tmp << 8) | tmp);
  948. dprintk("%s: snr=0x%x\n", __func__, *snr);
  949. return 0;
  950. }
  951. static int tda1004x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  952. {
  953. struct tda1004x_state* state = fe->demodulator_priv;
  954. int tmp;
  955. int tmp2;
  956. int counter;
  957. dprintk("%s\n", __func__);
  958. // read the UCBLOCKS and reset
  959. counter = 0;
  960. tmp = tda1004x_read_byte(state, TDA1004X_UNCOR);
  961. if (tmp < 0)
  962. return -EIO;
  963. tmp &= 0x7f;
  964. while (counter++ < 5) {
  965. tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
  966. tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
  967. tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
  968. tmp2 = tda1004x_read_byte(state, TDA1004X_UNCOR);
  969. if (tmp2 < 0)
  970. return -EIO;
  971. tmp2 &= 0x7f;
  972. if ((tmp2 < tmp) || (tmp2 == 0))
  973. break;
  974. }
  975. if (tmp != 0x7f)
  976. *ucblocks = tmp;
  977. else
  978. *ucblocks = 0xffffffff;
  979. dprintk("%s: ucblocks=0x%x\n", __func__, *ucblocks);
  980. return 0;
  981. }
  982. static int tda1004x_read_ber(struct dvb_frontend* fe, u32* ber)
  983. {
  984. struct tda1004x_state* state = fe->demodulator_priv;
  985. int tmp;
  986. dprintk("%s\n", __func__);
  987. // read it in
  988. tmp = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
  989. if (tmp < 0)
  990. return -EIO;
  991. *ber = tmp << 1;
  992. tmp = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
  993. if (tmp < 0)
  994. return -EIO;
  995. *ber |= (tmp << 9);
  996. // The address 0x20 should be read to cope with a TDA10046 bug
  997. tda1004x_read_byte(state, TDA1004X_CBER_RESET);
  998. dprintk("%s: ber=0x%x\n", __func__, *ber);
  999. return 0;
  1000. }
  1001. static int tda1004x_sleep(struct dvb_frontend* fe)
  1002. {
  1003. struct tda1004x_state* state = fe->demodulator_priv;
  1004. int gpio_conf;
  1005. switch (state->demod_type) {
  1006. case TDA1004X_DEMOD_TDA10045:
  1007. tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0x10);
  1008. break;
  1009. case TDA1004X_DEMOD_TDA10046:
  1010. /* set outputs to tristate */
  1011. tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0xff);
  1012. /* invert GPIO 1 and 3 if desired*/
  1013. gpio_conf = state->config->gpio_config;
  1014. if (gpio_conf >= TDA10046_GP00_I)
  1015. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f,
  1016. (gpio_conf & 0x0f) ^ 0x0a);
  1017. tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0xc0);
  1018. tda1004x_write_mask(state, TDA1004X_CONFC4, 1, 1);
  1019. break;
  1020. }
  1021. return 0;
  1022. }
  1023. static int tda1004x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  1024. {
  1025. struct tda1004x_state* state = fe->demodulator_priv;
  1026. if (enable) {
  1027. return tda1004x_enable_tuner_i2c(state);
  1028. } else {
  1029. return tda1004x_disable_tuner_i2c(state);
  1030. }
  1031. }
  1032. static int tda1004x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  1033. {
  1034. fesettings->min_delay_ms = 800;
  1035. /* Drift compensation makes no sense for DVB-T */
  1036. fesettings->step_size = 0;
  1037. fesettings->max_drift = 0;
  1038. return 0;
  1039. }
  1040. static void tda1004x_release(struct dvb_frontend* fe)
  1041. {
  1042. struct tda1004x_state *state = fe->demodulator_priv;
  1043. kfree(state);
  1044. }
  1045. static const struct dvb_frontend_ops tda10045_ops = {
  1046. .delsys = { SYS_DVBT },
  1047. .info = {
  1048. .name = "Philips TDA10045H DVB-T",
  1049. .frequency_min_hz = 51 * MHz,
  1050. .frequency_max_hz = 858 * MHz,
  1051. .frequency_stepsize_hz = 166667,
  1052. .caps =
  1053. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1054. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1055. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1056. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
  1057. },
  1058. .release = tda1004x_release,
  1059. .init = tda10045_init,
  1060. .sleep = tda1004x_sleep,
  1061. .write = tda1004x_write,
  1062. .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
  1063. .set_frontend = tda1004x_set_fe,
  1064. .get_frontend = tda1004x_get_fe,
  1065. .get_tune_settings = tda1004x_get_tune_settings,
  1066. .read_status = tda1004x_read_status,
  1067. .read_ber = tda1004x_read_ber,
  1068. .read_signal_strength = tda1004x_read_signal_strength,
  1069. .read_snr = tda1004x_read_snr,
  1070. .read_ucblocks = tda1004x_read_ucblocks,
  1071. };
  1072. struct dvb_frontend* tda10045_attach(const struct tda1004x_config* config,
  1073. struct i2c_adapter* i2c)
  1074. {
  1075. struct tda1004x_state *state;
  1076. int id;
  1077. /* allocate memory for the internal state */
  1078. state = kzalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
  1079. if (!state) {
  1080. printk(KERN_ERR "Can't allocate memory for tda10045 state\n");
  1081. return NULL;
  1082. }
  1083. /* setup the state */
  1084. state->config = config;
  1085. state->i2c = i2c;
  1086. state->demod_type = TDA1004X_DEMOD_TDA10045;
  1087. /* check if the demod is there */
  1088. id = tda1004x_read_byte(state, TDA1004X_CHIPID);
  1089. if (id < 0) {
  1090. printk(KERN_ERR "tda10045: chip is not answering. Giving up.\n");
  1091. kfree(state);
  1092. return NULL;
  1093. }
  1094. if (id != 0x25) {
  1095. printk(KERN_ERR "Invalid tda1004x ID = 0x%02x. Can't proceed\n", id);
  1096. kfree(state);
  1097. return NULL;
  1098. }
  1099. /* create dvb_frontend */
  1100. memcpy(&state->frontend.ops, &tda10045_ops, sizeof(struct dvb_frontend_ops));
  1101. state->frontend.demodulator_priv = state;
  1102. return &state->frontend;
  1103. }
  1104. static const struct dvb_frontend_ops tda10046_ops = {
  1105. .delsys = { SYS_DVBT },
  1106. .info = {
  1107. .name = "Philips TDA10046H DVB-T",
  1108. .frequency_min_hz = 51 * MHz,
  1109. .frequency_max_hz = 858 * MHz,
  1110. .frequency_stepsize_hz = 166667,
  1111. .caps =
  1112. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1113. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1114. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1115. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
  1116. },
  1117. .release = tda1004x_release,
  1118. .init = tda10046_init,
  1119. .sleep = tda1004x_sleep,
  1120. .write = tda1004x_write,
  1121. .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
  1122. .set_frontend = tda1004x_set_fe,
  1123. .get_frontend = tda1004x_get_fe,
  1124. .get_tune_settings = tda1004x_get_tune_settings,
  1125. .read_status = tda1004x_read_status,
  1126. .read_ber = tda1004x_read_ber,
  1127. .read_signal_strength = tda1004x_read_signal_strength,
  1128. .read_snr = tda1004x_read_snr,
  1129. .read_ucblocks = tda1004x_read_ucblocks,
  1130. };
  1131. struct dvb_frontend* tda10046_attach(const struct tda1004x_config* config,
  1132. struct i2c_adapter* i2c)
  1133. {
  1134. struct tda1004x_state *state;
  1135. int id;
  1136. /* allocate memory for the internal state */
  1137. state = kzalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
  1138. if (!state) {
  1139. printk(KERN_ERR "Can't allocate memory for tda10046 state\n");
  1140. return NULL;
  1141. }
  1142. /* setup the state */
  1143. state->config = config;
  1144. state->i2c = i2c;
  1145. state->demod_type = TDA1004X_DEMOD_TDA10046;
  1146. /* check if the demod is there */
  1147. id = tda1004x_read_byte(state, TDA1004X_CHIPID);
  1148. if (id < 0) {
  1149. printk(KERN_ERR "tda10046: chip is not answering. Giving up.\n");
  1150. kfree(state);
  1151. return NULL;
  1152. }
  1153. if (id != 0x46) {
  1154. printk(KERN_ERR "Invalid tda1004x ID = 0x%02x. Can't proceed\n", id);
  1155. kfree(state);
  1156. return NULL;
  1157. }
  1158. /* create dvb_frontend */
  1159. memcpy(&state->frontend.ops, &tda10046_ops, sizeof(struct dvb_frontend_ops));
  1160. state->frontend.demodulator_priv = state;
  1161. return &state->frontend;
  1162. }
  1163. module_param(debug, int, 0644);
  1164. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  1165. MODULE_DESCRIPTION("Philips TDA10045H & TDA10046H DVB-T Demodulator");
  1166. MODULE_AUTHOR("Andrew de Quincey & Robert Schlabbach");
  1167. MODULE_LICENSE("GPL");
  1168. EXPORT_SYMBOL(tda10045_attach);
  1169. EXPORT_SYMBOL(tda10046_attach);