zl10039.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. return zl10039_write(state, reg, &val, 1);
  124. }
  125. static int zl10039_init(struct dvb_frontend *fe)
  126. {
  127. struct zl10039_state *state = fe->tuner_priv;
  128. int ret;
  129. dprintk("%s\n", __func__);
  130. if (fe->ops.i2c_gate_ctrl)
  131. fe->ops.i2c_gate_ctrl(fe, 1);
  132. /* Reset logic */
  133. ret = zl10039_writereg(state, GENERAL, 0x40);
  134. if (ret < 0) {
  135. dprintk("Note: i2c write error normal when resetting the "
  136. "tuner\n");
  137. }
  138. /* Wake up */
  139. ret = zl10039_writereg(state, GENERAL, 0x01);
  140. if (ret < 0) {
  141. dprintk("Tuner power up failed\n");
  142. return ret;
  143. }
  144. if (fe->ops.i2c_gate_ctrl)
  145. fe->ops.i2c_gate_ctrl(fe, 0);
  146. return 0;
  147. }
  148. static int zl10039_sleep(struct dvb_frontend *fe)
  149. {
  150. struct zl10039_state *state = fe->tuner_priv;
  151. int ret;
  152. dprintk("%s\n", __func__);
  153. if (fe->ops.i2c_gate_ctrl)
  154. fe->ops.i2c_gate_ctrl(fe, 1);
  155. ret = zl10039_writereg(state, GENERAL, 0x80);
  156. if (ret < 0) {
  157. dprintk("Tuner sleep failed\n");
  158. return ret;
  159. }
  160. if (fe->ops.i2c_gate_ctrl)
  161. fe->ops.i2c_gate_ctrl(fe, 0);
  162. return 0;
  163. }
  164. static int zl10039_set_params(struct dvb_frontend *fe)
  165. {
  166. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  167. struct zl10039_state *state = fe->tuner_priv;
  168. u8 buf[6];
  169. u8 bf;
  170. u32 fbw;
  171. u32 div;
  172. int ret;
  173. dprintk("%s\n", __func__);
  174. dprintk("Set frequency = %d, symbol rate = %d\n",
  175. c->frequency, c->symbol_rate);
  176. /* Assumed 10.111 MHz crystal oscillator */
  177. /* Cancelled num/den 80 to prevent overflow */
  178. div = (c->frequency * 1000) / 126387;
  179. fbw = (c->symbol_rate * 27) / 32000;
  180. /* Cancelled num/den 10 to prevent overflow */
  181. bf = ((fbw * 5088) / 1011100) - 1;
  182. /*PLL divider*/
  183. buf[0] = (div >> 8) & 0x7f;
  184. buf[1] = (div >> 0) & 0xff;
  185. /*Reference divider*/
  186. /* Select reference ratio of 80 */
  187. buf[2] = 0x1D;
  188. /*PLL test modes*/
  189. buf[3] = 0x40;
  190. /*RF Control register*/
  191. buf[4] = 0x6E; /* Bypass enable */
  192. /*Baseband filter cutoff */
  193. buf[5] = bf;
  194. /* Open i2c gate */
  195. if (fe->ops.i2c_gate_ctrl)
  196. fe->ops.i2c_gate_ctrl(fe, 1);
  197. /* BR = 10, Enable filter adjustment */
  198. ret = zl10039_writereg(state, BASE1, 0x0A);
  199. if (ret < 0)
  200. goto error;
  201. /* Write new config values */
  202. ret = zl10039_write(state, PLL0, buf, sizeof(buf));
  203. if (ret < 0)
  204. goto error;
  205. /* BR = 10, Disable filter adjustment */
  206. ret = zl10039_writereg(state, BASE1, 0x6A);
  207. if (ret < 0)
  208. goto error;
  209. /* Close i2c gate */
  210. if (fe->ops.i2c_gate_ctrl)
  211. fe->ops.i2c_gate_ctrl(fe, 0);
  212. return 0;
  213. error:
  214. dprintk("Error setting tuner\n");
  215. return ret;
  216. }
  217. static int zl10039_release(struct dvb_frontend *fe)
  218. {
  219. struct zl10039_state *state = fe->tuner_priv;
  220. dprintk("%s\n", __func__);
  221. kfree(state);
  222. fe->tuner_priv = NULL;
  223. return 0;
  224. }
  225. static const struct dvb_tuner_ops zl10039_ops = {
  226. .release = zl10039_release,
  227. .init = zl10039_init,
  228. .sleep = zl10039_sleep,
  229. .set_params = zl10039_set_params,
  230. };
  231. struct dvb_frontend *zl10039_attach(struct dvb_frontend *fe,
  232. u8 i2c_addr, struct i2c_adapter *i2c)
  233. {
  234. struct zl10039_state *state = NULL;
  235. dprintk("%s\n", __func__);
  236. state = kmalloc(sizeof(struct zl10039_state), GFP_KERNEL);
  237. if (state == NULL)
  238. goto error;
  239. state->i2c = i2c;
  240. state->i2c_addr = i2c_addr;
  241. /* Open i2c gate */
  242. if (fe->ops.i2c_gate_ctrl)
  243. fe->ops.i2c_gate_ctrl(fe, 1);
  244. /* check if this is a valid tuner */
  245. if (zl10039_readreg(state, GENERAL, &state->id) < 0) {
  246. /* Close i2c gate */
  247. if (fe->ops.i2c_gate_ctrl)
  248. fe->ops.i2c_gate_ctrl(fe, 0);
  249. goto error;
  250. }
  251. /* Close i2c gate */
  252. if (fe->ops.i2c_gate_ctrl)
  253. fe->ops.i2c_gate_ctrl(fe, 0);
  254. state->id = state->id & 0x0f;
  255. switch (state->id) {
  256. case ID_ZL10039:
  257. strcpy(fe->ops.tuner_ops.info.name,
  258. "Zarlink ZL10039 DVB-S tuner");
  259. break;
  260. default:
  261. dprintk("Chip ID=%x does not match a known type\n", state->id);
  262. goto error;
  263. }
  264. memcpy(&fe->ops.tuner_ops, &zl10039_ops, sizeof(struct dvb_tuner_ops));
  265. fe->tuner_priv = state;
  266. dprintk("Tuner attached @ i2c address 0x%02x\n", i2c_addr);
  267. return fe;
  268. error:
  269. kfree(state);
  270. return NULL;
  271. }
  272. EXPORT_SYMBOL(zl10039_attach);
  273. module_param(debug, int, 0644);
  274. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  275. MODULE_DESCRIPTION("Zarlink ZL10039 DVB-S tuner driver");
  276. MODULE_AUTHOR("Jan D. Louw <jd.louw@mweb.co.za>");
  277. MODULE_LICENSE("GPL");