blob: aca48172504cf41ed92cfa41a0a95cc86529e25f [file] [log] [blame]
Malcolm Priestley1ae2c582010-08-28 18:07:37 -03001/**
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
21#include <linux/module.h>
22#include <linux/dvb/frontend.h>
23#include <linux/slab.h>
24#include <linux/types.h>
25
26#include "ix2505v.h"
27
28static int ix2505v_debug;
Mauro Carvalho Chehab9d10f3d2010-09-08 12:51:56 -030029#define dprintk(level, args...) do { \
30 if (ix2505v_debug & level) \
31 printk(KERN_DEBUG "ix2505v: " args); \
32} while (0)
Malcolm Priestley1ae2c582010-08-28 18:07:37 -030033
34#define deb_info(args...) dprintk(0x01, args)
35#define deb_i2c(args...) dprintk(0x02, args)
36
37struct ix2505v_state {
38 struct i2c_adapter *i2c;
39 const struct ix2505v_config *config;
40 u32 frequency;
41};
42
43/**
44 * Data read format of the Sharp IX2505V B0017
45 *
46 * byte1: 1 | 1 | 0 | 0 | 0 | MA1 | MA0 | 1
47 * byte2: POR | FL | RD2 | RD1 | RD0 | X | X | X
48 *
49 * byte1 = address
50 * byte2;
Mauro Carvalho Chehab9d10f3d2010-09-08 12:51:56 -030051 * POR = Power on Reset (VCC H=<2.2v L=>2.2v)
Malcolm Priestley1ae2c582010-08-28 18:07:37 -030052 * FL = Phase Lock (H=lock L=unlock)
53 * RD0-2 = Reserved internal operations
54 *
55 * Only POR can be used to check the tuner is present
56 *
57 * Caution: after byte2 the I2C reverts to write mode continuing to read
58 * may corrupt tuning data.
59 *
60 */
61
62static int ix2505v_read_status_reg(struct ix2505v_state *state)
63{
64 u8 addr = state->config->tuner_address;
65 u8 b2[] = {0};
66 int ret;
67
68 struct i2c_msg msg[1] = {
69 { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 }
70 };
71
72 ret = i2c_transfer(state->i2c, msg, 1);
73 deb_i2c("Read %s ", __func__);
74
Matthias Schwarzott13d28e42010-11-07 10:57:13 -030075 return (ret == 1) ? (int) b2[0] : -1;
Malcolm Priestley1ae2c582010-08-28 18:07:37 -030076}
77
78static int ix2505v_write(struct ix2505v_state *state, u8 buf[], u8 count)
79{
80 struct i2c_msg msg[1] = {
81 { .addr = state->config->tuner_address, .flags = 0,
82 .buf = buf, .len = count },
83 };
84
85 int ret;
86
87 ret = i2c_transfer(state->i2c, msg, 1);
88
89 if (ret != 1) {
90 deb_i2c("%s: i2c error, ret=%d\n", __func__, ret);
91 return -EIO;
92 }
93
94 return 0;
95}
96
97static int ix2505v_release(struct dvb_frontend *fe)
98{
99 struct ix2505v_state *state = fe->tuner_priv;
100
101 fe->tuner_priv = NULL;
102 kfree(state);
103
104 return 0;
105}
106
107/**
108 * Data write format of the Sharp IX2505V B0017
109 *
110 * byte1: 1 | 1 | 0 | 0 | 0 | 0(MA1)| 0(MA0)| 0
111 * byte2: 0 | BG1 | BG2 | N8 | N7 | N6 | N5 | N4
112 * byte3: N3 | N2 | N1 | A5 | A4 | A3 | A2 | A1
113 * byte4: 1 | 1(C1) | 1(C0) | PD5 | PD4 | TM | 0(RTS)| 1(REF)
114 * byte5: BA2 | BA1 | BA0 | PSC | PD3 |PD2/TS2|DIV/TS1|PD0/TS0
115 *
116 * byte1 = address
117 *
118 * Write order
119 * 1) byte1 -> byte2 -> byte3 -> byte4 -> byte5
120 * 2) byte1 -> byte4 -> byte5 -> byte2 -> byte3
121 * 3) byte1 -> byte2 -> byte3 -> byte4
122 * 4) byte1 -> byte4 -> byte5 -> byte2
123 * 5) byte1 -> byte2 -> byte3
124 * 6) byte1 -> byte4 -> byte5
125 * 7) byte1 -> byte2
126 * 8) byte1 -> byte4
127 *
128 * Recommended Setup
129 * 1 -> 8 -> 6
130 */
131
132static int ix2505v_set_params(struct dvb_frontend *fe,
133 struct dvb_frontend_parameters *params)
134{
Mauro Carvalho Chehab3cc66df2011-12-23 18:33:18 -0300135 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300136 struct ix2505v_state *state = fe->tuner_priv;
Mauro Carvalho Chehab3cc66df2011-12-23 18:33:18 -0300137 u32 frequency = c->frequency;
138 u32 b_w = (c->symbol_rate * 27) / 32000;
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300139 u32 div_factor, N , A, x;
140 int ret = 0, len;
141 u8 gain, cc, ref, psc, local_osc, lpf;
142 u8 data[4] = {0};
143
144 if ((frequency < fe->ops.info.frequency_min)
145 || (frequency > fe->ops.info.frequency_max))
146 return -EINVAL;
147
148 if (state->config->tuner_gain)
149 gain = (state->config->tuner_gain < 4)
150 ? state->config->tuner_gain : 0;
151 else
152 gain = 0x0;
153
154 if (state->config->tuner_chargepump)
155 cc = state->config->tuner_chargepump;
156 else
157 cc = 0x3;
158
159 ref = 8; /* REF =1 */
160 psc = 32; /* PSC = 0 */
161
162 div_factor = (frequency * ref) / 40; /* local osc = 4Mhz */
163 x = div_factor / psc;
164 N = x/100;
165 A = ((x - (N * 100)) * psc) / 100;
166
167 data[0] = ((gain & 0x3) << 5) | (N >> 3);
168 data[1] = (N << 5) | (A & 0x1f);
169 data[2] = 0x81 | ((cc & 0x3) << 5) ; /*PD5,PD4 & TM = 0|C1,C0|REF=1*/
170
Mauro Carvalho Chehab9d10f3d2010-09-08 12:51:56 -0300171 deb_info("Frq=%d x=%d N=%d A=%d\n", frequency, x, N, A);
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300172
173 if (frequency <= 1065000)
174 local_osc = (6 << 5) | 2;
175 else if (frequency <= 1170000)
176 local_osc = (7 << 5) | 2;
177 else if (frequency <= 1300000)
178 local_osc = (1 << 5);
179 else if (frequency <= 1445000)
180 local_osc = (2 << 5);
181 else if (frequency <= 1607000)
182 local_osc = (3 << 5);
183 else if (frequency <= 1778000)
184 local_osc = (4 << 5);
185 else if (frequency <= 1942000)
186 local_osc = (5 << 5);
Mauro Carvalho Chehab9d10f3d2010-09-08 12:51:56 -0300187 else /*frequency up to 2150000*/
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300188 local_osc = (6 << 5);
189
190 data[3] = local_osc; /* all other bits set 0 */
191
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300192 if (b_w <= 10000)
193 lpf = 0xc;
194 else if (b_w <= 12000)
195 lpf = 0x2;
196 else if (b_w <= 14000)
197 lpf = 0xa;
198 else if (b_w <= 16000)
199 lpf = 0x6;
200 else if (b_w <= 18000)
201 lpf = 0xe;
202 else if (b_w <= 20000)
203 lpf = 0x1;
204 else if (b_w <= 22000)
205 lpf = 0x9;
206 else if (b_w <= 24000)
207 lpf = 0x5;
208 else if (b_w <= 26000)
209 lpf = 0xd;
210 else if (b_w <= 28000)
211 lpf = 0x3;
212 else
213 lpf = 0xb;
214
215 deb_info("Osc=%x b_w=%x lpf=%x\n", local_osc, b_w, lpf);
Mauro Carvalho Chehab9d10f3d2010-09-08 12:51:56 -0300216 deb_info("Data 0=[%x%x%x%x]\n", data[0], data[1], data[2], data[3]);
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300217
218 if (fe->ops.i2c_gate_ctrl)
219 fe->ops.i2c_gate_ctrl(fe, 1);
220
221 len = sizeof(data);
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300222 ret |= ix2505v_write(state, data, len);
223
224 data[2] |= 0x4; /* set TM = 1 other bits same */
225
Malcolm Priestley853e3b22011-04-22 06:00:22 -0300226 if (fe->ops.i2c_gate_ctrl)
227 fe->ops.i2c_gate_ctrl(fe, 1);
228
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300229 len = 1;
230 ret |= ix2505v_write(state, &data[2], len); /* write byte 4 only */
231
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300232 msleep(10);
233
234 data[2] |= ((lpf >> 2) & 0x3) << 3; /* lpf */
235 data[3] |= (lpf & 0x3) << 2;
236
Mauro Carvalho Chehab9d10f3d2010-09-08 12:51:56 -0300237 deb_info("Data 2=[%x%x]\n", data[2], data[3]);
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300238
Malcolm Priestley853e3b22011-04-22 06:00:22 -0300239 if (fe->ops.i2c_gate_ctrl)
240 fe->ops.i2c_gate_ctrl(fe, 1);
241
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300242 len = 2;
243 ret |= ix2505v_write(state, &data[2], len); /* write byte 4 & 5 */
244
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300245 if (state->config->min_delay_ms)
246 msleep(state->config->min_delay_ms);
247
248 state->frequency = frequency;
249
250 return ret;
251}
252
253static int ix2505v_get_frequency(struct dvb_frontend *fe, u32 *frequency)
254{
255 struct ix2505v_state *state = fe->tuner_priv;
256
257 *frequency = state->frequency;
258
259 return 0;
260}
261
262static struct dvb_tuner_ops ix2505v_tuner_ops = {
263 .info = {
264 .name = "Sharp IX2505V (B0017)",
265 .frequency_min = 950000,
266 .frequency_max = 2175000
267 },
268 .release = ix2505v_release,
269 .set_params = ix2505v_set_params,
270 .get_frequency = ix2505v_get_frequency,
271};
272
273struct dvb_frontend *ix2505v_attach(struct dvb_frontend *fe,
274 const struct ix2505v_config *config,
275 struct i2c_adapter *i2c)
276{
277 struct ix2505v_state *state = NULL;
278 int ret;
279
280 if (NULL == config) {
281 deb_i2c("%s: no config ", __func__);
282 goto error;
283 }
284
285 state = kzalloc(sizeof(struct ix2505v_state), GFP_KERNEL);
286 if (NULL == state)
287 return NULL;
288
289 state->config = config;
290 state->i2c = i2c;
291
292 if (state->config->tuner_write_only) {
293 if (fe->ops.i2c_gate_ctrl)
294 fe->ops.i2c_gate_ctrl(fe, 1);
295
296 ret = ix2505v_read_status_reg(state);
297
298 if (ret & 0x80) {
299 deb_i2c("%s: No IX2505V found\n", __func__);
300 goto error;
301 }
302
303 if (fe->ops.i2c_gate_ctrl)
304 fe->ops.i2c_gate_ctrl(fe, 0);
305 }
306
307 fe->tuner_priv = state;
308
309 memcpy(&fe->ops.tuner_ops, &ix2505v_tuner_ops,
310 sizeof(struct dvb_tuner_ops));
311 deb_i2c("%s: initialization (%s addr=0x%02x) ok\n",
312 __func__, fe->ops.tuner_ops.info.name, config->tuner_address);
313
314 return fe;
315
316error:
Jesper Juhl36fd9782011-01-02 16:14:03 -0300317 kfree(state);
Malcolm Priestley1ae2c582010-08-28 18:07:37 -0300318 return NULL;
319}
320EXPORT_SYMBOL(ix2505v_attach);
321
322module_param_named(debug, ix2505v_debug, int, 0644);
323MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
324MODULE_DESCRIPTION("DVB IX2505V tuner driver");
325MODULE_AUTHOR("Malcolm Priestley");
326MODULE_LICENSE("GPL");