2 * HDIC HD29L2 DMB-TH demodulator driver
4 * Copyright (C) 2011 Metropolia University of Applied Sciences, Electria R&D
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "hd29l2_priv.h"
25 #define HD29L2_MAX_LEN (3)
27 /* write multiple registers */
28 static int hd29l2_wr_regs(struct hd29l2_priv *priv, u8 reg, u8 *val, int len)
31 u8 buf[2 + HD29L2_MAX_LEN];
32 struct i2c_msg msg[1] = {
34 .addr = priv->cfg.i2c_addr,
41 if (len > HD29L2_MAX_LEN)
45 memcpy(&buf[2], val, len);
47 ret = i2c_transfer(priv->i2c, msg, 1);
51 dev_warn(&priv->i2c->dev,
52 "%s: i2c wr failed=%d reg=%02x len=%d\n",
53 KBUILD_MODNAME, ret, reg, len);
60 /* read multiple registers */
61 static int hd29l2_rd_regs(struct hd29l2_priv *priv, u8 reg, u8 *val, int len)
64 u8 buf[2] = { 0x00, reg };
65 struct i2c_msg msg[2] = {
67 .addr = priv->cfg.i2c_addr,
72 .addr = priv->cfg.i2c_addr,
79 ret = i2c_transfer(priv->i2c, msg, 2);
83 dev_warn(&priv->i2c->dev,
84 "%s: i2c rd failed=%d reg=%02x len=%d\n",
85 KBUILD_MODNAME, ret, reg, len);
92 /* write single register */
93 static int hd29l2_wr_reg(struct hd29l2_priv *priv, u8 reg, u8 val)
95 return hd29l2_wr_regs(priv, reg, &val, 1);
98 /* read single register */
99 static int hd29l2_rd_reg(struct hd29l2_priv *priv, u8 reg, u8 *val)
101 return hd29l2_rd_regs(priv, reg, val, 1);
104 /* write single register with mask */
105 static int hd29l2_wr_reg_mask(struct hd29l2_priv *priv, u8 reg, u8 val, u8 mask)
110 /* no need for read if whole reg is written */
112 ret = hd29l2_rd_regs(priv, reg, &tmp, 1);
121 return hd29l2_wr_regs(priv, reg, &val, 1);
124 /* read single register with mask */
125 static int hd29l2_rd_reg_mask(struct hd29l2_priv *priv, u8 reg, u8 *val, u8 mask)
130 ret = hd29l2_rd_regs(priv, reg, &tmp, 1);
136 /* find position of the first bit */
137 for (i = 0; i < 8; i++) {
138 if ((mask >> i) & 0x01)
146 static int hd29l2_soft_reset(struct hd29l2_priv *priv)
151 ret = hd29l2_rd_reg(priv, 0x26, &tmp);
155 ret = hd29l2_wr_reg(priv, 0x26, 0x0d);
159 usleep_range(10000, 20000);
161 ret = hd29l2_wr_reg(priv, 0x26, tmp);
167 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
171 static int hd29l2_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
174 struct hd29l2_priv *priv = fe->demodulator_priv;
177 dev_dbg(&priv->i2c->dev, "%s: enable=%d\n", __func__, enable);
179 /* set tuner address for demod */
180 if (!priv->tuner_i2c_addr_programmed && enable) {
181 /* no need to set tuner address every time, once is enough */
182 ret = hd29l2_wr_reg(priv, 0x9d, priv->cfg.tuner_i2c_addr << 1);
186 priv->tuner_i2c_addr_programmed = true;
189 /* open / close gate */
190 ret = hd29l2_wr_reg(priv, 0x9f, enable);
194 /* wait demod ready */
195 for (i = 10; i; i--) {
196 ret = hd29l2_rd_reg(priv, 0x9e, &tmp);
203 usleep_range(5000, 10000);
206 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
210 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
214 static int hd29l2_read_status(struct dvb_frontend *fe, enum fe_status *status)
217 struct hd29l2_priv *priv = fe->demodulator_priv;
222 ret = hd29l2_rd_reg(priv, 0x05, &buf[0]);
228 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
229 FE_HAS_SYNC | FE_HAS_LOCK;
231 ret = hd29l2_rd_reg(priv, 0x0d, &buf[1]);
235 if ((buf[1] & 0xfe) == 0x78)
237 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
238 FE_HAS_VITERBI | FE_HAS_SYNC;
241 priv->fe_status = *status;
245 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
249 static int hd29l2_read_snr(struct dvb_frontend *fe, u16 *snr)
252 struct hd29l2_priv *priv = fe->demodulator_priv;
256 if (!(priv->fe_status & FE_HAS_LOCK)) {
262 ret = hd29l2_rd_regs(priv, 0x0b, buf, 2);
266 tmp = (buf[0] << 8) | buf[1];
268 /* report SNR in dB * 10 */
269 #define LOG10_20736_24 72422627 /* log10(20736) << 24 */
271 *snr = (LOG10_20736_24 - intlog10(tmp)) / ((1 << 24) / 100);
277 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
281 static int hd29l2_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
284 struct hd29l2_priv *priv = fe->demodulator_priv;
290 ret = hd29l2_rd_regs(priv, 0xd5, buf, 2);
294 tmp = buf[0] << 8 | buf[1];
297 /* scale value to 0x0000-0xffff from 0x0000-0x0fff */
298 *strength = tmp * 0xffff / 0x0fff;
302 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
306 static int hd29l2_read_ber(struct dvb_frontend *fe, u32 *ber)
309 struct hd29l2_priv *priv = fe->demodulator_priv;
312 if (!(priv->fe_status & FE_HAS_SYNC)) {
318 ret = hd29l2_rd_regs(priv, 0xd9, buf, 2);
325 *ber = ((buf[0] & 0x0f) << 8) | buf[1];
329 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
333 static int hd29l2_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
335 /* no way to read? */
340 static enum dvbfe_search hd29l2_search(struct dvb_frontend *fe)
343 struct hd29l2_priv *priv = fe->demodulator_priv;
344 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
346 u8 modulation, carrier, guard_interval, interleave, code_rate;
351 dev_dbg(&priv->i2c->dev, "%s: delivery_system=%d frequency=%d " \
352 "bandwidth_hz=%d modulation=%d inversion=%d " \
353 "fec_inner=%d guard_interval=%d\n", __func__,
354 c->delivery_system, c->frequency, c->bandwidth_hz,
355 c->modulation, c->inversion, c->fec_inner,
358 /* as for now we detect always params automatically */
362 if (fe->ops.tuner_ops.set_params)
363 fe->ops.tuner_ops.set_params(fe);
365 /* get and program IF */
366 if (fe->ops.tuner_ops.get_if_frequency)
367 fe->ops.tuner_ops.get_if_frequency(fe, &if_freq);
374 /* calc IF control value */
377 num64 = div_u64(num64, HD29L2_XTAL);
381 tmp = 0xfc; /* tuner type normal */
385 tmp = 0xfe; /* tuner type Zero-IF */
388 buf[0] = ((if_ctl >> 0) & 0xff);
389 buf[1] = ((if_ctl >> 8) & 0xff);
390 buf[2] = ((if_ctl >> 16) & 0xff);
392 /* program IF control */
393 ret = hd29l2_wr_regs(priv, 0x14, buf, 3);
397 /* program tuner type */
398 ret = hd29l2_wr_reg(priv, 0xab, tmp);
402 dev_dbg(&priv->i2c->dev, "%s: if_freq=%d if_ctl=%x\n",
403 __func__, if_freq, if_ctl);
410 /* disable quick mode */
411 ret = hd29l2_wr_reg_mask(priv, 0xac, 0 << 7, 0x80);
415 ret = hd29l2_wr_reg_mask(priv, 0x82, 1 << 1, 0x02);
419 /* enable auto mode */
420 ret = hd29l2_wr_reg_mask(priv, 0x7d, 1 << 6, 0x40);
424 ret = hd29l2_wr_reg_mask(priv, 0x81, 1 << 3, 0x08);
429 ret = hd29l2_soft_reset(priv);
433 /* detect modulation */
434 for (i = 30; i; i--) {
437 ret = hd29l2_rd_reg(priv, 0x0d, &tmp);
441 if ((((tmp & 0xf0) >= 0x10) &&
442 ((tmp & 0x0f) == 0x08)) || (tmp >= 0x2c))
446 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
449 /* detection failed */
450 return DVBFE_ALGO_SEARCH_FAILED;
452 /* read modulation */
453 ret = hd29l2_rd_reg_mask(priv, 0x7d, &modulation, 0x07);
461 modulation = HD29L2_QAM64;
462 carrier = HD29L2_CARRIER_MULTI;
463 guard_interval = HD29L2_PN945;
464 interleave = HD29L2_INTERLEAVER_420;
465 code_rate = HD29L2_CODE_RATE_08;
467 tmp = (code_rate << 3) | modulation;
468 ret = hd29l2_wr_reg_mask(priv, 0x7d, tmp, 0x5f);
472 tmp = (carrier << 2) | guard_interval;
473 ret = hd29l2_wr_reg_mask(priv, 0x81, tmp, 0x0f);
478 ret = hd29l2_wr_reg_mask(priv, 0x82, tmp, 0x03);
483 /* ensure modulation validy */
484 /* 0=QAM4_NR, 1=QAM4, 2=QAM16, 3=QAM32, 4=QAM64 */
485 if (modulation > (ARRAY_SIZE(reg_mod_vals_tab[0].val) - 1)) {
486 dev_dbg(&priv->i2c->dev, "%s: modulation=%d not valid\n",
487 __func__, modulation);
491 /* program registers according to modulation */
492 for (i = 0; i < ARRAY_SIZE(reg_mod_vals_tab); i++) {
493 ret = hd29l2_wr_reg(priv, reg_mod_vals_tab[i].reg,
494 reg_mod_vals_tab[i].val[modulation]);
499 /* read guard interval */
500 ret = hd29l2_rd_reg_mask(priv, 0x81, &guard_interval, 0x03);
504 /* read carrier mode */
505 ret = hd29l2_rd_reg_mask(priv, 0x81, &carrier, 0x04);
509 dev_dbg(&priv->i2c->dev,
510 "%s: modulation=%d guard_interval=%d carrier=%d\n",
511 __func__, modulation, guard_interval, carrier);
513 if ((carrier == HD29L2_CARRIER_MULTI) && (modulation == HD29L2_QAM64) &&
514 (guard_interval == HD29L2_PN945)) {
515 dev_dbg(&priv->i2c->dev, "%s: C=3780 && QAM64 && PN945\n",
518 ret = hd29l2_wr_reg(priv, 0x42, 0x33);
522 ret = hd29l2_wr_reg(priv, 0xdd, 0x01);
527 usleep_range(10000, 20000);
530 ret = hd29l2_soft_reset(priv);
534 /* wait demod lock */
535 for (i = 30; i; i--) {
539 ret = hd29l2_rd_reg_mask(priv, 0x05, &tmp, 0x01);
547 dev_dbg(&priv->i2c->dev, "%s: loop=%d\n", __func__, i);
550 return DVBFE_ALGO_SEARCH_AGAIN;
552 return DVBFE_ALGO_SEARCH_SUCCESS;
554 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
555 return DVBFE_ALGO_SEARCH_ERROR;
558 static int hd29l2_get_frontend_algo(struct dvb_frontend *fe)
560 return DVBFE_ALGO_CUSTOM;
563 static int hd29l2_get_frontend(struct dvb_frontend *fe,
564 struct dtv_frontend_properties *c)
567 struct hd29l2_priv *priv = fe->demodulator_priv;
570 char *str_constellation, *str_code_rate, *str_constellation_code_rate,
571 *str_guard_interval, *str_carrier, *str_guard_interval_carrier,
572 *str_interleave, *str_interleave_;
574 ret = hd29l2_rd_reg(priv, 0x7d, &buf[0]);
578 ret = hd29l2_rd_regs(priv, 0x81, &buf[1], 2);
582 /* constellation, 0x7d[2:0] */
583 switch ((buf[0] >> 0) & 0x07) {
585 str_constellation = "QAM4NR";
586 c->modulation = QAM_AUTO; /* FIXME */
589 str_constellation = "QAM4";
590 c->modulation = QPSK; /* FIXME */
593 str_constellation = "QAM16";
594 c->modulation = QAM_16;
597 str_constellation = "QAM32";
598 c->modulation = QAM_32;
601 str_constellation = "QAM64";
602 c->modulation = QAM_64;
605 str_constellation = "?";
608 /* LDPC code rate, 0x7d[4:3] */
609 switch ((buf[0] >> 3) & 0x03) {
611 str_code_rate = "0.4";
612 c->fec_inner = FEC_AUTO; /* FIXME */
615 str_code_rate = "0.6";
616 c->fec_inner = FEC_3_5;
619 str_code_rate = "0.8";
620 c->fec_inner = FEC_4_5;
626 /* constellation & code rate set, 0x7d[6] */
627 switch ((buf[0] >> 6) & 0x01) {
629 str_constellation_code_rate = "manual";
632 str_constellation_code_rate = "auto";
635 str_constellation_code_rate = "?";
638 /* frame header, 0x81[1:0] */
639 switch ((buf[1] >> 0) & 0x03) {
641 str_guard_interval = "PN945";
642 c->guard_interval = GUARD_INTERVAL_AUTO; /* FIXME */
645 str_guard_interval = "PN595";
646 c->guard_interval = GUARD_INTERVAL_AUTO; /* FIXME */
649 str_guard_interval = "PN420";
650 c->guard_interval = GUARD_INTERVAL_AUTO; /* FIXME */
653 str_guard_interval = "?";
656 /* carrier, 0x81[2] */
657 switch ((buf[1] >> 2) & 0x01) {
662 str_carrier = "C=3780";
668 /* frame header & carrier set, 0x81[3] */
669 switch ((buf[1] >> 3) & 0x01) {
671 str_guard_interval_carrier = "manual";
674 str_guard_interval_carrier = "auto";
677 str_guard_interval_carrier = "?";
680 /* interleave, 0x82[0] */
681 switch ((buf[2] >> 0) & 0x01) {
683 str_interleave = "M=720";
686 str_interleave = "M=240";
689 str_interleave = "?";
692 /* interleave set, 0x82[1] */
693 switch ((buf[2] >> 1) & 0x01) {
695 str_interleave_ = "manual";
698 str_interleave_ = "auto";
701 str_interleave_ = "?";
705 * We can read out current detected NCO and use that value next
706 * time instead of calculating new value from targed IF.
707 * I think it will not effect receiver sensitivity but gaining lock
708 * after tune could be easier...
710 ret = hd29l2_rd_regs(priv, 0xb1, &buf[0], 3);
714 if_ctl = (buf[0] << 16) | ((buf[1] - 7) << 8) | buf[2];
716 dev_dbg(&priv->i2c->dev, "%s: %s %s %s | %s %s %s | %s %s | NCO=%06x\n",
717 __func__, str_constellation, str_code_rate,
718 str_constellation_code_rate, str_guard_interval,
719 str_carrier, str_guard_interval_carrier, str_interleave,
720 str_interleave_, if_ctl);
723 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
727 static int hd29l2_init(struct dvb_frontend *fe)
730 struct hd29l2_priv *priv = fe->demodulator_priv;
732 static const struct reg_val tab[] = {
742 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
745 /* it is recommended to HW reset chip using RST_N pin */
747 ret = fe->callback(fe, DVB_FRONTEND_COMPONENT_DEMOD, 0, 0);
751 /* reprogramming needed because HW reset clears registers */
752 priv->tuner_i2c_addr_programmed = false;
756 for (i = 0; i < ARRAY_SIZE(tab); i++) {
757 ret = hd29l2_wr_reg(priv, tab[i].reg, tab[i].val);
763 ret = hd29l2_rd_reg(priv, 0x36, &tmp);
768 tmp |= priv->cfg.ts_mode;
769 ret = hd29l2_wr_reg(priv, 0x36, tmp);
773 ret = hd29l2_rd_reg(priv, 0x31, &tmp);
776 if (!(priv->cfg.ts_mode >> 7))
777 /* set b4 for serial TS */
780 ret = hd29l2_wr_reg(priv, 0x31, tmp);
786 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
790 static void hd29l2_release(struct dvb_frontend *fe)
792 struct hd29l2_priv *priv = fe->demodulator_priv;
796 static struct dvb_frontend_ops hd29l2_ops;
798 struct dvb_frontend *hd29l2_attach(const struct hd29l2_config *config,
799 struct i2c_adapter *i2c)
802 struct hd29l2_priv *priv = NULL;
805 /* allocate memory for the internal state */
806 priv = kzalloc(sizeof(struct hd29l2_priv), GFP_KERNEL);
810 /* setup the state */
812 memcpy(&priv->cfg, config, sizeof(struct hd29l2_config));
815 /* check if the demod is there */
816 ret = hd29l2_rd_reg(priv, 0x00, &tmp);
820 /* create dvb_frontend */
821 memcpy(&priv->fe.ops, &hd29l2_ops, sizeof(struct dvb_frontend_ops));
822 priv->fe.demodulator_priv = priv;
829 EXPORT_SYMBOL(hd29l2_attach);
831 static struct dvb_frontend_ops hd29l2_ops = {
832 .delsys = { SYS_DVBT },
834 .name = "HDIC HD29L2 DMB-TH",
835 .frequency_min = 474000000,
836 .frequency_max = 858000000,
837 .frequency_stepsize = 10000,
838 .caps = FE_CAN_FEC_AUTO |
844 FE_CAN_TRANSMISSION_MODE_AUTO |
845 FE_CAN_BANDWIDTH_AUTO |
846 FE_CAN_GUARD_INTERVAL_AUTO |
847 FE_CAN_HIERARCHY_AUTO |
851 .release = hd29l2_release,
855 .get_frontend_algo = hd29l2_get_frontend_algo,
856 .search = hd29l2_search,
857 .get_frontend = hd29l2_get_frontend,
859 .read_status = hd29l2_read_status,
860 .read_snr = hd29l2_read_snr,
861 .read_signal_strength = hd29l2_read_signal_strength,
862 .read_ber = hd29l2_read_ber,
863 .read_ucblocks = hd29l2_read_ucblocks,
865 .i2c_gate_ctrl = hd29l2_i2c_gate_ctrl,
869 MODULE_DESCRIPTION("HDIC HD29L2 DMB-TH demodulator driver");
870 MODULE_LICENSE("GPL");