18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * cxd2841er.c
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Sony digital demodulator driver for
68c2ecf20Sopenharmony_ci *	CXD2841ER - DVB-S/S2/T/T2/C/C2
78c2ecf20Sopenharmony_ci *	CXD2854ER - DVB-S/S2/T/T2/C/C2, ISDB-T/S
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright 2012 Sony Corporation
108c2ecf20Sopenharmony_ci * Copyright (C) 2014 NetUP Inc.
118c2ecf20Sopenharmony_ci * Copyright (C) 2014 Sergey Kozlov <serjk@netup.ru>
128c2ecf20Sopenharmony_ci * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
138c2ecf20Sopenharmony_ci  */
148c2ecf20Sopenharmony_ci
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/init.h>
178c2ecf20Sopenharmony_ci#include <linux/string.h>
188c2ecf20Sopenharmony_ci#include <linux/slab.h>
198c2ecf20Sopenharmony_ci#include <linux/bitops.h>
208c2ecf20Sopenharmony_ci#include <linux/math64.h>
218c2ecf20Sopenharmony_ci#include <linux/log2.h>
228c2ecf20Sopenharmony_ci#include <linux/dynamic_debug.h>
238c2ecf20Sopenharmony_ci#include <linux/kernel.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <media/dvb_math.h>
268c2ecf20Sopenharmony_ci#include <media/dvb_frontend.h>
278c2ecf20Sopenharmony_ci#include "cxd2841er.h"
288c2ecf20Sopenharmony_ci#include "cxd2841er_priv.h"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci#define MAX_WRITE_REGSIZE	16
318c2ecf20Sopenharmony_ci#define LOG2_E_100X 144
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci#define INTLOG10X100(x) ((u32) (((u64) intlog10(x) * 100) >> 24))
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci/* DVB-C constellation */
368c2ecf20Sopenharmony_cienum sony_dvbc_constellation_t {
378c2ecf20Sopenharmony_ci	SONY_DVBC_CONSTELLATION_16QAM,
388c2ecf20Sopenharmony_ci	SONY_DVBC_CONSTELLATION_32QAM,
398c2ecf20Sopenharmony_ci	SONY_DVBC_CONSTELLATION_64QAM,
408c2ecf20Sopenharmony_ci	SONY_DVBC_CONSTELLATION_128QAM,
418c2ecf20Sopenharmony_ci	SONY_DVBC_CONSTELLATION_256QAM
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_cienum cxd2841er_state {
458c2ecf20Sopenharmony_ci	STATE_SHUTDOWN = 0,
468c2ecf20Sopenharmony_ci	STATE_SLEEP_S,
478c2ecf20Sopenharmony_ci	STATE_ACTIVE_S,
488c2ecf20Sopenharmony_ci	STATE_SLEEP_TC,
498c2ecf20Sopenharmony_ci	STATE_ACTIVE_TC
508c2ecf20Sopenharmony_ci};
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistruct cxd2841er_priv {
538c2ecf20Sopenharmony_ci	struct dvb_frontend		frontend;
548c2ecf20Sopenharmony_ci	struct i2c_adapter		*i2c;
558c2ecf20Sopenharmony_ci	u8				i2c_addr_slvx;
568c2ecf20Sopenharmony_ci	u8				i2c_addr_slvt;
578c2ecf20Sopenharmony_ci	const struct cxd2841er_config	*config;
588c2ecf20Sopenharmony_ci	enum cxd2841er_state		state;
598c2ecf20Sopenharmony_ci	u8				system;
608c2ecf20Sopenharmony_ci	enum cxd2841er_xtal		xtal;
618c2ecf20Sopenharmony_ci	enum fe_caps caps;
628c2ecf20Sopenharmony_ci	u32				flags;
638c2ecf20Sopenharmony_ci	unsigned long			stats_time;
648c2ecf20Sopenharmony_ci};
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic const struct cxd2841er_cnr_data s_cn_data[] = {
678c2ecf20Sopenharmony_ci	{ 0x033e, 0 }, { 0x0339, 100 }, { 0x0333, 200 },
688c2ecf20Sopenharmony_ci	{ 0x032e, 300 }, { 0x0329, 400 }, { 0x0324, 500 },
698c2ecf20Sopenharmony_ci	{ 0x031e, 600 }, { 0x0319, 700 }, { 0x0314, 800 },
708c2ecf20Sopenharmony_ci	{ 0x030f, 900 }, { 0x030a, 1000 }, { 0x02ff, 1100 },
718c2ecf20Sopenharmony_ci	{ 0x02f4, 1200 }, { 0x02e9, 1300 }, { 0x02de, 1400 },
728c2ecf20Sopenharmony_ci	{ 0x02d4, 1500 }, { 0x02c9, 1600 }, { 0x02bf, 1700 },
738c2ecf20Sopenharmony_ci	{ 0x02b5, 1800 }, { 0x02ab, 1900 }, { 0x02a1, 2000 },
748c2ecf20Sopenharmony_ci	{ 0x029b, 2100 }, { 0x0295, 2200 }, { 0x0290, 2300 },
758c2ecf20Sopenharmony_ci	{ 0x028a, 2400 }, { 0x0284, 2500 }, { 0x027f, 2600 },
768c2ecf20Sopenharmony_ci	{ 0x0279, 2700 }, { 0x0274, 2800 }, { 0x026e, 2900 },
778c2ecf20Sopenharmony_ci	{ 0x0269, 3000 }, { 0x0262, 3100 }, { 0x025c, 3200 },
788c2ecf20Sopenharmony_ci	{ 0x0255, 3300 }, { 0x024f, 3400 }, { 0x0249, 3500 },
798c2ecf20Sopenharmony_ci	{ 0x0242, 3600 }, { 0x023c, 3700 }, { 0x0236, 3800 },
808c2ecf20Sopenharmony_ci	{ 0x0230, 3900 }, { 0x022a, 4000 }, { 0x0223, 4100 },
818c2ecf20Sopenharmony_ci	{ 0x021c, 4200 }, { 0x0215, 4300 }, { 0x020e, 4400 },
828c2ecf20Sopenharmony_ci	{ 0x0207, 4500 }, { 0x0201, 4600 }, { 0x01fa, 4700 },
838c2ecf20Sopenharmony_ci	{ 0x01f4, 4800 }, { 0x01ed, 4900 }, { 0x01e7, 5000 },
848c2ecf20Sopenharmony_ci	{ 0x01e0, 5100 }, { 0x01d9, 5200 }, { 0x01d2, 5300 },
858c2ecf20Sopenharmony_ci	{ 0x01cb, 5400 }, { 0x01c4, 5500 }, { 0x01be, 5600 },
868c2ecf20Sopenharmony_ci	{ 0x01b7, 5700 }, { 0x01b1, 5800 }, { 0x01aa, 5900 },
878c2ecf20Sopenharmony_ci	{ 0x01a4, 6000 }, { 0x019d, 6100 }, { 0x0196, 6200 },
888c2ecf20Sopenharmony_ci	{ 0x018f, 6300 }, { 0x0189, 6400 }, { 0x0182, 6500 },
898c2ecf20Sopenharmony_ci	{ 0x017c, 6600 }, { 0x0175, 6700 }, { 0x016f, 6800 },
908c2ecf20Sopenharmony_ci	{ 0x0169, 6900 }, { 0x0163, 7000 }, { 0x015c, 7100 },
918c2ecf20Sopenharmony_ci	{ 0x0156, 7200 }, { 0x0150, 7300 }, { 0x014a, 7400 },
928c2ecf20Sopenharmony_ci	{ 0x0144, 7500 }, { 0x013e, 7600 }, { 0x0138, 7700 },
938c2ecf20Sopenharmony_ci	{ 0x0132, 7800 }, { 0x012d, 7900 }, { 0x0127, 8000 },
948c2ecf20Sopenharmony_ci	{ 0x0121, 8100 }, { 0x011c, 8200 }, { 0x0116, 8300 },
958c2ecf20Sopenharmony_ci	{ 0x0111, 8400 }, { 0x010b, 8500 }, { 0x0106, 8600 },
968c2ecf20Sopenharmony_ci	{ 0x0101, 8700 }, { 0x00fc, 8800 }, { 0x00f7, 8900 },
978c2ecf20Sopenharmony_ci	{ 0x00f2, 9000 }, { 0x00ee, 9100 }, { 0x00ea, 9200 },
988c2ecf20Sopenharmony_ci	{ 0x00e6, 9300 }, { 0x00e2, 9400 }, { 0x00de, 9500 },
998c2ecf20Sopenharmony_ci	{ 0x00da, 9600 }, { 0x00d7, 9700 }, { 0x00d3, 9800 },
1008c2ecf20Sopenharmony_ci	{ 0x00d0, 9900 }, { 0x00cc, 10000 }, { 0x00c7, 10100 },
1018c2ecf20Sopenharmony_ci	{ 0x00c3, 10200 }, { 0x00bf, 10300 }, { 0x00ba, 10400 },
1028c2ecf20Sopenharmony_ci	{ 0x00b6, 10500 }, { 0x00b2, 10600 }, { 0x00ae, 10700 },
1038c2ecf20Sopenharmony_ci	{ 0x00aa, 10800 }, { 0x00a7, 10900 }, { 0x00a3, 11000 },
1048c2ecf20Sopenharmony_ci	{ 0x009f, 11100 }, { 0x009c, 11200 }, { 0x0098, 11300 },
1058c2ecf20Sopenharmony_ci	{ 0x0094, 11400 }, { 0x0091, 11500 }, { 0x008e, 11600 },
1068c2ecf20Sopenharmony_ci	{ 0x008a, 11700 }, { 0x0087, 11800 }, { 0x0084, 11900 },
1078c2ecf20Sopenharmony_ci	{ 0x0081, 12000 }, { 0x007e, 12100 }, { 0x007b, 12200 },
1088c2ecf20Sopenharmony_ci	{ 0x0079, 12300 }, { 0x0076, 12400 }, { 0x0073, 12500 },
1098c2ecf20Sopenharmony_ci	{ 0x0071, 12600 }, { 0x006e, 12700 }, { 0x006c, 12800 },
1108c2ecf20Sopenharmony_ci	{ 0x0069, 12900 }, { 0x0067, 13000 }, { 0x0065, 13100 },
1118c2ecf20Sopenharmony_ci	{ 0x0062, 13200 }, { 0x0060, 13300 }, { 0x005e, 13400 },
1128c2ecf20Sopenharmony_ci	{ 0x005c, 13500 }, { 0x005a, 13600 }, { 0x0058, 13700 },
1138c2ecf20Sopenharmony_ci	{ 0x0056, 13800 }, { 0x0054, 13900 }, { 0x0052, 14000 },
1148c2ecf20Sopenharmony_ci	{ 0x0050, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
1158c2ecf20Sopenharmony_ci	{ 0x004b, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
1168c2ecf20Sopenharmony_ci	{ 0x0046, 14700 }, { 0x0044, 14800 }, { 0x0043, 14900 },
1178c2ecf20Sopenharmony_ci	{ 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
1188c2ecf20Sopenharmony_ci	{ 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
1198c2ecf20Sopenharmony_ci	{ 0x0037, 15700 }, { 0x0036, 15800 }, { 0x0034, 15900 },
1208c2ecf20Sopenharmony_ci	{ 0x0033, 16000 }, { 0x0032, 16100 }, { 0x0031, 16200 },
1218c2ecf20Sopenharmony_ci	{ 0x0030, 16300 }, { 0x002f, 16400 }, { 0x002e, 16500 },
1228c2ecf20Sopenharmony_ci	{ 0x002d, 16600 }, { 0x002c, 16700 }, { 0x002b, 16800 },
1238c2ecf20Sopenharmony_ci	{ 0x002a, 16900 }, { 0x0029, 17000 }, { 0x0028, 17100 },
1248c2ecf20Sopenharmony_ci	{ 0x0027, 17200 }, { 0x0026, 17300 }, { 0x0025, 17400 },
1258c2ecf20Sopenharmony_ci	{ 0x0024, 17500 }, { 0x0023, 17600 }, { 0x0022, 17800 },
1268c2ecf20Sopenharmony_ci	{ 0x0021, 17900 }, { 0x0020, 18000 }, { 0x001f, 18200 },
1278c2ecf20Sopenharmony_ci	{ 0x001e, 18300 }, { 0x001d, 18500 }, { 0x001c, 18700 },
1288c2ecf20Sopenharmony_ci	{ 0x001b, 18900 }, { 0x001a, 19000 }, { 0x0019, 19200 },
1298c2ecf20Sopenharmony_ci	{ 0x0018, 19300 }, { 0x0017, 19500 }, { 0x0016, 19700 },
1308c2ecf20Sopenharmony_ci	{ 0x0015, 19900 }, { 0x0014, 20000 },
1318c2ecf20Sopenharmony_ci};
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_cistatic const struct cxd2841er_cnr_data s2_cn_data[] = {
1348c2ecf20Sopenharmony_ci	{ 0x05af, 0 }, { 0x0597, 100 }, { 0x057e, 200 },
1358c2ecf20Sopenharmony_ci	{ 0x0567, 300 }, { 0x0550, 400 }, { 0x0539, 500 },
1368c2ecf20Sopenharmony_ci	{ 0x0522, 600 }, { 0x050c, 700 }, { 0x04f6, 800 },
1378c2ecf20Sopenharmony_ci	{ 0x04e1, 900 }, { 0x04cc, 1000 }, { 0x04b6, 1100 },
1388c2ecf20Sopenharmony_ci	{ 0x04a1, 1200 }, { 0x048c, 1300 }, { 0x0477, 1400 },
1398c2ecf20Sopenharmony_ci	{ 0x0463, 1500 }, { 0x044f, 1600 }, { 0x043c, 1700 },
1408c2ecf20Sopenharmony_ci	{ 0x0428, 1800 }, { 0x0416, 1900 }, { 0x0403, 2000 },
1418c2ecf20Sopenharmony_ci	{ 0x03ef, 2100 }, { 0x03dc, 2200 }, { 0x03c9, 2300 },
1428c2ecf20Sopenharmony_ci	{ 0x03b6, 2400 }, { 0x03a4, 2500 }, { 0x0392, 2600 },
1438c2ecf20Sopenharmony_ci	{ 0x0381, 2700 }, { 0x036f, 2800 }, { 0x035f, 2900 },
1448c2ecf20Sopenharmony_ci	{ 0x034e, 3000 }, { 0x033d, 3100 }, { 0x032d, 3200 },
1458c2ecf20Sopenharmony_ci	{ 0x031d, 3300 }, { 0x030d, 3400 }, { 0x02fd, 3500 },
1468c2ecf20Sopenharmony_ci	{ 0x02ee, 3600 }, { 0x02df, 3700 }, { 0x02d0, 3800 },
1478c2ecf20Sopenharmony_ci	{ 0x02c2, 3900 }, { 0x02b4, 4000 }, { 0x02a6, 4100 },
1488c2ecf20Sopenharmony_ci	{ 0x0299, 4200 }, { 0x028c, 4300 }, { 0x027f, 4400 },
1498c2ecf20Sopenharmony_ci	{ 0x0272, 4500 }, { 0x0265, 4600 }, { 0x0259, 4700 },
1508c2ecf20Sopenharmony_ci	{ 0x024d, 4800 }, { 0x0241, 4900 }, { 0x0236, 5000 },
1518c2ecf20Sopenharmony_ci	{ 0x022b, 5100 }, { 0x0220, 5200 }, { 0x0215, 5300 },
1528c2ecf20Sopenharmony_ci	{ 0x020a, 5400 }, { 0x0200, 5500 }, { 0x01f6, 5600 },
1538c2ecf20Sopenharmony_ci	{ 0x01ec, 5700 }, { 0x01e2, 5800 }, { 0x01d8, 5900 },
1548c2ecf20Sopenharmony_ci	{ 0x01cf, 6000 }, { 0x01c6, 6100 }, { 0x01bc, 6200 },
1558c2ecf20Sopenharmony_ci	{ 0x01b3, 6300 }, { 0x01aa, 6400 }, { 0x01a2, 6500 },
1568c2ecf20Sopenharmony_ci	{ 0x0199, 6600 }, { 0x0191, 6700 }, { 0x0189, 6800 },
1578c2ecf20Sopenharmony_ci	{ 0x0181, 6900 }, { 0x0179, 7000 }, { 0x0171, 7100 },
1588c2ecf20Sopenharmony_ci	{ 0x0169, 7200 }, { 0x0161, 7300 }, { 0x015a, 7400 },
1598c2ecf20Sopenharmony_ci	{ 0x0153, 7500 }, { 0x014b, 7600 }, { 0x0144, 7700 },
1608c2ecf20Sopenharmony_ci	{ 0x013d, 7800 }, { 0x0137, 7900 }, { 0x0130, 8000 },
1618c2ecf20Sopenharmony_ci	{ 0x012a, 8100 }, { 0x0124, 8200 }, { 0x011e, 8300 },
1628c2ecf20Sopenharmony_ci	{ 0x0118, 8400 }, { 0x0112, 8500 }, { 0x010c, 8600 },
1638c2ecf20Sopenharmony_ci	{ 0x0107, 8700 }, { 0x0101, 8800 }, { 0x00fc, 8900 },
1648c2ecf20Sopenharmony_ci	{ 0x00f7, 9000 }, { 0x00f2, 9100 }, { 0x00ec, 9200 },
1658c2ecf20Sopenharmony_ci	{ 0x00e7, 9300 }, { 0x00e2, 9400 }, { 0x00dd, 9500 },
1668c2ecf20Sopenharmony_ci	{ 0x00d8, 9600 }, { 0x00d4, 9700 }, { 0x00cf, 9800 },
1678c2ecf20Sopenharmony_ci	{ 0x00ca, 9900 }, { 0x00c6, 10000 }, { 0x00c2, 10100 },
1688c2ecf20Sopenharmony_ci	{ 0x00be, 10200 }, { 0x00b9, 10300 }, { 0x00b5, 10400 },
1698c2ecf20Sopenharmony_ci	{ 0x00b1, 10500 }, { 0x00ae, 10600 }, { 0x00aa, 10700 },
1708c2ecf20Sopenharmony_ci	{ 0x00a6, 10800 }, { 0x00a3, 10900 }, { 0x009f, 11000 },
1718c2ecf20Sopenharmony_ci	{ 0x009b, 11100 }, { 0x0098, 11200 }, { 0x0095, 11300 },
1728c2ecf20Sopenharmony_ci	{ 0x0091, 11400 }, { 0x008e, 11500 }, { 0x008b, 11600 },
1738c2ecf20Sopenharmony_ci	{ 0x0088, 11700 }, { 0x0085, 11800 }, { 0x0082, 11900 },
1748c2ecf20Sopenharmony_ci	{ 0x007f, 12000 }, { 0x007c, 12100 }, { 0x007a, 12200 },
1758c2ecf20Sopenharmony_ci	{ 0x0077, 12300 }, { 0x0074, 12400 }, { 0x0072, 12500 },
1768c2ecf20Sopenharmony_ci	{ 0x006f, 12600 }, { 0x006d, 12700 }, { 0x006b, 12800 },
1778c2ecf20Sopenharmony_ci	{ 0x0068, 12900 }, { 0x0066, 13000 }, { 0x0064, 13100 },
1788c2ecf20Sopenharmony_ci	{ 0x0061, 13200 }, { 0x005f, 13300 }, { 0x005d, 13400 },
1798c2ecf20Sopenharmony_ci	{ 0x005b, 13500 }, { 0x0059, 13600 }, { 0x0057, 13700 },
1808c2ecf20Sopenharmony_ci	{ 0x0055, 13800 }, { 0x0053, 13900 }, { 0x0051, 14000 },
1818c2ecf20Sopenharmony_ci	{ 0x004f, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
1828c2ecf20Sopenharmony_ci	{ 0x004a, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
1838c2ecf20Sopenharmony_ci	{ 0x0045, 14700 }, { 0x0044, 14800 }, { 0x0042, 14900 },
1848c2ecf20Sopenharmony_ci	{ 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
1858c2ecf20Sopenharmony_ci	{ 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
1868c2ecf20Sopenharmony_ci	{ 0x0038, 15600 }, { 0x0037, 15700 }, { 0x0036, 15800 },
1878c2ecf20Sopenharmony_ci	{ 0x0034, 15900 }, { 0x0033, 16000 }, { 0x0032, 16100 },
1888c2ecf20Sopenharmony_ci	{ 0x0031, 16200 }, { 0x0030, 16300 }, { 0x002f, 16400 },
1898c2ecf20Sopenharmony_ci	{ 0x002e, 16500 }, { 0x002d, 16600 }, { 0x002c, 16700 },
1908c2ecf20Sopenharmony_ci	{ 0x002b, 16800 }, { 0x002a, 16900 }, { 0x0029, 17000 },
1918c2ecf20Sopenharmony_ci	{ 0x0028, 17100 }, { 0x0027, 17200 }, { 0x0026, 17300 },
1928c2ecf20Sopenharmony_ci	{ 0x0025, 17400 }, { 0x0024, 17500 }, { 0x0023, 17600 },
1938c2ecf20Sopenharmony_ci	{ 0x0022, 17800 }, { 0x0021, 17900 }, { 0x0020, 18000 },
1948c2ecf20Sopenharmony_ci	{ 0x001f, 18200 }, { 0x001e, 18300 }, { 0x001d, 18500 },
1958c2ecf20Sopenharmony_ci	{ 0x001c, 18700 }, { 0x001b, 18900 }, { 0x001a, 19000 },
1968c2ecf20Sopenharmony_ci	{ 0x0019, 19200 }, { 0x0018, 19300 }, { 0x0017, 19500 },
1978c2ecf20Sopenharmony_ci	{ 0x0016, 19700 }, { 0x0015, 19900 }, { 0x0014, 20000 },
1988c2ecf20Sopenharmony_ci};
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic int cxd2841er_freeze_regs(struct cxd2841er_priv *priv);
2018c2ecf20Sopenharmony_cistatic int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic void cxd2841er_i2c_debug(struct cxd2841er_priv *priv,
2048c2ecf20Sopenharmony_ci				u8 addr, u8 reg, u8 write,
2058c2ecf20Sopenharmony_ci				const u8 *data, u32 len)
2068c2ecf20Sopenharmony_ci{
2078c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev,
2088c2ecf20Sopenharmony_ci		"cxd2841er: I2C %s addr %02x reg 0x%02x size %d data %*ph\n",
2098c2ecf20Sopenharmony_ci		(write == 0 ? "read" : "write"), addr, reg, len, len, data);
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic int cxd2841er_write_regs(struct cxd2841er_priv *priv,
2138c2ecf20Sopenharmony_ci				u8 addr, u8 reg, const u8 *data, u32 len)
2148c2ecf20Sopenharmony_ci{
2158c2ecf20Sopenharmony_ci	int ret;
2168c2ecf20Sopenharmony_ci	u8 buf[MAX_WRITE_REGSIZE + 1];
2178c2ecf20Sopenharmony_ci	u8 i2c_addr = (addr == I2C_SLVX ?
2188c2ecf20Sopenharmony_ci		priv->i2c_addr_slvx : priv->i2c_addr_slvt);
2198c2ecf20Sopenharmony_ci	struct i2c_msg msg[1] = {
2208c2ecf20Sopenharmony_ci		{
2218c2ecf20Sopenharmony_ci			.addr = i2c_addr,
2228c2ecf20Sopenharmony_ci			.flags = 0,
2238c2ecf20Sopenharmony_ci			.len = len + 1,
2248c2ecf20Sopenharmony_ci			.buf = buf,
2258c2ecf20Sopenharmony_ci		}
2268c2ecf20Sopenharmony_ci	};
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	if (len + 1 >= sizeof(buf)) {
2298c2ecf20Sopenharmony_ci		dev_warn(&priv->i2c->dev, "wr reg=%04x: len=%d is too big!\n",
2308c2ecf20Sopenharmony_ci			 reg, len + 1);
2318c2ecf20Sopenharmony_ci		return -E2BIG;
2328c2ecf20Sopenharmony_ci	}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	cxd2841er_i2c_debug(priv, i2c_addr, reg, 1, data, len);
2358c2ecf20Sopenharmony_ci	buf[0] = reg;
2368c2ecf20Sopenharmony_ci	memcpy(&buf[1], data, len);
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	ret = i2c_transfer(priv->i2c, msg, 1);
2398c2ecf20Sopenharmony_ci	if (ret >= 0 && ret != 1)
2408c2ecf20Sopenharmony_ci		ret = -EIO;
2418c2ecf20Sopenharmony_ci	if (ret < 0) {
2428c2ecf20Sopenharmony_ci		dev_warn(&priv->i2c->dev,
2438c2ecf20Sopenharmony_ci			"%s: i2c wr failed=%d addr=%02x reg=%02x len=%d\n",
2448c2ecf20Sopenharmony_ci			KBUILD_MODNAME, ret, i2c_addr, reg, len);
2458c2ecf20Sopenharmony_ci		return ret;
2468c2ecf20Sopenharmony_ci	}
2478c2ecf20Sopenharmony_ci	return 0;
2488c2ecf20Sopenharmony_ci}
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cistatic int cxd2841er_write_reg(struct cxd2841er_priv *priv,
2518c2ecf20Sopenharmony_ci			       u8 addr, u8 reg, u8 val)
2528c2ecf20Sopenharmony_ci{
2538c2ecf20Sopenharmony_ci	u8 tmp = val; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
2548c2ecf20Sopenharmony_ci
2558c2ecf20Sopenharmony_ci	return cxd2841er_write_regs(priv, addr, reg, &tmp, 1);
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic int cxd2841er_read_regs(struct cxd2841er_priv *priv,
2598c2ecf20Sopenharmony_ci			       u8 addr, u8 reg, u8 *val, u32 len)
2608c2ecf20Sopenharmony_ci{
2618c2ecf20Sopenharmony_ci	int ret;
2628c2ecf20Sopenharmony_ci	u8 i2c_addr = (addr == I2C_SLVX ?
2638c2ecf20Sopenharmony_ci		priv->i2c_addr_slvx : priv->i2c_addr_slvt);
2648c2ecf20Sopenharmony_ci	struct i2c_msg msg[2] = {
2658c2ecf20Sopenharmony_ci		{
2668c2ecf20Sopenharmony_ci			.addr = i2c_addr,
2678c2ecf20Sopenharmony_ci			.flags = 0,
2688c2ecf20Sopenharmony_ci			.len = 1,
2698c2ecf20Sopenharmony_ci			.buf = &reg,
2708c2ecf20Sopenharmony_ci		}, {
2718c2ecf20Sopenharmony_ci			.addr = i2c_addr,
2728c2ecf20Sopenharmony_ci			.flags = I2C_M_RD,
2738c2ecf20Sopenharmony_ci			.len = len,
2748c2ecf20Sopenharmony_ci			.buf = val,
2758c2ecf20Sopenharmony_ci		}
2768c2ecf20Sopenharmony_ci	};
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	ret = i2c_transfer(priv->i2c, msg, 2);
2798c2ecf20Sopenharmony_ci	if (ret >= 0 && ret != 2)
2808c2ecf20Sopenharmony_ci		ret = -EIO;
2818c2ecf20Sopenharmony_ci	if (ret < 0) {
2828c2ecf20Sopenharmony_ci		dev_warn(&priv->i2c->dev,
2838c2ecf20Sopenharmony_ci			"%s: i2c rd failed=%d addr=%02x reg=%02x\n",
2848c2ecf20Sopenharmony_ci			KBUILD_MODNAME, ret, i2c_addr, reg);
2858c2ecf20Sopenharmony_ci		return ret;
2868c2ecf20Sopenharmony_ci	}
2878c2ecf20Sopenharmony_ci	cxd2841er_i2c_debug(priv, i2c_addr, reg, 0, val, len);
2888c2ecf20Sopenharmony_ci	return 0;
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic int cxd2841er_read_reg(struct cxd2841er_priv *priv,
2928c2ecf20Sopenharmony_ci			      u8 addr, u8 reg, u8 *val)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	return cxd2841er_read_regs(priv, addr, reg, val, 1);
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_cistatic int cxd2841er_set_reg_bits(struct cxd2841er_priv *priv,
2988c2ecf20Sopenharmony_ci				  u8 addr, u8 reg, u8 data, u8 mask)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	int res;
3018c2ecf20Sopenharmony_ci	u8 rdata;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	if (mask != 0xff) {
3048c2ecf20Sopenharmony_ci		res = cxd2841er_read_reg(priv, addr, reg, &rdata);
3058c2ecf20Sopenharmony_ci		if (res)
3068c2ecf20Sopenharmony_ci			return res;
3078c2ecf20Sopenharmony_ci		data = ((data & mask) | (rdata & (mask ^ 0xFF)));
3088c2ecf20Sopenharmony_ci	}
3098c2ecf20Sopenharmony_ci	return cxd2841er_write_reg(priv, addr, reg, data);
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_cistatic u32 cxd2841er_calc_iffreq_xtal(enum cxd2841er_xtal xtal, u32 ifhz)
3138c2ecf20Sopenharmony_ci{
3148c2ecf20Sopenharmony_ci	u64 tmp;
3158c2ecf20Sopenharmony_ci
3168c2ecf20Sopenharmony_ci	tmp = (u64) ifhz * 16777216;
3178c2ecf20Sopenharmony_ci	do_div(tmp, ((xtal == SONY_XTAL_24000) ? 48000000 : 41000000));
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	return (u32) tmp;
3208c2ecf20Sopenharmony_ci}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_cistatic u32 cxd2841er_calc_iffreq(u32 ifhz)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	return cxd2841er_calc_iffreq_xtal(SONY_XTAL_20500, ifhz);
3258c2ecf20Sopenharmony_ci}
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_cistatic int cxd2841er_get_if_hz(struct cxd2841er_priv *priv, u32 def_hz)
3288c2ecf20Sopenharmony_ci{
3298c2ecf20Sopenharmony_ci	u32 hz;
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	if (priv->frontend.ops.tuner_ops.get_if_frequency
3328c2ecf20Sopenharmony_ci			&& (priv->flags & CXD2841ER_AUTO_IFHZ))
3338c2ecf20Sopenharmony_ci		priv->frontend.ops.tuner_ops.get_if_frequency(
3348c2ecf20Sopenharmony_ci			&priv->frontend, &hz);
3358c2ecf20Sopenharmony_ci	else
3368c2ecf20Sopenharmony_ci		hz = def_hz;
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci	return hz;
3398c2ecf20Sopenharmony_ci}
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_cistatic int cxd2841er_tuner_set(struct dvb_frontend *fe)
3428c2ecf20Sopenharmony_ci{
3438c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	if ((priv->flags & CXD2841ER_USE_GATECTRL) && fe->ops.i2c_gate_ctrl)
3468c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 1);
3478c2ecf20Sopenharmony_ci	if (fe->ops.tuner_ops.set_params)
3488c2ecf20Sopenharmony_ci		fe->ops.tuner_ops.set_params(fe);
3498c2ecf20Sopenharmony_ci	if ((priv->flags & CXD2841ER_USE_GATECTRL) && fe->ops.i2c_gate_ctrl)
3508c2ecf20Sopenharmony_ci		fe->ops.i2c_gate_ctrl(fe, 0);
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	return 0;
3538c2ecf20Sopenharmony_ci}
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_cistatic int cxd2841er_dvbs2_set_symbol_rate(struct cxd2841er_priv *priv,
3568c2ecf20Sopenharmony_ci					   u32 symbol_rate)
3578c2ecf20Sopenharmony_ci{
3588c2ecf20Sopenharmony_ci	u32 reg_value = 0;
3598c2ecf20Sopenharmony_ci	u8 data[3] = {0, 0, 0};
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3628c2ecf20Sopenharmony_ci	/*
3638c2ecf20Sopenharmony_ci	 * regValue = (symbolRateKSps * 2^14 / 1000) + 0.5
3648c2ecf20Sopenharmony_ci	 *          = ((symbolRateKSps * 2^14) + 500) / 1000
3658c2ecf20Sopenharmony_ci	 *          = ((symbolRateKSps * 16384) + 500) / 1000
3668c2ecf20Sopenharmony_ci	 */
3678c2ecf20Sopenharmony_ci	reg_value = DIV_ROUND_CLOSEST(symbol_rate * 16384, 1000);
3688c2ecf20Sopenharmony_ci	if ((reg_value == 0) || (reg_value > 0xFFFFF)) {
3698c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev,
3708c2ecf20Sopenharmony_ci			"%s(): reg_value is out of range\n", __func__);
3718c2ecf20Sopenharmony_ci		return -EINVAL;
3728c2ecf20Sopenharmony_ci	}
3738c2ecf20Sopenharmony_ci	data[0] = (u8)((reg_value >> 16) & 0x0F);
3748c2ecf20Sopenharmony_ci	data[1] = (u8)((reg_value >>  8) & 0xFF);
3758c2ecf20Sopenharmony_ci	data[2] = (u8)(reg_value & 0xFF);
3768c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0xAE */
3778c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
3788c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0x20, data, 3);
3798c2ecf20Sopenharmony_ci	return 0;
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
3838c2ecf20Sopenharmony_ci					u8 system);
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_s_to_active_s(struct cxd2841er_priv *priv,
3868c2ecf20Sopenharmony_ci					 u8 system, u32 symbol_rate)
3878c2ecf20Sopenharmony_ci{
3888c2ecf20Sopenharmony_ci	int ret;
3898c2ecf20Sopenharmony_ci	u8 data[4] = { 0, 0, 0, 0 };
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	if (priv->state != STATE_SLEEP_S) {
3928c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
3938c2ecf20Sopenharmony_ci			__func__, (int)priv->state);
3948c2ecf20Sopenharmony_ci		return -EINVAL;
3958c2ecf20Sopenharmony_ci	}
3968c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3978c2ecf20Sopenharmony_ci	cxd2841er_set_ts_clock_mode(priv, SYS_DVBS);
3988c2ecf20Sopenharmony_ci	/* Set demod mode */
3998c2ecf20Sopenharmony_ci	if (system == SYS_DVBS) {
4008c2ecf20Sopenharmony_ci		data[0] = 0x0A;
4018c2ecf20Sopenharmony_ci	} else if (system == SYS_DVBS2) {
4028c2ecf20Sopenharmony_ci		data[0] = 0x0B;
4038c2ecf20Sopenharmony_ci	} else {
4048c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid delsys %d\n",
4058c2ecf20Sopenharmony_ci			__func__, system);
4068c2ecf20Sopenharmony_ci		return -EINVAL;
4078c2ecf20Sopenharmony_ci	}
4088c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
4098c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
4108c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, data[0]);
4118c2ecf20Sopenharmony_ci	/* DVB-S/S2 */
4128c2ecf20Sopenharmony_ci	data[0] = 0x00;
4138c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
4148c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
4158c2ecf20Sopenharmony_ci	/* Enable S/S2 auto detection 1 */
4168c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, data[0]);
4178c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0xAE */
4188c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
4198c2ecf20Sopenharmony_ci	/* Enable S/S2 auto detection 2 */
4208c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, data[0]);
4218c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
4228c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
4238c2ecf20Sopenharmony_ci	/* Enable demod clock */
4248c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
4258c2ecf20Sopenharmony_ci	/* Enable ADC clock */
4268c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x01);
4278c2ecf20Sopenharmony_ci	/* Enable ADC 1 */
4288c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
4298c2ecf20Sopenharmony_ci	/* Enable ADC 2 */
4308c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x3f);
4318c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
4328c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
4338c2ecf20Sopenharmony_ci	/* Enable ADC 3 */
4348c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
4358c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0xA3 */
4368c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa3);
4378c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xac, 0x00);
4388c2ecf20Sopenharmony_ci	data[0] = 0x07;
4398c2ecf20Sopenharmony_ci	data[1] = 0x3B;
4408c2ecf20Sopenharmony_ci	data[2] = 0x08;
4418c2ecf20Sopenharmony_ci	data[3] = 0xC5;
4428c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0xAB */
4438c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xab);
4448c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0x98, data, 4);
4458c2ecf20Sopenharmony_ci	data[0] = 0x05;
4468c2ecf20Sopenharmony_ci	data[1] = 0x80;
4478c2ecf20Sopenharmony_ci	data[2] = 0x0A;
4488c2ecf20Sopenharmony_ci	data[3] = 0x80;
4498c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0xa8, data, 4);
4508c2ecf20Sopenharmony_ci	data[0] = 0x0C;
4518c2ecf20Sopenharmony_ci	data[1] = 0xCC;
4528c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0xc3, data, 2);
4538c2ecf20Sopenharmony_ci	/* Set demod parameter */
4548c2ecf20Sopenharmony_ci	ret = cxd2841er_dvbs2_set_symbol_rate(priv, symbol_rate);
4558c2ecf20Sopenharmony_ci	if (ret != 0)
4568c2ecf20Sopenharmony_ci		return ret;
4578c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
4588c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
4598c2ecf20Sopenharmony_ci	/* disable Hi-Z setting 1 */
4608c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x10);
4618c2ecf20Sopenharmony_ci	/* disable Hi-Z setting 2 */
4628c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
4638c2ecf20Sopenharmony_ci	priv->state = STATE_ACTIVE_S;
4648c2ecf20Sopenharmony_ci	return 0;
4658c2ecf20Sopenharmony_ci}
4668c2ecf20Sopenharmony_ci
4678c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_t_band(struct cxd2841er_priv *priv,
4688c2ecf20Sopenharmony_ci					       u32 bandwidth);
4698c2ecf20Sopenharmony_ci
4708c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
4718c2ecf20Sopenharmony_ci						u32 bandwidth);
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
4748c2ecf20Sopenharmony_ci					       u32 bandwidth);
4758c2ecf20Sopenharmony_ci
4768c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
4778c2ecf20Sopenharmony_ci		u32 bandwidth);
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_cistatic int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv);
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv);
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_cistatic int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv);
4848c2ecf20Sopenharmony_ci
4858c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc(struct dvb_frontend *fe);
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cistatic int cxd2841er_retune_active(struct cxd2841er_priv *priv,
4888c2ecf20Sopenharmony_ci				   struct dtv_frontend_properties *p)
4898c2ecf20Sopenharmony_ci{
4908c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
4918c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_S &&
4928c2ecf20Sopenharmony_ci			priv->state != STATE_ACTIVE_TC) {
4938c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
4948c2ecf20Sopenharmony_ci			__func__, priv->state);
4958c2ecf20Sopenharmony_ci		return -EINVAL;
4968c2ecf20Sopenharmony_ci	}
4978c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
4988c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
4998c2ecf20Sopenharmony_ci	/* disable TS output */
5008c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
5018c2ecf20Sopenharmony_ci	if (priv->state == STATE_ACTIVE_S)
5028c2ecf20Sopenharmony_ci		return cxd2841er_dvbs2_set_symbol_rate(
5038c2ecf20Sopenharmony_ci				priv, p->symbol_rate / 1000);
5048c2ecf20Sopenharmony_ci	else if (priv->state == STATE_ACTIVE_TC) {
5058c2ecf20Sopenharmony_ci		switch (priv->system) {
5068c2ecf20Sopenharmony_ci		case SYS_DVBT:
5078c2ecf20Sopenharmony_ci			return cxd2841er_sleep_tc_to_active_t_band(
5088c2ecf20Sopenharmony_ci					priv, p->bandwidth_hz);
5098c2ecf20Sopenharmony_ci		case SYS_DVBT2:
5108c2ecf20Sopenharmony_ci			return cxd2841er_sleep_tc_to_active_t2_band(
5118c2ecf20Sopenharmony_ci					priv, p->bandwidth_hz);
5128c2ecf20Sopenharmony_ci		case SYS_DVBC_ANNEX_A:
5138c2ecf20Sopenharmony_ci			return cxd2841er_sleep_tc_to_active_c_band(
5148c2ecf20Sopenharmony_ci					priv, p->bandwidth_hz);
5158c2ecf20Sopenharmony_ci		case SYS_ISDBT:
5168c2ecf20Sopenharmony_ci			cxd2841er_active_i_to_sleep_tc(priv);
5178c2ecf20Sopenharmony_ci			cxd2841er_sleep_tc_to_shutdown(priv);
5188c2ecf20Sopenharmony_ci			cxd2841er_shutdown_to_sleep_tc(priv);
5198c2ecf20Sopenharmony_ci			return cxd2841er_sleep_tc_to_active_i(
5208c2ecf20Sopenharmony_ci					priv, p->bandwidth_hz);
5218c2ecf20Sopenharmony_ci		}
5228c2ecf20Sopenharmony_ci	}
5238c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
5248c2ecf20Sopenharmony_ci		__func__, priv->system);
5258c2ecf20Sopenharmony_ci	return -EINVAL;
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_cistatic int cxd2841er_active_s_to_sleep_s(struct cxd2841er_priv *priv)
5298c2ecf20Sopenharmony_ci{
5308c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
5318c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_S) {
5328c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
5338c2ecf20Sopenharmony_ci			__func__, priv->state);
5348c2ecf20Sopenharmony_ci		return -EINVAL;
5358c2ecf20Sopenharmony_ci	}
5368c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
5378c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
5388c2ecf20Sopenharmony_ci	/* disable TS output */
5398c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
5408c2ecf20Sopenharmony_ci	/* enable Hi-Z setting 1 */
5418c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1f);
5428c2ecf20Sopenharmony_ci	/* enable Hi-Z setting 2 */
5438c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
5448c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
5458c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
5468c2ecf20Sopenharmony_ci	/* disable ADC 1 */
5478c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
5488c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
5498c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
5508c2ecf20Sopenharmony_ci	/* disable ADC clock */
5518c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x00);
5528c2ecf20Sopenharmony_ci	/* disable ADC 2 */
5538c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
5548c2ecf20Sopenharmony_ci	/* disable ADC 3 */
5558c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
5568c2ecf20Sopenharmony_ci	/* SADC Bias ON */
5578c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
5588c2ecf20Sopenharmony_ci	/* disable demod clock */
5598c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
5608c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0xAE */
5618c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
5628c2ecf20Sopenharmony_ci	/* disable S/S2 auto detection1 */
5638c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
5648c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
5658c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
5668c2ecf20Sopenharmony_ci	/* disable S/S2 auto detection2 */
5678c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, 0x00);
5688c2ecf20Sopenharmony_ci	priv->state = STATE_SLEEP_S;
5698c2ecf20Sopenharmony_ci	return 0;
5708c2ecf20Sopenharmony_ci}
5718c2ecf20Sopenharmony_ci
5728c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_s_to_shutdown(struct cxd2841er_priv *priv)
5738c2ecf20Sopenharmony_ci{
5748c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
5758c2ecf20Sopenharmony_ci	if (priv->state != STATE_SLEEP_S) {
5768c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
5778c2ecf20Sopenharmony_ci			__func__, priv->state);
5788c2ecf20Sopenharmony_ci		return -EINVAL;
5798c2ecf20Sopenharmony_ci	}
5808c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
5818c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
5828c2ecf20Sopenharmony_ci	/* Disable DSQOUT */
5838c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
5848c2ecf20Sopenharmony_ci	/* Disable DSQIN */
5858c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x9c, 0x00);
5868c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
5878c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
5888c2ecf20Sopenharmony_ci	/* Disable oscillator */
5898c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
5908c2ecf20Sopenharmony_ci	/* Set demod mode */
5918c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
5928c2ecf20Sopenharmony_ci	priv->state = STATE_SHUTDOWN;
5938c2ecf20Sopenharmony_ci	return 0;
5948c2ecf20Sopenharmony_ci}
5958c2ecf20Sopenharmony_ci
5968c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv)
5978c2ecf20Sopenharmony_ci{
5988c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
5998c2ecf20Sopenharmony_ci	if (priv->state != STATE_SLEEP_TC) {
6008c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
6018c2ecf20Sopenharmony_ci			__func__, priv->state);
6028c2ecf20Sopenharmony_ci		return -EINVAL;
6038c2ecf20Sopenharmony_ci	}
6048c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
6058c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
6068c2ecf20Sopenharmony_ci	/* Disable oscillator */
6078c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
6088c2ecf20Sopenharmony_ci	/* Set demod mode */
6098c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
6108c2ecf20Sopenharmony_ci	priv->state = STATE_SHUTDOWN;
6118c2ecf20Sopenharmony_ci	return 0;
6128c2ecf20Sopenharmony_ci}
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_cistatic int cxd2841er_active_t_to_sleep_tc(struct cxd2841er_priv *priv)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
6178c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
6188c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
6198c2ecf20Sopenharmony_ci			__func__, priv->state);
6208c2ecf20Sopenharmony_ci		return -EINVAL;
6218c2ecf20Sopenharmony_ci	}
6228c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
6238c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
6248c2ecf20Sopenharmony_ci	/* disable TS output */
6258c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
6268c2ecf20Sopenharmony_ci	/* enable Hi-Z setting 1 */
6278c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
6288c2ecf20Sopenharmony_ci	/* enable Hi-Z setting 2 */
6298c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
6308c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
6318c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
6328c2ecf20Sopenharmony_ci	/* disable ADC 1 */
6338c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
6348c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
6358c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
6368c2ecf20Sopenharmony_ci	/* Disable ADC 2 */
6378c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
6388c2ecf20Sopenharmony_ci	/* Disable ADC 3 */
6398c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
6408c2ecf20Sopenharmony_ci	/* Disable ADC clock */
6418c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
6428c2ecf20Sopenharmony_ci	/* Disable RF level monitor */
6438c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
6448c2ecf20Sopenharmony_ci	/* Disable demod clock */
6458c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
6468c2ecf20Sopenharmony_ci	priv->state = STATE_SLEEP_TC;
6478c2ecf20Sopenharmony_ci	return 0;
6488c2ecf20Sopenharmony_ci}
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_cistatic int cxd2841er_active_t2_to_sleep_tc(struct cxd2841er_priv *priv)
6518c2ecf20Sopenharmony_ci{
6528c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
6538c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
6548c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
6558c2ecf20Sopenharmony_ci			__func__, priv->state);
6568c2ecf20Sopenharmony_ci		return -EINVAL;
6578c2ecf20Sopenharmony_ci	}
6588c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
6598c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
6608c2ecf20Sopenharmony_ci	/* disable TS output */
6618c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
6628c2ecf20Sopenharmony_ci	/* enable Hi-Z setting 1 */
6638c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
6648c2ecf20Sopenharmony_ci	/* enable Hi-Z setting 2 */
6658c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
6668c2ecf20Sopenharmony_ci	/* Cancel DVB-T2 setting */
6678c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
6688c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x40);
6698c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x21);
6708c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
6718c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xfb);
6728c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
6738c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x00, 0x0f);
6748c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
6758c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x00, 0x3f);
6768c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
6778c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
6788c2ecf20Sopenharmony_ci	/* disable ADC 1 */
6798c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
6808c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
6818c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
6828c2ecf20Sopenharmony_ci	/* Disable ADC 2 */
6838c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
6848c2ecf20Sopenharmony_ci	/* Disable ADC 3 */
6858c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
6868c2ecf20Sopenharmony_ci	/* Disable ADC clock */
6878c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
6888c2ecf20Sopenharmony_ci	/* Disable RF level monitor */
6898c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
6908c2ecf20Sopenharmony_ci	/* Disable demod clock */
6918c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
6928c2ecf20Sopenharmony_ci	priv->state = STATE_SLEEP_TC;
6938c2ecf20Sopenharmony_ci	return 0;
6948c2ecf20Sopenharmony_ci}
6958c2ecf20Sopenharmony_ci
6968c2ecf20Sopenharmony_cistatic int cxd2841er_active_c_to_sleep_tc(struct cxd2841er_priv *priv)
6978c2ecf20Sopenharmony_ci{
6988c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
6998c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
7008c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
7018c2ecf20Sopenharmony_ci			__func__, priv->state);
7028c2ecf20Sopenharmony_ci		return -EINVAL;
7038c2ecf20Sopenharmony_ci	}
7048c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
7058c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
7068c2ecf20Sopenharmony_ci	/* disable TS output */
7078c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
7088c2ecf20Sopenharmony_ci	/* enable Hi-Z setting 1 */
7098c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
7108c2ecf20Sopenharmony_ci	/* enable Hi-Z setting 2 */
7118c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
7128c2ecf20Sopenharmony_ci	/* Cancel DVB-C setting */
7138c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
7148c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
7158c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
7168c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
7178c2ecf20Sopenharmony_ci	/* disable ADC 1 */
7188c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
7198c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
7208c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
7218c2ecf20Sopenharmony_ci	/* Disable ADC 2 */
7228c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
7238c2ecf20Sopenharmony_ci	/* Disable ADC 3 */
7248c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
7258c2ecf20Sopenharmony_ci	/* Disable ADC clock */
7268c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
7278c2ecf20Sopenharmony_ci	/* Disable RF level monitor */
7288c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
7298c2ecf20Sopenharmony_ci	/* Disable demod clock */
7308c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
7318c2ecf20Sopenharmony_ci	priv->state = STATE_SLEEP_TC;
7328c2ecf20Sopenharmony_ci	return 0;
7338c2ecf20Sopenharmony_ci}
7348c2ecf20Sopenharmony_ci
7358c2ecf20Sopenharmony_cistatic int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv)
7368c2ecf20Sopenharmony_ci{
7378c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
7388c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
7398c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
7408c2ecf20Sopenharmony_ci				__func__, priv->state);
7418c2ecf20Sopenharmony_ci		return -EINVAL;
7428c2ecf20Sopenharmony_ci	}
7438c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
7448c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
7458c2ecf20Sopenharmony_ci	/* disable TS output */
7468c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
7478c2ecf20Sopenharmony_ci	/* enable Hi-Z setting 1 */
7488c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
7498c2ecf20Sopenharmony_ci	/* enable Hi-Z setting 2 */
7508c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
7518c2ecf20Sopenharmony_ci
7528c2ecf20Sopenharmony_ci	/* TODO: Cancel demod parameter */
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
7558c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
7568c2ecf20Sopenharmony_ci	/* disable ADC 1 */
7578c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
7588c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
7598c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
7608c2ecf20Sopenharmony_ci	/* Disable ADC 2 */
7618c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
7628c2ecf20Sopenharmony_ci	/* Disable ADC 3 */
7638c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
7648c2ecf20Sopenharmony_ci	/* Disable ADC clock */
7658c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
7668c2ecf20Sopenharmony_ci	/* Disable RF level monitor */
7678c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
7688c2ecf20Sopenharmony_ci	/* Disable demod clock */
7698c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
7708c2ecf20Sopenharmony_ci	priv->state = STATE_SLEEP_TC;
7718c2ecf20Sopenharmony_ci	return 0;
7728c2ecf20Sopenharmony_ci}
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_cistatic int cxd2841er_shutdown_to_sleep_s(struct cxd2841er_priv *priv)
7758c2ecf20Sopenharmony_ci{
7768c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
7778c2ecf20Sopenharmony_ci	if (priv->state != STATE_SHUTDOWN) {
7788c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
7798c2ecf20Sopenharmony_ci			__func__, priv->state);
7808c2ecf20Sopenharmony_ci		return -EINVAL;
7818c2ecf20Sopenharmony_ci	}
7828c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
7838c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
7848c2ecf20Sopenharmony_ci	/* Clear all demodulator registers */
7858c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
7868c2ecf20Sopenharmony_ci	usleep_range(3000, 5000);
7878c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
7888c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
7898c2ecf20Sopenharmony_ci	/* Set demod SW reset */
7908c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	switch (priv->xtal) {
7938c2ecf20Sopenharmony_ci	case SONY_XTAL_20500:
7948c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x00);
7958c2ecf20Sopenharmony_ci		break;
7968c2ecf20Sopenharmony_ci	case SONY_XTAL_24000:
7978c2ecf20Sopenharmony_ci		/* Select demod frequency */
7988c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
7998c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x03);
8008c2ecf20Sopenharmony_ci		break;
8018c2ecf20Sopenharmony_ci	case SONY_XTAL_41000:
8028c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x01);
8038c2ecf20Sopenharmony_ci		break;
8048c2ecf20Sopenharmony_ci	default:
8058c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid demod xtal %d\n",
8068c2ecf20Sopenharmony_ci				__func__, priv->xtal);
8078c2ecf20Sopenharmony_ci		return -EINVAL;
8088c2ecf20Sopenharmony_ci	}
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	/* Set demod mode */
8118c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x0a);
8128c2ecf20Sopenharmony_ci	/* Clear demod SW reset */
8138c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
8148c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
8158c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
8168c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
8178c2ecf20Sopenharmony_ci	/* enable DSQOUT */
8188c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1F);
8198c2ecf20Sopenharmony_ci	/* enable DSQIN */
8208c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x9C, 0x40);
8218c2ecf20Sopenharmony_ci	/* TADC Bias On */
8228c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
8238c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
8248c2ecf20Sopenharmony_ci	/* SADC Bias On */
8258c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
8268c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
8278c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
8288c2ecf20Sopenharmony_ci	priv->state = STATE_SLEEP_S;
8298c2ecf20Sopenharmony_ci	return 0;
8308c2ecf20Sopenharmony_ci}
8318c2ecf20Sopenharmony_ci
8328c2ecf20Sopenharmony_cistatic int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv)
8338c2ecf20Sopenharmony_ci{
8348c2ecf20Sopenharmony_ci	u8 data = 0;
8358c2ecf20Sopenharmony_ci
8368c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
8378c2ecf20Sopenharmony_ci	if (priv->state != STATE_SHUTDOWN) {
8388c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
8398c2ecf20Sopenharmony_ci			__func__, priv->state);
8408c2ecf20Sopenharmony_ci		return -EINVAL;
8418c2ecf20Sopenharmony_ci	}
8428c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
8438c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
8448c2ecf20Sopenharmony_ci	/* Clear all demodulator registers */
8458c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
8468c2ecf20Sopenharmony_ci	usleep_range(3000, 5000);
8478c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
8488c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
8498c2ecf20Sopenharmony_ci	/* Set demod SW reset */
8508c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
8518c2ecf20Sopenharmony_ci  /* Select ADC clock mode */
8528c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x13, 0x00);
8538c2ecf20Sopenharmony_ci
8548c2ecf20Sopenharmony_ci	switch (priv->xtal) {
8558c2ecf20Sopenharmony_ci	case SONY_XTAL_20500:
8568c2ecf20Sopenharmony_ci		data = 0x0;
8578c2ecf20Sopenharmony_ci		break;
8588c2ecf20Sopenharmony_ci	case SONY_XTAL_24000:
8598c2ecf20Sopenharmony_ci		/* Select demod frequency */
8608c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
8618c2ecf20Sopenharmony_ci		data = 0x3;
8628c2ecf20Sopenharmony_ci		break;
8638c2ecf20Sopenharmony_ci	case SONY_XTAL_41000:
8648c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
8658c2ecf20Sopenharmony_ci		data = 0x1;
8668c2ecf20Sopenharmony_ci		break;
8678c2ecf20Sopenharmony_ci	}
8688c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x14, data);
8698c2ecf20Sopenharmony_ci	/* Clear demod SW reset */
8708c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
8718c2ecf20Sopenharmony_ci	usleep_range(1000, 2000);
8728c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
8738c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
8748c2ecf20Sopenharmony_ci	/* TADC Bias On */
8758c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
8768c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
8778c2ecf20Sopenharmony_ci	/* SADC Bias On */
8788c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
8798c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
8808c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
8818c2ecf20Sopenharmony_ci	priv->state = STATE_SLEEP_TC;
8828c2ecf20Sopenharmony_ci	return 0;
8838c2ecf20Sopenharmony_ci}
8848c2ecf20Sopenharmony_ci
8858c2ecf20Sopenharmony_cistatic int cxd2841er_tune_done(struct cxd2841er_priv *priv)
8868c2ecf20Sopenharmony_ci{
8878c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
8888c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
8898c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0, 0);
8908c2ecf20Sopenharmony_ci	/* SW Reset */
8918c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xfe, 0x01);
8928c2ecf20Sopenharmony_ci	/* Enable TS output */
8938c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x00);
8948c2ecf20Sopenharmony_ci	return 0;
8958c2ecf20Sopenharmony_ci}
8968c2ecf20Sopenharmony_ci
8978c2ecf20Sopenharmony_ci/* Set TS parallel mode */
8988c2ecf20Sopenharmony_cistatic void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
8998c2ecf20Sopenharmony_ci					u8 system)
9008c2ecf20Sopenharmony_ci{
9018c2ecf20Sopenharmony_ci	u8 serial_ts, ts_rate_ctrl_off, ts_in_off;
9028c2ecf20Sopenharmony_ci
9038c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
9048c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
9058c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
9068c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0xc4, &serial_ts);
9078c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0xd3, &ts_rate_ctrl_off);
9088c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0xde, &ts_in_off);
9098c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): ser_ts=0x%02x rate_ctrl_off=0x%02x in_off=0x%02x\n",
9108c2ecf20Sopenharmony_ci		__func__, serial_ts, ts_rate_ctrl_off, ts_in_off);
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	/*
9138c2ecf20Sopenharmony_ci	 * slave    Bank    Addr    Bit    default    Name
9148c2ecf20Sopenharmony_ci	 * <SLV-T>  00h     C4h     [1:0]  2'b??      OSERCKMODE
9158c2ecf20Sopenharmony_ci	 */
9168c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4,
9178c2ecf20Sopenharmony_ci		((priv->flags & CXD2841ER_TS_SERIAL) ? 0x01 : 0x00), 0x03);
9188c2ecf20Sopenharmony_ci	/*
9198c2ecf20Sopenharmony_ci	 * slave    Bank    Addr    Bit    default    Name
9208c2ecf20Sopenharmony_ci	 * <SLV-T>  00h     D1h     [1:0]  2'b??      OSERDUTYMODE
9218c2ecf20Sopenharmony_ci	 */
9228c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd1,
9238c2ecf20Sopenharmony_ci		((priv->flags & CXD2841ER_TS_SERIAL) ? 0x01 : 0x00), 0x03);
9248c2ecf20Sopenharmony_ci	/*
9258c2ecf20Sopenharmony_ci	 * slave    Bank    Addr    Bit    default    Name
9268c2ecf20Sopenharmony_ci	 * <SLV-T>  00h     D9h     [7:0]  8'h08      OTSCKPERIOD
9278c2ecf20Sopenharmony_ci	 */
9288c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xd9, 0x08);
9298c2ecf20Sopenharmony_ci	/*
9308c2ecf20Sopenharmony_ci	 * Disable TS IF Clock
9318c2ecf20Sopenharmony_ci	 * slave    Bank    Addr    Bit    default    Name
9328c2ecf20Sopenharmony_ci	 * <SLV-T>  00h     32h     [0]    1'b1       OREG_CK_TSIF_EN
9338c2ecf20Sopenharmony_ci	 */
9348c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x00, 0x01);
9358c2ecf20Sopenharmony_ci	/*
9368c2ecf20Sopenharmony_ci	 * slave    Bank    Addr    Bit    default    Name
9378c2ecf20Sopenharmony_ci	 * <SLV-T>  00h     33h     [1:0]  2'b01      OREG_CKSEL_TSIF
9388c2ecf20Sopenharmony_ci	 */
9398c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x33,
9408c2ecf20Sopenharmony_ci		((priv->flags & CXD2841ER_TS_SERIAL) ? 0x01 : 0x00), 0x03);
9418c2ecf20Sopenharmony_ci	/*
9428c2ecf20Sopenharmony_ci	 * Enable TS IF Clock
9438c2ecf20Sopenharmony_ci	 * slave    Bank    Addr    Bit    default    Name
9448c2ecf20Sopenharmony_ci	 * <SLV-T>  00h     32h     [0]    1'b1       OREG_CK_TSIF_EN
9458c2ecf20Sopenharmony_ci	 */
9468c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x01, 0x01);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	if (system == SYS_DVBT) {
9498c2ecf20Sopenharmony_ci		/* Enable parity period for DVB-T */
9508c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
9518c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
9528c2ecf20Sopenharmony_ci	} else if (system == SYS_DVBC_ANNEX_A) {
9538c2ecf20Sopenharmony_ci		/* Enable parity period for DVB-C */
9548c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
9558c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
9568c2ecf20Sopenharmony_ci	}
9578c2ecf20Sopenharmony_ci}
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_cistatic u8 cxd2841er_chip_id(struct cxd2841er_priv *priv)
9608c2ecf20Sopenharmony_ci{
9618c2ecf20Sopenharmony_ci	u8 chip_id = 0;
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
9648c2ecf20Sopenharmony_ci	if (cxd2841er_write_reg(priv, I2C_SLVT, 0, 0) == 0)
9658c2ecf20Sopenharmony_ci		cxd2841er_read_reg(priv, I2C_SLVT, 0xfd, &chip_id);
9668c2ecf20Sopenharmony_ci	else if (cxd2841er_write_reg(priv, I2C_SLVX, 0, 0) == 0)
9678c2ecf20Sopenharmony_ci		cxd2841er_read_reg(priv, I2C_SLVX, 0xfd, &chip_id);
9688c2ecf20Sopenharmony_ci
9698c2ecf20Sopenharmony_ci	return chip_id;
9708c2ecf20Sopenharmony_ci}
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_cistatic int cxd2841er_read_status_s(struct dvb_frontend *fe,
9738c2ecf20Sopenharmony_ci				   enum fe_status *status)
9748c2ecf20Sopenharmony_ci{
9758c2ecf20Sopenharmony_ci	u8 reg = 0;
9768c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
9798c2ecf20Sopenharmony_ci	*status = 0;
9808c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_S) {
9818c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
9828c2ecf20Sopenharmony_ci			__func__, priv->state);
9838c2ecf20Sopenharmony_ci		return -EINVAL;
9848c2ecf20Sopenharmony_ci	}
9858c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0xA0 */
9868c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
9878c2ecf20Sopenharmony_ci	/*
9888c2ecf20Sopenharmony_ci	 *  slave     Bank      Addr      Bit      Signal name
9898c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       11h       [2]      ITSLOCK
9908c2ecf20Sopenharmony_ci	 */
9918c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0x11, &reg);
9928c2ecf20Sopenharmony_ci	if (reg & 0x04) {
9938c2ecf20Sopenharmony_ci		*status = FE_HAS_SIGNAL
9948c2ecf20Sopenharmony_ci			| FE_HAS_CARRIER
9958c2ecf20Sopenharmony_ci			| FE_HAS_VITERBI
9968c2ecf20Sopenharmony_ci			| FE_HAS_SYNC
9978c2ecf20Sopenharmony_ci			| FE_HAS_LOCK;
9988c2ecf20Sopenharmony_ci	}
9998c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): result 0x%x\n", __func__, *status);
10008c2ecf20Sopenharmony_ci	return 0;
10018c2ecf20Sopenharmony_ci}
10028c2ecf20Sopenharmony_ci
10038c2ecf20Sopenharmony_cistatic int cxd2841er_read_status_t_t2(struct cxd2841er_priv *priv,
10048c2ecf20Sopenharmony_ci				      u8 *sync, u8 *tslock, u8 *unlock)
10058c2ecf20Sopenharmony_ci{
10068c2ecf20Sopenharmony_ci	u8 data = 0;
10078c2ecf20Sopenharmony_ci
10088c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
10098c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC)
10108c2ecf20Sopenharmony_ci		return -EINVAL;
10118c2ecf20Sopenharmony_ci	if (priv->system == SYS_DVBT) {
10128c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x10 */
10138c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
10148c2ecf20Sopenharmony_ci	} else {
10158c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x20 */
10168c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
10178c2ecf20Sopenharmony_ci	}
10188c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
10198c2ecf20Sopenharmony_ci	if ((data & 0x07) == 0x07) {
10208c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
10218c2ecf20Sopenharmony_ci			"%s(): invalid hardware state detected\n", __func__);
10228c2ecf20Sopenharmony_ci		*sync = 0;
10238c2ecf20Sopenharmony_ci		*tslock = 0;
10248c2ecf20Sopenharmony_ci		*unlock = 0;
10258c2ecf20Sopenharmony_ci	} else {
10268c2ecf20Sopenharmony_ci		*sync = ((data & 0x07) == 0x6 ? 1 : 0);
10278c2ecf20Sopenharmony_ci		*tslock = ((data & 0x20) ? 1 : 0);
10288c2ecf20Sopenharmony_ci		*unlock = ((data & 0x10) ? 1 : 0);
10298c2ecf20Sopenharmony_ci	}
10308c2ecf20Sopenharmony_ci	return 0;
10318c2ecf20Sopenharmony_ci}
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_cistatic int cxd2841er_read_status_c(struct cxd2841er_priv *priv, u8 *tslock)
10348c2ecf20Sopenharmony_ci{
10358c2ecf20Sopenharmony_ci	u8 data;
10368c2ecf20Sopenharmony_ci
10378c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
10388c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC)
10398c2ecf20Sopenharmony_ci		return -EINVAL;
10408c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
10418c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0x88, &data);
10428c2ecf20Sopenharmony_ci	if ((data & 0x01) == 0) {
10438c2ecf20Sopenharmony_ci		*tslock = 0;
10448c2ecf20Sopenharmony_ci	} else {
10458c2ecf20Sopenharmony_ci		cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
10468c2ecf20Sopenharmony_ci		*tslock = ((data & 0x20) ? 1 : 0);
10478c2ecf20Sopenharmony_ci	}
10488c2ecf20Sopenharmony_ci	return 0;
10498c2ecf20Sopenharmony_ci}
10508c2ecf20Sopenharmony_ci
10518c2ecf20Sopenharmony_cistatic int cxd2841er_read_status_i(struct cxd2841er_priv *priv,
10528c2ecf20Sopenharmony_ci		u8 *sync, u8 *tslock, u8 *unlock)
10538c2ecf20Sopenharmony_ci{
10548c2ecf20Sopenharmony_ci	u8 data = 0;
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
10578c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC)
10588c2ecf20Sopenharmony_ci		return -EINVAL;
10598c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x60 */
10608c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
10618c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
10628c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev,
10638c2ecf20Sopenharmony_ci			"%s(): lock=0x%x\n", __func__, data);
10648c2ecf20Sopenharmony_ci	*sync = ((data & 0x02) ? 1 : 0);
10658c2ecf20Sopenharmony_ci	*tslock = ((data & 0x01) ? 1 : 0);
10668c2ecf20Sopenharmony_ci	*unlock = ((data & 0x10) ? 1 : 0);
10678c2ecf20Sopenharmony_ci	return 0;
10688c2ecf20Sopenharmony_ci}
10698c2ecf20Sopenharmony_ci
10708c2ecf20Sopenharmony_cistatic int cxd2841er_read_status_tc(struct dvb_frontend *fe,
10718c2ecf20Sopenharmony_ci				    enum fe_status *status)
10728c2ecf20Sopenharmony_ci{
10738c2ecf20Sopenharmony_ci	int ret = 0;
10748c2ecf20Sopenharmony_ci	u8 sync = 0;
10758c2ecf20Sopenharmony_ci	u8 tslock = 0;
10768c2ecf20Sopenharmony_ci	u8 unlock = 0;
10778c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
10788c2ecf20Sopenharmony_ci
10798c2ecf20Sopenharmony_ci	*status = 0;
10808c2ecf20Sopenharmony_ci	if (priv->state == STATE_ACTIVE_TC) {
10818c2ecf20Sopenharmony_ci		if (priv->system == SYS_DVBT || priv->system == SYS_DVBT2) {
10828c2ecf20Sopenharmony_ci			ret = cxd2841er_read_status_t_t2(
10838c2ecf20Sopenharmony_ci				priv, &sync, &tslock, &unlock);
10848c2ecf20Sopenharmony_ci			if (ret)
10858c2ecf20Sopenharmony_ci				goto done;
10868c2ecf20Sopenharmony_ci			if (unlock)
10878c2ecf20Sopenharmony_ci				goto done;
10888c2ecf20Sopenharmony_ci			if (sync)
10898c2ecf20Sopenharmony_ci				*status = FE_HAS_SIGNAL |
10908c2ecf20Sopenharmony_ci					FE_HAS_CARRIER |
10918c2ecf20Sopenharmony_ci					FE_HAS_VITERBI |
10928c2ecf20Sopenharmony_ci					FE_HAS_SYNC;
10938c2ecf20Sopenharmony_ci			if (tslock)
10948c2ecf20Sopenharmony_ci				*status |= FE_HAS_LOCK;
10958c2ecf20Sopenharmony_ci		} else if (priv->system == SYS_ISDBT) {
10968c2ecf20Sopenharmony_ci			ret = cxd2841er_read_status_i(
10978c2ecf20Sopenharmony_ci					priv, &sync, &tslock, &unlock);
10988c2ecf20Sopenharmony_ci			if (ret)
10998c2ecf20Sopenharmony_ci				goto done;
11008c2ecf20Sopenharmony_ci			if (unlock)
11018c2ecf20Sopenharmony_ci				goto done;
11028c2ecf20Sopenharmony_ci			if (sync)
11038c2ecf20Sopenharmony_ci				*status = FE_HAS_SIGNAL |
11048c2ecf20Sopenharmony_ci					FE_HAS_CARRIER |
11058c2ecf20Sopenharmony_ci					FE_HAS_VITERBI |
11068c2ecf20Sopenharmony_ci					FE_HAS_SYNC;
11078c2ecf20Sopenharmony_ci			if (tslock)
11088c2ecf20Sopenharmony_ci				*status |= FE_HAS_LOCK;
11098c2ecf20Sopenharmony_ci		} else if (priv->system == SYS_DVBC_ANNEX_A) {
11108c2ecf20Sopenharmony_ci			ret = cxd2841er_read_status_c(priv, &tslock);
11118c2ecf20Sopenharmony_ci			if (ret)
11128c2ecf20Sopenharmony_ci				goto done;
11138c2ecf20Sopenharmony_ci			if (tslock)
11148c2ecf20Sopenharmony_ci				*status = FE_HAS_SIGNAL |
11158c2ecf20Sopenharmony_ci					FE_HAS_CARRIER |
11168c2ecf20Sopenharmony_ci					FE_HAS_VITERBI |
11178c2ecf20Sopenharmony_ci					FE_HAS_SYNC |
11188c2ecf20Sopenharmony_ci					FE_HAS_LOCK;
11198c2ecf20Sopenharmony_ci		}
11208c2ecf20Sopenharmony_ci	}
11218c2ecf20Sopenharmony_cidone:
11228c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): status 0x%x\n", __func__, *status);
11238c2ecf20Sopenharmony_ci	return ret;
11248c2ecf20Sopenharmony_ci}
11258c2ecf20Sopenharmony_ci
11268c2ecf20Sopenharmony_cistatic int cxd2841er_get_carrier_offset_s_s2(struct cxd2841er_priv *priv,
11278c2ecf20Sopenharmony_ci					     int *offset)
11288c2ecf20Sopenharmony_ci{
11298c2ecf20Sopenharmony_ci	u8 data[3];
11308c2ecf20Sopenharmony_ci	u8 is_hs_mode;
11318c2ecf20Sopenharmony_ci	s32 cfrl_ctrlval;
11328c2ecf20Sopenharmony_ci	s32 temp_div, temp_q, temp_r;
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_S) {
11358c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
11368c2ecf20Sopenharmony_ci			__func__, priv->state);
11378c2ecf20Sopenharmony_ci		return -EINVAL;
11388c2ecf20Sopenharmony_ci	}
11398c2ecf20Sopenharmony_ci	/*
11408c2ecf20Sopenharmony_ci	 * Get High Sampling Rate mode
11418c2ecf20Sopenharmony_ci	 *  slave     Bank      Addr      Bit      Signal name
11428c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       10h       [0]      ITRL_LOCK
11438c2ecf20Sopenharmony_ci	 */
11448c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
11458c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data[0]);
11468c2ecf20Sopenharmony_ci	if (data[0] & 0x01) {
11478c2ecf20Sopenharmony_ci		/*
11488c2ecf20Sopenharmony_ci		 *  slave     Bank      Addr      Bit      Signal name
11498c2ecf20Sopenharmony_ci		 * <SLV-T>    A0h       50h       [4]      IHSMODE
11508c2ecf20Sopenharmony_ci		 */
11518c2ecf20Sopenharmony_ci		cxd2841er_read_reg(priv, I2C_SLVT, 0x50, &data[0]);
11528c2ecf20Sopenharmony_ci		is_hs_mode = (data[0] & 0x10 ? 1 : 0);
11538c2ecf20Sopenharmony_ci	} else {
11548c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
11558c2ecf20Sopenharmony_ci			"%s(): unable to detect sampling rate mode\n",
11568c2ecf20Sopenharmony_ci			__func__);
11578c2ecf20Sopenharmony_ci		return -EINVAL;
11588c2ecf20Sopenharmony_ci	}
11598c2ecf20Sopenharmony_ci	/*
11608c2ecf20Sopenharmony_ci	 *  slave     Bank      Addr      Bit      Signal name
11618c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       45h       [4:0]    ICFRL_CTRLVAL[20:16]
11628c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       46h       [7:0]    ICFRL_CTRLVAL[15:8]
11638c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       47h       [7:0]    ICFRL_CTRLVAL[7:0]
11648c2ecf20Sopenharmony_ci	 */
11658c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x45, data, 3);
11668c2ecf20Sopenharmony_ci	cfrl_ctrlval = sign_extend32((((u32)data[0] & 0x1F) << 16) |
11678c2ecf20Sopenharmony_ci				(((u32)data[1] & 0xFF) <<  8) |
11688c2ecf20Sopenharmony_ci				((u32)data[2] & 0xFF), 20);
11698c2ecf20Sopenharmony_ci	temp_div = (is_hs_mode ? 1048576 : 1572864);
11708c2ecf20Sopenharmony_ci	if (cfrl_ctrlval > 0) {
11718c2ecf20Sopenharmony_ci		temp_q = div_s64_rem(97375LL * cfrl_ctrlval,
11728c2ecf20Sopenharmony_ci			temp_div, &temp_r);
11738c2ecf20Sopenharmony_ci	} else {
11748c2ecf20Sopenharmony_ci		temp_q = div_s64_rem(-97375LL * cfrl_ctrlval,
11758c2ecf20Sopenharmony_ci			temp_div, &temp_r);
11768c2ecf20Sopenharmony_ci	}
11778c2ecf20Sopenharmony_ci	if (temp_r >= temp_div / 2)
11788c2ecf20Sopenharmony_ci		temp_q++;
11798c2ecf20Sopenharmony_ci	if (cfrl_ctrlval > 0)
11808c2ecf20Sopenharmony_ci		temp_q *= -1;
11818c2ecf20Sopenharmony_ci	*offset = temp_q;
11828c2ecf20Sopenharmony_ci	return 0;
11838c2ecf20Sopenharmony_ci}
11848c2ecf20Sopenharmony_ci
11858c2ecf20Sopenharmony_cistatic int cxd2841er_get_carrier_offset_i(struct cxd2841er_priv *priv,
11868c2ecf20Sopenharmony_ci					   u32 bandwidth, int *offset)
11878c2ecf20Sopenharmony_ci{
11888c2ecf20Sopenharmony_ci	u8 data[4];
11898c2ecf20Sopenharmony_ci
11908c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
11918c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
11928c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
11938c2ecf20Sopenharmony_ci			__func__, priv->state);
11948c2ecf20Sopenharmony_ci		return -EINVAL;
11958c2ecf20Sopenharmony_ci	}
11968c2ecf20Sopenharmony_ci	if (priv->system != SYS_ISDBT) {
11978c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
11988c2ecf20Sopenharmony_ci			__func__, priv->system);
11998c2ecf20Sopenharmony_ci		return -EINVAL;
12008c2ecf20Sopenharmony_ci	}
12018c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
12028c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
12038c2ecf20Sopenharmony_ci	*offset = -1 * sign_extend32(
12048c2ecf20Sopenharmony_ci		((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
12058c2ecf20Sopenharmony_ci		((u32)data[2] << 8) | (u32)data[3], 29);
12068c2ecf20Sopenharmony_ci
12078c2ecf20Sopenharmony_ci	switch (bandwidth) {
12088c2ecf20Sopenharmony_ci	case 6000000:
12098c2ecf20Sopenharmony_ci		*offset = -1 * ((*offset) * 8/264);
12108c2ecf20Sopenharmony_ci		break;
12118c2ecf20Sopenharmony_ci	case 7000000:
12128c2ecf20Sopenharmony_ci		*offset = -1 * ((*offset) * 8/231);
12138c2ecf20Sopenharmony_ci		break;
12148c2ecf20Sopenharmony_ci	case 8000000:
12158c2ecf20Sopenharmony_ci		*offset = -1 * ((*offset) * 8/198);
12168c2ecf20Sopenharmony_ci		break;
12178c2ecf20Sopenharmony_ci	default:
12188c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
12198c2ecf20Sopenharmony_ci				__func__, bandwidth);
12208c2ecf20Sopenharmony_ci		return -EINVAL;
12218c2ecf20Sopenharmony_ci	}
12228c2ecf20Sopenharmony_ci
12238c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): bandwidth %d offset %d\n",
12248c2ecf20Sopenharmony_ci			__func__, bandwidth, *offset);
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci	return 0;
12278c2ecf20Sopenharmony_ci}
12288c2ecf20Sopenharmony_ci
12298c2ecf20Sopenharmony_cistatic int cxd2841er_get_carrier_offset_t(struct cxd2841er_priv *priv,
12308c2ecf20Sopenharmony_ci					   u32 bandwidth, int *offset)
12318c2ecf20Sopenharmony_ci{
12328c2ecf20Sopenharmony_ci	u8 data[4];
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
12358c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
12368c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
12378c2ecf20Sopenharmony_ci			__func__, priv->state);
12388c2ecf20Sopenharmony_ci		return -EINVAL;
12398c2ecf20Sopenharmony_ci	}
12408c2ecf20Sopenharmony_ci	if (priv->system != SYS_DVBT) {
12418c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
12428c2ecf20Sopenharmony_ci			__func__, priv->system);
12438c2ecf20Sopenharmony_ci		return -EINVAL;
12448c2ecf20Sopenharmony_ci	}
12458c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
12468c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
12478c2ecf20Sopenharmony_ci	*offset = -1 * sign_extend32(
12488c2ecf20Sopenharmony_ci		((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
12498c2ecf20Sopenharmony_ci		((u32)data[2] << 8) | (u32)data[3], 29);
12508c2ecf20Sopenharmony_ci	*offset *= (bandwidth / 1000000);
12518c2ecf20Sopenharmony_ci	*offset /= 235;
12528c2ecf20Sopenharmony_ci	return 0;
12538c2ecf20Sopenharmony_ci}
12548c2ecf20Sopenharmony_ci
12558c2ecf20Sopenharmony_cistatic int cxd2841er_get_carrier_offset_t2(struct cxd2841er_priv *priv,
12568c2ecf20Sopenharmony_ci					   u32 bandwidth, int *offset)
12578c2ecf20Sopenharmony_ci{
12588c2ecf20Sopenharmony_ci	u8 data[4];
12598c2ecf20Sopenharmony_ci
12608c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
12618c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
12628c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
12638c2ecf20Sopenharmony_ci			__func__, priv->state);
12648c2ecf20Sopenharmony_ci		return -EINVAL;
12658c2ecf20Sopenharmony_ci	}
12668c2ecf20Sopenharmony_ci	if (priv->system != SYS_DVBT2) {
12678c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
12688c2ecf20Sopenharmony_ci			__func__, priv->system);
12698c2ecf20Sopenharmony_ci		return -EINVAL;
12708c2ecf20Sopenharmony_ci	}
12718c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
12728c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
12738c2ecf20Sopenharmony_ci	*offset = -1 * sign_extend32(
12748c2ecf20Sopenharmony_ci		((u32)(data[0] & 0x0F) << 24) | ((u32)data[1] << 16) |
12758c2ecf20Sopenharmony_ci		((u32)data[2] << 8) | (u32)data[3], 27);
12768c2ecf20Sopenharmony_ci	switch (bandwidth) {
12778c2ecf20Sopenharmony_ci	case 1712000:
12788c2ecf20Sopenharmony_ci		*offset /= 582;
12798c2ecf20Sopenharmony_ci		break;
12808c2ecf20Sopenharmony_ci	case 5000000:
12818c2ecf20Sopenharmony_ci	case 6000000:
12828c2ecf20Sopenharmony_ci	case 7000000:
12838c2ecf20Sopenharmony_ci	case 8000000:
12848c2ecf20Sopenharmony_ci		*offset *= (bandwidth / 1000000);
12858c2ecf20Sopenharmony_ci		*offset /= 940;
12868c2ecf20Sopenharmony_ci		break;
12878c2ecf20Sopenharmony_ci	default:
12888c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
12898c2ecf20Sopenharmony_ci			__func__, bandwidth);
12908c2ecf20Sopenharmony_ci		return -EINVAL;
12918c2ecf20Sopenharmony_ci	}
12928c2ecf20Sopenharmony_ci	return 0;
12938c2ecf20Sopenharmony_ci}
12948c2ecf20Sopenharmony_ci
12958c2ecf20Sopenharmony_cistatic int cxd2841er_get_carrier_offset_c(struct cxd2841er_priv *priv,
12968c2ecf20Sopenharmony_ci					  int *offset)
12978c2ecf20Sopenharmony_ci{
12988c2ecf20Sopenharmony_ci	u8 data[2];
12998c2ecf20Sopenharmony_ci
13008c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
13018c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
13028c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
13038c2ecf20Sopenharmony_ci			__func__, priv->state);
13048c2ecf20Sopenharmony_ci		return -EINVAL;
13058c2ecf20Sopenharmony_ci	}
13068c2ecf20Sopenharmony_ci	if (priv->system != SYS_DVBC_ANNEX_A) {
13078c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
13088c2ecf20Sopenharmony_ci			__func__, priv->system);
13098c2ecf20Sopenharmony_ci		return -EINVAL;
13108c2ecf20Sopenharmony_ci	}
13118c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
13128c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x15, data, sizeof(data));
13138c2ecf20Sopenharmony_ci	*offset = div_s64(41000LL * sign_extend32((((u32)data[0] & 0x3f) << 8)
13148c2ecf20Sopenharmony_ci						| (u32)data[1], 13), 16384);
13158c2ecf20Sopenharmony_ci	return 0;
13168c2ecf20Sopenharmony_ci}
13178c2ecf20Sopenharmony_ci
13188c2ecf20Sopenharmony_cistatic int cxd2841er_read_packet_errors_c(
13198c2ecf20Sopenharmony_ci		struct cxd2841er_priv *priv, u32 *penum)
13208c2ecf20Sopenharmony_ci{
13218c2ecf20Sopenharmony_ci	u8 data[3];
13228c2ecf20Sopenharmony_ci
13238c2ecf20Sopenharmony_ci	*penum = 0;
13248c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
13258c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
13268c2ecf20Sopenharmony_ci				__func__, priv->state);
13278c2ecf20Sopenharmony_ci		return -EINVAL;
13288c2ecf20Sopenharmony_ci	}
13298c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
13308c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
13318c2ecf20Sopenharmony_ci	if (data[2] & 0x01)
13328c2ecf20Sopenharmony_ci		*penum = ((u32)data[0] << 8) | (u32)data[1];
13338c2ecf20Sopenharmony_ci	return 0;
13348c2ecf20Sopenharmony_ci}
13358c2ecf20Sopenharmony_ci
13368c2ecf20Sopenharmony_cistatic int cxd2841er_read_packet_errors_t(
13378c2ecf20Sopenharmony_ci		struct cxd2841er_priv *priv, u32 *penum)
13388c2ecf20Sopenharmony_ci{
13398c2ecf20Sopenharmony_ci	u8 data[3];
13408c2ecf20Sopenharmony_ci
13418c2ecf20Sopenharmony_ci	*penum = 0;
13428c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
13438c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
13448c2ecf20Sopenharmony_ci			__func__, priv->state);
13458c2ecf20Sopenharmony_ci		return -EINVAL;
13468c2ecf20Sopenharmony_ci	}
13478c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
13488c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
13498c2ecf20Sopenharmony_ci	if (data[2] & 0x01)
13508c2ecf20Sopenharmony_ci		*penum = ((u32)data[0] << 8) | (u32)data[1];
13518c2ecf20Sopenharmony_ci	return 0;
13528c2ecf20Sopenharmony_ci}
13538c2ecf20Sopenharmony_ci
13548c2ecf20Sopenharmony_cistatic int cxd2841er_read_packet_errors_t2(
13558c2ecf20Sopenharmony_ci		struct cxd2841er_priv *priv, u32 *penum)
13568c2ecf20Sopenharmony_ci{
13578c2ecf20Sopenharmony_ci	u8 data[3];
13588c2ecf20Sopenharmony_ci
13598c2ecf20Sopenharmony_ci	*penum = 0;
13608c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
13618c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
13628c2ecf20Sopenharmony_ci			__func__, priv->state);
13638c2ecf20Sopenharmony_ci		return -EINVAL;
13648c2ecf20Sopenharmony_ci	}
13658c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
13668c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0xfd, data, sizeof(data));
13678c2ecf20Sopenharmony_ci	if (data[0] & 0x01)
13688c2ecf20Sopenharmony_ci		*penum = ((u32)data[1] << 8) | (u32)data[2];
13698c2ecf20Sopenharmony_ci	return 0;
13708c2ecf20Sopenharmony_ci}
13718c2ecf20Sopenharmony_ci
13728c2ecf20Sopenharmony_cistatic int cxd2841er_read_packet_errors_i(
13738c2ecf20Sopenharmony_ci		struct cxd2841er_priv *priv, u32 *penum)
13748c2ecf20Sopenharmony_ci{
13758c2ecf20Sopenharmony_ci	u8 data[2];
13768c2ecf20Sopenharmony_ci
13778c2ecf20Sopenharmony_ci	*penum = 0;
13788c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
13798c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
13808c2ecf20Sopenharmony_ci				__func__, priv->state);
13818c2ecf20Sopenharmony_ci		return -EINVAL;
13828c2ecf20Sopenharmony_ci	}
13838c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
13848c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0xA1, data, 1);
13858c2ecf20Sopenharmony_ci
13868c2ecf20Sopenharmony_ci	if (!(data[0] & 0x01))
13878c2ecf20Sopenharmony_ci		return 0;
13888c2ecf20Sopenharmony_ci
13898c2ecf20Sopenharmony_ci	/* Layer A */
13908c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0xA2, data, sizeof(data));
13918c2ecf20Sopenharmony_ci	*penum = ((u32)data[0] << 8) | (u32)data[1];
13928c2ecf20Sopenharmony_ci
13938c2ecf20Sopenharmony_ci	/* Layer B */
13948c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0xA4, data, sizeof(data));
13958c2ecf20Sopenharmony_ci	*penum += ((u32)data[0] << 8) | (u32)data[1];
13968c2ecf20Sopenharmony_ci
13978c2ecf20Sopenharmony_ci	/* Layer C */
13988c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0xA6, data, sizeof(data));
13998c2ecf20Sopenharmony_ci	*penum += ((u32)data[0] << 8) | (u32)data[1];
14008c2ecf20Sopenharmony_ci
14018c2ecf20Sopenharmony_ci	return 0;
14028c2ecf20Sopenharmony_ci}
14038c2ecf20Sopenharmony_ci
14048c2ecf20Sopenharmony_cistatic int cxd2841er_read_ber_c(struct cxd2841er_priv *priv,
14058c2ecf20Sopenharmony_ci		u32 *bit_error, u32 *bit_count)
14068c2ecf20Sopenharmony_ci{
14078c2ecf20Sopenharmony_ci	u8 data[3];
14088c2ecf20Sopenharmony_ci	u32 bit_err, period_exp;
14098c2ecf20Sopenharmony_ci
14108c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
14118c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
14128c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
14138c2ecf20Sopenharmony_ci				__func__, priv->state);
14148c2ecf20Sopenharmony_ci		return -EINVAL;
14158c2ecf20Sopenharmony_ci	}
14168c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
14178c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x62, data, sizeof(data));
14188c2ecf20Sopenharmony_ci	if (!(data[0] & 0x80)) {
14198c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
14208c2ecf20Sopenharmony_ci				"%s(): no valid BER data\n", __func__);
14218c2ecf20Sopenharmony_ci		return -EINVAL;
14228c2ecf20Sopenharmony_ci	}
14238c2ecf20Sopenharmony_ci	bit_err = ((u32)(data[0] & 0x3f) << 16) |
14248c2ecf20Sopenharmony_ci		((u32)data[1] << 8) |
14258c2ecf20Sopenharmony_ci		(u32)data[2];
14268c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0x60, data);
14278c2ecf20Sopenharmony_ci	period_exp = data[0] & 0x1f;
14288c2ecf20Sopenharmony_ci
14298c2ecf20Sopenharmony_ci	if ((period_exp <= 11) && (bit_err > (1 << period_exp) * 204 * 8)) {
14308c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
14318c2ecf20Sopenharmony_ci				"%s(): period_exp(%u) or bit_err(%u)  not in range. no valid BER data\n",
14328c2ecf20Sopenharmony_ci				__func__, period_exp, bit_err);
14338c2ecf20Sopenharmony_ci		return -EINVAL;
14348c2ecf20Sopenharmony_ci	}
14358c2ecf20Sopenharmony_ci
14368c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev,
14378c2ecf20Sopenharmony_ci			"%s(): period_exp(%u) or bit_err(%u) count=%d\n",
14388c2ecf20Sopenharmony_ci			__func__, period_exp, bit_err,
14398c2ecf20Sopenharmony_ci			((1 << period_exp) * 204 * 8));
14408c2ecf20Sopenharmony_ci
14418c2ecf20Sopenharmony_ci	*bit_error = bit_err;
14428c2ecf20Sopenharmony_ci	*bit_count = ((1 << period_exp) * 204 * 8);
14438c2ecf20Sopenharmony_ci
14448c2ecf20Sopenharmony_ci	return 0;
14458c2ecf20Sopenharmony_ci}
14468c2ecf20Sopenharmony_ci
14478c2ecf20Sopenharmony_cistatic int cxd2841er_read_ber_i(struct cxd2841er_priv *priv,
14488c2ecf20Sopenharmony_ci		u32 *bit_error, u32 *bit_count)
14498c2ecf20Sopenharmony_ci{
14508c2ecf20Sopenharmony_ci	u8 data[3];
14518c2ecf20Sopenharmony_ci	u8 pktnum[2];
14528c2ecf20Sopenharmony_ci
14538c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
14548c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
14558c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
14568c2ecf20Sopenharmony_ci				__func__, priv->state);
14578c2ecf20Sopenharmony_ci		return -EINVAL;
14588c2ecf20Sopenharmony_ci	}
14598c2ecf20Sopenharmony_ci
14608c2ecf20Sopenharmony_ci	cxd2841er_freeze_regs(priv);
14618c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
14628c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x5B, pktnum, sizeof(pktnum));
14638c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x16, data, sizeof(data));
14648c2ecf20Sopenharmony_ci	cxd2841er_unfreeze_regs(priv);
14658c2ecf20Sopenharmony_ci
14668c2ecf20Sopenharmony_ci	if (!pktnum[0] && !pktnum[1]) {
14678c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
14688c2ecf20Sopenharmony_ci				"%s(): no valid BER data\n", __func__);
14698c2ecf20Sopenharmony_ci		return -EINVAL;
14708c2ecf20Sopenharmony_ci	}
14718c2ecf20Sopenharmony_ci
14728c2ecf20Sopenharmony_ci	*bit_error = ((u32)(data[0] & 0x7F) << 16) |
14738c2ecf20Sopenharmony_ci		((u32)data[1] << 8) | data[2];
14748c2ecf20Sopenharmony_ci	*bit_count = ((((u32)pktnum[0] << 8) | pktnum[1]) * 204 * 8);
14758c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): bit_error=%u bit_count=%u\n",
14768c2ecf20Sopenharmony_ci			__func__, *bit_error, *bit_count);
14778c2ecf20Sopenharmony_ci
14788c2ecf20Sopenharmony_ci	return 0;
14798c2ecf20Sopenharmony_ci}
14808c2ecf20Sopenharmony_ci
14818c2ecf20Sopenharmony_cistatic int cxd2841er_mon_read_ber_s(struct cxd2841er_priv *priv,
14828c2ecf20Sopenharmony_ci				    u32 *bit_error, u32 *bit_count)
14838c2ecf20Sopenharmony_ci{
14848c2ecf20Sopenharmony_ci	u8 data[11];
14858c2ecf20Sopenharmony_ci
14868c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0xA0 */
14878c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
14888c2ecf20Sopenharmony_ci	/*
14898c2ecf20Sopenharmony_ci	 *  slave     Bank      Addr      Bit      Signal name
14908c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       35h       [0]      IFVBER_VALID
14918c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       36h       [5:0]    IFVBER_BITERR[21:16]
14928c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       37h       [7:0]    IFVBER_BITERR[15:8]
14938c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       38h       [7:0]    IFVBER_BITERR[7:0]
14948c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       3Dh       [5:0]    IFVBER_BITNUM[21:16]
14958c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       3Eh       [7:0]    IFVBER_BITNUM[15:8]
14968c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       3Fh       [7:0]    IFVBER_BITNUM[7:0]
14978c2ecf20Sopenharmony_ci	 */
14988c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x35, data, 11);
14998c2ecf20Sopenharmony_ci	if (data[0] & 0x01) {
15008c2ecf20Sopenharmony_ci		*bit_error = ((u32)(data[1]  & 0x3F) << 16) |
15018c2ecf20Sopenharmony_ci			     ((u32)(data[2]  & 0xFF) <<  8) |
15028c2ecf20Sopenharmony_ci			     (u32)(data[3]  & 0xFF);
15038c2ecf20Sopenharmony_ci		*bit_count = ((u32)(data[8]  & 0x3F) << 16) |
15048c2ecf20Sopenharmony_ci			     ((u32)(data[9]  & 0xFF) <<  8) |
15058c2ecf20Sopenharmony_ci			     (u32)(data[10] & 0xFF);
15068c2ecf20Sopenharmony_ci		if ((*bit_count == 0) || (*bit_error > *bit_count)) {
15078c2ecf20Sopenharmony_ci			dev_dbg(&priv->i2c->dev,
15088c2ecf20Sopenharmony_ci				"%s(): invalid bit_error %d, bit_count %d\n",
15098c2ecf20Sopenharmony_ci				__func__, *bit_error, *bit_count);
15108c2ecf20Sopenharmony_ci			return -EINVAL;
15118c2ecf20Sopenharmony_ci		}
15128c2ecf20Sopenharmony_ci		return 0;
15138c2ecf20Sopenharmony_ci	}
15148c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): no data available\n", __func__);
15158c2ecf20Sopenharmony_ci	return -EINVAL;
15168c2ecf20Sopenharmony_ci}
15178c2ecf20Sopenharmony_ci
15188c2ecf20Sopenharmony_ci
15198c2ecf20Sopenharmony_cistatic int cxd2841er_mon_read_ber_s2(struct cxd2841er_priv *priv,
15208c2ecf20Sopenharmony_ci				     u32 *bit_error, u32 *bit_count)
15218c2ecf20Sopenharmony_ci{
15228c2ecf20Sopenharmony_ci	u8 data[5];
15238c2ecf20Sopenharmony_ci	u32 period;
15248c2ecf20Sopenharmony_ci
15258c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0xB2 */
15268c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xb2);
15278c2ecf20Sopenharmony_ci	/*
15288c2ecf20Sopenharmony_ci	 *  slave     Bank      Addr      Bit      Signal name
15298c2ecf20Sopenharmony_ci	 * <SLV-T>    B2h       30h       [0]      IFLBER_VALID
15308c2ecf20Sopenharmony_ci	 * <SLV-T>    B2h       31h       [3:0]    IFLBER_BITERR[27:24]
15318c2ecf20Sopenharmony_ci	 * <SLV-T>    B2h       32h       [7:0]    IFLBER_BITERR[23:16]
15328c2ecf20Sopenharmony_ci	 * <SLV-T>    B2h       33h       [7:0]    IFLBER_BITERR[15:8]
15338c2ecf20Sopenharmony_ci	 * <SLV-T>    B2h       34h       [7:0]    IFLBER_BITERR[7:0]
15348c2ecf20Sopenharmony_ci	 */
15358c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x30, data, 5);
15368c2ecf20Sopenharmony_ci	if (data[0] & 0x01) {
15378c2ecf20Sopenharmony_ci		/* Bit error count */
15388c2ecf20Sopenharmony_ci		*bit_error = ((u32)(data[1] & 0x0F) << 24) |
15398c2ecf20Sopenharmony_ci			     ((u32)(data[2] & 0xFF) << 16) |
15408c2ecf20Sopenharmony_ci			     ((u32)(data[3] & 0xFF) <<  8) |
15418c2ecf20Sopenharmony_ci			     (u32)(data[4] & 0xFF);
15428c2ecf20Sopenharmony_ci
15438c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0xA0 */
15448c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
15458c2ecf20Sopenharmony_ci		cxd2841er_read_reg(priv, I2C_SLVT, 0x7a, data);
15468c2ecf20Sopenharmony_ci		/* Measurement period */
15478c2ecf20Sopenharmony_ci		period = (u32)(1 << (data[0] & 0x0F));
15488c2ecf20Sopenharmony_ci		if (period == 0) {
15498c2ecf20Sopenharmony_ci			dev_dbg(&priv->i2c->dev,
15508c2ecf20Sopenharmony_ci				"%s(): period is 0\n", __func__);
15518c2ecf20Sopenharmony_ci			return -EINVAL;
15528c2ecf20Sopenharmony_ci		}
15538c2ecf20Sopenharmony_ci		if (*bit_error > (period * 64800)) {
15548c2ecf20Sopenharmony_ci			dev_dbg(&priv->i2c->dev,
15558c2ecf20Sopenharmony_ci				"%s(): invalid bit_err 0x%x period 0x%x\n",
15568c2ecf20Sopenharmony_ci				__func__, *bit_error, period);
15578c2ecf20Sopenharmony_ci			return -EINVAL;
15588c2ecf20Sopenharmony_ci		}
15598c2ecf20Sopenharmony_ci		*bit_count = period * 64800;
15608c2ecf20Sopenharmony_ci
15618c2ecf20Sopenharmony_ci		return 0;
15628c2ecf20Sopenharmony_ci	} else {
15638c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
15648c2ecf20Sopenharmony_ci			"%s(): no data available\n", __func__);
15658c2ecf20Sopenharmony_ci	}
15668c2ecf20Sopenharmony_ci	return -EINVAL;
15678c2ecf20Sopenharmony_ci}
15688c2ecf20Sopenharmony_ci
15698c2ecf20Sopenharmony_cistatic int cxd2841er_read_ber_t2(struct cxd2841er_priv *priv,
15708c2ecf20Sopenharmony_ci				 u32 *bit_error, u32 *bit_count)
15718c2ecf20Sopenharmony_ci{
15728c2ecf20Sopenharmony_ci	u8 data[4];
15738c2ecf20Sopenharmony_ci	u32 period_exp, n_ldpc;
15748c2ecf20Sopenharmony_ci
15758c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
15768c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
15778c2ecf20Sopenharmony_ci			"%s(): invalid state %d\n", __func__, priv->state);
15788c2ecf20Sopenharmony_ci		return -EINVAL;
15798c2ecf20Sopenharmony_ci	}
15808c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
15818c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x39, data, sizeof(data));
15828c2ecf20Sopenharmony_ci	if (!(data[0] & 0x10)) {
15838c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
15848c2ecf20Sopenharmony_ci			"%s(): no valid BER data\n", __func__);
15858c2ecf20Sopenharmony_ci		return -EINVAL;
15868c2ecf20Sopenharmony_ci	}
15878c2ecf20Sopenharmony_ci	*bit_error = ((u32)(data[0] & 0x0f) << 24) |
15888c2ecf20Sopenharmony_ci		     ((u32)data[1] << 16) |
15898c2ecf20Sopenharmony_ci		     ((u32)data[2] << 8) |
15908c2ecf20Sopenharmony_ci		     (u32)data[3];
15918c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
15928c2ecf20Sopenharmony_ci	period_exp = data[0] & 0x0f;
15938c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x22);
15948c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0x5e, data);
15958c2ecf20Sopenharmony_ci	n_ldpc = ((data[0] & 0x03) == 0 ? 16200 : 64800);
15968c2ecf20Sopenharmony_ci	if (*bit_error > ((1U << period_exp) * n_ldpc)) {
15978c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
15988c2ecf20Sopenharmony_ci			"%s(): invalid BER value\n", __func__);
15998c2ecf20Sopenharmony_ci		return -EINVAL;
16008c2ecf20Sopenharmony_ci	}
16018c2ecf20Sopenharmony_ci
16028c2ecf20Sopenharmony_ci	/*
16038c2ecf20Sopenharmony_ci	 * FIXME: the right thing would be to return bit_error untouched,
16048c2ecf20Sopenharmony_ci	 * but, as we don't know the scale returned by the counters, let's
16058c2ecf20Sopenharmony_ci	 * at least preserver BER = bit_error/bit_count.
16068c2ecf20Sopenharmony_ci	 */
16078c2ecf20Sopenharmony_ci	if (period_exp >= 4) {
16088c2ecf20Sopenharmony_ci		*bit_count = (1U << (period_exp - 4)) * (n_ldpc / 200);
16098c2ecf20Sopenharmony_ci		*bit_error *= 3125ULL;
16108c2ecf20Sopenharmony_ci	} else {
16118c2ecf20Sopenharmony_ci		*bit_count = (1U << period_exp) * (n_ldpc / 200);
16128c2ecf20Sopenharmony_ci		*bit_error *= 50000ULL;
16138c2ecf20Sopenharmony_ci	}
16148c2ecf20Sopenharmony_ci	return 0;
16158c2ecf20Sopenharmony_ci}
16168c2ecf20Sopenharmony_ci
16178c2ecf20Sopenharmony_cistatic int cxd2841er_read_ber_t(struct cxd2841er_priv *priv,
16188c2ecf20Sopenharmony_ci				u32 *bit_error, u32 *bit_count)
16198c2ecf20Sopenharmony_ci{
16208c2ecf20Sopenharmony_ci	u8 data[2];
16218c2ecf20Sopenharmony_ci	u32 period;
16228c2ecf20Sopenharmony_ci
16238c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
16248c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
16258c2ecf20Sopenharmony_ci			"%s(): invalid state %d\n", __func__, priv->state);
16268c2ecf20Sopenharmony_ci		return -EINVAL;
16278c2ecf20Sopenharmony_ci	}
16288c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
16298c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0x39, data);
16308c2ecf20Sopenharmony_ci	if (!(data[0] & 0x01)) {
16318c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
16328c2ecf20Sopenharmony_ci			"%s(): no valid BER data\n", __func__);
16338c2ecf20Sopenharmony_ci		return 0;
16348c2ecf20Sopenharmony_ci	}
16358c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x22, data, sizeof(data));
16368c2ecf20Sopenharmony_ci	*bit_error = ((u32)data[0] << 8) | (u32)data[1];
16378c2ecf20Sopenharmony_ci	cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
16388c2ecf20Sopenharmony_ci	period = ((data[0] & 0x07) == 0) ? 256 : (4096 << (data[0] & 0x07));
16398c2ecf20Sopenharmony_ci
16408c2ecf20Sopenharmony_ci	/*
16418c2ecf20Sopenharmony_ci	 * FIXME: the right thing would be to return bit_error untouched,
16428c2ecf20Sopenharmony_ci	 * but, as we don't know the scale returned by the counters, let's
16438c2ecf20Sopenharmony_ci	 * at least preserver BER = bit_error/bit_count.
16448c2ecf20Sopenharmony_ci	 */
16458c2ecf20Sopenharmony_ci	*bit_count = period / 128;
16468c2ecf20Sopenharmony_ci	*bit_error *= 78125ULL;
16478c2ecf20Sopenharmony_ci	return 0;
16488c2ecf20Sopenharmony_ci}
16498c2ecf20Sopenharmony_ci
16508c2ecf20Sopenharmony_cistatic int cxd2841er_freeze_regs(struct cxd2841er_priv *priv)
16518c2ecf20Sopenharmony_ci{
16528c2ecf20Sopenharmony_ci	/*
16538c2ecf20Sopenharmony_ci	 * Freeze registers: ensure multiple separate register reads
16548c2ecf20Sopenharmony_ci	 * are from the same snapshot
16558c2ecf20Sopenharmony_ci	 */
16568c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x01);
16578c2ecf20Sopenharmony_ci	return 0;
16588c2ecf20Sopenharmony_ci}
16598c2ecf20Sopenharmony_ci
16608c2ecf20Sopenharmony_cistatic int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv)
16618c2ecf20Sopenharmony_ci{
16628c2ecf20Sopenharmony_ci	/*
16638c2ecf20Sopenharmony_ci	 * un-freeze registers
16648c2ecf20Sopenharmony_ci	 */
16658c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x00);
16668c2ecf20Sopenharmony_ci	return 0;
16678c2ecf20Sopenharmony_ci}
16688c2ecf20Sopenharmony_ci
16698c2ecf20Sopenharmony_cistatic u32 cxd2841er_dvbs_read_snr(struct cxd2841er_priv *priv,
16708c2ecf20Sopenharmony_ci		u8 delsys, u32 *snr)
16718c2ecf20Sopenharmony_ci{
16728c2ecf20Sopenharmony_ci	u8 data[3];
16738c2ecf20Sopenharmony_ci	u32 res = 0, value;
16748c2ecf20Sopenharmony_ci	int min_index, max_index, index;
16758c2ecf20Sopenharmony_ci	static const struct cxd2841er_cnr_data *cn_data;
16768c2ecf20Sopenharmony_ci
16778c2ecf20Sopenharmony_ci	cxd2841er_freeze_regs(priv);
16788c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0xA1 */
16798c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa1);
16808c2ecf20Sopenharmony_ci	/*
16818c2ecf20Sopenharmony_ci	 *  slave     Bank      Addr      Bit     Signal name
16828c2ecf20Sopenharmony_ci	 * <SLV-T>    A1h       10h       [0]     ICPM_QUICKRDY
16838c2ecf20Sopenharmony_ci	 * <SLV-T>    A1h       11h       [4:0]   ICPM_QUICKCNDT[12:8]
16848c2ecf20Sopenharmony_ci	 * <SLV-T>    A1h       12h       [7:0]   ICPM_QUICKCNDT[7:0]
16858c2ecf20Sopenharmony_ci	 */
16868c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x10, data, 3);
16878c2ecf20Sopenharmony_ci	cxd2841er_unfreeze_regs(priv);
16888c2ecf20Sopenharmony_ci
16898c2ecf20Sopenharmony_ci	if (data[0] & 0x01) {
16908c2ecf20Sopenharmony_ci		value = ((u32)(data[1] & 0x1F) << 8) | (u32)(data[2] & 0xFF);
16918c2ecf20Sopenharmony_ci		min_index = 0;
16928c2ecf20Sopenharmony_ci		if (delsys == SYS_DVBS) {
16938c2ecf20Sopenharmony_ci			cn_data = s_cn_data;
16948c2ecf20Sopenharmony_ci			max_index = ARRAY_SIZE(s_cn_data) - 1;
16958c2ecf20Sopenharmony_ci		} else {
16968c2ecf20Sopenharmony_ci			cn_data = s2_cn_data;
16978c2ecf20Sopenharmony_ci			max_index = ARRAY_SIZE(s2_cn_data) - 1;
16988c2ecf20Sopenharmony_ci		}
16998c2ecf20Sopenharmony_ci		if (value >= cn_data[min_index].value) {
17008c2ecf20Sopenharmony_ci			res = cn_data[min_index].cnr_x1000;
17018c2ecf20Sopenharmony_ci			goto done;
17028c2ecf20Sopenharmony_ci		}
17038c2ecf20Sopenharmony_ci		if (value <= cn_data[max_index].value) {
17048c2ecf20Sopenharmony_ci			res = cn_data[max_index].cnr_x1000;
17058c2ecf20Sopenharmony_ci			goto done;
17068c2ecf20Sopenharmony_ci		}
17078c2ecf20Sopenharmony_ci		while ((max_index - min_index) > 1) {
17088c2ecf20Sopenharmony_ci			index = (max_index + min_index) / 2;
17098c2ecf20Sopenharmony_ci			if (value == cn_data[index].value) {
17108c2ecf20Sopenharmony_ci				res = cn_data[index].cnr_x1000;
17118c2ecf20Sopenharmony_ci				goto done;
17128c2ecf20Sopenharmony_ci			} else if (value > cn_data[index].value)
17138c2ecf20Sopenharmony_ci				max_index = index;
17148c2ecf20Sopenharmony_ci			else
17158c2ecf20Sopenharmony_ci				min_index = index;
17168c2ecf20Sopenharmony_ci			if ((max_index - min_index) <= 1) {
17178c2ecf20Sopenharmony_ci				if (value == cn_data[max_index].value) {
17188c2ecf20Sopenharmony_ci					res = cn_data[max_index].cnr_x1000;
17198c2ecf20Sopenharmony_ci					goto done;
17208c2ecf20Sopenharmony_ci				} else {
17218c2ecf20Sopenharmony_ci					res = cn_data[min_index].cnr_x1000;
17228c2ecf20Sopenharmony_ci					goto done;
17238c2ecf20Sopenharmony_ci				}
17248c2ecf20Sopenharmony_ci			}
17258c2ecf20Sopenharmony_ci		}
17268c2ecf20Sopenharmony_ci	} else {
17278c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
17288c2ecf20Sopenharmony_ci			"%s(): no data available\n", __func__);
17298c2ecf20Sopenharmony_ci		return -EINVAL;
17308c2ecf20Sopenharmony_ci	}
17318c2ecf20Sopenharmony_cidone:
17328c2ecf20Sopenharmony_ci	*snr = res;
17338c2ecf20Sopenharmony_ci	return 0;
17348c2ecf20Sopenharmony_ci}
17358c2ecf20Sopenharmony_ci
17368c2ecf20Sopenharmony_cistatic uint32_t sony_log(uint32_t x)
17378c2ecf20Sopenharmony_ci{
17388c2ecf20Sopenharmony_ci	return (((10000>>8)*(intlog2(x)>>16) + LOG2_E_100X/2)/LOG2_E_100X);
17398c2ecf20Sopenharmony_ci}
17408c2ecf20Sopenharmony_ci
17418c2ecf20Sopenharmony_cistatic int cxd2841er_read_snr_c(struct cxd2841er_priv *priv, u32 *snr)
17428c2ecf20Sopenharmony_ci{
17438c2ecf20Sopenharmony_ci	u32 reg;
17448c2ecf20Sopenharmony_ci	u8 data[2];
17458c2ecf20Sopenharmony_ci	enum sony_dvbc_constellation_t qam = SONY_DVBC_CONSTELLATION_16QAM;
17468c2ecf20Sopenharmony_ci
17478c2ecf20Sopenharmony_ci	*snr = 0;
17488c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
17498c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
17508c2ecf20Sopenharmony_ci				"%s(): invalid state %d\n",
17518c2ecf20Sopenharmony_ci				__func__, priv->state);
17528c2ecf20Sopenharmony_ci		return -EINVAL;
17538c2ecf20Sopenharmony_ci	}
17548c2ecf20Sopenharmony_ci
17558c2ecf20Sopenharmony_ci	cxd2841er_freeze_regs(priv);
17568c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
17578c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x19, data, 1);
17588c2ecf20Sopenharmony_ci	qam = (enum sony_dvbc_constellation_t) (data[0] & 0x07);
17598c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x4C, data, 2);
17608c2ecf20Sopenharmony_ci	cxd2841er_unfreeze_regs(priv);
17618c2ecf20Sopenharmony_ci
17628c2ecf20Sopenharmony_ci	reg = ((u32)(data[0]&0x1f) << 8) | (u32)data[1];
17638c2ecf20Sopenharmony_ci	if (reg == 0) {
17648c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
17658c2ecf20Sopenharmony_ci				"%s(): reg value out of range\n", __func__);
17668c2ecf20Sopenharmony_ci		return 0;
17678c2ecf20Sopenharmony_ci	}
17688c2ecf20Sopenharmony_ci
17698c2ecf20Sopenharmony_ci	switch (qam) {
17708c2ecf20Sopenharmony_ci	case SONY_DVBC_CONSTELLATION_16QAM:
17718c2ecf20Sopenharmony_ci	case SONY_DVBC_CONSTELLATION_64QAM:
17728c2ecf20Sopenharmony_ci	case SONY_DVBC_CONSTELLATION_256QAM:
17738c2ecf20Sopenharmony_ci		/* SNR(dB) = -9.50 * ln(IREG_SNR_ESTIMATE / (24320)) */
17748c2ecf20Sopenharmony_ci		if (reg < 126)
17758c2ecf20Sopenharmony_ci			reg = 126;
17768c2ecf20Sopenharmony_ci		*snr = -95 * (int32_t)sony_log(reg) + 95941;
17778c2ecf20Sopenharmony_ci		break;
17788c2ecf20Sopenharmony_ci	case SONY_DVBC_CONSTELLATION_32QAM:
17798c2ecf20Sopenharmony_ci	case SONY_DVBC_CONSTELLATION_128QAM:
17808c2ecf20Sopenharmony_ci		/* SNR(dB) = -8.75 * ln(IREG_SNR_ESTIMATE / (20800)) */
17818c2ecf20Sopenharmony_ci		if (reg < 69)
17828c2ecf20Sopenharmony_ci			reg = 69;
17838c2ecf20Sopenharmony_ci		*snr = -88 * (int32_t)sony_log(reg) + 86999;
17848c2ecf20Sopenharmony_ci		break;
17858c2ecf20Sopenharmony_ci	default:
17868c2ecf20Sopenharmony_ci		return -EINVAL;
17878c2ecf20Sopenharmony_ci	}
17888c2ecf20Sopenharmony_ci
17898c2ecf20Sopenharmony_ci	return 0;
17908c2ecf20Sopenharmony_ci}
17918c2ecf20Sopenharmony_ci
17928c2ecf20Sopenharmony_cistatic int cxd2841er_read_snr_t(struct cxd2841er_priv *priv, u32 *snr)
17938c2ecf20Sopenharmony_ci{
17948c2ecf20Sopenharmony_ci	u32 reg;
17958c2ecf20Sopenharmony_ci	u8 data[2];
17968c2ecf20Sopenharmony_ci
17978c2ecf20Sopenharmony_ci	*snr = 0;
17988c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
17998c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
18008c2ecf20Sopenharmony_ci			"%s(): invalid state %d\n", __func__, priv->state);
18018c2ecf20Sopenharmony_ci		return -EINVAL;
18028c2ecf20Sopenharmony_ci	}
18038c2ecf20Sopenharmony_ci
18048c2ecf20Sopenharmony_ci	cxd2841er_freeze_regs(priv);
18058c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
18068c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
18078c2ecf20Sopenharmony_ci	cxd2841er_unfreeze_regs(priv);
18088c2ecf20Sopenharmony_ci
18098c2ecf20Sopenharmony_ci	reg = ((u32)data[0] << 8) | (u32)data[1];
18108c2ecf20Sopenharmony_ci	if (reg == 0) {
18118c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
18128c2ecf20Sopenharmony_ci			"%s(): reg value out of range\n", __func__);
18138c2ecf20Sopenharmony_ci		return 0;
18148c2ecf20Sopenharmony_ci	}
18158c2ecf20Sopenharmony_ci	if (reg > 4996)
18168c2ecf20Sopenharmony_ci		reg = 4996;
18178c2ecf20Sopenharmony_ci	*snr = 100 * ((INTLOG10X100(reg) - INTLOG10X100(5350 - reg)) + 285);
18188c2ecf20Sopenharmony_ci	return 0;
18198c2ecf20Sopenharmony_ci}
18208c2ecf20Sopenharmony_ci
18218c2ecf20Sopenharmony_cistatic int cxd2841er_read_snr_t2(struct cxd2841er_priv *priv, u32 *snr)
18228c2ecf20Sopenharmony_ci{
18238c2ecf20Sopenharmony_ci	u32 reg;
18248c2ecf20Sopenharmony_ci	u8 data[2];
18258c2ecf20Sopenharmony_ci
18268c2ecf20Sopenharmony_ci	*snr = 0;
18278c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
18288c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
18298c2ecf20Sopenharmony_ci			"%s(): invalid state %d\n", __func__, priv->state);
18308c2ecf20Sopenharmony_ci		return -EINVAL;
18318c2ecf20Sopenharmony_ci	}
18328c2ecf20Sopenharmony_ci
18338c2ecf20Sopenharmony_ci	cxd2841er_freeze_regs(priv);
18348c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
18358c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
18368c2ecf20Sopenharmony_ci	cxd2841er_unfreeze_regs(priv);
18378c2ecf20Sopenharmony_ci
18388c2ecf20Sopenharmony_ci	reg = ((u32)data[0] << 8) | (u32)data[1];
18398c2ecf20Sopenharmony_ci	if (reg == 0) {
18408c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
18418c2ecf20Sopenharmony_ci			"%s(): reg value out of range\n", __func__);
18428c2ecf20Sopenharmony_ci		return 0;
18438c2ecf20Sopenharmony_ci	}
18448c2ecf20Sopenharmony_ci	if (reg > 10876)
18458c2ecf20Sopenharmony_ci		reg = 10876;
18468c2ecf20Sopenharmony_ci	*snr = 100 * ((INTLOG10X100(reg) - INTLOG10X100(12600 - reg)) + 320);
18478c2ecf20Sopenharmony_ci	return 0;
18488c2ecf20Sopenharmony_ci}
18498c2ecf20Sopenharmony_ci
18508c2ecf20Sopenharmony_cistatic int cxd2841er_read_snr_i(struct cxd2841er_priv *priv, u32 *snr)
18518c2ecf20Sopenharmony_ci{
18528c2ecf20Sopenharmony_ci	u32 reg;
18538c2ecf20Sopenharmony_ci	u8 data[2];
18548c2ecf20Sopenharmony_ci
18558c2ecf20Sopenharmony_ci	*snr = 0;
18568c2ecf20Sopenharmony_ci	if (priv->state != STATE_ACTIVE_TC) {
18578c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
18588c2ecf20Sopenharmony_ci				"%s(): invalid state %d\n", __func__,
18598c2ecf20Sopenharmony_ci				priv->state);
18608c2ecf20Sopenharmony_ci		return -EINVAL;
18618c2ecf20Sopenharmony_ci	}
18628c2ecf20Sopenharmony_ci
18638c2ecf20Sopenharmony_ci	cxd2841er_freeze_regs(priv);
18648c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
18658c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
18668c2ecf20Sopenharmony_ci	cxd2841er_unfreeze_regs(priv);
18678c2ecf20Sopenharmony_ci
18688c2ecf20Sopenharmony_ci	reg = ((u32)data[0] << 8) | (u32)data[1];
18698c2ecf20Sopenharmony_ci	if (reg == 0) {
18708c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
18718c2ecf20Sopenharmony_ci				"%s(): reg value out of range\n", __func__);
18728c2ecf20Sopenharmony_ci		return 0;
18738c2ecf20Sopenharmony_ci	}
18748c2ecf20Sopenharmony_ci	*snr = 10000 * (intlog10(reg) >> 24) - 9031;
18758c2ecf20Sopenharmony_ci	return 0;
18768c2ecf20Sopenharmony_ci}
18778c2ecf20Sopenharmony_ci
18788c2ecf20Sopenharmony_cistatic u16 cxd2841er_read_agc_gain_c(struct cxd2841er_priv *priv,
18798c2ecf20Sopenharmony_ci					u8 delsys)
18808c2ecf20Sopenharmony_ci{
18818c2ecf20Sopenharmony_ci	u8 data[2];
18828c2ecf20Sopenharmony_ci
18838c2ecf20Sopenharmony_ci	cxd2841er_write_reg(
18848c2ecf20Sopenharmony_ci		priv, I2C_SLVT, 0x00, 0x40);
18858c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x49, data, 2);
18868c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev,
18878c2ecf20Sopenharmony_ci			"%s(): AGC value=%u\n",
18888c2ecf20Sopenharmony_ci			__func__, (((u16)data[0] & 0x0F) << 8) |
18898c2ecf20Sopenharmony_ci			(u16)(data[1] & 0xFF));
18908c2ecf20Sopenharmony_ci	return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
18918c2ecf20Sopenharmony_ci}
18928c2ecf20Sopenharmony_ci
18938c2ecf20Sopenharmony_cistatic u16 cxd2841er_read_agc_gain_t_t2(struct cxd2841er_priv *priv,
18948c2ecf20Sopenharmony_ci					u8 delsys)
18958c2ecf20Sopenharmony_ci{
18968c2ecf20Sopenharmony_ci	u8 data[2];
18978c2ecf20Sopenharmony_ci
18988c2ecf20Sopenharmony_ci	cxd2841er_write_reg(
18998c2ecf20Sopenharmony_ci		priv, I2C_SLVT, 0x00, (delsys == SYS_DVBT ? 0x10 : 0x20));
19008c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
19018c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev,
19028c2ecf20Sopenharmony_ci			"%s(): AGC value=%u\n",
19038c2ecf20Sopenharmony_ci			__func__, (((u16)data[0] & 0x0F) << 8) |
19048c2ecf20Sopenharmony_ci			(u16)(data[1] & 0xFF));
19058c2ecf20Sopenharmony_ci	return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
19068c2ecf20Sopenharmony_ci}
19078c2ecf20Sopenharmony_ci
19088c2ecf20Sopenharmony_cistatic u16 cxd2841er_read_agc_gain_i(struct cxd2841er_priv *priv,
19098c2ecf20Sopenharmony_ci		u8 delsys)
19108c2ecf20Sopenharmony_ci{
19118c2ecf20Sopenharmony_ci	u8 data[2];
19128c2ecf20Sopenharmony_ci
19138c2ecf20Sopenharmony_ci	cxd2841er_write_reg(
19148c2ecf20Sopenharmony_ci			priv, I2C_SLVT, 0x00, 0x60);
19158c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
19168c2ecf20Sopenharmony_ci
19178c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev,
19188c2ecf20Sopenharmony_ci			"%s(): AGC value=%u\n",
19198c2ecf20Sopenharmony_ci			__func__, (((u16)data[0] & 0x0F) << 8) |
19208c2ecf20Sopenharmony_ci			(u16)(data[1] & 0xFF));
19218c2ecf20Sopenharmony_ci	return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
19228c2ecf20Sopenharmony_ci}
19238c2ecf20Sopenharmony_ci
19248c2ecf20Sopenharmony_cistatic u16 cxd2841er_read_agc_gain_s(struct cxd2841er_priv *priv)
19258c2ecf20Sopenharmony_ci{
19268c2ecf20Sopenharmony_ci	u8 data[2];
19278c2ecf20Sopenharmony_ci
19288c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0xA0 */
19298c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
19308c2ecf20Sopenharmony_ci	/*
19318c2ecf20Sopenharmony_ci	 *  slave     Bank      Addr      Bit       Signal name
19328c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       1Fh       [4:0]     IRFAGC_GAIN[12:8]
19338c2ecf20Sopenharmony_ci	 * <SLV-T>    A0h       20h       [7:0]     IRFAGC_GAIN[7:0]
19348c2ecf20Sopenharmony_ci	 */
19358c2ecf20Sopenharmony_ci	cxd2841er_read_regs(priv, I2C_SLVT, 0x1f, data, 2);
19368c2ecf20Sopenharmony_ci	return ((((u16)data[0] & 0x1F) << 8) | (u16)(data[1] & 0xFF)) << 3;
19378c2ecf20Sopenharmony_ci}
19388c2ecf20Sopenharmony_ci
19398c2ecf20Sopenharmony_cistatic void cxd2841er_read_ber(struct dvb_frontend *fe)
19408c2ecf20Sopenharmony_ci{
19418c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
19428c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
19438c2ecf20Sopenharmony_ci	u32 ret, bit_error = 0, bit_count = 0;
19448c2ecf20Sopenharmony_ci
19458c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
19468c2ecf20Sopenharmony_ci	switch (p->delivery_system) {
19478c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_A:
19488c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_B:
19498c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_C:
19508c2ecf20Sopenharmony_ci		ret = cxd2841er_read_ber_c(priv, &bit_error, &bit_count);
19518c2ecf20Sopenharmony_ci		break;
19528c2ecf20Sopenharmony_ci	case SYS_ISDBT:
19538c2ecf20Sopenharmony_ci		ret = cxd2841er_read_ber_i(priv, &bit_error, &bit_count);
19548c2ecf20Sopenharmony_ci		break;
19558c2ecf20Sopenharmony_ci	case SYS_DVBS:
19568c2ecf20Sopenharmony_ci		ret = cxd2841er_mon_read_ber_s(priv, &bit_error, &bit_count);
19578c2ecf20Sopenharmony_ci		break;
19588c2ecf20Sopenharmony_ci	case SYS_DVBS2:
19598c2ecf20Sopenharmony_ci		ret = cxd2841er_mon_read_ber_s2(priv, &bit_error, &bit_count);
19608c2ecf20Sopenharmony_ci		break;
19618c2ecf20Sopenharmony_ci	case SYS_DVBT:
19628c2ecf20Sopenharmony_ci		ret = cxd2841er_read_ber_t(priv, &bit_error, &bit_count);
19638c2ecf20Sopenharmony_ci		break;
19648c2ecf20Sopenharmony_ci	case SYS_DVBT2:
19658c2ecf20Sopenharmony_ci		ret = cxd2841er_read_ber_t2(priv, &bit_error, &bit_count);
19668c2ecf20Sopenharmony_ci		break;
19678c2ecf20Sopenharmony_ci	default:
19688c2ecf20Sopenharmony_ci		p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
19698c2ecf20Sopenharmony_ci		p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
19708c2ecf20Sopenharmony_ci		return;
19718c2ecf20Sopenharmony_ci	}
19728c2ecf20Sopenharmony_ci
19738c2ecf20Sopenharmony_ci	if (!ret) {
19748c2ecf20Sopenharmony_ci		p->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
19758c2ecf20Sopenharmony_ci		p->post_bit_error.stat[0].uvalue += bit_error;
19768c2ecf20Sopenharmony_ci		p->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
19778c2ecf20Sopenharmony_ci		p->post_bit_count.stat[0].uvalue += bit_count;
19788c2ecf20Sopenharmony_ci	} else {
19798c2ecf20Sopenharmony_ci		p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
19808c2ecf20Sopenharmony_ci		p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
19818c2ecf20Sopenharmony_ci	}
19828c2ecf20Sopenharmony_ci}
19838c2ecf20Sopenharmony_ci
19848c2ecf20Sopenharmony_cistatic void cxd2841er_read_signal_strength(struct dvb_frontend *fe)
19858c2ecf20Sopenharmony_ci{
19868c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
19878c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
19888c2ecf20Sopenharmony_ci	s32 strength;
19898c2ecf20Sopenharmony_ci
19908c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
19918c2ecf20Sopenharmony_ci	switch (p->delivery_system) {
19928c2ecf20Sopenharmony_ci	case SYS_DVBT:
19938c2ecf20Sopenharmony_ci	case SYS_DVBT2:
19948c2ecf20Sopenharmony_ci		strength = cxd2841er_read_agc_gain_t_t2(priv,
19958c2ecf20Sopenharmony_ci							p->delivery_system);
19968c2ecf20Sopenharmony_ci		p->strength.stat[0].scale = FE_SCALE_DECIBEL;
19978c2ecf20Sopenharmony_ci		/* Formula was empirically determinated @ 410 MHz */
19988c2ecf20Sopenharmony_ci		p->strength.stat[0].uvalue = strength * 366 / 100 - 89520;
19998c2ecf20Sopenharmony_ci		break;	/* Code moved out of the function */
20008c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_A:
20018c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_B:
20028c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_C:
20038c2ecf20Sopenharmony_ci		strength = cxd2841er_read_agc_gain_c(priv,
20048c2ecf20Sopenharmony_ci							p->delivery_system);
20058c2ecf20Sopenharmony_ci		p->strength.stat[0].scale = FE_SCALE_DECIBEL;
20068c2ecf20Sopenharmony_ci		/*
20078c2ecf20Sopenharmony_ci		 * Formula was empirically determinated via linear regression,
20088c2ecf20Sopenharmony_ci		 * using frequencies: 175 MHz, 410 MHz and 800 MHz, and a
20098c2ecf20Sopenharmony_ci		 * stream modulated with QAM64
20108c2ecf20Sopenharmony_ci		 */
20118c2ecf20Sopenharmony_ci		p->strength.stat[0].uvalue = strength * 4045 / 1000 - 85224;
20128c2ecf20Sopenharmony_ci		break;
20138c2ecf20Sopenharmony_ci	case SYS_ISDBT:
20148c2ecf20Sopenharmony_ci		strength = cxd2841er_read_agc_gain_i(priv, p->delivery_system);
20158c2ecf20Sopenharmony_ci		p->strength.stat[0].scale = FE_SCALE_DECIBEL;
20168c2ecf20Sopenharmony_ci		/*
20178c2ecf20Sopenharmony_ci		 * Formula was empirically determinated via linear regression,
20188c2ecf20Sopenharmony_ci		 * using frequencies: 175 MHz, 410 MHz and 800 MHz.
20198c2ecf20Sopenharmony_ci		 */
20208c2ecf20Sopenharmony_ci		p->strength.stat[0].uvalue = strength * 3775 / 1000 - 90185;
20218c2ecf20Sopenharmony_ci		break;
20228c2ecf20Sopenharmony_ci	case SYS_DVBS:
20238c2ecf20Sopenharmony_ci	case SYS_DVBS2:
20248c2ecf20Sopenharmony_ci		strength = 65535 - cxd2841er_read_agc_gain_s(priv);
20258c2ecf20Sopenharmony_ci		p->strength.stat[0].scale = FE_SCALE_RELATIVE;
20268c2ecf20Sopenharmony_ci		p->strength.stat[0].uvalue = strength;
20278c2ecf20Sopenharmony_ci		break;
20288c2ecf20Sopenharmony_ci	default:
20298c2ecf20Sopenharmony_ci		p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
20308c2ecf20Sopenharmony_ci		break;
20318c2ecf20Sopenharmony_ci	}
20328c2ecf20Sopenharmony_ci}
20338c2ecf20Sopenharmony_ci
20348c2ecf20Sopenharmony_cistatic void cxd2841er_read_snr(struct dvb_frontend *fe)
20358c2ecf20Sopenharmony_ci{
20368c2ecf20Sopenharmony_ci	u32 tmp = 0;
20378c2ecf20Sopenharmony_ci	int ret = 0;
20388c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
20398c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
20408c2ecf20Sopenharmony_ci
20418c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
20428c2ecf20Sopenharmony_ci	switch (p->delivery_system) {
20438c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_A:
20448c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_B:
20458c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_C:
20468c2ecf20Sopenharmony_ci		ret = cxd2841er_read_snr_c(priv, &tmp);
20478c2ecf20Sopenharmony_ci		break;
20488c2ecf20Sopenharmony_ci	case SYS_DVBT:
20498c2ecf20Sopenharmony_ci		ret = cxd2841er_read_snr_t(priv, &tmp);
20508c2ecf20Sopenharmony_ci		break;
20518c2ecf20Sopenharmony_ci	case SYS_DVBT2:
20528c2ecf20Sopenharmony_ci		ret = cxd2841er_read_snr_t2(priv, &tmp);
20538c2ecf20Sopenharmony_ci		break;
20548c2ecf20Sopenharmony_ci	case SYS_ISDBT:
20558c2ecf20Sopenharmony_ci		ret = cxd2841er_read_snr_i(priv, &tmp);
20568c2ecf20Sopenharmony_ci		break;
20578c2ecf20Sopenharmony_ci	case SYS_DVBS:
20588c2ecf20Sopenharmony_ci	case SYS_DVBS2:
20598c2ecf20Sopenharmony_ci		ret = cxd2841er_dvbs_read_snr(priv, p->delivery_system, &tmp);
20608c2ecf20Sopenharmony_ci		break;
20618c2ecf20Sopenharmony_ci	default:
20628c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): unknown delivery system %d\n",
20638c2ecf20Sopenharmony_ci			__func__, p->delivery_system);
20648c2ecf20Sopenharmony_ci		p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
20658c2ecf20Sopenharmony_ci		return;
20668c2ecf20Sopenharmony_ci	}
20678c2ecf20Sopenharmony_ci
20688c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): snr=%d\n",
20698c2ecf20Sopenharmony_ci			__func__, (int32_t)tmp);
20708c2ecf20Sopenharmony_ci
20718c2ecf20Sopenharmony_ci	if (!ret) {
20728c2ecf20Sopenharmony_ci		p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
20738c2ecf20Sopenharmony_ci		p->cnr.stat[0].svalue = tmp;
20748c2ecf20Sopenharmony_ci	} else {
20758c2ecf20Sopenharmony_ci		p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
20768c2ecf20Sopenharmony_ci	}
20778c2ecf20Sopenharmony_ci}
20788c2ecf20Sopenharmony_ci
20798c2ecf20Sopenharmony_cistatic void cxd2841er_read_ucblocks(struct dvb_frontend *fe)
20808c2ecf20Sopenharmony_ci{
20818c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
20828c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
20838c2ecf20Sopenharmony_ci	u32 ucblocks = 0;
20848c2ecf20Sopenharmony_ci
20858c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
20868c2ecf20Sopenharmony_ci	switch (p->delivery_system) {
20878c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_A:
20888c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_B:
20898c2ecf20Sopenharmony_ci	case SYS_DVBC_ANNEX_C:
20908c2ecf20Sopenharmony_ci		cxd2841er_read_packet_errors_c(priv, &ucblocks);
20918c2ecf20Sopenharmony_ci		break;
20928c2ecf20Sopenharmony_ci	case SYS_DVBT:
20938c2ecf20Sopenharmony_ci		cxd2841er_read_packet_errors_t(priv, &ucblocks);
20948c2ecf20Sopenharmony_ci		break;
20958c2ecf20Sopenharmony_ci	case SYS_DVBT2:
20968c2ecf20Sopenharmony_ci		cxd2841er_read_packet_errors_t2(priv, &ucblocks);
20978c2ecf20Sopenharmony_ci		break;
20988c2ecf20Sopenharmony_ci	case SYS_ISDBT:
20998c2ecf20Sopenharmony_ci		cxd2841er_read_packet_errors_i(priv, &ucblocks);
21008c2ecf20Sopenharmony_ci		break;
21018c2ecf20Sopenharmony_ci	default:
21028c2ecf20Sopenharmony_ci		p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
21038c2ecf20Sopenharmony_ci		return;
21048c2ecf20Sopenharmony_ci	}
21058c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s() ucblocks=%u\n", __func__, ucblocks);
21068c2ecf20Sopenharmony_ci
21078c2ecf20Sopenharmony_ci	p->block_error.stat[0].scale = FE_SCALE_COUNTER;
21088c2ecf20Sopenharmony_ci	p->block_error.stat[0].uvalue = ucblocks;
21098c2ecf20Sopenharmony_ci}
21108c2ecf20Sopenharmony_ci
21118c2ecf20Sopenharmony_cistatic int cxd2841er_dvbt2_set_profile(
21128c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv, enum cxd2841er_dvbt2_profile_t profile)
21138c2ecf20Sopenharmony_ci{
21148c2ecf20Sopenharmony_ci	u8 tune_mode;
21158c2ecf20Sopenharmony_ci	u8 seq_not2d_time;
21168c2ecf20Sopenharmony_ci
21178c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
21188c2ecf20Sopenharmony_ci	switch (profile) {
21198c2ecf20Sopenharmony_ci	case DVBT2_PROFILE_BASE:
21208c2ecf20Sopenharmony_ci		tune_mode = 0x01;
21218c2ecf20Sopenharmony_ci		/* Set early unlock time */
21228c2ecf20Sopenharmony_ci		seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x0E:0x0C;
21238c2ecf20Sopenharmony_ci		break;
21248c2ecf20Sopenharmony_ci	case DVBT2_PROFILE_LITE:
21258c2ecf20Sopenharmony_ci		tune_mode = 0x05;
21268c2ecf20Sopenharmony_ci		/* Set early unlock time */
21278c2ecf20Sopenharmony_ci		seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
21288c2ecf20Sopenharmony_ci		break;
21298c2ecf20Sopenharmony_ci	case DVBT2_PROFILE_ANY:
21308c2ecf20Sopenharmony_ci		tune_mode = 0x00;
21318c2ecf20Sopenharmony_ci		/* Set early unlock time */
21328c2ecf20Sopenharmony_ci		seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
21338c2ecf20Sopenharmony_ci		break;
21348c2ecf20Sopenharmony_ci	default:
21358c2ecf20Sopenharmony_ci		return -EINVAL;
21368c2ecf20Sopenharmony_ci	}
21378c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x2E */
21388c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2e);
21398c2ecf20Sopenharmony_ci	/* Set profile and tune mode */
21408c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x10, tune_mode, 0x07);
21418c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x2B */
21428c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
21438c2ecf20Sopenharmony_ci	/* Set early unlock detection time */
21448c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x9d, seq_not2d_time);
21458c2ecf20Sopenharmony_ci	return 0;
21468c2ecf20Sopenharmony_ci}
21478c2ecf20Sopenharmony_ci
21488c2ecf20Sopenharmony_cistatic int cxd2841er_dvbt2_set_plp_config(struct cxd2841er_priv *priv,
21498c2ecf20Sopenharmony_ci					  u8 is_auto, u8 plp_id)
21508c2ecf20Sopenharmony_ci{
21518c2ecf20Sopenharmony_ci	if (is_auto) {
21528c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
21538c2ecf20Sopenharmony_ci			"%s() using auto PLP selection\n", __func__);
21548c2ecf20Sopenharmony_ci	} else {
21558c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
21568c2ecf20Sopenharmony_ci			"%s() using manual PLP selection, ID %d\n",
21578c2ecf20Sopenharmony_ci			__func__, plp_id);
21588c2ecf20Sopenharmony_ci	}
21598c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x23 */
21608c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
21618c2ecf20Sopenharmony_ci	if (!is_auto) {
21628c2ecf20Sopenharmony_ci		/* Manual PLP selection mode. Set the data PLP Id. */
21638c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0xaf, plp_id);
21648c2ecf20Sopenharmony_ci	}
21658c2ecf20Sopenharmony_ci	/* Auto PLP select (Scanning mode = 0x00). Data PLP select = 0x01. */
21668c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xad, (is_auto ? 0x00 : 0x01));
21678c2ecf20Sopenharmony_ci	return 0;
21688c2ecf20Sopenharmony_ci}
21698c2ecf20Sopenharmony_ci
21708c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
21718c2ecf20Sopenharmony_ci						u32 bandwidth)
21728c2ecf20Sopenharmony_ci{
21738c2ecf20Sopenharmony_ci	u32 iffreq, ifhz;
21748c2ecf20Sopenharmony_ci	u8 data[MAX_WRITE_REGSIZE];
21758c2ecf20Sopenharmony_ci
21768c2ecf20Sopenharmony_ci	static const uint8_t nominalRate8bw[3][5] = {
21778c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate [37:0] */
21788c2ecf20Sopenharmony_ci		{0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
21798c2ecf20Sopenharmony_ci		{0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
21808c2ecf20Sopenharmony_ci		{0x11, 0xF0, 0x00, 0x00, 0x00}  /* 41MHz XTal */
21818c2ecf20Sopenharmony_ci	};
21828c2ecf20Sopenharmony_ci
21838c2ecf20Sopenharmony_ci	static const uint8_t nominalRate7bw[3][5] = {
21848c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate [37:0] */
21858c2ecf20Sopenharmony_ci		{0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
21868c2ecf20Sopenharmony_ci		{0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
21878c2ecf20Sopenharmony_ci		{0x14, 0x80, 0x00, 0x00, 0x00}  /* 41MHz XTal */
21888c2ecf20Sopenharmony_ci	};
21898c2ecf20Sopenharmony_ci
21908c2ecf20Sopenharmony_ci	static const uint8_t nominalRate6bw[3][5] = {
21918c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate [37:0] */
21928c2ecf20Sopenharmony_ci		{0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
21938c2ecf20Sopenharmony_ci		{0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
21948c2ecf20Sopenharmony_ci		{0x17, 0xEA, 0xAA, 0xAA, 0xAA}  /* 41MHz XTal */
21958c2ecf20Sopenharmony_ci	};
21968c2ecf20Sopenharmony_ci
21978c2ecf20Sopenharmony_ci	static const uint8_t nominalRate5bw[3][5] = {
21988c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate [37:0] */
21998c2ecf20Sopenharmony_ci		{0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
22008c2ecf20Sopenharmony_ci		{0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
22018c2ecf20Sopenharmony_ci		{0x1C, 0xB3, 0x33, 0x33, 0x33}  /* 41MHz XTal */
22028c2ecf20Sopenharmony_ci	};
22038c2ecf20Sopenharmony_ci
22048c2ecf20Sopenharmony_ci	static const uint8_t nominalRate17bw[3][5] = {
22058c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate [37:0] */
22068c2ecf20Sopenharmony_ci		{0x58, 0xE2, 0xAF, 0xE0, 0xBC}, /* 20.5MHz XTal */
22078c2ecf20Sopenharmony_ci		{0x68, 0x0F, 0xA2, 0x32, 0xD0}, /* 24MHz XTal */
22088c2ecf20Sopenharmony_ci		{0x58, 0xE2, 0xAF, 0xE0, 0xBC}  /* 41MHz XTal */
22098c2ecf20Sopenharmony_ci	};
22108c2ecf20Sopenharmony_ci
22118c2ecf20Sopenharmony_ci	static const uint8_t itbCoef8bw[3][14] = {
22128c2ecf20Sopenharmony_ci		{0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
22138c2ecf20Sopenharmony_ci			0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
22148c2ecf20Sopenharmony_ci		{0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1,
22158c2ecf20Sopenharmony_ci			0x29, 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal   */
22168c2ecf20Sopenharmony_ci		{0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
22178c2ecf20Sopenharmony_ci			0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8}  /* 41MHz XTal   */
22188c2ecf20Sopenharmony_ci	};
22198c2ecf20Sopenharmony_ci
22208c2ecf20Sopenharmony_ci	static const uint8_t itbCoef7bw[3][14] = {
22218c2ecf20Sopenharmony_ci		{0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
22228c2ecf20Sopenharmony_ci			0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
22238c2ecf20Sopenharmony_ci		{0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0,
22248c2ecf20Sopenharmony_ci			0x29, 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal   */
22258c2ecf20Sopenharmony_ci		{0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
22268c2ecf20Sopenharmony_ci			0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5}  /* 41MHz XTal   */
22278c2ecf20Sopenharmony_ci	};
22288c2ecf20Sopenharmony_ci
22298c2ecf20Sopenharmony_ci	static const uint8_t itbCoef6bw[3][14] = {
22308c2ecf20Sopenharmony_ci		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
22318c2ecf20Sopenharmony_ci			0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
22328c2ecf20Sopenharmony_ci		{0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
22338c2ecf20Sopenharmony_ci			0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal   */
22348c2ecf20Sopenharmony_ci		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
22358c2ecf20Sopenharmony_ci			0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}  /* 41MHz XTal   */
22368c2ecf20Sopenharmony_ci	};
22378c2ecf20Sopenharmony_ci
22388c2ecf20Sopenharmony_ci	static const uint8_t itbCoef5bw[3][14] = {
22398c2ecf20Sopenharmony_ci		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
22408c2ecf20Sopenharmony_ci			0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
22418c2ecf20Sopenharmony_ci		{0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
22428c2ecf20Sopenharmony_ci			0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal   */
22438c2ecf20Sopenharmony_ci		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
22448c2ecf20Sopenharmony_ci			0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}  /* 41MHz XTal   */
22458c2ecf20Sopenharmony_ci	};
22468c2ecf20Sopenharmony_ci
22478c2ecf20Sopenharmony_ci	static const uint8_t itbCoef17bw[3][14] = {
22488c2ecf20Sopenharmony_ci		{0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
22498c2ecf20Sopenharmony_ci			0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99}, /* 20.5MHz XTal */
22508c2ecf20Sopenharmony_ci		{0x33, 0x8E, 0x2B, 0x97, 0x2D, 0x95, 0x37, 0x8B,
22518c2ecf20Sopenharmony_ci			0x30, 0x97, 0x2D, 0x9A, 0x21, 0xA4}, /* 24MHz XTal   */
22528c2ecf20Sopenharmony_ci		{0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
22538c2ecf20Sopenharmony_ci			0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99}  /* 41MHz XTal   */
22548c2ecf20Sopenharmony_ci	};
22558c2ecf20Sopenharmony_ci
22568c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x20 */
22578c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
22588c2ecf20Sopenharmony_ci
22598c2ecf20Sopenharmony_ci	switch (bandwidth) {
22608c2ecf20Sopenharmony_ci	case 8000000:
22618c2ecf20Sopenharmony_ci		/* <Timing Recovery setting> */
22628c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
22638c2ecf20Sopenharmony_ci				0x9F, nominalRate8bw[priv->xtal], 5);
22648c2ecf20Sopenharmony_ci
22658c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x27 */
22668c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
22678c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT,
22688c2ecf20Sopenharmony_ci				0x7a, 0x00, 0x0f);
22698c2ecf20Sopenharmony_ci
22708c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x10 */
22718c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
22728c2ecf20Sopenharmony_ci
22738c2ecf20Sopenharmony_ci		/* Group delay equaliser settings for
22748c2ecf20Sopenharmony_ci		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
22758c2ecf20Sopenharmony_ci		 */
22768c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
22778c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
22788c2ecf20Sopenharmony_ci				0xA6, itbCoef8bw[priv->xtal], 14);
22798c2ecf20Sopenharmony_ci		/* <IF freq setting> */
22808c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 4800000);
22818c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
22828c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
22838c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
22848c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
22858c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
22868c2ecf20Sopenharmony_ci		/* System bandwidth setting */
22878c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
22888c2ecf20Sopenharmony_ci				priv, I2C_SLVT, 0xD7, 0x00, 0x07);
22898c2ecf20Sopenharmony_ci		break;
22908c2ecf20Sopenharmony_ci	case 7000000:
22918c2ecf20Sopenharmony_ci		/* <Timing Recovery setting> */
22928c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
22938c2ecf20Sopenharmony_ci				0x9F, nominalRate7bw[priv->xtal], 5);
22948c2ecf20Sopenharmony_ci
22958c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x27 */
22968c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
22978c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT,
22988c2ecf20Sopenharmony_ci				0x7a, 0x00, 0x0f);
22998c2ecf20Sopenharmony_ci
23008c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x10 */
23018c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
23028c2ecf20Sopenharmony_ci
23038c2ecf20Sopenharmony_ci		/* Group delay equaliser settings for
23048c2ecf20Sopenharmony_ci		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
23058c2ecf20Sopenharmony_ci		 */
23068c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
23078c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
23088c2ecf20Sopenharmony_ci				0xA6, itbCoef7bw[priv->xtal], 14);
23098c2ecf20Sopenharmony_ci		/* <IF freq setting> */
23108c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 4200000);
23118c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
23128c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
23138c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
23148c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
23158c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
23168c2ecf20Sopenharmony_ci		/* System bandwidth setting */
23178c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
23188c2ecf20Sopenharmony_ci				priv, I2C_SLVT, 0xD7, 0x02, 0x07);
23198c2ecf20Sopenharmony_ci		break;
23208c2ecf20Sopenharmony_ci	case 6000000:
23218c2ecf20Sopenharmony_ci		/* <Timing Recovery setting> */
23228c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
23238c2ecf20Sopenharmony_ci				0x9F, nominalRate6bw[priv->xtal], 5);
23248c2ecf20Sopenharmony_ci
23258c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x27 */
23268c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
23278c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT,
23288c2ecf20Sopenharmony_ci				0x7a, 0x00, 0x0f);
23298c2ecf20Sopenharmony_ci
23308c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x10 */
23318c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
23328c2ecf20Sopenharmony_ci
23338c2ecf20Sopenharmony_ci		/* Group delay equaliser settings for
23348c2ecf20Sopenharmony_ci		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
23358c2ecf20Sopenharmony_ci		 */
23368c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
23378c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
23388c2ecf20Sopenharmony_ci				0xA6, itbCoef6bw[priv->xtal], 14);
23398c2ecf20Sopenharmony_ci		/* <IF freq setting> */
23408c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 3600000);
23418c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
23428c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
23438c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
23448c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
23458c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
23468c2ecf20Sopenharmony_ci		/* System bandwidth setting */
23478c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
23488c2ecf20Sopenharmony_ci				priv, I2C_SLVT, 0xD7, 0x04, 0x07);
23498c2ecf20Sopenharmony_ci		break;
23508c2ecf20Sopenharmony_ci	case 5000000:
23518c2ecf20Sopenharmony_ci		/* <Timing Recovery setting> */
23528c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
23538c2ecf20Sopenharmony_ci				0x9F, nominalRate5bw[priv->xtal], 5);
23548c2ecf20Sopenharmony_ci
23558c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x27 */
23568c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
23578c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT,
23588c2ecf20Sopenharmony_ci				0x7a, 0x00, 0x0f);
23598c2ecf20Sopenharmony_ci
23608c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x10 */
23618c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
23628c2ecf20Sopenharmony_ci
23638c2ecf20Sopenharmony_ci		/* Group delay equaliser settings for
23648c2ecf20Sopenharmony_ci		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
23658c2ecf20Sopenharmony_ci		 */
23668c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
23678c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
23688c2ecf20Sopenharmony_ci				0xA6, itbCoef5bw[priv->xtal], 14);
23698c2ecf20Sopenharmony_ci		/* <IF freq setting> */
23708c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 3600000);
23718c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
23728c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
23738c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
23748c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
23758c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
23768c2ecf20Sopenharmony_ci		/* System bandwidth setting */
23778c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
23788c2ecf20Sopenharmony_ci				priv, I2C_SLVT, 0xD7, 0x06, 0x07);
23798c2ecf20Sopenharmony_ci		break;
23808c2ecf20Sopenharmony_ci	case 1712000:
23818c2ecf20Sopenharmony_ci		/* <Timing Recovery setting> */
23828c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
23838c2ecf20Sopenharmony_ci				0x9F, nominalRate17bw[priv->xtal], 5);
23848c2ecf20Sopenharmony_ci
23858c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x27 */
23868c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
23878c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT,
23888c2ecf20Sopenharmony_ci				0x7a, 0x03, 0x0f);
23898c2ecf20Sopenharmony_ci
23908c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x10 */
23918c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
23928c2ecf20Sopenharmony_ci
23938c2ecf20Sopenharmony_ci		/* Group delay equaliser settings for
23948c2ecf20Sopenharmony_ci		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
23958c2ecf20Sopenharmony_ci		 */
23968c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
23978c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
23988c2ecf20Sopenharmony_ci				0xA6, itbCoef17bw[priv->xtal], 14);
23998c2ecf20Sopenharmony_ci		/* <IF freq setting> */
24008c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 3500000);
24018c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
24028c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
24038c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
24048c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
24058c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
24068c2ecf20Sopenharmony_ci		/* System bandwidth setting */
24078c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
24088c2ecf20Sopenharmony_ci				priv, I2C_SLVT, 0xD7, 0x03, 0x07);
24098c2ecf20Sopenharmony_ci		break;
24108c2ecf20Sopenharmony_ci	default:
24118c2ecf20Sopenharmony_ci		return -EINVAL;
24128c2ecf20Sopenharmony_ci	}
24138c2ecf20Sopenharmony_ci	return 0;
24148c2ecf20Sopenharmony_ci}
24158c2ecf20Sopenharmony_ci
24168c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_t_band(
24178c2ecf20Sopenharmony_ci		struct cxd2841er_priv *priv, u32 bandwidth)
24188c2ecf20Sopenharmony_ci{
24198c2ecf20Sopenharmony_ci	u8 data[MAX_WRITE_REGSIZE];
24208c2ecf20Sopenharmony_ci	u32 iffreq, ifhz;
24218c2ecf20Sopenharmony_ci	static const u8 nominalRate8bw[3][5] = {
24228c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate [37:0] */
24238c2ecf20Sopenharmony_ci		{0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
24248c2ecf20Sopenharmony_ci		{0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
24258c2ecf20Sopenharmony_ci		{0x11, 0xF0, 0x00, 0x00, 0x00}  /* 41MHz XTal */
24268c2ecf20Sopenharmony_ci	};
24278c2ecf20Sopenharmony_ci	static const u8 nominalRate7bw[3][5] = {
24288c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate [37:0] */
24298c2ecf20Sopenharmony_ci		{0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
24308c2ecf20Sopenharmony_ci		{0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
24318c2ecf20Sopenharmony_ci		{0x14, 0x80, 0x00, 0x00, 0x00}  /* 41MHz XTal */
24328c2ecf20Sopenharmony_ci	};
24338c2ecf20Sopenharmony_ci	static const u8 nominalRate6bw[3][5] = {
24348c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate [37:0] */
24358c2ecf20Sopenharmony_ci		{0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
24368c2ecf20Sopenharmony_ci		{0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
24378c2ecf20Sopenharmony_ci		{0x17, 0xEA, 0xAA, 0xAA, 0xAA}  /* 41MHz XTal */
24388c2ecf20Sopenharmony_ci	};
24398c2ecf20Sopenharmony_ci	static const u8 nominalRate5bw[3][5] = {
24408c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate [37:0] */
24418c2ecf20Sopenharmony_ci		{0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
24428c2ecf20Sopenharmony_ci		{0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
24438c2ecf20Sopenharmony_ci		{0x1C, 0xB3, 0x33, 0x33, 0x33}  /* 41MHz XTal */
24448c2ecf20Sopenharmony_ci	};
24458c2ecf20Sopenharmony_ci
24468c2ecf20Sopenharmony_ci	static const u8 itbCoef8bw[3][14] = {
24478c2ecf20Sopenharmony_ci		{0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
24488c2ecf20Sopenharmony_ci			0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
24498c2ecf20Sopenharmony_ci		{0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29, 0xA5,
24508c2ecf20Sopenharmony_ci			0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal   */
24518c2ecf20Sopenharmony_ci		{0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
24528c2ecf20Sopenharmony_ci			0x1F, 0xA8, 0x2C, 0xC8}  /* 41MHz XTal   */
24538c2ecf20Sopenharmony_ci	};
24548c2ecf20Sopenharmony_ci	static const u8 itbCoef7bw[3][14] = {
24558c2ecf20Sopenharmony_ci		{0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
24568c2ecf20Sopenharmony_ci			0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
24578c2ecf20Sopenharmony_ci		{0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29, 0xA2,
24588c2ecf20Sopenharmony_ci			0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal   */
24598c2ecf20Sopenharmony_ci		{0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
24608c2ecf20Sopenharmony_ci			0x26, 0xA9, 0x21, 0xA5}  /* 41MHz XTal   */
24618c2ecf20Sopenharmony_ci	};
24628c2ecf20Sopenharmony_ci	static const u8 itbCoef6bw[3][14] = {
24638c2ecf20Sopenharmony_ci		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
24648c2ecf20Sopenharmony_ci			0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
24658c2ecf20Sopenharmony_ci		{0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
24668c2ecf20Sopenharmony_ci			0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal   */
24678c2ecf20Sopenharmony_ci		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
24688c2ecf20Sopenharmony_ci			0x00, 0xE6, 0x23, 0xA4}  /* 41MHz XTal   */
24698c2ecf20Sopenharmony_ci	};
24708c2ecf20Sopenharmony_ci	static const u8 itbCoef5bw[3][14] = {
24718c2ecf20Sopenharmony_ci		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
24728c2ecf20Sopenharmony_ci			0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
24738c2ecf20Sopenharmony_ci		{0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
24748c2ecf20Sopenharmony_ci			0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal   */
24758c2ecf20Sopenharmony_ci		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
24768c2ecf20Sopenharmony_ci			0x00, 0xE6, 0x23, 0xA4}  /* 41MHz XTal   */
24778c2ecf20Sopenharmony_ci	};
24788c2ecf20Sopenharmony_ci
24798c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x13 */
24808c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
24818c2ecf20Sopenharmony_ci	/* Echo performance optimization setting */
24828c2ecf20Sopenharmony_ci	data[0] = 0x01;
24838c2ecf20Sopenharmony_ci	data[1] = 0x14;
24848c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0x9C, data, 2);
24858c2ecf20Sopenharmony_ci
24868c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x10 */
24878c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
24888c2ecf20Sopenharmony_ci
24898c2ecf20Sopenharmony_ci	switch (bandwidth) {
24908c2ecf20Sopenharmony_ci	case 8000000:
24918c2ecf20Sopenharmony_ci		/* <Timing Recovery setting> */
24928c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
24938c2ecf20Sopenharmony_ci				0x9F, nominalRate8bw[priv->xtal], 5);
24948c2ecf20Sopenharmony_ci		/* Group delay equaliser settings for
24958c2ecf20Sopenharmony_ci		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
24968c2ecf20Sopenharmony_ci		*/
24978c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
24988c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
24998c2ecf20Sopenharmony_ci				0xA6, itbCoef8bw[priv->xtal], 14);
25008c2ecf20Sopenharmony_ci		/* <IF freq setting> */
25018c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 4800000);
25028c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
25038c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
25048c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
25058c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
25068c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
25078c2ecf20Sopenharmony_ci		/* System bandwidth setting */
25088c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
25098c2ecf20Sopenharmony_ci			priv, I2C_SLVT, 0xD7, 0x00, 0x07);
25108c2ecf20Sopenharmony_ci
25118c2ecf20Sopenharmony_ci		/* Demod core latency setting */
25128c2ecf20Sopenharmony_ci		if (priv->xtal == SONY_XTAL_24000) {
25138c2ecf20Sopenharmony_ci			data[0] = 0x15;
25148c2ecf20Sopenharmony_ci			data[1] = 0x28;
25158c2ecf20Sopenharmony_ci		} else {
25168c2ecf20Sopenharmony_ci			data[0] = 0x01;
25178c2ecf20Sopenharmony_ci			data[1] = 0xE0;
25188c2ecf20Sopenharmony_ci		}
25198c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
25208c2ecf20Sopenharmony_ci
25218c2ecf20Sopenharmony_ci		/* Notch filter setting */
25228c2ecf20Sopenharmony_ci		data[0] = 0x01;
25238c2ecf20Sopenharmony_ci		data[1] = 0x02;
25248c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
25258c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
25268c2ecf20Sopenharmony_ci		break;
25278c2ecf20Sopenharmony_ci	case 7000000:
25288c2ecf20Sopenharmony_ci		/* <Timing Recovery setting> */
25298c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
25308c2ecf20Sopenharmony_ci				0x9F, nominalRate7bw[priv->xtal], 5);
25318c2ecf20Sopenharmony_ci		/* Group delay equaliser settings for
25328c2ecf20Sopenharmony_ci		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
25338c2ecf20Sopenharmony_ci		*/
25348c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
25358c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
25368c2ecf20Sopenharmony_ci				0xA6, itbCoef7bw[priv->xtal], 14);
25378c2ecf20Sopenharmony_ci		/* <IF freq setting> */
25388c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 4200000);
25398c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
25408c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
25418c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
25428c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
25438c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
25448c2ecf20Sopenharmony_ci		/* System bandwidth setting */
25458c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
25468c2ecf20Sopenharmony_ci			priv, I2C_SLVT, 0xD7, 0x02, 0x07);
25478c2ecf20Sopenharmony_ci
25488c2ecf20Sopenharmony_ci		/* Demod core latency setting */
25498c2ecf20Sopenharmony_ci		if (priv->xtal == SONY_XTAL_24000) {
25508c2ecf20Sopenharmony_ci			data[0] = 0x1F;
25518c2ecf20Sopenharmony_ci			data[1] = 0xF8;
25528c2ecf20Sopenharmony_ci		} else {
25538c2ecf20Sopenharmony_ci			data[0] = 0x12;
25548c2ecf20Sopenharmony_ci			data[1] = 0xF8;
25558c2ecf20Sopenharmony_ci		}
25568c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
25578c2ecf20Sopenharmony_ci
25588c2ecf20Sopenharmony_ci		/* Notch filter setting */
25598c2ecf20Sopenharmony_ci		data[0] = 0x00;
25608c2ecf20Sopenharmony_ci		data[1] = 0x03;
25618c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
25628c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
25638c2ecf20Sopenharmony_ci		break;
25648c2ecf20Sopenharmony_ci	case 6000000:
25658c2ecf20Sopenharmony_ci		/* <Timing Recovery setting> */
25668c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
25678c2ecf20Sopenharmony_ci				0x9F, nominalRate6bw[priv->xtal], 5);
25688c2ecf20Sopenharmony_ci		/* Group delay equaliser settings for
25698c2ecf20Sopenharmony_ci		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
25708c2ecf20Sopenharmony_ci		*/
25718c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
25728c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
25738c2ecf20Sopenharmony_ci				0xA6, itbCoef6bw[priv->xtal], 14);
25748c2ecf20Sopenharmony_ci		/* <IF freq setting> */
25758c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 3600000);
25768c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
25778c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
25788c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
25798c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
25808c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
25818c2ecf20Sopenharmony_ci		/* System bandwidth setting */
25828c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
25838c2ecf20Sopenharmony_ci			priv, I2C_SLVT, 0xD7, 0x04, 0x07);
25848c2ecf20Sopenharmony_ci
25858c2ecf20Sopenharmony_ci		/* Demod core latency setting */
25868c2ecf20Sopenharmony_ci		if (priv->xtal == SONY_XTAL_24000) {
25878c2ecf20Sopenharmony_ci			data[0] = 0x25;
25888c2ecf20Sopenharmony_ci			data[1] = 0x4C;
25898c2ecf20Sopenharmony_ci		} else {
25908c2ecf20Sopenharmony_ci			data[0] = 0x1F;
25918c2ecf20Sopenharmony_ci			data[1] = 0xDC;
25928c2ecf20Sopenharmony_ci		}
25938c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
25948c2ecf20Sopenharmony_ci
25958c2ecf20Sopenharmony_ci		/* Notch filter setting */
25968c2ecf20Sopenharmony_ci		data[0] = 0x00;
25978c2ecf20Sopenharmony_ci		data[1] = 0x03;
25988c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
25998c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
26008c2ecf20Sopenharmony_ci		break;
26018c2ecf20Sopenharmony_ci	case 5000000:
26028c2ecf20Sopenharmony_ci		/* <Timing Recovery setting> */
26038c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
26048c2ecf20Sopenharmony_ci				0x9F, nominalRate5bw[priv->xtal], 5);
26058c2ecf20Sopenharmony_ci		/* Group delay equaliser settings for
26068c2ecf20Sopenharmony_ci		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
26078c2ecf20Sopenharmony_ci		*/
26088c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
26098c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
26108c2ecf20Sopenharmony_ci				0xA6, itbCoef5bw[priv->xtal], 14);
26118c2ecf20Sopenharmony_ci		/* <IF freq setting> */
26128c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 3600000);
26138c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
26148c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
26158c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
26168c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
26178c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
26188c2ecf20Sopenharmony_ci		/* System bandwidth setting */
26198c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
26208c2ecf20Sopenharmony_ci			priv, I2C_SLVT, 0xD7, 0x06, 0x07);
26218c2ecf20Sopenharmony_ci
26228c2ecf20Sopenharmony_ci		/* Demod core latency setting */
26238c2ecf20Sopenharmony_ci		if (priv->xtal == SONY_XTAL_24000) {
26248c2ecf20Sopenharmony_ci			data[0] = 0x2C;
26258c2ecf20Sopenharmony_ci			data[1] = 0xC2;
26268c2ecf20Sopenharmony_ci		} else {
26278c2ecf20Sopenharmony_ci			data[0] = 0x26;
26288c2ecf20Sopenharmony_ci			data[1] = 0x3C;
26298c2ecf20Sopenharmony_ci		}
26308c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
26318c2ecf20Sopenharmony_ci
26328c2ecf20Sopenharmony_ci		/* Notch filter setting */
26338c2ecf20Sopenharmony_ci		data[0] = 0x00;
26348c2ecf20Sopenharmony_ci		data[1] = 0x03;
26358c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
26368c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
26378c2ecf20Sopenharmony_ci		break;
26388c2ecf20Sopenharmony_ci	}
26398c2ecf20Sopenharmony_ci
26408c2ecf20Sopenharmony_ci	return 0;
26418c2ecf20Sopenharmony_ci}
26428c2ecf20Sopenharmony_ci
26438c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_i_band(
26448c2ecf20Sopenharmony_ci		struct cxd2841er_priv *priv, u32 bandwidth)
26458c2ecf20Sopenharmony_ci{
26468c2ecf20Sopenharmony_ci	u32 iffreq, ifhz;
26478c2ecf20Sopenharmony_ci	u8 data[3];
26488c2ecf20Sopenharmony_ci
26498c2ecf20Sopenharmony_ci	/* TRCG Nominal Rate */
26508c2ecf20Sopenharmony_ci	static const u8 nominalRate8bw[3][5] = {
26518c2ecf20Sopenharmony_ci		{0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
26528c2ecf20Sopenharmony_ci		{0x11, 0xB8, 0x00, 0x00, 0x00}, /* 24MHz XTal */
26538c2ecf20Sopenharmony_ci		{0x00, 0x00, 0x00, 0x00, 0x00}  /* 41MHz XTal */
26548c2ecf20Sopenharmony_ci	};
26558c2ecf20Sopenharmony_ci
26568c2ecf20Sopenharmony_ci	static const u8 nominalRate7bw[3][5] = {
26578c2ecf20Sopenharmony_ci		{0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
26588c2ecf20Sopenharmony_ci		{0x14, 0x40, 0x00, 0x00, 0x00}, /* 24MHz XTal */
26598c2ecf20Sopenharmony_ci		{0x00, 0x00, 0x00, 0x00, 0x00}  /* 41MHz XTal */
26608c2ecf20Sopenharmony_ci	};
26618c2ecf20Sopenharmony_ci
26628c2ecf20Sopenharmony_ci	static const u8 nominalRate6bw[3][5] = {
26638c2ecf20Sopenharmony_ci		{0x14, 0x2E, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
26648c2ecf20Sopenharmony_ci		{0x17, 0xA0, 0x00, 0x00, 0x00}, /* 24MHz XTal */
26658c2ecf20Sopenharmony_ci		{0x14, 0x2E, 0x00, 0x00, 0x00}  /* 41MHz XTal */
26668c2ecf20Sopenharmony_ci	};
26678c2ecf20Sopenharmony_ci
26688c2ecf20Sopenharmony_ci	static const u8 itbCoef8bw[3][14] = {
26698c2ecf20Sopenharmony_ci		{0x00}, /* 20.5MHz XTal */
26708c2ecf20Sopenharmony_ci		{0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29,
26718c2ecf20Sopenharmony_ci			0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz Xtal */
26728c2ecf20Sopenharmony_ci		{0x0}, /* 41MHz XTal   */
26738c2ecf20Sopenharmony_ci	};
26748c2ecf20Sopenharmony_ci
26758c2ecf20Sopenharmony_ci	static const u8 itbCoef7bw[3][14] = {
26768c2ecf20Sopenharmony_ci		{0x00}, /* 20.5MHz XTal */
26778c2ecf20Sopenharmony_ci		{0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29,
26788c2ecf20Sopenharmony_ci			0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz Xtal */
26798c2ecf20Sopenharmony_ci		{0x00}, /* 41MHz XTal   */
26808c2ecf20Sopenharmony_ci	};
26818c2ecf20Sopenharmony_ci
26828c2ecf20Sopenharmony_ci	static const u8 itbCoef6bw[3][14] = {
26838c2ecf20Sopenharmony_ci		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
26848c2ecf20Sopenharmony_ci			0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
26858c2ecf20Sopenharmony_ci		{0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29,
26868c2ecf20Sopenharmony_ci			0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz Xtal   */
26878c2ecf20Sopenharmony_ci		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
26888c2ecf20Sopenharmony_ci			0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 41MHz XTal   */
26898c2ecf20Sopenharmony_ci	};
26908c2ecf20Sopenharmony_ci
26918c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s() bandwidth=%u\n", __func__, bandwidth);
26928c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x10 */
26938c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
26948c2ecf20Sopenharmony_ci
26958c2ecf20Sopenharmony_ci	/*  20.5/41MHz Xtal support is not available
26968c2ecf20Sopenharmony_ci	 *  on ISDB-T 7MHzBW and 8MHzBW
26978c2ecf20Sopenharmony_ci	*/
26988c2ecf20Sopenharmony_ci	if (priv->xtal != SONY_XTAL_24000 && bandwidth > 6000000) {
26998c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev,
27008c2ecf20Sopenharmony_ci			"%s(): bandwidth %d supported only for 24MHz xtal\n",
27018c2ecf20Sopenharmony_ci			__func__, bandwidth);
27028c2ecf20Sopenharmony_ci		return -EINVAL;
27038c2ecf20Sopenharmony_ci	}
27048c2ecf20Sopenharmony_ci
27058c2ecf20Sopenharmony_ci	switch (bandwidth) {
27068c2ecf20Sopenharmony_ci	case 8000000:
27078c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate */
27088c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
27098c2ecf20Sopenharmony_ci				0x9F, nominalRate8bw[priv->xtal], 5);
27108c2ecf20Sopenharmony_ci		/*  Group delay equaliser settings for ASCOT tuners optimized */
27118c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
27128c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
27138c2ecf20Sopenharmony_ci				0xA6, itbCoef8bw[priv->xtal], 14);
27148c2ecf20Sopenharmony_ci
27158c2ecf20Sopenharmony_ci		/* IF freq setting */
27168c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 4750000);
27178c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
27188c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
27198c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
27208c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
27218c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
27228c2ecf20Sopenharmony_ci
27238c2ecf20Sopenharmony_ci		/* System bandwidth setting */
27248c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x0, 0x7);
27258c2ecf20Sopenharmony_ci
27268c2ecf20Sopenharmony_ci		/* Demod core latency setting */
27278c2ecf20Sopenharmony_ci		data[0] = 0x13;
27288c2ecf20Sopenharmony_ci		data[1] = 0xFC;
27298c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
27308c2ecf20Sopenharmony_ci
27318c2ecf20Sopenharmony_ci		/* Acquisition optimization setting */
27328c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
27338c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
27348c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
27358c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x03);
27368c2ecf20Sopenharmony_ci		break;
27378c2ecf20Sopenharmony_ci	case 7000000:
27388c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate */
27398c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
27408c2ecf20Sopenharmony_ci				0x9F, nominalRate7bw[priv->xtal], 5);
27418c2ecf20Sopenharmony_ci		/*  Group delay equaliser settings for ASCOT tuners optimized */
27428c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
27438c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
27448c2ecf20Sopenharmony_ci				0xA6, itbCoef7bw[priv->xtal], 14);
27458c2ecf20Sopenharmony_ci
27468c2ecf20Sopenharmony_ci		/* IF freq setting */
27478c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 4150000);
27488c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
27498c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
27508c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
27518c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
27528c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
27538c2ecf20Sopenharmony_ci
27548c2ecf20Sopenharmony_ci		/* System bandwidth setting */
27558c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x02, 0x7);
27568c2ecf20Sopenharmony_ci
27578c2ecf20Sopenharmony_ci		/* Demod core latency setting */
27588c2ecf20Sopenharmony_ci		data[0] = 0x1A;
27598c2ecf20Sopenharmony_ci		data[1] = 0xFA;
27608c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
27618c2ecf20Sopenharmony_ci
27628c2ecf20Sopenharmony_ci		/* Acquisition optimization setting */
27638c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
27648c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
27658c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
27668c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
27678c2ecf20Sopenharmony_ci		break;
27688c2ecf20Sopenharmony_ci	case 6000000:
27698c2ecf20Sopenharmony_ci		/* TRCG Nominal Rate */
27708c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT,
27718c2ecf20Sopenharmony_ci				0x9F, nominalRate6bw[priv->xtal], 5);
27728c2ecf20Sopenharmony_ci		/*  Group delay equaliser settings for ASCOT tuners optimized */
27738c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
27748c2ecf20Sopenharmony_ci			cxd2841er_write_regs(priv, I2C_SLVT,
27758c2ecf20Sopenharmony_ci				0xA6, itbCoef6bw[priv->xtal], 14);
27768c2ecf20Sopenharmony_ci
27778c2ecf20Sopenharmony_ci		/* IF freq setting */
27788c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 3550000);
27798c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
27808c2ecf20Sopenharmony_ci		data[0] = (u8) ((iffreq >> 16) & 0xff);
27818c2ecf20Sopenharmony_ci		data[1] = (u8)((iffreq >> 8) & 0xff);
27828c2ecf20Sopenharmony_ci		data[2] = (u8)(iffreq & 0xff);
27838c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
27848c2ecf20Sopenharmony_ci
27858c2ecf20Sopenharmony_ci		/* System bandwidth setting */
27868c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x04, 0x7);
27878c2ecf20Sopenharmony_ci
27888c2ecf20Sopenharmony_ci		/* Demod core latency setting */
27898c2ecf20Sopenharmony_ci		if (priv->xtal == SONY_XTAL_24000) {
27908c2ecf20Sopenharmony_ci			data[0] = 0x1F;
27918c2ecf20Sopenharmony_ci			data[1] = 0x79;
27928c2ecf20Sopenharmony_ci		} else {
27938c2ecf20Sopenharmony_ci			data[0] = 0x1A;
27948c2ecf20Sopenharmony_ci			data[1] = 0xE2;
27958c2ecf20Sopenharmony_ci		}
27968c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
27978c2ecf20Sopenharmony_ci
27988c2ecf20Sopenharmony_ci		/* Acquisition optimization setting */
27998c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
28008c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x07, 0x07);
28018c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
28028c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
28038c2ecf20Sopenharmony_ci		break;
28048c2ecf20Sopenharmony_ci	default:
28058c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
28068c2ecf20Sopenharmony_ci				__func__, bandwidth);
28078c2ecf20Sopenharmony_ci		return -EINVAL;
28088c2ecf20Sopenharmony_ci	}
28098c2ecf20Sopenharmony_ci	return 0;
28108c2ecf20Sopenharmony_ci}
28118c2ecf20Sopenharmony_ci
28128c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
28138c2ecf20Sopenharmony_ci					       u32 bandwidth)
28148c2ecf20Sopenharmony_ci{
28158c2ecf20Sopenharmony_ci	u8 bw7_8mhz_b10_a6[] = {
28168c2ecf20Sopenharmony_ci		0x2D, 0xC7, 0x04, 0xF4, 0x07, 0xC5, 0x2A, 0xB8,
28178c2ecf20Sopenharmony_ci		0x27, 0x9E, 0x27, 0xA4, 0x29, 0xAB };
28188c2ecf20Sopenharmony_ci	u8 bw6mhz_b10_a6[] = {
28198c2ecf20Sopenharmony_ci		0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
28208c2ecf20Sopenharmony_ci		0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4 };
28218c2ecf20Sopenharmony_ci	u8 b10_b6[3];
28228c2ecf20Sopenharmony_ci	u32 iffreq, ifhz;
28238c2ecf20Sopenharmony_ci
28248c2ecf20Sopenharmony_ci	if (bandwidth != 6000000 &&
28258c2ecf20Sopenharmony_ci			bandwidth != 7000000 &&
28268c2ecf20Sopenharmony_ci			bandwidth != 8000000) {
28278c2ecf20Sopenharmony_ci		dev_info(&priv->i2c->dev, "%s(): unsupported bandwidth %d. Forcing 8Mhz!\n",
28288c2ecf20Sopenharmony_ci				__func__, bandwidth);
28298c2ecf20Sopenharmony_ci		bandwidth = 8000000;
28308c2ecf20Sopenharmony_ci	}
28318c2ecf20Sopenharmony_ci
28328c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s() bw=%d\n", __func__, bandwidth);
28338c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
28348c2ecf20Sopenharmony_ci	switch (bandwidth) {
28358c2ecf20Sopenharmony_ci	case 8000000:
28368c2ecf20Sopenharmony_ci	case 7000000:
28378c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
28388c2ecf20Sopenharmony_ci			cxd2841er_write_regs(
28398c2ecf20Sopenharmony_ci				priv, I2C_SLVT, 0xa6,
28408c2ecf20Sopenharmony_ci				bw7_8mhz_b10_a6, sizeof(bw7_8mhz_b10_a6));
28418c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 4900000);
28428c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq(ifhz);
28438c2ecf20Sopenharmony_ci		break;
28448c2ecf20Sopenharmony_ci	case 6000000:
28458c2ecf20Sopenharmony_ci		if (priv->flags & CXD2841ER_ASCOT)
28468c2ecf20Sopenharmony_ci			cxd2841er_write_regs(
28478c2ecf20Sopenharmony_ci				priv, I2C_SLVT, 0xa6,
28488c2ecf20Sopenharmony_ci				bw6mhz_b10_a6, sizeof(bw6mhz_b10_a6));
28498c2ecf20Sopenharmony_ci		ifhz = cxd2841er_get_if_hz(priv, 3700000);
28508c2ecf20Sopenharmony_ci		iffreq = cxd2841er_calc_iffreq(ifhz);
28518c2ecf20Sopenharmony_ci		break;
28528c2ecf20Sopenharmony_ci	default:
28538c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): unsupported bandwidth %d\n",
28548c2ecf20Sopenharmony_ci			__func__, bandwidth);
28558c2ecf20Sopenharmony_ci		return -EINVAL;
28568c2ecf20Sopenharmony_ci	}
28578c2ecf20Sopenharmony_ci	/* <IF freq setting> */
28588c2ecf20Sopenharmony_ci	b10_b6[0] = (u8) ((iffreq >> 16) & 0xff);
28598c2ecf20Sopenharmony_ci	b10_b6[1] = (u8)((iffreq >> 8) & 0xff);
28608c2ecf20Sopenharmony_ci	b10_b6[2] = (u8)(iffreq & 0xff);
28618c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0xb6, b10_b6, sizeof(b10_b6));
28628c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x11 */
28638c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
28648c2ecf20Sopenharmony_ci	switch (bandwidth) {
28658c2ecf20Sopenharmony_ci	case 8000000:
28668c2ecf20Sopenharmony_ci	case 7000000:
28678c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
28688c2ecf20Sopenharmony_ci			priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
28698c2ecf20Sopenharmony_ci		break;
28708c2ecf20Sopenharmony_ci	case 6000000:
28718c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
28728c2ecf20Sopenharmony_ci			priv, I2C_SLVT, 0xa3, 0x14, 0x1f);
28738c2ecf20Sopenharmony_ci		break;
28748c2ecf20Sopenharmony_ci	}
28758c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x40 */
28768c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
28778c2ecf20Sopenharmony_ci	switch (bandwidth) {
28788c2ecf20Sopenharmony_ci	case 8000000:
28798c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
28808c2ecf20Sopenharmony_ci			priv, I2C_SLVT, 0x26, 0x0b, 0x0f);
28818c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT,  0x27, 0x3e);
28828c2ecf20Sopenharmony_ci		break;
28838c2ecf20Sopenharmony_ci	case 7000000:
28848c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
28858c2ecf20Sopenharmony_ci			priv, I2C_SLVT, 0x26, 0x09, 0x0f);
28868c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT,  0x27, 0xd6);
28878c2ecf20Sopenharmony_ci		break;
28888c2ecf20Sopenharmony_ci	case 6000000:
28898c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(
28908c2ecf20Sopenharmony_ci			priv, I2C_SLVT, 0x26, 0x08, 0x0f);
28918c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT,  0x27, 0x6e);
28928c2ecf20Sopenharmony_ci		break;
28938c2ecf20Sopenharmony_ci	}
28948c2ecf20Sopenharmony_ci	return 0;
28958c2ecf20Sopenharmony_ci}
28968c2ecf20Sopenharmony_ci
28978c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_t(struct cxd2841er_priv *priv,
28988c2ecf20Sopenharmony_ci					  u32 bandwidth)
28998c2ecf20Sopenharmony_ci{
29008c2ecf20Sopenharmony_ci	u8 data[2] = { 0x09, 0x54 };
29018c2ecf20Sopenharmony_ci	u8 data24m[3] = {0xDC, 0x6C, 0x00};
29028c2ecf20Sopenharmony_ci
29038c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
29048c2ecf20Sopenharmony_ci	cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
29058c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
29068c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
29078c2ecf20Sopenharmony_ci	/* Set demod mode */
29088c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
29098c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
29108c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
29118c2ecf20Sopenharmony_ci	/* Enable demod clock */
29128c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
29138c2ecf20Sopenharmony_ci	/* Disable RF level monitor */
29148c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
29158c2ecf20Sopenharmony_ci	/* Enable ADC clock */
29168c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
29178c2ecf20Sopenharmony_ci	/* Enable ADC 1 */
29188c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
29198c2ecf20Sopenharmony_ci	/* Enable ADC 2 & 3 */
29208c2ecf20Sopenharmony_ci	if (priv->xtal == SONY_XTAL_41000) {
29218c2ecf20Sopenharmony_ci		data[0] = 0x0A;
29228c2ecf20Sopenharmony_ci		data[1] = 0xD4;
29238c2ecf20Sopenharmony_ci	}
29248c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
29258c2ecf20Sopenharmony_ci	/* Enable ADC 4 */
29268c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
29278c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x10 */
29288c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
29298c2ecf20Sopenharmony_ci	/* IFAGC gain settings */
29308c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
29318c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x11 */
29328c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
29338c2ecf20Sopenharmony_ci	/* BBAGC TARGET level setting */
29348c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
29358c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x10 */
29368c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
29378c2ecf20Sopenharmony_ci	/* ASCOT setting */
29388c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5,
29398c2ecf20Sopenharmony_ci		((priv->flags & CXD2841ER_ASCOT) ? 0x01 : 0x00), 0x01);
29408c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x18 */
29418c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
29428c2ecf20Sopenharmony_ci	/* Pre-RS BER monitor setting */
29438c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x36, 0x40, 0x07);
29448c2ecf20Sopenharmony_ci	/* FEC Auto Recovery setting */
29458c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
29468c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x01, 0x01);
29478c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
29488c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
29498c2ecf20Sopenharmony_ci	/* TSIF setting */
29508c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
29518c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
29528c2ecf20Sopenharmony_ci
29538c2ecf20Sopenharmony_ci	if (priv->xtal == SONY_XTAL_24000) {
29548c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x10 */
29558c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
29568c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0xBF, 0x60);
29578c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
29588c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data24m, 3);
29598c2ecf20Sopenharmony_ci	}
29608c2ecf20Sopenharmony_ci
29618c2ecf20Sopenharmony_ci	cxd2841er_sleep_tc_to_active_t_band(priv, bandwidth);
29628c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
29638c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
29648c2ecf20Sopenharmony_ci	/* Disable HiZ Setting 1 */
29658c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
29668c2ecf20Sopenharmony_ci	/* Disable HiZ Setting 2 */
29678c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
29688c2ecf20Sopenharmony_ci	priv->state = STATE_ACTIVE_TC;
29698c2ecf20Sopenharmony_ci	return 0;
29708c2ecf20Sopenharmony_ci}
29718c2ecf20Sopenharmony_ci
29728c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv,
29738c2ecf20Sopenharmony_ci					   u32 bandwidth)
29748c2ecf20Sopenharmony_ci{
29758c2ecf20Sopenharmony_ci	u8 data[MAX_WRITE_REGSIZE];
29768c2ecf20Sopenharmony_ci
29778c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
29788c2ecf20Sopenharmony_ci	cxd2841er_set_ts_clock_mode(priv, SYS_DVBT2);
29798c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
29808c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
29818c2ecf20Sopenharmony_ci	/* Set demod mode */
29828c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x02);
29838c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
29848c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
29858c2ecf20Sopenharmony_ci	/* Enable demod clock */
29868c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
29878c2ecf20Sopenharmony_ci	/* Disable RF level monitor */
29888c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
29898c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
29908c2ecf20Sopenharmony_ci	/* Enable ADC clock */
29918c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
29928c2ecf20Sopenharmony_ci	/* Enable ADC 1 */
29938c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
29948c2ecf20Sopenharmony_ci
29958c2ecf20Sopenharmony_ci	if (priv->xtal == SONY_XTAL_41000) {
29968c2ecf20Sopenharmony_ci		data[0] = 0x0A;
29978c2ecf20Sopenharmony_ci		data[1] = 0xD4;
29988c2ecf20Sopenharmony_ci	} else {
29998c2ecf20Sopenharmony_ci		data[0] = 0x09;
30008c2ecf20Sopenharmony_ci		data[1] = 0x54;
30018c2ecf20Sopenharmony_ci	}
30028c2ecf20Sopenharmony_ci
30038c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
30048c2ecf20Sopenharmony_ci	/* Enable ADC 4 */
30058c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
30068c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x10 */
30078c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
30088c2ecf20Sopenharmony_ci	/* IFAGC gain settings */
30098c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
30108c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x11 */
30118c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
30128c2ecf20Sopenharmony_ci	/* BBAGC TARGET level setting */
30138c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
30148c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x10 */
30158c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
30168c2ecf20Sopenharmony_ci	/* ASCOT setting */
30178c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5,
30188c2ecf20Sopenharmony_ci		((priv->flags & CXD2841ER_ASCOT) ? 0x01 : 0x00), 0x01);
30198c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x20 */
30208c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
30218c2ecf20Sopenharmony_ci	/* Acquisition optimization setting */
30228c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x8b, 0x3c);
30238c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x2b */
30248c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
30258c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x76, 0x20, 0x70);
30268c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x23 */
30278c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
30288c2ecf20Sopenharmony_ci	/* L1 Control setting */
30298c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE6, 0x00, 0x03);
30308c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
30318c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
30328c2ecf20Sopenharmony_ci	/* TSIF setting */
30338c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
30348c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
30358c2ecf20Sopenharmony_ci	/* DVB-T2 initial setting */
30368c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
30378c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x10);
30388c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x34);
30398c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
30408c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xd8);
30418c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x2a */
30428c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
30438c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x04, 0x0f);
30448c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x2b */
30458c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
30468c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x20, 0x3f);
30478c2ecf20Sopenharmony_ci
30488c2ecf20Sopenharmony_ci	/* 24MHz Xtal setting */
30498c2ecf20Sopenharmony_ci	if (priv->xtal == SONY_XTAL_24000) {
30508c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x11 */
30518c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
30528c2ecf20Sopenharmony_ci		data[0] = 0xEB;
30538c2ecf20Sopenharmony_ci		data[1] = 0x03;
30548c2ecf20Sopenharmony_ci		data[2] = 0x3B;
30558c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0x33, data, 3);
30568c2ecf20Sopenharmony_ci
30578c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x20 */
30588c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
30598c2ecf20Sopenharmony_ci		data[0] = 0x5E;
30608c2ecf20Sopenharmony_ci		data[1] = 0x5E;
30618c2ecf20Sopenharmony_ci		data[2] = 0x47;
30628c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0x95, data, 3);
30638c2ecf20Sopenharmony_ci
30648c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x99, 0x18);
30658c2ecf20Sopenharmony_ci
30668c2ecf20Sopenharmony_ci		data[0] = 0x3F;
30678c2ecf20Sopenharmony_ci		data[1] = 0xFF;
30688c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
30698c2ecf20Sopenharmony_ci
30708c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x24 */
30718c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
30728c2ecf20Sopenharmony_ci		data[0] = 0x0B;
30738c2ecf20Sopenharmony_ci		data[1] = 0x72;
30748c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0x34, data, 2);
30758c2ecf20Sopenharmony_ci
30768c2ecf20Sopenharmony_ci		data[0] = 0x93;
30778c2ecf20Sopenharmony_ci		data[1] = 0xF3;
30788c2ecf20Sopenharmony_ci		data[2] = 0x00;
30798c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xD2, data, 3);
30808c2ecf20Sopenharmony_ci
30818c2ecf20Sopenharmony_ci		data[0] = 0x05;
30828c2ecf20Sopenharmony_ci		data[1] = 0xB8;
30838c2ecf20Sopenharmony_ci		data[2] = 0xD8;
30848c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0xDD, data, 3);
30858c2ecf20Sopenharmony_ci
30868c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0xE0, 0x00);
30878c2ecf20Sopenharmony_ci
30888c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x25 */
30898c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x25);
30908c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0xED, 0x60);
30918c2ecf20Sopenharmony_ci
30928c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x27 */
30938c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
30948c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0xFA, 0x34);
30958c2ecf20Sopenharmony_ci
30968c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x2B */
30978c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2B);
30988c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x4B, 0x2F);
30998c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x9E, 0x0E);
31008c2ecf20Sopenharmony_ci
31018c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x2D */
31028c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2D);
31038c2ecf20Sopenharmony_ci		data[0] = 0x89;
31048c2ecf20Sopenharmony_ci		data[1] = 0x89;
31058c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data, 2);
31068c2ecf20Sopenharmony_ci
31078c2ecf20Sopenharmony_ci		/* Set SLV-T Bank : 0x5E */
31088c2ecf20Sopenharmony_ci		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x5E);
31098c2ecf20Sopenharmony_ci		data[0] = 0x24;
31108c2ecf20Sopenharmony_ci		data[1] = 0x95;
31118c2ecf20Sopenharmony_ci		cxd2841er_write_regs(priv, I2C_SLVT, 0x8C, data, 2);
31128c2ecf20Sopenharmony_ci	}
31138c2ecf20Sopenharmony_ci
31148c2ecf20Sopenharmony_ci	cxd2841er_sleep_tc_to_active_t2_band(priv, bandwidth);
31158c2ecf20Sopenharmony_ci
31168c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
31178c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
31188c2ecf20Sopenharmony_ci	/* Disable HiZ Setting 1 */
31198c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
31208c2ecf20Sopenharmony_ci	/* Disable HiZ Setting 2 */
31218c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
31228c2ecf20Sopenharmony_ci	priv->state = STATE_ACTIVE_TC;
31238c2ecf20Sopenharmony_ci	return 0;
31248c2ecf20Sopenharmony_ci}
31258c2ecf20Sopenharmony_ci
31268c2ecf20Sopenharmony_ci/* ISDB-Tb part */
31278c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
31288c2ecf20Sopenharmony_ci		u32 bandwidth)
31298c2ecf20Sopenharmony_ci{
31308c2ecf20Sopenharmony_ci	u8 data[2] = { 0x09, 0x54 };
31318c2ecf20Sopenharmony_ci	u8 data24m[2] = {0x60, 0x00};
31328c2ecf20Sopenharmony_ci	u8 data24m2[3] = {0xB7, 0x1B, 0x00};
31338c2ecf20Sopenharmony_ci
31348c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
31358c2ecf20Sopenharmony_ci	cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
31368c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
31378c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
31388c2ecf20Sopenharmony_ci	/* Set demod mode */
31398c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x06);
31408c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
31418c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
31428c2ecf20Sopenharmony_ci	/* Enable demod clock */
31438c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
31448c2ecf20Sopenharmony_ci	/* Enable RF level monitor */
31458c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x01);
31468c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x01);
31478c2ecf20Sopenharmony_ci	/* Enable ADC clock */
31488c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
31498c2ecf20Sopenharmony_ci	/* Enable ADC 1 */
31508c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
31518c2ecf20Sopenharmony_ci	/* xtal freq 20.5MHz or 24M */
31528c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
31538c2ecf20Sopenharmony_ci	/* Enable ADC 4 */
31548c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
31558c2ecf20Sopenharmony_ci	/* ASCOT setting */
31568c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5,
31578c2ecf20Sopenharmony_ci		((priv->flags & CXD2841ER_ASCOT) ? 0x01 : 0x00), 0x01);
31588c2ecf20Sopenharmony_ci	/* FEC Auto Recovery setting */
31598c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
31608c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x00, 0x01);
31618c2ecf20Sopenharmony_ci	/* ISDB-T initial setting */
31628c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
31638c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
31648c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x00, 0x01);
31658c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x00, 0x01);
31668c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x10 */
31678c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
31688c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x69, 0x04, 0x07);
31698c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x6B, 0x03, 0x07);
31708c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9D, 0x50, 0xFF);
31718c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xD3, 0x06, 0x1F);
31728c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xED, 0x00, 0x01);
31738c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE2, 0xCE, 0x80);
31748c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xF2, 0x13, 0x10);
31758c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x2E, 0x3F);
31768c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x15 */
31778c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
31788c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x02, 0x03);
31798c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x1E */
31808c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x1E);
31818c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x73, 0x68, 0xFF);
31828c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x63 */
31838c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x63);
31848c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x81, 0x00, 0x01);
31858c2ecf20Sopenharmony_ci
31868c2ecf20Sopenharmony_ci	/* for xtal 24MHz */
31878c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x10 */
31888c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
31898c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0xBF, data24m, 2);
31908c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x60 */
31918c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
31928c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0xA8, data24m2, 3);
31938c2ecf20Sopenharmony_ci
31948c2ecf20Sopenharmony_ci	cxd2841er_sleep_tc_to_active_i_band(priv, bandwidth);
31958c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
31968c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
31978c2ecf20Sopenharmony_ci	/* Disable HiZ Setting 1 */
31988c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
31998c2ecf20Sopenharmony_ci	/* Disable HiZ Setting 2 */
32008c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
32018c2ecf20Sopenharmony_ci	priv->state = STATE_ACTIVE_TC;
32028c2ecf20Sopenharmony_ci	return 0;
32038c2ecf20Sopenharmony_ci}
32048c2ecf20Sopenharmony_ci
32058c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc_to_active_c(struct cxd2841er_priv *priv,
32068c2ecf20Sopenharmony_ci					  u32 bandwidth)
32078c2ecf20Sopenharmony_ci{
32088c2ecf20Sopenharmony_ci	u8 data[2] = { 0x09, 0x54 };
32098c2ecf20Sopenharmony_ci
32108c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
32118c2ecf20Sopenharmony_ci	cxd2841er_set_ts_clock_mode(priv, SYS_DVBC_ANNEX_A);
32128c2ecf20Sopenharmony_ci	/* Set SLV-X Bank : 0x00 */
32138c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
32148c2ecf20Sopenharmony_ci	/* Set demod mode */
32158c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x04);
32168c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
32178c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
32188c2ecf20Sopenharmony_ci	/* Enable demod clock */
32198c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
32208c2ecf20Sopenharmony_ci	/* Disable RF level monitor */
32218c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
32228c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
32238c2ecf20Sopenharmony_ci	/* Enable ADC clock */
32248c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
32258c2ecf20Sopenharmony_ci	/* Enable ADC 1 */
32268c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
32278c2ecf20Sopenharmony_ci	/* xtal freq 20.5MHz */
32288c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
32298c2ecf20Sopenharmony_ci	/* Enable ADC 4 */
32308c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
32318c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x10 */
32328c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
32338c2ecf20Sopenharmony_ci	/* IFAGC gain settings */
32348c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x09, 0x1f);
32358c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x11 */
32368c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
32378c2ecf20Sopenharmony_ci	/* BBAGC TARGET level setting */
32388c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x48);
32398c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x10 */
32408c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
32418c2ecf20Sopenharmony_ci	/* ASCOT setting */
32428c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5,
32438c2ecf20Sopenharmony_ci		((priv->flags & CXD2841ER_ASCOT) ? 0x01 : 0x00), 0x01);
32448c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x40 */
32458c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
32468c2ecf20Sopenharmony_ci	/* Demod setting */
32478c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc3, 0x00, 0x04);
32488c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
32498c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
32508c2ecf20Sopenharmony_ci	/* TSIF setting */
32518c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
32528c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
32538c2ecf20Sopenharmony_ci
32548c2ecf20Sopenharmony_ci	cxd2841er_sleep_tc_to_active_c_band(priv, bandwidth);
32558c2ecf20Sopenharmony_ci	/* Set SLV-T Bank : 0x00 */
32568c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
32578c2ecf20Sopenharmony_ci	/* Disable HiZ Setting 1 */
32588c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
32598c2ecf20Sopenharmony_ci	/* Disable HiZ Setting 2 */
32608c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
32618c2ecf20Sopenharmony_ci	priv->state = STATE_ACTIVE_TC;
32628c2ecf20Sopenharmony_ci	return 0;
32638c2ecf20Sopenharmony_ci}
32648c2ecf20Sopenharmony_ci
32658c2ecf20Sopenharmony_cistatic int cxd2841er_get_frontend(struct dvb_frontend *fe,
32668c2ecf20Sopenharmony_ci				  struct dtv_frontend_properties *p)
32678c2ecf20Sopenharmony_ci{
32688c2ecf20Sopenharmony_ci	enum fe_status status = 0;
32698c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
32708c2ecf20Sopenharmony_ci
32718c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
32728c2ecf20Sopenharmony_ci	if (priv->state == STATE_ACTIVE_S)
32738c2ecf20Sopenharmony_ci		cxd2841er_read_status_s(fe, &status);
32748c2ecf20Sopenharmony_ci	else if (priv->state == STATE_ACTIVE_TC)
32758c2ecf20Sopenharmony_ci		cxd2841er_read_status_tc(fe, &status);
32768c2ecf20Sopenharmony_ci
32778c2ecf20Sopenharmony_ci	if (priv->state == STATE_ACTIVE_TC || priv->state == STATE_ACTIVE_S)
32788c2ecf20Sopenharmony_ci		cxd2841er_read_signal_strength(fe);
32798c2ecf20Sopenharmony_ci	else
32808c2ecf20Sopenharmony_ci		p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
32818c2ecf20Sopenharmony_ci
32828c2ecf20Sopenharmony_ci	if (status & FE_HAS_LOCK) {
32838c2ecf20Sopenharmony_ci		if (priv->stats_time &&
32848c2ecf20Sopenharmony_ci		    (!time_after(jiffies, priv->stats_time)))
32858c2ecf20Sopenharmony_ci			return 0;
32868c2ecf20Sopenharmony_ci
32878c2ecf20Sopenharmony_ci		/* Prevent retrieving stats faster than once per second */
32888c2ecf20Sopenharmony_ci		priv->stats_time = jiffies + msecs_to_jiffies(1000);
32898c2ecf20Sopenharmony_ci
32908c2ecf20Sopenharmony_ci		cxd2841er_read_snr(fe);
32918c2ecf20Sopenharmony_ci		cxd2841er_read_ucblocks(fe);
32928c2ecf20Sopenharmony_ci		cxd2841er_read_ber(fe);
32938c2ecf20Sopenharmony_ci	} else {
32948c2ecf20Sopenharmony_ci		p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
32958c2ecf20Sopenharmony_ci		p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
32968c2ecf20Sopenharmony_ci		p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
32978c2ecf20Sopenharmony_ci		p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
32988c2ecf20Sopenharmony_ci	}
32998c2ecf20Sopenharmony_ci	return 0;
33008c2ecf20Sopenharmony_ci}
33018c2ecf20Sopenharmony_ci
33028c2ecf20Sopenharmony_cistatic int cxd2841er_set_frontend_s(struct dvb_frontend *fe)
33038c2ecf20Sopenharmony_ci{
33048c2ecf20Sopenharmony_ci	int ret = 0, i, timeout, carr_offset;
33058c2ecf20Sopenharmony_ci	enum fe_status status;
33068c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
33078c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
33088c2ecf20Sopenharmony_ci	u32 symbol_rate = p->symbol_rate/1000;
33098c2ecf20Sopenharmony_ci
33108c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): %s frequency=%d symbol_rate=%d xtal=%d\n",
33118c2ecf20Sopenharmony_ci		__func__,
33128c2ecf20Sopenharmony_ci		(p->delivery_system == SYS_DVBS ? "DVB-S" : "DVB-S2"),
33138c2ecf20Sopenharmony_ci		 p->frequency, symbol_rate, priv->xtal);
33148c2ecf20Sopenharmony_ci
33158c2ecf20Sopenharmony_ci	if (priv->flags & CXD2841ER_EARLY_TUNE)
33168c2ecf20Sopenharmony_ci		cxd2841er_tuner_set(fe);
33178c2ecf20Sopenharmony_ci
33188c2ecf20Sopenharmony_ci	switch (priv->state) {
33198c2ecf20Sopenharmony_ci	case STATE_SLEEP_S:
33208c2ecf20Sopenharmony_ci		ret = cxd2841er_sleep_s_to_active_s(
33218c2ecf20Sopenharmony_ci			priv, p->delivery_system, symbol_rate);
33228c2ecf20Sopenharmony_ci		break;
33238c2ecf20Sopenharmony_ci	case STATE_ACTIVE_S:
33248c2ecf20Sopenharmony_ci		ret = cxd2841er_retune_active(priv, p);
33258c2ecf20Sopenharmony_ci		break;
33268c2ecf20Sopenharmony_ci	default:
33278c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
33288c2ecf20Sopenharmony_ci			__func__, priv->state);
33298c2ecf20Sopenharmony_ci		ret = -EINVAL;
33308c2ecf20Sopenharmony_ci		goto done;
33318c2ecf20Sopenharmony_ci	}
33328c2ecf20Sopenharmony_ci	if (ret) {
33338c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): tune failed\n", __func__);
33348c2ecf20Sopenharmony_ci		goto done;
33358c2ecf20Sopenharmony_ci	}
33368c2ecf20Sopenharmony_ci
33378c2ecf20Sopenharmony_ci	if (!(priv->flags & CXD2841ER_EARLY_TUNE))
33388c2ecf20Sopenharmony_ci		cxd2841er_tuner_set(fe);
33398c2ecf20Sopenharmony_ci
33408c2ecf20Sopenharmony_ci	cxd2841er_tune_done(priv);
33418c2ecf20Sopenharmony_ci	timeout = ((3000000 + (symbol_rate - 1)) / symbol_rate) + 150;
33428c2ecf20Sopenharmony_ci
33438c2ecf20Sopenharmony_ci	i = 0;
33448c2ecf20Sopenharmony_ci	do {
33458c2ecf20Sopenharmony_ci		usleep_range(CXD2841ER_DVBS_POLLING_INVL*1000,
33468c2ecf20Sopenharmony_ci			(CXD2841ER_DVBS_POLLING_INVL + 2) * 1000);
33478c2ecf20Sopenharmony_ci		cxd2841er_read_status_s(fe, &status);
33488c2ecf20Sopenharmony_ci		if (status & FE_HAS_LOCK)
33498c2ecf20Sopenharmony_ci			break;
33508c2ecf20Sopenharmony_ci		i++;
33518c2ecf20Sopenharmony_ci	} while (i < timeout / CXD2841ER_DVBS_POLLING_INVL);
33528c2ecf20Sopenharmony_ci
33538c2ecf20Sopenharmony_ci	if (status & FE_HAS_LOCK) {
33548c2ecf20Sopenharmony_ci		if (cxd2841er_get_carrier_offset_s_s2(
33558c2ecf20Sopenharmony_ci				priv, &carr_offset)) {
33568c2ecf20Sopenharmony_ci			ret = -EINVAL;
33578c2ecf20Sopenharmony_ci			goto done;
33588c2ecf20Sopenharmony_ci		}
33598c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): carrier_offset=%d\n",
33608c2ecf20Sopenharmony_ci			__func__, carr_offset);
33618c2ecf20Sopenharmony_ci	}
33628c2ecf20Sopenharmony_cidone:
33638c2ecf20Sopenharmony_ci	/* Reset stats */
33648c2ecf20Sopenharmony_ci	p->strength.stat[0].scale = FE_SCALE_RELATIVE;
33658c2ecf20Sopenharmony_ci	p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
33668c2ecf20Sopenharmony_ci	p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
33678c2ecf20Sopenharmony_ci	p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
33688c2ecf20Sopenharmony_ci	p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
33698c2ecf20Sopenharmony_ci
33708c2ecf20Sopenharmony_ci	/* Reset the wait for jiffies logic */
33718c2ecf20Sopenharmony_ci	priv->stats_time = 0;
33728c2ecf20Sopenharmony_ci
33738c2ecf20Sopenharmony_ci	return ret;
33748c2ecf20Sopenharmony_ci}
33758c2ecf20Sopenharmony_ci
33768c2ecf20Sopenharmony_cistatic int cxd2841er_set_frontend_tc(struct dvb_frontend *fe)
33778c2ecf20Sopenharmony_ci{
33788c2ecf20Sopenharmony_ci	int ret = 0, timeout;
33798c2ecf20Sopenharmony_ci	enum fe_status status;
33808c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
33818c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
33828c2ecf20Sopenharmony_ci
33838c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s() delivery_system=%d bandwidth_hz=%d\n",
33848c2ecf20Sopenharmony_ci		 __func__, p->delivery_system, p->bandwidth_hz);
33858c2ecf20Sopenharmony_ci
33868c2ecf20Sopenharmony_ci	if (priv->flags & CXD2841ER_EARLY_TUNE)
33878c2ecf20Sopenharmony_ci		cxd2841er_tuner_set(fe);
33888c2ecf20Sopenharmony_ci
33898c2ecf20Sopenharmony_ci	/* deconfigure/put demod to sleep on delsys switch if active */
33908c2ecf20Sopenharmony_ci	if (priv->state == STATE_ACTIVE_TC &&
33918c2ecf20Sopenharmony_ci	    priv->system != p->delivery_system) {
33928c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s(): old_delsys=%d, new_delsys=%d -> sleep\n",
33938c2ecf20Sopenharmony_ci			 __func__, priv->system, p->delivery_system);
33948c2ecf20Sopenharmony_ci		cxd2841er_sleep_tc(fe);
33958c2ecf20Sopenharmony_ci	}
33968c2ecf20Sopenharmony_ci
33978c2ecf20Sopenharmony_ci	if (p->delivery_system == SYS_DVBT) {
33988c2ecf20Sopenharmony_ci		priv->system = SYS_DVBT;
33998c2ecf20Sopenharmony_ci		switch (priv->state) {
34008c2ecf20Sopenharmony_ci		case STATE_SLEEP_TC:
34018c2ecf20Sopenharmony_ci			ret = cxd2841er_sleep_tc_to_active_t(
34028c2ecf20Sopenharmony_ci				priv, p->bandwidth_hz);
34038c2ecf20Sopenharmony_ci			break;
34048c2ecf20Sopenharmony_ci		case STATE_ACTIVE_TC:
34058c2ecf20Sopenharmony_ci			ret = cxd2841er_retune_active(priv, p);
34068c2ecf20Sopenharmony_ci			break;
34078c2ecf20Sopenharmony_ci		default:
34088c2ecf20Sopenharmony_ci			dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
34098c2ecf20Sopenharmony_ci				__func__, priv->state);
34108c2ecf20Sopenharmony_ci			ret = -EINVAL;
34118c2ecf20Sopenharmony_ci		}
34128c2ecf20Sopenharmony_ci	} else if (p->delivery_system == SYS_DVBT2) {
34138c2ecf20Sopenharmony_ci		priv->system = SYS_DVBT2;
34148c2ecf20Sopenharmony_ci		cxd2841er_dvbt2_set_plp_config(priv,
34158c2ecf20Sopenharmony_ci			(int)(p->stream_id > 255), p->stream_id);
34168c2ecf20Sopenharmony_ci		cxd2841er_dvbt2_set_profile(priv, DVBT2_PROFILE_BASE);
34178c2ecf20Sopenharmony_ci		switch (priv->state) {
34188c2ecf20Sopenharmony_ci		case STATE_SLEEP_TC:
34198c2ecf20Sopenharmony_ci			ret = cxd2841er_sleep_tc_to_active_t2(priv,
34208c2ecf20Sopenharmony_ci				p->bandwidth_hz);
34218c2ecf20Sopenharmony_ci			break;
34228c2ecf20Sopenharmony_ci		case STATE_ACTIVE_TC:
34238c2ecf20Sopenharmony_ci			ret = cxd2841er_retune_active(priv, p);
34248c2ecf20Sopenharmony_ci			break;
34258c2ecf20Sopenharmony_ci		default:
34268c2ecf20Sopenharmony_ci			dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
34278c2ecf20Sopenharmony_ci				__func__, priv->state);
34288c2ecf20Sopenharmony_ci			ret = -EINVAL;
34298c2ecf20Sopenharmony_ci		}
34308c2ecf20Sopenharmony_ci	} else if (p->delivery_system == SYS_ISDBT) {
34318c2ecf20Sopenharmony_ci		priv->system = SYS_ISDBT;
34328c2ecf20Sopenharmony_ci		switch (priv->state) {
34338c2ecf20Sopenharmony_ci		case STATE_SLEEP_TC:
34348c2ecf20Sopenharmony_ci			ret = cxd2841er_sleep_tc_to_active_i(
34358c2ecf20Sopenharmony_ci					priv, p->bandwidth_hz);
34368c2ecf20Sopenharmony_ci			break;
34378c2ecf20Sopenharmony_ci		case STATE_ACTIVE_TC:
34388c2ecf20Sopenharmony_ci			ret = cxd2841er_retune_active(priv, p);
34398c2ecf20Sopenharmony_ci			break;
34408c2ecf20Sopenharmony_ci		default:
34418c2ecf20Sopenharmony_ci			dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
34428c2ecf20Sopenharmony_ci					__func__, priv->state);
34438c2ecf20Sopenharmony_ci			ret = -EINVAL;
34448c2ecf20Sopenharmony_ci		}
34458c2ecf20Sopenharmony_ci	} else if (p->delivery_system == SYS_DVBC_ANNEX_A ||
34468c2ecf20Sopenharmony_ci			p->delivery_system == SYS_DVBC_ANNEX_C) {
34478c2ecf20Sopenharmony_ci		priv->system = SYS_DVBC_ANNEX_A;
34488c2ecf20Sopenharmony_ci		/* correct bandwidth */
34498c2ecf20Sopenharmony_ci		if (p->bandwidth_hz != 6000000 &&
34508c2ecf20Sopenharmony_ci				p->bandwidth_hz != 7000000 &&
34518c2ecf20Sopenharmony_ci				p->bandwidth_hz != 8000000) {
34528c2ecf20Sopenharmony_ci			p->bandwidth_hz = 8000000;
34538c2ecf20Sopenharmony_ci			dev_dbg(&priv->i2c->dev, "%s(): forcing bandwidth to %d\n",
34548c2ecf20Sopenharmony_ci					__func__, p->bandwidth_hz);
34558c2ecf20Sopenharmony_ci		}
34568c2ecf20Sopenharmony_ci
34578c2ecf20Sopenharmony_ci		switch (priv->state) {
34588c2ecf20Sopenharmony_ci		case STATE_SLEEP_TC:
34598c2ecf20Sopenharmony_ci			ret = cxd2841er_sleep_tc_to_active_c(
34608c2ecf20Sopenharmony_ci				priv, p->bandwidth_hz);
34618c2ecf20Sopenharmony_ci			break;
34628c2ecf20Sopenharmony_ci		case STATE_ACTIVE_TC:
34638c2ecf20Sopenharmony_ci			ret = cxd2841er_retune_active(priv, p);
34648c2ecf20Sopenharmony_ci			break;
34658c2ecf20Sopenharmony_ci		default:
34668c2ecf20Sopenharmony_ci			dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
34678c2ecf20Sopenharmony_ci				__func__, priv->state);
34688c2ecf20Sopenharmony_ci			ret = -EINVAL;
34698c2ecf20Sopenharmony_ci		}
34708c2ecf20Sopenharmony_ci	} else {
34718c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
34728c2ecf20Sopenharmony_ci			"%s(): invalid delivery system %d\n",
34738c2ecf20Sopenharmony_ci			__func__, p->delivery_system);
34748c2ecf20Sopenharmony_ci		ret = -EINVAL;
34758c2ecf20Sopenharmony_ci	}
34768c2ecf20Sopenharmony_ci	if (ret)
34778c2ecf20Sopenharmony_ci		goto done;
34788c2ecf20Sopenharmony_ci
34798c2ecf20Sopenharmony_ci	if (!(priv->flags & CXD2841ER_EARLY_TUNE))
34808c2ecf20Sopenharmony_ci		cxd2841er_tuner_set(fe);
34818c2ecf20Sopenharmony_ci
34828c2ecf20Sopenharmony_ci	cxd2841er_tune_done(priv);
34838c2ecf20Sopenharmony_ci
34848c2ecf20Sopenharmony_ci	if (priv->flags & CXD2841ER_NO_WAIT_LOCK)
34858c2ecf20Sopenharmony_ci		goto done;
34868c2ecf20Sopenharmony_ci
34878c2ecf20Sopenharmony_ci	timeout = 2500;
34888c2ecf20Sopenharmony_ci	while (timeout > 0) {
34898c2ecf20Sopenharmony_ci		ret = cxd2841er_read_status_tc(fe, &status);
34908c2ecf20Sopenharmony_ci		if (ret)
34918c2ecf20Sopenharmony_ci			goto done;
34928c2ecf20Sopenharmony_ci		if (status & FE_HAS_LOCK)
34938c2ecf20Sopenharmony_ci			break;
34948c2ecf20Sopenharmony_ci		msleep(20);
34958c2ecf20Sopenharmony_ci		timeout -= 20;
34968c2ecf20Sopenharmony_ci	}
34978c2ecf20Sopenharmony_ci	if (timeout < 0)
34988c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev,
34998c2ecf20Sopenharmony_ci			"%s(): LOCK wait timeout\n", __func__);
35008c2ecf20Sopenharmony_cidone:
35018c2ecf20Sopenharmony_ci	return ret;
35028c2ecf20Sopenharmony_ci}
35038c2ecf20Sopenharmony_ci
35048c2ecf20Sopenharmony_cistatic int cxd2841er_tune_s(struct dvb_frontend *fe,
35058c2ecf20Sopenharmony_ci			    bool re_tune,
35068c2ecf20Sopenharmony_ci			    unsigned int mode_flags,
35078c2ecf20Sopenharmony_ci			    unsigned int *delay,
35088c2ecf20Sopenharmony_ci			    enum fe_status *status)
35098c2ecf20Sopenharmony_ci{
35108c2ecf20Sopenharmony_ci	int ret, carrier_offset;
35118c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
35128c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
35138c2ecf20Sopenharmony_ci
35148c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s() re_tune=%d\n", __func__, re_tune);
35158c2ecf20Sopenharmony_ci	if (re_tune) {
35168c2ecf20Sopenharmony_ci		ret = cxd2841er_set_frontend_s(fe);
35178c2ecf20Sopenharmony_ci		if (ret)
35188c2ecf20Sopenharmony_ci			return ret;
35198c2ecf20Sopenharmony_ci		cxd2841er_read_status_s(fe, status);
35208c2ecf20Sopenharmony_ci		if (*status & FE_HAS_LOCK) {
35218c2ecf20Sopenharmony_ci			if (cxd2841er_get_carrier_offset_s_s2(
35228c2ecf20Sopenharmony_ci					priv, &carrier_offset))
35238c2ecf20Sopenharmony_ci				return -EINVAL;
35248c2ecf20Sopenharmony_ci			p->frequency += carrier_offset;
35258c2ecf20Sopenharmony_ci			ret = cxd2841er_set_frontend_s(fe);
35268c2ecf20Sopenharmony_ci			if (ret)
35278c2ecf20Sopenharmony_ci				return ret;
35288c2ecf20Sopenharmony_ci		}
35298c2ecf20Sopenharmony_ci	}
35308c2ecf20Sopenharmony_ci	*delay = HZ / 5;
35318c2ecf20Sopenharmony_ci	return cxd2841er_read_status_s(fe, status);
35328c2ecf20Sopenharmony_ci}
35338c2ecf20Sopenharmony_ci
35348c2ecf20Sopenharmony_cistatic int cxd2841er_tune_tc(struct dvb_frontend *fe,
35358c2ecf20Sopenharmony_ci			     bool re_tune,
35368c2ecf20Sopenharmony_ci			     unsigned int mode_flags,
35378c2ecf20Sopenharmony_ci			     unsigned int *delay,
35388c2ecf20Sopenharmony_ci			     enum fe_status *status)
35398c2ecf20Sopenharmony_ci{
35408c2ecf20Sopenharmony_ci	int ret, carrier_offset;
35418c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
35428c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
35438c2ecf20Sopenharmony_ci
35448c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): re_tune %d bandwidth=%d\n", __func__,
35458c2ecf20Sopenharmony_ci			re_tune, p->bandwidth_hz);
35468c2ecf20Sopenharmony_ci	if (re_tune) {
35478c2ecf20Sopenharmony_ci		ret = cxd2841er_set_frontend_tc(fe);
35488c2ecf20Sopenharmony_ci		if (ret)
35498c2ecf20Sopenharmony_ci			return ret;
35508c2ecf20Sopenharmony_ci		cxd2841er_read_status_tc(fe, status);
35518c2ecf20Sopenharmony_ci		if (*status & FE_HAS_LOCK) {
35528c2ecf20Sopenharmony_ci			switch (priv->system) {
35538c2ecf20Sopenharmony_ci			case SYS_ISDBT:
35548c2ecf20Sopenharmony_ci				ret = cxd2841er_get_carrier_offset_i(
35558c2ecf20Sopenharmony_ci						priv, p->bandwidth_hz,
35568c2ecf20Sopenharmony_ci						&carrier_offset);
35578c2ecf20Sopenharmony_ci				if (ret)
35588c2ecf20Sopenharmony_ci					return ret;
35598c2ecf20Sopenharmony_ci				break;
35608c2ecf20Sopenharmony_ci			case SYS_DVBT:
35618c2ecf20Sopenharmony_ci				ret = cxd2841er_get_carrier_offset_t(
35628c2ecf20Sopenharmony_ci					priv, p->bandwidth_hz,
35638c2ecf20Sopenharmony_ci					&carrier_offset);
35648c2ecf20Sopenharmony_ci				if (ret)
35658c2ecf20Sopenharmony_ci					return ret;
35668c2ecf20Sopenharmony_ci				break;
35678c2ecf20Sopenharmony_ci			case SYS_DVBT2:
35688c2ecf20Sopenharmony_ci				ret = cxd2841er_get_carrier_offset_t2(
35698c2ecf20Sopenharmony_ci					priv, p->bandwidth_hz,
35708c2ecf20Sopenharmony_ci					&carrier_offset);
35718c2ecf20Sopenharmony_ci				if (ret)
35728c2ecf20Sopenharmony_ci					return ret;
35738c2ecf20Sopenharmony_ci				break;
35748c2ecf20Sopenharmony_ci			case SYS_DVBC_ANNEX_A:
35758c2ecf20Sopenharmony_ci				ret = cxd2841er_get_carrier_offset_c(
35768c2ecf20Sopenharmony_ci					priv, &carrier_offset);
35778c2ecf20Sopenharmony_ci				if (ret)
35788c2ecf20Sopenharmony_ci					return ret;
35798c2ecf20Sopenharmony_ci				break;
35808c2ecf20Sopenharmony_ci			default:
35818c2ecf20Sopenharmony_ci				dev_dbg(&priv->i2c->dev,
35828c2ecf20Sopenharmony_ci					"%s(): invalid delivery system %d\n",
35838c2ecf20Sopenharmony_ci					__func__, priv->system);
35848c2ecf20Sopenharmony_ci				return -EINVAL;
35858c2ecf20Sopenharmony_ci			}
35868c2ecf20Sopenharmony_ci			dev_dbg(&priv->i2c->dev, "%s(): carrier offset %d\n",
35878c2ecf20Sopenharmony_ci				__func__, carrier_offset);
35888c2ecf20Sopenharmony_ci			p->frequency += carrier_offset;
35898c2ecf20Sopenharmony_ci			ret = cxd2841er_set_frontend_tc(fe);
35908c2ecf20Sopenharmony_ci			if (ret)
35918c2ecf20Sopenharmony_ci				return ret;
35928c2ecf20Sopenharmony_ci		}
35938c2ecf20Sopenharmony_ci	}
35948c2ecf20Sopenharmony_ci	*delay = HZ / 5;
35958c2ecf20Sopenharmony_ci	return cxd2841er_read_status_tc(fe, status);
35968c2ecf20Sopenharmony_ci}
35978c2ecf20Sopenharmony_ci
35988c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_s(struct dvb_frontend *fe)
35998c2ecf20Sopenharmony_ci{
36008c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
36018c2ecf20Sopenharmony_ci
36028c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
36038c2ecf20Sopenharmony_ci	cxd2841er_active_s_to_sleep_s(fe->demodulator_priv);
36048c2ecf20Sopenharmony_ci	cxd2841er_sleep_s_to_shutdown(fe->demodulator_priv);
36058c2ecf20Sopenharmony_ci	return 0;
36068c2ecf20Sopenharmony_ci}
36078c2ecf20Sopenharmony_ci
36088c2ecf20Sopenharmony_cistatic int cxd2841er_sleep_tc(struct dvb_frontend *fe)
36098c2ecf20Sopenharmony_ci{
36108c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
36118c2ecf20Sopenharmony_ci
36128c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
36138c2ecf20Sopenharmony_ci
36148c2ecf20Sopenharmony_ci	if (priv->state == STATE_ACTIVE_TC) {
36158c2ecf20Sopenharmony_ci		switch (priv->system) {
36168c2ecf20Sopenharmony_ci		case SYS_DVBT:
36178c2ecf20Sopenharmony_ci			cxd2841er_active_t_to_sleep_tc(priv);
36188c2ecf20Sopenharmony_ci			break;
36198c2ecf20Sopenharmony_ci		case SYS_DVBT2:
36208c2ecf20Sopenharmony_ci			cxd2841er_active_t2_to_sleep_tc(priv);
36218c2ecf20Sopenharmony_ci			break;
36228c2ecf20Sopenharmony_ci		case SYS_ISDBT:
36238c2ecf20Sopenharmony_ci			cxd2841er_active_i_to_sleep_tc(priv);
36248c2ecf20Sopenharmony_ci			break;
36258c2ecf20Sopenharmony_ci		case SYS_DVBC_ANNEX_A:
36268c2ecf20Sopenharmony_ci			cxd2841er_active_c_to_sleep_tc(priv);
36278c2ecf20Sopenharmony_ci			break;
36288c2ecf20Sopenharmony_ci		default:
36298c2ecf20Sopenharmony_ci			dev_warn(&priv->i2c->dev,
36308c2ecf20Sopenharmony_ci				"%s(): unknown delivery system %d\n",
36318c2ecf20Sopenharmony_ci				__func__, priv->system);
36328c2ecf20Sopenharmony_ci		}
36338c2ecf20Sopenharmony_ci	}
36348c2ecf20Sopenharmony_ci	if (priv->state != STATE_SLEEP_TC) {
36358c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
36368c2ecf20Sopenharmony_ci			__func__, priv->state);
36378c2ecf20Sopenharmony_ci		return -EINVAL;
36388c2ecf20Sopenharmony_ci	}
36398c2ecf20Sopenharmony_ci	return 0;
36408c2ecf20Sopenharmony_ci}
36418c2ecf20Sopenharmony_ci
36428c2ecf20Sopenharmony_cistatic int cxd2841er_shutdown_tc(struct dvb_frontend *fe)
36438c2ecf20Sopenharmony_ci{
36448c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
36458c2ecf20Sopenharmony_ci
36468c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
36478c2ecf20Sopenharmony_ci
36488c2ecf20Sopenharmony_ci	if (!cxd2841er_sleep_tc(fe))
36498c2ecf20Sopenharmony_ci		cxd2841er_sleep_tc_to_shutdown(priv);
36508c2ecf20Sopenharmony_ci	return 0;
36518c2ecf20Sopenharmony_ci}
36528c2ecf20Sopenharmony_ci
36538c2ecf20Sopenharmony_cistatic int cxd2841er_send_burst(struct dvb_frontend *fe,
36548c2ecf20Sopenharmony_ci				enum fe_sec_mini_cmd burst)
36558c2ecf20Sopenharmony_ci{
36568c2ecf20Sopenharmony_ci	u8 data;
36578c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv  = fe->demodulator_priv;
36588c2ecf20Sopenharmony_ci
36598c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): burst mode %s\n", __func__,
36608c2ecf20Sopenharmony_ci		(burst == SEC_MINI_A ? "A" : "B"));
36618c2ecf20Sopenharmony_ci	if (priv->state != STATE_SLEEP_S &&
36628c2ecf20Sopenharmony_ci			priv->state != STATE_ACTIVE_S) {
36638c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
36648c2ecf20Sopenharmony_ci			__func__, priv->state);
36658c2ecf20Sopenharmony_ci		return -EINVAL;
36668c2ecf20Sopenharmony_ci	}
36678c2ecf20Sopenharmony_ci	data = (burst == SEC_MINI_A ? 0 : 1);
36688c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
36698c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x34, 0x01);
36708c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x35, data);
36718c2ecf20Sopenharmony_ci	return 0;
36728c2ecf20Sopenharmony_ci}
36738c2ecf20Sopenharmony_ci
36748c2ecf20Sopenharmony_cistatic int cxd2841er_set_tone(struct dvb_frontend *fe,
36758c2ecf20Sopenharmony_ci			      enum fe_sec_tone_mode tone)
36768c2ecf20Sopenharmony_ci{
36778c2ecf20Sopenharmony_ci	u8 data;
36788c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv  = fe->demodulator_priv;
36798c2ecf20Sopenharmony_ci
36808c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): tone %s\n", __func__,
36818c2ecf20Sopenharmony_ci		(tone == SEC_TONE_ON ? "On" : "Off"));
36828c2ecf20Sopenharmony_ci	if (priv->state != STATE_SLEEP_S &&
36838c2ecf20Sopenharmony_ci			priv->state != STATE_ACTIVE_S) {
36848c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
36858c2ecf20Sopenharmony_ci			__func__, priv->state);
36868c2ecf20Sopenharmony_ci		return -EINVAL;
36878c2ecf20Sopenharmony_ci	}
36888c2ecf20Sopenharmony_ci	data = (tone == SEC_TONE_ON ? 1 : 0);
36898c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
36908c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x36, data);
36918c2ecf20Sopenharmony_ci	return 0;
36928c2ecf20Sopenharmony_ci}
36938c2ecf20Sopenharmony_ci
36948c2ecf20Sopenharmony_cistatic int cxd2841er_send_diseqc_msg(struct dvb_frontend *fe,
36958c2ecf20Sopenharmony_ci				     struct dvb_diseqc_master_cmd *cmd)
36968c2ecf20Sopenharmony_ci{
36978c2ecf20Sopenharmony_ci	int i;
36988c2ecf20Sopenharmony_ci	u8 data[12];
36998c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv  = fe->demodulator_priv;
37008c2ecf20Sopenharmony_ci
37018c2ecf20Sopenharmony_ci	if (priv->state != STATE_SLEEP_S &&
37028c2ecf20Sopenharmony_ci			priv->state != STATE_ACTIVE_S) {
37038c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
37048c2ecf20Sopenharmony_ci			__func__, priv->state);
37058c2ecf20Sopenharmony_ci		return -EINVAL;
37068c2ecf20Sopenharmony_ci	}
37078c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev,
37088c2ecf20Sopenharmony_ci		"%s(): cmd->len %d\n", __func__, cmd->msg_len);
37098c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
37108c2ecf20Sopenharmony_ci	/* DiDEqC enable */
37118c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x33, 0x01);
37128c2ecf20Sopenharmony_ci	/* cmd1 length & data */
37138c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x3d, cmd->msg_len);
37148c2ecf20Sopenharmony_ci	memset(data, 0, sizeof(data));
37158c2ecf20Sopenharmony_ci	for (i = 0; i < cmd->msg_len && i < sizeof(data); i++)
37168c2ecf20Sopenharmony_ci		data[i] = cmd->msg[i];
37178c2ecf20Sopenharmony_ci	cxd2841er_write_regs(priv, I2C_SLVT, 0x3e, data, sizeof(data));
37188c2ecf20Sopenharmony_ci	/* repeat count for cmd1 */
37198c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x37, 1);
37208c2ecf20Sopenharmony_ci	/* repeat count for cmd2: always 0 */
37218c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x38, 0);
37228c2ecf20Sopenharmony_ci	/* start transmit */
37238c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x32, 0x01);
37248c2ecf20Sopenharmony_ci	/* wait for 1 sec timeout */
37258c2ecf20Sopenharmony_ci	for (i = 0; i < 50; i++) {
37268c2ecf20Sopenharmony_ci		cxd2841er_read_reg(priv, I2C_SLVT, 0x10, data);
37278c2ecf20Sopenharmony_ci		if (!data[0]) {
37288c2ecf20Sopenharmony_ci			dev_dbg(&priv->i2c->dev,
37298c2ecf20Sopenharmony_ci				"%s(): DiSEqC cmd has been sent\n", __func__);
37308c2ecf20Sopenharmony_ci			return 0;
37318c2ecf20Sopenharmony_ci		}
37328c2ecf20Sopenharmony_ci		msleep(20);
37338c2ecf20Sopenharmony_ci	}
37348c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev,
37358c2ecf20Sopenharmony_ci		"%s(): DiSEqC cmd transmit timeout\n", __func__);
37368c2ecf20Sopenharmony_ci	return -ETIMEDOUT;
37378c2ecf20Sopenharmony_ci}
37388c2ecf20Sopenharmony_ci
37398c2ecf20Sopenharmony_cistatic void cxd2841er_release(struct dvb_frontend *fe)
37408c2ecf20Sopenharmony_ci{
37418c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv  = fe->demodulator_priv;
37428c2ecf20Sopenharmony_ci
37438c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
37448c2ecf20Sopenharmony_ci	kfree(priv);
37458c2ecf20Sopenharmony_ci}
37468c2ecf20Sopenharmony_ci
37478c2ecf20Sopenharmony_cistatic int cxd2841er_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
37488c2ecf20Sopenharmony_ci{
37498c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
37508c2ecf20Sopenharmony_ci
37518c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s(): enable=%d\n", __func__, enable);
37528c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(
37538c2ecf20Sopenharmony_ci		priv, I2C_SLVX, 0x8, (enable ? 0x01 : 0x00), 0x01);
37548c2ecf20Sopenharmony_ci	return 0;
37558c2ecf20Sopenharmony_ci}
37568c2ecf20Sopenharmony_ci
37578c2ecf20Sopenharmony_cistatic enum dvbfe_algo cxd2841er_get_algo(struct dvb_frontend *fe)
37588c2ecf20Sopenharmony_ci{
37598c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
37608c2ecf20Sopenharmony_ci
37618c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
37628c2ecf20Sopenharmony_ci	return DVBFE_ALGO_HW;
37638c2ecf20Sopenharmony_ci}
37648c2ecf20Sopenharmony_ci
37658c2ecf20Sopenharmony_cistatic void cxd2841er_init_stats(struct dvb_frontend *fe)
37668c2ecf20Sopenharmony_ci{
37678c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
37688c2ecf20Sopenharmony_ci
37698c2ecf20Sopenharmony_ci	p->strength.len = 1;
37708c2ecf20Sopenharmony_ci	p->strength.stat[0].scale = FE_SCALE_RELATIVE;
37718c2ecf20Sopenharmony_ci	p->cnr.len = 1;
37728c2ecf20Sopenharmony_ci	p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
37738c2ecf20Sopenharmony_ci	p->block_error.len = 1;
37748c2ecf20Sopenharmony_ci	p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
37758c2ecf20Sopenharmony_ci	p->post_bit_error.len = 1;
37768c2ecf20Sopenharmony_ci	p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
37778c2ecf20Sopenharmony_ci	p->post_bit_count.len = 1;
37788c2ecf20Sopenharmony_ci	p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
37798c2ecf20Sopenharmony_ci}
37808c2ecf20Sopenharmony_ci
37818c2ecf20Sopenharmony_ci
37828c2ecf20Sopenharmony_cistatic int cxd2841er_init_s(struct dvb_frontend *fe)
37838c2ecf20Sopenharmony_ci{
37848c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
37858c2ecf20Sopenharmony_ci
37868c2ecf20Sopenharmony_ci	/* sanity. force demod to SHUTDOWN state */
37878c2ecf20Sopenharmony_ci	if (priv->state == STATE_SLEEP_S) {
37888c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s() forcing sleep->shutdown\n",
37898c2ecf20Sopenharmony_ci				__func__);
37908c2ecf20Sopenharmony_ci		cxd2841er_sleep_s_to_shutdown(priv);
37918c2ecf20Sopenharmony_ci	} else if (priv->state == STATE_ACTIVE_S) {
37928c2ecf20Sopenharmony_ci		dev_dbg(&priv->i2c->dev, "%s() forcing active->sleep->shutdown\n",
37938c2ecf20Sopenharmony_ci				__func__);
37948c2ecf20Sopenharmony_ci		cxd2841er_active_s_to_sleep_s(priv);
37958c2ecf20Sopenharmony_ci		cxd2841er_sleep_s_to_shutdown(priv);
37968c2ecf20Sopenharmony_ci	}
37978c2ecf20Sopenharmony_ci
37988c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
37998c2ecf20Sopenharmony_ci	cxd2841er_shutdown_to_sleep_s(priv);
38008c2ecf20Sopenharmony_ci	/* SONY_DEMOD_CONFIG_SAT_IFAGCNEG set to 1 */
38018c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
38028c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xb9, 0x01, 0x01);
38038c2ecf20Sopenharmony_ci
38048c2ecf20Sopenharmony_ci	cxd2841er_init_stats(fe);
38058c2ecf20Sopenharmony_ci
38068c2ecf20Sopenharmony_ci	return 0;
38078c2ecf20Sopenharmony_ci}
38088c2ecf20Sopenharmony_ci
38098c2ecf20Sopenharmony_cistatic int cxd2841er_init_tc(struct dvb_frontend *fe)
38108c2ecf20Sopenharmony_ci{
38118c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = fe->demodulator_priv;
38128c2ecf20Sopenharmony_ci	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
38138c2ecf20Sopenharmony_ci
38148c2ecf20Sopenharmony_ci	dev_dbg(&priv->i2c->dev, "%s() bandwidth_hz=%d\n",
38158c2ecf20Sopenharmony_ci			__func__, p->bandwidth_hz);
38168c2ecf20Sopenharmony_ci	cxd2841er_shutdown_to_sleep_tc(priv);
38178c2ecf20Sopenharmony_ci	/* SONY_DEMOD_CONFIG_IFAGCNEG = 1 (0 for NO_AGCNEG */
38188c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
38198c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcb,
38208c2ecf20Sopenharmony_ci		((priv->flags & CXD2841ER_NO_AGCNEG) ? 0x00 : 0x40), 0x40);
38218c2ecf20Sopenharmony_ci	/* SONY_DEMOD_CONFIG_IFAGC_ADC_FS = 0 */
38228c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0xcd, 0x50);
38238c2ecf20Sopenharmony_ci	/* SONY_DEMOD_CONFIG_PARALLEL_SEL = 1 */
38248c2ecf20Sopenharmony_ci	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
38258c2ecf20Sopenharmony_ci	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4,
38268c2ecf20Sopenharmony_ci		((priv->flags & CXD2841ER_TS_SERIAL) ? 0x80 : 0x00), 0x80);
38278c2ecf20Sopenharmony_ci
38288c2ecf20Sopenharmony_ci	/* clear TSCFG bits 3+4 */
38298c2ecf20Sopenharmony_ci	if (priv->flags & CXD2841ER_TSBITS)
38308c2ecf20Sopenharmony_ci		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4, 0x00, 0x18);
38318c2ecf20Sopenharmony_ci
38328c2ecf20Sopenharmony_ci	cxd2841er_init_stats(fe);
38338c2ecf20Sopenharmony_ci
38348c2ecf20Sopenharmony_ci	return 0;
38358c2ecf20Sopenharmony_ci}
38368c2ecf20Sopenharmony_ci
38378c2ecf20Sopenharmony_cistatic const struct dvb_frontend_ops cxd2841er_dvbs_s2_ops;
38388c2ecf20Sopenharmony_cistatic struct dvb_frontend_ops cxd2841er_t_c_ops;
38398c2ecf20Sopenharmony_ci
38408c2ecf20Sopenharmony_cistatic struct dvb_frontend *cxd2841er_attach(struct cxd2841er_config *cfg,
38418c2ecf20Sopenharmony_ci					     struct i2c_adapter *i2c,
38428c2ecf20Sopenharmony_ci					     u8 system)
38438c2ecf20Sopenharmony_ci{
38448c2ecf20Sopenharmony_ci	u8 chip_id = 0;
38458c2ecf20Sopenharmony_ci	const char *type;
38468c2ecf20Sopenharmony_ci	const char *name;
38478c2ecf20Sopenharmony_ci	struct cxd2841er_priv *priv = NULL;
38488c2ecf20Sopenharmony_ci
38498c2ecf20Sopenharmony_ci	/* allocate memory for the internal state */
38508c2ecf20Sopenharmony_ci	priv = kzalloc(sizeof(struct cxd2841er_priv), GFP_KERNEL);
38518c2ecf20Sopenharmony_ci	if (!priv)
38528c2ecf20Sopenharmony_ci		return NULL;
38538c2ecf20Sopenharmony_ci	priv->i2c = i2c;
38548c2ecf20Sopenharmony_ci	priv->config = cfg;
38558c2ecf20Sopenharmony_ci	priv->i2c_addr_slvx = (cfg->i2c_addr + 4) >> 1;
38568c2ecf20Sopenharmony_ci	priv->i2c_addr_slvt = (cfg->i2c_addr) >> 1;
38578c2ecf20Sopenharmony_ci	priv->xtal = cfg->xtal;
38588c2ecf20Sopenharmony_ci	priv->flags = cfg->flags;
38598c2ecf20Sopenharmony_ci	priv->frontend.demodulator_priv = priv;
38608c2ecf20Sopenharmony_ci	dev_info(&priv->i2c->dev,
38618c2ecf20Sopenharmony_ci		"%s(): I2C adapter %p SLVX addr %x SLVT addr %x\n",
38628c2ecf20Sopenharmony_ci		__func__, priv->i2c,
38638c2ecf20Sopenharmony_ci		priv->i2c_addr_slvx, priv->i2c_addr_slvt);
38648c2ecf20Sopenharmony_ci	chip_id = cxd2841er_chip_id(priv);
38658c2ecf20Sopenharmony_ci	switch (chip_id) {
38668c2ecf20Sopenharmony_ci	case CXD2837ER_CHIP_ID:
38678c2ecf20Sopenharmony_ci		snprintf(cxd2841er_t_c_ops.info.name, 128,
38688c2ecf20Sopenharmony_ci				"Sony CXD2837ER DVB-T/T2/C demodulator");
38698c2ecf20Sopenharmony_ci		name = "CXD2837ER";
38708c2ecf20Sopenharmony_ci		type = "C/T/T2";
38718c2ecf20Sopenharmony_ci		break;
38728c2ecf20Sopenharmony_ci	case CXD2838ER_CHIP_ID:
38738c2ecf20Sopenharmony_ci		snprintf(cxd2841er_t_c_ops.info.name, 128,
38748c2ecf20Sopenharmony_ci				"Sony CXD2838ER ISDB-T demodulator");
38758c2ecf20Sopenharmony_ci		cxd2841er_t_c_ops.delsys[0] = SYS_ISDBT;
38768c2ecf20Sopenharmony_ci		cxd2841er_t_c_ops.delsys[1] = SYS_UNDEFINED;
38778c2ecf20Sopenharmony_ci		cxd2841er_t_c_ops.delsys[2] = SYS_UNDEFINED;
38788c2ecf20Sopenharmony_ci		name = "CXD2838ER";
38798c2ecf20Sopenharmony_ci		type = "ISDB-T";
38808c2ecf20Sopenharmony_ci		break;
38818c2ecf20Sopenharmony_ci	case CXD2841ER_CHIP_ID:
38828c2ecf20Sopenharmony_ci		snprintf(cxd2841er_t_c_ops.info.name, 128,
38838c2ecf20Sopenharmony_ci				"Sony CXD2841ER DVB-T/T2/C demodulator");
38848c2ecf20Sopenharmony_ci		name = "CXD2841ER";
38858c2ecf20Sopenharmony_ci		type = "T/T2/C/ISDB-T";
38868c2ecf20Sopenharmony_ci		break;
38878c2ecf20Sopenharmony_ci	case CXD2843ER_CHIP_ID:
38888c2ecf20Sopenharmony_ci		snprintf(cxd2841er_t_c_ops.info.name, 128,
38898c2ecf20Sopenharmony_ci				"Sony CXD2843ER DVB-T/T2/C/C2 demodulator");
38908c2ecf20Sopenharmony_ci		name = "CXD2843ER";
38918c2ecf20Sopenharmony_ci		type = "C/C2/T/T2";
38928c2ecf20Sopenharmony_ci		break;
38938c2ecf20Sopenharmony_ci	case CXD2854ER_CHIP_ID:
38948c2ecf20Sopenharmony_ci		snprintf(cxd2841er_t_c_ops.info.name, 128,
38958c2ecf20Sopenharmony_ci				"Sony CXD2854ER DVB-T/T2/C and ISDB-T demodulator");
38968c2ecf20Sopenharmony_ci		cxd2841er_t_c_ops.delsys[3] = SYS_ISDBT;
38978c2ecf20Sopenharmony_ci		name = "CXD2854ER";
38988c2ecf20Sopenharmony_ci		type = "C/C2/T/T2/ISDB-T";
38998c2ecf20Sopenharmony_ci		break;
39008c2ecf20Sopenharmony_ci	default:
39018c2ecf20Sopenharmony_ci		dev_err(&priv->i2c->dev, "%s(): invalid chip ID 0x%02x\n",
39028c2ecf20Sopenharmony_ci				__func__, chip_id);
39038c2ecf20Sopenharmony_ci		priv->frontend.demodulator_priv = NULL;
39048c2ecf20Sopenharmony_ci		kfree(priv);
39058c2ecf20Sopenharmony_ci		return NULL;
39068c2ecf20Sopenharmony_ci	}
39078c2ecf20Sopenharmony_ci
39088c2ecf20Sopenharmony_ci	/* create dvb_frontend */
39098c2ecf20Sopenharmony_ci	if (system == SYS_DVBS) {
39108c2ecf20Sopenharmony_ci		memcpy(&priv->frontend.ops,
39118c2ecf20Sopenharmony_ci			&cxd2841er_dvbs_s2_ops,
39128c2ecf20Sopenharmony_ci			sizeof(struct dvb_frontend_ops));
39138c2ecf20Sopenharmony_ci		type = "S/S2";
39148c2ecf20Sopenharmony_ci	} else {
39158c2ecf20Sopenharmony_ci		memcpy(&priv->frontend.ops,
39168c2ecf20Sopenharmony_ci			&cxd2841er_t_c_ops,
39178c2ecf20Sopenharmony_ci			sizeof(struct dvb_frontend_ops));
39188c2ecf20Sopenharmony_ci	}
39198c2ecf20Sopenharmony_ci
39208c2ecf20Sopenharmony_ci	dev_info(&priv->i2c->dev,
39218c2ecf20Sopenharmony_ci		"%s(): attaching %s DVB-%s frontend\n",
39228c2ecf20Sopenharmony_ci		__func__, name, type);
39238c2ecf20Sopenharmony_ci	dev_info(&priv->i2c->dev, "%s(): chip ID 0x%02x OK.\n",
39248c2ecf20Sopenharmony_ci		__func__, chip_id);
39258c2ecf20Sopenharmony_ci	return &priv->frontend;
39268c2ecf20Sopenharmony_ci}
39278c2ecf20Sopenharmony_ci
39288c2ecf20Sopenharmony_cistruct dvb_frontend *cxd2841er_attach_s(struct cxd2841er_config *cfg,
39298c2ecf20Sopenharmony_ci					struct i2c_adapter *i2c)
39308c2ecf20Sopenharmony_ci{
39318c2ecf20Sopenharmony_ci	return cxd2841er_attach(cfg, i2c, SYS_DVBS);
39328c2ecf20Sopenharmony_ci}
39338c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cxd2841er_attach_s);
39348c2ecf20Sopenharmony_ci
39358c2ecf20Sopenharmony_cistruct dvb_frontend *cxd2841er_attach_t_c(struct cxd2841er_config *cfg,
39368c2ecf20Sopenharmony_ci					struct i2c_adapter *i2c)
39378c2ecf20Sopenharmony_ci{
39388c2ecf20Sopenharmony_ci	return cxd2841er_attach(cfg, i2c, 0);
39398c2ecf20Sopenharmony_ci}
39408c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(cxd2841er_attach_t_c);
39418c2ecf20Sopenharmony_ci
39428c2ecf20Sopenharmony_cistatic const struct dvb_frontend_ops cxd2841er_dvbs_s2_ops = {
39438c2ecf20Sopenharmony_ci	.delsys = { SYS_DVBS, SYS_DVBS2 },
39448c2ecf20Sopenharmony_ci	.info = {
39458c2ecf20Sopenharmony_ci		.name		= "Sony CXD2841ER DVB-S/S2 demodulator",
39468c2ecf20Sopenharmony_ci		.frequency_min_hz	=  500 * MHz,
39478c2ecf20Sopenharmony_ci		.frequency_max_hz	= 2500 * MHz,
39488c2ecf20Sopenharmony_ci		.symbol_rate_min = 1000000,
39498c2ecf20Sopenharmony_ci		.symbol_rate_max = 45000000,
39508c2ecf20Sopenharmony_ci		.symbol_rate_tolerance = 500,
39518c2ecf20Sopenharmony_ci		.caps = FE_CAN_INVERSION_AUTO |
39528c2ecf20Sopenharmony_ci			FE_CAN_FEC_AUTO |
39538c2ecf20Sopenharmony_ci			FE_CAN_QPSK,
39548c2ecf20Sopenharmony_ci	},
39558c2ecf20Sopenharmony_ci	.init = cxd2841er_init_s,
39568c2ecf20Sopenharmony_ci	.sleep = cxd2841er_sleep_s,
39578c2ecf20Sopenharmony_ci	.release = cxd2841er_release,
39588c2ecf20Sopenharmony_ci	.set_frontend = cxd2841er_set_frontend_s,
39598c2ecf20Sopenharmony_ci	.get_frontend = cxd2841er_get_frontend,
39608c2ecf20Sopenharmony_ci	.read_status = cxd2841er_read_status_s,
39618c2ecf20Sopenharmony_ci	.i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
39628c2ecf20Sopenharmony_ci	.get_frontend_algo = cxd2841er_get_algo,
39638c2ecf20Sopenharmony_ci	.set_tone = cxd2841er_set_tone,
39648c2ecf20Sopenharmony_ci	.diseqc_send_burst = cxd2841er_send_burst,
39658c2ecf20Sopenharmony_ci	.diseqc_send_master_cmd = cxd2841er_send_diseqc_msg,
39668c2ecf20Sopenharmony_ci	.tune = cxd2841er_tune_s
39678c2ecf20Sopenharmony_ci};
39688c2ecf20Sopenharmony_ci
39698c2ecf20Sopenharmony_cistatic struct dvb_frontend_ops cxd2841er_t_c_ops = {
39708c2ecf20Sopenharmony_ci	.delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A },
39718c2ecf20Sopenharmony_ci	.info = {
39728c2ecf20Sopenharmony_ci		.name	= "", /* will set in attach function */
39738c2ecf20Sopenharmony_ci		.caps = FE_CAN_FEC_1_2 |
39748c2ecf20Sopenharmony_ci			FE_CAN_FEC_2_3 |
39758c2ecf20Sopenharmony_ci			FE_CAN_FEC_3_4 |
39768c2ecf20Sopenharmony_ci			FE_CAN_FEC_5_6 |
39778c2ecf20Sopenharmony_ci			FE_CAN_FEC_7_8 |
39788c2ecf20Sopenharmony_ci			FE_CAN_FEC_AUTO |
39798c2ecf20Sopenharmony_ci			FE_CAN_QPSK |
39808c2ecf20Sopenharmony_ci			FE_CAN_QAM_16 |
39818c2ecf20Sopenharmony_ci			FE_CAN_QAM_32 |
39828c2ecf20Sopenharmony_ci			FE_CAN_QAM_64 |
39838c2ecf20Sopenharmony_ci			FE_CAN_QAM_128 |
39848c2ecf20Sopenharmony_ci			FE_CAN_QAM_256 |
39858c2ecf20Sopenharmony_ci			FE_CAN_QAM_AUTO |
39868c2ecf20Sopenharmony_ci			FE_CAN_TRANSMISSION_MODE_AUTO |
39878c2ecf20Sopenharmony_ci			FE_CAN_GUARD_INTERVAL_AUTO |
39888c2ecf20Sopenharmony_ci			FE_CAN_HIERARCHY_AUTO |
39898c2ecf20Sopenharmony_ci			FE_CAN_MUTE_TS |
39908c2ecf20Sopenharmony_ci			FE_CAN_2G_MODULATION,
39918c2ecf20Sopenharmony_ci		.frequency_min_hz =   42 * MHz,
39928c2ecf20Sopenharmony_ci		.frequency_max_hz = 1002 * MHz,
39938c2ecf20Sopenharmony_ci		.symbol_rate_min = 870000,
39948c2ecf20Sopenharmony_ci		.symbol_rate_max = 11700000
39958c2ecf20Sopenharmony_ci	},
39968c2ecf20Sopenharmony_ci	.init = cxd2841er_init_tc,
39978c2ecf20Sopenharmony_ci	.sleep = cxd2841er_shutdown_tc,
39988c2ecf20Sopenharmony_ci	.release = cxd2841er_release,
39998c2ecf20Sopenharmony_ci	.set_frontend = cxd2841er_set_frontend_tc,
40008c2ecf20Sopenharmony_ci	.get_frontend = cxd2841er_get_frontend,
40018c2ecf20Sopenharmony_ci	.read_status = cxd2841er_read_status_tc,
40028c2ecf20Sopenharmony_ci	.tune = cxd2841er_tune_tc,
40038c2ecf20Sopenharmony_ci	.i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
40048c2ecf20Sopenharmony_ci	.get_frontend_algo = cxd2841er_get_algo
40058c2ecf20Sopenharmony_ci};
40068c2ecf20Sopenharmony_ci
40078c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Sony CXD2837/38/41/43/54ER DVB-C/C2/T/T2/S/S2 demodulator driver");
40088c2ecf20Sopenharmony_ciMODULE_AUTHOR("Sergey Kozlov <serjk@netup.ru>, Abylay Ospan <aospan@netup.ru>");
40098c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
4010