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