tda1004x.c 35 KB

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