mn88473.c 12.1 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
/*
 * Panasonic MN88473 DVB-T/T2/C demodulator driver
 *
 * Copyright (C) 2014 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 "mn88473_priv.h"

static int mn88473_get_tune_settings(struct dvb_frontend *fe,
	struct dvb_frontend_tune_settings *s)
{
	s->min_delay_ms = 1000;
	return 0;
}

static int mn88473_set_frontend(struct dvb_frontend *fe)
{
28 29
	struct i2c_client *client = fe->demodulator_priv;
	struct mn88473_dev *dev = i2c_get_clientdata(client);
30
	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
31
	int ret, i;
32 33
	u32 if_frequency;
	u8 delivery_system_val, if_val[3], bw_val[7];
34

35 36 37
	dev_dbg(&client->dev,
			"delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%d stream_id=%d\n",
			c->delivery_system, c->modulation,
38 39
			c->frequency, c->bandwidth_hz, c->symbol_rate,
			c->inversion, c->stream_id);
40 41 42 43 44 45

	if (!dev->warm) {
		ret = -EAGAIN;
		goto err;
	}

46
	switch (c->delivery_system) {
47
	case SYS_DVBT:
48
		delivery_system_val = 0x02;
49
		break;
50
	case SYS_DVBT2:
51
		delivery_system_val = 0x03;
52 53
		break;
	case SYS_DVBC_ANNEX_A:
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
		delivery_system_val = 0x04;
		break;
	default:
		ret = -EINVAL;
		goto err;
	}

	switch (c->delivery_system) {
	case SYS_DVBT:
	case SYS_DVBT2:
		if (c->bandwidth_hz <= 6000000) {
			/* IF 3570000 Hz, BW 6000000 Hz */
			memcpy(if_val, "\x24\x8e\x8a", 3);
			memcpy(bw_val, "\xe9\x55\x55\x1c\x29\x1c\x29", 7);
		} else if (c->bandwidth_hz <= 7000000) {
			/* IF 4570000 Hz, BW 7000000 Hz */
			memcpy(if_val, "\x2e\xcb\xfb", 3);
			memcpy(bw_val, "\xc8\x00\x00\x17\x0a\x17\x0a", 7);
		} else if (c->bandwidth_hz <= 8000000) {
			/* IF 4570000 Hz, BW 8000000 Hz */
			memcpy(if_val, "\x2e\xcb\xfb", 3);
			memcpy(bw_val, "\xaf\x00\x00\x11\xec\x11\xec", 7);
		} else {
			ret = -EINVAL;
			goto err;
		}
		break;
	case SYS_DVBC_ANNEX_A:
		/* IF 5070000 Hz, BW 8000000 Hz */
		memcpy(if_val, "\x33\xea\xb3", 3);
		memcpy(bw_val, "\xaf\x00\x00\x11\xec\x11\xec", 7);
85 86 87 88 89 90
		break;
	default:
		ret = -EINVAL;
		goto err;
	}

91 92 93 94 95 96 97 98 99 100 101 102
	/* program tuner */
	if (fe->ops.tuner_ops.set_params) {
		ret = fe->ops.tuner_ops.set_params(fe);
		if (ret)
			goto err;
	}

	if (fe->ops.tuner_ops.get_if_frequency) {
		ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
		if (ret)
			goto err;

103
		dev_dbg(&client->dev, "get_if_frequency=%d\n", if_frequency);
104 105
	} else {
		if_frequency = 0;
106 107
	}

108
	switch (if_frequency) {
109
	case 3570000:
110 111 112 113
	case 4570000:
	case 5070000:
		break;
	default:
114 115
		dev_err(&client->dev, "IF frequency %d not supported\n",
				if_frequency);
116 117 118 119
		ret = -EINVAL;
		goto err;
	}

120 121 122 123 124 125 126 127 128
	ret = regmap_write(dev->regmap[2], 0x05, 0x00);
	ret = regmap_write(dev->regmap[2], 0xfb, 0x13);
	ret = regmap_write(dev->regmap[2], 0xef, 0x13);
	ret = regmap_write(dev->regmap[2], 0xf9, 0x13);
	ret = regmap_write(dev->regmap[2], 0x00, 0x18);
	ret = regmap_write(dev->regmap[2], 0x01, 0x01);
	ret = regmap_write(dev->regmap[2], 0x02, 0x21);
	ret = regmap_write(dev->regmap[2], 0x03, delivery_system_val);
	ret = regmap_write(dev->regmap[2], 0x0b, 0x00);
