2 * ZyDAS ZD1301 driver (demodulator)
4 * Copyright (C) 2015 Antti Palosaari <crope@iki.fi>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
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.
17 #include "zd1301_demod.h"
19 static u8 zd1301_demod_gain
= 0x38;
20 module_param_named(gain
, zd1301_demod_gain
, byte
, 0644);
21 MODULE_PARM_DESC(gain
, "gain (value: 0x00 - 0x70, default: 0x38)");
23 struct zd1301_demod_dev
{
24 struct platform_device
*pdev
;
25 struct dvb_frontend frontend
;
26 struct i2c_adapter adapter
;
30 static int zd1301_demod_wreg(struct zd1301_demod_dev
*dev
, u16 reg
, u8 val
)
32 struct platform_device
*pdev
= dev
->pdev
;
33 struct zd1301_demod_platform_data
*pdata
= pdev
->dev
.platform_data
;
35 return pdata
->reg_write(pdata
->reg_priv
, reg
, val
);
38 static int zd1301_demod_rreg(struct zd1301_demod_dev
*dev
, u16 reg
, u8
*val
)
40 struct platform_device
*pdev
= dev
->pdev
;
41 struct zd1301_demod_platform_data
*pdata
= pdev
->dev
.platform_data
;
43 return pdata
->reg_read(pdata
->reg_priv
, reg
, val
);
46 static int zd1301_demod_set_frontend(struct dvb_frontend
*fe
)
48 struct zd1301_demod_dev
*dev
= fe
->demodulator_priv
;
49 struct platform_device
*pdev
= dev
->pdev
;
50 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
55 dev_dbg(&pdev
->dev
, "frequency=%u bandwidth_hz=%u\n",
56 c
->frequency
, c
->bandwidth_hz
);
59 if (fe
->ops
.tuner_ops
.set_params
&&
60 fe
->ops
.tuner_ops
.get_if_frequency
) {
61 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
64 ret
= fe
->ops
.tuner_ops
.get_if_frequency(fe
, &if_frequency
);
72 dev_dbg(&pdev
->dev
, "if_frequency=%u\n", if_frequency
);
73 if (if_frequency
!= 36150000) {
78 switch (c
->bandwidth_hz
) {
93 ret
= zd1301_demod_wreg(dev
, 0x6a60, 0x11);
96 ret
= zd1301_demod_wreg(dev
, 0x6a47, 0x46);
99 ret
= zd1301_demod_wreg(dev
, 0x6a48, 0x46);
102 ret
= zd1301_demod_wreg(dev
, 0x6a4a, 0x15);
105 ret
= zd1301_demod_wreg(dev
, 0x6a4b, 0x63);
108 ret
= zd1301_demod_wreg(dev
, 0x6a5b, 0x99);
111 ret
= zd1301_demod_wreg(dev
, 0x6a3b, 0x10);
114 ret
= zd1301_demod_wreg(dev
, 0x6806, 0x01);
117 ret
= zd1301_demod_wreg(dev
, 0x6a41, 0x08);
120 ret
= zd1301_demod_wreg(dev
, 0x6a42, 0x46);
123 ret
= zd1301_demod_wreg(dev
, 0x6a44, 0x14);
126 ret
= zd1301_demod_wreg(dev
, 0x6a45, 0x67);
129 ret
= zd1301_demod_wreg(dev
, 0x6a38, 0x00);
132 ret
= zd1301_demod_wreg(dev
, 0x6a4c, 0x52);
135 ret
= zd1301_demod_wreg(dev
, 0x6a49, 0x2a);
138 ret
= zd1301_demod_wreg(dev
, 0x6840, 0x2e);
141 ret
= zd1301_demod_wreg(dev
, 0x6a50, r6a50_val
);
144 ret
= zd1301_demod_wreg(dev
, 0x6a38, 0x07);
150 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
154 static int zd1301_demod_sleep(struct dvb_frontend
*fe
)
156 struct zd1301_demod_dev
*dev
= fe
->demodulator_priv
;
157 struct platform_device
*pdev
= dev
->pdev
;
160 dev_dbg(&pdev
->dev
, "\n");
162 ret
= zd1301_demod_wreg(dev
, 0x6a43, 0x70);
165 ret
= zd1301_demod_wreg(dev
, 0x684e, 0x00);
168 ret
= zd1301_demod_wreg(dev
, 0x6849, 0x00);
171 ret
= zd1301_demod_wreg(dev
, 0x68e2, 0xd7);
174 ret
= zd1301_demod_wreg(dev
, 0x68e0, 0x39);
177 ret
= zd1301_demod_wreg(dev
, 0x6840, 0x21);
183 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
187 static int zd1301_demod_init(struct dvb_frontend
*fe
)
189 struct zd1301_demod_dev
*dev
= fe
->demodulator_priv
;
190 struct platform_device
*pdev
= dev
->pdev
;
193 dev_dbg(&pdev
->dev
, "\n");
195 ret
= zd1301_demod_wreg(dev
, 0x6840, 0x26);
198 ret
= zd1301_demod_wreg(dev
, 0x68e0, 0xff);
201 ret
= zd1301_demod_wreg(dev
, 0x68e2, 0xd8);
204 ret
= zd1301_demod_wreg(dev
, 0x6849, 0x4e);
207 ret
= zd1301_demod_wreg(dev
, 0x684e, 0x01);
210 ret
= zd1301_demod_wreg(dev
, 0x6a43, zd1301_demod_gain
);
216 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
220 static int zd1301_demod_get_tune_settings(struct dvb_frontend
*fe
,
221 struct dvb_frontend_tune_settings
*settings
)
223 struct zd1301_demod_dev
*dev
= fe
->demodulator_priv
;
224 struct platform_device
*pdev
= dev
->pdev
;
226 dev_dbg(&pdev
->dev
, "\n");
228 /* ~180ms seems to be enough */
229 settings
->min_delay_ms
= 400;
234 static int zd1301_demod_read_status(struct dvb_frontend
*fe
,
235 enum fe_status
*status
)
237 struct zd1301_demod_dev
*dev
= fe
->demodulator_priv
;
238 struct platform_device
*pdev
= dev
->pdev
;
242 ret
= zd1301_demod_rreg(dev
, 0x6a24, &u8tmp
);
245 if (u8tmp
> 0x00 && u8tmp
< 0x20)
246 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
| FE_HAS_VITERBI
|
247 FE_HAS_SYNC
| FE_HAS_LOCK
;
251 dev_dbg(&pdev
->dev
, "lock byte=%02x\n", u8tmp
);
254 * Interesting registers here are:
255 * 0x6a05: get some gain value
256 * 0x6a06: get about same gain value than set to 0x6a43
257 * 0x6a07: get some gain value
258 * 0x6a43: set gain value by driver
259 * 0x6a24: get demod lock bits (FSM stage?)
261 * Driver should implement some kind of algorithm to calculate suitable
262 * value for register 0x6a43, based likely values from register 0x6a05
263 * and 0x6a07. Looks like gain register 0x6a43 value could be from
267 if (dev
->gain
!= zd1301_demod_gain
) {
268 dev
->gain
= zd1301_demod_gain
;
270 ret
= zd1301_demod_wreg(dev
, 0x6a43, dev
->gain
);
277 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
281 static const struct dvb_frontend_ops zd1301_demod_ops
= {
282 .delsys
= {SYS_DVBT
},
284 .name
= "ZyDAS ZD1301",
285 .caps
= FE_CAN_FEC_1_2
|
295 FE_CAN_TRANSMISSION_MODE_AUTO
|
296 FE_CAN_GUARD_INTERVAL_AUTO
|
297 FE_CAN_HIERARCHY_AUTO
|
301 .sleep
= zd1301_demod_sleep
,
302 .init
= zd1301_demod_init
,
303 .set_frontend
= zd1301_demod_set_frontend
,
304 .get_tune_settings
= zd1301_demod_get_tune_settings
,
305 .read_status
= zd1301_demod_read_status
,
308 struct dvb_frontend
*zd1301_demod_get_dvb_frontend(struct platform_device
*pdev
)
310 struct zd1301_demod_dev
*dev
= platform_get_drvdata(pdev
);
312 dev_dbg(&pdev
->dev
, "\n");
314 return &dev
->frontend
;
316 EXPORT_SYMBOL(zd1301_demod_get_dvb_frontend
);
318 static int zd1301_demod_i2c_master_xfer(struct i2c_adapter
*adapter
,
319 struct i2c_msg msg
[], int num
)
321 struct zd1301_demod_dev
*dev
= i2c_get_adapdata(adapter
);
322 struct platform_device
*pdev
= dev
->pdev
;
324 unsigned long timeout
;
327 #define I2C_XFER_TIMEOUT 5
328 #define ZD1301_IS_I2C_XFER_WRITE_READ(_msg, _num) \
329 (_num == 2 && !(_msg[0].flags & I2C_M_RD) && (_msg[1].flags & I2C_M_RD))
330 #define ZD1301_IS_I2C_XFER_WRITE(_msg, _num) \
331 (_num == 1 && !(_msg[0].flags & I2C_M_RD))
332 #define ZD1301_IS_I2C_XFER_READ(_msg, _num) \
333 (_num == 1 && (_msg[0].flags & I2C_M_RD))
334 if (ZD1301_IS_I2C_XFER_WRITE_READ(msg
, num
)) {
335 dev_dbg(&pdev
->dev
, "write&read msg[0].len=%u msg[1].len=%u\n",
336 msg
[0].len
, msg
[1].len
);
337 if (msg
[0].len
> 1 || msg
[1].len
> 8) {
342 ret
= zd1301_demod_wreg(dev
, 0x6811, 0x80);
345 ret
= zd1301_demod_wreg(dev
, 0x6812, 0x05);
348 ret
= zd1301_demod_wreg(dev
, 0x6813, msg
[1].addr
<< 1);
351 ret
= zd1301_demod_wreg(dev
, 0x6801, msg
[0].buf
[0]);
354 ret
= zd1301_demod_wreg(dev
, 0x6802, 0x00);
357 ret
= zd1301_demod_wreg(dev
, 0x6803, 0x06);
360 ret
= zd1301_demod_wreg(dev
, 0x6805, 0x00);
363 ret
= zd1301_demod_wreg(dev
, 0x6804, msg
[1].len
);
367 /* Poll xfer ready */
368 timeout
= jiffies
+ msecs_to_jiffies(I2C_XFER_TIMEOUT
);
369 for (u8tmp
= 1; !time_after(jiffies
, timeout
) && u8tmp
;) {
370 usleep_range(500, 800);
372 ret
= zd1301_demod_rreg(dev
, 0x6804, &u8tmp
);
377 for (i
= 0; i
< msg
[1].len
; i
++) {
378 ret
= zd1301_demod_rreg(dev
, 0x0600 + i
, &msg
[1].buf
[i
]);
382 } else if (ZD1301_IS_I2C_XFER_WRITE(msg
, num
)) {
383 dev_dbg(&pdev
->dev
, "write msg[0].len=%u\n", msg
[0].len
);
384 if (msg
[0].len
> 1 + 8) {
389 ret
= zd1301_demod_wreg(dev
, 0x6811, 0x80);
392 ret
= zd1301_demod_wreg(dev
, 0x6812, 0x01);
395 ret
= zd1301_demod_wreg(dev
, 0x6813, msg
[0].addr
<< 1);
398 ret
= zd1301_demod_wreg(dev
, 0x6800, msg
[0].buf
[0]);
401 ret
= zd1301_demod_wreg(dev
, 0x6802, 0x00);
404 ret
= zd1301_demod_wreg(dev
, 0x6803, 0x06);
408 for (i
= 0; i
< msg
[0].len
- 1; i
++) {
409 ret
= zd1301_demod_wreg(dev
, 0x0600 + i
, msg
[0].buf
[1 + i
]);
414 ret
= zd1301_demod_wreg(dev
, 0x6805, 0x80);
417 ret
= zd1301_demod_wreg(dev
, 0x6804, msg
[0].len
- 1);
421 /* Poll xfer ready */
422 timeout
= jiffies
+ msecs_to_jiffies(I2C_XFER_TIMEOUT
);
423 for (u8tmp
= 1; !time_after(jiffies
, timeout
) && u8tmp
;) {
424 usleep_range(500, 800);
426 ret
= zd1301_demod_rreg(dev
, 0x6804, &u8tmp
);
431 dev_dbg(&pdev
->dev
, "unknown msg[0].len=%u\n", msg
[0].len
);
439 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
443 static u32
zd1301_demod_i2c_functionality(struct i2c_adapter
*adapter
)
448 static const struct i2c_algorithm zd1301_demod_i2c_algorithm
= {
449 .master_xfer
= zd1301_demod_i2c_master_xfer
,
450 .functionality
= zd1301_demod_i2c_functionality
,
453 struct i2c_adapter
*zd1301_demod_get_i2c_adapter(struct platform_device
*pdev
)
455 struct zd1301_demod_dev
*dev
= platform_get_drvdata(pdev
);
457 dev_dbg(&pdev
->dev
, "\n");
459 return &dev
->adapter
;
461 EXPORT_SYMBOL(zd1301_demod_get_i2c_adapter
);
463 /* Platform driver interface */
464 static int zd1301_demod_probe(struct platform_device
*pdev
)
466 struct zd1301_demod_dev
*dev
;
467 struct zd1301_demod_platform_data
*pdata
= pdev
->dev
.platform_data
;
470 dev_dbg(&pdev
->dev
, "\n");
474 dev_err(&pdev
->dev
, "cannot proceed without platform data\n");
477 if (!pdev
->dev
.parent
->driver
) {
479 dev_dbg(&pdev
->dev
, "no parent device\n");
483 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
489 /* Setup the state */
491 dev
->gain
= zd1301_demod_gain
;
494 ret
= zd1301_demod_wreg(dev
, 0x6840, 0x21);
497 ret
= zd1301_demod_wreg(dev
, 0x6a38, 0x07);
501 /* Create I2C adapter */
502 strlcpy(dev
->adapter
.name
, "ZyDAS ZD1301 demod", sizeof(dev
->adapter
.name
));
503 dev
->adapter
.algo
= &zd1301_demod_i2c_algorithm
;
504 dev
->adapter
.algo_data
= NULL
;
505 dev
->adapter
.dev
.parent
= pdev
->dev
.parent
;
506 i2c_set_adapdata(&dev
->adapter
, dev
);
507 ret
= i2c_add_adapter(&dev
->adapter
);
509 dev_err(&pdev
->dev
, "I2C adapter add failed %d\n", ret
);
513 /* Create dvb frontend */
514 memcpy(&dev
->frontend
.ops
, &zd1301_demod_ops
, sizeof(dev
->frontend
.ops
));
515 dev
->frontend
.demodulator_priv
= dev
;
516 platform_set_drvdata(pdev
, dev
);
517 dev_info(&pdev
->dev
, "ZyDAS ZD1301 demod attached\n");
523 dev_dbg(&pdev
->dev
, "failed=%d\n", ret
);
527 static int zd1301_demod_remove(struct platform_device
*pdev
)
529 struct zd1301_demod_dev
*dev
= platform_get_drvdata(pdev
);
531 dev_dbg(&pdev
->dev
, "\n");
533 i2c_del_adapter(&dev
->adapter
);
539 static struct platform_driver zd1301_demod_driver
= {
541 .name
= "zd1301_demod",
542 .suppress_bind_attrs
= true,
544 .probe
= zd1301_demod_probe
,
545 .remove
= zd1301_demod_remove
,
547 module_platform_driver(zd1301_demod_driver
);
549 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
550 MODULE_DESCRIPTION("ZyDAS ZD1301 demodulator driver");
551 MODULE_LICENSE("GPL");