tda1004x.c 36 KB

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