lgs8gl5.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. Legend Silicon LGS-8GL5 DMB-TH OFDM demodulator driver
  3. Copyright (C) 2008 Sirius International (Hong Kong) Limited
  4. Timothy Lee <timothy.lee@siriushk.com>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/module.h>
  20. #include <linux/string.h>
  21. #include <linux/slab.h>
  22. #include "dvb_frontend.h"
  23. #include "lgs8gl5.h"
  24. #define REG_RESET 0x02
  25. #define REG_RESET_OFF 0x01
  26. #define REG_03 0x03
  27. #define REG_04 0x04
  28. #define REG_07 0x07
  29. #define REG_09 0x09
  30. #define REG_0A 0x0a
  31. #define REG_0B 0x0b
  32. #define REG_0C 0x0c
  33. #define REG_37 0x37
  34. #define REG_STRENGTH 0x4b
  35. #define REG_STRENGTH_MASK 0x7f
  36. #define REG_STRENGTH_CARRIER 0x80
  37. #define REG_INVERSION 0x7c
  38. #define REG_INVERSION_ON 0x80
  39. #define REG_7D 0x7d
  40. #define REG_7E 0x7e
  41. #define REG_A2 0xa2
  42. #define REG_STATUS 0xa4
  43. #define REG_STATUS_SYNC 0x04
  44. #define REG_STATUS_LOCK 0x01
  45. struct lgs8gl5_state {
  46. struct i2c_adapter *i2c;
  47. const struct lgs8gl5_config *config;
  48. struct dvb_frontend frontend;
  49. };
  50. static int debug;
  51. #define dprintk(args...) \
  52. do { \
  53. if (debug) \
  54. printk(KERN_DEBUG "lgs8gl5: " args); \
  55. } while (0)
  56. /* Writes into demod's register */
  57. static int
  58. lgs8gl5_write_reg(struct lgs8gl5_state *state, u8 reg, u8 data)
  59. {
  60. int ret;
  61. u8 buf[] = {reg, data};
  62. struct i2c_msg msg = {
  63. .addr = state->config->demod_address,
  64. .flags = 0,
  65. .buf = buf,
  66. .len = 2
  67. };
  68. ret = i2c_transfer(state->i2c, &msg, 1);
  69. if (ret != 1)
  70. dprintk("%s: error (reg=0x%02x, val=0x%02x, ret=%i)\n",
  71. __func__, reg, data, ret);
  72. return (ret != 1) ? -1 : 0;
  73. }
  74. /* Reads from demod's register */
  75. static int
  76. lgs8gl5_read_reg(struct lgs8gl5_state *state, u8 reg)
  77. {
  78. int ret;
  79. u8 b0[] = {reg};
  80. u8 b1[] = {0};
  81. struct i2c_msg msg[2] = {
  82. {
  83. .addr = state->config->demod_address,
  84. .flags = 0,
  85. .buf = b0,
  86. .len = 1
  87. },
  88. {
  89. .addr = state->config->demod_address,
  90. .flags = I2C_M_RD,
  91. .buf = b1,
  92. .len = 1
  93. }
  94. };
  95. ret = i2c_transfer(state->i2c, msg, 2);
  96. if (ret != 2)
  97. return -EIO;
  98. return b1[0];
  99. }
  100. static int
  101. lgs8gl5_update_reg(struct lgs8gl5_state *state, u8 reg, u8 data)
  102. {
  103. lgs8gl5_read_reg(state, reg);
  104. lgs8gl5_write_reg(state, reg, data);
  105. return 0;
  106. }
  107. /* Writes into alternate device's register */
  108. /* TODO: Find out what that device is for! */
  109. static int
  110. lgs8gl5_update_alt_reg(struct lgs8gl5_state *state, u8 reg, u8 data)
  111. {
  112. int ret;
  113. u8 b0[] = {reg};
  114. u8 b1[] = {0};
  115. u8 b2[] = {reg, data};
  116. struct i2c_msg msg[3] = {
  117. {
  118. .addr = state->config->demod_address + 2,
  119. .flags = 0,
  120. .buf = b0,
  121. .len = 1
  122. },
  123. {
  124. .addr = state->config->demod_address + 2,
  125. .flags = I2C_M_RD,
  126. .buf = b1,
  127. .len = 1
  128. },
  129. {
  130. .addr = state->config->demod_address + 2,
  131. .flags = 0,
  132. .buf = b2,
  133. .len = 2
  134. },
  135. };
  136. ret = i2c_transfer(state->i2c, msg, 3);
  137. return (ret != 3) ? -1 : 0;
  138. }
  139. static void
  140. lgs8gl5_soft_reset(struct lgs8gl5_state *state)
  141. {
  142. u8 val;
  143. dprintk("%s\n", __func__);
  144. val = lgs8gl5_read_reg(state, REG_RESET);
  145. lgs8gl5_write_reg(state, REG_RESET, val & ~REG_RESET_OFF);
  146. lgs8gl5_write_reg(state, REG_RESET, val | REG_RESET_OFF);
  147. msleep(5);
  148. }
  149. /* Starts demodulation */
  150. static void
  151. lgs8gl5_start_demod(struct lgs8gl5_state *state)
  152. {
  153. u8 val;
  154. int n;
  155. dprintk("%s\n", __func__);
  156. lgs8gl5_update_alt_reg(state, 0xc2, 0x28);
  157. lgs8gl5_soft_reset(state);
  158. lgs8gl5_update_reg(state, REG_07, 0x10);
  159. lgs8gl5_update_reg(state, REG_07, 0x10);
  160. lgs8gl5_write_reg(state, REG_09, 0x0e);
  161. lgs8gl5_write_reg(state, REG_0A, 0xe5);
  162. lgs8gl5_write_reg(state, REG_0B, 0x35);
  163. lgs8gl5_write_reg(state, REG_0C, 0x30);
  164. lgs8gl5_update_reg(state, REG_03, 0x00);
  165. lgs8gl5_update_reg(state, REG_7E, 0x01);
  166. lgs8gl5_update_alt_reg(state, 0xc5, 0x00);
  167. lgs8gl5_update_reg(state, REG_04, 0x02);
  168. lgs8gl5_update_reg(state, REG_37, 0x01);
  169. lgs8gl5_soft_reset(state);
  170. /* Wait for carrier */
  171. for (n = 0; n < 10; n++) {
  172. val = lgs8gl5_read_reg(state, REG_STRENGTH);
  173. dprintk("Wait for carrier[%d] 0x%02X\n", n, val);
  174. if (val & REG_STRENGTH_CARRIER)
  175. break;
  176. msleep(4);
  177. }
  178. if (!(val & REG_STRENGTH_CARRIER))
  179. return;
  180. /* Wait for lock */
  181. for (n = 0; n < 20; n++) {
  182. val = lgs8gl5_read_reg(state, REG_STATUS);
  183. dprintk("Wait for lock[%d] 0x%02X\n", n, val);
  184. if (val & REG_STATUS_LOCK)
  185. break;
  186. msleep(12);
  187. }
  188. if (!(val & REG_STATUS_LOCK))
  189. return;
  190. lgs8gl5_write_reg(state, REG_7D, lgs8gl5_read_reg(state, REG_A2));
  191. lgs8gl5_soft_reset(state);
  192. }
  193. static int
  194. lgs8gl5_init(struct dvb_frontend *fe)
  195. {
  196. struct lgs8gl5_state *state = fe->demodulator_priv;
  197. dprintk("%s\n", __func__);
  198. lgs8gl5_update_alt_reg(state, 0xc2, 0x28);
  199. lgs8gl5_soft_reset(state);
  200. lgs8gl5_update_reg(state, REG_07, 0x10);
  201. lgs8gl5_update_reg(state, REG_07, 0x10);
  202. lgs8gl5_write_reg(state, REG_09, 0x0e);
  203. lgs8gl5_write_reg(state, REG_0A, 0xe5);
  204. lgs8gl5_write_reg(state, REG_0B, 0x35);
  205. lgs8gl5_write_reg(state, REG_0C, 0x30);
  206. return 0;
  207. }
  208. static int
  209. lgs8gl5_read_status(struct dvb_frontend *fe, enum fe_status *status)
  210. {
  211. struct lgs8gl5_state *state = fe->demodulator_priv;
  212. u8 level = lgs8gl5_read_reg(state, REG_STRENGTH);
  213. u8 flags = lgs8gl5_read_reg(state, REG_STATUS);
  214. *status = 0;
  215. if ((level & REG_STRENGTH_MASK) > 0)
  216. *status |= FE_HAS_SIGNAL;
  217. if (level & REG_STRENGTH_CARRIER)
  218. *status |= FE_HAS_CARRIER;
  219. if (flags & REG_STATUS_SYNC)
  220. *status |= FE_HAS_SYNC;
  221. if (flags & REG_STATUS_LOCK)
  222. *status |= FE_HAS_LOCK;
  223. return 0;
  224. }
  225. static int
  226. lgs8gl5_read_ber(struct dvb_frontend *fe, u32 *ber)
  227. {
  228. *ber = 0;
  229. return 0;
  230. }
  231. static int
  232. lgs8gl5_read_signal_strength(struct dvb_frontend *fe, u16 *signal_strength)
  233. {
  234. struct lgs8gl5_state *state = fe->demodulator_priv;
  235. u8 level = lgs8gl5_read_reg(state, REG_STRENGTH);
  236. *signal_strength = (level & REG_STRENGTH_MASK) << 8;
  237. return 0;
  238. }
  239. static int
  240. lgs8gl5_read_snr(struct dvb_frontend *fe, u16 *snr)
  241. {
  242. struct lgs8gl5_state *state = fe->demodulator_priv;
  243. u8 level = lgs8gl5_read_reg(state, REG_STRENGTH);
  244. *snr = (level & REG_STRENGTH_MASK) << 8;
  245. return 0;
  246. }
  247. static int
  248. lgs8gl5_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  249. {
  250. *ucblocks = 0;
  251. return 0;
  252. }
  253. static int
  254. lgs8gl5_set_frontend(struct dvb_frontend *fe)
  255. {
  256. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  257. struct lgs8gl5_state *state = fe->demodulator_priv;
  258. dprintk("%s\n", __func__);
  259. if (p->bandwidth_hz != 8000000)
  260. return -EINVAL;
  261. if (fe->ops.tuner_ops.set_params) {
  262. fe->ops.tuner_ops.set_params(fe);
  263. if (fe->ops.i2c_gate_ctrl)
  264. fe->ops.i2c_gate_ctrl(fe, 0);
  265. }
  266. /* lgs8gl5_set_inversion(state, p->inversion); */
  267. lgs8gl5_start_demod(state);
  268. return 0;
  269. }
  270. static int
  271. lgs8gl5_get_frontend(struct dvb_frontend *fe)
  272. {
  273. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  274. struct lgs8gl5_state *state = fe->demodulator_priv;
  275. u8 inv = lgs8gl5_read_reg(state, REG_INVERSION);
  276. p->inversion = (inv & REG_INVERSION_ON) ? INVERSION_ON : INVERSION_OFF;
  277. p->code_rate_HP = FEC_1_2;
  278. p->code_rate_LP = FEC_7_8;
  279. p->guard_interval = GUARD_INTERVAL_1_32;
  280. p->transmission_mode = TRANSMISSION_MODE_2K;
  281. p->modulation = QAM_64;
  282. p->hierarchy = HIERARCHY_NONE;
  283. p->bandwidth_hz = 8000000;
  284. return 0;
  285. }
  286. static int
  287. lgs8gl5_get_tune_settings(struct dvb_frontend *fe,
  288. struct dvb_frontend_tune_settings *fesettings)
  289. {
  290. fesettings->min_delay_ms = 240;
  291. fesettings->step_size = 0;
  292. fesettings->max_drift = 0;
  293. return 0;
  294. }
  295. static void
  296. lgs8gl5_release(struct dvb_frontend *fe)
  297. {
  298. struct lgs8gl5_state *state = fe->demodulator_priv;
  299. kfree(state);
  300. }
  301. static struct dvb_frontend_ops lgs8gl5_ops;
  302. struct dvb_frontend*
  303. lgs8gl5_attach(const struct lgs8gl5_config *config, struct i2c_adapter *i2c)
  304. {
  305. struct lgs8gl5_state *state = NULL;
  306. dprintk("%s\n", __func__);
  307. /* Allocate memory for the internal state */
  308. state = kzalloc(sizeof(struct lgs8gl5_state), GFP_KERNEL);
  309. if (state == NULL)
  310. goto error;
  311. /* Setup the state */
  312. state->config = config;
  313. state->i2c = i2c;
  314. /* Check if the demod is there */
  315. if (lgs8gl5_read_reg(state, REG_RESET) < 0)
  316. goto error;
  317. /* Create dvb_frontend */
  318. memcpy(&state->frontend.ops, &lgs8gl5_ops,
  319. sizeof(struct dvb_frontend_ops));
  320. state->frontend.demodulator_priv = state;
  321. return &state->frontend;
  322. error:
  323. kfree(state);
  324. return NULL;
  325. }
  326. EXPORT_SYMBOL(lgs8gl5_attach);
  327. static struct dvb_frontend_ops lgs8gl5_ops = {
  328. .delsys = { SYS_DTMB },
  329. .info = {
  330. .name = "Legend Silicon LGS-8GL5 DMB-TH",
  331. .frequency_min = 474000000,
  332. .frequency_max = 858000000,
  333. .frequency_stepsize = 10000,
  334. .frequency_tolerance = 0,
  335. .caps = FE_CAN_FEC_AUTO |
  336. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_32 |
  337. FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  338. FE_CAN_TRANSMISSION_MODE_AUTO |
  339. FE_CAN_BANDWIDTH_AUTO |
  340. FE_CAN_GUARD_INTERVAL_AUTO |
  341. FE_CAN_HIERARCHY_AUTO |
  342. FE_CAN_RECOVER
  343. },
  344. .release = lgs8gl5_release,
  345. .init = lgs8gl5_init,
  346. .set_frontend = lgs8gl5_set_frontend,
  347. .get_frontend = lgs8gl5_get_frontend,
  348. .get_tune_settings = lgs8gl5_get_tune_settings,
  349. .read_status = lgs8gl5_read_status,
  350. .read_ber = lgs8gl5_read_ber,
  351. .read_signal_strength = lgs8gl5_read_signal_strength,
  352. .read_snr = lgs8gl5_read_snr,
  353. .read_ucblocks = lgs8gl5_read_ucblocks,
  354. };
  355. module_param(debug, int, 0644);
  356. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  357. MODULE_DESCRIPTION("Legend Silicon LGS-8GL5 DMB-TH Demodulator driver");
  358. MODULE_AUTHOR("Timothy Lee");
  359. MODULE_LICENSE("GPL");