stv0288.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. stv0288_writeregI(state, 0x22, 0);
  106. stv0288_writeregI(state, 0x23, 0);
  107. stv0288_writeregI(state, 0x2b, 0xff);
  108. stv0288_writeregI(state, 0x2c, 0xf7);
  109. temp = (unsigned int)srate / 1000;
  110. temp = temp * 32768;
  111. temp = temp / 25;
  112. temp = temp / 125;
  113. b[0] = (unsigned char)((temp >> 12) & 0xff);
  114. b[1] = (unsigned char)((temp >> 4) & 0xff);
  115. b[2] = (unsigned char)((temp << 4) & 0xf0);
  116. stv0288_writeregI(state, 0x28, 0x80); /* SFRH */
  117. stv0288_writeregI(state, 0x29, 0); /* SFRM */
  118. stv0288_writeregI(state, 0x2a, 0); /* SFRL */
  119. stv0288_writeregI(state, 0x28, b[0]);
  120. stv0288_writeregI(state, 0x29, b[1]);
  121. stv0288_writeregI(state, 0x2a, b[2]);
  122. dprintk("stv0288: stv0288_set_symbolrate\n");
  123. return 0;
  124. }
  125. static int stv0288_send_diseqc_msg(struct dvb_frontend *fe,
  126. struct dvb_diseqc_master_cmd *m)
  127. {
  128. struct stv0288_state *state = fe->demodulator_priv;
  129. int i;
  130. dprintk("%s\n", __func__);
  131. stv0288_writeregI(state, 0x09, 0);
  132. msleep(30);
  133. stv0288_writeregI(state, 0x05, 0x12);/* modulated mode, single shot */
  134. for (i = 0; i < m->msg_len; i++) {
  135. if (stv0288_writeregI(state, 0x06, m->msg[i]))
  136. return -EREMOTEIO;
  137. }
  138. msleep(m->msg_len*12);
  139. return 0;
  140. }
  141. static int stv0288_send_diseqc_burst(struct dvb_frontend *fe,
  142. fe_sec_mini_cmd_t burst)
  143. {
  144. struct stv0288_state *state = fe->demodulator_priv;
  145. dprintk("%s\n", __func__);
  146. if (stv0288_writeregI(state, 0x05, 0x03))/* burst mode, single shot */
  147. return -EREMOTEIO;
  148. if (stv0288_writeregI(state, 0x06, burst == SEC_MINI_A ? 0x00 : 0xff))
  149. return -EREMOTEIO;
  150. msleep(15);
  151. if (stv0288_writeregI(state, 0x05, 0x12))
  152. return -EREMOTEIO;
  153. return 0;
  154. }
  155. static int stv0288_set_tone(struct dvb_frontend *fe, fe_sec_tone_mode_t tone)
  156. {
  157. struct stv0288_state *state = fe->demodulator_priv;
  158. switch (tone) {
  159. case SEC_TONE_ON:
  160. if (stv0288_writeregI(state, 0x05, 0x10))/* cont carrier */
  161. return -EREMOTEIO;
  162. break;
  163. case SEC_TONE_OFF:
  164. if (stv0288_writeregI(state, 0x05, 0x12))/* burst mode off*/
  165. return -EREMOTEIO;
  166. break;
  167. default:
  168. return -EINVAL;
  169. }
  170. return 0;
  171. }
  172. static u8 stv0288_inittab[] = {
  173. 0x01, 0x15,
  174. 0x02, 0x20,
  175. 0x09, 0x0,
  176. 0x0a, 0x4,
  177. 0x0b, 0x0,
  178. 0x0c, 0x0,
  179. 0x0d, 0x0,
  180. 0x0e, 0xd4,
  181. 0x0f, 0x30,
  182. 0x11, 0x80,
  183. 0x12, 0x03,
  184. 0x13, 0x48,
  185. 0x14, 0x84,
  186. 0x15, 0x45,
  187. 0x16, 0xb7,
  188. 0x17, 0x9c,
  189. 0x18, 0x0,
  190. 0x19, 0xa6,
  191. 0x1a, 0x88,
  192. 0x1b, 0x8f,
  193. 0x1c, 0xf0,
  194. 0x20, 0x0b,
  195. 0x21, 0x54,
  196. 0x22, 0x0,
  197. 0x23, 0x0,
  198. 0x2b, 0xff,
  199. 0x2c, 0xf7,
  200. 0x30, 0x0,
  201. 0x31, 0x1e,
  202. 0x32, 0x14,
  203. 0x33, 0x0f,
  204. 0x34, 0x09,
  205. 0x35, 0x0c,
  206. 0x36, 0x05,
  207. 0x37, 0x2f,
  208. 0x38, 0x16,
  209. 0x39, 0xbe,
  210. 0x3a, 0x0,
  211. 0x3b, 0x13,
  212. 0x3c, 0x11,
  213. 0x3d, 0x30,
  214. 0x40, 0x63,
  215. 0x41, 0x04,
  216. 0x42, 0x20,
  217. 0x43, 0x00,
  218. 0x44, 0x00,
  219. 0x45, 0x00,
  220. 0x46, 0x00,
  221. 0x47, 0x00,
  222. 0x4a, 0x00,
  223. 0x50, 0x10,
  224. 0x51, 0x38,
  225. 0x52, 0x21,
  226. 0x58, 0x54,
  227. 0x59, 0x86,
  228. 0x5a, 0x0,
  229. 0x5b, 0x9b,
  230. 0x5c, 0x08,
  231. 0x5d, 0x7f,
  232. 0x5e, 0x0,
  233. 0x5f, 0xff,
  234. 0x70, 0x0,
  235. 0x71, 0x0,
  236. 0x72, 0x0,
  237. 0x74, 0x0,
  238. 0x75, 0x0,
  239. 0x76, 0x0,
  240. 0x81, 0x0,
  241. 0x82, 0x3f,
  242. 0x83, 0x3f,
  243. 0x84, 0x0,
  244. 0x85, 0x0,
  245. 0x88, 0x0,
  246. 0x89, 0x0,
  247. 0x8a, 0x0,
  248. 0x8b, 0x0,
  249. 0x8c, 0x0,
  250. 0x90, 0x0,
  251. 0x91, 0x0,
  252. 0x92, 0x0,
  253. 0x93, 0x0,
  254. 0x94, 0x1c,
  255. 0x97, 0x0,
  256. 0xa0, 0x48,
  257. 0xa1, 0x0,
  258. 0xb0, 0xb8,
  259. 0xb1, 0x3a,
  260. 0xb2, 0x10,
  261. 0xb3, 0x82,
  262. 0xb4, 0x80,
  263. 0xb5, 0x82,
  264. 0xb6, 0x82,
  265. 0xb7, 0x82,
  266. 0xb8, 0x20,
  267. 0xb9, 0x0,
  268. 0xf0, 0x0,
  269. 0xf1, 0x0,
  270. 0xf2, 0xc0,
  271. 0x51, 0x36,
  272. 0x52, 0x09,
  273. 0x53, 0x94,
  274. 0x54, 0x62,
  275. 0x55, 0x29,
  276. 0x56, 0x64,
  277. 0x57, 0x2b,
  278. 0xff, 0xff,
  279. };
  280. static int stv0288_set_voltage(struct dvb_frontend *fe, fe_sec_voltage_t volt)
  281. {
  282. dprintk("%s: %s\n", __func__,
  283. volt == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
  284. volt == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
  285. return 0;
  286. }
  287. static int stv0288_init(struct dvb_frontend *fe)
  288. {
  289. struct stv0288_state *state = fe->demodulator_priv;
  290. int i;
  291. u8 reg;
  292. u8 val;
  293. dprintk("stv0288: init chip\n");
  294. stv0288_writeregI(state, 0x41, 0x04);
  295. msleep(50);
  296. /* we have default inittab */
  297. if (state->config->inittab == NULL) {
  298. for (i = 0; !(stv0288_inittab[i] == 0xff &&
  299. stv0288_inittab[i + 1] == 0xff); i += 2)
  300. stv0288_writeregI(state, stv0288_inittab[i],
  301. stv0288_inittab[i + 1]);
  302. } else {
  303. for (i = 0; ; i += 2) {
  304. reg = state->config->inittab[i];
  305. val = state->config->inittab[i+1];
  306. if (reg == 0xff && val == 0xff)
  307. break;
  308. stv0288_writeregI(state, reg, val);
  309. }
  310. }
  311. return 0;
  312. }
  313. static int stv0288_read_status(struct dvb_frontend *fe, fe_status_t *status)
  314. {
  315. struct stv0288_state *state = fe->demodulator_priv;
  316. u8 sync = stv0288_readreg(state, 0x24);
  317. if (sync == 255)
  318. sync = 0;
  319. dprintk("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, sync);
  320. *status = 0;
  321. if (sync & 0x80)
  322. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  323. if (sync & 0x10)
  324. *status |= FE_HAS_VITERBI;
  325. if (sync & 0x08) {
  326. *status |= FE_HAS_LOCK;
  327. dprintk("stv0288 has locked\n");
  328. }
  329. return 0;
  330. }
  331. static int stv0288_read_ber(struct dvb_frontend *fe, u32 *ber)
  332. {
  333. struct stv0288_state *state = fe->demodulator_priv;
  334. if (state->errmode != STATUS_BER)
  335. return 0;
  336. *ber = (stv0288_readreg(state, 0x26) << 8) |
  337. stv0288_readreg(state, 0x27);
  338. dprintk("stv0288_read_ber %d\n", *ber);
  339. return 0;
  340. }
  341. static int stv0288_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  342. {
  343. struct stv0288_state *state = fe->demodulator_priv;
  344. s32 signal = 0xffff - ((stv0288_readreg(state, 0x10) << 8));
  345. signal = signal * 5 / 4;
  346. *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
  347. dprintk("stv0288_read_signal_strength %d\n", *strength);
  348. return 0;
  349. }
  350. static int stv0288_sleep(struct dvb_frontend *fe)
  351. {
  352. struct stv0288_state *state = fe->demodulator_priv;
  353. stv0288_writeregI(state, 0x41, 0x84);
  354. state->initialised = 0;
  355. return 0;
  356. }
  357. static int stv0288_read_snr(struct dvb_frontend *fe, u16 *snr)
  358. {
  359. struct stv0288_state *state = fe->demodulator_priv;
  360. s32 xsnr = 0xffff - ((stv0288_readreg(state, 0x2d) << 8)
  361. | stv0288_readreg(state, 0x2e));
  362. xsnr = 3 * (xsnr - 0xa100);
  363. *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
  364. dprintk("stv0288_read_snr %d\n", *snr);
  365. return 0;
  366. }
  367. static int stv0288_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  368. {
  369. struct stv0288_state *state = fe->demodulator_priv;
  370. if (state->errmode != STATUS_BER)
  371. return 0;
  372. *ucblocks = (stv0288_readreg(state, 0x26) << 8) |
  373. stv0288_readreg(state, 0x27);
  374. dprintk("stv0288_read_ber %d\n", *ucblocks);
  375. return 0;
  376. }
  377. static int stv0288_set_property(struct dvb_frontend *fe, struct dtv_property *p)
  378. {
  379. dprintk("%s(..)\n", __func__);
  380. return 0;
  381. }
  382. static int stv0288_get_property(struct dvb_frontend *fe, struct dtv_property *p)
  383. {
  384. dprintk("%s(..)\n", __func__);
  385. return 0;
  386. }
  387. static int stv0288_set_frontend(struct dvb_frontend *fe,
  388. struct dvb_frontend_parameters *dfp)
  389. {
  390. struct stv0288_state *state = fe->demodulator_priv;
  391. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  392. char tm;
  393. unsigned char tda[3];
  394. u8 reg, time_out = 0;
  395. dprintk("%s : FE_SET_FRONTEND\n", __func__);
  396. if (c->delivery_system != SYS_DVBS) {
  397. dprintk("%s: unsupported delivery "
  398. "system selected (%d)\n",
  399. __func__, c->delivery_system);
  400. return -EOPNOTSUPP;
  401. }
  402. if (state->config->set_ts_params)
  403. state->config->set_ts_params(fe, 0);
  404. /* only frequency & symbol_rate are used for tuner*/
  405. dfp->frequency = c->frequency;
  406. dfp->u.qpsk.symbol_rate = c->symbol_rate;
  407. if (fe->ops.tuner_ops.set_params) {
  408. fe->ops.tuner_ops.set_params(fe);
  409. if (fe->ops.i2c_gate_ctrl)
  410. fe->ops.i2c_gate_ctrl(fe, 0);
  411. }
  412. udelay(10);
  413. stv0288_set_symbolrate(fe, c->symbol_rate);
  414. /* Carrier lock control register */
  415. stv0288_writeregI(state, 0x15, 0xc5);
  416. tda[2] = 0x0; /* CFRL */
  417. for (tm = -9; tm < 7;) {
  418. /* Viterbi status */
  419. reg = stv0288_readreg(state, 0x24);
  420. if (reg & 0x8)
  421. break;
  422. if (reg & 0x80) {
  423. time_out++;
  424. if (time_out > 10)
  425. break;
  426. tda[2] += 40;
  427. if (tda[2] < 40)
  428. tm++;
  429. } else {
  430. tm++;
  431. tda[2] = 0;
  432. time_out = 0;
  433. }
  434. tda[1] = (unsigned char)tm;
  435. stv0288_writeregI(state, 0x2b, tda[1]);
  436. stv0288_writeregI(state, 0x2c, tda[2]);
  437. udelay(30);
  438. }
  439. state->tuner_frequency = c->frequency;
  440. state->fec_inner = FEC_AUTO;
  441. state->symbol_rate = c->symbol_rate;
  442. return 0;
  443. }
  444. static int stv0288_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  445. {
  446. struct stv0288_state *state = fe->demodulator_priv;
  447. if (enable)
  448. stv0288_writeregI(state, 0x01, 0xb5);
  449. else
  450. stv0288_writeregI(state, 0x01, 0x35);
  451. udelay(1);
  452. return 0;
  453. }
  454. static void stv0288_release(struct dvb_frontend *fe)
  455. {
  456. struct stv0288_state *state = fe->demodulator_priv;
  457. kfree(state);
  458. }
  459. static struct dvb_frontend_ops stv0288_ops = {
  460. .info = {
  461. .name = "ST STV0288 DVB-S",
  462. .type = FE_QPSK,
  463. .frequency_min = 950000,
  464. .frequency_max = 2150000,
  465. .frequency_stepsize = 1000, /* kHz for QPSK frontends */
  466. .frequency_tolerance = 0,
  467. .symbol_rate_min = 1000000,
  468. .symbol_rate_max = 45000000,
  469. .symbol_rate_tolerance = 500, /* ppm */
  470. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  471. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  472. FE_CAN_QPSK |
  473. FE_CAN_FEC_AUTO
  474. },
  475. .release = stv0288_release,
  476. .init = stv0288_init,
  477. .sleep = stv0288_sleep,
  478. .write = stv0288_write,
  479. .i2c_gate_ctrl = stv0288_i2c_gate_ctrl,
  480. .read_status = stv0288_read_status,
  481. .read_ber = stv0288_read_ber,
  482. .read_signal_strength = stv0288_read_signal_strength,
  483. .read_snr = stv0288_read_snr,
  484. .read_ucblocks = stv0288_read_ucblocks,
  485. .diseqc_send_master_cmd = stv0288_send_diseqc_msg,
  486. .diseqc_send_burst = stv0288_send_diseqc_burst,
  487. .set_tone = stv0288_set_tone,
  488. .set_voltage = stv0288_set_voltage,
  489. .set_property = stv0288_set_property,
  490. .get_property = stv0288_get_property,
  491. .set_frontend_legacy = stv0288_set_frontend,
  492. };
  493. struct dvb_frontend *stv0288_attach(const struct stv0288_config *config,
  494. struct i2c_adapter *i2c)
  495. {
  496. struct stv0288_state *state = NULL;
  497. int id;
  498. /* allocate memory for the internal state */
  499. state = kzalloc(sizeof(struct stv0288_state), GFP_KERNEL);
  500. if (state == NULL)
  501. goto error;
  502. /* setup the state */
  503. state->config = config;
  504. state->i2c = i2c;
  505. state->initialised = 0;
  506. state->tuner_frequency = 0;
  507. state->symbol_rate = 0;
  508. state->fec_inner = 0;
  509. state->errmode = STATUS_BER;
  510. stv0288_writeregI(state, 0x41, 0x04);
  511. msleep(200);
  512. id = stv0288_readreg(state, 0x00);
  513. dprintk("stv0288 id %x\n", id);
  514. /* register 0x00 contains 0x11 for STV0288 */
  515. if (id != 0x11)
  516. goto error;
  517. /* create dvb_frontend */
  518. memcpy(&state->frontend.ops, &stv0288_ops,
  519. sizeof(struct dvb_frontend_ops));
  520. state->frontend.demodulator_priv = state;
  521. return &state->frontend;
  522. error:
  523. kfree(state);
  524. return NULL;
  525. }
  526. EXPORT_SYMBOL(stv0288_attach);
  527. module_param(debug_legacy_dish_switch, int, 0444);
  528. MODULE_PARM_DESC(debug_legacy_dish_switch,
  529. "Enable timing analysis for Dish Network legacy switches");
  530. module_param(debug, int, 0644);
  531. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  532. MODULE_DESCRIPTION("ST STV0288 DVB Demodulator driver");
  533. MODULE_AUTHOR("Georg Acher, Bob Liu, Igor liplianin");
  534. MODULE_LICENSE("GPL");