zl10039.c 6.8 KB

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