tda1004x.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  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:
  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_GPL:
  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. }
  563. tda1004x_write_byteI(state, TDA1004X_CONFADC2, 0x38);
  564. tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0x61); // Turn both AGC outputs on
  565. tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MIN, 0); // }
  566. tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MAX, 0xff); // } AGC min/max values
  567. tda1004x_write_byteI(state, TDA10046H_AGC_IF_MIN, 0); // }
  568. tda1004x_write_byteI(state, TDA10046H_AGC_IF_MAX, 0xff); // }
  569. tda1004x_write_byteI(state, TDA10046H_AGC_GAINS, 0x12); // IF gain 2, TUN gain 1
  570. tda1004x_write_byteI(state, TDA10046H_CVBER_CTRL, 0x1a); // 10^6 VBER measurement bits
  571. tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 7); // MPEG2 interface config
  572. tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0xc0); // MPEG2 interface config
  573. // tda1004x_write_mask(state, 0x50, 0x80, 0x80); // handle out of guard echoes
  574. tda1004x_write_mask(state, 0x3a, 0x80, state->config->invert_oclk << 7);
  575. return 0;
  576. }
  577. static int tda1004x_set_fe(struct dvb_frontend* fe,
  578. struct dvb_frontend_parameters *fe_params)
  579. {
  580. struct tda1004x_state* state = fe->demodulator_priv;
  581. int tmp;
  582. int inversion;
  583. dprintk("%s\n", __FUNCTION__);
  584. if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
  585. // setup auto offset
  586. tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10);
  587. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x80, 0);
  588. tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0);
  589. // disable agc_conf[2]
  590. tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 0);
  591. }
  592. // set frequency
  593. if (fe->ops.tuner_ops.set_params) {
  594. fe->ops.tuner_ops.set_params(fe, fe_params);
  595. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  596. }
  597. // Hardcoded to use auto as much as possible on the TDA10045 as it
  598. // is very unreliable if AUTO mode is _not_ used.
  599. if (state->demod_type == TDA1004X_DEMOD_TDA10045) {
  600. fe_params->u.ofdm.code_rate_HP = FEC_AUTO;
  601. fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_AUTO;
  602. fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_AUTO;
  603. }
  604. // Set standard params.. or put them to auto
  605. if ((fe_params->u.ofdm.code_rate_HP == FEC_AUTO) ||
  606. (fe_params->u.ofdm.code_rate_LP == FEC_AUTO) ||
  607. (fe_params->u.ofdm.constellation == QAM_AUTO) ||
  608. (fe_params->u.ofdm.hierarchy_information == HIERARCHY_AUTO)) {
  609. tda1004x_write_mask(state, TDA1004X_AUTO, 1, 1); // enable auto
  610. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x03, 0); // turn off constellation bits
  611. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0); // turn off hierarchy bits
  612. tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x3f, 0); // turn off FEC bits
  613. } else {
  614. tda1004x_write_mask(state, TDA1004X_AUTO, 1, 0); // disable auto
  615. // set HP FEC
  616. tmp = tda1004x_encode_fec(fe_params->u.ofdm.code_rate_HP);
  617. if (tmp < 0)
  618. return tmp;
  619. tda1004x_write_mask(state, TDA1004X_IN_CONF2, 7, tmp);
  620. // set LP FEC
  621. tmp = tda1004x_encode_fec(fe_params->u.ofdm.code_rate_LP);
  622. if (tmp < 0)
  623. return tmp;
  624. tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x38, tmp << 3);
  625. // set constellation
  626. switch (fe_params->u.ofdm.constellation) {
  627. case QPSK:
  628. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 0);
  629. break;
  630. case QAM_16:
  631. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 1);
  632. break;
  633. case QAM_64:
  634. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 2);
  635. break;
  636. default:
  637. return -EINVAL;
  638. }
  639. // set hierarchy
  640. switch (fe_params->u.ofdm.hierarchy_information) {
  641. case HIERARCHY_NONE:
  642. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0 << 5);
  643. break;
  644. case HIERARCHY_1:
  645. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 1 << 5);
  646. break;
  647. case HIERARCHY_2:
  648. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 2 << 5);
  649. break;
  650. case HIERARCHY_4:
  651. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 3 << 5);
  652. break;
  653. default:
  654. return -EINVAL;
  655. }
  656. }
  657. // set bandwidth
  658. switch (state->demod_type) {
  659. case TDA1004X_DEMOD_TDA10045:
  660. tda10045h_set_bandwidth(state, fe_params->u.ofdm.bandwidth);
  661. break;
  662. case TDA1004X_DEMOD_TDA10046:
  663. tda10046h_set_bandwidth(state, fe_params->u.ofdm.bandwidth);
  664. break;
  665. }
  666. // set inversion
  667. inversion = fe_params->inversion;
  668. if (state->config->invert)
  669. inversion = inversion ? INVERSION_OFF : INVERSION_ON;
  670. switch (inversion) {
  671. case INVERSION_OFF:
  672. tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0);
  673. break;
  674. case INVERSION_ON:
  675. tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0x20);
  676. break;
  677. default:
  678. return -EINVAL;
  679. }
  680. // set guard interval
  681. switch (fe_params->u.ofdm.guard_interval) {
  682. case GUARD_INTERVAL_1_32:
  683. tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
  684. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
  685. break;
  686. case GUARD_INTERVAL_1_16:
  687. tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
  688. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 1 << 2);
  689. break;
  690. case GUARD_INTERVAL_1_8:
  691. tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
  692. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 2 << 2);
  693. break;
  694. case GUARD_INTERVAL_1_4:
  695. tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
  696. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 3 << 2);
  697. break;
  698. case GUARD_INTERVAL_AUTO:
  699. tda1004x_write_mask(state, TDA1004X_AUTO, 2, 2);
  700. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
  701. break;
  702. default:
  703. return -EINVAL;
  704. }
  705. // set transmission mode
  706. switch (fe_params->u.ofdm.transmission_mode) {
  707. case TRANSMISSION_MODE_2K:
  708. tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
  709. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0 << 4);
  710. break;
  711. case TRANSMISSION_MODE_8K:
  712. tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
  713. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 1 << 4);
  714. break;
  715. case TRANSMISSION_MODE_AUTO:
  716. tda1004x_write_mask(state, TDA1004X_AUTO, 4, 4);
  717. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0);
  718. break;
  719. default:
  720. return -EINVAL;
  721. }
  722. // start the lock
  723. switch (state->demod_type) {
  724. case TDA1004X_DEMOD_TDA10045:
  725. tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
  726. tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
  727. break;
  728. case TDA1004X_DEMOD_TDA10046:
  729. tda1004x_write_mask(state, TDA1004X_AUTO, 0x40, 0x40);
  730. msleep(1);
  731. tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 1);
  732. break;
  733. }
  734. msleep(10);
  735. return 0;
  736. }
  737. static int tda1004x_get_fe(struct dvb_frontend* fe, struct dvb_frontend_parameters *fe_params)
  738. {
  739. struct tda1004x_state* state = fe->demodulator_priv;
  740. dprintk("%s\n", __FUNCTION__);
  741. // inversion status
  742. fe_params->inversion = INVERSION_OFF;
  743. if (tda1004x_read_byte(state, TDA1004X_CONFC1) & 0x20)
  744. fe_params->inversion = INVERSION_ON;
  745. if (state->config->invert)
  746. fe_params->inversion = fe_params->inversion ? INVERSION_OFF : INVERSION_ON;
  747. // bandwidth
  748. switch (state->demod_type) {
  749. case TDA1004X_DEMOD_TDA10045:
  750. switch (tda1004x_read_byte(state, TDA10045H_WREF_LSB)) {
  751. case 0x14:
  752. fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
  753. break;
  754. case 0xdb:
  755. fe_params->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
  756. break;
  757. case 0x4f:
  758. fe_params->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
  759. break;
  760. }
  761. break;
  762. case TDA1004X_DEMOD_TDA10046:
  763. switch (tda1004x_read_byte(state, TDA10046H_TIME_WREF1)) {
  764. case 0x5c:
  765. case 0x54:
  766. fe_params->u.ofdm.bandwidth = BANDWIDTH_8_MHZ;
  767. break;
  768. case 0x6a:
  769. case 0x60:
  770. fe_params->u.ofdm.bandwidth = BANDWIDTH_7_MHZ;
  771. break;
  772. case 0x7b:
  773. case 0x70:
  774. fe_params->u.ofdm.bandwidth = BANDWIDTH_6_MHZ;
  775. break;
  776. }
  777. break;
  778. }
  779. // FEC
  780. fe_params->u.ofdm.code_rate_HP =
  781. tda1004x_decode_fec(tda1004x_read_byte(state, TDA1004X_OUT_CONF2) & 7);
  782. fe_params->u.ofdm.code_rate_LP =
  783. tda1004x_decode_fec((tda1004x_read_byte(state, TDA1004X_OUT_CONF2) >> 3) & 7);
  784. // constellation
  785. switch (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 3) {
  786. case 0:
  787. fe_params->u.ofdm.constellation = QPSK;
  788. break;
  789. case 1:
  790. fe_params->u.ofdm.constellation = QAM_16;
  791. break;
  792. case 2:
  793. fe_params->u.ofdm.constellation = QAM_64;
  794. break;
  795. }
  796. // transmission mode
  797. fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_2K;
  798. if (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x10)
  799. fe_params->u.ofdm.transmission_mode = TRANSMISSION_MODE_8K;
  800. // guard interval
  801. switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x0c) >> 2) {
  802. case 0:
  803. fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_32;
  804. break;
  805. case 1:
  806. fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_16;
  807. break;
  808. case 2:
  809. fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_8;
  810. break;
  811. case 3:
  812. fe_params->u.ofdm.guard_interval = GUARD_INTERVAL_1_4;
  813. break;
  814. }
  815. // hierarchy
  816. switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x60) >> 5) {
  817. case 0:
  818. fe_params->u.ofdm.hierarchy_information = HIERARCHY_NONE;
  819. break;
  820. case 1:
  821. fe_params->u.ofdm.hierarchy_information = HIERARCHY_1;
  822. break;
  823. case 2:
  824. fe_params->u.ofdm.hierarchy_information = HIERARCHY_2;
  825. break;
  826. case 3:
  827. fe_params->u.ofdm.hierarchy_information = HIERARCHY_4;
  828. break;
  829. }
  830. return 0;
  831. }
  832. static int tda1004x_read_status(struct dvb_frontend* fe, fe_status_t * fe_status)
  833. {
  834. struct tda1004x_state* state = fe->demodulator_priv;
  835. int status;
  836. int cber;
  837. int vber;
  838. dprintk("%s\n", __FUNCTION__);
  839. // read status
  840. status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
  841. if (status == -1)
  842. return -EIO;
  843. // decode
  844. *fe_status = 0;
  845. if (status & 4)
  846. *fe_status |= FE_HAS_SIGNAL;
  847. if (status & 2)
  848. *fe_status |= FE_HAS_CARRIER;
  849. if (status & 8)
  850. *fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
  851. // if we don't already have VITERBI (i.e. not LOCKED), see if the viterbi
  852. // is getting anything valid
  853. if (!(*fe_status & FE_HAS_VITERBI)) {
  854. // read the CBER
  855. cber = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
  856. if (cber == -1)
  857. return -EIO;
  858. status = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
  859. if (status == -1)
  860. return -EIO;
  861. cber |= (status << 8);
  862. // The address 0x20 should be read to cope with a TDA10046 bug
  863. tda1004x_read_byte(state, TDA1004X_CBER_RESET);
  864. if (cber != 65535)
  865. *fe_status |= FE_HAS_VITERBI;
  866. }
  867. // if we DO have some valid VITERBI output, but don't already have SYNC
  868. // bytes (i.e. not LOCKED), see if the RS decoder is getting anything valid.
  869. if ((*fe_status & FE_HAS_VITERBI) && (!(*fe_status & FE_HAS_SYNC))) {
  870. // read the VBER
  871. vber = tda1004x_read_byte(state, TDA1004X_VBER_LSB);
  872. if (vber == -1)
  873. return -EIO;
  874. status = tda1004x_read_byte(state, TDA1004X_VBER_MID);
  875. if (status == -1)
  876. return -EIO;
  877. vber |= (status << 8);
  878. status = tda1004x_read_byte(state, TDA1004X_VBER_MSB);
  879. if (status == -1)
  880. return -EIO;
  881. vber |= (status & 0x0f) << 16;
  882. // The CVBER_LUT should be read to cope with TDA10046 hardware bug
  883. tda1004x_read_byte(state, TDA1004X_CVBER_LUT);
  884. // if RS has passed some valid TS packets, then we must be
  885. // getting some SYNC bytes
  886. if (vber < 16632)
  887. *fe_status |= FE_HAS_SYNC;
  888. }
  889. // success
  890. dprintk("%s: fe_status=0x%x\n", __FUNCTION__, *fe_status);
  891. return 0;
  892. }
  893. static int tda1004x_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
  894. {
  895. struct tda1004x_state* state = fe->demodulator_priv;
  896. int tmp;
  897. int reg = 0;
  898. dprintk("%s\n", __FUNCTION__);
  899. // determine the register to use
  900. switch (state->demod_type) {
  901. case TDA1004X_DEMOD_TDA10045:
  902. reg = TDA10045H_S_AGC;
  903. break;
  904. case TDA1004X_DEMOD_TDA10046:
  905. reg = TDA10046H_AGC_IF_LEVEL;
  906. break;
  907. }
  908. // read it
  909. tmp = tda1004x_read_byte(state, reg);
  910. if (tmp < 0)
  911. return -EIO;
  912. *signal = (tmp << 8) | tmp;
  913. dprintk("%s: signal=0x%x\n", __FUNCTION__, *signal);
  914. return 0;
  915. }
  916. static int tda1004x_read_snr(struct dvb_frontend* fe, u16 * snr)
  917. {
  918. struct tda1004x_state* state = fe->demodulator_priv;
  919. int tmp;
  920. dprintk("%s\n", __FUNCTION__);
  921. // read it
  922. tmp = tda1004x_read_byte(state, TDA1004X_SNR);
  923. if (tmp < 0)
  924. return -EIO;
  925. tmp = 255 - tmp;
  926. *snr = ((tmp << 8) | tmp);
  927. dprintk("%s: snr=0x%x\n", __FUNCTION__, *snr);
  928. return 0;
  929. }
  930. static int tda1004x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  931. {
  932. struct tda1004x_state* state = fe->demodulator_priv;
  933. int tmp;
  934. int tmp2;
  935. int counter;
  936. dprintk("%s\n", __FUNCTION__);
  937. // read the UCBLOCKS and reset
  938. counter = 0;
  939. tmp = tda1004x_read_byte(state, TDA1004X_UNCOR);
  940. if (tmp < 0)
  941. return -EIO;
  942. tmp &= 0x7f;
  943. while (counter++ < 5) {
  944. tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
  945. tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
  946. tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
  947. tmp2 = tda1004x_read_byte(state, TDA1004X_UNCOR);
  948. if (tmp2 < 0)
  949. return -EIO;
  950. tmp2 &= 0x7f;
  951. if ((tmp2 < tmp) || (tmp2 == 0))
  952. break;
  953. }
  954. if (tmp != 0x7f)
  955. *ucblocks = tmp;
  956. else
  957. *ucblocks = 0xffffffff;
  958. dprintk("%s: ucblocks=0x%x\n", __FUNCTION__, *ucblocks);
  959. return 0;
  960. }
  961. static int tda1004x_read_ber(struct dvb_frontend* fe, u32* ber)
  962. {
  963. struct tda1004x_state* state = fe->demodulator_priv;
  964. int tmp;
  965. dprintk("%s\n", __FUNCTION__);
  966. // read it in
  967. tmp = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
  968. if (tmp < 0)
  969. return -EIO;
  970. *ber = tmp << 1;
  971. tmp = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
  972. if (tmp < 0)
  973. return -EIO;
  974. *ber |= (tmp << 9);
  975. // The address 0x20 should be read to cope with a TDA10046 bug
  976. tda1004x_read_byte(state, TDA1004X_CBER_RESET);
  977. dprintk("%s: ber=0x%x\n", __FUNCTION__, *ber);
  978. return 0;
  979. }
  980. static int tda1004x_sleep(struct dvb_frontend* fe)
  981. {
  982. struct tda1004x_state* state = fe->demodulator_priv;
  983. switch (state->demod_type) {
  984. case TDA1004X_DEMOD_TDA10045:
  985. tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0x10);
  986. break;
  987. case TDA1004X_DEMOD_TDA10046:
  988. /* set outputs to tristate */
  989. tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0xff);
  990. tda1004x_write_mask(state, TDA1004X_CONFC4, 1, 1);
  991. break;
  992. }
  993. return 0;
  994. }
  995. static int tda1004x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  996. {
  997. struct tda1004x_state* state = fe->demodulator_priv;
  998. if (enable) {
  999. return tda1004x_enable_tuner_i2c(state);
  1000. } else {
  1001. return tda1004x_disable_tuner_i2c(state);
  1002. }
  1003. }
  1004. static int tda1004x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  1005. {
  1006. fesettings->min_delay_ms = 800;
  1007. /* Drift compensation makes no sense for DVB-T */
  1008. fesettings->step_size = 0;
  1009. fesettings->max_drift = 0;
  1010. return 0;
  1011. }
  1012. static void tda1004x_release(struct dvb_frontend* fe)
  1013. {
  1014. struct tda1004x_state *state = fe->demodulator_priv;
  1015. kfree(state);
  1016. }
  1017. static struct dvb_frontend_ops tda10045_ops = {
  1018. .info = {
  1019. .name = "Philips TDA10045H DVB-T",
  1020. .type = FE_OFDM,
  1021. .frequency_min = 51000000,
  1022. .frequency_max = 858000000,
  1023. .frequency_stepsize = 166667,
  1024. .caps =
  1025. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1026. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1027. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1028. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
  1029. },
  1030. .release = tda1004x_release,
  1031. .init = tda10045_init,
  1032. .sleep = tda1004x_sleep,
  1033. .write = tda1004x_write,
  1034. .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
  1035. .set_frontend = tda1004x_set_fe,
  1036. .get_frontend = tda1004x_get_fe,
  1037. .get_tune_settings = tda1004x_get_tune_settings,
  1038. .read_status = tda1004x_read_status,
  1039. .read_ber = tda1004x_read_ber,
  1040. .read_signal_strength = tda1004x_read_signal_strength,
  1041. .read_snr = tda1004x_read_snr,
  1042. .read_ucblocks = tda1004x_read_ucblocks,
  1043. };
  1044. struct dvb_frontend* tda10045_attach(const struct tda1004x_config* config,
  1045. struct i2c_adapter* i2c)
  1046. {
  1047. struct tda1004x_state *state;
  1048. /* allocate memory for the internal state */
  1049. state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
  1050. if (!state)
  1051. return NULL;
  1052. /* setup the state */
  1053. state->config = config;
  1054. state->i2c = i2c;
  1055. state->demod_type = TDA1004X_DEMOD_TDA10045;
  1056. /* check if the demod is there */
  1057. if (tda1004x_read_byte(state, TDA1004X_CHIPID) != 0x25) {
  1058. kfree(state);
  1059. return NULL;
  1060. }
  1061. /* create dvb_frontend */
  1062. memcpy(&state->frontend.ops, &tda10045_ops, sizeof(struct dvb_frontend_ops));
  1063. state->frontend.demodulator_priv = state;
  1064. return &state->frontend;
  1065. }
  1066. static struct dvb_frontend_ops tda10046_ops = {
  1067. .info = {
  1068. .name = "Philips TDA10046H DVB-T",
  1069. .type = FE_OFDM,
  1070. .frequency_min = 51000000,
  1071. .frequency_max = 858000000,
  1072. .frequency_stepsize = 166667,
  1073. .caps =
  1074. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1075. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1076. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1077. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
  1078. },
  1079. .release = tda1004x_release,
  1080. .init = tda10046_init,
  1081. .sleep = tda1004x_sleep,
  1082. .write = tda1004x_write,
  1083. .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
  1084. .set_frontend = tda1004x_set_fe,
  1085. .get_frontend = tda1004x_get_fe,
  1086. .get_tune_settings = tda1004x_get_tune_settings,
  1087. .read_status = tda1004x_read_status,
  1088. .read_ber = tda1004x_read_ber,
  1089. .read_signal_strength = tda1004x_read_signal_strength,
  1090. .read_snr = tda1004x_read_snr,
  1091. .read_ucblocks = tda1004x_read_ucblocks,
  1092. };
  1093. struct dvb_frontend* tda10046_attach(const struct tda1004x_config* config,
  1094. struct i2c_adapter* i2c)
  1095. {
  1096. struct tda1004x_state *state;
  1097. /* allocate memory for the internal state */
  1098. state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
  1099. if (!state)
  1100. return NULL;
  1101. /* setup the state */
  1102. state->config = config;
  1103. state->i2c = i2c;
  1104. state->demod_type = TDA1004X_DEMOD_TDA10046;
  1105. /* check if the demod is there */
  1106. if (tda1004x_read_byte(state, TDA1004X_CHIPID) != 0x46) {
  1107. kfree(state);
  1108. return NULL;
  1109. }
  1110. /* create dvb_frontend */
  1111. memcpy(&state->frontend.ops, &tda10046_ops, sizeof(struct dvb_frontend_ops));
  1112. state->frontend.demodulator_priv = state;
  1113. return &state->frontend;
  1114. }
  1115. module_param(debug, int, 0644);
  1116. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  1117. MODULE_DESCRIPTION("Philips TDA10045H & TDA10046H DVB-T Demodulator");
  1118. MODULE_AUTHOR("Andrew de Quincey & Robert Schlabbach");
  1119. MODULE_LICENSE("GPL");
  1120. EXPORT_SYMBOL(tda10045_attach);
  1121. EXPORT_SYMBOL(tda10046_attach);