1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * mxl5007t.c - driver for the MaxLinear MxL5007T silicon tuner
5 * Copyright (C) 2008, 2009 Michael Krufky <mkrufky@linuxtv.org>
9 #include <linux/types.h>
10 #include <linux/videodev2.h>
11 #include "tuner-i2c.h"
14 static DEFINE_MUTEX(mxl5007t_list_mutex);
15 static LIST_HEAD(hybrid_tuner_instance_list);
17 static int mxl5007t_debug;
18 module_param_named(debug, mxl5007t_debug, int, 0644);
19 MODULE_PARM_DESC(debug, "set debug level");
21 /* ------------------------------------------------------------------------- */
23 #define mxl_printk(kern, fmt, arg...) \
24 printk(kern "%s: " fmt "\n", __func__, ##arg)
26 #define mxl_err(fmt, arg...) \
27 mxl_printk(KERN_ERR, "%d: " fmt, __LINE__, ##arg)
29 #define mxl_warn(fmt, arg...) \
30 mxl_printk(KERN_WARNING, fmt, ##arg)
32 #define mxl_info(fmt, arg...) \
33 mxl_printk(KERN_INFO, fmt, ##arg)
35 #define mxl_debug(fmt, arg...) \
38 mxl_printk(KERN_DEBUG, fmt, ##arg); \
41 #define mxl_fail(ret) \
46 mxl_printk(KERN_ERR, "error %d on line %d", \
51 /* ------------------------------------------------------------------------- */
57 MxL_MODE_CABLE = 0x10,
60 enum mxl5007t_chip_version {
61 MxL_UNKNOWN_ID = 0x00,
62 MxL_5007_V1_F1 = 0x11,
63 MxL_5007_V1_F2 = 0x12,
65 MxL_5007_V2_100_F1 = 0x21,
66 MxL_5007_V2_100_F2 = 0x22,
67 MxL_5007_V2_200_F1 = 0x23,
68 MxL_5007_V2_200_F2 = 0x24,
76 /* ------------------------------------------------------------------------- */
78 static struct reg_pair_t init_tab[] = {
83 { 0x2e, 0x15 }, /* OVERRIDE */
84 { 0x30, 0x10 }, /* OVERRIDE */
85 { 0x45, 0x58 }, /* OVERRIDE */
86 { 0x48, 0x19 }, /* OVERRIDE */
87 { 0x52, 0x03 }, /* OVERRIDE */
88 { 0x53, 0x44 }, /* OVERRIDE */
89 { 0x6a, 0x4b }, /* OVERRIDE */
90 { 0x76, 0x00 }, /* OVERRIDE */
91 { 0x78, 0x18 }, /* OVERRIDE */
92 { 0x7a, 0x17 }, /* OVERRIDE */
93 { 0x85, 0x06 }, /* OVERRIDE */
94 { 0x01, 0x01 }, /* TOP_MASTER_ENABLE */
98 static struct reg_pair_t init_tab_cable[] = {
106 { 0x2e, 0x15 }, /* OVERRIDE */
107 { 0x30, 0x10 }, /* OVERRIDE */
108 { 0x45, 0x58 }, /* OVERRIDE */
109 { 0x48, 0x19 }, /* OVERRIDE */
110 { 0x52, 0x03 }, /* OVERRIDE */
111 { 0x53, 0x44 }, /* OVERRIDE */
112 { 0x6a, 0x4b }, /* OVERRIDE */
113 { 0x76, 0x00 }, /* OVERRIDE */
114 { 0x78, 0x18 }, /* OVERRIDE */
115 { 0x7a, 0x17 }, /* OVERRIDE */
116 { 0x85, 0x06 }, /* OVERRIDE */
117 { 0x01, 0x01 }, /* TOP_MASTER_ENABLE */
121 /* ------------------------------------------------------------------------- */
123 static struct reg_pair_t reg_pair_rftune[] = {
124 { 0x0f, 0x00 }, /* abort tune */
128 { 0x1f, 0x87 }, /* OVERRIDE */
129 { 0x20, 0x1f }, /* OVERRIDE */
130 { 0x21, 0x87 }, /* OVERRIDE */
131 { 0x22, 0x1f }, /* OVERRIDE */
132 { 0x80, 0x01 }, /* freq dependent */
133 { 0x0f, 0x01 }, /* start tune */
137 /* ------------------------------------------------------------------------- */
139 struct mxl5007t_state {
140 struct list_head hybrid_tuner_instance_list;
141 struct tuner_i2c_props i2c_props;
145 struct mxl5007t_config *config;
147 enum mxl5007t_chip_version chip_id;
149 struct reg_pair_t tab_init[ARRAY_SIZE(init_tab)];
150 struct reg_pair_t tab_init_cable[ARRAY_SIZE(init_tab_cable)];
151 struct reg_pair_t tab_rftune[ARRAY_SIZE(reg_pair_rftune)];
153 enum mxl5007t_if_freq if_freq;
159 /* ------------------------------------------------------------------------- */
161 /* called by _init and _rftun to manipulate the register arrays */
163 static void set_reg_bits(struct reg_pair_t *reg_pair, u8 reg, u8 mask, u8 val)
167 while (reg_pair[i].reg || reg_pair[i].val) {
168 if (reg_pair[i].reg == reg) {
169 reg_pair[i].val &= ~mask;
170 reg_pair[i].val |= val;
178 static void copy_reg_bits(struct reg_pair_t *reg_pair1,
179 struct reg_pair_t *reg_pair2)
185 while (reg_pair1[i].reg || reg_pair1[i].val) {
186 while (reg_pair2[j].reg || reg_pair2[j].val) {
187 if (reg_pair1[i].reg != reg_pair2[j].reg) {
191 reg_pair2[j].val = reg_pair1[i].val;
199 /* ------------------------------------------------------------------------- */
201 static void mxl5007t_set_mode_bits(struct mxl5007t_state *state,
202 enum mxl5007t_mode mode,
203 s32 if_diff_out_level)
207 set_reg_bits(state->tab_init, 0x06, 0x1f, 0x12);
210 set_reg_bits(state->tab_init, 0x06, 0x1f, 0x11);
213 set_reg_bits(state->tab_init, 0x06, 0x1f, 0x10);
216 set_reg_bits(state->tab_init_cable, 0x09, 0xff, 0xc1);
217 set_reg_bits(state->tab_init_cable, 0x0a, 0xff,
218 8 - if_diff_out_level);
219 set_reg_bits(state->tab_init_cable, 0x0b, 0xff, 0x17);
227 static void mxl5007t_set_if_freq_bits(struct mxl5007t_state *state,
228 enum mxl5007t_if_freq if_freq,
240 case MxL_IF_4_57_MHZ:
246 case MxL_IF_5_38_MHZ:
252 case MxL_IF_6_28_MHZ:
255 case MxL_IF_9_1915_MHZ:
258 case MxL_IF_35_25_MHZ:
261 case MxL_IF_36_15_MHZ:
271 set_reg_bits(state->tab_init, 0x02, 0x0f, val);
273 /* set inverted IF or normal IF */
274 set_reg_bits(state->tab_init, 0x02, 0x10, invert_if ? 0x10 : 0x00);
276 state->if_freq = if_freq;
281 static void mxl5007t_set_xtal_freq_bits(struct mxl5007t_state *state,
282 enum mxl5007t_xtal_freq xtal_freq)
285 case MxL_XTAL_16_MHZ:
286 /* select xtal freq & ref freq */
287 set_reg_bits(state->tab_init, 0x03, 0xf0, 0x00);
288 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x00);
290 case MxL_XTAL_20_MHZ:
291 set_reg_bits(state->tab_init, 0x03, 0xf0, 0x10);
292 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x01);
294 case MxL_XTAL_20_25_MHZ:
295 set_reg_bits(state->tab_init, 0x03, 0xf0, 0x20);
296 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x02);
298 case MxL_XTAL_20_48_MHZ:
299 set_reg_bits(state->tab_init, 0x03, 0xf0, 0x30);
300 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x03);
302 case MxL_XTAL_24_MHZ:
303 set_reg_bits(state->tab_init, 0x03, 0xf0, 0x40);
304 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x04);
306 case MxL_XTAL_25_MHZ:
307 set_reg_bits(state->tab_init, 0x03, 0xf0, 0x50);
308 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x05);
310 case MxL_XTAL_25_14_MHZ:
311 set_reg_bits(state->tab_init, 0x03, 0xf0, 0x60);
312 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x06);
314 case MxL_XTAL_27_MHZ:
315 set_reg_bits(state->tab_init, 0x03, 0xf0, 0x70);
316 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x07);
318 case MxL_XTAL_28_8_MHZ:
319 set_reg_bits(state->tab_init, 0x03, 0xf0, 0x80);
320 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x08);
322 case MxL_XTAL_32_MHZ:
323 set_reg_bits(state->tab_init, 0x03, 0xf0, 0x90);
324 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x09);
326 case MxL_XTAL_40_MHZ:
327 set_reg_bits(state->tab_init, 0x03, 0xf0, 0xa0);
328 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0a);
330 case MxL_XTAL_44_MHZ:
331 set_reg_bits(state->tab_init, 0x03, 0xf0, 0xb0);
332 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0b);
334 case MxL_XTAL_48_MHZ:
335 set_reg_bits(state->tab_init, 0x03, 0xf0, 0xc0);
336 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0c);
338 case MxL_XTAL_49_3811_MHZ:
339 set_reg_bits(state->tab_init, 0x03, 0xf0, 0xd0);
340 set_reg_bits(state->tab_init, 0x05, 0x0f, 0x0d);
350 static struct reg_pair_t *mxl5007t_calc_init_regs(struct mxl5007t_state *state,
351 enum mxl5007t_mode mode)
353 struct mxl5007t_config *cfg = state->config;
355 memcpy(&state->tab_init, &init_tab, sizeof(init_tab));
356 memcpy(&state->tab_init_cable, &init_tab_cable, sizeof(init_tab_cable));
358 mxl5007t_set_mode_bits(state, mode, cfg->if_diff_out_level);
359 mxl5007t_set_if_freq_bits(state, cfg->if_freq_hz, cfg->invert_if);
360 mxl5007t_set_xtal_freq_bits(state, cfg->xtal_freq_hz);
362 set_reg_bits(state->tab_init, 0x03, 0x08, cfg->clk_out_enable << 3);
363 set_reg_bits(state->tab_init, 0x03, 0x07, cfg->clk_out_amp);
365 if (mode >= MxL_MODE_CABLE) {
366 copy_reg_bits(state->tab_init, state->tab_init_cable);
367 return state->tab_init_cable;
369 return state->tab_init;
372 /* ------------------------------------------------------------------------- */
374 enum mxl5007t_bw_mhz {
380 static void mxl5007t_set_bw_bits(struct mxl5007t_state *state,
381 enum mxl5007t_bw_mhz bw)
387 val = 0x15; /* set DIG_MODEINDEX, DIG_MODEINDEX_A,
388 * and DIG_MODEINDEX_CSF */
400 set_reg_bits(state->tab_rftune, 0x0c, 0x3f, val);
406 reg_pair_t *mxl5007t_calc_rf_tune_regs(struct mxl5007t_state *state,
407 u32 rf_freq, enum mxl5007t_bw_mhz bw)
411 u32 frac_divider = 1000000;
414 memcpy(&state->tab_rftune, ®_pair_rftune, sizeof(reg_pair_rftune));
416 mxl5007t_set_bw_bits(state, bw);
418 /* Convert RF frequency into 16 bits =>
419 * 10 bit integer (MHz) + 6 bit fraction */
420 dig_rf_freq = rf_freq / MHz;
422 temp = rf_freq % MHz;
424 for (i = 0; i < 6; i++) {
427 if (temp > frac_divider) {
428 temp -= frac_divider;
433 /* add to have shift center point by 7.8124 kHz */
437 set_reg_bits(state->tab_rftune, 0x0d, 0xff, (u8) dig_rf_freq);
438 set_reg_bits(state->tab_rftune, 0x0e, 0xff, (u8) (dig_rf_freq >> 8));
440 if (rf_freq >= 333000000)
441 set_reg_bits(state->tab_rftune, 0x80, 0x40, 0x40);
443 return state->tab_rftune;
446 /* ------------------------------------------------------------------------- */
448 static int mxl5007t_write_reg(struct mxl5007t_state *state, u8 reg, u8 val)
450 u8 buf[] = { reg, val };
451 struct i2c_msg msg = { .addr = state->i2c_props.addr, .flags = 0,
452 .buf = buf, .len = 2 };
455 ret = i2c_transfer(state->i2c_props.adap, &msg, 1);
463 static int mxl5007t_write_regs(struct mxl5007t_state *state,
464 struct reg_pair_t *reg_pair)
469 while ((ret == 0) && (reg_pair[i].reg || reg_pair[i].val)) {
470 ret = mxl5007t_write_reg(state,
471 reg_pair[i].reg, reg_pair[i].val);
477 static int mxl5007t_read_reg(struct mxl5007t_state *state, u8 reg, u8 *val)
479 u8 buf[2] = { 0xfb, reg };
480 struct i2c_msg msg[] = {
481 { .addr = state->i2c_props.addr, .flags = 0,
482 .buf = buf, .len = 2 },
483 { .addr = state->i2c_props.addr, .flags = I2C_M_RD,
484 .buf = val, .len = 1 },
488 ret = i2c_transfer(state->i2c_props.adap, msg, 2);
496 static int mxl5007t_soft_reset(struct mxl5007t_state *state)
499 struct i2c_msg msg = {
500 .addr = state->i2c_props.addr, .flags = 0,
503 int ret = i2c_transfer(state->i2c_props.adap, &msg, 1);
512 static int mxl5007t_tuner_init(struct mxl5007t_state *state,
513 enum mxl5007t_mode mode)
515 struct reg_pair_t *init_regs;
518 /* calculate initialization reg array */
519 init_regs = mxl5007t_calc_init_regs(state, mode);
521 ret = mxl5007t_write_regs(state, init_regs);
529 static int mxl5007t_tuner_rf_tune(struct mxl5007t_state *state, u32 rf_freq_hz,
530 enum mxl5007t_bw_mhz bw)
532 struct reg_pair_t *rf_tune_regs;
535 /* calculate channel change reg array */
536 rf_tune_regs = mxl5007t_calc_rf_tune_regs(state, rf_freq_hz, bw);
538 ret = mxl5007t_write_regs(state, rf_tune_regs);
546 /* ------------------------------------------------------------------------- */
548 static int mxl5007t_synth_lock_status(struct mxl5007t_state *state,
549 int *rf_locked, int *ref_locked)
557 ret = mxl5007t_read_reg(state, 0xd8, &d);
561 if ((d & 0x0c) == 0x0c)
564 if ((d & 0x03) == 0x03)
570 /* ------------------------------------------------------------------------- */
572 static int mxl5007t_get_status(struct dvb_frontend *fe, u32 *status)
574 struct mxl5007t_state *state = fe->tuner_priv;
575 int rf_locked, ref_locked, ret;
579 if (fe->ops.i2c_gate_ctrl)
580 fe->ops.i2c_gate_ctrl(fe, 1);
582 ret = mxl5007t_synth_lock_status(state, &rf_locked, &ref_locked);
585 mxl_debug("%s%s", rf_locked ? "rf locked " : "",
586 ref_locked ? "ref locked" : "");
588 if ((rf_locked) || (ref_locked))
589 *status |= TUNER_STATUS_LOCKED;
591 if (fe->ops.i2c_gate_ctrl)
592 fe->ops.i2c_gate_ctrl(fe, 0);
597 /* ------------------------------------------------------------------------- */
599 static int mxl5007t_set_params(struct dvb_frontend *fe)
601 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
602 u32 delsys = c->delivery_system;
603 struct mxl5007t_state *state = fe->tuner_priv;
604 enum mxl5007t_bw_mhz bw;
605 enum mxl5007t_mode mode;
607 u32 freq = c->frequency;
611 mode = MxL_MODE_ATSC;
614 case SYS_DVBC_ANNEX_B:
615 mode = MxL_MODE_CABLE;
620 mode = MxL_MODE_DVBT;
621 switch (c->bandwidth_hz) {
636 mxl_err("modulation type not supported!");
640 if (fe->ops.i2c_gate_ctrl)
641 fe->ops.i2c_gate_ctrl(fe, 1);
643 mutex_lock(&state->lock);
645 ret = mxl5007t_tuner_init(state, mode);
649 ret = mxl5007t_tuner_rf_tune(state, freq, bw);
653 state->frequency = freq;
654 state->bandwidth = c->bandwidth_hz;
656 mutex_unlock(&state->lock);
658 if (fe->ops.i2c_gate_ctrl)
659 fe->ops.i2c_gate_ctrl(fe, 0);
664 /* ------------------------------------------------------------------------- */
666 static int mxl5007t_init(struct dvb_frontend *fe)
668 struct mxl5007t_state *state = fe->tuner_priv;
671 if (fe->ops.i2c_gate_ctrl)
672 fe->ops.i2c_gate_ctrl(fe, 1);
674 /* wake from standby */
675 ret = mxl5007t_write_reg(state, 0x01, 0x01);
678 if (fe->ops.i2c_gate_ctrl)
679 fe->ops.i2c_gate_ctrl(fe, 0);
684 static int mxl5007t_sleep(struct dvb_frontend *fe)
686 struct mxl5007t_state *state = fe->tuner_priv;
689 if (fe->ops.i2c_gate_ctrl)
690 fe->ops.i2c_gate_ctrl(fe, 1);
692 /* enter standby mode */
693 ret = mxl5007t_write_reg(state, 0x01, 0x00);
695 ret = mxl5007t_write_reg(state, 0x0f, 0x00);
698 if (fe->ops.i2c_gate_ctrl)
699 fe->ops.i2c_gate_ctrl(fe, 0);
704 /* ------------------------------------------------------------------------- */
706 static int mxl5007t_get_frequency(struct dvb_frontend *fe, u32 *frequency)
708 struct mxl5007t_state *state = fe->tuner_priv;
709 *frequency = state->frequency;
713 static int mxl5007t_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
715 struct mxl5007t_state *state = fe->tuner_priv;
716 *bandwidth = state->bandwidth;
720 static int mxl5007t_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
722 struct mxl5007t_state *state = fe->tuner_priv;
726 switch (state->if_freq) {
728 *frequency = 4000000;
731 *frequency = 4500000;
733 case MxL_IF_4_57_MHZ:
734 *frequency = 4570000;
737 *frequency = 5000000;
739 case MxL_IF_5_38_MHZ:
740 *frequency = 5380000;
743 *frequency = 6000000;
745 case MxL_IF_6_28_MHZ:
746 *frequency = 6280000;
748 case MxL_IF_9_1915_MHZ:
749 *frequency = 9191500;
751 case MxL_IF_35_25_MHZ:
752 *frequency = 35250000;
754 case MxL_IF_36_15_MHZ:
755 *frequency = 36150000;
758 *frequency = 44000000;
764 static void mxl5007t_release(struct dvb_frontend *fe)
766 struct mxl5007t_state *state = fe->tuner_priv;
768 mutex_lock(&mxl5007t_list_mutex);
771 hybrid_tuner_release_state(state);
773 mutex_unlock(&mxl5007t_list_mutex);
775 fe->tuner_priv = NULL;
778 /* ------------------------------------------------------------------------- */
780 static const struct dvb_tuner_ops mxl5007t_tuner_ops = {
782 .name = "MaxLinear MxL5007T",
784 .init = mxl5007t_init,
785 .sleep = mxl5007t_sleep,
786 .set_params = mxl5007t_set_params,
787 .get_status = mxl5007t_get_status,
788 .get_frequency = mxl5007t_get_frequency,
789 .get_bandwidth = mxl5007t_get_bandwidth,
790 .release = mxl5007t_release,
791 .get_if_frequency = mxl5007t_get_if_frequency,
794 static int mxl5007t_get_chip_id(struct mxl5007t_state *state)
800 ret = mxl5007t_read_reg(state, 0xd9, &id);
806 name = "MxL5007.v1.f1";
809 name = "MxL5007.v1.f2";
811 case MxL_5007_V2_100_F1:
812 name = "MxL5007.v2.100.f1";
814 case MxL_5007_V2_100_F2:
815 name = "MxL5007.v2.100.f2";
817 case MxL_5007_V2_200_F1:
818 name = "MxL5007.v2.200.f1";
820 case MxL_5007_V2_200_F2:
821 name = "MxL5007.v2.200.f2";
824 name = "MxL5007T.v4";
828 printk(KERN_WARNING "%s: unknown rev (%02x)\n", __func__, id);
832 mxl_info("%s detected @ %d-%04x", name,
833 i2c_adapter_id(state->i2c_props.adap),
834 state->i2c_props.addr);
837 mxl_warn("unable to identify device @ %d-%04x",
838 i2c_adapter_id(state->i2c_props.adap),
839 state->i2c_props.addr);
841 state->chip_id = MxL_UNKNOWN_ID;
845 struct dvb_frontend *mxl5007t_attach(struct dvb_frontend *fe,
846 struct i2c_adapter *i2c, u8 addr,
847 struct mxl5007t_config *cfg)
849 struct mxl5007t_state *state = NULL;
852 mutex_lock(&mxl5007t_list_mutex);
853 instance = hybrid_tuner_request_state(struct mxl5007t_state, state,
854 hybrid_tuner_instance_list,
855 i2c, addr, "mxl5007t");
860 /* new tuner instance */
863 mutex_init(&state->lock);
865 if (fe->ops.i2c_gate_ctrl)
866 fe->ops.i2c_gate_ctrl(fe, 1);
868 ret = mxl5007t_get_chip_id(state);
870 if (fe->ops.i2c_gate_ctrl)
871 fe->ops.i2c_gate_ctrl(fe, 0);
873 /* check return value of mxl5007t_get_chip_id */
878 /* existing tuner instance */
882 if (fe->ops.i2c_gate_ctrl)
883 fe->ops.i2c_gate_ctrl(fe, 1);
885 ret = mxl5007t_soft_reset(state);
887 if (fe->ops.i2c_gate_ctrl)
888 fe->ops.i2c_gate_ctrl(fe, 0);
893 if (fe->ops.i2c_gate_ctrl)
894 fe->ops.i2c_gate_ctrl(fe, 1);
896 ret = mxl5007t_write_reg(state, 0x04,
897 state->config->loop_thru_enable);
899 if (fe->ops.i2c_gate_ctrl)
900 fe->ops.i2c_gate_ctrl(fe, 0);
905 fe->tuner_priv = state;
907 mutex_unlock(&mxl5007t_list_mutex);
909 memcpy(&fe->ops.tuner_ops, &mxl5007t_tuner_ops,
910 sizeof(struct dvb_tuner_ops));
914 mutex_unlock(&mxl5007t_list_mutex);
916 mxl5007t_release(fe);
919 EXPORT_SYMBOL_GPL(mxl5007t_attach);
920 MODULE_DESCRIPTION("MaxLinear MxL5007T Silicon IC tuner driver");
921 MODULE_AUTHOR("Michael Krufky <mkrufky@linuxtv.org>");
922 MODULE_LICENSE("GPL");
923 MODULE_VERSION("0.2");