blob: f9703a1dd758cbdec39fbab279d2d4ce8322e8e0 [file] [log] [blame]
Thomas Gleixner74ba9202019-05-20 09:19:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew de Quincey6bca35802006-08-08 09:10:10 -03002 /*
3 Driver for Philips tda8262/tda8263 DVBS Silicon tuners
4
5 (c) 2006 Andrew de Quincey
6
Andrew de Quincey6bca35802006-08-08 09:10:10 -03007
8 */
9
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090010#include <linux/slab.h>
Andrew de Quincey6bca35802006-08-08 09:10:10 -030011#include <linux/module.h>
12#include <linux/dvb/frontend.h>
13#include <asm/types.h>
14
15#include "tda826x.h"
16
Douglas Schilling Landgrafff699e62008-04-22 14:41:48 -030017static int debug;
Andrew de Quincey8b9a4662006-08-08 09:10:11 -030018#define dprintk(args...) \
19 do { \
20 if (debug) printk(KERN_DEBUG "tda826x: " args); \
21 } while (0)
22
Andrew de Quincey6bca35802006-08-08 09:10:10 -030023struct tda826x_priv {
24 /* i2c details */
25 int i2c_address;
26 struct i2c_adapter *i2c;
27 u8 has_loopthrough:1;
28 u32 frequency;
29};
30
Mauro Carvalho Chehabf2709c22016-11-18 20:30:51 -020031static void tda826x_release(struct dvb_frontend *fe)
32{
33 kfree(fe->tuner_priv);
34 fe->tuner_priv = NULL;
35}
36
Andrew de Quincey6bca35802006-08-08 09:10:10 -030037static int tda826x_sleep(struct dvb_frontend *fe)
38{
39 struct tda826x_priv *priv = fe->tuner_priv;
40 int ret;
41 u8 buf [] = { 0x00, 0x8d };
42 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 2 };
43
Harvey Harrison271ddbf2008-04-08 23:20:00 -030044 dprintk("%s:\n", __func__);
Andrew de Quincey8b9a4662006-08-08 09:10:11 -030045
Andrew de Quincey6bca35802006-08-08 09:10:10 -030046 if (!priv->has_loopthrough)
47 buf[1] = 0xad;
48
49 if (fe->ops.i2c_gate_ctrl)
50 fe->ops.i2c_gate_ctrl(fe, 1);
51 if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -030052 dprintk("%s: i2c error\n", __func__);
Andrew de Quincey6bca35802006-08-08 09:10:10 -030053 }
54 if (fe->ops.i2c_gate_ctrl)
55 fe->ops.i2c_gate_ctrl(fe, 0);
Andrew de Quincey6bca35802006-08-08 09:10:10 -030056
57 return (ret == 1) ? 0 : ret;
58}
59
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030060static int tda826x_set_params(struct dvb_frontend *fe)
Andrew de Quincey6bca35802006-08-08 09:10:10 -030061{
Mauro Carvalho Chehab66e6cd52011-12-23 18:43:29 -030062 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Andrew de Quincey6bca35802006-08-08 09:10:10 -030063 struct tda826x_priv *priv = fe->tuner_priv;
64 int ret;
65 u32 div;
Hartmut Hackmann3ff9a812008-04-13 22:41:19 -030066 u32 ksyms;
67 u32 bandwidth;
Andrew de Quincey6bca35802006-08-08 09:10:10 -030068 u8 buf [11];
69 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 11 };
70
Harvey Harrison271ddbf2008-04-08 23:20:00 -030071 dprintk("%s:\n", __func__);
Andrew de Quincey8b9a4662006-08-08 09:10:11 -030072
Mauro Carvalho Chehab66e6cd52011-12-23 18:43:29 -030073 div = (p->frequency + (1000-1)) / 1000;
Andrew de Quincey6bca35802006-08-08 09:10:10 -030074
Hartmut Hackmann3ff9a812008-04-13 22:41:19 -030075 /* BW = ((1 + RO) * SR/2 + 5) * 1.3 [SR in MSPS, BW in MHz] */
76 /* with R0 = 0.35 and some transformations: */
Mauro Carvalho Chehab66e6cd52011-12-23 18:43:29 -030077 ksyms = p->symbol_rate / 1000;
Hartmut Hackmann3ff9a812008-04-13 22:41:19 -030078 bandwidth = (878 * ksyms + 6500000) / 1000000 + 1;
79 if (bandwidth < 5)
80 bandwidth = 5;
81 else if (bandwidth > 36)
82 bandwidth = 36;
83
Andrew de Quincey6bca35802006-08-08 09:10:10 -030084 buf[0] = 0x00; // subaddress
85 buf[1] = 0x09; // powerdown RSSI + the magic value 1
86 if (!priv->has_loopthrough)
87 buf[1] |= 0x20; // power down loopthrough if not needed
88 buf[2] = (1<<5) | 0x0b; // 1Mhz + 0.45 VCO
89 buf[3] = div >> 7;
90 buf[4] = div << 1;
Hartmut Hackmann3ff9a812008-04-13 22:41:19 -030091 buf[5] = ((bandwidth - 5) << 3) | 7; /* baseband cut-off */
Oliver Endrissc6604152007-05-28 18:06:27 -030092 buf[6] = 0xfe; // baseband gain 9 db + no RF attenuation
Andrew de Quincey6bca35802006-08-08 09:10:10 -030093 buf[7] = 0x83; // charge pumps at high, tests off
94 buf[8] = 0x80; // recommended value 4 for AMPVCO + disable ports.
95 buf[9] = 0x1a; // normal caltime + recommended values for SELTH + SELVTL
96 buf[10] = 0xd4; // recommended value 13 for BBIAS + unknown bit set on
97
98 if (fe->ops.i2c_gate_ctrl)
99 fe->ops.i2c_gate_ctrl(fe, 1);
100 if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300101 dprintk("%s: i2c error\n", __func__);
Andrew de Quincey6bca35802006-08-08 09:10:10 -0300102 }
103 if (fe->ops.i2c_gate_ctrl)
104 fe->ops.i2c_gate_ctrl(fe, 0);
105
106 priv->frequency = div * 1000;
Andrew de Quincey6bca35802006-08-08 09:10:10 -0300107
108 return (ret == 1) ? 0 : ret;
109}
110
111static int tda826x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
112{
113 struct tda826x_priv *priv = fe->tuner_priv;
114 *frequency = priv->frequency;
115 return 0;
116}
117
Julia Lawall14c4bf32016-09-11 11:44:12 -0300118static const struct dvb_tuner_ops tda826x_tuner_ops = {
Trent Piephoef09c072006-08-08 09:10:11 -0300119 .info = {
120 .name = "Philips TDA826X",
Mauro Carvalho Chehaba3f90c72018-07-05 18:59:35 -0400121 .frequency_min_hz = 950 * MHz,
122 .frequency_max_hz = 2175 * MHz
Trent Piephoef09c072006-08-08 09:10:11 -0300123 },
Mauro Carvalho Chehabf2709c22016-11-18 20:30:51 -0200124 .release = tda826x_release,
Andrew de Quincey6bca35802006-08-08 09:10:10 -0300125 .sleep = tda826x_sleep,
126 .set_params = tda826x_set_params,
127 .get_frequency = tda826x_get_frequency,
128};
129
130struct dvb_frontend *tda826x_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c, int has_loopthrough)
131{
132 struct tda826x_priv *priv = NULL;
133 u8 b1 [] = { 0, 0 };
Patrick Boettcher8949f1a2006-11-21 05:35:10 -0300134 struct i2c_msg msg[2] = {
135 { .addr = addr, .flags = 0, .buf = NULL, .len = 0 },
136 { .addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 2 }
137 };
Andrew de Quincey6bca35802006-08-08 09:10:10 -0300138 int ret;
139
Harvey Harrison271ddbf2008-04-08 23:20:00 -0300140 dprintk("%s:\n", __func__);
Andrew de Quincey8b9a4662006-08-08 09:10:11 -0300141
Andrew de Quincey6bca35802006-08-08 09:10:10 -0300142 if (fe->ops.i2c_gate_ctrl)
143 fe->ops.i2c_gate_ctrl(fe, 1);
Patrick Boettcher8949f1a2006-11-21 05:35:10 -0300144 ret = i2c_transfer (i2c, msg, 2);
Andrew de Quincey6bca35802006-08-08 09:10:10 -0300145 if (fe->ops.i2c_gate_ctrl)
146 fe->ops.i2c_gate_ctrl(fe, 0);
147
Patrick Boettcher8949f1a2006-11-21 05:35:10 -0300148 if (ret != 2)
Andrew de Quincey6bca35802006-08-08 09:10:10 -0300149 return NULL;
150 if (!(b1[1] & 0x80))
151 return NULL;
152
153 priv = kzalloc(sizeof(struct tda826x_priv), GFP_KERNEL);
154 if (priv == NULL)
155 return NULL;
156
157 priv->i2c_address = addr;
158 priv->i2c = i2c;
159 priv->has_loopthrough = has_loopthrough;
160
161 memcpy(&fe->ops.tuner_ops, &tda826x_tuner_ops, sizeof(struct dvb_tuner_ops));
Andrew de Quincey6bca35802006-08-08 09:10:10 -0300162
163 fe->tuner_priv = priv;
Andrew de Quincey6bca35802006-08-08 09:10:10 -0300164
165 return fe;
166}
167EXPORT_SYMBOL(tda826x_attach);
168
Andrew de Quincey8b9a4662006-08-08 09:10:11 -0300169module_param(debug, int, 0644);
170MODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
171
Andrew de Quincey6bca35802006-08-08 09:10:10 -0300172MODULE_DESCRIPTION("DVB TDA826x driver");
173MODULE_AUTHOR("Andrew de Quincey");
174MODULE_LICENSE("GPL");