friio-fe.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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, fe_status_t *state)
  180. {
  181. u8 result;
  182. int ret;
  183. *state = FE_HAS_SIGNAL;
  184. ret = jdvbt90502_pll_read(fe->demodulator_priv, &result);
  185. if (ret) {
  186. deb_fe("%s:ret == %d\n", __func__, ret);
  187. return -EREMOTEIO;
  188. }
  189. *state = FE_HAS_SIGNAL
  190. | FE_HAS_CARRIER
  191. | FE_HAS_VITERBI
  192. | FE_HAS_SYNC;
  193. if (result & PLL_STATUS_LOCKED)
  194. *state |= FE_HAS_LOCK;
  195. return 0;
  196. }
  197. static int jdvbt90502_read_signal_strength(struct dvb_frontend *fe,
  198. u16 *strength)
  199. {
  200. int ret;
  201. u8 rbuf[37];
  202. *strength = 0;
  203. /* status register (incl. signal strength) : 0x89 */
  204. /* TODO: read just the necessary registers [0x8B..0x8D]? */
  205. ret = jdvbt90502_reg_read(fe->demodulator_priv, 0x0089,
  206. rbuf, sizeof(rbuf));
  207. if (ret) {
  208. deb_fe("%s:ret == %d\n", __func__, ret);
  209. return -EREMOTEIO;
  210. }
  211. /* signal_strength: rbuf[2-4] (24bit BE), use lower 16bit for now. */
  212. *strength = (rbuf[3] << 8) + rbuf[4];
  213. if (rbuf[2])
  214. *strength = 0xffff;
  215. return 0;
  216. }
  217. /* filter out un-supported properties to notify users */
  218. static int jdvbt90502_set_property(struct dvb_frontend *fe,
  219. struct dtv_property *tvp)
  220. {
  221. int r = 0;
  222. switch (tvp->cmd) {
  223. case DTV_DELIVERY_SYSTEM:
  224. if (tvp->u.data != SYS_ISDBT)
  225. r = -EINVAL;
  226. break;
  227. case DTV_CLEAR:
  228. case DTV_TUNE:
  229. case DTV_FREQUENCY:
  230. break;
  231. default:
  232. r = -EINVAL;
  233. }
  234. return r;
  235. }
  236. static int jdvbt90502_get_frontend(struct dvb_frontend *fe)
  237. {
  238. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  239. p->inversion = INVERSION_AUTO;
  240. p->bandwidth_hz = 6000000;
  241. p->code_rate_HP = FEC_AUTO;
  242. p->code_rate_LP = FEC_AUTO;
  243. p->modulation = QAM_64;
  244. p->transmission_mode = TRANSMISSION_MODE_AUTO;
  245. p->guard_interval = GUARD_INTERVAL_AUTO;
  246. p->hierarchy = HIERARCHY_AUTO;
  247. return 0;
  248. }
  249. static int jdvbt90502_set_frontend(struct dvb_frontend *fe)
  250. {
  251. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  252. /**
  253. * NOTE: ignore all the parameters except frequency.
  254. * others should be fixed to the proper value for ISDB-T,
  255. * but don't check here.
  256. */
  257. struct jdvbt90502_state *state = fe->demodulator_priv;
  258. int ret;
  259. deb_fe("%s: Freq:%d\n", __func__, p->frequency);
  260. /* for recovery from DTV_CLEAN */
  261. fe->dtv_property_cache.delivery_system = SYS_ISDBT;
  262. ret = jdvbt90502_pll_set_freq(state, p->frequency);
  263. if (ret) {
  264. deb_fe("%s:ret == %d\n", __func__, ret);
  265. return -EREMOTEIO;
  266. }
  267. return 0;
  268. }
  269. /**
  270. * (reg, val) commad list to initialize this module.
  271. * captured on a Windows box.
  272. */
  273. static u8 init_code[][2] = {
  274. {0x01, 0x40},
  275. {0x04, 0x38},
  276. {0x05, 0x40},
  277. {0x07, 0x40},
  278. {0x0F, 0x4F},
  279. {0x11, 0x21},
  280. {0x12, 0x0B},
  281. {0x13, 0x2F},
  282. {0x14, 0x31},
  283. {0x16, 0x02},
  284. {0x21, 0xC4},
  285. {0x22, 0x20},
  286. {0x2C, 0x79},
  287. {0x2D, 0x34},
  288. {0x2F, 0x00},
  289. {0x30, 0x28},
  290. {0x31, 0x31},
  291. {0x32, 0xDF},
  292. {0x38, 0x01},
  293. {0x39, 0x78},
  294. {0x3B, 0x33},
  295. {0x3C, 0x33},
  296. {0x48, 0x90},
  297. {0x51, 0x68},
  298. {0x5E, 0x38},
  299. {0x71, 0x00},
  300. {0x72, 0x08},
  301. {0x77, 0x00},
  302. {0xC0, 0x21},
  303. {0xC1, 0x10},
  304. {0xE4, 0x1A},
  305. {0xEA, 0x1F},
  306. {0x77, 0x00},
  307. {0x71, 0x00},
  308. {0x71, 0x00},
  309. {0x76, 0x0C},
  310. };
  311. static const int init_code_len = sizeof(init_code) / sizeof(u8[2]);
  312. static int jdvbt90502_init(struct dvb_frontend *fe)
  313. {
  314. int i = -1;
  315. int ret;
  316. struct i2c_msg msg;
  317. struct jdvbt90502_state *state = fe->demodulator_priv;
  318. deb_fe("%s called.\n", __func__);
  319. msg.addr = state->config.demod_address;
  320. msg.flags = 0;
  321. msg.len = 2;
  322. for (i = 0; i < init_code_len; i++) {
  323. msg.buf = init_code[i];
  324. ret = i2c_transfer(state->i2c, &msg, 1);
  325. if (ret != 1)
  326. goto error;
  327. }
  328. fe->dtv_property_cache.delivery_system = SYS_ISDBT;
  329. msleep(100);
  330. return 0;
  331. error:
  332. deb_fe("%s: init_code[%d] failed. ret==%d\n", __func__, i, ret);
  333. return -EREMOTEIO;
  334. }
  335. static void jdvbt90502_release(struct dvb_frontend *fe)
  336. {
  337. struct jdvbt90502_state *state = fe->demodulator_priv;
  338. kfree(state);
  339. }
  340. static struct dvb_frontend_ops jdvbt90502_ops;
  341. struct dvb_frontend *jdvbt90502_attach(struct dvb_usb_device *d)
  342. {
  343. struct jdvbt90502_state *state = NULL;
  344. deb_info("%s called.\n", __func__);
  345. /* allocate memory for the internal state */
  346. state = kzalloc(sizeof(struct jdvbt90502_state), GFP_KERNEL);
  347. if (state == NULL)
  348. goto error;
  349. /* setup the state */
  350. state->i2c = &d->i2c_adap;
  351. memcpy(&state->config, &friio_fe_config, sizeof(friio_fe_config));
  352. /* create dvb_frontend */
  353. memcpy(&state->frontend.ops, &jdvbt90502_ops,
  354. sizeof(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. };