sp8870.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  1. /*
  2. Driver for Spase SP8870 demodulator
  3. Copyright (C) 1999 Juergen Peitz
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. /*
  17. * This driver needs external firmware. Please use the command
  18. * "<kerneldir>/Documentation/dvb/get_dvb_firmware alps_tdlb7" to
  19. * download/extract it, and then copy it to /usr/lib/hotplug/firmware
  20. * or /lib/firmware (depending on configuration of firmware hotplug).
  21. */
  22. #define SP8870_DEFAULT_FIRMWARE "dvb-fe-sp8870.fw"
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/device.h>
  26. #include <linux/firmware.h>
  27. #include <linux/delay.h>
  28. #include <linux/string.h>
  29. #include <linux/slab.h>
  30. #include "dvb_frontend.h"
  31. #include "sp8870.h"
  32. struct sp8870_state {
  33. struct i2c_adapter* i2c;
  34. const struct sp8870_config* config;
  35. struct dvb_frontend frontend;
  36. /* demodulator private data */
  37. u8 initialised:1;
  38. };
  39. static int debug;
  40. #define dprintk(args...) \
  41. do { \
  42. if (debug) printk(KERN_DEBUG "sp8870: " args); \
  43. } while (0)
  44. /* firmware size for sp8870 */
  45. #define SP8870_FIRMWARE_SIZE 16382
  46. /* starting point for firmware in file 'Sc_main.mc' */
  47. #define SP8870_FIRMWARE_OFFSET 0x0A
  48. static int sp8870_writereg (struct sp8870_state* state, u16 reg, u16 data)
  49. {
  50. u8 buf [] = { reg >> 8, reg & 0xff, data >> 8, data & 0xff };
  51. struct i2c_msg msg = { .addr = state->config->demod_address, .flags = 0, .buf = buf, .len = 4 };
  52. int err;
  53. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  54. dprintk ("%s: writereg error (err == %i, reg == 0x%02x, data == 0x%02x)\n", __func__, err, reg, data);
  55. return -EREMOTEIO;
  56. }
  57. return 0;
  58. }
  59. static int sp8870_readreg (struct sp8870_state* state, u16 reg)
  60. {
  61. int ret;
  62. u8 b0 [] = { reg >> 8 , reg & 0xff };
  63. u8 b1 [] = { 0, 0 };
  64. struct i2c_msg msg [] = { { .addr = state->config->demod_address, .flags = 0, .buf = b0, .len = 2 },
  65. { .addr = state->config->demod_address, .flags = I2C_M_RD, .buf = b1, .len = 2 } };
  66. ret = i2c_transfer (state->i2c, msg, 2);
  67. if (ret != 2) {
  68. dprintk("%s: readreg error (ret == %i)\n", __func__, ret);
  69. return -1;
  70. }
  71. return (b1[0] << 8 | b1[1]);
  72. }
  73. static int sp8870_firmware_upload (struct sp8870_state* state, const struct firmware *fw)
  74. {
  75. struct i2c_msg msg;
  76. const char *fw_buf = fw->data;
  77. int fw_pos;
  78. u8 tx_buf[255];
  79. int tx_len;
  80. int err = 0;
  81. dprintk ("%s: ...\n", __func__);
  82. if (fw->size < SP8870_FIRMWARE_SIZE + SP8870_FIRMWARE_OFFSET)
  83. return -EINVAL;
  84. // system controller stop
  85. sp8870_writereg(state, 0x0F00, 0x0000);
  86. // instruction RAM register hiword
  87. sp8870_writereg(state, 0x8F08, ((SP8870_FIRMWARE_SIZE / 2) & 0xFFFF));
  88. // instruction RAM MWR
  89. sp8870_writereg(state, 0x8F0A, ((SP8870_FIRMWARE_SIZE / 2) >> 16));
  90. // do firmware upload
  91. fw_pos = SP8870_FIRMWARE_OFFSET;
  92. while (fw_pos < SP8870_FIRMWARE_SIZE + SP8870_FIRMWARE_OFFSET){
  93. tx_len = (fw_pos <= SP8870_FIRMWARE_SIZE + SP8870_FIRMWARE_OFFSET - 252) ? 252 : SP8870_FIRMWARE_SIZE + SP8870_FIRMWARE_OFFSET - fw_pos;
  94. // write register 0xCF0A
  95. tx_buf[0] = 0xCF;
  96. tx_buf[1] = 0x0A;
  97. memcpy(&tx_buf[2], fw_buf + fw_pos, tx_len);
  98. msg.addr = state->config->demod_address;
  99. msg.flags = 0;
  100. msg.buf = tx_buf;
  101. msg.len = tx_len + 2;
  102. if ((err = i2c_transfer (state->i2c, &msg, 1)) != 1) {
  103. printk("%s: firmware upload failed!\n", __func__);
  104. printk ("%s: i2c error (err == %i)\n", __func__, err);
  105. return err;
  106. }
  107. fw_pos += tx_len;
  108. }
  109. dprintk ("%s: done!\n", __func__);
  110. return 0;
  111. };
  112. static void sp8870_microcontroller_stop (struct sp8870_state* state)
  113. {
  114. sp8870_writereg(state, 0x0F08, 0x000);
  115. sp8870_writereg(state, 0x0F09, 0x000);
  116. // microcontroller STOP
  117. sp8870_writereg(state, 0x0F00, 0x000);
  118. }
  119. static void sp8870_microcontroller_start (struct sp8870_state* state)
  120. {
  121. sp8870_writereg(state, 0x0F08, 0x000);
  122. sp8870_writereg(state, 0x0F09, 0x000);
  123. // microcontroller START
  124. sp8870_writereg(state, 0x0F00, 0x001);
  125. // not documented but if we don't read 0x0D01 out here
  126. // we don't get a correct data valid signal
  127. sp8870_readreg(state, 0x0D01);
  128. }
  129. static int sp8870_read_data_valid_signal(struct sp8870_state* state)
  130. {
  131. return (sp8870_readreg(state, 0x0D02) > 0);
  132. }
  133. static int configure_reg0xc05 (struct dvb_frontend_parameters *p, u16 *reg0xc05)
  134. {
  135. int known_parameters = 1;
  136. *reg0xc05 = 0x000;
  137. switch (p->u.ofdm.constellation) {
  138. case QPSK:
  139. break;
  140. case QAM_16:
  141. *reg0xc05 |= (1 << 10);
  142. break;
  143. case QAM_64:
  144. *reg0xc05 |= (2 << 10);
  145. break;
  146. case QAM_AUTO:
  147. known_parameters = 0;
  148. break;
  149. default:
  150. return -EINVAL;
  151. };
  152. switch (p->u.ofdm.hierarchy_information) {
  153. case HIERARCHY_NONE:
  154. break;
  155. case HIERARCHY_1:
  156. *reg0xc05 |= (1 << 7);
  157. break;
  158. case HIERARCHY_2:
  159. *reg0xc05 |= (2 << 7);
  160. break;
  161. case HIERARCHY_4:
  162. *reg0xc05 |= (3 << 7);
  163. break;
  164. case HIERARCHY_AUTO:
  165. known_parameters = 0;
  166. break;
  167. default:
  168. return -EINVAL;
  169. };
  170. switch (p->u.ofdm.code_rate_HP) {
  171. case FEC_1_2:
  172. break;
  173. case FEC_2_3:
  174. *reg0xc05 |= (1 << 3);
  175. break;
  176. case FEC_3_4:
  177. *reg0xc05 |= (2 << 3);
  178. break;
  179. case FEC_5_6:
  180. *reg0xc05 |= (3 << 3);
  181. break;
  182. case FEC_7_8:
  183. *reg0xc05 |= (4 << 3);
  184. break;
  185. case FEC_AUTO:
  186. known_parameters = 0;
  187. break;
  188. default:
  189. return -EINVAL;
  190. };
  191. if (known_parameters)
  192. *reg0xc05 |= (2 << 1); /* use specified parameters */
  193. else
  194. *reg0xc05 |= (1 << 1); /* enable autoprobing */
  195. return 0;
  196. }
  197. static int sp8870_wake_up(struct sp8870_state* state)
  198. {
  199. // enable TS output and interface pins
  200. return sp8870_writereg(state, 0xC18, 0x00D);
  201. }
  202. static int sp8870_set_frontend_parameters (struct dvb_frontend* fe,
  203. struct dvb_frontend_parameters *p)
  204. {
  205. struct sp8870_state* state = fe->demodulator_priv;
  206. int err;
  207. u16 reg0xc05;
  208. if ((err = configure_reg0xc05(p, &reg0xc05)))
  209. return err;
  210. // system controller stop
  211. sp8870_microcontroller_stop(state);
  212. // set tuner parameters
  213. if (fe->ops.tuner_ops.set_params) {
  214. fe->ops.tuner_ops.set_params(fe, p);
  215. if (fe->ops.i2c_gate_ctrl) fe->ops.i2c_gate_ctrl(fe, 0);
  216. }
  217. // sample rate correction bit [23..17]
  218. sp8870_writereg(state, 0x0319, 0x000A);
  219. // sample rate correction bit [16..0]
  220. sp8870_writereg(state, 0x031A, 0x0AAB);
  221. // integer carrier offset
  222. sp8870_writereg(state, 0x0309, 0x0400);
  223. // fractional carrier offset
  224. sp8870_writereg(state, 0x030A, 0x0000);
  225. // filter for 6/7/8 Mhz channel
  226. if (p->u.ofdm.bandwidth == BANDWIDTH_6_MHZ)
  227. sp8870_writereg(state, 0x0311, 0x0002);
  228. else if (p->u.ofdm.bandwidth == BANDWIDTH_7_MHZ)
  229. sp8870_writereg(state, 0x0311, 0x0001);
  230. else
  231. sp8870_writereg(state, 0x0311, 0x0000);
  232. // scan order: 2k first = 0x0000, 8k first = 0x0001
  233. if (p->u.ofdm.transmission_mode == TRANSMISSION_MODE_2K)
  234. sp8870_writereg(state, 0x0338, 0x0000);
  235. else
  236. sp8870_writereg(state, 0x0338, 0x0001);
  237. sp8870_writereg(state, 0xc05, reg0xc05);
  238. // read status reg in order to clear pending irqs
  239. sp8870_readreg(state, 0x200);
  240. // system controller start
  241. sp8870_microcontroller_start(state);
  242. return 0;
  243. }
  244. static int sp8870_init (struct dvb_frontend* fe)
  245. {
  246. struct sp8870_state* state = fe->demodulator_priv;
  247. const struct firmware *fw = NULL;
  248. sp8870_wake_up(state);
  249. if (state->initialised) return 0;
  250. state->initialised = 1;
  251. dprintk ("%s\n", __func__);
  252. /* request the firmware, this will block until someone uploads it */
  253. printk("sp8870: waiting for firmware upload (%s)...\n", SP8870_DEFAULT_FIRMWARE);
  254. if (state->config->request_firmware(fe, &fw, SP8870_DEFAULT_FIRMWARE)) {
  255. printk("sp8870: no firmware upload (timeout or file not found?)\n");
  256. return -EIO;
  257. }
  258. if (sp8870_firmware_upload(state, fw)) {
  259. printk("sp8870: writing firmware to device failed\n");
  260. release_firmware(fw);
  261. return -EIO;
  262. }
  263. release_firmware(fw);
  264. printk("sp8870: firmware upload complete\n");
  265. /* enable TS output and interface pins */
  266. sp8870_writereg(state, 0xc18, 0x00d);
  267. // system controller stop
  268. sp8870_microcontroller_stop(state);
  269. // ADC mode
  270. sp8870_writereg(state, 0x0301, 0x0003);
  271. // Reed Solomon parity bytes passed to output
  272. sp8870_writereg(state, 0x0C13, 0x0001);
  273. // MPEG clock is suppressed if no valid data
  274. sp8870_writereg(state, 0x0C14, 0x0001);
  275. /* bit 0x010: enable data valid signal */
  276. sp8870_writereg(state, 0x0D00, 0x010);
  277. sp8870_writereg(state, 0x0D01, 0x000);
  278. return 0;
  279. }
  280. static int sp8870_read_status (struct dvb_frontend* fe, fe_status_t * fe_status)
  281. {
  282. struct sp8870_state* state = fe->demodulator_priv;
  283. int status;
  284. int signal;
  285. *fe_status = 0;
  286. status = sp8870_readreg (state, 0x0200);
  287. if (status < 0)
  288. return -EIO;
  289. signal = sp8870_readreg (state, 0x0303);
  290. if (signal < 0)
  291. return -EIO;
  292. if (signal > 0x0F)
  293. *fe_status |= FE_HAS_SIGNAL;
  294. if (status & 0x08)
  295. *fe_status |= FE_HAS_SYNC;
  296. if (status & 0x04)
  297. *fe_status |= FE_HAS_LOCK | FE_HAS_CARRIER | FE_HAS_VITERBI;
  298. return 0;
  299. }
  300. static int sp8870_read_ber (struct dvb_frontend* fe, u32 * ber)
  301. {
  302. struct sp8870_state* state = fe->demodulator_priv;
  303. int ret;
  304. u32 tmp;
  305. *ber = 0;
  306. ret = sp8870_readreg(state, 0xC08);
  307. if (ret < 0)
  308. return -EIO;
  309. tmp = ret & 0x3F;
  310. ret = sp8870_readreg(state, 0xC07);
  311. if (ret < 0)
  312. return -EIO;
  313. tmp = ret << 6;
  314. if (tmp >= 0x3FFF0)
  315. tmp = ~0;
  316. *ber = tmp;
  317. return 0;
  318. }
  319. static int sp8870_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
  320. {
  321. struct sp8870_state* state = fe->demodulator_priv;
  322. int ret;
  323. u16 tmp;
  324. *signal = 0;
  325. ret = sp8870_readreg (state, 0x306);
  326. if (ret < 0)
  327. return -EIO;
  328. tmp = ret << 8;
  329. ret = sp8870_readreg (state, 0x303);
  330. if (ret < 0)
  331. return -EIO;
  332. tmp |= ret;
  333. if (tmp)
  334. *signal = 0xFFFF - tmp;
  335. return 0;
  336. }
  337. static int sp8870_read_uncorrected_blocks (struct dvb_frontend* fe, u32* ublocks)
  338. {
  339. struct sp8870_state* state = fe->demodulator_priv;
  340. int ret;
  341. *ublocks = 0;
  342. ret = sp8870_readreg(state, 0xC0C);
  343. if (ret < 0)
  344. return -EIO;
  345. if (ret == 0xFFFF)
  346. ret = ~0;
  347. *ublocks = ret;
  348. return 0;
  349. }
  350. /* number of trials to recover from lockup */
  351. #define MAXTRIALS 5
  352. /* maximum checks for data valid signal */
  353. #define MAXCHECKS 100
  354. /* only for debugging: counter for detected lockups */
  355. static int lockups;
  356. /* only for debugging: counter for channel switches */
  357. static int switches;
  358. static int sp8870_set_frontend (struct dvb_frontend* fe, struct dvb_frontend_parameters *p)
  359. {
  360. struct sp8870_state* state = fe->demodulator_priv;
  361. /*
  362. The firmware of the sp8870 sometimes locks up after setting frontend parameters.
  363. We try to detect this by checking the data valid signal.
  364. If it is not set after MAXCHECKS we try to recover the lockup by setting
  365. the frontend parameters again.
  366. */
  367. int err = 0;
  368. int valid = 0;
  369. int trials = 0;
  370. int check_count = 0;
  371. dprintk("%s: frequency = %i\n", __func__, p->frequency);
  372. for (trials = 1; trials <= MAXTRIALS; trials++) {
  373. if ((err = sp8870_set_frontend_parameters(fe, p)))
  374. return err;
  375. for (check_count = 0; check_count < MAXCHECKS; check_count++) {
  376. // valid = ((sp8870_readreg(i2c, 0x0200) & 4) == 0);
  377. valid = sp8870_read_data_valid_signal(state);
  378. if (valid) {
  379. dprintk("%s: delay = %i usec\n",
  380. __func__, check_count * 10);
  381. break;
  382. }
  383. udelay(10);
  384. }
  385. if (valid)
  386. break;
  387. }
  388. if (!valid) {
  389. printk("%s: firmware crash!!!!!!\n", __func__);
  390. return -EIO;
  391. }
  392. if (debug) {
  393. if (valid) {
  394. if (trials > 1) {
  395. printk("%s: firmware lockup!!!\n", __func__);
  396. printk("%s: recovered after %i trial(s))\n", __func__, trials - 1);
  397. lockups++;
  398. }
  399. }
  400. switches++;
  401. printk("%s: switches = %i lockups = %i\n", __func__, switches, lockups);
  402. }
  403. return 0;
  404. }
  405. static int sp8870_sleep(struct dvb_frontend* fe)
  406. {
  407. struct sp8870_state* state = fe->demodulator_priv;
  408. // tristate TS output and disable interface pins
  409. return sp8870_writereg(state, 0xC18, 0x000);
  410. }
  411. static int sp8870_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  412. {
  413. fesettings->min_delay_ms = 350;
  414. fesettings->step_size = 0;
  415. fesettings->max_drift = 0;
  416. return 0;
  417. }
  418. static int sp8870_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  419. {
  420. struct sp8870_state* state = fe->demodulator_priv;
  421. if (enable) {
  422. return sp8870_writereg(state, 0x206, 0x001);
  423. } else {
  424. return sp8870_writereg(state, 0x206, 0x000);
  425. }
  426. }
  427. static void sp8870_release(struct dvb_frontend* fe)
  428. {
  429. struct sp8870_state* state = fe->demodulator_priv;
  430. kfree(state);
  431. }
  432. static struct dvb_frontend_ops sp8870_ops;
  433. struct dvb_frontend* sp8870_attach(const struct sp8870_config* config,
  434. struct i2c_adapter* i2c)
  435. {
  436. struct sp8870_state* state = NULL;
  437. /* allocate memory for the internal state */
  438. state = kzalloc(sizeof(struct sp8870_state), GFP_KERNEL);
  439. if (state == NULL) goto error;
  440. /* setup the state */
  441. state->config = config;
  442. state->i2c = i2c;
  443. state->initialised = 0;
  444. /* check if the demod is there */
  445. if (sp8870_readreg(state, 0x0200) < 0) goto error;
  446. /* create dvb_frontend */
  447. memcpy(&state->frontend.ops, &sp8870_ops, sizeof(struct dvb_frontend_ops));
  448. state->frontend.demodulator_priv = state;
  449. return &state->frontend;
  450. error:
  451. kfree(state);
  452. return NULL;
  453. }
  454. static struct dvb_frontend_ops sp8870_ops = {
  455. .info = {
  456. .name = "Spase SP8870 DVB-T",
  457. .type = FE_OFDM,
  458. .frequency_min = 470000000,
  459. .frequency_max = 860000000,
  460. .frequency_stepsize = 166666,
  461. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
  462. FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 |
  463. FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  464. FE_CAN_QPSK | FE_CAN_QAM_16 |
  465. FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  466. FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER
  467. },
  468. .release = sp8870_release,
  469. .init = sp8870_init,
  470. .sleep = sp8870_sleep,
  471. .i2c_gate_ctrl = sp8870_i2c_gate_ctrl,
  472. .set_frontend = sp8870_set_frontend,
  473. .get_tune_settings = sp8870_get_tune_settings,
  474. .read_status = sp8870_read_status,
  475. .read_ber = sp8870_read_ber,
  476. .read_signal_strength = sp8870_read_signal_strength,
  477. .read_ucblocks = sp8870_read_uncorrected_blocks,
  478. };
  479. module_param(debug, int, 0644);
  480. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  481. MODULE_DESCRIPTION("Spase SP8870 DVB-T Demodulator driver");
  482. MODULE_AUTHOR("Juergen Peitz");
  483. MODULE_LICENSE("GPL");
  484. EXPORT_SYMBOL(sp8870_attach);