129

130
	for (i = 0; i < sizeof(if_val); i++) {
131
		ret = regmap_write(dev->regmap[2], 0x10 + i, if_val[i]);
132 133 134 135 136
		if (ret)
			goto err;
	}

	for (i = 0; i < sizeof(bw_val); i++) {
137
		ret = regmap_write(dev->regmap[2], 0x13 + i, bw_val[i]);
138 139 140 141
		if (ret)
			goto err;
	}

142 143 144 145 146 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
	ret = regmap_write(dev->regmap[2], 0x2d, 0x3b);
	ret = regmap_write(dev->regmap[2], 0x2e, 0x00);
	ret = regmap_write(dev->regmap[2], 0x56, 0x0d);
	ret = regmap_write(dev->regmap[0], 0x01, 0xba);
	ret = regmap_write(dev->regmap[0], 0x02, 0x13);
	ret = regmap_write(dev->regmap[0], 0x03, 0x80);
	ret = regmap_write(dev->regmap[0], 0x04, 0xba);
	ret = regmap_write(dev->regmap[0], 0x05, 0x91);
	ret = regmap_write(dev->regmap[0], 0x07, 0xe7);
	ret = regmap_write(dev->regmap[0], 0x08, 0x28);
	ret = regmap_write(dev->regmap[0], 0x0a, 0x1a);
	ret = regmap_write(dev->regmap[0], 0x13, 0x1f);
	ret = regmap_write(dev->regmap[0], 0x19, 0x03);
	ret = regmap_write(dev->regmap[0], 0x1d, 0xb0);
	ret = regmap_write(dev->regmap[0], 0x2a, 0x72);
	ret = regmap_write(dev->regmap[0], 0x2d, 0x00);
	ret = regmap_write(dev->regmap[0], 0x3c, 0x00);
	ret = regmap_write(dev->regmap[0], 0x3f, 0xf8);
	ret = regmap_write(dev->regmap[0], 0x40, 0xf4);
	ret = regmap_write(dev->regmap[0], 0x41, 0x08);
	ret = regmap_write(dev->regmap[0], 0xd2, 0x29);
	ret = regmap_write(dev->regmap[0], 0xd4, 0x55);
	ret = regmap_write(dev->regmap[1], 0x10, 0x10);
	ret = regmap_write(dev->regmap[1], 0x11, 0xab);
	ret = regmap_write(dev->regmap[1], 0x12, 0x0d);
	ret = regmap_write(dev->regmap[1], 0x13, 0xae);
	ret = regmap_write(dev->regmap[1], 0x14, 0x1d);
	ret = regmap_write(dev->regmap[1], 0x15, 0x9d);
	ret = regmap_write(dev->regmap[1], 0xbe, 0x08);
	ret = regmap_write(dev->regmap[2], 0x09, 0x08);
	ret = regmap_write(dev->regmap[2], 0x08, 0x1d);
	ret = regmap_write(dev->regmap[0], 0xb2, 0x37);
	ret = regmap_write(dev->regmap[0], 0xd7, 0x04);
	ret = regmap_write(dev->regmap[2], 0x32, 0x80);
	ret = regmap_write(dev->regmap[2], 0x36, 0x00);
	ret = regmap_write(dev->regmap[2], 0xf8, 0x9f);
178 179 180 181 182 183 184
	if (ret)
		goto err;

	dev->delivery_system = c->delivery_system;

	return 0;
err:
185
	dev_dbg(&client->dev, "failed=%d\n", ret);
186 187 188 189 190
	return ret;
}

static int mn88473_read_status(struct dvb_frontend *fe, fe_status_t *status)
{
191 192
	struct i2c_client *client = fe->demodulator_priv;
	struct mn88473_dev *dev = i2c_get_clientdata(client);
193 194 195 196 197 198 199 200 201 202 203 204 205 206
	int ret;

	*status = 0;

	if (!dev->warm) {
		ret = -EAGAIN;
		goto err;
	}

	*status = FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
			FE_HAS_SYNC | FE_HAS_LOCK;

	return 0;
err:
207
	dev_dbg(&client->dev, "failed=%d\n", ret);
208 209 210 211 212
	return ret;
}

