tua9001.c 8.42 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*
 * Infineon TUA 9001 silicon tuner driver
 *
 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "tua9001.h"
#include "tua9001_priv.h"

/* write register */
static int tua9001_wr_reg(struct tua9001_priv *priv, u8 reg, u16 val)
{
	int ret;
	u8 buf[3] = { reg, (val >> 8) & 0xff, (val >> 0) & 0xff };
	struct i2c_msg msg[1] = {
		{
31
			.addr = priv->i2c_addr,
32 33 34 35 36 37 38 39 40 41
			.flags = 0,
			.len = sizeof(buf),
			.buf = buf,
		}
	};

	ret = i2c_transfer(priv->i2c, msg, 1);
	if (ret == 1) {
		ret = 0;
	} else {
42 43
		dev_warn(&priv->i2c->dev, "%s: i2c wr failed=%d reg=%02x\n",
				KBUILD_MODNAME, ret, reg);
44 45 46 47 48 49 50 51
		ret = -EREMOTEIO;
	}

	return ret;
}

static int tua9001_release(struct dvb_frontend *fe)
{
52 53 54
	struct tua9001_priv *priv = fe->tuner_priv;
	int ret = 0;

55 56
	dev_dbg(&priv->i2c->dev, "%s:\n", __func__);

57 58 59 60
	if (fe->callback)
		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
				TUA9001_CMD_CEN, 0);

61 62 63
	kfree(fe->tuner_priv);
	fe->tuner_priv = NULL;

64
	return ret;
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
}

static int tua9001_init(struct dvb_frontend *fe)
{
	struct tua9001_priv *priv = fe->tuner_priv;
	int ret = 0;
	u8 i;
	struct reg_val data[] = {
		{ 0x1e, 0x6512 },
		{ 0x25, 0xb888 },
		{ 0x39, 0x5460 },
		{ 0x3b, 0x00c0 },
		{ 0x3a, 0xf000 },
		{ 0x08, 0x0000 },
		{ 0x32, 0x0030 },
		{ 0x41, 0x703a },
		{ 0x40, 0x1c78 },
		{ 0x2c, 0x1c00 },
		{ 0x36, 0xc013 },
		{ 0x37, 0x6f18 },
		{ 0x27, 0x0008 },
		{ 0x2a, 0x0001 },
		{ 0x34, 0x0a40 },
	};

90 91
	dev_dbg(&priv->i2c->dev, "%s:\n", __func__);

92 93 94 95 96 97 98
	if (fe->callback) {
		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
				TUA9001_CMD_RESETN, 0);
		if (ret < 0)
			goto err;
	}

99 100 101 102 103
	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */

	for (i = 0; i < ARRAY_SIZE(data); i++) {
		ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
104 105
		if (ret < 0)
			goto err_i2c_gate_ctrl;
106 107
	}

108
err_i2c_gate_ctrl:
109 110
	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
111 112
err:
	if (ret < 0)
113
		dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
114 115 116 117 118 119 120 121 122

	return ret;
}

static int tua9001_sleep(struct dvb_frontend *fe)
{
	struct tua9001_priv *priv = fe->tuner_priv;
	int ret = 0;

123 124
	dev_dbg(&priv->i2c->dev, "%s:\n", __func__);

125 126 127
	if (fe->callback)
		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
				TUA9001_CMD_RESETN, 1);
128 129

	if (ret < 0)
130
		dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
131 132 133 134 135 136 137 138

	return ret;
}

static int tua9001_set_params(struct dvb_frontend *fe)
{
	struct tua9001_priv *priv = fe->tuner_priv;
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
139
	int ret = 0, i;
140 141 142 143
	u16 val;
	u32 frequency;
	struct reg_val data[2];

144 145 146
	dev_dbg(&priv->i2c->dev, "%s: delivery_system=%d frequency=%d " \
			"bandwidth_hz=%d\n", __func__,
			c->delivery_system, c->frequency, c->bandwidth_hz);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

	switch (c->delivery_system) {
	case SYS_DVBT:
		switch (c->bandwidth_hz) {
		case 8000000:
			val  = 0x0000;
			break;
		case 7000000:
			val  = 0x1000;
			break;
		case 6000000:
			val  = 0x2000;
			break;
		case 5000000:
			val  = 0x3000;
			break;
		default:
			ret = -EINVAL;
			goto err;
		}
		break;
	default:
		ret = -EINVAL;
		goto err;
	}

	data[0].reg = 0x04;
	data[0].val = val;

	frequency = (c->frequency - 150000000);
	frequency /= 100;
	frequency *= 48;
	frequency /= 10000;

	data[1].reg = 0x1f;
	data[1].val = frequency;

	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 1); /* open i2c-gate */

187 188 189 190 191 192 193
	if (fe->callback) {
		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
				TUA9001_CMD_RXEN, 0);
		if (ret < 0)
			goto err_i2c_gate_ctrl;
	}

194 195 196
	for (i = 0; i < ARRAY_SIZE(data); i++) {
		ret = tua9001_wr_reg(priv, data[i].reg, data[i].val);
		if (ret < 0)
197 198 199 200 201 202 203 204
			goto err_i2c_gate_ctrl;
	}

	if (fe->callback) {
		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
				TUA9001_CMD_RXEN, 1);
		if (ret < 0)
			goto err_i2c_gate_ctrl;
205 206
	}

207
err_i2c_gate_ctrl:
208 209 210 211
	if (fe->ops.i2c_gate_ctrl)
		fe->ops.i2c_gate_ctrl(fe, 0); /* close i2c-gate */
