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