blob: f7c3e6be8e4d6828b776a6f3435f107829be6785 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Mauro Carvalho Chehabcba862d2017-11-29 08:33:45 -05002/*
Andrew de Quincey00be2e72006-09-05 17:53:06 -03003 * Driver for Infineon tua6100 pll.
4 *
5 * (c) 2006 Andrew de Quincey
6 *
7 * Based on code found in budget-av.c, which has the following:
8 * Compiled from various sources by Michael Hunold <michael@mihu.de>
9 *
10 * CI interface support (c) 2004 Olivier Gournet <ogournet@anevia.com> &
11 * Andrew de Quincey <adq_dvb@lidskialf.net>
12 *
13 * Copyright (C) 2002 Ralph Metzler <rjkm@metzlerbros.de>
14 *
15 * Copyright (C) 1999-2002 Ralph Metzler
16 * & Marcus Metzler for convergence integrated media GmbH
Andrew de Quincey00be2e72006-09-05 17:53:06 -030017 */
18
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Andrew de Quincey00be2e72006-09-05 17:53:06 -030020#include <linux/module.h>
21#include <linux/dvb/frontend.h>
22#include <asm/types.h>
23
24#include "tua6100.h"
25
26struct tua6100_priv {
27 /* i2c details */
28 int i2c_address;
29 struct i2c_adapter *i2c;
30 u32 frequency;
31};
32
Mauro Carvalho Chehabf2709c22016-11-18 20:30:51 -020033static void tua6100_release(struct dvb_frontend *fe)
34{
35 kfree(fe->tuner_priv);
36 fe->tuner_priv = NULL;
37}
38
Andrew de Quincey00be2e72006-09-05 17:53:06 -030039static int tua6100_sleep(struct dvb_frontend *fe)
40{
41 struct tua6100_priv *priv = fe->tuner_priv;
42 int ret;
43 u8 reg0[] = { 0x00, 0x00 };
44 struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
45
46 if (fe->ops.i2c_gate_ctrl)
47 fe->ops.i2c_gate_ctrl(fe, 1);
48 if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
Harvey Harrison271ddbf2008-04-08 23:20:00 -030049 printk("%s: i2c error\n", __func__);
Andrew de Quincey00be2e72006-09-05 17:53:06 -030050 }
51 if (fe->ops.i2c_gate_ctrl)
52 fe->ops.i2c_gate_ctrl(fe, 0);
53
54 return (ret == 1) ? 0 : ret;
55}
56
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030057static int tua6100_set_params(struct dvb_frontend *fe)
Andrew de Quincey00be2e72006-09-05 17:53:06 -030058{
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030059 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Andrew de Quincey00be2e72006-09-05 17:53:06 -030060 struct tua6100_priv *priv = fe->tuner_priv;
61 u32 div;
62 u32 prediv;
63 u8 reg0[] = { 0x00, 0x00 };
64 u8 reg1[] = { 0x01, 0x00, 0x00, 0x00 };
65 u8 reg2[] = { 0x02, 0x00, 0x00 };
66 struct i2c_msg msg0 = { .addr = priv->i2c_address, .flags = 0, .buf = reg0, .len = 2 };
67 struct i2c_msg msg1 = { .addr = priv->i2c_address, .flags = 0, .buf = reg1, .len = 4 };
68 struct i2c_msg msg2 = { .addr = priv->i2c_address, .flags = 0, .buf = reg2, .len = 3 };
69
70#define _R 4
71#define _P 32
72#define _ri 4000000
73
74 // setup register 0
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030075 if (c->frequency < 2000000)
Andrew de Quincey00be2e72006-09-05 17:53:06 -030076 reg0[1] = 0x03;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030077 else
Andrew de Quincey00be2e72006-09-05 17:53:06 -030078 reg0[1] = 0x07;
Andrew de Quincey00be2e72006-09-05 17:53:06 -030079
80 // setup register 1
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030081 if (c->frequency < 1630000)
Andrew de Quincey00be2e72006-09-05 17:53:06 -030082 reg1[1] = 0x2c;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030083 else
Andrew de Quincey00be2e72006-09-05 17:53:06 -030084 reg1[1] = 0x0c;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030085
Andrew de Quincey00be2e72006-09-05 17:53:06 -030086 if (_P == 64)
87 reg1[1] |= 0x40;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030088 if (c->frequency >= 1525000)
Andrew de Quincey00be2e72006-09-05 17:53:06 -030089 reg1[1] |= 0x80;
90
91 // register 2
92 reg2[1] = (_R >> 8) & 0x03;
93 reg2[2] = _R;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030094 if (c->frequency < 1455000)
Andrew de Quincey00be2e72006-09-05 17:53:06 -030095 reg2[1] |= 0x1c;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030096 else if (c->frequency < 1630000)
Andrew de Quincey00be2e72006-09-05 17:53:06 -030097 reg2[1] |= 0x0c;
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -030098 else
Andrew de Quincey00be2e72006-09-05 17:53:06 -030099 reg2[1] |= 0x1c;
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300100
Mauro Carvalho Chehab59182882011-12-23 17:56:57 -0300101 /*
102 * The N divisor ratio (note: c->frequency is in kHz, but we
103 * need it in Hz)
104 */
105 prediv = (c->frequency * _R) / (_ri / 1000);
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300106 div = prediv / _P;
107 reg1[1] |= (div >> 9) & 0x03;
108 reg1[2] = div >> 1;
109 reg1[3] = (div << 7);
110 priv->frequency = ((div * _P) * (_ri / 1000)) / _R;
111
112 // Finally, calculate and store the value for A
113 reg1[3] |= (prediv - (div*_P)) & 0x7f;
114
115#undef _R
116#undef _P
117#undef _ri
118
119 if (fe->ops.i2c_gate_ctrl)
120 fe->ops.i2c_gate_ctrl(fe, 1);
121 if (i2c_transfer(priv->i2c, &msg0, 1) != 1)
122 return -EIO;
123
124 if (fe->ops.i2c_gate_ctrl)
125 fe->ops.i2c_gate_ctrl(fe, 1);
126 if (i2c_transfer(priv->i2c, &msg2, 1) != 1)
127 return -EIO;
128
129 if (fe->ops.i2c_gate_ctrl)
130 fe->ops.i2c_gate_ctrl(fe, 1);
131 if (i2c_transfer(priv->i2c, &msg1, 1) != 1)
132 return -EIO;
133
134 if (fe->ops.i2c_gate_ctrl)
135 fe->ops.i2c_gate_ctrl(fe, 0);
136
137 return 0;
138}
139
140static int tua6100_get_frequency(struct dvb_frontend *fe, u32 *frequency)
141{
142 struct tua6100_priv *priv = fe->tuner_priv;
143 *frequency = priv->frequency;
144 return 0;
145}
146
Julia Lawall14c4bf32016-09-11 11:44:12 -0300147static const struct dvb_tuner_ops tua6100_tuner_ops = {
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300148 .info = {
149 .name = "Infineon TUA6100",
Mauro Carvalho Chehaba3f90c72018-07-05 18:59:35 -0400150 .frequency_min_hz = 950 * MHz,
151 .frequency_max_hz = 2150 * MHz,
152 .frequency_step_hz = 1 * MHz,
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300153 },
Mauro Carvalho Chehabf2709c22016-11-18 20:30:51 -0200154 .release = tua6100_release,
Andrew de Quincey00be2e72006-09-05 17:53:06 -0300155 .sleep = tua6100_sleep,
156 .set_params = tua6100_set_params,
157 .get_frequency = tua6100_get_frequency,
158};
159
160struct dvb_frontend *tua6100_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c)
161{
162 struct tua6100_priv *priv = NULL;
163 u8 b1 [] = { 0x80 };
164 u8 b2 [] = { 0x00 };
165 struct i2c_msg msg [] = { { .addr = addr, .flags = 0, .buf = b1, .len = 1 },
166 { .addr = addr, .flags = I2C_M_RD, .buf = b2, .len = 1 } };
167 int ret;
168
169 if (fe->ops.i2c_gate_ctrl)
170 fe->ops.i2c_gate_ctrl(fe, 1);
171 ret = i2c_transfer (i2c, msg, 2);
172 if (fe->ops.i2c_gate_ctrl)
173 fe->ops.i2c_gate_ctrl(fe, 0);
174
175 if (ret != 2)
176 return NULL;
177
178 priv = kzalloc(sizeof(struct tua6100_priv), GFP_KERNEL);
179 if (priv == NULL)
180 return NULL;
181
182 priv->i2c_address = addr;
183 priv->i2c = i2c;
184
185 memcpy(&fe->ops.tuner_ops, &tua6100_tuner_ops, sizeof(struct dvb_tuner_ops));
186 fe->tuner_priv = priv;
187 return fe;
188}
189EXPORT_SYMBOL(tua6100_attach);
190
191MODULE_DESCRIPTION("DVB tua6100 driver");
192MODULE_AUTHOR("Andrew de Quincey");
193MODULE_LICENSE("GPL");