ix2505v.c 7.7 KB

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