static int mn88473_init(struct dvb_frontend *fe)
{
213 214
	struct i2c_client *client = fe->demodulator_priv;
	struct mn88473_dev *dev = i2c_get_clientdata(client);
215 216 217 218
	int ret, len, remaining;
	const struct firmware *fw = NULL;
	u8 *fw_file = MN88473_FIRMWARE;

219
	dev_dbg(&client->dev, "\n");
220 221 222 223 224

	if (dev->warm)
		return 0;

	/* request the firmware, this will block and timeout */
225
	ret = request_firmware(&fw, fw_file, &client->dev);
226
	if (ret) {
227
		dev_err(&client->dev, "firmare file '%s' not found\n", fw_file);
228 229 230
		goto err;
	}

231 232
	dev_info(&client->dev, "downloading firmware from file '%s'\n",
			fw_file);
233

234
	ret = regmap_write(dev->regmap[0], 0xf5, 0x03);
235 236 237 238
	if (ret)
		goto err;

	for (remaining = fw->size; remaining > 0;
239
			remaining -= (dev->i2c_wr_max - 1)) {
240
		len = remaining;
241 242
		if (len > (dev->i2c_wr_max - 1))
			len = (dev->i2c_wr_max - 1);
243

244
		ret = regmap_bulk_write(dev->regmap[0], 0xf6,
245 246
				&fw->data[fw->size - remaining], len);
		if (ret) {
247 248
			dev_err(&client->dev, "firmware download failed=%d\n",
					ret);
249 250 251 252
			goto err;
		}
	}

253
	ret = regmap_write(dev->regmap[0], 0xf5, 0x00);
254 255 256 257 258 259 260 261 262 263 264 265 266 267
	if (ret)
		goto err;

	release_firmware(fw);
	fw = NULL;

	/* warm state */
	dev->warm = true;

	return 0;
err:
	if (fw)
		release_firmware(fw);

268
	dev_dbg(&client->dev, "failed=%d\n", ret);
269 270 271 272 273
	return ret;
}

static int mn88473_sleep(struct dvb_frontend *fe)
{
274 275
	struct i2c_client *client = fe->demodulator_priv;
	struct mn88473_dev *dev = i2c_get_clientdata(client);
276 277
	int ret;

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

280
	ret = regmap_write(dev->regmap[2], 0x05, 0x3e);
281 282 283 284 285 286 287
	if (ret)
		goto err;

	dev->delivery_system = SYS_UNDEFINED;

	return 0;
err:
288
	dev_dbg(&client->dev, "failed=%d\n", ret);
289 290 291
	return ret;
}

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
static struct dvb_frontend_ops mn88473_ops = {
	.delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_AC},
	.info = {
		.name = "Panasonic MN88473",
		.caps =	FE_CAN_FEC_1_2                 |
			FE_CAN_FEC_2_3                 |
			FE_CAN_FEC_3_4                 |
			FE_CAN_FEC_5_6                 |
			FE_CAN_FEC_7_8                 |
			FE_CAN_FEC_AUTO                |
			FE_CAN_QPSK                    |
			FE_CAN_QAM_16                  |
			FE_CAN_QAM_32                  |
			FE_CAN_QAM_64                  |
			FE_CAN_QAM_128                 |
			FE_CAN_QAM_256                 |
			FE_CAN_QAM_AUTO                |
			FE_CAN_TRANSMISSION_MODE_AUTO  |
			FE_CAN_GUARD_INTERVAL_AUTO     |
			FE_CAN_HIERARCHY_AUTO          |
			FE_CAN_MUTE_TS                 |
			FE_CAN_2G_MODULATION           |
			FE_CAN_MULTISTREAM
	},

	.get_tune_settings = mn88473_get_tune_settings,

	.init = mn88473_init,
	.sleep = mn88473_sleep,
321

322 323 324 325 326 327 328
	.set_frontend = mn88473_set_frontend,

	.read_status = mn88473_read_status,
};

static int mn88473_probe(struct i2c_client *client,
		const struct i2c_device_id *id)
