tda1004x.c 39 KB

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