tda1004x.c 39 KB

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