329
{
330
	struct mn88473_config *config = client->dev.platform_data;
331
	struct mn88473_dev *dev;
332
	int ret;
333 334 335 336 337
	unsigned int utmp;
	static const struct regmap_config regmap_config = {
		.reg_bits = 8,
		.val_bits = 8,
	};
338

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

341 342 343 344 345 346 347 348 349
	/* Caller really need to provide pointer for frontend we create. */
	if (config->fe == NULL) {
		dev_err(&client->dev, "frontend pointer not defined\n");
		ret = -EINVAL;
		goto err;
	}

	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
	if (dev == NULL) {
350 351 352 353
		ret = -ENOMEM;
		goto err;
	}

354
	dev->i2c_wr_max = config->i2c_wr_max;
355 356 357 358 359 360
	dev->client[0] = client;
	dev->regmap[0] = regmap_init_i2c(dev->client[0], &regmap_config);
	if (IS_ERR(dev->regmap[0])) {
		ret = PTR_ERR(dev->regmap[0]);
		goto err_kfree;
	}
361

362
	/* check demod answers to I2C */
363
	ret = regmap_read(dev->regmap[0], 0x00, &utmp);
364
	if (ret)
365
		goto err_regmap_0_regmap_exit;
366 367 368 369 370 371 372 373 374 375 376

	/*
	 * Chip has three I2C addresses for different register pages. Used
	 * addresses are 0x18, 0x1a and 0x1c. We register two dummy clients,
	 * 0x1a and 0x1c, in order to get own I2C client for each register page.
	 */
	dev->client[1] = i2c_new_dummy(client->adapter, 0x1a);
	if (dev->client[1] == NULL) {
		ret = -ENODEV;
		dev_err(&client->dev, "I2C registration failed\n");
		if (ret)
377 378 379 380 381 382
			goto err_regmap_0_regmap_exit;
	}
	dev->regmap[1] = regmap_init_i2c(dev->client[1], &regmap_config);
	if (IS_ERR(dev->regmap[1])) {
		ret = PTR_ERR(dev->regmap[1]);
		goto err_client_1_i2c_unregister_device;
383 384 385 386 387 388 389 390
	}
	i2c_set_clientdata(dev->client[1], dev);

	dev->client[2] = i2c_new_dummy(client->adapter, 0x1c);
	if (dev->client[2] == NULL) {
		ret = -ENODEV;
		dev_err(&client->dev, "2nd I2C registration failed\n");
		if (ret)
391 392 393 394 395 396
			goto err_regmap_1_regmap_exit;
	}
	dev->regmap[2] = regmap_init_i2c(dev->client[2], &regmap_config);
	if (IS_ERR(dev->regmap[2])) {
		ret = PTR_ERR(dev->regmap[2]);
		goto err_client_2_i2c_unregister_device;
397 398
	}
	i2c_set_clientdata(dev->client[2], dev);
399 400 401

	/* create dvb_frontend */
	memcpy(&dev->fe.ops, &mn88473_ops, sizeof(struct dvb_frontend_ops));
402 403 404
	dev->fe.demodulator_priv = client;
	*config->fe = &dev->fe;
	i2c_set_clientdata(client, dev);
405

406 407 408
	dev_info(&dev->client[0]->dev, "Panasonic MN88473 successfully attached\n");
	return 0;

409 410 411 412
err_client_2_i2c_unregister_device:
	i2c_unregister_device(dev->client[2]);
err_regmap_1_regmap_exit:
	regmap_exit(dev->regmap[1]);
413 414
err_client_1_i2c_unregister_device:
	i2c_unregister_device(dev->client[1]);
415 416
err_regmap_0_regmap_exit:
	regmap_exit(dev->regmap[0]);
417
err_kfree:
418
	kfree(dev);
419 420 421
err:
	dev_dbg(&client->dev, "failed=%d\n", ret);
	return ret;
422 423
}

424 425 426
static int mn88473_remove(struct i2c_client *client)
{
	struct mn88473_dev *dev = i2c_get_clientdata(client);
427

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

430
	regmap_exit(dev->regmap[2]);
431
	i2c_unregister_device(dev->client[2]);
432 433

	regmap_exit(dev->regmap[1]);
434
	i2c_unregister_device(dev->client[1]);
435

436 437
	regmap_exit(dev->regmap[0]);

438
	kfree(dev);
439

440 441
	return 0;
}
442

443 444 445 446 447 448 449 450 451 452 453 454 455 456
static const struct i2c_device_id mn88473_id_table[] = {
	{"mn88473", 0},
	{}
};
MODULE_DEVICE_TABLE(i2c, mn88473_id_table);

static struct i2c_driver mn88473_driver = {
	.driver = {
		.owner	= THIS_MODULE,
		.name	= "mn88473",
	},
	.probe		= mn88473_probe,
	.remove		= mn88473_remove,
	.id_table	= mn88473_id_table,
457 458
};

459 460
module_i2c_driver(mn88473_driver);

461 462 463 464
MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
MODULE_DESCRIPTION("Panasonic MN88473 DVB-T/T2/C demodulator driver");
MODULE_LICENSE("GPL");
MODULE_FIRMWARE(MN88473_FIRMWARE);