stv0288.c 14 KB

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