sp887x.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618
  1. /*
  2. Driver for the Spase sp887x demodulator
  3. */
  4. /*
  5. * This driver needs external firmware. Please use the command
  6. * "<kerneldir>/Documentation/dvb/get_dvb_firmware sp887x" to
  7. * download/extract it, and then copy it to /usr/lib/hotplug/firmware
  8. * or /lib/firmware (depending on configuration of firmware hotplug).
  9. */
  10. #define SP887X_DEFAULT_FIRMWARE "dvb-fe-sp887x.fw"
  11. #include <linux/init.h>
  12. #include <linux/module.h>
  13. #include <linux/device.h>
  14. #include <linux/firmware.h>
  15. #include <linux/string.h>
  16. #include <linux/slab.h>
  17. #include "dvb_frontend.h"
  18. #include "sp887x.h"
  19. struct sp887x_state {
  20. struct i2c_adapter* i2c;
  21. const struct sp887x_config* config;
  22. struct dvb_frontend frontend;
  23. /* demodulator private data */
  24. u8 initialised:1;
  25. };
  26. static int debug;
  27. #define dprintk(args...) \
  28. do { \
  29. if (debug) printk(KERN_DEBUG "sp887x: " args); \
  30. } while (0)
  31. static int i2c_writebytes (struct sp887x_state* state, u8 *buf, u8 len)
  32. {
  33. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = len };
  34. int err;
  35. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  36. printk ("%s: i2c write error (addr %02x, err == %i)\n",
  37. __func__, state->config->demod_address, err);
  38. return -EREMOTEIO;
  39. }
  40. return 0;
  41. }
  42. static int sp887x_writereg (struct sp887x_state* state, u16 reg, u16 data)
  43. {
  44. u8 b0 [] = { reg >> 8 , reg & 0xff, data >> 8, data & 0xff };
  45. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 4 };
  46. int ret;
  47. if ((ret = i2c_transfer(state->i2c, &msg, 1)) != 1) {
  48. /**
  49. * in case of soft reset we ignore ACK errors...
  50. */
  51. if (!(reg == 0xf1a && data == 0x000 &&
  52. (ret == -EREMOTEIO || ret == -EFAULT)))
  53. {
  54. printk("%s: writereg error "
  55. "(reg %03x, data %03x, ret == %i)\n",
  56. __func__, reg & 0xffff, data & 0xffff, ret);
  57. return ret;
  58. }
  59. }
  60. return 0;
  61. }
  62. static int sp887x_readreg (struct sp887x_state* state, u16 reg)
  63. {
  64. u8 b0 [] = { reg >> 8 , reg & 0xff };
  65. u8 b1 [2];
  66. int ret;
  67. struct i2c_msg msg[] = {{ .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 2 },
  68. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 2 }};
  69. if ((ret = i2c_transfer(state->i2c, msg, 2)) != 2) {
  70. printk("%s: readreg error (ret == %i)\n", __func__, ret);
  71. return -1;
  72. }
  73. return (((b1[0] << 8) | b1[1]) & 0xfff);
  74. }
  75. static void sp887x_microcontroller_stop (struct sp887x_state* state)
  76. {
  77. dprintk("%s\n", __func__);
  78. sp887x_writereg(state, 0xf08, 0x000);
  79. sp887x_writereg(state, 0xf09, 0x000);
  80. /* microcontroller STOP */
  81. sp887x_writereg(state, 0xf00, 0x000);
  82. }
  83. static void sp887x_microcontroller_start (struct sp887x_state* state)
  84. {
  85. dprintk("%s\n", __func__);
  86. sp887x_writereg(state, 0xf08, 0x000);
  87. sp887x_writereg(state, 0xf09, 0x000);
  88. /* microcontroller START */
  89. sp887x_writereg(state, 0xf00, 0x001);
  90. }
  91. static void sp887x_setup_agc (struct sp887x_state* state)
  92. {
  93. /* setup AGC parameters */
  94. dprintk("%s\n", __func__);
  95. sp887x_writereg(state, 0x33c, 0x054);
  96. sp887x_writereg(state, 0x33b, 0x04c);
  97. sp887x_writereg(state, 0x328, 0x000);
  98. sp887x_writereg(state, 0x327, 0x005);
  99. sp887x_writereg(state, 0x326, 0x001);
  100. sp887x_writereg(state, 0x325, 0x001);
  101. sp887x_writereg(state, 0x324, 0x001);
  102. sp887x_writereg(state, 0x318, 0x050);
  103. sp887x_writereg(state, 0x317, 0x3fe);
  104. sp887x_writereg(state, 0x316, 0x001);
  105. sp887x_writereg(state, 0x313, 0x005);
  106. sp887x_writereg(state, 0x312, 0x002);
  107. sp887x_writereg(state, 0x306, 0x000);
  108. sp887x_writereg(state, 0x303, 0x000);
  109. }
  110. #define BLOCKSIZE 30
  111. #define FW_SIZE 0x4000
  112. /**
  113. * load firmware and setup MPEG interface...
  114. */
  115. static int sp887x_initial_setup (struct dvb_frontend* fe, const struct firmware *fw)
  116. {
  117. struct sp887x_state* state = fe->demodulator_priv;
  118. u8 buf [BLOCKSIZE+2];
  119. int i;
  120. int fw_size = fw->size;
  121. const unsigned char *mem = fw->data;
  122. dprintk("%s\n", __func__);
  123. /* ignore the first 10 bytes, then we expect 0x4000 bytes of firmware */
  124. if (fw_size < FW_SIZE+10)
  125. return -ENODEV;
  126. mem = fw->data + 10;
  127. /* soft reset */
  128. sp887x_writereg(state, 0xf1a, 0x000);
  129. sp887x_microcontroller_stop (state);
  130. printk ("%s: firmware upload... ", __func__);
  131. /* setup write pointer to -1 (end of memory) */
  132. /* bit 0x8000 in address is set to enable 13bit mode */
  133. sp887x_writereg(state, 0x8f08, 0x1fff);
  134. /* dummy write (wrap around to start of memory) */
  135. sp887x_writereg(state, 0x8f0a, 0x0000);
  136. for (i = 0; i < FW_SIZE; i += BLOCKSIZE) {
  137. int c = BLOCKSIZE;
  138. int err;
  139. if (i+c > FW_SIZE)
  140. c = FW_SIZE - i;
  141. /* bit 0x8000 in address is set to enable 13bit mode */
  142. /* bit 0x4000 enables multibyte read/write transfers */
  143. /* write register is 0xf0a */
  144. buf[0] = 0xcf;
  145. buf[1] = 0x0a;
  146. memcpy(&buf[2], mem + i, c);
  147. if ((err = i2c_writebytes (state, buf, c+2)) < 0) {
  148. printk ("failed.\n");
  149. printk ("%s: i2c error (err == %i)\n", __func__, err);
  150. return err;
  151. }
  152. }
  153. /* don't write RS bytes between packets */
  154. sp887x_writereg(state, 0xc13, 0x001);
  155. /* suppress clock if (!data_valid) */
  156. sp887x_writereg(state, 0xc14, 0x000);
  157. /* setup MPEG interface... */
  158. sp887x_writereg(state, 0xc1a, 0x872);
  159. sp887x_writereg(state, 0xc1b, 0x001);
  160. sp887x_writereg(state, 0xc1c, 0x000); /* parallel mode (serial mode == 1) */
  161. sp887x_writereg(state, 0xc1a, 0x871);
  162. /* ADC mode, 2 for MT8872, 3 for SP8870/SP8871 */
  163. sp887x_writereg(state, 0x301, 0x002);
  164. sp887x_setup_agc(state);
  165. /* bit 0x010: enable data valid signal */
  166. sp887x_writereg(state, 0xd00, 0x010);
  167. sp887x_writereg(state, 0x0d1, 0x000);
  168. return 0;
  169. };
  170. static int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05)
  171. {
  172. int known_parameters = 1;
  173. *reg0xc05 = 0x000;
  174. switch (p->u.ofdm.constellation) {
  175. case QPSK:
  176. break;
  177. case QAM_16:
  178. *reg0xc05 |= (1 << 10);
  179. break;
  180. case QAM_64:
  181. *reg0xc05 |= (2 << 10);
  182. break;
  183. case QAM_AUTO:
  184. known_parameters = 0;
  185. break;
  186. default:
  187. return -EINVAL;
  188. };
  189. switch (p->u.ofdm.hierarchy_information) {
  190. case HIERARCHY_NONE:
  191. break;
  192. case HIERARCHY_1:
  193. *reg0xc05 |= (1 << 7);
  194. break;
  195. case HIERARCHY_2:
  196. *reg0xc05 |= (2 << 7);
  197. break;
  198. case HIERARCHY_4:
  199. *reg0xc05 |= (3 << 7);
  200. break;
  201. case HIERARCHY_AUTO:
  202. known_parameters = 0;
  203. break;
  204. default:
  205. return -EINVAL;
  206. };
  207. switch (p->u.ofdm.code_rate_HP) {
  208. case FEC_1_2:
  209. break;
  210. case FEC_2_3:
  211. *reg0xc05 |= (1 << 3);
  212. break;
  213. case FEC_3_4:
  214. *reg0xc05 |= (2 << 3);
  215. break;
  216. case FEC_5_6:
  217. *reg0xc05 |= (3 << 3);
  218. break;
  219. case FEC_7_8:
  220. *reg0xc05 |= (4 << 3);
  221. break;
  222. case FEC_AUTO:
  223. known_parameters = 0;
  224. break;
  225. default:
  226. return -EINVAL;
  227. };
  228. if (known_parameters)
  229. *reg0xc05 |= (2 << 1); /* use specified parameters */
  230. else
  231. *reg0xc05 |= (1 << 1); /* enable autoprobing */
  232. return 0;
  233. }
  234. /**
  235. * estimates division of two 24bit numbers,
  236. * derived from the ves1820/stv0299 driver code
  237. */
  238. static void divide (int n, int d, int *quotient_i, int *quotient_f)
  239. {
  240. unsigned int q, r;
  241. r = (n % d) << 8;
  242. q = (r / d);
  243. if (quotient_i)
  244. *quotient_i = q;
  245. if (quotient_f) {
  246. r = (r % d) << 8;
  247. q = (q << 8) | (r / d);
  248. r = (r % d) << 8;
  249. *quotient_f = (q << 8) | (r / d);
  250. }
  251. }
  252. static void sp887x_correct_offsets (struct sp887x_state* state,
  253. struct dvb_frontend_parameters *p,
  254. int actual_freq)
  255. {
  256. static const u32 srate_correction [] = { 1879617, 4544878, 8098561 };
  257. int bw_index = p->u.ofdm.bandwidth - BANDWIDTH_8_MHZ;
  258. int freq_offset = actual_freq - p->frequency;
  259. int sysclock = 61003; //[kHz]
  260. int ifreq = 36000000;
  261. int freq;
  262. int frequency_shift;
  263. if (p->inversion == INVERSION_ON)
  264. freq = ifreq - freq_offset;
  265. else
  266. freq = ifreq + freq_offset;
  267. divide(freq / 333, sysclock, NULL, &frequency_shift);
  268. if (p->inversion == INVERSION_ON)
  269. frequency_shift = -frequency_shift;
  270. /* sample rate correction */
  271. sp887x_writereg(state, 0x319, srate_correction[bw_index] >> 12);
  272. sp887x_writereg(state, 0x31a, srate_correction[bw_index] & 0xfff);
  273. /* carrier offset correction */
  274. sp887x_writereg(state, 0x309, frequency_shift >> 12);
  275. sp887x_writereg(state, 0x30a, frequency_shift & 0xfff);
  276. }
  277. static int sp887x_setup_frontend_parameters (struct dvb_frontend* fe,
  278. struct dvb_frontend_parameters *p)
  279. {
  280. struct sp887x_state* state = fe->demodulator_priv;
  281. unsigned actual_freq;
  282. int err;
  283. u16 val, reg0xc05;
  284. if (p->u.ofdm.bandwidth != BANDWIDTH_8_MHZ &&
  285. p->u.ofdm.bandwidth != BANDWIDTH_7_MHZ &&
  286. p->u.ofdm.bandwidth != BANDWIDTH_6_MHZ)
  287. return -EINVAL;
  288. if ((err = configure_reg0xc05(p, &reg0xc05)))
  289. return err;
  290. sp887x_microcontroller_stop(state);
  291. /* setup the PLL */
  292. if (fe->ops.tuner_ops.set_params) {
  293. fe->ops.tuner_ops.set_params(fe, p);
  294. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  295. }
  296. if (fe->ops.tuner_ops.get_frequency) {
  297. fe->ops.tuner_ops.get_frequency(fe, &actual_freq);
  298. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  299. } else {
  300. actual_freq = p->frequency;
  301. }
  302. /* read status reg in order to clear <pending irqs */
  303. sp887x_readreg(state, 0x200);
  304. sp887x_correct_offsets(state, p, actual_freq);
  305. /* filter for 6/7/8 Mhz channel */
  306. if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
  307. val = 2;
  308. else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
  309. val = 1;
  310. else
  311. val = 0;
  312. sp887x_writereg(state, 0x311, val);
  313. /* scan order: 2k first = 0, 8k first = 1 */
  314. if (p->u.ofdm.transmission_mode == TRANSMISSION_MODE_2K)
  315. sp887x_writereg(state, 0x338, 0x000);
  316. else
  317. sp887x_writereg(state, 0x338, 0x001);
  318. sp887x_writereg(state, 0xc05, reg0xc05);
  319. if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
  320. val = 2 << 3;
  321. else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
  322. val = 3 << 3;
  323. else
  324. val = 0 << 3;
  325. /* enable OFDM and SAW bits as lock indicators in sync register 0xf17,
  326. * optimize algorithm for given bandwidth...
  327. */
  328. sp887x_writereg(state, 0xf14, 0x160 | val);
  329. sp887x_writereg(state, 0xf15, 0x000);
  330. sp887x_microcontroller_start(state);
  331. return 0;
  332. }
  333. static int sp887x_read_status(struct dvb_frontend* fe, fe_status_t* status)
  334. {
  335. struct sp887x_state* state = fe->demodulator_priv;
  336. u16 snr12 = sp887x_readreg(state, 0xf16);
  337. u16 sync0x200 = sp887x_readreg(state, 0x200);
  338. u16 sync0xf17 = sp887x_readreg(state, 0xf17);
  339. *status = 0;
  340. if (snr12 > 0x00f)
  341. *status |= FE_HAS_SIGNAL;
  342. //if (sync0x200 & 0x004)
  343. // *status |= FE_HAS_SYNC | FE_HAS_CARRIER;
  344. //if (sync0x200 & 0x008)
  345. // *status |= FE_HAS_VITERBI;
  346. if ((sync0xf17 & 0x00f) == 0x002) {
  347. *status |= FE_HAS_LOCK;
  348. *status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_CARRIER;
  349. }
  350. if (sync0x200 & 0x001) { /* tuner adjustment requested...*/
  351. int steps = (sync0x200 >> 4) & 0x00f;
  352. if (steps & 0x008)
  353. steps = -steps;
  354. dprintk("sp887x: implement tuner adjustment (%+i steps)!!\n",
  355. steps);
  356. }
  357. return 0;
  358. }
  359. static int sp887x_read_ber(struct dvb_frontend* fe, u32* ber)
  360. {
  361. struct sp887x_state* state = fe->demodulator_priv;
  362. *ber = (sp887x_readreg(state, 0xc08) & 0x3f) |
  363. (sp887x_readreg(state, 0xc07) << 6);
  364. sp887x_writereg(state, 0xc08, 0x000);
  365. sp887x_writereg(state, 0xc07, 0x000);
  366. if (*ber >= 0x3fff0)
  367. *ber = ~0;
  368. return 0;
  369. }
  370. static int sp887x_read_signal_strength(struct dvb_frontend* fe, u16* strength)
  371. {
  372. struct sp887x_state* state = fe->demodulator_priv;
  373. u16 snr12 = sp887x_readreg(state, 0xf16);
  374. u32 signal = 3 * (snr12 << 4);
  375. *strength = (signal < 0xffff) ? signal : 0xffff;
  376. return 0;
  377. }
  378. static int sp887x_read_snr(struct dvb_frontend* fe, u16* snr)
  379. {
  380. struct sp887x_state* state = fe->demodulator_priv;
  381. u16 snr12 = sp887x_readreg(state, 0xf16);
  382. *snr = (snr12 << 4) | (snr12 >> 8);
  383. return 0;
  384. }
  385. static int sp887x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
  386. {
  387. struct sp887x_state* state = fe->demodulator_priv;
  388. *ucblocks = sp887x_readreg(state, 0xc0c);
  389. if (*ucblocks == 0xfff)
  390. *ucblocks = ~0;
  391. return 0;
  392. }
  393. static int sp887x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  394. {
  395. struct sp887x_state* state = fe->demodulator_priv;
  396. if (enable) {
  397. return sp887x_writereg(state, 0x206, 0x001);
  398. } else {
  399. return sp887x_writereg(state, 0x206, 0x000);
  400. }
  401. }
  402. static int sp887x_sleep(struct dvb_frontend* fe)
  403. {
  404. struct sp887x_state* state = fe->demodulator_priv;
  405. /* tristate TS output and disable interface pins */
  406. sp887x_writereg(state, 0xc18, 0x000);
  407. return 0;
  408. }
  409. static int sp887x_init(struct dvb_frontend* fe)
  410. {
  411. struct sp887x_state* state = fe->demodulator_priv;
  412. const struct firmware *fw = NULL;
  413. int ret;
  414. if (!state->initialised) {
  415. /* request the firmware, this will block until someone uploads it */
  416. printk("sp887x: waiting for firmware upload (%s)...\n", SP887X_DEFAULT_FIRMWARE);
  417. ret = state->config->request_firmware(fe, &fw, SP887X_DEFAULT_FIRMWARE);
  418. if (ret) {
  419. printk("sp887x: no firmware upload (timeout or file not found?)\n");
  420. return ret;
  421. }
  422. ret = sp887x_initial_setup(fe, fw);
  423. release_firmware(fw);
  424. if (ret) {
  425. printk("sp887x: writing firmware to device failed\n");
  426. return ret;
  427. }
  428. printk("sp887x: firmware upload complete\n");
  429. state->initialised = 1;
  430. }
  431. /* enable TS output and interface pins */
  432. sp887x_writereg(state, 0xc18, 0x00d);
  433. return 0;
  434. }
  435. static int sp887x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  436. {
  437. fesettings->min_delay_ms = 350;
  438. fesettings->step_size = 166666*2;
  439. fesettings->max_drift = (166666*2)+1;
  440. return 0;
  441. }
  442. static void sp887x_release(struct dvb_frontend* fe)
  443. {
  444. struct sp887x_state* state = fe->demodulator_priv;
  445. kfree(state);
  446. }
  447. static struct dvb_frontend_ops sp887x_ops;
  448. struct dvb_frontend* sp887x_attach(const struct sp887x_config* config,
  449. struct i2c_adapter* i2c)
  450. {
  451. struct sp887x_state* state = NULL;
  452. /* allocate memory for the internal state */
  453. state = kzalloc(sizeof(struct sp887x_state), GFP_KERNEL);
  454. if (state == NULL) goto error;
  455. /* setup the state */
  456. state->config = config;
  457. state->i2c = i2c;
  458. state->initialised = 0;
  459. /* check if the demod is there */
  460. if (sp887x_readreg(state, 0x0200) < 0) goto error;
  461. /* create dvb_frontend */
  462. memcpy(&state->frontend.ops, &sp887x_ops, sizeof(struct dvb_frontend_ops));
  463. state->frontend.demodulator_priv = state;
  464. return &state->frontend;
  465. error:
  466. kfree(state);
  467. return NULL;
  468. }
  469. static struct dvb_frontend_ops sp887x_ops = {
  470. .info = {
  471. .name = "Spase SP887x DVB-T",
  472. .type = FE_OFDM,
  473. .frequency_min = 50500000,
  474. .frequency_max = 858000000,
  475. .frequency_stepsize = 166666,
  476. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
  477. FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  478. FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 |
  479. FE_CAN_RECOVER
  480. },
  481. .release = sp887x_release,
  482. .init = sp887x_init,
  483. .sleep = sp887x_sleep,
  484. .i2c_gate_ctrl = sp887x_i2c_gate_ctrl,
  485. .set_frontend = sp887x_setup_frontend_parameters,
  486. .get_tune_settings = sp887x_get_tune_settings,
  487. .read_status = sp887x_read_status,
  488. .read_ber = sp887x_read_ber,
  489. .read_signal_strength = sp887x_read_signal_strength,
  490. .read_snr = sp887x_read_snr,
  491. .read_ucblocks = sp887x_read_ucblocks,
  492. };
  493. module_param(debug, int, 0644);
  494. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  495. MODULE_DESCRIPTION("Spase sp887x DVB-T demodulator driver");
  496. MODULE_LICENSE("GPL");
  497. EXPORT_SYMBOL(sp887x_attach);