tda1004x.c 37 KB

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