18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci  /*
38c2ecf20Sopenharmony_ci     Driver for Philips tda1004xh OFDM Demodulator
48c2ecf20Sopenharmony_ci
58c2ecf20Sopenharmony_ci     (c) 2003, 2004 Andrew de Quincey & Robert Schlabbach
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci
88c2ecf20Sopenharmony_ci   */
98c2ecf20Sopenharmony_ci/*
108c2ecf20Sopenharmony_ci * This driver needs external firmware. Please use the commands
118c2ecf20Sopenharmony_ci * "<kerneldir>/scripts/get_dvb_firmware tda10045",
128c2ecf20Sopenharmony_ci * "<kerneldir>/scripts/get_dvb_firmware tda10046" to
138c2ecf20Sopenharmony_ci * download/extract them, and then copy them to /usr/lib/hotplug/firmware
148c2ecf20Sopenharmony_ci * or /lib/firmware (depending on configuration of firmware hotplug).
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci#define TDA10045_DEFAULT_FIRMWARE "dvb-fe-tda10045.fw"
178c2ecf20Sopenharmony_ci#define TDA10046_DEFAULT_FIRMWARE "dvb-fe-tda10046.fw"
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/init.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/device.h>
228c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
238c2ecf20Sopenharmony_ci#include <linux/string.h>
248c2ecf20Sopenharmony_ci#include <linux/slab.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include <media/dvb_frontend.h>
278c2ecf20Sopenharmony_ci#include "tda1004x.h"
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_cistatic int debug;
308c2ecf20Sopenharmony_ci#define dprintk(args...) \
318c2ecf20Sopenharmony_ci	do { \
328c2ecf20Sopenharmony_ci		if (debug) printk(KERN_DEBUG "tda1004x: " args); \
338c2ecf20Sopenharmony_ci	} while (0)
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define TDA1004X_CHIPID		 0x00
368c2ecf20Sopenharmony_ci#define TDA1004X_AUTO		 0x01
378c2ecf20Sopenharmony_ci#define TDA1004X_IN_CONF1	 0x02
388c2ecf20Sopenharmony_ci#define TDA1004X_IN_CONF2	 0x03
398c2ecf20Sopenharmony_ci#define TDA1004X_OUT_CONF1	 0x04
408c2ecf20Sopenharmony_ci#define TDA1004X_OUT_CONF2	 0x05
418c2ecf20Sopenharmony_ci#define TDA1004X_STATUS_CD	 0x06
428c2ecf20Sopenharmony_ci#define TDA1004X_CONFC4		 0x07
438c2ecf20Sopenharmony_ci#define TDA1004X_DSSPARE2	 0x0C
448c2ecf20Sopenharmony_ci#define TDA10045H_CODE_IN	 0x0D
458c2ecf20Sopenharmony_ci#define TDA10045H_FWPAGE	 0x0E
468c2ecf20Sopenharmony_ci#define TDA1004X_SCAN_CPT	 0x10
478c2ecf20Sopenharmony_ci#define TDA1004X_DSP_CMD	 0x11
488c2ecf20Sopenharmony_ci#define TDA1004X_DSP_ARG	 0x12
498c2ecf20Sopenharmony_ci#define TDA1004X_DSP_DATA1	 0x13
508c2ecf20Sopenharmony_ci#define TDA1004X_DSP_DATA2	 0x14
518c2ecf20Sopenharmony_ci#define TDA1004X_CONFADC1	 0x15
528c2ecf20Sopenharmony_ci#define TDA1004X_CONFC1		 0x16
538c2ecf20Sopenharmony_ci#define TDA10045H_S_AGC		 0x1a
548c2ecf20Sopenharmony_ci#define TDA10046H_AGC_TUN_LEVEL	 0x1a
558c2ecf20Sopenharmony_ci#define TDA1004X_SNR		 0x1c
568c2ecf20Sopenharmony_ci#define TDA1004X_CONF_TS1	 0x1e
578c2ecf20Sopenharmony_ci#define TDA1004X_CONF_TS2	 0x1f
588c2ecf20Sopenharmony_ci#define TDA1004X_CBER_RESET	 0x20
598c2ecf20Sopenharmony_ci#define TDA1004X_CBER_MSB	 0x21
608c2ecf20Sopenharmony_ci#define TDA1004X_CBER_LSB	 0x22
618c2ecf20Sopenharmony_ci#define TDA1004X_CVBER_LUT	 0x23
628c2ecf20Sopenharmony_ci#define TDA1004X_VBER_MSB	 0x24
638c2ecf20Sopenharmony_ci#define TDA1004X_VBER_MID	 0x25
648c2ecf20Sopenharmony_ci#define TDA1004X_VBER_LSB	 0x26
658c2ecf20Sopenharmony_ci#define TDA1004X_UNCOR		 0x27
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci#define TDA10045H_CONFPLL_P	 0x2D
688c2ecf20Sopenharmony_ci#define TDA10045H_CONFPLL_M_MSB	 0x2E
698c2ecf20Sopenharmony_ci#define TDA10045H_CONFPLL_M_LSB	 0x2F
708c2ecf20Sopenharmony_ci#define TDA10045H_CONFPLL_N	 0x30
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define TDA10046H_CONFPLL1	 0x2D
738c2ecf20Sopenharmony_ci#define TDA10046H_CONFPLL2	 0x2F
748c2ecf20Sopenharmony_ci#define TDA10046H_CONFPLL3	 0x30
758c2ecf20Sopenharmony_ci#define TDA10046H_TIME_WREF1	 0x31
768c2ecf20Sopenharmony_ci#define TDA10046H_TIME_WREF2	 0x32
778c2ecf20Sopenharmony_ci#define TDA10046H_TIME_WREF3	 0x33
788c2ecf20Sopenharmony_ci#define TDA10046H_TIME_WREF4	 0x34
798c2ecf20Sopenharmony_ci#define TDA10046H_TIME_WREF5	 0x35
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci#define TDA10045H_UNSURW_MSB	 0x31
828c2ecf20Sopenharmony_ci#define TDA10045H_UNSURW_LSB	 0x32
838c2ecf20Sopenharmony_ci#define TDA10045H_WREF_MSB	 0x33
848c2ecf20Sopenharmony_ci#define TDA10045H_WREF_MID	 0x34
858c2ecf20Sopenharmony_ci#define TDA10045H_WREF_LSB	 0x35
868c2ecf20Sopenharmony_ci#define TDA10045H_MUXOUT	 0x36
878c2ecf20Sopenharmony_ci#define TDA1004X_CONFADC2	 0x37
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci#define TDA10045H_IOFFSET	 0x38
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#define TDA10046H_CONF_TRISTATE1 0x3B
928c2ecf20Sopenharmony_ci#define TDA10046H_CONF_TRISTATE2 0x3C
938c2ecf20Sopenharmony_ci#define TDA10046H_CONF_POLARITY	 0x3D
948c2ecf20Sopenharmony_ci#define TDA10046H_FREQ_OFFSET	 0x3E
958c2ecf20Sopenharmony_ci#define TDA10046H_GPIO_OUT_SEL	 0x41
968c2ecf20Sopenharmony_ci#define TDA10046H_GPIO_SELECT	 0x42
978c2ecf20Sopenharmony_ci#define TDA10046H_AGC_CONF	 0x43
988c2ecf20Sopenharmony_ci#define TDA10046H_AGC_THR	 0x44
998c2ecf20Sopenharmony_ci#define TDA10046H_AGC_RENORM	 0x45
1008c2ecf20Sopenharmony_ci#define TDA10046H_AGC_GAINS	 0x46
1018c2ecf20Sopenharmony_ci#define TDA10046H_AGC_TUN_MIN	 0x47
1028c2ecf20Sopenharmony_ci#define TDA10046H_AGC_TUN_MAX	 0x48
1038c2ecf20Sopenharmony_ci#define TDA10046H_AGC_IF_MIN	 0x49
1048c2ecf20Sopenharmony_ci#define TDA10046H_AGC_IF_MAX	 0x4A
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci#define TDA10046H_FREQ_PHY2_MSB	 0x4D
1078c2ecf20Sopenharmony_ci#define TDA10046H_FREQ_PHY2_LSB	 0x4E
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#define TDA10046H_CVBER_CTRL	 0x4F
1108c2ecf20Sopenharmony_ci#define TDA10046H_AGC_IF_LEVEL	 0x52
1118c2ecf20Sopenharmony_ci#define TDA10046H_CODE_CPT	 0x57
1128c2ecf20Sopenharmony_ci#define TDA10046H_CODE_IN	 0x58
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_cistatic int tda1004x_write_byteI(struct tda1004x_state *state, int reg, int data)
1168c2ecf20Sopenharmony_ci{
1178c2ecf20Sopenharmony_ci	int ret;
1188c2ecf20Sopenharmony_ci	u8 buf[] = { reg, data };
1198c2ecf20Sopenharmony_ci	struct i2c_msg msg = { .flags = 0, .buf = buf, .len = 2 };
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci	dprintk("%s: reg=0x%x, data=0x%x\n", __func__, reg, data);
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci	msg.addr = state->config->demod_address;
1248c2ecf20Sopenharmony_ci	ret = i2c_transfer(state->i2c, &msg, 1);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	if (ret != 1)
1278c2ecf20Sopenharmony_ci		dprintk("%s: error reg=0x%x, data=0x%x, ret=%i\n",
1288c2ecf20Sopenharmony_ci			__func__, reg, data, ret);
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci	dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __func__,
1318c2ecf20Sopenharmony_ci		reg, data, ret);
1328c2ecf20Sopenharmony_ci	return (ret != 1) ? -1 : 0;
1338c2ecf20Sopenharmony_ci}
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_cistatic int tda1004x_read_byte(struct tda1004x_state *state, int reg)
1368c2ecf20Sopenharmony_ci{
1378c2ecf20Sopenharmony_ci	int ret;
1388c2ecf20Sopenharmony_ci	u8 b0[] = { reg };
1398c2ecf20Sopenharmony_ci	u8 b1[] = { 0 };
1408c2ecf20Sopenharmony_ci	struct i2c_msg msg[] = {{ .flags = 0, .buf = b0, .len = 1 },
1418c2ecf20Sopenharmony_ci				{ .flags = I2C_M_RD, .buf = b1, .len = 1 }};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	dprintk("%s: reg=0x%x\n", __func__, reg);
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	msg[0].addr = state->config->demod_address;
1468c2ecf20Sopenharmony_ci	msg[1].addr = state->config->demod_address;
1478c2ecf20Sopenharmony_ci	ret = i2c_transfer(state->i2c, msg, 2);
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	if (ret != 2) {
1508c2ecf20Sopenharmony_ci		dprintk("%s: error reg=0x%x, ret=%i\n", __func__, reg,
1518c2ecf20Sopenharmony_ci			ret);
1528c2ecf20Sopenharmony_ci		return -EINVAL;
1538c2ecf20Sopenharmony_ci	}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	dprintk("%s: success reg=0x%x, data=0x%x, ret=%i\n", __func__,
1568c2ecf20Sopenharmony_ci		reg, b1[0], ret);
1578c2ecf20Sopenharmony_ci	return b1[0];
1588c2ecf20Sopenharmony_ci}
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_cistatic int tda1004x_write_mask(struct tda1004x_state *state, int reg, int mask, int data)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	int val;
1638c2ecf20Sopenharmony_ci	dprintk("%s: reg=0x%x, mask=0x%x, data=0x%x\n", __func__, reg,
1648c2ecf20Sopenharmony_ci		mask, data);
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	// read a byte and check
1678c2ecf20Sopenharmony_ci	val = tda1004x_read_byte(state, reg);
1688c2ecf20Sopenharmony_ci	if (val < 0)
1698c2ecf20Sopenharmony_ci		return val;
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_ci	// mask if off
1728c2ecf20Sopenharmony_ci	val = val & ~mask;
1738c2ecf20Sopenharmony_ci	val |= data & 0xff;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	// write it out again
1768c2ecf20Sopenharmony_ci	return tda1004x_write_byteI(state, reg, val);
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic int tda1004x_write_buf(struct tda1004x_state *state, int reg, unsigned char *buf, int len)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	int i;
1828c2ecf20Sopenharmony_ci	int result;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	dprintk("%s: reg=0x%x, len=0x%x\n", __func__, reg, len);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	result = 0;
1878c2ecf20Sopenharmony_ci	for (i = 0; i < len; i++) {
1888c2ecf20Sopenharmony_ci		result = tda1004x_write_byteI(state, reg + i, buf[i]);
1898c2ecf20Sopenharmony_ci		if (result != 0)
1908c2ecf20Sopenharmony_ci			break;
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci	return result;
1948c2ecf20Sopenharmony_ci}
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistatic int tda1004x_enable_tuner_i2c(struct tda1004x_state *state)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	int result;
1998c2ecf20Sopenharmony_ci	dprintk("%s\n", __func__);
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	result = tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 2);
2028c2ecf20Sopenharmony_ci	msleep(20);
2038c2ecf20Sopenharmony_ci	return result;
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_cistatic int tda1004x_disable_tuner_i2c(struct tda1004x_state *state)
2078c2ecf20Sopenharmony_ci{
2088c2ecf20Sopenharmony_ci	dprintk("%s\n", __func__);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci	return tda1004x_write_mask(state, TDA1004X_CONFC4, 2, 0);
2118c2ecf20Sopenharmony_ci}
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_cistatic int tda10045h_set_bandwidth(struct tda1004x_state *state,
2148c2ecf20Sopenharmony_ci				   u32 bandwidth)
2158c2ecf20Sopenharmony_ci{
2168c2ecf20Sopenharmony_ci	static u8 bandwidth_6mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x60, 0x1e, 0xa7, 0x45, 0x4f };
2178c2ecf20Sopenharmony_ci	static u8 bandwidth_7mhz[] = { 0x02, 0x00, 0x37, 0x00, 0x4a, 0x2f, 0x6d, 0x76, 0xdb };
2188c2ecf20Sopenharmony_ci	static u8 bandwidth_8mhz[] = { 0x02, 0x00, 0x3d, 0x00, 0x48, 0x17, 0x89, 0xc7, 0x14 };
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	switch (bandwidth) {
2218c2ecf20Sopenharmony_ci	case 6000000:
2228c2ecf20Sopenharmony_ci		tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_6mhz, sizeof(bandwidth_6mhz));
2238c2ecf20Sopenharmony_ci		break;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	case 7000000:
2268c2ecf20Sopenharmony_ci		tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_7mhz, sizeof(bandwidth_7mhz));
2278c2ecf20Sopenharmony_ci		break;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	case 8000000:
2308c2ecf20Sopenharmony_ci		tda1004x_write_buf(state, TDA10045H_CONFPLL_P, bandwidth_8mhz, sizeof(bandwidth_8mhz));
2318c2ecf20Sopenharmony_ci		break;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	default:
2348c2ecf20Sopenharmony_ci		return -EINVAL;
2358c2ecf20Sopenharmony_ci	}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA10045H_IOFFSET, 0);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	return 0;
2408c2ecf20Sopenharmony_ci}
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_cistatic int tda10046h_set_bandwidth(struct tda1004x_state *state,
2438c2ecf20Sopenharmony_ci				   u32 bandwidth)
2448c2ecf20Sopenharmony_ci{
2458c2ecf20Sopenharmony_ci	static u8 bandwidth_6mhz_53M[] = { 0x7b, 0x2e, 0x11, 0xf0, 0xd2 };
2468c2ecf20Sopenharmony_ci	static u8 bandwidth_7mhz_53M[] = { 0x6a, 0x02, 0x6a, 0x43, 0x9f };
2478c2ecf20Sopenharmony_ci	static u8 bandwidth_8mhz_53M[] = { 0x5c, 0x32, 0xc2, 0x96, 0x6d };
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	static u8 bandwidth_6mhz_48M[] = { 0x70, 0x02, 0x49, 0x24, 0x92 };
2508c2ecf20Sopenharmony_ci	static u8 bandwidth_7mhz_48M[] = { 0x60, 0x02, 0xaa, 0xaa, 0xab };
2518c2ecf20Sopenharmony_ci	static u8 bandwidth_8mhz_48M[] = { 0x54, 0x03, 0x0c, 0x30, 0xc3 };
2528c2ecf20Sopenharmony_ci	int tda10046_clk53m;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	if ((state->config->if_freq == TDA10046_FREQ_045) ||
2558c2ecf20Sopenharmony_ci	    (state->config->if_freq == TDA10046_FREQ_052))
2568c2ecf20Sopenharmony_ci		tda10046_clk53m = 0;
2578c2ecf20Sopenharmony_ci	else
2588c2ecf20Sopenharmony_ci		tda10046_clk53m = 1;
2598c2ecf20Sopenharmony_ci	switch (bandwidth) {
2608c2ecf20Sopenharmony_ci	case 6000000:
2618c2ecf20Sopenharmony_ci		if (tda10046_clk53m)
2628c2ecf20Sopenharmony_ci			tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_53M,
2638c2ecf20Sopenharmony_ci						  sizeof(bandwidth_6mhz_53M));
2648c2ecf20Sopenharmony_ci		else
2658c2ecf20Sopenharmony_ci			tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_6mhz_48M,
2668c2ecf20Sopenharmony_ci						  sizeof(bandwidth_6mhz_48M));
2678c2ecf20Sopenharmony_ci		if (state->config->if_freq == TDA10046_FREQ_045) {
2688c2ecf20Sopenharmony_ci			tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0a);
2698c2ecf20Sopenharmony_ci			tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xab);
2708c2ecf20Sopenharmony_ci		}
2718c2ecf20Sopenharmony_ci		break;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	case 7000000:
2748c2ecf20Sopenharmony_ci		if (tda10046_clk53m)
2758c2ecf20Sopenharmony_ci			tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_53M,
2768c2ecf20Sopenharmony_ci						  sizeof(bandwidth_7mhz_53M));
2778c2ecf20Sopenharmony_ci		else
2788c2ecf20Sopenharmony_ci			tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_7mhz_48M,
2798c2ecf20Sopenharmony_ci						  sizeof(bandwidth_7mhz_48M));
2808c2ecf20Sopenharmony_ci		if (state->config->if_freq == TDA10046_FREQ_045) {
2818c2ecf20Sopenharmony_ci			tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
2828c2ecf20Sopenharmony_ci			tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
2838c2ecf20Sopenharmony_ci		}
2848c2ecf20Sopenharmony_ci		break;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	case 8000000:
2878c2ecf20Sopenharmony_ci		if (tda10046_clk53m)
2888c2ecf20Sopenharmony_ci			tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_53M,
2898c2ecf20Sopenharmony_ci						  sizeof(bandwidth_8mhz_53M));
2908c2ecf20Sopenharmony_ci		else
2918c2ecf20Sopenharmony_ci			tda1004x_write_buf(state, TDA10046H_TIME_WREF1, bandwidth_8mhz_48M,
2928c2ecf20Sopenharmony_ci						  sizeof(bandwidth_8mhz_48M));
2938c2ecf20Sopenharmony_ci		if (state->config->if_freq == TDA10046_FREQ_045) {
2948c2ecf20Sopenharmony_ci			tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
2958c2ecf20Sopenharmony_ci			tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x55);
2968c2ecf20Sopenharmony_ci		}
2978c2ecf20Sopenharmony_ci		break;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	default:
3008c2ecf20Sopenharmony_ci		return -EINVAL;
3018c2ecf20Sopenharmony_ci	}
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	return 0;
3048c2ecf20Sopenharmony_ci}
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_cistatic int tda1004x_do_upload(struct tda1004x_state *state,
3078c2ecf20Sopenharmony_ci			      const unsigned char *mem, unsigned int len,
3088c2ecf20Sopenharmony_ci			      u8 dspCodeCounterReg, u8 dspCodeInReg)
3098c2ecf20Sopenharmony_ci{
3108c2ecf20Sopenharmony_ci	u8 buf[65];
3118c2ecf20Sopenharmony_ci	struct i2c_msg fw_msg = { .flags = 0, .buf = buf, .len = 0 };
3128c2ecf20Sopenharmony_ci	int tx_size;
3138c2ecf20Sopenharmony_ci	int pos = 0;
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	/* clear code counter */
3168c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, dspCodeCounterReg, 0);
3178c2ecf20Sopenharmony_ci	fw_msg.addr = state->config->demod_address;
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	i2c_lock_bus(state->i2c, I2C_LOCK_SEGMENT);
3208c2ecf20Sopenharmony_ci	buf[0] = dspCodeInReg;
3218c2ecf20Sopenharmony_ci	while (pos != len) {
3228c2ecf20Sopenharmony_ci		// work out how much to send this time
3238c2ecf20Sopenharmony_ci		tx_size = len - pos;
3248c2ecf20Sopenharmony_ci		if (tx_size > 0x10)
3258c2ecf20Sopenharmony_ci			tx_size = 0x10;
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci		// send the chunk
3288c2ecf20Sopenharmony_ci		memcpy(buf + 1, mem + pos, tx_size);
3298c2ecf20Sopenharmony_ci		fw_msg.len = tx_size + 1;
3308c2ecf20Sopenharmony_ci		if (__i2c_transfer(state->i2c, &fw_msg, 1) != 1) {
3318c2ecf20Sopenharmony_ci			printk(KERN_ERR "tda1004x: Error during firmware upload\n");
3328c2ecf20Sopenharmony_ci			i2c_unlock_bus(state->i2c, I2C_LOCK_SEGMENT);
3338c2ecf20Sopenharmony_ci			return -EIO;
3348c2ecf20Sopenharmony_ci		}
3358c2ecf20Sopenharmony_ci		pos += tx_size;
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci		dprintk("%s: fw_pos=0x%x\n", __func__, pos);
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci	i2c_unlock_bus(state->i2c, I2C_LOCK_SEGMENT);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	/* give the DSP a chance to settle 03/10/05 Hac */
3428c2ecf20Sopenharmony_ci	msleep(100);
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	return 0;
3458c2ecf20Sopenharmony_ci}
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_cistatic int tda1004x_check_upload_ok(struct tda1004x_state *state)
3488c2ecf20Sopenharmony_ci{
3498c2ecf20Sopenharmony_ci	u8 data1, data2;
3508c2ecf20Sopenharmony_ci	unsigned long timeout;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
3538c2ecf20Sopenharmony_ci		timeout = jiffies + 2 * HZ;
3548c2ecf20Sopenharmony_ci		while(!(tda1004x_read_byte(state, TDA1004X_STATUS_CD) & 0x20)) {
3558c2ecf20Sopenharmony_ci			if (time_after(jiffies, timeout)) {
3568c2ecf20Sopenharmony_ci				printk(KERN_ERR "tda1004x: timeout waiting for DSP ready\n");
3578c2ecf20Sopenharmony_ci				break;
3588c2ecf20Sopenharmony_ci			}
3598c2ecf20Sopenharmony_ci			msleep(1);
3608c2ecf20Sopenharmony_ci		}
3618c2ecf20Sopenharmony_ci	} else
3628c2ecf20Sopenharmony_ci		msleep(100);
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	// check upload was OK
3658c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0); // we want to read from the DSP
3668c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_DSP_CMD, 0x67);
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci	data1 = tda1004x_read_byte(state, TDA1004X_DSP_DATA1);
3698c2ecf20Sopenharmony_ci	data2 = tda1004x_read_byte(state, TDA1004X_DSP_DATA2);
3708c2ecf20Sopenharmony_ci	if (data1 != 0x67 || data2 < 0x20 || data2 > 0x2e) {
3718c2ecf20Sopenharmony_ci		printk(KERN_INFO "tda1004x: found firmware revision %x -- invalid\n", data2);
3728c2ecf20Sopenharmony_ci		return -EIO;
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci	printk(KERN_INFO "tda1004x: found firmware revision %x -- ok\n", data2);
3758c2ecf20Sopenharmony_ci	return 0;
3768c2ecf20Sopenharmony_ci}
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_cistatic int tda10045_fwupload(struct dvb_frontend* fe)
3798c2ecf20Sopenharmony_ci{
3808c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
3818c2ecf20Sopenharmony_ci	int ret;
3828c2ecf20Sopenharmony_ci	const struct firmware *fw;
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci	/* don't re-upload unless necessary */
3858c2ecf20Sopenharmony_ci	if (tda1004x_check_upload_ok(state) == 0)
3868c2ecf20Sopenharmony_ci		return 0;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	/* request the firmware, this will block until someone uploads it */
3898c2ecf20Sopenharmony_ci	printk(KERN_INFO "tda1004x: waiting for firmware upload (%s)...\n", TDA10045_DEFAULT_FIRMWARE);
3908c2ecf20Sopenharmony_ci	ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
3918c2ecf20Sopenharmony_ci	if (ret) {
3928c2ecf20Sopenharmony_ci		printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
3938c2ecf20Sopenharmony_ci		return ret;
3948c2ecf20Sopenharmony_ci	}
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci	/* reset chip */
3978c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFC4, 0x10, 0);
3988c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
3998c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
4008c2ecf20Sopenharmony_ci	msleep(10);
4018c2ecf20Sopenharmony_ci
4028c2ecf20Sopenharmony_ci	/* set parameters */
4038c2ecf20Sopenharmony_ci	tda10045h_set_bandwidth(state, 8000000);
4048c2ecf20Sopenharmony_ci
4058c2ecf20Sopenharmony_ci	ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10045H_FWPAGE, TDA10045H_CODE_IN);
4068c2ecf20Sopenharmony_ci	release_firmware(fw);
4078c2ecf20Sopenharmony_ci	if (ret)
4088c2ecf20Sopenharmony_ci		return ret;
4098c2ecf20Sopenharmony_ci	printk(KERN_INFO "tda1004x: firmware upload complete\n");
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	/* wait for DSP to initialise */
4128c2ecf20Sopenharmony_ci	/* DSPREADY doesn't seem to work on the TDA10045H */
4138c2ecf20Sopenharmony_ci	msleep(100);
4148c2ecf20Sopenharmony_ci
4158c2ecf20Sopenharmony_ci	return tda1004x_check_upload_ok(state);
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_cistatic void tda10046_init_plls(struct dvb_frontend* fe)
4198c2ecf20Sopenharmony_ci{
4208c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
4218c2ecf20Sopenharmony_ci	int tda10046_clk53m;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	if ((state->config->if_freq == TDA10046_FREQ_045) ||
4248c2ecf20Sopenharmony_ci	    (state->config->if_freq == TDA10046_FREQ_052))
4258c2ecf20Sopenharmony_ci		tda10046_clk53m = 0;
4268c2ecf20Sopenharmony_ci	else
4278c2ecf20Sopenharmony_ci		tda10046_clk53m = 1;
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA10046H_CONFPLL1, 0xf0);
4308c2ecf20Sopenharmony_ci	if(tda10046_clk53m) {
4318c2ecf20Sopenharmony_ci		printk(KERN_INFO "tda1004x: setting up plls for 53MHz sampling clock\n");
4328c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x08); // PLL M = 8
4338c2ecf20Sopenharmony_ci	} else {
4348c2ecf20Sopenharmony_ci		printk(KERN_INFO "tda1004x: setting up plls for 48MHz sampling clock\n");
4358c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_CONFPLL2, 0x03); // PLL M = 3
4368c2ecf20Sopenharmony_ci	}
4378c2ecf20Sopenharmony_ci	if (state->config->xtal_freq == TDA10046_XTAL_4M ) {
4388c2ecf20Sopenharmony_ci		dprintk("%s: setting up PLLs for a 4 MHz Xtal\n", __func__);
4398c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 0); // PLL P = N = 0
4408c2ecf20Sopenharmony_ci	} else {
4418c2ecf20Sopenharmony_ci		dprintk("%s: setting up PLLs for a 16 MHz Xtal\n", __func__);
4428c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_CONFPLL3, 3); // PLL P = 0, N = 3
4438c2ecf20Sopenharmony_ci	}
4448c2ecf20Sopenharmony_ci	if(tda10046_clk53m)
4458c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x67);
4468c2ecf20Sopenharmony_ci	else
4478c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_FREQ_OFFSET, 0x72);
4488c2ecf20Sopenharmony_ci	/* Note clock frequency is handled implicitly */
4498c2ecf20Sopenharmony_ci	switch (state->config->if_freq) {
4508c2ecf20Sopenharmony_ci	case TDA10046_FREQ_045:
4518c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0c);
4528c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x00);
4538c2ecf20Sopenharmony_ci		break;
4548c2ecf20Sopenharmony_ci	case TDA10046_FREQ_052:
4558c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0x0d);
4568c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0xc7);
4578c2ecf20Sopenharmony_ci		break;
4588c2ecf20Sopenharmony_ci	case TDA10046_FREQ_3617:
4598c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
4608c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x59);
4618c2ecf20Sopenharmony_ci		break;
4628c2ecf20Sopenharmony_ci	case TDA10046_FREQ_3613:
4638c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_MSB, 0xd7);
4648c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_FREQ_PHY2_LSB, 0x3f);
4658c2ecf20Sopenharmony_ci		break;
4668c2ecf20Sopenharmony_ci	}
4678c2ecf20Sopenharmony_ci	tda10046h_set_bandwidth(state, 8000000); /* default bandwidth 8 MHz */
4688c2ecf20Sopenharmony_ci	/* let the PLLs settle */
4698c2ecf20Sopenharmony_ci	msleep(120);
4708c2ecf20Sopenharmony_ci}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_cistatic int tda10046_fwupload(struct dvb_frontend* fe)
4738c2ecf20Sopenharmony_ci{
4748c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
4758c2ecf20Sopenharmony_ci	int ret, confc4;
4768c2ecf20Sopenharmony_ci	const struct firmware *fw;
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	/* reset + wake up chip */
4798c2ecf20Sopenharmony_ci	if (state->config->xtal_freq == TDA10046_XTAL_4M) {
4808c2ecf20Sopenharmony_ci		confc4 = 0;
4818c2ecf20Sopenharmony_ci	} else {
4828c2ecf20Sopenharmony_ci		dprintk("%s: 16MHz Xtal, reducing I2C speed\n", __func__);
4838c2ecf20Sopenharmony_ci		confc4 = 0x80;
4848c2ecf20Sopenharmony_ci	}
4858c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_CONFC4, confc4);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 1, 0);
4888c2ecf20Sopenharmony_ci	/* set GPIO 1 and 3 */
4898c2ecf20Sopenharmony_ci	if (state->config->gpio_config != TDA10046_GPTRI) {
4908c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE2, 0x33);
4918c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f, state->config->gpio_config &0x0f);
4928c2ecf20Sopenharmony_ci	}
4938c2ecf20Sopenharmony_ci	/* let the clocks recover from sleep */
4948c2ecf20Sopenharmony_ci	msleep(10);
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_ci	/* The PLLs need to be reprogrammed after sleep */
4978c2ecf20Sopenharmony_ci	tda10046_init_plls(fe);
4988c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0);
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_ci	/* don't re-upload unless necessary */
5018c2ecf20Sopenharmony_ci	if (tda1004x_check_upload_ok(state) == 0)
5028c2ecf20Sopenharmony_ci		return 0;
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_ci	/*
5058c2ecf20Sopenharmony_ci	   For i2c normal work, we need to slow down the bus speed.
5068c2ecf20Sopenharmony_ci	   However, the slow down breaks the eeprom firmware load.
5078c2ecf20Sopenharmony_ci	   So, use normal speed for eeprom booting and then restore the
5088c2ecf20Sopenharmony_ci	   i2c speed after that. Tested with MSI TV @nyware A/D board,
5098c2ecf20Sopenharmony_ci	   that comes with firmware version 29 inside their eeprom.
5108c2ecf20Sopenharmony_ci
5118c2ecf20Sopenharmony_ci	   It should also be noticed that no other I2C transfer should
5128c2ecf20Sopenharmony_ci	   be in course while booting from eeprom, otherwise, tda10046
5138c2ecf20Sopenharmony_ci	   goes into an instable state. So, proper locking are needed
5148c2ecf20Sopenharmony_ci	   at the i2c bus master.
5158c2ecf20Sopenharmony_ci	 */
5168c2ecf20Sopenharmony_ci	printk(KERN_INFO "tda1004x: trying to boot from eeprom\n");
5178c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_CONFC4, 4);
5188c2ecf20Sopenharmony_ci	msleep(300);
5198c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_CONFC4, confc4);
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_ci	/* Checks if eeprom firmware went without troubles */
5228c2ecf20Sopenharmony_ci	if (tda1004x_check_upload_ok(state) == 0)
5238c2ecf20Sopenharmony_ci		return 0;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	/* eeprom firmware didn't work. Load one manually. */
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (state->config->request_firmware != NULL) {
5288c2ecf20Sopenharmony_ci		/* request the firmware, this will block until someone uploads it */
5298c2ecf20Sopenharmony_ci		printk(KERN_INFO "tda1004x: waiting for firmware upload...\n");
5308c2ecf20Sopenharmony_ci		ret = state->config->request_firmware(fe, &fw, TDA10046_DEFAULT_FIRMWARE);
5318c2ecf20Sopenharmony_ci		if (ret) {
5328c2ecf20Sopenharmony_ci			/* remain compatible to old bug: try to load with tda10045 image name */
5338c2ecf20Sopenharmony_ci			ret = state->config->request_firmware(fe, &fw, TDA10045_DEFAULT_FIRMWARE);
5348c2ecf20Sopenharmony_ci			if (ret) {
5358c2ecf20Sopenharmony_ci				printk(KERN_ERR "tda1004x: no firmware upload (timeout or file not found?)\n");
5368c2ecf20Sopenharmony_ci				return ret;
5378c2ecf20Sopenharmony_ci			} else {
5388c2ecf20Sopenharmony_ci				printk(KERN_INFO "tda1004x: please rename the firmware file to %s\n",
5398c2ecf20Sopenharmony_ci						  TDA10046_DEFAULT_FIRMWARE);
5408c2ecf20Sopenharmony_ci			}
5418c2ecf20Sopenharmony_ci		}
5428c2ecf20Sopenharmony_ci	} else {
5438c2ecf20Sopenharmony_ci		printk(KERN_ERR "tda1004x: no request function defined, can't upload from file\n");
5448c2ecf20Sopenharmony_ci		return -EIO;
5458c2ecf20Sopenharmony_ci	}
5468c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8); // going to boot from HOST
5478c2ecf20Sopenharmony_ci	ret = tda1004x_do_upload(state, fw->data, fw->size, TDA10046H_CODE_CPT, TDA10046H_CODE_IN);
5488c2ecf20Sopenharmony_ci	release_firmware(fw);
5498c2ecf20Sopenharmony_ci	return tda1004x_check_upload_ok(state);
5508c2ecf20Sopenharmony_ci}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_cistatic int tda1004x_encode_fec(int fec)
5538c2ecf20Sopenharmony_ci{
5548c2ecf20Sopenharmony_ci	// convert known FEC values
5558c2ecf20Sopenharmony_ci	switch (fec) {
5568c2ecf20Sopenharmony_ci	case FEC_1_2:
5578c2ecf20Sopenharmony_ci		return 0;
5588c2ecf20Sopenharmony_ci	case FEC_2_3:
5598c2ecf20Sopenharmony_ci		return 1;
5608c2ecf20Sopenharmony_ci	case FEC_3_4:
5618c2ecf20Sopenharmony_ci		return 2;
5628c2ecf20Sopenharmony_ci	case FEC_5_6:
5638c2ecf20Sopenharmony_ci		return 3;
5648c2ecf20Sopenharmony_ci	case FEC_7_8:
5658c2ecf20Sopenharmony_ci		return 4;
5668c2ecf20Sopenharmony_ci	}
5678c2ecf20Sopenharmony_ci
5688c2ecf20Sopenharmony_ci	// unsupported
5698c2ecf20Sopenharmony_ci	return -EINVAL;
5708c2ecf20Sopenharmony_ci}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistatic int tda1004x_decode_fec(int tdafec)
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci	// convert known FEC values
5758c2ecf20Sopenharmony_ci	switch (tdafec) {
5768c2ecf20Sopenharmony_ci	case 0:
5778c2ecf20Sopenharmony_ci		return FEC_1_2;
5788c2ecf20Sopenharmony_ci	case 1:
5798c2ecf20Sopenharmony_ci		return FEC_2_3;
5808c2ecf20Sopenharmony_ci	case 2:
5818c2ecf20Sopenharmony_ci		return FEC_3_4;
5828c2ecf20Sopenharmony_ci	case 3:
5838c2ecf20Sopenharmony_ci		return FEC_5_6;
5848c2ecf20Sopenharmony_ci	case 4:
5858c2ecf20Sopenharmony_ci		return FEC_7_8;
5868c2ecf20Sopenharmony_ci	}
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	// unsupported
5898c2ecf20Sopenharmony_ci	return -1;
5908c2ecf20Sopenharmony_ci}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_cistatic int tda1004x_write(struct dvb_frontend* fe, const u8 buf[], int len)
5938c2ecf20Sopenharmony_ci{
5948c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_ci	if (len != 2)
5978c2ecf20Sopenharmony_ci		return -EINVAL;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	return tda1004x_write_byteI(state, buf[0], buf[1]);
6008c2ecf20Sopenharmony_ci}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_cistatic int tda10045_init(struct dvb_frontend* fe)
6038c2ecf20Sopenharmony_ci{
6048c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	dprintk("%s\n", __func__);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	if (tda10045_fwupload(fe)) {
6098c2ecf20Sopenharmony_ci		printk("tda1004x: firmware upload failed\n");
6108c2ecf20Sopenharmony_ci		return -EIO;
6118c2ecf20Sopenharmony_ci	}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0); // wake up the ADC
6148c2ecf20Sopenharmony_ci
6158c2ecf20Sopenharmony_ci	// tda setup
6168c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
6178c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_AUTO, 8, 0); // select HP stream
6188c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFC1, 0x40, 0); // set polarity of VAGC signal
6198c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFC1, 0x80, 0x80); // enable pulse killer
6208c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10); // enable auto offset
6218c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0x0); // no frequency offset
6228c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 0); // setup MPEG2 TS interface
6238c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0); // setup MPEG2 TS interface
6248c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_VBER_MSB, 0xe0, 0xa0); // 10^6 VBER measurement bits
6258c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFC1, 0x10, 0); // VAGC polarity
6268c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_CONFADC1, 0x2e);
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, 0x1f, 0x01, state->config->invert_oclk);
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_ci	return 0;
6318c2ecf20Sopenharmony_ci}
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_cistatic int tda10046_init(struct dvb_frontend* fe)
6348c2ecf20Sopenharmony_ci{
6358c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
6368c2ecf20Sopenharmony_ci	dprintk("%s\n", __func__);
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	if (tda10046_fwupload(fe)) {
6398c2ecf20Sopenharmony_ci		printk("tda1004x: firmware upload failed\n");
6408c2ecf20Sopenharmony_ci		return -EIO;
6418c2ecf20Sopenharmony_ci	}
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	// tda setup
6448c2ecf20Sopenharmony_ci	tda1004x_write_mask(state, TDA1004X_CONFC4, 0x20, 0); // disable DSP watchdog timer
6458c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_AUTO, 0x87);    // 100 ppm crystal, select HP stream
6468c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_CONFC1, 0x88);      // enable pulse killer
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	switch (state->config->agc_config) {
6498c2ecf20Sopenharmony_ci	case TDA10046_AGC_DEFAULT:
6508c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x00); // AGC setup
6518c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60);  // set AGC polarities
6528c2ecf20Sopenharmony_ci		break;
6538c2ecf20Sopenharmony_ci	case TDA10046_AGC_IFO_AUTO_NEG:
6548c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
6558c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60);  // set AGC polarities
6568c2ecf20Sopenharmony_ci		break;
6578c2ecf20Sopenharmony_ci	case TDA10046_AGC_IFO_AUTO_POS:
6588c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x0a); // AGC setup
6598c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x00);  // set AGC polarities
6608c2ecf20Sopenharmony_ci		break;
6618c2ecf20Sopenharmony_ci	case TDA10046_AGC_TDA827X:
6628c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_AGC_CONF, 0x02);   // AGC setup
6638c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_AGC_THR, 0x70);    // AGC Threshold
6648c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_AGC_RENORM, 0x08); // Gain Renormalize
6658c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0xf0, 0x60);  // set AGC polarities
6668c2ecf20Sopenharmony_ci		break;
6678c2ecf20Sopenharmony_ci	}
6688c2ecf20Sopenharmony_ci	if (state->config->ts_mode == 0) {
6698c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x40);
6708c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, 0x3a, 0x80, state->config->invert_oclk << 7);
6718c2ecf20Sopenharmony_ci	} else {
6728c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA10046H_CONF_TRISTATE1, 0xc0, 0x80);
6738c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x10,
6748c2ecf20Sopenharmony_ci							state->config->invert_oclk << 4);
6758c2ecf20Sopenharmony_ci	}
6768c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_CONFADC2, 0x38);
6778c2ecf20Sopenharmony_ci	tda1004x_write_mask (state, TDA10046H_CONF_TRISTATE1, 0x3e, 0x38); // Turn IF AGC output on
6788c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MIN, 0);	  // }
6798c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA10046H_AGC_TUN_MAX, 0xff); // } AGC min/max values
6808c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA10046H_AGC_IF_MIN, 0);	  // }
6818c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA10046H_AGC_IF_MAX, 0xff);  // }
6828c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA10046H_AGC_GAINS, 0x12); // IF gain 2, TUN gain 1
6838c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA10046H_CVBER_CTRL, 0x1a); // 10^6 VBER measurement bits
6848c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_CONF_TS1, 7); // MPEG2 interface config
6858c2ecf20Sopenharmony_ci	tda1004x_write_byteI(state, TDA1004X_CONF_TS2, 0xc0); // MPEG2 interface config
6868c2ecf20Sopenharmony_ci	// tda1004x_write_mask(state, 0x50, 0x80, 0x80);         // handle out of guard echoes
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_ci	return 0;
6898c2ecf20Sopenharmony_ci}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_cistatic int tda1004x_set_fe(struct dvb_frontend *fe)
6928c2ecf20Sopenharmony_ci{
6938c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *fe_params = &fe->dtv_property_cache;
6948c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
6958c2ecf20Sopenharmony_ci	int tmp;
6968c2ecf20Sopenharmony_ci	int inversion;
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_ci	dprintk("%s\n", __func__);
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	if (state->demod_type == TDA1004X_DEMOD_TDA10046) {
7018c2ecf20Sopenharmony_ci		// setup auto offset
7028c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 0x10, 0x10);
7038c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x80, 0);
7048c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0xC0, 0);
7058c2ecf20Sopenharmony_ci
7068c2ecf20Sopenharmony_ci		// disable agc_conf[2]
7078c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 0);
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	// set frequency
7118c2ecf20Sopenharmony_ci	if (fe->ops.tuner_ops.set_params) {
7128c2ecf20Sopenharmony_ci		fe->ops.tuner_ops.set_params(fe);
7138c2ecf20Sopenharmony_ci		if (fe->ops.i2c_gate_ctrl)
7148c2ecf20Sopenharmony_ci			fe->ops.i2c_gate_ctrl(fe, 0);
7158c2ecf20Sopenharmony_ci	}
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_ci	// Hardcoded to use auto as much as possible on the TDA10045 as it
7188c2ecf20Sopenharmony_ci	// is very unreliable if AUTO mode is _not_ used.
7198c2ecf20Sopenharmony_ci	if (state->demod_type == TDA1004X_DEMOD_TDA10045) {
7208c2ecf20Sopenharmony_ci		fe_params->code_rate_HP = FEC_AUTO;
7218c2ecf20Sopenharmony_ci		fe_params->guard_interval = GUARD_INTERVAL_AUTO;
7228c2ecf20Sopenharmony_ci		fe_params->transmission_mode = TRANSMISSION_MODE_AUTO;
7238c2ecf20Sopenharmony_ci	}
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci	// Set standard params.. or put them to auto
7268c2ecf20Sopenharmony_ci	if ((fe_params->code_rate_HP == FEC_AUTO) ||
7278c2ecf20Sopenharmony_ci		(fe_params->code_rate_LP == FEC_AUTO) ||
7288c2ecf20Sopenharmony_ci		(fe_params->modulation == QAM_AUTO) ||
7298c2ecf20Sopenharmony_ci		(fe_params->hierarchy == HIERARCHY_AUTO)) {
7308c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 1, 1);	// enable auto
7318c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x03, 0);	/* turn off modulation bits */
7328c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0);	// turn off hierarchy bits
7338c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x3f, 0);	// turn off FEC bits
7348c2ecf20Sopenharmony_ci	} else {
7358c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 1, 0);	// disable auto
7368c2ecf20Sopenharmony_ci
7378c2ecf20Sopenharmony_ci		// set HP FEC
7388c2ecf20Sopenharmony_ci		tmp = tda1004x_encode_fec(fe_params->code_rate_HP);
7398c2ecf20Sopenharmony_ci		if (tmp < 0)
7408c2ecf20Sopenharmony_ci			return tmp;
7418c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF2, 7, tmp);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci		// set LP FEC
7448c2ecf20Sopenharmony_ci		tmp = tda1004x_encode_fec(fe_params->code_rate_LP);
7458c2ecf20Sopenharmony_ci		if (tmp < 0)
7468c2ecf20Sopenharmony_ci			return tmp;
7478c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF2, 0x38, tmp << 3);
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci		/* set modulation */
7508c2ecf20Sopenharmony_ci		switch (fe_params->modulation) {
7518c2ecf20Sopenharmony_ci		case QPSK:
7528c2ecf20Sopenharmony_ci			tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 0);
7538c2ecf20Sopenharmony_ci			break;
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci		case QAM_16:
7568c2ecf20Sopenharmony_ci			tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 1);
7578c2ecf20Sopenharmony_ci			break;
7588c2ecf20Sopenharmony_ci
7598c2ecf20Sopenharmony_ci		case QAM_64:
7608c2ecf20Sopenharmony_ci			tda1004x_write_mask(state, TDA1004X_IN_CONF1, 3, 2);
7618c2ecf20Sopenharmony_ci			break;
7628c2ecf20Sopenharmony_ci
7638c2ecf20Sopenharmony_ci		default:
7648c2ecf20Sopenharmony_ci			return -EINVAL;
7658c2ecf20Sopenharmony_ci		}
7668c2ecf20Sopenharmony_ci
7678c2ecf20Sopenharmony_ci		// set hierarchy
7688c2ecf20Sopenharmony_ci		switch (fe_params->hierarchy) {
7698c2ecf20Sopenharmony_ci		case HIERARCHY_NONE:
7708c2ecf20Sopenharmony_ci			tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 0 << 5);
7718c2ecf20Sopenharmony_ci			break;
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci		case HIERARCHY_1:
7748c2ecf20Sopenharmony_ci			tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 1 << 5);
7758c2ecf20Sopenharmony_ci			break;
7768c2ecf20Sopenharmony_ci
7778c2ecf20Sopenharmony_ci		case HIERARCHY_2:
7788c2ecf20Sopenharmony_ci			tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 2 << 5);
7798c2ecf20Sopenharmony_ci			break;
7808c2ecf20Sopenharmony_ci
7818c2ecf20Sopenharmony_ci		case HIERARCHY_4:
7828c2ecf20Sopenharmony_ci			tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x60, 3 << 5);
7838c2ecf20Sopenharmony_ci			break;
7848c2ecf20Sopenharmony_ci
7858c2ecf20Sopenharmony_ci		default:
7868c2ecf20Sopenharmony_ci			return -EINVAL;
7878c2ecf20Sopenharmony_ci		}
7888c2ecf20Sopenharmony_ci	}
7898c2ecf20Sopenharmony_ci
7908c2ecf20Sopenharmony_ci	// set bandwidth
7918c2ecf20Sopenharmony_ci	switch (state->demod_type) {
7928c2ecf20Sopenharmony_ci	case TDA1004X_DEMOD_TDA10045:
7938c2ecf20Sopenharmony_ci		tda10045h_set_bandwidth(state, fe_params->bandwidth_hz);
7948c2ecf20Sopenharmony_ci		break;
7958c2ecf20Sopenharmony_ci
7968c2ecf20Sopenharmony_ci	case TDA1004X_DEMOD_TDA10046:
7978c2ecf20Sopenharmony_ci		tda10046h_set_bandwidth(state, fe_params->bandwidth_hz);
7988c2ecf20Sopenharmony_ci		break;
7998c2ecf20Sopenharmony_ci	}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	// set inversion
8028c2ecf20Sopenharmony_ci	inversion = fe_params->inversion;
8038c2ecf20Sopenharmony_ci	if (state->config->invert)
8048c2ecf20Sopenharmony_ci		inversion = inversion ? INVERSION_OFF : INVERSION_ON;
8058c2ecf20Sopenharmony_ci	switch (inversion) {
8068c2ecf20Sopenharmony_ci	case INVERSION_OFF:
8078c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0);
8088c2ecf20Sopenharmony_ci		break;
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	case INVERSION_ON:
8118c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_CONFC1, 0x20, 0x20);
8128c2ecf20Sopenharmony_ci		break;
8138c2ecf20Sopenharmony_ci
8148c2ecf20Sopenharmony_ci	default:
8158c2ecf20Sopenharmony_ci		return -EINVAL;
8168c2ecf20Sopenharmony_ci	}
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	// set guard interval
8198c2ecf20Sopenharmony_ci	switch (fe_params->guard_interval) {
8208c2ecf20Sopenharmony_ci	case GUARD_INTERVAL_1_32:
8218c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
8228c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
8238c2ecf20Sopenharmony_ci		break;
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_ci	case GUARD_INTERVAL_1_16:
8268c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
8278c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 1 << 2);
8288c2ecf20Sopenharmony_ci		break;
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	case GUARD_INTERVAL_1_8:
8318c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
8328c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 2 << 2);
8338c2ecf20Sopenharmony_ci		break;
8348c2ecf20Sopenharmony_ci
8358c2ecf20Sopenharmony_ci	case GUARD_INTERVAL_1_4:
8368c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 2, 0);
8378c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 3 << 2);
8388c2ecf20Sopenharmony_ci		break;
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	case GUARD_INTERVAL_AUTO:
8418c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 2, 2);
8428c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x0c, 0 << 2);
8438c2ecf20Sopenharmony_ci		break;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	default:
8468c2ecf20Sopenharmony_ci		return -EINVAL;
8478c2ecf20Sopenharmony_ci	}
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	// set transmission mode
8508c2ecf20Sopenharmony_ci	switch (fe_params->transmission_mode) {
8518c2ecf20Sopenharmony_ci	case TRANSMISSION_MODE_2K:
8528c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
8538c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0 << 4);
8548c2ecf20Sopenharmony_ci		break;
8558c2ecf20Sopenharmony_ci
8568c2ecf20Sopenharmony_ci	case TRANSMISSION_MODE_8K:
8578c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 4, 0);
8588c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 1 << 4);
8598c2ecf20Sopenharmony_ci		break;
8608c2ecf20Sopenharmony_ci
8618c2ecf20Sopenharmony_ci	case TRANSMISSION_MODE_AUTO:
8628c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 4, 4);
8638c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_IN_CONF1, 0x10, 0);
8648c2ecf20Sopenharmony_ci		break;
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_ci	default:
8678c2ecf20Sopenharmony_ci		return -EINVAL;
8688c2ecf20Sopenharmony_ci	}
8698c2ecf20Sopenharmony_ci
8708c2ecf20Sopenharmony_ci	// start the lock
8718c2ecf20Sopenharmony_ci	switch (state->demod_type) {
8728c2ecf20Sopenharmony_ci	case TDA1004X_DEMOD_TDA10045:
8738c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 8);
8748c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_CONFC4, 8, 0);
8758c2ecf20Sopenharmony_ci		break;
8768c2ecf20Sopenharmony_ci
8778c2ecf20Sopenharmony_ci	case TDA1004X_DEMOD_TDA10046:
8788c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_AUTO, 0x40, 0x40);
8798c2ecf20Sopenharmony_ci		msleep(1);
8808c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA10046H_AGC_CONF, 4, 1);
8818c2ecf20Sopenharmony_ci		break;
8828c2ecf20Sopenharmony_ci	}
8838c2ecf20Sopenharmony_ci
8848c2ecf20Sopenharmony_ci	msleep(10);
8858c2ecf20Sopenharmony_ci
8868c2ecf20Sopenharmony_ci	return 0;
8878c2ecf20Sopenharmony_ci}
8888c2ecf20Sopenharmony_ci
8898c2ecf20Sopenharmony_cistatic int tda1004x_get_fe(struct dvb_frontend *fe,
8908c2ecf20Sopenharmony_ci			   struct dtv_frontend_properties *fe_params)
8918c2ecf20Sopenharmony_ci{
8928c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
8938c2ecf20Sopenharmony_ci	int status;
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	dprintk("%s\n", __func__);
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci	status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
8988c2ecf20Sopenharmony_ci	if (status == -1)
8998c2ecf20Sopenharmony_ci		return -EIO;
9008c2ecf20Sopenharmony_ci
9018c2ecf20Sopenharmony_ci	/* Only update the properties cache if device is locked */
9028c2ecf20Sopenharmony_ci	if (!(status & 8))
9038c2ecf20Sopenharmony_ci		return 0;
9048c2ecf20Sopenharmony_ci
9058c2ecf20Sopenharmony_ci	// inversion status
9068c2ecf20Sopenharmony_ci	fe_params->inversion = INVERSION_OFF;
9078c2ecf20Sopenharmony_ci	if (tda1004x_read_byte(state, TDA1004X_CONFC1) & 0x20)
9088c2ecf20Sopenharmony_ci		fe_params->inversion = INVERSION_ON;
9098c2ecf20Sopenharmony_ci	if (state->config->invert)
9108c2ecf20Sopenharmony_ci		fe_params->inversion = fe_params->inversion ? INVERSION_OFF : INVERSION_ON;
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	// bandwidth
9138c2ecf20Sopenharmony_ci	switch (state->demod_type) {
9148c2ecf20Sopenharmony_ci	case TDA1004X_DEMOD_TDA10045:
9158c2ecf20Sopenharmony_ci		switch (tda1004x_read_byte(state, TDA10045H_WREF_LSB)) {
9168c2ecf20Sopenharmony_ci		case 0x14:
9178c2ecf20Sopenharmony_ci			fe_params->bandwidth_hz = 8000000;
9188c2ecf20Sopenharmony_ci			break;
9198c2ecf20Sopenharmony_ci		case 0xdb:
9208c2ecf20Sopenharmony_ci			fe_params->bandwidth_hz = 7000000;
9218c2ecf20Sopenharmony_ci			break;
9228c2ecf20Sopenharmony_ci		case 0x4f:
9238c2ecf20Sopenharmony_ci			fe_params->bandwidth_hz = 6000000;
9248c2ecf20Sopenharmony_ci			break;
9258c2ecf20Sopenharmony_ci		}
9268c2ecf20Sopenharmony_ci		break;
9278c2ecf20Sopenharmony_ci	case TDA1004X_DEMOD_TDA10046:
9288c2ecf20Sopenharmony_ci		switch (tda1004x_read_byte(state, TDA10046H_TIME_WREF1)) {
9298c2ecf20Sopenharmony_ci		case 0x5c:
9308c2ecf20Sopenharmony_ci		case 0x54:
9318c2ecf20Sopenharmony_ci			fe_params->bandwidth_hz = 8000000;
9328c2ecf20Sopenharmony_ci			break;
9338c2ecf20Sopenharmony_ci		case 0x6a:
9348c2ecf20Sopenharmony_ci		case 0x60:
9358c2ecf20Sopenharmony_ci			fe_params->bandwidth_hz = 7000000;
9368c2ecf20Sopenharmony_ci			break;
9378c2ecf20Sopenharmony_ci		case 0x7b:
9388c2ecf20Sopenharmony_ci		case 0x70:
9398c2ecf20Sopenharmony_ci			fe_params->bandwidth_hz = 6000000;
9408c2ecf20Sopenharmony_ci			break;
9418c2ecf20Sopenharmony_ci		}
9428c2ecf20Sopenharmony_ci		break;
9438c2ecf20Sopenharmony_ci	}
9448c2ecf20Sopenharmony_ci
9458c2ecf20Sopenharmony_ci	// FEC
9468c2ecf20Sopenharmony_ci	fe_params->code_rate_HP =
9478c2ecf20Sopenharmony_ci	    tda1004x_decode_fec(tda1004x_read_byte(state, TDA1004X_OUT_CONF2) & 7);
9488c2ecf20Sopenharmony_ci	fe_params->code_rate_LP =
9498c2ecf20Sopenharmony_ci	    tda1004x_decode_fec((tda1004x_read_byte(state, TDA1004X_OUT_CONF2) >> 3) & 7);
9508c2ecf20Sopenharmony_ci
9518c2ecf20Sopenharmony_ci	/* modulation */
9528c2ecf20Sopenharmony_ci	switch (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 3) {
9538c2ecf20Sopenharmony_ci	case 0:
9548c2ecf20Sopenharmony_ci		fe_params->modulation = QPSK;
9558c2ecf20Sopenharmony_ci		break;
9568c2ecf20Sopenharmony_ci	case 1:
9578c2ecf20Sopenharmony_ci		fe_params->modulation = QAM_16;
9588c2ecf20Sopenharmony_ci		break;
9598c2ecf20Sopenharmony_ci	case 2:
9608c2ecf20Sopenharmony_ci		fe_params->modulation = QAM_64;
9618c2ecf20Sopenharmony_ci		break;
9628c2ecf20Sopenharmony_ci	}
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	// transmission mode
9658c2ecf20Sopenharmony_ci	fe_params->transmission_mode = TRANSMISSION_MODE_2K;
9668c2ecf20Sopenharmony_ci	if (tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x10)
9678c2ecf20Sopenharmony_ci		fe_params->transmission_mode = TRANSMISSION_MODE_8K;
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	// guard interval
9708c2ecf20Sopenharmony_ci	switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x0c) >> 2) {
9718c2ecf20Sopenharmony_ci	case 0:
9728c2ecf20Sopenharmony_ci		fe_params->guard_interval = GUARD_INTERVAL_1_32;
9738c2ecf20Sopenharmony_ci		break;
9748c2ecf20Sopenharmony_ci	case 1:
9758c2ecf20Sopenharmony_ci		fe_params->guard_interval = GUARD_INTERVAL_1_16;
9768c2ecf20Sopenharmony_ci		break;
9778c2ecf20Sopenharmony_ci	case 2:
9788c2ecf20Sopenharmony_ci		fe_params->guard_interval = GUARD_INTERVAL_1_8;
9798c2ecf20Sopenharmony_ci		break;
9808c2ecf20Sopenharmony_ci	case 3:
9818c2ecf20Sopenharmony_ci		fe_params->guard_interval = GUARD_INTERVAL_1_4;
9828c2ecf20Sopenharmony_ci		break;
9838c2ecf20Sopenharmony_ci	}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_ci	// hierarchy
9868c2ecf20Sopenharmony_ci	switch ((tda1004x_read_byte(state, TDA1004X_OUT_CONF1) & 0x60) >> 5) {
9878c2ecf20Sopenharmony_ci	case 0:
9888c2ecf20Sopenharmony_ci		fe_params->hierarchy = HIERARCHY_NONE;
9898c2ecf20Sopenharmony_ci		break;
9908c2ecf20Sopenharmony_ci	case 1:
9918c2ecf20Sopenharmony_ci		fe_params->hierarchy = HIERARCHY_1;
9928c2ecf20Sopenharmony_ci		break;
9938c2ecf20Sopenharmony_ci	case 2:
9948c2ecf20Sopenharmony_ci		fe_params->hierarchy = HIERARCHY_2;
9958c2ecf20Sopenharmony_ci		break;
9968c2ecf20Sopenharmony_ci	case 3:
9978c2ecf20Sopenharmony_ci		fe_params->hierarchy = HIERARCHY_4;
9988c2ecf20Sopenharmony_ci		break;
9998c2ecf20Sopenharmony_ci	}
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	return 0;
10028c2ecf20Sopenharmony_ci}
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_cistatic int tda1004x_read_status(struct dvb_frontend *fe,
10058c2ecf20Sopenharmony_ci				enum fe_status *fe_status)
10068c2ecf20Sopenharmony_ci{
10078c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
10088c2ecf20Sopenharmony_ci	int status;
10098c2ecf20Sopenharmony_ci	int cber;
10108c2ecf20Sopenharmony_ci	int vber;
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_ci	dprintk("%s\n", __func__);
10138c2ecf20Sopenharmony_ci
10148c2ecf20Sopenharmony_ci	// read status
10158c2ecf20Sopenharmony_ci	status = tda1004x_read_byte(state, TDA1004X_STATUS_CD);
10168c2ecf20Sopenharmony_ci	if (status == -1)
10178c2ecf20Sopenharmony_ci		return -EIO;
10188c2ecf20Sopenharmony_ci
10198c2ecf20Sopenharmony_ci	// decode
10208c2ecf20Sopenharmony_ci	*fe_status = 0;
10218c2ecf20Sopenharmony_ci	if (status & 4)
10228c2ecf20Sopenharmony_ci		*fe_status |= FE_HAS_SIGNAL;
10238c2ecf20Sopenharmony_ci	if (status & 2)
10248c2ecf20Sopenharmony_ci		*fe_status |= FE_HAS_CARRIER;
10258c2ecf20Sopenharmony_ci	if (status & 8)
10268c2ecf20Sopenharmony_ci		*fe_status |= FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
10278c2ecf20Sopenharmony_ci
10288c2ecf20Sopenharmony_ci	// if we don't already have VITERBI (i.e. not LOCKED), see if the viterbi
10298c2ecf20Sopenharmony_ci	// is getting anything valid
10308c2ecf20Sopenharmony_ci	if (!(*fe_status & FE_HAS_VITERBI)) {
10318c2ecf20Sopenharmony_ci		// read the CBER
10328c2ecf20Sopenharmony_ci		cber = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
10338c2ecf20Sopenharmony_ci		if (cber == -1)
10348c2ecf20Sopenharmony_ci			return -EIO;
10358c2ecf20Sopenharmony_ci		status = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
10368c2ecf20Sopenharmony_ci		if (status == -1)
10378c2ecf20Sopenharmony_ci			return -EIO;
10388c2ecf20Sopenharmony_ci		cber |= (status << 8);
10398c2ecf20Sopenharmony_ci		// The address 0x20 should be read to cope with a TDA10046 bug
10408c2ecf20Sopenharmony_ci		tda1004x_read_byte(state, TDA1004X_CBER_RESET);
10418c2ecf20Sopenharmony_ci
10428c2ecf20Sopenharmony_ci		if (cber != 65535)
10438c2ecf20Sopenharmony_ci			*fe_status |= FE_HAS_VITERBI;
10448c2ecf20Sopenharmony_ci	}
10458c2ecf20Sopenharmony_ci
10468c2ecf20Sopenharmony_ci	// if we DO have some valid VITERBI output, but don't already have SYNC
10478c2ecf20Sopenharmony_ci	// bytes (i.e. not LOCKED), see if the RS decoder is getting anything valid.
10488c2ecf20Sopenharmony_ci	if ((*fe_status & FE_HAS_VITERBI) && (!(*fe_status & FE_HAS_SYNC))) {
10498c2ecf20Sopenharmony_ci		// read the VBER
10508c2ecf20Sopenharmony_ci		vber = tda1004x_read_byte(state, TDA1004X_VBER_LSB);
10518c2ecf20Sopenharmony_ci		if (vber == -1)
10528c2ecf20Sopenharmony_ci			return -EIO;
10538c2ecf20Sopenharmony_ci		status = tda1004x_read_byte(state, TDA1004X_VBER_MID);
10548c2ecf20Sopenharmony_ci		if (status == -1)
10558c2ecf20Sopenharmony_ci			return -EIO;
10568c2ecf20Sopenharmony_ci		vber |= (status << 8);
10578c2ecf20Sopenharmony_ci		status = tda1004x_read_byte(state, TDA1004X_VBER_MSB);
10588c2ecf20Sopenharmony_ci		if (status == -1)
10598c2ecf20Sopenharmony_ci			return -EIO;
10608c2ecf20Sopenharmony_ci		vber |= (status & 0x0f) << 16;
10618c2ecf20Sopenharmony_ci		// The CVBER_LUT should be read to cope with TDA10046 hardware bug
10628c2ecf20Sopenharmony_ci		tda1004x_read_byte(state, TDA1004X_CVBER_LUT);
10638c2ecf20Sopenharmony_ci
10648c2ecf20Sopenharmony_ci		// if RS has passed some valid TS packets, then we must be
10658c2ecf20Sopenharmony_ci		// getting some SYNC bytes
10668c2ecf20Sopenharmony_ci		if (vber < 16632)
10678c2ecf20Sopenharmony_ci			*fe_status |= FE_HAS_SYNC;
10688c2ecf20Sopenharmony_ci	}
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_ci	// success
10718c2ecf20Sopenharmony_ci	dprintk("%s: fe_status=0x%x\n", __func__, *fe_status);
10728c2ecf20Sopenharmony_ci	return 0;
10738c2ecf20Sopenharmony_ci}
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_cistatic int tda1004x_read_signal_strength(struct dvb_frontend* fe, u16 * signal)
10768c2ecf20Sopenharmony_ci{
10778c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
10788c2ecf20Sopenharmony_ci	int tmp;
10798c2ecf20Sopenharmony_ci	int reg = 0;
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	dprintk("%s\n", __func__);
10828c2ecf20Sopenharmony_ci
10838c2ecf20Sopenharmony_ci	// determine the register to use
10848c2ecf20Sopenharmony_ci	switch (state->demod_type) {
10858c2ecf20Sopenharmony_ci	case TDA1004X_DEMOD_TDA10045:
10868c2ecf20Sopenharmony_ci		reg = TDA10045H_S_AGC;
10878c2ecf20Sopenharmony_ci		break;
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	case TDA1004X_DEMOD_TDA10046:
10908c2ecf20Sopenharmony_ci		reg = TDA10046H_AGC_IF_LEVEL;
10918c2ecf20Sopenharmony_ci		break;
10928c2ecf20Sopenharmony_ci	}
10938c2ecf20Sopenharmony_ci
10948c2ecf20Sopenharmony_ci	// read it
10958c2ecf20Sopenharmony_ci	tmp = tda1004x_read_byte(state, reg);
10968c2ecf20Sopenharmony_ci	if (tmp < 0)
10978c2ecf20Sopenharmony_ci		return -EIO;
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	*signal = (tmp << 8) | tmp;
11008c2ecf20Sopenharmony_ci	dprintk("%s: signal=0x%x\n", __func__, *signal);
11018c2ecf20Sopenharmony_ci	return 0;
11028c2ecf20Sopenharmony_ci}
11038c2ecf20Sopenharmony_ci
11048c2ecf20Sopenharmony_cistatic int tda1004x_read_snr(struct dvb_frontend* fe, u16 * snr)
11058c2ecf20Sopenharmony_ci{
11068c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
11078c2ecf20Sopenharmony_ci	int tmp;
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	dprintk("%s\n", __func__);
11108c2ecf20Sopenharmony_ci
11118c2ecf20Sopenharmony_ci	// read it
11128c2ecf20Sopenharmony_ci	tmp = tda1004x_read_byte(state, TDA1004X_SNR);
11138c2ecf20Sopenharmony_ci	if (tmp < 0)
11148c2ecf20Sopenharmony_ci		return -EIO;
11158c2ecf20Sopenharmony_ci	tmp = 255 - tmp;
11168c2ecf20Sopenharmony_ci
11178c2ecf20Sopenharmony_ci	*snr = ((tmp << 8) | tmp);
11188c2ecf20Sopenharmony_ci	dprintk("%s: snr=0x%x\n", __func__, *snr);
11198c2ecf20Sopenharmony_ci	return 0;
11208c2ecf20Sopenharmony_ci}
11218c2ecf20Sopenharmony_ci
11228c2ecf20Sopenharmony_cistatic int tda1004x_read_ucblocks(struct dvb_frontend* fe, u32* ucblocks)
11238c2ecf20Sopenharmony_ci{
11248c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
11258c2ecf20Sopenharmony_ci	int tmp;
11268c2ecf20Sopenharmony_ci	int tmp2;
11278c2ecf20Sopenharmony_ci	int counter;
11288c2ecf20Sopenharmony_ci
11298c2ecf20Sopenharmony_ci	dprintk("%s\n", __func__);
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci	// read the UCBLOCKS and reset
11328c2ecf20Sopenharmony_ci	counter = 0;
11338c2ecf20Sopenharmony_ci	tmp = tda1004x_read_byte(state, TDA1004X_UNCOR);
11348c2ecf20Sopenharmony_ci	if (tmp < 0)
11358c2ecf20Sopenharmony_ci		return -EIO;
11368c2ecf20Sopenharmony_ci	tmp &= 0x7f;
11378c2ecf20Sopenharmony_ci	while (counter++ < 5) {
11388c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
11398c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
11408c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_UNCOR, 0x80, 0);
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci		tmp2 = tda1004x_read_byte(state, TDA1004X_UNCOR);
11438c2ecf20Sopenharmony_ci		if (tmp2 < 0)
11448c2ecf20Sopenharmony_ci			return -EIO;
11458c2ecf20Sopenharmony_ci		tmp2 &= 0x7f;
11468c2ecf20Sopenharmony_ci		if ((tmp2 < tmp) || (tmp2 == 0))
11478c2ecf20Sopenharmony_ci			break;
11488c2ecf20Sopenharmony_ci	}
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	if (tmp != 0x7f)
11518c2ecf20Sopenharmony_ci		*ucblocks = tmp;
11528c2ecf20Sopenharmony_ci	else
11538c2ecf20Sopenharmony_ci		*ucblocks = 0xffffffff;
11548c2ecf20Sopenharmony_ci
11558c2ecf20Sopenharmony_ci	dprintk("%s: ucblocks=0x%x\n", __func__, *ucblocks);
11568c2ecf20Sopenharmony_ci	return 0;
11578c2ecf20Sopenharmony_ci}
11588c2ecf20Sopenharmony_ci
11598c2ecf20Sopenharmony_cistatic int tda1004x_read_ber(struct dvb_frontend* fe, u32* ber)
11608c2ecf20Sopenharmony_ci{
11618c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
11628c2ecf20Sopenharmony_ci	int tmp;
11638c2ecf20Sopenharmony_ci
11648c2ecf20Sopenharmony_ci	dprintk("%s\n", __func__);
11658c2ecf20Sopenharmony_ci
11668c2ecf20Sopenharmony_ci	// read it in
11678c2ecf20Sopenharmony_ci	tmp = tda1004x_read_byte(state, TDA1004X_CBER_LSB);
11688c2ecf20Sopenharmony_ci	if (tmp < 0)
11698c2ecf20Sopenharmony_ci		return -EIO;
11708c2ecf20Sopenharmony_ci	*ber = tmp << 1;
11718c2ecf20Sopenharmony_ci	tmp = tda1004x_read_byte(state, TDA1004X_CBER_MSB);
11728c2ecf20Sopenharmony_ci	if (tmp < 0)
11738c2ecf20Sopenharmony_ci		return -EIO;
11748c2ecf20Sopenharmony_ci	*ber |= (tmp << 9);
11758c2ecf20Sopenharmony_ci	// The address 0x20 should be read to cope with a TDA10046 bug
11768c2ecf20Sopenharmony_ci	tda1004x_read_byte(state, TDA1004X_CBER_RESET);
11778c2ecf20Sopenharmony_ci
11788c2ecf20Sopenharmony_ci	dprintk("%s: ber=0x%x\n", __func__, *ber);
11798c2ecf20Sopenharmony_ci	return 0;
11808c2ecf20Sopenharmony_ci}
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_cistatic int tda1004x_sleep(struct dvb_frontend* fe)
11838c2ecf20Sopenharmony_ci{
11848c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
11858c2ecf20Sopenharmony_ci	int gpio_conf;
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci	switch (state->demod_type) {
11888c2ecf20Sopenharmony_ci	case TDA1004X_DEMOD_TDA10045:
11898c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_CONFADC1, 0x10, 0x10);
11908c2ecf20Sopenharmony_ci		break;
11918c2ecf20Sopenharmony_ci
11928c2ecf20Sopenharmony_ci	case TDA1004X_DEMOD_TDA10046:
11938c2ecf20Sopenharmony_ci		/* set outputs to tristate */
11948c2ecf20Sopenharmony_ci		tda1004x_write_byteI(state, TDA10046H_CONF_TRISTATE1, 0xff);
11958c2ecf20Sopenharmony_ci		/* invert GPIO 1 and 3 if desired*/
11968c2ecf20Sopenharmony_ci		gpio_conf = state->config->gpio_config;
11978c2ecf20Sopenharmony_ci		if (gpio_conf >= TDA10046_GP00_I)
11988c2ecf20Sopenharmony_ci			tda1004x_write_mask(state, TDA10046H_CONF_POLARITY, 0x0f,
11998c2ecf20Sopenharmony_ci							(gpio_conf & 0x0f) ^ 0x0a);
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_CONFADC2, 0xc0, 0xc0);
12028c2ecf20Sopenharmony_ci		tda1004x_write_mask(state, TDA1004X_CONFC4, 1, 1);
12038c2ecf20Sopenharmony_ci		break;
12048c2ecf20Sopenharmony_ci	}
12058c2ecf20Sopenharmony_ci
12068c2ecf20Sopenharmony_ci	return 0;
12078c2ecf20Sopenharmony_ci}
12088c2ecf20Sopenharmony_ci
12098c2ecf20Sopenharmony_cistatic int tda1004x_i2c_gate_ctrl(struct dvb_frontend* fe, int enable)
12108c2ecf20Sopenharmony_ci{
12118c2ecf20Sopenharmony_ci	struct tda1004x_state* state = fe->demodulator_priv;
12128c2ecf20Sopenharmony_ci
12138c2ecf20Sopenharmony_ci	if (enable) {
12148c2ecf20Sopenharmony_ci		return tda1004x_enable_tuner_i2c(state);
12158c2ecf20Sopenharmony_ci	} else {
12168c2ecf20Sopenharmony_ci		return tda1004x_disable_tuner_i2c(state);
12178c2ecf20Sopenharmony_ci	}
12188c2ecf20Sopenharmony_ci}
12198c2ecf20Sopenharmony_ci
12208c2ecf20Sopenharmony_cistatic int tda1004x_get_tune_settings(struct dvb_frontend* fe, struct dvb_frontend_tune_settings* fesettings)
12218c2ecf20Sopenharmony_ci{
12228c2ecf20Sopenharmony_ci	fesettings->min_delay_ms = 800;
12238c2ecf20Sopenharmony_ci	/* Drift compensation makes no sense for DVB-T */
12248c2ecf20Sopenharmony_ci	fesettings->step_size = 0;
12258c2ecf20Sopenharmony_ci	fesettings->max_drift = 0;
12268c2ecf20Sopenharmony_ci	return 0;
12278c2ecf20Sopenharmony_ci}
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_cistatic void tda1004x_release(struct dvb_frontend* fe)
12308c2ecf20Sopenharmony_ci{
12318c2ecf20Sopenharmony_ci	struct tda1004x_state *state = fe->demodulator_priv;
12328c2ecf20Sopenharmony_ci	kfree(state);
12338c2ecf20Sopenharmony_ci}
12348c2ecf20Sopenharmony_ci
12358c2ecf20Sopenharmony_cistatic const struct dvb_frontend_ops tda10045_ops = {
12368c2ecf20Sopenharmony_ci	.delsys = { SYS_DVBT },
12378c2ecf20Sopenharmony_ci	.info = {
12388c2ecf20Sopenharmony_ci		.name = "Philips TDA10045H DVB-T",
12398c2ecf20Sopenharmony_ci		.frequency_min_hz =  51 * MHz,
12408c2ecf20Sopenharmony_ci		.frequency_max_hz = 858 * MHz,
12418c2ecf20Sopenharmony_ci		.frequency_stepsize_hz = 166667,
12428c2ecf20Sopenharmony_ci		.caps =
12438c2ecf20Sopenharmony_ci		    FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
12448c2ecf20Sopenharmony_ci		    FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
12458c2ecf20Sopenharmony_ci		    FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
12468c2ecf20Sopenharmony_ci		    FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
12478c2ecf20Sopenharmony_ci	},
12488c2ecf20Sopenharmony_ci
12498c2ecf20Sopenharmony_ci	.release = tda1004x_release,
12508c2ecf20Sopenharmony_ci
12518c2ecf20Sopenharmony_ci	.init = tda10045_init,
12528c2ecf20Sopenharmony_ci	.sleep = tda1004x_sleep,
12538c2ecf20Sopenharmony_ci	.write = tda1004x_write,
12548c2ecf20Sopenharmony_ci	.i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
12558c2ecf20Sopenharmony_ci
12568c2ecf20Sopenharmony_ci	.set_frontend = tda1004x_set_fe,
12578c2ecf20Sopenharmony_ci	.get_frontend = tda1004x_get_fe,
12588c2ecf20Sopenharmony_ci	.get_tune_settings = tda1004x_get_tune_settings,
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci	.read_status = tda1004x_read_status,
12618c2ecf20Sopenharmony_ci	.read_ber = tda1004x_read_ber,
12628c2ecf20Sopenharmony_ci	.read_signal_strength = tda1004x_read_signal_strength,
12638c2ecf20Sopenharmony_ci	.read_snr = tda1004x_read_snr,
12648c2ecf20Sopenharmony_ci	.read_ucblocks = tda1004x_read_ucblocks,
12658c2ecf20Sopenharmony_ci};
12668c2ecf20Sopenharmony_ci
12678c2ecf20Sopenharmony_cistruct dvb_frontend* tda10045_attach(const struct tda1004x_config* config,
12688c2ecf20Sopenharmony_ci				     struct i2c_adapter* i2c)
12698c2ecf20Sopenharmony_ci{
12708c2ecf20Sopenharmony_ci	struct tda1004x_state *state;
12718c2ecf20Sopenharmony_ci	int id;
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_ci	/* allocate memory for the internal state */
12748c2ecf20Sopenharmony_ci	state = kzalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
12758c2ecf20Sopenharmony_ci	if (!state) {
12768c2ecf20Sopenharmony_ci		printk(KERN_ERR "Can't allocate memory for tda10045 state\n");
12778c2ecf20Sopenharmony_ci		return NULL;
12788c2ecf20Sopenharmony_ci	}
12798c2ecf20Sopenharmony_ci
12808c2ecf20Sopenharmony_ci	/* setup the state */
12818c2ecf20Sopenharmony_ci	state->config = config;
12828c2ecf20Sopenharmony_ci	state->i2c = i2c;
12838c2ecf20Sopenharmony_ci	state->demod_type = TDA1004X_DEMOD_TDA10045;
12848c2ecf20Sopenharmony_ci
12858c2ecf20Sopenharmony_ci	/* check if the demod is there */
12868c2ecf20Sopenharmony_ci	id = tda1004x_read_byte(state, TDA1004X_CHIPID);
12878c2ecf20Sopenharmony_ci	if (id < 0) {
12888c2ecf20Sopenharmony_ci		printk(KERN_ERR "tda10045: chip is not answering. Giving up.\n");
12898c2ecf20Sopenharmony_ci		kfree(state);
12908c2ecf20Sopenharmony_ci		return NULL;
12918c2ecf20Sopenharmony_ci	}
12928c2ecf20Sopenharmony_ci
12938c2ecf20Sopenharmony_ci	if (id != 0x25) {
12948c2ecf20Sopenharmony_ci		printk(KERN_ERR "Invalid tda1004x ID = 0x%02x. Can't proceed\n", id);
12958c2ecf20Sopenharmony_ci		kfree(state);
12968c2ecf20Sopenharmony_ci		return NULL;
12978c2ecf20Sopenharmony_ci	}
12988c2ecf20Sopenharmony_ci
12998c2ecf20Sopenharmony_ci	/* create dvb_frontend */
13008c2ecf20Sopenharmony_ci	memcpy(&state->frontend.ops, &tda10045_ops, sizeof(struct dvb_frontend_ops));
13018c2ecf20Sopenharmony_ci	state->frontend.demodulator_priv = state;
13028c2ecf20Sopenharmony_ci	return &state->frontend;
13038c2ecf20Sopenharmony_ci}
13048c2ecf20Sopenharmony_ci
13058c2ecf20Sopenharmony_cistatic const struct dvb_frontend_ops tda10046_ops = {
13068c2ecf20Sopenharmony_ci	.delsys = { SYS_DVBT },
13078c2ecf20Sopenharmony_ci	.info = {
13088c2ecf20Sopenharmony_ci		.name = "Philips TDA10046H DVB-T",
13098c2ecf20Sopenharmony_ci		.frequency_min_hz =  51 * MHz,
13108c2ecf20Sopenharmony_ci		.frequency_max_hz = 858 * MHz,
13118c2ecf20Sopenharmony_ci		.frequency_stepsize_hz = 166667,
13128c2ecf20Sopenharmony_ci		.caps =
13138c2ecf20Sopenharmony_ci		    FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
13148c2ecf20Sopenharmony_ci		    FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
13158c2ecf20Sopenharmony_ci		    FE_CAN_QPSK | FE_CAN_QAM_16 | FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
13168c2ecf20Sopenharmony_ci		    FE_CAN_TRANSMISSION_MODE_AUTO | FE_CAN_GUARD_INTERVAL_AUTO
13178c2ecf20Sopenharmony_ci	},
13188c2ecf20Sopenharmony_ci
13198c2ecf20Sopenharmony_ci	.release = tda1004x_release,
13208c2ecf20Sopenharmony_ci
13218c2ecf20Sopenharmony_ci	.init = tda10046_init,
13228c2ecf20Sopenharmony_ci	.sleep = tda1004x_sleep,
13238c2ecf20Sopenharmony_ci	.write = tda1004x_write,
13248c2ecf20Sopenharmony_ci	.i2c_gate_ctrl = tda1004x_i2c_gate_ctrl,
13258c2ecf20Sopenharmony_ci
13268c2ecf20Sopenharmony_ci	.set_frontend = tda1004x_set_fe,
13278c2ecf20Sopenharmony_ci	.get_frontend = tda1004x_get_fe,
13288c2ecf20Sopenharmony_ci	.get_tune_settings = tda1004x_get_tune_settings,
13298c2ecf20Sopenharmony_ci
13308c2ecf20Sopenharmony_ci	.read_status = tda1004x_read_status,
13318c2ecf20Sopenharmony_ci	.read_ber = tda1004x_read_ber,
13328c2ecf20Sopenharmony_ci	.read_signal_strength = tda1004x_read_signal_strength,
13338c2ecf20Sopenharmony_ci	.read_snr = tda1004x_read_snr,
13348c2ecf20Sopenharmony_ci	.read_ucblocks = tda1004x_read_ucblocks,
13358c2ecf20Sopenharmony_ci};
13368c2ecf20Sopenharmony_ci
13378c2ecf20Sopenharmony_cistruct dvb_frontend* tda10046_attach(const struct tda1004x_config* config,
13388c2ecf20Sopenharmony_ci				     struct i2c_adapter* i2c)
13398c2ecf20Sopenharmony_ci{
13408c2ecf20Sopenharmony_ci	struct tda1004x_state *state;
13418c2ecf20Sopenharmony_ci	int id;
13428c2ecf20Sopenharmony_ci
13438c2ecf20Sopenharmony_ci	/* allocate memory for the internal state */
13448c2ecf20Sopenharmony_ci	state = kzalloc(sizeof(struct tda1004x_state), GFP_KERNEL);
13458c2ecf20Sopenharmony_ci	if (!state) {
13468c2ecf20Sopenharmony_ci		printk(KERN_ERR "Can't allocate memory for tda10046 state\n");
13478c2ecf20Sopenharmony_ci		return NULL;
13488c2ecf20Sopenharmony_ci	}
13498c2ecf20Sopenharmony_ci
13508c2ecf20Sopenharmony_ci	/* setup the state */
13518c2ecf20Sopenharmony_ci	state->config = config;
13528c2ecf20Sopenharmony_ci	state->i2c = i2c;
13538c2ecf20Sopenharmony_ci	state->demod_type = TDA1004X_DEMOD_TDA10046;
13548c2ecf20Sopenharmony_ci
13558c2ecf20Sopenharmony_ci	/* check if the demod is there */
13568c2ecf20Sopenharmony_ci	id = tda1004x_read_byte(state, TDA1004X_CHIPID);
13578c2ecf20Sopenharmony_ci	if (id < 0) {
13588c2ecf20Sopenharmony_ci		printk(KERN_ERR "tda10046: chip is not answering. Giving up.\n");
13598c2ecf20Sopenharmony_ci		kfree(state);
13608c2ecf20Sopenharmony_ci		return NULL;
13618c2ecf20Sopenharmony_ci	}
13628c2ecf20Sopenharmony_ci	if (id != 0x46) {
13638c2ecf20Sopenharmony_ci		printk(KERN_ERR "Invalid tda1004x ID = 0x%02x. Can't proceed\n", id);
13648c2ecf20Sopenharmony_ci		kfree(state);
13658c2ecf20Sopenharmony_ci		return NULL;
13668c2ecf20Sopenharmony_ci	}
13678c2ecf20Sopenharmony_ci
13688c2ecf20Sopenharmony_ci	/* create dvb_frontend */
13698c2ecf20Sopenharmony_ci	memcpy(&state->frontend.ops, &tda10046_ops, sizeof(struct dvb_frontend_ops));
13708c2ecf20Sopenharmony_ci	state->frontend.demodulator_priv = state;
13718c2ecf20Sopenharmony_ci	return &state->frontend;
13728c2ecf20Sopenharmony_ci}
13738c2ecf20Sopenharmony_ci
13748c2ecf20Sopenharmony_cimodule_param(debug, int, 0644);
13758c2ecf20Sopenharmony_ciMODULE_PARM_DESC(debug, "Turn on/off frontend debugging (default:off).");
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Philips TDA10045H & TDA10046H DVB-T Demodulator");
13788c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andrew de Quincey & Robert Schlabbach");
13798c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
13808c2ecf20Sopenharmony_ci
13818c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(tda10045_attach);
13828c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(tda10046_attach);
1383