x86/mm/pat: Don't report PAT on CPUs that don't support it
[linux/fpc-iii.git] / drivers / media / tuners / mxl301rf.c
blob1575a5db776ad085cecc57c32e8ba78ed0d98952
1 /*
2 * MaxLinear MxL301RF OFDM tuner driver
4 * Copyright (C) 2014 Akihiro Tsukada <tskd08@gmail.com>
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.
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.
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.
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.
31 #include <linux/kernel.h>
32 #include "mxl301rf.h"
34 struct mxl301rf_state {
35 struct mxl301rf_config cfg;
36 struct i2c_client *i2c;
39 static struct mxl301rf_state *cfg_to_state(struct mxl301rf_config *c)
41 return container_of(c, struct mxl301rf_state, cfg);
44 static int raw_write(struct mxl301rf_state *state, const u8 *buf, int len)
46 int ret;
48 ret = i2c_master_send(state->i2c, buf, len);
49 if (ret >= 0 && ret < len)
50 ret = -EIO;
51 return (ret == len) ? 0 : ret;
54 static int reg_write(struct mxl301rf_state *state, u8 reg, u8 val)
56 u8 buf[2] = { reg, val };
58 return raw_write(state, buf, 2);
61 static int reg_read(struct mxl301rf_state *state, u8 reg, u8 *val)
63 u8 wbuf[2] = { 0xfb, reg };
64 int ret;
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;
74 /* tuner_ops */
76 /* get RSSI and update propery cache, set to *out in % */
77 static int mxl301rf_get_rf_strength(struct dvb_frontend *fe, u16 *out)
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;
86 rssi = &fe->dtv_property_cache.strength;
87 rssi->len = 1;
88 rssi->stat[0].scale = FE_SCALE_NOT_AVAILABLE;
89 *out = 0;
91 state = fe->tuner_priv;
92 ret = reg_write(state, 0x14, 0x01);
93 if (ret < 0)
94 return ret;
95 usleep_range(1000, 2000);
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;
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;
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 */
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 }
146 struct reg_val {
147 u8 reg;
148 u8 val;
149 } __attribute__ ((__packed__));
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 },
162 static int mxl301rf_set_params(struct dvb_frontend *fe)
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) */
174 struct reg_val tune1[] = {
175 { 0x11, 0x40 }, /* RF frequency L (placeholder) */
176 { 0x12, 0x0e }, /* RF frequency H (placeholder) */
177 { 0x13, 0x01 } /* start tune */
180 struct mxl301rf_state *state;
181 u32 freq;
182 u16 f;
183 u32 tmp, div;
184 int i, ret;
186 state = fe->tuner_priv;
187 freq = fe->dtv_property_cache.frequency;
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;
198 ret = raw_write(state, (u8 *) tune0, sizeof(tune0));
199 if (ret < 0)
200 goto failed;
201 usleep_range(3000, 4000);
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;
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);
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;
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;
238 static const struct reg_val standby_data[] = {
239 { 0x01, 0x00 },
240 { 0x13, 0x00 }
243 static int mxl301rf_sleep(struct dvb_frontend *fe)
245 struct mxl301rf_state *state;
246 int ret;
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;
257 /* init sequence is not public.
258 * the parent must have init'ed the device.
259 * just wake up here.
261 static int mxl301rf_init(struct dvb_frontend *fe)
263 struct mxl301rf_state *state;
264 int ret;
266 state = fe->tuner_priv;
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;
274 return 0;
277 /* I2C driver functions */
279 static const struct dvb_tuner_ops mxl301rf_ops = {
280 .info = {
281 .name = "MaxLinear MxL301RF",
283 .frequency_min = 93000000,
284 .frequency_max = 803142857,
287 .init = mxl301rf_init,
288 .sleep = mxl301rf_sleep,
290 .set_params = mxl301rf_set_params,
291 .get_rf_strength = mxl301rf_get_rf_strength,
295 static int mxl301rf_probe(struct i2c_client *client,
296 const struct i2c_device_id *id)
298 struct mxl301rf_state *state;
299 struct mxl301rf_config *cfg;
300 struct dvb_frontend *fe;
302 state = kzalloc(sizeof(*state), GFP_KERNEL);
303 if (!state)
304 return -ENOMEM;
306 state->i2c = client;
307 cfg = client->dev.platform_data;
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));
314 i2c_set_clientdata(client, &state->cfg);
315 dev_info(&client->dev, "MaxLinear MxL301RF attached.\n");
316 return 0;
319 static int mxl301rf_remove(struct i2c_client *client)
321 struct mxl301rf_state *state;
323 state = cfg_to_state(i2c_get_clientdata(client));
324 state->cfg.fe->tuner_priv = NULL;
325 kfree(state);
326 return 0;
330 static const struct i2c_device_id mxl301rf_id[] = {
331 {"mxl301rf", 0},
334 MODULE_DEVICE_TABLE(i2c, mxl301rf_id);
336 static struct i2c_driver mxl301rf_driver = {
337 .driver = {
338 .name = "mxl301rf",
340 .probe = mxl301rf_probe,
341 .remove = mxl301rf_remove,
342 .id_table = mxl301rf_id,
345 module_i2c_driver(mxl301rf_driver);
347 MODULE_DESCRIPTION("MaxLinear MXL301RF tuner");
348 MODULE_AUTHOR("Akihiro TSUKADA");
349 MODULE_LICENSE("GPL");