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 /* 64 bit div with round closest, like DIV_ROUND_CLOSEST but 64 bit */
248 u32
cxd2820r_div_u64_round_closest(u64 dividend
, u32 divisor
)
250 return div_u64(dividend
+ (divisor
/ 2), divisor
);
253 static int cxd2820r_set_frontend(struct dvb_frontend
*fe
)
255 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
256 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
259 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
260 fe
->dtv_property_cache
.delivery_system
);
262 switch (c
->delivery_system
) {
264 ret
= cxd2820r_init_t(fe
);
267 ret
= cxd2820r_set_frontend_t(fe
);
272 ret
= cxd2820r_init_t(fe
);
275 ret
= cxd2820r_set_frontend_t2(fe
);
279 case SYS_DVBC_ANNEX_A
:
280 ret
= cxd2820r_init_c(fe
);
283 ret
= cxd2820r_set_frontend_c(fe
);
288 dev_dbg(&priv
->i2c
->dev
, "%s: error state=%d\n", __func__
,
289 fe
->dtv_property_cache
.delivery_system
);
296 static int cxd2820r_read_status(struct dvb_frontend
*fe
, fe_status_t
*status
)
298 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
301 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
302 fe
->dtv_property_cache
.delivery_system
);
304 switch (fe
->dtv_property_cache
.delivery_system
) {
306 ret
= cxd2820r_read_status_t(fe
, status
);
309 ret
= cxd2820r_read_status_t2(fe
, status
);
311 case SYS_DVBC_ANNEX_A
:
312 ret
= cxd2820r_read_status_c(fe
, status
);
321 static int cxd2820r_get_frontend(struct dvb_frontend
*fe
)
323 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
326 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
327 fe
->dtv_property_cache
.delivery_system
);
329 if (priv
->delivery_system
== SYS_UNDEFINED
)
332 switch (fe
->dtv_property_cache
.delivery_system
) {
334 ret
= cxd2820r_get_frontend_t(fe
);
337 ret
= cxd2820r_get_frontend_t2(fe
);
339 case SYS_DVBC_ANNEX_A
:
340 ret
= cxd2820r_get_frontend_c(fe
);
349 static int cxd2820r_read_ber(struct dvb_frontend
*fe
, u32
*ber
)
351 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
354 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
355 fe
->dtv_property_cache
.delivery_system
);
357 switch (fe
->dtv_property_cache
.delivery_system
) {
359 ret
= cxd2820r_read_ber_t(fe
, ber
);
362 ret
= cxd2820r_read_ber_t2(fe
, ber
);
364 case SYS_DVBC_ANNEX_A
:
365 ret
= cxd2820r_read_ber_c(fe
, ber
);
374 static int cxd2820r_read_signal_strength(struct dvb_frontend
*fe
, u16
*strength
)
376 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
379 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
380 fe
->dtv_property_cache
.delivery_system
);
382 switch (fe
->dtv_property_cache
.delivery_system
) {
384 ret
= cxd2820r_read_signal_strength_t(fe
, strength
);
387 ret
= cxd2820r_read_signal_strength_t2(fe
, strength
);
389 case SYS_DVBC_ANNEX_A
:
390 ret
= cxd2820r_read_signal_strength_c(fe
, strength
);
399 static int cxd2820r_read_snr(struct dvb_frontend
*fe
, u16
*snr
)
401 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
404 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
405 fe
->dtv_property_cache
.delivery_system
);
407 switch (fe
->dtv_property_cache
.delivery_system
) {
409 ret
= cxd2820r_read_snr_t(fe
, snr
);
412 ret
= cxd2820r_read_snr_t2(fe
, snr
);
414 case SYS_DVBC_ANNEX_A
:
415 ret
= cxd2820r_read_snr_c(fe
, snr
);
424 static int cxd2820r_read_ucblocks(struct dvb_frontend
*fe
, u32
*ucblocks
)
426 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
429 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
430 fe
->dtv_property_cache
.delivery_system
);
432 switch (fe
->dtv_property_cache
.delivery_system
) {
434 ret
= cxd2820r_read_ucblocks_t(fe
, ucblocks
);
437 ret
= cxd2820r_read_ucblocks_t2(fe
, ucblocks
);
439 case SYS_DVBC_ANNEX_A
:
440 ret
= cxd2820r_read_ucblocks_c(fe
, ucblocks
);
449 static int cxd2820r_init(struct dvb_frontend
*fe
)
454 static int cxd2820r_sleep(struct dvb_frontend
*fe
)
456 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
459 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
460 fe
->dtv_property_cache
.delivery_system
);
462 switch (fe
->dtv_property_cache
.delivery_system
) {
464 ret
= cxd2820r_sleep_t(fe
);
467 ret
= cxd2820r_sleep_t2(fe
);
469 case SYS_DVBC_ANNEX_A
:
470 ret
= cxd2820r_sleep_c(fe
);
479 static int cxd2820r_get_tune_settings(struct dvb_frontend
*fe
,
480 struct dvb_frontend_tune_settings
*s
)
482 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
485 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
486 fe
->dtv_property_cache
.delivery_system
);
488 switch (fe
->dtv_property_cache
.delivery_system
) {
490 ret
= cxd2820r_get_tune_settings_t(fe
, s
);
493 ret
= cxd2820r_get_tune_settings_t2(fe
, s
);
495 case SYS_DVBC_ANNEX_A
:
496 ret
= cxd2820r_get_tune_settings_c(fe
, s
);
505 static enum dvbfe_search
cxd2820r_search(struct dvb_frontend
*fe
)
507 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
508 struct dtv_frontend_properties
*c
= &fe
->dtv_property_cache
;
510 fe_status_t status
= 0;
512 dev_dbg(&priv
->i2c
->dev
, "%s: delsys=%d\n", __func__
,
513 fe
->dtv_property_cache
.delivery_system
);
515 /* switch between DVB-T and DVB-T2 when tune fails */
516 if (priv
->last_tune_failed
) {
517 if (priv
->delivery_system
== SYS_DVBT
) {
518 ret
= cxd2820r_sleep_t(fe
);
522 c
->delivery_system
= SYS_DVBT2
;
523 } else if (priv
->delivery_system
== SYS_DVBT2
) {
524 ret
= cxd2820r_sleep_t2(fe
);
528 c
->delivery_system
= SYS_DVBT
;
533 ret
= cxd2820r_set_frontend(fe
);
538 /* frontend lock wait loop count */
539 switch (priv
->delivery_system
) {
541 case SYS_DVBC_ANNEX_A
:
553 /* wait frontend lock */
555 dev_dbg(&priv
->i2c
->dev
, "%s: loop=%d\n", __func__
, i
);
557 ret
= cxd2820r_read_status(fe
, &status
);
561 if (status
& FE_HAS_LOCK
)
565 /* check if we have a valid signal */
566 if (status
& FE_HAS_LOCK
) {
567 priv
->last_tune_failed
= 0;
568 return DVBFE_ALGO_SEARCH_SUCCESS
;
570 priv
->last_tune_failed
= 1;
571 return DVBFE_ALGO_SEARCH_AGAIN
;
575 dev_dbg(&priv
->i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
576 return DVBFE_ALGO_SEARCH_ERROR
;
579 static int cxd2820r_get_frontend_algo(struct dvb_frontend
*fe
)
581 return DVBFE_ALGO_CUSTOM
;
584 static void cxd2820r_release(struct dvb_frontend
*fe
)
586 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
587 int uninitialized_var(ret
); /* silence compiler warning */
589 dev_dbg(&priv
->i2c
->dev
, "%s\n", __func__
);
591 #ifdef CONFIG_GPIOLIB
593 if (priv
->gpio_chip
.label
) {
594 ret
= gpiochip_remove(&priv
->gpio_chip
);
596 dev_err(&priv
->i2c
->dev
, "%s: gpiochip_remove() " \
597 "failed=%d\n", KBUILD_MODNAME
, ret
);
604 static int cxd2820r_i2c_gate_ctrl(struct dvb_frontend
*fe
, int enable
)
606 struct cxd2820r_priv
*priv
= fe
->demodulator_priv
;
608 dev_dbg(&priv
->i2c
->dev
, "%s: %d\n", __func__
, enable
);
610 /* Bit 0 of reg 0xdb in bank 0x00 controls I2C repeater */
611 return cxd2820r_wr_reg_mask(priv
, 0xdb, enable
? 1 : 0, 0x1);
614 #ifdef CONFIG_GPIOLIB
615 static int cxd2820r_gpio_direction_output(struct gpio_chip
*chip
, unsigned nr
,
618 struct cxd2820r_priv
*priv
=
619 container_of(chip
, struct cxd2820r_priv
, gpio_chip
);
622 dev_dbg(&priv
->i2c
->dev
, "%s: nr=%d val=%d\n", __func__
, nr
, val
);
624 memcpy(gpio
, priv
->gpio
, sizeof(gpio
));
625 gpio
[nr
] = CXD2820R_GPIO_E
| CXD2820R_GPIO_O
| (val
<< 2);
627 return cxd2820r_gpio(&priv
->fe
, gpio
);
630 static void cxd2820r_gpio_set(struct gpio_chip
*chip
, unsigned nr
, int val
)
632 struct cxd2820r_priv
*priv
=
633 container_of(chip
, struct cxd2820r_priv
, gpio_chip
);
636 dev_dbg(&priv
->i2c
->dev
, "%s: nr=%d val=%d\n", __func__
, nr
, val
);
638 memcpy(gpio
, priv
->gpio
, sizeof(gpio
));
639 gpio
[nr
] = CXD2820R_GPIO_E
| CXD2820R_GPIO_O
| (val
<< 2);
641 (void) cxd2820r_gpio(&priv
->fe
, gpio
);
646 static int cxd2820r_gpio_get(struct gpio_chip
*chip
, unsigned nr
)
648 struct cxd2820r_priv
*priv
=
649 container_of(chip
, struct cxd2820r_priv
, gpio_chip
);
651 dev_dbg(&priv
->i2c
->dev
, "%s: nr=%d\n", __func__
, nr
);
653 return (priv
->gpio
[nr
] >> 2) & 0x01;
657 static const struct dvb_frontend_ops cxd2820r_ops
= {
658 .delsys
= { SYS_DVBT
, SYS_DVBT2
, SYS_DVBC_ANNEX_A
},
659 /* default: DVB-T/T2 */
661 .name
= "Sony CXD2820R",
663 .caps
= FE_CAN_FEC_1_2
|
676 FE_CAN_TRANSMISSION_MODE_AUTO
|
677 FE_CAN_GUARD_INTERVAL_AUTO
|
678 FE_CAN_HIERARCHY_AUTO
|
680 FE_CAN_2G_MODULATION
|
684 .release
= cxd2820r_release
,
685 .init
= cxd2820r_init
,
686 .sleep
= cxd2820r_sleep
,
688 .get_tune_settings
= cxd2820r_get_tune_settings
,
689 .i2c_gate_ctrl
= cxd2820r_i2c_gate_ctrl
,
691 .get_frontend
= cxd2820r_get_frontend
,
693 .get_frontend_algo
= cxd2820r_get_frontend_algo
,
694 .search
= cxd2820r_search
,
696 .read_status
= cxd2820r_read_status
,
697 .read_snr
= cxd2820r_read_snr
,
698 .read_ber
= cxd2820r_read_ber
,
699 .read_ucblocks
= cxd2820r_read_ucblocks
,
700 .read_signal_strength
= cxd2820r_read_signal_strength
,
703 struct dvb_frontend
*cxd2820r_attach(const struct cxd2820r_config
*cfg
,
704 struct i2c_adapter
*i2c
, int *gpio_chip_base
707 struct cxd2820r_priv
*priv
;
711 priv
= kzalloc(sizeof(struct cxd2820r_priv
), GFP_KERNEL
);
714 dev_err(&i2c
->dev
, "%s: kzalloc() failed\n",
720 memcpy(&priv
->cfg
, cfg
, sizeof(struct cxd2820r_config
));
721 memcpy(&priv
->fe
.ops
, &cxd2820r_ops
, sizeof(struct dvb_frontend_ops
));
722 priv
->fe
.demodulator_priv
= priv
;
724 priv
->bank
[0] = priv
->bank
[1] = 0xff;
725 ret
= cxd2820r_rd_reg(priv
, 0x000fd, &tmp
);
726 dev_dbg(&priv
->i2c
->dev
, "%s: chip id=%02x\n", __func__
, tmp
);
727 if (ret
|| tmp
!= 0xe1)
730 if (gpio_chip_base
) {
731 #ifdef CONFIG_GPIOLIB
733 priv
->gpio_chip
.label
= KBUILD_MODNAME
;
734 priv
->gpio_chip
.dev
= &priv
->i2c
->dev
;
735 priv
->gpio_chip
.owner
= THIS_MODULE
;
736 priv
->gpio_chip
.direction_output
=
737 cxd2820r_gpio_direction_output
;
738 priv
->gpio_chip
.set
= cxd2820r_gpio_set
;
739 priv
->gpio_chip
.get
= cxd2820r_gpio_get
;
740 priv
->gpio_chip
.base
= -1; /* dynamic allocation */
741 priv
->gpio_chip
.ngpio
= GPIO_COUNT
;
742 priv
->gpio_chip
.can_sleep
= 1;
743 ret
= gpiochip_add(&priv
->gpio_chip
);
747 dev_dbg(&priv
->i2c
->dev
, "%s: gpio_chip.base=%d\n", __func__
,
748 priv
->gpio_chip
.base
);
750 *gpio_chip_base
= priv
->gpio_chip
.base
;
753 * Use static GPIO configuration if GPIOLIB is undefined.
754 * This is fallback condition.
757 gpio
[0] = (*gpio_chip_base
>> 0) & 0x07;
758 gpio
[1] = (*gpio_chip_base
>> 3) & 0x07;
760 ret
= cxd2820r_gpio(&priv
->fe
, gpio
);
768 dev_dbg(&i2c
->dev
, "%s: failed=%d\n", __func__
, ret
);
772 EXPORT_SYMBOL(cxd2820r_attach
);
774 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
775 MODULE_DESCRIPTION("Sony CXD2820R demodulator driver");
776 MODULE_LICENSE("GPL");