stb6000.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. Driver for ST STB6000 DVBS Silicon tuner
  3. Copyright (C) 2008 Igor M. Liplianin (liplianin@me.by)
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <linux/slab.h>
  17. #include <linux/module.h>
  18. #include <linux/dvb/frontend.h>
  19. #include <asm/types.h>
  20. #include "stb6000.h"
  21. static int debug;
  22. #define dprintk(args...) \
  23. do { \
  24. if (debug) \
  25. printk(KERN_DEBUG "stb6000: " args); \
  26. } while (0)
  27. struct stb6000_priv {
  28. /* i2c details */
  29. int i2c_address;
  30. struct i2c_adapter *i2c;
  31. u32 frequency;
  32. };
  33. static int stb6000_release(struct dvb_frontend *fe)
  34. {
  35. kfree(fe->tuner_priv);
  36. fe->tuner_priv = NULL;
  37. return 0;
  38. }
  39. static int stb6000_sleep(struct dvb_frontend *fe)
  40. {
  41. struct stb6000_priv *priv = fe->tuner_priv;
  42. int ret;
  43. u8 buf[] = { 10, 0 };
  44. struct i2c_msg msg = {
  45. .addr = priv->i2c_address,
  46. .flags = 0,
  47. .buf = buf,
  48. .len = 2
  49. };
  50. dprintk("%s:\n", __func__);
  51. if (fe->ops.i2c_gate_ctrl)
  52. fe->ops.i2c_gate_ctrl(fe, 1);
  53. ret = i2c_transfer(priv->i2c, &msg, 1);
  54. if (ret != 1)
  55. dprintk("%s: i2c error\n", __func__);
  56. if (fe->ops.i2c_gate_ctrl)
  57. fe->ops.i2c_gate_ctrl(fe, 0);
  58. return (ret == 1) ? 0 : ret;
  59. }
  60. static int stb6000_set_params(struct dvb_frontend *fe,
  61. struct dvb_frontend_parameters *params)
  62. {
  63. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  64. struct stb6000_priv *priv = fe->tuner_priv;
  65. unsigned int n, m;
  66. int ret;
  67. u32 freq_mhz;
  68. int bandwidth;
  69. u8 buf[12];
  70. struct i2c_msg msg = {
  71. .addr = priv->i2c_address,
  72. .flags = 0,
  73. .buf = buf,
  74. .len = 12
  75. };
  76. dprintk("%s:\n", __func__);
  77. freq_mhz = p->frequency / 1000;
  78. bandwidth = p->symbol_rate / 1000000;
  79. if (bandwidth > 31)
  80. bandwidth = 31;
  81. if ((freq_mhz > 949) && (freq_mhz < 2151)) {
  82. buf[0] = 0x01;
  83. buf[1] = 0xac;
  84. if (freq_mhz < 1950)
  85. buf[1] = 0xaa;
  86. if (freq_mhz < 1800)
  87. buf[1] = 0xa8;
  88. if (freq_mhz < 1650)
  89. buf[1] = 0xa6;
  90. if (freq_mhz < 1530)
  91. buf[1] = 0xa5;
  92. if (freq_mhz < 1470)
  93. buf[1] = 0xa4;
  94. if (freq_mhz < 1370)
  95. buf[1] = 0xa2;
  96. if (freq_mhz < 1300)
  97. buf[1] = 0xa1;
  98. if (freq_mhz < 1200)
  99. buf[1] = 0xa0;
  100. if (freq_mhz < 1075)
  101. buf[1] = 0xbc;
  102. if (freq_mhz < 1000)
  103. buf[1] = 0xba;
  104. if (freq_mhz < 1075) {
  105. n = freq_mhz / 8; /* vco=lo*4 */
  106. m = 2;
  107. } else {
  108. n = freq_mhz / 16; /* vco=lo*2 */
  109. m = 1;
  110. }
  111. buf[2] = n >> 1;
  112. buf[3] = (unsigned char)(((n & 1) << 7) |
  113. (m * freq_mhz - n * 16) | 0x60);
  114. buf[4] = 0x04;
  115. buf[5] = 0x0e;
  116. buf[6] = (unsigned char)(bandwidth);
  117. buf[7] = 0xd8;
  118. buf[8] = 0xd0;
  119. buf[9] = 0x50;
  120. buf[10] = 0xeb;
  121. buf[11] = 0x4f;
  122. if (fe->ops.i2c_gate_ctrl)
  123. fe->ops.i2c_gate_ctrl(fe, 1);
  124. ret = i2c_transfer(priv->i2c, &msg, 1);
  125. if (ret != 1)
  126. dprintk("%s: i2c error\n", __func__);
  127. udelay(10);
  128. if (fe->ops.i2c_gate_ctrl)
  129. fe->ops.i2c_gate_ctrl(fe, 0);
  130. buf[0] = 0x07;
  131. buf[1] = 0xdf;
  132. buf[2] = 0xd0;
  133. buf[3] = 0x50;
  134. buf[4] = 0xfb;
  135. msg.len = 5;
  136. if (fe->ops.i2c_gate_ctrl)
  137. fe->ops.i2c_gate_ctrl(fe, 1);
  138. ret = i2c_transfer(priv->i2c, &msg, 1);
  139. if (ret != 1)
  140. dprintk("%s: i2c error\n", __func__);
  141. udelay(10);
  142. if (fe->ops.i2c_gate_ctrl)
  143. fe->ops.i2c_gate_ctrl(fe, 0);
  144. priv->frequency = freq_mhz * 1000;
  145. return (ret == 1) ? 0 : ret;
  146. }
  147. return -1;
  148. }
  149. static int stb6000_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  150. {
  151. struct stb6000_priv *priv = fe->tuner_priv;
  152. *frequency = priv->frequency;
  153. return 0;
  154. }
  155. static struct dvb_tuner_ops stb6000_tuner_ops = {
  156. .info = {
  157. .name = "ST STB6000",
  158. .frequency_min = 950000,
  159. .frequency_max = 2150000
  160. },
  161. .release = stb6000_release,
  162. .sleep = stb6000_sleep,
  163. .set_params = stb6000_set_params,
  164. .get_frequency = stb6000_get_frequency,
  165. };
  166. struct dvb_frontend *stb6000_attach(struct dvb_frontend *fe, int addr,
  167. struct i2c_adapter *i2c)
  168. {
  169. struct stb6000_priv *priv = NULL;
  170. u8 b0[] = { 0 };
  171. u8 b1[] = { 0, 0 };
  172. struct i2c_msg msg[2] = {
  173. {
  174. .addr = addr,
  175. .flags = 0,
  176. .buf = b0,
  177. .len = 0
  178. }, {
  179. .addr = addr,
  180. .flags = I2C_M_RD,
  181. .buf = b1,
  182. .len = 2
  183. }
  184. };
  185. int ret;
  186. dprintk("%s:\n", __func__);
  187. if (fe->ops.i2c_gate_ctrl)
  188. fe->ops.i2c_gate_ctrl(fe, 1);
  189. /* is some i2c device here ? */
  190. ret = i2c_transfer(i2c, msg, 2);
  191. if (fe->ops.i2c_gate_ctrl)
  192. fe->ops.i2c_gate_ctrl(fe, 0);
  193. if (ret != 2)
  194. return NULL;
  195. priv = kzalloc(sizeof(struct stb6000_priv), GFP_KERNEL);
  196. if (priv == NULL)
  197. return NULL;
  198. priv->i2c_address = addr;
  199. priv->i2c = i2c;
  200. memcpy(&fe->ops.tuner_ops, &stb6000_tuner_ops,
  201. sizeof(struct dvb_tuner_ops));
  202. fe->tuner_priv = priv;
  203. return fe;
  204. }
  205. EXPORT_SYMBOL(stb6000_attach);
  206. module_param(debug, int, 0644);
  207. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  208. MODULE_DESCRIPTION("DVB STB6000 driver");
  209. MODULE_AUTHOR("Igor M. Liplianin <liplianin@me.by>");
  210. MODULE_LICENSE("GPL");