ix2505v.c 7.7 KB

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