1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ZyDAS ZD1301 driver (demodulator)
5 * Copyright (C) 2015 Antti Palosaari <crope@iki.fi>
8 #include "zd1301_demod.h"
10 static u8 zd1301_demod_gain
= 0x38;
11 module_param_named(gain
, zd1301_demod_gain
, byte
, 0644);
12 MODULE_PARM_DESC(gain
, "gain (value: 0x00 - 0x70, default: 0x38)");
14 struct zd1301_demod_dev
{
15 struct platform_device
*pdev
;
16 struct dvb_frontend frontend
;
17 struct i2c_adapter adapter
;
21 static int zd1301_demod_wreg(struct zd1301_demod_dev
*dev
, u16 reg
, u8 val
)
23 struct platform_device
*pdev
= dev
->pdev
;
24 struct zd1301_demod_platform_data
*pdata
= pdev
->dev
.platform_data
;
26 return pdata
->reg_write(pdata
->reg_priv
, reg
, val
);
29 static int zd1301_demod_rreg(struct zd1301_demod_dev
*dev
, u16 reg
, u8
*val
)
31 struct platform_device
*pdev
= dev
->pdev
;
32 struct zd1301_demod_platform_data
*pdata
= pdev
->dev
.platform_data
;
34 return pdata
->reg_read(pdata
->reg_priv
, reg
, val
);
37 static int zd1301_demod_set_frontend(struct dvb_frontend
*fe
)
39 struct zd1301_demod_dev
*dev
= fe
->demodulator_priv
;
40 struct platform_device
*pdev
= dev
->pdev
;
41 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
46 dev_dbg(&pdev
->dev
, "frequency=%u bandwidth_hz=%u\n",
47 c
->frequency
, c
->bandwidth_hz
);
50 if (fe
->ops
.tuner_ops
.set_params
&&
51 fe
->ops
.tuner_ops
.get_if_frequency
) {
52 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
55 ret
= fe
->ops
.tuner_ops
.get_if_frequency(fe
, &if_frequency
);
63 dev_dbg(&pdev
->dev
, "if_frequency=%u\n", if_frequency
);
64 if (if_frequency
!= 36150000) {
69 switch (c
->bandwidth_hz
) {
84 ret
= zd1301_demod_wreg(dev
, 0x6a60, 0x11);
87 ret
= zd1301_demod_wreg(dev
, 0x6a47, 0x46);
90 ret
= zd1301_demod_wreg(dev
, 0x6a48, 0x46);
93 ret
= zd1301_demod_wreg(dev
, 0x6a4a, 0x15);
96 ret
= zd1301_demod_wreg(dev
, 0x6a4b, 0x63);
99 ret
= zd1301_demod_wreg(dev
, 0x6a5b, 0x99);
102 ret
= zd1301_demod_wreg(dev
, 0x6a3b, 0x10);
105 ret
= zd1301_demod_wreg(dev
, 0x6806, 0x01);
108 ret
= zd1301_demod_wreg(dev
, 0x6a41, 0x08);
111 ret
= zd1301_demod_wreg(dev
, 0x6a42, 0x46);
114 ret
= zd1301_demod_wreg(dev
, 0x6a44, 0x14);
117 ret
= zd1301_demod_wreg(dev
, 0x6a45, 0x67);
120 ret
= zd1301_demod_wreg(dev
, 0x6a38, 0x00);
123 ret
= zd1301_demod_wreg(dev
, 0x6a4c, 0x52);
126 ret
= zd1301_demod_wreg(dev
, 0x6a49, 0x2a);
129 ret
= zd1301_demod_wreg(dev
, 0x6840, 0x2e);
132 ret
= zd1301_demod_wreg(dev
, 0x6a50, r6a50_val
);
135 ret
= zd1301_demod_wreg(dev
, 0x6a38, 0x07);
141 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
145 static int zd1301_demod_sleep(struct dvb_frontend
*fe
)
147 struct zd1301_demod_dev
*dev
= fe
->demodulator_priv
;
148 struct platform_device
*pdev
= dev
->pdev
;
151 dev_dbg(&pdev
->dev
, "\n");
153 ret
= zd1301_demod_wreg(dev
, 0x6a43, 0x70);
156 ret
= zd1301_demod_wreg(dev
, 0x684e, 0x00);
159 ret
= zd1301_demod_wreg(dev
, 0x6849, 0x00);
162 ret
= zd1301_demod_wreg(dev
, 0x68e2, 0xd7);
165 ret
= zd1301_demod_wreg(dev
, 0x68e0, 0x39);
168 ret
= zd1301_demod_wreg(dev
, 0x6840, 0x21);
174 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
178 static int zd1301_demod_init(struct dvb_frontend
*fe
)
180 struct zd1301_demod_dev
*dev
= fe
->demodulator_priv
;
181 struct platform_device
*pdev
= dev
->pdev
;
184 dev_dbg(&pdev
->dev
, "\n");
186 ret
= zd1301_demod_wreg(dev
, 0x6840, 0x26);
189 ret
= zd1301_demod_wreg(dev
, 0x68e0, 0xff);
192 ret
= zd1301_demod_wreg(dev
, 0x68e2, 0xd8);
195 ret
= zd1301_demod_wreg(dev
, 0x6849, 0x4e);
198 ret
= zd1301_demod_wreg(dev
, 0x684e, 0x01);
201 ret
= zd1301_demod_wreg(dev
, 0x6a43, zd1301_demod_gain
);
207 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
211 static int zd1301_demod_get_tune_settings(struct dvb_frontend
*fe
,
212 struct dvb_frontend_tune_settings
*settings
)
214 struct zd1301_demod_dev
*dev
= fe
->demodulator_priv
;
215 struct platform_device
*pdev
= dev
->pdev
;
217 dev_dbg(&pdev
->dev
, "\n");
219 /* ~180ms seems to be enough */
220 settings
->min_delay_ms
= 400;
225 static int zd1301_demod_read_status(struct dvb_frontend
*fe
,
226 enum fe_status
*status
)
228 struct zd1301_demod_dev
*dev
= fe
->demodulator_priv
;
229 struct platform_device
*pdev
= dev
->pdev
;
233 ret
= zd1301_demod_rreg(dev
, 0x6a24, &u8tmp
);
236 if (u8tmp
> 0x00 && u8tmp
< 0x20)
237 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
238 FE_HAS_SYNC
| FE_HAS_LOCK
;
242 dev_dbg(&pdev
->dev
, "lock byte=%02x\n", u8tmp
);
245 * Interesting registers here are:
246 * 0x6a05: get some gain value
247 * 0x6a06: get about same gain value than set to 0x6a43
248 * 0x6a07: get some gain value
249 * 0x6a43: set gain value by driver
250 * 0x6a24: get demod lock bits (FSM stage?)
252 * Driver should implement some kind of algorithm to calculate suitable
253 * value for register 0x6a43, based likely values from register 0x6a05
254 * and 0x6a07. Looks like gain register 0x6a43 value could be from
258 if (dev
->gain
!= zd1301_demod_gain
) {
259 dev
->gain
= zd1301_demod_gain
;
261 ret
= zd1301_demod_wreg(dev
, 0x6a43, dev
->gain
);
268 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
272 static const struct dvb_frontend_ops zd1301_demod_ops
= {
273 .delsys
= {SYS_DVBT
},
275 .name
= "ZyDAS ZD1301",
276 .caps
= FE_CAN_FEC_1_2
|
286 FE_CAN_TRANSMISSION_MODE_AUTO
|
287 FE_CAN_GUARD_INTERVAL_AUTO
|
288 FE_CAN_HIERARCHY_AUTO
|
292 .sleep
= zd1301_demod_sleep
,
293 .init
= zd1301_demod_init
,
294 .set_frontend
= zd1301_demod_set_frontend
,
295 .get_tune_settings
= zd1301_demod_get_tune_settings
,
296 .read_status
= zd1301_demod_read_status
,
299 struct dvb_frontend
*zd1301_demod_get_dvb_frontend(struct platform_device
*pdev
)
301 struct zd1301_demod_dev
*dev
= platform_get_drvdata(pdev
);
303 dev_dbg(&pdev
->dev
, "\n");
305 return &dev
->frontend
;
307 EXPORT_SYMBOL(zd1301_demod_get_dvb_frontend
);
309 static int zd1301_demod_i2c_master_xfer(struct i2c_adapter
*adapter
,
310 struct i2c_msg msg
[], int num
)
312 struct zd1301_demod_dev
*dev
= i2c_get_adapdata(adapter
);
313 struct platform_device
*pdev
= dev
->pdev
;
315 unsigned long timeout
;
318 #define I2C_XFER_TIMEOUT 5
319 #define ZD1301_IS_I2C_XFER_WRITE_READ(_msg, _num) \
320 (_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD))
321 #define ZD1301_IS_I2C_XFER_WRITE(_msg, _num) \
322 (_num == 1 && !(_msg[0].flags & I2C_M_RD))
323 #define ZD1301_IS_I2C_XFER_READ(_msg, _num) \
324 (_num == 1 && (_msg[0].flags & I2C_M_RD))
325 if (ZD1301_IS_I2C_XFER_WRITE_READ(msg
, num
)) {
326 dev_dbg(&pdev
->dev
, "write&read msg[0].len=%u msg[1].len=%u\n",
327 msg
[0].len
, msg
[1].len
);
328 if (msg
[0].len
> 1 || msg
[1].len
> 8) {
333 ret
= zd1301_demod_wreg(dev
, 0x6811, 0x80);
336 ret
= zd1301_demod_wreg(dev
, 0x6812, 0x05);
339 ret
= zd1301_demod_wreg(dev
, 0x6813, msg
[1].addr
<< 1);
342 ret
= zd1301_demod_wreg(dev
, 0x6801, msg
[0].buf
[0]);
345 ret
= zd1301_demod_wreg(dev
, 0x6802, 0x00);
348 ret
= zd1301_demod_wreg(dev
, 0x6803, 0x06);
351 ret
= zd1301_demod_wreg(dev
, 0x6805, 0x00);
354 ret
= zd1301_demod_wreg(dev
, 0x6804, msg
[1].len
);
358 /* Poll xfer ready */
359 timeout
= jiffies
+ msecs_to_jiffies(I2C_XFER_TIMEOUT
);
360 for (u8tmp
= 1; !time_after(jiffies
, timeout
) && u8tmp
;) {
361 usleep_range(500, 800);
363 ret
= zd1301_demod_rreg(dev
, 0x6804, &u8tmp
);
368 for (i
= 0; i
< msg
[1].len
; i
++) {
369 ret
= zd1301_demod_rreg(dev
, 0x0600 + i
, &msg
[1].buf
[i
]);
373 } else if (ZD1301_IS_I2C_XFER_WRITE(msg
, num
)) {
374 dev_dbg(&pdev
->dev
, "write msg[0].len=%u\n", msg
[0].len
);
375 if (msg
[0].len
> 1 + 8) {
380 ret
= zd1301_demod_wreg(dev
, 0x6811, 0x80);
383 ret
= zd1301_demod_wreg(dev
, 0x6812, 0x01);
386 ret
= zd1301_demod_wreg(dev
, 0x6813, msg
[0].addr
<< 1);
389 ret
= zd1301_demod_wreg(dev
, 0x6800, msg
[0].buf
[0]);
392 ret
= zd1301_demod_wreg(dev
, 0x6802, 0x00);
395 ret
= zd1301_demod_wreg(dev
, 0x6803, 0x06);
399 for (i
= 0; i
< msg
[0].len
- 1; i
++) {
400 ret
= zd1301_demod_wreg(dev
, 0x0600 + i
, msg
[0].buf
[1 + i
]);
405 ret
= zd1301_demod_wreg(dev
, 0x6805, 0x80);
408 ret
= zd1301_demod_wreg(dev
, 0x6804, msg
[0].len
- 1);
412 /* Poll xfer ready */
413 timeout
= jiffies
+ msecs_to_jiffies(I2C_XFER_TIMEOUT
);
414 for (u8tmp
= 1; !time_after(jiffies
, timeout
) && u8tmp
;) {
415 usleep_range(500, 800);
417 ret
= zd1301_demod_rreg(dev
, 0x6804, &u8tmp
);
422 dev_dbg(&pdev
->dev
, "unknown msg[0].len=%u\n", msg
[0].len
);
429 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
433 static u32
zd1301_demod_i2c_functionality(struct i2c_adapter
*adapter
)
438 static const struct i2c_algorithm zd1301_demod_i2c_algorithm
= {
439 .master_xfer
= zd1301_demod_i2c_master_xfer
,
440 .functionality
= zd1301_demod_i2c_functionality
,
443 struct i2c_adapter
*zd1301_demod_get_i2c_adapter(struct platform_device
*pdev
)
445 struct zd1301_demod_dev
*dev
= platform_get_drvdata(pdev
);
447 dev_dbg(&pdev
->dev
, "\n");
449 return &dev
->adapter
;
451 EXPORT_SYMBOL(zd1301_demod_get_i2c_adapter
);
453 /* Platform driver interface */
454 static int zd1301_demod_probe(struct platform_device
*pdev
)
456 struct zd1301_demod_dev
*dev
;
457 struct zd1301_demod_platform_data
*pdata
= pdev
->dev
.platform_data
;
460 dev_dbg(&pdev
->dev
, "\n");
464 dev_err(&pdev
->dev
, "cannot proceed without platform data\n");
467 if (!pdev
->dev
.parent
->driver
) {
469 dev_dbg(&pdev
->dev
, "no parent device\n");
473 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
479 /* Setup the state */
481 dev
->gain
= zd1301_demod_gain
;
484 ret
= zd1301_demod_wreg(dev
, 0x6840, 0x21);
487 ret
= zd1301_demod_wreg(dev
, 0x6a38, 0x07);
491 /* Create I2C adapter */
492 strscpy(dev
->adapter
.name
, "ZyDAS ZD1301 demod",
493 sizeof(dev
->adapter
.name
));
494 dev
->adapter
.algo
= &zd1301_demod_i2c_algorithm
;
495 dev
->adapter
.algo_data
= NULL
;
496 dev
->adapter
.dev
.parent
= pdev
->dev
.parent
;
497 i2c_set_adapdata(&dev
->adapter
, dev
);
498 ret
= i2c_add_adapter(&dev
->adapter
);
500 dev_err(&pdev
->dev
, "I2C adapter add failed %d\n", ret
);
504 /* Create dvb frontend */
505 memcpy(&dev
->frontend
.ops
, &zd1301_demod_ops
, sizeof(dev
->frontend
.ops
));
506 dev
->frontend
.demodulator_priv
= dev
;
507 platform_set_drvdata(pdev
, dev
);
508 dev_info(&pdev
->dev
, "ZyDAS ZD1301 demod attached\n");
514 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
518 static int zd1301_demod_remove(struct platform_device
*pdev
)
520 struct zd1301_demod_dev
*dev
= platform_get_drvdata(pdev
);
522 dev_dbg(&pdev
->dev
, "\n");
524 i2c_del_adapter(&dev
->adapter
);
530 static struct platform_driver zd1301_demod_driver
= {
532 .name
= "zd1301_demod",
533 .suppress_bind_attrs
= true,
535 .probe
= zd1301_demod_probe
,
536 .remove
= zd1301_demod_remove
,
538 module_platform_driver(zd1301_demod_driver
);
540 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
541 MODULE_DESCRIPTION("ZyDAS ZD1301 demodulator driver");
542 MODULE_LICENSE("GPL");