diff options
Diffstat (limited to 'drivers/media/tuners/mxl301rf.c')
-rw-r--r-- | drivers/media/tuners/mxl301rf.c | 349 |
1 files changed, 349 insertions, 0 deletions
diff --git a/drivers/media/tuners/mxl301rf.c b/drivers/media/tuners/mxl301rf.c new file mode 100644 index 000000000000..1575a5db776a --- /dev/null +++ b/drivers/media/tuners/mxl301rf.c | |||
@@ -0,0 +1,349 @@ | |||
1 | /* | ||
2 | * MaxLinear MxL301RF OFDM tuner driver | ||
3 | * | ||
4 | * Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com> | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or | ||
7 | * modify it under the terms of the GNU General Public License as | ||
8 | * published by the Free Software Foundation version 2. | ||
9 | * | ||
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 | * GNU General Public License for more details. | ||
15 | */ | ||
16 | |||
17 | /* | ||
18 | * NOTICE: | ||
19 | * This driver is incomplete and lacks init/config of the chips, | ||
20 | * as the necessary info is not disclosed. | ||
21 | * Other features like get_if_frequency() are missing as well. | ||
22 | * It assumes that users of this driver (such as a PCI bridge of | ||
23 | * DTV receiver cards) properly init and configure the chip | ||
24 | * via I2C *before* calling this driver's init() function. | ||
25 | * | ||
26 | * Currently, PT3 driver is the only one that uses this driver, | ||
27 | * and contains init/config code in its firmware. | ||
28 | * Thus some part of the code might be dependent on PT3 specific config. | ||
29 | */ | ||
30 | |||
31 | #include <linux/kernel.h> | ||
32 | #include "mxl301rf.h" | ||
33 | |||
34 | struct mxl301rf_state { | ||
35 | struct mxl301rf_config cfg; | ||
36 | struct i2c_client *i2c; | ||
37 | }; | ||
38 | |||
39 | static struct mxl301rf_state *cfg_to_state(struct mxl301rf_config *c) | ||
40 | { | ||
41 | return container_of(c, struct mxl301rf_state, cfg); | ||
42 | } | ||
43 | |||
44 | static int raw_write(struct mxl301rf_state *state, const u8 *buf, int len) | ||
45 | { | ||
46 | int ret; | ||
47 | |||
48 | ret = i2c_master_send(state->i2c, buf, len); | ||
49 | if (ret >= 0 && ret < len) | ||
50 | ret = -EIO; | ||
51 | return (ret == len) ? 0 : ret; | ||
52 | } | ||
53 | |||
54 | static int reg_write(struct mxl301rf_state *state, u8 reg, u8 val) | ||
55 | { | ||
56 | u8 buf[2] = { reg, val }; | ||
57 | |||
58 | return raw_write(state, buf, 2); | ||
59 | } | ||
60 | |||
61 | static int reg_read(struct mxl301rf_state *state, u8 reg, u8 *val) | ||
62 | { | ||
63 | u8 wbuf[2] = { 0xfb, reg }; | ||
64 | int ret; | ||
65 | |||
66 | ret = raw_write(state, wbuf, sizeof(wbuf)); | ||
67 | if (ret == 0) | ||
68 | ret = i2c_master_recv(state->i2c, val, 1); | ||
69 | if (ret >= 0 && ret < 1) | ||
70 | ret = -EIO; | ||
71 | return (ret == 1) ? 0 : ret; | ||
72 | } | ||
73 | |||
74 | /* tuner_ops */ | ||
75 | |||
76 | /* get RSSI and update propery cache, set to *out in % */ | ||
77 | static int mxl301rf_get_rf_strength(struct dvb_frontend *fe, u16 *out) | ||
78 | { | ||
79 | struct mxl301rf_state *state; | ||
80 | int ret; | ||
81 | u8 rf_in1, rf_in2, rf_off1, rf_off2; | ||
82 | u16 rf_in, rf_off; | ||
83 | s64 level; | ||
84 | struct dtv_fe_stats *rssi; | ||
85 | |||
86 | rssi = &fe->dtv_property_cache.strength; | ||
87 | rssi->len = 1; | ||
88 | rssi->stat[0].scale = FE_SCALE_NOT_AVAILABLE; | ||
89 | *out = 0; | ||
90 | |||
91 | state = fe->tuner_priv; | ||
92 | ret = reg_write(state, 0x14, 0x01); | ||
93 | if (ret < 0) | ||
94 | return ret; | ||
95 | usleep_range(1000, 2000); | ||
96 | |||
97 | ret = reg_read(state, 0x18, &rf_in1); | ||
98 | if (ret == 0) | ||
99 | ret = reg_read(state, 0x19, &rf_in2); | ||
100 | if (ret == 0) | ||
101 | ret = reg_read(state, 0xd6, &rf_off1); | ||
102 | if (ret == 0) | ||
103 | ret = reg_read(state, 0xd7, &rf_off2); | ||
104 | if (ret != 0) | ||
105 | return ret; | ||
106 | |||
107 | rf_in = (rf_in2 & 0x07) << 8 | rf_in1; | ||
108 | rf_off = (rf_off2 & 0x0f) << 5 | (rf_off1 >> 3); | ||
109 | level = rf_in - rf_off - (113 << 3); /* x8 dBm */ | ||
110 | level = level * 1000 / 8; | ||
111 | rssi->stat[0].svalue = level; | ||
112 | rssi->stat[0].scale = FE_SCALE_DECIBEL; | ||
113 | /* *out = (level - min) * 100 / (max - min) */ | ||
114 | *out = (rf_in - rf_off + (1 << 9) - 1) * 100 / ((5 << 9) - 2); | ||
115 | return 0; | ||
116 | } | ||
117 | |||
118 | /* spur shift parameters */ | ||
119 | struct shf { | ||
120 | u32 freq; /* Channel center frequency */ | ||
121 | u32 ofst_th; /* Offset frequency threshold */ | ||
122 | u8 shf_val; /* Spur shift value */ | ||
123 | u8 shf_dir; /* Spur shift direction */ | ||
124 | }; | ||
125 | |||
126 | static const struct shf shf_tab[] = { | ||
127 | { 64500, 500, 0x92, 0x07 }, | ||
128 | { 191500, 300, 0xe2, 0x07 }, | ||
129 | { 205500, 500, 0x2c, 0x04 }, | ||
130 | { 212500, 500, 0x1e, 0x04 }, | ||
131 | { 226500, 500, 0xd4, 0x07 }, | ||
132 | { 99143, 500, 0x9c, 0x07 }, | ||
133 | { 173143, 500, 0xd4, 0x07 }, | ||
134 | { 191143, 300, 0xd4, 0x07 }, | ||
135 | { 207143, 500, 0xce, 0x07 }, | ||
136 | { 225143, 500, 0xce, 0x07 }, | ||
137 | { 243143, 500, 0xd4, 0x07 }, | ||
138 | { 261143, 500, 0xd4, 0x07 }, | ||
139 | { 291143, 500, 0xd4, 0x07 }, | ||
140 | { 339143, 500, 0x2c, 0x04 }, | ||
141 | { 117143, 500, 0x7a, 0x07 }, | ||
142 | { 135143, 300, 0x7a, 0x07 }, | ||
143 | { 153143, 500, 0x01, 0x07 } | ||
144 | }; | ||
145 | |||
146 | struct reg_val { | ||
147 | u8 reg; | ||
148 | u8 val; | ||
149 | } __attribute__ ((__packed__)); | ||
150 | |||
151 | static const struct reg_val set_idac[] = { | ||
152 | { 0x0d, 0x00 }, | ||
153 | { 0x0c, 0x67 }, | ||
154 | { 0x6f, 0x89 }, | ||
155 | { 0x70, 0x0c }, | ||
156 | { 0x6f, 0x8a }, | ||
157 | { 0x70, 0x0e }, | ||
158 | { 0x6f, 0x8b }, | ||
159 | { 0x70, 0x1c }, | ||
160 | }; | ||
161 | |||
162 | static int mxl301rf_set_params(struct dvb_frontend *fe) | ||
163 | { | ||
164 | struct reg_val tune0[] = { | ||
165 | { 0x13, 0x00 }, /* abort tuning */ | ||
166 | { 0x3b, 0xc0 }, | ||
167 | { 0x3b, 0x80 }, | ||
168 | { 0x10, 0x95 }, /* BW */ | ||
169 | { 0x1a, 0x05 }, | ||
170 | { 0x61, 0x00 }, /* spur shift value (placeholder) */ | ||
171 | { 0x62, 0xa0 } /* spur shift direction (placeholder) */ | ||
172 | }; | ||
173 | |||
174 | struct reg_val tune1[] = { | ||
175 | { 0x11, 0x40 }, /* RF frequency L (placeholder) */ | ||
176 | { 0x12, 0x0e }, /* RF frequency H (placeholder) */ | ||
177 | { 0x13, 0x01 } /* start tune */ | ||
178 | }; | ||
179 | |||
180 | struct mxl301rf_state *state; | ||
181 | u32 freq; | ||
182 | u16 f; | ||
183 | u32 tmp, div; | ||
184 | int i, ret; | ||
185 | |||
186 | state = fe->tuner_priv; | ||
187 | freq = fe->dtv_property_cache.frequency; | ||
188 | |||
189 | /* spur shift function (for analog) */ | ||
190 | for (i = 0; i < ARRAY_SIZE(shf_tab); i++) { | ||
191 | if (freq >= (shf_tab[i].freq - shf_tab[i].ofst_th) * 1000 && | ||
192 | freq <= (shf_tab[i].freq + shf_tab[i].ofst_th) * 1000) { | ||
193 | tune0[5].val = shf_tab[i].shf_val; | ||
194 | tune0[6].val = 0xa0 | shf_tab[i].shf_dir; | ||
195 | break; | ||
196 | } | ||
197 | } | ||
198 | ret = raw_write(state, (u8 *) tune0, sizeof(tune0)); | ||
199 | if (ret < 0) | ||
200 | goto failed; | ||
201 | usleep_range(3000, 4000); | ||
202 | |||
203 | /* convert freq to 10.6 fixed point float [MHz] */ | ||
204 | f = freq / 1000000; | ||
205 | tmp = freq % 1000000; | ||
206 | div = 1000000; | ||
207 | for (i = 0; i < 6; i++) { | ||
208 | f <<= 1; | ||
209 | div >>= 1; | ||
210 | if (tmp > div) { | ||
211 | tmp -= div; | ||
212 | f |= 1; | ||
213 | } | ||
214 | } | ||
215 | if (tmp > 7812) | ||
216 | f++; | ||
217 | tune1[0].val = f & 0xff; | ||
218 | tune1[1].val = f >> 8; | ||
219 | ret = raw_write(state, (u8 *) tune1, sizeof(tune1)); | ||
220 | if (ret < 0) | ||
221 | goto failed; | ||
222 | msleep(31); | ||
223 | |||
224 | ret = reg_write(state, 0x1a, 0x0d); | ||
225 | if (ret < 0) | ||
226 | goto failed; | ||
227 | ret = raw_write(state, (u8 *) set_idac, sizeof(set_idac)); | ||
228 | if (ret < 0) | ||
229 | goto failed; | ||
230 | return 0; | ||
231 | |||
232 | failed: | ||
233 | dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n", | ||
234 | __func__, fe->dvb->num, fe->id); | ||
235 | return ret; | ||
236 | } | ||
237 | |||
238 | static const struct reg_val standby_data[] = { | ||
239 | { 0x01, 0x00 }, | ||
240 | { 0x13, 0x00 } | ||
241 | }; | ||
242 | |||
243 | static int mxl301rf_sleep(struct dvb_frontend *fe) | ||
244 | { | ||
245 | struct mxl301rf_state *state; | ||
246 | int ret; | ||
247 | |||
248 | state = fe->tuner_priv; | ||
249 | ret = raw_write(state, (u8 *)standby_data, sizeof(standby_data)); | ||
250 | if (ret < 0) | ||
251 | dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n", | ||
252 | __func__, fe->dvb->num, fe->id); | ||
253 | return ret; | ||
254 | } | ||
255 | |||
256 | |||
257 | /* init sequence is not public. | ||
258 | * the parent must have init'ed the device. | ||
259 | * just wake up here. | ||
260 | */ | ||
261 | static int mxl301rf_init(struct dvb_frontend *fe) | ||
262 | { | ||
263 | struct mxl301rf_state *state; | ||
264 | int ret; | ||
265 | |||
266 | state = fe->tuner_priv; | ||
267 | |||
268 | ret = reg_write(state, 0x01, 0x01); | ||
269 | if (ret < 0) { | ||
270 | dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n", | ||
271 | __func__, fe->dvb->num, fe->id); | ||
272 | return ret; | ||
273 | } | ||
274 | return 0; | ||
275 | } | ||
276 | |||
277 | /* I2C driver functions */ | ||
278 | |||
279 | static const struct dvb_tuner_ops mxl301rf_ops = { | ||
280 | .info = { | ||
281 | .name = "MaxLinear MxL301RF", | ||
282 | |||
283 | .frequency_min = 93000000, | ||
284 | .frequency_max = 803142857, | ||
285 | }, | ||
286 | |||
287 | .init = mxl301rf_init, | ||
288 | .sleep = mxl301rf_sleep, | ||
289 | |||
290 | .set_params = mxl301rf_set_params, | ||
291 | .get_rf_strength = mxl301rf_get_rf_strength, | ||
292 | }; | ||
293 | |||
294 | |||
295 | static int mxl301rf_probe(struct i2c_client *client, | ||
296 | const struct i2c_device_id *id) | ||
297 | { | ||
298 | struct mxl301rf_state *state; | ||
299 | struct mxl301rf_config *cfg; | ||
300 | struct dvb_frontend *fe; | ||
301 | |||
302 | state = kzalloc(sizeof(*state), GFP_KERNEL); | ||
303 | if (!state) | ||
304 | return -ENOMEM; | ||
305 | |||
306 | state->i2c = client; | ||
307 | cfg = client->dev.platform_data; | ||
308 | |||
309 | memcpy(&state->cfg, cfg, sizeof(state->cfg)); | ||
310 | fe = cfg->fe; | ||
311 | fe->tuner_priv = state; | ||
312 | memcpy(&fe->ops.tuner_ops, &mxl301rf_ops, sizeof(mxl301rf_ops)); | ||
313 | |||
314 | i2c_set_clientdata(client, &state->cfg); | ||
315 | dev_info(&client->dev, "MaxLinear MxL301RF attached.\n"); | ||
316 | return 0; | ||
317 | } | ||
318 | |||
319 | static int mxl301rf_remove(struct i2c_client *client) | ||
320 | { | ||
321 | struct mxl301rf_state *state; | ||
322 | |||
323 | state = cfg_to_state(i2c_get_clientdata(client)); | ||
324 | state->cfg.fe->tuner_priv = NULL; | ||
325 | kfree(state); | ||
326 | return 0; | ||
327 | } | ||
328 | |||
329 | |||
330 | static const struct i2c_device_id mxl301rf_id[] = { | ||
331 | {"mxl301rf", 0}, | ||
332 | {} | ||
333 | }; | ||
334 | MODULE_DEVICE_TABLE(i2c, mxl301rf_id); | ||
335 | |||
336 | static struct i2c_driver mxl301rf_driver = { | ||
337 | .driver = { | ||
338 | .name = "mxl301rf", | ||
339 | }, | ||
340 | .probe = mxl301rf_probe, | ||
341 | .remove = mxl301rf_remove, | ||
342 | .id_table = mxl301rf_id, | ||
343 | }; | ||
344 | |||
345 | module_i2c_driver(mxl301rf_driver); | ||
346 | |||
347 | MODULE_DESCRIPTION("MaxLinear MXL301RF tuner"); | ||
348 | MODULE_AUTHOR("Akihiro TSUKADA"); | ||
349 | MODULE_LICENSE("GPL"); | ||