tda1004x.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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. static int debug;
  35. #define dprintk(args...) \
  36. do { \
  37. if (debug) printk(KERN_DEBUG "tda1004x: " args); \
  38. } while (0)
  39. #define TDA1004X_CHIPID 0x00
  40. #define TDA1004X_AUTO 0x01
  41. #define TDA1004X_IN_CONF1 0x02
  42. #define TDA1004X_IN_CONF2 0x03
  43. #define TDA1004X_OUT_CONF1 0x04
  44. #define TDA1004X_OUT_CONF2 0x05
  45. #define TDA1004X_STATUS_CD 0x06
  46. #define TDA1004X_CONFC4 0x07
  47. #define TDA1004X_DSSPARE2 0x0C
  48. #define TDA10045H_CODE_IN 0x0D
  49. #define TDA10045H_FWPAGE 0x0E
  50. #define TDA1004X_SCAN_CPT 0x10
  51. #define TDA1004X_DSP_CMD 0x11
  52. #define TDA1004X_DSP_ARG 0x12
  53. #define TDA1004X_DSP_DATA1 0x13
  54. #define TDA1004X_DSP_DATA2 0x14
  55. #define TDA1004X_CONFADC1 0x15
  56. #define TDA1004X_CONFC1 0x16
  57. #define TDA10045H_S_AGC 0x1a
  58. #define TDA10046H_AGC_TUN_LEVEL 0x1a
  59. #define TDA1004X_SNR 0x1c
  60. #define TDA1004X_CONF_TS1 0x1e
  61. #define TDA1004X_CONF_TS2 0x1f
  62. #define TDA1004X_CBER_RESET 0x20
  63. #define TDA1004X_CBER_MSB 0x21
  64. #define TDA1004X_CBER_LSB 0x22
  65. #define TDA1004X_CVBER_LUT 0x23
  66. #define TDA1004X_VBER_MSB 0x24
  67. #define TDA1004X_VBER_MID 0x25
  68. #define TDA1004X_VBER_LSB 0x26
  69. #define TDA1004X_UNCOR 0x27
  70. #define TDA10045H_CONFPLL_P 0x2D
  71. #define TDA10045H_CONFPLL_M_MSB 0x2E
  72. #define TDA10045H_CONFPLL_M_LSB 0x2F
  73. #define TDA10045H_CONFPLL_N 0x30
  74. #define TDA10046H_CONFPLL1 0x2D
  75. #define TDA10046H_CONFPLL2 0x2F
  76. #define TDA10046H_CONFPLL3 0x30
  77. #define TDA10046H_TIME_WREF1 0x31
  78. #define TDA10046H_TIME_WREF2 0x32
  79. #define TDA10046H_TIME_WREF3 0x33
  80. #define TDA10046H_TIME_WREF4 0x34
  81. #define TDA10046H_TIME_WREF5 0x35
  82. #define TDA10045H_UNSURW_MSB 0x31
  83. #define TDA10045H_UNSURW_LSB 0x32
  84. #define TDA10045H_WREF_MSB 0x33
  85. #define TDA10045H_WREF_MID 0x34
  86. #define TDA10045H_WREF_LSB 0x35
  87. #define TDA10045H_MUXOUT 0x36
  88. #define TDA1004X_CONFADC2 0x37
  89. #define TDA10045H_IOFFSET 0x38
  90. #define TDA10046H_CONF_TRISTATE1 0x3B
  91. #define TDA10046H_CONF_TRISTATE2 0x3C
  92. #define TDA10046H_CONF_POLARITY 0x3D
  93. #define TDA10046H_FREQ_OFFSET 0x3E
  94. #define TDA10046H_GPIO_OUT_SEL 0x41
  95. #define TDA10046H_GPIO_SELECT 0x42
  96. #define TDA10046H_AGC_CONF 0x43
  97. #define TDA10046H_AGC_THR 0x44
  98. #define TDA10046H_AGC_RENORM 0x45
  99. #define TDA10046H_AGC_GAINS 0x46
  100. #define TDA10046H_AGC_TUN_MIN 0x47
  101. #define TDA10046H_AGC_TUN_MAX 0x48
  102. #define TDA10046H_AGC_IF_MIN 0x49
  103. #define TDA10046H_AGC_IF_MAX 0x4A
  104. #define TDA10046H_FREQ_PHY2_MSB 0x4D
  105. #define TDA10046H_FREQ_PHY2_LSB 0x4E
  106. #define TDA10046H_CVBER_CTRL 0x4F
  107. #define TDA10046H_AGC_IF_LEVEL 0x52
  108. #define TDA10046H_CODE_CPT 0x57
  109. #define TDA10046H_CODE_IN 0x58
  110. static int tda1004x_write_byteI(struct tda1004x_state *state, int reg, int data)
  111. {
  112. int ret;
  113. u8 buf[] = { reg, data };
  114. struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
  115. dprintk("%s: reg=0x%x, data=0x%x\n", __FUNCTION__, reg, data);
  116. msg.addr = state->config->demod_address;
  117. ret = i2c_transfer(state->i2c, &msg, 1);
  118. if (ret != 1)
  119. dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
  120. __FUNCTION__, reg, data, ret);
  121. dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __FUNCTION__,
  122. reg, data, ret);
  123. return (ret != 1) ? -1 : 0;
  124. }
  125. static int tda1004x_read_byte(struct tda1004x_state *state, int reg)
  126. {
  127. int ret;
  128. u8 b0[] = { reg };
  129. u8 b1[] = { 0 };
  130. struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
  131. { .flags = I2C_M_RD, .buf = b1, .len = 1 }};
  132. dprintk("%s: reg=0x%x\n", __FUNCTION__, reg);
  133. msg[0].addr = state->config->demod_address;
  134. msg[1].addr = state->config->demod_address;
  135. ret = i2c_transfer(state->i2c, msg, 2);
  136. if (ret != 2) {
  137. dprintk("%s: error reg=0x%x, ret=%i\n", __FUNCTION__, reg,
  138. ret);
  139. return -1;
  140. }
  141. dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __FUNCTION__,
  142. reg, b1[0], ret);
  143. return b1[0];
  144. }
  145. static int tda1004x_write_mask(struct tda1004x_state *state, int reg, int mask, int data)
  146. {
  147. int val;
  148. dprintk("%s: reg=0x%x, mask=0x%x, data=0x%x\n", __FUNCTION__, reg,
  149. mask, data);
  150. // read a byte and check
  151. val = tda1004x_read_byte(state, reg);
  152. if (val < 0)
  153. return val;
  154. // mask if off
  155. val = val & ~mask;
  156. val |= data & 0xff;
  157. // write it out again
  158. return tda1004x_write_byteI(state, reg, val);
  159. }
  160. static int tda1004x_write_buf(struct tda1004x_state *state, int reg, unsigned char *buf, int len)
  161. {
  162. int i;
  163. int result;
  164. dprintk("%s: reg=0x%x, len=0x%x\n", __FUNCTION__, reg, len);
  165. result = 0;
  166. for (i = 0; i < len; i++) {
  167. result = tda1004x_write_byteI(state, reg + i, buf[i]);
  168. if (result != 0)
  169. break;
  170. }
  171. return result;
  172. }
  173. static int tda1004x_enable_tuner_i2c(struct tda1004x_state *state)
  174. {
  175. int result;
  176. dprintk("%s\n", __FUNCTION__);
  177. result = tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 2);
  178. msleep(20);
  179. return result;
  180. }
  181. static int tda1004x_disable_tuner_i2c(struct tda1004x_state *state)
  182. {
  183. dprintk("%s\n", __FUNCTION__);
  184. return tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 0);
  185. }
  186. static int tda10045h_set_bandwidth(struct tda1004x_state *state,
  187. fe_bandwidth_t bandwidth)
  188. {
  189. static u8 bandwidth_6mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x60, 0x1e, 0xa7, 0x45, 0x4f };
  190. static u8 bandwidth_7mhz[] = { 0x02, 0x00, 0x37, 0x00, 0x4a, 0x2f, 0x6d, 0x76, 0xdb };
  191. static u8 bandwidth_8mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x48, 0x17, 0x89, 0xc7, 0x14 };
  192. switch (bandwidth) {
  193. case BANDWIDTH_6_MHZ:
  194. tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_6mhz, sizeof(bandwidth_6mhz));
  195. break;
  196. case BANDWIDTH_7_MHZ:
  197. tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_7mhz, sizeof(bandwidth_7mhz));
  198. break;
  199. case BANDWIDTH_8_MHZ:
  200. tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_8mhz, sizeof(bandwidth_8mhz));
  201. break;
  202. default:
  203. return -EINVAL;
  204. }
  205. tda1004x_write_byteI(state, TDA10045H_IOFFSET, 0);
  206. return 0;
  207. }
  208. static int tda10046h_set_bandwidth(struct tda1004x_state *state,
  209. fe_bandwidth_t bandwidth)
  210. {
  211. static u8 bandwidth_6mhz_53M[] = { 0x7b, 0x2e, 0x11, 0xf0, 0xd2 };
  212. static u8 bandwidth_7mhz_53M[] = { 0x6a, 0x02, 0x6a, 0x43, 0x9f };
  213. static u8 bandwidth_8mhz_53M[] = { 0x5c, 0x32, 0xc2, 0x96, 0x6d };
  214. static u8 bandwidth_6mhz_48M[] = { 0x70, 0x02, 0x49, 0x24, 0x92 };
  215. static u8 bandwidth_7mhz_48M[] = { 0x60, 0x02, 0xaa, 0xaa, 0xab };
  216. static u8 bandwidth_8mhz_48M[] = { 0x54, 0x03, 0x0c, 0x30, 0xc3 };
  217. int tda10046_clk53m;
  218. if ((state->config->if_freq == TDA10046_FREQ_045) ||
  219. (state->config->if_freq == TDA10046_FREQ_052))
  220. tda10046_clk53m = 0;
  221. else
  222. tda10046_clk53m = 1;
  223. switch (bandwidth) {
  224. case BANDWIDTH_6_MHZ:
  225. if (tda10046_clk53m)
  226. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_53M,
  227. sizeof(bandwidth_6mhz_53M));
  228. else
  229. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_48M,
  230. sizeof(bandwidth_6mhz_48M));
  231. if (state->config->if_freq == TDA10046_FREQ_045) {
  232. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0a);
  233. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xab);
  234. }
  235. break;
  236. case BANDWIDTH_7_MHZ:
  237. if (tda10046_clk53m)
  238. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_53M,
  239. sizeof(bandwidth_7mhz_53M));
  240. else
  241. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_48M,
  242. sizeof(bandwidth_7mhz_48M));
  243. if (state->config->if_freq == TDA10046_FREQ_045) {
  244. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
  245. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
  246. }
  247. break;
  248. case BANDWIDTH_8_MHZ:
  249. if (tda10046_clk53m)
  250. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_53M,
  251. sizeof(bandwidth_8mhz_53M));
  252. else
  253. tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_48M,
  254. sizeof(bandwidth_8mhz_48M));
  255. if (state->config->if_freq == TDA10046_FREQ_045) {
  256. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
  257. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x55);
  258. }
  259. break;
  260. default:
  261. return -EINVAL;
  262. }
  263. return 0;
  264. }
  265. static int tda1004x_do_upload(struct tda1004x_state *state,
  266. unsigned char *mem, unsigned int len,
  267. u8 dspCodeCounterReg, u8 dspCodeInReg)
  268. {
  269. u8 buf[65];
  270. struct i2c_msg fw_msg = { .flags = 0, .buf = buf, .len = 0 };
  271. int tx_size;
  272. int pos = 0;
  273. /* clear code counter */
  274. tda1004x_write_byteI(state, dspCodeCounterReg, 0);
  275. fw_msg.addr = state->config->demod_address;
  276. buf[0] = dspCodeInReg;
  277. while (pos != len) {
  278. // work out how much to send this time
  279. tx_size = len - pos;
  280. if (tx_size > 0x10)
  281. tx_size = 0x10;
  282. // send the chunk
  283. memcpy(buf + 1, mem + pos, tx_size);
  284. fw_msg.len = tx_size + 1;
  285. if (i2c_transfer(state->i2c, &fw_msg, 1) != 1) {
  286. printk(KERN_ERR "tda1004x: Error during firmware upload\n");
  287. return -EIO;
  288. }
  289. pos += tx_size;
  290. dprintk("%s: fw_pos=0x%x\n", __FUNCTION__, pos);
  291. }
  292. // give the DSP a chance to settle 03/10/05 Hac
  293. msleep(100);
  294. return 0;
  295. }
  296. static int tda1004x_check_upload_ok(struct tda1004x_state *state)
  297. {
  298. u8 data1, data2;
  299. unsigned long timeout;
  300. if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
  301. timeout = jiffies + 2 * HZ;
  302. while(!(tda1004x_read_byte(state, TDA1004X_STATUS_CD) & 0x20)) {
  303. if (time_after(jiffies, timeout)) {
  304. printk(KERN_ERR "tda1004x: timeout waiting for DSP ready\n");
  305. break;
  306. }
  307. msleep(1);
  308. }
  309. } else
  310. msleep(100);
  311. // check upload was OK
  312. tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0); // we want to read from the DSP
  313. tda1004x_write_byteI(state, TDA1004X_DSP_CMD, 0x67);
  314. data1 = tda1004x_read_byte(state, TDA1004X_DSP_DATA1);
  315. data2 = tda1004x_read_byte(state, TDA1004X_DSP_DATA2);
  316. if (data1 != 0x67 || data2 < 0x20 || data2 > 0x2e) {
  317. printk(KERN_INFO "tda1004x: found firmware revision %x -- invalid\n", data2);
  318. return -EIO;
  319. }
  320. printk(KERN_INFO "tda1004x: found firmware revision %x -- ok\n", data2);
  321. return 0;
  322. }
  323. static int tda10045_fwupload(struct dvb_frontend* fe)
  324. {
  325. struct tda1004x_state* state = fe->demodulator_priv;
  326. int ret;
  327. const struct firmware *fw;
  328. /* don't re-upload unless necessary */
  329. if (tda1004x_check_upload_ok(state) == 0)
  330. return 0;
  331. /* request the firmware, this will block until someone uploads it */
  332. printk(KERN_INFO "tda1004x: waiting for firmware upload (%s)...\n", TDA10045_DEFAULT_FIRMWARE);
  333. ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
  334. if (ret) {
  335. printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
  336. return ret;
  337. }
  338. /* reset chip */
  339. tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0);
  340. tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
  341. tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
  342. msleep(10);
  343. /* set parameters */
  344. tda10045h_set_bandwidth(state, BANDWIDTH_8_MHZ);
  345. ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10045H_FWPAGE, TDA10045H_CODE_IN);
  346. release_firmware(fw);
  347. if (ret)
  348. return ret;
  349. printk(KERN_INFO "tda1004x: firmware upload complete\n");
  350. /* wait for DSP to initialise */
  351. /* DSPREADY doesn't seem to work on the TDA10045H */
  352. msleep(100);
  353. return tda1004x_check_upload_ok(state);
  354. }
  355. static void tda10046_init_plls(struct dvb_frontend* fe)
  356. {
  357. struct tda1004x_state* state = fe->demodulator_priv;
  358. int tda10046_clk53m;
  359. if ((state->config->if_freq == TDA10046_FREQ_045) ||
  360. (state->config->if_freq == TDA10046_FREQ_052))
  361. tda10046_clk53m = 0;
  362. else
  363. tda10046_clk53m = 1;
  364. tda1004x_write_byteI(state, TDA10046H_CONFPLL1, 0xf0);
  365. if(tda10046_clk53m) {
  366. printk(KERN_INFO "tda1004x: setting up plls for 53MHz sampling clock\n");
  367. tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x08); // PLL M = 8
  368. } else {
  369. printk(KERN_INFO "tda1004x: setting up plls for 48MHz sampling clock\n");
  370. tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x03); // PLL M = 3
  371. }
  372. if (state->config->xtal_freq == TDA10046_XTAL_4M ) {
  373. dprintk("%s: setting up PLLs for a 4 MHz Xtal\n", __FUNCTION__);
  374. tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 0); // PLL P = N = 0
  375. } else {
  376. dprintk("%s: setting up PLLs for a 16 MHz Xtal\n", __FUNCTION__);
  377. tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 3); // PLL P = 0, N = 3
  378. }
  379. if(tda10046_clk53m)
  380. tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x67);
  381. else
  382. tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x72);
  383. /* Note clock frequency is handled implicitly */
  384. switch (state->config->if_freq) {
  385. case TDA10046_FREQ_045:
  386. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
  387. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
  388. break;
  389. case TDA10046_FREQ_052:
  390. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
  391. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xc7);
  392. break;
  393. case TDA10046_FREQ_3617:
  394. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
  395. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x59);
  396. break;
  397. case TDA10046_FREQ_3613:
  398. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
  399. tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x3f);
  400. break;
  401. }
  402. tda10046h_set_bandwidth(state, BANDWIDTH_8_MHZ); // default bandwidth 8 MHz
  403. /* let the PLLs settle */
  404. msleep(120);
  405. }
  406. static int tda10046_fwupload(struct dvb_frontend* fe)
  407. {
  408. struct tda1004x_state* state = fe->demodulator_priv;
  409. int ret;
  410. const struct firmware *fw;
  411. /* reset + wake up chip */
  412. if (state->config->xtal_freq == TDA10046_XTAL_4M) {
  413. tda1004x_write_byteI(state, TDA1004X_CONFC4, 0);
  414. } else {
  415. dprintk("%s: 16MHz Xtal, reducing I2C speed\n", __FUNCTION__);
  416. tda1004x_write_byteI(state, TDA1004X_CONFC4, 0x80);
  417. }
  418. tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 1, 0);
  419. /* set GPIO 1 and 3 */
  420. if (state->config->gpio_config != TDA10046_GPTRI) {
  421. tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE2, 0x33);
  422. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f, state->config->gpio_config &0x0f);
  423. }
  424. /* let the clocks recover from sleep */
  425. msleep(10);
  426. /* The PLLs need to be reprogrammed after sleep */
  427. tda10046_init_plls(fe);
  428. tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0);
  429. /* don't re-upload unless necessary */
  430. if (tda1004x_check_upload_ok(state) == 0)
  431. return 0;
  432. printk(KERN_INFO "tda1004x: trying to boot from eeprom\n");
  433. tda1004x_write_mask(state, TDA1004X_CONFC4, 4, 4);
  434. msleep(300);
  435. /* don't re-upload unless necessary */
  436. if (tda1004x_check_upload_ok(state) == 0)
  437. return 0;
  438. if (state->config->request_firmware != NULL) {
  439. /* request the firmware, this will block until someone uploads it */
  440. printk(KERN_INFO "tda1004x: waiting for firmware upload...\n");
  441. ret = state->config->request_firmware(fe, &fw, TDA10046_DEFAULT_FIRMWARE);
  442. if (ret) {
  443. /* remain compatible to old bug: try to load with tda10045 image name */
  444. ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
  445. if (ret) {
  446. printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
  447. return ret;
  448. } else {
  449. printk(KERN_INFO "tda1004x: please rename the firmware file to %s\n",
  450. TDA10046_DEFAULT_FIRMWARE);
  451. }
  452. }
  453. } else {
  454. printk(KERN_ERR "tda1004x: no request function defined, can't upload from file\n");
  455. return -EIO;
  456. }
  457. tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8); // going to boot from HOST
  458. ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10046H_CODE_CPT, TDA10046H_CODE_IN);
  459. release_firmware(fw);
  460. return tda1004x_check_upload_ok(state);
  461. }
  462. static int tda1004x_encode_fec(int fec)
  463. {
  464. // convert known FEC values
  465. switch (fec) {
  466. case FEC_1_2:
  467. return 0;
  468. case FEC_2_3:
  469. return 1;
  470. case FEC_3_4:
  471. return 2;
  472. case FEC_5_6:
  473. return 3;
  474. case FEC_7_8:
  475. return 4;
  476. }
  477. // unsupported
  478. return -EINVAL;
  479. }
  480. static int tda1004x_decode_fec(int tdafec)
  481. {
  482. // convert known FEC values
  483. switch (tdafec) {
  484. case 0:
  485. return FEC_1_2;
  486. case 1:
  487. return FEC_2_3;
  488. case 2:
  489. return FEC_3_4;
  490. case 3:
  491. return FEC_5_6;
  492. case 4:
  493. return FEC_7_8;
  494. }
  495. // unsupported
  496. return -1;
  497. }
  498. static int tda1004x_write(struct dvb_frontend* fe, u8 *buf, int len)
  499. {
  500. struct tda1004x_state* state = fe->demodulator_priv;
  501. if (len != 2)
  502. return -EINVAL;
  503. return tda1004x_write_byteI(state, buf[0], buf[1]);
  504. }
  505. static int tda10045_init(struct dvb_frontend* fe)
  506. {
  507. struct tda1004x_state* state = fe->demodulator_priv;
  508. dprintk("%s\n", __FUNCTION__);
  509. if (tda10045_fwupload(fe)) {
  510. printk("tda1004x: firmware upload failed\n");
  511. return -EIO;
  512. }
  513. tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0); // wake up the ADC
  514. // tda setup
  515. tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
  516. tda1004x_write_mask(state, TDA1004X_AUTO, 8, 0); // select HP stream
  517. tda1004x_write_mask(state, TDA1004X_CONFC1, 0x40, 0); // set polarity of VAGC signal
  518. tda1004x_write_mask(state, TDA1004X_CONFC1, 0x80, 0x80); // enable pulse killer
  519. tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10); // enable auto offset
  520. tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0x0); // no frequency offset
  521. tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 0); // setup MPEG2 TS interface
  522. tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0); // setup MPEG2 TS interface
  523. tda1004x_write_mask(state, TDA1004X_VBER_MSB, 0xe0, 0xa0); // 10^6 VBER measurement bits
  524. tda1004x_write_mask(state, TDA1004X_CONFC1, 0x10, 0); // VAGC polarity
  525. tda1004x_write_byteI(state, TDA1004X_CONFADC1, 0x2e);
  526. tda1004x_write_mask(state, 0x1f, 0x01, state->config->invert_oclk);
  527. return 0;
  528. }
  529. static int tda10046_init(struct dvb_frontend* fe)
  530. {
  531. struct tda1004x_state* state = fe->demodulator_priv;
  532. dprintk("%s\n", __FUNCTION__);
  533. if (tda10046_fwupload(fe)) {
  534. printk("tda1004x: firmware upload failed\n");
  535. return -EIO;
  536. }
  537. // tda setup
  538. tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
  539. tda1004x_write_byteI(state, TDA1004X_AUTO, 0x87); // 100 ppm crystal, select HP stream
  540. tda1004x_write_byteI(state, TDA1004X_CONFC1, 0x88); // enable pulse killer
  541. switch (state->config->agc_config) {
  542. case TDA10046_AGC_DEFAULT:
  543. tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x00); // AGC setup
  544. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
  545. break;
  546. case TDA10046_AGC_IFO_AUTO_NEG:
  547. tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
  548. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
  549. break;
  550. case TDA10046_AGC_IFO_AUTO_POS:
  551. tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
  552. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x00); // set AGC polarities
  553. break;
  554. case TDA10046_AGC_TDA827X:
  555. tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x02); // AGC setup
  556. tda1004x_write_byteI(state, TDA10046H_AGC_THR, 0x70); // AGC Threshold
  557. tda1004x_write_byteI(state, TDA10046H_AGC_RENORM, 0x08); // Gain Renormalize
  558. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60); // set AGC polarities
  559. break;
  560. }
  561. if (state->config->ts_mode == 0) {
  562. tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x40);
  563. tda1004x_write_mask(state, 0x3a, 0x80, state->config->invert_oclk << 7);
  564. } else {
  565. tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x80);
  566. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x10,
  567. state->config->invert_oclk << 4);
  568. }
  569. tda1004x_write_byteI(state, TDA1004X_CONFADC2, 0x38);
  570. tda1004x_write_mask (state, TDA10046H_CONF_TRISTATE1, 0x3e, 0x38); // Turn IF AGC output 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. return 0;
  581. }
  582. static int tda1004x_set_fe(struct dvb_frontend* fe,
  583. struct dvb_frontend_parameters *fe_params)
  584. {
  585. struct tda1004x_state* state = fe->demodulator_priv;
  586. int tmp;
  587. int inversion;
  588. dprintk("%s\n", __FUNCTION__);
  589. if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
  590. // setup auto offset
  591. tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10);
  592. tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x80, 0);
  593. tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0);
  594. // disable agc_conf[2]
  595. tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 0);
  596. }
  597. // set frequency
  598. if (fe->ops.tuner_ops.set_params) {
  599. fe->ops.tuner_ops.set_params(fe, fe_params);
  600. if (fe->ops.i2c_gate_ctrl)
  601. 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. int gpio_conf;
  990. switch (state->demod_type) {
  991. case TDA1004X_DEMOD_TDA10045:
  992. tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0x10);
  993. break;
  994. case TDA1004X_DEMOD_TDA10046:
  995. /* set outputs to tristate */
  996. tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0xff);
  997. /* invert GPIO 1 and 3 if desired*/
  998. gpio_conf = state->config->gpio_config;
  999. if (gpio_conf >= TDA10046_GP00_I)
  1000. tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f,
  1001. (gpio_conf & 0x0f) ^ 0x0a);
  1002. tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0xc0);
  1003. tda1004x_write_mask(state, TDA1004X_CONFC4, 1, 1);
  1004. break;
  1005. }
  1006. return 0;
  1007. }
  1008. static int tda1004x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  1009. {
  1010. struct tda1004x_state* state = fe->demodulator_priv;
  1011. if (enable) {
  1012. return tda1004x_enable_tuner_i2c(state);
  1013. } else {
  1014. return tda1004x_disable_tuner_i2c(state);
  1015. }
  1016. }
  1017. static int tda1004x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  1018. {
  1019. fesettings->min_delay_ms = 800;
  1020. /* Drift compensation makes no sense for DVB-T */
  1021. fesettings->step_size = 0;
  1022. fesettings->max_drift = 0;
  1023. return 0;
  1024. }
  1025. static void tda1004x_release(struct dvb_frontend* fe)
  1026. {
  1027. struct tda1004x_state *state = fe->demodulator_priv;
  1028. kfree(state);
  1029. }
  1030. static struct dvb_frontend_ops tda10045_ops = {
  1031. .info = {
  1032. .name = "Philips TDA10045H DVB-T",
  1033. .type = FE_OFDM,
  1034. .frequency_min = 51000000,
  1035. .frequency_max = 858000000,
  1036. .frequency_stepsize = 166667,
  1037. .caps =
  1038. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1039. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1040. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1041. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
  1042. },
  1043. .release = tda1004x_release,
  1044. .init = tda10045_init,
  1045. .sleep = tda1004x_sleep,
  1046. .write = tda1004x_write,
  1047. .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
  1048. .set_frontend = tda1004x_set_fe,
  1049. .get_frontend = tda1004x_get_fe,
  1050. .get_tune_settings = tda1004x_get_tune_settings,
  1051. .read_status = tda1004x_read_status,
  1052. .read_ber = tda1004x_read_ber,
  1053. .read_signal_strength = tda1004x_read_signal_strength,
  1054. .read_snr = tda1004x_read_snr,
  1055. .read_ucblocks = tda1004x_read_ucblocks,
  1056. };
  1057. struct dvb_frontend* tda10045_attach(const struct tda1004x_config* config,
  1058. struct i2c_adapter* i2c)
  1059. {
  1060. struct tda1004x_state *state;
  1061. /* allocate memory for the internal state */
  1062. state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
  1063. if (!state)
  1064. return NULL;
  1065. /* setup the state */
  1066. state->config = config;
  1067. state->i2c = i2c;
  1068. state->demod_type = TDA1004X_DEMOD_TDA10045;
  1069. /* check if the demod is there */
  1070. if (tda1004x_read_byte(state, TDA1004X_CHIPID) != 0x25) {
  1071. kfree(state);
  1072. return NULL;
  1073. }
  1074. /* create dvb_frontend */
  1075. memcpy(&state->frontend.ops, &tda10045_ops, sizeof(struct dvb_frontend_ops));
  1076. state->frontend.demodulator_priv = state;
  1077. return &state->frontend;
  1078. }
  1079. static struct dvb_frontend_ops tda10046_ops = {
  1080. .info = {
  1081. .name = "Philips TDA10046H DVB-T",
  1082. .type = FE_OFDM,
  1083. .frequency_min = 51000000,
  1084. .frequency_max = 858000000,
  1085. .frequency_stepsize = 166667,
  1086. .caps =
  1087. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  1088. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  1089. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  1090. FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
  1091. },
  1092. .release = tda1004x_release,
  1093. .init = tda10046_init,
  1094. .sleep = tda1004x_sleep,
  1095. .write = tda1004x_write,
  1096. .i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
  1097. .set_frontend = tda1004x_set_fe,
  1098. .get_frontend = tda1004x_get_fe,
  1099. .get_tune_settings = tda1004x_get_tune_settings,
  1100. .read_status = tda1004x_read_status,
  1101. .read_ber = tda1004x_read_ber,
  1102. .read_signal_strength = tda1004x_read_signal_strength,
  1103. .read_snr = tda1004x_read_snr,
  1104. .read_ucblocks = tda1004x_read_ucblocks,
  1105. };
  1106. struct dvb_frontend* tda10046_attach(const struct tda1004x_config* config,
  1107. struct i2c_adapter* i2c)
  1108. {
  1109. struct tda1004x_state *state;
  1110. /* allocate memory for the internal state */
  1111. state = kmalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
  1112. if (!state)
  1113. return NULL;
  1114. /* setup the state */
  1115. state->config = config;
  1116. state->i2c = i2c;
  1117. state->demod_type = TDA1004X_DEMOD_TDA10046;
  1118. /* check if the demod is there */
  1119. if (tda1004x_read_byte(state, TDA1004X_CHIPID) != 0x46) {
  1120. kfree(state);
  1121. return NULL;
  1122. }
  1123. /* create dvb_frontend */
  1124. memcpy(&state->frontend.ops, &tda10046_ops, sizeof(struct dvb_frontend_ops));
  1125. state->frontend.demodulator_priv = state;
  1126. return &state->frontend;
  1127. }
  1128. module_param(debug, int, 0644);
  1129. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  1130. MODULE_DESCRIPTION("Philips TDA10045H & TDA10046H DVB-T Demodulator");
  1131. MODULE_AUTHOR("Andrew de Quincey & Robert Schlabbach");
  1132. MODULE_LICENSE("GPL");
  1133. EXPORT_SYMBOL(tda10045_attach);
  1134. EXPORT_SYMBOL(tda10046_attach);