tda1004x.c 37 KB

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