zl10039.c 7.1 KB

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