zl10039.c 6.5 KB

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