mt312.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. /*
  2. Driver for Zarlink VP310/MT312/ZL10313 Satellite Channel Decoder
  3. Copyright (C) 2003 Andreas Oberritter <obi@linuxtv.org>
  4. Copyright (C) 2008 Matthias Schwarzott <zzam@gentoo.org>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. References:
  17. http://products.zarlink.com/product_profiles/MT312.htm
  18. http://products.zarlink.com/product_profiles/SL1935.htm
  19. */
  20. #include <linux/delay.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/kernel.h>
  24. #include <linux/module.h>
  25. #include <linux/string.h>
  26. #include <linux/slab.h>
  27. #include "dvb_frontend.h"
  28. #include "mt312_priv.h"
  29. #include "mt312.h"
  30. /* Max transfer size done by I2C transfer functions */
  31. #define MAX_XFER_SIZE 64
  32. struct mt312_state {
  33. struct i2c_adapter *i2c;
  34. /* configuration settings */
  35. const struct mt312_config *config;
  36. struct dvb_frontend frontend;
  37. u8 id;
  38. unsigned long xtal;
  39. u8 freq_mult;
  40. };
  41. static int debug;
  42. #define dprintk(args...) \
  43. do { \
  44. if (debug) \
  45. printk(KERN_DEBUG "mt312: " args); \
  46. } while (0)
  47. #define MT312_PLL_CLK 10000000UL /* 10 MHz */
  48. #define MT312_PLL_CLK_10_111 10111000UL /* 10.111 MHz */
  49. static int mt312_read(struct mt312_state *state, const enum mt312_reg_addr reg,
  50. u8 *buf, const size_t count)
  51. {
  52. int ret;
  53. struct i2c_msg msg[2];
  54. u8 regbuf[1] = { reg };
  55. msg[0].addr = state->config->demod_address;
  56. msg[0].flags = 0;
  57. msg[0].buf = regbuf;
  58. msg[0].len = 1;
  59. msg[1].addr = state->config->demod_address;
  60. msg[1].flags = I2C_M_RD;
  61. msg[1].buf = buf;
  62. msg[1].len = count;
  63. ret = i2c_transfer(state->i2c, msg, 2);
  64. if (ret != 2) {
  65. printk(KERN_DEBUG "%s: ret == %d\n", __func__, ret);
  66. return -EREMOTEIO;
  67. }
  68. if (debug) {
  69. int i;
  70. dprintk("R(%d):", reg & 0x7f);
  71. for (i = 0; i < count; i++)
  72. printk(KERN_CONT " %02x", buf[i]);
  73. printk("\n");
  74. }
  75. return 0;
  76. }
  77. static int mt312_write(struct mt312_state *state, const enum mt312_reg_addr reg,
  78. const u8 *src, const size_t count)
  79. {
  80. int ret;
  81. u8 buf[MAX_XFER_SIZE];
  82. struct i2c_msg msg;
  83. if (1 + count > sizeof(buf)) {
  84. printk(KERN_WARNING
  85. "mt312: write: len=%zu is too big!\n", count);
  86. return -EINVAL;
  87. }
  88. if (debug) {
  89. int i;
  90. dprintk("W(%d):", reg & 0x7f);
  91. for (i = 0; i < count; i++)
  92. printk(KERN_CONT " %02x", src[i]);
  93. printk("\n");
  94. }
  95. buf[0] = reg;
  96. memcpy(&buf[1], src, count);
  97. msg.addr = state->config->demod_address;
  98. msg.flags = 0;
  99. msg.buf = buf;
  100. msg.len = count + 1;
  101. ret = i2c_transfer(state->i2c, &msg, 1);
  102. if (ret != 1) {
  103. dprintk("%s: ret == %d\n", __func__, ret);
  104. return -EREMOTEIO;
  105. }
  106. return 0;
  107. }
  108. static inline int mt312_readreg(struct mt312_state *state,
  109. const enum mt312_reg_addr reg, u8 *val)
  110. {
  111. return mt312_read(state, reg, val, 1);
  112. }
  113. static inline int mt312_writereg(struct mt312_state *state,
  114. const enum mt312_reg_addr reg, const u8 val)
  115. {
  116. u8 tmp = val; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
  117. return mt312_write(state, reg, &tmp, 1);
  118. }
  119. static inline u32 mt312_div(u32 a, u32 b)
  120. {
  121. return (a + (b / 2)) / b;
  122. }
  123. static int mt312_reset(struct mt312_state *state, const u8 full)
  124. {
  125. return mt312_writereg(state, RESET, full ? 0x80 : 0x40);
  126. }
  127. static int mt312_get_inversion(struct mt312_state *state,
  128. enum fe_spectral_inversion *i)
  129. {
  130. int ret;
  131. u8 vit_mode;
  132. ret = mt312_readreg(state, VIT_MODE, &vit_mode);
  133. if (ret < 0)
  134. return ret;
  135. if (vit_mode & 0x80) /* auto inversion was used */
  136. *i = (vit_mode & 0x40) ? INVERSION_ON : INVERSION_OFF;
  137. return 0;
  138. }
  139. static int mt312_get_symbol_rate(struct mt312_state *state, u32 *sr)
  140. {
  141. int ret;
  142. u8 sym_rate_h;
  143. u8 dec_ratio;
  144. u16 sym_rat_op;
  145. u16 monitor;
  146. u8 buf[2];
  147. ret = mt312_readreg(state, SYM_RATE_H, &sym_rate_h);
  148. if (ret < 0)
  149. return ret;
  150. if (sym_rate_h & 0x80) {
  151. /* symbol rate search was used */
  152. ret = mt312_writereg(state, MON_CTRL, 0x03);
  153. if (ret < 0)
  154. return ret;
  155. ret = mt312_read(state, MONITOR_H, buf, sizeof(buf));
  156. if (ret < 0)
  157. return ret;
  158. monitor = (buf[0] << 8) | buf[1];
  159. dprintk("sr(auto) = %u\n",
  160. mt312_div(monitor * 15625, 4));
  161. } else {
  162. ret = mt312_writereg(state, MON_CTRL, 0x05);
  163. if (ret < 0)
  164. return ret;
  165. ret = mt312_read(state, MONITOR_H, buf, sizeof(buf));
  166. if (ret < 0)
  167. return ret;
  168. dec_ratio = ((buf[0] >> 5) & 0x07) * 32;
  169. ret = mt312_read(state, SYM_RAT_OP_H, buf, sizeof(buf));
  170. if (ret < 0)
  171. return ret;
  172. sym_rat_op = (buf[0] << 8) | buf[1];
  173. dprintk("sym_rat_op=%d dec_ratio=%d\n",
  174. sym_rat_op, dec_ratio);
  175. dprintk("*sr(manual) = %lu\n",
  176. (((state->xtal * 8192) / (sym_rat_op + 8192)) *
  177. 2) - dec_ratio);
  178. }
  179. return 0;
  180. }
  181. static int mt312_get_code_rate(struct mt312_state *state, enum fe_code_rate *cr)
  182. {
  183. const enum fe_code_rate fec_tab[8] =
  184. { FEC_1_2, FEC_2_3, FEC_3_4, FEC_5_6, FEC_6_7, FEC_7_8,
  185. FEC_AUTO, FEC_AUTO };
  186. int ret;
  187. u8 fec_status;
  188. ret = mt312_readreg(state, FEC_STATUS, &fec_status);
  189. if (ret < 0)
  190. return ret;
  191. *cr = fec_tab[(fec_status >> 4) & 0x07];
  192. return 0;
  193. }
  194. static int mt312_initfe(struct dvb_frontend *fe)
  195. {
  196. struct mt312_state *state = fe->demodulator_priv;
  197. int ret;
  198. u8 buf[2];
  199. /* wake up */
  200. ret = mt312_writereg(state, CONFIG,
  201. (state->freq_mult == 6 ? 0x88 : 0x8c));
  202. if (ret < 0)
  203. return ret;
  204. /* wait at least 150 usec */
  205. udelay(150);
  206. /* full reset */
  207. ret = mt312_reset(state, 1);
  208. if (ret < 0)
  209. return ret;
  210. /* Per datasheet, write correct values. 09/28/03 ACCJr.
  211. * If we don't do this, we won't get FE_HAS_VITERBI in the VP310. */
  212. {
  213. u8 buf_def[8] = { 0x14, 0x12, 0x03, 0x02,
  214. 0x01, 0x00, 0x00, 0x00 };
  215. ret = mt312_write(state, VIT_SETUP, buf_def, sizeof(buf_def));
  216. if (ret < 0)
  217. return ret;
  218. }
  219. switch (state->id) {
  220. case ID_ZL10313:
  221. /* enable ADC */
  222. ret = mt312_writereg(state, GPP_CTRL, 0x80);
  223. if (ret < 0)
  224. return ret;
  225. /* configure ZL10313 for optimal ADC performance */
  226. buf[0] = 0x80;
  227. buf[1] = 0xB0;
  228. ret = mt312_write(state, HW_CTRL, buf, 2);
  229. if (ret < 0)
  230. return ret;
  231. /* enable MPEG output and ADCs */
  232. ret = mt312_writereg(state, HW_CTRL, 0x00);
  233. if (ret < 0)
  234. return ret;
  235. ret = mt312_writereg(state, MPEG_CTRL, 0x00);
  236. if (ret < 0)
  237. return ret;
  238. break;
  239. }
  240. /* SYS_CLK */
  241. buf[0] = mt312_div(state->xtal * state->freq_mult * 2, 1000000);
  242. /* DISEQC_RATIO */
  243. buf[1] = mt312_div(state->xtal, 22000 * 4);
  244. ret = mt312_write(state, SYS_CLK, buf, sizeof(buf));
  245. if (ret < 0)
  246. return ret;
  247. ret = mt312_writereg(state, SNR_THS_HIGH, 0x32);
  248. if (ret < 0)
  249. return ret;
  250. /* different MOCLK polarity */
  251. switch (state->id) {
  252. case ID_ZL10313:
  253. buf[0] = 0x33;
  254. break;
  255. default:
  256. buf[0] = 0x53;
  257. break;
  258. }
  259. ret = mt312_writereg(state, OP_CTRL, buf[0]);
  260. if (ret < 0)
  261. return ret;
  262. /* TS_SW_LIM */
  263. buf[0] = 0x8c;
  264. buf[1] = 0x98;
  265. ret = mt312_write(state, TS_SW_LIM_L, buf, sizeof(buf));
  266. if (ret < 0)
  267. return ret;
  268. ret = mt312_writereg(state, CS_SW_LIM, 0x69);
  269. if (ret < 0)
  270. return ret;
  271. return 0;
  272. }
  273. static int mt312_send_master_cmd(struct dvb_frontend *fe,
  274. struct dvb_diseqc_master_cmd *c)
  275. {
  276. struct mt312_state *state = fe->demodulator_priv;
  277. int ret;
  278. u8 diseqc_mode;
  279. if ((c->msg_len == 0) || (c->msg_len > sizeof(c->msg)))
  280. return -EINVAL;
  281. ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
  282. if (ret < 0)
  283. return ret;
  284. ret = mt312_write(state, (0x80 | DISEQC_INSTR), c->msg, c->msg_len);
  285. if (ret < 0)
  286. return ret;
  287. ret = mt312_writereg(state, DISEQC_MODE,
  288. (diseqc_mode & 0x40) | ((c->msg_len - 1) << 3)
  289. | 0x04);
  290. if (ret < 0)
  291. return ret;
  292. /* is there a better way to wait for message to be transmitted */
  293. msleep(100);
  294. /* set DISEQC_MODE[2:0] to zero if a return message is expected */
  295. if (c->msg[0] & 0x02) {
  296. ret = mt312_writereg(state, DISEQC_MODE, (diseqc_mode & 0x40));
  297. if (ret < 0)
  298. return ret;
  299. }
  300. return 0;
  301. }
  302. static int mt312_send_burst(struct dvb_frontend *fe,
  303. const enum fe_sec_mini_cmd c)
  304. {
  305. struct mt312_state *state = fe->demodulator_priv;
  306. const u8 mini_tab[2] = { 0x02, 0x03 };
  307. int ret;
  308. u8 diseqc_mode;
  309. if (c > SEC_MINI_B)
  310. return -EINVAL;
  311. ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
  312. if (ret < 0)
  313. return ret;
  314. ret = mt312_writereg(state, DISEQC_MODE,
  315. (diseqc_mode & 0x40) | mini_tab[c]);
  316. if (ret < 0)
  317. return ret;
  318. return 0;
  319. }
  320. static int mt312_set_tone(struct dvb_frontend *fe,
  321. const enum fe_sec_tone_mode t)
  322. {
  323. struct mt312_state *state = fe->demodulator_priv;
  324. const u8 tone_tab[2] = { 0x01, 0x00 };
  325. int ret;
  326. u8 diseqc_mode;
  327. if (t > SEC_TONE_OFF)
  328. return -EINVAL;
  329. ret = mt312_readreg(state, DISEQC_MODE, &diseqc_mode);
  330. if (ret < 0)
  331. return ret;
  332. ret = mt312_writereg(state, DISEQC_MODE,
  333. (diseqc_mode & 0x40) | tone_tab[t]);
  334. if (ret < 0)
  335. return ret;
  336. return 0;
  337. }
  338. static int mt312_set_voltage(struct dvb_frontend *fe,
  339. const enum fe_sec_voltage v)
  340. {
  341. struct mt312_state *state = fe->demodulator_priv;
  342. const u8 volt_tab[3] = { 0x00, 0x40, 0x00 };
  343. u8 val;
  344. if (v > SEC_VOLTAGE_OFF)
  345. return -EINVAL;
  346. val = volt_tab[v];
  347. if (state->config->voltage_inverted)
  348. val ^= 0x40;
  349. return mt312_writereg(state, DISEQC_MODE, val);
  350. }
  351. static int mt312_read_status(struct dvb_frontend *fe, enum fe_status *s)
  352. {
  353. struct mt312_state *state = fe->demodulator_priv;
  354. int ret;
  355. u8 status[3];
  356. *s = 0;
  357. ret = mt312_read(state, QPSK_STAT_H, status, sizeof(status));
  358. if (ret < 0)
  359. return ret;
  360. dprintk("QPSK_STAT_H: 0x%02x, QPSK_STAT_L: 0x%02x,"
  361. " FEC_STATUS: 0x%02x\n", status[0], status[1], status[2]);
  362. if (status[0] & 0xc0)
  363. *s |= FE_HAS_SIGNAL; /* signal noise ratio */
  364. if (status[0] & 0x04)
  365. *s |= FE_HAS_CARRIER; /* qpsk carrier lock */
  366. if (status[2] & 0x02)
  367. *s |= FE_HAS_VITERBI; /* viterbi lock */
  368. if (status[2] & 0x04)
  369. *s |= FE_HAS_SYNC; /* byte align lock */
  370. if (status[0] & 0x01)
  371. *s |= FE_HAS_LOCK; /* qpsk lock */
  372. return 0;
  373. }
  374. static int mt312_read_ber(struct dvb_frontend *fe, u32 *ber)
  375. {
  376. struct mt312_state *state = fe->demodulator_priv;
  377. int ret;
  378. u8 buf[3];
  379. ret = mt312_read(state, RS_BERCNT_H, buf, 3);
  380. if (ret < 0)
  381. return ret;
  382. *ber = ((buf[0] << 16) | (buf[1] << 8) | buf[2]) * 64;
  383. return 0;
  384. }
  385. static int mt312_read_signal_strength(struct dvb_frontend *fe,
  386. u16 *signal_strength)
  387. {
  388. struct mt312_state *state = fe->demodulator_priv;
  389. int ret;
  390. u8 buf[3];
  391. u16 agc;
  392. s16 err_db;
  393. ret = mt312_read(state, AGC_H, buf, sizeof(buf));
  394. if (ret < 0)
  395. return ret;
  396. agc = (buf[0] << 6) | (buf[1] >> 2);
  397. err_db = (s16) (((buf[1] & 0x03) << 14) | buf[2] << 6) >> 6;
  398. *signal_strength = agc;
  399. dprintk("agc=%08x err_db=%hd\n", agc, err_db);
  400. return 0;
  401. }
  402. static int mt312_read_snr(struct dvb_frontend *fe, u16 *snr)
  403. {
  404. struct mt312_state *state = fe->demodulator_priv;
  405. int ret;
  406. u8 buf[2];
  407. ret = mt312_read(state, M_SNR_H, buf, sizeof(buf));
  408. if (ret < 0)
  409. return ret;
  410. *snr = 0xFFFF - ((((buf[0] & 0x7f) << 8) | buf[1]) << 1);
  411. return 0;
  412. }
  413. static int mt312_read_ucblocks(struct dvb_frontend *fe, u32 *ubc)
  414. {
  415. struct mt312_state *state = fe->demodulator_priv;
  416. int ret;
  417. u8 buf[2];
  418. ret = mt312_read(state, RS_UBC_H, buf, sizeof(buf));
  419. if (ret < 0)
  420. return ret;
  421. *ubc = (buf[0] << 8) | buf[1];
  422. return 0;
  423. }
  424. static int mt312_set_frontend(struct dvb_frontend *fe)
  425. {
  426. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  427. struct mt312_state *state = fe->demodulator_priv;
  428. int ret;
  429. u8 buf[5], config_val;
  430. u16 sr;
  431. const u8 fec_tab[10] =
  432. { 0x00, 0x01, 0x02, 0x04, 0x3f, 0x08, 0x10, 0x20, 0x3f, 0x3f };
  433. const u8 inv_tab[3] = { 0x00, 0x40, 0x80 };
  434. dprintk("%s: Freq %d\n", __func__, p->frequency);
  435. if ((p->frequency < fe->ops.info.frequency_min)
  436. || (p->frequency > fe->ops.info.frequency_max))
  437. return -EINVAL;
  438. if (((int)p->inversion < INVERSION_OFF)
  439. || (p->inversion > INVERSION_ON))
  440. return -EINVAL;
  441. if ((p->symbol_rate < fe->ops.info.symbol_rate_min)
  442. || (p->symbol_rate > fe->ops.info.symbol_rate_max))
  443. return -EINVAL;
  444. if (((int)p->fec_inner < FEC_NONE)
  445. || (p->fec_inner > FEC_AUTO))
  446. return -EINVAL;
  447. if ((p->fec_inner == FEC_4_5)
  448. || (p->fec_inner == FEC_8_9))
  449. return -EINVAL;
  450. switch (state->id) {
  451. case ID_VP310:
  452. /* For now we will do this only for the VP310.
  453. * It should be better for the mt312 as well,
  454. * but tuning will be slower. ACCJr 09/29/03
  455. */
  456. ret = mt312_readreg(state, CONFIG, &config_val);
  457. if (ret < 0)
  458. return ret;
  459. if (p->symbol_rate >= 30000000) {
  460. /* Note that 30MS/s should use 90MHz */
  461. if (state->freq_mult == 6) {
  462. /* We are running 60MHz */
  463. state->freq_mult = 9;
  464. ret = mt312_initfe(fe);
  465. if (ret < 0)
  466. return ret;
  467. }
  468. } else {
  469. if (state->freq_mult == 9) {
  470. /* We are running 90MHz */
  471. state->freq_mult = 6;
  472. ret = mt312_initfe(fe);
  473. if (ret < 0)
  474. return ret;
  475. }
  476. }
  477. break;
  478. case ID_MT312:
  479. case ID_ZL10313:
  480. break;
  481. default:
  482. return -EINVAL;
  483. }
  484. if (fe->ops.tuner_ops.set_params) {
  485. fe->ops.tuner_ops.set_params(fe);
  486. if (fe->ops.i2c_gate_ctrl)
  487. fe->ops.i2c_gate_ctrl(fe, 0);
  488. }
  489. /* sr = (u16)(sr * 256.0 / 1000000.0) */
  490. sr = mt312_div(p->symbol_rate * 4, 15625);
  491. /* SYM_RATE */
  492. buf[0] = (sr >> 8) & 0x3f;
  493. buf[1] = (sr >> 0) & 0xff;
  494. /* VIT_MODE */
  495. buf[2] = inv_tab[p->inversion] | fec_tab[p->fec_inner];
  496. /* QPSK_CTRL */
  497. buf[3] = 0x40; /* swap I and Q before QPSK demodulation */
  498. if (p->symbol_rate < 10000000)
  499. buf[3] |= 0x04; /* use afc mode */
  500. /* GO */
  501. buf[4] = 0x01;
  502. ret = mt312_write(state, SYM_RATE_H, buf, sizeof(buf));
  503. if (ret < 0)
  504. return ret;
  505. mt312_reset(state, 0);
  506. return 0;
  507. }
  508. static int mt312_get_frontend(struct dvb_frontend *fe)
  509. {
  510. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  511. struct mt312_state *state = fe->demodulator_priv;
  512. int ret;
  513. ret = mt312_get_inversion(state, &p->inversion);
  514. if (ret < 0)
  515. return ret;
  516. ret = mt312_get_symbol_rate(state, &p->symbol_rate);
  517. if (ret < 0)
  518. return ret;
  519. ret = mt312_get_code_rate(state, &p->fec_inner);
  520. if (ret < 0)
  521. return ret;
  522. return 0;
  523. }
  524. static int mt312_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
  525. {
  526. struct mt312_state *state = fe->demodulator_priv;
  527. u8 val = 0x00;
  528. int ret;
  529. switch (state->id) {
  530. case ID_ZL10313:
  531. ret = mt312_readreg(state, GPP_CTRL, &val);
  532. if (ret < 0)
  533. goto error;
  534. /* preserve this bit to not accidentally shutdown ADC */
  535. val &= 0x80;
  536. break;
  537. }
  538. if (enable)
  539. val |= 0x40;
  540. else
  541. val &= ~0x40;
  542. ret = mt312_writereg(state, GPP_CTRL, val);
  543. error:
  544. return ret;
  545. }
  546. static int mt312_sleep(struct dvb_frontend *fe)
  547. {
  548. struct mt312_state *state = fe->demodulator_priv;
  549. int ret;
  550. u8 config;
  551. /* reset all registers to defaults */
  552. ret = mt312_reset(state, 1);
  553. if (ret < 0)
  554. return ret;
  555. if (state->id == ID_ZL10313) {
  556. /* reset ADC */
  557. ret = mt312_writereg(state, GPP_CTRL, 0x00);
  558. if (ret < 0)
  559. return ret;
  560. /* full shutdown of ADCs, mpeg bus tristated */
  561. ret = mt312_writereg(state, HW_CTRL, 0x0d);
  562. if (ret < 0)
  563. return ret;
  564. }
  565. ret = mt312_readreg(state, CONFIG, &config);
  566. if (ret < 0)
  567. return ret;
  568. /* enter standby */
  569. ret = mt312_writereg(state, CONFIG, config & 0x7f);
  570. if (ret < 0)
  571. return ret;
  572. return 0;
  573. }
  574. static int mt312_get_tune_settings(struct dvb_frontend *fe,
  575. struct dvb_frontend_tune_settings *fesettings)
  576. {
  577. fesettings->min_delay_ms = 50;
  578. fesettings->step_size = 0;
  579. fesettings->max_drift = 0;
  580. return 0;
  581. }
  582. static void mt312_release(struct dvb_frontend *fe)
  583. {
  584. struct mt312_state *state = fe->demodulator_priv;
  585. kfree(state);
  586. }
  587. #define MT312_SYS_CLK 90000000UL /* 90 MHz */
  588. static struct dvb_frontend_ops mt312_ops = {
  589. .delsys = { SYS_DVBS },
  590. .info = {
  591. .name = "Zarlink ???? DVB-S",
  592. .frequency_min = 950000,
  593. .frequency_max = 2150000,
  594. /* FIXME: adjust freq to real used xtal */
  595. .frequency_stepsize = (MT312_PLL_CLK / 1000) / 128,
  596. .symbol_rate_min = MT312_SYS_CLK / 128, /* FIXME as above */
  597. .symbol_rate_max = MT312_SYS_CLK / 2,
  598. .caps =
  599. FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 |
  600. FE_CAN_FEC_3_4 | FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 |
  601. FE_CAN_FEC_AUTO | FE_CAN_QPSK | FE_CAN_MUTE_TS |
  602. FE_CAN_RECOVER
  603. },
  604. .release = mt312_release,
  605. .init = mt312_initfe,
  606. .sleep = mt312_sleep,
  607. .i2c_gate_ctrl = mt312_i2c_gate_ctrl,
  608. .set_frontend = mt312_set_frontend,
  609. .get_frontend = mt312_get_frontend,
  610. .get_tune_settings = mt312_get_tune_settings,
  611. .read_status = mt312_read_status,
  612. .read_ber = mt312_read_ber,
  613. .read_signal_strength = mt312_read_signal_strength,
  614. .read_snr = mt312_read_snr,
  615. .read_ucblocks = mt312_read_ucblocks,
  616. .diseqc_send_master_cmd = mt312_send_master_cmd,
  617. .diseqc_send_burst = mt312_send_burst,
  618. .set_tone = mt312_set_tone,
  619. .set_voltage = mt312_set_voltage,
  620. };
  621. struct dvb_frontend *mt312_attach(const struct mt312_config *config,
  622. struct i2c_adapter *i2c)
  623. {
  624. struct mt312_state *state = NULL;
  625. /* allocate memory for the internal state */
  626. state = kzalloc(sizeof(struct mt312_state), GFP_KERNEL);
  627. if (state == NULL)
  628. goto error;
  629. /* setup the state */
  630. state->config = config;
  631. state->i2c = i2c;
  632. /* check if the demod is there */
  633. if (mt312_readreg(state, ID, &state->id) < 0)
  634. goto error;
  635. /* create dvb_frontend */
  636. memcpy(&state->frontend.ops, &mt312_ops,
  637. sizeof(struct dvb_frontend_ops));
  638. state->frontend.demodulator_priv = state;
  639. switch (state->id) {
  640. case ID_VP310:
  641. strcpy(state->frontend.ops.info.name, "Zarlink VP310 DVB-S");
  642. state->xtal = MT312_PLL_CLK;
  643. state->freq_mult = 9;
  644. break;
  645. case ID_MT312:
  646. strcpy(state->frontend.ops.info.name, "Zarlink MT312 DVB-S");
  647. state->xtal = MT312_PLL_CLK;
  648. state->freq_mult = 6;
  649. break;
  650. case ID_ZL10313:
  651. strcpy(state->frontend.ops.info.name, "Zarlink ZL10313 DVB-S");
  652. state->xtal = MT312_PLL_CLK_10_111;
  653. state->freq_mult = 9;
  654. break;
  655. default:
  656. printk(KERN_WARNING "Only Zarlink VP310/MT312/ZL10313"
  657. " are supported chips.\n");
  658. goto error;
  659. }
  660. return &state->frontend;
  661. error:
  662. kfree(state);
  663. return NULL;
  664. }
  665. EXPORT_SYMBOL(mt312_attach);
  666. module_param(debug, int, 0644);
  667. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  668. MODULE_DESCRIPTION("Zarlink VP310/MT312/ZL10313 DVB-S Demodulator driver");
  669. MODULE_AUTHOR("Andreas Oberritter <obi@linuxtv.org>");
  670. MODULE_AUTHOR("Matthias Schwarzott <zzam@gentoo.org>");
  671. MODULE_LICENSE("GPL");