tda1004x.c 38 KB

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