stb6000.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. {
  62. struct dtv_frontend_properties *p = &fe->dtv_property_cache;
  63. struct stb6000_priv *priv = fe->tuner_priv;
  64. unsigned int n, m;
  65. int ret;
  66. u32 freq_mhz;
  67. int bandwidth;
  68. u8 buf[12];
  69. struct i2c_msg msg = {
  70. .addr = priv->i2c_address,
  71. .flags = 0,
  72. .buf = buf,
  73. .len = 12
  74. };
  75. dprintk("%s:\n", __func__);
  76. freq_mhz = p->frequency / 1000;
  77. bandwidth = p->symbol_rate / 1000000;
  78. if (bandwidth > 31)
  79. bandwidth = 31;
  80. if ((freq_mhz > 949) && (freq_mhz < 2151)) {
  81. buf[0] = 0x01;
  82. buf[1] = 0xac;
  83. if (freq_mhz < 1950)
  84. buf[1] = 0xaa;
  85. if (freq_mhz < 1800)
  86. buf[1] = 0xa8;
  87. if (freq_mhz < 1650)
  88. buf[1] = 0xa6;
  89. if (freq_mhz < 1530)
  90. buf[1] = 0xa5;
  91. if (freq_mhz < 1470)
  92. buf[1] = 0xa4;
  93. if (freq_mhz < 1370)
  94. buf[1] = 0xa2;
  95. if (freq_mhz < 1300)
  96. buf[1] = 0xa1;
  97. if (freq_mhz < 1200)
  98. buf[1] = 0xa0;
  99. if (freq_mhz < 1075)
  100. buf[1] = 0xbc;
  101. if (freq_mhz < 1000)
  102. buf[1] = 0xba;
  103. if (freq_mhz < 1075) {
  104. n = freq_mhz / 8; /* vco=lo*4 */
  105. m = 2;
  106. } else {
  107. n = freq_mhz / 16; /* vco=lo*2 */
  108. m = 1;
  109. }
  110. buf[2] = n >> 1;
  111. buf[3] = (unsigned char)(((n & 1) << 7) |
  112. (m * freq_mhz - n * 16) | 0x60);
  113. buf[4] = 0x04;
  114. buf[5] = 0x0e;
  115. buf[6] = (unsigned char)(bandwidth);
  116. buf[7] = 0xd8;
  117. buf[8] = 0xd0;
  118. buf[9] = 0x50;
  119. buf[10] = 0xeb;
  120. buf[11] = 0x4f;
  121. if (fe->ops.i2c_gate_ctrl)
  122. fe->ops.i2c_gate_ctrl(fe, 1);
  123. ret = i2c_transfer(priv->i2c, &msg, 1);
  124. if (ret != 1)
  125. dprintk("%s: i2c error\n", __func__);
  126. udelay(10);
  127. if (fe->ops.i2c_gate_ctrl)
  128. fe->ops.i2c_gate_ctrl(fe, 0);
  129. buf[0] = 0x07;
  130. buf[1] = 0xdf;
  131. buf[2] = 0xd0;
  132. buf[3] = 0x50;
  133. buf[4] = 0xfb;
  134. msg.len = 5;
  135. if (fe->ops.i2c_gate_ctrl)
  136. fe->ops.i2c_gate_ctrl(fe, 1);
  137. ret = i2c_transfer(priv->i2c, &msg, 1);
  138. if (ret != 1)
  139. dprintk("%s: i2c error\n", __func__);
  140. udelay(10);
  141. if (fe->ops.i2c_gate_ctrl)
  142. fe->ops.i2c_gate_ctrl(fe, 0);
  143. priv->frequency = freq_mhz * 1000;
  144. return (ret == 1) ? 0 : ret;
  145. }
  146. return -1;
  147. }
  148. static int stb6000_get_frequency(struct dvb_frontend *fe, u32 *frequency)
  149. {
  150. struct stb6000_priv *priv = fe->tuner_priv;
  151. *frequency = priv->frequency;
  152. return 0;
  153. }
  154. static struct dvb_tuner_ops stb6000_tuner_ops = {
  155. .info = {
  156. .name = "ST STB6000",
  157. .frequency_min = 950000,
  158. .frequency_max = 2150000
  159. },
  160. .release = stb6000_release,
  161. .sleep = stb6000_sleep,
  162. .set_params = stb6000_set_params,
  163. .get_frequency = stb6000_get_frequency,
  164. };
  165. struct dvb_frontend *stb6000_attach(struct dvb_frontend *fe, int addr,
  166. struct i2c_adapter *i2c)
  167. {
  168. struct stb6000_priv *priv = NULL;
  169. u8 b0[] = { 0 };
  170. u8 b1[] = { 0, 0 };
  171. struct i2c_msg msg[2] = {
  172. {
  173. .addr = addr,
  174. .flags = 0,
  175. .buf = b0,
  176. .len = 0
  177. }, {
  178. .addr = addr,
  179. .flags = I2C_M_RD,
  180. .buf = b1,
  181. .len = 2
  182. }
  183. };
  184. int ret;
  185. dprintk("%s:\n", __func__);
  186. if (fe->ops.i2c_gate_ctrl)
  187. fe->ops.i2c_gate_ctrl(fe, 1);
  188. /* is some i2c device here ? */
  189. ret = i2c_transfer(i2c, msg, 2);
  190. if (fe->ops.i2c_gate_ctrl)
  191. fe->ops.i2c_gate_ctrl(fe, 0);
  192. if (ret != 2)
  193. return NULL;
  194. priv = kzalloc(sizeof(struct stb6000_priv), GFP_KERNEL);
  195. if (priv == NULL)
  196. return NULL;
  197. priv->i2c_address = addr;
  198. priv->i2c = i2c;
  199. memcpy(&fe->ops.tuner_ops, &stb6000_tuner_ops,
  200. sizeof(struct dvb_tuner_ops));
  201. fe->tuner_priv = priv;
  202. return fe;
  203. }
  204. EXPORT_SYMBOL(stb6000_attach);
  205. module_param(debug, int, 0644);
  206. MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
  207. MODULE_DESCRIPTION("DVB STB6000 driver");
  208. MODULE_AUTHOR("Igor M. Liplianin <liplianin@me.by>");
  209. MODULE_LICENSE("GPL");