Merge tag 'fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/arm...
[linux-2.6.git] / drivers / media / tuners / tua9001.c
blob83a6240f64d3d17b1a486bb7dbddac50864ee490
1 /*
2 * Infineon TUA 9001 silicon tuner driver
4 * Copyright (C) 2009 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.
21 #include "tua9001.h"
22 #include "tua9001_priv.h"
24 /* write register */
25 static int tua9001_wr_reg(struct tua9001_priv *priv, u8 reg, u16 val)
27 int ret;
28 u8 buf[3] = { reg, (val >> 8) & 0xff, (val >> 0) & 0xff };
29 struct i2c_msg msg[1] = {
31 .addr = priv->cfg->i2c_addr,
32 .flags = 0,
33 .len = sizeof(buf),
34 .buf = buf,
38 ret = i2c_transfer(priv->i2c, msg, 1);
39 if (ret == 1) {
40 ret = 0;
41 } else {
42 dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x\n",
43 KBUILD_MODNAME, ret, reg);
44 ret = -EREMOTEIO;
47 return ret;
50 static int tua9001_release(struct dvb_frontend *fe)
52 struct tua9001_priv *priv = fe->tuner_priv;
53 int ret = 0;
55 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
57 if (fe->callback)
58 ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
59 TUA9001_CMD_CEN, 0);
61 kfree(fe->tuner_priv);
62 fe->tuner_priv = NULL;
64 return ret;
67 static int tua9001_init(struct dvb_frontend *fe)
69 struct tua9001_priv *priv = fe->tuner_priv;
70 int ret = 0;
71 u8 i;
72 struct reg_val data[] = {
73 { 0x1e, 0x6512 },
74 { 0x25, 0xb888 },
75 { 0x39, 0x5460 },
76 { 0x3b, 0x00c0 },
77 { 0x3a, 0xf000 },
78 { 0x08, 0x0000 },
79 { 0x32, 0x0030 },
80 { 0x41, 0x703a },
81 { 0x40, 0x1c78 },
82 { 0x2c, 0x1c00 },
83 { 0x36, 0xc013 },
84 { 0x37, 0x6f18 },
85 { 0x27, 0x0008 },
86 { 0x2a, 0x0001 },
87 { 0x34, 0x0a40 },
90 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
92 if (fe->callback) {
93 ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
94 TUA9001_CMD_RESETN, 0);
95 if (ret < 0)
96 goto err;
99 if (fe->ops.i2c_gate_ctrl)
100 fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */
102 for (i = 0; i < ARRAY_SIZE(data); i++) {
103 ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
104 if (ret < 0)
105 goto err_i2c_gate_ctrl;
108 err_i2c_gate_ctrl:
109 if (fe->ops.i2c_gate_ctrl)
110 fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
111 err:
112 if (ret < 0)
113 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
115 return ret;
118 static int tua9001_sleep(struct dvb_frontend *fe)
120 struct tua9001_priv *priv = fe->tuner_priv;
121 int ret = 0;
123 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
125 if (fe->callback)
126 ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
127 TUA9001_CMD_RESETN, 1);
129 if (ret < 0)
130 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
132 return ret;
135 static int tua9001_set_params(struct dvb_frontend *fe)
137 struct tua9001_priv *priv = fe->tuner_priv;
138 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
139 int ret = 0, i;
140 u16 val;
141 u32 frequency;
142 struct reg_val data[2];
144 dev_dbg(&priv->i2c->dev, "%s: delivery_system=%d frequency=%d " \
145 "bandwidth_hz=%d\n", __func__,
146 c->delivery_system, c->frequency, c->bandwidth_hz);
148 switch (c->delivery_system) {
149 case SYS_DVBT:
150 switch (c->bandwidth_hz) {
151 case 8000000:
152 val = 0x0000;
153 break;
154 case 7000000:
155 val = 0x1000;
156 break;
157 case 6000000:
158 val = 0x2000;
159 break;
160 case 5000000:
161 val = 0x3000;
162 break;
163 default:
164 ret = -EINVAL;
165 goto err;
167 break;
168 default:
169 ret = -EINVAL;
170 goto err;
173 data[0].reg = 0x04;
174 data[0].val = val;
176 frequency = (c->frequency - 150000000);
177 frequency /= 100;
178 frequency *= 48;
179 frequency /= 10000;
181 data[1].reg = 0x1f;
182 data[1].val = frequency;
184 if (fe->ops.i2c_gate_ctrl)
185 fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */
187 if (fe->callback) {
188 ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
189 TUA9001_CMD_RXEN, 0);
190 if (ret < 0)
191 goto err_i2c_gate_ctrl;
194 for (i = 0; i < ARRAY_SIZE(data); i++) {
195 ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
196 if (ret < 0)
197 goto err_i2c_gate_ctrl;
200 if (fe->callback) {
201 ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
202 TUA9001_CMD_RXEN, 1);
203 if (ret < 0)
204 goto err_i2c_gate_ctrl;
207 err_i2c_gate_ctrl:
208 if (fe->ops.i2c_gate_ctrl)
209 fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
210 err:
211 if (ret < 0)
212 dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
214 return ret;
217 static int tua9001_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
219 struct tua9001_priv *priv = fe->tuner_priv;
221 dev_dbg(&priv->i2c->dev, "%s:\n", __func__);
223 *frequency = 0; /* Zero-IF */
225 return 0;
228 static const struct dvb_tuner_ops tua9001_tuner_ops = {
229 .info = {
230 .name = "Infineon TUA 9001",
232 .frequency_min = 170000000,
233 .frequency_max = 862000000,
234 .frequency_step = 0,
237 .release = tua9001_release,
239 .init = tua9001_init,
240 .sleep = tua9001_sleep,
241 .set_params = tua9001_set_params,
243 .get_if_frequency = tua9001_get_if_frequency,
246 struct dvb_frontend *tua9001_attach(struct dvb_frontend *fe,
247 struct i2c_adapter *i2c, struct tua9001_config *cfg)
249 struct tua9001_priv *priv = NULL;
250 int ret;
252 priv = kzalloc(sizeof(struct tua9001_priv), GFP_KERNEL);
253 if (priv == NULL)
254 return NULL;
256 priv->cfg = cfg;
257 priv->i2c = i2c;
259 if (fe->callback) {
260 ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
261 TUA9001_CMD_CEN, 1);
262 if (ret < 0)
263 goto err;
265 ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
266 TUA9001_CMD_RXEN, 0);
267 if (ret < 0)
268 goto err;
270 ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
271 TUA9001_CMD_RESETN, 1);
272 if (ret < 0)
273 goto err;
276 dev_info(&priv->i2c->dev,
277 "%s: Infineon TUA 9001 successfully attached\n",
278 KBUILD_MODNAME);
280 memcpy(&fe->ops.tuner_ops, &tua9001_tuner_ops,
281 sizeof(struct dvb_tuner_ops));
283 fe->tuner_priv = priv;
284 return fe;
285 err:
286 dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
287 kfree(priv);
288 return NULL;
290 EXPORT_SYMBOL(tua9001_attach);
292 MODULE_DESCRIPTION("Infineon TUA 9001 silicon tuner driver");
293 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
294 MODULE_LICENSE("GPL");