stv0288.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. Driver for ST STV0288 demodulator
  3. Copyright (C) 2006 Georg Acher, BayCom GmbH, acher (at) baycom (dot) de
  4. for Reel Multimedia
  5. Copyright (C) 2008 TurboSight.com, Bob Liu <bob@turbosight.com>
  6. Copyright (C) 2008 Igor M. Liplianin <liplianin@me.by>
  7. Removed stb6000 specific tuner code and revised some
  8. procedures.
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/string.h>
  25. #include <linux/slab.h>
  26. #include <linux/jiffies.h>
  27. #include <asm/div64.h>
  28. #include "dvb_frontend.h"
  29. #include "stv0288.h"
  30. struct stv0288_state {
  31. struct i2c_adapter *i2c;
  32. const struct stv0288_config *config;
  33. struct dvb_frontend frontend;
  34. u8 initialised:1;
  35. u32 tuner_frequency;
  36. u32 symbol_rate;
  37. fe_code_rate_t fec_inner;
  38. int errmode;
  39. };
  40. #define STATUS_BER 0
  41. #define STATUS_UCBLOCKS 1
  42. static int debug;
  43. static int debug_legacy_dish_switch;
  44. #define dprintk(args...) \
  45. do { \
  46. if (debug) \
  47. printk(KERN_DEBUG "stv0288: " args); \
  48. } while (0)
  49. static int stv0288_writeregI(struct stv0288_state *state, u8 reg, u8 data)
  50. {
  51. int ret;
  52. u8 buf[] = { reg, data };
  53. struct i2c_msg msg = {
  54. .addr = state->config->demod_address,
  55. .flags = 0,
  56. .buf = buf,
  57. .len = 2
  58. };
  59. ret = i2c_transfer(state->i2c, &msg, 1);
  60. if (ret != 1)
  61. dprintk("%s: writereg error (reg == 0x%02x, val == 0x%02x, "
  62. "ret == %i)\n", __func__, reg, data, ret);
  63. return (ret != 1) ? -EREMOTEIO : 0;
  64. }
  65. static int stv0288_write(struct dvb_frontend *fe, u8 *buf, int len)
  66. {
  67. struct stv0288_state *state = fe->demodulator_priv;
  68. if (len != 2)
  69. return -EINVAL;
  70. return stv0288_writeregI(state, buf[0], buf[1]);
  71. }
  72. static u8 stv0288_readreg(struct stv0288_state *state, u8 reg)
  73. {
  74. int ret;
  75. u8 b0[] = { reg };
  76. u8 b1[] = { 0 };
  77. struct i2c_msg msg[] = {
  78. {
  79. .addr = state->config->demod_address,
  80. .flags = 0,
  81. .buf = b0,
  82. .len = 1
  83. }, {
  84. .addr = state->config->demod_address,
  85. .flags = I2C_M_RD,
  86. .buf = b1,
  87. .len = 1
  88. }
  89. };
  90. ret = i2c_transfer(state->i2c, msg, 2);
  91. if (ret != 2)
  92. dprintk("%s: readreg error (reg == 0x%02x, ret == %i)\n",
  93. __func__, reg, ret);
  94. return b1[0];
  95. }
  96. static int stv0288_set_symbolrate(struct dvb_frontend *fe, u32 srate)
  97. {
  98. struct stv0288_state *state = fe->demodulator_priv;
  99. unsigned int temp;
  100. unsigned char b[3];
  101. if ((srate < 1000000) || (srate > 45000000))
  102. return -EINVAL;
  103. temp = (unsigned int)srate / 1000;
  104. temp = temp * 32768;
  105. temp = temp / 25;
  106. temp = temp / 125;
  107. b[0] = (unsigned char)((temp >> 12) & 0xff);
  108. b[1] = (unsigned char)((temp >> 4) & 0xff);
  109. b[2] = (unsigned char)((temp << 4) & 0xf0);
  110. stv0288_writeregI(state, 0x28, 0x80); /* SFRH */
  111. stv0288_writeregI(state, 0x29, 0); /* SFRM */
  112. stv0288_writeregI(state, 0x2a, 0); /* SFRL */
  113. stv0288_writeregI(state, 0x28, b[0]);
  114. stv0288_writeregI(state, 0x29, b[1]);
  115. stv0288_writeregI(state, 0x2a, b[2]);
  116. dprintk("stv0288: stv0288_set_symbolrate\n");
  117. return 0;
  118. }
  119. static int stv0288_send_diseqc_msg(struct dvb_frontend *fe,
  120. struct dvb_diseqc_master_cmd *m)
  121. {
  122. struct stv0288_state *state = fe->demodulator_priv;
  123. int i;
  124. dprintk("%s\n", __func__);
  125. stv0288_writeregI(state, 0x09, 0);
  126. msleep(30);
  127. stv0288_writeregI(state, 0x05, 0x16);
  128. for (i = 0; i < m->msg_len; i++) {
  129. if (stv0288_writeregI(state, 0x06, m->msg[i]))
  130. return -EREMOTEIO;
  131. msleep(12);
  132. }
  133. return 0;
  134. }
  135. static int stv0288_send_diseqc_burst(struct dvb_frontend *fe,
  136. fe_sec_mini_cmd_t burst)
  137. {
  138. struct stv0288_state *state = fe->demodulator_priv;
  139. dprintk("%s\n", __func__);
  140. if (stv0288_writeregI(state, 0x05, 0x16))/* burst mode */
  141. return -EREMOTEIO;
  142. if (stv0288_writeregI(state, 0x06, burst == SEC_MINI_A ? 0x00 : 0xff))
  143. return -EREMOTEIO;
  144. if (stv0288_writeregI(state, 0x06, 0x12))
  145. return -EREMOTEIO;
  146. return 0;
  147. }
  148. static int stv0288_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  149. {
  150. struct stv0288_state *state = fe->demodulator_priv;
  151. switch (tone) {
  152. case SEC_TONE_ON:
  153. if (stv0288_writeregI(state, 0x05, 0x10))/* burst mode */
  154. return -EREMOTEIO;
  155. return stv0288_writeregI(state, 0x06, 0xff);
  156. case SEC_TONE_OFF:
  157. if (stv0288_writeregI(state, 0x05, 0x13))/* burst mode */
  158. return -EREMOTEIO;
  159. return stv0288_writeregI(state, 0x06, 0x00);
  160. default:
  161. return -EINVAL;
  162. }
  163. }
  164. static u8 stv0288_inittab[] = {
  165. 0x01, 0x15,
  166. 0x02, 0x20,
  167. 0x09, 0x0,
  168. 0x0a, 0x4,
  169. 0x0b, 0x0,
  170. 0x0c, 0x0,
  171. 0x0d, 0x0,
  172. 0x0e, 0xd4,
  173. 0x0f, 0x30,
  174. 0x11, 0x80,
  175. 0x12, 0x03,
  176. 0x13, 0x48,
  177. 0x14, 0x84,
  178. 0x15, 0x45,
  179. 0x16, 0xb7,
  180. 0x17, 0x9c,
  181. 0x18, 0x0,
  182. 0x19, 0xa6,
  183. 0x1a, 0x88,
  184. 0x1b, 0x8f,
  185. 0x1c, 0xf0,
  186. 0x20, 0x0b,
  187. 0x21, 0x54,
  188. 0x22, 0x0,
  189. 0x23, 0x0,
  190. 0x2b, 0xff,
  191. 0x2c, 0xf7,
  192. 0x30, 0x0,
  193. 0x31, 0x1e,
  194. 0x32, 0x14,
  195. 0x33, 0x0f,
  196. 0x34, 0x09,
  197. 0x35, 0x0c,
  198. 0x36, 0x05,
  199. 0x37, 0x2f,
  200. 0x38, 0x16,
  201. 0x39, 0xbe,
  202. 0x3a, 0x0,
  203. 0x3b, 0x13,
  204. 0x3c, 0x11,
  205. 0x3d, 0x30,
  206. 0x40, 0x63,
  207. 0x41, 0x04,
  208. 0x42, 0x60,
  209. 0x43, 0x00,
  210. 0x44, 0x00,
  211. 0x45, 0x00,
  212. 0x46, 0x00,
  213. 0x47, 0x00,
  214. 0x4a, 0x00,
  215. 0x50, 0x10,
  216. 0x51, 0x38,
  217. 0x52, 0x21,
  218. 0x58, 0x54,
  219. 0x59, 0x86,
  220. 0x5a, 0x0,
  221. 0x5b, 0x9b,
  222. 0x5c, 0x08,
  223. 0x5d, 0x7f,
  224. 0x5e, 0x0,
  225. 0x5f, 0xff,
  226. 0x70, 0x0,
  227. 0x71, 0x0,
  228. 0x72, 0x0,
  229. 0x74, 0x0,
  230. 0x75, 0x0,
  231. 0x76, 0x0,
  232. 0x81, 0x0,
  233. 0x82, 0x3f,
  234. 0x83, 0x3f,
  235. 0x84, 0x0,
  236. 0x85, 0x0,
  237. 0x88, 0x0,
  238. 0x89, 0x0,
  239. 0x8a, 0x0,
  240. 0x8b, 0x0,
  241. 0x8c, 0x0,
  242. 0x90, 0x0,
  243. 0x91, 0x0,
  244. 0x92, 0x0,
  245. 0x93, 0x0,
  246. 0x94, 0x1c,
  247. 0x97, 0x0,
  248. 0xa0, 0x48,
  249. 0xa1, 0x0,
  250. 0xb0, 0xb8,
  251. 0xb1, 0x3a,
  252. 0xb2, 0x10,
  253. 0xb3, 0x82,
  254. 0xb4, 0x80,
  255. 0xb5, 0x82,
  256. 0xb6, 0x82,
  257. 0xb7, 0x82,
  258. 0xb8, 0x20,
  259. 0xb9, 0x0,
  260. 0xf0, 0x0,
  261. 0xf1, 0x0,
  262. 0xf2, 0xc0,
  263. 0x51, 0x36,
  264. 0x52, 0x09,
  265. 0x53, 0x94,
  266. 0x54, 0x62,
  267. 0x55, 0x29,
  268. 0x56, 0x64,
  269. 0x57, 0x2b,
  270. 0xff, 0xff,
  271. };
  272. static int stv0288_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t volt)
  273. {
  274. dprintk("%s: %s\n", __func__,
  275. volt == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
  276. volt == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
  277. return 0;
  278. }
  279. static int stv0288_init(struct dvb_frontend *fe)
  280. {
  281. struct stv0288_state *state = fe->demodulator_priv;
  282. int i;
  283. u8 reg;
  284. u8 val;
  285. dprintk("stv0288: init chip\n");
  286. stv0288_writeregI(state, 0x41, 0x04);
  287. msleep(50);
  288. /* we have default inittab */
  289. if (state->config->inittab == NULL) {
  290. for (i = 0; !(stv0288_inittab[i] == 0xff &&
  291. stv0288_inittab[i + 1] == 0xff); i += 2)
  292. stv0288_writeregI(state, stv0288_inittab[i],
  293. stv0288_inittab[i + 1]);
  294. } else {
  295. for (i = 0; ; i += 2) {
  296. reg = state->config->inittab[i];
  297. val = state->config->inittab[i+1];
  298. if (reg == 0xff && val == 0xff)
  299. break;
  300. stv0288_writeregI(state, reg, val);
  301. }
  302. }
  303. return 0;
  304. }
  305. static int stv0288_read_status(struct dvb_frontend *fe, fe_status_t *status)
  306. {
  307. struct stv0288_state *state = fe->demodulator_priv;
  308. u8 sync = stv0288_readreg(state, 0x24);
  309. if (sync == 255)
  310. sync = 0;
  311. dprintk("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, sync);
  312. *status = 0;
  313. if ((sync & 0x08) == 0x08) {
  314. *status |= FE_HAS_LOCK;
  315. dprintk("stv0288 has locked\n");
  316. }
  317. return 0;
  318. }
  319. static int stv0288_read_ber(struct dvb_frontend *fe, u32 *ber)
  320. {
  321. struct stv0288_state *state = fe->demodulator_priv;
  322. if (state->errmode != STATUS_BER)
  323. return 0;
  324. *ber = (stv0288_readreg(state, 0x26) << 8) |
  325. stv0288_readreg(state, 0x27);
  326. dprintk("stv0288_read_ber %d\n", *ber);
  327. return 0;
  328. }
  329. static int stv0288_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  330. {
  331. struct stv0288_state *state = fe->demodulator_priv;
  332. s32 signal = 0xffff - ((stv0288_readreg(state, 0x10) << 8));
  333. signal = signal * 5 / 4;
  334. *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
  335. dprintk("stv0288_read_signal_strength %d\n", *strength);
  336. return 0;
  337. }
  338. static int stv0288_sleep(struct dvb_frontend *fe)
  339. {
  340. struct stv0288_state *state = fe->demodulator_priv;
  341. stv0288_writeregI(state, 0x41, 0x84);
  342. state->initialised = 0;
  343. return 0;
  344. }
  345. static int stv0288_read_snr(struct dvb_frontend *fe, u16 *snr)
  346. {
  347. struct stv0288_state *state = fe->demodulator_priv;
  348. s32 xsnr = 0xffff - ((stv0288_readreg(state, 0x2d) << 8)
  349. | stv0288_readreg(state, 0x2e));
  350. xsnr = 3 * (xsnr - 0xa100);
  351. *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
  352. dprintk("stv0288_read_snr %d\n", *snr);
  353. return 0;
  354. }
  355. static int stv0288_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  356. {
  357. struct stv0288_state *state = fe->demodulator_priv;
  358. if (state->errmode != STATUS_BER)
  359. return 0;
  360. *ucblocks = (stv0288_readreg(state, 0x26) << 8) |
  361. stv0288_readreg(state, 0x27);
  362. dprintk("stv0288_read_ber %d\n", *ucblocks);
  363. return 0;
  364. }
  365. static int stv0288_set_property(struct dvb_frontend *fe, struct dtv_property *p)
  366. {
  367. dprintk("%s(..)\n", __func__);
  368. return 0;
  369. }
  370. static int stv0288_get_property(struct dvb_frontend *fe, struct dtv_property *p)
  371. {
  372. dprintk("%s(..)\n", __func__);
  373. return 0;
  374. }
  375. static int stv0288_set_frontend(struct dvb_frontend *fe,
  376. struct dvb_frontend_parameters *dfp)
  377. {
  378. struct stv0288_state *state = fe->demodulator_priv;
  379. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  380. char tm;
  381. unsigned char tda[3];
  382. dprintk("%s : FE_SET_FRONTEND\n", __func__);
  383. if (c->delivery_system != SYS_DVBS) {
  384. dprintk("%s: unsupported delivery "
  385. "system selected (%d)\n",
  386. __func__, c->delivery_system);
  387. return -EOPNOTSUPP;
  388. }
  389. if (state->config->set_ts_params)
  390. state->config->set_ts_params(fe, 0);
  391. /* only frequency & symbol_rate are used for tuner*/
  392. dfp->frequency = c->frequency;
  393. dfp->u.qpsk.symbol_rate = c->symbol_rate;
  394. if (fe->ops.tuner_ops.set_params) {
  395. fe->ops.tuner_ops.set_params(fe, dfp);
  396. if (fe->ops.i2c_gate_ctrl)
  397. fe->ops.i2c_gate_ctrl(fe, 0);
  398. }
  399. udelay(10);
  400. stv0288_set_symbolrate(fe, c->symbol_rate);
  401. /* Carrier lock control register */
  402. stv0288_writeregI(state, 0x15, 0xc5);
  403. tda[0] = 0x2b; /* CFRM */
  404. tda[2] = 0x0; /* CFRL */
  405. for (tm = -6; tm < 7;) {
  406. /* Viterbi status */
  407. if (stv0288_readreg(state, 0x24) & 0x80)
  408. break;
  409. tda[2] += 40;
  410. if (tda[2] < 40)
  411. tm++;
  412. tda[1] = (unsigned char)tm;
  413. stv0288_writeregI(state, 0x2b, tda[1]);
  414. stv0288_writeregI(state, 0x2c, tda[2]);
  415. udelay(30);
  416. }
  417. state->tuner_frequency = c->frequency;
  418. state->fec_inner = FEC_AUTO;
  419. state->symbol_rate = c->symbol_rate;
  420. return 0;
  421. }
  422. static int stv0288_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  423. {
  424. struct stv0288_state *state = fe->demodulator_priv;
  425. if (enable)
  426. stv0288_writeregI(state, 0x01, 0xb5);
  427. else
  428. stv0288_writeregI(state, 0x01, 0x35);
  429. udelay(1);
  430. return 0;
  431. }
  432. static void stv0288_release(struct dvb_frontend *fe)
  433. {
  434. struct stv0288_state *state = fe->demodulator_priv;
  435. kfree(state);
  436. }
  437. static struct dvb_frontend_ops stv0288_ops = {
  438. .info = {
  439. .name = "ST STV0288 DVB-S",
  440. .type = FE_QPSK,
  441. .frequency_min = 950000,
  442. .frequency_max = 2150000,
  443. .frequency_stepsize = 1000, /* kHz for QPSK frontends */
  444. .frequency_tolerance = 0,
  445. .symbol_rate_min = 1000000,
  446. .symbol_rate_max = 45000000,
  447. .symbol_rate_tolerance = 500, /* ppm */
  448. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  449. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  450. FE_CAN_QPSK |
  451. FE_CAN_FEC_AUTO
  452. },
  453. .release = stv0288_release,
  454. .init = stv0288_init,
  455. .sleep = stv0288_sleep,
  456. .write = stv0288_write,
  457. .i2c_gate_ctrl = stv0288_i2c_gate_ctrl,
  458. .read_status = stv0288_read_status,
  459. .read_ber = stv0288_read_ber,
  460. .read_signal_strength = stv0288_read_signal_strength,
  461. .read_snr = stv0288_read_snr,
  462. .read_ucblocks = stv0288_read_ucblocks,
  463. .diseqc_send_master_cmd = stv0288_send_diseqc_msg,
  464. .diseqc_send_burst = stv0288_send_diseqc_burst,
  465. .set_tone = stv0288_set_tone,
  466. .set_voltage = stv0288_set_voltage,
  467. .set_property = stv0288_set_property,
  468. .get_property = stv0288_get_property,
  469. .set_frontend = stv0288_set_frontend,
  470. };
  471. struct dvb_frontend *stv0288_attach(const struct stv0288_config *config,
  472. struct i2c_adapter *i2c)
  473. {
  474. struct stv0288_state *state = NULL;
  475. int id;
  476. /* allocate memory for the internal state */
  477. state = kmalloc(sizeof(struct stv0288_state), GFP_KERNEL);
  478. if (state == NULL)
  479. goto error;
  480. /* setup the state */
  481. state->config = config;
  482. state->i2c = i2c;
  483. state->initialised = 0;
  484. state->tuner_frequency = 0;
  485. state->symbol_rate = 0;
  486. state->fec_inner = 0;
  487. state->errmode = STATUS_BER;
  488. stv0288_writeregI(state, 0x41, 0x04);
  489. msleep(200);
  490. id = stv0288_readreg(state, 0x00);
  491. dprintk("stv0288 id %x\n", id);
  492. /* register 0x00 contains 0x11 for STV0288 */
  493. if (id != 0x11)
  494. goto error;
  495. /* create dvb_frontend */
  496. memcpy(&state->frontend.ops, &stv0288_ops,
  497. sizeof(struct dvb_frontend_ops));
  498. state->frontend.demodulator_priv = state;
  499. return &state->frontend;
  500. error:
  501. kfree(state);
  502. return NULL;
  503. }
  504. EXPORT_SYMBOL(stv0288_attach);
  505. module_param(debug_legacy_dish_switch, int, 0444);
  506. MODULE_PARM_DESC(debug_legacy_dish_switch,
  507. "Enable timing analysis for Dish Network legacy switches");
  508. module_param(debug, int, 0644);
  509. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  510. MODULE_DESCRIPTION("ST STV0288 DVB Demodulator driver");
  511. MODULE_AUTHOR("Georg Acher, Bob Liu, Igor liplianin");
  512. MODULE_LICENSE("GPL");