mt312.c 18 KB

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