tda1004x.c 38 KB

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