blob: 575d08ad2be3d423d30cb8371f0d4b0ffa20ac12 [file] [log] [blame]
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001/*
2 * cxd2841er.c
3 *
Abylay Ospan83808c22016-03-22 19:20:34 -03004 * Sony digital demodulator driver for
Abylay Ospan9ca17362016-05-16 11:57:04 -03005 * CXD2841ER - DVB-S/S2/T/T2/C/C2
6 * CXD2854ER - DVB-S/S2/T/T2/C/C2, ISDB-T/S
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03007 *
8 * Copyright 2012 Sony Corporation
9 * Copyright (C) 2014 NetUP Inc.
10 * Copyright (C) 2014 Sergey Kozlov <serjk@netup.ru>
11 * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/string.h>
27#include <linux/slab.h>
28#include <linux/bitops.h>
29#include <linux/math64.h>
30#include <linux/log2.h>
31#include <linux/dynamic_debug.h>
32
33#include "dvb_math.h"
34#include "dvb_frontend.h"
35#include "cxd2841er.h"
36#include "cxd2841er_priv.h"
37
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -030038#define MAX_WRITE_REGSIZE 16
Abylay Ospana6f330c2016-07-15 15:34:22 -030039#define LOG2_E_100X 144
40
41/* DVB-C constellation */
42enum sony_dvbc_constellation_t {
43 SONY_DVBC_CONSTELLATION_16QAM,
44 SONY_DVBC_CONSTELLATION_32QAM,
45 SONY_DVBC_CONSTELLATION_64QAM,
46 SONY_DVBC_CONSTELLATION_128QAM,
47 SONY_DVBC_CONSTELLATION_256QAM
48};
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -030049
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -030050enum cxd2841er_state {
51 STATE_SHUTDOWN = 0,
52 STATE_SLEEP_S,
53 STATE_ACTIVE_S,
54 STATE_SLEEP_TC,
55 STATE_ACTIVE_TC
56};
57
58struct cxd2841er_priv {
59 struct dvb_frontend frontend;
60 struct i2c_adapter *i2c;
61 u8 i2c_addr_slvx;
62 u8 i2c_addr_slvt;
63 const struct cxd2841er_config *config;
64 enum cxd2841er_state state;
65 u8 system;
Abylay Ospan83808c22016-03-22 19:20:34 -030066 enum cxd2841er_xtal xtal;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -030067 enum fe_caps caps;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -030068};
69
70static const struct cxd2841er_cnr_data s_cn_data[] = {
71 { 0x033e, 0 }, { 0x0339, 100 }, { 0x0333, 200 },
72 { 0x032e, 300 }, { 0x0329, 400 }, { 0x0324, 500 },
73 { 0x031e, 600 }, { 0x0319, 700 }, { 0x0314, 800 },
74 { 0x030f, 900 }, { 0x030a, 1000 }, { 0x02ff, 1100 },
75 { 0x02f4, 1200 }, { 0x02e9, 1300 }, { 0x02de, 1400 },
76 { 0x02d4, 1500 }, { 0x02c9, 1600 }, { 0x02bf, 1700 },
77 { 0x02b5, 1800 }, { 0x02ab, 1900 }, { 0x02a1, 2000 },
78 { 0x029b, 2100 }, { 0x0295, 2200 }, { 0x0290, 2300 },
79 { 0x028a, 2400 }, { 0x0284, 2500 }, { 0x027f, 2600 },
80 { 0x0279, 2700 }, { 0x0274, 2800 }, { 0x026e, 2900 },
81 { 0x0269, 3000 }, { 0x0262, 3100 }, { 0x025c, 3200 },
82 { 0x0255, 3300 }, { 0x024f, 3400 }, { 0x0249, 3500 },
83 { 0x0242, 3600 }, { 0x023c, 3700 }, { 0x0236, 3800 },
84 { 0x0230, 3900 }, { 0x022a, 4000 }, { 0x0223, 4100 },
85 { 0x021c, 4200 }, { 0x0215, 4300 }, { 0x020e, 4400 },
86 { 0x0207, 4500 }, { 0x0201, 4600 }, { 0x01fa, 4700 },
87 { 0x01f4, 4800 }, { 0x01ed, 4900 }, { 0x01e7, 5000 },
88 { 0x01e0, 5100 }, { 0x01d9, 5200 }, { 0x01d2, 5300 },
89 { 0x01cb, 5400 }, { 0x01c4, 5500 }, { 0x01be, 5600 },
90 { 0x01b7, 5700 }, { 0x01b1, 5800 }, { 0x01aa, 5900 },
91 { 0x01a4, 6000 }, { 0x019d, 6100 }, { 0x0196, 6200 },
92 { 0x018f, 6300 }, { 0x0189, 6400 }, { 0x0182, 6500 },
93 { 0x017c, 6600 }, { 0x0175, 6700 }, { 0x016f, 6800 },
94 { 0x0169, 6900 }, { 0x0163, 7000 }, { 0x015c, 7100 },
95 { 0x0156, 7200 }, { 0x0150, 7300 }, { 0x014a, 7400 },
96 { 0x0144, 7500 }, { 0x013e, 7600 }, { 0x0138, 7700 },
97 { 0x0132, 7800 }, { 0x012d, 7900 }, { 0x0127, 8000 },
98 { 0x0121, 8100 }, { 0x011c, 8200 }, { 0x0116, 8300 },
99 { 0x0111, 8400 }, { 0x010b, 8500 }, { 0x0106, 8600 },
100 { 0x0101, 8700 }, { 0x00fc, 8800 }, { 0x00f7, 8900 },
101 { 0x00f2, 9000 }, { 0x00ee, 9100 }, { 0x00ea, 9200 },
102 { 0x00e6, 9300 }, { 0x00e2, 9400 }, { 0x00de, 9500 },
103 { 0x00da, 9600 }, { 0x00d7, 9700 }, { 0x00d3, 9800 },
104 { 0x00d0, 9900 }, { 0x00cc, 10000 }, { 0x00c7, 10100 },
105 { 0x00c3, 10200 }, { 0x00bf, 10300 }, { 0x00ba, 10400 },
106 { 0x00b6, 10500 }, { 0x00b2, 10600 }, { 0x00ae, 10700 },
107 { 0x00aa, 10800 }, { 0x00a7, 10900 }, { 0x00a3, 11000 },
108 { 0x009f, 11100 }, { 0x009c, 11200 }, { 0x0098, 11300 },
109 { 0x0094, 11400 }, { 0x0091, 11500 }, { 0x008e, 11600 },
110 { 0x008a, 11700 }, { 0x0087, 11800 }, { 0x0084, 11900 },
111 { 0x0081, 12000 }, { 0x007e, 12100 }, { 0x007b, 12200 },
112 { 0x0079, 12300 }, { 0x0076, 12400 }, { 0x0073, 12500 },
113 { 0x0071, 12600 }, { 0x006e, 12700 }, { 0x006c, 12800 },
114 { 0x0069, 12900 }, { 0x0067, 13000 }, { 0x0065, 13100 },
115 { 0x0062, 13200 }, { 0x0060, 13300 }, { 0x005e, 13400 },
116 { 0x005c, 13500 }, { 0x005a, 13600 }, { 0x0058, 13700 },
117 { 0x0056, 13800 }, { 0x0054, 13900 }, { 0x0052, 14000 },
118 { 0x0050, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
119 { 0x004b, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
120 { 0x0046, 14700 }, { 0x0044, 14800 }, { 0x0043, 14900 },
121 { 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
122 { 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
123 { 0x0037, 15700 }, { 0x0036, 15800 }, { 0x0034, 15900 },
124 { 0x0033, 16000 }, { 0x0032, 16100 }, { 0x0031, 16200 },
125 { 0x0030, 16300 }, { 0x002f, 16400 }, { 0x002e, 16500 },
126 { 0x002d, 16600 }, { 0x002c, 16700 }, { 0x002b, 16800 },
127 { 0x002a, 16900 }, { 0x0029, 17000 }, { 0x0028, 17100 },
128 { 0x0027, 17200 }, { 0x0026, 17300 }, { 0x0025, 17400 },
129 { 0x0024, 17500 }, { 0x0023, 17600 }, { 0x0022, 17800 },
130 { 0x0021, 17900 }, { 0x0020, 18000 }, { 0x001f, 18200 },
131 { 0x001e, 18300 }, { 0x001d, 18500 }, { 0x001c, 18700 },
132 { 0x001b, 18900 }, { 0x001a, 19000 }, { 0x0019, 19200 },
133 { 0x0018, 19300 }, { 0x0017, 19500 }, { 0x0016, 19700 },
134 { 0x0015, 19900 }, { 0x0014, 20000 },
135};
136
137static const struct cxd2841er_cnr_data s2_cn_data[] = {
138 { 0x05af, 0 }, { 0x0597, 100 }, { 0x057e, 200 },
139 { 0x0567, 300 }, { 0x0550, 400 }, { 0x0539, 500 },
140 { 0x0522, 600 }, { 0x050c, 700 }, { 0x04f6, 800 },
141 { 0x04e1, 900 }, { 0x04cc, 1000 }, { 0x04b6, 1100 },
142 { 0x04a1, 1200 }, { 0x048c, 1300 }, { 0x0477, 1400 },
143 { 0x0463, 1500 }, { 0x044f, 1600 }, { 0x043c, 1700 },
144 { 0x0428, 1800 }, { 0x0416, 1900 }, { 0x0403, 2000 },
145 { 0x03ef, 2100 }, { 0x03dc, 2200 }, { 0x03c9, 2300 },
146 { 0x03b6, 2400 }, { 0x03a4, 2500 }, { 0x0392, 2600 },
147 { 0x0381, 2700 }, { 0x036f, 2800 }, { 0x035f, 2900 },
148 { 0x034e, 3000 }, { 0x033d, 3100 }, { 0x032d, 3200 },
149 { 0x031d, 3300 }, { 0x030d, 3400 }, { 0x02fd, 3500 },
150 { 0x02ee, 3600 }, { 0x02df, 3700 }, { 0x02d0, 3800 },
151 { 0x02c2, 3900 }, { 0x02b4, 4000 }, { 0x02a6, 4100 },
152 { 0x0299, 4200 }, { 0x028c, 4300 }, { 0x027f, 4400 },
153 { 0x0272, 4500 }, { 0x0265, 4600 }, { 0x0259, 4700 },
154 { 0x024d, 4800 }, { 0x0241, 4900 }, { 0x0236, 5000 },
155 { 0x022b, 5100 }, { 0x0220, 5200 }, { 0x0215, 5300 },
156 { 0x020a, 5400 }, { 0x0200, 5500 }, { 0x01f6, 5600 },
157 { 0x01ec, 5700 }, { 0x01e2, 5800 }, { 0x01d8, 5900 },
158 { 0x01cf, 6000 }, { 0x01c6, 6100 }, { 0x01bc, 6200 },
159 { 0x01b3, 6300 }, { 0x01aa, 6400 }, { 0x01a2, 6500 },
160 { 0x0199, 6600 }, { 0x0191, 6700 }, { 0x0189, 6800 },
161 { 0x0181, 6900 }, { 0x0179, 7000 }, { 0x0171, 7100 },
162 { 0x0169, 7200 }, { 0x0161, 7300 }, { 0x015a, 7400 },
163 { 0x0153, 7500 }, { 0x014b, 7600 }, { 0x0144, 7700 },
164 { 0x013d, 7800 }, { 0x0137, 7900 }, { 0x0130, 8000 },
165 { 0x012a, 8100 }, { 0x0124, 8200 }, { 0x011e, 8300 },
166 { 0x0118, 8400 }, { 0x0112, 8500 }, { 0x010c, 8600 },
167 { 0x0107, 8700 }, { 0x0101, 8800 }, { 0x00fc, 8900 },
168 { 0x00f7, 9000 }, { 0x00f2, 9100 }, { 0x00ec, 9200 },
169 { 0x00e7, 9300 }, { 0x00e2, 9400 }, { 0x00dd, 9500 },
170 { 0x00d8, 9600 }, { 0x00d4, 9700 }, { 0x00cf, 9800 },
171 { 0x00ca, 9900 }, { 0x00c6, 10000 }, { 0x00c2, 10100 },
172 { 0x00be, 10200 }, { 0x00b9, 10300 }, { 0x00b5, 10400 },
173 { 0x00b1, 10500 }, { 0x00ae, 10600 }, { 0x00aa, 10700 },
174 { 0x00a6, 10800 }, { 0x00a3, 10900 }, { 0x009f, 11000 },
175 { 0x009b, 11100 }, { 0x0098, 11200 }, { 0x0095, 11300 },
176 { 0x0091, 11400 }, { 0x008e, 11500 }, { 0x008b, 11600 },
177 { 0x0088, 11700 }, { 0x0085, 11800 }, { 0x0082, 11900 },
178 { 0x007f, 12000 }, { 0x007c, 12100 }, { 0x007a, 12200 },
179 { 0x0077, 12300 }, { 0x0074, 12400 }, { 0x0072, 12500 },
180 { 0x006f, 12600 }, { 0x006d, 12700 }, { 0x006b, 12800 },
181 { 0x0068, 12900 }, { 0x0066, 13000 }, { 0x0064, 13100 },
182 { 0x0061, 13200 }, { 0x005f, 13300 }, { 0x005d, 13400 },
183 { 0x005b, 13500 }, { 0x0059, 13600 }, { 0x0057, 13700 },
184 { 0x0055, 13800 }, { 0x0053, 13900 }, { 0x0051, 14000 },
185 { 0x004f, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
186 { 0x004a, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
187 { 0x0045, 14700 }, { 0x0044, 14800 }, { 0x0042, 14900 },
188 { 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
189 { 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
190 { 0x0038, 15600 }, { 0x0037, 15700 }, { 0x0036, 15800 },
191 { 0x0034, 15900 }, { 0x0033, 16000 }, { 0x0032, 16100 },
192 { 0x0031, 16200 }, { 0x0030, 16300 }, { 0x002f, 16400 },
193 { 0x002e, 16500 }, { 0x002d, 16600 }, { 0x002c, 16700 },
194 { 0x002b, 16800 }, { 0x002a, 16900 }, { 0x0029, 17000 },
195 { 0x0028, 17100 }, { 0x0027, 17200 }, { 0x0026, 17300 },
196 { 0x0025, 17400 }, { 0x0024, 17500 }, { 0x0023, 17600 },
197 { 0x0022, 17800 }, { 0x0021, 17900 }, { 0x0020, 18000 },
198 { 0x001f, 18200 }, { 0x001e, 18300 }, { 0x001d, 18500 },
199 { 0x001c, 18700 }, { 0x001b, 18900 }, { 0x001a, 19000 },
200 { 0x0019, 19200 }, { 0x0018, 19300 }, { 0x0017, 19500 },
201 { 0x0016, 19700 }, { 0x0015, 19900 }, { 0x0014, 20000 },
202};
203
204#define MAKE_IFFREQ_CONFIG(iffreq) ((u32)(((iffreq)/41.0)*16777216.0 + 0.5))
Abylay Ospan83808c22016-03-22 19:20:34 -0300205#define MAKE_IFFREQ_CONFIG_XTAL(xtal, iffreq) ((xtal == SONY_XTAL_24000) ? \
206 (u32)(((iffreq)/48.0)*16777216.0 + 0.5) : \
207 (u32)(((iffreq)/41.0)*16777216.0 + 0.5))
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300208
Abylay Ospan0854df72016-07-19 12:22:03 -0300209static int cxd2841er_freeze_regs(struct cxd2841er_priv *priv);
210static int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv);
211
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300212static void cxd2841er_i2c_debug(struct cxd2841er_priv *priv,
213 u8 addr, u8 reg, u8 write,
214 const u8 *data, u32 len)
215{
216 dev_dbg(&priv->i2c->dev,
Daniel Scheller5d6d93a2017-04-09 16:38:10 -0300217 "cxd2841er: I2C %s addr %02x reg 0x%02x size %d data %*ph\n",
218 (write == 0 ? "read" : "write"), addr, reg, len, len, data);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300219}
220
221static int cxd2841er_write_regs(struct cxd2841er_priv *priv,
222 u8 addr, u8 reg, const u8 *data, u32 len)
223{
224 int ret;
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300225 u8 buf[MAX_WRITE_REGSIZE + 1];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300226 u8 i2c_addr = (addr == I2C_SLVX ?
227 priv->i2c_addr_slvx : priv->i2c_addr_slvt);
228 struct i2c_msg msg[1] = {
229 {
230 .addr = i2c_addr,
231 .flags = 0,
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300232 .len = len + 1,
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300233 .buf = buf,
234 }
235 };
236
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300237 if (len + 1 >= sizeof(buf)) {
Abylay Ospan83808c22016-03-22 19:20:34 -0300238 dev_warn(&priv->i2c->dev, "wr reg=%04x: len=%d is too big!\n",
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300239 reg, len + 1);
240 return -E2BIG;
241 }
242
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300243 cxd2841er_i2c_debug(priv, i2c_addr, reg, 1, data, len);
244 buf[0] = reg;
245 memcpy(&buf[1], data, len);
246
247 ret = i2c_transfer(priv->i2c, msg, 1);
248 if (ret >= 0 && ret != 1)
249 ret = -EIO;
250 if (ret < 0) {
251 dev_warn(&priv->i2c->dev,
252 "%s: i2c wr failed=%d addr=%02x reg=%02x len=%d\n",
253 KBUILD_MODNAME, ret, i2c_addr, reg, len);
254 return ret;
255 }
256 return 0;
257}
258
259static int cxd2841er_write_reg(struct cxd2841er_priv *priv,
260 u8 addr, u8 reg, u8 val)
261{
262 return cxd2841er_write_regs(priv, addr, reg, &val, 1);
263}
264
265static int cxd2841er_read_regs(struct cxd2841er_priv *priv,
266 u8 addr, u8 reg, u8 *val, u32 len)
267{
268 int ret;
269 u8 i2c_addr = (addr == I2C_SLVX ?
270 priv->i2c_addr_slvx : priv->i2c_addr_slvt);
271 struct i2c_msg msg[2] = {
272 {
273 .addr = i2c_addr,
274 .flags = 0,
275 .len = 1,
276 .buf = &reg,
277 }, {
278 .addr = i2c_addr,
279 .flags = I2C_M_RD,
280 .len = len,
281 .buf = val,
282 }
283 };
284
Daniel Scheller725e93e2017-04-09 16:38:11 -0300285 ret = i2c_transfer(priv->i2c, msg, 2);
286 if (ret >= 0 && ret != 2)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300287 ret = -EIO;
288 if (ret < 0) {
289 dev_warn(&priv->i2c->dev,
290 "%s: i2c rd failed=%d addr=%02x reg=%02x\n",
291 KBUILD_MODNAME, ret, i2c_addr, reg);
292 return ret;
293 }
Abylay Ospan6c771612016-05-16 11:43:25 -0300294 cxd2841er_i2c_debug(priv, i2c_addr, reg, 0, val, len);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300295 return 0;
296}
297
298static int cxd2841er_read_reg(struct cxd2841er_priv *priv,
299 u8 addr, u8 reg, u8 *val)
300{
301 return cxd2841er_read_regs(priv, addr, reg, val, 1);
302}
303
304static int cxd2841er_set_reg_bits(struct cxd2841er_priv *priv,
305 u8 addr, u8 reg, u8 data, u8 mask)
306{
307 int res;
308 u8 rdata;
309
310 if (mask != 0xff) {
311 res = cxd2841er_read_reg(priv, addr, reg, &rdata);
312 if (res)
313 return res;
314 data = ((data & mask) | (rdata & (mask ^ 0xFF)));
315 }
316 return cxd2841er_write_reg(priv, addr, reg, data);
317}
318
319static int cxd2841er_dvbs2_set_symbol_rate(struct cxd2841er_priv *priv,
320 u32 symbol_rate)
321{
322 u32 reg_value = 0;
323 u8 data[3] = {0, 0, 0};
324
325 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
326 /*
327 * regValue = (symbolRateKSps * 2^14 / 1000) + 0.5
328 * = ((symbolRateKSps * 2^14) + 500) / 1000
329 * = ((symbolRateKSps * 16384) + 500) / 1000
330 */
331 reg_value = DIV_ROUND_CLOSEST(symbol_rate * 16384, 1000);
332 if ((reg_value == 0) || (reg_value > 0xFFFFF)) {
333 dev_err(&priv->i2c->dev,
334 "%s(): reg_value is out of range\n", __func__);
335 return -EINVAL;
336 }
337 data[0] = (u8)((reg_value >> 16) & 0x0F);
338 data[1] = (u8)((reg_value >> 8) & 0xFF);
339 data[2] = (u8)(reg_value & 0xFF);
340 /* Set SLV-T Bank : 0xAE */
341 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
342 cxd2841er_write_regs(priv, I2C_SLVT, 0x20, data, 3);
343 return 0;
344}
345
346static void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
347 u8 system);
348
349static int cxd2841er_sleep_s_to_active_s(struct cxd2841er_priv *priv,
350 u8 system, u32 symbol_rate)
351{
352 int ret;
353 u8 data[4] = { 0, 0, 0, 0 };
354
355 if (priv->state != STATE_SLEEP_S) {
356 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
357 __func__, (int)priv->state);
358 return -EINVAL;
359 }
360 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
361 cxd2841er_set_ts_clock_mode(priv, SYS_DVBS);
362 /* Set demod mode */
363 if (system == SYS_DVBS) {
364 data[0] = 0x0A;
365 } else if (system == SYS_DVBS2) {
366 data[0] = 0x0B;
367 } else {
368 dev_err(&priv->i2c->dev, "%s(): invalid delsys %d\n",
369 __func__, system);
370 return -EINVAL;
371 }
372 /* Set SLV-X Bank : 0x00 */
373 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
374 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, data[0]);
375 /* DVB-S/S2 */
376 data[0] = 0x00;
377 /* Set SLV-T Bank : 0x00 */
378 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
379 /* Enable S/S2 auto detection 1 */
380 cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, data[0]);
381 /* Set SLV-T Bank : 0xAE */
382 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
383 /* Enable S/S2 auto detection 2 */
384 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, data[0]);
385 /* Set SLV-T Bank : 0x00 */
386 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
387 /* Enable demod clock */
388 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
389 /* Enable ADC clock */
390 cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x01);
391 /* Enable ADC 1 */
392 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
393 /* Enable ADC 2 */
394 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x3f);
395 /* Set SLV-X Bank : 0x00 */
396 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
397 /* Enable ADC 3 */
398 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
399 /* Set SLV-T Bank : 0xA3 */
400 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa3);
401 cxd2841er_write_reg(priv, I2C_SLVT, 0xac, 0x00);
402 data[0] = 0x07;
403 data[1] = 0x3B;
404 data[2] = 0x08;
405 data[3] = 0xC5;
406 /* Set SLV-T Bank : 0xAB */
407 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xab);
408 cxd2841er_write_regs(priv, I2C_SLVT, 0x98, data, 4);
409 data[0] = 0x05;
410 data[1] = 0x80;
411 data[2] = 0x0A;
412 data[3] = 0x80;
413 cxd2841er_write_regs(priv, I2C_SLVT, 0xa8, data, 4);
414 data[0] = 0x0C;
415 data[1] = 0xCC;
416 cxd2841er_write_regs(priv, I2C_SLVT, 0xc3, data, 2);
417 /* Set demod parameter */
418 ret = cxd2841er_dvbs2_set_symbol_rate(priv, symbol_rate);
419 if (ret != 0)
420 return ret;
421 /* Set SLV-T Bank : 0x00 */
422 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
423 /* disable Hi-Z setting 1 */
424 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x10);
425 /* disable Hi-Z setting 2 */
426 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
427 priv->state = STATE_ACTIVE_S;
428 return 0;
429}
430
431static int cxd2841er_sleep_tc_to_active_t_band(struct cxd2841er_priv *priv,
432 u32 bandwidth);
433
434static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
435 u32 bandwidth);
436
437static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
438 u32 bandwidth);
439
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -0300440static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
441 u32 bandwidth);
442
443static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv);
444
445static int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv);
446
447static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv);
448
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300449static int cxd2841er_retune_active(struct cxd2841er_priv *priv,
450 struct dtv_frontend_properties *p)
451{
452 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
453 if (priv->state != STATE_ACTIVE_S &&
454 priv->state != STATE_ACTIVE_TC) {
455 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
456 __func__, priv->state);
457 return -EINVAL;
458 }
459 /* Set SLV-T Bank : 0x00 */
460 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
461 /* disable TS output */
462 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
463 if (priv->state == STATE_ACTIVE_S)
464 return cxd2841er_dvbs2_set_symbol_rate(
465 priv, p->symbol_rate / 1000);
466 else if (priv->state == STATE_ACTIVE_TC) {
467 switch (priv->system) {
468 case SYS_DVBT:
469 return cxd2841er_sleep_tc_to_active_t_band(
470 priv, p->bandwidth_hz);
471 case SYS_DVBT2:
472 return cxd2841er_sleep_tc_to_active_t2_band(
473 priv, p->bandwidth_hz);
474 case SYS_DVBC_ANNEX_A:
475 return cxd2841er_sleep_tc_to_active_c_band(
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -0300476 priv, p->bandwidth_hz);
477 case SYS_ISDBT:
478 cxd2841er_active_i_to_sleep_tc(priv);
479 cxd2841er_sleep_tc_to_shutdown(priv);
480 cxd2841er_shutdown_to_sleep_tc(priv);
481 return cxd2841er_sleep_tc_to_active_i(
482 priv, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300483 }
484 }
485 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
486 __func__, priv->system);
487 return -EINVAL;
488}
489
490static int cxd2841er_active_s_to_sleep_s(struct cxd2841er_priv *priv)
491{
492 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
493 if (priv->state != STATE_ACTIVE_S) {
494 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
495 __func__, priv->state);
496 return -EINVAL;
497 }
498 /* Set SLV-T Bank : 0x00 */
499 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
500 /* disable TS output */
501 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
502 /* enable Hi-Z setting 1 */
503 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1f);
504 /* enable Hi-Z setting 2 */
505 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
506 /* Set SLV-X Bank : 0x00 */
507 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
508 /* disable ADC 1 */
509 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
510 /* Set SLV-T Bank : 0x00 */
511 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
512 /* disable ADC clock */
513 cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x00);
514 /* disable ADC 2 */
515 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
516 /* disable ADC 3 */
517 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
518 /* SADC Bias ON */
519 cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
520 /* disable demod clock */
521 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
522 /* Set SLV-T Bank : 0xAE */
523 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
524 /* disable S/S2 auto detection1 */
525 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
526 /* Set SLV-T Bank : 0x00 */
527 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
528 /* disable S/S2 auto detection2 */
529 cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, 0x00);
530 priv->state = STATE_SLEEP_S;
531 return 0;
532}
533
534static int cxd2841er_sleep_s_to_shutdown(struct cxd2841er_priv *priv)
535{
536 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
537 if (priv->state != STATE_SLEEP_S) {
538 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
539 __func__, priv->state);
540 return -EINVAL;
541 }
542 /* Set SLV-T Bank : 0x00 */
543 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
544 /* Disable DSQOUT */
545 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
546 /* Disable DSQIN */
547 cxd2841er_write_reg(priv, I2C_SLVT, 0x9c, 0x00);
548 /* Set SLV-X Bank : 0x00 */
549 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
550 /* Disable oscillator */
551 cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
552 /* Set demod mode */
553 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
554 priv->state = STATE_SHUTDOWN;
555 return 0;
556}
557
558static int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv)
559{
560 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
561 if (priv->state != STATE_SLEEP_TC) {
562 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
563 __func__, priv->state);
564 return -EINVAL;
565 }
566 /* Set SLV-X Bank : 0x00 */
567 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
568 /* Disable oscillator */
569 cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
570 /* Set demod mode */
571 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
572 priv->state = STATE_SHUTDOWN;
573 return 0;
574}
575
576static int cxd2841er_active_t_to_sleep_tc(struct cxd2841er_priv *priv)
577{
578 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
579 if (priv->state != STATE_ACTIVE_TC) {
580 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
581 __func__, priv->state);
582 return -EINVAL;
583 }
584 /* Set SLV-T Bank : 0x00 */
585 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
586 /* disable TS output */
587 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
588 /* enable Hi-Z setting 1 */
589 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
590 /* enable Hi-Z setting 2 */
591 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
592 /* Set SLV-X Bank : 0x00 */
593 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
594 /* disable ADC 1 */
595 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
596 /* Set SLV-T Bank : 0x00 */
597 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
598 /* Disable ADC 2 */
599 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
600 /* Disable ADC 3 */
601 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
602 /* Disable ADC clock */
603 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
604 /* Disable RF level monitor */
605 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
606 /* Disable demod clock */
607 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
608 priv->state = STATE_SLEEP_TC;
609 return 0;
610}
611
612static int cxd2841er_active_t2_to_sleep_tc(struct cxd2841er_priv *priv)
613{
614 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
615 if (priv->state != STATE_ACTIVE_TC) {
616 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
617 __func__, priv->state);
618 return -EINVAL;
619 }
620 /* Set SLV-T Bank : 0x00 */
621 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
622 /* disable TS output */
623 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
624 /* enable Hi-Z setting 1 */
625 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
626 /* enable Hi-Z setting 2 */
627 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
628 /* Cancel DVB-T2 setting */
629 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
630 cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x40);
631 cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x21);
632 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
633 cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xfb);
634 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
635 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x00, 0x0f);
636 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
637 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x00, 0x3f);
638 /* Set SLV-X Bank : 0x00 */
639 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
640 /* disable ADC 1 */
641 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
642 /* Set SLV-T Bank : 0x00 */
643 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
644 /* Disable ADC 2 */
645 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
646 /* Disable ADC 3 */
647 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
648 /* Disable ADC clock */
649 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
650 /* Disable RF level monitor */
651 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
652 /* Disable demod clock */
653 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
654 priv->state = STATE_SLEEP_TC;
655 return 0;
656}
657
658static int cxd2841er_active_c_to_sleep_tc(struct cxd2841er_priv *priv)
659{
660 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
661 if (priv->state != STATE_ACTIVE_TC) {
662 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
663 __func__, priv->state);
664 return -EINVAL;
665 }
666 /* Set SLV-T Bank : 0x00 */
667 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
668 /* disable TS output */
669 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
670 /* enable Hi-Z setting 1 */
671 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
672 /* enable Hi-Z setting 2 */
673 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
674 /* Cancel DVB-C setting */
675 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
676 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
677 /* Set SLV-X Bank : 0x00 */
678 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
679 /* disable ADC 1 */
680 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
681 /* Set SLV-T Bank : 0x00 */
682 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
683 /* Disable ADC 2 */
684 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
685 /* Disable ADC 3 */
686 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
687 /* Disable ADC clock */
688 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
689 /* Disable RF level monitor */
690 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
691 /* Disable demod clock */
692 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
693 priv->state = STATE_SLEEP_TC;
694 return 0;
695}
696
Abylay Ospan83808c22016-03-22 19:20:34 -0300697static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv)
698{
699 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
700 if (priv->state != STATE_ACTIVE_TC) {
701 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
702 __func__, priv->state);
703 return -EINVAL;
704 }
705 /* Set SLV-T Bank : 0x00 */
706 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
707 /* disable TS output */
708 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
709 /* enable Hi-Z setting 1 */
710 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
711 /* enable Hi-Z setting 2 */
712 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
713
714 /* TODO: Cancel demod parameter */
715
716 /* Set SLV-X Bank : 0x00 */
717 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
718 /* disable ADC 1 */
719 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
720 /* Set SLV-T Bank : 0x00 */
721 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
722 /* Disable ADC 2 */
723 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
724 /* Disable ADC 3 */
725 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
726 /* Disable ADC clock */
727 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
728 /* Disable RF level monitor */
729 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
730 /* Disable demod clock */
731 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
732 priv->state = STATE_SLEEP_TC;
733 return 0;
734}
735
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300736static int cxd2841er_shutdown_to_sleep_s(struct cxd2841er_priv *priv)
737{
738 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
739 if (priv->state != STATE_SHUTDOWN) {
740 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
741 __func__, priv->state);
742 return -EINVAL;
743 }
744 /* Set SLV-X Bank : 0x00 */
745 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
746 /* Clear all demodulator registers */
747 cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
748 usleep_range(3000, 5000);
749 /* Set SLV-X Bank : 0x00 */
750 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
751 /* Set demod SW reset */
752 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
Abylay Ospan83808c22016-03-22 19:20:34 -0300753
754 switch (priv->xtal) {
755 case SONY_XTAL_20500:
756 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x00);
757 break;
758 case SONY_XTAL_24000:
759 /* Select demod frequency */
760 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
761 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x03);
762 break;
763 case SONY_XTAL_41000:
764 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x01);
765 break;
766 default:
767 dev_dbg(&priv->i2c->dev, "%s(): invalid demod xtal %d\n",
768 __func__, priv->xtal);
769 return -EINVAL;
770 }
771
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300772 /* Set demod mode */
773 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x0a);
774 /* Clear demod SW reset */
775 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
776 usleep_range(1000, 2000);
777 /* Set SLV-T Bank : 0x00 */
778 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
779 /* enable DSQOUT */
780 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1F);
781 /* enable DSQIN */
782 cxd2841er_write_reg(priv, I2C_SLVT, 0x9C, 0x40);
783 /* TADC Bias On */
784 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
785 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
786 /* SADC Bias On */
787 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
788 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
789 cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
790 priv->state = STATE_SLEEP_S;
791 return 0;
792}
793
794static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv)
795{
Abylay Ospan6c771612016-05-16 11:43:25 -0300796 u8 data = 0;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -0300797
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300798 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
799 if (priv->state != STATE_SHUTDOWN) {
800 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
801 __func__, priv->state);
802 return -EINVAL;
803 }
804 /* Set SLV-X Bank : 0x00 */
805 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
806 /* Clear all demodulator registers */
807 cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
808 usleep_range(3000, 5000);
809 /* Set SLV-X Bank : 0x00 */
810 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
811 /* Set demod SW reset */
812 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
Abylay Ospan6c771612016-05-16 11:43:25 -0300813 /* Select ADC clock mode */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300814 cxd2841er_write_reg(priv, I2C_SLVX, 0x13, 0x00);
Abylay Ospan6c771612016-05-16 11:43:25 -0300815
816 switch (priv->xtal) {
817 case SONY_XTAL_20500:
818 data = 0x0;
819 break;
820 case SONY_XTAL_24000:
821 /* Select demod frequency */
822 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
823 data = 0x3;
824 break;
825 case SONY_XTAL_41000:
826 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
827 data = 0x1;
828 break;
829 }
830 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, data);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300831 /* Clear demod SW reset */
832 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
833 usleep_range(1000, 2000);
834 /* Set SLV-T Bank : 0x00 */
835 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
836 /* TADC Bias On */
837 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
838 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
839 /* SADC Bias On */
840 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
841 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
842 cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
843 priv->state = STATE_SLEEP_TC;
844 return 0;
845}
846
847static int cxd2841er_tune_done(struct cxd2841er_priv *priv)
848{
849 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
850 /* Set SLV-T Bank : 0x00 */
851 cxd2841er_write_reg(priv, I2C_SLVT, 0, 0);
852 /* SW Reset */
853 cxd2841er_write_reg(priv, I2C_SLVT, 0xfe, 0x01);
854 /* Enable TS output */
855 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x00);
856 return 0;
857}
858
859/* Set TS parallel mode */
860static void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
861 u8 system)
862{
863 u8 serial_ts, ts_rate_ctrl_off, ts_in_off;
864
865 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
866 /* Set SLV-T Bank : 0x00 */
867 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
868 cxd2841er_read_reg(priv, I2C_SLVT, 0xc4, &serial_ts);
869 cxd2841er_read_reg(priv, I2C_SLVT, 0xd3, &ts_rate_ctrl_off);
870 cxd2841er_read_reg(priv, I2C_SLVT, 0xde, &ts_in_off);
871 dev_dbg(&priv->i2c->dev, "%s(): ser_ts=0x%02x rate_ctrl_off=0x%02x in_off=0x%02x\n",
872 __func__, serial_ts, ts_rate_ctrl_off, ts_in_off);
873
874 /*
875 * slave Bank Addr Bit default Name
876 * <SLV-T> 00h D9h [7:0] 8'h08 OTSCKPERIOD
877 */
878 cxd2841er_write_reg(priv, I2C_SLVT, 0xd9, 0x08);
879 /*
880 * Disable TS IF Clock
881 * slave Bank Addr Bit default Name
882 * <SLV-T> 00h 32h [0] 1'b1 OREG_CK_TSIF_EN
883 */
884 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x00, 0x01);
885 /*
886 * slave Bank Addr Bit default Name
887 * <SLV-T> 00h 33h [1:0] 2'b01 OREG_CKSEL_TSIF
888 */
889 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x33, 0x00, 0x03);
890 /*
891 * Enable TS IF Clock
892 * slave Bank Addr Bit default Name
893 * <SLV-T> 00h 32h [0] 1'b1 OREG_CK_TSIF_EN
894 */
895 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x01, 0x01);
896
897 if (system == SYS_DVBT) {
898 /* Enable parity period for DVB-T */
899 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
900 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
901 } else if (system == SYS_DVBC_ANNEX_A) {
902 /* Enable parity period for DVB-C */
903 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
904 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
905 }
906}
907
908static u8 cxd2841er_chip_id(struct cxd2841er_priv *priv)
909{
Abylay Ospan83808c22016-03-22 19:20:34 -0300910 u8 chip_id = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300911
912 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
Abylay Ospan83808c22016-03-22 19:20:34 -0300913 if (cxd2841er_write_reg(priv, I2C_SLVT, 0, 0) == 0)
914 cxd2841er_read_reg(priv, I2C_SLVT, 0xfd, &chip_id);
915 else if (cxd2841er_write_reg(priv, I2C_SLVX, 0, 0) == 0)
916 cxd2841er_read_reg(priv, I2C_SLVX, 0xfd, &chip_id);
917
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300918 return chip_id;
919}
920
921static int cxd2841er_read_status_s(struct dvb_frontend *fe,
922 enum fe_status *status)
923{
924 u8 reg = 0;
925 struct cxd2841er_priv *priv = fe->demodulator_priv;
926
927 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
928 *status = 0;
929 if (priv->state != STATE_ACTIVE_S) {
930 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
931 __func__, priv->state);
932 return -EINVAL;
933 }
934 /* Set SLV-T Bank : 0xA0 */
935 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
936 /*
937 * slave Bank Addr Bit Signal name
938 * <SLV-T> A0h 11h [2] ITSLOCK
939 */
940 cxd2841er_read_reg(priv, I2C_SLVT, 0x11, &reg);
941 if (reg & 0x04) {
942 *status = FE_HAS_SIGNAL
943 | FE_HAS_CARRIER
944 | FE_HAS_VITERBI
945 | FE_HAS_SYNC
946 | FE_HAS_LOCK;
947 }
948 dev_dbg(&priv->i2c->dev, "%s(): result 0x%x\n", __func__, *status);
949 return 0;
950}
951
952static int cxd2841er_read_status_t_t2(struct cxd2841er_priv *priv,
953 u8 *sync, u8 *tslock, u8 *unlock)
954{
955 u8 data = 0;
956
957 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
958 if (priv->state != STATE_ACTIVE_TC)
959 return -EINVAL;
960 if (priv->system == SYS_DVBT) {
961 /* Set SLV-T Bank : 0x10 */
962 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
963 } else {
964 /* Set SLV-T Bank : 0x20 */
965 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
966 }
967 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
968 if ((data & 0x07) == 0x07) {
969 dev_dbg(&priv->i2c->dev,
970 "%s(): invalid hardware state detected\n", __func__);
971 *sync = 0;
972 *tslock = 0;
973 *unlock = 0;
974 } else {
975 *sync = ((data & 0x07) == 0x6 ? 1 : 0);
976 *tslock = ((data & 0x20) ? 1 : 0);
977 *unlock = ((data & 0x10) ? 1 : 0);
978 }
979 return 0;
980}
981
982static int cxd2841er_read_status_c(struct cxd2841er_priv *priv, u8 *tslock)
983{
984 u8 data;
985
986 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
987 if (priv->state != STATE_ACTIVE_TC)
988 return -EINVAL;
989 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
990 cxd2841er_read_reg(priv, I2C_SLVT, 0x88, &data);
991 if ((data & 0x01) == 0) {
992 *tslock = 0;
993 } else {
994 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
995 *tslock = ((data & 0x20) ? 1 : 0);
996 }
997 return 0;
998}
999
Abylay Ospan83808c22016-03-22 19:20:34 -03001000static int cxd2841er_read_status_i(struct cxd2841er_priv *priv,
1001 u8 *sync, u8 *tslock, u8 *unlock)
1002{
1003 u8 data = 0;
1004
1005 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1006 if (priv->state != STATE_ACTIVE_TC)
1007 return -EINVAL;
1008 /* Set SLV-T Bank : 0x60 */
1009 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1010 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
1011 dev_dbg(&priv->i2c->dev,
1012 "%s(): lock=0x%x\n", __func__, data);
1013 *sync = ((data & 0x02) ? 1 : 0);
1014 *tslock = ((data & 0x01) ? 1 : 0);
1015 *unlock = ((data & 0x10) ? 1 : 0);
1016 return 0;
1017}
1018
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001019static int cxd2841er_read_status_tc(struct dvb_frontend *fe,
1020 enum fe_status *status)
1021{
1022 int ret = 0;
1023 u8 sync = 0;
1024 u8 tslock = 0;
1025 u8 unlock = 0;
1026 struct cxd2841er_priv *priv = fe->demodulator_priv;
1027
1028 *status = 0;
1029 if (priv->state == STATE_ACTIVE_TC) {
1030 if (priv->system == SYS_DVBT || priv->system == SYS_DVBT2) {
1031 ret = cxd2841er_read_status_t_t2(
1032 priv, &sync, &tslock, &unlock);
1033 if (ret)
1034 goto done;
1035 if (unlock)
1036 goto done;
1037 if (sync)
1038 *status = FE_HAS_SIGNAL |
1039 FE_HAS_CARRIER |
1040 FE_HAS_VITERBI |
1041 FE_HAS_SYNC;
1042 if (tslock)
1043 *status |= FE_HAS_LOCK;
Abylay Ospan83808c22016-03-22 19:20:34 -03001044 } else if (priv->system == SYS_ISDBT) {
1045 ret = cxd2841er_read_status_i(
1046 priv, &sync, &tslock, &unlock);
1047 if (ret)
1048 goto done;
1049 if (unlock)
1050 goto done;
1051 if (sync)
1052 *status = FE_HAS_SIGNAL |
1053 FE_HAS_CARRIER |
1054 FE_HAS_VITERBI |
1055 FE_HAS_SYNC;
1056 if (tslock)
1057 *status |= FE_HAS_LOCK;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001058 } else if (priv->system == SYS_DVBC_ANNEX_A) {
1059 ret = cxd2841er_read_status_c(priv, &tslock);
1060 if (ret)
1061 goto done;
1062 if (tslock)
1063 *status = FE_HAS_SIGNAL |
1064 FE_HAS_CARRIER |
1065 FE_HAS_VITERBI |
1066 FE_HAS_SYNC |
1067 FE_HAS_LOCK;
1068 }
1069 }
1070done:
1071 dev_dbg(&priv->i2c->dev, "%s(): status 0x%x\n", __func__, *status);
1072 return ret;
1073}
1074
1075static int cxd2841er_get_carrier_offset_s_s2(struct cxd2841er_priv *priv,
1076 int *offset)
1077{
1078 u8 data[3];
1079 u8 is_hs_mode;
1080 s32 cfrl_ctrlval;
1081 s32 temp_div, temp_q, temp_r;
1082
1083 if (priv->state != STATE_ACTIVE_S) {
1084 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1085 __func__, priv->state);
1086 return -EINVAL;
1087 }
1088 /*
1089 * Get High Sampling Rate mode
1090 * slave Bank Addr Bit Signal name
1091 * <SLV-T> A0h 10h [0] ITRL_LOCK
1092 */
1093 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1094 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data[0]);
1095 if (data[0] & 0x01) {
1096 /*
1097 * slave Bank Addr Bit Signal name
1098 * <SLV-T> A0h 50h [4] IHSMODE
1099 */
1100 cxd2841er_read_reg(priv, I2C_SLVT, 0x50, &data[0]);
1101 is_hs_mode = (data[0] & 0x10 ? 1 : 0);
1102 } else {
1103 dev_dbg(&priv->i2c->dev,
1104 "%s(): unable to detect sampling rate mode\n",
1105 __func__);
1106 return -EINVAL;
1107 }
1108 /*
1109 * slave Bank Addr Bit Signal name
1110 * <SLV-T> A0h 45h [4:0] ICFRL_CTRLVAL[20:16]
1111 * <SLV-T> A0h 46h [7:0] ICFRL_CTRLVAL[15:8]
1112 * <SLV-T> A0h 47h [7:0] ICFRL_CTRLVAL[7:0]
1113 */
1114 cxd2841er_read_regs(priv, I2C_SLVT, 0x45, data, 3);
1115 cfrl_ctrlval = sign_extend32((((u32)data[0] & 0x1F) << 16) |
1116 (((u32)data[1] & 0xFF) << 8) |
1117 ((u32)data[2] & 0xFF), 20);
1118 temp_div = (is_hs_mode ? 1048576 : 1572864);
1119 if (cfrl_ctrlval > 0) {
1120 temp_q = div_s64_rem(97375LL * cfrl_ctrlval,
1121 temp_div, &temp_r);
1122 } else {
1123 temp_q = div_s64_rem(-97375LL * cfrl_ctrlval,
1124 temp_div, &temp_r);
1125 }
1126 if (temp_r >= temp_div / 2)
1127 temp_q++;
1128 if (cfrl_ctrlval > 0)
1129 temp_q *= -1;
1130 *offset = temp_q;
1131 return 0;
1132}
1133
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -03001134static int cxd2841er_get_carrier_offset_i(struct cxd2841er_priv *priv,
1135 u32 bandwidth, int *offset)
1136{
1137 u8 data[4];
1138
1139 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1140 if (priv->state != STATE_ACTIVE_TC) {
1141 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1142 __func__, priv->state);
1143 return -EINVAL;
1144 }
1145 if (priv->system != SYS_ISDBT) {
1146 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1147 __func__, priv->system);
1148 return -EINVAL;
1149 }
1150 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1151 cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1152 *offset = -1 * sign_extend32(
1153 ((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
1154 ((u32)data[2] << 8) | (u32)data[3], 29);
1155
1156 switch (bandwidth) {
1157 case 6000000:
1158 *offset = -1 * ((*offset) * 8/264);
1159 break;
1160 case 7000000:
1161 *offset = -1 * ((*offset) * 8/231);
1162 break;
1163 case 8000000:
1164 *offset = -1 * ((*offset) * 8/198);
1165 break;
1166 default:
1167 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
1168 __func__, bandwidth);
1169 return -EINVAL;
1170 }
1171
1172 dev_dbg(&priv->i2c->dev, "%s(): bandwidth %d offset %d\n",
1173 __func__, bandwidth, *offset);
1174
1175 return 0;
1176}
1177
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03001178static int cxd2841er_get_carrier_offset_t(struct cxd2841er_priv *priv,
1179 u32 bandwidth, int *offset)
1180{
1181 u8 data[4];
1182
1183 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1184 if (priv->state != STATE_ACTIVE_TC) {
1185 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1186 __func__, priv->state);
1187 return -EINVAL;
1188 }
1189 if (priv->system != SYS_DVBT) {
1190 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1191 __func__, priv->system);
1192 return -EINVAL;
1193 }
1194 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1195 cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1196 *offset = -1 * sign_extend32(
1197 ((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
1198 ((u32)data[2] << 8) | (u32)data[3], 29);
Abylay Ospan6c771612016-05-16 11:43:25 -03001199 *offset *= (bandwidth / 1000000);
1200 *offset /= 235;
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03001201 return 0;
1202}
1203
Mauro Carvalho Chehabc8946c82015-08-11 15:08:47 -03001204static int cxd2841er_get_carrier_offset_t2(struct cxd2841er_priv *priv,
1205 u32 bandwidth, int *offset)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001206{
1207 u8 data[4];
1208
1209 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1210 if (priv->state != STATE_ACTIVE_TC) {
1211 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1212 __func__, priv->state);
1213 return -EINVAL;
1214 }
1215 if (priv->system != SYS_DVBT2) {
1216 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1217 __func__, priv->system);
1218 return -EINVAL;
1219 }
1220 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1221 cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1222 *offset = -1 * sign_extend32(
1223 ((u32)(data[0] & 0x0F) << 24) | ((u32)data[1] << 16) |
1224 ((u32)data[2] << 8) | (u32)data[3], 27);
1225 switch (bandwidth) {
1226 case 1712000:
1227 *offset /= 582;
1228 break;
1229 case 5000000:
1230 case 6000000:
1231 case 7000000:
1232 case 8000000:
1233 *offset *= (bandwidth / 1000000);
1234 *offset /= 940;
1235 break;
1236 default:
1237 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
1238 __func__, bandwidth);
1239 return -EINVAL;
1240 }
1241 return 0;
1242}
1243
Mauro Carvalho Chehabc8946c82015-08-11 15:08:47 -03001244static int cxd2841er_get_carrier_offset_c(struct cxd2841er_priv *priv,
1245 int *offset)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001246{
1247 u8 data[2];
1248
1249 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1250 if (priv->state != STATE_ACTIVE_TC) {
1251 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1252 __func__, priv->state);
1253 return -EINVAL;
1254 }
1255 if (priv->system != SYS_DVBC_ANNEX_A) {
1256 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1257 __func__, priv->system);
1258 return -EINVAL;
1259 }
1260 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1261 cxd2841er_read_regs(priv, I2C_SLVT, 0x15, data, sizeof(data));
1262 *offset = div_s64(41000LL * sign_extend32((((u32)data[0] & 0x3f) << 8)
1263 | (u32)data[1], 13), 16384);
1264 return 0;
1265}
1266
Abylay Ospana6f330c2016-07-15 15:34:22 -03001267static int cxd2841er_read_packet_errors_c(
1268 struct cxd2841er_priv *priv, u32 *penum)
1269{
1270 u8 data[3];
1271
1272 *penum = 0;
1273 if (priv->state != STATE_ACTIVE_TC) {
1274 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1275 __func__, priv->state);
1276 return -EINVAL;
1277 }
1278 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1279 cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
1280 if (data[2] & 0x01)
1281 *penum = ((u32)data[0] << 8) | (u32)data[1];
1282 return 0;
1283}
1284
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001285static int cxd2841er_read_packet_errors_t(
1286 struct cxd2841er_priv *priv, u32 *penum)
1287{
1288 u8 data[3];
1289
1290 *penum = 0;
1291 if (priv->state != STATE_ACTIVE_TC) {
1292 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1293 __func__, priv->state);
1294 return -EINVAL;
1295 }
1296 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1297 cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
1298 if (data[2] & 0x01)
1299 *penum = ((u32)data[0] << 8) | (u32)data[1];
1300 return 0;
1301}
1302
1303static int cxd2841er_read_packet_errors_t2(
1304 struct cxd2841er_priv *priv, u32 *penum)
1305{
1306 u8 data[3];
1307
1308 *penum = 0;
1309 if (priv->state != STATE_ACTIVE_TC) {
1310 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1311 __func__, priv->state);
1312 return -EINVAL;
1313 }
1314 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
1315 cxd2841er_read_regs(priv, I2C_SLVT, 0xfd, data, sizeof(data));
1316 if (data[0] & 0x01)
1317 *penum = ((u32)data[1] << 8) | (u32)data[2];
1318 return 0;
1319}
1320
Abylay Ospan83808c22016-03-22 19:20:34 -03001321static int cxd2841er_read_packet_errors_i(
1322 struct cxd2841er_priv *priv, u32 *penum)
1323{
1324 u8 data[2];
1325
1326 *penum = 0;
1327 if (priv->state != STATE_ACTIVE_TC) {
1328 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1329 __func__, priv->state);
1330 return -EINVAL;
1331 }
1332 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1333 cxd2841er_read_regs(priv, I2C_SLVT, 0xA1, data, 1);
1334
1335 if (!(data[0] & 0x01))
1336 return 0;
1337
1338 /* Layer A */
1339 cxd2841er_read_regs(priv, I2C_SLVT, 0xA2, data, sizeof(data));
1340 *penum = ((u32)data[0] << 8) | (u32)data[1];
1341
1342 /* Layer B */
1343 cxd2841er_read_regs(priv, I2C_SLVT, 0xA4, data, sizeof(data));
1344 *penum += ((u32)data[0] << 8) | (u32)data[1];
1345
1346 /* Layer C */
1347 cxd2841er_read_regs(priv, I2C_SLVT, 0xA6, data, sizeof(data));
1348 *penum += ((u32)data[0] << 8) | (u32)data[1];
1349
1350 return 0;
1351}
1352
Abylay Ospana6f330c2016-07-15 15:34:22 -03001353static int cxd2841er_read_ber_c(struct cxd2841er_priv *priv,
1354 u32 *bit_error, u32 *bit_count)
1355{
1356 u8 data[3];
1357 u32 bit_err, period_exp;
1358
1359 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1360 if (priv->state != STATE_ACTIVE_TC) {
1361 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1362 __func__, priv->state);
1363 return -EINVAL;
1364 }
1365 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1366 cxd2841er_read_regs(priv, I2C_SLVT, 0x62, data, sizeof(data));
1367 if (!(data[0] & 0x80)) {
1368 dev_dbg(&priv->i2c->dev,
1369 "%s(): no valid BER data\n", __func__);
1370 return -EINVAL;
1371 }
1372 bit_err = ((u32)(data[0] & 0x3f) << 16) |
1373 ((u32)data[1] << 8) |
1374 (u32)data[2];
1375 cxd2841er_read_reg(priv, I2C_SLVT, 0x60, data);
1376 period_exp = data[0] & 0x1f;
1377
1378 if ((period_exp <= 11) && (bit_err > (1 << period_exp) * 204 * 8)) {
1379 dev_dbg(&priv->i2c->dev,
1380 "%s(): period_exp(%u) or bit_err(%u) not in range. no valid BER data\n",
1381 __func__, period_exp, bit_err);
1382 return -EINVAL;
1383 }
1384
1385 dev_dbg(&priv->i2c->dev,
1386 "%s(): period_exp(%u) or bit_err(%u) count=%d\n",
1387 __func__, period_exp, bit_err,
1388 ((1 << period_exp) * 204 * 8));
1389
1390 *bit_error = bit_err;
1391 *bit_count = ((1 << period_exp) * 204 * 8);
1392
1393 return 0;
1394}
1395
Abylay Ospan0854df72016-07-19 12:22:03 -03001396static int cxd2841er_read_ber_i(struct cxd2841er_priv *priv,
1397 u32 *bit_error, u32 *bit_count)
1398{
1399 u8 data[3];
1400 u8 pktnum[2];
1401
1402 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1403 if (priv->state != STATE_ACTIVE_TC) {
1404 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1405 __func__, priv->state);
1406 return -EINVAL;
1407 }
1408
1409 cxd2841er_freeze_regs(priv);
1410 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1411 cxd2841er_read_regs(priv, I2C_SLVT, 0x5B, pktnum, sizeof(pktnum));
1412 cxd2841er_read_regs(priv, I2C_SLVT, 0x16, data, sizeof(data));
1413
1414 if (!pktnum[0] && !pktnum[1]) {
1415 dev_dbg(&priv->i2c->dev,
1416 "%s(): no valid BER data\n", __func__);
1417 cxd2841er_unfreeze_regs(priv);
1418 return -EINVAL;
1419 }
1420
1421 *bit_error = ((u32)(data[0] & 0x7F) << 16) |
1422 ((u32)data[1] << 8) | data[2];
1423 *bit_count = ((((u32)pktnum[0] << 8) | pktnum[1]) * 204 * 8);
1424 dev_dbg(&priv->i2c->dev, "%s(): bit_error=%u bit_count=%u\n",
1425 __func__, *bit_error, *bit_count);
1426
1427 cxd2841er_unfreeze_regs(priv);
1428 return 0;
1429}
1430
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001431static int cxd2841er_mon_read_ber_s(struct cxd2841er_priv *priv,
1432 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001433{
1434 u8 data[11];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001435
1436 /* Set SLV-T Bank : 0xA0 */
1437 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1438 /*
1439 * slave Bank Addr Bit Signal name
1440 * <SLV-T> A0h 35h [0] IFVBER_VALID
1441 * <SLV-T> A0h 36h [5:0] IFVBER_BITERR[21:16]
1442 * <SLV-T> A0h 37h [7:0] IFVBER_BITERR[15:8]
1443 * <SLV-T> A0h 38h [7:0] IFVBER_BITERR[7:0]
1444 * <SLV-T> A0h 3Dh [5:0] IFVBER_BITNUM[21:16]
1445 * <SLV-T> A0h 3Eh [7:0] IFVBER_BITNUM[15:8]
1446 * <SLV-T> A0h 3Fh [7:0] IFVBER_BITNUM[7:0]
1447 */
1448 cxd2841er_read_regs(priv, I2C_SLVT, 0x35, data, 11);
1449 if (data[0] & 0x01) {
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001450 *bit_error = ((u32)(data[1] & 0x3F) << 16) |
1451 ((u32)(data[2] & 0xFF) << 8) |
1452 (u32)(data[3] & 0xFF);
1453 *bit_count = ((u32)(data[8] & 0x3F) << 16) |
1454 ((u32)(data[9] & 0xFF) << 8) |
1455 (u32)(data[10] & 0xFF);
1456 if ((*bit_count == 0) || (*bit_error > *bit_count)) {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001457 dev_dbg(&priv->i2c->dev,
1458 "%s(): invalid bit_error %d, bit_count %d\n",
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001459 __func__, *bit_error, *bit_count);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001460 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001461 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001462 return 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001463 }
1464 dev_dbg(&priv->i2c->dev, "%s(): no data available\n", __func__);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001465 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001466}
1467
1468
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001469static int cxd2841er_mon_read_ber_s2(struct cxd2841er_priv *priv,
1470 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001471{
1472 u8 data[5];
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001473 u32 period;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001474
1475 /* Set SLV-T Bank : 0xB2 */
1476 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xb2);
1477 /*
1478 * slave Bank Addr Bit Signal name
1479 * <SLV-T> B2h 30h [0] IFLBER_VALID
1480 * <SLV-T> B2h 31h [3:0] IFLBER_BITERR[27:24]
1481 * <SLV-T> B2h 32h [7:0] IFLBER_BITERR[23:16]
1482 * <SLV-T> B2h 33h [7:0] IFLBER_BITERR[15:8]
1483 * <SLV-T> B2h 34h [7:0] IFLBER_BITERR[7:0]
1484 */
1485 cxd2841er_read_regs(priv, I2C_SLVT, 0x30, data, 5);
1486 if (data[0] & 0x01) {
1487 /* Bit error count */
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001488 *bit_error = ((u32)(data[1] & 0x0F) << 24) |
1489 ((u32)(data[2] & 0xFF) << 16) |
1490 ((u32)(data[3] & 0xFF) << 8) |
1491 (u32)(data[4] & 0xFF);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001492
1493 /* Set SLV-T Bank : 0xA0 */
1494 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1495 cxd2841er_read_reg(priv, I2C_SLVT, 0x7a, data);
1496 /* Measurement period */
1497 period = (u32)(1 << (data[0] & 0x0F));
1498 if (period == 0) {
1499 dev_dbg(&priv->i2c->dev,
1500 "%s(): period is 0\n", __func__);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001501 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001502 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001503 if (*bit_error > (period * 64800)) {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001504 dev_dbg(&priv->i2c->dev,
1505 "%s(): invalid bit_err 0x%x period 0x%x\n",
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001506 __func__, *bit_error, period);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001507 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001508 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001509 *bit_count = period * 64800;
1510
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001511 return 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001512 } else {
1513 dev_dbg(&priv->i2c->dev,
1514 "%s(): no data available\n", __func__);
1515 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001516 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001517}
1518
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001519static int cxd2841er_read_ber_t2(struct cxd2841er_priv *priv,
1520 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001521{
1522 u8 data[4];
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001523 u32 period_exp, n_ldpc;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001524
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001525 if (priv->state != STATE_ACTIVE_TC) {
1526 dev_dbg(&priv->i2c->dev,
1527 "%s(): invalid state %d\n", __func__, priv->state);
1528 return -EINVAL;
1529 }
1530 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1531 cxd2841er_read_regs(priv, I2C_SLVT, 0x39, data, sizeof(data));
1532 if (!(data[0] & 0x10)) {
1533 dev_dbg(&priv->i2c->dev,
1534 "%s(): no valid BER data\n", __func__);
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001535 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001536 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001537 *bit_error = ((u32)(data[0] & 0x0f) << 24) |
1538 ((u32)data[1] << 16) |
1539 ((u32)data[2] << 8) |
1540 (u32)data[3];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001541 cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
1542 period_exp = data[0] & 0x0f;
1543 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x22);
1544 cxd2841er_read_reg(priv, I2C_SLVT, 0x5e, data);
1545 n_ldpc = ((data[0] & 0x03) == 0 ? 16200 : 64800);
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001546 if (*bit_error > ((1U << period_exp) * n_ldpc)) {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001547 dev_dbg(&priv->i2c->dev,
1548 "%s(): invalid BER value\n", __func__);
1549 return -EINVAL;
1550 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001551
1552 /*
1553 * FIXME: the right thing would be to return bit_error untouched,
1554 * but, as we don't know the scale returned by the counters, let's
1555 * at least preserver BER = bit_error/bit_count.
1556 */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001557 if (period_exp >= 4) {
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001558 *bit_count = (1U << (period_exp - 4)) * (n_ldpc / 200);
1559 *bit_error *= 3125ULL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001560 } else {
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001561 *bit_count = (1U << period_exp) * (n_ldpc / 200);
Abylay Ospana6f330c2016-07-15 15:34:22 -03001562 *bit_error *= 50000ULL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001563 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001564 return 0;
1565}
1566
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001567static int cxd2841er_read_ber_t(struct cxd2841er_priv *priv,
1568 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001569{
1570 u8 data[2];
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001571 u32 period;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001572
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001573 if (priv->state != STATE_ACTIVE_TC) {
1574 dev_dbg(&priv->i2c->dev,
1575 "%s(): invalid state %d\n", __func__, priv->state);
1576 return -EINVAL;
1577 }
1578 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1579 cxd2841er_read_reg(priv, I2C_SLVT, 0x39, data);
1580 if (!(data[0] & 0x01)) {
1581 dev_dbg(&priv->i2c->dev,
1582 "%s(): no valid BER data\n", __func__);
1583 return 0;
1584 }
1585 cxd2841er_read_regs(priv, I2C_SLVT, 0x22, data, sizeof(data));
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001586 *bit_error = ((u32)data[0] << 8) | (u32)data[1];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001587 cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
1588 period = ((data[0] & 0x07) == 0) ? 256 : (4096 << (data[0] & 0x07));
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001589
1590 /*
1591 * FIXME: the right thing would be to return bit_error untouched,
1592 * but, as we don't know the scale returned by the counters, let's
1593 * at least preserver BER = bit_error/bit_count.
1594 */
1595 *bit_count = period / 128;
1596 *bit_error *= 78125ULL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001597 return 0;
1598}
1599
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001600static int cxd2841er_freeze_regs(struct cxd2841er_priv *priv)
1601{
1602 /*
1603 * Freeze registers: ensure multiple separate register reads
1604 * are from the same snapshot
1605 */
1606 cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x01);
1607 return 0;
1608}
1609
1610static int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv)
1611{
1612 /*
1613 * un-freeze registers
1614 */
1615 cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x00);
1616 return 0;
1617}
1618
Abylay Ospane05b1872016-07-15 17:04:17 -03001619static u32 cxd2841er_dvbs_read_snr(struct cxd2841er_priv *priv,
1620 u8 delsys, u32 *snr)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001621{
1622 u8 data[3];
1623 u32 res = 0, value;
1624 int min_index, max_index, index;
1625 static const struct cxd2841er_cnr_data *cn_data;
1626
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001627 cxd2841er_freeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001628 /* Set SLV-T Bank : 0xA1 */
1629 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa1);
1630 /*
1631 * slave Bank Addr Bit Signal name
1632 * <SLV-T> A1h 10h [0] ICPM_QUICKRDY
1633 * <SLV-T> A1h 11h [4:0] ICPM_QUICKCNDT[12:8]
1634 * <SLV-T> A1h 12h [7:0] ICPM_QUICKCNDT[7:0]
1635 */
1636 cxd2841er_read_regs(priv, I2C_SLVT, 0x10, data, 3);
1637 if (data[0] & 0x01) {
1638 value = ((u32)(data[1] & 0x1F) << 8) | (u32)(data[2] & 0xFF);
1639 min_index = 0;
1640 if (delsys == SYS_DVBS) {
1641 cn_data = s_cn_data;
1642 max_index = sizeof(s_cn_data) /
1643 sizeof(s_cn_data[0]) - 1;
1644 } else {
1645 cn_data = s2_cn_data;
1646 max_index = sizeof(s2_cn_data) /
1647 sizeof(s2_cn_data[0]) - 1;
1648 }
1649 if (value >= cn_data[min_index].value) {
1650 res = cn_data[min_index].cnr_x1000;
1651 goto done;
1652 }
1653 if (value <= cn_data[max_index].value) {
1654 res = cn_data[max_index].cnr_x1000;
1655 goto done;
1656 }
1657 while ((max_index - min_index) > 1) {
1658 index = (max_index + min_index) / 2;
1659 if (value == cn_data[index].value) {
1660 res = cn_data[index].cnr_x1000;
1661 goto done;
1662 } else if (value > cn_data[index].value)
1663 max_index = index;
1664 else
1665 min_index = index;
1666 if ((max_index - min_index) <= 1) {
1667 if (value == cn_data[max_index].value) {
1668 res = cn_data[max_index].cnr_x1000;
1669 goto done;
1670 } else {
1671 res = cn_data[min_index].cnr_x1000;
1672 goto done;
1673 }
1674 }
1675 }
1676 } else {
1677 dev_dbg(&priv->i2c->dev,
1678 "%s(): no data available\n", __func__);
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001679 cxd2841er_unfreeze_regs(priv);
Abylay Ospane05b1872016-07-15 17:04:17 -03001680 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001681 }
1682done:
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001683 cxd2841er_unfreeze_regs(priv);
Abylay Ospane05b1872016-07-15 17:04:17 -03001684 *snr = res;
1685 return 0;
1686}
1687
1688static uint32_t sony_log(uint32_t x)
1689{
1690 return (((10000>>8)*(intlog2(x)>>16) + LOG2_E_100X/2)/LOG2_E_100X);
1691}
1692
1693static int cxd2841er_read_snr_c(struct cxd2841er_priv *priv, u32 *snr)
1694{
1695 u32 reg;
1696 u8 data[2];
1697 enum sony_dvbc_constellation_t qam = SONY_DVBC_CONSTELLATION_16QAM;
1698
1699 *snr = 0;
1700 if (priv->state != STATE_ACTIVE_TC) {
1701 dev_dbg(&priv->i2c->dev,
1702 "%s(): invalid state %d\n",
1703 __func__, priv->state);
1704 return -EINVAL;
1705 }
1706
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001707 cxd2841er_freeze_regs(priv);
Abylay Ospane05b1872016-07-15 17:04:17 -03001708 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1709 cxd2841er_read_regs(priv, I2C_SLVT, 0x19, data, 1);
1710 qam = (enum sony_dvbc_constellation_t) (data[0] & 0x07);
1711 cxd2841er_read_regs(priv, I2C_SLVT, 0x4C, data, 2);
1712
1713 reg = ((u32)(data[0]&0x1f) << 8) | (u32)data[1];
1714 if (reg == 0) {
1715 dev_dbg(&priv->i2c->dev,
1716 "%s(): reg value out of range\n", __func__);
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001717 cxd2841er_unfreeze_regs(priv);
Abylay Ospane05b1872016-07-15 17:04:17 -03001718 return 0;
1719 }
1720
1721 switch (qam) {
1722 case SONY_DVBC_CONSTELLATION_16QAM:
1723 case SONY_DVBC_CONSTELLATION_64QAM:
1724 case SONY_DVBC_CONSTELLATION_256QAM:
1725 /* SNR(dB) = -9.50 * ln(IREG_SNR_ESTIMATE / (24320)) */
1726 if (reg < 126)
1727 reg = 126;
1728 *snr = -95 * (int32_t)sony_log(reg) + 95941;
1729 break;
1730 case SONY_DVBC_CONSTELLATION_32QAM:
1731 case SONY_DVBC_CONSTELLATION_128QAM:
1732 /* SNR(dB) = -8.75 * ln(IREG_SNR_ESTIMATE / (20800)) */
1733 if (reg < 69)
1734 reg = 69;
1735 *snr = -88 * (int32_t)sony_log(reg) + 86999;
1736 break;
1737 default:
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001738 cxd2841er_unfreeze_regs(priv);
Abylay Ospane05b1872016-07-15 17:04:17 -03001739 return -EINVAL;
1740 }
1741
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001742 cxd2841er_unfreeze_regs(priv);
Abylay Ospane05b1872016-07-15 17:04:17 -03001743 return 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001744}
1745
1746static int cxd2841er_read_snr_t(struct cxd2841er_priv *priv, u32 *snr)
1747{
1748 u32 reg;
1749 u8 data[2];
1750
1751 *snr = 0;
1752 if (priv->state != STATE_ACTIVE_TC) {
1753 dev_dbg(&priv->i2c->dev,
1754 "%s(): invalid state %d\n", __func__, priv->state);
1755 return -EINVAL;
1756 }
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001757
1758 cxd2841er_freeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001759 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1760 cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
1761 reg = ((u32)data[0] << 8) | (u32)data[1];
1762 if (reg == 0) {
1763 dev_dbg(&priv->i2c->dev,
1764 "%s(): reg value out of range\n", __func__);
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001765 cxd2841er_unfreeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001766 return 0;
1767 }
1768 if (reg > 4996)
1769 reg = 4996;
1770 *snr = 10000 * ((intlog10(reg) - intlog10(5350 - reg)) >> 24) + 28500;
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001771 cxd2841er_unfreeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001772 return 0;
1773}
1774
Mauro Carvalho Chehabc8946c82015-08-11 15:08:47 -03001775static int cxd2841er_read_snr_t2(struct cxd2841er_priv *priv, u32 *snr)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001776{
1777 u32 reg;
1778 u8 data[2];
1779
1780 *snr = 0;
1781 if (priv->state != STATE_ACTIVE_TC) {
1782 dev_dbg(&priv->i2c->dev,
1783 "%s(): invalid state %d\n", __func__, priv->state);
1784 return -EINVAL;
1785 }
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001786
1787 cxd2841er_freeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001788 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1789 cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
1790 reg = ((u32)data[0] << 8) | (u32)data[1];
1791 if (reg == 0) {
1792 dev_dbg(&priv->i2c->dev,
1793 "%s(): reg value out of range\n", __func__);
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001794 cxd2841er_unfreeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001795 return 0;
1796 }
1797 if (reg > 10876)
1798 reg = 10876;
1799 *snr = 10000 * ((intlog10(reg) -
1800 intlog10(12600 - reg)) >> 24) + 32000;
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001801 cxd2841er_unfreeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001802 return 0;
1803}
1804
Abylay Ospan83808c22016-03-22 19:20:34 -03001805static int cxd2841er_read_snr_i(struct cxd2841er_priv *priv, u32 *snr)
1806{
1807 u32 reg;
1808 u8 data[2];
1809
1810 *snr = 0;
1811 if (priv->state != STATE_ACTIVE_TC) {
1812 dev_dbg(&priv->i2c->dev,
1813 "%s(): invalid state %d\n", __func__,
1814 priv->state);
1815 return -EINVAL;
1816 }
1817
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001818 cxd2841er_freeze_regs(priv);
Abylay Ospan83808c22016-03-22 19:20:34 -03001819 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1820 cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
1821 reg = ((u32)data[0] << 8) | (u32)data[1];
1822 if (reg == 0) {
1823 dev_dbg(&priv->i2c->dev,
1824 "%s(): reg value out of range\n", __func__);
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001825 cxd2841er_unfreeze_regs(priv);
Abylay Ospan83808c22016-03-22 19:20:34 -03001826 return 0;
1827 }
Abylay Ospan0854df72016-07-19 12:22:03 -03001828 *snr = 10000 * (intlog10(reg) >> 24) - 9031;
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001829 cxd2841er_unfreeze_regs(priv);
Abylay Ospan83808c22016-03-22 19:20:34 -03001830 return 0;
1831}
1832
Abylay Ospand0998ce2016-06-30 23:09:48 -03001833static u16 cxd2841er_read_agc_gain_c(struct cxd2841er_priv *priv,
1834 u8 delsys)
1835{
1836 u8 data[2];
1837
1838 cxd2841er_write_reg(
1839 priv, I2C_SLVT, 0x00, 0x40);
1840 cxd2841er_read_regs(priv, I2C_SLVT, 0x49, data, 2);
1841 dev_dbg(&priv->i2c->dev,
1842 "%s(): AGC value=%u\n",
1843 __func__, (((u16)data[0] & 0x0F) << 8) |
1844 (u16)(data[1] & 0xFF));
1845 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1846}
1847
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001848static u16 cxd2841er_read_agc_gain_t_t2(struct cxd2841er_priv *priv,
1849 u8 delsys)
1850{
1851 u8 data[2];
1852
1853 cxd2841er_write_reg(
1854 priv, I2C_SLVT, 0x00, (delsys == SYS_DVBT ? 0x10 : 0x20));
1855 cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03001856 dev_dbg(&priv->i2c->dev,
1857 "%s(): AGC value=%u\n",
1858 __func__, (((u16)data[0] & 0x0F) << 8) |
1859 (u16)(data[1] & 0xFF));
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001860 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1861}
1862
Abylay Ospan83808c22016-03-22 19:20:34 -03001863static u16 cxd2841er_read_agc_gain_i(struct cxd2841er_priv *priv,
1864 u8 delsys)
1865{
1866 u8 data[2];
1867
1868 cxd2841er_write_reg(
1869 priv, I2C_SLVT, 0x00, 0x60);
1870 cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
1871
1872 dev_dbg(&priv->i2c->dev,
1873 "%s(): AGC value=%u\n",
1874 __func__, (((u16)data[0] & 0x0F) << 8) |
1875 (u16)(data[1] & 0xFF));
1876 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1877}
1878
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001879static u16 cxd2841er_read_agc_gain_s(struct cxd2841er_priv *priv)
1880{
1881 u8 data[2];
1882
1883 /* Set SLV-T Bank : 0xA0 */
1884 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1885 /*
1886 * slave Bank Addr Bit Signal name
1887 * <SLV-T> A0h 1Fh [4:0] IRFAGC_GAIN[12:8]
1888 * <SLV-T> A0h 20h [7:0] IRFAGC_GAIN[7:0]
1889 */
1890 cxd2841er_read_regs(priv, I2C_SLVT, 0x1f, data, 2);
1891 return ((((u16)data[0] & 0x1F) << 8) | (u16)(data[1] & 0xFF)) << 3;
1892}
1893
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001894static void cxd2841er_read_ber(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001895{
1896 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1897 struct cxd2841er_priv *priv = fe->demodulator_priv;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001898 u32 ret, bit_error = 0, bit_count = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001899
1900 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001901 switch (p->delivery_system) {
Abylay Ospana6f330c2016-07-15 15:34:22 -03001902 case SYS_DVBC_ANNEX_A:
1903 case SYS_DVBC_ANNEX_B:
1904 case SYS_DVBC_ANNEX_C:
1905 ret = cxd2841er_read_ber_c(priv, &bit_error, &bit_count);
1906 break;
Abylay Ospan0854df72016-07-19 12:22:03 -03001907 case SYS_ISDBT:
1908 ret = cxd2841er_read_ber_i(priv, &bit_error, &bit_count);
1909 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001910 case SYS_DVBS:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001911 ret = cxd2841er_mon_read_ber_s(priv, &bit_error, &bit_count);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001912 break;
1913 case SYS_DVBS2:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001914 ret = cxd2841er_mon_read_ber_s2(priv, &bit_error, &bit_count);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001915 break;
1916 case SYS_DVBT:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001917 ret = cxd2841er_read_ber_t(priv, &bit_error, &bit_count);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001918 break;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001919 case SYS_DVBT2:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001920 ret = cxd2841er_read_ber_t2(priv, &bit_error, &bit_count);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001921 break;
1922 default:
1923 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001924 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001925 return;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001926 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001927
1928 if (!ret) {
1929 p->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
Abylay Ospana6f330c2016-07-15 15:34:22 -03001930 p->post_bit_error.stat[0].uvalue += bit_error;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001931 p->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Abylay Ospana6f330c2016-07-15 15:34:22 -03001932 p->post_bit_count.stat[0].uvalue += bit_count;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001933 } else {
1934 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001935 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001936 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001937}
1938
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03001939static void cxd2841er_read_signal_strength(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001940{
1941 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1942 struct cxd2841er_priv *priv = fe->demodulator_priv;
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001943 s32 strength;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001944
1945 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1946 switch (p->delivery_system) {
1947 case SYS_DVBT:
1948 case SYS_DVBT2:
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03001949 strength = cxd2841er_read_agc_gain_t_t2(priv,
1950 p->delivery_system);
1951 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1952 /* Formula was empirically determinated @ 410 MHz */
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001953 p->strength.stat[0].uvalue = strength * 366 / 100 - 89520;
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03001954 break; /* Code moved out of the function */
Mauro Carvalho Chehab988bd282016-07-01 11:03:14 -03001955 case SYS_DVBC_ANNEX_A:
Abylay Ospan997bdc02016-07-15 14:59:37 -03001956 case SYS_DVBC_ANNEX_B:
1957 case SYS_DVBC_ANNEX_C:
1958 strength = cxd2841er_read_agc_gain_c(priv,
Mauro Carvalho Chehab988bd282016-07-01 11:03:14 -03001959 p->delivery_system);
Mauro Carvalho Chehabd12b7912016-07-01 11:03:16 -03001960 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1961 /*
1962 * Formula was empirically determinated via linear regression,
1963 * using frequencies: 175 MHz, 410 MHz and 800 MHz, and a
1964 * stream modulated with QAM64
1965 */
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001966 p->strength.stat[0].uvalue = strength * 4045 / 1000 - 85224;
Mauro Carvalho Chehab988bd282016-07-01 11:03:14 -03001967 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03001968 case SYS_ISDBT:
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001969 strength = cxd2841er_read_agc_gain_i(priv, p->delivery_system);
1970 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1971 /*
1972 * Formula was empirically determinated via linear regression,
1973 * using frequencies: 175 MHz, 410 MHz and 800 MHz.
1974 */
1975 p->strength.stat[0].uvalue = strength * 3775 / 1000 - 90185;
Abylay Ospan83808c22016-03-22 19:20:34 -03001976 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001977 case SYS_DVBS:
1978 case SYS_DVBS2:
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03001979 strength = 65535 - cxd2841er_read_agc_gain_s(priv);
1980 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
1981 p->strength.stat[0].uvalue = strength;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001982 break;
1983 default:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001984 p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001985 break;
1986 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001987}
1988
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001989static void cxd2841er_read_snr(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001990{
1991 u32 tmp = 0;
Abylay Ospane05b1872016-07-15 17:04:17 -03001992 int ret = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001993 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1994 struct cxd2841er_priv *priv = fe->demodulator_priv;
1995
1996 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1997 switch (p->delivery_system) {
Abylay Ospane05b1872016-07-15 17:04:17 -03001998 case SYS_DVBC_ANNEX_A:
1999 case SYS_DVBC_ANNEX_B:
2000 case SYS_DVBC_ANNEX_C:
2001 ret = cxd2841er_read_snr_c(priv, &tmp);
2002 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002003 case SYS_DVBT:
Abylay Ospane05b1872016-07-15 17:04:17 -03002004 ret = cxd2841er_read_snr_t(priv, &tmp);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002005 break;
2006 case SYS_DVBT2:
Abylay Ospane05b1872016-07-15 17:04:17 -03002007 ret = cxd2841er_read_snr_t2(priv, &tmp);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002008 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03002009 case SYS_ISDBT:
Abylay Ospane05b1872016-07-15 17:04:17 -03002010 ret = cxd2841er_read_snr_i(priv, &tmp);
Abylay Ospan83808c22016-03-22 19:20:34 -03002011 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002012 case SYS_DVBS:
2013 case SYS_DVBS2:
Abylay Ospane05b1872016-07-15 17:04:17 -03002014 ret = cxd2841er_dvbs_read_snr(priv, p->delivery_system, &tmp);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002015 break;
2016 default:
2017 dev_dbg(&priv->i2c->dev, "%s(): unknown delivery system %d\n",
2018 __func__, p->delivery_system);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002019 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2020 return;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002021 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002022
Abylay Ospan0854df72016-07-19 12:22:03 -03002023 dev_dbg(&priv->i2c->dev, "%s(): snr=%d\n",
2024 __func__, (int32_t)tmp);
2025
Abylay Ospane05b1872016-07-15 17:04:17 -03002026 if (!ret) {
2027 p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
2028 p->cnr.stat[0].svalue = tmp;
2029 } else {
2030 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2031 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002032}
2033
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002034static void cxd2841er_read_ucblocks(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002035{
2036 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2037 struct cxd2841er_priv *priv = fe->demodulator_priv;
Abylay Ospan4a86bc12016-07-19 00:10:20 -03002038 u32 ucblocks = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002039
2040 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2041 switch (p->delivery_system) {
Abylay Ospana6f330c2016-07-15 15:34:22 -03002042 case SYS_DVBC_ANNEX_A:
2043 case SYS_DVBC_ANNEX_B:
2044 case SYS_DVBC_ANNEX_C:
2045 cxd2841er_read_packet_errors_c(priv, &ucblocks);
2046 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002047 case SYS_DVBT:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002048 cxd2841er_read_packet_errors_t(priv, &ucblocks);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002049 break;
2050 case SYS_DVBT2:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002051 cxd2841er_read_packet_errors_t2(priv, &ucblocks);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002052 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03002053 case SYS_ISDBT:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002054 cxd2841er_read_packet_errors_i(priv, &ucblocks);
Abylay Ospan83808c22016-03-22 19:20:34 -03002055 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002056 default:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002057 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2058 return;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002059 }
Abylay Ospan4a86bc12016-07-19 00:10:20 -03002060 dev_dbg(&priv->i2c->dev, "%s() ucblocks=%u\n", __func__, ucblocks);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002061
2062 p->block_error.stat[0].scale = FE_SCALE_COUNTER;
2063 p->block_error.stat[0].uvalue = ucblocks;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002064}
2065
2066static int cxd2841er_dvbt2_set_profile(
2067 struct cxd2841er_priv *priv, enum cxd2841er_dvbt2_profile_t profile)
2068{
2069 u8 tune_mode;
2070 u8 seq_not2d_time;
2071
2072 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2073 switch (profile) {
2074 case DVBT2_PROFILE_BASE:
2075 tune_mode = 0x01;
Abylay Ospan6c771612016-05-16 11:43:25 -03002076 /* Set early unlock time */
2077 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x0E:0x0C;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002078 break;
2079 case DVBT2_PROFILE_LITE:
2080 tune_mode = 0x05;
Abylay Ospan6c771612016-05-16 11:43:25 -03002081 /* Set early unlock time */
2082 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002083 break;
2084 case DVBT2_PROFILE_ANY:
2085 tune_mode = 0x00;
Abylay Ospan6c771612016-05-16 11:43:25 -03002086 /* Set early unlock time */
2087 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002088 break;
2089 default:
2090 return -EINVAL;
2091 }
2092 /* Set SLV-T Bank : 0x2E */
2093 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2e);
2094 /* Set profile and tune mode */
2095 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x10, tune_mode, 0x07);
2096 /* Set SLV-T Bank : 0x2B */
2097 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2098 /* Set early unlock detection time */
2099 cxd2841er_write_reg(priv, I2C_SLVT, 0x9d, seq_not2d_time);
2100 return 0;
2101}
2102
2103static int cxd2841er_dvbt2_set_plp_config(struct cxd2841er_priv *priv,
2104 u8 is_auto, u8 plp_id)
2105{
2106 if (is_auto) {
2107 dev_dbg(&priv->i2c->dev,
2108 "%s() using auto PLP selection\n", __func__);
2109 } else {
2110 dev_dbg(&priv->i2c->dev,
2111 "%s() using manual PLP selection, ID %d\n",
2112 __func__, plp_id);
2113 }
2114 /* Set SLV-T Bank : 0x23 */
2115 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
2116 if (!is_auto) {
2117 /* Manual PLP selection mode. Set the data PLP Id. */
2118 cxd2841er_write_reg(priv, I2C_SLVT, 0xaf, plp_id);
2119 }
2120 /* Auto PLP select (Scanning mode = 0x00). Data PLP select = 0x01. */
2121 cxd2841er_write_reg(priv, I2C_SLVT, 0xad, (is_auto ? 0x00 : 0x01));
2122 return 0;
2123}
2124
2125static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
2126 u32 bandwidth)
2127{
2128 u32 iffreq;
Abylay Ospan6c771612016-05-16 11:43:25 -03002129 u8 data[MAX_WRITE_REGSIZE];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002130
Abylay Ospan6c771612016-05-16 11:43:25 -03002131 const uint8_t nominalRate8bw[3][5] = {
2132 /* TRCG Nominal Rate [37:0] */
2133 {0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2134 {0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2135 {0x11, 0xF0, 0x00, 0x00, 0x00} /* 41MHz XTal */
2136 };
2137
2138 const uint8_t nominalRate7bw[3][5] = {
2139 /* TRCG Nominal Rate [37:0] */
2140 {0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2141 {0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2142 {0x14, 0x80, 0x00, 0x00, 0x00} /* 41MHz XTal */
2143 };
2144
2145 const uint8_t nominalRate6bw[3][5] = {
2146 /* TRCG Nominal Rate [37:0] */
2147 {0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
2148 {0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2149 {0x17, 0xEA, 0xAA, 0xAA, 0xAA} /* 41MHz XTal */
2150 };
2151
2152 const uint8_t nominalRate5bw[3][5] = {
2153 /* TRCG Nominal Rate [37:0] */
2154 {0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
2155 {0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
2156 {0x1C, 0xB3, 0x33, 0x33, 0x33} /* 41MHz XTal */
2157 };
2158
2159 const uint8_t nominalRate17bw[3][5] = {
2160 /* TRCG Nominal Rate [37:0] */
2161 {0x58, 0xE2, 0xAF, 0xE0, 0xBC}, /* 20.5MHz XTal */
2162 {0x68, 0x0F, 0xA2, 0x32, 0xD0}, /* 24MHz XTal */
2163 {0x58, 0xE2, 0xAF, 0xE0, 0xBC} /* 41MHz XTal */
2164 };
2165
2166 const uint8_t itbCoef8bw[3][14] = {
2167 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
2168 0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
2169 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1,
2170 0x29, 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal */
2171 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
2172 0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8} /* 41MHz XTal */
2173 };
2174
2175 const uint8_t itbCoef7bw[3][14] = {
2176 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
2177 0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
2178 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0,
2179 0x29, 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal */
2180 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
2181 0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5} /* 41MHz XTal */
2182 };
2183
2184 const uint8_t itbCoef6bw[3][14] = {
2185 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2186 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2187 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
2188 0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2189 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2190 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2191 };
2192
2193 const uint8_t itbCoef5bw[3][14] = {
2194 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2195 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2196 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
2197 0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2198 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2199 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2200 };
2201
2202 const uint8_t itbCoef17bw[3][14] = {
2203 {0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
2204 0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99}, /* 20.5MHz XTal */
2205 {0x33, 0x8E, 0x2B, 0x97, 0x2D, 0x95, 0x37, 0x8B,
2206 0x30, 0x97, 0x2D, 0x9A, 0x21, 0xA4}, /* 24MHz XTal */
2207 {0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
2208 0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99} /* 41MHz XTal */
2209 };
2210
2211 /* Set SLV-T Bank : 0x20 */
2212 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
2213
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002214 switch (bandwidth) {
2215 case 8000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002216 /* <Timing Recovery setting> */
2217 cxd2841er_write_regs(priv, I2C_SLVT,
2218 0x9F, nominalRate8bw[priv->xtal], 5);
2219
2220 /* Set SLV-T Bank : 0x27 */
2221 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2222 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2223 0x7a, 0x00, 0x0f);
2224
2225 /* Set SLV-T Bank : 0x10 */
2226 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2227
2228 /* Group delay equaliser settings for
2229 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2230 */
2231 cxd2841er_write_regs(priv, I2C_SLVT,
2232 0xA6, itbCoef8bw[priv->xtal], 14);
2233 /* <IF freq setting> */
2234 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.80);
2235 data[0] = (u8) ((iffreq >> 16) & 0xff);
2236 data[1] = (u8)((iffreq >> 8) & 0xff);
2237 data[2] = (u8)(iffreq & 0xff);
2238 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2239 /* System bandwidth setting */
2240 cxd2841er_set_reg_bits(
2241 priv, I2C_SLVT, 0xD7, 0x00, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002242 break;
2243 case 7000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002244 /* <Timing Recovery setting> */
2245 cxd2841er_write_regs(priv, I2C_SLVT,
2246 0x9F, nominalRate7bw[priv->xtal], 5);
2247
2248 /* Set SLV-T Bank : 0x27 */
2249 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2250 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2251 0x7a, 0x00, 0x0f);
2252
2253 /* Set SLV-T Bank : 0x10 */
2254 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2255
2256 /* Group delay equaliser settings for
2257 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2258 */
2259 cxd2841er_write_regs(priv, I2C_SLVT,
2260 0xA6, itbCoef7bw[priv->xtal], 14);
2261 /* <IF freq setting> */
2262 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.20);
2263 data[0] = (u8) ((iffreq >> 16) & 0xff);
2264 data[1] = (u8)((iffreq >> 8) & 0xff);
2265 data[2] = (u8)(iffreq & 0xff);
2266 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2267 /* System bandwidth setting */
2268 cxd2841er_set_reg_bits(
2269 priv, I2C_SLVT, 0xD7, 0x02, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002270 break;
2271 case 6000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002272 /* <Timing Recovery setting> */
2273 cxd2841er_write_regs(priv, I2C_SLVT,
2274 0x9F, nominalRate6bw[priv->xtal], 5);
2275
2276 /* Set SLV-T Bank : 0x27 */
2277 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2278 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2279 0x7a, 0x00, 0x0f);
2280
2281 /* Set SLV-T Bank : 0x10 */
2282 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2283
2284 /* Group delay equaliser settings for
2285 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2286 */
2287 cxd2841er_write_regs(priv, I2C_SLVT,
2288 0xA6, itbCoef6bw[priv->xtal], 14);
2289 /* <IF freq setting> */
2290 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
2291 data[0] = (u8) ((iffreq >> 16) & 0xff);
2292 data[1] = (u8)((iffreq >> 8) & 0xff);
2293 data[2] = (u8)(iffreq & 0xff);
2294 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2295 /* System bandwidth setting */
2296 cxd2841er_set_reg_bits(
2297 priv, I2C_SLVT, 0xD7, 0x04, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002298 break;
2299 case 5000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002300 /* <Timing Recovery setting> */
2301 cxd2841er_write_regs(priv, I2C_SLVT,
2302 0x9F, nominalRate5bw[priv->xtal], 5);
2303
2304 /* Set SLV-T Bank : 0x27 */
2305 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2306 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2307 0x7a, 0x00, 0x0f);
2308
2309 /* Set SLV-T Bank : 0x10 */
2310 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2311
2312 /* Group delay equaliser settings for
2313 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2314 */
2315 cxd2841er_write_regs(priv, I2C_SLVT,
2316 0xA6, itbCoef5bw[priv->xtal], 14);
2317 /* <IF freq setting> */
2318 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
2319 data[0] = (u8) ((iffreq >> 16) & 0xff);
2320 data[1] = (u8)((iffreq >> 8) & 0xff);
2321 data[2] = (u8)(iffreq & 0xff);
2322 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2323 /* System bandwidth setting */
2324 cxd2841er_set_reg_bits(
2325 priv, I2C_SLVT, 0xD7, 0x06, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002326 break;
2327 case 1712000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002328 /* <Timing Recovery setting> */
2329 cxd2841er_write_regs(priv, I2C_SLVT,
2330 0x9F, nominalRate17bw[priv->xtal], 5);
2331
2332 /* Set SLV-T Bank : 0x27 */
2333 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2334 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2335 0x7a, 0x03, 0x0f);
2336
2337 /* Set SLV-T Bank : 0x10 */
2338 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2339
2340 /* Group delay equaliser settings for
2341 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2342 */
2343 cxd2841er_write_regs(priv, I2C_SLVT,
2344 0xA6, itbCoef17bw[priv->xtal], 14);
2345 /* <IF freq setting> */
2346 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.50);
2347 data[0] = (u8) ((iffreq >> 16) & 0xff);
2348 data[1] = (u8)((iffreq >> 8) & 0xff);
2349 data[2] = (u8)(iffreq & 0xff);
2350 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2351 /* System bandwidth setting */
2352 cxd2841er_set_reg_bits(
2353 priv, I2C_SLVT, 0xD7, 0x03, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002354 break;
2355 default:
2356 return -EINVAL;
2357 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002358 return 0;
2359}
2360
2361static int cxd2841er_sleep_tc_to_active_t_band(
2362 struct cxd2841er_priv *priv, u32 bandwidth)
2363{
Abylay Ospan83808c22016-03-22 19:20:34 -03002364 u8 data[MAX_WRITE_REGSIZE];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002365 u32 iffreq;
Abylay Ospan83808c22016-03-22 19:20:34 -03002366 u8 nominalRate8bw[3][5] = {
2367 /* TRCG Nominal Rate [37:0] */
2368 {0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2369 {0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2370 {0x11, 0xF0, 0x00, 0x00, 0x00} /* 41MHz XTal */
2371 };
2372 u8 nominalRate7bw[3][5] = {
2373 /* TRCG Nominal Rate [37:0] */
2374 {0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2375 {0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2376 {0x14, 0x80, 0x00, 0x00, 0x00} /* 41MHz XTal */
2377 };
2378 u8 nominalRate6bw[3][5] = {
2379 /* TRCG Nominal Rate [37:0] */
2380 {0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
2381 {0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2382 {0x17, 0xEA, 0xAA, 0xAA, 0xAA} /* 41MHz XTal */
2383 };
2384 u8 nominalRate5bw[3][5] = {
2385 /* TRCG Nominal Rate [37:0] */
2386 {0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
2387 {0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
2388 {0x1C, 0xB3, 0x33, 0x33, 0x33} /* 41MHz XTal */
2389 };
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002390
Abylay Ospan83808c22016-03-22 19:20:34 -03002391 u8 itbCoef8bw[3][14] = {
2392 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
2393 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
2394 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29, 0xA5,
2395 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal */
2396 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
2397 0x1F, 0xA8, 0x2C, 0xC8} /* 41MHz XTal */
2398 };
2399 u8 itbCoef7bw[3][14] = {
2400 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
2401 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
2402 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29, 0xA2,
2403 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal */
2404 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
2405 0x26, 0xA9, 0x21, 0xA5} /* 41MHz XTal */
2406 };
2407 u8 itbCoef6bw[3][14] = {
2408 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2409 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2410 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
2411 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2412 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2413 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2414 };
2415 u8 itbCoef5bw[3][14] = {
2416 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2417 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2418 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
2419 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2420 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2421 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2422 };
2423
2424 /* Set SLV-T Bank : 0x13 */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002425 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
2426 /* Echo performance optimization setting */
Abylay Ospan83808c22016-03-22 19:20:34 -03002427 data[0] = 0x01;
2428 data[1] = 0x14;
2429 cxd2841er_write_regs(priv, I2C_SLVT, 0x9C, data, 2);
2430
2431 /* Set SLV-T Bank : 0x10 */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002432 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2433
2434 switch (bandwidth) {
2435 case 8000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002436 /* <Timing Recovery setting> */
2437 cxd2841er_write_regs(priv, I2C_SLVT,
2438 0x9F, nominalRate8bw[priv->xtal], 5);
2439 /* Group delay equaliser settings for
2440 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2441 */
2442 cxd2841er_write_regs(priv, I2C_SLVT,
2443 0xA6, itbCoef8bw[priv->xtal], 14);
2444 /* <IF freq setting> */
2445 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.80);
2446 data[0] = (u8) ((iffreq >> 16) & 0xff);
2447 data[1] = (u8)((iffreq >> 8) & 0xff);
2448 data[2] = (u8)(iffreq & 0xff);
2449 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2450 /* System bandwidth setting */
2451 cxd2841er_set_reg_bits(
2452 priv, I2C_SLVT, 0xD7, 0x00, 0x07);
2453
2454 /* Demod core latency setting */
2455 if (priv->xtal == SONY_XTAL_24000) {
2456 data[0] = 0x15;
2457 data[1] = 0x28;
2458 } else {
2459 data[0] = 0x01;
2460 data[1] = 0xE0;
2461 }
2462 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2463
2464 /* Notch filter setting */
2465 data[0] = 0x01;
2466 data[1] = 0x02;
2467 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2468 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002469 break;
2470 case 7000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002471 /* <Timing Recovery setting> */
2472 cxd2841er_write_regs(priv, I2C_SLVT,
2473 0x9F, nominalRate7bw[priv->xtal], 5);
2474 /* Group delay equaliser settings for
2475 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2476 */
2477 cxd2841er_write_regs(priv, I2C_SLVT,
2478 0xA6, itbCoef7bw[priv->xtal], 14);
2479 /* <IF freq setting> */
2480 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.20);
2481 data[0] = (u8) ((iffreq >> 16) & 0xff);
2482 data[1] = (u8)((iffreq >> 8) & 0xff);
2483 data[2] = (u8)(iffreq & 0xff);
2484 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2485 /* System bandwidth setting */
2486 cxd2841er_set_reg_bits(
2487 priv, I2C_SLVT, 0xD7, 0x02, 0x07);
2488
2489 /* Demod core latency setting */
2490 if (priv->xtal == SONY_XTAL_24000) {
2491 data[0] = 0x1F;
2492 data[1] = 0xF8;
2493 } else {
2494 data[0] = 0x12;
2495 data[1] = 0xF8;
2496 }
2497 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2498
2499 /* Notch filter setting */
2500 data[0] = 0x00;
2501 data[1] = 0x03;
2502 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2503 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002504 break;
2505 case 6000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002506 /* <Timing Recovery setting> */
2507 cxd2841er_write_regs(priv, I2C_SLVT,
2508 0x9F, nominalRate6bw[priv->xtal], 5);
2509 /* Group delay equaliser settings for
2510 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2511 */
2512 cxd2841er_write_regs(priv, I2C_SLVT,
2513 0xA6, itbCoef6bw[priv->xtal], 14);
2514 /* <IF freq setting> */
2515 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
2516 data[0] = (u8) ((iffreq >> 16) & 0xff);
2517 data[1] = (u8)((iffreq >> 8) & 0xff);
2518 data[2] = (u8)(iffreq & 0xff);
2519 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2520 /* System bandwidth setting */
2521 cxd2841er_set_reg_bits(
2522 priv, I2C_SLVT, 0xD7, 0x04, 0x07);
2523
2524 /* Demod core latency setting */
2525 if (priv->xtal == SONY_XTAL_24000) {
2526 data[0] = 0x25;
2527 data[1] = 0x4C;
2528 } else {
2529 data[0] = 0x1F;
2530 data[1] = 0xDC;
2531 }
2532 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2533
2534 /* Notch filter setting */
2535 data[0] = 0x00;
2536 data[1] = 0x03;
2537 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2538 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002539 break;
2540 case 5000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002541 /* <Timing Recovery setting> */
2542 cxd2841er_write_regs(priv, I2C_SLVT,
2543 0x9F, nominalRate5bw[priv->xtal], 5);
2544 /* Group delay equaliser settings for
2545 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2546 */
2547 cxd2841er_write_regs(priv, I2C_SLVT,
2548 0xA6, itbCoef5bw[priv->xtal], 14);
2549 /* <IF freq setting> */
2550 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.60);
2551 data[0] = (u8) ((iffreq >> 16) & 0xff);
2552 data[1] = (u8)((iffreq >> 8) & 0xff);
2553 data[2] = (u8)(iffreq & 0xff);
2554 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2555 /* System bandwidth setting */
2556 cxd2841er_set_reg_bits(
2557 priv, I2C_SLVT, 0xD7, 0x06, 0x07);
2558
2559 /* Demod core latency setting */
2560 if (priv->xtal == SONY_XTAL_24000) {
2561 data[0] = 0x2C;
2562 data[1] = 0xC2;
2563 } else {
2564 data[0] = 0x26;
2565 data[1] = 0x3C;
2566 }
2567 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2568
2569 /* Notch filter setting */
2570 data[0] = 0x00;
2571 data[1] = 0x03;
2572 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2573 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002574 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03002575 }
2576
2577 return 0;
2578}
2579
2580static int cxd2841er_sleep_tc_to_active_i_band(
2581 struct cxd2841er_priv *priv, u32 bandwidth)
2582{
2583 u32 iffreq;
2584 u8 data[3];
2585
2586 /* TRCG Nominal Rate */
2587 u8 nominalRate8bw[3][5] = {
2588 {0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2589 {0x11, 0xB8, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2590 {0x00, 0x00, 0x00, 0x00, 0x00} /* 41MHz XTal */
2591 };
2592
2593 u8 nominalRate7bw[3][5] = {
2594 {0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2595 {0x14, 0x40, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2596 {0x00, 0x00, 0x00, 0x00, 0x00} /* 41MHz XTal */
2597 };
2598
2599 u8 nominalRate6bw[3][5] = {
2600 {0x14, 0x2E, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2601 {0x17, 0xA0, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2602 {0x14, 0x2E, 0x00, 0x00, 0x00} /* 41MHz XTal */
2603 };
2604
2605 u8 itbCoef8bw[3][14] = {
2606 {0x00}, /* 20.5MHz XTal */
2607 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29,
2608 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz Xtal */
2609 {0x0}, /* 41MHz XTal */
2610 };
2611
2612 u8 itbCoef7bw[3][14] = {
2613 {0x00}, /* 20.5MHz XTal */
2614 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29,
2615 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz Xtal */
2616 {0x00}, /* 41MHz XTal */
2617 };
2618
2619 u8 itbCoef6bw[3][14] = {
2620 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
2621 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2622 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29,
2623 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz Xtal */
2624 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
2625 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 41MHz XTal */
2626 };
2627
2628 dev_dbg(&priv->i2c->dev, "%s() bandwidth=%u\n", __func__, bandwidth);
2629 /* Set SLV-T Bank : 0x10 */
2630 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2631
2632 /* 20.5/41MHz Xtal support is not available
2633 * on ISDB-T 7MHzBW and 8MHzBW
2634 */
2635 if (priv->xtal != SONY_XTAL_24000 && bandwidth > 6000000) {
2636 dev_err(&priv->i2c->dev,
2637 "%s(): bandwidth %d supported only for 24MHz xtal\n",
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002638 __func__, bandwidth);
2639 return -EINVAL;
2640 }
Abylay Ospan83808c22016-03-22 19:20:34 -03002641
2642 switch (bandwidth) {
2643 case 8000000:
2644 /* TRCG Nominal Rate */
2645 cxd2841er_write_regs(priv, I2C_SLVT,
2646 0x9F, nominalRate8bw[priv->xtal], 5);
2647 /* Group delay equaliser settings for ASCOT tuners optimized */
2648 cxd2841er_write_regs(priv, I2C_SLVT,
2649 0xA6, itbCoef8bw[priv->xtal], 14);
2650
2651 /* IF freq setting */
2652 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.75);
2653 data[0] = (u8) ((iffreq >> 16) & 0xff);
2654 data[1] = (u8)((iffreq >> 8) & 0xff);
2655 data[2] = (u8)(iffreq & 0xff);
2656 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2657
2658 /* System bandwidth setting */
2659 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x0, 0x7);
2660
2661 /* Demod core latency setting */
2662 data[0] = 0x13;
2663 data[1] = 0xFC;
2664 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2665
2666 /* Acquisition optimization setting */
2667 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2668 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
2669 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2670 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x03);
2671 break;
2672 case 7000000:
2673 /* TRCG Nominal Rate */
2674 cxd2841er_write_regs(priv, I2C_SLVT,
2675 0x9F, nominalRate7bw[priv->xtal], 5);
2676 /* Group delay equaliser settings for ASCOT tuners optimized */
2677 cxd2841er_write_regs(priv, I2C_SLVT,
2678 0xA6, itbCoef7bw[priv->xtal], 14);
2679
2680 /* IF freq setting */
2681 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 4.15);
2682 data[0] = (u8) ((iffreq >> 16) & 0xff);
2683 data[1] = (u8)((iffreq >> 8) & 0xff);
2684 data[2] = (u8)(iffreq & 0xff);
2685 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2686
2687 /* System bandwidth setting */
2688 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x02, 0x7);
2689
2690 /* Demod core latency setting */
2691 data[0] = 0x1A;
2692 data[1] = 0xFA;
2693 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2694
2695 /* Acquisition optimization setting */
2696 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2697 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
2698 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2699 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
2700 break;
2701 case 6000000:
2702 /* TRCG Nominal Rate */
2703 cxd2841er_write_regs(priv, I2C_SLVT,
2704 0x9F, nominalRate6bw[priv->xtal], 5);
2705 /* Group delay equaliser settings for ASCOT tuners optimized */
2706 cxd2841er_write_regs(priv, I2C_SLVT,
2707 0xA6, itbCoef6bw[priv->xtal], 14);
2708
2709 /* IF freq setting */
2710 iffreq = MAKE_IFFREQ_CONFIG_XTAL(priv->xtal, 3.55);
2711 data[0] = (u8) ((iffreq >> 16) & 0xff);
2712 data[1] = (u8)((iffreq >> 8) & 0xff);
2713 data[2] = (u8)(iffreq & 0xff);
2714 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2715
2716 /* System bandwidth setting */
2717 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x04, 0x7);
2718
2719 /* Demod core latency setting */
2720 if (priv->xtal == SONY_XTAL_24000) {
2721 data[0] = 0x1F;
2722 data[1] = 0x79;
2723 } else {
2724 data[0] = 0x1A;
2725 data[1] = 0xE2;
2726 }
2727 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2728
2729 /* Acquisition optimization setting */
2730 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2731 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x07, 0x07);
2732 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2733 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
2734 break;
2735 default:
2736 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
2737 __func__, bandwidth);
2738 return -EINVAL;
2739 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002740 return 0;
2741}
2742
2743static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
2744 u32 bandwidth)
2745{
2746 u8 bw7_8mhz_b10_a6[] = {
2747 0x2D, 0xC7, 0x04, 0xF4, 0x07, 0xC5, 0x2A, 0xB8,
2748 0x27, 0x9E, 0x27, 0xA4, 0x29, 0xAB };
2749 u8 bw6mhz_b10_a6[] = {
2750 0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2751 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4 };
2752 u8 b10_b6[3];
2753 u32 iffreq;
2754
Abylay Ospanaf4cc462016-07-21 10:56:25 -03002755 if (bandwidth != 6000000 &&
2756 bandwidth != 7000000 &&
2757 bandwidth != 8000000) {
2758 dev_info(&priv->i2c->dev, "%s(): unsupported bandwidth %d. Forcing 8Mhz!\n",
2759 __func__, bandwidth);
2760 bandwidth = 8000000;
2761 }
2762
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03002763 dev_dbg(&priv->i2c->dev, "%s() bw=%d\n", __func__, bandwidth);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002764 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2765 switch (bandwidth) {
2766 case 8000000:
2767 case 7000000:
2768 cxd2841er_write_regs(
2769 priv, I2C_SLVT, 0xa6,
2770 bw7_8mhz_b10_a6, sizeof(bw7_8mhz_b10_a6));
2771 iffreq = MAKE_IFFREQ_CONFIG(4.9);
2772 break;
2773 case 6000000:
2774 cxd2841er_write_regs(
2775 priv, I2C_SLVT, 0xa6,
2776 bw6mhz_b10_a6, sizeof(bw6mhz_b10_a6));
2777 iffreq = MAKE_IFFREQ_CONFIG(3.7);
2778 break;
2779 default:
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03002780 dev_err(&priv->i2c->dev, "%s(): unsupported bandwidth %d\n",
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002781 __func__, bandwidth);
2782 return -EINVAL;
2783 }
2784 /* <IF freq setting> */
2785 b10_b6[0] = (u8) ((iffreq >> 16) & 0xff);
2786 b10_b6[1] = (u8)((iffreq >> 8) & 0xff);
2787 b10_b6[2] = (u8)(iffreq & 0xff);
2788 cxd2841er_write_regs(priv, I2C_SLVT, 0xb6, b10_b6, sizeof(b10_b6));
2789 /* Set SLV-T Bank : 0x11 */
2790 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2791 switch (bandwidth) {
2792 case 8000000:
2793 case 7000000:
2794 cxd2841er_set_reg_bits(
2795 priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
2796 break;
2797 case 6000000:
2798 cxd2841er_set_reg_bits(
2799 priv, I2C_SLVT, 0xa3, 0x14, 0x1f);
2800 break;
2801 }
2802 /* Set SLV-T Bank : 0x40 */
2803 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
2804 switch (bandwidth) {
2805 case 8000000:
2806 cxd2841er_set_reg_bits(
2807 priv, I2C_SLVT, 0x26, 0x0b, 0x0f);
2808 cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0x3e);
2809 break;
2810 case 7000000:
2811 cxd2841er_set_reg_bits(
2812 priv, I2C_SLVT, 0x26, 0x09, 0x0f);
2813 cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0xd6);
2814 break;
2815 case 6000000:
2816 cxd2841er_set_reg_bits(
2817 priv, I2C_SLVT, 0x26, 0x08, 0x0f);
2818 cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0x6e);
2819 break;
2820 }
2821 return 0;
2822}
2823
2824static int cxd2841er_sleep_tc_to_active_t(struct cxd2841er_priv *priv,
2825 u32 bandwidth)
2826{
2827 u8 data[2] = { 0x09, 0x54 };
Abylay Ospan83808c22016-03-22 19:20:34 -03002828 u8 data24m[3] = {0xDC, 0x6C, 0x00};
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002829
2830 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2831 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
2832 /* Set SLV-X Bank : 0x00 */
2833 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
2834 /* Set demod mode */
2835 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
2836 /* Set SLV-T Bank : 0x00 */
2837 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2838 /* Enable demod clock */
2839 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
2840 /* Disable RF level monitor */
2841 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
2842 /* Enable ADC clock */
2843 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
2844 /* Enable ADC 1 */
2845 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
Abylay Ospan83808c22016-03-22 19:20:34 -03002846 /* Enable ADC 2 & 3 */
2847 if (priv->xtal == SONY_XTAL_41000) {
2848 data[0] = 0x0A;
2849 data[1] = 0xD4;
2850 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002851 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
2852 /* Enable ADC 4 */
2853 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
2854 /* Set SLV-T Bank : 0x10 */
2855 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2856 /* IFAGC gain settings */
2857 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
2858 /* Set SLV-T Bank : 0x11 */
2859 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2860 /* BBAGC TARGET level setting */
2861 cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
2862 /* Set SLV-T Bank : 0x10 */
2863 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2864 /* ASCOT setting ON */
2865 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
2866 /* Set SLV-T Bank : 0x18 */
2867 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
2868 /* Pre-RS BER moniter setting */
2869 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x36, 0x40, 0x07);
2870 /* FEC Auto Recovery setting */
2871 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
2872 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x01, 0x01);
2873 /* Set SLV-T Bank : 0x00 */
2874 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2875 /* TSIF setting */
2876 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
2877 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
Abylay Ospan83808c22016-03-22 19:20:34 -03002878
2879 if (priv->xtal == SONY_XTAL_24000) {
2880 /* Set SLV-T Bank : 0x10 */
2881 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2882 cxd2841er_write_reg(priv, I2C_SLVT, 0xBF, 0x60);
2883 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
2884 cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data24m, 3);
2885 }
2886
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002887 cxd2841er_sleep_tc_to_active_t_band(priv, bandwidth);
2888 /* Set SLV-T Bank : 0x00 */
2889 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2890 /* Disable HiZ Setting 1 */
2891 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
2892 /* Disable HiZ Setting 2 */
2893 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
2894 priv->state = STATE_ACTIVE_TC;
2895 return 0;
2896}
2897
2898static int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv,
2899 u32 bandwidth)
2900{
Abylay Ospan6c771612016-05-16 11:43:25 -03002901 u8 data[MAX_WRITE_REGSIZE];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002902
2903 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2904 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT2);
2905 /* Set SLV-X Bank : 0x00 */
2906 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
2907 /* Set demod mode */
2908 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x02);
2909 /* Set SLV-T Bank : 0x00 */
2910 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2911 /* Enable demod clock */
2912 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
2913 /* Disable RF level monitor */
Abylay Ospan6c771612016-05-16 11:43:25 -03002914 cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002915 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
2916 /* Enable ADC clock */
2917 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
2918 /* Enable ADC 1 */
2919 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
Abylay Ospan6c771612016-05-16 11:43:25 -03002920
2921 if (priv->xtal == SONY_XTAL_41000) {
2922 data[0] = 0x0A;
2923 data[1] = 0xD4;
2924 } else {
2925 data[0] = 0x09;
2926 data[1] = 0x54;
2927 }
2928
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002929 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
2930 /* Enable ADC 4 */
2931 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
2932 /* Set SLV-T Bank : 0x10 */
2933 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2934 /* IFAGC gain settings */
2935 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
2936 /* Set SLV-T Bank : 0x11 */
2937 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2938 /* BBAGC TARGET level setting */
2939 cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
2940 /* Set SLV-T Bank : 0x10 */
2941 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2942 /* ASCOT setting ON */
2943 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
2944 /* Set SLV-T Bank : 0x20 */
2945 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
2946 /* Acquisition optimization setting */
2947 cxd2841er_write_reg(priv, I2C_SLVT, 0x8b, 0x3c);
2948 /* Set SLV-T Bank : 0x2b */
2949 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2950 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x76, 0x20, 0x70);
Abylay Ospan6c771612016-05-16 11:43:25 -03002951 /* Set SLV-T Bank : 0x23 */
2952 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
2953 /* L1 Control setting */
2954 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE6, 0x00, 0x03);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002955 /* Set SLV-T Bank : 0x00 */
2956 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2957 /* TSIF setting */
2958 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
2959 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
2960 /* DVB-T2 initial setting */
2961 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
2962 cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x10);
2963 cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x34);
2964 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
2965 cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xd8);
2966 /* Set SLV-T Bank : 0x2a */
2967 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
2968 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x04, 0x0f);
2969 /* Set SLV-T Bank : 0x2b */
2970 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2971 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x20, 0x3f);
2972
Abylay Ospan6c771612016-05-16 11:43:25 -03002973 /* 24MHz Xtal setting */
2974 if (priv->xtal == SONY_XTAL_24000) {
2975 /* Set SLV-T Bank : 0x11 */
2976 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2977 data[0] = 0xEB;
2978 data[1] = 0x03;
2979 data[2] = 0x3B;
2980 cxd2841er_write_regs(priv, I2C_SLVT, 0x33, data, 3);
2981
2982 /* Set SLV-T Bank : 0x20 */
2983 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
2984 data[0] = 0x5E;
2985 data[1] = 0x5E;
2986 data[2] = 0x47;
2987 cxd2841er_write_regs(priv, I2C_SLVT, 0x95, data, 3);
2988
2989 cxd2841er_write_reg(priv, I2C_SLVT, 0x99, 0x18);
2990
2991 data[0] = 0x3F;
2992 data[1] = 0xFF;
2993 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2994
2995 /* Set SLV-T Bank : 0x24 */
2996 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
2997 data[0] = 0x0B;
2998 data[1] = 0x72;
2999 cxd2841er_write_regs(priv, I2C_SLVT, 0x34, data, 2);
3000
3001 data[0] = 0x93;
3002 data[1] = 0xF3;
3003 data[2] = 0x00;
3004 cxd2841er_write_regs(priv, I2C_SLVT, 0xD2, data, 3);
3005
3006 data[0] = 0x05;
3007 data[1] = 0xB8;
3008 data[2] = 0xD8;
3009 cxd2841er_write_regs(priv, I2C_SLVT, 0xDD, data, 3);
3010
3011 cxd2841er_write_reg(priv, I2C_SLVT, 0xE0, 0x00);
3012
3013 /* Set SLV-T Bank : 0x25 */
3014 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x25);
3015 cxd2841er_write_reg(priv, I2C_SLVT, 0xED, 0x60);
3016
3017 /* Set SLV-T Bank : 0x27 */
3018 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
3019 cxd2841er_write_reg(priv, I2C_SLVT, 0xFA, 0x34);
3020
3021 /* Set SLV-T Bank : 0x2B */
3022 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2B);
3023 cxd2841er_write_reg(priv, I2C_SLVT, 0x4B, 0x2F);
3024 cxd2841er_write_reg(priv, I2C_SLVT, 0x9E, 0x0E);
3025
3026 /* Set SLV-T Bank : 0x2D */
3027 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2D);
3028 data[0] = 0x89;
3029 data[1] = 0x89;
3030 cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data, 2);
3031
3032 /* Set SLV-T Bank : 0x5E */
3033 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x5E);
3034 data[0] = 0x24;
3035 data[1] = 0x95;
3036 cxd2841er_write_regs(priv, I2C_SLVT, 0x8C, data, 2);
3037 }
3038
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003039 cxd2841er_sleep_tc_to_active_t2_band(priv, bandwidth);
3040
3041 /* Set SLV-T Bank : 0x00 */
3042 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3043 /* Disable HiZ Setting 1 */
3044 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3045 /* Disable HiZ Setting 2 */
3046 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3047 priv->state = STATE_ACTIVE_TC;
3048 return 0;
3049}
3050
Abylay Ospan83808c22016-03-22 19:20:34 -03003051/* ISDB-Tb part */
3052static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
3053 u32 bandwidth)
3054{
3055 u8 data[2] = { 0x09, 0x54 };
3056 u8 data24m[2] = {0x60, 0x00};
3057 u8 data24m2[3] = {0xB7, 0x1B, 0x00};
3058
3059 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3060 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
3061 /* Set SLV-X Bank : 0x00 */
3062 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
3063 /* Set demod mode */
3064 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x06);
3065 /* Set SLV-T Bank : 0x00 */
3066 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3067 /* Enable demod clock */
3068 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
3069 /* Enable RF level monitor */
3070 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x01);
3071 cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x01);
3072 /* Enable ADC clock */
3073 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
3074 /* Enable ADC 1 */
3075 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
3076 /* xtal freq 20.5MHz or 24M */
3077 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
3078 /* Enable ADC 4 */
3079 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
3080 /* ASCOT setting ON */
3081 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
3082 /* FEC Auto Recovery setting */
3083 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
3084 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x00, 0x01);
3085 /* ISDB-T initial setting */
3086 /* Set SLV-T Bank : 0x00 */
3087 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3088 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x00, 0x01);
3089 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x00, 0x01);
3090 /* Set SLV-T Bank : 0x10 */
3091 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3092 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x69, 0x04, 0x07);
3093 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x6B, 0x03, 0x07);
3094 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9D, 0x50, 0xFF);
3095 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xD3, 0x06, 0x1F);
3096 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xED, 0x00, 0x01);
3097 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE2, 0xCE, 0x80);
3098 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xF2, 0x13, 0x10);
3099 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x2E, 0x3F);
3100 /* Set SLV-T Bank : 0x15 */
3101 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
3102 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x02, 0x03);
3103 /* Set SLV-T Bank : 0x1E */
3104 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x1E);
3105 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x73, 0x68, 0xFF);
3106 /* Set SLV-T Bank : 0x63 */
3107 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x63);
3108 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x81, 0x00, 0x01);
3109
3110 /* for xtal 24MHz */
3111 /* Set SLV-T Bank : 0x10 */
3112 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3113 cxd2841er_write_regs(priv, I2C_SLVT, 0xBF, data24m, 2);
3114 /* Set SLV-T Bank : 0x60 */
3115 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
3116 cxd2841er_write_regs(priv, I2C_SLVT, 0xA8, data24m2, 3);
3117
3118 cxd2841er_sleep_tc_to_active_i_band(priv, bandwidth);
3119 /* Set SLV-T Bank : 0x00 */
3120 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3121 /* Disable HiZ Setting 1 */
3122 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3123 /* Disable HiZ Setting 2 */
3124 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3125 priv->state = STATE_ACTIVE_TC;
3126 return 0;
3127}
3128
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003129static int cxd2841er_sleep_tc_to_active_c(struct cxd2841er_priv *priv,
3130 u32 bandwidth)
3131{
3132 u8 data[2] = { 0x09, 0x54 };
3133
3134 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3135 cxd2841er_set_ts_clock_mode(priv, SYS_DVBC_ANNEX_A);
3136 /* Set SLV-X Bank : 0x00 */
3137 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
3138 /* Set demod mode */
3139 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x04);
3140 /* Set SLV-T Bank : 0x00 */
3141 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3142 /* Enable demod clock */
3143 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
3144 /* Disable RF level monitor */
Abylay Ospan4a86bc12016-07-19 00:10:20 -03003145 cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003146 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
3147 /* Enable ADC clock */
3148 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
3149 /* Enable ADC 1 */
3150 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
3151 /* xtal freq 20.5MHz */
3152 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
3153 /* Enable ADC 4 */
3154 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
3155 /* Set SLV-T Bank : 0x10 */
3156 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3157 /* IFAGC gain settings */
3158 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x09, 0x1f);
3159 /* Set SLV-T Bank : 0x11 */
3160 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
3161 /* BBAGC TARGET level setting */
3162 cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x48);
3163 /* Set SLV-T Bank : 0x10 */
3164 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3165 /* ASCOT setting ON */
3166 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
3167 /* Set SLV-T Bank : 0x40 */
3168 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
3169 /* Demod setting */
3170 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc3, 0x00, 0x04);
3171 /* Set SLV-T Bank : 0x00 */
3172 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3173 /* TSIF setting */
3174 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
3175 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
3176
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003177 cxd2841er_sleep_tc_to_active_c_band(priv, bandwidth);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003178 /* Set SLV-T Bank : 0x00 */
3179 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3180 /* Disable HiZ Setting 1 */
3181 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3182 /* Disable HiZ Setting 2 */
3183 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3184 priv->state = STATE_ACTIVE_TC;
3185 return 0;
3186}
3187
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02003188static int cxd2841er_get_frontend(struct dvb_frontend *fe,
3189 struct dtv_frontend_properties *p)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003190{
3191 enum fe_status status = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003192 struct cxd2841er_priv *priv = fe->demodulator_priv;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003193
3194 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3195 if (priv->state == STATE_ACTIVE_S)
3196 cxd2841er_read_status_s(fe, &status);
3197 else if (priv->state == STATE_ACTIVE_TC)
3198 cxd2841er_read_status_tc(fe, &status);
3199
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03003200 cxd2841er_read_signal_strength(fe);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003201
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003202 if (status & FE_HAS_LOCK) {
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03003203 cxd2841er_read_snr(fe);
3204 cxd2841er_read_ucblocks(fe);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003205
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03003206 cxd2841er_read_ber(fe);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003207 } else {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003208 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003209 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003210 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03003211 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003212 }
3213 return 0;
3214}
3215
3216static int cxd2841er_set_frontend_s(struct dvb_frontend *fe)
3217{
3218 int ret = 0, i, timeout, carr_offset;
3219 enum fe_status status;
3220 struct cxd2841er_priv *priv = fe->demodulator_priv;
3221 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3222 u32 symbol_rate = p->symbol_rate/1000;
3223
Abylay Ospan83808c22016-03-22 19:20:34 -03003224 dev_dbg(&priv->i2c->dev, "%s(): %s frequency=%d symbol_rate=%d xtal=%d\n",
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003225 __func__,
3226 (p->delivery_system == SYS_DVBS ? "DVB-S" : "DVB-S2"),
Abylay Ospan83808c22016-03-22 19:20:34 -03003227 p->frequency, symbol_rate, priv->xtal);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003228 switch (priv->state) {
3229 case STATE_SLEEP_S:
3230 ret = cxd2841er_sleep_s_to_active_s(
3231 priv, p->delivery_system, symbol_rate);
3232 break;
3233 case STATE_ACTIVE_S:
3234 ret = cxd2841er_retune_active(priv, p);
3235 break;
3236 default:
3237 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3238 __func__, priv->state);
3239 ret = -EINVAL;
3240 goto done;
3241 }
3242 if (ret) {
3243 dev_dbg(&priv->i2c->dev, "%s(): tune failed\n", __func__);
3244 goto done;
3245 }
3246 if (fe->ops.i2c_gate_ctrl)
3247 fe->ops.i2c_gate_ctrl(fe, 1);
3248 if (fe->ops.tuner_ops.set_params)
3249 fe->ops.tuner_ops.set_params(fe);
3250 if (fe->ops.i2c_gate_ctrl)
3251 fe->ops.i2c_gate_ctrl(fe, 0);
3252 cxd2841er_tune_done(priv);
3253 timeout = ((3000000 + (symbol_rate - 1)) / symbol_rate) + 150;
3254 for (i = 0; i < timeout / CXD2841ER_DVBS_POLLING_INVL; i++) {
3255 usleep_range(CXD2841ER_DVBS_POLLING_INVL*1000,
3256 (CXD2841ER_DVBS_POLLING_INVL + 2) * 1000);
3257 cxd2841er_read_status_s(fe, &status);
3258 if (status & FE_HAS_LOCK)
3259 break;
3260 }
3261 if (status & FE_HAS_LOCK) {
3262 if (cxd2841er_get_carrier_offset_s_s2(
3263 priv, &carr_offset)) {
3264 ret = -EINVAL;
3265 goto done;
3266 }
3267 dev_dbg(&priv->i2c->dev, "%s(): carrier_offset=%d\n",
3268 __func__, carr_offset);
3269 }
3270done:
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003271 /* Reset stats */
3272 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
3273 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3274 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3275 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03003276 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003277
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003278 return ret;
3279}
3280
3281static int cxd2841er_set_frontend_tc(struct dvb_frontend *fe)
3282{
3283 int ret = 0, timeout;
3284 enum fe_status status;
3285 struct cxd2841er_priv *priv = fe->demodulator_priv;
3286 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3287
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003288 dev_dbg(&priv->i2c->dev, "%s() delivery_system=%d bandwidth_hz=%d\n",
3289 __func__, p->delivery_system, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003290 if (p->delivery_system == SYS_DVBT) {
3291 priv->system = SYS_DVBT;
3292 switch (priv->state) {
3293 case STATE_SLEEP_TC:
3294 ret = cxd2841er_sleep_tc_to_active_t(
3295 priv, p->bandwidth_hz);
3296 break;
3297 case STATE_ACTIVE_TC:
3298 ret = cxd2841er_retune_active(priv, p);
3299 break;
3300 default:
3301 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3302 __func__, priv->state);
3303 ret = -EINVAL;
3304 }
3305 } else if (p->delivery_system == SYS_DVBT2) {
3306 priv->system = SYS_DVBT2;
3307 cxd2841er_dvbt2_set_plp_config(priv,
3308 (int)(p->stream_id > 255), p->stream_id);
3309 cxd2841er_dvbt2_set_profile(priv, DVBT2_PROFILE_BASE);
3310 switch (priv->state) {
3311 case STATE_SLEEP_TC:
3312 ret = cxd2841er_sleep_tc_to_active_t2(priv,
3313 p->bandwidth_hz);
3314 break;
3315 case STATE_ACTIVE_TC:
3316 ret = cxd2841er_retune_active(priv, p);
3317 break;
3318 default:
3319 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3320 __func__, priv->state);
3321 ret = -EINVAL;
3322 }
Abylay Ospan83808c22016-03-22 19:20:34 -03003323 } else if (p->delivery_system == SYS_ISDBT) {
3324 priv->system = SYS_ISDBT;
3325 switch (priv->state) {
3326 case STATE_SLEEP_TC:
3327 ret = cxd2841er_sleep_tc_to_active_i(
3328 priv, p->bandwidth_hz);
3329 break;
3330 case STATE_ACTIVE_TC:
3331 ret = cxd2841er_retune_active(priv, p);
3332 break;
3333 default:
3334 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3335 __func__, priv->state);
3336 ret = -EINVAL;
3337 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003338 } else if (p->delivery_system == SYS_DVBC_ANNEX_A ||
3339 p->delivery_system == SYS_DVBC_ANNEX_C) {
3340 priv->system = SYS_DVBC_ANNEX_A;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003341 /* correct bandwidth */
3342 if (p->bandwidth_hz != 6000000 &&
3343 p->bandwidth_hz != 7000000 &&
3344 p->bandwidth_hz != 8000000) {
3345 p->bandwidth_hz = 8000000;
3346 dev_dbg(&priv->i2c->dev, "%s(): forcing bandwidth to %d\n",
3347 __func__, p->bandwidth_hz);
3348 }
3349
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003350 switch (priv->state) {
3351 case STATE_SLEEP_TC:
3352 ret = cxd2841er_sleep_tc_to_active_c(
3353 priv, p->bandwidth_hz);
3354 break;
3355 case STATE_ACTIVE_TC:
3356 ret = cxd2841er_retune_active(priv, p);
3357 break;
3358 default:
3359 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3360 __func__, priv->state);
3361 ret = -EINVAL;
3362 }
3363 } else {
3364 dev_dbg(&priv->i2c->dev,
3365 "%s(): invalid delivery system %d\n",
3366 __func__, p->delivery_system);
3367 ret = -EINVAL;
3368 }
3369 if (ret)
3370 goto done;
3371 if (fe->ops.i2c_gate_ctrl)
3372 fe->ops.i2c_gate_ctrl(fe, 1);
3373 if (fe->ops.tuner_ops.set_params)
3374 fe->ops.tuner_ops.set_params(fe);
3375 if (fe->ops.i2c_gate_ctrl)
3376 fe->ops.i2c_gate_ctrl(fe, 0);
3377 cxd2841er_tune_done(priv);
3378 timeout = 2500;
3379 while (timeout > 0) {
3380 ret = cxd2841er_read_status_tc(fe, &status);
3381 if (ret)
3382 goto done;
3383 if (status & FE_HAS_LOCK)
3384 break;
3385 msleep(20);
3386 timeout -= 20;
3387 }
3388 if (timeout < 0)
3389 dev_dbg(&priv->i2c->dev,
3390 "%s(): LOCK wait timeout\n", __func__);
3391done:
3392 return ret;
3393}
3394
3395static int cxd2841er_tune_s(struct dvb_frontend *fe,
3396 bool re_tune,
3397 unsigned int mode_flags,
3398 unsigned int *delay,
3399 enum fe_status *status)
3400{
3401 int ret, carrier_offset;
3402 struct cxd2841er_priv *priv = fe->demodulator_priv;
3403 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3404
3405 dev_dbg(&priv->i2c->dev, "%s() re_tune=%d\n", __func__, re_tune);
3406 if (re_tune) {
3407 ret = cxd2841er_set_frontend_s(fe);
3408 if (ret)
3409 return ret;
3410 cxd2841er_read_status_s(fe, status);
3411 if (*status & FE_HAS_LOCK) {
3412 if (cxd2841er_get_carrier_offset_s_s2(
3413 priv, &carrier_offset))
3414 return -EINVAL;
3415 p->frequency += carrier_offset;
3416 ret = cxd2841er_set_frontend_s(fe);
3417 if (ret)
3418 return ret;
3419 }
3420 }
3421 *delay = HZ / 5;
3422 return cxd2841er_read_status_s(fe, status);
3423}
3424
3425static int cxd2841er_tune_tc(struct dvb_frontend *fe,
3426 bool re_tune,
3427 unsigned int mode_flags,
3428 unsigned int *delay,
3429 enum fe_status *status)
3430{
3431 int ret, carrier_offset;
3432 struct cxd2841er_priv *priv = fe->demodulator_priv;
3433 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3434
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003435 dev_dbg(&priv->i2c->dev, "%s(): re_tune %d bandwidth=%d\n", __func__,
3436 re_tune, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003437 if (re_tune) {
3438 ret = cxd2841er_set_frontend_tc(fe);
3439 if (ret)
3440 return ret;
3441 cxd2841er_read_status_tc(fe, status);
3442 if (*status & FE_HAS_LOCK) {
3443 switch (priv->system) {
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -03003444 case SYS_ISDBT:
3445 ret = cxd2841er_get_carrier_offset_i(
3446 priv, p->bandwidth_hz,
3447 &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003448 if (ret)
3449 return ret;
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -03003450 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003451 case SYS_DVBT:
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03003452 ret = cxd2841er_get_carrier_offset_t(
3453 priv, p->bandwidth_hz,
3454 &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003455 if (ret)
3456 return ret;
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03003457 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003458 case SYS_DVBT2:
3459 ret = cxd2841er_get_carrier_offset_t2(
3460 priv, p->bandwidth_hz,
3461 &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003462 if (ret)
3463 return ret;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003464 break;
3465 case SYS_DVBC_ANNEX_A:
3466 ret = cxd2841er_get_carrier_offset_c(
3467 priv, &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003468 if (ret)
3469 return ret;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003470 break;
3471 default:
3472 dev_dbg(&priv->i2c->dev,
3473 "%s(): invalid delivery system %d\n",
3474 __func__, priv->system);
3475 return -EINVAL;
3476 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003477 dev_dbg(&priv->i2c->dev, "%s(): carrier offset %d\n",
3478 __func__, carrier_offset);
3479 p->frequency += carrier_offset;
3480 ret = cxd2841er_set_frontend_tc(fe);
3481 if (ret)
3482 return ret;
3483 }
3484 }
3485 *delay = HZ / 5;
3486 return cxd2841er_read_status_tc(fe, status);
3487}
3488
3489static int cxd2841er_sleep_s(struct dvb_frontend *fe)
3490{
3491 struct cxd2841er_priv *priv = fe->demodulator_priv;
3492
3493 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3494 cxd2841er_active_s_to_sleep_s(fe->demodulator_priv);
3495 cxd2841er_sleep_s_to_shutdown(fe->demodulator_priv);
3496 return 0;
3497}
3498
3499static int cxd2841er_sleep_tc(struct dvb_frontend *fe)
3500{
3501 struct cxd2841er_priv *priv = fe->demodulator_priv;
3502
3503 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3504 if (priv->state == STATE_ACTIVE_TC) {
3505 switch (priv->system) {
3506 case SYS_DVBT:
3507 cxd2841er_active_t_to_sleep_tc(priv);
3508 break;
3509 case SYS_DVBT2:
3510 cxd2841er_active_t2_to_sleep_tc(priv);
3511 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03003512 case SYS_ISDBT:
3513 cxd2841er_active_i_to_sleep_tc(priv);
3514 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003515 case SYS_DVBC_ANNEX_A:
3516 cxd2841er_active_c_to_sleep_tc(priv);
3517 break;
3518 default:
3519 dev_warn(&priv->i2c->dev,
3520 "%s(): unknown delivery system %d\n",
3521 __func__, priv->system);
3522 }
3523 }
3524 if (priv->state != STATE_SLEEP_TC) {
3525 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
3526 __func__, priv->state);
3527 return -EINVAL;
3528 }
3529 cxd2841er_sleep_tc_to_shutdown(priv);
3530 return 0;
3531}
3532
3533static int cxd2841er_send_burst(struct dvb_frontend *fe,
3534 enum fe_sec_mini_cmd burst)
3535{
3536 u8 data;
3537 struct cxd2841er_priv *priv = fe->demodulator_priv;
3538
3539 dev_dbg(&priv->i2c->dev, "%s(): burst mode %s\n", __func__,
3540 (burst == SEC_MINI_A ? "A" : "B"));
3541 if (priv->state != STATE_SLEEP_S &&
3542 priv->state != STATE_ACTIVE_S) {
3543 dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3544 __func__, priv->state);
3545 return -EINVAL;
3546 }
3547 data = (burst == SEC_MINI_A ? 0 : 1);
3548 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3549 cxd2841er_write_reg(priv, I2C_SLVT, 0x34, 0x01);
3550 cxd2841er_write_reg(priv, I2C_SLVT, 0x35, data);
3551 return 0;
3552}
3553
3554static int cxd2841er_set_tone(struct dvb_frontend *fe,
3555 enum fe_sec_tone_mode tone)
3556{
3557 u8 data;
3558 struct cxd2841er_priv *priv = fe->demodulator_priv;
3559
3560 dev_dbg(&priv->i2c->dev, "%s(): tone %s\n", __func__,
3561 (tone == SEC_TONE_ON ? "On" : "Off"));
3562 if (priv->state != STATE_SLEEP_S &&
3563 priv->state != STATE_ACTIVE_S) {
3564 dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3565 __func__, priv->state);
3566 return -EINVAL;
3567 }
3568 data = (tone == SEC_TONE_ON ? 1 : 0);
3569 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3570 cxd2841er_write_reg(priv, I2C_SLVT, 0x36, data);
3571 return 0;
3572}
3573
3574static int cxd2841er_send_diseqc_msg(struct dvb_frontend *fe,
3575 struct dvb_diseqc_master_cmd *cmd)
3576{
3577 int i;
3578 u8 data[12];
3579 struct cxd2841er_priv *priv = fe->demodulator_priv;
3580
3581 if (priv->state != STATE_SLEEP_S &&
3582 priv->state != STATE_ACTIVE_S) {
3583 dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3584 __func__, priv->state);
3585 return -EINVAL;
3586 }
3587 dev_dbg(&priv->i2c->dev,
3588 "%s(): cmd->len %d\n", __func__, cmd->msg_len);
3589 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3590 /* DiDEqC enable */
3591 cxd2841er_write_reg(priv, I2C_SLVT, 0x33, 0x01);
3592 /* cmd1 length & data */
3593 cxd2841er_write_reg(priv, I2C_SLVT, 0x3d, cmd->msg_len);
3594 memset(data, 0, sizeof(data));
3595 for (i = 0; i < cmd->msg_len && i < sizeof(data); i++)
3596 data[i] = cmd->msg[i];
3597 cxd2841er_write_regs(priv, I2C_SLVT, 0x3e, data, sizeof(data));
3598 /* repeat count for cmd1 */
3599 cxd2841er_write_reg(priv, I2C_SLVT, 0x37, 1);
3600 /* repeat count for cmd2: always 0 */
3601 cxd2841er_write_reg(priv, I2C_SLVT, 0x38, 0);
3602 /* start transmit */
3603 cxd2841er_write_reg(priv, I2C_SLVT, 0x32, 0x01);
3604 /* wait for 1 sec timeout */
3605 for (i = 0; i < 50; i++) {
3606 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, data);
3607 if (!data[0]) {
3608 dev_dbg(&priv->i2c->dev,
3609 "%s(): DiSEqC cmd has been sent\n", __func__);
3610 return 0;
3611 }
3612 msleep(20);
3613 }
3614 dev_dbg(&priv->i2c->dev,
3615 "%s(): DiSEqC cmd transmit timeout\n", __func__);
3616 return -ETIMEDOUT;
3617}
3618
3619static void cxd2841er_release(struct dvb_frontend *fe)
3620{
3621 struct cxd2841er_priv *priv = fe->demodulator_priv;
3622
3623 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3624 kfree(priv);
3625}
3626
3627static int cxd2841er_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
3628{
3629 struct cxd2841er_priv *priv = fe->demodulator_priv;
3630
3631 dev_dbg(&priv->i2c->dev, "%s(): enable=%d\n", __func__, enable);
3632 cxd2841er_set_reg_bits(
3633 priv, I2C_SLVX, 0x8, (enable ? 0x01 : 0x00), 0x01);
3634 return 0;
3635}
3636
3637static enum dvbfe_algo cxd2841er_get_algo(struct dvb_frontend *fe)
3638{
3639 struct cxd2841er_priv *priv = fe->demodulator_priv;
3640
3641 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3642 return DVBFE_ALGO_HW;
3643}
3644
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003645static void cxd2841er_init_stats(struct dvb_frontend *fe)
3646{
3647 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3648
3649 p->strength.len = 1;
3650 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
3651 p->cnr.len = 1;
3652 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3653 p->block_error.len = 1;
3654 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3655 p->post_bit_error.len = 1;
3656 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03003657 p->post_bit_count.len = 1;
3658 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003659}
3660
3661
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003662static int cxd2841er_init_s(struct dvb_frontend *fe)
3663{
3664 struct cxd2841er_priv *priv = fe->demodulator_priv;
3665
Abylay Ospan30ae3302016-04-05 15:02:37 -03003666 /* sanity. force demod to SHUTDOWN state */
3667 if (priv->state == STATE_SLEEP_S) {
3668 dev_dbg(&priv->i2c->dev, "%s() forcing sleep->shutdown\n",
3669 __func__);
3670 cxd2841er_sleep_s_to_shutdown(priv);
3671 } else if (priv->state == STATE_ACTIVE_S) {
3672 dev_dbg(&priv->i2c->dev, "%s() forcing active->sleep->shutdown\n",
3673 __func__);
3674 cxd2841er_active_s_to_sleep_s(priv);
3675 cxd2841er_sleep_s_to_shutdown(priv);
3676 }
3677
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003678 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3679 cxd2841er_shutdown_to_sleep_s(priv);
3680 /* SONY_DEMOD_CONFIG_SAT_IFAGCNEG set to 1 */
3681 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
3682 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xb9, 0x01, 0x01);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003683
3684 cxd2841er_init_stats(fe);
3685
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003686 return 0;
3687}
3688
3689static int cxd2841er_init_tc(struct dvb_frontend *fe)
3690{
3691 struct cxd2841er_priv *priv = fe->demodulator_priv;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003692 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003693
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003694 dev_dbg(&priv->i2c->dev, "%s() bandwidth_hz=%d\n",
3695 __func__, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003696 cxd2841er_shutdown_to_sleep_tc(priv);
3697 /* SONY_DEMOD_CONFIG_IFAGCNEG = 1 */
3698 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3699 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcb, 0x40, 0x40);
3700 /* SONY_DEMOD_CONFIG_IFAGC_ADC_FS = 0 */
3701 cxd2841er_write_reg(priv, I2C_SLVT, 0xcd, 0x50);
3702 /* SONY_DEMOD_CONFIG_PARALLEL_SEL = 1 */
3703 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3704 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4, 0x00, 0x80);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003705
3706 cxd2841er_init_stats(fe);
3707
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003708 return 0;
3709}
3710
Max Kellermannbd336e62016-08-09 18:32:21 -03003711static const struct dvb_frontend_ops cxd2841er_dvbs_s2_ops;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003712static struct dvb_frontend_ops cxd2841er_t_c_ops;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003713
3714static struct dvb_frontend *cxd2841er_attach(struct cxd2841er_config *cfg,
3715 struct i2c_adapter *i2c,
3716 u8 system)
3717{
3718 u8 chip_id = 0;
3719 const char *type;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003720 const char *name;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003721 struct cxd2841er_priv *priv = NULL;
3722
3723 /* allocate memory for the internal state */
3724 priv = kzalloc(sizeof(struct cxd2841er_priv), GFP_KERNEL);
3725 if (!priv)
3726 return NULL;
3727 priv->i2c = i2c;
3728 priv->config = cfg;
3729 priv->i2c_addr_slvx = (cfg->i2c_addr + 4) >> 1;
3730 priv->i2c_addr_slvt = (cfg->i2c_addr) >> 1;
Abylay Ospan83808c22016-03-22 19:20:34 -03003731 priv->xtal = cfg->xtal;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003732 priv->frontend.demodulator_priv = priv;
3733 dev_info(&priv->i2c->dev,
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003734 "%s(): I2C adapter %p SLVX addr %x SLVT addr %x\n",
3735 __func__, priv->i2c,
3736 priv->i2c_addr_slvx, priv->i2c_addr_slvt);
3737 chip_id = cxd2841er_chip_id(priv);
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003738 switch (chip_id) {
3739 case CXD2841ER_CHIP_ID:
3740 snprintf(cxd2841er_t_c_ops.info.name, 128,
3741 "Sony CXD2841ER DVB-T/T2/C demodulator");
3742 name = "CXD2841ER";
3743 break;
3744 case CXD2854ER_CHIP_ID:
3745 snprintf(cxd2841er_t_c_ops.info.name, 128,
3746 "Sony CXD2854ER DVB-T/T2/C and ISDB-T demodulator");
3747 cxd2841er_t_c_ops.delsys[3] = SYS_ISDBT;
3748 name = "CXD2854ER";
3749 break;
3750 default:
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003751 dev_err(&priv->i2c->dev, "%s(): invalid chip ID 0x%02x\n",
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003752 __func__, chip_id);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003753 priv->frontend.demodulator_priv = NULL;
3754 kfree(priv);
3755 return NULL;
3756 }
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003757
3758 /* create dvb_frontend */
3759 if (system == SYS_DVBS) {
3760 memcpy(&priv->frontend.ops,
3761 &cxd2841er_dvbs_s2_ops,
3762 sizeof(struct dvb_frontend_ops));
3763 type = "S/S2";
3764 } else {
3765 memcpy(&priv->frontend.ops,
3766 &cxd2841er_t_c_ops,
3767 sizeof(struct dvb_frontend_ops));
3768 type = "T/T2/C/ISDB-T";
3769 }
3770
3771 dev_info(&priv->i2c->dev,
3772 "%s(): attaching %s DVB-%s frontend\n",
3773 __func__, name, type);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003774 dev_info(&priv->i2c->dev, "%s(): chip ID 0x%02x OK.\n",
3775 __func__, chip_id);
3776 return &priv->frontend;
3777}
3778
3779struct dvb_frontend *cxd2841er_attach_s(struct cxd2841er_config *cfg,
3780 struct i2c_adapter *i2c)
3781{
3782 return cxd2841er_attach(cfg, i2c, SYS_DVBS);
3783}
3784EXPORT_SYMBOL(cxd2841er_attach_s);
3785
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003786struct dvb_frontend *cxd2841er_attach_t_c(struct cxd2841er_config *cfg,
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003787 struct i2c_adapter *i2c)
3788{
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003789 return cxd2841er_attach(cfg, i2c, 0);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003790}
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003791EXPORT_SYMBOL(cxd2841er_attach_t_c);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003792
Max Kellermannbd336e62016-08-09 18:32:21 -03003793static const struct dvb_frontend_ops cxd2841er_dvbs_s2_ops = {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003794 .delsys = { SYS_DVBS, SYS_DVBS2 },
3795 .info = {
3796 .name = "Sony CXD2841ER DVB-S/S2 demodulator",
3797 .frequency_min = 500000,
3798 .frequency_max = 2500000,
3799 .frequency_stepsize = 0,
3800 .symbol_rate_min = 1000000,
3801 .symbol_rate_max = 45000000,
3802 .symbol_rate_tolerance = 500,
3803 .caps = FE_CAN_INVERSION_AUTO |
3804 FE_CAN_FEC_AUTO |
3805 FE_CAN_QPSK,
3806 },
3807 .init = cxd2841er_init_s,
3808 .sleep = cxd2841er_sleep_s,
3809 .release = cxd2841er_release,
3810 .set_frontend = cxd2841er_set_frontend_s,
3811 .get_frontend = cxd2841er_get_frontend,
3812 .read_status = cxd2841er_read_status_s,
3813 .i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
3814 .get_frontend_algo = cxd2841er_get_algo,
3815 .set_tone = cxd2841er_set_tone,
3816 .diseqc_send_burst = cxd2841er_send_burst,
3817 .diseqc_send_master_cmd = cxd2841er_send_diseqc_msg,
3818 .tune = cxd2841er_tune_s
3819};
3820
Max Kellermannbd336e62016-08-09 18:32:21 -03003821static struct dvb_frontend_ops cxd2841er_t_c_ops = {
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003822 .delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A },
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003823 .info = {
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003824 .name = "", /* will set in attach function */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003825 .caps = FE_CAN_FEC_1_2 |
3826 FE_CAN_FEC_2_3 |
3827 FE_CAN_FEC_3_4 |
3828 FE_CAN_FEC_5_6 |
3829 FE_CAN_FEC_7_8 |
3830 FE_CAN_FEC_AUTO |
3831 FE_CAN_QPSK |
3832 FE_CAN_QAM_16 |
3833 FE_CAN_QAM_32 |
3834 FE_CAN_QAM_64 |
3835 FE_CAN_QAM_128 |
3836 FE_CAN_QAM_256 |
3837 FE_CAN_QAM_AUTO |
3838 FE_CAN_TRANSMISSION_MODE_AUTO |
3839 FE_CAN_GUARD_INTERVAL_AUTO |
3840 FE_CAN_HIERARCHY_AUTO |
3841 FE_CAN_MUTE_TS |
3842 FE_CAN_2G_MODULATION,
3843 .frequency_min = 42000000,
Daniel Scheller158f0322017-03-19 12:26:39 -03003844 .frequency_max = 1002000000,
3845 .symbol_rate_min = 870000,
3846 .symbol_rate_max = 11700000
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003847 },
3848 .init = cxd2841er_init_tc,
3849 .sleep = cxd2841er_sleep_tc,
3850 .release = cxd2841er_release,
3851 .set_frontend = cxd2841er_set_frontend_tc,
3852 .get_frontend = cxd2841er_get_frontend,
3853 .read_status = cxd2841er_read_status_tc,
3854 .tune = cxd2841er_tune_tc,
3855 .i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
3856 .get_frontend_algo = cxd2841er_get_algo
3857};
3858
Abylay Ospan83808c22016-03-22 19:20:34 -03003859MODULE_DESCRIPTION("Sony CXD2841ER/CXD2854ER DVB-C/C2/T/T2/S/S2 demodulator driver");
3860MODULE_AUTHOR("Sergey Kozlov <serjk@netup.ru>, Abylay Ospan <aospan@netup.ru>");
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003861MODULE_LICENSE("GPL");