tua9001.c 6.1 KB
Newer Older
1
/*
2
 * Infineon TUA9001 silicon tuner driver
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * 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.
 */

#include "tua9001_priv.h"

static int tua9001_init(struct dvb_frontend *fe)
{
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
	struct tua9001_dev *dev = fe->tuner_priv;
	struct i2c_client *client = dev->client;
	int ret, i;
	static const struct tua9001_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},
40 41
	};

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

44
	if (fe->callback) {
45 46 47 48
		ret = fe->callback(client->adapter,
				   DVB_FRONTEND_COMPONENT_TUNER,
				   TUA9001_CMD_RESETN, 0);
		if (ret)
49 50 51
			goto err;
	}

52
	for (i = 0; i < ARRAY_SIZE(data); i++) {
53
		ret = regmap_write(dev->regmap, data[i].reg, data[i].val);
54
		if (ret)
55
			goto err;
56
	}
57
	return 0;
58
err:
59
	dev_dbg(&client->dev, "failed=%d\n", ret);
60 61 62 63 64
	return ret;
}

static int tua9001_sleep(struct dvb_frontend *fe)
{
65 66 67
	struct tua9001_dev *dev = fe->tuner_priv;
	struct i2c_client *client = dev->client;
	int ret;
68

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

71 72 73 74 75 76 77 78 79 80
	if (fe->callback) {
		ret = fe->callback(client->adapter,
				   DVB_FRONTEND_COMPONENT_TUNER,
				   TUA9001_CMD_RESETN, 1);
		if (ret)
			goto err;
	}
	return 0;
err:
	dev_dbg(&client->dev, "failed=%d\n", ret);
81 82 83 84 85
	return ret;
}

static int tua9001_set_params(struct dvb_frontend *fe)
{
86 87
	struct tua9001_dev *dev = fe->tuner_priv;
	struct i2c_client *client = dev->client;
88
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
89
	int ret, i;
90 91
	u16 val;
	u32 frequency;
92
	struct tua9001_reg_val data[2];
93

94 95 96
	dev_dbg(&client->dev,
		"delivery_system=%u frequency=%u bandwidth_hz=%u\n",
		c->delivery_system, c->frequency, c->bandwidth_hz);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

	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;

134
	if (fe->callback) {
135 136 137 138
		ret = fe->callback(client->adapter,
				   DVB_FRONTEND_COMPONENT_TUNER,
				   TUA9001_CMD_RXEN, 0);
		if (ret)
139
			goto err;
140 141
	}

142
	for (i = 0; i < ARRAY_SIZE(data); i++) {
143
		ret = regmap_write(dev->regmap, data[i].reg, data[i].val);
144
		if (ret)
145
			goto err;
146 147 148
	}

	if (fe->callback) {
149 150 151 152
		ret = fe->callback(client->adapter,
				   DVB_FRONTEND_COMPONENT_TUNER,
				   TUA9001_CMD_RXEN, 1);
		if (ret)
153
			goto err;
154
	}
155
	return 0;
156
err:
157
	dev_dbg(&client->dev, "failed=%d\n", ret);
158 159 160 161 162
	return ret;
}

static int tua9001_get_if_frequency(struct dvb_frontend *fe, u32 *frequency)
{
163 164
	struct tua9001_dev *dev = fe->tuner_priv;
	struct i2c_client *client = dev->client;
165

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

168 169 170 171 172 173
	*frequency = 0; /* Zero-IF */
	return 0;
}

static const struct dvb_tuner_ops tua9001_tuner_ops = {
	.info = {
174
		.name           = "Infineon TUA9001",
175 176 177 178 179
		.frequency_min  = 170000000,
		.frequency_max  = 862000000,
	},

	.init = tua9001_init,
180
	.sleep = tua9001_sleep,
181 182 183 184 185
	.set_params = tua9001_set_params,

	.get_if_frequency = tua9001_get_if_frequency,
};

186 187 188
static int tua9001_probe(struct i2c_client *client,
			const struct i2c_device_id *id)
{
189
	struct tua9001_dev *dev;
190 191 192
	struct tua9001_platform_data *pdata = client->dev.platform_data;
	struct dvb_frontend *fe = pdata->dvb_frontend;
	int ret;
193 194 195 196
	static const struct regmap_config regmap_config = {
		.reg_bits =  8,
		.val_bits = 16,
	};
197 198 199 200 201 202 203 204

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

	dev->fe = pdata->dvb_frontend;
205 206 207 208 209 210
	dev->client = client;
	dev->regmap = devm_regmap_init_i2c(client, &regmap_config);
	if (IS_ERR(dev->regmap)) {
		ret = PTR_ERR(dev->regmap);
		goto err_kfree;
	}
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

	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));
	i2c_set_clientdata(client, dev);

237
	dev_info(&client->dev, "Infineon TUA9001 successfully attached\n");
238 239 240 241 242 243 244 245 246 247
	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)
{
248
	struct tua9001_dev *dev = i2c_get_clientdata(client);
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
	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);

288
MODULE_DESCRIPTION("Infineon TUA9001 silicon tuner driver");
289 290
MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_LICENSE("GPL");