tda1004x.c 37 KB

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