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 /* Max transfer size done by I2C transfer functions */
25 #define MAX_XFER_SIZE 64
27 /* write multiple registers */
28 static int cxd2820r_wr_regs_i2c(struct cxd2820r_priv
*priv
, u8 i2c
, u8 reg
,
32 u8 buf
[MAX_XFER_SIZE
];
33 struct i2c_msg msg
[1] = {
42 if (1 + len
> sizeof(buf
)) {
43 dev_warn(&priv
->i2c
->dev
,
44 "%s: i2c wr reg=%04x: len=%d is too big!\n",
45 KBUILD_MODNAME
, reg
, len
);
50 memcpy(&buf
[1], val
, len
);
52 ret
= i2c_transfer(priv
->i2c
, msg
, 1);
56 dev_warn(&priv
->i2c
->dev
, "%s: i2c wr failed=%d reg=%02x " \
57 "len=%d\n", KBUILD_MODNAME
, ret
, reg
, len
);
63 /* read multiple registers */
64 static int cxd2820r_rd_regs_i2c(struct cxd2820r_priv
*priv
, u8 i2c
, u8 reg
,
68 u8 buf
[MAX_XFER_SIZE
];
69 struct i2c_msg msg
[2] = {
83 if (len
> sizeof(buf
)) {
84 dev_warn(&priv
->i2c
->dev
,
85 "%s: i2c wr reg=%04x: len=%d is too big!\n",
86 KBUILD_MODNAME
, reg
, len
);
90 ret
= i2c_transfer(priv
->i2c
, msg
, 2);
92 memcpy(val
, buf
, len
);
95 dev_warn(&priv
->i2c
->dev
, "%s: i2c rd failed=%d reg=%02x " \
96 "len=%d\n", KBUILD_MODNAME
, ret
, reg
, len
);
103 /* write multiple registers */
104 int cxd2820r_wr_regs(struct cxd2820r_priv
*priv
, u32 reginfo
, u8
*val
,
109 u8 reg
= (reginfo
>> 0) & 0xff;
110 u8 bank
= (reginfo
>> 8) & 0xff;
111 u8 i2c
= (reginfo
>> 16) & 0x01;
115 i2c_addr
= priv
->cfg
.i2c_address
| (1 << 1); /* DVB-C */
117 i2c_addr
= priv
->cfg
.i2c_address
; /* DVB-T/T2 */
119 /* switch bank if needed */
120 if (bank
!= priv
->bank
[i2c
]) {
121 ret
= cxd2820r_wr_regs_i2c(priv
, i2c_addr
, 0x00, &bank
, 1);
124 priv
->bank
[i2c
] = bank
;
126 return cxd2820r_wr_regs_i2c(priv
, i2c_addr
, reg
, val
, len
);
129 /* read multiple registers */
130 int cxd2820r_rd_regs(struct cxd2820r_priv
*priv
, u32 reginfo
, u8
*val
,
135 u8 reg
= (reginfo
>> 0) & 0xff;
136 u8 bank
= (reginfo
>> 8) & 0xff;
137 u8 i2c
= (reginfo
>> 16) & 0x01;
141 i2c_addr
= priv
->cfg
.i2c_address
| (1 << 1); /* DVB-C */
143 i2c_addr
= priv
->cfg
.i2c_address
; /* DVB-T/T2 */
145 /* switch bank if needed */
146 if (bank
!= priv
->bank
[i2c
]) {
147 ret
= cxd2820r_wr_regs_i2c(priv
, i2c_addr
, 0x00, &bank
, 1);
150 priv
->bank
[i2c
] = bank
;
152 return cxd2820r_rd_regs_i2c(priv
, i2c_addr
, reg
, val
, len
);
155 /* write single register */
156 int cxd2820r_wr_reg(struct cxd2820r_priv
*priv
, u32 reg
, u8 val
)
158 return cxd2820r_wr_regs(priv
, reg
, &val
, 1);
161 /* read single register */
162 int cxd2820r_rd_reg(struct cxd2820r_priv
*priv
, u32 reg
, u8
*val
)
164 return cxd2820r_rd_regs(priv
, reg
, val
, 1);
167 /* write single register with mask */
168 int cxd2820r_wr_reg_mask(struct cxd2820r_priv
*priv
, u32 reg
, u8 val
,
174 /* no need for read if whole reg is written */
176 ret
= cxd2820r_rd_reg(priv
, reg
, &tmp
);
185 return cxd2820r_wr_reg(priv
, reg
, val
);
188 int cxd2820r_gpio(struct dvb_frontend
*fe
, u8
*gpio
)
190 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
194 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
195 fe
->dtv_property_cache
.delivery_system
);
197 /* update GPIOs only when needed */
198 if (!memcmp(gpio
, priv
->gpio
, sizeof(priv
->gpio
)))
203 for (i
= 0; i
< sizeof(priv
->gpio
); i
++) {
204 /* enable / disable */
205 if (gpio
[i
] & CXD2820R_GPIO_E
)
206 tmp0
|= (2 << 6) >> (2 * i
);
208 tmp0
|= (1 << 6) >> (2 * i
);
211 if (gpio
[i
] & CXD2820R_GPIO_I
)
212 tmp1
|= (1 << (3 + i
));
214 tmp1
|= (0 << (3 + i
));
217 if (gpio
[i
] & CXD2820R_GPIO_H
)
218 tmp1
|= (1 << (0 + i
));
220 tmp1
|= (0 << (0 + i
));
222 dev_dbg(&priv
->i2c
->dev
, "%s: gpio i=%d %02x %02x\n", __func__
,
226 dev_dbg(&priv
->i2c
->dev
, "%s: wr gpio=%02x %02x\n", __func__
, tmp0
,
229 /* write bits [7:2] */
230 ret
= cxd2820r_wr_reg_mask(priv
, 0x00089, tmp0
, 0xfc);
234 /* write bits [5:0] */
235 ret
= cxd2820r_wr_reg_mask(priv
, 0x0008e, tmp1
, 0x3f);
239 memcpy(priv
->gpio
, gpio
, sizeof(priv
->gpio
));
243 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
247 static int cxd2820r_set_frontend(struct dvb_frontend
*fe
)
249 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
250 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
253 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
254 fe
->dtv_property_cache
.delivery_system
);
256 switch (c
->delivery_system
) {
258 ret
= cxd2820r_init_t(fe
);
261 ret
= cxd2820r_set_frontend_t(fe
);
266 ret
= cxd2820r_init_t(fe
);
269 ret
= cxd2820r_set_frontend_t2(fe
);
273 case SYS_DVBC_ANNEX_A
:
274 ret
= cxd2820r_init_c(fe
);
277 ret
= cxd2820r_set_frontend_c(fe
);
282 dev_dbg(&priv
->i2c
->dev
, "%s: error state=%d\n", __func__
,
283 fe
->dtv_property_cache
.delivery_system
);
291 static int cxd2820r_read_status(struct dvb_frontend
*fe
, enum fe_status
*status
)
293 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
296 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
297 fe
->dtv_property_cache
.delivery_system
);
299 switch (fe
->dtv_property_cache
.delivery_system
) {
301 ret
= cxd2820r_read_status_t(fe
, status
);
304 ret
= cxd2820r_read_status_t2(fe
, status
);
306 case SYS_DVBC_ANNEX_A
:
307 ret
= cxd2820r_read_status_c(fe
, status
);
316 static int cxd2820r_get_frontend(struct dvb_frontend
*fe
,
317 struct dtv_frontend_properties
*p
)
319 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
322 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
323 fe
->dtv_property_cache
.delivery_system
);
325 if (priv
->delivery_system
== SYS_UNDEFINED
)
328 switch (fe
->dtv_property_cache
.delivery_system
) {
330 ret
= cxd2820r_get_frontend_t(fe
, p
);
333 ret
= cxd2820r_get_frontend_t2(fe
, p
);
335 case SYS_DVBC_ANNEX_A
:
336 ret
= cxd2820r_get_frontend_c(fe
, p
);
345 static int cxd2820r_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
347 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
350 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
351 fe
->dtv_property_cache
.delivery_system
);
353 switch (fe
->dtv_property_cache
.delivery_system
) {
355 ret
= cxd2820r_read_ber_t(fe
, ber
);
358 ret
= cxd2820r_read_ber_t2(fe
, ber
);
360 case SYS_DVBC_ANNEX_A
:
361 ret
= cxd2820r_read_ber_c(fe
, ber
);
370 static int cxd2820r_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
372 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
375 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
376 fe
->dtv_property_cache
.delivery_system
);
378 switch (fe
->dtv_property_cache
.delivery_system
) {
380 ret
= cxd2820r_read_signal_strength_t(fe
, strength
);
383 ret
= cxd2820r_read_signal_strength_t2(fe
, strength
);
385 case SYS_DVBC_ANNEX_A
:
386 ret
= cxd2820r_read_signal_strength_c(fe
, strength
);
395 static int cxd2820r_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
397 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
400 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
401 fe
->dtv_property_cache
.delivery_system
);
403 switch (fe
->dtv_property_cache
.delivery_system
) {
405 ret
= cxd2820r_read_snr_t(fe
, snr
);
408 ret
= cxd2820r_read_snr_t2(fe
, snr
);
410 case SYS_DVBC_ANNEX_A
:
411 ret
= cxd2820r_read_snr_c(fe
, snr
);
420 static int cxd2820r_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
422 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
425 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
426 fe
->dtv_property_cache
.delivery_system
);
428 switch (fe
->dtv_property_cache
.delivery_system
) {
430 ret
= cxd2820r_read_ucblocks_t(fe
, ucblocks
);
433 ret
= cxd2820r_read_ucblocks_t2(fe
, ucblocks
);
435 case SYS_DVBC_ANNEX_A
:
436 ret
= cxd2820r_read_ucblocks_c(fe
, ucblocks
);
445 static int cxd2820r_init(struct dvb_frontend
*fe
)
450 static int cxd2820r_sleep(struct dvb_frontend
*fe
)
452 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
455 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
456 fe
->dtv_property_cache
.delivery_system
);
458 switch (fe
->dtv_property_cache
.delivery_system
) {
460 ret
= cxd2820r_sleep_t(fe
);
463 ret
= cxd2820r_sleep_t2(fe
);
465 case SYS_DVBC_ANNEX_A
:
466 ret
= cxd2820r_sleep_c(fe
);
475 static int cxd2820r_get_tune_settings(struct dvb_frontend
*fe
,
476 struct dvb_frontend_tune_settings
*s
)
478 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
481 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
482 fe
->dtv_property_cache
.delivery_system
);
484 switch (fe
->dtv_property_cache
.delivery_system
) {
486 ret
= cxd2820r_get_tune_settings_t(fe
, s
);
489 ret
= cxd2820r_get_tune_settings_t2(fe
, s
);
491 case SYS_DVBC_ANNEX_A
:
492 ret
= cxd2820r_get_tune_settings_c(fe
, s
);
501 static enum dvbfe_search
cxd2820r_search(struct dvb_frontend
*fe
)
503 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
504 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
506 enum fe_status status
= 0;
508 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
509 fe
->dtv_property_cache
.delivery_system
);
511 /* switch between DVB-T and DVB-T2 when tune fails */
512 if (priv
->last_tune_failed
) {
513 if (priv
->delivery_system
== SYS_DVBT
) {
514 ret
= cxd2820r_sleep_t(fe
);
518 c
->delivery_system
= SYS_DVBT2
;
519 } else if (priv
->delivery_system
== SYS_DVBT2
) {
520 ret
= cxd2820r_sleep_t2(fe
);
524 c
->delivery_system
= SYS_DVBT
;
529 ret
= cxd2820r_set_frontend(fe
);
534 /* frontend lock wait loop count */
535 switch (priv
->delivery_system
) {
537 case SYS_DVBC_ANNEX_A
:
549 /* wait frontend lock */
551 dev_dbg(&priv
->i2c
->dev
, "%s: loop=%d\n", __func__
, i
);
553 ret
= cxd2820r_read_status(fe
, &status
);
557 if (status
& FE_HAS_LOCK
)
561 /* check if we have a valid signal */
562 if (status
& FE_HAS_LOCK
) {
563 priv
->last_tune_failed
= false;
564 return DVBFE_ALGO_SEARCH_SUCCESS
;
566 priv
->last_tune_failed
= true;
567 return DVBFE_ALGO_SEARCH_AGAIN
;
571 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
572 return DVBFE_ALGO_SEARCH_ERROR
;
575 static int cxd2820r_get_frontend_algo(struct dvb_frontend
*fe
)
577 return DVBFE_ALGO_CUSTOM
;
580 static void cxd2820r_release(struct dvb_frontend
*fe
)
582 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
584 dev_dbg(&priv
->i2c
->dev
, "%s\n", __func__
);
586 #ifdef CONFIG_GPIOLIB
588 if (priv
->gpio_chip
.label
)
589 gpiochip_remove(&priv
->gpio_chip
);
596 static int cxd2820r_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
598 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
600 dev_dbg(&priv
->i2c
->dev
, "%s: %d\n", __func__
, enable
);
602 /* Bit 0 of reg 0xdb in bank 0x00 controls I2C repeater */
603 return cxd2820r_wr_reg_mask(priv
, 0xdb, enable
? 1 : 0, 0x1);
606 #ifdef CONFIG_GPIOLIB
607 static int cxd2820r_gpio_direction_output(struct gpio_chip
*chip
, unsigned nr
,
610 struct cxd2820r_priv
*priv
= gpiochip_get_data(chip
);
613 dev_dbg(&priv
->i2c
->dev
, "%s: nr=%d val=%d\n", __func__
, nr
, val
);
615 memcpy(gpio
, priv
->gpio
, sizeof(gpio
));
616 gpio
[nr
] = CXD2820R_GPIO_E
| CXD2820R_GPIO_O
| (val
<< 2);
618 return cxd2820r_gpio(&priv
->fe
, gpio
);
621 static void cxd2820r_gpio_set(struct gpio_chip
*chip
, unsigned nr
, int val
)
623 struct cxd2820r_priv
*priv
= gpiochip_get_data(chip
);
626 dev_dbg(&priv
->i2c
->dev
, "%s: nr=%d val=%d\n", __func__
, nr
, val
);
628 memcpy(gpio
, priv
->gpio
, sizeof(gpio
));
629 gpio
[nr
] = CXD2820R_GPIO_E
| CXD2820R_GPIO_O
| (val
<< 2);
631 (void) cxd2820r_gpio(&priv
->fe
, gpio
);
636 static int cxd2820r_gpio_get(struct gpio_chip
*chip
, unsigned nr
)
638 struct cxd2820r_priv
*priv
= gpiochip_get_data(chip
);
640 dev_dbg(&priv
->i2c
->dev
, "%s: nr=%d\n", __func__
, nr
);
642 return (priv
->gpio
[nr
] >> 2) & 0x01;
646 static const struct dvb_frontend_ops cxd2820r_ops
= {
647 .delsys
= { SYS_DVBT
, SYS_DVBT2
, SYS_DVBC_ANNEX_A
},
648 /* default: DVB-T/T2 */
650 .name
= "Sony CXD2820R",
652 .caps
= FE_CAN_FEC_1_2
|
665 FE_CAN_TRANSMISSION_MODE_AUTO
|
666 FE_CAN_GUARD_INTERVAL_AUTO
|
667 FE_CAN_HIERARCHY_AUTO
|
669 FE_CAN_2G_MODULATION
|
673 .release
= cxd2820r_release
,
674 .init
= cxd2820r_init
,
675 .sleep
= cxd2820r_sleep
,
677 .get_tune_settings
= cxd2820r_get_tune_settings
,
678 .i2c_gate_ctrl
= cxd2820r_i2c_gate_ctrl
,
680 .get_frontend
= cxd2820r_get_frontend
,
682 .get_frontend_algo
= cxd2820r_get_frontend_algo
,
683 .search
= cxd2820r_search
,
685 .read_status
= cxd2820r_read_status
,
686 .read_snr
= cxd2820r_read_snr
,
687 .read_ber
= cxd2820r_read_ber
,
688 .read_ucblocks
= cxd2820r_read_ucblocks
,
689 .read_signal_strength
= cxd2820r_read_signal_strength
,
692 struct dvb_frontend
*cxd2820r_attach(const struct cxd2820r_config
*cfg
,
693 struct i2c_adapter
*i2c
, int *gpio_chip_base
696 struct cxd2820r_priv
*priv
;
700 priv
= kzalloc(sizeof(struct cxd2820r_priv
), GFP_KERNEL
);
703 dev_err(&i2c
->dev
, "%s: kzalloc() failed\n",
709 memcpy(&priv
->cfg
, cfg
, sizeof(struct cxd2820r_config
));
710 memcpy(&priv
->fe
.ops
, &cxd2820r_ops
, sizeof(struct dvb_frontend_ops
));
711 priv
->fe
.demodulator_priv
= priv
;
713 priv
->bank
[0] = priv
->bank
[1] = 0xff;
714 ret
= cxd2820r_rd_reg(priv
, 0x000fd, &tmp
);
715 dev_dbg(&priv
->i2c
->dev
, "%s: chip id=%02x\n", __func__
, tmp
);
716 if (ret
|| tmp
!= 0xe1)
719 if (gpio_chip_base
) {
720 #ifdef CONFIG_GPIOLIB
722 priv
->gpio_chip
.label
= KBUILD_MODNAME
;
723 priv
->gpio_chip
.parent
= &priv
->i2c
->dev
;
724 priv
->gpio_chip
.owner
= THIS_MODULE
;
725 priv
->gpio_chip
.direction_output
=
726 cxd2820r_gpio_direction_output
;
727 priv
->gpio_chip
.set
= cxd2820r_gpio_set
;
728 priv
->gpio_chip
.get
= cxd2820r_gpio_get
;
729 priv
->gpio_chip
.base
= -1; /* dynamic allocation */
730 priv
->gpio_chip
.ngpio
= GPIO_COUNT
;
731 priv
->gpio_chip
.can_sleep
= 1;
732 ret
= gpiochip_add_data(&priv
->gpio_chip
, priv
);
736 dev_dbg(&priv
->i2c
->dev
, "%s: gpio_chip.base=%d\n", __func__
,
737 priv
->gpio_chip
.base
);
739 *gpio_chip_base
= priv
->gpio_chip
.base
;
742 * Use static GPIO configuration if GPIOLIB is undefined.
743 * This is fallback condition.
746 gpio
[0] = (*gpio_chip_base
>> 0) & 0x07;
747 gpio
[1] = (*gpio_chip_base
>> 3) & 0x07;
749 ret
= cxd2820r_gpio(&priv
->fe
, gpio
);
757 dev_dbg(&i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
761 EXPORT_SYMBOL(cxd2820r_attach
);
763 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
764 MODULE_DESCRIPTION("Sony CXD2820R demodulator driver");
765 MODULE_LICENSE("GPL");