stv0288.c 13 KB

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