stv0288.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. enum fe_code_rate 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. enum fe_sec_mini_cmd 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, enum fe_sec_tone_mode 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,
  281. enum fe_sec_voltage volt)
  282. {
  283. dprintk("%s: %s\n", __func__,
  284. volt == SEC_VOLTAGE_13 ? "SEC_VOLTAGE_13" :
  285. volt == SEC_VOLTAGE_18 ? "SEC_VOLTAGE_18" : "??");
  286. return 0;
  287. }
  288. static int stv0288_init(struct dvb_frontend *fe)
  289. {
  290. struct stv0288_state *state = fe->demodulator_priv;
  291. int i;
  292. u8 reg;
  293. u8 val;
  294. dprintk("stv0288: init chip\n");
  295. stv0288_writeregI(state, 0x41, 0x04);
  296. msleep(50);
  297. /* we have default inittab */
  298. if (state->config->inittab == NULL) {
  299. for (i = 0; !(stv0288_inittab[i] == 0xff &&
  300. stv0288_inittab[i + 1] == 0xff); i += 2)
  301. stv0288_writeregI(state, stv0288_inittab[i],
  302. stv0288_inittab[i + 1]);
  303. } else {
  304. for (i = 0; ; i += 2) {
  305. reg = state->config->inittab[i];
  306. val = state->config->inittab[i+1];
  307. if (reg == 0xff && val == 0xff)
  308. break;
  309. stv0288_writeregI(state, reg, val);
  310. }
  311. }
  312. return 0;
  313. }
  314. static int stv0288_read_status(struct dvb_frontend *fe, enum fe_status *status)
  315. {
  316. struct stv0288_state *state = fe->demodulator_priv;
  317. u8 sync = stv0288_readreg(state, 0x24);
  318. if (sync == 255)
  319. sync = 0;
  320. dprintk("%s : FE_READ_STATUS : VSTATUS: 0x%02x\n", __func__, sync);
  321. *status = 0;
  322. if (sync & 0x80)
  323. *status |= FE_HAS_CARRIER | FE_HAS_SIGNAL;
  324. if (sync & 0x10)
  325. *status |= FE_HAS_VITERBI;
  326. if (sync & 0x08) {
  327. *status |= FE_HAS_LOCK;
  328. dprintk("stv0288 has locked\n");
  329. }
  330. return 0;
  331. }
  332. static int stv0288_read_ber(struct dvb_frontend *fe, u32 *ber)
  333. {
  334. struct stv0288_state *state = fe->demodulator_priv;
  335. if (state->errmode != STATUS_BER)
  336. return 0;
  337. *ber = (stv0288_readreg(state, 0x26) << 8) |
  338. stv0288_readreg(state, 0x27);
  339. dprintk("stv0288_read_ber %d\n", *ber);
  340. return 0;
  341. }
  342. static int stv0288_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
  343. {
  344. struct stv0288_state *state = fe->demodulator_priv;
  345. s32 signal = 0xffff - ((stv0288_readreg(state, 0x10) << 8));
  346. signal = signal * 5 / 4;
  347. *strength = (signal > 0xffff) ? 0xffff : (signal < 0) ? 0 : signal;
  348. dprintk("stv0288_read_signal_strength %d\n", *strength);
  349. return 0;
  350. }
  351. static int stv0288_sleep(struct dvb_frontend *fe)
  352. {
  353. struct stv0288_state *state = fe->demodulator_priv;
  354. stv0288_writeregI(state, 0x41, 0x84);
  355. state->initialised = 0;
  356. return 0;
  357. }
  358. static int stv0288_read_snr(struct dvb_frontend *fe, u16 *snr)
  359. {
  360. struct stv0288_state *state = fe->demodulator_priv;
  361. s32 xsnr = 0xffff - ((stv0288_readreg(state, 0x2d) << 8)
  362. | stv0288_readreg(state, 0x2e));
  363. xsnr = 3 * (xsnr - 0xa100);
  364. *snr = (xsnr > 0xffff) ? 0xffff : (xsnr < 0) ? 0 : xsnr;
  365. dprintk("stv0288_read_snr %d\n", *snr);
  366. return 0;
  367. }
  368. static int stv0288_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
  369. {
  370. struct stv0288_state *state = fe->demodulator_priv;
  371. if (state->errmode != STATUS_BER)
  372. return 0;
  373. *ucblocks = (stv0288_readreg(state, 0x26) << 8) |
  374. stv0288_readreg(state, 0x27);
  375. dprintk("stv0288_read_ber %d\n", *ucblocks);
  376. return 0;
  377. }
  378. static int stv0288_set_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. {
  385. struct stv0288_state *state = fe->demodulator_priv;
  386. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  387. char tm;
  388. unsigned char tda[3];
  389. u8 reg, time_out = 0;
  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. if (fe->ops.tuner_ops.set_params) {
  401. fe->ops.tuner_ops.set_params(fe);
  402. if (fe->ops.i2c_gate_ctrl)
  403. fe->ops.i2c_gate_ctrl(fe, 0);
  404. }
  405. udelay(10);
  406. stv0288_set_symbolrate(fe, c->symbol_rate);
  407. /* Carrier lock control register */
  408. stv0288_writeregI(state, 0x15, 0xc5);
  409. tda[2] = 0x0; /* CFRL */
  410. for (tm = -9; tm < 7;) {
  411. /* Viterbi status */
  412. reg = stv0288_readreg(state, 0x24);
  413. if (reg & 0x8)
  414. break;
  415. if (reg & 0x80) {
  416. time_out++;
  417. if (time_out > 10)
  418. break;
  419. tda[2] += 40;
  420. if (tda[2] < 40)
  421. tm++;
  422. } else {
  423. tm++;
  424. tda[2] = 0;
  425. time_out = 0;
  426. }
  427. tda[1] = (unsigned char)tm;
  428. stv0288_writeregI(state, 0x2b, tda[1]);
  429. stv0288_writeregI(state, 0x2c, tda[2]);
  430. msleep(30);
  431. }
  432. state->tuner_frequency = c->frequency;
  433. state->fec_inner = FEC_AUTO;
  434. state->symbol_rate = c->symbol_rate;
  435. return 0;
  436. }
  437. static int stv0288_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  438. {
  439. struct stv0288_state *state = fe->demodulator_priv;
  440. if (enable)
  441. stv0288_writeregI(state, 0x01, 0xb5);
  442. else
  443. stv0288_writeregI(state, 0x01, 0x35);
  444. udelay(1);
  445. return 0;
  446. }
  447. static void stv0288_release(struct dvb_frontend *fe)
  448. {
  449. struct stv0288_state *state = fe->demodulator_priv;
  450. kfree(state);
  451. }
  452. static struct dvb_frontend_ops stv0288_ops = {
  453. .delsys = { SYS_DVBS },
  454. .info = {
  455. .name = "ST STV0288 DVB-S",
  456. .frequency_min = 950000,
  457. .frequency_max = 2150000,
  458. .frequency_stepsize = 1000, /* kHz for QPSK frontends */
  459. .frequency_tolerance = 0,
  460. .symbol_rate_min = 1000000,
  461. .symbol_rate_max = 45000000,
  462. .symbol_rate_tolerance = 500, /* ppm */
  463. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  464. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  465. FE_CAN_QPSK |
  466. FE_CAN_FEC_AUTO
  467. },
  468. .release = stv0288_release,
  469. .init = stv0288_init,
  470. .sleep = stv0288_sleep,
  471. .write = stv0288_write,
  472. .i2c_gate_ctrl = stv0288_i2c_gate_ctrl,
  473. .read_status = stv0288_read_status,
  474. .read_ber = stv0288_read_ber,
  475. .read_signal_strength = stv0288_read_signal_strength,
  476. .read_snr = stv0288_read_snr,
  477. .read_ucblocks = stv0288_read_ucblocks,
  478. .diseqc_send_master_cmd = stv0288_send_diseqc_msg,
  479. .diseqc_send_burst = stv0288_send_diseqc_burst,
  480. .set_tone = stv0288_set_tone,
  481. .set_voltage = stv0288_set_voltage,
  482. .set_property = stv0288_set_property,
  483. .set_frontend = stv0288_set_frontend,
  484. };
  485. struct dvb_frontend *stv0288_attach(const struct stv0288_config *config,
  486. struct i2c_adapter *i2c)
  487. {
  488. struct stv0288_state *state = NULL;
  489. int id;
  490. /* allocate memory for the internal state */
  491. state = kzalloc(sizeof(struct stv0288_state), GFP_KERNEL);
  492. if (state == NULL)
  493. goto error;
  494. /* setup the state */
  495. state->config = config;
  496. state->i2c = i2c;
  497. state->initialised = 0;
  498. state->tuner_frequency = 0;
  499. state->symbol_rate = 0;
  500. state->fec_inner = 0;
  501. state->errmode = STATUS_BER;
  502. stv0288_writeregI(state, 0x41, 0x04);
  503. msleep(200);
  504. id = stv0288_readreg(state, 0x00);
  505. dprintk("stv0288 id %x\n", id);
  506. /* register 0x00 contains 0x11 for STV0288 */
  507. if (id != 0x11)
  508. goto error;
  509. /* create dvb_frontend */
  510. memcpy(&state->frontend.ops, &stv0288_ops,
  511. sizeof(struct dvb_frontend_ops));
  512. state->frontend.demodulator_priv = state;
  513. return &state->frontend;
  514. error:
  515. kfree(state);
  516. return NULL;
  517. }
  518. EXPORT_SYMBOL(stv0288_attach);
  519. module_param(debug_legacy_dish_switch, int, 0444);
  520. MODULE_PARM_DESC(debug_legacy_dish_switch,
  521. "Enable timing analysis for Dish Network legacy switches");
  522. module_param(debug, int, 0644);
  523. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  524. MODULE_DESCRIPTION("ST STV0288 DVB Demodulator driver");
  525. MODULE_AUTHOR("Georg Acher, Bob Liu, Igor liplianin");
  526. MODULE_LICENSE("GPL");