ix2505v.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. struct dvb_frontend_parameters *params)
  114. {
  115. struct dtv_frontend_properties *c = &fe->dtv_property_cache;
  116. struct ix2505v_state *state = fe->tuner_priv;
  117. u32 frequency = c->frequency;
  118. u32 b_w = (c->symbol_rate * 27) / 32000;
  119. u32 div_factor, N , A, x;
  120. int ret = 0, len;
  121. u8 gain, cc, ref, psc, local_osc, lpf;
  122. u8 data[4] = {0};
  123. if ((frequency < fe->ops.info.frequency_min)
  124. || (frequency > fe->ops.info.frequency_max))
  125. return -EINVAL;
  126. if (state->config->tuner_gain)
  127. gain = (state->config->tuner_gain < 4)
  128. ? state->config->tuner_gain : 0;
  129. else
  130. gain = 0x0;
  131. if (state->config->tuner_chargepump)
  132. cc = state->config->tuner_chargepump;
  133. else
  134. cc = 0x3;
  135. ref = 8; /* REF =1 */
  136. psc = 32; /* PSC = 0 */
  137. div_factor = (frequency * ref) / 40; /* local osc = 4Mhz */
  138. x = div_factor / psc;
  139. N = x/100;
  140. A = ((x - (N * 100)) * psc) / 100;
  141. data[0] = ((gain & 0x3) << 5) | (N >> 3);
  142. data[1] = (N << 5) | (A & 0x1f);
  143. data[2] = 0x81 | ((cc & 0x3) << 5) ; /*PD5,PD4 & TM = 0|C1,C0|REF=1*/
  144. deb_info("Frq=%d x=%d N=%d A=%d\n", frequency, x, N, A);
  145. if (frequency <= 1065000)
  146. local_osc = (6 << 5) | 2;
  147. else if (frequency <= 1170000)
  148. local_osc = (7 << 5) | 2;
  149. else if (frequency <= 1300000)
  150. local_osc = (1 << 5);
  151. else if (frequency <= 1445000)
  152. local_osc = (2 << 5);
  153. else if (frequency <= 1607000)
  154. local_osc = (3 << 5);
  155. else if (frequency <= 1778000)
  156. local_osc = (4 << 5);
  157. else if (frequency <= 1942000)
  158. local_osc = (5 << 5);
  159. else /*frequency up to 2150000*/
  160. local_osc = (6 << 5);
  161. data[3] = local_osc; /* all other bits set 0 */
  162. if (b_w <= 10000)
  163. lpf = 0xc;
  164. else if (b_w <= 12000)
  165. lpf = 0x2;
  166. else if (b_w <= 14000)
  167. lpf = 0xa;
  168. else if (b_w <= 16000)
  169. lpf = 0x6;
  170. else if (b_w <= 18000)
  171. lpf = 0xe;
  172. else if (b_w <= 20000)
  173. lpf = 0x1;
  174. else if (b_w <= 22000)
  175. lpf = 0x9;
  176. else if (b_w <= 24000)
  177. lpf = 0x5;
  178. else if (b_w <= 26000)
  179. lpf = 0xd;
  180. else if (b_w <= 28000)
  181. lpf = 0x3;
  182. else
  183. lpf = 0xb;
  184. deb_info("Osc=%x b_w=%x lpf=%x\n", local_osc, b_w, lpf);
  185. deb_info("Data 0=[%x%x%x%x]\n", data[0], data[1], data[2], data[3]);
  186. if (fe->ops.i2c_gate_ctrl)
  187. fe->ops.i2c_gate_ctrl(fe, 1);
  188. len = sizeof(data);
  189. ret |= ix2505v_write(state, data, len);
  190. data[2] |= 0x4; /* set TM = 1 other bits same */
  191. if (fe->ops.i2c_gate_ctrl)
  192. fe->ops.i2c_gate_ctrl(fe, 1);
  193. len = 1;
  194. ret |= ix2505v_write(state, &data[2], len); /* write byte 4 only */
  195. msleep(10);
  196. data[2] |= ((lpf >> 2) & 0x3) << 3; /* lpf */
  197. data[3] |= (lpf & 0x3) << 2;
  198. deb_info("Data 2=[%x%x]\n", data[2], data[3]);
  199. if (fe->ops.i2c_gate_ctrl)
  200. fe->ops.i2c_gate_ctrl(fe, 1);
  201. len = 2;
  202. ret |= ix2505v_write(state, &data[2], len); /* write byte 4 & 5 */
  203. if (state->config->min_delay_ms)
  204. msleep(state->config->min_delay_ms);
  205. state->frequency = frequency;
  206. return ret;
  207. }
  208. static int ix2505v_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  209. {
  210. struct ix2505v_state *state = fe->tuner_priv;
  211. *frequency = state->frequency;
  212. return 0;
  213. }
  214. static struct dvb_tuner_ops ix2505v_tuner_ops = {
  215. .info = {
  216. .name = "Sharp IX2505V (B0017)",
  217. .frequency_min = 950000,
  218. .frequency_max = 2175000
  219. },
  220. .release = ix2505v_release,
  221. .set_params = ix2505v_set_params,
  222. .get_frequency = ix2505v_get_frequency,
  223. };
  224. struct dvb_frontend *ix2505v_attach(struct dvb_frontend *fe,
  225. const struct ix2505v_config *config,
  226. struct i2c_adapter *i2c)
  227. {
  228. struct ix2505v_state *state = NULL;
  229. int ret;
  230. if (NULL == config) {
  231. deb_i2c("%s: no config ", __func__);
  232. goto error;
  233. }
  234. state = kzalloc(sizeof(struct ix2505v_state), GFP_KERNEL);
  235. if (NULL == state)
  236. return NULL;
  237. state->config = config;
  238. state->i2c = i2c;
  239. if (state->config->tuner_write_only) {
  240. if (fe->ops.i2c_gate_ctrl)
  241. fe->ops.i2c_gate_ctrl(fe, 1);
  242. ret = ix2505v_read_status_reg(state);
  243. if (ret & 0x80) {
  244. deb_i2c("%s: No IX2505V found\n", __func__);
  245. goto error;
  246. }
  247. if (fe->ops.i2c_gate_ctrl)
  248. fe->ops.i2c_gate_ctrl(fe, 0);
  249. }
  250. fe->tuner_priv = state;
  251. memcpy(&fe->ops.tuner_ops, &ix2505v_tuner_ops,
  252. sizeof(struct dvb_tuner_ops));
  253. deb_i2c("%s: initialization (%s addr=0x%02x) ok\n",
  254. __func__, fe->ops.tuner_ops.info.name, config->tuner_address);
  255. return fe;
  256. error:
  257. kfree(state);
  258. return NULL;
  259. }
  260. EXPORT_SYMBOL(ix2505v_attach);
  261. module_param_named(debug, ix2505v_debug, int, 0644);
  262. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  263. MODULE_DESCRIPTION("DVB IX2505V tuner driver");
  264. MODULE_AUTHOR("Malcolm Priestley");
  265. MODULE_LICENSE("GPL");