tda1004x.c 38 KB

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