tda1004x.c 35 KB

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