18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Driver for Microtune MT2266 "Direct conversion low power broadband tuner"
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Copyright (c) 2007 Olivier DANET <odanet@caramail.com>
68c2ecf20Sopenharmony_ci */
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci#include <linux/module.h>
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/dvb/frontend.h>
118c2ecf20Sopenharmony_ci#include <linux/i2c.h>
128c2ecf20Sopenharmony_ci#include <linux/slab.h>
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#include <media/dvb_frontend.h>
158c2ecf20Sopenharmony_ci#include "mt2266.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#define I2C_ADDRESS 0x60
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#define REG_PART_REV   0
208c2ecf20Sopenharmony_ci#define REG_TUNE       1
218c2ecf20Sopenharmony_ci#define REG_BAND       6
228c2ecf20Sopenharmony_ci#define REG_BANDWIDTH  8
238c2ecf20Sopenharmony_ci#define REG_LOCK       0x12
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#define PART_REV 0x85
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_cistruct mt2266_priv {
288c2ecf20Sopenharmony_ci	struct mt2266_config *cfg;
298c2ecf20Sopenharmony_ci	struct i2c_adapter   *i2c;
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci	u32 frequency;
328c2ecf20Sopenharmony_ci	u32 bandwidth;
338c2ecf20Sopenharmony_ci	u8 band;
348c2ecf20Sopenharmony_ci};
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define MT2266_VHF 1
378c2ecf20Sopenharmony_ci#define MT2266_UHF 0
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci/* Here, frequencies are expressed in kiloHertz to avoid 32 bits overflows */
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_cistatic int debug;
428c2ecf20Sopenharmony_cimodule_param(debug, int, 0644);
438c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Turn on/off debugging (default:off).");
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#define dprintk(args...) do { if (debug) {printk(KERN_DEBUG "MT2266: " args); printk("\n"); }} while (0)
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci// Reads a single register
488c2ecf20Sopenharmony_cistatic int mt2266_readreg(struct mt2266_priv *priv, u8 reg, u8 *val)
498c2ecf20Sopenharmony_ci{
508c2ecf20Sopenharmony_ci	struct i2c_msg msg[2] = {
518c2ecf20Sopenharmony_ci		{ .addr = priv->cfg->i2c_address, .flags = 0,        .buf = &reg, .len = 1 },
528c2ecf20Sopenharmony_ci		{ .addr = priv->cfg->i2c_address, .flags = I2C_M_RD, .buf = val,  .len = 1 },
538c2ecf20Sopenharmony_ci	};
548c2ecf20Sopenharmony_ci	if (i2c_transfer(priv->i2c, msg, 2) != 2) {
558c2ecf20Sopenharmony_ci		printk(KERN_WARNING "MT2266 I2C read failed\n");
568c2ecf20Sopenharmony_ci		return -EREMOTEIO;
578c2ecf20Sopenharmony_ci	}
588c2ecf20Sopenharmony_ci	return 0;
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci// Writes a single register
628c2ecf20Sopenharmony_cistatic int mt2266_writereg(struct mt2266_priv *priv, u8 reg, u8 val)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	u8 buf[2] = { reg, val };
658c2ecf20Sopenharmony_ci	struct i2c_msg msg = {
668c2ecf20Sopenharmony_ci		.addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = 2
678c2ecf20Sopenharmony_ci	};
688c2ecf20Sopenharmony_ci	if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
698c2ecf20Sopenharmony_ci		printk(KERN_WARNING "MT2266 I2C write failed\n");
708c2ecf20Sopenharmony_ci		return -EREMOTEIO;
718c2ecf20Sopenharmony_ci	}
728c2ecf20Sopenharmony_ci	return 0;
738c2ecf20Sopenharmony_ci}
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci// Writes a set of consecutive registers
768c2ecf20Sopenharmony_cistatic int mt2266_writeregs(struct mt2266_priv *priv,u8 *buf, u8 len)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	struct i2c_msg msg = {
798c2ecf20Sopenharmony_ci		.addr = priv->cfg->i2c_address, .flags = 0, .buf = buf, .len = len
808c2ecf20Sopenharmony_ci	};
818c2ecf20Sopenharmony_ci	if (i2c_transfer(priv->i2c, &msg, 1) != 1) {
828c2ecf20Sopenharmony_ci		printk(KERN_WARNING "MT2266 I2C write failed (len=%i)\n",(int)len);
838c2ecf20Sopenharmony_ci		return -EREMOTEIO;
848c2ecf20Sopenharmony_ci	}
858c2ecf20Sopenharmony_ci	return 0;
868c2ecf20Sopenharmony_ci}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci// Initialisation sequences
898c2ecf20Sopenharmony_cistatic u8 mt2266_init1[] = { REG_TUNE, 0x00, 0x00, 0x28,
908c2ecf20Sopenharmony_ci				 0x00, 0x52, 0x99, 0x3f };
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic u8 mt2266_init2[] = {
938c2ecf20Sopenharmony_ci    0x17, 0x6d, 0x71, 0x61, 0xc0, 0xbf, 0xff, 0xdc, 0x00, 0x0a, 0xd4,
948c2ecf20Sopenharmony_ci    0x03, 0x64, 0x64, 0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14,
958c2ecf20Sopenharmony_ci    0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x7f, 0x5e, 0x3f, 0xff, 0xff,
968c2ecf20Sopenharmony_ci    0xff, 0x00, 0x77, 0x0f, 0x2d
978c2ecf20Sopenharmony_ci};
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_cistatic u8 mt2266_init_8mhz[] = { REG_BANDWIDTH, 0x22, 0x22, 0x22, 0x22,
1008c2ecf20Sopenharmony_ci						0x22, 0x22, 0x22, 0x22 };
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic u8 mt2266_init_7mhz[] = { REG_BANDWIDTH, 0x32, 0x32, 0x32, 0x32,
1038c2ecf20Sopenharmony_ci						0x32, 0x32, 0x32, 0x32 };
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_cistatic u8 mt2266_init_6mhz[] = { REG_BANDWIDTH, 0xa7, 0xa7, 0xa7, 0xa7,
1068c2ecf20Sopenharmony_ci						0xa7, 0xa7, 0xa7, 0xa7 };
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic u8 mt2266_uhf[] = { 0x1d, 0xdc, 0x00, 0x0a, 0xd4, 0x03, 0x64, 0x64,
1098c2ecf20Sopenharmony_ci			   0x64, 0x64, 0x22, 0xaa, 0xf2, 0x1e, 0x80, 0x14 };
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic u8 mt2266_vhf[] = { 0x1d, 0xfe, 0x00, 0x00, 0xb4, 0x03, 0xa5, 0xa5,
1128c2ecf20Sopenharmony_ci			   0xa5, 0xa5, 0x82, 0xaa, 0xf1, 0x17, 0x80, 0x1f };
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#define FREF 30000       // Quartz oscillator 30 MHz
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_cistatic int mt2266_set_params(struct dvb_frontend *fe)
1178c2ecf20Sopenharmony_ci{
1188c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
1198c2ecf20Sopenharmony_ci	struct mt2266_priv *priv;
1208c2ecf20Sopenharmony_ci	int ret=0;
1218c2ecf20Sopenharmony_ci	u32 freq;
1228c2ecf20Sopenharmony_ci	u32 tune;
1238c2ecf20Sopenharmony_ci	u8  lnaband;
1248c2ecf20Sopenharmony_ci	u8  b[10];
1258c2ecf20Sopenharmony_ci	int i;
1268c2ecf20Sopenharmony_ci	u8 band;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	priv = fe->tuner_priv;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	freq = priv->frequency / 1000; /* Hz -> kHz */
1318c2ecf20Sopenharmony_ci	if (freq < 470000 && freq > 230000)
1328c2ecf20Sopenharmony_ci		return -EINVAL; /* Gap between VHF and UHF bands */
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	priv->frequency = c->frequency;
1358c2ecf20Sopenharmony_ci	tune = 2 * freq * (8192/16) / (FREF/16);
1368c2ecf20Sopenharmony_ci	band = (freq < 300000) ? MT2266_VHF : MT2266_UHF;
1378c2ecf20Sopenharmony_ci	if (band == MT2266_VHF)
1388c2ecf20Sopenharmony_ci		tune *= 2;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	switch (c->bandwidth_hz) {
1418c2ecf20Sopenharmony_ci	case 6000000:
1428c2ecf20Sopenharmony_ci		mt2266_writeregs(priv, mt2266_init_6mhz,
1438c2ecf20Sopenharmony_ci				 sizeof(mt2266_init_6mhz));
1448c2ecf20Sopenharmony_ci		break;
1458c2ecf20Sopenharmony_ci	case 8000000:
1468c2ecf20Sopenharmony_ci		mt2266_writeregs(priv, mt2266_init_8mhz,
1478c2ecf20Sopenharmony_ci				 sizeof(mt2266_init_8mhz));
1488c2ecf20Sopenharmony_ci		break;
1498c2ecf20Sopenharmony_ci	case 7000000:
1508c2ecf20Sopenharmony_ci	default:
1518c2ecf20Sopenharmony_ci		mt2266_writeregs(priv, mt2266_init_7mhz,
1528c2ecf20Sopenharmony_ci				 sizeof(mt2266_init_7mhz));
1538c2ecf20Sopenharmony_ci		break;
1548c2ecf20Sopenharmony_ci	}
1558c2ecf20Sopenharmony_ci	priv->bandwidth = c->bandwidth_hz;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	if (band == MT2266_VHF && priv->band == MT2266_UHF) {
1588c2ecf20Sopenharmony_ci		dprintk("Switch from UHF to VHF");
1598c2ecf20Sopenharmony_ci		mt2266_writereg(priv, 0x05, 0x04);
1608c2ecf20Sopenharmony_ci		mt2266_writereg(priv, 0x19, 0x61);
1618c2ecf20Sopenharmony_ci		mt2266_writeregs(priv, mt2266_vhf, sizeof(mt2266_vhf));
1628c2ecf20Sopenharmony_ci	} else if (band == MT2266_UHF && priv->band == MT2266_VHF) {
1638c2ecf20Sopenharmony_ci		dprintk("Switch from VHF to UHF");
1648c2ecf20Sopenharmony_ci		mt2266_writereg(priv, 0x05, 0x52);
1658c2ecf20Sopenharmony_ci		mt2266_writereg(priv, 0x19, 0x61);
1668c2ecf20Sopenharmony_ci		mt2266_writeregs(priv, mt2266_uhf, sizeof(mt2266_uhf));
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci	msleep(10);
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci	if (freq <= 495000)
1718c2ecf20Sopenharmony_ci		lnaband = 0xEE;
1728c2ecf20Sopenharmony_ci	else if (freq <= 525000)
1738c2ecf20Sopenharmony_ci		lnaband = 0xDD;
1748c2ecf20Sopenharmony_ci	else if (freq <= 550000)
1758c2ecf20Sopenharmony_ci		lnaband = 0xCC;
1768c2ecf20Sopenharmony_ci	else if (freq <= 580000)
1778c2ecf20Sopenharmony_ci		lnaband = 0xBB;
1788c2ecf20Sopenharmony_ci	else if (freq <= 605000)
1798c2ecf20Sopenharmony_ci		lnaband = 0xAA;
1808c2ecf20Sopenharmony_ci	else if (freq <= 630000)
1818c2ecf20Sopenharmony_ci		lnaband = 0x99;
1828c2ecf20Sopenharmony_ci	else if (freq <= 655000)
1838c2ecf20Sopenharmony_ci		lnaband = 0x88;
1848c2ecf20Sopenharmony_ci	else if (freq <= 685000)
1858c2ecf20Sopenharmony_ci		lnaband = 0x77;
1868c2ecf20Sopenharmony_ci	else if (freq <= 710000)
1878c2ecf20Sopenharmony_ci		lnaband = 0x66;
1888c2ecf20Sopenharmony_ci	else if (freq <= 735000)
1898c2ecf20Sopenharmony_ci		lnaband = 0x55;
1908c2ecf20Sopenharmony_ci	else if (freq <= 765000)
1918c2ecf20Sopenharmony_ci		lnaband = 0x44;
1928c2ecf20Sopenharmony_ci	else if (freq <= 802000)
1938c2ecf20Sopenharmony_ci		lnaband = 0x33;
1948c2ecf20Sopenharmony_ci	else if (freq <= 840000)
1958c2ecf20Sopenharmony_ci		lnaband = 0x22;
1968c2ecf20Sopenharmony_ci	else
1978c2ecf20Sopenharmony_ci		lnaband = 0x11;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci	b[0] = REG_TUNE;
2008c2ecf20Sopenharmony_ci	b[1] = (tune >> 8) & 0x1F;
2018c2ecf20Sopenharmony_ci	b[2] = tune & 0xFF;
2028c2ecf20Sopenharmony_ci	b[3] = tune >> 13;
2038c2ecf20Sopenharmony_ci	mt2266_writeregs(priv,b,4);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	dprintk("set_parms: tune=%d band=%d %s",
2068c2ecf20Sopenharmony_ci		(int) tune, (int) lnaband,
2078c2ecf20Sopenharmony_ci		(band == MT2266_UHF) ? "UHF" : "VHF");
2088c2ecf20Sopenharmony_ci	dprintk("set_parms: [1..3]: %2x %2x %2x",
2098c2ecf20Sopenharmony_ci		(int) b[1], (int) b[2], (int)b[3]);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	if (band == MT2266_UHF) {
2128c2ecf20Sopenharmony_ci		b[0] = 0x05;
2138c2ecf20Sopenharmony_ci		b[1] = (priv->band == MT2266_VHF) ? 0x52 : 0x62;
2148c2ecf20Sopenharmony_ci		b[2] = lnaband;
2158c2ecf20Sopenharmony_ci		mt2266_writeregs(priv, b, 3);
2168c2ecf20Sopenharmony_ci	}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	/* Wait for pll lock or timeout */
2198c2ecf20Sopenharmony_ci	i = 0;
2208c2ecf20Sopenharmony_ci	do {
2218c2ecf20Sopenharmony_ci		mt2266_readreg(priv,REG_LOCK,b);
2228c2ecf20Sopenharmony_ci		if (b[0] & 0x40)
2238c2ecf20Sopenharmony_ci			break;
2248c2ecf20Sopenharmony_ci		msleep(10);
2258c2ecf20Sopenharmony_ci		i++;
2268c2ecf20Sopenharmony_ci	} while (i<10);
2278c2ecf20Sopenharmony_ci	dprintk("Lock when i=%i",(int)i);
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (band == MT2266_UHF && priv->band == MT2266_VHF)
2308c2ecf20Sopenharmony_ci		mt2266_writereg(priv, 0x05, 0x62);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	priv->band = band;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	return ret;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_cistatic void mt2266_calibrate(struct mt2266_priv *priv)
2388c2ecf20Sopenharmony_ci{
2398c2ecf20Sopenharmony_ci	mt2266_writereg(priv, 0x11, 0x03);
2408c2ecf20Sopenharmony_ci	mt2266_writereg(priv, 0x11, 0x01);
2418c2ecf20Sopenharmony_ci	mt2266_writeregs(priv, mt2266_init1, sizeof(mt2266_init1));
2428c2ecf20Sopenharmony_ci	mt2266_writeregs(priv, mt2266_init2, sizeof(mt2266_init2));
2438c2ecf20Sopenharmony_ci	mt2266_writereg(priv, 0x33, 0x5e);
2448c2ecf20Sopenharmony_ci	mt2266_writereg(priv, 0x10, 0x10);
2458c2ecf20Sopenharmony_ci	mt2266_writereg(priv, 0x10, 0x00);
2468c2ecf20Sopenharmony_ci	mt2266_writeregs(priv, mt2266_init_8mhz, sizeof(mt2266_init_8mhz));
2478c2ecf20Sopenharmony_ci	msleep(25);
2488c2ecf20Sopenharmony_ci	mt2266_writereg(priv, 0x17, 0x6d);
2498c2ecf20Sopenharmony_ci	mt2266_writereg(priv, 0x1c, 0x00);
2508c2ecf20Sopenharmony_ci	msleep(75);
2518c2ecf20Sopenharmony_ci	mt2266_writereg(priv, 0x17, 0x6d);
2528c2ecf20Sopenharmony_ci	mt2266_writereg(priv, 0x1c, 0xff);
2538c2ecf20Sopenharmony_ci}
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_cistatic int mt2266_get_frequency(struct dvb_frontend *fe, u32 *frequency)
2568c2ecf20Sopenharmony_ci{
2578c2ecf20Sopenharmony_ci	struct mt2266_priv *priv = fe->tuner_priv;
2588c2ecf20Sopenharmony_ci	*frequency = priv->frequency;
2598c2ecf20Sopenharmony_ci	return 0;
2608c2ecf20Sopenharmony_ci}
2618c2ecf20Sopenharmony_ci
2628c2ecf20Sopenharmony_cistatic int mt2266_get_bandwidth(struct dvb_frontend *fe, u32 *bandwidth)
2638c2ecf20Sopenharmony_ci{
2648c2ecf20Sopenharmony_ci	struct mt2266_priv *priv = fe->tuner_priv;
2658c2ecf20Sopenharmony_ci	*bandwidth = priv->bandwidth;
2668c2ecf20Sopenharmony_ci	return 0;
2678c2ecf20Sopenharmony_ci}
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_cistatic int mt2266_init(struct dvb_frontend *fe)
2708c2ecf20Sopenharmony_ci{
2718c2ecf20Sopenharmony_ci	int ret;
2728c2ecf20Sopenharmony_ci	struct mt2266_priv *priv = fe->tuner_priv;
2738c2ecf20Sopenharmony_ci	ret = mt2266_writereg(priv, 0x17, 0x6d);
2748c2ecf20Sopenharmony_ci	if (ret < 0)
2758c2ecf20Sopenharmony_ci		return ret;
2768c2ecf20Sopenharmony_ci	ret = mt2266_writereg(priv, 0x1c, 0xff);
2778c2ecf20Sopenharmony_ci	if (ret < 0)
2788c2ecf20Sopenharmony_ci		return ret;
2798c2ecf20Sopenharmony_ci	return 0;
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic int mt2266_sleep(struct dvb_frontend *fe)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	struct mt2266_priv *priv = fe->tuner_priv;
2858c2ecf20Sopenharmony_ci	mt2266_writereg(priv, 0x17, 0x6d);
2868c2ecf20Sopenharmony_ci	mt2266_writereg(priv, 0x1c, 0x00);
2878c2ecf20Sopenharmony_ci	return 0;
2888c2ecf20Sopenharmony_ci}
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_cistatic void mt2266_release(struct dvb_frontend *fe)
2918c2ecf20Sopenharmony_ci{
2928c2ecf20Sopenharmony_ci	kfree(fe->tuner_priv);
2938c2ecf20Sopenharmony_ci	fe->tuner_priv = NULL;
2948c2ecf20Sopenharmony_ci}
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_cistatic const struct dvb_tuner_ops mt2266_tuner_ops = {
2978c2ecf20Sopenharmony_ci	.info = {
2988c2ecf20Sopenharmony_ci		.name              = "Microtune MT2266",
2998c2ecf20Sopenharmony_ci		.frequency_min_hz  = 174 * MHz,
3008c2ecf20Sopenharmony_ci		.frequency_max_hz  = 862 * MHz,
3018c2ecf20Sopenharmony_ci		.frequency_step_hz =  50 * kHz,
3028c2ecf20Sopenharmony_ci	},
3038c2ecf20Sopenharmony_ci	.release       = mt2266_release,
3048c2ecf20Sopenharmony_ci	.init          = mt2266_init,
3058c2ecf20Sopenharmony_ci	.sleep         = mt2266_sleep,
3068c2ecf20Sopenharmony_ci	.set_params    = mt2266_set_params,
3078c2ecf20Sopenharmony_ci	.get_frequency = mt2266_get_frequency,
3088c2ecf20Sopenharmony_ci	.get_bandwidth = mt2266_get_bandwidth
3098c2ecf20Sopenharmony_ci};
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_cistruct dvb_frontend * mt2266_attach(struct dvb_frontend *fe, struct i2c_adapter *i2c, struct mt2266_config *cfg)
3128c2ecf20Sopenharmony_ci{
3138c2ecf20Sopenharmony_ci	struct mt2266_priv *priv = NULL;
3148c2ecf20Sopenharmony_ci	u8 id = 0;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	priv = kzalloc(sizeof(struct mt2266_priv), GFP_KERNEL);
3178c2ecf20Sopenharmony_ci	if (priv == NULL)
3188c2ecf20Sopenharmony_ci		return NULL;
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	priv->cfg      = cfg;
3218c2ecf20Sopenharmony_ci	priv->i2c      = i2c;
3228c2ecf20Sopenharmony_ci	priv->band     = MT2266_UHF;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	if (mt2266_readreg(priv, 0, &id)) {
3258c2ecf20Sopenharmony_ci		kfree(priv);
3268c2ecf20Sopenharmony_ci		return NULL;
3278c2ecf20Sopenharmony_ci	}
3288c2ecf20Sopenharmony_ci	if (id != PART_REV) {
3298c2ecf20Sopenharmony_ci		kfree(priv);
3308c2ecf20Sopenharmony_ci		return NULL;
3318c2ecf20Sopenharmony_ci	}
3328c2ecf20Sopenharmony_ci	printk(KERN_INFO "MT2266: successfully identified\n");
3338c2ecf20Sopenharmony_ci	memcpy(&fe->ops.tuner_ops, &mt2266_tuner_ops, sizeof(struct dvb_tuner_ops));
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci	fe->tuner_priv = priv;
3368c2ecf20Sopenharmony_ci	mt2266_calibrate(priv);
3378c2ecf20Sopenharmony_ci	return fe;
3388c2ecf20Sopenharmony_ci}
3398c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(mt2266_attach);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ciMODULE_AUTHOR("Olivier DANET");
3428c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Microtune MT2266 silicon tuner driver");
3438c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
344