zl10036.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  1. /**
  2. * Driver for Zarlink zl10036 DVB-S silicon tuner
  3. *
  4. * Copyright (C) 2006 Tino Reichardt
  5. * Copyright (C) 2007-2009 Matthias Schwarzott <zzam@gentoo.de>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License Version 2, as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. **
  21. * The data sheet for this tuner can be found at:
  22. * http://www.mcmilk.de/projects/dvb-card/datasheets/ZL10036.pdf
  23. *
  24. * This one is working: (at my Avermedia DVB-S Pro)
  25. * - zl10036 (40pin, FTA)
  26. *
  27. * A driver for zl10038 should be very similar.
  28. */
  29. #include <linux/module.h>
  30. #include <linux/dvb/frontend.h>
  31. #include <asm/types.h>
  32. #include "zl10036.h"
  33. static int zl10036_debug;
  34. #define dprintk(level, args...) \
  35. do { if (zl10036_debug & level) printk(KERN_DEBUG "zl10036: " args); \
  36. } while (0)
  37. #define deb_info(args...) dprintk(0x01, args)
  38. #define deb_i2c(args...) dprintk(0x02, args)
  39. struct zl10036_state {
  40. struct i2c_adapter *i2c;
  41. const struct zl10036_config *config;
  42. u32 frequency;
  43. u8 br, bf;
  44. };
  45. /* This driver assumes the tuner is driven by a 10.111MHz Cristal */
  46. #define _XTAL 10111
  47. /* Some of the possible dividers:
  48. * 64, (write 0x05 to reg), freq step size 158kHz
  49. * 10, (write 0x0a to reg), freq step size 1.011kHz (used here)
  50. * 5, (write 0x09 to reg), freq step size 2.022kHz
  51. */
  52. #define _RDIV 10
  53. #define _RDIV_REG 0x0a
  54. #define _FR (_XTAL/_RDIV)
  55. #define STATUS_POR 0x80 /* Power on Reset */
  56. #define STATUS_FL 0x40 /* Frequency & Phase Lock */
  57. /* read/write for zl10036 and zl10038 */
  58. static int zl10036_read_status_reg(struct zl10036_state *state)
  59. {
  60. u8 status;
  61. struct i2c_msg msg[1] = {
  62. { .addr = state->config->tuner_address, .flags = I2C_M_RD,
  63. .buf = &status, .len = sizeof(status) },
  64. };
  65. if (i2c_transfer(state->i2c, msg, 1) != 1) {
  66. printk(KERN_ERR "%s: i2c read failed at addr=%02x\n",
  67. __func__, state->config->tuner_address);
  68. return -EIO;
  69. }
  70. deb_i2c("R(status): %02x [FL=%d]\n", status,
  71. (status & STATUS_FL) ? 1 : 0);
  72. if (status & STATUS_POR)
  73. deb_info("%s: Power-On-Reset bit enabled - "
  74. "need to initialize the tuner\n", __func__);
  75. return status;
  76. }
  77. static int zl10036_write(struct zl10036_state *state, u8 buf[], u8 count)
  78. {
  79. struct i2c_msg msg[1] = {
  80. { .addr = state->config->tuner_address, .flags = 0,
  81. .buf = buf, .len = count },
  82. };
  83. u8 reg = 0;
  84. int ret;
  85. if (zl10036_debug & 0x02) {
  86. /* every 8bit-value satisifes this!
  87. * so only check for debug log */
  88. if ((buf[0] & 0x80) == 0x00)
  89. reg = 2;
  90. else if ((buf[0] & 0xc0) == 0x80)
  91. reg = 4;
  92. else if ((buf[0] & 0xf0) == 0xc0)
  93. reg = 6;
  94. else if ((buf[0] & 0xf0) == 0xd0)
  95. reg = 8;
  96. else if ((buf[0] & 0xf0) == 0xe0)
  97. reg = 10;
  98. else if ((buf[0] & 0xf0) == 0xf0)
  99. reg = 12;
  100. deb_i2c("W(%d):", reg);
  101. {
  102. int i;
  103. for (i = 0; i < count; i++)
  104. printk(KERN_CONT " %02x", buf[i]);
  105. printk(KERN_CONT "\n");
  106. }
  107. }
  108. ret = i2c_transfer(state->i2c, msg, 1);
  109. if (ret != 1) {
  110. printk(KERN_ERR "%s: i2c error, ret=%d\n", __func__, ret);
  111. return -EIO;
  112. }
  113. return 0;
  114. }
  115. static int zl10036_release(struct dvb_frontend *fe)
  116. {
  117. struct zl10036_state *state = fe->tuner_priv;
  118. fe->tuner_priv = NULL;
  119. kfree(state);
  120. return 0;
  121. }
  122. static int zl10036_sleep(struct dvb_frontend *fe)
  123. {
  124. struct zl10036_state *state = fe->tuner_priv;
  125. u8 buf[] = { 0xf0, 0x80 }; /* regs 12/13 */
  126. int ret;
  127. deb_info("%s\n", __func__);
  128. if (fe->ops.i2c_gate_ctrl)
  129. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  130. ret = zl10036_write(state, buf, sizeof(buf));
  131. if (fe->ops.i2c_gate_ctrl)
  132. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  133. return ret;
  134. }
  135. /**
  136. * register map of the ZL10036/ZL10038
  137. *
  138. * reg[default] content
  139. * 2[0x00]: 0 | N14 | N13 | N12 | N11 | N10 | N9 | N8
  140. * 3[0x00]: N7 | N6 | N5 | N4 | N3 | N2 | N1 | N0
  141. * 4[0x80]: 1 | 0 | RFG | BA1 | BA0 | BG1 | BG0 | LEN
  142. * 5[0x00]: P0 | C1 | C0 | R4 | R3 | R2 | R1 | R0
  143. * 6[0xc0]: 1 | 1 | 0 | 0 | RSD | 0 | 0 | 0
  144. * 7[0x20]: P1 | BF6 | BF5 | BF4 | BF3 | BF2 | BF1 | 0
  145. * 8[0xdb]: 1 | 1 | 0 | 1 | 0 | CC | 1 | 1
  146. * 9[0x30]: VSD | V2 | V1 | V0 | S3 | S2 | S1 | S0
  147. * 10[0xe1]: 1 | 1 | 1 | 0 | 0 | LS2 | LS1 | LS0
  148. * 11[0xf5]: WS | WH2 | WH1 | WH0 | WL2 | WL1 | WL0 | WRE
  149. * 12[0xf0]: 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0
  150. * 13[0x28]: PD | BR4 | BR3 | BR2 | BR1 | BR0 | CLR | TL
  151. */
  152. static int zl10036_set_frequency(struct zl10036_state *state, u32 frequency)
  153. {
  154. u8 buf[2];
  155. u32 div, foffset;
  156. div = (frequency + _FR/2) / _FR;
  157. state->frequency = div * _FR;
  158. foffset = frequency - state->frequency;
  159. buf[0] = (div >> 8) & 0x7f;
  160. buf[1] = (div >> 0) & 0xff;
  161. deb_info("%s: ftodo=%u fpriv=%u ferr=%d div=%u\n", __func__,
  162. frequency, state->frequency, foffset, div);
  163. return zl10036_write(state, buf, sizeof(buf));
  164. }
  165. static int zl10036_set_bandwidth(struct zl10036_state *state, u32 fbw)
  166. {
  167. /* fbw is measured in kHz */
  168. u8 br, bf;
  169. int ret;
  170. u8 buf_bf[] = {
  171. 0xc0, 0x00, /* 6/7: rsd=0 bf=0 */
  172. };
  173. u8 buf_br[] = {
  174. 0xf0, 0x00, /* 12/13: br=0xa clr=0 tl=0*/
  175. };
  176. u8 zl10036_rsd_off[] = { 0xc8 }; /* set RSD=1 */
  177. /* ensure correct values */
  178. if (fbw > 35000)
  179. fbw = 35000;
  180. if (fbw < 8000)
  181. fbw = 8000;
  182. #define _BR_MAXIMUM (_XTAL/575) /* _XTAL / 575kHz = 17 */
  183. /* <= 28,82 MHz */
  184. if (fbw <= 28820) {
  185. br = _BR_MAXIMUM;
  186. } else {
  187. /**
  188. * f(bw)=34,6MHz f(xtal)=10.111MHz
  189. * br = (10111/34600) * 63 * 1/K = 14;
  190. */
  191. br = ((_XTAL * 21 * 1000) / (fbw * 419));
  192. }
  193. /* ensure correct values */
  194. if (br < 4)
  195. br = 4;
  196. if (br > _BR_MAXIMUM)
  197. br = _BR_MAXIMUM;
  198. /*
  199. * k = 1.257
  200. * bf = fbw/_XTAL * br * k - 1 */
  201. bf = (fbw * br * 1257) / (_XTAL * 1000) - 1;
  202. /* ensure correct values */
  203. if (bf > 62)
  204. bf = 62;
  205. buf_bf[1] = (bf << 1) & 0x7e;
  206. buf_br[1] = (br << 2) & 0x7c;
  207. deb_info("%s: BW=%d br=%u bf=%u\n", __func__, fbw, br, bf);
  208. if (br != state->br) {
  209. ret = zl10036_write(state, buf_br, sizeof(buf_br));
  210. if (ret < 0)
  211. return ret;
  212. }
  213. if (bf != state->bf) {
  214. ret = zl10036_write(state, buf_bf, sizeof(buf_bf));
  215. if (ret < 0)
  216. return ret;
  217. /* time = br/(32* fxtal) */
  218. /* minimal sleep time to be calculated
  219. * maximum br is 63 -> max time = 2 /10 MHz = 2e-7 */
  220. msleep(1);
  221. ret = zl10036_write(state, zl10036_rsd_off,
  222. sizeof(zl10036_rsd_off));
  223. if (ret < 0)
  224. return ret;
  225. }
  226. state->br = br;
  227. state->bf = bf;
  228. return 0;
  229. }
  230. static int zl10036_set_gain_params(struct zl10036_state *state,
  231. int c)
  232. {
  233. u8 buf[2];
  234. u8 rfg, ba, bg;
  235. /* default values */
  236. rfg = 0; /* enable when using an lna */
  237. ba = 1;
  238. bg = 1;
  239. /* reg 4 */
  240. buf[0] = 0x80 | ((rfg << 5) & 0x20)
  241. | ((ba << 3) & 0x18) | ((bg << 1) & 0x06);
  242. if (!state->config->rf_loop_enable)
  243. buf[0] |= 0x01;
  244. /* P0=0 */
  245. buf[1] = _RDIV_REG | ((c << 5) & 0x60);
  246. deb_info("%s: c=%u rfg=%u ba=%u bg=%u\n", __func__, c, rfg, ba, bg);
  247. return zl10036_write(state, buf, sizeof(buf));
  248. }
  249. static int zl10036_set_params(struct dvb_frontend *fe,
  250. struct dvb_frontend_parameters *params)
  251. {
  252. struct zl10036_state *state = fe->tuner_priv;
  253. int ret = 0;
  254. u32 frequency = params->frequency;
  255. u32 fbw;
  256. int i;
  257. u8 c;
  258. /* ensure correct values
  259. * maybe redundant as core already checks this */
  260. if ((frequency < fe->ops.info.frequency_min)
  261. || (frequency > fe->ops.info.frequency_max))
  262. return -EINVAL;
  263. /**
  264. * alpha = 1.35 for dvb-s
  265. * fBW = (alpha*symbolrate)/(2*0.8)
  266. * 1.35 / (2*0.8) = 27 / 32
  267. */
  268. fbw = (27 * params->u.qpsk.symbol_rate) / 32;
  269. /* scale to kHz */
  270. fbw /= 1000;
  271. /* Add safe margin of 3MHz */
  272. fbw += 3000;
  273. /* setting the charge pump - guessed values */
  274. if (frequency < 950000)
  275. return -EINVAL;
  276. else if (frequency < 1250000)
  277. c = 0;
  278. else if (frequency < 1750000)
  279. c = 1;
  280. else if (frequency < 2175000)
  281. c = 2;
  282. else
  283. return -EINVAL;
  284. if (fe->ops.i2c_gate_ctrl)
  285. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  286. ret = zl10036_set_gain_params(state, c);
  287. if (ret < 0)
  288. goto error;
  289. ret = zl10036_set_frequency(state, params->frequency);
  290. if (ret < 0)
  291. goto error;
  292. ret = zl10036_set_bandwidth(state, fbw);
  293. if (ret < 0)
  294. goto error;
  295. /* wait for tuner lock - no idea if this is really needed */
  296. for (i = 0; i < 20; i++) {
  297. ret = zl10036_read_status_reg(state);
  298. if (ret < 0)
  299. goto error;
  300. /* check Frequency & Phase Lock Bit */
  301. if (ret & STATUS_FL)
  302. break;
  303. msleep(10);
  304. }
  305. error:
  306. if (fe->ops.i2c_gate_ctrl)
  307. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  308. return ret;
  309. }
  310. static int zl10036_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  311. {
  312. struct zl10036_state *state = fe->tuner_priv;
  313. *frequency = state->frequency;
  314. return 0;
  315. }
  316. static int zl10036_init_regs(struct zl10036_state *state)
  317. {
  318. int ret;
  319. int i;
  320. /* could also be one block from reg 2 to 13 and additional 10/11 */
  321. u8 zl10036_init_tab[][2] = {
  322. { 0x04, 0x00 }, /* 2/3: div=0x400 - arbitrary value */
  323. { 0x8b, _RDIV_REG }, /* 4/5: rfg=0 ba=1 bg=1 len=? */
  324. /* p0=0 c=0 r=_RDIV_REG */
  325. { 0xc0, 0x20 }, /* 6/7: rsd=0 bf=0x10 */
  326. { 0xd3, 0x40 }, /* 8/9: from datasheet */
  327. { 0xe3, 0x5b }, /* 10/11: lock window level */
  328. { 0xf0, 0x28 }, /* 12/13: br=0xa clr=0 tl=0*/
  329. { 0xe3, 0xf9 }, /* 10/11: unlock window level */
  330. };
  331. /* invalid values to trigger writing */
  332. state->br = 0xff;
  333. state->bf = 0xff;
  334. if (!state->config->rf_loop_enable)
  335. zl10036_init_tab[1][2] |= 0x01;
  336. deb_info("%s\n", __func__);
  337. for (i = 0; i < ARRAY_SIZE(zl10036_init_tab); i++) {
  338. ret = zl10036_write(state, zl10036_init_tab[i], 2);
  339. if (ret < 0)
  340. return ret;
  341. }
  342. return 0;
  343. }
  344. static int zl10036_init(struct dvb_frontend *fe)
  345. {
  346. struct zl10036_state *state = fe->tuner_priv;
  347. int ret = 0;
  348. if (fe->ops.i2c_gate_ctrl)
  349. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  350. ret = zl10036_read_status_reg(state);
  351. if (ret < 0)
  352. return ret;
  353. /* Only init if Power-on-Reset bit is set? */
  354. ret = zl10036_init_regs(state);
  355. if (fe->ops.i2c_gate_ctrl)
  356. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  357. return ret;
  358. }
  359. static struct dvb_tuner_ops zl10036_tuner_ops = {
  360. .info = {
  361. .name = "Zarlink ZL10036",
  362. .frequency_min = 950000,
  363. .frequency_max = 2175000
  364. },
  365. .init = zl10036_init,
  366. .release = zl10036_release,
  367. .sleep = zl10036_sleep,
  368. .set_params = zl10036_set_params,
  369. .get_frequency = zl10036_get_frequency,
  370. };
  371. struct dvb_frontend *zl10036_attach(struct dvb_frontend *fe,
  372. const struct zl10036_config *config,
  373. struct i2c_adapter *i2c)
  374. {
  375. struct zl10036_state *state = NULL;
  376. int ret;
  377. if (NULL == config) {
  378. printk(KERN_ERR "%s: no config specified", __func__);
  379. goto error;
  380. }
  381. state = kzalloc(sizeof(struct zl10036_state), GFP_KERNEL);
  382. if (NULL == state)
  383. return NULL;
  384. state->config = config;
  385. state->i2c = i2c;
  386. if (fe->ops.i2c_gate_ctrl)
  387. fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c_gate */
  388. ret = zl10036_read_status_reg(state);
  389. if (ret < 0) {
  390. printk(KERN_ERR "%s: No zl10036 found\n", __func__);
  391. goto error;
  392. }
  393. ret = zl10036_init_regs(state);
  394. if (ret < 0) {
  395. printk(KERN_ERR "%s: tuner initialization failed\n",
  396. __func__);
  397. goto error;
  398. }
  399. if (fe->ops.i2c_gate_ctrl)
  400. fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c_gate */
  401. fe->tuner_priv = state;
  402. memcpy(&fe->ops.tuner_ops, &zl10036_tuner_ops,
  403. sizeof(struct dvb_tuner_ops));
  404. printk(KERN_INFO "%s: tuner initialization (%s addr=0x%02x) ok\n",
  405. __func__, fe->ops.tuner_ops.info.name, config->tuner_address);
  406. return fe;
  407. error:
  408. zl10036_release(fe);
  409. return NULL;
  410. }
  411. EXPORT_SYMBOL(zl10036_attach);
  412. module_param_named(debug, zl10036_debug, int, 0644);
  413. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  414. MODULE_DESCRIPTION("DVB ZL10036 driver");
  415. MODULE_AUTHOR("Tino Reichardt");
  416. MODULE_AUTHOR("Matthias Schwarzott");
  417. MODULE_LICENSE("GPL");