friio-fe.c 10 KB

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