2 * Sony CXD2820R demodulator driver
4 * Copyright (C) 2010 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.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "cxd2820r_priv.h"
24 /* Write register table */
25 int cxd2820r_wr_reg_val_mask_tab(struct cxd2820r_priv
*priv
,
26 const struct reg_val_mask
*tab
, int tab_len
)
28 struct i2c_client
*client
= priv
->client
[0];
30 unsigned int i
, reg
, mask
, val
;
31 struct regmap
*regmap
;
33 dev_dbg(&client
->dev
, "tab_len=%d\n", tab_len
);
35 for (i
= 0; i
< tab_len
; i
++) {
36 if ((tab
[i
].reg
>> 16) & 0x1)
37 regmap
= priv
->regmap
[1];
39 regmap
= priv
->regmap
[0];
41 reg
= (tab
[i
].reg
>> 0) & 0xffff;
46 ret
= regmap_write(regmap
, reg
, val
);
48 ret
= regmap_write_bits(regmap
, reg
, mask
, val
);
55 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
59 int cxd2820r_gpio(struct dvb_frontend
*fe
, u8
*gpio
)
61 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
62 struct i2c_client
*client
= priv
->client
[0];
63 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
67 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
69 /* update GPIOs only when needed */
70 if (!memcmp(gpio
, priv
->gpio
, sizeof(priv
->gpio
)))
75 for (i
= 0; i
< sizeof(priv
->gpio
); i
++) {
76 /* enable / disable */
77 if (gpio
[i
] & CXD2820R_GPIO_E
)
78 tmp0
|= (2 << 6) >> (2 * i
);
80 tmp0
|= (1 << 6) >> (2 * i
);
83 if (gpio
[i
] & CXD2820R_GPIO_I
)
84 tmp1
|= (1 << (3 + i
));
86 tmp1
|= (0 << (3 + i
));
89 if (gpio
[i
] & CXD2820R_GPIO_H
)
90 tmp1
|= (1 << (0 + i
));
92 tmp1
|= (0 << (0 + i
));
94 dev_dbg(&client
->dev
, "gpio i=%d %02x %02x\n", i
, tmp0
, tmp1
);
97 dev_dbg(&client
->dev
, "wr gpio=%02x %02x\n", tmp0
, tmp1
);
99 /* write bits [7:2] */
100 ret
= regmap_update_bits(priv
->regmap
[0], 0x0089, 0xfc, tmp0
);
104 /* write bits [5:0] */
105 ret
= regmap_update_bits(priv
->regmap
[0], 0x008e, 0x3f, tmp1
);
109 memcpy(priv
->gpio
, gpio
, sizeof(priv
->gpio
));
113 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
117 static int cxd2820r_set_frontend(struct dvb_frontend
*fe
)
119 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
120 struct i2c_client
*client
= priv
->client
[0];
121 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
124 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
126 switch (c
->delivery_system
) {
128 ret
= cxd2820r_init_t(fe
);
131 ret
= cxd2820r_set_frontend_t(fe
);
136 ret
= cxd2820r_init_t(fe
);
139 ret
= cxd2820r_set_frontend_t2(fe
);
143 case SYS_DVBC_ANNEX_A
:
144 ret
= cxd2820r_init_c(fe
);
147 ret
= cxd2820r_set_frontend_c(fe
);
152 dev_dbg(&client
->dev
, "invalid delivery_system\n");
160 static int cxd2820r_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
162 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
163 struct i2c_client
*client
= priv
->client
[0];
164 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
167 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
169 switch (c
->delivery_system
) {
171 ret
= cxd2820r_read_status_t(fe
, status
);
174 ret
= cxd2820r_read_status_t2(fe
, status
);
176 case SYS_DVBC_ANNEX_A
:
177 ret
= cxd2820r_read_status_c(fe
, status
);
186 static int cxd2820r_get_frontend(struct dvb_frontend
*fe
,
187 struct dtv_frontend_properties
*p
)
189 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
190 struct i2c_client
*client
= priv
->client
[0];
191 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
194 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
196 if (priv
->delivery_system
== SYS_UNDEFINED
)
199 switch (c
->delivery_system
) {
201 ret
= cxd2820r_get_frontend_t(fe
, p
);
204 ret
= cxd2820r_get_frontend_t2(fe
, p
);
206 case SYS_DVBC_ANNEX_A
:
207 ret
= cxd2820r_get_frontend_c(fe
, p
);
216 static int cxd2820r_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
218 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
219 struct i2c_client
*client
= priv
->client
[0];
220 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
222 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
224 *ber
= (priv
->post_bit_error
- priv
->post_bit_error_prev_dvbv3
);
225 priv
->post_bit_error_prev_dvbv3
= priv
->post_bit_error
;
230 static int cxd2820r_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
232 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
233 struct i2c_client
*client
= priv
->client
[0];
234 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
236 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
238 if (c
->strength
.stat
[0].scale
== FE_SCALE_RELATIVE
)
239 *strength
= c
->strength
.stat
[0].uvalue
;
246 static int cxd2820r_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
248 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
249 struct i2c_client
*client
= priv
->client
[0];
250 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
252 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
254 if (c
->cnr
.stat
[0].scale
== FE_SCALE_DECIBEL
)
255 *snr
= div_s64(c
->cnr
.stat
[0].svalue
, 100);
262 static int cxd2820r_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
264 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
265 struct i2c_client
*client
= priv
->client
[0];
266 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
268 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
275 static int cxd2820r_init(struct dvb_frontend
*fe
)
280 static int cxd2820r_sleep(struct dvb_frontend
*fe
)
282 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
283 struct i2c_client
*client
= priv
->client
[0];
284 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
287 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
289 switch (c
->delivery_system
) {
291 ret
= cxd2820r_sleep_t(fe
);
294 ret
= cxd2820r_sleep_t2(fe
);
296 case SYS_DVBC_ANNEX_A
:
297 ret
= cxd2820r_sleep_c(fe
);
306 static int cxd2820r_get_tune_settings(struct dvb_frontend
*fe
,
307 struct dvb_frontend_tune_settings
*s
)
309 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
310 struct i2c_client
*client
= priv
->client
[0];
311 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
314 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
316 switch (c
->delivery_system
) {
318 ret
= cxd2820r_get_tune_settings_t(fe
, s
);
321 ret
= cxd2820r_get_tune_settings_t2(fe
, s
);
323 case SYS_DVBC_ANNEX_A
:
324 ret
= cxd2820r_get_tune_settings_c(fe
, s
);
333 static enum dvbfe_search
cxd2820r_search(struct dvb_frontend
*fe
)
335 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
336 struct i2c_client
*client
= priv
->client
[0];
337 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
339 enum fe_status status
= 0;
341 dev_dbg(&client
->dev
, "delivery_system=%d\n", c
->delivery_system
);
343 /* switch between DVB-T and DVB-T2 when tune fails */
344 if (priv
->last_tune_failed
) {
345 if (priv
->delivery_system
== SYS_DVBT
) {
346 ret
= cxd2820r_sleep_t(fe
);
350 c
->delivery_system
= SYS_DVBT2
;
351 } else if (priv
->delivery_system
== SYS_DVBT2
) {
352 ret
= cxd2820r_sleep_t2(fe
);
356 c
->delivery_system
= SYS_DVBT
;
361 ret
= cxd2820r_set_frontend(fe
);
365 /* frontend lock wait loop count */
366 switch (priv
->delivery_system
) {
368 case SYS_DVBC_ANNEX_A
:
380 /* wait frontend lock */
382 dev_dbg(&client
->dev
, "loop=%d\n", i
);
384 ret
= cxd2820r_read_status(fe
, &status
);
388 if (status
& FE_HAS_LOCK
)
392 /* check if we have a valid signal */
393 if (status
& FE_HAS_LOCK
) {
394 priv
->last_tune_failed
= false;
395 return DVBFE_ALGO_SEARCH_SUCCESS
;
397 priv
->last_tune_failed
= true;
398 return DVBFE_ALGO_SEARCH_AGAIN
;
402 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
403 return DVBFE_ALGO_SEARCH_ERROR
;
406 static int cxd2820r_get_frontend_algo(struct dvb_frontend
*fe
)
408 return DVBFE_ALGO_CUSTOM
;
411 static void cxd2820r_release(struct dvb_frontend
*fe
)
413 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
414 struct i2c_client
*client
= priv
->client
[0];
416 dev_dbg(&client
->dev
, "\n");
418 i2c_unregister_device(client
);
423 static int cxd2820r_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
425 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
426 struct i2c_client
*client
= priv
->client
[0];
428 dev_dbg_ratelimited(&client
->dev
, "enable=%d\n", enable
);
430 return regmap_update_bits(priv
->regmap
[0], 0x00db, 0x01, enable
? 1 : 0);
433 #ifdef CONFIG_GPIOLIB
434 static int cxd2820r_gpio_direction_output(struct gpio_chip
*chip
, unsigned nr
,
437 struct cxd2820r_priv
*priv
= gpiochip_get_data(chip
);
438 struct i2c_client
*client
= priv
->client
[0];
441 dev_dbg(&client
->dev
, "nr=%u val=%d\n", nr
, val
);
443 memcpy(gpio
, priv
->gpio
, sizeof(gpio
));
444 gpio
[nr
] = CXD2820R_GPIO_E
| CXD2820R_GPIO_O
| (val
<< 2);
446 return cxd2820r_gpio(&priv
->fe
, gpio
);
449 static void cxd2820r_gpio_set(struct gpio_chip
*chip
, unsigned nr
, int val
)
451 struct cxd2820r_priv
*priv
= gpiochip_get_data(chip
);
452 struct i2c_client
*client
= priv
->client
[0];
455 dev_dbg(&client
->dev
, "nr=%u val=%d\n", nr
, val
);
457 memcpy(gpio
, priv
->gpio
, sizeof(gpio
));
458 gpio
[nr
] = CXD2820R_GPIO_E
| CXD2820R_GPIO_O
| (val
<< 2);
460 (void) cxd2820r_gpio(&priv
->fe
, gpio
);
465 static int cxd2820r_gpio_get(struct gpio_chip
*chip
, unsigned nr
)
467 struct cxd2820r_priv
*priv
= gpiochip_get_data(chip
);
468 struct i2c_client
*client
= priv
->client
[0];
470 dev_dbg(&client
->dev
, "nr=%u\n", nr
);
472 return (priv
->gpio
[nr
] >> 2) & 0x01;
476 static const struct dvb_frontend_ops cxd2820r_ops
= {
477 .delsys
= { SYS_DVBT
, SYS_DVBT2
, SYS_DVBC_ANNEX_A
},
478 /* default: DVB-T/T2 */
480 .name
= "Sony CXD2820R",
482 .caps
= FE_CAN_FEC_1_2
|
495 FE_CAN_TRANSMISSION_MODE_AUTO
|
496 FE_CAN_GUARD_INTERVAL_AUTO
|
497 FE_CAN_HIERARCHY_AUTO
|
499 FE_CAN_2G_MODULATION
|
503 .release
= cxd2820r_release
,
504 .init
= cxd2820r_init
,
505 .sleep
= cxd2820r_sleep
,
507 .get_tune_settings
= cxd2820r_get_tune_settings
,
508 .i2c_gate_ctrl
= cxd2820r_i2c_gate_ctrl
,
510 .get_frontend
= cxd2820r_get_frontend
,
512 .get_frontend_algo
= cxd2820r_get_frontend_algo
,
513 .search
= cxd2820r_search
,
515 .read_status
= cxd2820r_read_status
,
516 .read_snr
= cxd2820r_read_snr
,
517 .read_ber
= cxd2820r_read_ber
,
518 .read_ucblocks
= cxd2820r_read_ucblocks
,
519 .read_signal_strength
= cxd2820r_read_signal_strength
,
523 * XXX: That is wrapper to cxd2820r_probe() via driver core in order to provide
524 * proper I2C client for legacy media attach binding.
525 * New users must use I2C client binding directly!
527 struct dvb_frontend
*cxd2820r_attach(const struct cxd2820r_config
*config
,
528 struct i2c_adapter
*adapter
,
531 struct i2c_client
*client
;
532 struct i2c_board_info board_info
;
533 struct cxd2820r_platform_data pdata
;
535 pdata
.ts_mode
= config
->ts_mode
;
536 pdata
.ts_clk_inv
= config
->ts_clock_inv
;
537 pdata
.if_agc_polarity
= config
->if_agc_polarity
;
538 pdata
.spec_inv
= config
->spec_inv
;
539 pdata
.gpio_chip_base
= &gpio_chip_base
;
540 pdata
.attach_in_use
= true;
542 memset(&board_info
, 0, sizeof(board_info
));
543 strlcpy(board_info
.type
, "cxd2820r", I2C_NAME_SIZE
);
544 board_info
.addr
= config
->i2c_address
;
545 board_info
.platform_data
= &pdata
;
546 client
= i2c_new_device(adapter
, &board_info
);
547 if (!client
|| !client
->dev
.driver
)
550 return pdata
.get_dvb_frontend(client
);
552 EXPORT_SYMBOL(cxd2820r_attach
);
554 static struct dvb_frontend
*cxd2820r_get_dvb_frontend(struct i2c_client
*client
)
556 struct cxd2820r_priv
*priv
= i2c_get_clientdata(client
);
558 dev_dbg(&client
->dev
, "\n");
563 static int cxd2820r_probe(struct i2c_client
*client
,
564 const struct i2c_device_id
*id
)
566 struct cxd2820r_platform_data
*pdata
= client
->dev
.platform_data
;
567 struct cxd2820r_priv
*priv
;
568 int ret
, *gpio_chip_base
;
570 static const struct regmap_range_cfg regmap_range_cfg0
[] = {
574 .selector_reg
= 0x00,
575 .selector_mask
= 0xff,
577 .window_start
= 0x00,
581 static const struct regmap_range_cfg regmap_range_cfg1
[] = {
585 .selector_reg
= 0x00,
586 .selector_mask
= 0xff,
588 .window_start
= 0x00,
592 static const struct regmap_config regmap_config0
= {
595 .max_register
= 0x3fff,
596 .ranges
= regmap_range_cfg0
,
597 .num_ranges
= ARRAY_SIZE(regmap_range_cfg0
),
598 .cache_type
= REGCACHE_NONE
,
600 static const struct regmap_config regmap_config1
= {
603 .max_register
= 0x01ff,
604 .ranges
= regmap_range_cfg1
,
605 .num_ranges
= ARRAY_SIZE(regmap_range_cfg1
),
606 .cache_type
= REGCACHE_NONE
,
609 dev_dbg(&client
->dev
, "\n");
611 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
617 priv
->client
[0] = client
;
618 priv
->i2c
= client
->adapter
;
619 priv
->ts_mode
= pdata
->ts_mode
;
620 priv
->ts_clk_inv
= pdata
->ts_clk_inv
;
621 priv
->if_agc_polarity
= pdata
->if_agc_polarity
;
622 priv
->spec_inv
= pdata
->spec_inv
;
623 gpio_chip_base
= *pdata
->gpio_chip_base
;
624 priv
->regmap
[0] = regmap_init_i2c(priv
->client
[0], ®map_config0
);
625 if (IS_ERR(priv
->regmap
[0])) {
626 ret
= PTR_ERR(priv
->regmap
[0]);
630 /* Check demod answers with correct chip id */
631 ret
= regmap_read(priv
->regmap
[0], 0x00fd, &utmp
);
633 goto err_regmap_0_regmap_exit
;
635 dev_dbg(&client
->dev
, "chip_id=%02x\n", utmp
);
639 goto err_regmap_0_regmap_exit
;
643 * Chip has two I2C addresses for different register banks. We register
644 * one dummy I2C client in in order to get own I2C client for each
647 priv
->client
[1] = i2c_new_dummy(client
->adapter
, client
->addr
| (1 << 1));
648 if (!priv
->client
[1]) {
650 dev_err(&client
->dev
, "I2C registration failed\n");
652 goto err_regmap_0_regmap_exit
;
655 priv
->regmap
[1] = regmap_init_i2c(priv
->client
[1], ®map_config1
);
656 if (IS_ERR(priv
->regmap
[1])) {
657 ret
= PTR_ERR(priv
->regmap
[1]);
658 goto err_client_1_i2c_unregister_device
;
661 if (gpio_chip_base
) {
662 #ifdef CONFIG_GPIOLIB
664 priv
->gpio_chip
.label
= KBUILD_MODNAME
;
665 priv
->gpio_chip
.parent
= &client
->dev
;
666 priv
->gpio_chip
.owner
= THIS_MODULE
;
667 priv
->gpio_chip
.direction_output
= cxd2820r_gpio_direction_output
;
668 priv
->gpio_chip
.set
= cxd2820r_gpio_set
;
669 priv
->gpio_chip
.get
= cxd2820r_gpio_get
;
670 priv
->gpio_chip
.base
= -1; /* Dynamic allocation */
671 priv
->gpio_chip
.ngpio
= GPIO_COUNT
;
672 priv
->gpio_chip
.can_sleep
= 1;
673 ret
= gpiochip_add_data(&priv
->gpio_chip
, priv
);
675 goto err_regmap_1_regmap_exit
;
677 dev_dbg(&client
->dev
, "gpio_chip.base=%d\n",
678 priv
->gpio_chip
.base
);
680 *gpio_chip_base
= priv
->gpio_chip
.base
;
683 * Use static GPIO configuration if GPIOLIB is undefined.
684 * This is fallback condition.
687 gpio
[0] = (*gpio_chip_base
>> 0) & 0x07;
688 gpio
[1] = (*gpio_chip_base
>> 3) & 0x07;
690 ret
= cxd2820r_gpio(&priv
->fe
, gpio
);
692 goto err_regmap_1_regmap_exit
;
696 /* Create dvb frontend */
697 memcpy(&priv
->fe
.ops
, &cxd2820r_ops
, sizeof(priv
->fe
.ops
));
698 if (!pdata
->attach_in_use
)
699 priv
->fe
.ops
.release
= NULL
;
700 priv
->fe
.demodulator_priv
= priv
;
701 i2c_set_clientdata(client
, priv
);
703 /* Setup callbacks */
704 pdata
->get_dvb_frontend
= cxd2820r_get_dvb_frontend
;
706 dev_info(&client
->dev
, "Sony CXD2820R successfully identified\n");
709 err_regmap_1_regmap_exit
:
710 regmap_exit(priv
->regmap
[1]);
711 err_client_1_i2c_unregister_device
:
712 i2c_unregister_device(priv
->client
[1]);
713 err_regmap_0_regmap_exit
:
714 regmap_exit(priv
->regmap
[0]);
718 dev_dbg(&client
->dev
, "failed=%d\n", ret
);
722 static int cxd2820r_remove(struct i2c_client
*client
)
724 struct cxd2820r_priv
*priv
= i2c_get_clientdata(client
);
726 dev_dbg(&client
->dev
, "\n");
728 #ifdef CONFIG_GPIOLIB
729 if (priv
->gpio_chip
.label
)
730 gpiochip_remove(&priv
->gpio_chip
);
732 regmap_exit(priv
->regmap
[1]);
733 i2c_unregister_device(priv
->client
[1]);
735 regmap_exit(priv
->regmap
[0]);
742 static const struct i2c_device_id cxd2820r_id_table
[] = {
746 MODULE_DEVICE_TABLE(i2c
, cxd2820r_id_table
);
748 static struct i2c_driver cxd2820r_driver
= {
751 .suppress_bind_attrs
= true,
753 .probe
= cxd2820r_probe
,
754 .remove
= cxd2820r_remove
,
755 .id_table
= cxd2820r_id_table
,
758 module_i2c_driver(cxd2820r_driver
);
760 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
761 MODULE_DESCRIPTION("Sony CXD2820R demodulator driver");
762 MODULE_LICENSE("GPL");