2 * Sharp QM1D1C0042 8PSK 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.
19 * As the disclosed information on the chip is very limited,
20 * this driver lacks some features, including chip config like IF freq.
21 * It assumes that users of this driver (such as a PCI bridge of
22 * DTV receiver cards) know the relevant info and
23 * configure the chip via I2C if necessary.
25 * Currently, PT3 driver is the only one that uses this driver,
26 * and contains init/config code in its firmware.
27 * Thus some part of the code might be dependent on PT3 specific config.
30 #include <linux/kernel.h>
31 #include <linux/math64.h>
32 #include "qm1d1c0042.h"
34 #define QM1D1C0042_NUM_REGS 0x20
36 static const u8 reg_initval[QM1D1C0042_NUM_REGS] = {
37 0x48, 0x1c, 0xa0, 0x10, 0xbc, 0xc5, 0x20, 0x33,
38 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
39 0x00, 0xff, 0xf3, 0x00, 0x2a, 0x64, 0xa6, 0x86,
40 0x8c, 0xcf, 0xb8, 0xf1, 0xa8, 0xf2, 0x89, 0x00
43 static const struct qm1d1c0042_config default_cfg = {
49 .normal_srch_wait = 15,
52 struct qm1d1c0042_state {
53 struct qm1d1c0042_config cfg;
54 struct i2c_client *i2c;
55 u8 regs[QM1D1C0042_NUM_REGS];
58 static struct qm1d1c0042_state *cfg_to_state(struct qm1d1c0042_config *c)
60 return container_of(c, struct qm1d1c0042_state, cfg);
63 static int reg_write(struct qm1d1c0042_state *state, u8 reg, u8 val)
65 u8 wbuf[2] = { reg, val };
68 ret = i2c_master_send(state->i2c, wbuf, sizeof(wbuf));
69 if (ret >= 0 && ret < sizeof(wbuf))
71 return (ret == sizeof(wbuf)) ? 0 : ret;
74 static int reg_read(struct qm1d1c0042_state *state, u8 reg, u8 *val)
76 struct i2c_msg msgs[2] = {
78 .addr = state->i2c->addr,
84 .addr = state->i2c->addr,
92 ret = i2c_transfer(state->i2c->adapter, msgs, ARRAY_SIZE(msgs));
93 if (ret >= 0 && ret < ARRAY_SIZE(msgs))
95 return (ret == ARRAY_SIZE(msgs)) ? 0 : ret;
99 static int qm1d1c0042_set_srch_mode(struct qm1d1c0042_state *state, bool fast)
102 state->regs[0x03] |= 0x01; /* set fast search mode */
104 state->regs[0x03] &= ~0x01 & 0xff;
106 return reg_write(state, 0x03, state->regs[0x03]);
109 static int qm1d1c0042_wakeup(struct qm1d1c0042_state *state)
113 state->regs[0x01] |= 1 << 3; /* BB_Reg_enable */
114 state->regs[0x01] &= (~(1 << 0)) & 0xff; /* NORMAL (wake-up) */
115 state->regs[0x05] &= (~(1 << 3)) & 0xff; /* pfd_rst NORMAL */
116 ret = reg_write(state, 0x01, state->regs[0x01]);
118 ret = reg_write(state, 0x05, state->regs[0x05]);
121 dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
122 __func__, state->cfg.fe->dvb->num, state->cfg.fe->id);
128 static int qm1d1c0042_set_config(struct dvb_frontend *fe, void *priv_cfg)
130 struct qm1d1c0042_state *state;
131 struct qm1d1c0042_config *cfg;
133 state = fe->tuner_priv;
137 state->cfg.fe = cfg->fe;
139 if (cfg->xtal_freq != QM1D1C0042_CFG_XTAL_DFLT)
140 dev_warn(&state->i2c->dev,
141 "(%s) changing xtal_freq not supported. ", __func__);
142 state->cfg.xtal_freq = default_cfg.xtal_freq;
144 state->cfg.lpf = cfg->lpf;
145 state->cfg.fast_srch = cfg->fast_srch;
147 if (cfg->lpf_wait != QM1D1C0042_CFG_WAIT_DFLT)
148 state->cfg.lpf_wait = cfg->lpf_wait;
150 state->cfg.lpf_wait = default_cfg.lpf_wait;
152 if (cfg->fast_srch_wait != QM1D1C0042_CFG_WAIT_DFLT)
153 state->cfg.fast_srch_wait = cfg->fast_srch_wait;
155 state->cfg.fast_srch_wait = default_cfg.fast_srch_wait;
157 if (cfg->normal_srch_wait != QM1D1C0042_CFG_WAIT_DFLT)
158 state->cfg.normal_srch_wait = cfg->normal_srch_wait;
160 state->cfg.normal_srch_wait = default_cfg.normal_srch_wait;
164 /* divisor, vco_band parameters */
165 /* {maxfreq, param1(band?), param2(div?) */
166 static const u32 conv_table[9][3] = {
178 static int qm1d1c0042_set_params(struct dvb_frontend *fe)
180 struct qm1d1c0042_state *state;
187 state = fe->tuner_priv;
188 freq = fe->dtv_property_cache.frequency;
190 state->regs[0x08] &= 0xf0;
191 state->regs[0x08] |= 0x09;
193 state->regs[0x13] &= 0x9f;
194 state->regs[0x13] |= 0x20;
197 val = state->regs[0x02] & 0x0f;
198 for (i = 0; i < 8; i++)
199 if (freq < conv_table[i][0] && freq >= conv_table[i + 1][0]) {
200 val |= conv_table[i][1] << 7;
201 val |= conv_table[i][2] << 4;
204 ret = reg_write(state, 0x02, val);
208 a = (freq + state->cfg.xtal_freq / 2) / state->cfg.xtal_freq;
210 state->regs[0x06] &= 0x40;
211 state->regs[0x06] |= (a - 12) / 4;
212 ret = reg_write(state, 0x06, state->regs[0x06]);
216 state->regs[0x07] &= 0xf0;
217 state->regs[0x07] |= (a - 4 * ((a - 12) / 4 + 1) - 5) & 0x0f;
218 ret = reg_write(state, 0x07, state->regs[0x07]);
223 val = state->regs[0x08];
224 if (state->cfg.lpf) {
225 /* LPF_CLK, LPF_FC */
229 ret = reg_write(state, 0x08, val);
234 * b = (freq / state->cfg.xtal_freq - a) << 20;
238 b = (s32)div64_s64(((s64) freq) << 20, state->cfg.xtal_freq)
246 state->regs[0x09] &= 0xc0;
247 state->regs[0x09] |= (sd >> 16) & 0x3f;
248 state->regs[0x0a] = (sd >> 8) & 0xff;
249 state->regs[0x0b] = sd & 0xff;
250 ret = reg_write(state, 0x09, state->regs[0x09]);
252 ret = reg_write(state, 0x0a, state->regs[0x0a]);
254 ret = reg_write(state, 0x0b, state->regs[0x0b]);
258 if (!state->cfg.lpf) {
260 ret = reg_write(state, 0x13, state->regs[0x13]);
266 mask = state->cfg.lpf ? 0x3f : 0x7f;
267 val = state->regs[0x0c] & mask;
268 ret = reg_write(state, 0x0c, val);
271 usleep_range(2000, 3000);
272 val = state->regs[0x0c] | ~mask;
273 ret = reg_write(state, 0x0c, val);
278 msleep(state->cfg.lpf_wait);
279 else if (state->regs[0x03] & 0x01)
280 msleep(state->cfg.fast_srch_wait);
282 msleep(state->cfg.normal_srch_wait);
284 if (state->cfg.lpf) {
286 ret = reg_write(state, 0x08, 0x09);
291 ret = reg_write(state, 0x13, state->regs[0x13]);
298 static int qm1d1c0042_sleep(struct dvb_frontend *fe)
300 struct qm1d1c0042_state *state;
303 state = fe->tuner_priv;
304 state->regs[0x01] &= (~(1 << 3)) & 0xff; /* BB_Reg_disable */
305 state->regs[0x01] |= 1 << 0; /* STDBY */
306 state->regs[0x05] |= 1 << 3; /* pfd_rst STANDBY */
307 ret = reg_write(state, 0x05, state->regs[0x05]);
309 ret = reg_write(state, 0x01, state->regs[0x01]);
311 dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
312 __func__, fe->dvb->num, fe->id);
316 static int qm1d1c0042_init(struct dvb_frontend *fe)
318 struct qm1d1c0042_state *state;
322 state = fe->tuner_priv;
323 memcpy(state->regs, reg_initval, sizeof(reg_initval));
325 reg_write(state, 0x01, 0x0c);
326 reg_write(state, 0x01, 0x0c);
328 ret = reg_write(state, 0x01, 0x0c); /* soft reset on */
331 usleep_range(2000, 3000);
333 val = state->regs[0x01] | 0x10;
334 ret = reg_write(state, 0x01, val); /* soft reset off */
339 ret = reg_read(state, 0x00, &val);
340 if (ret < 0 || val != 0x48)
342 usleep_range(2000, 3000);
344 state->regs[0x0c] |= 0x40;
345 ret = reg_write(state, 0x0c, state->regs[0x0c]);
348 msleep(state->cfg.lpf_wait);
350 /* set all writable registers */
351 for (i = 1; i <= 0x0c ; i++) {
352 ret = reg_write(state, i, state->regs[i]);
356 for (i = 0x11; i < QM1D1C0042_NUM_REGS; i++) {
357 ret = reg_write(state, i, state->regs[i]);
362 ret = qm1d1c0042_wakeup(state);
366 ret = qm1d1c0042_set_srch_mode(state, state->cfg.fast_srch);
373 dev_warn(&state->i2c->dev, "(%s) failed. [adap%d-fe%d]\n",
374 __func__, fe->dvb->num, fe->id);
378 /* I2C driver functions */
380 static const struct dvb_tuner_ops qm1d1c0042_ops = {
382 .name = "Sharp QM1D1C0042",
384 .frequency_min = 950000,
385 .frequency_max = 2150000,
388 .init = qm1d1c0042_init,
389 .sleep = qm1d1c0042_sleep,
390 .set_config = qm1d1c0042_set_config,
391 .set_params = qm1d1c0042_set_params,
395 static int qm1d1c0042_probe(struct i2c_client *client,
396 const struct i2c_device_id *id)
398 struct qm1d1c0042_state *state;
399 struct qm1d1c0042_config *cfg;
400 struct dvb_frontend *fe;
402 state = kzalloc(sizeof(*state), GFP_KERNEL);
407 cfg = client->dev.platform_data;
409 fe->tuner_priv = state;
410 qm1d1c0042_set_config(fe, cfg);
411 memcpy(&fe->ops.tuner_ops, &qm1d1c0042_ops, sizeof(qm1d1c0042_ops));
413 i2c_set_clientdata(client, &state->cfg);
414 dev_info(&client->dev, "Sharp QM1D1C0042 attached.\n");
418 static int qm1d1c0042_remove(struct i2c_client *client)
420 struct qm1d1c0042_state *state;
422 state = cfg_to_state(i2c_get_clientdata(client));
423 state->cfg.fe->tuner_priv = NULL;
429 static const struct i2c_device_id qm1d1c0042_id[] = {
433 MODULE_DEVICE_TABLE(i2c, qm1d1c0042_id);
435 static struct i2c_driver qm1d1c0042_driver = {
437 .name = "qm1d1c0042",
439 .probe = qm1d1c0042_probe,
440 .remove = qm1d1c0042_remove,
441 .id_table = qm1d1c0042_id,
444 module_i2c_driver(qm1d1c0042_driver);
446 MODULE_DESCRIPTION("Sharp QM1D1C0042 tuner");
447 MODULE_AUTHOR("Akihiro TSUKADA");
448 MODULE_LICENSE("GPL");