tda1004x.c 37 KB

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