zl10036.c 11 KB

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