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