tda1004x.c 35 KB

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