ix2505v.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Driver for Sharp IX2505V (marked B0017) DVB-S silicon tuner
  4. *
  5. * Copyright (C) 2010 Malcolm Priestley
  6. */
  7. #include <linux/module.h>
  8. #include <linux/dvb/frontend.h>
  9. #include <linux/slab.h>
  10. #include <linux/types.h>
  11. #include "ix2505v.h"
  12. static int ix2505v_debug;
  13. #define dprintk(level, args...) do { \
  14. if (ix2505v_debug & level) \
  15. printk(KERN_DEBUG "ix2505v: " args); \
  16. } while (0)
  17. #define deb_info(args...) dprintk(0x01, args)
  18. #define deb_i2c(args...) dprintk(0x02, args)
  19. struct ix2505v_state {
  20. struct i2c_adapter *i2c;
  21. const struct ix2505v_config *config;
  22. u32 frequency;
  23. };
  24. /*
  25. * Data read format of the Sharp IX2505V B0017
  26. *
  27. * byte1: 1 | 1 | 0 | 0 | 0 | MA1 | MA0 | 1
  28. * byte2: POR | FL | RD2 | RD1 | RD0 | X | X | X
  29. *
  30. * byte1 = address
  31. * byte2;
  32. * POR = Power on Reset (VCC H=<2.2v L=>2.2v)
  33. * FL = Phase Lock (H=lock L=unlock)
  34. * RD0-2 = Reserved internal operations
  35. *
  36. * Only POR can be used to check the tuner is present
  37. *
  38. * Caution: after byte2 the I2C reverts to write mode continuing to read
  39. * may corrupt tuning data.
  40. *
  41. */
  42. static int ix2505v_read_status_reg(struct ix2505v_state *state)
  43. {
  44. u8 addr = state->config->tuner_address;
  45. u8 b2[] = {0};
  46. int ret;
  47. struct i2c_msg msg[1] = {
  48. { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 }
  49. };
  50. ret = i2c_transfer(state->i2c, msg, 1);
  51. deb_i2c("Read %s ", __func__);
  52. return (ret == 1) ? (int) b2[0] : -1;
  53. }
  54. static int ix2505v_write(struct ix2505v_state *state, u8 buf[], u8 count)
  55. {
  56. struct i2c_msg msg[1] = {
  57. { .addr = state->config->tuner_address, .flags = 0,
  58. .buf = buf, .len = count },
  59. };
  60. int ret;
  61. ret = i2c_transfer(state->i2c, msg, 1);
  62. if (ret != 1) {
  63. deb_i2c("%s: i2c error, ret=%d\n", __func__, ret);
  64. return -EIO;
  65. }
  66. return 0;
  67. }
  68. static void ix2505v_release(struct dvb_frontend *fe)
  69. {
  70. struct ix2505v_state *state = fe->tuner_priv;
  71. fe->tuner_priv = NULL;
  72. kfree(state);
  73. }
  74. /*
  75. * Data write format of the Sharp IX2505V B0017
  76. *
  77. * byte1: 1 | 1 | 0 | 0 | 0 | 0(MA1)| 0(MA0)| 0
  78. * byte2: 0 | BG1 | BG2 | N8 | N7 | N6 | N5 | N4
  79. * byte3: N3 | N2 | N1 | A5 | A4 | A3 | A2 | A1
  80. * byte4: 1 | 1(C1) | 1(C0) | PD5 | PD4 | TM | 0(RTS)| 1(REF)
  81. * byte5: BA2 | BA1 | BA0 | PSC | PD3 |PD2/TS2|DIV/TS1|PD0/TS0
  82. *
  83. * byte1 = address
  84. *
  85. * Write order
  86. * 1) byte1 -> byte2 -> byte3 -> byte4 -> byte5
  87. * 2) byte1 -> byte4 -> byte5 -> byte2 -> byte3
  88. * 3) byte1 -> byte2 -> byte3 -> byte4
  89. * 4) byte1 -> byte4 -> byte5 -> byte2
  90. * 5) byte1 -> byte2 -> byte3
  91. * 6) byte1 -> byte4 -> byte5
  92. * 7) byte1 -> byte2
  93. * 8) byte1 -> byte4
  94. *
  95. * Recommended Setup
  96. * 1 -> 8 -> 6
  97. */
  98. static int ix2505v_set_params(struct dvb_frontend *fe)
  99. {
  100. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  101. struct ix2505v_state *state = fe->tuner_priv;
  102. u32 frequency = c->frequency;
  103. u32 b_w = (c->symbol_rate * 27) / 32000;
  104. u32 div_factor, N , A, x;
  105. int ret = 0, len;
  106. u8 gain, cc, ref, psc, local_osc, lpf;
  107. u8 data[4] = {0};
  108. if ((frequency < fe->ops.info.frequency_min_hz / kHz)
  109. || (frequency > fe->ops.info.frequency_max_hz / kHz))
  110. return -EINVAL;
  111. if (state->config->tuner_gain)
  112. gain = (state->config->tuner_gain < 4)
  113. ? state->config->tuner_gain : 0;
  114. else
  115. gain = 0x0;
  116. if (state->config->tuner_chargepump)
  117. cc = state->config->tuner_chargepump;
  118. else
  119. cc = 0x3;
  120. ref = 8; /* REF =1 */
  121. psc = 32; /* PSC = 0 */
  122. div_factor = (frequency * ref) / 40; /* local osc = 4Mhz */
  123. x = div_factor / psc;
  124. N = x/100;
  125. A = ((x - (N * 100)) * psc) / 100;
  126. data[0] = ((gain & 0x3) << 5) | (N >> 3);
  127. data[1] = (N << 5) | (A & 0x1f);
  128. data[2] = 0x81 | ((cc & 0x3) << 5) ; /*PD5,PD4 & TM = 0|C1,C0|REF=1*/
  129. deb_info("Frq=%d x=%d N=%d A=%d\n", frequency, x, N, A);
  130. if (frequency <= 1065000)
  131. local_osc = (6 << 5) | 2;
  132. else if (frequency <= 1170000)
  133. local_osc = (7 << 5) | 2;
  134. else if (frequency <= 1300000)
  135. local_osc = (1 << 5);
  136. else if (frequency <= 1445000)
  137. local_osc = (2 << 5);
  138. else if (frequency <= 1607000)
  139. local_osc = (3 << 5);
  140. else if (frequency <= 1778000)
  141. local_osc = (4 << 5);
  142. else if (frequency <= 1942000)
  143. local_osc = (5 << 5);
  144. else /*frequency up to 2150000*/
  145. local_osc = (6 << 5);
  146. data[3] = local_osc; /* all other bits set 0 */
  147. if (b_w <= 10000)
  148. lpf = 0xc;
  149. else if (b_w <= 12000)
  150. lpf = 0x2;
  151. else if (b_w <= 14000)
  152. lpf = 0xa;
  153. else if (b_w <= 16000)
  154. lpf = 0x6;
  155. else if (b_w <= 18000)
  156. lpf = 0xe;
  157. else if (b_w <= 20000)
  158. lpf = 0x1;
  159. else if (b_w <= 22000)
  160. lpf = 0x9;
  161. else if (b_w <= 24000)
  162. lpf = 0x5;
  163. else if (b_w <= 26000)
  164. lpf = 0xd;
  165. else if (b_w <= 28000)
  166. lpf = 0x3;
  167. else
  168. lpf = 0xb;
  169. deb_info("Osc=%x b_w=%x lpf=%x\n", local_osc, b_w, lpf);
  170. deb_info("Data 0=[%4phN]\n", data);
  171. if (fe->ops.i2c_gate_ctrl)
  172. fe->ops.i2c_gate_ctrl(fe, 1);
  173. len = sizeof(data);
  174. ret |= ix2505v_write(state, data, len);
  175. data[2] |= 0x4; /* set TM = 1 other bits same */
  176. if (fe->ops.i2c_gate_ctrl)
  177. fe->ops.i2c_gate_ctrl(fe, 1);
  178. len = 1;
  179. ret |= ix2505v_write(state, &data[2], len); /* write byte 4 only */
  180. msleep(10);
  181. data[2] |= ((lpf >> 2) & 0x3) << 3; /* lpf */
  182. data[3] |= (lpf & 0x3) << 2;
  183. deb_info("Data 2=[%x%x]\n", data[2], data[3]);
  184. if (fe->ops.i2c_gate_ctrl)
  185. fe->ops.i2c_gate_ctrl(fe, 1);
  186. len = 2;
  187. ret |= ix2505v_write(state, &data[2], len); /* write byte 4 & 5 */
  188. if (state->config->min_delay_ms)
  189. msleep(state->config->min_delay_ms);
  190. state->frequency = frequency;
  191. return ret;
  192. }
  193. static int ix2505v_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  194. {
  195. struct ix2505v_state *state = fe->tuner_priv;
  196. *frequency = state->frequency;
  197. return 0;
  198. }
  199. static const struct dvb_tuner_ops ix2505v_tuner_ops = {
  200. .info = {
  201. .name = "Sharp IX2505V (B0017)",
  202. .frequency_min_hz = 950 * MHz,
  203. .frequency_max_hz = 2175 * MHz
  204. },
  205. .release = ix2505v_release,
  206. .set_params = ix2505v_set_params,
  207. .get_frequency = ix2505v_get_frequency,
  208. };
  209. struct dvb_frontend *ix2505v_attach(struct dvb_frontend *fe,
  210. const struct ix2505v_config *config,
  211. struct i2c_adapter *i2c)
  212. {
  213. struct ix2505v_state *state = NULL;
  214. int ret;
  215. if (NULL == config) {
  216. deb_i2c("%s: no config ", __func__);
  217. goto error;
  218. }
  219. state = kzalloc(sizeof(struct ix2505v_state), GFP_KERNEL);
  220. if (NULL == state)
  221. return NULL;
  222. state->config = config;
  223. state->i2c = i2c;
  224. if (state->config->tuner_write_only) {
  225. if (fe->ops.i2c_gate_ctrl)
  226. fe->ops.i2c_gate_ctrl(fe, 1);
  227. ret = ix2505v_read_status_reg(state);
  228. if (ret & 0x80) {
  229. deb_i2c("%s: No IX2505V found\n", __func__);
  230. goto error;
  231. }
  232. if (fe->ops.i2c_gate_ctrl)
  233. fe->ops.i2c_gate_ctrl(fe, 0);
  234. }
  235. fe->tuner_priv = state;
  236. memcpy(&fe->ops.tuner_ops, &ix2505v_tuner_ops,
  237. sizeof(struct dvb_tuner_ops));
  238. deb_i2c("%s: initialization (%s addr=0x%02x) ok\n",
  239. __func__, fe->ops.tuner_ops.info.name, config->tuner_address);
  240. return fe;
  241. error:
  242. kfree(state);
  243. return NULL;
  244. }
  245. EXPORT_SYMBOL(ix2505v_attach);
  246. module_param_named(debug, ix2505v_debug, int, 0644);
  247. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  248. MODULE_DESCRIPTION("DVB IX2505V tuner driver");
  249. MODULE_AUTHOR("Malcolm Priestley");
  250. MODULE_LICENSE("GPL");