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
)
318 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
321 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
322 fe
->dtv_property_cache
.delivery_system
);
324 if (priv
->delivery_system
== SYS_UNDEFINED
)
327 switch (fe
->dtv_property_cache
.delivery_system
) {
329 ret
= cxd2820r_get_frontend_t(fe
);
332 ret
= cxd2820r_get_frontend_t2(fe
);
334 case SYS_DVBC_ANNEX_A
:
335 ret
= cxd2820r_get_frontend_c(fe
);
344 static int cxd2820r_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
346 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
349 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
350 fe
->dtv_property_cache
.delivery_system
);
352 switch (fe
->dtv_property_cache
.delivery_system
) {
354 ret
= cxd2820r_read_ber_t(fe
, ber
);
357 ret
= cxd2820r_read_ber_t2(fe
, ber
);
359 case SYS_DVBC_ANNEX_A
:
360 ret
= cxd2820r_read_ber_c(fe
, ber
);
369 static int cxd2820r_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
371 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
374 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
375 fe
->dtv_property_cache
.delivery_system
);
377 switch (fe
->dtv_property_cache
.delivery_system
) {
379 ret
= cxd2820r_read_signal_strength_t(fe
, strength
);
382 ret
= cxd2820r_read_signal_strength_t2(fe
, strength
);
384 case SYS_DVBC_ANNEX_A
:
385 ret
= cxd2820r_read_signal_strength_c(fe
, strength
);
394 static int cxd2820r_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
396 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
399 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
400 fe
->dtv_property_cache
.delivery_system
);
402 switch (fe
->dtv_property_cache
.delivery_system
) {
404 ret
= cxd2820r_read_snr_t(fe
, snr
);
407 ret
= cxd2820r_read_snr_t2(fe
, snr
);
409 case SYS_DVBC_ANNEX_A
:
410 ret
= cxd2820r_read_snr_c(fe
, snr
);
419 static int cxd2820r_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
421 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
424 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
425 fe
->dtv_property_cache
.delivery_system
);
427 switch (fe
->dtv_property_cache
.delivery_system
) {
429 ret
= cxd2820r_read_ucblocks_t(fe
, ucblocks
);
432 ret
= cxd2820r_read_ucblocks_t2(fe
, ucblocks
);
434 case SYS_DVBC_ANNEX_A
:
435 ret
= cxd2820r_read_ucblocks_c(fe
, ucblocks
);
444 static int cxd2820r_init(struct dvb_frontend
*fe
)
449 static int cxd2820r_sleep(struct dvb_frontend
*fe
)
451 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
454 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
455 fe
->dtv_property_cache
.delivery_system
);
457 switch (fe
->dtv_property_cache
.delivery_system
) {
459 ret
= cxd2820r_sleep_t(fe
);
462 ret
= cxd2820r_sleep_t2(fe
);
464 case SYS_DVBC_ANNEX_A
:
465 ret
= cxd2820r_sleep_c(fe
);
474 static int cxd2820r_get_tune_settings(struct dvb_frontend
*fe
,
475 struct dvb_frontend_tune_settings
*s
)
477 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
480 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
481 fe
->dtv_property_cache
.delivery_system
);
483 switch (fe
->dtv_property_cache
.delivery_system
) {
485 ret
= cxd2820r_get_tune_settings_t(fe
, s
);
488 ret
= cxd2820r_get_tune_settings_t2(fe
, s
);
490 case SYS_DVBC_ANNEX_A
:
491 ret
= cxd2820r_get_tune_settings_c(fe
, s
);
500 static enum dvbfe_search
cxd2820r_search(struct dvb_frontend
*fe
)
502 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
503 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
505 enum fe_status status
= 0;
507 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
508 fe
->dtv_property_cache
.delivery_system
);
510 /* switch between DVB-T and DVB-T2 when tune fails */
511 if (priv
->last_tune_failed
) {
512 if (priv
->delivery_system
== SYS_DVBT
) {
513 ret
= cxd2820r_sleep_t(fe
);
517 c
->delivery_system
= SYS_DVBT2
;
518 } else if (priv
->delivery_system
== SYS_DVBT2
) {
519 ret
= cxd2820r_sleep_t2(fe
);
523 c
->delivery_system
= SYS_DVBT
;
528 ret
= cxd2820r_set_frontend(fe
);
533 /* frontend lock wait loop count */
534 switch (priv
->delivery_system
) {
536 case SYS_DVBC_ANNEX_A
:
548 /* wait frontend lock */
550 dev_dbg(&priv
->i2c
->dev
, "%s: loop=%d\n", __func__
, i
);
552 ret
= cxd2820r_read_status(fe
, &status
);
556 if (status
& FE_HAS_LOCK
)
560 /* check if we have a valid signal */
561 if (status
& FE_HAS_LOCK
) {
562 priv
->last_tune_failed
= false;
563 return DVBFE_ALGO_SEARCH_SUCCESS
;
565 priv
->last_tune_failed
= true;
566 return DVBFE_ALGO_SEARCH_AGAIN
;
570 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
571 return DVBFE_ALGO_SEARCH_ERROR
;
574 static int cxd2820r_get_frontend_algo(struct dvb_frontend
*fe
)
576 return DVBFE_ALGO_CUSTOM
;
579 static void cxd2820r_release(struct dvb_frontend
*fe
)
581 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
583 dev_dbg(&priv
->i2c
->dev
, "%s\n", __func__
);
585 #ifdef CONFIG_GPIOLIB
587 if (priv
->gpio_chip
.label
)
588 gpiochip_remove(&priv
->gpio_chip
);
595 static int cxd2820r_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
597 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
599 dev_dbg(&priv
->i2c
->dev
, "%s: %d\n", __func__
, enable
);
601 /* Bit 0 of reg 0xdb in bank 0x00 controls I2C repeater */
602 return cxd2820r_wr_reg_mask(priv
, 0xdb, enable
? 1 : 0, 0x1);
605 #ifdef CONFIG_GPIOLIB
606 static int cxd2820r_gpio_direction_output(struct gpio_chip
*chip
, unsigned nr
,
609 struct cxd2820r_priv
*priv
=
610 container_of(chip
, struct cxd2820r_priv
, gpio_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
=
624 container_of(chip
, struct cxd2820r_priv
, gpio_chip
);
627 dev_dbg(&priv
->i2c
->dev
, "%s: nr=%d val=%d\n", __func__
, nr
, val
);
629 memcpy(gpio
, priv
->gpio
, sizeof(gpio
));
630 gpio
[nr
] = CXD2820R_GPIO_E
| CXD2820R_GPIO_O
| (val
<< 2);
632 (void) cxd2820r_gpio(&priv
->fe
, gpio
);
637 static int cxd2820r_gpio_get(struct gpio_chip
*chip
, unsigned nr
)
639 struct cxd2820r_priv
*priv
=
640 container_of(chip
, struct cxd2820r_priv
, gpio_chip
);
642 dev_dbg(&priv
->i2c
->dev
, "%s: nr=%d\n", __func__
, nr
);
644 return (priv
->gpio
[nr
] >> 2) & 0x01;
648 static const struct dvb_frontend_ops cxd2820r_ops
= {
649 .delsys
= { SYS_DVBT
, SYS_DVBT2
, SYS_DVBC_ANNEX_A
},
650 /* default: DVB-T/T2 */
652 .name
= "Sony CXD2820R",
654 .caps
= FE_CAN_FEC_1_2
|
667 FE_CAN_TRANSMISSION_MODE_AUTO
|
668 FE_CAN_GUARD_INTERVAL_AUTO
|
669 FE_CAN_HIERARCHY_AUTO
|
671 FE_CAN_2G_MODULATION
|
675 .release
= cxd2820r_release
,
676 .init
= cxd2820r_init
,
677 .sleep
= cxd2820r_sleep
,
679 .get_tune_settings
= cxd2820r_get_tune_settings
,
680 .i2c_gate_ctrl
= cxd2820r_i2c_gate_ctrl
,
682 .get_frontend
= cxd2820r_get_frontend
,
684 .get_frontend_algo
= cxd2820r_get_frontend_algo
,
685 .search
= cxd2820r_search
,
687 .read_status
= cxd2820r_read_status
,
688 .read_snr
= cxd2820r_read_snr
,
689 .read_ber
= cxd2820r_read_ber
,
690 .read_ucblocks
= cxd2820r_read_ucblocks
,
691 .read_signal_strength
= cxd2820r_read_signal_strength
,
694 struct dvb_frontend
*cxd2820r_attach(const struct cxd2820r_config
*cfg
,
695 struct i2c_adapter
*i2c
, int *gpio_chip_base
698 struct cxd2820r_priv
*priv
;
702 priv
= kzalloc(sizeof(struct cxd2820r_priv
), GFP_KERNEL
);
705 dev_err(&i2c
->dev
, "%s: kzalloc() failed\n",
711 memcpy(&priv
->cfg
, cfg
, sizeof(struct cxd2820r_config
));
712 memcpy(&priv
->fe
.ops
, &cxd2820r_ops
, sizeof(struct dvb_frontend_ops
));
713 priv
->fe
.demodulator_priv
= priv
;
715 priv
->bank
[0] = priv
->bank
[1] = 0xff;
716 ret
= cxd2820r_rd_reg(priv
, 0x000fd, &tmp
);
717 dev_dbg(&priv
->i2c
->dev
, "%s: chip id=%02x\n", __func__
, tmp
);
718 if (ret
|| tmp
!= 0xe1)
721 if (gpio_chip_base
) {
722 #ifdef CONFIG_GPIOLIB
724 priv
->gpio_chip
.label
= KBUILD_MODNAME
;
725 priv
->gpio_chip
.dev
= &priv
->i2c
->dev
;
726 priv
->gpio_chip
.owner
= THIS_MODULE
;
727 priv
->gpio_chip
.direction_output
=
728 cxd2820r_gpio_direction_output
;
729 priv
->gpio_chip
.set
= cxd2820r_gpio_set
;
730 priv
->gpio_chip
.get
= cxd2820r_gpio_get
;
731 priv
->gpio_chip
.base
= -1; /* dynamic allocation */
732 priv
->gpio_chip
.ngpio
= GPIO_COUNT
;
733 priv
->gpio_chip
.can_sleep
= 1;
734 ret
= gpiochip_add(&priv
->gpio_chip
);
738 dev_dbg(&priv
->i2c
->dev
, "%s: gpio_chip.base=%d\n", __func__
,
739 priv
->gpio_chip
.base
);
741 *gpio_chip_base
= priv
->gpio_chip
.base
;
744 * Use static GPIO configuration if GPIOLIB is undefined.
745 * This is fallback condition.
748 gpio
[0] = (*gpio_chip_base
>> 0) & 0x07;
749 gpio
[1] = (*gpio_chip_base
>> 3) & 0x07;
751 ret
= cxd2820r_gpio(&priv
->fe
, gpio
);
759 dev_dbg(&i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
763 EXPORT_SYMBOL(cxd2820r_attach
);
765 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
766 MODULE_DESCRIPTION("Sony CXD2820R demodulator driver");
767 MODULE_LICENSE("GPL");