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 dtv_frontend_properties *p, u16 *reg0xc05)
  134. {
  135. int known_parameters = 1;
  136. *reg0xc05 = 0x000;
  137. switch (p->modulation) {
  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->hierarchy) {
  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->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. {
  204. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  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);
  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->bandwidth_hz == 6000000)
  227. sp8870_writereg(state, 0x0311, 0x0002);
  228. else if (p->bandwidth_hz == 7000000)
  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->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,
  281. enum fe_status *fe_status)
  282. {
  283. struct sp8870_state* state = fe->demodulator_priv;
  284. int status;
  285. int signal;
  286. *fe_status = 0;
  287. status = sp8870_readreg (state, 0x0200);
  288. if (status < 0)
  289. return -EIO;
  290. signal = sp8870_readreg (state, 0x0303);
  291. if (signal < 0)
  292. return -EIO;
  293. if (signal > 0x0F)
  294. *fe_status |= FE_HAS_SIGNAL;
  295. if (status & 0x08)
  296. *fe_status |= FE_HAS_SYNC;
  297. if (status & 0x04)
  298. *fe_status |= FE_HAS_LOCK | FE_HAS_CARRIER | FE_HAS_VITERBI;
  299. return 0;
  300. }
  301. static int sp8870_read_ber (struct dvb_frontend* fe, u32 * ber)
  302. {
  303. struct sp8870_state* state = fe->demodulator_priv;
  304. int ret;
  305. u32 tmp;
  306. *ber = 0;
  307. ret = sp8870_readreg(state, 0xC08);
  308. if (ret < 0)
  309. return -EIO;
  310. tmp = ret & 0x3F;
  311. ret = sp8870_readreg(state, 0xC07);
  312. if (ret < 0)
  313. return -EIO;
  314. tmp = ret << 6;
  315. if (tmp >= 0x3FFF0)
  316. tmp = ~0;
  317. *ber = tmp;
  318. return 0;
  319. }
  320. static int sp8870_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
  321. {
  322. struct sp8870_state* state = fe->demodulator_priv;
  323. int ret;
  324. u16 tmp;
  325. *signal = 0;
  326. ret = sp8870_readreg (state, 0x306);
  327. if (ret < 0)
  328. return -EIO;
  329. tmp = ret << 8;
  330. ret = sp8870_readreg (state, 0x303);
  331. if (ret < 0)
  332. return -EIO;
  333. tmp |= ret;
  334. if (tmp)
  335. *signal = 0xFFFF - tmp;
  336. return 0;
  337. }
  338. static int sp8870_read_uncorrected_blocks (struct dvb_frontend* fe, u32* ublocks)
  339. {
  340. struct sp8870_state* state = fe->demodulator_priv;
  341. int ret;
  342. *ublocks = 0;
  343. ret = sp8870_readreg(state, 0xC0C);
  344. if (ret < 0)
  345. return -EIO;
  346. if (ret == 0xFFFF)
  347. ret = ~0;
  348. *ublocks = ret;
  349. return 0;
  350. }
  351. /* number of trials to recover from lockup */
  352. #define MAXTRIALS 5
  353. /* maximum checks for data valid signal */
  354. #define MAXCHECKS 100
  355. /* only for debugging: counter for detected lockups */
  356. static int lockups;
  357. /* only for debugging: counter for channel switches */
  358. static int switches;
  359. static int sp8870_set_frontend(struct dvb_frontend *fe)
  360. {
  361. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  362. struct sp8870_state* state = fe->demodulator_priv;
  363. /*
  364. The firmware of the sp8870 sometimes locks up after setting frontend parameters.
  365. We try to detect this by checking the data valid signal.
  366. If it is not set after MAXCHECKS we try to recover the lockup by setting
  367. the frontend parameters again.
  368. */
  369. int err = 0;
  370. int valid = 0;
  371. int trials = 0;
  372. int check_count = 0;
  373. dprintk("%s: frequency = %i\n", __func__, p->frequency);
  374. for (trials = 1; trials <= MAXTRIALS; trials++) {
  375. err = sp8870_set_frontend_parameters(fe);
  376. if (err)
  377. return err;
  378. for (check_count = 0; check_count < MAXCHECKS; check_count++) {
  379. // valid = ((sp8870_readreg(i2c, 0x0200) & 4) == 0);
  380. valid = sp8870_read_data_valid_signal(state);
  381. if (valid) {
  382. dprintk("%s: delay = %i usec\n",
  383. __func__, check_count * 10);
  384. break;
  385. }
  386. udelay(10);
  387. }
  388. if (valid)
  389. break;
  390. }
  391. if (!valid) {
  392. printk("%s: firmware crash!!!!!!\n", __func__);
  393. return -EIO;
  394. }
  395. if (debug) {
  396. if (valid) {
  397. if (trials > 1) {
  398. printk("%s: firmware lockup!!!\n", __func__);
  399. printk("%s: recovered after %i trial(s))\n", __func__, trials - 1);
  400. lockups++;
  401. }
  402. }
  403. switches++;
  404. printk("%s: switches = %i lockups = %i\n", __func__, switches, lockups);
  405. }
  406. return 0;
  407. }
  408. static int sp8870_sleep(struct dvb_frontend* fe)
  409. {
  410. struct sp8870_state* state = fe->demodulator_priv;
  411. // tristate TS output and disable interface pins
  412. return sp8870_writereg(state, 0xC18, 0x000);
  413. }
  414. static int sp8870_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
  415. {
  416. fesettings->min_delay_ms = 350;
  417. fesettings->step_size = 0;
  418. fesettings->max_drift = 0;
  419. return 0;
  420. }
  421. static int sp8870_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
  422. {
  423. struct sp8870_state* state = fe->demodulator_priv;
  424. if (enable) {
  425. return sp8870_writereg(state, 0x206, 0x001);
  426. } else {
  427. return sp8870_writereg(state, 0x206, 0x000);
  428. }
  429. }
  430. static void sp8870_release(struct dvb_frontend* fe)
  431. {
  432. struct sp8870_state* state = fe->demodulator_priv;
  433. kfree(state);
  434. }
  435. static struct dvb_frontend_ops sp8870_ops;
  436. struct dvb_frontend* sp8870_attach(const struct sp8870_config* config,
  437. struct i2c_adapter* i2c)
  438. {
  439. struct sp8870_state* state = NULL;
  440. /* allocate memory for the internal state */
  441. state = kzalloc(sizeof(struct sp8870_state), GFP_KERNEL);
  442. if (state == NULL) goto error;
  443. /* setup the state */
  444. state->config = config;
  445. state->i2c = i2c;
  446. state->initialised = 0;
  447. /* check if the demod is there */
  448. if (sp8870_readreg(state, 0x0200) < 0) goto error;
  449. /* create dvb_frontend */
  450. memcpy(&state->frontend.ops, &sp8870_ops, sizeof(struct dvb_frontend_ops));
  451. state->frontend.demodulator_priv = state;
  452. return &state->frontend;
  453. error:
  454. kfree(state);
  455. return NULL;
  456. }
  457. static struct dvb_frontend_ops sp8870_ops = {
  458. .delsys = { SYS_DVBT },
  459. .info = {
  460. .name = "Spase SP8870 DVB-T",
  461. .frequency_min = 470000000,
  462. .frequency_max = 860000000,
  463. .frequency_stepsize = 166666,
  464. .caps = FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
  465. FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 |
  466. FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
  467. FE_CAN_QPSK | FE_CAN_QAM_16 |
  468. FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
  469. FE_CAN_HIERARCHY_AUTO | FE_CAN_RECOVER
  470. },
  471. .release = sp8870_release,
  472. .init = sp8870_init,
  473. .sleep = sp8870_sleep,
  474. .i2c_gate_ctrl = sp8870_i2c_gate_ctrl,
  475. .set_frontend = sp8870_set_frontend,
  476. .get_tune_settings = sp8870_get_tune_settings,
  477. .read_status = sp8870_read_status,
  478. .read_ber = sp8870_read_ber,
  479. .read_signal_strength = sp8870_read_signal_strength,
  480. .read_ucblocks = sp8870_read_uncorrected_blocks,
  481. };
  482. module_param(debug, int, 0644);
  483. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  484. MODULE_DESCRIPTION("Spase SP8870 DVB-T Demodulator driver");
  485. MODULE_AUTHOR("Juergen Peitz");
  486. MODULE_LICENSE("GPL");
  487. EXPORT_SYMBOL(sp8870_attach);