zl10039.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * Driver for Zarlink ZL10039 DVB-S tuner
  3. *
  4. * Copyright 2007 Jan D. Louw <jd.louw@mweb.co.za>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  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. *
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/init.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/dvb/frontend.h>
  22. #include <media/dvb_frontend.h>
  23. #include "zl10039.h"
  24. static int debug;
  25. /* Max transfer size done by I2C transfer functions */
  26. #define MAX_XFER_SIZE 64
  27. #define dprintk(args...) \
  28. do { \
  29. if (debug) \
  30. printk(KERN_DEBUG args); \
  31. } while (0)
  32. enum zl10039_model_id {
  33. ID_ZL10039 = 1
  34. };
  35. struct zl10039_state {
  36. struct i2c_adapter *i2c;
  37. u8 i2c_addr;
  38. u8 id;
  39. };
  40. enum zl10039_reg_addr {
  41. PLL0 = 0,
  42. PLL1,
  43. PLL2,
  44. PLL3,
  45. RFFE,
  46. BASE0,
  47. BASE1,
  48. BASE2,
  49. LO0,
  50. LO1,
  51. LO2,
  52. LO3,
  53. LO4,
  54. LO5,
  55. LO6,
  56. GENERAL
  57. };
  58. static int zl10039_read(const struct zl10039_state *state,
  59. const enum zl10039_reg_addr reg, u8 *buf,
  60. const size_t count)
  61. {
  62. u8 regbuf[] = { reg };
  63. struct i2c_msg msg[] = {
  64. {/* Write register address */
  65. .addr = state->i2c_addr,
  66. .flags = 0,
  67. .buf = regbuf,
  68. .len = 1,
  69. }, {/* Read count bytes */
  70. .addr = state->i2c_addr,
  71. .flags = I2C_M_RD,
  72. .buf = buf,
  73. .len = count,
  74. },
  75. };
  76. dprintk("%s\n", __func__);
  77. if (i2c_transfer(state->i2c, msg, 2) != 2) {
  78. dprintk("%s: i2c read error\n", __func__);
  79. return -EREMOTEIO;
  80. }
  81. return 0; /* Success */
  82. }
  83. static int zl10039_write(struct zl10039_state *state,
  84. const enum zl10039_reg_addr reg, const u8 *src,
  85. const size_t count)
  86. {
  87. u8 buf[MAX_XFER_SIZE];
  88. struct i2c_msg msg = {
  89. .addr = state->i2c_addr,
  90. .flags = 0,
  91. .buf = buf,
  92. .len = count + 1,
  93. };
  94. if (1 + count > sizeof(buf)) {
  95. printk(KERN_WARNING
  96. "%s: i2c wr reg=%04x: len=%zu is too big!\n",
  97. KBUILD_MODNAME, reg, count);
  98. return -EINVAL;
  99. }
  100. dprintk("%s\n", __func__);
  101. /* Write register address and data in one go */
  102. buf[0] = reg;
  103. memcpy(&buf[1], src, count);
  104. if (i2c_transfer(state->i2c, &msg, 1) != 1) {
  105. dprintk("%s: i2c write error\n", __func__);
  106. return -EREMOTEIO;
  107. }
  108. return 0; /* Success */
  109. }
  110. static inline int zl10039_readreg(struct zl10039_state *state,
  111. const enum zl10039_reg_addr reg, u8 *val)
  112. {
  113. return zl10039_read(state, reg, val, 1);
  114. }
  115. static inline int zl10039_writereg(struct zl10039_state *state,
  116. const enum zl10039_reg_addr reg,
  117. const u8 val)
  118. {
  119. const u8 tmp = val; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
  120. return zl10039_write(state, reg, &tmp, 1);
  121. }
  122. static int zl10039_init(struct dvb_frontend *fe)
  123. {
  124. struct zl10039_state *state = fe->tuner_priv;
  125. int ret;
  126. dprintk("%s\n", __func__);
  127. if (fe->ops.i2c_gate_ctrl)
  128. fe->ops.i2c_gate_ctrl(fe, 1);
  129. /* Reset logic */
  130. ret = zl10039_writereg(state, GENERAL, 0x40);
  131. if (ret < 0) {
  132. dprintk("Note: i2c write error normal when resetting the tuner\n");
  133. }
  134. /* Wake up */
  135. ret = zl10039_writereg(state, GENERAL, 0x01);
  136. if (ret < 0) {
  137. dprintk("Tuner power up failed\n");
  138. return ret;
  139. }
  140. if (fe->ops.i2c_gate_ctrl)
  141. fe->ops.i2c_gate_ctrl(fe, 0);
  142. return 0;
  143. }
  144. static int zl10039_sleep(struct dvb_frontend *fe)
  145. {
  146. struct zl10039_state *state = fe->tuner_priv;
  147. int ret;
  148. dprintk("%s\n", __func__);
  149. if (fe->ops.i2c_gate_ctrl)
  150. fe->ops.i2c_gate_ctrl(fe, 1);
  151. ret = zl10039_writereg(state, GENERAL, 0x80);
  152. if (ret < 0) {
  153. dprintk("Tuner sleep failed\n");
  154. return ret;
  155. }
  156. if (fe->ops.i2c_gate_ctrl)
  157. fe->ops.i2c_gate_ctrl(fe, 0);
  158. return 0;
  159. }
  160. static int zl10039_set_params(struct dvb_frontend *fe)
  161. {
  162. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  163. struct zl10039_state *state = fe->tuner_priv;
  164. u8 buf[6];
  165. u8 bf;
  166. u32 fbw;
  167. u32 div;
  168. int ret;
  169. dprintk("%s\n", __func__);
  170. dprintk("Set frequency = %d, symbol rate = %d\n",
  171. c->frequency, c->symbol_rate);
  172. /* Assumed 10.111 MHz crystal oscillator */
  173. /* Cancelled num/den 80 to prevent overflow */
  174. div = (c->frequency * 1000) / 126387;
  175. fbw = (c->symbol_rate * 27) / 32000;
  176. /* Cancelled num/den 10 to prevent overflow */
  177. bf = ((fbw * 5088) / 1011100) - 1;
  178. /*PLL divider*/
  179. buf[0] = (div >> 8) & 0x7f;
  180. buf[1] = (div >> 0) & 0xff;
  181. /*Reference divider*/
  182. /* Select reference ratio of 80 */
  183. buf[2] = 0x1D;
  184. /*PLL test modes*/
  185. buf[3] = 0x40;
  186. /*RF Control register*/
  187. buf[4] = 0x6E; /* Bypass enable */
  188. /*Baseband filter cutoff */
  189. buf[5] = bf;
  190. /* Open i2c gate */
  191. if (fe->ops.i2c_gate_ctrl)
  192. fe->ops.i2c_gate_ctrl(fe, 1);
  193. /* BR = 10, Enable filter adjustment */
  194. ret = zl10039_writereg(state, BASE1, 0x0A);
  195. if (ret < 0)
  196. goto error;
  197. /* Write new config values */
  198. ret = zl10039_write(state, PLL0, buf, sizeof(buf));
  199. if (ret < 0)
  200. goto error;
  201. /* BR = 10, Disable filter adjustment */
  202. ret = zl10039_writereg(state, BASE1, 0x6A);
  203. if (ret < 0)
  204. goto error;
  205. /* Close i2c gate */
  206. if (fe->ops.i2c_gate_ctrl)
  207. fe->ops.i2c_gate_ctrl(fe, 0);
  208. return 0;
  209. error:
  210. dprintk("Error setting tuner\n");
  211. return ret;
  212. }
  213. static void zl10039_release(struct dvb_frontend *fe)
  214. {
  215. struct zl10039_state *state = fe->tuner_priv;
  216. dprintk("%s\n", __func__);
  217. kfree(state);
  218. fe->tuner_priv = NULL;
  219. }
  220. static const struct dvb_tuner_ops zl10039_ops = {
  221. .release = zl10039_release,
  222. .init = zl10039_init,
  223. .sleep = zl10039_sleep,
  224. .set_params = zl10039_set_params,
  225. };
  226. struct dvb_frontend *zl10039_attach(struct dvb_frontend *fe,
  227. u8 i2c_addr, struct i2c_adapter *i2c)
  228. {
  229. struct zl10039_state *state = NULL;
  230. dprintk("%s\n", __func__);
  231. state = kmalloc(sizeof(struct zl10039_state), GFP_KERNEL);
  232. if (state == NULL)
  233. goto error;
  234. state->i2c = i2c;
  235. state->i2c_addr = i2c_addr;
  236. /* Open i2c gate */
  237. if (fe->ops.i2c_gate_ctrl)
  238. fe->ops.i2c_gate_ctrl(fe, 1);
  239. /* check if this is a valid tuner */
  240. if (zl10039_readreg(state, GENERAL, &state->id) < 0) {
  241. /* Close i2c gate */
  242. if (fe->ops.i2c_gate_ctrl)
  243. fe->ops.i2c_gate_ctrl(fe, 0);
  244. goto error;
  245. }
  246. /* Close i2c gate */
  247. if (fe->ops.i2c_gate_ctrl)
  248. fe->ops.i2c_gate_ctrl(fe, 0);
  249. state->id = state->id & 0x0f;
  250. switch (state->id) {
  251. case ID_ZL10039:
  252. strcpy(fe->ops.tuner_ops.info.name,
  253. "Zarlink ZL10039 DVB-S tuner");
  254. break;
  255. default:
  256. dprintk("Chip ID=%x does not match a known type\n", state->id);
  257. goto error;
  258. }
  259. memcpy(&fe->ops.tuner_ops, &zl10039_ops, sizeof(struct dvb_tuner_ops));
  260. fe->tuner_priv = state;
  261. dprintk("Tuner attached @ i2c address 0x%02x\n", i2c_addr);
  262. return fe;
  263. error:
  264. kfree(state);
  265. return NULL;
  266. }
  267. EXPORT_SYMBOL(zl10039_attach);
  268. module_param(debug, int, 0644);
  269. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  270. MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver");
  271. MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>");
  272. MODULE_LICENSE("GPL");