friio-fe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /* DVB USB compliant Linux driver for the Friio USB2.0 ISDB-T receiver.
  2. *
  3. * Copyright (C) 2009 Akihiro Tsukada <tskd2@yahoo.co.jp>
  4. *
  5. * This module is based off the the gl861 and vp702x modules.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation, version 2.
  10. *
  11. * see Documentation/dvb/README.dvb-usb for more information
  12. */
  13. #include <linux/init.h>
  14. #include <linux/string.h>
  15. #include <linux/slab.h>
  16. #include "friio.h"
  17. struct jdvbt90502_state {
  18. struct i2c_adapter *i2c;
  19. struct dvb_frontend frontend;
  20. struct jdvbt90502_config config;
  21. };
  22. /* NOTE: TC90502 has 16bit register-address? */
  23. /* register 0x0100 is used for reading PLL status, so reg is u16 here */
  24. static int jdvbt90502_reg_read(struct jdvbt90502_state *state,
  25. const u16 reg, u8 *buf, const size_t count)
  26. {
  27. int ret;
  28. u8 wbuf[3];
  29. struct i2c_msg msg[2];
  30. wbuf[0] = reg & 0xFF;
  31. wbuf[1] = 0;
  32. wbuf[2] = reg >> 8;
  33. msg[0].addr = state->config.demod_address;
  34. msg[0].flags = 0;
  35. msg[0].buf = wbuf;
  36. msg[0].len = sizeof(wbuf);
  37. msg[1].addr = msg[0].addr;
  38. msg[1].flags = I2C_M_RD;
  39. msg[1].buf = buf;
  40. msg[1].len = count;
  41. ret = i2c_transfer(state->i2c, msg, 2);
  42. if (ret != 2) {
  43. deb_fe(" reg read failed.\n");
  44. return -EREMOTEIO;
  45. }
  46. return 0;
  47. }
  48. /* currently 16bit register-address is not used, so reg is u8 here */
  49. static int jdvbt90502_single_reg_write(struct jdvbt90502_state *state,
  50. const u8 reg, const u8 val)
  51. {
  52. struct i2c_msg msg;
  53. u8 wbuf[2];
  54. wbuf[0] = reg;
  55. wbuf[1] = val;
  56. msg.addr = state->config.demod_address;
  57. msg.flags = 0;
  58. msg.buf = wbuf;
  59. msg.len = sizeof(wbuf);
  60. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  61. deb_fe(" reg write failed.");
  62. return -EREMOTEIO;
  63. }
  64. return 0;
  65. }
  66. static int _jdvbt90502_write(struct dvb_frontend *fe, const u8 buf[], int len)
  67. {
  68. struct jdvbt90502_state *state = fe->demodulator_priv;
  69. int err, i;
  70. for (i = 0; i < len - 1; i++) {
  71. err = jdvbt90502_single_reg_write(state,
  72. buf[0] + i, buf[i + 1]);
  73. if (err)
  74. return err;
  75. }
  76. return 0;
  77. }
  78. /* read pll status byte via the demodulator's I2C register */
  79. /* note: Win box reads it by 8B block at the I2C addr 0x30 from reg:0x80 */
  80. static int jdvbt90502_pll_read(struct jdvbt90502_state *state, u8 *result)
  81. {
  82. int ret;
  83. /* +1 for reading */
  84. u8 pll_addr_byte = (state->config.pll_address << 1) + 1;
  85. *result = 0;
  86. ret = jdvbt90502_single_reg_write(state, JDVBT90502_2ND_I2C_REG,
  87. pll_addr_byte);
  88. if (ret)
  89. goto error;
  90. ret = jdvbt90502_reg_read(state, 0x0100, result, 1);
  91. if (ret)
  92. goto error;
  93. deb_fe("PLL read val:%02x\n", *result);
  94. return 0;
  95. error:
  96. deb_fe("%s:ret == %d\n", __func__, ret);
  97. return -EREMOTEIO;
  98. }
  99. /* set pll frequency via the demodulator's I2C register */
  100. static int jdvbt90502_pll_set_freq(struct jdvbt90502_state *state, u32 freq)
  101. {
  102. int ret;
  103. int retry;
  104. u8 res1;
  105. u8 res2[9];
  106. u8 pll_freq_cmd[PLL_CMD_LEN];
  107. u8 pll_agc_cmd[PLL_CMD_LEN];
  108. struct i2c_msg msg[2];
  109. u32 f;
  110. deb_fe("%s: freq=%d, step=%d\n", __func__, freq,
  111. state->frontend.ops.info.frequency_stepsize);
  112. /* freq -> oscilator frequency conversion. */
  113. /* freq: 473,000,000 + n*6,000,000 [+ 142857 (center freq. shift)] */
  114. f = freq / state->frontend.ops.info.frequency_stepsize;
  115. /* add 399[1/7 MHZ] = 57MHz for the IF */
  116. f += 399;
  117. /* add center frequency shift if necessary */
  118. if (f % 7 == 0)
  119. f++;
  120. pll_freq_cmd[DEMOD_REDIRECT_REG] = JDVBT90502_2ND_I2C_REG; /* 0xFE */
  121. pll_freq_cmd[ADDRESS_BYTE] = state->config.pll_address << 1;
  122. pll_freq_cmd[DIVIDER_BYTE1] = (f >> 8) & 0x7F;
  123. pll_freq_cmd[DIVIDER_BYTE2] = f & 0xFF;
  124. pll_freq_cmd[CONTROL_BYTE] = 0xB2; /* ref.divider:28, 4MHz/28=1/7MHz */
  125. pll_freq_cmd[BANDSWITCH_BYTE] = 0x08; /* UHF band */
  126. msg[0].addr = state->config.demod_address;
  127. msg[0].flags = 0;
  128. msg[0].buf = pll_freq_cmd;
  129. msg[0].len = sizeof(pll_freq_cmd);
  130. ret = i2c_transfer(state->i2c, &msg[0], 1);
  131. if (ret != 1)
  132. goto error;
  133. udelay(50);
  134. pll_agc_cmd[DEMOD_REDIRECT_REG] = pll_freq_cmd[DEMOD_REDIRECT_REG];
  135. pll_agc_cmd[ADDRESS_BYTE] = pll_freq_cmd[ADDRESS_BYTE];
  136. pll_agc_cmd[DIVIDER_BYTE1] = pll_freq_cmd[DIVIDER_BYTE1];
  137. pll_agc_cmd[DIVIDER_BYTE2] = pll_freq_cmd[DIVIDER_BYTE2];
  138. pll_agc_cmd[CONTROL_BYTE] = 0x9A; /* AGC_CTRL instead of BANDSWITCH */
  139. pll_agc_cmd[AGC_CTRL_BYTE] = 0x50;
  140. /* AGC Time Constant 2s, AGC take-over point:103dBuV(lowest) */
  141. msg[1].addr = msg[0].addr;
  142. msg[1].flags = 0;
  143. msg[1].buf = pll_agc_cmd;
  144. msg[1].len = sizeof(pll_agc_cmd);
  145. ret = i2c_transfer(state->i2c, &msg[1], 1);
  146. if (ret != 1)
  147. goto error;
  148. /* I don't know what these cmds are for, */
  149. /* but the USB log on a windows box contains them */
  150. ret = jdvbt90502_single_reg_write(state, 0x01, 0x40);
  151. ret |= jdvbt90502_single_reg_write(state, 0x01, 0x00);
  152. if (ret)
  153. goto error;
  154. udelay(100);
  155. /* wait for the demod to be ready? */
  156. #define RETRY_COUNT 5
  157. for (retry = 0; retry < RETRY_COUNT; retry++) {
  158. ret = jdvbt90502_reg_read(state, 0x0096, &res1, 1);
  159. if (ret)
  160. goto error;
  161. /* if (res1 != 0x00) goto error; */
  162. ret = jdvbt90502_reg_read(state, 0x00B0, res2, sizeof(res2));
  163. if (ret)
  164. goto error;
  165. if (res2[0] >= 0xA7)
  166. break;
  167. msleep(100);
  168. }
  169. if (retry >= RETRY_COUNT) {
  170. deb_fe("%s: FE does not get ready after freq setting.\n",
  171. __func__);
  172. return -EREMOTEIO;
  173. }
  174. return 0;
  175. error:
  176. deb_fe("%s:ret == %d\n", __func__, ret);
  177. return -EREMOTEIO;
  178. }
  179. static int jdvbt90502_read_status(struct dvb_frontend *fe,
  180. enum fe_status *state)
  181. {
  182. u8 result;
  183. int ret;
  184. *state = FE_HAS_SIGNAL;
  185. ret = jdvbt90502_pll_read(fe->demodulator_priv, &result);
  186. if (ret) {
  187. deb_fe("%s:ret == %d\n", __func__, ret);
  188. return -EREMOTEIO;
  189. }
  190. *state = FE_HAS_SIGNAL
  191. | FE_HAS_CARRIER
  192. | FE_HAS_VITERBI
  193. | FE_HAS_SYNC;
  194. if (result & PLL_STATUS_LOCKED)
  195. *state |= FE_HAS_LOCK;
  196. return 0;
  197. }
  198. static int jdvbt90502_read_signal_strength(struct dvb_frontend *fe,
  199. u16 *strength)
  200. {
  201. int ret;
  202. u8 rbuf[37];
  203. *strength = 0;
  204. /* status register (incl. signal strength) : 0x89 */
  205. /* TODO: read just the necessary registers [0x8B..0x8D]? */
  206. ret = jdvbt90502_reg_read(fe->demodulator_priv, 0x0089,
  207. rbuf, sizeof(rbuf));
  208. if (ret) {
  209. deb_fe("%s:ret == %d\n", __func__, ret);
  210. return -EREMOTEIO;
  211. }
  212. /* signal_strength: rbuf[2-4] (24bit BE), use lower 16bit for now. */
  213. *strength = (rbuf[3] << 8) + rbuf[4];
  214. if (rbuf[2])
  215. *strength = 0xffff;
  216. return 0;
  217. }
  218. /* filter out un-supported properties to notify users */
  219. static int jdvbt90502_set_property(struct dvb_frontend *fe,
  220. struct dtv_property *tvp)
  221. {
  222. int r = 0;
  223. switch (tvp->cmd) {
  224. case DTV_DELIVERY_SYSTEM:
  225. if (tvp->u.data != SYS_ISDBT)
  226. r = -EINVAL;
  227. break;
  228. case DTV_CLEAR:
  229. case DTV_TUNE:
  230. case DTV_FREQUENCY:
  231. break;
  232. default:
  233. r = -EINVAL;
  234. }
  235. return r;
  236. }
  237. static int jdvbt90502_get_frontend(struct dvb_frontend *fe)
  238. {
  239. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  240. p->inversion = INVERSION_AUTO;
  241. p->bandwidth_hz = 6000000;
  242. p->code_rate_HP = FEC_AUTO;
  243. p->code_rate_LP = FEC_AUTO;
  244. p->modulation = QAM_64;
  245. p->transmission_mode = TRANSMISSION_MODE_AUTO;
  246. p->guard_interval = GUARD_INTERVAL_AUTO;
  247. p->hierarchy = HIERARCHY_AUTO;
  248. return 0;
  249. }
  250. static int jdvbt90502_set_frontend(struct dvb_frontend *fe)
  251. {
  252. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  253. /**
  254. * NOTE: ignore all the parameters except frequency.
  255. * others should be fixed to the proper value for ISDB-T,
  256. * but don't check here.
  257. */
  258. struct jdvbt90502_state *state = fe->demodulator_priv;
  259. int ret;
  260. deb_fe("%s: Freq:%d\n", __func__, p->frequency);
  261. /* for recovery from DTV_CLEAN */
  262. fe->dtv_property_cache.delivery_system = SYS_ISDBT;
  263. ret = jdvbt90502_pll_set_freq(state, p->frequency);
  264. if (ret) {
  265. deb_fe("%s:ret == %d\n", __func__, ret);
  266. return -EREMOTEIO;
  267. }
  268. return 0;
  269. }
  270. /**
  271. * (reg, val) commad list to initialize this module.
  272. * captured on a Windows box.
  273. */
  274. static u8 init_code[][2] = {
  275. {0x01, 0x40},
  276. {0x04, 0x38},
  277. {0x05, 0x40},
  278. {0x07, 0x40},
  279. {0x0F, 0x4F},
  280. {0x11, 0x21},
  281. {0x12, 0x0B},
  282. {0x13, 0x2F},
  283. {0x14, 0x31},
  284. {0x16, 0x02},
  285. {0x21, 0xC4},
  286. {0x22, 0x20},
  287. {0x2C, 0x79},
  288. {0x2D, 0x34},
  289. {0x2F, 0x00},
  290. {0x30, 0x28},
  291. {0x31, 0x31},
  292. {0x32, 0xDF},
  293. {0x38, 0x01},
  294. {0x39, 0x78},
  295. {0x3B, 0x33},
  296. {0x3C, 0x33},
  297. {0x48, 0x90},
  298. {0x51, 0x68},
  299. {0x5E, 0x38},
  300. {0x71, 0x00},
  301. {0x72, 0x08},
  302. {0x77, 0x00},
  303. {0xC0, 0x21},
  304. {0xC1, 0x10},
  305. {0xE4, 0x1A},
  306. {0xEA, 0x1F},
  307. {0x77, 0x00},
  308. {0x71, 0x00},
  309. {0x71, 0x00},
  310. {0x76, 0x0C},
  311. };
  312. static const int init_code_len = sizeof(init_code) / sizeof(u8[2]);
  313. static int jdvbt90502_init(struct dvb_frontend *fe)
  314. {
  315. int i = -1;
  316. int ret;
  317. struct i2c_msg msg;
  318. struct jdvbt90502_state *state = fe->demodulator_priv;
  319. deb_fe("%s called.\n", __func__);
  320. msg.addr = state->config.demod_address;
  321. msg.flags = 0;
  322. msg.len = 2;
  323. for (i = 0; i < init_code_len; i++) {
  324. msg.buf = init_code[i];
  325. ret = i2c_transfer(state->i2c, &msg, 1);
  326. if (ret != 1)
  327. goto error;
  328. }
  329. fe->dtv_property_cache.delivery_system = SYS_ISDBT;
  330. msleep(100);
  331. return 0;
  332. error:
  333. deb_fe("%s: init_code[%d] failed. ret==%d\n", __func__, i, ret);
  334. return -EREMOTEIO;
  335. }
  336. static void jdvbt90502_release(struct dvb_frontend *fe)
  337. {
  338. struct jdvbt90502_state *state = fe->demodulator_priv;
  339. kfree(state);
  340. }
  341. static struct dvb_frontend_ops jdvbt90502_ops;
  342. struct dvb_frontend *jdvbt90502_attach(struct dvb_usb_device *d)
  343. {
  344. struct jdvbt90502_state *state = NULL;
  345. deb_info("%s called.\n", __func__);
  346. /* allocate memory for the internal state */
  347. state = kzalloc(sizeof(struct jdvbt90502_state), GFP_KERNEL);
  348. if (state == NULL)
  349. goto error;
  350. /* setup the state */
  351. state->i2c = &d->i2c_adap;
  352. state->config = friio_fe_config;
  353. /* create dvb_frontend */
  354. state->frontend.ops = jdvbt90502_ops;
  355. state->frontend.demodulator_priv = state;
  356. if (jdvbt90502_init(&state->frontend) < 0)
  357. goto error;
  358. return &state->frontend;
  359. error:
  360. kfree(state);
  361. return NULL;
  362. }
  363. static struct dvb_frontend_ops jdvbt90502_ops = {
  364. .delsys = { SYS_ISDBT },
  365. .info = {
  366. .name = "Comtech JDVBT90502 ISDB-T",
  367. .frequency_min = 473000000, /* UHF 13ch, center */
  368. .frequency_max = 767142857, /* UHF 62ch, center */
  369. .frequency_stepsize = JDVBT90502_PLL_CLK / JDVBT90502_PLL_DIVIDER,
  370. .frequency_tolerance = 0,
  371. /* NOTE: this driver ignores all parameters but frequency. */
  372. .caps = FE_CAN_INVERSION_AUTO |
  373. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  374. FE_CAN_FEC_4_5 | FE_CAN_FEC_5_6 | FE_CAN_FEC_6_7 |
  375. FE_CAN_FEC_7_8 | FE_CAN_FEC_8_9 | FE_CAN_FEC_AUTO |
  376. FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  377. FE_CAN_TRANSMISSION_MODE_AUTO |
  378. FE_CAN_GUARD_INTERVAL_AUTO |
  379. FE_CAN_HIERARCHY_AUTO,
  380. },
  381. .release = jdvbt90502_release,
  382. .init = jdvbt90502_init,
  383. .write = _jdvbt90502_write,
  384. .set_property = jdvbt90502_set_property,
  385. .set_frontend = jdvbt90502_set_frontend,
  386. .get_frontend = jdvbt90502_get_frontend,
  387. .read_status = jdvbt90502_read_status,
  388. .read_signal_strength = jdvbt90502_read_signal_strength,
  389. };