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
35 #define QM1D1C0042_NUM_REG_ROWS 2
38 reg_initval
[QM1D1C0042_NUM_REG_ROWS
][QM1D1C0042_NUM_REGS
] = { {
39 0x48, 0x1c, 0xa0, 0x10, 0xbc, 0xc5, 0x20, 0x33,
40 0x06, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
41 0x00, 0xff, 0xf3, 0x00, 0x2a, 0x64, 0xa6, 0x86,
42 0x8c, 0xcf, 0xb8, 0xf1, 0xa8, 0xf2, 0x89, 0x00
44 0x68, 0x1c, 0xc0, 0x10, 0xbc, 0xc1, 0x11, 0x33,
45 0x03, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,
46 0x00, 0xff, 0xf3, 0x00, 0x3f, 0x25, 0x5c, 0xd6,
47 0x55, 0xcf, 0x95, 0xf6, 0x36, 0xf2, 0x09, 0x00
53 static const struct qm1d1c0042_config default_cfg
= {
59 .normal_srch_wait
= 15,
62 struct qm1d1c0042_state
{
63 struct qm1d1c0042_config cfg
;
64 struct i2c_client
*i2c
;
65 u8 regs
[QM1D1C0042_NUM_REGS
];
68 static struct qm1d1c0042_state
*cfg_to_state(struct qm1d1c0042_config
*c
)
70 return container_of(c
, struct qm1d1c0042_state
, cfg
);
73 static int reg_write(struct qm1d1c0042_state
*state
, u8 reg
, u8 val
)
75 u8 wbuf
[2] = { reg
, val
};
78 ret
= i2c_master_send(state
->i2c
, wbuf
, sizeof(wbuf
));
79 if (ret
>= 0 && ret
< sizeof(wbuf
))
81 return (ret
== sizeof(wbuf
)) ? 0 : ret
;
84 static int reg_read(struct qm1d1c0042_state
*state
, u8 reg
, u8
*val
)
86 struct i2c_msg msgs
[2] = {
88 .addr
= state
->i2c
->addr
,
94 .addr
= state
->i2c
->addr
,
102 ret
= i2c_transfer(state
->i2c
->adapter
, msgs
, ARRAY_SIZE(msgs
));
103 if (ret
>= 0 && ret
< ARRAY_SIZE(msgs
))
105 return (ret
== ARRAY_SIZE(msgs
)) ? 0 : ret
;
109 static int qm1d1c0042_set_srch_mode(struct qm1d1c0042_state
*state
, bool fast
)
112 state
->regs
[0x03] |= 0x01; /* set fast search mode */
114 state
->regs
[0x03] &= ~0x01 & 0xff;
116 return reg_write(state
, 0x03, state
->regs
[0x03]);
119 static int qm1d1c0042_wakeup(struct qm1d1c0042_state
*state
)
123 state
->regs
[0x01] |= 1 << 3; /* BB_Reg_enable */
124 state
->regs
[0x01] &= (~(1 << 0)) & 0xff; /* NORMAL (wake-up) */
125 state
->regs
[0x05] &= (~(1 << 3)) & 0xff; /* pfd_rst NORMAL */
126 ret
= reg_write(state
, 0x01, state
->regs
[0x01]);
128 ret
= reg_write(state
, 0x05, state
->regs
[0x05]);
131 dev_warn(&state
->i2c
->dev
, "(%s) failed. [adap%d-fe%d]\n",
132 __func__
, state
->cfg
.fe
->dvb
->num
, state
->cfg
.fe
->id
);
138 static int qm1d1c0042_set_config(struct dvb_frontend
*fe
, void *priv_cfg
)
140 struct qm1d1c0042_state
*state
;
141 struct qm1d1c0042_config
*cfg
;
143 state
= fe
->tuner_priv
;
147 state
->cfg
.fe
= cfg
->fe
;
149 if (cfg
->xtal_freq
!= QM1D1C0042_CFG_XTAL_DFLT
)
150 dev_warn(&state
->i2c
->dev
,
151 "(%s) changing xtal_freq not supported. ", __func__
);
152 state
->cfg
.xtal_freq
= default_cfg
.xtal_freq
;
154 state
->cfg
.lpf
= cfg
->lpf
;
155 state
->cfg
.fast_srch
= cfg
->fast_srch
;
157 if (cfg
->lpf_wait
!= QM1D1C0042_CFG_WAIT_DFLT
)
158 state
->cfg
.lpf_wait
= cfg
->lpf_wait
;
160 state
->cfg
.lpf_wait
= default_cfg
.lpf_wait
;
162 if (cfg
->fast_srch_wait
!= QM1D1C0042_CFG_WAIT_DFLT
)
163 state
->cfg
.fast_srch_wait
= cfg
->fast_srch_wait
;
165 state
->cfg
.fast_srch_wait
= default_cfg
.fast_srch_wait
;
167 if (cfg
->normal_srch_wait
!= QM1D1C0042_CFG_WAIT_DFLT
)
168 state
->cfg
.normal_srch_wait
= cfg
->normal_srch_wait
;
170 state
->cfg
.normal_srch_wait
= default_cfg
.normal_srch_wait
;
174 /* divisor, vco_band parameters */
175 /* {maxfreq, param1(band?), param2(div?) */
176 static const u32 conv_table
[9][3] = {
188 static int qm1d1c0042_set_params(struct dvb_frontend
*fe
)
190 struct qm1d1c0042_state
*state
;
197 state
= fe
->tuner_priv
;
198 freq
= fe
->dtv_property_cache
.frequency
;
200 state
->regs
[0x08] &= 0xf0;
201 state
->regs
[0x08] |= 0x09;
203 state
->regs
[0x13] &= 0x9f;
204 state
->regs
[0x13] |= 0x20;
207 val
= state
->regs
[0x02] & 0x0f;
208 for (i
= 0; i
< 8; i
++)
209 if (freq
< conv_table
[i
][0] && freq
>= conv_table
[i
+ 1][0]) {
210 val
|= conv_table
[i
][1] << 7;
211 val
|= conv_table
[i
][2] << 4;
214 ret
= reg_write(state
, 0x02, val
);
218 a
= (freq
+ state
->cfg
.xtal_freq
/ 2) / state
->cfg
.xtal_freq
;
220 state
->regs
[0x06] &= 0x40;
221 state
->regs
[0x06] |= (a
- 12) / 4;
222 ret
= reg_write(state
, 0x06, state
->regs
[0x06]);
226 state
->regs
[0x07] &= 0xf0;
227 state
->regs
[0x07] |= (a
- 4 * ((a
- 12) / 4 + 1) - 5) & 0x0f;
228 ret
= reg_write(state
, 0x07, state
->regs
[0x07]);
233 val
= state
->regs
[0x08];
234 if (state
->cfg
.lpf
) {
235 /* LPF_CLK, LPF_FC */
239 ret
= reg_write(state
, 0x08, val
);
244 * b = (freq / state->cfg.xtal_freq - a) << 20;
248 b
= (s32
)div64_s64(((s64
) freq
) << 20, state
->cfg
.xtal_freq
)
256 state
->regs
[0x09] &= 0xc0;
257 state
->regs
[0x09] |= (sd
>> 16) & 0x3f;
258 state
->regs
[0x0a] = (sd
>> 8) & 0xff;
259 state
->regs
[0x0b] = sd
& 0xff;
260 ret
= reg_write(state
, 0x09, state
->regs
[0x09]);
262 ret
= reg_write(state
, 0x0a, state
->regs
[0x0a]);
264 ret
= reg_write(state
, 0x0b, state
->regs
[0x0b]);
268 if (!state
->cfg
.lpf
) {
270 ret
= reg_write(state
, 0x13, state
->regs
[0x13]);
276 mask
= state
->cfg
.lpf
? 0x3f : 0x7f;
277 val
= state
->regs
[0x0c] & mask
;
278 ret
= reg_write(state
, 0x0c, val
);
281 usleep_range(2000, 3000);
282 val
= state
->regs
[0x0c] | ~mask
;
283 ret
= reg_write(state
, 0x0c, val
);
288 msleep(state
->cfg
.lpf_wait
);
289 else if (state
->regs
[0x03] & 0x01)
290 msleep(state
->cfg
.fast_srch_wait
);
292 msleep(state
->cfg
.normal_srch_wait
);
294 if (state
->cfg
.lpf
) {
296 ret
= reg_write(state
, 0x08, 0x09);
301 ret
= reg_write(state
, 0x13, state
->regs
[0x13]);
308 static int qm1d1c0042_sleep(struct dvb_frontend
*fe
)
310 struct qm1d1c0042_state
*state
;
313 state
= fe
->tuner_priv
;
314 state
->regs
[0x01] &= (~(1 << 3)) & 0xff; /* BB_Reg_disable */
315 state
->regs
[0x01] |= 1 << 0; /* STDBY */
316 state
->regs
[0x05] |= 1 << 3; /* pfd_rst STANDBY */
317 ret
= reg_write(state
, 0x05, state
->regs
[0x05]);
319 ret
= reg_write(state
, 0x01, state
->regs
[0x01]);
321 dev_warn(&state
->i2c
->dev
, "(%s) failed. [adap%d-fe%d]\n",
322 __func__
, fe
->dvb
->num
, fe
->id
);
326 static int qm1d1c0042_init(struct dvb_frontend
*fe
)
328 struct qm1d1c0042_state
*state
;
332 state
= fe
->tuner_priv
;
334 reg_write(state
, 0x01, 0x0c);
335 reg_write(state
, 0x01, 0x0c);
337 ret
= reg_write(state
, 0x01, 0x0c); /* soft reset on */
340 usleep_range(2000, 3000);
342 ret
= reg_write(state
, 0x01, 0x1c); /* soft reset off */
346 /* check ID and choose initial registers corresponding ID */
347 ret
= reg_read(state
, 0x00, &val
);
350 for (reg_index
= 0; reg_index
< QM1D1C0042_NUM_REG_ROWS
;
352 if (val
== reg_initval
[reg_index
][0x00])
355 if (reg_index
>= QM1D1C0042_NUM_REG_ROWS
)
357 memcpy(state
->regs
, reg_initval
[reg_index
], QM1D1C0042_NUM_REGS
);
358 usleep_range(2000, 3000);
360 state
->regs
[0x0c] |= 0x40;
361 ret
= reg_write(state
, 0x0c, state
->regs
[0x0c]);
364 msleep(state
->cfg
.lpf_wait
);
366 /* set all writable registers */
367 for (i
= 1; i
<= 0x0c ; i
++) {
368 ret
= reg_write(state
, i
, state
->regs
[i
]);
372 for (i
= 0x11; i
< QM1D1C0042_NUM_REGS
; i
++) {
373 ret
= reg_write(state
, i
, state
->regs
[i
]);
378 ret
= qm1d1c0042_wakeup(state
);
382 ret
= qm1d1c0042_set_srch_mode(state
, state
->cfg
.fast_srch
);
389 dev_warn(&state
->i2c
->dev
, "(%s) failed. [adap%d-fe%d]\n",
390 __func__
, fe
->dvb
->num
, fe
->id
);
394 /* I2C driver functions */
396 static const struct dvb_tuner_ops qm1d1c0042_ops
= {
398 .name
= "Sharp QM1D1C0042",
400 .frequency_min
= 950000,
401 .frequency_max
= 2150000,
404 .init
= qm1d1c0042_init
,
405 .sleep
= qm1d1c0042_sleep
,
406 .set_config
= qm1d1c0042_set_config
,
407 .set_params
= qm1d1c0042_set_params
,
411 static int qm1d1c0042_probe(struct i2c_client
*client
,
412 const struct i2c_device_id
*id
)
414 struct qm1d1c0042_state
*state
;
415 struct qm1d1c0042_config
*cfg
;
416 struct dvb_frontend
*fe
;
418 state
= kzalloc(sizeof(*state
), GFP_KERNEL
);
423 cfg
= client
->dev
.platform_data
;
425 fe
->tuner_priv
= state
;
426 qm1d1c0042_set_config(fe
, cfg
);
427 memcpy(&fe
->ops
.tuner_ops
, &qm1d1c0042_ops
, sizeof(qm1d1c0042_ops
));
429 i2c_set_clientdata(client
, &state
->cfg
);
430 dev_info(&client
->dev
, "Sharp QM1D1C0042 attached.\n");
434 static int qm1d1c0042_remove(struct i2c_client
*client
)
436 struct qm1d1c0042_state
*state
;
438 state
= cfg_to_state(i2c_get_clientdata(client
));
439 state
->cfg
.fe
->tuner_priv
= NULL
;
445 static const struct i2c_device_id qm1d1c0042_id
[] = {
449 MODULE_DEVICE_TABLE(i2c
, qm1d1c0042_id
);
451 static struct i2c_driver qm1d1c0042_driver
= {
453 .name
= "qm1d1c0042",
455 .probe
= qm1d1c0042_probe
,
456 .remove
= qm1d1c0042_remove
,
457 .id_table
= qm1d1c0042_id
,
460 module_i2c_driver(qm1d1c0042_driver
);
462 MODULE_DESCRIPTION("Sharp QM1D1C0042 tuner");
463 MODULE_AUTHOR("Akihiro TSUKADA");
464 MODULE_LICENSE("GPL");