zl10036.c 12 KB

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