err:
	if (ret < 0)
212
		dev_dbg(&priv->i2c->dev, "%s: failed=%d\n", __func__, ret);
213 214 215 216 217 218

	return ret;
}

static int tua9001_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
{
219 220 221 222
	struct tua9001_priv *priv = fe->tuner_priv;

	dev_dbg(&priv->i2c->dev, "%s:\n", __func__);

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	*frequency = 0; /* Zero-IF */

	return 0;
}

static const struct dvb_tuner_ops tua9001_tuner_ops = {
	.info = {
		.name           = "Infineon TUA 9001",

		.frequency_min  = 170000000,
		.frequency_max  = 862000000,
		.frequency_step = 0,
	},

	.release = tua9001_release,

	.init = tua9001_init,
240
	.sleep = tua9001_sleep,
241 242 243 244 245 246 247 248 249
	.set_params = tua9001_set_params,

	.get_if_frequency = tua9001_get_if_frequency,
};

struct dvb_frontend *tua9001_attach(struct dvb_frontend *fe,
		struct i2c_adapter *i2c, struct tua9001_config *cfg)
{
	struct tua9001_priv *priv = NULL;
250
	int ret;
251 252 253 254 255

	priv = kzalloc(sizeof(struct tua9001_priv), GFP_KERNEL);
	if (priv == NULL)
		return NULL;

256
	priv->i2c_addr = cfg->i2c_addr;
257 258
	priv->i2c = i2c;

259 260 261 262 263
	if (fe->callback) {
		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
				TUA9001_CMD_CEN, 1);
		if (ret < 0)
			goto err;
264 265 266 267 268 269 270 271 272 273

		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
				TUA9001_CMD_RXEN, 0);
		if (ret < 0)
			goto err;

		ret = fe->callback(priv->i2c, DVB_FRONTEND_COMPONENT_TUNER,
				TUA9001_CMD_RESETN, 1);
		if (ret < 0)
			goto err;
274 275
	}

276 277 278
	dev_info(&priv->i2c->dev,
			"%s: Infineon TUA 9001 successfully attached\n",
			KBUILD_MODNAME);
279 280 281 282 283 284

	memcpy(&fe->ops.tuner_ops, &tua9001_tuner_ops,
			sizeof(struct dvb_tuner_ops));

	fe->tuner_priv = priv;
	return fe;
285
err:
286
	dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
287 288
	kfree(priv);
	return NULL;
289 290 291
}
EXPORT_SYMBOL(tua9001_attach);

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
static int tua9001_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
	struct tua9001_priv *dev;
	struct tua9001_platform_data *pdata = client->dev.platform_data;
	struct dvb_frontend *fe = pdata->dvb_frontend;
	int ret;

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (!dev) {
		ret = -ENOMEM;
		goto err;
	}

	dev->client = client;
	dev->i2c_addr = client->addr;
	dev->i2c = client->adapter;
	dev->fe = pdata->dvb_frontend;

	if (fe->callback) {
		ret = fe->callback(client->adapter,
				   DVB_FRONTEND_COMPONENT_TUNER,
				   TUA9001_CMD_CEN, 1);
		if (ret)
			goto err_kfree;

		ret = fe->callback(client->adapter,
				   DVB_FRONTEND_COMPONENT_TUNER,
				   TUA9001_CMD_RXEN, 0);
		if (ret)
			goto err_kfree;

		ret = fe->callback(client->adapter,
				   DVB_FRONTEND_COMPONENT_TUNER,
				   TUA9001_CMD_RESETN, 1);
		if (ret)
			goto err_kfree;
	}

	fe->tuner_priv = dev;
	memcpy(&fe->ops.tuner_ops, &tua9001_tuner_ops,
			sizeof(struct dvb_tuner_ops));
	fe->ops.tuner_ops.release = NULL;
	i2c_set_clientdata(client, dev);

	dev_info(&client->dev, "Infineon TUA 9001 successfully attached\n");
	return 0;
err_kfree:
	kfree(dev);
err:
	dev_dbg(&client->dev, "failed=%d\n", ret);
	return ret;
}

static int tua9001_remove(struct i2c_client *client)
{
	struct tua9001_priv *dev = i2c_get_clientdata(client);
	struct dvb_frontend *fe = dev->fe;
	int ret;

	dev_dbg(&client->dev, "\n");

	if (fe->callback) {
		ret = fe->callback(client->adapter,
				   DVB_FRONTEND_COMPONENT_TUNER,
				   TUA9001_CMD_CEN, 0);
		if (ret)
			goto err_kfree;
	}
	kfree(dev);
	return 0;
err_kfree:
	kfree(dev);
	dev_dbg(&client->dev, "failed=%d\n", ret);
	return ret;
}

static const struct i2c_device_id tua9001_id_table[] = {
	{"tua9001", 0},
	{}
};
MODULE_DEVICE_TABLE(i2c, tua9001_id_table);

static struct i2c_driver tua9001_driver = {
	.driver = {
		.owner	= THIS_MODULE,
		.name	= "tua9001",
		.suppress_bind_attrs = true,
	},
	.probe		= tua9001_probe,
	.remove		= tua9001_remove,
	.id_table	= tua9001_id_table,
};

module_i2c_driver(tua9001_driver);

388 389 390
MODULE_DESCRIPTION("Infineon TUA 9001 silicon tuner driver");
MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_LICENSE("GPL");