2 * Panasonic MN88473 DVB-T/T2/C demodulator driver
4 * Copyright (C) 2014 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 "mn88473_priv.h"
19 static int mn88473_get_tune_settings(struct dvb_frontend
*fe
,
20 struct dvb_frontend_tune_settings
*s
)
22 s
->min_delay_ms
= 1000;
26 static int mn88473_set_frontend(struct dvb_frontend
*fe
)
28 struct i2c_client
*client
= fe
->demodulator_priv
;
29 struct mn88473_dev
*dev
= i2c_get_clientdata(client
);
30 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
34 u8 delivery_system_val
, if_val
[3], *conf_val_ptr
;
35 u8 reg_bank2_2d_val
, reg_bank0_d2_val
;
38 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%d stream_id=%d\n",
39 c
->delivery_system
, c
->modulation
, c
->frequency
,
40 c
->bandwidth_hz
, c
->symbol_rate
, c
->inversion
, c
->stream_id
);
47 switch (c
->delivery_system
) {
49 delivery_system_val
= 0x02;
50 reg_bank2_2d_val
= 0x23;
51 reg_bank0_d2_val
= 0x2a;
54 delivery_system_val
= 0x03;
55 reg_bank2_2d_val
= 0x3b;
56 reg_bank0_d2_val
= 0x29;
58 case SYS_DVBC_ANNEX_A
:
59 delivery_system_val
= 0x04;
60 reg_bank2_2d_val
= 0x3b;
61 reg_bank0_d2_val
= 0x29;
68 switch (c
->delivery_system
) {
71 switch (c
->bandwidth_hz
) {
73 conf_val_ptr
= "\xe9\x55\x55\x1c\x29\x1c\x29";
76 conf_val_ptr
= "\xc8\x00\x00\x17\x0a\x17\x0a";
79 conf_val_ptr
= "\xaf\x00\x00\x11\xec\x11\xec";
86 case SYS_DVBC_ANNEX_A
:
87 conf_val_ptr
= "\x10\xab\x0d\xae\x1d\x9d";
94 if (fe
->ops
.tuner_ops
.set_params
) {
95 ret
= fe
->ops
.tuner_ops
.set_params(fe
);
100 if (fe
->ops
.tuner_ops
.get_if_frequency
) {
101 ret
= fe
->ops
.tuner_ops
.get_if_frequency(fe
, &if_frequency
);
105 dev_dbg(&client
->dev
, "get_if_frequency=%u\n", if_frequency
);
111 /* Calculate IF registers */
112 uitmp
= DIV_ROUND_CLOSEST_ULL((u64
) if_frequency
* 0x1000000, dev
->clk
);
113 if_val
[0] = (uitmp
>> 16) & 0xff;
114 if_val
[1] = (uitmp
>> 8) & 0xff;
115 if_val
[2] = (uitmp
>> 0) & 0xff;
117 ret
= regmap_write(dev
->regmap
[2], 0x05, 0x00);
120 ret
= regmap_write(dev
->regmap
[2], 0xfb, 0x13);
123 ret
= regmap_write(dev
->regmap
[2], 0xef, 0x13);
126 ret
= regmap_write(dev
->regmap
[2], 0xf9, 0x13);
129 ret
= regmap_write(dev
->regmap
[2], 0x00, 0x18);
132 ret
= regmap_write(dev
->regmap
[2], 0x01, 0x01);
135 ret
= regmap_write(dev
->regmap
[2], 0x02, 0x21);
138 ret
= regmap_write(dev
->regmap
[2], 0x03, delivery_system_val
);
141 ret
= regmap_write(dev
->regmap
[2], 0x0b, 0x00);
145 for (i
= 0; i
< sizeof(if_val
); i
++) {
146 ret
= regmap_write(dev
->regmap
[2], 0x10 + i
, if_val
[i
]);
151 switch (c
->delivery_system
) {
154 for (i
= 0; i
< 7; i
++) {
155 ret
= regmap_write(dev
->regmap
[2], 0x13 + i
,
161 case SYS_DVBC_ANNEX_A
:
162 ret
= regmap_bulk_write(dev
->regmap
[1], 0x10, conf_val_ptr
, 6);
170 ret
= regmap_write(dev
->regmap
[2], 0x2d, reg_bank2_2d_val
);
173 ret
= regmap_write(dev
->regmap
[2], 0x2e, 0x00);
176 ret
= regmap_write(dev
->regmap
[2], 0x56, 0x0d);
179 ret
= regmap_bulk_write(dev
->regmap
[0], 0x01,
180 "\xba\x13\x80\xba\x91\xdd\xe7\x28", 8);
183 ret
= regmap_write(dev
->regmap
[0], 0x0a, 0x1a);
186 ret
= regmap_write(dev
->regmap
[0], 0x13, 0x1f);
189 ret
= regmap_write(dev
->regmap
[0], 0x19, 0x03);
192 ret
= regmap_write(dev
->regmap
[0], 0x1d, 0xb0);
195 ret
= regmap_write(dev
->regmap
[0], 0x2a, 0x72);
198 ret
= regmap_write(dev
->regmap
[0], 0x2d, 0x00);
201 ret
= regmap_write(dev
->regmap
[0], 0x3c, 0x00);
204 ret
= regmap_write(dev
->regmap
[0], 0x3f, 0xf8);
207 ret
= regmap_bulk_write(dev
->regmap
[0], 0x40, "\xf4\x08", 2);
210 ret
= regmap_write(dev
->regmap
[0], 0xd2, reg_bank0_d2_val
);
213 ret
= regmap_write(dev
->regmap
[0], 0xd4, 0x55);
216 ret
= regmap_write(dev
->regmap
[1], 0xbe, 0x08);
219 ret
= regmap_write(dev
->regmap
[0], 0xb2, 0x37);
222 ret
= regmap_write(dev
->regmap
[0], 0xd7, 0x04);
227 ret
= regmap_write(dev
->regmap
[2], 0xf8, 0x9f);
233 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
237 static int mn88473_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
239 struct i2c_client
*client
= fe
->demodulator_priv
;
240 struct mn88473_dev
*dev
= i2c_get_clientdata(client
);
241 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
252 switch (c
->delivery_system
) {
254 ret
= regmap_read(dev
->regmap
[0], 0x62, &uitmp
);
258 if (!(uitmp
& 0xa0)) {
259 if ((uitmp
& 0x0f) >= 0x09)
260 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
261 FE_HAS_VITERBI
| FE_HAS_SYNC
|
263 else if ((uitmp
& 0x0f) >= 0x03)
264 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
268 ret
= regmap_read(dev
->regmap
[2], 0x8b, &uitmp
);
272 if (!(uitmp
& 0x40)) {
273 if ((uitmp
& 0x0f) >= 0x0d)
274 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
275 FE_HAS_VITERBI
| FE_HAS_SYNC
|
277 else if ((uitmp
& 0x0f) >= 0x0a)
278 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
280 else if ((uitmp
& 0x0f) >= 0x07)
281 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
;
284 case SYS_DVBC_ANNEX_A
:
285 ret
= regmap_read(dev
->regmap
[1], 0x85, &uitmp
);
289 if (!(uitmp
& 0x40)) {
290 ret
= regmap_read(dev
->regmap
[1], 0x89, &uitmp
);
295 *status
= FE_HAS_SIGNAL
| FE_HAS_CARRIER
|
296 FE_HAS_VITERBI
| FE_HAS_SYNC
|
307 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
311 static int mn88473_init(struct dvb_frontend
*fe
)
313 struct i2c_client
*client
= fe
->demodulator_priv
;
314 struct mn88473_dev
*dev
= i2c_get_clientdata(client
);
315 int ret
, len
, remain
;
317 const struct firmware
*fw
;
318 const char *name
= MN88473_FIRMWARE
;
320 dev_dbg(&client
->dev
, "\n");
322 /* Check if firmware is already running */
323 ret
= regmap_read(dev
->regmap
[0], 0xf5, &uitmp
);
330 /* Request the firmware, this will block and timeout */
331 ret
= request_firmware(&fw
, name
, &client
->dev
);
333 dev_err(&client
->dev
, "firmare file '%s' not found\n", name
);
337 dev_info(&client
->dev
, "downloading firmware from file '%s'\n", name
);
339 ret
= regmap_write(dev
->regmap
[0], 0xf5, 0x03);
341 goto err_release_firmware
;
343 for (remain
= fw
->size
; remain
> 0; remain
-= (dev
->i2c_wr_max
- 1)) {
344 len
= min(dev
->i2c_wr_max
- 1, remain
);
345 ret
= regmap_bulk_write(dev
->regmap
[0], 0xf6,
346 &fw
->data
[fw
->size
- remain
], len
);
348 dev_err(&client
->dev
, "firmware download failed %d\n",
350 goto err_release_firmware
;
354 release_firmware(fw
);
356 /* Parity check of firmware */
357 ret
= regmap_read(dev
->regmap
[0], 0xf8, &uitmp
);
362 dev_err(&client
->dev
, "firmware parity check failed\n");
367 ret
= regmap_write(dev
->regmap
[0], 0xf5, 0x00);
372 ret
= regmap_write(dev
->regmap
[2], 0x09, 0x08);
375 ret
= regmap_write(dev
->regmap
[2], 0x08, 0x1d);
382 err_release_firmware
:
383 release_firmware(fw
);
385 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
389 static int mn88473_sleep(struct dvb_frontend
*fe
)
391 struct i2c_client
*client
= fe
->demodulator_priv
;
392 struct mn88473_dev
*dev
= i2c_get_clientdata(client
);
395 dev_dbg(&client
->dev
, "\n");
399 ret
= regmap_write(dev
->regmap
[2], 0x05, 0x3e);
405 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
409 static const struct dvb_frontend_ops mn88473_ops
= {
410 .delsys
= {SYS_DVBT
, SYS_DVBT2
, SYS_DVBC_ANNEX_A
},
412 .name
= "Panasonic MN88473",
413 .symbol_rate_min
= 1000000,
414 .symbol_rate_max
= 7200000,
415 .caps
= FE_CAN_FEC_1_2
|
428 FE_CAN_TRANSMISSION_MODE_AUTO
|
429 FE_CAN_GUARD_INTERVAL_AUTO
|
430 FE_CAN_HIERARCHY_AUTO
|
435 .get_tune_settings
= mn88473_get_tune_settings
,
437 .init
= mn88473_init
,
438 .sleep
= mn88473_sleep
,
440 .set_frontend
= mn88473_set_frontend
,
442 .read_status
= mn88473_read_status
,
445 static int mn88473_probe(struct i2c_client
*client
,
446 const struct i2c_device_id
*id
)
448 struct mn88473_config
*config
= client
->dev
.platform_data
;
449 struct mn88473_dev
*dev
;
452 static const struct regmap_config regmap_config
= {
457 dev_dbg(&client
->dev
, "\n");
459 /* Caller really need to provide pointer for frontend we create */
460 if (config
->fe
== NULL
) {
461 dev_err(&client
->dev
, "frontend pointer not defined\n");
466 dev
= kzalloc(sizeof(*dev
), GFP_KERNEL
);
472 if (config
->i2c_wr_max
)
473 dev
->i2c_wr_max
= config
->i2c_wr_max
;
475 dev
->i2c_wr_max
= ~0;
478 dev
->clk
= config
->xtal
;
481 dev
->client
[0] = client
;
482 dev
->regmap
[0] = regmap_init_i2c(dev
->client
[0], ®map_config
);
483 if (IS_ERR(dev
->regmap
[0])) {
484 ret
= PTR_ERR(dev
->regmap
[0]);
488 /* Check demod answers with correct chip id */
489 ret
= regmap_read(dev
->regmap
[0], 0xff, &uitmp
);
491 goto err_regmap_0_regmap_exit
;
493 dev_dbg(&client
->dev
, "chip id=%02x\n", uitmp
);
497 goto err_regmap_0_regmap_exit
;
501 * Chip has three I2C addresses for different register banks. Used
502 * addresses are 0x18, 0x1a and 0x1c. We register two dummy clients,
503 * 0x1a and 0x1c, in order to get own I2C client for each register bank.
505 * Also, register bank 2 do not support sequential I/O. Only single
506 * register write or read is allowed to that bank.
508 dev
->client
[1] = i2c_new_dummy(client
->adapter
, 0x1a);
509 if (dev
->client
[1] == NULL
) {
511 dev_err(&client
->dev
, "I2C registration failed\n");
513 goto err_regmap_0_regmap_exit
;
515 dev
->regmap
[1] = regmap_init_i2c(dev
->client
[1], ®map_config
);
516 if (IS_ERR(dev
->regmap
[1])) {
517 ret
= PTR_ERR(dev
->regmap
[1]);
518 goto err_client_1_i2c_unregister_device
;
520 i2c_set_clientdata(dev
->client
[1], dev
);
522 dev
->client
[2] = i2c_new_dummy(client
->adapter
, 0x1c);
523 if (dev
->client
[2] == NULL
) {
525 dev_err(&client
->dev
, "2nd I2C registration failed\n");
527 goto err_regmap_1_regmap_exit
;
529 dev
->regmap
[2] = regmap_init_i2c(dev
->client
[2], ®map_config
);
530 if (IS_ERR(dev
->regmap
[2])) {
531 ret
= PTR_ERR(dev
->regmap
[2]);
532 goto err_client_2_i2c_unregister_device
;
534 i2c_set_clientdata(dev
->client
[2], dev
);
536 /* Sleep because chip is active by default */
537 ret
= regmap_write(dev
->regmap
[2], 0x05, 0x3e);
539 goto err_client_2_i2c_unregister_device
;
541 /* Create dvb frontend */
542 memcpy(&dev
->frontend
.ops
, &mn88473_ops
, sizeof(dev
->frontend
.ops
));
543 dev
->frontend
.demodulator_priv
= client
;
544 *config
->fe
= &dev
->frontend
;
545 i2c_set_clientdata(client
, dev
);
547 dev_info(&client
->dev
, "Panasonic MN88473 successfully identified\n");
551 err_client_2_i2c_unregister_device
:
552 i2c_unregister_device(dev
->client
[2]);
553 err_regmap_1_regmap_exit
:
554 regmap_exit(dev
->regmap
[1]);
555 err_client_1_i2c_unregister_device
:
556 i2c_unregister_device(dev
->client
[1]);
557 err_regmap_0_regmap_exit
:
558 regmap_exit(dev
->regmap
[0]);
562 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
566 static int mn88473_remove(struct i2c_client
*client
)
568 struct mn88473_dev
*dev
= i2c_get_clientdata(client
);
570 dev_dbg(&client
->dev
, "\n");
572 regmap_exit(dev
->regmap
[2]);
573 i2c_unregister_device(dev
->client
[2]);
575 regmap_exit(dev
->regmap
[1]);
576 i2c_unregister_device(dev
->client
[1]);
578 regmap_exit(dev
->regmap
[0]);
585 static const struct i2c_device_id mn88473_id_table
[] = {
589 MODULE_DEVICE_TABLE(i2c
, mn88473_id_table
);
591 static struct i2c_driver mn88473_driver
= {
594 .suppress_bind_attrs
= true,
596 .probe
= mn88473_probe
,
597 .remove
= mn88473_remove
,
598 .id_table
= mn88473_id_table
,
601 module_i2c_driver(mn88473_driver
);
603 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
604 MODULE_DESCRIPTION("Panasonic MN88473 DVB-T/T2/C demodulator driver");
605 MODULE_LICENSE("GPL");
606 MODULE_FIRMWARE(MN88473_FIRMWARE
);