1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Sony CXD2820R demodulator driver
5 * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
9 #include "cxd2820r_priv.h"
11 /* Write register table */
12 int cxd2820r_wr_reg_val_mask_tab(struct cxd2820r_priv
*priv
,
13 const struct reg_val_mask
*tab
, int tab_len
)
15 struct i2c_client
*client
= priv
->client
[0];
17 unsigned int i
, reg
, mask
, val
;
18 struct regmap
*regmap
;
20 dev_dbg(&client
->dev
, "tab_len=%d\n", tab_len
);
22 for (i
= 0; i
< tab_len
; i
++) {
23 if ((tab
[i
].reg
>> 16) & 0x1)
24 regmap
= priv
->regmap
[1];
26 regmap
= priv
->regmap
[0];
28 reg
= (tab
[i
].reg
>> 0) & 0xffff;
33 ret
= regmap_write(regmap
, reg
, val
);
35 ret
= regmap_write_bits(regmap
, reg
, mask
, val
);
42 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
46 int cxd2820r_gpio(struct dvb_frontend
*fe
, u8
*gpio
)
48 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
49 struct i2c_client
*client
= priv
->client
[0];
50 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
54 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
56 /* update GPIOs only when needed */
57 if (!memcmp(gpio
, priv
->gpio
, sizeof(priv
->gpio
)))
62 for (i
= 0; i
< sizeof(priv
->gpio
); i
++) {
63 /* enable / disable */
64 if (gpio
[i
] & CXD2820R_GPIO_E
)
65 tmp0
|= (2 << 6) >> (2 * i
);
67 tmp0
|= (1 << 6) >> (2 * i
);
70 if (gpio
[i
] & CXD2820R_GPIO_I
)
71 tmp1
|= (1 << (3 + i
));
73 tmp1
|= (0 << (3 + i
));
76 if (gpio
[i
] & CXD2820R_GPIO_H
)
77 tmp1
|= (1 << (0 + i
));
79 tmp1
|= (0 << (0 + i
));
81 dev_dbg(&client
->dev
, "gpio i=%d %02x %02x\n", i
, tmp0
, tmp1
);
84 dev_dbg(&client
->dev
, "wr gpio=%02x %02x\n", tmp0
, tmp1
);
86 /* write bits [7:2] */
87 ret
= regmap_update_bits(priv
->regmap
[0], 0x0089, 0xfc, tmp0
);
91 /* write bits [5:0] */
92 ret
= regmap_update_bits(priv
->regmap
[0], 0x008e, 0x3f, tmp1
);
96 memcpy(priv
->gpio
, gpio
, sizeof(priv
->gpio
));
100 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
104 static int cxd2820r_set_frontend(struct dvb_frontend
*fe
)
106 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
107 struct i2c_client
*client
= priv
->client
[0];
108 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
111 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
113 switch (c
->delivery_system
) {
115 ret
= cxd2820r_init_t(fe
);
118 ret
= cxd2820r_set_frontend_t(fe
);
123 ret
= cxd2820r_init_t(fe
);
126 ret
= cxd2820r_set_frontend_t2(fe
);
130 case SYS_DVBC_ANNEX_A
:
131 ret
= cxd2820r_init_c(fe
);
134 ret
= cxd2820r_set_frontend_c(fe
);
139 dev_dbg(&client
->dev
, "invalid delivery_system\n");
147 static int cxd2820r_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
149 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
150 struct i2c_client
*client
= priv
->client
[0];
151 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
154 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
156 switch (c
->delivery_system
) {
158 ret
= cxd2820r_read_status_t(fe
, status
);
161 ret
= cxd2820r_read_status_t2(fe
, status
);
163 case SYS_DVBC_ANNEX_A
:
164 ret
= cxd2820r_read_status_c(fe
, status
);
173 static int cxd2820r_get_frontend(struct dvb_frontend
*fe
,
174 struct dtv_frontend_properties
*p
)
176 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
177 struct i2c_client
*client
= priv
->client
[0];
178 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
181 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
183 if (priv
->delivery_system
== SYS_UNDEFINED
)
186 switch (c
->delivery_system
) {
188 ret
= cxd2820r_get_frontend_t(fe
, p
);
191 ret
= cxd2820r_get_frontend_t2(fe
, p
);
193 case SYS_DVBC_ANNEX_A
:
194 ret
= cxd2820r_get_frontend_c(fe
, p
);
203 static int cxd2820r_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
205 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
206 struct i2c_client
*client
= priv
->client
[0];
207 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
209 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
211 *ber
= (priv
->post_bit_error
- priv
->post_bit_error_prev_dvbv3
);
212 priv
->post_bit_error_prev_dvbv3
= priv
->post_bit_error
;
217 static int cxd2820r_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
219 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
220 struct i2c_client
*client
= priv
->client
[0];
221 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
223 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
225 if (c
->strength
.stat
[0].scale
== FE_SCALE_RELATIVE
)
226 *strength
= c
->strength
.stat
[0].uvalue
;
233 static int cxd2820r_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
235 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
236 struct i2c_client
*client
= priv
->client
[0];
237 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
239 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
241 if (c
->cnr
.stat
[0].scale
== FE_SCALE_DECIBEL
)
242 *snr
= div_s64(c
->cnr
.stat
[0].svalue
, 100);
249 static int cxd2820r_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
251 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
252 struct i2c_client
*client
= priv
->client
[0];
253 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
255 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
262 static int cxd2820r_init(struct dvb_frontend
*fe
)
267 static int cxd2820r_sleep(struct dvb_frontend
*fe
)
269 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
270 struct i2c_client
*client
= priv
->client
[0];
271 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
274 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
276 switch (c
->delivery_system
) {
278 ret
= cxd2820r_sleep_t(fe
);
281 ret
= cxd2820r_sleep_t2(fe
);
283 case SYS_DVBC_ANNEX_A
:
284 ret
= cxd2820r_sleep_c(fe
);
293 static int cxd2820r_get_tune_settings(struct dvb_frontend
*fe
,
294 struct dvb_frontend_tune_settings
*s
)
296 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
297 struct i2c_client
*client
= priv
->client
[0];
298 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
301 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
303 switch (c
->delivery_system
) {
305 ret
= cxd2820r_get_tune_settings_t(fe
, s
);
308 ret
= cxd2820r_get_tune_settings_t2(fe
, s
);
310 case SYS_DVBC_ANNEX_A
:
311 ret
= cxd2820r_get_tune_settings_c(fe
, s
);
320 static enum dvbfe_search
cxd2820r_search(struct dvb_frontend
*fe
)
322 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
323 struct i2c_client
*client
= priv
->client
[0];
324 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
326 enum fe_status status
= 0;
328 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
330 /* switch between DVB-T and DVB-T2 when tune fails */
331 if (priv
->last_tune_failed
) {
332 if (priv
->delivery_system
== SYS_DVBT
) {
333 ret
= cxd2820r_sleep_t(fe
);
337 c
->delivery_system
= SYS_DVBT2
;
338 } else if (priv
->delivery_system
== SYS_DVBT2
) {
339 ret
= cxd2820r_sleep_t2(fe
);
343 c
->delivery_system
= SYS_DVBT
;
348 ret
= cxd2820r_set_frontend(fe
);
352 /* frontend lock wait loop count */
353 switch (priv
->delivery_system
) {
355 case SYS_DVBC_ANNEX_A
:
367 /* wait frontend lock */
369 dev_dbg(&client
->dev
, "loop=%d\n", i
);
371 ret
= cxd2820r_read_status(fe
, &status
);
375 if (status
& FE_HAS_LOCK
)
379 /* check if we have a valid signal */
380 if (status
& FE_HAS_LOCK
) {
381 priv
->last_tune_failed
= false;
382 return DVBFE_ALGO_SEARCH_SUCCESS
;
384 priv
->last_tune_failed
= true;
385 return DVBFE_ALGO_SEARCH_AGAIN
;
389 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
390 return DVBFE_ALGO_SEARCH_ERROR
;
393 static enum dvbfe_algo
cxd2820r_get_frontend_algo(struct dvb_frontend
*fe
)
395 return DVBFE_ALGO_CUSTOM
;
398 static void cxd2820r_release(struct dvb_frontend
*fe
)
400 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
401 struct i2c_client
*client
= priv
->client
[0];
403 dev_dbg(&client
->dev
, "\n");
405 i2c_unregister_device(client
);
410 static int cxd2820r_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
412 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
413 struct i2c_client
*client
= priv
->client
[0];
415 dev_dbg_ratelimited(&client
->dev
, "enable=%d\n", enable
);
417 return regmap_update_bits(priv
->regmap
[0], 0x00db, 0x01, enable
? 1 : 0);
420 #ifdef CONFIG_GPIOLIB
421 static int cxd2820r_gpio_direction_output(struct gpio_chip
*chip
, unsigned nr
,
424 struct cxd2820r_priv
*priv
= gpiochip_get_data(chip
);
425 struct i2c_client
*client
= priv
->client
[0];
428 dev_dbg(&client
->dev
, "nr=%u val=%d\n", nr
, val
);
430 memcpy(gpio
, priv
->gpio
, sizeof(gpio
));
431 gpio
[nr
] = CXD2820R_GPIO_E
| CXD2820R_GPIO_O
| (val
<< 2);
433 return cxd2820r_gpio(&priv
->fe
, gpio
);
436 static void cxd2820r_gpio_set(struct gpio_chip
*chip
, unsigned nr
, int val
)
438 struct cxd2820r_priv
*priv
= gpiochip_get_data(chip
);
439 struct i2c_client
*client
= priv
->client
[0];
442 dev_dbg(&client
->dev
, "nr=%u val=%d\n", nr
, val
);
444 memcpy(gpio
, priv
->gpio
, sizeof(gpio
));
445 gpio
[nr
] = CXD2820R_GPIO_E
| CXD2820R_GPIO_O
| (val
<< 2);
447 (void) cxd2820r_gpio(&priv
->fe
, gpio
);
452 static int cxd2820r_gpio_get(struct gpio_chip
*chip
, unsigned nr
)
454 struct cxd2820r_priv
*priv
= gpiochip_get_data(chip
);
455 struct i2c_client
*client
= priv
->client
[0];
457 dev_dbg(&client
->dev
, "nr=%u\n", nr
);
459 return (priv
->gpio
[nr
] >> 2) & 0x01;
463 static const struct dvb_frontend_ops cxd2820r_ops
= {
464 .delsys
= { SYS_DVBT
, SYS_DVBT2
, SYS_DVBC_ANNEX_A
},
465 /* default: DVB-T/T2 */
467 .name
= "Sony CXD2820R",
469 .caps
= FE_CAN_FEC_1_2
|
482 FE_CAN_TRANSMISSION_MODE_AUTO
|
483 FE_CAN_GUARD_INTERVAL_AUTO
|
484 FE_CAN_HIERARCHY_AUTO
|
486 FE_CAN_2G_MODULATION
|
490 .release
= cxd2820r_release
,
491 .init
= cxd2820r_init
,
492 .sleep
= cxd2820r_sleep
,
494 .get_tune_settings
= cxd2820r_get_tune_settings
,
495 .i2c_gate_ctrl
= cxd2820r_i2c_gate_ctrl
,
497 .get_frontend
= cxd2820r_get_frontend
,
499 .get_frontend_algo
= cxd2820r_get_frontend_algo
,
500 .search
= cxd2820r_search
,
502 .read_status
= cxd2820r_read_status
,
503 .read_snr
= cxd2820r_read_snr
,
504 .read_ber
= cxd2820r_read_ber
,
505 .read_ucblocks
= cxd2820r_read_ucblocks
,
506 .read_signal_strength
= cxd2820r_read_signal_strength
,
510 * XXX: That is wrapper to cxd2820r_probe() via driver core in order to provide
511 * proper I2C client for legacy media attach binding.
512 * New users must use I2C client binding directly!
514 struct dvb_frontend
*cxd2820r_attach(const struct cxd2820r_config
*config
,
515 struct i2c_adapter
*adapter
,
518 struct i2c_client
*client
;
519 struct i2c_board_info board_info
;
520 struct cxd2820r_platform_data pdata
;
522 pdata
.ts_mode
= config
->ts_mode
;
523 pdata
.ts_clk_inv
= config
->ts_clock_inv
;
524 pdata
.if_agc_polarity
= config
->if_agc_polarity
;
525 pdata
.spec_inv
= config
->spec_inv
;
526 pdata
.gpio_chip_base
= &gpio_chip_base
;
527 pdata
.attach_in_use
= true;
529 memset(&board_info
, 0, sizeof(board_info
));
530 strscpy(board_info
.type
, "cxd2820r", I2C_NAME_SIZE
);
531 board_info
.addr
= config
->i2c_address
;
532 board_info
.platform_data
= &pdata
;
533 client
= i2c_new_client_device(adapter
, &board_info
);
534 if (!i2c_client_has_driver(client
))
537 return pdata
.get_dvb_frontend(client
);
539 EXPORT_SYMBOL(cxd2820r_attach
);
541 static struct dvb_frontend
*cxd2820r_get_dvb_frontend(struct i2c_client
*client
)
543 struct cxd2820r_priv
*priv
= i2c_get_clientdata(client
);
545 dev_dbg(&client
->dev
, "\n");
550 static int cxd2820r_probe(struct i2c_client
*client
,
551 const struct i2c_device_id
*id
)
553 struct cxd2820r_platform_data
*pdata
= client
->dev
.platform_data
;
554 struct cxd2820r_priv
*priv
;
555 int ret
, *gpio_chip_base
;
557 static const struct regmap_range_cfg regmap_range_cfg0
[] = {
561 .selector_reg
= 0x00,
562 .selector_mask
= 0xff,
564 .window_start
= 0x00,
568 static const struct regmap_range_cfg regmap_range_cfg1
[] = {
572 .selector_reg
= 0x00,
573 .selector_mask
= 0xff,
575 .window_start
= 0x00,
579 static const struct regmap_config regmap_config0
= {
582 .max_register
= 0x3fff,
583 .ranges
= regmap_range_cfg0
,
584 .num_ranges
= ARRAY_SIZE(regmap_range_cfg0
),
585 .cache_type
= REGCACHE_NONE
,
587 static const struct regmap_config regmap_config1
= {
590 .max_register
= 0x01ff,
591 .ranges
= regmap_range_cfg1
,
592 .num_ranges
= ARRAY_SIZE(regmap_range_cfg1
),
593 .cache_type
= REGCACHE_NONE
,
596 dev_dbg(&client
->dev
, "\n");
598 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
604 priv
->client
[0] = client
;
605 priv
->fe
.demodulator_priv
= priv
;
606 priv
->i2c
= client
->adapter
;
607 priv
->ts_mode
= pdata
->ts_mode
;
608 priv
->ts_clk_inv
= pdata
->ts_clk_inv
;
609 priv
->if_agc_polarity
= pdata
->if_agc_polarity
;
610 priv
->spec_inv
= pdata
->spec_inv
;
611 gpio_chip_base
= *pdata
->gpio_chip_base
;
612 priv
->regmap
[0] = regmap_init_i2c(priv
->client
[0], ®map_config0
);
613 if (IS_ERR(priv
->regmap
[0])) {
614 ret
= PTR_ERR(priv
->regmap
[0]);
618 /* Check demod answers with correct chip id */
619 ret
= regmap_read(priv
->regmap
[0], 0x00fd, &utmp
);
621 goto err_regmap_0_regmap_exit
;
623 dev_dbg(&client
->dev
, "chip_id=%02x\n", utmp
);
627 goto err_regmap_0_regmap_exit
;
631 * Chip has two I2C addresses for different register banks. We register
632 * one dummy I2C client in in order to get own I2C client for each
635 priv
->client
[1] = i2c_new_dummy_device(client
->adapter
, client
->addr
| (1 << 1));
636 if (IS_ERR(priv
->client
[1])) {
637 ret
= PTR_ERR(priv
->client
[1]);
638 dev_err(&client
->dev
, "I2C registration failed\n");
639 goto err_regmap_0_regmap_exit
;
642 priv
->regmap
[1] = regmap_init_i2c(priv
->client
[1], ®map_config1
);
643 if (IS_ERR(priv
->regmap
[1])) {
644 ret
= PTR_ERR(priv
->regmap
[1]);
645 goto err_client_1_i2c_unregister_device
;
648 if (gpio_chip_base
) {
649 #ifdef CONFIG_GPIOLIB
651 priv
->gpio_chip
.label
= KBUILD_MODNAME
;
652 priv
->gpio_chip
.parent
= &client
->dev
;
653 priv
->gpio_chip
.owner
= THIS_MODULE
;
654 priv
->gpio_chip
.direction_output
= cxd2820r_gpio_direction_output
;
655 priv
->gpio_chip
.set
= cxd2820r_gpio_set
;
656 priv
->gpio_chip
.get
= cxd2820r_gpio_get
;
657 priv
->gpio_chip
.base
= -1; /* Dynamic allocation */
658 priv
->gpio_chip
.ngpio
= GPIO_COUNT
;
659 priv
->gpio_chip
.can_sleep
= 1;
660 ret
= gpiochip_add_data(&priv
->gpio_chip
, priv
);
662 goto err_regmap_1_regmap_exit
;
664 dev_dbg(&client
->dev
, "gpio_chip.base=%d\n",
665 priv
->gpio_chip
.base
);
667 *gpio_chip_base
= priv
->gpio_chip
.base
;
670 * Use static GPIO configuration if GPIOLIB is undefined.
671 * This is fallback condition.
674 gpio
[0] = (*gpio_chip_base
>> 0) & 0x07;
675 gpio
[1] = (*gpio_chip_base
>> 3) & 0x07;
677 ret
= cxd2820r_gpio(&priv
->fe
, gpio
);
679 goto err_regmap_1_regmap_exit
;
683 /* Create dvb frontend */
684 memcpy(&priv
->fe
.ops
, &cxd2820r_ops
, sizeof(priv
->fe
.ops
));
685 if (!pdata
->attach_in_use
)
686 priv
->fe
.ops
.release
= NULL
;
687 i2c_set_clientdata(client
, priv
);
689 /* Setup callbacks */
690 pdata
->get_dvb_frontend
= cxd2820r_get_dvb_frontend
;
692 dev_info(&client
->dev
, "Sony CXD2820R successfully identified\n");
695 err_regmap_1_regmap_exit
:
696 regmap_exit(priv
->regmap
[1]);
697 err_client_1_i2c_unregister_device
:
698 i2c_unregister_device(priv
->client
[1]);
699 err_regmap_0_regmap_exit
:
700 regmap_exit(priv
->regmap
[0]);
704 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
708 static int cxd2820r_remove(struct i2c_client
*client
)
710 struct cxd2820r_priv
*priv
= i2c_get_clientdata(client
);
712 dev_dbg(&client
->dev
, "\n");
714 #ifdef CONFIG_GPIOLIB
715 if (priv
->gpio_chip
.label
)
716 gpiochip_remove(&priv
->gpio_chip
);
718 regmap_exit(priv
->regmap
[1]);
719 i2c_unregister_device(priv
->client
[1]);
721 regmap_exit(priv
->regmap
[0]);
728 static const struct i2c_device_id cxd2820r_id_table
[] = {
732 MODULE_DEVICE_TABLE(i2c
, cxd2820r_id_table
);
734 static struct i2c_driver cxd2820r_driver
= {
737 .suppress_bind_attrs
= true,
739 .probe
= cxd2820r_probe
,
740 .remove
= cxd2820r_remove
,
741 .id_table
= cxd2820r_id_table
,
744 module_i2c_driver(cxd2820r_driver
);
746 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
747 MODULE_DESCRIPTION("Sony CXD2820R demodulator driver");
748 MODULE_LICENSE("GPL");