18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci  /*
38c2ecf20Sopenharmony_ci     Driver for Philips tda8262/tda8263 DVBS Silicon tuners
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci     (c) 2006 Andrew de Quincey
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci  */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/slab.h>
118c2ecf20Sopenharmony_ci#include <linux/module.h>
128c2ecf20Sopenharmony_ci#include <linux/dvb/frontend.h>
138c2ecf20Sopenharmony_ci#include <asm/types.h>
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include "tda826x.h"
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_cistatic int debug;
188c2ecf20Sopenharmony_ci#define dprintk(args...) \
198c2ecf20Sopenharmony_ci	do { \
208c2ecf20Sopenharmony_ci		if (debug) printk(KERN_DEBUG "tda826x: " args); \
218c2ecf20Sopenharmony_ci	} while (0)
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_cistruct tda826x_priv {
248c2ecf20Sopenharmony_ci	/* i2c details */
258c2ecf20Sopenharmony_ci	int i2c_address;
268c2ecf20Sopenharmony_ci	struct i2c_adapter *i2c;
278c2ecf20Sopenharmony_ci	u8 has_loopthrough:1;
288c2ecf20Sopenharmony_ci	u32 frequency;
298c2ecf20Sopenharmony_ci};
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_cistatic void tda826x_release(struct dvb_frontend *fe)
328c2ecf20Sopenharmony_ci{
338c2ecf20Sopenharmony_ci	kfree(fe->tuner_priv);
348c2ecf20Sopenharmony_ci	fe->tuner_priv = NULL;
358c2ecf20Sopenharmony_ci}
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_cistatic int tda826x_sleep(struct dvb_frontend *fe)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	struct tda826x_priv *priv = fe->tuner_priv;
408c2ecf20Sopenharmony_ci	int ret;
418c2ecf20Sopenharmony_ci	u8 buf [] = { 0x00, 0x8d };
428c2ecf20Sopenharmony_ci	struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 2 };
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	dprintk("%s:\n", __func__);
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci	if (!priv->has_loopthrough)
478c2ecf20Sopenharmony_ci		buf[1] = 0xad;
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
508c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 1);
518c2ecf20Sopenharmony_ci	if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
528c2ecf20Sopenharmony_ci		dprintk("%s: i2c error\n", __func__);
538c2ecf20Sopenharmony_ci	}
548c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
558c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 0);
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci	return (ret == 1) ? 0 : ret;
588c2ecf20Sopenharmony_ci}
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_cistatic int tda826x_set_params(struct dvb_frontend *fe)
618c2ecf20Sopenharmony_ci{
628c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
638c2ecf20Sopenharmony_ci	struct tda826x_priv *priv = fe->tuner_priv;
648c2ecf20Sopenharmony_ci	int ret;
658c2ecf20Sopenharmony_ci	u32 div;
668c2ecf20Sopenharmony_ci	u32 ksyms;
678c2ecf20Sopenharmony_ci	u32 bandwidth;
688c2ecf20Sopenharmony_ci	u8 buf [11];
698c2ecf20Sopenharmony_ci	struct i2c_msg msg = { .addr = priv->i2c_address, .flags = 0, .buf = buf, .len = 11 };
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci	dprintk("%s:\n", __func__);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci	div = (p->frequency + (1000-1)) / 1000;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	/* BW = ((1 + RO) * SR/2 + 5) * 1.3      [SR in MSPS, BW in MHz] */
768c2ecf20Sopenharmony_ci	/* with R0 = 0.35 and some transformations: */
778c2ecf20Sopenharmony_ci	ksyms = p->symbol_rate / 1000;
788c2ecf20Sopenharmony_ci	bandwidth = (878 * ksyms + 6500000) / 1000000 + 1;
798c2ecf20Sopenharmony_ci	if (bandwidth < 5)
808c2ecf20Sopenharmony_ci		bandwidth = 5;
818c2ecf20Sopenharmony_ci	else if (bandwidth > 36)
828c2ecf20Sopenharmony_ci		bandwidth = 36;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	buf[0] = 0x00; // subaddress
858c2ecf20Sopenharmony_ci	buf[1] = 0x09; // powerdown RSSI + the magic value 1
868c2ecf20Sopenharmony_ci	if (!priv->has_loopthrough)
878c2ecf20Sopenharmony_ci		buf[1] |= 0x20; // power down loopthrough if not needed
888c2ecf20Sopenharmony_ci	buf[2] = (1<<5) | 0x0b; // 1Mhz + 0.45 VCO
898c2ecf20Sopenharmony_ci	buf[3] = div >> 7;
908c2ecf20Sopenharmony_ci	buf[4] = div << 1;
918c2ecf20Sopenharmony_ci	buf[5] = ((bandwidth - 5) << 3) | 7; /* baseband cut-off */
928c2ecf20Sopenharmony_ci	buf[6] = 0xfe; // baseband gain 9 db + no RF attenuation
938c2ecf20Sopenharmony_ci	buf[7] = 0x83; // charge pumps at high, tests off
948c2ecf20Sopenharmony_ci	buf[8] = 0x80; // recommended value 4 for AMPVCO + disable ports.
958c2ecf20Sopenharmony_ci	buf[9] = 0x1a; // normal caltime + recommended values for SELTH + SELVTL
968c2ecf20Sopenharmony_ci	buf[10] = 0xd4; // recommended value 13 for BBIAS + unknown bit set on
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
998c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 1);
1008c2ecf20Sopenharmony_ci	if ((ret = i2c_transfer (priv->i2c, &msg, 1)) != 1) {
1018c2ecf20Sopenharmony_ci		dprintk("%s: i2c error\n", __func__);
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
1048c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 0);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	priv->frequency = div * 1000;
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return (ret == 1) ? 0 : ret;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int tda826x_get_frequency(struct dvb_frontend *fe, u32 *frequency)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct tda826x_priv *priv = fe->tuner_priv;
1148c2ecf20Sopenharmony_ci	*frequency = priv->frequency;
1158c2ecf20Sopenharmony_ci	return 0;
1168c2ecf20Sopenharmony_ci}
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_cistatic const struct dvb_tuner_ops tda826x_tuner_ops = {
1198c2ecf20Sopenharmony_ci	.info = {
1208c2ecf20Sopenharmony_ci		.name = "Philips TDA826X",
1218c2ecf20Sopenharmony_ci		.frequency_min_hz =  950 * MHz,
1228c2ecf20Sopenharmony_ci		.frequency_max_hz = 2175 * MHz
1238c2ecf20Sopenharmony_ci	},
1248c2ecf20Sopenharmony_ci	.release = tda826x_release,
1258c2ecf20Sopenharmony_ci	.sleep = tda826x_sleep,
1268c2ecf20Sopenharmony_ci	.set_params = tda826x_set_params,
1278c2ecf20Sopenharmony_ci	.get_frequency = tda826x_get_frequency,
1288c2ecf20Sopenharmony_ci};
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistruct dvb_frontend *tda826x_attach(struct dvb_frontend *fe, int addr, struct i2c_adapter *i2c, int has_loopthrough)
1318c2ecf20Sopenharmony_ci{
1328c2ecf20Sopenharmony_ci	struct tda826x_priv *priv = NULL;
1338c2ecf20Sopenharmony_ci	u8 b1 [] = { 0, 0 };
1348c2ecf20Sopenharmony_ci	struct i2c_msg msg[2] = {
1358c2ecf20Sopenharmony_ci		{ .addr = addr, .flags = 0,        .buf = NULL, .len = 0 },
1368c2ecf20Sopenharmony_ci		{ .addr = addr, .flags = I2C_M_RD, .buf = b1, .len = 2 }
1378c2ecf20Sopenharmony_ci	};
1388c2ecf20Sopenharmony_ci	int ret;
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	dprintk("%s:\n", __func__);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
1438c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 1);
1448c2ecf20Sopenharmony_ci	ret = i2c_transfer (i2c, msg, 2);
1458c2ecf20Sopenharmony_ci	if (fe->ops.i2c_gate_ctrl)
1468c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 0);
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	if (ret != 2)
1498c2ecf20Sopenharmony_ci		return NULL;
1508c2ecf20Sopenharmony_ci	if (!(b1[1] & 0x80))
1518c2ecf20Sopenharmony_ci		return NULL;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	priv = kzalloc(sizeof(struct tda826x_priv), GFP_KERNEL);
1548c2ecf20Sopenharmony_ci	if (priv == NULL)
1558c2ecf20Sopenharmony_ci		return NULL;
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	priv->i2c_address = addr;
1588c2ecf20Sopenharmony_ci	priv->i2c = i2c;
1598c2ecf20Sopenharmony_ci	priv->has_loopthrough = has_loopthrough;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	memcpy(&fe->ops.tuner_ops, &tda826x_tuner_ops, sizeof(struct dvb_tuner_ops));
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	fe->tuner_priv = priv;
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	return fe;
1668c2ecf20Sopenharmony_ci}
1678c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(tda826x_attach);
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cimodule_param(debug, int, 0644);
1708c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("DVB TDA826x driver");
1738c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andrew de Quincey");
1748c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
175