1a6dc60ffSKozlov Sergey /*
2a6dc60ffSKozlov Sergey  * cxd2841er.c
3a6dc60ffSKozlov Sergey  *
483808c23SAbylay Ospan  * Sony digital demodulator driver for
59ca1736fSAbylay Ospan  *	CXD2841ER - DVB-S/S2/T/T2/C/C2
69ca1736fSAbylay Ospan  *	CXD2854ER - DVB-S/S2/T/T2/C/C2, ISDB-T/S
7a6dc60ffSKozlov Sergey  *
8a6dc60ffSKozlov Sergey  * Copyright 2012 Sony Corporation
9a6dc60ffSKozlov Sergey  * Copyright (C) 2014 NetUP Inc.
10a6dc60ffSKozlov Sergey  * Copyright (C) 2014 Sergey Kozlov <serjk@netup.ru>
11a6dc60ffSKozlov Sergey  * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
12a6dc60ffSKozlov Sergey  *
13a6dc60ffSKozlov Sergey  * This program is free software; you can redistribute it and/or modify
14a6dc60ffSKozlov Sergey  * it under the terms of the GNU General Public License as published by
15a6dc60ffSKozlov Sergey  * the Free Software Foundation; either version 2 of the License, or
16a6dc60ffSKozlov Sergey  * (at your option) any later version.
17a6dc60ffSKozlov Sergey  *
18a6dc60ffSKozlov Sergey  * This program is distributed in the hope that it will be useful,
19a6dc60ffSKozlov Sergey  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20a6dc60ffSKozlov Sergey  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21a6dc60ffSKozlov Sergey  * GNU General Public License for more details.
22a6dc60ffSKozlov Sergey   */
23a6dc60ffSKozlov Sergey 
24a6dc60ffSKozlov Sergey #include <linux/module.h>
25a6dc60ffSKozlov Sergey #include <linux/init.h>
26a6dc60ffSKozlov Sergey #include <linux/string.h>
27a6dc60ffSKozlov Sergey #include <linux/slab.h>
28a6dc60ffSKozlov Sergey #include <linux/bitops.h>
29a6dc60ffSKozlov Sergey #include <linux/math64.h>
30a6dc60ffSKozlov Sergey #include <linux/log2.h>
31a6dc60ffSKozlov Sergey #include <linux/dynamic_debug.h>
32a6dc60ffSKozlov Sergey 
33a6dc60ffSKozlov Sergey #include "dvb_math.h"
34a6dc60ffSKozlov Sergey #include "dvb_frontend.h"
35a6dc60ffSKozlov Sergey #include "cxd2841er.h"
36a6dc60ffSKozlov Sergey #include "cxd2841er_priv.h"
37a6dc60ffSKozlov Sergey 
38d13a7b67SMauro Carvalho Chehab #define MAX_WRITE_REGSIZE	16
39a6f330cbSAbylay Ospan #define LOG2_E_100X 144
40a6f330cbSAbylay Ospan 
41df61f828SDaniel Scheller #define INTLOG10X100(x) ((u32) (((u64) intlog10(x) * 100) >> 24))
42df61f828SDaniel Scheller 
43a6f330cbSAbylay Ospan /* DVB-C constellation */
44a6f330cbSAbylay Ospan enum sony_dvbc_constellation_t {
45a6f330cbSAbylay Ospan 	SONY_DVBC_CONSTELLATION_16QAM,
46a6f330cbSAbylay Ospan 	SONY_DVBC_CONSTELLATION_32QAM,
47a6f330cbSAbylay Ospan 	SONY_DVBC_CONSTELLATION_64QAM,
48a6f330cbSAbylay Ospan 	SONY_DVBC_CONSTELLATION_128QAM,
49a6f330cbSAbylay Ospan 	SONY_DVBC_CONSTELLATION_256QAM
50a6f330cbSAbylay Ospan };
51d13a7b67SMauro Carvalho Chehab 
52a6dc60ffSKozlov Sergey enum cxd2841er_state {
53a6dc60ffSKozlov Sergey 	STATE_SHUTDOWN = 0,
54a6dc60ffSKozlov Sergey 	STATE_SLEEP_S,
55a6dc60ffSKozlov Sergey 	STATE_ACTIVE_S,
56a6dc60ffSKozlov Sergey 	STATE_SLEEP_TC,
57a6dc60ffSKozlov Sergey 	STATE_ACTIVE_TC
58a6dc60ffSKozlov Sergey };
59a6dc60ffSKozlov Sergey 
60a6dc60ffSKozlov Sergey struct cxd2841er_priv {
61a6dc60ffSKozlov Sergey 	struct dvb_frontend		frontend;
62a6dc60ffSKozlov Sergey 	struct i2c_adapter		*i2c;
63a6dc60ffSKozlov Sergey 	u8				i2c_addr_slvx;
64a6dc60ffSKozlov Sergey 	u8				i2c_addr_slvt;
65a6dc60ffSKozlov Sergey 	const struct cxd2841er_config	*config;
66a6dc60ffSKozlov Sergey 	enum cxd2841er_state		state;
67a6dc60ffSKozlov Sergey 	u8				system;
6883808c23SAbylay Ospan 	enum cxd2841er_xtal		xtal;
693f3b48a0SAbylay Ospan 	enum fe_caps caps;
70050863aaSDaniel Scheller 	u32				flags;
71a6dc60ffSKozlov Sergey };
72a6dc60ffSKozlov Sergey 
73a6dc60ffSKozlov Sergey static const struct cxd2841er_cnr_data s_cn_data[] = {
74a6dc60ffSKozlov Sergey 	{ 0x033e, 0 }, { 0x0339, 100 }, { 0x0333, 200 },
75a6dc60ffSKozlov Sergey 	{ 0x032e, 300 }, { 0x0329, 400 }, { 0x0324, 500 },
76a6dc60ffSKozlov Sergey 	{ 0x031e, 600 }, { 0x0319, 700 }, { 0x0314, 800 },
77a6dc60ffSKozlov Sergey 	{ 0x030f, 900 }, { 0x030a, 1000 }, { 0x02ff, 1100 },
78a6dc60ffSKozlov Sergey 	{ 0x02f4, 1200 }, { 0x02e9, 1300 }, { 0x02de, 1400 },
79a6dc60ffSKozlov Sergey 	{ 0x02d4, 1500 }, { 0x02c9, 1600 }, { 0x02bf, 1700 },
80a6dc60ffSKozlov Sergey 	{ 0x02b5, 1800 }, { 0x02ab, 1900 }, { 0x02a1, 2000 },
81a6dc60ffSKozlov Sergey 	{ 0x029b, 2100 }, { 0x0295, 2200 }, { 0x0290, 2300 },
82a6dc60ffSKozlov Sergey 	{ 0x028a, 2400 }, { 0x0284, 2500 }, { 0x027f, 2600 },
83a6dc60ffSKozlov Sergey 	{ 0x0279, 2700 }, { 0x0274, 2800 }, { 0x026e, 2900 },
84a6dc60ffSKozlov Sergey 	{ 0x0269, 3000 }, { 0x0262, 3100 }, { 0x025c, 3200 },
85a6dc60ffSKozlov Sergey 	{ 0x0255, 3300 }, { 0x024f, 3400 }, { 0x0249, 3500 },
86a6dc60ffSKozlov Sergey 	{ 0x0242, 3600 }, { 0x023c, 3700 }, { 0x0236, 3800 },
87a6dc60ffSKozlov Sergey 	{ 0x0230, 3900 }, { 0x022a, 4000 }, { 0x0223, 4100 },
88a6dc60ffSKozlov Sergey 	{ 0x021c, 4200 }, { 0x0215, 4300 }, { 0x020e, 4400 },
89a6dc60ffSKozlov Sergey 	{ 0x0207, 4500 }, { 0x0201, 4600 }, { 0x01fa, 4700 },
90a6dc60ffSKozlov Sergey 	{ 0x01f4, 4800 }, { 0x01ed, 4900 }, { 0x01e7, 5000 },
91a6dc60ffSKozlov Sergey 	{ 0x01e0, 5100 }, { 0x01d9, 5200 }, { 0x01d2, 5300 },
92a6dc60ffSKozlov Sergey 	{ 0x01cb, 5400 }, { 0x01c4, 5500 }, { 0x01be, 5600 },
93a6dc60ffSKozlov Sergey 	{ 0x01b7, 5700 }, { 0x01b1, 5800 }, { 0x01aa, 5900 },
94a6dc60ffSKozlov Sergey 	{ 0x01a4, 6000 }, { 0x019d, 6100 }, { 0x0196, 6200 },
95a6dc60ffSKozlov Sergey 	{ 0x018f, 6300 }, { 0x0189, 6400 }, { 0x0182, 6500 },
96a6dc60ffSKozlov Sergey 	{ 0x017c, 6600 }, { 0x0175, 6700 }, { 0x016f, 6800 },
97a6dc60ffSKozlov Sergey 	{ 0x0169, 6900 }, { 0x0163, 7000 }, { 0x015c, 7100 },
98a6dc60ffSKozlov Sergey 	{ 0x0156, 7200 }, { 0x0150, 7300 }, { 0x014a, 7400 },
99a6dc60ffSKozlov Sergey 	{ 0x0144, 7500 }, { 0x013e, 7600 }, { 0x0138, 7700 },
100a6dc60ffSKozlov Sergey 	{ 0x0132, 7800 }, { 0x012d, 7900 }, { 0x0127, 8000 },
101a6dc60ffSKozlov Sergey 	{ 0x0121, 8100 }, { 0x011c, 8200 }, { 0x0116, 8300 },
102a6dc60ffSKozlov Sergey 	{ 0x0111, 8400 }, { 0x010b, 8500 }, { 0x0106, 8600 },
103a6dc60ffSKozlov Sergey 	{ 0x0101, 8700 }, { 0x00fc, 8800 }, { 0x00f7, 8900 },
104a6dc60ffSKozlov Sergey 	{ 0x00f2, 9000 }, { 0x00ee, 9100 }, { 0x00ea, 9200 },
105a6dc60ffSKozlov Sergey 	{ 0x00e6, 9300 }, { 0x00e2, 9400 }, { 0x00de, 9500 },
106a6dc60ffSKozlov Sergey 	{ 0x00da, 9600 }, { 0x00d7, 9700 }, { 0x00d3, 9800 },
107a6dc60ffSKozlov Sergey 	{ 0x00d0, 9900 }, { 0x00cc, 10000 }, { 0x00c7, 10100 },
108a6dc60ffSKozlov Sergey 	{ 0x00c3, 10200 }, { 0x00bf, 10300 }, { 0x00ba, 10400 },
109a6dc60ffSKozlov Sergey 	{ 0x00b6, 10500 }, { 0x00b2, 10600 }, { 0x00ae, 10700 },
110a6dc60ffSKozlov Sergey 	{ 0x00aa, 10800 }, { 0x00a7, 10900 }, { 0x00a3, 11000 },
111a6dc60ffSKozlov Sergey 	{ 0x009f, 11100 }, { 0x009c, 11200 }, { 0x0098, 11300 },
112a6dc60ffSKozlov Sergey 	{ 0x0094, 11400 }, { 0x0091, 11500 }, { 0x008e, 11600 },
113a6dc60ffSKozlov Sergey 	{ 0x008a, 11700 }, { 0x0087, 11800 }, { 0x0084, 11900 },
114a6dc60ffSKozlov Sergey 	{ 0x0081, 12000 }, { 0x007e, 12100 }, { 0x007b, 12200 },
115a6dc60ffSKozlov Sergey 	{ 0x0079, 12300 }, { 0x0076, 12400 }, { 0x0073, 12500 },
116a6dc60ffSKozlov Sergey 	{ 0x0071, 12600 }, { 0x006e, 12700 }, { 0x006c, 12800 },
117a6dc60ffSKozlov Sergey 	{ 0x0069, 12900 }, { 0x0067, 13000 }, { 0x0065, 13100 },
118a6dc60ffSKozlov Sergey 	{ 0x0062, 13200 }, { 0x0060, 13300 }, { 0x005e, 13400 },
119a6dc60ffSKozlov Sergey 	{ 0x005c, 13500 }, { 0x005a, 13600 }, { 0x0058, 13700 },
120a6dc60ffSKozlov Sergey 	{ 0x0056, 13800 }, { 0x0054, 13900 }, { 0x0052, 14000 },
121a6dc60ffSKozlov Sergey 	{ 0x0050, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
122a6dc60ffSKozlov Sergey 	{ 0x004b, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
123a6dc60ffSKozlov Sergey 	{ 0x0046, 14700 }, { 0x0044, 14800 }, { 0x0043, 14900 },
124a6dc60ffSKozlov Sergey 	{ 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
125a6dc60ffSKozlov Sergey 	{ 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
126a6dc60ffSKozlov Sergey 	{ 0x0037, 15700 }, { 0x0036, 15800 }, { 0x0034, 15900 },
127a6dc60ffSKozlov Sergey 	{ 0x0033, 16000 }, { 0x0032, 16100 }, { 0x0031, 16200 },
128a6dc60ffSKozlov Sergey 	{ 0x0030, 16300 }, { 0x002f, 16400 }, { 0x002e, 16500 },
129a6dc60ffSKozlov Sergey 	{ 0x002d, 16600 }, { 0x002c, 16700 }, { 0x002b, 16800 },
130a6dc60ffSKozlov Sergey 	{ 0x002a, 16900 }, { 0x0029, 17000 }, { 0x0028, 17100 },
131a6dc60ffSKozlov Sergey 	{ 0x0027, 17200 }, { 0x0026, 17300 }, { 0x0025, 17400 },
132a6dc60ffSKozlov Sergey 	{ 0x0024, 17500 }, { 0x0023, 17600 }, { 0x0022, 17800 },
133a6dc60ffSKozlov Sergey 	{ 0x0021, 17900 }, { 0x0020, 18000 }, { 0x001f, 18200 },
134a6dc60ffSKozlov Sergey 	{ 0x001e, 18300 }, { 0x001d, 18500 }, { 0x001c, 18700 },
135a6dc60ffSKozlov Sergey 	{ 0x001b, 18900 }, { 0x001a, 19000 }, { 0x0019, 19200 },
136a6dc60ffSKozlov Sergey 	{ 0x0018, 19300 }, { 0x0017, 19500 }, { 0x0016, 19700 },
137a6dc60ffSKozlov Sergey 	{ 0x0015, 19900 }, { 0x0014, 20000 },
138a6dc60ffSKozlov Sergey };
139a6dc60ffSKozlov Sergey 
140a6dc60ffSKozlov Sergey static const struct cxd2841er_cnr_data s2_cn_data[] = {
141a6dc60ffSKozlov Sergey 	{ 0x05af, 0 }, { 0x0597, 100 }, { 0x057e, 200 },
142a6dc60ffSKozlov Sergey 	{ 0x0567, 300 }, { 0x0550, 400 }, { 0x0539, 500 },
143a6dc60ffSKozlov Sergey 	{ 0x0522, 600 }, { 0x050c, 700 }, { 0x04f6, 800 },
144a6dc60ffSKozlov Sergey 	{ 0x04e1, 900 }, { 0x04cc, 1000 }, { 0x04b6, 1100 },
145a6dc60ffSKozlov Sergey 	{ 0x04a1, 1200 }, { 0x048c, 1300 }, { 0x0477, 1400 },
146a6dc60ffSKozlov Sergey 	{ 0x0463, 1500 }, { 0x044f, 1600 }, { 0x043c, 1700 },
147a6dc60ffSKozlov Sergey 	{ 0x0428, 1800 }, { 0x0416, 1900 }, { 0x0403, 2000 },
148a6dc60ffSKozlov Sergey 	{ 0x03ef, 2100 }, { 0x03dc, 2200 }, { 0x03c9, 2300 },
149a6dc60ffSKozlov Sergey 	{ 0x03b6, 2400 }, { 0x03a4, 2500 }, { 0x0392, 2600 },
150a6dc60ffSKozlov Sergey 	{ 0x0381, 2700 }, { 0x036f, 2800 }, { 0x035f, 2900 },
151a6dc60ffSKozlov Sergey 	{ 0x034e, 3000 }, { 0x033d, 3100 }, { 0x032d, 3200 },
152a6dc60ffSKozlov Sergey 	{ 0x031d, 3300 }, { 0x030d, 3400 }, { 0x02fd, 3500 },
153a6dc60ffSKozlov Sergey 	{ 0x02ee, 3600 }, { 0x02df, 3700 }, { 0x02d0, 3800 },
154a6dc60ffSKozlov Sergey 	{ 0x02c2, 3900 }, { 0x02b4, 4000 }, { 0x02a6, 4100 },
155a6dc60ffSKozlov Sergey 	{ 0x0299, 4200 }, { 0x028c, 4300 }, { 0x027f, 4400 },
156a6dc60ffSKozlov Sergey 	{ 0x0272, 4500 }, { 0x0265, 4600 }, { 0x0259, 4700 },
157a6dc60ffSKozlov Sergey 	{ 0x024d, 4800 }, { 0x0241, 4900 }, { 0x0236, 5000 },
158a6dc60ffSKozlov Sergey 	{ 0x022b, 5100 }, { 0x0220, 5200 }, { 0x0215, 5300 },
159a6dc60ffSKozlov Sergey 	{ 0x020a, 5400 }, { 0x0200, 5500 }, { 0x01f6, 5600 },
160a6dc60ffSKozlov Sergey 	{ 0x01ec, 5700 }, { 0x01e2, 5800 }, { 0x01d8, 5900 },
161a6dc60ffSKozlov Sergey 	{ 0x01cf, 6000 }, { 0x01c6, 6100 }, { 0x01bc, 6200 },
162a6dc60ffSKozlov Sergey 	{ 0x01b3, 6300 }, { 0x01aa, 6400 }, { 0x01a2, 6500 },
163a6dc60ffSKozlov Sergey 	{ 0x0199, 6600 }, { 0x0191, 6700 }, { 0x0189, 6800 },
164a6dc60ffSKozlov Sergey 	{ 0x0181, 6900 }, { 0x0179, 7000 }, { 0x0171, 7100 },
165a6dc60ffSKozlov Sergey 	{ 0x0169, 7200 }, { 0x0161, 7300 }, { 0x015a, 7400 },
166a6dc60ffSKozlov Sergey 	{ 0x0153, 7500 }, { 0x014b, 7600 }, { 0x0144, 7700 },
167a6dc60ffSKozlov Sergey 	{ 0x013d, 7800 }, { 0x0137, 7900 }, { 0x0130, 8000 },
168a6dc60ffSKozlov Sergey 	{ 0x012a, 8100 }, { 0x0124, 8200 }, { 0x011e, 8300 },
169a6dc60ffSKozlov Sergey 	{ 0x0118, 8400 }, { 0x0112, 8500 }, { 0x010c, 8600 },
170a6dc60ffSKozlov Sergey 	{ 0x0107, 8700 }, { 0x0101, 8800 }, { 0x00fc, 8900 },
171a6dc60ffSKozlov Sergey 	{ 0x00f7, 9000 }, { 0x00f2, 9100 }, { 0x00ec, 9200 },
172a6dc60ffSKozlov Sergey 	{ 0x00e7, 9300 }, { 0x00e2, 9400 }, { 0x00dd, 9500 },
173a6dc60ffSKozlov Sergey 	{ 0x00d8, 9600 }, { 0x00d4, 9700 }, { 0x00cf, 9800 },
174a6dc60ffSKozlov Sergey 	{ 0x00ca, 9900 }, { 0x00c6, 10000 }, { 0x00c2, 10100 },
175a6dc60ffSKozlov Sergey 	{ 0x00be, 10200 }, { 0x00b9, 10300 }, { 0x00b5, 10400 },
176a6dc60ffSKozlov Sergey 	{ 0x00b1, 10500 }, { 0x00ae, 10600 }, { 0x00aa, 10700 },
177a6dc60ffSKozlov Sergey 	{ 0x00a6, 10800 }, { 0x00a3, 10900 }, { 0x009f, 11000 },
178a6dc60ffSKozlov Sergey 	{ 0x009b, 11100 }, { 0x0098, 11200 }, { 0x0095, 11300 },
179a6dc60ffSKozlov Sergey 	{ 0x0091, 11400 }, { 0x008e, 11500 }, { 0x008b, 11600 },
180a6dc60ffSKozlov Sergey 	{ 0x0088, 11700 }, { 0x0085, 11800 }, { 0x0082, 11900 },
181a6dc60ffSKozlov Sergey 	{ 0x007f, 12000 }, { 0x007c, 12100 }, { 0x007a, 12200 },
182a6dc60ffSKozlov Sergey 	{ 0x0077, 12300 }, { 0x0074, 12400 }, { 0x0072, 12500 },
183a6dc60ffSKozlov Sergey 	{ 0x006f, 12600 }, { 0x006d, 12700 }, { 0x006b, 12800 },
184a6dc60ffSKozlov Sergey 	{ 0x0068, 12900 }, { 0x0066, 13000 }, { 0x0064, 13100 },
185a6dc60ffSKozlov Sergey 	{ 0x0061, 13200 }, { 0x005f, 13300 }, { 0x005d, 13400 },
186a6dc60ffSKozlov Sergey 	{ 0x005b, 13500 }, { 0x0059, 13600 }, { 0x0057, 13700 },
187a6dc60ffSKozlov Sergey 	{ 0x0055, 13800 }, { 0x0053, 13900 }, { 0x0051, 14000 },
188a6dc60ffSKozlov Sergey 	{ 0x004f, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
189a6dc60ffSKozlov Sergey 	{ 0x004a, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
190a6dc60ffSKozlov Sergey 	{ 0x0045, 14700 }, { 0x0044, 14800 }, { 0x0042, 14900 },
191a6dc60ffSKozlov Sergey 	{ 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
192a6dc60ffSKozlov Sergey 	{ 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
193a6dc60ffSKozlov Sergey 	{ 0x0038, 15600 }, { 0x0037, 15700 }, { 0x0036, 15800 },
194a6dc60ffSKozlov Sergey 	{ 0x0034, 15900 }, { 0x0033, 16000 }, { 0x0032, 16100 },
195a6dc60ffSKozlov Sergey 	{ 0x0031, 16200 }, { 0x0030, 16300 }, { 0x002f, 16400 },
196a6dc60ffSKozlov Sergey 	{ 0x002e, 16500 }, { 0x002d, 16600 }, { 0x002c, 16700 },
197a6dc60ffSKozlov Sergey 	{ 0x002b, 16800 }, { 0x002a, 16900 }, { 0x0029, 17000 },
198a6dc60ffSKozlov Sergey 	{ 0x0028, 17100 }, { 0x0027, 17200 }, { 0x0026, 17300 },
199a6dc60ffSKozlov Sergey 	{ 0x0025, 17400 }, { 0x0024, 17500 }, { 0x0023, 17600 },
200a6dc60ffSKozlov Sergey 	{ 0x0022, 17800 }, { 0x0021, 17900 }, { 0x0020, 18000 },
201a6dc60ffSKozlov Sergey 	{ 0x001f, 18200 }, { 0x001e, 18300 }, { 0x001d, 18500 },
202a6dc60ffSKozlov Sergey 	{ 0x001c, 18700 }, { 0x001b, 18900 }, { 0x001a, 19000 },
203a6dc60ffSKozlov Sergey 	{ 0x0019, 19200 }, { 0x0018, 19300 }, { 0x0017, 19500 },
204a6dc60ffSKozlov Sergey 	{ 0x0016, 19700 }, { 0x0015, 19900 }, { 0x0014, 20000 },
205a6dc60ffSKozlov Sergey };
206a6dc60ffSKozlov Sergey 
2070854df79SAbylay Ospan static int cxd2841er_freeze_regs(struct cxd2841er_priv *priv);
2080854df79SAbylay Ospan static int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv);
2090854df79SAbylay Ospan 
210a6dc60ffSKozlov Sergey static void cxd2841er_i2c_debug(struct cxd2841er_priv *priv,
211a6dc60ffSKozlov Sergey 				u8 addr, u8 reg, u8 write,
212a6dc60ffSKozlov Sergey 				const u8 *data, u32 len)
213a6dc60ffSKozlov Sergey {
214a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev,
2155d6d93a1SDaniel Scheller 		"cxd2841er: I2C %s addr %02x reg 0x%02x size %d data %*ph\n",
2165d6d93a1SDaniel Scheller 		(write == 0 ? "read" : "write"), addr, reg, len, len, data);
217a6dc60ffSKozlov Sergey }
218a6dc60ffSKozlov Sergey 
219a6dc60ffSKozlov Sergey static int cxd2841er_write_regs(struct cxd2841er_priv *priv,
220a6dc60ffSKozlov Sergey 				u8 addr, u8 reg, const u8 *data, u32 len)
221a6dc60ffSKozlov Sergey {
222a6dc60ffSKozlov Sergey 	int ret;
223d13a7b67SMauro Carvalho Chehab 	u8 buf[MAX_WRITE_REGSIZE + 1];
224a6dc60ffSKozlov Sergey 	u8 i2c_addr = (addr == I2C_SLVX ?
225a6dc60ffSKozlov Sergey 		priv->i2c_addr_slvx : priv->i2c_addr_slvt);
226a6dc60ffSKozlov Sergey 	struct i2c_msg msg[1] = {
227a6dc60ffSKozlov Sergey 		{
228a6dc60ffSKozlov Sergey 			.addr = i2c_addr,
229a6dc60ffSKozlov Sergey 			.flags = 0,
230d13a7b67SMauro Carvalho Chehab 			.len = len + 1,
231a6dc60ffSKozlov Sergey 			.buf = buf,
232a6dc60ffSKozlov Sergey 		}
233a6dc60ffSKozlov Sergey 	};
234a6dc60ffSKozlov Sergey 
235d13a7b67SMauro Carvalho Chehab 	if (len + 1 >= sizeof(buf)) {
236d13a7b67SMauro Carvalho Chehab 		dev_warn(&priv->i2c->dev, "wr reg=%04x: len=%d is too big!\n",
237d13a7b67SMauro Carvalho Chehab 			 reg, len + 1);
238d13a7b67SMauro Carvalho Chehab 		return -E2BIG;
239d13a7b67SMauro Carvalho Chehab 	}
240d13a7b67SMauro Carvalho Chehab 
241a6dc60ffSKozlov Sergey 	cxd2841er_i2c_debug(priv, i2c_addr, reg, 1, data, len);
242a6dc60ffSKozlov Sergey 	buf[0] = reg;
243a6dc60ffSKozlov Sergey 	memcpy(&buf[1], data, len);
244a6dc60ffSKozlov Sergey 
245a6dc60ffSKozlov Sergey 	ret = i2c_transfer(priv->i2c, msg, 1);
246a6dc60ffSKozlov Sergey 	if (ret >= 0 && ret != 1)
247a6dc60ffSKozlov Sergey 		ret = -EIO;
248a6dc60ffSKozlov Sergey 	if (ret < 0) {
249a6dc60ffSKozlov Sergey 		dev_warn(&priv->i2c->dev,
250a6dc60ffSKozlov Sergey 			"%s: i2c wr failed=%d addr=%02x reg=%02x len=%d\n",
251a6dc60ffSKozlov Sergey 			KBUILD_MODNAME, ret, i2c_addr, reg, len);
252a6dc60ffSKozlov Sergey 		return ret;
253a6dc60ffSKozlov Sergey 	}
254a6dc60ffSKozlov Sergey 	return 0;
255a6dc60ffSKozlov Sergey }
256a6dc60ffSKozlov Sergey 
257a6dc60ffSKozlov Sergey static int cxd2841er_write_reg(struct cxd2841er_priv *priv,
258a6dc60ffSKozlov Sergey 			       u8 addr, u8 reg, u8 val)
259a6dc60ffSKozlov Sergey {
260a6dc60ffSKozlov Sergey 	return cxd2841er_write_regs(priv, addr, reg, &val, 1);
261a6dc60ffSKozlov Sergey }
262a6dc60ffSKozlov Sergey 
263a6dc60ffSKozlov Sergey static int cxd2841er_read_regs(struct cxd2841er_priv *priv,
264a6dc60ffSKozlov Sergey 			       u8 addr, u8 reg, u8 *val, u32 len)
265a6dc60ffSKozlov Sergey {
266a6dc60ffSKozlov Sergey 	int ret;
267a6dc60ffSKozlov Sergey 	u8 i2c_addr = (addr == I2C_SLVX ?
268a6dc60ffSKozlov Sergey 		priv->i2c_addr_slvx : priv->i2c_addr_slvt);
269a6dc60ffSKozlov Sergey 	struct i2c_msg msg[2] = {
270a6dc60ffSKozlov Sergey 		{
271a6dc60ffSKozlov Sergey 			.addr = i2c_addr,
272a6dc60ffSKozlov Sergey 			.flags = 0,
273a6dc60ffSKozlov Sergey 			.len = 1,
274a6dc60ffSKozlov Sergey 			.buf = &reg,
275a6dc60ffSKozlov Sergey 		}, {
276a6dc60ffSKozlov Sergey 			.addr = i2c_addr,
277a6dc60ffSKozlov Sergey 			.flags = I2C_M_RD,
278a6dc60ffSKozlov Sergey 			.len = len,
279a6dc60ffSKozlov Sergey 			.buf = val,
280a6dc60ffSKozlov Sergey 		}
281a6dc60ffSKozlov Sergey 	};
282a6dc60ffSKozlov Sergey 
283725e93ebSDaniel Scheller 	ret = i2c_transfer(priv->i2c, msg, 2);
284725e93ebSDaniel Scheller 	if (ret >= 0 && ret != 2)
285a6dc60ffSKozlov Sergey 		ret = -EIO;
286a6dc60ffSKozlov Sergey 	if (ret < 0) {
287a6dc60ffSKozlov Sergey 		dev_warn(&priv->i2c->dev,
288a6dc60ffSKozlov Sergey 			"%s: i2c rd failed=%d addr=%02x reg=%02x\n",
289a6dc60ffSKozlov Sergey 			KBUILD_MODNAME, ret, i2c_addr, reg);
290a6dc60ffSKozlov Sergey 		return ret;
291a6dc60ffSKozlov Sergey 	}
2926c77161aSAbylay Ospan 	cxd2841er_i2c_debug(priv, i2c_addr, reg, 0, val, len);
293a6dc60ffSKozlov Sergey 	return 0;
294a6dc60ffSKozlov Sergey }
295a6dc60ffSKozlov Sergey 
296a6dc60ffSKozlov Sergey static int cxd2841er_read_reg(struct cxd2841er_priv *priv,
297a6dc60ffSKozlov Sergey 			      u8 addr, u8 reg, u8 *val)
298a6dc60ffSKozlov Sergey {
299a6dc60ffSKozlov Sergey 	return cxd2841er_read_regs(priv, addr, reg, val, 1);
300a6dc60ffSKozlov Sergey }
301a6dc60ffSKozlov Sergey 
302a6dc60ffSKozlov Sergey static int cxd2841er_set_reg_bits(struct cxd2841er_priv *priv,
303a6dc60ffSKozlov Sergey 				  u8 addr, u8 reg, u8 data, u8 mask)
304a6dc60ffSKozlov Sergey {
305a6dc60ffSKozlov Sergey 	int res;
306a6dc60ffSKozlov Sergey 	u8 rdata;
307a6dc60ffSKozlov Sergey 
308a6dc60ffSKozlov Sergey 	if (mask != 0xff) {
309a6dc60ffSKozlov Sergey 		res = cxd2841er_read_reg(priv, addr, reg, &rdata);
310a6dc60ffSKozlov Sergey 		if (res)
311a6dc60ffSKozlov Sergey 			return res;
312a6dc60ffSKozlov Sergey 		data = ((data & mask) | (rdata & (mask ^ 0xFF)));
313a6dc60ffSKozlov Sergey 	}
314a6dc60ffSKozlov Sergey 	return cxd2841er_write_reg(priv, addr, reg, data);
315a6dc60ffSKozlov Sergey }
316a6dc60ffSKozlov Sergey 
317cbc85a47SDaniel Scheller static u32 cxd2841er_calc_iffreq_xtal(enum cxd2841er_xtal xtal, u32 ifhz)
318cbc85a47SDaniel Scheller {
319cbc85a47SDaniel Scheller 	u64 tmp;
320cbc85a47SDaniel Scheller 
321cbc85a47SDaniel Scheller 	tmp = (u64) ifhz * 16777216;
322cbc85a47SDaniel Scheller 	do_div(tmp, ((xtal == SONY_XTAL_24000) ? 48000000 : 41000000));
323cbc85a47SDaniel Scheller 
324cbc85a47SDaniel Scheller 	return (u32) tmp;
325cbc85a47SDaniel Scheller }
326cbc85a47SDaniel Scheller 
327cbc85a47SDaniel Scheller static u32 cxd2841er_calc_iffreq(u32 ifhz)
328cbc85a47SDaniel Scheller {
329cbc85a47SDaniel Scheller 	return cxd2841er_calc_iffreq_xtal(SONY_XTAL_20500, ifhz);
330cbc85a47SDaniel Scheller }
331cbc85a47SDaniel Scheller 
3324b866c4eSDaniel Scheller static int cxd2841er_get_if_hz(struct cxd2841er_priv *priv, u32 def_hz)
3334b866c4eSDaniel Scheller {
3344b866c4eSDaniel Scheller 	u32 hz;
3354b866c4eSDaniel Scheller 
3364b866c4eSDaniel Scheller 	if (priv->frontend.ops.tuner_ops.get_if_frequency
3374b866c4eSDaniel Scheller 			&& (priv->flags & CXD2841ER_AUTO_IFHZ))
3384b866c4eSDaniel Scheller 		priv->frontend.ops.tuner_ops.get_if_frequency(
3394b866c4eSDaniel Scheller 			&priv->frontend, &hz);
3404b866c4eSDaniel Scheller 	else
3414b866c4eSDaniel Scheller 		hz = def_hz;
3424b866c4eSDaniel Scheller 
3434b866c4eSDaniel Scheller 	return hz;
3444b866c4eSDaniel Scheller }
3454b866c4eSDaniel Scheller 
346c7518d13SDaniel Scheller static int cxd2841er_tuner_set(struct dvb_frontend *fe)
347c7518d13SDaniel Scheller {
348c7518d13SDaniel Scheller 	struct cxd2841er_priv *priv = fe->demodulator_priv;
349c7518d13SDaniel Scheller 
350c7518d13SDaniel Scheller 	if ((priv->flags & CXD2841ER_USE_GATECTRL) && fe->ops.i2c_gate_ctrl)
351c7518d13SDaniel Scheller 		fe->ops.i2c_gate_ctrl(fe, 1);
352c7518d13SDaniel Scheller 	if (fe->ops.tuner_ops.set_params)
353c7518d13SDaniel Scheller 		fe->ops.tuner_ops.set_params(fe);
354c7518d13SDaniel Scheller 	if ((priv->flags & CXD2841ER_USE_GATECTRL) && fe->ops.i2c_gate_ctrl)
355c7518d13SDaniel Scheller 		fe->ops.i2c_gate_ctrl(fe, 0);
356c7518d13SDaniel Scheller 
357c7518d13SDaniel Scheller 	return 0;
358c7518d13SDaniel Scheller }
359c7518d13SDaniel Scheller 
360a6dc60ffSKozlov Sergey static int cxd2841er_dvbs2_set_symbol_rate(struct cxd2841er_priv *priv,
361a6dc60ffSKozlov Sergey 					   u32 symbol_rate)
362a6dc60ffSKozlov Sergey {
363a6dc60ffSKozlov Sergey 	u32 reg_value = 0;
364a6dc60ffSKozlov Sergey 	u8 data[3] = {0, 0, 0};
365a6dc60ffSKozlov Sergey 
366a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
367a6dc60ffSKozlov Sergey 	/*
368a6dc60ffSKozlov Sergey 	 * regValue = (symbolRateKSps * 2^14 / 1000) + 0.5
369a6dc60ffSKozlov Sergey 	 *          = ((symbolRateKSps * 2^14) + 500) / 1000
370a6dc60ffSKozlov Sergey 	 *          = ((symbolRateKSps * 16384) + 500) / 1000
371a6dc60ffSKozlov Sergey 	 */
372a6dc60ffSKozlov Sergey 	reg_value = DIV_ROUND_CLOSEST(symbol_rate * 16384, 1000);
373a6dc60ffSKozlov Sergey 	if ((reg_value == 0) || (reg_value > 0xFFFFF)) {
374a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev,
375a6dc60ffSKozlov Sergey 			"%s(): reg_value is out of range\n", __func__);
376a6dc60ffSKozlov Sergey 		return -EINVAL;
377a6dc60ffSKozlov Sergey 	}
378a6dc60ffSKozlov Sergey 	data[0] = (u8)((reg_value >> 16) & 0x0F);
379a6dc60ffSKozlov Sergey 	data[1] = (u8)((reg_value >>  8) & 0xFF);
380a6dc60ffSKozlov Sergey 	data[2] = (u8)(reg_value & 0xFF);
381a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0xAE */
382a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
383a6dc60ffSKozlov Sergey 	cxd2841er_write_regs(priv, I2C_SLVT, 0x20, data, 3);
384a6dc60ffSKozlov Sergey 	return 0;
385a6dc60ffSKozlov Sergey }
386a6dc60ffSKozlov Sergey 
387a6dc60ffSKozlov Sergey static void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
388a6dc60ffSKozlov Sergey 					u8 system);
389a6dc60ffSKozlov Sergey 
390a6dc60ffSKozlov Sergey static int cxd2841er_sleep_s_to_active_s(struct cxd2841er_priv *priv,
391a6dc60ffSKozlov Sergey 					 u8 system, u32 symbol_rate)
392a6dc60ffSKozlov Sergey {
393a6dc60ffSKozlov Sergey 	int ret;
394a6dc60ffSKozlov Sergey 	u8 data[4] = { 0, 0, 0, 0 };
395a6dc60ffSKozlov Sergey 
396a6dc60ffSKozlov Sergey 	if (priv->state != STATE_SLEEP_S) {
397a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
398a6dc60ffSKozlov Sergey 			__func__, (int)priv->state);
399a6dc60ffSKozlov Sergey 		return -EINVAL;
400a6dc60ffSKozlov Sergey 	}
401a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
402a6dc60ffSKozlov Sergey 	cxd2841er_set_ts_clock_mode(priv, SYS_DVBS);
403a6dc60ffSKozlov Sergey 	/* Set demod mode */
404a6dc60ffSKozlov Sergey 	if (system == SYS_DVBS) {
405a6dc60ffSKozlov Sergey 		data[0] = 0x0A;
406a6dc60ffSKozlov Sergey 	} else if (system == SYS_DVBS2) {
407a6dc60ffSKozlov Sergey 		data[0] = 0x0B;
408a6dc60ffSKozlov Sergey 	} else {
409a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid delsys %d\n",
410a6dc60ffSKozlov Sergey 			__func__, system);
411a6dc60ffSKozlov Sergey 		return -EINVAL;
412a6dc60ffSKozlov Sergey 	}
413a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
414a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
415a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, data[0]);
416a6dc60ffSKozlov Sergey 	/* DVB-S/S2 */
417a6dc60ffSKozlov Sergey 	data[0] = 0x00;
418a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
419a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
420a6dc60ffSKozlov Sergey 	/* Enable S/S2 auto detection 1 */
421a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, data[0]);
422a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0xAE */
423a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
424a6dc60ffSKozlov Sergey 	/* Enable S/S2 auto detection 2 */
425a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, data[0]);
426a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
427a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
428a6dc60ffSKozlov Sergey 	/* Enable demod clock */
429a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
430a6dc60ffSKozlov Sergey 	/* Enable ADC clock */
431a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x01);
432a6dc60ffSKozlov Sergey 	/* Enable ADC 1 */
433a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
434a6dc60ffSKozlov Sergey 	/* Enable ADC 2 */
435a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x3f);
436a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
437a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
438a6dc60ffSKozlov Sergey 	/* Enable ADC 3 */
439a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
440a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0xA3 */
441a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa3);
442a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0xac, 0x00);
443a6dc60ffSKozlov Sergey 	data[0] = 0x07;
444a6dc60ffSKozlov Sergey 	data[1] = 0x3B;
445a6dc60ffSKozlov Sergey 	data[2] = 0x08;
446a6dc60ffSKozlov Sergey 	data[3] = 0xC5;
447a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0xAB */
448a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xab);
449a6dc60ffSKozlov Sergey 	cxd2841er_write_regs(priv, I2C_SLVT, 0x98, data, 4);
450a6dc60ffSKozlov Sergey 	data[0] = 0x05;
451a6dc60ffSKozlov Sergey 	data[1] = 0x80;
452a6dc60ffSKozlov Sergey 	data[2] = 0x0A;
453a6dc60ffSKozlov Sergey 	data[3] = 0x80;
454a6dc60ffSKozlov Sergey 	cxd2841er_write_regs(priv, I2C_SLVT, 0xa8, data, 4);
455a6dc60ffSKozlov Sergey 	data[0] = 0x0C;
456a6dc60ffSKozlov Sergey 	data[1] = 0xCC;
457a6dc60ffSKozlov Sergey 	cxd2841er_write_regs(priv, I2C_SLVT, 0xc3, data, 2);
458a6dc60ffSKozlov Sergey 	/* Set demod parameter */
459a6dc60ffSKozlov Sergey 	ret = cxd2841er_dvbs2_set_symbol_rate(priv, symbol_rate);
460a6dc60ffSKozlov Sergey 	if (ret != 0)
461a6dc60ffSKozlov Sergey 		return ret;
462a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
463a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
464a6dc60ffSKozlov Sergey 	/* disable Hi-Z setting 1 */
465a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x10);
466a6dc60ffSKozlov Sergey 	/* disable Hi-Z setting 2 */
467a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
468a6dc60ffSKozlov Sergey 	priv->state = STATE_ACTIVE_S;
469a6dc60ffSKozlov Sergey 	return 0;
470a6dc60ffSKozlov Sergey }
471a6dc60ffSKozlov Sergey 
472a6dc60ffSKozlov Sergey static int cxd2841er_sleep_tc_to_active_t_band(struct cxd2841er_priv *priv,
473a6dc60ffSKozlov Sergey 					       u32 bandwidth);
474a6dc60ffSKozlov Sergey 
475a6dc60ffSKozlov Sergey static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
476a6dc60ffSKozlov Sergey 						u32 bandwidth);
477a6dc60ffSKozlov Sergey 
478a6dc60ffSKozlov Sergey static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
479a6dc60ffSKozlov Sergey 					       u32 bandwidth);
480a6dc60ffSKozlov Sergey 
48176344a3fSMauro Carvalho Chehab static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
48276344a3fSMauro Carvalho Chehab 		u32 bandwidth);
48376344a3fSMauro Carvalho Chehab 
48476344a3fSMauro Carvalho Chehab static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv);
48576344a3fSMauro Carvalho Chehab 
48676344a3fSMauro Carvalho Chehab static int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv);
48776344a3fSMauro Carvalho Chehab 
48876344a3fSMauro Carvalho Chehab static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv);
48976344a3fSMauro Carvalho Chehab 
490a6dc60ffSKozlov Sergey static int cxd2841er_retune_active(struct cxd2841er_priv *priv,
491a6dc60ffSKozlov Sergey 				   struct dtv_frontend_properties *p)
492a6dc60ffSKozlov Sergey {
493a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
494a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_S &&
495a6dc60ffSKozlov Sergey 			priv->state != STATE_ACTIVE_TC) {
496a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
497a6dc60ffSKozlov Sergey 			__func__, priv->state);
498a6dc60ffSKozlov Sergey 		return -EINVAL;
499a6dc60ffSKozlov Sergey 	}
500a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
501a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
502a6dc60ffSKozlov Sergey 	/* disable TS output */
503a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
504a6dc60ffSKozlov Sergey 	if (priv->state == STATE_ACTIVE_S)
505a6dc60ffSKozlov Sergey 		return cxd2841er_dvbs2_set_symbol_rate(
506a6dc60ffSKozlov Sergey 				priv, p->symbol_rate / 1000);
507a6dc60ffSKozlov Sergey 	else if (priv->state == STATE_ACTIVE_TC) {
508a6dc60ffSKozlov Sergey 		switch (priv->system) {
509a6dc60ffSKozlov Sergey 		case SYS_DVBT:
510a6dc60ffSKozlov Sergey 			return cxd2841er_sleep_tc_to_active_t_band(
511a6dc60ffSKozlov Sergey 					priv, p->bandwidth_hz);
512a6dc60ffSKozlov Sergey 		case SYS_DVBT2:
513a6dc60ffSKozlov Sergey 			return cxd2841er_sleep_tc_to_active_t2_band(
514a6dc60ffSKozlov Sergey 					priv, p->bandwidth_hz);
515a6dc60ffSKozlov Sergey 		case SYS_DVBC_ANNEX_A:
516a6dc60ffSKozlov Sergey 			return cxd2841er_sleep_tc_to_active_c_band(
51776344a3fSMauro Carvalho Chehab 					priv, p->bandwidth_hz);
51876344a3fSMauro Carvalho Chehab 		case SYS_ISDBT:
51976344a3fSMauro Carvalho Chehab 			cxd2841er_active_i_to_sleep_tc(priv);
52076344a3fSMauro Carvalho Chehab 			cxd2841er_sleep_tc_to_shutdown(priv);
52176344a3fSMauro Carvalho Chehab 			cxd2841er_shutdown_to_sleep_tc(priv);
52276344a3fSMauro Carvalho Chehab 			return cxd2841er_sleep_tc_to_active_i(
52376344a3fSMauro Carvalho Chehab 					priv, p->bandwidth_hz);
524a6dc60ffSKozlov Sergey 		}
525a6dc60ffSKozlov Sergey 	}
526a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
527a6dc60ffSKozlov Sergey 		__func__, priv->system);
528a6dc60ffSKozlov Sergey 	return -EINVAL;
529a6dc60ffSKozlov Sergey }
530a6dc60ffSKozlov Sergey 
531a6dc60ffSKozlov Sergey static int cxd2841er_active_s_to_sleep_s(struct cxd2841er_priv *priv)
532a6dc60ffSKozlov Sergey {
533a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
534a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_S) {
535a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
536a6dc60ffSKozlov Sergey 			__func__, priv->state);
537a6dc60ffSKozlov Sergey 		return -EINVAL;
538a6dc60ffSKozlov Sergey 	}
539a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
540a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
541a6dc60ffSKozlov Sergey 	/* disable TS output */
542a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
543a6dc60ffSKozlov Sergey 	/* enable Hi-Z setting 1 */
544a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1f);
545a6dc60ffSKozlov Sergey 	/* enable Hi-Z setting 2 */
546a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
547a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
548a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
549a6dc60ffSKozlov Sergey 	/* disable ADC 1 */
550a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
551a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
552a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
553a6dc60ffSKozlov Sergey 	/* disable ADC clock */
554a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x00);
555a6dc60ffSKozlov Sergey 	/* disable ADC 2 */
556a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
557a6dc60ffSKozlov Sergey 	/* disable ADC 3 */
558a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
559a6dc60ffSKozlov Sergey 	/* SADC Bias ON */
560a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
561a6dc60ffSKozlov Sergey 	/* disable demod clock */
562a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
563a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0xAE */
564a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
565a6dc60ffSKozlov Sergey 	/* disable S/S2 auto detection1 */
566a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
567a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
568a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
569a6dc60ffSKozlov Sergey 	/* disable S/S2 auto detection2 */
570a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, 0x00);
571a6dc60ffSKozlov Sergey 	priv->state = STATE_SLEEP_S;
572a6dc60ffSKozlov Sergey 	return 0;
573a6dc60ffSKozlov Sergey }
574a6dc60ffSKozlov Sergey 
575a6dc60ffSKozlov Sergey static int cxd2841er_sleep_s_to_shutdown(struct cxd2841er_priv *priv)
576a6dc60ffSKozlov Sergey {
577a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
578a6dc60ffSKozlov Sergey 	if (priv->state != STATE_SLEEP_S) {
579a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
580a6dc60ffSKozlov Sergey 			__func__, priv->state);
581a6dc60ffSKozlov Sergey 		return -EINVAL;
582a6dc60ffSKozlov Sergey 	}
583a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
584a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
585a6dc60ffSKozlov Sergey 	/* Disable DSQOUT */
586a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
587a6dc60ffSKozlov Sergey 	/* Disable DSQIN */
588a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x9c, 0x00);
589a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
590a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
591a6dc60ffSKozlov Sergey 	/* Disable oscillator */
592a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
593a6dc60ffSKozlov Sergey 	/* Set demod mode */
594a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
595a6dc60ffSKozlov Sergey 	priv->state = STATE_SHUTDOWN;
596a6dc60ffSKozlov Sergey 	return 0;
597a6dc60ffSKozlov Sergey }
598a6dc60ffSKozlov Sergey 
599a6dc60ffSKozlov Sergey static int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv)
600a6dc60ffSKozlov Sergey {
601a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
602a6dc60ffSKozlov Sergey 	if (priv->state != STATE_SLEEP_TC) {
603a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
604a6dc60ffSKozlov Sergey 			__func__, priv->state);
605a6dc60ffSKozlov Sergey 		return -EINVAL;
606a6dc60ffSKozlov Sergey 	}
607a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
608a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
609a6dc60ffSKozlov Sergey 	/* Disable oscillator */
610a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
611a6dc60ffSKozlov Sergey 	/* Set demod mode */
612a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
613a6dc60ffSKozlov Sergey 	priv->state = STATE_SHUTDOWN;
614a6dc60ffSKozlov Sergey 	return 0;
615a6dc60ffSKozlov Sergey }
616a6dc60ffSKozlov Sergey 
617a6dc60ffSKozlov Sergey static int cxd2841er_active_t_to_sleep_tc(struct cxd2841er_priv *priv)
618a6dc60ffSKozlov Sergey {
619a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
620a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC) {
621a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
622a6dc60ffSKozlov Sergey 			__func__, priv->state);
623a6dc60ffSKozlov Sergey 		return -EINVAL;
624a6dc60ffSKozlov Sergey 	}
625a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
626a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
627a6dc60ffSKozlov Sergey 	/* disable TS output */
628a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
629a6dc60ffSKozlov Sergey 	/* enable Hi-Z setting 1 */
630a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
631a6dc60ffSKozlov Sergey 	/* enable Hi-Z setting 2 */
632a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
633a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
634a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
635a6dc60ffSKozlov Sergey 	/* disable ADC 1 */
636a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
637a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
638a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
639a6dc60ffSKozlov Sergey 	/* Disable ADC 2 */
640a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
641a6dc60ffSKozlov Sergey 	/* Disable ADC 3 */
642a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
643a6dc60ffSKozlov Sergey 	/* Disable ADC clock */
644a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
645a6dc60ffSKozlov Sergey 	/* Disable RF level monitor */
646a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
647a6dc60ffSKozlov Sergey 	/* Disable demod clock */
648a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
649a6dc60ffSKozlov Sergey 	priv->state = STATE_SLEEP_TC;
650a6dc60ffSKozlov Sergey 	return 0;
651a6dc60ffSKozlov Sergey }
652a6dc60ffSKozlov Sergey 
653a6dc60ffSKozlov Sergey static int cxd2841er_active_t2_to_sleep_tc(struct cxd2841er_priv *priv)
654a6dc60ffSKozlov Sergey {
655a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
656a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC) {
657a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
658a6dc60ffSKozlov Sergey 			__func__, priv->state);
659a6dc60ffSKozlov Sergey 		return -EINVAL;
660a6dc60ffSKozlov Sergey 	}
661a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
662a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
663a6dc60ffSKozlov Sergey 	/* disable TS output */
664a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
665a6dc60ffSKozlov Sergey 	/* enable Hi-Z setting 1 */
666a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
667a6dc60ffSKozlov Sergey 	/* enable Hi-Z setting 2 */
668a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
669a6dc60ffSKozlov Sergey 	/* Cancel DVB-T2 setting */
670a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
671a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x40);
672a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x21);
673a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
674a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xfb);
675a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
676a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x00, 0x0f);
677a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
678a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x00, 0x3f);
679a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
680a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
681a6dc60ffSKozlov Sergey 	/* disable ADC 1 */
682a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
683a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
684a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
685a6dc60ffSKozlov Sergey 	/* Disable ADC 2 */
686a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
687a6dc60ffSKozlov Sergey 	/* Disable ADC 3 */
688a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
689a6dc60ffSKozlov Sergey 	/* Disable ADC clock */
690a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
691a6dc60ffSKozlov Sergey 	/* Disable RF level monitor */
692a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
693a6dc60ffSKozlov Sergey 	/* Disable demod clock */
694a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
695a6dc60ffSKozlov Sergey 	priv->state = STATE_SLEEP_TC;
696a6dc60ffSKozlov Sergey 	return 0;
697a6dc60ffSKozlov Sergey }
698a6dc60ffSKozlov Sergey 
699a6dc60ffSKozlov Sergey static int cxd2841er_active_c_to_sleep_tc(struct cxd2841er_priv *priv)
700a6dc60ffSKozlov Sergey {
701a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
702a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC) {
703a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
704a6dc60ffSKozlov Sergey 			__func__, priv->state);
705a6dc60ffSKozlov Sergey 		return -EINVAL;
706a6dc60ffSKozlov Sergey 	}
707a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
708a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
709a6dc60ffSKozlov Sergey 	/* disable TS output */
710a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
711a6dc60ffSKozlov Sergey 	/* enable Hi-Z setting 1 */
712a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
713a6dc60ffSKozlov Sergey 	/* enable Hi-Z setting 2 */
714a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
715a6dc60ffSKozlov Sergey 	/* Cancel DVB-C setting */
716a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
717a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
718a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
719a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
720a6dc60ffSKozlov Sergey 	/* disable ADC 1 */
721a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
722a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
723a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
724a6dc60ffSKozlov Sergey 	/* Disable ADC 2 */
725a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
726a6dc60ffSKozlov Sergey 	/* Disable ADC 3 */
727a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
728a6dc60ffSKozlov Sergey 	/* Disable ADC clock */
729a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
730a6dc60ffSKozlov Sergey 	/* Disable RF level monitor */
731a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
732a6dc60ffSKozlov Sergey 	/* Disable demod clock */
733a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
734a6dc60ffSKozlov Sergey 	priv->state = STATE_SLEEP_TC;
735a6dc60ffSKozlov Sergey 	return 0;
736a6dc60ffSKozlov Sergey }
737a6dc60ffSKozlov Sergey 
73883808c23SAbylay Ospan static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv)
73983808c23SAbylay Ospan {
74083808c23SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
74183808c23SAbylay Ospan 	if (priv->state != STATE_ACTIVE_TC) {
74283808c23SAbylay Ospan 		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
74383808c23SAbylay Ospan 				__func__, priv->state);
74483808c23SAbylay Ospan 		return -EINVAL;
74583808c23SAbylay Ospan 	}
74683808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x00 */
74783808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
74883808c23SAbylay Ospan 	/* disable TS output */
74983808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
75083808c23SAbylay Ospan 	/* enable Hi-Z setting 1 */
75183808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
75283808c23SAbylay Ospan 	/* enable Hi-Z setting 2 */
75383808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
75483808c23SAbylay Ospan 
75583808c23SAbylay Ospan 	/* TODO: Cancel demod parameter */
75683808c23SAbylay Ospan 
75783808c23SAbylay Ospan 	/* Set SLV-X Bank : 0x00 */
75883808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
75983808c23SAbylay Ospan 	/* disable ADC 1 */
76083808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
76183808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x00 */
76283808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
76383808c23SAbylay Ospan 	/* Disable ADC 2 */
76483808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
76583808c23SAbylay Ospan 	/* Disable ADC 3 */
76683808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
76783808c23SAbylay Ospan 	/* Disable ADC clock */
76883808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
76983808c23SAbylay Ospan 	/* Disable RF level monitor */
77083808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
77183808c23SAbylay Ospan 	/* Disable demod clock */
77283808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
77383808c23SAbylay Ospan 	priv->state = STATE_SLEEP_TC;
77483808c23SAbylay Ospan 	return 0;
77583808c23SAbylay Ospan }
77683808c23SAbylay Ospan 
777a6dc60ffSKozlov Sergey static int cxd2841er_shutdown_to_sleep_s(struct cxd2841er_priv *priv)
778a6dc60ffSKozlov Sergey {
779a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
780a6dc60ffSKozlov Sergey 	if (priv->state != STATE_SHUTDOWN) {
781a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
782a6dc60ffSKozlov Sergey 			__func__, priv->state);
783a6dc60ffSKozlov Sergey 		return -EINVAL;
784a6dc60ffSKozlov Sergey 	}
785a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
786a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
787a6dc60ffSKozlov Sergey 	/* Clear all demodulator registers */
788a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
789a6dc60ffSKozlov Sergey 	usleep_range(3000, 5000);
790a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
791a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
792a6dc60ffSKozlov Sergey 	/* Set demod SW reset */
793a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
79483808c23SAbylay Ospan 
79583808c23SAbylay Ospan 	switch (priv->xtal) {
79683808c23SAbylay Ospan 	case SONY_XTAL_20500:
797a6dc60ffSKozlov Sergey 		cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x00);
79883808c23SAbylay Ospan 		break;
79983808c23SAbylay Ospan 	case SONY_XTAL_24000:
80083808c23SAbylay Ospan 		/* Select demod frequency */
80183808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
80283808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x03);
80383808c23SAbylay Ospan 		break;
80483808c23SAbylay Ospan 	case SONY_XTAL_41000:
80583808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x01);
80683808c23SAbylay Ospan 		break;
80783808c23SAbylay Ospan 	default:
80883808c23SAbylay Ospan 		dev_dbg(&priv->i2c->dev, "%s(): invalid demod xtal %d\n",
80983808c23SAbylay Ospan 				__func__, priv->xtal);
81083808c23SAbylay Ospan 		return -EINVAL;
81183808c23SAbylay Ospan 	}
81283808c23SAbylay Ospan 
813a6dc60ffSKozlov Sergey 	/* Set demod mode */
814a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x0a);
815a6dc60ffSKozlov Sergey 	/* Clear demod SW reset */
816a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
817a6dc60ffSKozlov Sergey 	usleep_range(1000, 2000);
818a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
819a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
820a6dc60ffSKozlov Sergey 	/* enable DSQOUT */
821a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1F);
822a6dc60ffSKozlov Sergey 	/* enable DSQIN */
823a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x9C, 0x40);
824a6dc60ffSKozlov Sergey 	/* TADC Bias On */
825a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
826a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
827a6dc60ffSKozlov Sergey 	/* SADC Bias On */
828a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
829a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
830a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
831a6dc60ffSKozlov Sergey 	priv->state = STATE_SLEEP_S;
832a6dc60ffSKozlov Sergey 	return 0;
833a6dc60ffSKozlov Sergey }
834a6dc60ffSKozlov Sergey 
835a6dc60ffSKozlov Sergey static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv)
836a6dc60ffSKozlov Sergey {
8376c77161aSAbylay Ospan 	u8 data = 0;
8383f3b48a0SAbylay Ospan 
839a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
840a6dc60ffSKozlov Sergey 	if (priv->state != STATE_SHUTDOWN) {
841a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
842a6dc60ffSKozlov Sergey 			__func__, priv->state);
843a6dc60ffSKozlov Sergey 		return -EINVAL;
844a6dc60ffSKozlov Sergey 	}
845a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
846a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
847a6dc60ffSKozlov Sergey 	/* Clear all demodulator registers */
848a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
849a6dc60ffSKozlov Sergey 	usleep_range(3000, 5000);
850a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
851a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
852a6dc60ffSKozlov Sergey 	/* Set demod SW reset */
853a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
8546c77161aSAbylay Ospan   /* Select ADC clock mode */
855a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x13, 0x00);
8566c77161aSAbylay Ospan 
8576c77161aSAbylay Ospan 	switch (priv->xtal) {
8586c77161aSAbylay Ospan 	case SONY_XTAL_20500:
8596c77161aSAbylay Ospan 		data = 0x0;
8606c77161aSAbylay Ospan 		break;
8616c77161aSAbylay Ospan 	case SONY_XTAL_24000:
8626c77161aSAbylay Ospan 		/* Select demod frequency */
8636c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
8646c77161aSAbylay Ospan 		data = 0x3;
8656c77161aSAbylay Ospan 		break;
8666c77161aSAbylay Ospan 	case SONY_XTAL_41000:
8676c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
8686c77161aSAbylay Ospan 		data = 0x1;
8696c77161aSAbylay Ospan 		break;
8706c77161aSAbylay Ospan 	}
8716c77161aSAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVX, 0x14, data);
872a6dc60ffSKozlov Sergey 	/* Clear demod SW reset */
873a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
874a6dc60ffSKozlov Sergey 	usleep_range(1000, 2000);
875a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
876a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
877a6dc60ffSKozlov Sergey 	/* TADC Bias On */
878a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
879a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
880a6dc60ffSKozlov Sergey 	/* SADC Bias On */
881a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
882a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
883a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
884a6dc60ffSKozlov Sergey 	priv->state = STATE_SLEEP_TC;
885a6dc60ffSKozlov Sergey 	return 0;
886a6dc60ffSKozlov Sergey }
887a6dc60ffSKozlov Sergey 
888a6dc60ffSKozlov Sergey static int cxd2841er_tune_done(struct cxd2841er_priv *priv)
889a6dc60ffSKozlov Sergey {
890a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
891a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
892a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0, 0);
893a6dc60ffSKozlov Sergey 	/* SW Reset */
894a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0xfe, 0x01);
895a6dc60ffSKozlov Sergey 	/* Enable TS output */
896a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x00);
897a6dc60ffSKozlov Sergey 	return 0;
898a6dc60ffSKozlov Sergey }
899a6dc60ffSKozlov Sergey 
900a6dc60ffSKozlov Sergey /* Set TS parallel mode */
901a6dc60ffSKozlov Sergey static void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
902a6dc60ffSKozlov Sergey 					u8 system)
903a6dc60ffSKozlov Sergey {
904a6dc60ffSKozlov Sergey 	u8 serial_ts, ts_rate_ctrl_off, ts_in_off;
905a6dc60ffSKozlov Sergey 
906a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
907a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
908a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
909a6dc60ffSKozlov Sergey 	cxd2841er_read_reg(priv, I2C_SLVT, 0xc4, &serial_ts);
910a6dc60ffSKozlov Sergey 	cxd2841er_read_reg(priv, I2C_SLVT, 0xd3, &ts_rate_ctrl_off);
911a6dc60ffSKozlov Sergey 	cxd2841er_read_reg(priv, I2C_SLVT, 0xde, &ts_in_off);
912a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s(): ser_ts=0x%02x rate_ctrl_off=0x%02x in_off=0x%02x\n",
913a6dc60ffSKozlov Sergey 		__func__, serial_ts, ts_rate_ctrl_off, ts_in_off);
914a6dc60ffSKozlov Sergey 
915a6dc60ffSKozlov Sergey 	/*
916a6dc60ffSKozlov Sergey 	 * slave    Bank    Addr    Bit    default    Name
91703ab1bd5SDaniel Scheller 	 * <SLV-T>  00h     C4h     [1:0]  2'b??      OSERCKMODE
91803ab1bd5SDaniel Scheller 	 */
91903ab1bd5SDaniel Scheller 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4,
92003ab1bd5SDaniel Scheller 		((priv->flags & CXD2841ER_TS_SERIAL) ? 0x01 : 0x00), 0x03);
92103ab1bd5SDaniel Scheller 	/*
92203ab1bd5SDaniel Scheller 	 * slave    Bank    Addr    Bit    default    Name
92303ab1bd5SDaniel Scheller 	 * <SLV-T>  00h     D1h     [1:0]  2'b??      OSERDUTYMODE
92403ab1bd5SDaniel Scheller 	 */
92503ab1bd5SDaniel Scheller 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd1,
92603ab1bd5SDaniel Scheller 		((priv->flags & CXD2841ER_TS_SERIAL) ? 0x01 : 0x00), 0x03);
92703ab1bd5SDaniel Scheller 	/*
92803ab1bd5SDaniel Scheller 	 * slave    Bank    Addr    Bit    default    Name
929a6dc60ffSKozlov Sergey 	 * <SLV-T>  00h     D9h     [7:0]  8'h08      OTSCKPERIOD
930a6dc60ffSKozlov Sergey 	 */
931a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0xd9, 0x08);
932a6dc60ffSKozlov Sergey 	/*
933a6dc60ffSKozlov Sergey 	 * Disable TS IF Clock
934a6dc60ffSKozlov Sergey 	 * slave    Bank    Addr    Bit    default    Name
935a6dc60ffSKozlov Sergey 	 * <SLV-T>  00h     32h     [0]    1'b1       OREG_CK_TSIF_EN
936a6dc60ffSKozlov Sergey 	 */
937a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x00, 0x01);
938a6dc60ffSKozlov Sergey 	/*
939a6dc60ffSKozlov Sergey 	 * slave    Bank    Addr    Bit    default    Name
940a6dc60ffSKozlov Sergey 	 * <SLV-T>  00h     33h     [1:0]  2'b01      OREG_CKSEL_TSIF
941a6dc60ffSKozlov Sergey 	 */
94203ab1bd5SDaniel Scheller 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x33,
94303ab1bd5SDaniel Scheller 		((priv->flags & CXD2841ER_TS_SERIAL) ? 0x01 : 0x00), 0x03);
944a6dc60ffSKozlov Sergey 	/*
945a6dc60ffSKozlov Sergey 	 * Enable TS IF Clock
946a6dc60ffSKozlov Sergey 	 * slave    Bank    Addr    Bit    default    Name
947a6dc60ffSKozlov Sergey 	 * <SLV-T>  00h     32h     [0]    1'b1       OREG_CK_TSIF_EN
948a6dc60ffSKozlov Sergey 	 */
949a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x01, 0x01);
950a6dc60ffSKozlov Sergey 
951a6dc60ffSKozlov Sergey 	if (system == SYS_DVBT) {
952a6dc60ffSKozlov Sergey 		/* Enable parity period for DVB-T */
953a6dc60ffSKozlov Sergey 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
954a6dc60ffSKozlov Sergey 		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
955a6dc60ffSKozlov Sergey 	} else if (system == SYS_DVBC_ANNEX_A) {
956a6dc60ffSKozlov Sergey 		/* Enable parity period for DVB-C */
957a6dc60ffSKozlov Sergey 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
958a6dc60ffSKozlov Sergey 		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
959a6dc60ffSKozlov Sergey 	}
960a6dc60ffSKozlov Sergey }
961a6dc60ffSKozlov Sergey 
962a6dc60ffSKozlov Sergey static u8 cxd2841er_chip_id(struct cxd2841er_priv *priv)
963a6dc60ffSKozlov Sergey {
96483808c23SAbylay Ospan 	u8 chip_id = 0;
965a6dc60ffSKozlov Sergey 
966a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
96783808c23SAbylay Ospan 	if (cxd2841er_write_reg(priv, I2C_SLVT, 0, 0) == 0)
968a6dc60ffSKozlov Sergey 		cxd2841er_read_reg(priv, I2C_SLVT, 0xfd, &chip_id);
96983808c23SAbylay Ospan 	else if (cxd2841er_write_reg(priv, I2C_SLVX, 0, 0) == 0)
97083808c23SAbylay Ospan 		cxd2841er_read_reg(priv, I2C_SLVX, 0xfd, &chip_id);
97183808c23SAbylay Ospan 
972a6dc60ffSKozlov Sergey 	return chip_id;
973a6dc60ffSKozlov Sergey }
974a6dc60ffSKozlov Sergey 
975a6dc60ffSKozlov Sergey static int cxd2841er_read_status_s(struct dvb_frontend *fe,
976a6dc60ffSKozlov Sergey 				   enum fe_status *status)
977a6dc60ffSKozlov Sergey {
978a6dc60ffSKozlov Sergey 	u8 reg = 0;
979a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
980a6dc60ffSKozlov Sergey 
981a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
982a6dc60ffSKozlov Sergey 	*status = 0;
983a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_S) {
984a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
985a6dc60ffSKozlov Sergey 			__func__, priv->state);
986a6dc60ffSKozlov Sergey 		return -EINVAL;
987a6dc60ffSKozlov Sergey 	}
988a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0xA0 */
989a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
990a6dc60ffSKozlov Sergey 	/*
991a6dc60ffSKozlov Sergey 	 *  slave     Bank      Addr      Bit      Signal name
992a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       11h       [2]      ITSLOCK
993a6dc60ffSKozlov Sergey 	 */
994a6dc60ffSKozlov Sergey 	cxd2841er_read_reg(priv, I2C_SLVT, 0x11, &reg);
995a6dc60ffSKozlov Sergey 	if (reg & 0x04) {
996a6dc60ffSKozlov Sergey 		*status = FE_HAS_SIGNAL
997a6dc60ffSKozlov Sergey 			| FE_HAS_CARRIER
998a6dc60ffSKozlov Sergey 			| FE_HAS_VITERBI
999a6dc60ffSKozlov Sergey 			| FE_HAS_SYNC
1000a6dc60ffSKozlov Sergey 			| FE_HAS_LOCK;
1001a6dc60ffSKozlov Sergey 	}
1002a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s(): result 0x%x\n", __func__, *status);
1003a6dc60ffSKozlov Sergey 	return 0;
1004a6dc60ffSKozlov Sergey }
1005a6dc60ffSKozlov Sergey 
1006a6dc60ffSKozlov Sergey static int cxd2841er_read_status_t_t2(struct cxd2841er_priv *priv,
1007a6dc60ffSKozlov Sergey 				      u8 *sync, u8 *tslock, u8 *unlock)
1008a6dc60ffSKozlov Sergey {
1009a6dc60ffSKozlov Sergey 	u8 data = 0;
1010a6dc60ffSKozlov Sergey 
1011a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1012a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC)
1013a6dc60ffSKozlov Sergey 		return -EINVAL;
1014a6dc60ffSKozlov Sergey 	if (priv->system == SYS_DVBT) {
1015a6dc60ffSKozlov Sergey 		/* Set SLV-T Bank : 0x10 */
1016a6dc60ffSKozlov Sergey 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1017a6dc60ffSKozlov Sergey 	} else {
1018a6dc60ffSKozlov Sergey 		/* Set SLV-T Bank : 0x20 */
1019a6dc60ffSKozlov Sergey 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1020a6dc60ffSKozlov Sergey 	}
1021a6dc60ffSKozlov Sergey 	cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
1022a6dc60ffSKozlov Sergey 	if ((data & 0x07) == 0x07) {
1023a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1024a6dc60ffSKozlov Sergey 			"%s(): invalid hardware state detected\n", __func__);
1025a6dc60ffSKozlov Sergey 		*sync = 0;
1026a6dc60ffSKozlov Sergey 		*tslock = 0;
1027a6dc60ffSKozlov Sergey 		*unlock = 0;
1028a6dc60ffSKozlov Sergey 	} else {
1029a6dc60ffSKozlov Sergey 		*sync = ((data & 0x07) == 0x6 ? 1 : 0);
1030a6dc60ffSKozlov Sergey 		*tslock = ((data & 0x20) ? 1 : 0);
1031a6dc60ffSKozlov Sergey 		*unlock = ((data & 0x10) ? 1 : 0);
1032a6dc60ffSKozlov Sergey 	}
1033a6dc60ffSKozlov Sergey 	return 0;
1034a6dc60ffSKozlov Sergey }
1035a6dc60ffSKozlov Sergey 
1036a6dc60ffSKozlov Sergey static int cxd2841er_read_status_c(struct cxd2841er_priv *priv, u8 *tslock)
1037a6dc60ffSKozlov Sergey {
1038a6dc60ffSKozlov Sergey 	u8 data;
1039a6dc60ffSKozlov Sergey 
1040a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1041a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC)
1042a6dc60ffSKozlov Sergey 		return -EINVAL;
1043a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1044a6dc60ffSKozlov Sergey 	cxd2841er_read_reg(priv, I2C_SLVT, 0x88, &data);
1045a6dc60ffSKozlov Sergey 	if ((data & 0x01) == 0) {
1046a6dc60ffSKozlov Sergey 		*tslock = 0;
1047a6dc60ffSKozlov Sergey 	} else {
1048a6dc60ffSKozlov Sergey 		cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
1049a6dc60ffSKozlov Sergey 		*tslock = ((data & 0x20) ? 1 : 0);
1050a6dc60ffSKozlov Sergey 	}
1051a6dc60ffSKozlov Sergey 	return 0;
1052a6dc60ffSKozlov Sergey }
1053a6dc60ffSKozlov Sergey 
105483808c23SAbylay Ospan static int cxd2841er_read_status_i(struct cxd2841er_priv *priv,
105583808c23SAbylay Ospan 		u8 *sync, u8 *tslock, u8 *unlock)
105683808c23SAbylay Ospan {
105783808c23SAbylay Ospan 	u8 data = 0;
105883808c23SAbylay Ospan 
105983808c23SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
106083808c23SAbylay Ospan 	if (priv->state != STATE_ACTIVE_TC)
106183808c23SAbylay Ospan 		return -EINVAL;
106283808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x60 */
106383808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
106483808c23SAbylay Ospan 	cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
106583808c23SAbylay Ospan 	dev_dbg(&priv->i2c->dev,
106683808c23SAbylay Ospan 			"%s(): lock=0x%x\n", __func__, data);
106783808c23SAbylay Ospan 	*sync = ((data & 0x02) ? 1 : 0);
106883808c23SAbylay Ospan 	*tslock = ((data & 0x01) ? 1 : 0);
106983808c23SAbylay Ospan 	*unlock = ((data & 0x10) ? 1 : 0);
107083808c23SAbylay Ospan 	return 0;
107183808c23SAbylay Ospan }
107283808c23SAbylay Ospan 
1073a6dc60ffSKozlov Sergey static int cxd2841er_read_status_tc(struct dvb_frontend *fe,
1074a6dc60ffSKozlov Sergey 				    enum fe_status *status)
1075a6dc60ffSKozlov Sergey {
1076a6dc60ffSKozlov Sergey 	int ret = 0;
1077a6dc60ffSKozlov Sergey 	u8 sync = 0;
1078a6dc60ffSKozlov Sergey 	u8 tslock = 0;
1079a6dc60ffSKozlov Sergey 	u8 unlock = 0;
1080a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
1081a6dc60ffSKozlov Sergey 
1082a6dc60ffSKozlov Sergey 	*status = 0;
1083a6dc60ffSKozlov Sergey 	if (priv->state == STATE_ACTIVE_TC) {
1084a6dc60ffSKozlov Sergey 		if (priv->system == SYS_DVBT || priv->system == SYS_DVBT2) {
1085a6dc60ffSKozlov Sergey 			ret = cxd2841er_read_status_t_t2(
1086a6dc60ffSKozlov Sergey 				priv, &sync, &tslock, &unlock);
1087a6dc60ffSKozlov Sergey 			if (ret)
1088a6dc60ffSKozlov Sergey 				goto done;
1089a6dc60ffSKozlov Sergey 			if (unlock)
1090a6dc60ffSKozlov Sergey 				goto done;
1091a6dc60ffSKozlov Sergey 			if (sync)
1092a6dc60ffSKozlov Sergey 				*status = FE_HAS_SIGNAL |
1093a6dc60ffSKozlov Sergey 					FE_HAS_CARRIER |
1094a6dc60ffSKozlov Sergey 					FE_HAS_VITERBI |
1095a6dc60ffSKozlov Sergey 					FE_HAS_SYNC;
1096a6dc60ffSKozlov Sergey 			if (tslock)
1097a6dc60ffSKozlov Sergey 				*status |= FE_HAS_LOCK;
109883808c23SAbylay Ospan 		} else if (priv->system == SYS_ISDBT) {
109983808c23SAbylay Ospan 			ret = cxd2841er_read_status_i(
110083808c23SAbylay Ospan 					priv, &sync, &tslock, &unlock);
110183808c23SAbylay Ospan 			if (ret)
110283808c23SAbylay Ospan 				goto done;
110383808c23SAbylay Ospan 			if (unlock)
110483808c23SAbylay Ospan 				goto done;
110583808c23SAbylay Ospan 			if (sync)
110683808c23SAbylay Ospan 				*status = FE_HAS_SIGNAL |
110783808c23SAbylay Ospan 					FE_HAS_CARRIER |
110883808c23SAbylay Ospan 					FE_HAS_VITERBI |
110983808c23SAbylay Ospan 					FE_HAS_SYNC;
111083808c23SAbylay Ospan 			if (tslock)
111183808c23SAbylay Ospan 				*status |= FE_HAS_LOCK;
1112a6dc60ffSKozlov Sergey 		} else if (priv->system == SYS_DVBC_ANNEX_A) {
1113a6dc60ffSKozlov Sergey 			ret = cxd2841er_read_status_c(priv, &tslock);
1114a6dc60ffSKozlov Sergey 			if (ret)
1115a6dc60ffSKozlov Sergey 				goto done;
1116a6dc60ffSKozlov Sergey 			if (tslock)
1117a6dc60ffSKozlov Sergey 				*status = FE_HAS_SIGNAL |
1118a6dc60ffSKozlov Sergey 					FE_HAS_CARRIER |
1119a6dc60ffSKozlov Sergey 					FE_HAS_VITERBI |
1120a6dc60ffSKozlov Sergey 					FE_HAS_SYNC |
1121a6dc60ffSKozlov Sergey 					FE_HAS_LOCK;
1122a6dc60ffSKozlov Sergey 		}
1123a6dc60ffSKozlov Sergey 	}
1124a6dc60ffSKozlov Sergey done:
1125a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s(): status 0x%x\n", __func__, *status);
1126a6dc60ffSKozlov Sergey 	return ret;
1127a6dc60ffSKozlov Sergey }
1128a6dc60ffSKozlov Sergey 
1129a6dc60ffSKozlov Sergey static int cxd2841er_get_carrier_offset_s_s2(struct cxd2841er_priv *priv,
1130a6dc60ffSKozlov Sergey 					     int *offset)
1131a6dc60ffSKozlov Sergey {
1132a6dc60ffSKozlov Sergey 	u8 data[3];
1133a6dc60ffSKozlov Sergey 	u8 is_hs_mode;
1134a6dc60ffSKozlov Sergey 	s32 cfrl_ctrlval;
1135a6dc60ffSKozlov Sergey 	s32 temp_div, temp_q, temp_r;
1136a6dc60ffSKozlov Sergey 
1137a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_S) {
1138a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1139a6dc60ffSKozlov Sergey 			__func__, priv->state);
1140a6dc60ffSKozlov Sergey 		return -EINVAL;
1141a6dc60ffSKozlov Sergey 	}
1142a6dc60ffSKozlov Sergey 	/*
1143a6dc60ffSKozlov Sergey 	 * Get High Sampling Rate mode
1144a6dc60ffSKozlov Sergey 	 *  slave     Bank      Addr      Bit      Signal name
1145a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       10h       [0]      ITRL_LOCK
1146a6dc60ffSKozlov Sergey 	 */
1147a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1148a6dc60ffSKozlov Sergey 	cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data[0]);
1149a6dc60ffSKozlov Sergey 	if (data[0] & 0x01) {
1150a6dc60ffSKozlov Sergey 		/*
1151a6dc60ffSKozlov Sergey 		 *  slave     Bank      Addr      Bit      Signal name
1152a6dc60ffSKozlov Sergey 		 * <SLV-T>    A0h       50h       [4]      IHSMODE
1153a6dc60ffSKozlov Sergey 		 */
1154a6dc60ffSKozlov Sergey 		cxd2841er_read_reg(priv, I2C_SLVT, 0x50, &data[0]);
1155a6dc60ffSKozlov Sergey 		is_hs_mode = (data[0] & 0x10 ? 1 : 0);
1156a6dc60ffSKozlov Sergey 	} else {
1157a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1158a6dc60ffSKozlov Sergey 			"%s(): unable to detect sampling rate mode\n",
1159a6dc60ffSKozlov Sergey 			__func__);
1160a6dc60ffSKozlov Sergey 		return -EINVAL;
1161a6dc60ffSKozlov Sergey 	}
1162a6dc60ffSKozlov Sergey 	/*
1163a6dc60ffSKozlov Sergey 	 *  slave     Bank      Addr      Bit      Signal name
1164a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       45h       [4:0]    ICFRL_CTRLVAL[20:16]
1165a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       46h       [7:0]    ICFRL_CTRLVAL[15:8]
1166a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       47h       [7:0]    ICFRL_CTRLVAL[7:0]
1167a6dc60ffSKozlov Sergey 	 */
1168a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x45, data, 3);
1169a6dc60ffSKozlov Sergey 	cfrl_ctrlval = sign_extend32((((u32)data[0] & 0x1F) << 16) |
1170a6dc60ffSKozlov Sergey 				(((u32)data[1] & 0xFF) <<  8) |
1171a6dc60ffSKozlov Sergey 				((u32)data[2] & 0xFF), 20);
1172a6dc60ffSKozlov Sergey 	temp_div = (is_hs_mode ? 1048576 : 1572864);
1173a6dc60ffSKozlov Sergey 	if (cfrl_ctrlval > 0) {
1174a6dc60ffSKozlov Sergey 		temp_q = div_s64_rem(97375LL * cfrl_ctrlval,
1175a6dc60ffSKozlov Sergey 			temp_div, &temp_r);
1176a6dc60ffSKozlov Sergey 	} else {
1177a6dc60ffSKozlov Sergey 		temp_q = div_s64_rem(-97375LL * cfrl_ctrlval,
1178a6dc60ffSKozlov Sergey 			temp_div, &temp_r);
1179a6dc60ffSKozlov Sergey 	}
1180a6dc60ffSKozlov Sergey 	if (temp_r >= temp_div / 2)
1181a6dc60ffSKozlov Sergey 		temp_q++;
1182a6dc60ffSKozlov Sergey 	if (cfrl_ctrlval > 0)
1183a6dc60ffSKozlov Sergey 		temp_q *= -1;
1184a6dc60ffSKozlov Sergey 	*offset = temp_q;
1185a6dc60ffSKozlov Sergey 	return 0;
1186a6dc60ffSKozlov Sergey }
1187a6dc60ffSKozlov Sergey 
118876344a3fSMauro Carvalho Chehab static int cxd2841er_get_carrier_offset_i(struct cxd2841er_priv *priv,
118976344a3fSMauro Carvalho Chehab 					   u32 bandwidth, int *offset)
119076344a3fSMauro Carvalho Chehab {
119176344a3fSMauro Carvalho Chehab 	u8 data[4];
119276344a3fSMauro Carvalho Chehab 
119376344a3fSMauro Carvalho Chehab 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
119476344a3fSMauro Carvalho Chehab 	if (priv->state != STATE_ACTIVE_TC) {
119576344a3fSMauro Carvalho Chehab 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
119676344a3fSMauro Carvalho Chehab 			__func__, priv->state);
119776344a3fSMauro Carvalho Chehab 		return -EINVAL;
119876344a3fSMauro Carvalho Chehab 	}
119976344a3fSMauro Carvalho Chehab 	if (priv->system != SYS_ISDBT) {
120076344a3fSMauro Carvalho Chehab 		dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
120176344a3fSMauro Carvalho Chehab 			__func__, priv->system);
120276344a3fSMauro Carvalho Chehab 		return -EINVAL;
120376344a3fSMauro Carvalho Chehab 	}
120476344a3fSMauro Carvalho Chehab 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
120576344a3fSMauro Carvalho Chehab 	cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
120676344a3fSMauro Carvalho Chehab 	*offset = -1 * sign_extend32(
120776344a3fSMauro Carvalho Chehab 		((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
120876344a3fSMauro Carvalho Chehab 		((u32)data[2] << 8) | (u32)data[3], 29);
120976344a3fSMauro Carvalho Chehab 
121076344a3fSMauro Carvalho Chehab 	switch (bandwidth) {
121176344a3fSMauro Carvalho Chehab 	case 6000000:
121276344a3fSMauro Carvalho Chehab 		*offset = -1 * ((*offset) * 8/264);
121376344a3fSMauro Carvalho Chehab 		break;
121476344a3fSMauro Carvalho Chehab 	case 7000000:
121576344a3fSMauro Carvalho Chehab 		*offset = -1 * ((*offset) * 8/231);
121676344a3fSMauro Carvalho Chehab 		break;
121776344a3fSMauro Carvalho Chehab 	case 8000000:
121876344a3fSMauro Carvalho Chehab 		*offset = -1 * ((*offset) * 8/198);
121976344a3fSMauro Carvalho Chehab 		break;
122076344a3fSMauro Carvalho Chehab 	default:
122176344a3fSMauro Carvalho Chehab 		dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
122276344a3fSMauro Carvalho Chehab 				__func__, bandwidth);
122376344a3fSMauro Carvalho Chehab 		return -EINVAL;
122476344a3fSMauro Carvalho Chehab 	}
122576344a3fSMauro Carvalho Chehab 
122676344a3fSMauro Carvalho Chehab 	dev_dbg(&priv->i2c->dev, "%s(): bandwidth %d offset %d\n",
122776344a3fSMauro Carvalho Chehab 			__func__, bandwidth, *offset);
122876344a3fSMauro Carvalho Chehab 
122976344a3fSMauro Carvalho Chehab 	return 0;
123076344a3fSMauro Carvalho Chehab }
123176344a3fSMauro Carvalho Chehab 
1232c5ea46daSAbylay Ospan static int cxd2841er_get_carrier_offset_t(struct cxd2841er_priv *priv,
1233c5ea46daSAbylay Ospan 					   u32 bandwidth, int *offset)
1234c5ea46daSAbylay Ospan {
1235c5ea46daSAbylay Ospan 	u8 data[4];
1236c5ea46daSAbylay Ospan 
1237c5ea46daSAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1238c5ea46daSAbylay Ospan 	if (priv->state != STATE_ACTIVE_TC) {
1239c5ea46daSAbylay Ospan 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1240c5ea46daSAbylay Ospan 			__func__, priv->state);
1241c5ea46daSAbylay Ospan 		return -EINVAL;
1242c5ea46daSAbylay Ospan 	}
1243c5ea46daSAbylay Ospan 	if (priv->system != SYS_DVBT) {
1244c5ea46daSAbylay Ospan 		dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1245c5ea46daSAbylay Ospan 			__func__, priv->system);
1246c5ea46daSAbylay Ospan 		return -EINVAL;
1247c5ea46daSAbylay Ospan 	}
1248c5ea46daSAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1249c5ea46daSAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1250c5ea46daSAbylay Ospan 	*offset = -1 * sign_extend32(
1251c5ea46daSAbylay Ospan 		((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
1252c5ea46daSAbylay Ospan 		((u32)data[2] << 8) | (u32)data[3], 29);
12534da093ceSAbylay Ospan 	*offset *= (bandwidth / 1000000);
12544da093ceSAbylay Ospan 	*offset /= 235;
1255c5ea46daSAbylay Ospan 	return 0;
1256c5ea46daSAbylay Ospan }
1257c5ea46daSAbylay Ospan 
1258c8946c8dSMauro Carvalho Chehab static int cxd2841er_get_carrier_offset_t2(struct cxd2841er_priv *priv,
1259c8946c8dSMauro Carvalho Chehab 					   u32 bandwidth, int *offset)
1260a6dc60ffSKozlov Sergey {
1261a6dc60ffSKozlov Sergey 	u8 data[4];
1262a6dc60ffSKozlov Sergey 
1263a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1264a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC) {
1265a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1266a6dc60ffSKozlov Sergey 			__func__, priv->state);
1267a6dc60ffSKozlov Sergey 		return -EINVAL;
1268a6dc60ffSKozlov Sergey 	}
1269a6dc60ffSKozlov Sergey 	if (priv->system != SYS_DVBT2) {
1270a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1271a6dc60ffSKozlov Sergey 			__func__, priv->system);
1272a6dc60ffSKozlov Sergey 		return -EINVAL;
1273a6dc60ffSKozlov Sergey 	}
1274a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1275a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1276a6dc60ffSKozlov Sergey 	*offset = -1 * sign_extend32(
1277a6dc60ffSKozlov Sergey 		((u32)(data[0] & 0x0F) << 24) | ((u32)data[1] << 16) |
1278a6dc60ffSKozlov Sergey 		((u32)data[2] << 8) | (u32)data[3], 27);
1279a6dc60ffSKozlov Sergey 	switch (bandwidth) {
1280a6dc60ffSKozlov Sergey 	case 1712000:
1281a6dc60ffSKozlov Sergey 		*offset /= 582;
1282a6dc60ffSKozlov Sergey 		break;
1283a6dc60ffSKozlov Sergey 	case 5000000:
1284a6dc60ffSKozlov Sergey 	case 6000000:
1285a6dc60ffSKozlov Sergey 	case 7000000:
1286a6dc60ffSKozlov Sergey 	case 8000000:
1287a6dc60ffSKozlov Sergey 		*offset *= (bandwidth / 1000000);
1288a6dc60ffSKozlov Sergey 		*offset /= 940;
1289a6dc60ffSKozlov Sergey 		break;
1290a6dc60ffSKozlov Sergey 	default:
1291a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
1292a6dc60ffSKozlov Sergey 			__func__, bandwidth);
1293a6dc60ffSKozlov Sergey 		return -EINVAL;
1294a6dc60ffSKozlov Sergey 	}
1295a6dc60ffSKozlov Sergey 	return 0;
1296a6dc60ffSKozlov Sergey }
1297a6dc60ffSKozlov Sergey 
1298c8946c8dSMauro Carvalho Chehab static int cxd2841er_get_carrier_offset_c(struct cxd2841er_priv *priv,
1299c8946c8dSMauro Carvalho Chehab 					  int *offset)
1300a6dc60ffSKozlov Sergey {
1301a6dc60ffSKozlov Sergey 	u8 data[2];
1302a6dc60ffSKozlov Sergey 
1303a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1304a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC) {
1305a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1306a6dc60ffSKozlov Sergey 			__func__, priv->state);
1307a6dc60ffSKozlov Sergey 		return -EINVAL;
1308a6dc60ffSKozlov Sergey 	}
1309a6dc60ffSKozlov Sergey 	if (priv->system != SYS_DVBC_ANNEX_A) {
1310a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1311a6dc60ffSKozlov Sergey 			__func__, priv->system);
1312a6dc60ffSKozlov Sergey 		return -EINVAL;
1313a6dc60ffSKozlov Sergey 	}
1314a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1315a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x15, data, sizeof(data));
1316a6dc60ffSKozlov Sergey 	*offset = div_s64(41000LL * sign_extend32((((u32)data[0] & 0x3f) << 8)
1317a6dc60ffSKozlov Sergey 						| (u32)data[1], 13), 16384);
1318a6dc60ffSKozlov Sergey 	return 0;
1319a6dc60ffSKozlov Sergey }
1320a6dc60ffSKozlov Sergey 
1321a6f330cbSAbylay Ospan static int cxd2841er_read_packet_errors_c(
1322a6f330cbSAbylay Ospan 		struct cxd2841er_priv *priv, u32 *penum)
1323a6f330cbSAbylay Ospan {
1324a6f330cbSAbylay Ospan 	u8 data[3];
1325a6f330cbSAbylay Ospan 
1326a6f330cbSAbylay Ospan 	*penum = 0;
1327a6f330cbSAbylay Ospan 	if (priv->state != STATE_ACTIVE_TC) {
1328a6f330cbSAbylay Ospan 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1329a6f330cbSAbylay Ospan 				__func__, priv->state);
1330a6f330cbSAbylay Ospan 		return -EINVAL;
1331a6f330cbSAbylay Ospan 	}
1332a6f330cbSAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1333a6f330cbSAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
1334a6f330cbSAbylay Ospan 	if (data[2] & 0x01)
1335a6f330cbSAbylay Ospan 		*penum = ((u32)data[0] << 8) | (u32)data[1];
1336a6f330cbSAbylay Ospan 	return 0;
1337a6f330cbSAbylay Ospan }
1338a6f330cbSAbylay Ospan 
1339a6dc60ffSKozlov Sergey static int cxd2841er_read_packet_errors_t(
1340a6dc60ffSKozlov Sergey 		struct cxd2841er_priv *priv, u32 *penum)
1341a6dc60ffSKozlov Sergey {
1342a6dc60ffSKozlov Sergey 	u8 data[3];
1343a6dc60ffSKozlov Sergey 
1344a6dc60ffSKozlov Sergey 	*penum = 0;
1345a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC) {
1346a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1347a6dc60ffSKozlov Sergey 			__func__, priv->state);
1348a6dc60ffSKozlov Sergey 		return -EINVAL;
1349a6dc60ffSKozlov Sergey 	}
1350a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1351a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
1352a6dc60ffSKozlov Sergey 	if (data[2] & 0x01)
1353a6dc60ffSKozlov Sergey 		*penum = ((u32)data[0] << 8) | (u32)data[1];
1354a6dc60ffSKozlov Sergey 	return 0;
1355a6dc60ffSKozlov Sergey }
1356a6dc60ffSKozlov Sergey 
1357a6dc60ffSKozlov Sergey static int cxd2841er_read_packet_errors_t2(
1358a6dc60ffSKozlov Sergey 		struct cxd2841er_priv *priv, u32 *penum)
1359a6dc60ffSKozlov Sergey {
1360a6dc60ffSKozlov Sergey 	u8 data[3];
1361a6dc60ffSKozlov Sergey 
1362a6dc60ffSKozlov Sergey 	*penum = 0;
1363a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC) {
1364a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1365a6dc60ffSKozlov Sergey 			__func__, priv->state);
1366a6dc60ffSKozlov Sergey 		return -EINVAL;
1367a6dc60ffSKozlov Sergey 	}
1368a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
1369a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0xfd, data, sizeof(data));
1370a6dc60ffSKozlov Sergey 	if (data[0] & 0x01)
1371a6dc60ffSKozlov Sergey 		*penum = ((u32)data[1] << 8) | (u32)data[2];
1372a6dc60ffSKozlov Sergey 	return 0;
1373a6dc60ffSKozlov Sergey }
1374a6dc60ffSKozlov Sergey 
137583808c23SAbylay Ospan static int cxd2841er_read_packet_errors_i(
137683808c23SAbylay Ospan 		struct cxd2841er_priv *priv, u32 *penum)
137783808c23SAbylay Ospan {
137883808c23SAbylay Ospan 	u8 data[2];
137983808c23SAbylay Ospan 
138083808c23SAbylay Ospan 	*penum = 0;
138183808c23SAbylay Ospan 	if (priv->state != STATE_ACTIVE_TC) {
138283808c23SAbylay Ospan 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
138383808c23SAbylay Ospan 				__func__, priv->state);
138483808c23SAbylay Ospan 		return -EINVAL;
138583808c23SAbylay Ospan 	}
138683808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
138783808c23SAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0xA1, data, 1);
138883808c23SAbylay Ospan 
138983808c23SAbylay Ospan 	if (!(data[0] & 0x01))
139083808c23SAbylay Ospan 		return 0;
139183808c23SAbylay Ospan 
139283808c23SAbylay Ospan 	/* Layer A */
139383808c23SAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0xA2, data, sizeof(data));
139483808c23SAbylay Ospan 	*penum = ((u32)data[0] << 8) | (u32)data[1];
139583808c23SAbylay Ospan 
139683808c23SAbylay Ospan 	/* Layer B */
139783808c23SAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0xA4, data, sizeof(data));
139883808c23SAbylay Ospan 	*penum += ((u32)data[0] << 8) | (u32)data[1];
139983808c23SAbylay Ospan 
140083808c23SAbylay Ospan 	/* Layer C */
140183808c23SAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0xA6, data, sizeof(data));
140283808c23SAbylay Ospan 	*penum += ((u32)data[0] << 8) | (u32)data[1];
140383808c23SAbylay Ospan 
140483808c23SAbylay Ospan 	return 0;
140583808c23SAbylay Ospan }
140683808c23SAbylay Ospan 
1407a6f330cbSAbylay Ospan static int cxd2841er_read_ber_c(struct cxd2841er_priv *priv,
1408a6f330cbSAbylay Ospan 		u32 *bit_error, u32 *bit_count)
1409a6f330cbSAbylay Ospan {
1410a6f330cbSAbylay Ospan 	u8 data[3];
1411a6f330cbSAbylay Ospan 	u32 bit_err, period_exp;
1412a6f330cbSAbylay Ospan 
1413a6f330cbSAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1414a6f330cbSAbylay Ospan 	if (priv->state != STATE_ACTIVE_TC) {
1415a6f330cbSAbylay Ospan 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1416a6f330cbSAbylay Ospan 				__func__, priv->state);
1417a6f330cbSAbylay Ospan 		return -EINVAL;
1418a6f330cbSAbylay Ospan 	}
1419a6f330cbSAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1420a6f330cbSAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0x62, data, sizeof(data));
1421a6f330cbSAbylay Ospan 	if (!(data[0] & 0x80)) {
1422a6f330cbSAbylay Ospan 		dev_dbg(&priv->i2c->dev,
1423a6f330cbSAbylay Ospan 				"%s(): no valid BER data\n", __func__);
1424a6f330cbSAbylay Ospan 		return -EINVAL;
1425a6f330cbSAbylay Ospan 	}
1426a6f330cbSAbylay Ospan 	bit_err = ((u32)(data[0] & 0x3f) << 16) |
1427a6f330cbSAbylay Ospan 		((u32)data[1] << 8) |
1428a6f330cbSAbylay Ospan 		(u32)data[2];
1429a6f330cbSAbylay Ospan 	cxd2841er_read_reg(priv, I2C_SLVT, 0x60, data);
1430a6f330cbSAbylay Ospan 	period_exp = data[0] & 0x1f;
1431a6f330cbSAbylay Ospan 
1432a6f330cbSAbylay Ospan 	if ((period_exp <= 11) && (bit_err > (1 << period_exp) * 204 * 8)) {
1433a6f330cbSAbylay Ospan 		dev_dbg(&priv->i2c->dev,
1434a6f330cbSAbylay Ospan 				"%s(): period_exp(%u) or bit_err(%u)  not in range. no valid BER data\n",
1435a6f330cbSAbylay Ospan 				__func__, period_exp, bit_err);
1436a6f330cbSAbylay Ospan 		return -EINVAL;
1437a6f330cbSAbylay Ospan 	}
1438a6f330cbSAbylay Ospan 
1439a6f330cbSAbylay Ospan 	dev_dbg(&priv->i2c->dev,
1440a6f330cbSAbylay Ospan 			"%s(): period_exp(%u) or bit_err(%u) count=%d\n",
1441a6f330cbSAbylay Ospan 			__func__, period_exp, bit_err,
1442a6f330cbSAbylay Ospan 			((1 << period_exp) * 204 * 8));
1443a6f330cbSAbylay Ospan 
1444a6f330cbSAbylay Ospan 	*bit_error = bit_err;
1445a6f330cbSAbylay Ospan 	*bit_count = ((1 << period_exp) * 204 * 8);
1446a6f330cbSAbylay Ospan 
1447a6f330cbSAbylay Ospan 	return 0;
1448a6f330cbSAbylay Ospan }
1449a6f330cbSAbylay Ospan 
14500854df79SAbylay Ospan static int cxd2841er_read_ber_i(struct cxd2841er_priv *priv,
14510854df79SAbylay Ospan 		u32 *bit_error, u32 *bit_count)
14520854df79SAbylay Ospan {
14530854df79SAbylay Ospan 	u8 data[3];
14540854df79SAbylay Ospan 	u8 pktnum[2];
14550854df79SAbylay Ospan 
14560854df79SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
14570854df79SAbylay Ospan 	if (priv->state != STATE_ACTIVE_TC) {
14580854df79SAbylay Ospan 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
14590854df79SAbylay Ospan 				__func__, priv->state);
14600854df79SAbylay Ospan 		return -EINVAL;
14610854df79SAbylay Ospan 	}
14620854df79SAbylay Ospan 
14630854df79SAbylay Ospan 	cxd2841er_freeze_regs(priv);
14640854df79SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
14650854df79SAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0x5B, pktnum, sizeof(pktnum));
14660854df79SAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0x16, data, sizeof(data));
14676ccf821cSDaniel Scheller 	cxd2841er_unfreeze_regs(priv);
14680854df79SAbylay Ospan 
14690854df79SAbylay Ospan 	if (!pktnum[0] && !pktnum[1]) {
14700854df79SAbylay Ospan 		dev_dbg(&priv->i2c->dev,
14710854df79SAbylay Ospan 				"%s(): no valid BER data\n", __func__);
14720854df79SAbylay Ospan 		return -EINVAL;
14730854df79SAbylay Ospan 	}
14740854df79SAbylay Ospan 
14750854df79SAbylay Ospan 	*bit_error = ((u32)(data[0] & 0x7F) << 16) |
14760854df79SAbylay Ospan 		((u32)data[1] << 8) | data[2];
14770854df79SAbylay Ospan 	*bit_count = ((((u32)pktnum[0] << 8) | pktnum[1]) * 204 * 8);
14780854df79SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s(): bit_error=%u bit_count=%u\n",
14790854df79SAbylay Ospan 			__func__, *bit_error, *bit_count);
14800854df79SAbylay Ospan 
14810854df79SAbylay Ospan 	return 0;
14820854df79SAbylay Ospan }
14830854df79SAbylay Ospan 
14844216be14SMauro Carvalho Chehab static int cxd2841er_mon_read_ber_s(struct cxd2841er_priv *priv,
14854216be14SMauro Carvalho Chehab 				    u32 *bit_error, u32 *bit_count)
1486a6dc60ffSKozlov Sergey {
1487a6dc60ffSKozlov Sergey 	u8 data[11];
1488a6dc60ffSKozlov Sergey 
1489a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0xA0 */
1490a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1491a6dc60ffSKozlov Sergey 	/*
1492a6dc60ffSKozlov Sergey 	 *  slave     Bank      Addr      Bit      Signal name
1493a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       35h       [0]      IFVBER_VALID
1494a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       36h       [5:0]    IFVBER_BITERR[21:16]
1495a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       37h       [7:0]    IFVBER_BITERR[15:8]
1496a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       38h       [7:0]    IFVBER_BITERR[7:0]
1497a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       3Dh       [5:0]    IFVBER_BITNUM[21:16]
1498a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       3Eh       [7:0]    IFVBER_BITNUM[15:8]
1499a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       3Fh       [7:0]    IFVBER_BITNUM[7:0]
1500a6dc60ffSKozlov Sergey 	 */
1501a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x35, data, 11);
1502a6dc60ffSKozlov Sergey 	if (data[0] & 0x01) {
15034216be14SMauro Carvalho Chehab 		*bit_error = ((u32)(data[1]  & 0x3F) << 16) |
1504a6dc60ffSKozlov Sergey 			     ((u32)(data[2]  & 0xFF) <<  8) |
1505a6dc60ffSKozlov Sergey 			     (u32)(data[3]  & 0xFF);
15064216be14SMauro Carvalho Chehab 		*bit_count = ((u32)(data[8]  & 0x3F) << 16) |
1507a6dc60ffSKozlov Sergey 			     ((u32)(data[9]  & 0xFF) <<  8) |
1508a6dc60ffSKozlov Sergey 			     (u32)(data[10] & 0xFF);
15094216be14SMauro Carvalho Chehab 		if ((*bit_count == 0) || (*bit_error > *bit_count)) {
1510a6dc60ffSKozlov Sergey 			dev_dbg(&priv->i2c->dev,
1511a6dc60ffSKozlov Sergey 				"%s(): invalid bit_error %d, bit_count %d\n",
15124216be14SMauro Carvalho Chehab 				__func__, *bit_error, *bit_count);
1513f1b26622SMauro Carvalho Chehab 			return -EINVAL;
1514a6dc60ffSKozlov Sergey 		}
1515a6dc60ffSKozlov Sergey 		return 0;
1516a6dc60ffSKozlov Sergey 	}
1517f1b26622SMauro Carvalho Chehab 	dev_dbg(&priv->i2c->dev, "%s(): no data available\n", __func__);
1518f1b26622SMauro Carvalho Chehab 	return -EINVAL;
1519f1b26622SMauro Carvalho Chehab }
1520a6dc60ffSKozlov Sergey 
1521a6dc60ffSKozlov Sergey 
15224216be14SMauro Carvalho Chehab static int cxd2841er_mon_read_ber_s2(struct cxd2841er_priv *priv,
15234216be14SMauro Carvalho Chehab 				     u32 *bit_error, u32 *bit_count)
1524a6dc60ffSKozlov Sergey {
1525a6dc60ffSKozlov Sergey 	u8 data[5];
15264216be14SMauro Carvalho Chehab 	u32 period;
1527a6dc60ffSKozlov Sergey 
1528a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0xB2 */
1529a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xb2);
1530a6dc60ffSKozlov Sergey 	/*
1531a6dc60ffSKozlov Sergey 	 *  slave     Bank      Addr      Bit      Signal name
1532a6dc60ffSKozlov Sergey 	 * <SLV-T>    B2h       30h       [0]      IFLBER_VALID
1533a6dc60ffSKozlov Sergey 	 * <SLV-T>    B2h       31h       [3:0]    IFLBER_BITERR[27:24]
1534a6dc60ffSKozlov Sergey 	 * <SLV-T>    B2h       32h       [7:0]    IFLBER_BITERR[23:16]
1535a6dc60ffSKozlov Sergey 	 * <SLV-T>    B2h       33h       [7:0]    IFLBER_BITERR[15:8]
1536a6dc60ffSKozlov Sergey 	 * <SLV-T>    B2h       34h       [7:0]    IFLBER_BITERR[7:0]
1537a6dc60ffSKozlov Sergey 	 */
1538a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x30, data, 5);
1539a6dc60ffSKozlov Sergey 	if (data[0] & 0x01) {
1540a6dc60ffSKozlov Sergey 		/* Bit error count */
15414216be14SMauro Carvalho Chehab 		*bit_error = ((u32)(data[1] & 0x0F) << 24) |
1542a6dc60ffSKozlov Sergey 			     ((u32)(data[2] & 0xFF) << 16) |
1543a6dc60ffSKozlov Sergey 			     ((u32)(data[3] & 0xFF) <<  8) |
1544a6dc60ffSKozlov Sergey 			     (u32)(data[4] & 0xFF);
1545a6dc60ffSKozlov Sergey 
1546a6dc60ffSKozlov Sergey 		/* Set SLV-T Bank : 0xA0 */
1547a6dc60ffSKozlov Sergey 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1548a6dc60ffSKozlov Sergey 		cxd2841er_read_reg(priv, I2C_SLVT, 0x7a, data);
1549a6dc60ffSKozlov Sergey 		/* Measurement period */
1550a6dc60ffSKozlov Sergey 		period = (u32)(1 << (data[0] & 0x0F));
1551a6dc60ffSKozlov Sergey 		if (period == 0) {
1552a6dc60ffSKozlov Sergey 			dev_dbg(&priv->i2c->dev,
1553a6dc60ffSKozlov Sergey 				"%s(): period is 0\n", __func__);
1554f1b26622SMauro Carvalho Chehab 			return -EINVAL;
1555a6dc60ffSKozlov Sergey 		}
15564216be14SMauro Carvalho Chehab 		if (*bit_error > (period * 64800)) {
1557a6dc60ffSKozlov Sergey 			dev_dbg(&priv->i2c->dev,
1558a6dc60ffSKozlov Sergey 				"%s(): invalid bit_err 0x%x period 0x%x\n",
15594216be14SMauro Carvalho Chehab 				__func__, *bit_error, period);
1560f1b26622SMauro Carvalho Chehab 			return -EINVAL;
1561a6dc60ffSKozlov Sergey 		}
15624216be14SMauro Carvalho Chehab 		*bit_count = period * 64800;
15634216be14SMauro Carvalho Chehab 
1564f1b26622SMauro Carvalho Chehab 		return 0;
1565a6dc60ffSKozlov Sergey 	} else {
1566a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1567a6dc60ffSKozlov Sergey 			"%s(): no data available\n", __func__);
1568a6dc60ffSKozlov Sergey 	}
1569f1b26622SMauro Carvalho Chehab 	return -EINVAL;
1570a6dc60ffSKozlov Sergey }
1571a6dc60ffSKozlov Sergey 
15724216be14SMauro Carvalho Chehab static int cxd2841er_read_ber_t2(struct cxd2841er_priv *priv,
15734216be14SMauro Carvalho Chehab 				 u32 *bit_error, u32 *bit_count)
1574a6dc60ffSKozlov Sergey {
1575a6dc60ffSKozlov Sergey 	u8 data[4];
15764216be14SMauro Carvalho Chehab 	u32 period_exp, n_ldpc;
1577a6dc60ffSKozlov Sergey 
1578a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC) {
1579a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1580a6dc60ffSKozlov Sergey 			"%s(): invalid state %d\n", __func__, priv->state);
1581a6dc60ffSKozlov Sergey 		return -EINVAL;
1582a6dc60ffSKozlov Sergey 	}
1583a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1584a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x39, data, sizeof(data));
1585a6dc60ffSKozlov Sergey 	if (!(data[0] & 0x10)) {
1586a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1587a6dc60ffSKozlov Sergey 			"%s(): no valid BER data\n", __func__);
15884216be14SMauro Carvalho Chehab 		return -EINVAL;
1589a6dc60ffSKozlov Sergey 	}
15904216be14SMauro Carvalho Chehab 	*bit_error = ((u32)(data[0] & 0x0f) << 24) |
1591a6dc60ffSKozlov Sergey 		     ((u32)data[1] << 16) |
1592a6dc60ffSKozlov Sergey 		     ((u32)data[2] << 8) |
1593a6dc60ffSKozlov Sergey 		     (u32)data[3];
1594a6dc60ffSKozlov Sergey 	cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
1595a6dc60ffSKozlov Sergey 	period_exp = data[0] & 0x0f;
1596a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x22);
1597a6dc60ffSKozlov Sergey 	cxd2841er_read_reg(priv, I2C_SLVT, 0x5e, data);
1598a6dc60ffSKozlov Sergey 	n_ldpc = ((data[0] & 0x03) == 0 ? 16200 : 64800);
15994216be14SMauro Carvalho Chehab 	if (*bit_error > ((1U << period_exp) * n_ldpc)) {
1600a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1601a6dc60ffSKozlov Sergey 			"%s(): invalid BER value\n", __func__);
1602a6dc60ffSKozlov Sergey 		return -EINVAL;
1603a6dc60ffSKozlov Sergey 	}
16044216be14SMauro Carvalho Chehab 
16054216be14SMauro Carvalho Chehab 	/*
16064216be14SMauro Carvalho Chehab 	 * FIXME: the right thing would be to return bit_error untouched,
16074216be14SMauro Carvalho Chehab 	 * but, as we don't know the scale returned by the counters, let's
16084216be14SMauro Carvalho Chehab 	 * at least preserver BER = bit_error/bit_count.
16094216be14SMauro Carvalho Chehab 	 */
1610a6dc60ffSKozlov Sergey 	if (period_exp >= 4) {
16114216be14SMauro Carvalho Chehab 		*bit_count = (1U << (period_exp - 4)) * (n_ldpc / 200);
16124216be14SMauro Carvalho Chehab 		*bit_error *= 3125ULL;
1613a6dc60ffSKozlov Sergey 	} else {
16144216be14SMauro Carvalho Chehab 		*bit_count = (1U << period_exp) * (n_ldpc / 200);
1615a6f330cbSAbylay Ospan 		*bit_error *= 50000ULL;
1616a6dc60ffSKozlov Sergey 	}
1617a6dc60ffSKozlov Sergey 	return 0;
1618a6dc60ffSKozlov Sergey }
1619a6dc60ffSKozlov Sergey 
16204216be14SMauro Carvalho Chehab static int cxd2841er_read_ber_t(struct cxd2841er_priv *priv,
16214216be14SMauro Carvalho Chehab 				u32 *bit_error, u32 *bit_count)
1622a6dc60ffSKozlov Sergey {
1623a6dc60ffSKozlov Sergey 	u8 data[2];
16244216be14SMauro Carvalho Chehab 	u32 period;
1625a6dc60ffSKozlov Sergey 
1626a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC) {
1627a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1628a6dc60ffSKozlov Sergey 			"%s(): invalid state %d\n", __func__, priv->state);
1629a6dc60ffSKozlov Sergey 		return -EINVAL;
1630a6dc60ffSKozlov Sergey 	}
1631a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1632a6dc60ffSKozlov Sergey 	cxd2841er_read_reg(priv, I2C_SLVT, 0x39, data);
1633a6dc60ffSKozlov Sergey 	if (!(data[0] & 0x01)) {
1634a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1635a6dc60ffSKozlov Sergey 			"%s(): no valid BER data\n", __func__);
1636a6dc60ffSKozlov Sergey 		return 0;
1637a6dc60ffSKozlov Sergey 	}
1638a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x22, data, sizeof(data));
16394216be14SMauro Carvalho Chehab 	*bit_error = ((u32)data[0] << 8) | (u32)data[1];
1640a6dc60ffSKozlov Sergey 	cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
1641a6dc60ffSKozlov Sergey 	period = ((data[0] & 0x07) == 0) ? 256 : (4096 << (data[0] & 0x07));
16424216be14SMauro Carvalho Chehab 
16434216be14SMauro Carvalho Chehab 	/*
16444216be14SMauro Carvalho Chehab 	 * FIXME: the right thing would be to return bit_error untouched,
16454216be14SMauro Carvalho Chehab 	 * but, as we don't know the scale returned by the counters, let's
16464216be14SMauro Carvalho Chehab 	 * at least preserver BER = bit_error/bit_count.
16474216be14SMauro Carvalho Chehab 	 */
16484216be14SMauro Carvalho Chehab 	*bit_count = period / 128;
16494216be14SMauro Carvalho Chehab 	*bit_error *= 78125ULL;
1650a6dc60ffSKozlov Sergey 	return 0;
1651a6dc60ffSKozlov Sergey }
1652a6dc60ffSKozlov Sergey 
16534a86bc10SAbylay Ospan static int cxd2841er_freeze_regs(struct cxd2841er_priv *priv)
16544a86bc10SAbylay Ospan {
16554a86bc10SAbylay Ospan 	/*
16564a86bc10SAbylay Ospan 	 * Freeze registers: ensure multiple separate register reads
16574a86bc10SAbylay Ospan 	 * are from the same snapshot
16584a86bc10SAbylay Ospan 	 */
16594a86bc10SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x01);
16604a86bc10SAbylay Ospan 	return 0;
16614a86bc10SAbylay Ospan }
16624a86bc10SAbylay Ospan 
16634a86bc10SAbylay Ospan static int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv)
16644a86bc10SAbylay Ospan {
16654a86bc10SAbylay Ospan 	/*
16664a86bc10SAbylay Ospan 	 * un-freeze registers
16674a86bc10SAbylay Ospan 	 */
16684a86bc10SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x00);
16694a86bc10SAbylay Ospan 	return 0;
16704a86bc10SAbylay Ospan }
16714a86bc10SAbylay Ospan 
1672e05b1872SAbylay Ospan static u32 cxd2841er_dvbs_read_snr(struct cxd2841er_priv *priv,
1673e05b1872SAbylay Ospan 		u8 delsys, u32 *snr)
1674a6dc60ffSKozlov Sergey {
1675a6dc60ffSKozlov Sergey 	u8 data[3];
1676a6dc60ffSKozlov Sergey 	u32 res = 0, value;
1677a6dc60ffSKozlov Sergey 	int min_index, max_index, index;
1678a6dc60ffSKozlov Sergey 	static const struct cxd2841er_cnr_data *cn_data;
1679a6dc60ffSKozlov Sergey 
16804a86bc10SAbylay Ospan 	cxd2841er_freeze_regs(priv);
1681a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0xA1 */
1682a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa1);
1683a6dc60ffSKozlov Sergey 	/*
1684a6dc60ffSKozlov Sergey 	 *  slave     Bank      Addr      Bit     Signal name
1685a6dc60ffSKozlov Sergey 	 * <SLV-T>    A1h       10h       [0]     ICPM_QUICKRDY
1686a6dc60ffSKozlov Sergey 	 * <SLV-T>    A1h       11h       [4:0]   ICPM_QUICKCNDT[12:8]
1687a6dc60ffSKozlov Sergey 	 * <SLV-T>    A1h       12h       [7:0]   ICPM_QUICKCNDT[7:0]
1688a6dc60ffSKozlov Sergey 	 */
1689a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x10, data, 3);
16906ccf821cSDaniel Scheller 	cxd2841er_unfreeze_regs(priv);
16916ccf821cSDaniel Scheller 
1692a6dc60ffSKozlov Sergey 	if (data[0] & 0x01) {
1693a6dc60ffSKozlov Sergey 		value = ((u32)(data[1] & 0x1F) << 8) | (u32)(data[2] & 0xFF);
1694a6dc60ffSKozlov Sergey 		min_index = 0;
1695a6dc60ffSKozlov Sergey 		if (delsys == SYS_DVBS) {
1696a6dc60ffSKozlov Sergey 			cn_data = s_cn_data;
1697a6dc60ffSKozlov Sergey 			max_index = sizeof(s_cn_data) /
1698a6dc60ffSKozlov Sergey 				sizeof(s_cn_data[0]) - 1;
1699a6dc60ffSKozlov Sergey 		} else {
1700a6dc60ffSKozlov Sergey 			cn_data = s2_cn_data;
1701a6dc60ffSKozlov Sergey 			max_index = sizeof(s2_cn_data) /
1702a6dc60ffSKozlov Sergey 				sizeof(s2_cn_data[0]) - 1;
1703a6dc60ffSKozlov Sergey 		}
1704a6dc60ffSKozlov Sergey 		if (value >= cn_data[min_index].value) {
1705a6dc60ffSKozlov Sergey 			res = cn_data[min_index].cnr_x1000;
1706a6dc60ffSKozlov Sergey 			goto done;
1707a6dc60ffSKozlov Sergey 		}
1708a6dc60ffSKozlov Sergey 		if (value <= cn_data[max_index].value) {
1709a6dc60ffSKozlov Sergey 			res = cn_data[max_index].cnr_x1000;
1710a6dc60ffSKozlov Sergey 			goto done;
1711a6dc60ffSKozlov Sergey 		}
1712a6dc60ffSKozlov Sergey 		while ((max_index - min_index) > 1) {
1713a6dc60ffSKozlov Sergey 			index = (max_index + min_index) / 2;
1714a6dc60ffSKozlov Sergey 			if (value == cn_data[index].value) {
1715a6dc60ffSKozlov Sergey 				res = cn_data[index].cnr_x1000;
1716a6dc60ffSKozlov Sergey 				goto done;
1717a6dc60ffSKozlov Sergey 			} else if (value > cn_data[index].value)
1718a6dc60ffSKozlov Sergey 				max_index = index;
1719a6dc60ffSKozlov Sergey 			else
1720a6dc60ffSKozlov Sergey 				min_index = index;
1721a6dc60ffSKozlov Sergey 			if ((max_index - min_index) <= 1) {
1722a6dc60ffSKozlov Sergey 				if (value == cn_data[max_index].value) {
1723a6dc60ffSKozlov Sergey 					res = cn_data[max_index].cnr_x1000;
1724a6dc60ffSKozlov Sergey 					goto done;
1725a6dc60ffSKozlov Sergey 				} else {
1726a6dc60ffSKozlov Sergey 					res = cn_data[min_index].cnr_x1000;
1727a6dc60ffSKozlov Sergey 					goto done;
1728a6dc60ffSKozlov Sergey 				}
1729a6dc60ffSKozlov Sergey 			}
1730a6dc60ffSKozlov Sergey 		}
1731a6dc60ffSKozlov Sergey 	} else {
1732a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1733a6dc60ffSKozlov Sergey 			"%s(): no data available\n", __func__);
1734e05b1872SAbylay Ospan 		return -EINVAL;
1735a6dc60ffSKozlov Sergey 	}
1736a6dc60ffSKozlov Sergey done:
1737e05b1872SAbylay Ospan 	*snr = res;
1738e05b1872SAbylay Ospan 	return 0;
1739e05b1872SAbylay Ospan }
1740e05b1872SAbylay Ospan 
1741e05b1872SAbylay Ospan static uint32_t sony_log(uint32_t x)
1742e05b1872SAbylay Ospan {
1743e05b1872SAbylay Ospan 	return (((10000>>8)*(intlog2(x)>>16) + LOG2_E_100X/2)/LOG2_E_100X);
1744e05b1872SAbylay Ospan }
1745e05b1872SAbylay Ospan 
1746e05b1872SAbylay Ospan static int cxd2841er_read_snr_c(struct cxd2841er_priv *priv, u32 *snr)
1747e05b1872SAbylay Ospan {
1748e05b1872SAbylay Ospan 	u32 reg;
1749e05b1872SAbylay Ospan 	u8 data[2];
1750e05b1872SAbylay Ospan 	enum sony_dvbc_constellation_t qam = SONY_DVBC_CONSTELLATION_16QAM;
1751e05b1872SAbylay Ospan 
1752e05b1872SAbylay Ospan 	*snr = 0;
1753e05b1872SAbylay Ospan 	if (priv->state != STATE_ACTIVE_TC) {
1754e05b1872SAbylay Ospan 		dev_dbg(&priv->i2c->dev,
1755e05b1872SAbylay Ospan 				"%s(): invalid state %d\n",
1756e05b1872SAbylay Ospan 				__func__, priv->state);
1757e05b1872SAbylay Ospan 		return -EINVAL;
1758e05b1872SAbylay Ospan 	}
1759e05b1872SAbylay Ospan 
17604a86bc10SAbylay Ospan 	cxd2841er_freeze_regs(priv);
1761e05b1872SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1762e05b1872SAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0x19, data, 1);
1763e05b1872SAbylay Ospan 	qam = (enum sony_dvbc_constellation_t) (data[0] & 0x07);
1764e05b1872SAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0x4C, data, 2);
17656ccf821cSDaniel Scheller 	cxd2841er_unfreeze_regs(priv);
1766e05b1872SAbylay Ospan 
1767e05b1872SAbylay Ospan 	reg = ((u32)(data[0]&0x1f) << 8) | (u32)data[1];
1768e05b1872SAbylay Ospan 	if (reg == 0) {
1769e05b1872SAbylay Ospan 		dev_dbg(&priv->i2c->dev,
1770e05b1872SAbylay Ospan 				"%s(): reg value out of range\n", __func__);
1771e05b1872SAbylay Ospan 		return 0;
1772e05b1872SAbylay Ospan 	}
1773e05b1872SAbylay Ospan 
1774e05b1872SAbylay Ospan 	switch (qam) {
1775e05b1872SAbylay Ospan 	case SONY_DVBC_CONSTELLATION_16QAM:
1776e05b1872SAbylay Ospan 	case SONY_DVBC_CONSTELLATION_64QAM:
1777e05b1872SAbylay Ospan 	case SONY_DVBC_CONSTELLATION_256QAM:
1778e05b1872SAbylay Ospan 		/* SNR(dB) = -9.50 * ln(IREG_SNR_ESTIMATE / (24320)) */
1779e05b1872SAbylay Ospan 		if (reg < 126)
1780e05b1872SAbylay Ospan 			reg = 126;
1781e05b1872SAbylay Ospan 		*snr = -95 * (int32_t)sony_log(reg) + 95941;
1782e05b1872SAbylay Ospan 		break;
1783e05b1872SAbylay Ospan 	case SONY_DVBC_CONSTELLATION_32QAM:
1784e05b1872SAbylay Ospan 	case SONY_DVBC_CONSTELLATION_128QAM:
1785e05b1872SAbylay Ospan 		/* SNR(dB) = -8.75 * ln(IREG_SNR_ESTIMATE / (20800)) */
1786e05b1872SAbylay Ospan 		if (reg < 69)
1787e05b1872SAbylay Ospan 			reg = 69;
1788e05b1872SAbylay Ospan 		*snr = -88 * (int32_t)sony_log(reg) + 86999;
1789e05b1872SAbylay Ospan 		break;
1790e05b1872SAbylay Ospan 	default:
1791e05b1872SAbylay Ospan 		return -EINVAL;
1792e05b1872SAbylay Ospan 	}
1793e05b1872SAbylay Ospan 
1794e05b1872SAbylay Ospan 	return 0;
1795a6dc60ffSKozlov Sergey }
1796a6dc60ffSKozlov Sergey 
1797a6dc60ffSKozlov Sergey static int cxd2841er_read_snr_t(struct cxd2841er_priv *priv, u32 *snr)
1798a6dc60ffSKozlov Sergey {
1799a6dc60ffSKozlov Sergey 	u32 reg;
1800a6dc60ffSKozlov Sergey 	u8 data[2];
1801a6dc60ffSKozlov Sergey 
1802a6dc60ffSKozlov Sergey 	*snr = 0;
1803a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC) {
1804a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1805a6dc60ffSKozlov Sergey 			"%s(): invalid state %d\n", __func__, priv->state);
1806a6dc60ffSKozlov Sergey 		return -EINVAL;
1807a6dc60ffSKozlov Sergey 	}
18084a86bc10SAbylay Ospan 
18094a86bc10SAbylay Ospan 	cxd2841er_freeze_regs(priv);
1810a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1811a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
18126ccf821cSDaniel Scheller 	cxd2841er_unfreeze_regs(priv);
18136ccf821cSDaniel Scheller 
1814a6dc60ffSKozlov Sergey 	reg = ((u32)data[0] << 8) | (u32)data[1];
1815a6dc60ffSKozlov Sergey 	if (reg == 0) {
1816a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1817a6dc60ffSKozlov Sergey 			"%s(): reg value out of range\n", __func__);
1818a6dc60ffSKozlov Sergey 		return 0;
1819a6dc60ffSKozlov Sergey 	}
1820a6dc60ffSKozlov Sergey 	if (reg > 4996)
1821a6dc60ffSKozlov Sergey 		reg = 4996;
1822df61f828SDaniel Scheller 	*snr = 100 * ((INTLOG10X100(reg) - INTLOG10X100(5350 - reg)) + 285);
1823a6dc60ffSKozlov Sergey 	return 0;
1824a6dc60ffSKozlov Sergey }
1825a6dc60ffSKozlov Sergey 
1826c8946c8dSMauro Carvalho Chehab static int cxd2841er_read_snr_t2(struct cxd2841er_priv *priv, u32 *snr)
1827a6dc60ffSKozlov Sergey {
1828a6dc60ffSKozlov Sergey 	u32 reg;
1829a6dc60ffSKozlov Sergey 	u8 data[2];
1830a6dc60ffSKozlov Sergey 
1831a6dc60ffSKozlov Sergey 	*snr = 0;
1832a6dc60ffSKozlov Sergey 	if (priv->state != STATE_ACTIVE_TC) {
1833a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1834a6dc60ffSKozlov Sergey 			"%s(): invalid state %d\n", __func__, priv->state);
1835a6dc60ffSKozlov Sergey 		return -EINVAL;
1836a6dc60ffSKozlov Sergey 	}
18374a86bc10SAbylay Ospan 
18384a86bc10SAbylay Ospan 	cxd2841er_freeze_regs(priv);
1839a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1840a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
18416ccf821cSDaniel Scheller 	cxd2841er_unfreeze_regs(priv);
18426ccf821cSDaniel Scheller 
1843a6dc60ffSKozlov Sergey 	reg = ((u32)data[0] << 8) | (u32)data[1];
1844a6dc60ffSKozlov Sergey 	if (reg == 0) {
1845a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
1846a6dc60ffSKozlov Sergey 			"%s(): reg value out of range\n", __func__);
1847a6dc60ffSKozlov Sergey 		return 0;
1848a6dc60ffSKozlov Sergey 	}
1849a6dc60ffSKozlov Sergey 	if (reg > 10876)
1850a6dc60ffSKozlov Sergey 		reg = 10876;
1851df61f828SDaniel Scheller 	*snr = 100 * ((INTLOG10X100(reg) - INTLOG10X100(12600 - reg)) + 320);
1852a6dc60ffSKozlov Sergey 	return 0;
1853a6dc60ffSKozlov Sergey }
1854a6dc60ffSKozlov Sergey 
185583808c23SAbylay Ospan static int cxd2841er_read_snr_i(struct cxd2841er_priv *priv, u32 *snr)
185683808c23SAbylay Ospan {
185783808c23SAbylay Ospan 	u32 reg;
185883808c23SAbylay Ospan 	u8 data[2];
185983808c23SAbylay Ospan 
186083808c23SAbylay Ospan 	*snr = 0;
186183808c23SAbylay Ospan 	if (priv->state != STATE_ACTIVE_TC) {
186283808c23SAbylay Ospan 		dev_dbg(&priv->i2c->dev,
186383808c23SAbylay Ospan 				"%s(): invalid state %d\n", __func__,
186483808c23SAbylay Ospan 				priv->state);
186583808c23SAbylay Ospan 		return -EINVAL;
186683808c23SAbylay Ospan 	}
186783808c23SAbylay Ospan 
18684a86bc10SAbylay Ospan 	cxd2841er_freeze_regs(priv);
186983808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
187083808c23SAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
18716ccf821cSDaniel Scheller 	cxd2841er_unfreeze_regs(priv);
18726ccf821cSDaniel Scheller 
187383808c23SAbylay Ospan 	reg = ((u32)data[0] << 8) | (u32)data[1];
187483808c23SAbylay Ospan 	if (reg == 0) {
187583808c23SAbylay Ospan 		dev_dbg(&priv->i2c->dev,
187683808c23SAbylay Ospan 				"%s(): reg value out of range\n", __func__);
187783808c23SAbylay Ospan 		return 0;
187883808c23SAbylay Ospan 	}
18790854df79SAbylay Ospan 	*snr = 10000 * (intlog10(reg) >> 24) - 9031;
188083808c23SAbylay Ospan 	return 0;
188183808c23SAbylay Ospan }
188283808c23SAbylay Ospan 
1883d0998ce7SAbylay Ospan static u16 cxd2841er_read_agc_gain_c(struct cxd2841er_priv *priv,
1884d0998ce7SAbylay Ospan 					u8 delsys)
1885d0998ce7SAbylay Ospan {
1886d0998ce7SAbylay Ospan 	u8 data[2];
1887d0998ce7SAbylay Ospan 
1888d0998ce7SAbylay Ospan 	cxd2841er_write_reg(
1889d0998ce7SAbylay Ospan 		priv, I2C_SLVT, 0x00, 0x40);
1890d0998ce7SAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0x49, data, 2);
1891d0998ce7SAbylay Ospan 	dev_dbg(&priv->i2c->dev,
1892d0998ce7SAbylay Ospan 			"%s(): AGC value=%u\n",
1893d0998ce7SAbylay Ospan 			__func__, (((u16)data[0] & 0x0F) << 8) |
1894d0998ce7SAbylay Ospan 			(u16)(data[1] & 0xFF));
1895d0998ce7SAbylay Ospan 	return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1896d0998ce7SAbylay Ospan }
1897d0998ce7SAbylay Ospan 
1898a6dc60ffSKozlov Sergey static u16 cxd2841er_read_agc_gain_t_t2(struct cxd2841er_priv *priv,
1899a6dc60ffSKozlov Sergey 					u8 delsys)
1900a6dc60ffSKozlov Sergey {
1901a6dc60ffSKozlov Sergey 	u8 data[2];
1902a6dc60ffSKozlov Sergey 
1903a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(
1904a6dc60ffSKozlov Sergey 		priv, I2C_SLVT, 0x00, (delsys == SYS_DVBT ? 0x10 : 0x20));
1905a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
1906c5ea46daSAbylay Ospan 	dev_dbg(&priv->i2c->dev,
1907c5ea46daSAbylay Ospan 			"%s(): AGC value=%u\n",
1908c5ea46daSAbylay Ospan 			__func__, (((u16)data[0] & 0x0F) << 8) |
1909c5ea46daSAbylay Ospan 			(u16)(data[1] & 0xFF));
1910a6dc60ffSKozlov Sergey 	return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1911a6dc60ffSKozlov Sergey }
1912a6dc60ffSKozlov Sergey 
191383808c23SAbylay Ospan static u16 cxd2841er_read_agc_gain_i(struct cxd2841er_priv *priv,
191483808c23SAbylay Ospan 		u8 delsys)
191583808c23SAbylay Ospan {
191683808c23SAbylay Ospan 	u8 data[2];
191783808c23SAbylay Ospan 
191883808c23SAbylay Ospan 	cxd2841er_write_reg(
191983808c23SAbylay Ospan 			priv, I2C_SLVT, 0x00, 0x60);
192083808c23SAbylay Ospan 	cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
192183808c23SAbylay Ospan 
192283808c23SAbylay Ospan 	dev_dbg(&priv->i2c->dev,
192383808c23SAbylay Ospan 			"%s(): AGC value=%u\n",
192483808c23SAbylay Ospan 			__func__, (((u16)data[0] & 0x0F) << 8) |
192583808c23SAbylay Ospan 			(u16)(data[1] & 0xFF));
192683808c23SAbylay Ospan 	return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
192783808c23SAbylay Ospan }
192883808c23SAbylay Ospan 
1929a6dc60ffSKozlov Sergey static u16 cxd2841er_read_agc_gain_s(struct cxd2841er_priv *priv)
1930a6dc60ffSKozlov Sergey {
1931a6dc60ffSKozlov Sergey 	u8 data[2];
1932a6dc60ffSKozlov Sergey 
1933a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0xA0 */
1934a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1935a6dc60ffSKozlov Sergey 	/*
1936a6dc60ffSKozlov Sergey 	 *  slave     Bank      Addr      Bit       Signal name
1937a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       1Fh       [4:0]     IRFAGC_GAIN[12:8]
1938a6dc60ffSKozlov Sergey 	 * <SLV-T>    A0h       20h       [7:0]     IRFAGC_GAIN[7:0]
1939a6dc60ffSKozlov Sergey 	 */
1940a6dc60ffSKozlov Sergey 	cxd2841er_read_regs(priv, I2C_SLVT, 0x1f, data, 2);
1941a6dc60ffSKozlov Sergey 	return ((((u16)data[0] & 0x1F) << 8) | (u16)(data[1] & 0xFF)) << 3;
1942a6dc60ffSKozlov Sergey }
1943a6dc60ffSKozlov Sergey 
1944f1b26622SMauro Carvalho Chehab static void cxd2841er_read_ber(struct dvb_frontend *fe)
1945a6dc60ffSKozlov Sergey {
1946a6dc60ffSKozlov Sergey 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1947a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
19484216be14SMauro Carvalho Chehab 	u32 ret, bit_error = 0, bit_count = 0;
1949a6dc60ffSKozlov Sergey 
1950a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1951a6dc60ffSKozlov Sergey 	switch (p->delivery_system) {
1952a6f330cbSAbylay Ospan 	case SYS_DVBC_ANNEX_A:
1953a6f330cbSAbylay Ospan 	case SYS_DVBC_ANNEX_B:
1954a6f330cbSAbylay Ospan 	case SYS_DVBC_ANNEX_C:
1955a6f330cbSAbylay Ospan 		ret = cxd2841er_read_ber_c(priv, &bit_error, &bit_count);
1956a6f330cbSAbylay Ospan 		break;
19570854df79SAbylay Ospan 	case SYS_ISDBT:
19580854df79SAbylay Ospan 		ret = cxd2841er_read_ber_i(priv, &bit_error, &bit_count);
19590854df79SAbylay Ospan 		break;
1960a6dc60ffSKozlov Sergey 	case SYS_DVBS:
19614216be14SMauro Carvalho Chehab 		ret = cxd2841er_mon_read_ber_s(priv, &bit_error, &bit_count);
1962a6dc60ffSKozlov Sergey 		break;
1963a6dc60ffSKozlov Sergey 	case SYS_DVBS2:
19644216be14SMauro Carvalho Chehab 		ret = cxd2841er_mon_read_ber_s2(priv, &bit_error, &bit_count);
1965a6dc60ffSKozlov Sergey 		break;
1966a6dc60ffSKozlov Sergey 	case SYS_DVBT:
19674216be14SMauro Carvalho Chehab 		ret = cxd2841er_read_ber_t(priv, &bit_error, &bit_count);
1968a6dc60ffSKozlov Sergey 		break;
1969f1b26622SMauro Carvalho Chehab 	case SYS_DVBT2:
19704216be14SMauro Carvalho Chehab 		ret = cxd2841er_read_ber_t2(priv, &bit_error, &bit_count);
1971f1b26622SMauro Carvalho Chehab 		break;
1972f1b26622SMauro Carvalho Chehab 	default:
1973f1b26622SMauro Carvalho Chehab 		p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
19744216be14SMauro Carvalho Chehab 		p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1975f1b26622SMauro Carvalho Chehab 		return;
1976a6dc60ffSKozlov Sergey 	}
1977f1b26622SMauro Carvalho Chehab 
1978f1b26622SMauro Carvalho Chehab 	if (!ret) {
1979f1b26622SMauro Carvalho Chehab 		p->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
1980a6f330cbSAbylay Ospan 		p->post_bit_error.stat[0].uvalue += bit_error;
19814216be14SMauro Carvalho Chehab 		p->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
1982a6f330cbSAbylay Ospan 		p->post_bit_count.stat[0].uvalue += bit_count;
1983f1b26622SMauro Carvalho Chehab 	} else {
1984f1b26622SMauro Carvalho Chehab 		p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
19854216be14SMauro Carvalho Chehab 		p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
1986f1b26622SMauro Carvalho Chehab 	}
1987a6dc60ffSKozlov Sergey }
1988a6dc60ffSKozlov Sergey 
19895fda1b65SMauro Carvalho Chehab static void cxd2841er_read_signal_strength(struct dvb_frontend *fe)
1990a6dc60ffSKozlov Sergey {
1991a6dc60ffSKozlov Sergey 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1992a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
1993313a7dfbSMauro Carvalho Chehab 	s32 strength;
1994a6dc60ffSKozlov Sergey 
1995a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1996a6dc60ffSKozlov Sergey 	switch (p->delivery_system) {
1997a6dc60ffSKozlov Sergey 	case SYS_DVBT:
1998a6dc60ffSKozlov Sergey 	case SYS_DVBT2:
19995fda1b65SMauro Carvalho Chehab 		strength = cxd2841er_read_agc_gain_t_t2(priv,
20005fda1b65SMauro Carvalho Chehab 							p->delivery_system);
20015fda1b65SMauro Carvalho Chehab 		p->strength.stat[0].scale = FE_SCALE_DECIBEL;
20025fda1b65SMauro Carvalho Chehab 		/* Formula was empirically determinated @ 410 MHz */
2003313a7dfbSMauro Carvalho Chehab 		p->strength.stat[0].uvalue = strength * 366 / 100 - 89520;
20045fda1b65SMauro Carvalho Chehab 		break;	/* Code moved out of the function */
2005988bd281SMauro Carvalho Chehab 	case SYS_DVBC_ANNEX_A:
2006997bdc0cSAbylay Ospan 	case SYS_DVBC_ANNEX_B:
2007997bdc0cSAbylay Ospan 	case SYS_DVBC_ANNEX_C:
2008997bdc0cSAbylay Ospan 		strength = cxd2841er_read_agc_gain_c(priv,
2009988bd281SMauro Carvalho Chehab 							p->delivery_system);
2010d12b791eSMauro Carvalho Chehab 		p->strength.stat[0].scale = FE_SCALE_DECIBEL;
2011d12b791eSMauro Carvalho Chehab 		/*
2012d12b791eSMauro Carvalho Chehab 		 * Formula was empirically determinated via linear regression,
2013d12b791eSMauro Carvalho Chehab 		 * using frequencies: 175 MHz, 410 MHz and 800 MHz, and a
2014d12b791eSMauro Carvalho Chehab 		 * stream modulated with QAM64
2015d12b791eSMauro Carvalho Chehab 		 */
2016313a7dfbSMauro Carvalho Chehab 		p->strength.stat[0].uvalue = strength * 4045 / 1000 - 85224;
2017988bd281SMauro Carvalho Chehab 		break;
201883808c23SAbylay Ospan 	case SYS_ISDBT:
2019313a7dfbSMauro Carvalho Chehab 		strength = cxd2841er_read_agc_gain_i(priv, p->delivery_system);
2020313a7dfbSMauro Carvalho Chehab 		p->strength.stat[0].scale = FE_SCALE_DECIBEL;
2021313a7dfbSMauro Carvalho Chehab 		/*
2022313a7dfbSMauro Carvalho Chehab 		 * Formula was empirically determinated via linear regression,
2023313a7dfbSMauro Carvalho Chehab 		 * using frequencies: 175 MHz, 410 MHz and 800 MHz.
2024313a7dfbSMauro Carvalho Chehab 		 */
2025313a7dfbSMauro Carvalho Chehab 		p->strength.stat[0].uvalue = strength * 3775 / 1000 - 90185;
202683808c23SAbylay Ospan 		break;
2027a6dc60ffSKozlov Sergey 	case SYS_DVBS:
2028a6dc60ffSKozlov Sergey 	case SYS_DVBS2:
20295fda1b65SMauro Carvalho Chehab 		strength = 65535 - cxd2841er_read_agc_gain_s(priv);
20305fda1b65SMauro Carvalho Chehab 		p->strength.stat[0].scale = FE_SCALE_RELATIVE;
20315fda1b65SMauro Carvalho Chehab 		p->strength.stat[0].uvalue = strength;
2032a6dc60ffSKozlov Sergey 		break;
2033a6dc60ffSKozlov Sergey 	default:
2034f1b26622SMauro Carvalho Chehab 		p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2035a6dc60ffSKozlov Sergey 		break;
2036a6dc60ffSKozlov Sergey 	}
2037a6dc60ffSKozlov Sergey }
2038a6dc60ffSKozlov Sergey 
2039f1b26622SMauro Carvalho Chehab static void cxd2841er_read_snr(struct dvb_frontend *fe)
2040a6dc60ffSKozlov Sergey {
2041a6dc60ffSKozlov Sergey 	u32 tmp = 0;
2042e05b1872SAbylay Ospan 	int ret = 0;
2043a6dc60ffSKozlov Sergey 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2044a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
2045a6dc60ffSKozlov Sergey 
2046a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2047a6dc60ffSKozlov Sergey 	switch (p->delivery_system) {
2048e05b1872SAbylay Ospan 	case SYS_DVBC_ANNEX_A:
2049e05b1872SAbylay Ospan 	case SYS_DVBC_ANNEX_B:
2050e05b1872SAbylay Ospan 	case SYS_DVBC_ANNEX_C:
2051e05b1872SAbylay Ospan 		ret = cxd2841er_read_snr_c(priv, &tmp);
2052e05b1872SAbylay Ospan 		break;
2053a6dc60ffSKozlov Sergey 	case SYS_DVBT:
2054e05b1872SAbylay Ospan 		ret = cxd2841er_read_snr_t(priv, &tmp);
2055a6dc60ffSKozlov Sergey 		break;
2056a6dc60ffSKozlov Sergey 	case SYS_DVBT2:
2057e05b1872SAbylay Ospan 		ret = cxd2841er_read_snr_t2(priv, &tmp);
2058a6dc60ffSKozlov Sergey 		break;
205983808c23SAbylay Ospan 	case SYS_ISDBT:
2060e05b1872SAbylay Ospan 		ret = cxd2841er_read_snr_i(priv, &tmp);
206183808c23SAbylay Ospan 		break;
2062a6dc60ffSKozlov Sergey 	case SYS_DVBS:
2063a6dc60ffSKozlov Sergey 	case SYS_DVBS2:
2064e05b1872SAbylay Ospan 		ret = cxd2841er_dvbs_read_snr(priv, p->delivery_system, &tmp);
2065a6dc60ffSKozlov Sergey 		break;
2066a6dc60ffSKozlov Sergey 	default:
2067a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): unknown delivery system %d\n",
2068a6dc60ffSKozlov Sergey 			__func__, p->delivery_system);
2069f1b26622SMauro Carvalho Chehab 		p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2070f1b26622SMauro Carvalho Chehab 		return;
2071a6dc60ffSKozlov Sergey 	}
2072a6dc60ffSKozlov Sergey 
20730854df79SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s(): snr=%d\n",
20740854df79SAbylay Ospan 			__func__, (int32_t)tmp);
20750854df79SAbylay Ospan 
2076e05b1872SAbylay Ospan 	if (!ret) {
2077f1b26622SMauro Carvalho Chehab 		p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
2078f1b26622SMauro Carvalho Chehab 		p->cnr.stat[0].svalue = tmp;
2079e05b1872SAbylay Ospan 	} else {
2080e05b1872SAbylay Ospan 		p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2081e05b1872SAbylay Ospan 	}
2082f1b26622SMauro Carvalho Chehab }
2083f1b26622SMauro Carvalho Chehab 
2084f1b26622SMauro Carvalho Chehab static void cxd2841er_read_ucblocks(struct dvb_frontend *fe)
2085a6dc60ffSKozlov Sergey {
2086a6dc60ffSKozlov Sergey 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2087a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
20884a86bc10SAbylay Ospan 	u32 ucblocks = 0;
2089a6dc60ffSKozlov Sergey 
2090a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2091a6dc60ffSKozlov Sergey 	switch (p->delivery_system) {
2092a6f330cbSAbylay Ospan 	case SYS_DVBC_ANNEX_A:
2093a6f330cbSAbylay Ospan 	case SYS_DVBC_ANNEX_B:
2094a6f330cbSAbylay Ospan 	case SYS_DVBC_ANNEX_C:
2095a6f330cbSAbylay Ospan 		cxd2841er_read_packet_errors_c(priv, &ucblocks);
2096a6f330cbSAbylay Ospan 		break;
2097a6dc60ffSKozlov Sergey 	case SYS_DVBT:
2098f1b26622SMauro Carvalho Chehab 		cxd2841er_read_packet_errors_t(priv, &ucblocks);
2099a6dc60ffSKozlov Sergey 		break;
2100a6dc60ffSKozlov Sergey 	case SYS_DVBT2:
2101f1b26622SMauro Carvalho Chehab 		cxd2841er_read_packet_errors_t2(priv, &ucblocks);
2102a6dc60ffSKozlov Sergey 		break;
210383808c23SAbylay Ospan 	case SYS_ISDBT:
2104f1b26622SMauro Carvalho Chehab 		cxd2841er_read_packet_errors_i(priv, &ucblocks);
210583808c23SAbylay Ospan 		break;
2106a6dc60ffSKozlov Sergey 	default:
2107f1b26622SMauro Carvalho Chehab 		p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2108f1b26622SMauro Carvalho Chehab 		return;
2109a6dc60ffSKozlov Sergey 	}
21104a86bc10SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s() ucblocks=%u\n", __func__, ucblocks);
2111f1b26622SMauro Carvalho Chehab 
2112f1b26622SMauro Carvalho Chehab 	p->block_error.stat[0].scale = FE_SCALE_COUNTER;
2113f1b26622SMauro Carvalho Chehab 	p->block_error.stat[0].uvalue = ucblocks;
2114a6dc60ffSKozlov Sergey }
2115a6dc60ffSKozlov Sergey 
2116a6dc60ffSKozlov Sergey static int cxd2841er_dvbt2_set_profile(
2117a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv, enum cxd2841er_dvbt2_profile_t profile)
2118a6dc60ffSKozlov Sergey {
2119a6dc60ffSKozlov Sergey 	u8 tune_mode;
2120a6dc60ffSKozlov Sergey 	u8 seq_not2d_time;
2121a6dc60ffSKozlov Sergey 
2122a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2123a6dc60ffSKozlov Sergey 	switch (profile) {
2124a6dc60ffSKozlov Sergey 	case DVBT2_PROFILE_BASE:
2125a6dc60ffSKozlov Sergey 		tune_mode = 0x01;
21266c77161aSAbylay Ospan 		/* Set early unlock time */
21276c77161aSAbylay Ospan 		seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x0E:0x0C;
2128a6dc60ffSKozlov Sergey 		break;
2129a6dc60ffSKozlov Sergey 	case DVBT2_PROFILE_LITE:
2130a6dc60ffSKozlov Sergey 		tune_mode = 0x05;
21316c77161aSAbylay Ospan 		/* Set early unlock time */
21326c77161aSAbylay Ospan 		seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
2133a6dc60ffSKozlov Sergey 		break;
2134a6dc60ffSKozlov Sergey 	case DVBT2_PROFILE_ANY:
2135a6dc60ffSKozlov Sergey 		tune_mode = 0x00;
21366c77161aSAbylay Ospan 		/* Set early unlock time */
21376c77161aSAbylay Ospan 		seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
2138a6dc60ffSKozlov Sergey 		break;
2139a6dc60ffSKozlov Sergey 	default:
2140a6dc60ffSKozlov Sergey 		return -EINVAL;
2141a6dc60ffSKozlov Sergey 	}
2142a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x2E */
2143a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2e);
2144a6dc60ffSKozlov Sergey 	/* Set profile and tune mode */
2145a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x10, tune_mode, 0x07);
2146a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x2B */
2147a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2148a6dc60ffSKozlov Sergey 	/* Set early unlock detection time */
2149a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x9d, seq_not2d_time);
2150a6dc60ffSKozlov Sergey 	return 0;
2151a6dc60ffSKozlov Sergey }
2152a6dc60ffSKozlov Sergey 
2153a6dc60ffSKozlov Sergey static int cxd2841er_dvbt2_set_plp_config(struct cxd2841er_priv *priv,
2154a6dc60ffSKozlov Sergey 					  u8 is_auto, u8 plp_id)
2155a6dc60ffSKozlov Sergey {
2156a6dc60ffSKozlov Sergey 	if (is_auto) {
2157a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
2158a6dc60ffSKozlov Sergey 			"%s() using auto PLP selection\n", __func__);
2159a6dc60ffSKozlov Sergey 	} else {
2160a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
2161a6dc60ffSKozlov Sergey 			"%s() using manual PLP selection, ID %d\n",
2162a6dc60ffSKozlov Sergey 			__func__, plp_id);
2163a6dc60ffSKozlov Sergey 	}
2164a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x23 */
2165a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
2166a6dc60ffSKozlov Sergey 	if (!is_auto) {
2167a6dc60ffSKozlov Sergey 		/* Manual PLP selection mode. Set the data PLP Id. */
2168a6dc60ffSKozlov Sergey 		cxd2841er_write_reg(priv, I2C_SLVT, 0xaf, plp_id);
2169a6dc60ffSKozlov Sergey 	}
2170a6dc60ffSKozlov Sergey 	/* Auto PLP select (Scanning mode = 0x00). Data PLP select = 0x01. */
2171a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0xad, (is_auto ? 0x00 : 0x01));
2172a6dc60ffSKozlov Sergey 	return 0;
2173a6dc60ffSKozlov Sergey }
2174a6dc60ffSKozlov Sergey 
2175a6dc60ffSKozlov Sergey static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
2176a6dc60ffSKozlov Sergey 						u32 bandwidth)
2177a6dc60ffSKozlov Sergey {
21784b866c4eSDaniel Scheller 	u32 iffreq, ifhz;
21796c77161aSAbylay Ospan 	u8 data[MAX_WRITE_REGSIZE];
2180a6dc60ffSKozlov Sergey 
21813802c1bcSColin Ian King 	static const uint8_t nominalRate8bw[3][5] = {
21826c77161aSAbylay Ospan 		/* TRCG Nominal Rate [37:0] */
21836c77161aSAbylay Ospan 		{0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
21846c77161aSAbylay Ospan 		{0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
21856c77161aSAbylay Ospan 		{0x11, 0xF0, 0x00, 0x00, 0x00}  /* 41MHz XTal */
21866c77161aSAbylay Ospan 	};
21876c77161aSAbylay Ospan 
21883802c1bcSColin Ian King 	static const uint8_t nominalRate7bw[3][5] = {
21896c77161aSAbylay Ospan 		/* TRCG Nominal Rate [37:0] */
21906c77161aSAbylay Ospan 		{0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
21916c77161aSAbylay Ospan 		{0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
21926c77161aSAbylay Ospan 		{0x14, 0x80, 0x00, 0x00, 0x00}  /* 41MHz XTal */
21936c77161aSAbylay Ospan 	};
21946c77161aSAbylay Ospan 
21953802c1bcSColin Ian King 	static const uint8_t nominalRate6bw[3][5] = {
21966c77161aSAbylay Ospan 		/* TRCG Nominal Rate [37:0] */
21976c77161aSAbylay Ospan 		{0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
21986c77161aSAbylay Ospan 		{0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
21996c77161aSAbylay Ospan 		{0x17, 0xEA, 0xAA, 0xAA, 0xAA}  /* 41MHz XTal */
22006c77161aSAbylay Ospan 	};
22016c77161aSAbylay Ospan 
22023802c1bcSColin Ian King 	static const uint8_t nominalRate5bw[3][5] = {
22036c77161aSAbylay Ospan 		/* TRCG Nominal Rate [37:0] */
22046c77161aSAbylay Ospan 		{0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
22056c77161aSAbylay Ospan 		{0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
22066c77161aSAbylay Ospan 		{0x1C, 0xB3, 0x33, 0x33, 0x33}  /* 41MHz XTal */
22076c77161aSAbylay Ospan 	};
22086c77161aSAbylay Ospan 
22093802c1bcSColin Ian King 	static const uint8_t nominalRate17bw[3][5] = {
22106c77161aSAbylay Ospan 		/* TRCG Nominal Rate [37:0] */
22116c77161aSAbylay Ospan 		{0x58, 0xE2, 0xAF, 0xE0, 0xBC}, /* 20.5MHz XTal */
22126c77161aSAbylay Ospan 		{0x68, 0x0F, 0xA2, 0x32, 0xD0}, /* 24MHz XTal */
22136c77161aSAbylay Ospan 		{0x58, 0xE2, 0xAF, 0xE0, 0xBC}  /* 41MHz XTal */
22146c77161aSAbylay Ospan 	};
22156c77161aSAbylay Ospan 
22163802c1bcSColin Ian King 	static const uint8_t itbCoef8bw[3][14] = {
22176c77161aSAbylay Ospan 		{0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
22186c77161aSAbylay Ospan 			0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
22196c77161aSAbylay Ospan 		{0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1,
22206c77161aSAbylay Ospan 			0x29, 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal   */
22216c77161aSAbylay Ospan 		{0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
22226c77161aSAbylay Ospan 			0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8}  /* 41MHz XTal   */
22236c77161aSAbylay Ospan 	};
22246c77161aSAbylay Ospan 
22253802c1bcSColin Ian King 	static const uint8_t itbCoef7bw[3][14] = {
22266c77161aSAbylay Ospan 		{0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
22276c77161aSAbylay Ospan 			0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
22286c77161aSAbylay Ospan 		{0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0,
22296c77161aSAbylay Ospan 			0x29, 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal   */
22306c77161aSAbylay Ospan 		{0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
22316c77161aSAbylay Ospan 			0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5}  /* 41MHz XTal   */
22326c77161aSAbylay Ospan 	};
22336c77161aSAbylay Ospan 
22343802c1bcSColin Ian King 	static const uint8_t itbCoef6bw[3][14] = {
22356c77161aSAbylay Ospan 		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
22366c77161aSAbylay Ospan 			0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
22376c77161aSAbylay Ospan 		{0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
22386c77161aSAbylay Ospan 			0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal   */
22396c77161aSAbylay Ospan 		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
22406c77161aSAbylay Ospan 			0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}  /* 41MHz XTal   */
22416c77161aSAbylay Ospan 	};
22426c77161aSAbylay Ospan 
22433802c1bcSColin Ian King 	static const uint8_t itbCoef5bw[3][14] = {
22446c77161aSAbylay Ospan 		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
22456c77161aSAbylay Ospan 			0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
22466c77161aSAbylay Ospan 		{0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
22476c77161aSAbylay Ospan 			0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal   */
22486c77161aSAbylay Ospan 		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
22496c77161aSAbylay Ospan 			0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}  /* 41MHz XTal   */
22506c77161aSAbylay Ospan 	};
22516c77161aSAbylay Ospan 
22523802c1bcSColin Ian King 	static const uint8_t itbCoef17bw[3][14] = {
22536c77161aSAbylay Ospan 		{0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
22546c77161aSAbylay Ospan 			0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99}, /* 20.5MHz XTal */
22556c77161aSAbylay Ospan 		{0x33, 0x8E, 0x2B, 0x97, 0x2D, 0x95, 0x37, 0x8B,
22566c77161aSAbylay Ospan 			0x30, 0x97, 0x2D, 0x9A, 0x21, 0xA4}, /* 24MHz XTal   */
22576c77161aSAbylay Ospan 		{0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
22586c77161aSAbylay Ospan 			0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99}  /* 41MHz XTal   */
22596c77161aSAbylay Ospan 	};
22606c77161aSAbylay Ospan 
22616c77161aSAbylay Ospan 	/* Set SLV-T Bank : 0x20 */
22626c77161aSAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
22636c77161aSAbylay Ospan 
2264a6dc60ffSKozlov Sergey 	switch (bandwidth) {
2265a6dc60ffSKozlov Sergey 	case 8000000:
22666c77161aSAbylay Ospan 		/* <Timing Recovery setting> */
22676c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
22686c77161aSAbylay Ospan 				0x9F, nominalRate8bw[priv->xtal], 5);
22696c77161aSAbylay Ospan 
22706c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x27 */
22716c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
22726c77161aSAbylay Ospan 		cxd2841er_set_reg_bits(priv, I2C_SLVT,
22736c77161aSAbylay Ospan 				0x7a, 0x00, 0x0f);
22746c77161aSAbylay Ospan 
22756c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x10 */
22766c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
22776c77161aSAbylay Ospan 
22786c77161aSAbylay Ospan 		/* Group delay equaliser settings for
22796c77161aSAbylay Ospan 		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
22806c77161aSAbylay Ospan 		 */
22817afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
22826c77161aSAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
22836c77161aSAbylay Ospan 				0xA6, itbCoef8bw[priv->xtal], 14);
22846c77161aSAbylay Ospan 		/* <IF freq setting> */
22854b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 4800000);
22864b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
22876c77161aSAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
22886c77161aSAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
22896c77161aSAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
22906c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
22916c77161aSAbylay Ospan 		/* System bandwidth setting */
22926c77161aSAbylay Ospan 		cxd2841er_set_reg_bits(
22936c77161aSAbylay Ospan 				priv, I2C_SLVT, 0xD7, 0x00, 0x07);
2294a6dc60ffSKozlov Sergey 		break;
2295a6dc60ffSKozlov Sergey 	case 7000000:
22966c77161aSAbylay Ospan 		/* <Timing Recovery setting> */
22976c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
22986c77161aSAbylay Ospan 				0x9F, nominalRate7bw[priv->xtal], 5);
22996c77161aSAbylay Ospan 
23006c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x27 */
23016c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
23026c77161aSAbylay Ospan 		cxd2841er_set_reg_bits(priv, I2C_SLVT,
23036c77161aSAbylay Ospan 				0x7a, 0x00, 0x0f);
23046c77161aSAbylay Ospan 
23056c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x10 */
23066c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
23076c77161aSAbylay Ospan 
23086c77161aSAbylay Ospan 		/* Group delay equaliser settings for
23096c77161aSAbylay Ospan 		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
23106c77161aSAbylay Ospan 		 */
23117afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
23126c77161aSAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
23136c77161aSAbylay Ospan 				0xA6, itbCoef7bw[priv->xtal], 14);
23146c77161aSAbylay Ospan 		/* <IF freq setting> */
23154b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 4200000);
23164b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
23176c77161aSAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
23186c77161aSAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
23196c77161aSAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
23206c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
23216c77161aSAbylay Ospan 		/* System bandwidth setting */
23226c77161aSAbylay Ospan 		cxd2841er_set_reg_bits(
23236c77161aSAbylay Ospan 				priv, I2C_SLVT, 0xD7, 0x02, 0x07);
2324a6dc60ffSKozlov Sergey 		break;
2325a6dc60ffSKozlov Sergey 	case 6000000:
23266c77161aSAbylay Ospan 		/* <Timing Recovery setting> */
23276c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
23286c77161aSAbylay Ospan 				0x9F, nominalRate6bw[priv->xtal], 5);
23296c77161aSAbylay Ospan 
23306c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x27 */
23316c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
23326c77161aSAbylay Ospan 		cxd2841er_set_reg_bits(priv, I2C_SLVT,
23336c77161aSAbylay Ospan 				0x7a, 0x00, 0x0f);
23346c77161aSAbylay Ospan 
23356c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x10 */
23366c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
23376c77161aSAbylay Ospan 
23386c77161aSAbylay Ospan 		/* Group delay equaliser settings for
23396c77161aSAbylay Ospan 		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
23406c77161aSAbylay Ospan 		 */
23417afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
23426c77161aSAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
23436c77161aSAbylay Ospan 				0xA6, itbCoef6bw[priv->xtal], 14);
23446c77161aSAbylay Ospan 		/* <IF freq setting> */
23454b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 3600000);
23464b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
23476c77161aSAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
23486c77161aSAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
23496c77161aSAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
23506c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
23516c77161aSAbylay Ospan 		/* System bandwidth setting */
23526c77161aSAbylay Ospan 		cxd2841er_set_reg_bits(
23536c77161aSAbylay Ospan 				priv, I2C_SLVT, 0xD7, 0x04, 0x07);
2354a6dc60ffSKozlov Sergey 		break;
2355a6dc60ffSKozlov Sergey 	case 5000000:
23566c77161aSAbylay Ospan 		/* <Timing Recovery setting> */
23576c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
23586c77161aSAbylay Ospan 				0x9F, nominalRate5bw[priv->xtal], 5);
23596c77161aSAbylay Ospan 
23606c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x27 */
23616c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
23626c77161aSAbylay Ospan 		cxd2841er_set_reg_bits(priv, I2C_SLVT,
23636c77161aSAbylay Ospan 				0x7a, 0x00, 0x0f);
23646c77161aSAbylay Ospan 
23656c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x10 */
23666c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
23676c77161aSAbylay Ospan 
23686c77161aSAbylay Ospan 		/* Group delay equaliser settings for
23696c77161aSAbylay Ospan 		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
23706c77161aSAbylay Ospan 		 */
23717afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
23726c77161aSAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
23736c77161aSAbylay Ospan 				0xA6, itbCoef5bw[priv->xtal], 14);
23746c77161aSAbylay Ospan 		/* <IF freq setting> */
23754b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 3600000);
23764b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
23776c77161aSAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
23786c77161aSAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
23796c77161aSAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
23806c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
23816c77161aSAbylay Ospan 		/* System bandwidth setting */
23826c77161aSAbylay Ospan 		cxd2841er_set_reg_bits(
23836c77161aSAbylay Ospan 				priv, I2C_SLVT, 0xD7, 0x06, 0x07);
2384a6dc60ffSKozlov Sergey 		break;
2385a6dc60ffSKozlov Sergey 	case 1712000:
23866c77161aSAbylay Ospan 		/* <Timing Recovery setting> */
23876c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
23886c77161aSAbylay Ospan 				0x9F, nominalRate17bw[priv->xtal], 5);
23896c77161aSAbylay Ospan 
23906c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x27 */
23916c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
23926c77161aSAbylay Ospan 		cxd2841er_set_reg_bits(priv, I2C_SLVT,
23936c77161aSAbylay Ospan 				0x7a, 0x03, 0x0f);
23946c77161aSAbylay Ospan 
23956c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x10 */
23966c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
23976c77161aSAbylay Ospan 
23986c77161aSAbylay Ospan 		/* Group delay equaliser settings for
23996c77161aSAbylay Ospan 		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
24006c77161aSAbylay Ospan 		 */
24017afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
24026c77161aSAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
24036c77161aSAbylay Ospan 				0xA6, itbCoef17bw[priv->xtal], 14);
24046c77161aSAbylay Ospan 		/* <IF freq setting> */
24054b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 3500000);
24064b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
24076c77161aSAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
24086c77161aSAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
24096c77161aSAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
24106c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
24116c77161aSAbylay Ospan 		/* System bandwidth setting */
24126c77161aSAbylay Ospan 		cxd2841er_set_reg_bits(
24136c77161aSAbylay Ospan 				priv, I2C_SLVT, 0xD7, 0x03, 0x07);
2414a6dc60ffSKozlov Sergey 		break;
2415a6dc60ffSKozlov Sergey 	default:
2416a6dc60ffSKozlov Sergey 		return -EINVAL;
2417a6dc60ffSKozlov Sergey 	}
2418a6dc60ffSKozlov Sergey 	return 0;
2419a6dc60ffSKozlov Sergey }
2420a6dc60ffSKozlov Sergey 
2421a6dc60ffSKozlov Sergey static int cxd2841er_sleep_tc_to_active_t_band(
2422a6dc60ffSKozlov Sergey 		struct cxd2841er_priv *priv, u32 bandwidth)
2423a6dc60ffSKozlov Sergey {
242483808c23SAbylay Ospan 	u8 data[MAX_WRITE_REGSIZE];
24254b866c4eSDaniel Scheller 	u32 iffreq, ifhz;
24263802c1bcSColin Ian King 	static const u8 nominalRate8bw[3][5] = {
242783808c23SAbylay Ospan 		/* TRCG Nominal Rate [37:0] */
242883808c23SAbylay Ospan 		{0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
242983808c23SAbylay Ospan 		{0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
243083808c23SAbylay Ospan 		{0x11, 0xF0, 0x00, 0x00, 0x00}  /* 41MHz XTal */
243183808c23SAbylay Ospan 	};
24323802c1bcSColin Ian King 	static const u8 nominalRate7bw[3][5] = {
243383808c23SAbylay Ospan 		/* TRCG Nominal Rate [37:0] */
243483808c23SAbylay Ospan 		{0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
243583808c23SAbylay Ospan 		{0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
243683808c23SAbylay Ospan 		{0x14, 0x80, 0x00, 0x00, 0x00}  /* 41MHz XTal */
243783808c23SAbylay Ospan 	};
24383802c1bcSColin Ian King 	static const u8 nominalRate6bw[3][5] = {
243983808c23SAbylay Ospan 		/* TRCG Nominal Rate [37:0] */
244083808c23SAbylay Ospan 		{0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
244183808c23SAbylay Ospan 		{0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
244283808c23SAbylay Ospan 		{0x17, 0xEA, 0xAA, 0xAA, 0xAA}  /* 41MHz XTal */
244383808c23SAbylay Ospan 	};
24443802c1bcSColin Ian King 	static const u8 nominalRate5bw[3][5] = {
244583808c23SAbylay Ospan 		/* TRCG Nominal Rate [37:0] */
244683808c23SAbylay Ospan 		{0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
244783808c23SAbylay Ospan 		{0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
244883808c23SAbylay Ospan 		{0x1C, 0xB3, 0x33, 0x33, 0x33}  /* 41MHz XTal */
244983808c23SAbylay Ospan 	};
2450a6dc60ffSKozlov Sergey 
24513802c1bcSColin Ian King 	static const u8 itbCoef8bw[3][14] = {
245283808c23SAbylay Ospan 		{0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
245383808c23SAbylay Ospan 			0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
245483808c23SAbylay Ospan 		{0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29, 0xA5,
245583808c23SAbylay Ospan 			0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal   */
245683808c23SAbylay Ospan 		{0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
245783808c23SAbylay Ospan 			0x1F, 0xA8, 0x2C, 0xC8}  /* 41MHz XTal   */
245883808c23SAbylay Ospan 	};
24593802c1bcSColin Ian King 	static const u8 itbCoef7bw[3][14] = {
246083808c23SAbylay Ospan 		{0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
246183808c23SAbylay Ospan 			0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
246283808c23SAbylay Ospan 		{0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29, 0xA2,
246383808c23SAbylay Ospan 			0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal   */
246483808c23SAbylay Ospan 		{0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
246583808c23SAbylay Ospan 			0x26, 0xA9, 0x21, 0xA5}  /* 41MHz XTal   */
246683808c23SAbylay Ospan 	};
24673802c1bcSColin Ian King 	static const u8 itbCoef6bw[3][14] = {
246883808c23SAbylay Ospan 		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
246983808c23SAbylay Ospan 			0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
247083808c23SAbylay Ospan 		{0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
247183808c23SAbylay Ospan 			0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal   */
247283808c23SAbylay Ospan 		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
247383808c23SAbylay Ospan 			0x00, 0xE6, 0x23, 0xA4}  /* 41MHz XTal   */
247483808c23SAbylay Ospan 	};
24753802c1bcSColin Ian King 	static const u8 itbCoef5bw[3][14] = {
247683808c23SAbylay Ospan 		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
247783808c23SAbylay Ospan 			0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
247883808c23SAbylay Ospan 		{0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
247983808c23SAbylay Ospan 			0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal   */
248083808c23SAbylay Ospan 		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
248183808c23SAbylay Ospan 			0x00, 0xE6, 0x23, 0xA4}  /* 41MHz XTal   */
248283808c23SAbylay Ospan 	};
248383808c23SAbylay Ospan 
248483808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x13 */
2485a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
2486a6dc60ffSKozlov Sergey 	/* Echo performance optimization setting */
248783808c23SAbylay Ospan 	data[0] = 0x01;
248883808c23SAbylay Ospan 	data[1] = 0x14;
248983808c23SAbylay Ospan 	cxd2841er_write_regs(priv, I2C_SLVT, 0x9C, data, 2);
249083808c23SAbylay Ospan 
249183808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x10 */
2492a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2493a6dc60ffSKozlov Sergey 
2494a6dc60ffSKozlov Sergey 	switch (bandwidth) {
2495a6dc60ffSKozlov Sergey 	case 8000000:
249683808c23SAbylay Ospan 		/* <Timing Recovery setting> */
249783808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
249883808c23SAbylay Ospan 				0x9F, nominalRate8bw[priv->xtal], 5);
249983808c23SAbylay Ospan 		/* Group delay equaliser settings for
250083808c23SAbylay Ospan 		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
250183808c23SAbylay Ospan 		*/
25027afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
250383808c23SAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
250483808c23SAbylay Ospan 				0xA6, itbCoef8bw[priv->xtal], 14);
250583808c23SAbylay Ospan 		/* <IF freq setting> */
25064b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 4800000);
25074b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
250883808c23SAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
250983808c23SAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
251083808c23SAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
251183808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
251283808c23SAbylay Ospan 		/* System bandwidth setting */
251383808c23SAbylay Ospan 		cxd2841er_set_reg_bits(
251483808c23SAbylay Ospan 			priv, I2C_SLVT, 0xD7, 0x00, 0x07);
251583808c23SAbylay Ospan 
251683808c23SAbylay Ospan 		/* Demod core latency setting */
251783808c23SAbylay Ospan 		if (priv->xtal == SONY_XTAL_24000) {
251883808c23SAbylay Ospan 			data[0] = 0x15;
251983808c23SAbylay Ospan 			data[1] = 0x28;
252083808c23SAbylay Ospan 		} else {
252183808c23SAbylay Ospan 			data[0] = 0x01;
252283808c23SAbylay Ospan 			data[1] = 0xE0;
252383808c23SAbylay Ospan 		}
252483808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
252583808c23SAbylay Ospan 
252683808c23SAbylay Ospan 		/* Notch filter setting */
252783808c23SAbylay Ospan 		data[0] = 0x01;
252883808c23SAbylay Ospan 		data[1] = 0x02;
252983808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
253083808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
2531a6dc60ffSKozlov Sergey 		break;
2532a6dc60ffSKozlov Sergey 	case 7000000:
253383808c23SAbylay Ospan 		/* <Timing Recovery setting> */
253483808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
253583808c23SAbylay Ospan 				0x9F, nominalRate7bw[priv->xtal], 5);
253683808c23SAbylay Ospan 		/* Group delay equaliser settings for
253783808c23SAbylay Ospan 		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
253883808c23SAbylay Ospan 		*/
25397afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
254083808c23SAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
254183808c23SAbylay Ospan 				0xA6, itbCoef7bw[priv->xtal], 14);
254283808c23SAbylay Ospan 		/* <IF freq setting> */
25434b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 4200000);
25444b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
254583808c23SAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
254683808c23SAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
254783808c23SAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
254883808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
254983808c23SAbylay Ospan 		/* System bandwidth setting */
255083808c23SAbylay Ospan 		cxd2841er_set_reg_bits(
255183808c23SAbylay Ospan 			priv, I2C_SLVT, 0xD7, 0x02, 0x07);
255283808c23SAbylay Ospan 
255383808c23SAbylay Ospan 		/* Demod core latency setting */
255483808c23SAbylay Ospan 		if (priv->xtal == SONY_XTAL_24000) {
255583808c23SAbylay Ospan 			data[0] = 0x1F;
255683808c23SAbylay Ospan 			data[1] = 0xF8;
255783808c23SAbylay Ospan 		} else {
255883808c23SAbylay Ospan 			data[0] = 0x12;
255983808c23SAbylay Ospan 			data[1] = 0xF8;
256083808c23SAbylay Ospan 		}
256183808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
256283808c23SAbylay Ospan 
256383808c23SAbylay Ospan 		/* Notch filter setting */
256483808c23SAbylay Ospan 		data[0] = 0x00;
256583808c23SAbylay Ospan 		data[1] = 0x03;
256683808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
256783808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
2568a6dc60ffSKozlov Sergey 		break;
2569a6dc60ffSKozlov Sergey 	case 6000000:
257083808c23SAbylay Ospan 		/* <Timing Recovery setting> */
257183808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
257283808c23SAbylay Ospan 				0x9F, nominalRate6bw[priv->xtal], 5);
257383808c23SAbylay Ospan 		/* Group delay equaliser settings for
257483808c23SAbylay Ospan 		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
257583808c23SAbylay Ospan 		*/
25767afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
257783808c23SAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
257883808c23SAbylay Ospan 				0xA6, itbCoef6bw[priv->xtal], 14);
257983808c23SAbylay Ospan 		/* <IF freq setting> */
25804b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 3600000);
25814b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
258283808c23SAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
258383808c23SAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
258483808c23SAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
258583808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
258683808c23SAbylay Ospan 		/* System bandwidth setting */
258783808c23SAbylay Ospan 		cxd2841er_set_reg_bits(
258883808c23SAbylay Ospan 			priv, I2C_SLVT, 0xD7, 0x04, 0x07);
258983808c23SAbylay Ospan 
259083808c23SAbylay Ospan 		/* Demod core latency setting */
259183808c23SAbylay Ospan 		if (priv->xtal == SONY_XTAL_24000) {
259283808c23SAbylay Ospan 			data[0] = 0x25;
259383808c23SAbylay Ospan 			data[1] = 0x4C;
259483808c23SAbylay Ospan 		} else {
259583808c23SAbylay Ospan 			data[0] = 0x1F;
259683808c23SAbylay Ospan 			data[1] = 0xDC;
259783808c23SAbylay Ospan 		}
259883808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
259983808c23SAbylay Ospan 
260083808c23SAbylay Ospan 		/* Notch filter setting */
260183808c23SAbylay Ospan 		data[0] = 0x00;
260283808c23SAbylay Ospan 		data[1] = 0x03;
260383808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
260483808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
2605a6dc60ffSKozlov Sergey 		break;
2606a6dc60ffSKozlov Sergey 	case 5000000:
260783808c23SAbylay Ospan 		/* <Timing Recovery setting> */
260883808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
260983808c23SAbylay Ospan 				0x9F, nominalRate5bw[priv->xtal], 5);
261083808c23SAbylay Ospan 		/* Group delay equaliser settings for
261183808c23SAbylay Ospan 		 * ASCOT2D, ASCOT2E and ASCOT3 tuners
261283808c23SAbylay Ospan 		*/
26137afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
261483808c23SAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
261583808c23SAbylay Ospan 				0xA6, itbCoef5bw[priv->xtal], 14);
261683808c23SAbylay Ospan 		/* <IF freq setting> */
26174b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 3600000);
26184b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
261983808c23SAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
262083808c23SAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
262183808c23SAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
262283808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
262383808c23SAbylay Ospan 		/* System bandwidth setting */
262483808c23SAbylay Ospan 		cxd2841er_set_reg_bits(
262583808c23SAbylay Ospan 			priv, I2C_SLVT, 0xD7, 0x06, 0x07);
262683808c23SAbylay Ospan 
262783808c23SAbylay Ospan 		/* Demod core latency setting */
262883808c23SAbylay Ospan 		if (priv->xtal == SONY_XTAL_24000) {
262983808c23SAbylay Ospan 			data[0] = 0x2C;
263083808c23SAbylay Ospan 			data[1] = 0xC2;
263183808c23SAbylay Ospan 		} else {
263283808c23SAbylay Ospan 			data[0] = 0x26;
263383808c23SAbylay Ospan 			data[1] = 0x3C;
263483808c23SAbylay Ospan 		}
263583808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
263683808c23SAbylay Ospan 
263783808c23SAbylay Ospan 		/* Notch filter setting */
263883808c23SAbylay Ospan 		data[0] = 0x00;
263983808c23SAbylay Ospan 		data[1] = 0x03;
264083808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
264183808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
264283808c23SAbylay Ospan 		break;
264383808c23SAbylay Ospan 	}
264483808c23SAbylay Ospan 
264583808c23SAbylay Ospan 	return 0;
264683808c23SAbylay Ospan }
264783808c23SAbylay Ospan 
264883808c23SAbylay Ospan static int cxd2841er_sleep_tc_to_active_i_band(
264983808c23SAbylay Ospan 		struct cxd2841er_priv *priv, u32 bandwidth)
265083808c23SAbylay Ospan {
26514b866c4eSDaniel Scheller 	u32 iffreq, ifhz;
265283808c23SAbylay Ospan 	u8 data[3];
265383808c23SAbylay Ospan 
265483808c23SAbylay Ospan 	/* TRCG Nominal Rate */
26553802c1bcSColin Ian King 	static const u8 nominalRate8bw[3][5] = {
265683808c23SAbylay Ospan 		{0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
265783808c23SAbylay Ospan 		{0x11, 0xB8, 0x00, 0x00, 0x00}, /* 24MHz XTal */
265883808c23SAbylay Ospan 		{0x00, 0x00, 0x00, 0x00, 0x00}  /* 41MHz XTal */
265983808c23SAbylay Ospan 	};
266083808c23SAbylay Ospan 
26613802c1bcSColin Ian King 	static const u8 nominalRate7bw[3][5] = {
266283808c23SAbylay Ospan 		{0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
266383808c23SAbylay Ospan 		{0x14, 0x40, 0x00, 0x00, 0x00}, /* 24MHz XTal */
266483808c23SAbylay Ospan 		{0x00, 0x00, 0x00, 0x00, 0x00}  /* 41MHz XTal */
266583808c23SAbylay Ospan 	};
266683808c23SAbylay Ospan 
26673802c1bcSColin Ian King 	static const u8 nominalRate6bw[3][5] = {
266883808c23SAbylay Ospan 		{0x14, 0x2E, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
266983808c23SAbylay Ospan 		{0x17, 0xA0, 0x00, 0x00, 0x00}, /* 24MHz XTal */
267083808c23SAbylay Ospan 		{0x14, 0x2E, 0x00, 0x00, 0x00}  /* 41MHz XTal */
267183808c23SAbylay Ospan 	};
267283808c23SAbylay Ospan 
26733802c1bcSColin Ian King 	static const u8 itbCoef8bw[3][14] = {
267483808c23SAbylay Ospan 		{0x00}, /* 20.5MHz XTal */
267583808c23SAbylay Ospan 		{0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29,
267683808c23SAbylay Ospan 			0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz Xtal */
267783808c23SAbylay Ospan 		{0x0}, /* 41MHz XTal   */
267883808c23SAbylay Ospan 	};
267983808c23SAbylay Ospan 
26803802c1bcSColin Ian King 	static const u8 itbCoef7bw[3][14] = {
268183808c23SAbylay Ospan 		{0x00}, /* 20.5MHz XTal */
268283808c23SAbylay Ospan 		{0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29,
268383808c23SAbylay Ospan 			0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz Xtal */
268483808c23SAbylay Ospan 		{0x00}, /* 41MHz XTal   */
268583808c23SAbylay Ospan 	};
268683808c23SAbylay Ospan 
26873802c1bcSColin Ian King 	static const u8 itbCoef6bw[3][14] = {
268883808c23SAbylay Ospan 		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
268983808c23SAbylay Ospan 			0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
269083808c23SAbylay Ospan 		{0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29,
269183808c23SAbylay Ospan 			0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz Xtal   */
269283808c23SAbylay Ospan 		{0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
269383808c23SAbylay Ospan 			0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 41MHz XTal   */
269483808c23SAbylay Ospan 	};
269583808c23SAbylay Ospan 
269683808c23SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s() bandwidth=%u\n", __func__, bandwidth);
269783808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x10 */
269883808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
269983808c23SAbylay Ospan 
270083808c23SAbylay Ospan 	/*  20.5/41MHz Xtal support is not available
270183808c23SAbylay Ospan 	 *  on ISDB-T 7MHzBW and 8MHzBW
270283808c23SAbylay Ospan 	*/
270383808c23SAbylay Ospan 	if (priv->xtal != SONY_XTAL_24000 && bandwidth > 6000000) {
270483808c23SAbylay Ospan 		dev_err(&priv->i2c->dev,
270583808c23SAbylay Ospan 			"%s(): bandwidth %d supported only for 24MHz xtal\n",
270683808c23SAbylay Ospan 			__func__, bandwidth);
270783808c23SAbylay Ospan 		return -EINVAL;
270883808c23SAbylay Ospan 	}
270983808c23SAbylay Ospan 
271083808c23SAbylay Ospan 	switch (bandwidth) {
271183808c23SAbylay Ospan 	case 8000000:
271283808c23SAbylay Ospan 		/* TRCG Nominal Rate */
271383808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
271483808c23SAbylay Ospan 				0x9F, nominalRate8bw[priv->xtal], 5);
271583808c23SAbylay Ospan 		/*  Group delay equaliser settings for ASCOT tuners optimized */
27167afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
271783808c23SAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
271883808c23SAbylay Ospan 				0xA6, itbCoef8bw[priv->xtal], 14);
271983808c23SAbylay Ospan 
272083808c23SAbylay Ospan 		/* IF freq setting */
27214b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 4750000);
27224b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
272383808c23SAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
272483808c23SAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
272583808c23SAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
272683808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
272783808c23SAbylay Ospan 
272883808c23SAbylay Ospan 		/* System bandwidth setting */
272983808c23SAbylay Ospan 		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x0, 0x7);
273083808c23SAbylay Ospan 
273183808c23SAbylay Ospan 		/* Demod core latency setting */
273283808c23SAbylay Ospan 		data[0] = 0x13;
273383808c23SAbylay Ospan 		data[1] = 0xFC;
273483808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
273583808c23SAbylay Ospan 
273683808c23SAbylay Ospan 		/* Acquisition optimization setting */
273783808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
273883808c23SAbylay Ospan 		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
273983808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
274083808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x03);
274183808c23SAbylay Ospan 		break;
274283808c23SAbylay Ospan 	case 7000000:
274383808c23SAbylay Ospan 		/* TRCG Nominal Rate */
274483808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
274583808c23SAbylay Ospan 				0x9F, nominalRate7bw[priv->xtal], 5);
274683808c23SAbylay Ospan 		/*  Group delay equaliser settings for ASCOT tuners optimized */
27477afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
274883808c23SAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
274983808c23SAbylay Ospan 				0xA6, itbCoef7bw[priv->xtal], 14);
275083808c23SAbylay Ospan 
275183808c23SAbylay Ospan 		/* IF freq setting */
27524b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 4150000);
27534b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
275483808c23SAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
275583808c23SAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
275683808c23SAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
275783808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
275883808c23SAbylay Ospan 
275983808c23SAbylay Ospan 		/* System bandwidth setting */
276083808c23SAbylay Ospan 		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x02, 0x7);
276183808c23SAbylay Ospan 
276283808c23SAbylay Ospan 		/* Demod core latency setting */
276383808c23SAbylay Ospan 		data[0] = 0x1A;
276483808c23SAbylay Ospan 		data[1] = 0xFA;
276583808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
276683808c23SAbylay Ospan 
276783808c23SAbylay Ospan 		/* Acquisition optimization setting */
276883808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
276983808c23SAbylay Ospan 		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
277083808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
277183808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
277283808c23SAbylay Ospan 		break;
277383808c23SAbylay Ospan 	case 6000000:
277483808c23SAbylay Ospan 		/* TRCG Nominal Rate */
277583808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT,
277683808c23SAbylay Ospan 				0x9F, nominalRate6bw[priv->xtal], 5);
277783808c23SAbylay Ospan 		/*  Group delay equaliser settings for ASCOT tuners optimized */
27787afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
277983808c23SAbylay Ospan 			cxd2841er_write_regs(priv, I2C_SLVT,
278083808c23SAbylay Ospan 				0xA6, itbCoef6bw[priv->xtal], 14);
278183808c23SAbylay Ospan 
278283808c23SAbylay Ospan 		/* IF freq setting */
27834b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 3550000);
27844b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, ifhz);
278583808c23SAbylay Ospan 		data[0] = (u8) ((iffreq >> 16) & 0xff);
278683808c23SAbylay Ospan 		data[1] = (u8)((iffreq >> 8) & 0xff);
278783808c23SAbylay Ospan 		data[2] = (u8)(iffreq & 0xff);
278883808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
278983808c23SAbylay Ospan 
279083808c23SAbylay Ospan 		/* System bandwidth setting */
279183808c23SAbylay Ospan 		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x04, 0x7);
279283808c23SAbylay Ospan 
279383808c23SAbylay Ospan 		/* Demod core latency setting */
279483808c23SAbylay Ospan 		if (priv->xtal == SONY_XTAL_24000) {
279583808c23SAbylay Ospan 			data[0] = 0x1F;
279683808c23SAbylay Ospan 			data[1] = 0x79;
279783808c23SAbylay Ospan 		} else {
279883808c23SAbylay Ospan 			data[0] = 0x1A;
279983808c23SAbylay Ospan 			data[1] = 0xE2;
280083808c23SAbylay Ospan 		}
280183808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
280283808c23SAbylay Ospan 
280383808c23SAbylay Ospan 		/* Acquisition optimization setting */
280483808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
280583808c23SAbylay Ospan 		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x07, 0x07);
280683808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
280783808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
2808a6dc60ffSKozlov Sergey 		break;
2809a6dc60ffSKozlov Sergey 	default:
2810a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
2811a6dc60ffSKozlov Sergey 				__func__, bandwidth);
2812a6dc60ffSKozlov Sergey 		return -EINVAL;
2813a6dc60ffSKozlov Sergey 	}
2814a6dc60ffSKozlov Sergey 	return 0;
2815a6dc60ffSKozlov Sergey }
2816a6dc60ffSKozlov Sergey 
2817a6dc60ffSKozlov Sergey static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
2818a6dc60ffSKozlov Sergey 					       u32 bandwidth)
2819a6dc60ffSKozlov Sergey {
2820a6dc60ffSKozlov Sergey 	u8 bw7_8mhz_b10_a6[] = {
2821a6dc60ffSKozlov Sergey 		0x2D, 0xC7, 0x04, 0xF4, 0x07, 0xC5, 0x2A, 0xB8,
2822a6dc60ffSKozlov Sergey 		0x27, 0x9E, 0x27, 0xA4, 0x29, 0xAB };
2823a6dc60ffSKozlov Sergey 	u8 bw6mhz_b10_a6[] = {
2824a6dc60ffSKozlov Sergey 		0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2825a6dc60ffSKozlov Sergey 		0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4 };
2826a6dc60ffSKozlov Sergey 	u8 b10_b6[3];
28274b866c4eSDaniel Scheller 	u32 iffreq, ifhz;
2828a6dc60ffSKozlov Sergey 
2829af4cc462SAbylay Ospan 	if (bandwidth != 6000000 &&
2830af4cc462SAbylay Ospan 			bandwidth != 7000000 &&
2831af4cc462SAbylay Ospan 			bandwidth != 8000000) {
2832af4cc462SAbylay Ospan 		dev_info(&priv->i2c->dev, "%s(): unsupported bandwidth %d. Forcing 8Mhz!\n",
2833af4cc462SAbylay Ospan 				__func__, bandwidth);
2834af4cc462SAbylay Ospan 		bandwidth = 8000000;
2835af4cc462SAbylay Ospan 	}
2836af4cc462SAbylay Ospan 
28373f3b48a0SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s() bw=%d\n", __func__, bandwidth);
2838a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2839a6dc60ffSKozlov Sergey 	switch (bandwidth) {
2840a6dc60ffSKozlov Sergey 	case 8000000:
2841a6dc60ffSKozlov Sergey 	case 7000000:
28427afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
2843a6dc60ffSKozlov Sergey 			cxd2841er_write_regs(
2844a6dc60ffSKozlov Sergey 				priv, I2C_SLVT, 0xa6,
2845a6dc60ffSKozlov Sergey 				bw7_8mhz_b10_a6, sizeof(bw7_8mhz_b10_a6));
28464b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 4900000);
28474b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq(ifhz);
2848a6dc60ffSKozlov Sergey 		break;
2849a6dc60ffSKozlov Sergey 	case 6000000:
28507afe510aSDaniel Scheller 		if (priv->flags & CXD2841ER_ASCOT)
2851a6dc60ffSKozlov Sergey 			cxd2841er_write_regs(
2852a6dc60ffSKozlov Sergey 				priv, I2C_SLVT, 0xa6,
2853a6dc60ffSKozlov Sergey 				bw6mhz_b10_a6, sizeof(bw6mhz_b10_a6));
28544b866c4eSDaniel Scheller 		ifhz = cxd2841er_get_if_hz(priv, 3700000);
28554b866c4eSDaniel Scheller 		iffreq = cxd2841er_calc_iffreq(ifhz);
2856a6dc60ffSKozlov Sergey 		break;
2857a6dc60ffSKozlov Sergey 	default:
28583f3b48a0SAbylay Ospan 		dev_err(&priv->i2c->dev, "%s(): unsupported bandwidth %d\n",
2859a6dc60ffSKozlov Sergey 			__func__, bandwidth);
2860a6dc60ffSKozlov Sergey 		return -EINVAL;
2861a6dc60ffSKozlov Sergey 	}
2862a6dc60ffSKozlov Sergey 	/* <IF freq setting> */
2863a6dc60ffSKozlov Sergey 	b10_b6[0] = (u8) ((iffreq >> 16) & 0xff);
2864a6dc60ffSKozlov Sergey 	b10_b6[1] = (u8)((iffreq >> 8) & 0xff);
2865a6dc60ffSKozlov Sergey 	b10_b6[2] = (u8)(iffreq & 0xff);
2866a6dc60ffSKozlov Sergey 	cxd2841er_write_regs(priv, I2C_SLVT, 0xb6, b10_b6, sizeof(b10_b6));
2867a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x11 */
2868a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2869a6dc60ffSKozlov Sergey 	switch (bandwidth) {
2870a6dc60ffSKozlov Sergey 	case 8000000:
2871a6dc60ffSKozlov Sergey 	case 7000000:
2872a6dc60ffSKozlov Sergey 		cxd2841er_set_reg_bits(
2873a6dc60ffSKozlov Sergey 			priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
2874a6dc60ffSKozlov Sergey 		break;
2875a6dc60ffSKozlov Sergey 	case 6000000:
2876a6dc60ffSKozlov Sergey 		cxd2841er_set_reg_bits(
2877a6dc60ffSKozlov Sergey 			priv, I2C_SLVT, 0xa3, 0x14, 0x1f);
2878a6dc60ffSKozlov Sergey 		break;
2879a6dc60ffSKozlov Sergey 	}
2880a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x40 */
2881a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
2882a6dc60ffSKozlov Sergey 	switch (bandwidth) {
2883a6dc60ffSKozlov Sergey 	case 8000000:
2884a6dc60ffSKozlov Sergey 		cxd2841er_set_reg_bits(
2885a6dc60ffSKozlov Sergey 			priv, I2C_SLVT, 0x26, 0x0b, 0x0f);
2886a6dc60ffSKozlov Sergey 		cxd2841er_write_reg(priv, I2C_SLVT,  0x27, 0x3e);
2887a6dc60ffSKozlov Sergey 		break;
2888a6dc60ffSKozlov Sergey 	case 7000000:
2889a6dc60ffSKozlov Sergey 		cxd2841er_set_reg_bits(
2890a6dc60ffSKozlov Sergey 			priv, I2C_SLVT, 0x26, 0x09, 0x0f);
2891a6dc60ffSKozlov Sergey 		cxd2841er_write_reg(priv, I2C_SLVT,  0x27, 0xd6);
2892a6dc60ffSKozlov Sergey 		break;
2893a6dc60ffSKozlov Sergey 	case 6000000:
2894a6dc60ffSKozlov Sergey 		cxd2841er_set_reg_bits(
2895a6dc60ffSKozlov Sergey 			priv, I2C_SLVT, 0x26, 0x08, 0x0f);
2896a6dc60ffSKozlov Sergey 		cxd2841er_write_reg(priv, I2C_SLVT,  0x27, 0x6e);
2897a6dc60ffSKozlov Sergey 		break;
2898a6dc60ffSKozlov Sergey 	}
2899a6dc60ffSKozlov Sergey 	return 0;
2900a6dc60ffSKozlov Sergey }
2901a6dc60ffSKozlov Sergey 
2902a6dc60ffSKozlov Sergey static int cxd2841er_sleep_tc_to_active_t(struct cxd2841er_priv *priv,
2903a6dc60ffSKozlov Sergey 					  u32 bandwidth)
2904a6dc60ffSKozlov Sergey {
2905a6dc60ffSKozlov Sergey 	u8 data[2] = { 0x09, 0x54 };
290683808c23SAbylay Ospan 	u8 data24m[3] = {0xDC, 0x6C, 0x00};
2907a6dc60ffSKozlov Sergey 
2908a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2909a6dc60ffSKozlov Sergey 	cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
2910a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
2911a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
2912a6dc60ffSKozlov Sergey 	/* Set demod mode */
2913a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
2914a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
2915a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2916a6dc60ffSKozlov Sergey 	/* Enable demod clock */
2917a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
2918a6dc60ffSKozlov Sergey 	/* Disable RF level monitor */
2919a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
2920a6dc60ffSKozlov Sergey 	/* Enable ADC clock */
2921a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
2922a6dc60ffSKozlov Sergey 	/* Enable ADC 1 */
2923a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
292483808c23SAbylay Ospan 	/* Enable ADC 2 & 3 */
292583808c23SAbylay Ospan 	if (priv->xtal == SONY_XTAL_41000) {
292683808c23SAbylay Ospan 		data[0] = 0x0A;
292783808c23SAbylay Ospan 		data[1] = 0xD4;
292883808c23SAbylay Ospan 	}
2929a6dc60ffSKozlov Sergey 	cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
2930a6dc60ffSKozlov Sergey 	/* Enable ADC 4 */
2931a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
2932a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x10 */
2933a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2934a6dc60ffSKozlov Sergey 	/* IFAGC gain settings */
2935a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
2936a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x11 */
2937a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2938a6dc60ffSKozlov Sergey 	/* BBAGC TARGET level setting */
2939a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
2940a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x10 */
2941a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
29427afe510aSDaniel Scheller 	/* ASCOT setting */
29437afe510aSDaniel Scheller 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5,
29447afe510aSDaniel Scheller 		((priv->flags & CXD2841ER_ASCOT) ? 0x01 : 0x00), 0x01);
2945a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x18 */
2946a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
2947a6dc60ffSKozlov Sergey 	/* Pre-RS BER moniter setting */
2948a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x36, 0x40, 0x07);
2949a6dc60ffSKozlov Sergey 	/* FEC Auto Recovery setting */
2950a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
2951a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x01, 0x01);
2952a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
2953a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2954a6dc60ffSKozlov Sergey 	/* TSIF setting */
2955a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
2956a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
295783808c23SAbylay Ospan 
295883808c23SAbylay Ospan 	if (priv->xtal == SONY_XTAL_24000) {
295983808c23SAbylay Ospan 		/* Set SLV-T Bank : 0x10 */
296083808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
296183808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0xBF, 0x60);
296283808c23SAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
296383808c23SAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data24m, 3);
296483808c23SAbylay Ospan 	}
296583808c23SAbylay Ospan 
2966a6dc60ffSKozlov Sergey 	cxd2841er_sleep_tc_to_active_t_band(priv, bandwidth);
2967a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
2968a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2969a6dc60ffSKozlov Sergey 	/* Disable HiZ Setting 1 */
2970a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
2971a6dc60ffSKozlov Sergey 	/* Disable HiZ Setting 2 */
2972a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
2973a6dc60ffSKozlov Sergey 	priv->state = STATE_ACTIVE_TC;
2974a6dc60ffSKozlov Sergey 	return 0;
2975a6dc60ffSKozlov Sergey }
2976a6dc60ffSKozlov Sergey 
2977a6dc60ffSKozlov Sergey static int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv,
2978a6dc60ffSKozlov Sergey 					   u32 bandwidth)
2979a6dc60ffSKozlov Sergey {
29806c77161aSAbylay Ospan 	u8 data[MAX_WRITE_REGSIZE];
2981a6dc60ffSKozlov Sergey 
2982a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2983a6dc60ffSKozlov Sergey 	cxd2841er_set_ts_clock_mode(priv, SYS_DVBT2);
2984a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
2985a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
2986a6dc60ffSKozlov Sergey 	/* Set demod mode */
2987a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x02);
2988a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
2989a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2990a6dc60ffSKozlov Sergey 	/* Enable demod clock */
2991a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
2992a6dc60ffSKozlov Sergey 	/* Disable RF level monitor */
29936c77161aSAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
2994a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
2995a6dc60ffSKozlov Sergey 	/* Enable ADC clock */
2996a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
2997a6dc60ffSKozlov Sergey 	/* Enable ADC 1 */
2998a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
29996c77161aSAbylay Ospan 
30006c77161aSAbylay Ospan 	if (priv->xtal == SONY_XTAL_41000) {
30016c77161aSAbylay Ospan 		data[0] = 0x0A;
30026c77161aSAbylay Ospan 		data[1] = 0xD4;
30036c77161aSAbylay Ospan 	} else {
30046c77161aSAbylay Ospan 		data[0] = 0x09;
30056c77161aSAbylay Ospan 		data[1] = 0x54;
30066c77161aSAbylay Ospan 	}
30076c77161aSAbylay Ospan 
3008a6dc60ffSKozlov Sergey 	cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
3009a6dc60ffSKozlov Sergey 	/* Enable ADC 4 */
3010a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
3011a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x10 */
3012a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3013a6dc60ffSKozlov Sergey 	/* IFAGC gain settings */
3014a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
3015a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x11 */
3016a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
3017a6dc60ffSKozlov Sergey 	/* BBAGC TARGET level setting */
3018a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
3019a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x10 */
3020a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
30217afe510aSDaniel Scheller 	/* ASCOT setting */
30227afe510aSDaniel Scheller 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5,
30237afe510aSDaniel Scheller 		((priv->flags & CXD2841ER_ASCOT) ? 0x01 : 0x00), 0x01);
3024a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x20 */
3025a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
3026a6dc60ffSKozlov Sergey 	/* Acquisition optimization setting */
3027a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x8b, 0x3c);
3028a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x2b */
3029a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
3030a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x76, 0x20, 0x70);
30316c77161aSAbylay Ospan 	/* Set SLV-T Bank : 0x23 */
30326c77161aSAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
30336c77161aSAbylay Ospan 	/* L1 Control setting */
30346c77161aSAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE6, 0x00, 0x03);
3035a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
3036a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3037a6dc60ffSKozlov Sergey 	/* TSIF setting */
3038a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
3039a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
3040a6dc60ffSKozlov Sergey 	/* DVB-T2 initial setting */
3041a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
3042a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x10);
3043a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x34);
3044a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
3045a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xd8);
3046a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x2a */
3047a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
3048a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x04, 0x0f);
3049a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x2b */
3050a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
3051a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x20, 0x3f);
3052a6dc60ffSKozlov Sergey 
30536c77161aSAbylay Ospan 	/* 24MHz Xtal setting */
30546c77161aSAbylay Ospan 	if (priv->xtal == SONY_XTAL_24000) {
30556c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x11 */
30566c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
30576c77161aSAbylay Ospan 		data[0] = 0xEB;
30586c77161aSAbylay Ospan 		data[1] = 0x03;
30596c77161aSAbylay Ospan 		data[2] = 0x3B;
30606c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0x33, data, 3);
30616c77161aSAbylay Ospan 
30626c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x20 */
30636c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
30646c77161aSAbylay Ospan 		data[0] = 0x5E;
30656c77161aSAbylay Ospan 		data[1] = 0x5E;
30666c77161aSAbylay Ospan 		data[2] = 0x47;
30676c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0x95, data, 3);
30686c77161aSAbylay Ospan 
30696c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x99, 0x18);
30706c77161aSAbylay Ospan 
30716c77161aSAbylay Ospan 		data[0] = 0x3F;
30726c77161aSAbylay Ospan 		data[1] = 0xFF;
30736c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
30746c77161aSAbylay Ospan 
30756c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x24 */
30766c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
30776c77161aSAbylay Ospan 		data[0] = 0x0B;
30786c77161aSAbylay Ospan 		data[1] = 0x72;
30796c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0x34, data, 2);
30806c77161aSAbylay Ospan 
30816c77161aSAbylay Ospan 		data[0] = 0x93;
30826c77161aSAbylay Ospan 		data[1] = 0xF3;
30836c77161aSAbylay Ospan 		data[2] = 0x00;
30846c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xD2, data, 3);
30856c77161aSAbylay Ospan 
30866c77161aSAbylay Ospan 		data[0] = 0x05;
30876c77161aSAbylay Ospan 		data[1] = 0xB8;
30886c77161aSAbylay Ospan 		data[2] = 0xD8;
30896c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0xDD, data, 3);
30906c77161aSAbylay Ospan 
30916c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0xE0, 0x00);
30926c77161aSAbylay Ospan 
30936c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x25 */
30946c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x25);
30956c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0xED, 0x60);
30966c77161aSAbylay Ospan 
30976c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x27 */
30986c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
30996c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0xFA, 0x34);
31006c77161aSAbylay Ospan 
31016c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x2B */
31026c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2B);
31036c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x4B, 0x2F);
31046c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x9E, 0x0E);
31056c77161aSAbylay Ospan 
31066c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x2D */
31076c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2D);
31086c77161aSAbylay Ospan 		data[0] = 0x89;
31096c77161aSAbylay Ospan 		data[1] = 0x89;
31106c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data, 2);
31116c77161aSAbylay Ospan 
31126c77161aSAbylay Ospan 		/* Set SLV-T Bank : 0x5E */
31136c77161aSAbylay Ospan 		cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x5E);
31146c77161aSAbylay Ospan 		data[0] = 0x24;
31156c77161aSAbylay Ospan 		data[1] = 0x95;
31166c77161aSAbylay Ospan 		cxd2841er_write_regs(priv, I2C_SLVT, 0x8C, data, 2);
31176c77161aSAbylay Ospan 	}
31186c77161aSAbylay Ospan 
3119a6dc60ffSKozlov Sergey 	cxd2841er_sleep_tc_to_active_t2_band(priv, bandwidth);
3120a6dc60ffSKozlov Sergey 
3121a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
3122a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3123a6dc60ffSKozlov Sergey 	/* Disable HiZ Setting 1 */
3124a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3125a6dc60ffSKozlov Sergey 	/* Disable HiZ Setting 2 */
3126a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3127a6dc60ffSKozlov Sergey 	priv->state = STATE_ACTIVE_TC;
3128a6dc60ffSKozlov Sergey 	return 0;
3129a6dc60ffSKozlov Sergey }
3130a6dc60ffSKozlov Sergey 
313183808c23SAbylay Ospan /* ISDB-Tb part */
313283808c23SAbylay Ospan static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
313383808c23SAbylay Ospan 		u32 bandwidth)
313483808c23SAbylay Ospan {
313583808c23SAbylay Ospan 	u8 data[2] = { 0x09, 0x54 };
313683808c23SAbylay Ospan 	u8 data24m[2] = {0x60, 0x00};
313783808c23SAbylay Ospan 	u8 data24m2[3] = {0xB7, 0x1B, 0x00};
313883808c23SAbylay Ospan 
313983808c23SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
314083808c23SAbylay Ospan 	cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
314183808c23SAbylay Ospan 	/* Set SLV-X Bank : 0x00 */
314283808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
314383808c23SAbylay Ospan 	/* Set demod mode */
314483808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x06);
314583808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x00 */
314683808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
314783808c23SAbylay Ospan 	/* Enable demod clock */
314883808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
314983808c23SAbylay Ospan 	/* Enable RF level monitor */
315083808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x01);
315183808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x01);
315283808c23SAbylay Ospan 	/* Enable ADC clock */
315383808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
315483808c23SAbylay Ospan 	/* Enable ADC 1 */
315583808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
315683808c23SAbylay Ospan 	/* xtal freq 20.5MHz or 24M */
315783808c23SAbylay Ospan 	cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
315883808c23SAbylay Ospan 	/* Enable ADC 4 */
315983808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
31607afe510aSDaniel Scheller 	/* ASCOT setting */
31617afe510aSDaniel Scheller 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5,
31627afe510aSDaniel Scheller 		((priv->flags & CXD2841ER_ASCOT) ? 0x01 : 0x00), 0x01);
316383808c23SAbylay Ospan 	/* FEC Auto Recovery setting */
316483808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
316583808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x00, 0x01);
316683808c23SAbylay Ospan 	/* ISDB-T initial setting */
316783808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x00 */
316883808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
316983808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x00, 0x01);
317083808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x00, 0x01);
317183808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x10 */
317283808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
317383808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x69, 0x04, 0x07);
317483808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x6B, 0x03, 0x07);
317583808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9D, 0x50, 0xFF);
317683808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xD3, 0x06, 0x1F);
317783808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xED, 0x00, 0x01);
317883808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE2, 0xCE, 0x80);
317983808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xF2, 0x13, 0x10);
318083808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x2E, 0x3F);
318183808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x15 */
318283808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
318383808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x02, 0x03);
318483808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x1E */
318583808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x1E);
318683808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x73, 0x68, 0xFF);
318783808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x63 */
318883808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x63);
318983808c23SAbylay Ospan 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x81, 0x00, 0x01);
319083808c23SAbylay Ospan 
319183808c23SAbylay Ospan 	/* for xtal 24MHz */
319283808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x10 */
319383808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
319483808c23SAbylay Ospan 	cxd2841er_write_regs(priv, I2C_SLVT, 0xBF, data24m, 2);
319583808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x60 */
319683808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
319783808c23SAbylay Ospan 	cxd2841er_write_regs(priv, I2C_SLVT, 0xA8, data24m2, 3);
319883808c23SAbylay Ospan 
319983808c23SAbylay Ospan 	cxd2841er_sleep_tc_to_active_i_band(priv, bandwidth);
320083808c23SAbylay Ospan 	/* Set SLV-T Bank : 0x00 */
320183808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
320283808c23SAbylay Ospan 	/* Disable HiZ Setting 1 */
320383808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
320483808c23SAbylay Ospan 	/* Disable HiZ Setting 2 */
320583808c23SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
320683808c23SAbylay Ospan 	priv->state = STATE_ACTIVE_TC;
320783808c23SAbylay Ospan 	return 0;
320883808c23SAbylay Ospan }
320983808c23SAbylay Ospan 
3210a6dc60ffSKozlov Sergey static int cxd2841er_sleep_tc_to_active_c(struct cxd2841er_priv *priv,
3211a6dc60ffSKozlov Sergey 					  u32 bandwidth)
3212a6dc60ffSKozlov Sergey {
3213a6dc60ffSKozlov Sergey 	u8 data[2] = { 0x09, 0x54 };
3214a6dc60ffSKozlov Sergey 
3215a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3216a6dc60ffSKozlov Sergey 	cxd2841er_set_ts_clock_mode(priv, SYS_DVBC_ANNEX_A);
3217a6dc60ffSKozlov Sergey 	/* Set SLV-X Bank : 0x00 */
3218a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
3219a6dc60ffSKozlov Sergey 	/* Set demod mode */
3220a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x04);
3221a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
3222a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3223a6dc60ffSKozlov Sergey 	/* Enable demod clock */
3224a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
3225a6dc60ffSKozlov Sergey 	/* Disable RF level monitor */
32264a86bc10SAbylay Ospan 	cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
3227a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
3228a6dc60ffSKozlov Sergey 	/* Enable ADC clock */
3229a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
3230a6dc60ffSKozlov Sergey 	/* Enable ADC 1 */
3231a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
3232a6dc60ffSKozlov Sergey 	/* xtal freq 20.5MHz */
3233a6dc60ffSKozlov Sergey 	cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
3234a6dc60ffSKozlov Sergey 	/* Enable ADC 4 */
3235a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
3236a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x10 */
3237a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3238a6dc60ffSKozlov Sergey 	/* IFAGC gain settings */
3239a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x09, 0x1f);
3240a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x11 */
3241a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
3242a6dc60ffSKozlov Sergey 	/* BBAGC TARGET level setting */
3243a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x48);
3244a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x10 */
3245a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
32467afe510aSDaniel Scheller 	/* ASCOT setting */
32477afe510aSDaniel Scheller 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5,
32487afe510aSDaniel Scheller 		((priv->flags & CXD2841ER_ASCOT) ? 0x01 : 0x00), 0x01);
3249a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x40 */
3250a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
3251a6dc60ffSKozlov Sergey 	/* Demod setting */
3252a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc3, 0x00, 0x04);
3253a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
3254a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3255a6dc60ffSKozlov Sergey 	/* TSIF setting */
3256a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
3257a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
3258a6dc60ffSKozlov Sergey 
32593f3b48a0SAbylay Ospan 	cxd2841er_sleep_tc_to_active_c_band(priv, bandwidth);
3260a6dc60ffSKozlov Sergey 	/* Set SLV-T Bank : 0x00 */
3261a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3262a6dc60ffSKozlov Sergey 	/* Disable HiZ Setting 1 */
3263a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3264a6dc60ffSKozlov Sergey 	/* Disable HiZ Setting 2 */
3265a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3266a6dc60ffSKozlov Sergey 	priv->state = STATE_ACTIVE_TC;
3267a6dc60ffSKozlov Sergey 	return 0;
3268a6dc60ffSKozlov Sergey }
3269a6dc60ffSKozlov Sergey 
32707e3e68bcSMauro Carvalho Chehab static int cxd2841er_get_frontend(struct dvb_frontend *fe,
32717e3e68bcSMauro Carvalho Chehab 				  struct dtv_frontend_properties *p)
3272a6dc60ffSKozlov Sergey {
3273a6dc60ffSKozlov Sergey 	enum fe_status status = 0;
3274a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
3275a6dc60ffSKozlov Sergey 
3276a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3277a6dc60ffSKozlov Sergey 	if (priv->state == STATE_ACTIVE_S)
3278a6dc60ffSKozlov Sergey 		cxd2841er_read_status_s(fe, &status);
3279a6dc60ffSKozlov Sergey 	else if (priv->state == STATE_ACTIVE_TC)
3280a6dc60ffSKozlov Sergey 		cxd2841er_read_status_tc(fe, &status);
3281a6dc60ffSKozlov Sergey 
32828982735fSDaniel Scheller 	if (priv->state == STATE_ACTIVE_TC || priv->state == STATE_ACTIVE_S)
32835fda1b65SMauro Carvalho Chehab 		cxd2841er_read_signal_strength(fe);
32848982735fSDaniel Scheller 	else
32858982735fSDaniel Scheller 		p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3286d0e20e13SMauro Carvalho Chehab 
3287d0e20e13SMauro Carvalho Chehab 	if (status & FE_HAS_LOCK) {
3288f1b26622SMauro Carvalho Chehab 		cxd2841er_read_snr(fe);
3289f1b26622SMauro Carvalho Chehab 		cxd2841er_read_ucblocks(fe);
3290d0e20e13SMauro Carvalho Chehab 
3291f1b26622SMauro Carvalho Chehab 		cxd2841er_read_ber(fe);
3292a6dc60ffSKozlov Sergey 	} else {
3293a6dc60ffSKozlov Sergey 		p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3294a6dc60ffSKozlov Sergey 		p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3295a6dc60ffSKozlov Sergey 		p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
32964216be14SMauro Carvalho Chehab 		p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3297a6dc60ffSKozlov Sergey 	}
3298a6dc60ffSKozlov Sergey 	return 0;
3299a6dc60ffSKozlov Sergey }
3300a6dc60ffSKozlov Sergey 
3301a6dc60ffSKozlov Sergey static int cxd2841er_set_frontend_s(struct dvb_frontend *fe)
3302a6dc60ffSKozlov Sergey {
3303a6dc60ffSKozlov Sergey 	int ret = 0, i, timeout, carr_offset;
3304a6dc60ffSKozlov Sergey 	enum fe_status status;
3305a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
3306a6dc60ffSKozlov Sergey 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3307a6dc60ffSKozlov Sergey 	u32 symbol_rate = p->symbol_rate/1000;
3308a6dc60ffSKozlov Sergey 
330983808c23SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s(): %s frequency=%d symbol_rate=%d xtal=%d\n",
3310a6dc60ffSKozlov Sergey 		__func__,
3311a6dc60ffSKozlov Sergey 		(p->delivery_system == SYS_DVBS ? "DVB-S" : "DVB-S2"),
331283808c23SAbylay Ospan 		 p->frequency, symbol_rate, priv->xtal);
3313763f857eSDaniel Scheller 
3314763f857eSDaniel Scheller 	if (priv->flags & CXD2841ER_EARLY_TUNE)
3315763f857eSDaniel Scheller 		cxd2841er_tuner_set(fe);
3316763f857eSDaniel Scheller 
3317a6dc60ffSKozlov Sergey 	switch (priv->state) {
3318a6dc60ffSKozlov Sergey 	case STATE_SLEEP_S:
3319a6dc60ffSKozlov Sergey 		ret = cxd2841er_sleep_s_to_active_s(
3320a6dc60ffSKozlov Sergey 			priv, p->delivery_system, symbol_rate);
3321a6dc60ffSKozlov Sergey 		break;
3322a6dc60ffSKozlov Sergey 	case STATE_ACTIVE_S:
3323a6dc60ffSKozlov Sergey 		ret = cxd2841er_retune_active(priv, p);
3324a6dc60ffSKozlov Sergey 		break;
3325a6dc60ffSKozlov Sergey 	default:
3326a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3327a6dc60ffSKozlov Sergey 			__func__, priv->state);
3328a6dc60ffSKozlov Sergey 		ret = -EINVAL;
3329a6dc60ffSKozlov Sergey 		goto done;
3330a6dc60ffSKozlov Sergey 	}
3331a6dc60ffSKozlov Sergey 	if (ret) {
3332a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): tune failed\n", __func__);
3333a6dc60ffSKozlov Sergey 		goto done;
3334a6dc60ffSKozlov Sergey 	}
3335c7518d13SDaniel Scheller 
3336763f857eSDaniel Scheller 	if (!(priv->flags & CXD2841ER_EARLY_TUNE))
3337c7518d13SDaniel Scheller 		cxd2841er_tuner_set(fe);
3338c7518d13SDaniel Scheller 
3339a6dc60ffSKozlov Sergey 	cxd2841er_tune_done(priv);
3340a6dc60ffSKozlov Sergey 	timeout = ((3000000 + (symbol_rate - 1)) / symbol_rate) + 150;
3341a6dc60ffSKozlov Sergey 	for (i = 0; i < timeout / CXD2841ER_DVBS_POLLING_INVL; i++) {
3342a6dc60ffSKozlov Sergey 		usleep_range(CXD2841ER_DVBS_POLLING_INVL*1000,
3343a6dc60ffSKozlov Sergey 			(CXD2841ER_DVBS_POLLING_INVL + 2) * 1000);
3344a6dc60ffSKozlov Sergey 		cxd2841er_read_status_s(fe, &status);
3345a6dc60ffSKozlov Sergey 		if (status & FE_HAS_LOCK)
3346a6dc60ffSKozlov Sergey 			break;
3347a6dc60ffSKozlov Sergey 	}
3348a6dc60ffSKozlov Sergey 	if (status & FE_HAS_LOCK) {
3349a6dc60ffSKozlov Sergey 		if (cxd2841er_get_carrier_offset_s_s2(
3350a6dc60ffSKozlov Sergey 				priv, &carr_offset)) {
3351a6dc60ffSKozlov Sergey 			ret = -EINVAL;
3352a6dc60ffSKozlov Sergey 			goto done;
3353a6dc60ffSKozlov Sergey 		}
3354a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev, "%s(): carrier_offset=%d\n",
3355a6dc60ffSKozlov Sergey 			__func__, carr_offset);
3356a6dc60ffSKozlov Sergey 	}
3357a6dc60ffSKozlov Sergey done:
3358d0e20e13SMauro Carvalho Chehab 	/* Reset stats */
3359d0e20e13SMauro Carvalho Chehab 	p->strength.stat[0].scale = FE_SCALE_RELATIVE;
3360d0e20e13SMauro Carvalho Chehab 	p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3361d0e20e13SMauro Carvalho Chehab 	p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3362d0e20e13SMauro Carvalho Chehab 	p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
33634216be14SMauro Carvalho Chehab 	p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3364d0e20e13SMauro Carvalho Chehab 
3365a6dc60ffSKozlov Sergey 	return ret;
3366a6dc60ffSKozlov Sergey }
3367a6dc60ffSKozlov Sergey 
3368a6dc60ffSKozlov Sergey static int cxd2841er_set_frontend_tc(struct dvb_frontend *fe)
3369a6dc60ffSKozlov Sergey {
3370a6dc60ffSKozlov Sergey 	int ret = 0, timeout;
3371a6dc60ffSKozlov Sergey 	enum fe_status status;
3372a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
3373a6dc60ffSKozlov Sergey 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3374a6dc60ffSKozlov Sergey 
33753f3b48a0SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s() delivery_system=%d bandwidth_hz=%d\n",
33763f3b48a0SAbylay Ospan 		 __func__, p->delivery_system, p->bandwidth_hz);
3377763f857eSDaniel Scheller 
3378763f857eSDaniel Scheller 	if (priv->flags & CXD2841ER_EARLY_TUNE)
3379763f857eSDaniel Scheller 		cxd2841er_tuner_set(fe);
3380763f857eSDaniel Scheller 
3381a6dc60ffSKozlov Sergey 	if (p->delivery_system == SYS_DVBT) {
3382a6dc60ffSKozlov Sergey 		priv->system = SYS_DVBT;
3383a6dc60ffSKozlov Sergey 		switch (priv->state) {
3384a6dc60ffSKozlov Sergey 		case STATE_SLEEP_TC:
3385a6dc60ffSKozlov Sergey 			ret = cxd2841er_sleep_tc_to_active_t(
3386a6dc60ffSKozlov Sergey 				priv, p->bandwidth_hz);
3387a6dc60ffSKozlov Sergey 			break;
3388a6dc60ffSKozlov Sergey 		case STATE_ACTIVE_TC:
3389a6dc60ffSKozlov Sergey 			ret = cxd2841er_retune_active(priv, p);
3390a6dc60ffSKozlov Sergey 			break;
3391a6dc60ffSKozlov Sergey 		default:
3392a6dc60ffSKozlov Sergey 			dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3393a6dc60ffSKozlov Sergey 				__func__, priv->state);
3394a6dc60ffSKozlov Sergey 			ret = -EINVAL;
3395a6dc60ffSKozlov Sergey 		}
3396a6dc60ffSKozlov Sergey 	} else if (p->delivery_system == SYS_DVBT2) {
3397a6dc60ffSKozlov Sergey 		priv->system = SYS_DVBT2;
3398a6dc60ffSKozlov Sergey 		cxd2841er_dvbt2_set_plp_config(priv,
3399a6dc60ffSKozlov Sergey 			(int)(p->stream_id > 255), p->stream_id);
3400a6dc60ffSKozlov Sergey 		cxd2841er_dvbt2_set_profile(priv, DVBT2_PROFILE_BASE);
3401a6dc60ffSKozlov Sergey 		switch (priv->state) {
3402a6dc60ffSKozlov Sergey 		case STATE_SLEEP_TC:
3403a6dc60ffSKozlov Sergey 			ret = cxd2841er_sleep_tc_to_active_t2(priv,
3404a6dc60ffSKozlov Sergey 				p->bandwidth_hz);
3405a6dc60ffSKozlov Sergey 			break;
3406a6dc60ffSKozlov Sergey 		case STATE_ACTIVE_TC:
3407a6dc60ffSKozlov Sergey 			ret = cxd2841er_retune_active(priv, p);
3408a6dc60ffSKozlov Sergey 			break;
3409a6dc60ffSKozlov Sergey 		default:
3410a6dc60ffSKozlov Sergey 			dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3411a6dc60ffSKozlov Sergey 				__func__, priv->state);
3412a6dc60ffSKozlov Sergey 			ret = -EINVAL;
3413a6dc60ffSKozlov Sergey 		}
341483808c23SAbylay Ospan 	} else if (p->delivery_system == SYS_ISDBT) {
341583808c23SAbylay Ospan 		priv->system = SYS_ISDBT;
341683808c23SAbylay Ospan 		switch (priv->state) {
341783808c23SAbylay Ospan 		case STATE_SLEEP_TC:
341883808c23SAbylay Ospan 			ret = cxd2841er_sleep_tc_to_active_i(
341983808c23SAbylay Ospan 					priv, p->bandwidth_hz);
342083808c23SAbylay Ospan 			break;
342183808c23SAbylay Ospan 		case STATE_ACTIVE_TC:
342283808c23SAbylay Ospan 			ret = cxd2841er_retune_active(priv, p);
342383808c23SAbylay Ospan 			break;
342483808c23SAbylay Ospan 		default:
342583808c23SAbylay Ospan 			dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
342683808c23SAbylay Ospan 					__func__, priv->state);
342783808c23SAbylay Ospan 			ret = -EINVAL;
342883808c23SAbylay Ospan 		}
3429a6dc60ffSKozlov Sergey 	} else if (p->delivery_system == SYS_DVBC_ANNEX_A ||
3430a6dc60ffSKozlov Sergey 			p->delivery_system == SYS_DVBC_ANNEX_C) {
3431a6dc60ffSKozlov Sergey 		priv->system = SYS_DVBC_ANNEX_A;
34323f3b48a0SAbylay Ospan 		/* correct bandwidth */
34333f3b48a0SAbylay Ospan 		if (p->bandwidth_hz != 6000000 &&
34343f3b48a0SAbylay Ospan 				p->bandwidth_hz != 7000000 &&
34353f3b48a0SAbylay Ospan 				p->bandwidth_hz != 8000000) {
34363f3b48a0SAbylay Ospan 			p->bandwidth_hz = 8000000;
34373f3b48a0SAbylay Ospan 			dev_dbg(&priv->i2c->dev, "%s(): forcing bandwidth to %d\n",
34383f3b48a0SAbylay Ospan 					__func__, p->bandwidth_hz);
34393f3b48a0SAbylay Ospan 		}
34403f3b48a0SAbylay Ospan 
3441a6dc60ffSKozlov Sergey 		switch (priv->state) {
3442a6dc60ffSKozlov Sergey 		case STATE_SLEEP_TC:
3443a6dc60ffSKozlov Sergey 			ret = cxd2841er_sleep_tc_to_active_c(
3444a6dc60ffSKozlov Sergey 				priv, p->bandwidth_hz);
3445a6dc60ffSKozlov Sergey 			break;
3446a6dc60ffSKozlov Sergey 		case STATE_ACTIVE_TC:
3447a6dc60ffSKozlov Sergey 			ret = cxd2841er_retune_active(priv, p);
3448a6dc60ffSKozlov Sergey 			break;
3449a6dc60ffSKozlov Sergey 		default:
3450a6dc60ffSKozlov Sergey 			dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3451a6dc60ffSKozlov Sergey 				__func__, priv->state);
3452a6dc60ffSKozlov Sergey 			ret = -EINVAL;
3453a6dc60ffSKozlov Sergey 		}
3454a6dc60ffSKozlov Sergey 	} else {
3455a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
3456a6dc60ffSKozlov Sergey 			"%s(): invalid delivery system %d\n",
3457a6dc60ffSKozlov Sergey 			__func__, p->delivery_system);
3458a6dc60ffSKozlov Sergey 		ret = -EINVAL;
3459a6dc60ffSKozlov Sergey 	}
3460a6dc60ffSKozlov Sergey 	if (ret)
3461a6dc60ffSKozlov Sergey 		goto done;
3462c7518d13SDaniel Scheller 
3463763f857eSDaniel Scheller 	if (!(priv->flags & CXD2841ER_EARLY_TUNE))
3464c7518d13SDaniel Scheller 		cxd2841er_tuner_set(fe);
3465c7518d13SDaniel Scheller 
3466a6dc60ffSKozlov Sergey 	cxd2841er_tune_done(priv);
3467e3943aa6SDaniel Scheller 
3468e3943aa6SDaniel Scheller 	if (priv->flags & CXD2841ER_NO_WAIT_LOCK)
3469e3943aa6SDaniel Scheller 		goto done;
3470e3943aa6SDaniel Scheller 
3471a6dc60ffSKozlov Sergey 	timeout = 2500;
3472a6dc60ffSKozlov Sergey 	while (timeout > 0) {
3473a6dc60ffSKozlov Sergey 		ret = cxd2841er_read_status_tc(fe, &status);
3474a6dc60ffSKozlov Sergey 		if (ret)
3475a6dc60ffSKozlov Sergey 			goto done;
3476a6dc60ffSKozlov Sergey 		if (status & FE_HAS_LOCK)
3477a6dc60ffSKozlov Sergey 			break;
3478a6dc60ffSKozlov Sergey 		msleep(20);
3479a6dc60ffSKozlov Sergey 		timeout -= 20;
3480a6dc60ffSKozlov Sergey 	}
3481a6dc60ffSKozlov Sergey 	if (timeout < 0)
3482a6dc60ffSKozlov Sergey 		dev_dbg(&priv->i2c->dev,
3483a6dc60ffSKozlov Sergey 			"%s(): LOCK wait timeout\n", __func__);
3484a6dc60ffSKozlov Sergey done:
3485a6dc60ffSKozlov Sergey 	return ret;
3486a6dc60ffSKozlov Sergey }
3487a6dc60ffSKozlov Sergey 
3488a6dc60ffSKozlov Sergey static int cxd2841er_tune_s(struct dvb_frontend *fe,
3489a6dc60ffSKozlov Sergey 			    bool re_tune,
3490a6dc60ffSKozlov Sergey 			    unsigned int mode_flags,
3491a6dc60ffSKozlov Sergey 			    unsigned int *delay,
3492a6dc60ffSKozlov Sergey 			    enum fe_status *status)
3493a6dc60ffSKozlov Sergey {
3494a6dc60ffSKozlov Sergey 	int ret, carrier_offset;
3495a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
3496a6dc60ffSKozlov Sergey 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3497a6dc60ffSKozlov Sergey 
3498a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s() re_tune=%d\n", __func__, re_tune);
3499a6dc60ffSKozlov Sergey 	if (re_tune) {
3500a6dc60ffSKozlov Sergey 		ret = cxd2841er_set_frontend_s(fe);
3501a6dc60ffSKozlov Sergey 		if (ret)
3502a6dc60ffSKozlov Sergey 			return ret;
3503a6dc60ffSKozlov Sergey 		cxd2841er_read_status_s(fe, status);
3504a6dc60ffSKozlov Sergey 		if (*status & FE_HAS_LOCK) {
3505a6dc60ffSKozlov Sergey 			if (cxd2841er_get_carrier_offset_s_s2(
3506a6dc60ffSKozlov Sergey 					priv, &carrier_offset))
3507a6dc60ffSKozlov Sergey 				return -EINVAL;
3508a6dc60ffSKozlov Sergey 			p->frequency += carrier_offset;
3509a6dc60ffSKozlov Sergey 			ret = cxd2841er_set_frontend_s(fe);
3510a6dc60ffSKozlov Sergey 			if (ret)
3511a6dc60ffSKozlov Sergey 				return ret;
3512a6dc60ffSKozlov Sergey 		}
3513a6dc60ffSKozlov Sergey 	}
3514a6dc60ffSKozlov Sergey 	*delay = HZ / 5;
3515a6dc60ffSKozlov Sergey 	return cxd2841er_read_status_s(fe, status);
3516a6dc60ffSKozlov Sergey }
3517a6dc60ffSKozlov Sergey 
3518a6dc60ffSKozlov Sergey static int cxd2841er_tune_tc(struct dvb_frontend *fe,
3519a6dc60ffSKozlov Sergey 			     bool re_tune,
3520a6dc60ffSKozlov Sergey 			     unsigned int mode_flags,
3521a6dc60ffSKozlov Sergey 			     unsigned int *delay,
3522a6dc60ffSKozlov Sergey 			     enum fe_status *status)
3523a6dc60ffSKozlov Sergey {
3524a6dc60ffSKozlov Sergey 	int ret, carrier_offset;
3525a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
3526a6dc60ffSKozlov Sergey 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3527a6dc60ffSKozlov Sergey 
35283f3b48a0SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s(): re_tune %d bandwidth=%d\n", __func__,
35293f3b48a0SAbylay Ospan 			re_tune, p->bandwidth_hz);
3530a6dc60ffSKozlov Sergey 	if (re_tune) {
3531a6dc60ffSKozlov Sergey 		ret = cxd2841er_set_frontend_tc(fe);
3532a6dc60ffSKozlov Sergey 		if (ret)
3533a6dc60ffSKozlov Sergey 			return ret;
3534a6dc60ffSKozlov Sergey 		cxd2841er_read_status_tc(fe, status);
3535a6dc60ffSKozlov Sergey 		if (*status & FE_HAS_LOCK) {
3536a6dc60ffSKozlov Sergey 			switch (priv->system) {
353776344a3fSMauro Carvalho Chehab 			case SYS_ISDBT:
353876344a3fSMauro Carvalho Chehab 				ret = cxd2841er_get_carrier_offset_i(
353976344a3fSMauro Carvalho Chehab 						priv, p->bandwidth_hz,
354076344a3fSMauro Carvalho Chehab 						&carrier_offset);
3541bb9bd878SArnd Bergmann 				if (ret)
3542bb9bd878SArnd Bergmann 					return ret;
354376344a3fSMauro Carvalho Chehab 				break;
3544a6dc60ffSKozlov Sergey 			case SYS_DVBT:
3545c5ea46daSAbylay Ospan 				ret = cxd2841er_get_carrier_offset_t(
3546c5ea46daSAbylay Ospan 					priv, p->bandwidth_hz,
3547c5ea46daSAbylay Ospan 					&carrier_offset);
3548bb9bd878SArnd Bergmann 				if (ret)
3549bb9bd878SArnd Bergmann 					return ret;
3550c5ea46daSAbylay Ospan 				break;
3551a6dc60ffSKozlov Sergey 			case SYS_DVBT2:
3552a6dc60ffSKozlov Sergey 				ret = cxd2841er_get_carrier_offset_t2(
3553a6dc60ffSKozlov Sergey 					priv, p->bandwidth_hz,
3554a6dc60ffSKozlov Sergey 					&carrier_offset);
3555bb9bd878SArnd Bergmann 				if (ret)
3556bb9bd878SArnd Bergmann 					return ret;
3557a6dc60ffSKozlov Sergey 				break;
3558a6dc60ffSKozlov Sergey 			case SYS_DVBC_ANNEX_A:
3559a6dc60ffSKozlov Sergey 				ret = cxd2841er_get_carrier_offset_c(
3560a6dc60ffSKozlov Sergey 					priv, &carrier_offset);
3561bb9bd878SArnd Bergmann 				if (ret)
3562bb9bd878SArnd Bergmann 					return ret;
3563a6dc60ffSKozlov Sergey 				break;
3564a6dc60ffSKozlov Sergey 			default:
3565a6dc60ffSKozlov Sergey 				dev_dbg(&priv->i2c->dev,
3566a6dc60ffSKozlov Sergey 					"%s(): invalid delivery system %d\n",
3567a6dc60ffSKozlov Sergey 					__func__, priv->system);
3568a6dc60ffSKozlov Sergey 				return -EINVAL;
3569a6dc60ffSKozlov Sergey 			}
3570a6dc60ffSKozlov Sergey 			dev_dbg(&priv->i2c->dev, "%s(): carrier offset %d\n",
3571a6dc60ffSKozlov Sergey 				__func__, carrier_offset);
3572a6dc60ffSKozlov Sergey 			p->frequency += carrier_offset;
3573a6dc60ffSKozlov Sergey 			ret = cxd2841er_set_frontend_tc(fe);
3574a6dc60ffSKozlov Sergey 			if (ret)
3575a6dc60ffSKozlov Sergey 				return ret;
3576a6dc60ffSKozlov Sergey 		}
3577a6dc60ffSKozlov Sergey 	}
3578a6dc60ffSKozlov Sergey 	*delay = HZ / 5;
3579a6dc60ffSKozlov Sergey 	return cxd2841er_read_status_tc(fe, status);
3580a6dc60ffSKozlov Sergey }
3581a6dc60ffSKozlov Sergey 
3582a6dc60ffSKozlov Sergey static int cxd2841er_sleep_s(struct dvb_frontend *fe)
3583a6dc60ffSKozlov Sergey {
3584a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
3585a6dc60ffSKozlov Sergey 
3586a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3587a6dc60ffSKozlov Sergey 	cxd2841er_active_s_to_sleep_s(fe->demodulator_priv);
3588a6dc60ffSKozlov Sergey 	cxd2841er_sleep_s_to_shutdown(fe->demodulator_priv);
3589a6dc60ffSKozlov Sergey 	return 0;
3590a6dc60ffSKozlov Sergey }
3591a6dc60ffSKozlov Sergey 
3592a6dc60ffSKozlov Sergey static int cxd2841er_sleep_tc(struct dvb_frontend *fe)
3593a6dc60ffSKozlov Sergey {
3594a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
3595a6dc60ffSKozlov Sergey 
3596a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3597a6dc60ffSKozlov Sergey 	if (priv->state == STATE_ACTIVE_TC) {
3598a6dc60ffSKozlov Sergey 		switch (priv->system) {
3599a6dc60ffSKozlov Sergey 		case SYS_DVBT:
3600a6dc60ffSKozlov Sergey 			cxd2841er_active_t_to_sleep_tc(priv);
3601a6dc60ffSKozlov Sergey 			break;
3602a6dc60ffSKozlov Sergey 		case SYS_DVBT2:
3603a6dc60ffSKozlov Sergey 			cxd2841er_active_t2_to_sleep_tc(priv);
3604a6dc60ffSKozlov Sergey 			break;
360583808c23SAbylay Ospan 		case SYS_ISDBT:
360683808c23SAbylay Ospan 			cxd2841er_active_i_to_sleep_tc(priv);
360783808c23SAbylay Ospan 			break;
3608a6dc60ffSKozlov Sergey 		case SYS_DVBC_ANNEX_A:
3609a6dc60ffSKozlov Sergey 			cxd2841er_active_c_to_sleep_tc(priv);
3610a6dc60ffSKozlov Sergey 			break;
3611a6dc60ffSKozlov Sergey 		default:
3612a6dc60ffSKozlov Sergey 			dev_warn(&priv->i2c->dev,
3613a6dc60ffSKozlov Sergey 				"%s(): unknown delivery system %d\n",
3614a6dc60ffSKozlov Sergey 				__func__, priv->system);
3615a6dc60ffSKozlov Sergey 		}
3616a6dc60ffSKozlov Sergey 	}
3617a6dc60ffSKozlov Sergey 	if (priv->state != STATE_SLEEP_TC) {
3618a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
3619a6dc60ffSKozlov Sergey 			__func__, priv->state);
3620a6dc60ffSKozlov Sergey 		return -EINVAL;
3621a6dc60ffSKozlov Sergey 	}
3622a6dc60ffSKozlov Sergey 	cxd2841er_sleep_tc_to_shutdown(priv);
3623a6dc60ffSKozlov Sergey 	return 0;
3624a6dc60ffSKozlov Sergey }
3625a6dc60ffSKozlov Sergey 
3626a6dc60ffSKozlov Sergey static int cxd2841er_send_burst(struct dvb_frontend *fe,
3627a6dc60ffSKozlov Sergey 				enum fe_sec_mini_cmd burst)
3628a6dc60ffSKozlov Sergey {
3629a6dc60ffSKozlov Sergey 	u8 data;
3630a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv  = fe->demodulator_priv;
3631a6dc60ffSKozlov Sergey 
3632a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s(): burst mode %s\n", __func__,
3633a6dc60ffSKozlov Sergey 		(burst == SEC_MINI_A ? "A" : "B"));
3634a6dc60ffSKozlov Sergey 	if (priv->state != STATE_SLEEP_S &&
3635a6dc60ffSKozlov Sergey 			priv->state != STATE_ACTIVE_S) {
3636a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3637a6dc60ffSKozlov Sergey 			__func__, priv->state);
3638a6dc60ffSKozlov Sergey 		return -EINVAL;
3639a6dc60ffSKozlov Sergey 	}
3640a6dc60ffSKozlov Sergey 	data = (burst == SEC_MINI_A ? 0 : 1);
3641a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3642a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x34, 0x01);
3643a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x35, data);
3644a6dc60ffSKozlov Sergey 	return 0;
3645a6dc60ffSKozlov Sergey }
3646a6dc60ffSKozlov Sergey 
3647a6dc60ffSKozlov Sergey static int cxd2841er_set_tone(struct dvb_frontend *fe,
3648a6dc60ffSKozlov Sergey 			      enum fe_sec_tone_mode tone)
3649a6dc60ffSKozlov Sergey {
3650a6dc60ffSKozlov Sergey 	u8 data;
3651a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv  = fe->demodulator_priv;
3652a6dc60ffSKozlov Sergey 
3653a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s(): tone %s\n", __func__,
3654a6dc60ffSKozlov Sergey 		(tone == SEC_TONE_ON ? "On" : "Off"));
3655a6dc60ffSKozlov Sergey 	if (priv->state != STATE_SLEEP_S &&
3656a6dc60ffSKozlov Sergey 			priv->state != STATE_ACTIVE_S) {
3657a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3658a6dc60ffSKozlov Sergey 			__func__, priv->state);
3659a6dc60ffSKozlov Sergey 		return -EINVAL;
3660a6dc60ffSKozlov Sergey 	}
3661a6dc60ffSKozlov Sergey 	data = (tone == SEC_TONE_ON ? 1 : 0);
3662a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3663a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x36, data);
3664a6dc60ffSKozlov Sergey 	return 0;
3665a6dc60ffSKozlov Sergey }
3666a6dc60ffSKozlov Sergey 
3667a6dc60ffSKozlov Sergey static int cxd2841er_send_diseqc_msg(struct dvb_frontend *fe,
3668a6dc60ffSKozlov Sergey 				     struct dvb_diseqc_master_cmd *cmd)
3669a6dc60ffSKozlov Sergey {
3670a6dc60ffSKozlov Sergey 	int i;
3671a6dc60ffSKozlov Sergey 	u8 data[12];
3672a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv  = fe->demodulator_priv;
3673a6dc60ffSKozlov Sergey 
3674a6dc60ffSKozlov Sergey 	if (priv->state != STATE_SLEEP_S &&
3675a6dc60ffSKozlov Sergey 			priv->state != STATE_ACTIVE_S) {
3676a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3677a6dc60ffSKozlov Sergey 			__func__, priv->state);
3678a6dc60ffSKozlov Sergey 		return -EINVAL;
3679a6dc60ffSKozlov Sergey 	}
3680a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev,
3681a6dc60ffSKozlov Sergey 		"%s(): cmd->len %d\n", __func__, cmd->msg_len);
3682a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3683a6dc60ffSKozlov Sergey 	/* DiDEqC enable */
3684a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x33, 0x01);
3685a6dc60ffSKozlov Sergey 	/* cmd1 length & data */
3686a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x3d, cmd->msg_len);
3687a6dc60ffSKozlov Sergey 	memset(data, 0, sizeof(data));
3688a6dc60ffSKozlov Sergey 	for (i = 0; i < cmd->msg_len && i < sizeof(data); i++)
3689a6dc60ffSKozlov Sergey 		data[i] = cmd->msg[i];
3690a6dc60ffSKozlov Sergey 	cxd2841er_write_regs(priv, I2C_SLVT, 0x3e, data, sizeof(data));
3691a6dc60ffSKozlov Sergey 	/* repeat count for cmd1 */
3692a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x37, 1);
3693a6dc60ffSKozlov Sergey 	/* repeat count for cmd2: always 0 */
3694a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x38, 0);
3695a6dc60ffSKozlov Sergey 	/* start transmit */
3696a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x32, 0x01);
3697a6dc60ffSKozlov Sergey 	/* wait for 1 sec timeout */
3698a6dc60ffSKozlov Sergey 	for (i = 0; i < 50; i++) {
3699a6dc60ffSKozlov Sergey 		cxd2841er_read_reg(priv, I2C_SLVT, 0x10, data);
3700a6dc60ffSKozlov Sergey 		if (!data[0]) {
3701a6dc60ffSKozlov Sergey 			dev_dbg(&priv->i2c->dev,
3702a6dc60ffSKozlov Sergey 				"%s(): DiSEqC cmd has been sent\n", __func__);
3703a6dc60ffSKozlov Sergey 			return 0;
3704a6dc60ffSKozlov Sergey 		}
3705a6dc60ffSKozlov Sergey 		msleep(20);
3706a6dc60ffSKozlov Sergey 	}
3707a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev,
3708a6dc60ffSKozlov Sergey 		"%s(): DiSEqC cmd transmit timeout\n", __func__);
3709a6dc60ffSKozlov Sergey 	return -ETIMEDOUT;
3710a6dc60ffSKozlov Sergey }
3711a6dc60ffSKozlov Sergey 
3712a6dc60ffSKozlov Sergey static void cxd2841er_release(struct dvb_frontend *fe)
3713a6dc60ffSKozlov Sergey {
3714a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv  = fe->demodulator_priv;
3715a6dc60ffSKozlov Sergey 
3716a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3717a6dc60ffSKozlov Sergey 	kfree(priv);
3718a6dc60ffSKozlov Sergey }
3719a6dc60ffSKozlov Sergey 
3720a6dc60ffSKozlov Sergey static int cxd2841er_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
3721a6dc60ffSKozlov Sergey {
3722a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
3723a6dc60ffSKozlov Sergey 
3724a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s(): enable=%d\n", __func__, enable);
3725a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(
3726a6dc60ffSKozlov Sergey 		priv, I2C_SLVX, 0x8, (enable ? 0x01 : 0x00), 0x01);
3727a6dc60ffSKozlov Sergey 	return 0;
3728a6dc60ffSKozlov Sergey }
3729a6dc60ffSKozlov Sergey 
3730a6dc60ffSKozlov Sergey static enum dvbfe_algo cxd2841er_get_algo(struct dvb_frontend *fe)
3731a6dc60ffSKozlov Sergey {
3732a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
3733a6dc60ffSKozlov Sergey 
3734a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3735a6dc60ffSKozlov Sergey 	return DVBFE_ALGO_HW;
3736a6dc60ffSKozlov Sergey }
3737a6dc60ffSKozlov Sergey 
3738d0e20e13SMauro Carvalho Chehab static void cxd2841er_init_stats(struct dvb_frontend *fe)
3739d0e20e13SMauro Carvalho Chehab {
3740d0e20e13SMauro Carvalho Chehab 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3741d0e20e13SMauro Carvalho Chehab 
3742d0e20e13SMauro Carvalho Chehab 	p->strength.len = 1;
3743d0e20e13SMauro Carvalho Chehab 	p->strength.stat[0].scale = FE_SCALE_RELATIVE;
3744d0e20e13SMauro Carvalho Chehab 	p->cnr.len = 1;
3745d0e20e13SMauro Carvalho Chehab 	p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3746d0e20e13SMauro Carvalho Chehab 	p->block_error.len = 1;
3747d0e20e13SMauro Carvalho Chehab 	p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3748d0e20e13SMauro Carvalho Chehab 	p->post_bit_error.len = 1;
3749d0e20e13SMauro Carvalho Chehab 	p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
37504216be14SMauro Carvalho Chehab 	p->post_bit_count.len = 1;
37514216be14SMauro Carvalho Chehab 	p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3752d0e20e13SMauro Carvalho Chehab }
3753d0e20e13SMauro Carvalho Chehab 
3754d0e20e13SMauro Carvalho Chehab 
3755a6dc60ffSKozlov Sergey static int cxd2841er_init_s(struct dvb_frontend *fe)
3756a6dc60ffSKozlov Sergey {
3757a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
3758a6dc60ffSKozlov Sergey 
375930ae3307SAbylay Ospan 	/* sanity. force demod to SHUTDOWN state */
376030ae3307SAbylay Ospan 	if (priv->state == STATE_SLEEP_S) {
376130ae3307SAbylay Ospan 		dev_dbg(&priv->i2c->dev, "%s() forcing sleep->shutdown\n",
376230ae3307SAbylay Ospan 				__func__);
376330ae3307SAbylay Ospan 		cxd2841er_sleep_s_to_shutdown(priv);
376430ae3307SAbylay Ospan 	} else if (priv->state == STATE_ACTIVE_S) {
376530ae3307SAbylay Ospan 		dev_dbg(&priv->i2c->dev, "%s() forcing active->sleep->shutdown\n",
376630ae3307SAbylay Ospan 				__func__);
376730ae3307SAbylay Ospan 		cxd2841er_active_s_to_sleep_s(priv);
376830ae3307SAbylay Ospan 		cxd2841er_sleep_s_to_shutdown(priv);
376930ae3307SAbylay Ospan 	}
377030ae3307SAbylay Ospan 
3771a6dc60ffSKozlov Sergey 	dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3772a6dc60ffSKozlov Sergey 	cxd2841er_shutdown_to_sleep_s(priv);
3773a6dc60ffSKozlov Sergey 	/* SONY_DEMOD_CONFIG_SAT_IFAGCNEG set to 1 */
3774a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
3775a6dc60ffSKozlov Sergey 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xb9, 0x01, 0x01);
3776d0e20e13SMauro Carvalho Chehab 
3777d0e20e13SMauro Carvalho Chehab 	cxd2841er_init_stats(fe);
3778d0e20e13SMauro Carvalho Chehab 
3779a6dc60ffSKozlov Sergey 	return 0;
3780a6dc60ffSKozlov Sergey }
3781a6dc60ffSKozlov Sergey 
3782a6dc60ffSKozlov Sergey static int cxd2841er_init_tc(struct dvb_frontend *fe)
3783a6dc60ffSKozlov Sergey {
3784a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = fe->demodulator_priv;
37853f3b48a0SAbylay Ospan 	struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3786a6dc60ffSKozlov Sergey 
37873f3b48a0SAbylay Ospan 	dev_dbg(&priv->i2c->dev, "%s() bandwidth_hz=%d\n",
37883f3b48a0SAbylay Ospan 			__func__, p->bandwidth_hz);
3789a6dc60ffSKozlov Sergey 	cxd2841er_shutdown_to_sleep_tc(priv);
379014fd8629SDaniel Scheller 	/* SONY_DEMOD_CONFIG_IFAGCNEG = 1 (0 for NO_AGCNEG */
3791a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
379214fd8629SDaniel Scheller 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcb,
379314fd8629SDaniel Scheller 		((priv->flags & CXD2841ER_NO_AGCNEG) ? 0x00 : 0x40), 0x40);
3794a6dc60ffSKozlov Sergey 	/* SONY_DEMOD_CONFIG_IFAGC_ADC_FS = 0 */
3795a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0xcd, 0x50);
3796a6dc60ffSKozlov Sergey 	/* SONY_DEMOD_CONFIG_PARALLEL_SEL = 1 */
3797a6dc60ffSKozlov Sergey 	cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
379803ab1bd5SDaniel Scheller 	cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4,
379903ab1bd5SDaniel Scheller 		((priv->flags & CXD2841ER_TS_SERIAL) ? 0x80 : 0x00), 0x80);
3800d0e20e13SMauro Carvalho Chehab 
3801bf51bbb0SDaniel Scheller 	/* clear TSCFG bits 3+4 */
3802bf51bbb0SDaniel Scheller 	if (priv->flags & CXD2841ER_TSBITS)
3803bf51bbb0SDaniel Scheller 		cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4, 0x00, 0x18);
3804bf51bbb0SDaniel Scheller 
3805d0e20e13SMauro Carvalho Chehab 	cxd2841er_init_stats(fe);
3806d0e20e13SMauro Carvalho Chehab 
3807a6dc60ffSKozlov Sergey 	return 0;
3808a6dc60ffSKozlov Sergey }
3809a6dc60ffSKozlov Sergey 
3810bd336e63SMax Kellermann static const struct dvb_frontend_ops cxd2841er_dvbs_s2_ops;
38113f3b48a0SAbylay Ospan static struct dvb_frontend_ops cxd2841er_t_c_ops;
3812a6dc60ffSKozlov Sergey 
3813a6dc60ffSKozlov Sergey static struct dvb_frontend *cxd2841er_attach(struct cxd2841er_config *cfg,
3814a6dc60ffSKozlov Sergey 					     struct i2c_adapter *i2c,
3815a6dc60ffSKozlov Sergey 					     u8 system)
3816a6dc60ffSKozlov Sergey {
3817a6dc60ffSKozlov Sergey 	u8 chip_id = 0;
3818a6dc60ffSKozlov Sergey 	const char *type;
38193f3b48a0SAbylay Ospan 	const char *name;
3820a6dc60ffSKozlov Sergey 	struct cxd2841er_priv *priv = NULL;
3821a6dc60ffSKozlov Sergey 
3822a6dc60ffSKozlov Sergey 	/* allocate memory for the internal state */
3823a6dc60ffSKozlov Sergey 	priv = kzalloc(sizeof(struct cxd2841er_priv), GFP_KERNEL);
3824a6dc60ffSKozlov Sergey 	if (!priv)
3825a6dc60ffSKozlov Sergey 		return NULL;
3826a6dc60ffSKozlov Sergey 	priv->i2c = i2c;
3827a6dc60ffSKozlov Sergey 	priv->config = cfg;
3828a6dc60ffSKozlov Sergey 	priv->i2c_addr_slvx = (cfg->i2c_addr + 4) >> 1;
3829a6dc60ffSKozlov Sergey 	priv->i2c_addr_slvt = (cfg->i2c_addr) >> 1;
383083808c23SAbylay Ospan 	priv->xtal = cfg->xtal;
3831050863aaSDaniel Scheller 	priv->flags = cfg->flags;
3832a6dc60ffSKozlov Sergey 	priv->frontend.demodulator_priv = priv;
3833a6dc60ffSKozlov Sergey 	dev_info(&priv->i2c->dev,
3834a6dc60ffSKozlov Sergey 		"%s(): I2C adapter %p SLVX addr %x SLVT addr %x\n",
3835a6dc60ffSKozlov Sergey 		__func__, priv->i2c,
3836a6dc60ffSKozlov Sergey 		priv->i2c_addr_slvx, priv->i2c_addr_slvt);
3837a6dc60ffSKozlov Sergey 	chip_id = cxd2841er_chip_id(priv);
38383f3b48a0SAbylay Ospan 	switch (chip_id) {
38391ecda28cSDaniel Scheller 	case CXD2837ER_CHIP_ID:
38401ecda28cSDaniel Scheller 		snprintf(cxd2841er_t_c_ops.info.name, 128,
38411ecda28cSDaniel Scheller 				"Sony CXD2837ER DVB-T/T2/C demodulator");
38421ecda28cSDaniel Scheller 		name = "CXD2837ER";
38431ecda28cSDaniel Scheller 		type = "C/T/T2";
38441ecda28cSDaniel Scheller 		break;
38451ecda28cSDaniel Scheller 	case CXD2838ER_CHIP_ID:
38461ecda28cSDaniel Scheller 		snprintf(cxd2841er_t_c_ops.info.name, 128,
38471ecda28cSDaniel Scheller 				"Sony CXD2838ER ISDB-T demodulator");
38481ecda28cSDaniel Scheller 		cxd2841er_t_c_ops.delsys[0] = SYS_ISDBT;
38491ecda28cSDaniel Scheller 		cxd2841er_t_c_ops.delsys[1] = SYS_UNDEFINED;
38501ecda28cSDaniel Scheller 		cxd2841er_t_c_ops.delsys[2] = SYS_UNDEFINED;
38511ecda28cSDaniel Scheller 		name = "CXD2838ER";
38521ecda28cSDaniel Scheller 		type = "ISDB-T";
38531ecda28cSDaniel Scheller 		break;
38543f3b48a0SAbylay Ospan 	case CXD2841ER_CHIP_ID:
38553f3b48a0SAbylay Ospan 		snprintf(cxd2841er_t_c_ops.info.name, 128,
38563f3b48a0SAbylay Ospan 				"Sony CXD2841ER DVB-T/T2/C demodulator");
38573f3b48a0SAbylay Ospan 		name = "CXD2841ER";
38581ecda28cSDaniel Scheller 		type = "T/T2/C/ISDB-T";
38591ecda28cSDaniel Scheller 		break;
38601ecda28cSDaniel Scheller 	case CXD2843ER_CHIP_ID:
38611ecda28cSDaniel Scheller 		snprintf(cxd2841er_t_c_ops.info.name, 128,
38621ecda28cSDaniel Scheller 				"Sony CXD2843ER DVB-T/T2/C/C2 demodulator");
38631ecda28cSDaniel Scheller 		name = "CXD2843ER";
38641ecda28cSDaniel Scheller 		type = "C/C2/T/T2";
38653f3b48a0SAbylay Ospan 		break;
38663f3b48a0SAbylay Ospan 	case CXD2854ER_CHIP_ID:
38673f3b48a0SAbylay Ospan 		snprintf(cxd2841er_t_c_ops.info.name, 128,
38683f3b48a0SAbylay Ospan 				"Sony CXD2854ER DVB-T/T2/C and ISDB-T demodulator");
38693f3b48a0SAbylay Ospan 		cxd2841er_t_c_ops.delsys[3] = SYS_ISDBT;
38703f3b48a0SAbylay Ospan 		name = "CXD2854ER";
38711ecda28cSDaniel Scheller 		type = "C/C2/T/T2/ISDB-T";
38723f3b48a0SAbylay Ospan 		break;
38733f3b48a0SAbylay Ospan 	default:
3874a6dc60ffSKozlov Sergey 		dev_err(&priv->i2c->dev, "%s(): invalid chip ID 0x%02x\n",
3875a6dc60ffSKozlov Sergey 				__func__, chip_id);
3876a6dc60ffSKozlov Sergey 		priv->frontend.demodulator_priv = NULL;
3877a6dc60ffSKozlov Sergey 		kfree(priv);
3878a6dc60ffSKozlov Sergey 		return NULL;
3879a6dc60ffSKozlov Sergey 	}
38803f3b48a0SAbylay Ospan 
38813f3b48a0SAbylay Ospan 	/* create dvb_frontend */
38823f3b48a0SAbylay Ospan 	if (system == SYS_DVBS) {
38833f3b48a0SAbylay Ospan 		memcpy(&priv->frontend.ops,
38843f3b48a0SAbylay Ospan 			&cxd2841er_dvbs_s2_ops,
38853f3b48a0SAbylay Ospan 			sizeof(struct dvb_frontend_ops));
38863f3b48a0SAbylay Ospan 		type = "S/S2";
38873f3b48a0SAbylay Ospan 	} else {
38883f3b48a0SAbylay Ospan 		memcpy(&priv->frontend.ops,
38893f3b48a0SAbylay Ospan 			&cxd2841er_t_c_ops,
38903f3b48a0SAbylay Ospan 			sizeof(struct dvb_frontend_ops));
38913f3b48a0SAbylay Ospan 	}
38923f3b48a0SAbylay Ospan 
38933f3b48a0SAbylay Ospan 	dev_info(&priv->i2c->dev,
38943f3b48a0SAbylay Ospan 		"%s(): attaching %s DVB-%s frontend\n",
38953f3b48a0SAbylay Ospan 		__func__, name, type);
3896a6dc60ffSKozlov Sergey 	dev_info(&priv->i2c->dev, "%s(): chip ID 0x%02x OK.\n",
3897a6dc60ffSKozlov Sergey 		__func__, chip_id);
3898a6dc60ffSKozlov Sergey 	return &priv->frontend;
3899a6dc60ffSKozlov Sergey }
3900a6dc60ffSKozlov Sergey 
3901a6dc60ffSKozlov Sergey struct dvb_frontend *cxd2841er_attach_s(struct cxd2841er_config *cfg,
3902a6dc60ffSKozlov Sergey 					struct i2c_adapter *i2c)
3903a6dc60ffSKozlov Sergey {
3904a6dc60ffSKozlov Sergey 	return cxd2841er_attach(cfg, i2c, SYS_DVBS);
3905a6dc60ffSKozlov Sergey }
3906a6dc60ffSKozlov Sergey EXPORT_SYMBOL(cxd2841er_attach_s);
3907a6dc60ffSKozlov Sergey 
39083f3b48a0SAbylay Ospan struct dvb_frontend *cxd2841er_attach_t_c(struct cxd2841er_config *cfg,
3909a6dc60ffSKozlov Sergey 					struct i2c_adapter *i2c)
3910a6dc60ffSKozlov Sergey {
39113f3b48a0SAbylay Ospan 	return cxd2841er_attach(cfg, i2c, 0);
3912a6dc60ffSKozlov Sergey }
39133f3b48a0SAbylay Ospan EXPORT_SYMBOL(cxd2841er_attach_t_c);
3914a6dc60ffSKozlov Sergey 
3915bd336e63SMax Kellermann static const struct dvb_frontend_ops cxd2841er_dvbs_s2_ops = {
3916a6dc60ffSKozlov Sergey 	.delsys = { SYS_DVBS, SYS_DVBS2 },
3917a6dc60ffSKozlov Sergey 	.info = {
3918a6dc60ffSKozlov Sergey 		.name		= "Sony CXD2841ER DVB-S/S2 demodulator",
3919a6dc60ffSKozlov Sergey 		.frequency_min	= 500000,
3920a6dc60ffSKozlov Sergey 		.frequency_max	= 2500000,
3921a6dc60ffSKozlov Sergey 		.frequency_stepsize	= 0,
3922a6dc60ffSKozlov Sergey 		.symbol_rate_min = 1000000,
3923a6dc60ffSKozlov Sergey 		.symbol_rate_max = 45000000,
3924a6dc60ffSKozlov Sergey 		.symbol_rate_tolerance = 500,
3925a6dc60ffSKozlov Sergey 		.caps = FE_CAN_INVERSION_AUTO |
3926a6dc60ffSKozlov Sergey 			FE_CAN_FEC_AUTO |
3927a6dc60ffSKozlov Sergey 			FE_CAN_QPSK,
3928a6dc60ffSKozlov Sergey 	},
3929a6dc60ffSKozlov Sergey 	.init = cxd2841er_init_s,
3930a6dc60ffSKozlov Sergey 	.sleep = cxd2841er_sleep_s,
3931a6dc60ffSKozlov Sergey 	.release = cxd2841er_release,
3932a6dc60ffSKozlov Sergey 	.set_frontend = cxd2841er_set_frontend_s,
3933a6dc60ffSKozlov Sergey 	.get_frontend = cxd2841er_get_frontend,
3934a6dc60ffSKozlov Sergey 	.read_status = cxd2841er_read_status_s,
3935a6dc60ffSKozlov Sergey 	.i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
3936a6dc60ffSKozlov Sergey 	.get_frontend_algo = cxd2841er_get_algo,
3937a6dc60ffSKozlov Sergey 	.set_tone = cxd2841er_set_tone,
3938a6dc60ffSKozlov Sergey 	.diseqc_send_burst = cxd2841er_send_burst,
3939a6dc60ffSKozlov Sergey 	.diseqc_send_master_cmd = cxd2841er_send_diseqc_msg,
3940a6dc60ffSKozlov Sergey 	.tune = cxd2841er_tune_s
3941a6dc60ffSKozlov Sergey };
3942a6dc60ffSKozlov Sergey 
39433f3b48a0SAbylay Ospan static struct dvb_frontend_ops cxd2841er_t_c_ops = {
39443f3b48a0SAbylay Ospan 	.delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A },
3945a6dc60ffSKozlov Sergey 	.info = {
39463f3b48a0SAbylay Ospan 		.name	= "", /* will set in attach function */
3947a6dc60ffSKozlov Sergey 		.caps = FE_CAN_FEC_1_2 |
3948a6dc60ffSKozlov Sergey 			FE_CAN_FEC_2_3 |
3949a6dc60ffSKozlov Sergey 			FE_CAN_FEC_3_4 |
3950a6dc60ffSKozlov Sergey 			FE_CAN_FEC_5_6 |
3951a6dc60ffSKozlov Sergey 			FE_CAN_FEC_7_8 |
3952a6dc60ffSKozlov Sergey 			FE_CAN_FEC_AUTO |
3953a6dc60ffSKozlov Sergey 			FE_CAN_QPSK |
3954a6dc60ffSKozlov Sergey 			FE_CAN_QAM_16 |
3955a6dc60ffSKozlov Sergey 			FE_CAN_QAM_32 |
3956a6dc60ffSKozlov Sergey 			FE_CAN_QAM_64 |
3957a6dc60ffSKozlov Sergey 			FE_CAN_QAM_128 |
3958a6dc60ffSKozlov Sergey 			FE_CAN_QAM_256 |
3959a6dc60ffSKozlov Sergey 			FE_CAN_QAM_AUTO |
3960a6dc60ffSKozlov Sergey 			FE_CAN_TRANSMISSION_MODE_AUTO |
3961a6dc60ffSKozlov Sergey 			FE_CAN_GUARD_INTERVAL_AUTO |
3962a6dc60ffSKozlov Sergey 			FE_CAN_HIERARCHY_AUTO |
3963a6dc60ffSKozlov Sergey 			FE_CAN_MUTE_TS |
3964a6dc60ffSKozlov Sergey 			FE_CAN_2G_MODULATION,
3965a6dc60ffSKozlov Sergey 		.frequency_min = 42000000,
3966158f0328SDaniel Scheller 		.frequency_max = 1002000000,
3967158f0328SDaniel Scheller 		.symbol_rate_min = 870000,
3968158f0328SDaniel Scheller 		.symbol_rate_max = 11700000
3969a6dc60ffSKozlov Sergey 	},
3970a6dc60ffSKozlov Sergey 	.init = cxd2841er_init_tc,
3971a6dc60ffSKozlov Sergey 	.sleep = cxd2841er_sleep_tc,
3972a6dc60ffSKozlov Sergey 	.release = cxd2841er_release,
3973a6dc60ffSKozlov Sergey 	.set_frontend = cxd2841er_set_frontend_tc,
3974a6dc60ffSKozlov Sergey 	.get_frontend = cxd2841er_get_frontend,
3975a6dc60ffSKozlov Sergey 	.read_status = cxd2841er_read_status_tc,
3976a6dc60ffSKozlov Sergey 	.tune = cxd2841er_tune_tc,
3977a6dc60ffSKozlov Sergey 	.i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
3978a6dc60ffSKozlov Sergey 	.get_frontend_algo = cxd2841er_get_algo
3979a6dc60ffSKozlov Sergey };
3980a6dc60ffSKozlov Sergey 
398183808c23SAbylay Ospan MODULE_DESCRIPTION("Sony CXD2841ER/CXD2854ER DVB-C/C2/T/T2/S/S2 demodulator driver");
398283808c23SAbylay Ospan MODULE_AUTHOR("Sergey Kozlov <serjk@netup.ru>, Abylay Ospan <aospan@netup.ru>");
3983a6dc60ffSKozlov Sergey MODULE_LICENSE("GPL");
3984