blob: d0035d2bf88d1c8c59c28819626c232c7d07905e [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;
Daniel Scheller050863a2017-04-09 16:38:15 -030068 u32 flags;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -030069};
70
71static const struct cxd2841er_cnr_data s_cn_data[] = {
72 { 0x033e, 0 }, { 0x0339, 100 }, { 0x0333, 200 },
73 { 0x032e, 300 }, { 0x0329, 400 }, { 0x0324, 500 },
74 { 0x031e, 600 }, { 0x0319, 700 }, { 0x0314, 800 },
75 { 0x030f, 900 }, { 0x030a, 1000 }, { 0x02ff, 1100 },
76 { 0x02f4, 1200 }, { 0x02e9, 1300 }, { 0x02de, 1400 },
77 { 0x02d4, 1500 }, { 0x02c9, 1600 }, { 0x02bf, 1700 },
78 { 0x02b5, 1800 }, { 0x02ab, 1900 }, { 0x02a1, 2000 },
79 { 0x029b, 2100 }, { 0x0295, 2200 }, { 0x0290, 2300 },
80 { 0x028a, 2400 }, { 0x0284, 2500 }, { 0x027f, 2600 },
81 { 0x0279, 2700 }, { 0x0274, 2800 }, { 0x026e, 2900 },
82 { 0x0269, 3000 }, { 0x0262, 3100 }, { 0x025c, 3200 },
83 { 0x0255, 3300 }, { 0x024f, 3400 }, { 0x0249, 3500 },
84 { 0x0242, 3600 }, { 0x023c, 3700 }, { 0x0236, 3800 },
85 { 0x0230, 3900 }, { 0x022a, 4000 }, { 0x0223, 4100 },
86 { 0x021c, 4200 }, { 0x0215, 4300 }, { 0x020e, 4400 },
87 { 0x0207, 4500 }, { 0x0201, 4600 }, { 0x01fa, 4700 },
88 { 0x01f4, 4800 }, { 0x01ed, 4900 }, { 0x01e7, 5000 },
89 { 0x01e0, 5100 }, { 0x01d9, 5200 }, { 0x01d2, 5300 },
90 { 0x01cb, 5400 }, { 0x01c4, 5500 }, { 0x01be, 5600 },
91 { 0x01b7, 5700 }, { 0x01b1, 5800 }, { 0x01aa, 5900 },
92 { 0x01a4, 6000 }, { 0x019d, 6100 }, { 0x0196, 6200 },
93 { 0x018f, 6300 }, { 0x0189, 6400 }, { 0x0182, 6500 },
94 { 0x017c, 6600 }, { 0x0175, 6700 }, { 0x016f, 6800 },
95 { 0x0169, 6900 }, { 0x0163, 7000 }, { 0x015c, 7100 },
96 { 0x0156, 7200 }, { 0x0150, 7300 }, { 0x014a, 7400 },
97 { 0x0144, 7500 }, { 0x013e, 7600 }, { 0x0138, 7700 },
98 { 0x0132, 7800 }, { 0x012d, 7900 }, { 0x0127, 8000 },
99 { 0x0121, 8100 }, { 0x011c, 8200 }, { 0x0116, 8300 },
100 { 0x0111, 8400 }, { 0x010b, 8500 }, { 0x0106, 8600 },
101 { 0x0101, 8700 }, { 0x00fc, 8800 }, { 0x00f7, 8900 },
102 { 0x00f2, 9000 }, { 0x00ee, 9100 }, { 0x00ea, 9200 },
103 { 0x00e6, 9300 }, { 0x00e2, 9400 }, { 0x00de, 9500 },
104 { 0x00da, 9600 }, { 0x00d7, 9700 }, { 0x00d3, 9800 },
105 { 0x00d0, 9900 }, { 0x00cc, 10000 }, { 0x00c7, 10100 },
106 { 0x00c3, 10200 }, { 0x00bf, 10300 }, { 0x00ba, 10400 },
107 { 0x00b6, 10500 }, { 0x00b2, 10600 }, { 0x00ae, 10700 },
108 { 0x00aa, 10800 }, { 0x00a7, 10900 }, { 0x00a3, 11000 },
109 { 0x009f, 11100 }, { 0x009c, 11200 }, { 0x0098, 11300 },
110 { 0x0094, 11400 }, { 0x0091, 11500 }, { 0x008e, 11600 },
111 { 0x008a, 11700 }, { 0x0087, 11800 }, { 0x0084, 11900 },
112 { 0x0081, 12000 }, { 0x007e, 12100 }, { 0x007b, 12200 },
113 { 0x0079, 12300 }, { 0x0076, 12400 }, { 0x0073, 12500 },
114 { 0x0071, 12600 }, { 0x006e, 12700 }, { 0x006c, 12800 },
115 { 0x0069, 12900 }, { 0x0067, 13000 }, { 0x0065, 13100 },
116 { 0x0062, 13200 }, { 0x0060, 13300 }, { 0x005e, 13400 },
117 { 0x005c, 13500 }, { 0x005a, 13600 }, { 0x0058, 13700 },
118 { 0x0056, 13800 }, { 0x0054, 13900 }, { 0x0052, 14000 },
119 { 0x0050, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
120 { 0x004b, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
121 { 0x0046, 14700 }, { 0x0044, 14800 }, { 0x0043, 14900 },
122 { 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
123 { 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
124 { 0x0037, 15700 }, { 0x0036, 15800 }, { 0x0034, 15900 },
125 { 0x0033, 16000 }, { 0x0032, 16100 }, { 0x0031, 16200 },
126 { 0x0030, 16300 }, { 0x002f, 16400 }, { 0x002e, 16500 },
127 { 0x002d, 16600 }, { 0x002c, 16700 }, { 0x002b, 16800 },
128 { 0x002a, 16900 }, { 0x0029, 17000 }, { 0x0028, 17100 },
129 { 0x0027, 17200 }, { 0x0026, 17300 }, { 0x0025, 17400 },
130 { 0x0024, 17500 }, { 0x0023, 17600 }, { 0x0022, 17800 },
131 { 0x0021, 17900 }, { 0x0020, 18000 }, { 0x001f, 18200 },
132 { 0x001e, 18300 }, { 0x001d, 18500 }, { 0x001c, 18700 },
133 { 0x001b, 18900 }, { 0x001a, 19000 }, { 0x0019, 19200 },
134 { 0x0018, 19300 }, { 0x0017, 19500 }, { 0x0016, 19700 },
135 { 0x0015, 19900 }, { 0x0014, 20000 },
136};
137
138static const struct cxd2841er_cnr_data s2_cn_data[] = {
139 { 0x05af, 0 }, { 0x0597, 100 }, { 0x057e, 200 },
140 { 0x0567, 300 }, { 0x0550, 400 }, { 0x0539, 500 },
141 { 0x0522, 600 }, { 0x050c, 700 }, { 0x04f6, 800 },
142 { 0x04e1, 900 }, { 0x04cc, 1000 }, { 0x04b6, 1100 },
143 { 0x04a1, 1200 }, { 0x048c, 1300 }, { 0x0477, 1400 },
144 { 0x0463, 1500 }, { 0x044f, 1600 }, { 0x043c, 1700 },
145 { 0x0428, 1800 }, { 0x0416, 1900 }, { 0x0403, 2000 },
146 { 0x03ef, 2100 }, { 0x03dc, 2200 }, { 0x03c9, 2300 },
147 { 0x03b6, 2400 }, { 0x03a4, 2500 }, { 0x0392, 2600 },
148 { 0x0381, 2700 }, { 0x036f, 2800 }, { 0x035f, 2900 },
149 { 0x034e, 3000 }, { 0x033d, 3100 }, { 0x032d, 3200 },
150 { 0x031d, 3300 }, { 0x030d, 3400 }, { 0x02fd, 3500 },
151 { 0x02ee, 3600 }, { 0x02df, 3700 }, { 0x02d0, 3800 },
152 { 0x02c2, 3900 }, { 0x02b4, 4000 }, { 0x02a6, 4100 },
153 { 0x0299, 4200 }, { 0x028c, 4300 }, { 0x027f, 4400 },
154 { 0x0272, 4500 }, { 0x0265, 4600 }, { 0x0259, 4700 },
155 { 0x024d, 4800 }, { 0x0241, 4900 }, { 0x0236, 5000 },
156 { 0x022b, 5100 }, { 0x0220, 5200 }, { 0x0215, 5300 },
157 { 0x020a, 5400 }, { 0x0200, 5500 }, { 0x01f6, 5600 },
158 { 0x01ec, 5700 }, { 0x01e2, 5800 }, { 0x01d8, 5900 },
159 { 0x01cf, 6000 }, { 0x01c6, 6100 }, { 0x01bc, 6200 },
160 { 0x01b3, 6300 }, { 0x01aa, 6400 }, { 0x01a2, 6500 },
161 { 0x0199, 6600 }, { 0x0191, 6700 }, { 0x0189, 6800 },
162 { 0x0181, 6900 }, { 0x0179, 7000 }, { 0x0171, 7100 },
163 { 0x0169, 7200 }, { 0x0161, 7300 }, { 0x015a, 7400 },
164 { 0x0153, 7500 }, { 0x014b, 7600 }, { 0x0144, 7700 },
165 { 0x013d, 7800 }, { 0x0137, 7900 }, { 0x0130, 8000 },
166 { 0x012a, 8100 }, { 0x0124, 8200 }, { 0x011e, 8300 },
167 { 0x0118, 8400 }, { 0x0112, 8500 }, { 0x010c, 8600 },
168 { 0x0107, 8700 }, { 0x0101, 8800 }, { 0x00fc, 8900 },
169 { 0x00f7, 9000 }, { 0x00f2, 9100 }, { 0x00ec, 9200 },
170 { 0x00e7, 9300 }, { 0x00e2, 9400 }, { 0x00dd, 9500 },
171 { 0x00d8, 9600 }, { 0x00d4, 9700 }, { 0x00cf, 9800 },
172 { 0x00ca, 9900 }, { 0x00c6, 10000 }, { 0x00c2, 10100 },
173 { 0x00be, 10200 }, { 0x00b9, 10300 }, { 0x00b5, 10400 },
174 { 0x00b1, 10500 }, { 0x00ae, 10600 }, { 0x00aa, 10700 },
175 { 0x00a6, 10800 }, { 0x00a3, 10900 }, { 0x009f, 11000 },
176 { 0x009b, 11100 }, { 0x0098, 11200 }, { 0x0095, 11300 },
177 { 0x0091, 11400 }, { 0x008e, 11500 }, { 0x008b, 11600 },
178 { 0x0088, 11700 }, { 0x0085, 11800 }, { 0x0082, 11900 },
179 { 0x007f, 12000 }, { 0x007c, 12100 }, { 0x007a, 12200 },
180 { 0x0077, 12300 }, { 0x0074, 12400 }, { 0x0072, 12500 },
181 { 0x006f, 12600 }, { 0x006d, 12700 }, { 0x006b, 12800 },
182 { 0x0068, 12900 }, { 0x0066, 13000 }, { 0x0064, 13100 },
183 { 0x0061, 13200 }, { 0x005f, 13300 }, { 0x005d, 13400 },
184 { 0x005b, 13500 }, { 0x0059, 13600 }, { 0x0057, 13700 },
185 { 0x0055, 13800 }, { 0x0053, 13900 }, { 0x0051, 14000 },
186 { 0x004f, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
187 { 0x004a, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
188 { 0x0045, 14700 }, { 0x0044, 14800 }, { 0x0042, 14900 },
189 { 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
190 { 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
191 { 0x0038, 15600 }, { 0x0037, 15700 }, { 0x0036, 15800 },
192 { 0x0034, 15900 }, { 0x0033, 16000 }, { 0x0032, 16100 },
193 { 0x0031, 16200 }, { 0x0030, 16300 }, { 0x002f, 16400 },
194 { 0x002e, 16500 }, { 0x002d, 16600 }, { 0x002c, 16700 },
195 { 0x002b, 16800 }, { 0x002a, 16900 }, { 0x0029, 17000 },
196 { 0x0028, 17100 }, { 0x0027, 17200 }, { 0x0026, 17300 },
197 { 0x0025, 17400 }, { 0x0024, 17500 }, { 0x0023, 17600 },
198 { 0x0022, 17800 }, { 0x0021, 17900 }, { 0x0020, 18000 },
199 { 0x001f, 18200 }, { 0x001e, 18300 }, { 0x001d, 18500 },
200 { 0x001c, 18700 }, { 0x001b, 18900 }, { 0x001a, 19000 },
201 { 0x0019, 19200 }, { 0x0018, 19300 }, { 0x0017, 19500 },
202 { 0x0016, 19700 }, { 0x0015, 19900 }, { 0x0014, 20000 },
203};
204
Abylay Ospan0854df72016-07-19 12:22:03 -0300205static int cxd2841er_freeze_regs(struct cxd2841er_priv *priv);
206static int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv);
207
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300208static void cxd2841er_i2c_debug(struct cxd2841er_priv *priv,
209 u8 addr, u8 reg, u8 write,
210 const u8 *data, u32 len)
211{
212 dev_dbg(&priv->i2c->dev,
Daniel Scheller5d6d93a2017-04-09 16:38:10 -0300213 "cxd2841er: I2C %s addr %02x reg 0x%02x size %d data %*ph\n",
214 (write == 0 ? "read" : "write"), addr, reg, len, len, data);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300215}
216
217static int cxd2841er_write_regs(struct cxd2841er_priv *priv,
218 u8 addr, u8 reg, const u8 *data, u32 len)
219{
220 int ret;
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300221 u8 buf[MAX_WRITE_REGSIZE + 1];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300222 u8 i2c_addr = (addr == I2C_SLVX ?
223 priv->i2c_addr_slvx : priv->i2c_addr_slvt);
224 struct i2c_msg msg[1] = {
225 {
226 .addr = i2c_addr,
227 .flags = 0,
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300228 .len = len + 1,
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300229 .buf = buf,
230 }
231 };
232
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300233 if (len + 1 >= sizeof(buf)) {
Abylay Ospan83808c22016-03-22 19:20:34 -0300234 dev_warn(&priv->i2c->dev, "wr reg=%04x: len=%d is too big!\n",
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300235 reg, len + 1);
236 return -E2BIG;
237 }
238
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300239 cxd2841er_i2c_debug(priv, i2c_addr, reg, 1, data, len);
240 buf[0] = reg;
241 memcpy(&buf[1], data, len);
242
243 ret = i2c_transfer(priv->i2c, msg, 1);
244 if (ret >= 0 && ret != 1)
245 ret = -EIO;
246 if (ret < 0) {
247 dev_warn(&priv->i2c->dev,
248 "%s: i2c wr failed=%d addr=%02x reg=%02x len=%d\n",
249 KBUILD_MODNAME, ret, i2c_addr, reg, len);
250 return ret;
251 }
252 return 0;
253}
254
255static int cxd2841er_write_reg(struct cxd2841er_priv *priv,
256 u8 addr, u8 reg, u8 val)
257{
258 return cxd2841er_write_regs(priv, addr, reg, &val, 1);
259}
260
261static int cxd2841er_read_regs(struct cxd2841er_priv *priv,
262 u8 addr, u8 reg, u8 *val, u32 len)
263{
264 int ret;
265 u8 i2c_addr = (addr == I2C_SLVX ?
266 priv->i2c_addr_slvx : priv->i2c_addr_slvt);
267 struct i2c_msg msg[2] = {
268 {
269 .addr = i2c_addr,
270 .flags = 0,
271 .len = 1,
272 .buf = &reg,
273 }, {
274 .addr = i2c_addr,
275 .flags = I2C_M_RD,
276 .len = len,
277 .buf = val,
278 }
279 };
280
Daniel Scheller725e93e2017-04-09 16:38:11 -0300281 ret = i2c_transfer(priv->i2c, msg, 2);
282 if (ret >= 0 && ret != 2)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300283 ret = -EIO;
284 if (ret < 0) {
285 dev_warn(&priv->i2c->dev,
286 "%s: i2c rd failed=%d addr=%02x reg=%02x\n",
287 KBUILD_MODNAME, ret, i2c_addr, reg);
288 return ret;
289 }
Abylay Ospan6c771612016-05-16 11:43:25 -0300290 cxd2841er_i2c_debug(priv, i2c_addr, reg, 0, val, len);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300291 return 0;
292}
293
294static int cxd2841er_read_reg(struct cxd2841er_priv *priv,
295 u8 addr, u8 reg, u8 *val)
296{
297 return cxd2841er_read_regs(priv, addr, reg, val, 1);
298}
299
300static int cxd2841er_set_reg_bits(struct cxd2841er_priv *priv,
301 u8 addr, u8 reg, u8 data, u8 mask)
302{
303 int res;
304 u8 rdata;
305
306 if (mask != 0xff) {
307 res = cxd2841er_read_reg(priv, addr, reg, &rdata);
308 if (res)
309 return res;
310 data = ((data & mask) | (rdata & (mask ^ 0xFF)));
311 }
312 return cxd2841er_write_reg(priv, addr, reg, data);
313}
314
Daniel Schellercbc85a42017-04-09 16:38:14 -0300315static u32 cxd2841er_calc_iffreq_xtal(enum cxd2841er_xtal xtal, u32 ifhz)
316{
317 u64 tmp;
318
319 tmp = (u64) ifhz * 16777216;
320 do_div(tmp, ((xtal == SONY_XTAL_24000) ? 48000000 : 41000000));
321
322 return (u32) tmp;
323}
324
325static u32 cxd2841er_calc_iffreq(u32 ifhz)
326{
327 return cxd2841er_calc_iffreq_xtal(SONY_XTAL_20500, ifhz);
328}
329
Daniel Schellerc7518d12017-04-09 16:38:16 -0300330static int cxd2841er_tuner_set(struct dvb_frontend *fe)
331{
332 struct cxd2841er_priv *priv = fe->demodulator_priv;
333
334 if ((priv->flags & CXD2841ER_USE_GATECTRL) && fe->ops.i2c_gate_ctrl)
335 fe->ops.i2c_gate_ctrl(fe, 1);
336 if (fe->ops.tuner_ops.set_params)
337 fe->ops.tuner_ops.set_params(fe);
338 if ((priv->flags & CXD2841ER_USE_GATECTRL) && fe->ops.i2c_gate_ctrl)
339 fe->ops.i2c_gate_ctrl(fe, 0);
340
341 return 0;
342}
343
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300344static int cxd2841er_dvbs2_set_symbol_rate(struct cxd2841er_priv *priv,
345 u32 symbol_rate)
346{
347 u32 reg_value = 0;
348 u8 data[3] = {0, 0, 0};
349
350 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
351 /*
352 * regValue = (symbolRateKSps * 2^14 / 1000) + 0.5
353 * = ((symbolRateKSps * 2^14) + 500) / 1000
354 * = ((symbolRateKSps * 16384) + 500) / 1000
355 */
356 reg_value = DIV_ROUND_CLOSEST(symbol_rate * 16384, 1000);
357 if ((reg_value == 0) || (reg_value > 0xFFFFF)) {
358 dev_err(&priv->i2c->dev,
359 "%s(): reg_value is out of range\n", __func__);
360 return -EINVAL;
361 }
362 data[0] = (u8)((reg_value >> 16) & 0x0F);
363 data[1] = (u8)((reg_value >> 8) & 0xFF);
364 data[2] = (u8)(reg_value & 0xFF);
365 /* Set SLV-T Bank : 0xAE */
366 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
367 cxd2841er_write_regs(priv, I2C_SLVT, 0x20, data, 3);
368 return 0;
369}
370
371static void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
372 u8 system);
373
374static int cxd2841er_sleep_s_to_active_s(struct cxd2841er_priv *priv,
375 u8 system, u32 symbol_rate)
376{
377 int ret;
378 u8 data[4] = { 0, 0, 0, 0 };
379
380 if (priv->state != STATE_SLEEP_S) {
381 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
382 __func__, (int)priv->state);
383 return -EINVAL;
384 }
385 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
386 cxd2841er_set_ts_clock_mode(priv, SYS_DVBS);
387 /* Set demod mode */
388 if (system == SYS_DVBS) {
389 data[0] = 0x0A;
390 } else if (system == SYS_DVBS2) {
391 data[0] = 0x0B;
392 } else {
393 dev_err(&priv->i2c->dev, "%s(): invalid delsys %d\n",
394 __func__, system);
395 return -EINVAL;
396 }
397 /* Set SLV-X Bank : 0x00 */
398 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
399 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, data[0]);
400 /* DVB-S/S2 */
401 data[0] = 0x00;
402 /* Set SLV-T Bank : 0x00 */
403 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
404 /* Enable S/S2 auto detection 1 */
405 cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, data[0]);
406 /* Set SLV-T Bank : 0xAE */
407 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
408 /* Enable S/S2 auto detection 2 */
409 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, data[0]);
410 /* Set SLV-T Bank : 0x00 */
411 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
412 /* Enable demod clock */
413 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
414 /* Enable ADC clock */
415 cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x01);
416 /* Enable ADC 1 */
417 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
418 /* Enable ADC 2 */
419 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x3f);
420 /* Set SLV-X Bank : 0x00 */
421 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
422 /* Enable ADC 3 */
423 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
424 /* Set SLV-T Bank : 0xA3 */
425 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa3);
426 cxd2841er_write_reg(priv, I2C_SLVT, 0xac, 0x00);
427 data[0] = 0x07;
428 data[1] = 0x3B;
429 data[2] = 0x08;
430 data[3] = 0xC5;
431 /* Set SLV-T Bank : 0xAB */
432 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xab);
433 cxd2841er_write_regs(priv, I2C_SLVT, 0x98, data, 4);
434 data[0] = 0x05;
435 data[1] = 0x80;
436 data[2] = 0x0A;
437 data[3] = 0x80;
438 cxd2841er_write_regs(priv, I2C_SLVT, 0xa8, data, 4);
439 data[0] = 0x0C;
440 data[1] = 0xCC;
441 cxd2841er_write_regs(priv, I2C_SLVT, 0xc3, data, 2);
442 /* Set demod parameter */
443 ret = cxd2841er_dvbs2_set_symbol_rate(priv, symbol_rate);
444 if (ret != 0)
445 return ret;
446 /* Set SLV-T Bank : 0x00 */
447 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
448 /* disable Hi-Z setting 1 */
449 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x10);
450 /* disable Hi-Z setting 2 */
451 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
452 priv->state = STATE_ACTIVE_S;
453 return 0;
454}
455
456static int cxd2841er_sleep_tc_to_active_t_band(struct cxd2841er_priv *priv,
457 u32 bandwidth);
458
459static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
460 u32 bandwidth);
461
462static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
463 u32 bandwidth);
464
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -0300465static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
466 u32 bandwidth);
467
468static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv);
469
470static int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv);
471
472static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv);
473
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300474static int cxd2841er_retune_active(struct cxd2841er_priv *priv,
475 struct dtv_frontend_properties *p)
476{
477 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
478 if (priv->state != STATE_ACTIVE_S &&
479 priv->state != STATE_ACTIVE_TC) {
480 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
481 __func__, priv->state);
482 return -EINVAL;
483 }
484 /* Set SLV-T Bank : 0x00 */
485 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
486 /* disable TS output */
487 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
488 if (priv->state == STATE_ACTIVE_S)
489 return cxd2841er_dvbs2_set_symbol_rate(
490 priv, p->symbol_rate / 1000);
491 else if (priv->state == STATE_ACTIVE_TC) {
492 switch (priv->system) {
493 case SYS_DVBT:
494 return cxd2841er_sleep_tc_to_active_t_band(
495 priv, p->bandwidth_hz);
496 case SYS_DVBT2:
497 return cxd2841er_sleep_tc_to_active_t2_band(
498 priv, p->bandwidth_hz);
499 case SYS_DVBC_ANNEX_A:
500 return cxd2841er_sleep_tc_to_active_c_band(
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -0300501 priv, p->bandwidth_hz);
502 case SYS_ISDBT:
503 cxd2841er_active_i_to_sleep_tc(priv);
504 cxd2841er_sleep_tc_to_shutdown(priv);
505 cxd2841er_shutdown_to_sleep_tc(priv);
506 return cxd2841er_sleep_tc_to_active_i(
507 priv, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300508 }
509 }
510 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
511 __func__, priv->system);
512 return -EINVAL;
513}
514
515static int cxd2841er_active_s_to_sleep_s(struct cxd2841er_priv *priv)
516{
517 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
518 if (priv->state != STATE_ACTIVE_S) {
519 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
520 __func__, priv->state);
521 return -EINVAL;
522 }
523 /* Set SLV-T Bank : 0x00 */
524 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
525 /* disable TS output */
526 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
527 /* enable Hi-Z setting 1 */
528 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1f);
529 /* enable Hi-Z setting 2 */
530 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
531 /* Set SLV-X Bank : 0x00 */
532 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
533 /* disable ADC 1 */
534 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
535 /* Set SLV-T Bank : 0x00 */
536 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
537 /* disable ADC clock */
538 cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x00);
539 /* disable ADC 2 */
540 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
541 /* disable ADC 3 */
542 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
543 /* SADC Bias ON */
544 cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
545 /* disable demod clock */
546 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
547 /* Set SLV-T Bank : 0xAE */
548 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
549 /* disable S/S2 auto detection1 */
550 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
551 /* Set SLV-T Bank : 0x00 */
552 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
553 /* disable S/S2 auto detection2 */
554 cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, 0x00);
555 priv->state = STATE_SLEEP_S;
556 return 0;
557}
558
559static int cxd2841er_sleep_s_to_shutdown(struct cxd2841er_priv *priv)
560{
561 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
562 if (priv->state != STATE_SLEEP_S) {
563 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
564 __func__, priv->state);
565 return -EINVAL;
566 }
567 /* Set SLV-T Bank : 0x00 */
568 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
569 /* Disable DSQOUT */
570 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
571 /* Disable DSQIN */
572 cxd2841er_write_reg(priv, I2C_SLVT, 0x9c, 0x00);
573 /* Set SLV-X Bank : 0x00 */
574 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
575 /* Disable oscillator */
576 cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
577 /* Set demod mode */
578 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
579 priv->state = STATE_SHUTDOWN;
580 return 0;
581}
582
583static int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv)
584{
585 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
586 if (priv->state != STATE_SLEEP_TC) {
587 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
588 __func__, priv->state);
589 return -EINVAL;
590 }
591 /* Set SLV-X Bank : 0x00 */
592 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
593 /* Disable oscillator */
594 cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
595 /* Set demod mode */
596 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
597 priv->state = STATE_SHUTDOWN;
598 return 0;
599}
600
601static int cxd2841er_active_t_to_sleep_tc(struct cxd2841er_priv *priv)
602{
603 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
604 if (priv->state != STATE_ACTIVE_TC) {
605 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
606 __func__, priv->state);
607 return -EINVAL;
608 }
609 /* Set SLV-T Bank : 0x00 */
610 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
611 /* disable TS output */
612 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
613 /* enable Hi-Z setting 1 */
614 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
615 /* enable Hi-Z setting 2 */
616 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
617 /* Set SLV-X Bank : 0x00 */
618 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
619 /* disable ADC 1 */
620 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
621 /* Set SLV-T Bank : 0x00 */
622 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
623 /* Disable ADC 2 */
624 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
625 /* Disable ADC 3 */
626 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
627 /* Disable ADC clock */
628 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
629 /* Disable RF level monitor */
630 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
631 /* Disable demod clock */
632 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
633 priv->state = STATE_SLEEP_TC;
634 return 0;
635}
636
637static int cxd2841er_active_t2_to_sleep_tc(struct cxd2841er_priv *priv)
638{
639 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
640 if (priv->state != STATE_ACTIVE_TC) {
641 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
642 __func__, priv->state);
643 return -EINVAL;
644 }
645 /* Set SLV-T Bank : 0x00 */
646 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
647 /* disable TS output */
648 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
649 /* enable Hi-Z setting 1 */
650 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
651 /* enable Hi-Z setting 2 */
652 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
653 /* Cancel DVB-T2 setting */
654 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
655 cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x40);
656 cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x21);
657 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
658 cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xfb);
659 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
660 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x00, 0x0f);
661 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
662 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x00, 0x3f);
663 /* Set SLV-X Bank : 0x00 */
664 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
665 /* disable ADC 1 */
666 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
667 /* Set SLV-T Bank : 0x00 */
668 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
669 /* Disable ADC 2 */
670 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
671 /* Disable ADC 3 */
672 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
673 /* Disable ADC clock */
674 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
675 /* Disable RF level monitor */
676 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
677 /* Disable demod clock */
678 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
679 priv->state = STATE_SLEEP_TC;
680 return 0;
681}
682
683static int cxd2841er_active_c_to_sleep_tc(struct cxd2841er_priv *priv)
684{
685 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
686 if (priv->state != STATE_ACTIVE_TC) {
687 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
688 __func__, priv->state);
689 return -EINVAL;
690 }
691 /* Set SLV-T Bank : 0x00 */
692 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
693 /* disable TS output */
694 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
695 /* enable Hi-Z setting 1 */
696 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
697 /* enable Hi-Z setting 2 */
698 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
699 /* Cancel DVB-C setting */
700 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
701 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
702 /* Set SLV-X Bank : 0x00 */
703 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
704 /* disable ADC 1 */
705 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
706 /* Set SLV-T Bank : 0x00 */
707 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
708 /* Disable ADC 2 */
709 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
710 /* Disable ADC 3 */
711 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
712 /* Disable ADC clock */
713 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
714 /* Disable RF level monitor */
715 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
716 /* Disable demod clock */
717 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
718 priv->state = STATE_SLEEP_TC;
719 return 0;
720}
721
Abylay Ospan83808c22016-03-22 19:20:34 -0300722static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv)
723{
724 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
725 if (priv->state != STATE_ACTIVE_TC) {
726 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
727 __func__, priv->state);
728 return -EINVAL;
729 }
730 /* Set SLV-T Bank : 0x00 */
731 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
732 /* disable TS output */
733 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
734 /* enable Hi-Z setting 1 */
735 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
736 /* enable Hi-Z setting 2 */
737 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
738
739 /* TODO: Cancel demod parameter */
740
741 /* Set SLV-X Bank : 0x00 */
742 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
743 /* disable ADC 1 */
744 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
745 /* Set SLV-T Bank : 0x00 */
746 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
747 /* Disable ADC 2 */
748 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
749 /* Disable ADC 3 */
750 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
751 /* Disable ADC clock */
752 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
753 /* Disable RF level monitor */
754 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
755 /* Disable demod clock */
756 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
757 priv->state = STATE_SLEEP_TC;
758 return 0;
759}
760
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300761static int cxd2841er_shutdown_to_sleep_s(struct cxd2841er_priv *priv)
762{
763 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
764 if (priv->state != STATE_SHUTDOWN) {
765 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
766 __func__, priv->state);
767 return -EINVAL;
768 }
769 /* Set SLV-X Bank : 0x00 */
770 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
771 /* Clear all demodulator registers */
772 cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
773 usleep_range(3000, 5000);
774 /* Set SLV-X Bank : 0x00 */
775 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
776 /* Set demod SW reset */
777 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
Abylay Ospan83808c22016-03-22 19:20:34 -0300778
779 switch (priv->xtal) {
780 case SONY_XTAL_20500:
781 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x00);
782 break;
783 case SONY_XTAL_24000:
784 /* Select demod frequency */
785 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
786 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x03);
787 break;
788 case SONY_XTAL_41000:
789 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x01);
790 break;
791 default:
792 dev_dbg(&priv->i2c->dev, "%s(): invalid demod xtal %d\n",
793 __func__, priv->xtal);
794 return -EINVAL;
795 }
796
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300797 /* Set demod mode */
798 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x0a);
799 /* Clear demod SW reset */
800 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
801 usleep_range(1000, 2000);
802 /* Set SLV-T Bank : 0x00 */
803 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
804 /* enable DSQOUT */
805 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1F);
806 /* enable DSQIN */
807 cxd2841er_write_reg(priv, I2C_SLVT, 0x9C, 0x40);
808 /* TADC Bias On */
809 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
810 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
811 /* SADC Bias On */
812 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
813 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
814 cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
815 priv->state = STATE_SLEEP_S;
816 return 0;
817}
818
819static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv)
820{
Abylay Ospan6c771612016-05-16 11:43:25 -0300821 u8 data = 0;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -0300822
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300823 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
824 if (priv->state != STATE_SHUTDOWN) {
825 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
826 __func__, priv->state);
827 return -EINVAL;
828 }
829 /* Set SLV-X Bank : 0x00 */
830 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
831 /* Clear all demodulator registers */
832 cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
833 usleep_range(3000, 5000);
834 /* Set SLV-X Bank : 0x00 */
835 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
836 /* Set demod SW reset */
837 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
Abylay Ospan6c771612016-05-16 11:43:25 -0300838 /* Select ADC clock mode */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300839 cxd2841er_write_reg(priv, I2C_SLVX, 0x13, 0x00);
Abylay Ospan6c771612016-05-16 11:43:25 -0300840
841 switch (priv->xtal) {
842 case SONY_XTAL_20500:
843 data = 0x0;
844 break;
845 case SONY_XTAL_24000:
846 /* Select demod frequency */
847 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
848 data = 0x3;
849 break;
850 case SONY_XTAL_41000:
851 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
852 data = 0x1;
853 break;
854 }
855 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, data);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300856 /* Clear demod SW reset */
857 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
858 usleep_range(1000, 2000);
859 /* Set SLV-T Bank : 0x00 */
860 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
861 /* TADC Bias On */
862 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
863 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
864 /* SADC Bias On */
865 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
866 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
867 cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
868 priv->state = STATE_SLEEP_TC;
869 return 0;
870}
871
872static int cxd2841er_tune_done(struct cxd2841er_priv *priv)
873{
874 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
875 /* Set SLV-T Bank : 0x00 */
876 cxd2841er_write_reg(priv, I2C_SLVT, 0, 0);
877 /* SW Reset */
878 cxd2841er_write_reg(priv, I2C_SLVT, 0xfe, 0x01);
879 /* Enable TS output */
880 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x00);
881 return 0;
882}
883
884/* Set TS parallel mode */
885static void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
886 u8 system)
887{
888 u8 serial_ts, ts_rate_ctrl_off, ts_in_off;
889
890 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
891 /* Set SLV-T Bank : 0x00 */
892 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
893 cxd2841er_read_reg(priv, I2C_SLVT, 0xc4, &serial_ts);
894 cxd2841er_read_reg(priv, I2C_SLVT, 0xd3, &ts_rate_ctrl_off);
895 cxd2841er_read_reg(priv, I2C_SLVT, 0xde, &ts_in_off);
896 dev_dbg(&priv->i2c->dev, "%s(): ser_ts=0x%02x rate_ctrl_off=0x%02x in_off=0x%02x\n",
897 __func__, serial_ts, ts_rate_ctrl_off, ts_in_off);
898
899 /*
900 * slave Bank Addr Bit default Name
901 * <SLV-T> 00h D9h [7:0] 8'h08 OTSCKPERIOD
902 */
903 cxd2841er_write_reg(priv, I2C_SLVT, 0xd9, 0x08);
904 /*
905 * Disable TS IF Clock
906 * slave Bank Addr Bit default Name
907 * <SLV-T> 00h 32h [0] 1'b1 OREG_CK_TSIF_EN
908 */
909 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x00, 0x01);
910 /*
911 * slave Bank Addr Bit default Name
912 * <SLV-T> 00h 33h [1:0] 2'b01 OREG_CKSEL_TSIF
913 */
914 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x33, 0x00, 0x03);
915 /*
916 * Enable TS IF Clock
917 * slave Bank Addr Bit default Name
918 * <SLV-T> 00h 32h [0] 1'b1 OREG_CK_TSIF_EN
919 */
920 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x01, 0x01);
921
922 if (system == SYS_DVBT) {
923 /* Enable parity period for DVB-T */
924 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
925 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
926 } else if (system == SYS_DVBC_ANNEX_A) {
927 /* Enable parity period for DVB-C */
928 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
929 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
930 }
931}
932
933static u8 cxd2841er_chip_id(struct cxd2841er_priv *priv)
934{
Abylay Ospan83808c22016-03-22 19:20:34 -0300935 u8 chip_id = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300936
937 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
Abylay Ospan83808c22016-03-22 19:20:34 -0300938 if (cxd2841er_write_reg(priv, I2C_SLVT, 0, 0) == 0)
939 cxd2841er_read_reg(priv, I2C_SLVT, 0xfd, &chip_id);
940 else if (cxd2841er_write_reg(priv, I2C_SLVX, 0, 0) == 0)
941 cxd2841er_read_reg(priv, I2C_SLVX, 0xfd, &chip_id);
942
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300943 return chip_id;
944}
945
946static int cxd2841er_read_status_s(struct dvb_frontend *fe,
947 enum fe_status *status)
948{
949 u8 reg = 0;
950 struct cxd2841er_priv *priv = fe->demodulator_priv;
951
952 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
953 *status = 0;
954 if (priv->state != STATE_ACTIVE_S) {
955 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
956 __func__, priv->state);
957 return -EINVAL;
958 }
959 /* Set SLV-T Bank : 0xA0 */
960 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
961 /*
962 * slave Bank Addr Bit Signal name
963 * <SLV-T> A0h 11h [2] ITSLOCK
964 */
965 cxd2841er_read_reg(priv, I2C_SLVT, 0x11, &reg);
966 if (reg & 0x04) {
967 *status = FE_HAS_SIGNAL
968 | FE_HAS_CARRIER
969 | FE_HAS_VITERBI
970 | FE_HAS_SYNC
971 | FE_HAS_LOCK;
972 }
973 dev_dbg(&priv->i2c->dev, "%s(): result 0x%x\n", __func__, *status);
974 return 0;
975}
976
977static int cxd2841er_read_status_t_t2(struct cxd2841er_priv *priv,
978 u8 *sync, u8 *tslock, u8 *unlock)
979{
980 u8 data = 0;
981
982 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
983 if (priv->state != STATE_ACTIVE_TC)
984 return -EINVAL;
985 if (priv->system == SYS_DVBT) {
986 /* Set SLV-T Bank : 0x10 */
987 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
988 } else {
989 /* Set SLV-T Bank : 0x20 */
990 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
991 }
992 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
993 if ((data & 0x07) == 0x07) {
994 dev_dbg(&priv->i2c->dev,
995 "%s(): invalid hardware state detected\n", __func__);
996 *sync = 0;
997 *tslock = 0;
998 *unlock = 0;
999 } else {
1000 *sync = ((data & 0x07) == 0x6 ? 1 : 0);
1001 *tslock = ((data & 0x20) ? 1 : 0);
1002 *unlock = ((data & 0x10) ? 1 : 0);
1003 }
1004 return 0;
1005}
1006
1007static int cxd2841er_read_status_c(struct cxd2841er_priv *priv, u8 *tslock)
1008{
1009 u8 data;
1010
1011 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1012 if (priv->state != STATE_ACTIVE_TC)
1013 return -EINVAL;
1014 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1015 cxd2841er_read_reg(priv, I2C_SLVT, 0x88, &data);
1016 if ((data & 0x01) == 0) {
1017 *tslock = 0;
1018 } else {
1019 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
1020 *tslock = ((data & 0x20) ? 1 : 0);
1021 }
1022 return 0;
1023}
1024
Abylay Ospan83808c22016-03-22 19:20:34 -03001025static int cxd2841er_read_status_i(struct cxd2841er_priv *priv,
1026 u8 *sync, u8 *tslock, u8 *unlock)
1027{
1028 u8 data = 0;
1029
1030 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1031 if (priv->state != STATE_ACTIVE_TC)
1032 return -EINVAL;
1033 /* Set SLV-T Bank : 0x60 */
1034 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1035 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
1036 dev_dbg(&priv->i2c->dev,
1037 "%s(): lock=0x%x\n", __func__, data);
1038 *sync = ((data & 0x02) ? 1 : 0);
1039 *tslock = ((data & 0x01) ? 1 : 0);
1040 *unlock = ((data & 0x10) ? 1 : 0);
1041 return 0;
1042}
1043
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001044static int cxd2841er_read_status_tc(struct dvb_frontend *fe,
1045 enum fe_status *status)
1046{
1047 int ret = 0;
1048 u8 sync = 0;
1049 u8 tslock = 0;
1050 u8 unlock = 0;
1051 struct cxd2841er_priv *priv = fe->demodulator_priv;
1052
1053 *status = 0;
1054 if (priv->state == STATE_ACTIVE_TC) {
1055 if (priv->system == SYS_DVBT || priv->system == SYS_DVBT2) {
1056 ret = cxd2841er_read_status_t_t2(
1057 priv, &sync, &tslock, &unlock);
1058 if (ret)
1059 goto done;
1060 if (unlock)
1061 goto done;
1062 if (sync)
1063 *status = FE_HAS_SIGNAL |
1064 FE_HAS_CARRIER |
1065 FE_HAS_VITERBI |
1066 FE_HAS_SYNC;
1067 if (tslock)
1068 *status |= FE_HAS_LOCK;
Abylay Ospan83808c22016-03-22 19:20:34 -03001069 } else if (priv->system == SYS_ISDBT) {
1070 ret = cxd2841er_read_status_i(
1071 priv, &sync, &tslock, &unlock);
1072 if (ret)
1073 goto done;
1074 if (unlock)
1075 goto done;
1076 if (sync)
1077 *status = FE_HAS_SIGNAL |
1078 FE_HAS_CARRIER |
1079 FE_HAS_VITERBI |
1080 FE_HAS_SYNC;
1081 if (tslock)
1082 *status |= FE_HAS_LOCK;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001083 } else if (priv->system == SYS_DVBC_ANNEX_A) {
1084 ret = cxd2841er_read_status_c(priv, &tslock);
1085 if (ret)
1086 goto done;
1087 if (tslock)
1088 *status = FE_HAS_SIGNAL |
1089 FE_HAS_CARRIER |
1090 FE_HAS_VITERBI |
1091 FE_HAS_SYNC |
1092 FE_HAS_LOCK;
1093 }
1094 }
1095done:
1096 dev_dbg(&priv->i2c->dev, "%s(): status 0x%x\n", __func__, *status);
1097 return ret;
1098}
1099
1100static int cxd2841er_get_carrier_offset_s_s2(struct cxd2841er_priv *priv,
1101 int *offset)
1102{
1103 u8 data[3];
1104 u8 is_hs_mode;
1105 s32 cfrl_ctrlval;
1106 s32 temp_div, temp_q, temp_r;
1107
1108 if (priv->state != STATE_ACTIVE_S) {
1109 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1110 __func__, priv->state);
1111 return -EINVAL;
1112 }
1113 /*
1114 * Get High Sampling Rate mode
1115 * slave Bank Addr Bit Signal name
1116 * <SLV-T> A0h 10h [0] ITRL_LOCK
1117 */
1118 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1119 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data[0]);
1120 if (data[0] & 0x01) {
1121 /*
1122 * slave Bank Addr Bit Signal name
1123 * <SLV-T> A0h 50h [4] IHSMODE
1124 */
1125 cxd2841er_read_reg(priv, I2C_SLVT, 0x50, &data[0]);
1126 is_hs_mode = (data[0] & 0x10 ? 1 : 0);
1127 } else {
1128 dev_dbg(&priv->i2c->dev,
1129 "%s(): unable to detect sampling rate mode\n",
1130 __func__);
1131 return -EINVAL;
1132 }
1133 /*
1134 * slave Bank Addr Bit Signal name
1135 * <SLV-T> A0h 45h [4:0] ICFRL_CTRLVAL[20:16]
1136 * <SLV-T> A0h 46h [7:0] ICFRL_CTRLVAL[15:8]
1137 * <SLV-T> A0h 47h [7:0] ICFRL_CTRLVAL[7:0]
1138 */
1139 cxd2841er_read_regs(priv, I2C_SLVT, 0x45, data, 3);
1140 cfrl_ctrlval = sign_extend32((((u32)data[0] & 0x1F) << 16) |
1141 (((u32)data[1] & 0xFF) << 8) |
1142 ((u32)data[2] & 0xFF), 20);
1143 temp_div = (is_hs_mode ? 1048576 : 1572864);
1144 if (cfrl_ctrlval > 0) {
1145 temp_q = div_s64_rem(97375LL * cfrl_ctrlval,
1146 temp_div, &temp_r);
1147 } else {
1148 temp_q = div_s64_rem(-97375LL * cfrl_ctrlval,
1149 temp_div, &temp_r);
1150 }
1151 if (temp_r >= temp_div / 2)
1152 temp_q++;
1153 if (cfrl_ctrlval > 0)
1154 temp_q *= -1;
1155 *offset = temp_q;
1156 return 0;
1157}
1158
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -03001159static int cxd2841er_get_carrier_offset_i(struct cxd2841er_priv *priv,
1160 u32 bandwidth, int *offset)
1161{
1162 u8 data[4];
1163
1164 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1165 if (priv->state != STATE_ACTIVE_TC) {
1166 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1167 __func__, priv->state);
1168 return -EINVAL;
1169 }
1170 if (priv->system != SYS_ISDBT) {
1171 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1172 __func__, priv->system);
1173 return -EINVAL;
1174 }
1175 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1176 cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1177 *offset = -1 * sign_extend32(
1178 ((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
1179 ((u32)data[2] << 8) | (u32)data[3], 29);
1180
1181 switch (bandwidth) {
1182 case 6000000:
1183 *offset = -1 * ((*offset) * 8/264);
1184 break;
1185 case 7000000:
1186 *offset = -1 * ((*offset) * 8/231);
1187 break;
1188 case 8000000:
1189 *offset = -1 * ((*offset) * 8/198);
1190 break;
1191 default:
1192 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
1193 __func__, bandwidth);
1194 return -EINVAL;
1195 }
1196
1197 dev_dbg(&priv->i2c->dev, "%s(): bandwidth %d offset %d\n",
1198 __func__, bandwidth, *offset);
1199
1200 return 0;
1201}
1202
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03001203static int cxd2841er_get_carrier_offset_t(struct cxd2841er_priv *priv,
1204 u32 bandwidth, int *offset)
1205{
1206 u8 data[4];
1207
1208 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1209 if (priv->state != STATE_ACTIVE_TC) {
1210 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1211 __func__, priv->state);
1212 return -EINVAL;
1213 }
1214 if (priv->system != SYS_DVBT) {
1215 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1216 __func__, priv->system);
1217 return -EINVAL;
1218 }
1219 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1220 cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1221 *offset = -1 * sign_extend32(
1222 ((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
1223 ((u32)data[2] << 8) | (u32)data[3], 29);
Abylay Ospan6c771612016-05-16 11:43:25 -03001224 *offset *= (bandwidth / 1000000);
1225 *offset /= 235;
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03001226 return 0;
1227}
1228
Mauro Carvalho Chehabc8946c82015-08-11 15:08:47 -03001229static int cxd2841er_get_carrier_offset_t2(struct cxd2841er_priv *priv,
1230 u32 bandwidth, int *offset)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001231{
1232 u8 data[4];
1233
1234 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1235 if (priv->state != STATE_ACTIVE_TC) {
1236 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1237 __func__, priv->state);
1238 return -EINVAL;
1239 }
1240 if (priv->system != SYS_DVBT2) {
1241 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1242 __func__, priv->system);
1243 return -EINVAL;
1244 }
1245 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1246 cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1247 *offset = -1 * sign_extend32(
1248 ((u32)(data[0] & 0x0F) << 24) | ((u32)data[1] << 16) |
1249 ((u32)data[2] << 8) | (u32)data[3], 27);
1250 switch (bandwidth) {
1251 case 1712000:
1252 *offset /= 582;
1253 break;
1254 case 5000000:
1255 case 6000000:
1256 case 7000000:
1257 case 8000000:
1258 *offset *= (bandwidth / 1000000);
1259 *offset /= 940;
1260 break;
1261 default:
1262 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
1263 __func__, bandwidth);
1264 return -EINVAL;
1265 }
1266 return 0;
1267}
1268
Mauro Carvalho Chehabc8946c82015-08-11 15:08:47 -03001269static int cxd2841er_get_carrier_offset_c(struct cxd2841er_priv *priv,
1270 int *offset)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001271{
1272 u8 data[2];
1273
1274 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1275 if (priv->state != STATE_ACTIVE_TC) {
1276 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1277 __func__, priv->state);
1278 return -EINVAL;
1279 }
1280 if (priv->system != SYS_DVBC_ANNEX_A) {
1281 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1282 __func__, priv->system);
1283 return -EINVAL;
1284 }
1285 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1286 cxd2841er_read_regs(priv, I2C_SLVT, 0x15, data, sizeof(data));
1287 *offset = div_s64(41000LL * sign_extend32((((u32)data[0] & 0x3f) << 8)
1288 | (u32)data[1], 13), 16384);
1289 return 0;
1290}
1291
Abylay Ospana6f330c2016-07-15 15:34:22 -03001292static int cxd2841er_read_packet_errors_c(
1293 struct cxd2841er_priv *priv, u32 *penum)
1294{
1295 u8 data[3];
1296
1297 *penum = 0;
1298 if (priv->state != STATE_ACTIVE_TC) {
1299 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1300 __func__, priv->state);
1301 return -EINVAL;
1302 }
1303 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1304 cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
1305 if (data[2] & 0x01)
1306 *penum = ((u32)data[0] << 8) | (u32)data[1];
1307 return 0;
1308}
1309
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001310static int cxd2841er_read_packet_errors_t(
1311 struct cxd2841er_priv *priv, u32 *penum)
1312{
1313 u8 data[3];
1314
1315 *penum = 0;
1316 if (priv->state != STATE_ACTIVE_TC) {
1317 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1318 __func__, priv->state);
1319 return -EINVAL;
1320 }
1321 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1322 cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
1323 if (data[2] & 0x01)
1324 *penum = ((u32)data[0] << 8) | (u32)data[1];
1325 return 0;
1326}
1327
1328static int cxd2841er_read_packet_errors_t2(
1329 struct cxd2841er_priv *priv, u32 *penum)
1330{
1331 u8 data[3];
1332
1333 *penum = 0;
1334 if (priv->state != STATE_ACTIVE_TC) {
1335 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1336 __func__, priv->state);
1337 return -EINVAL;
1338 }
1339 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
1340 cxd2841er_read_regs(priv, I2C_SLVT, 0xfd, data, sizeof(data));
1341 if (data[0] & 0x01)
1342 *penum = ((u32)data[1] << 8) | (u32)data[2];
1343 return 0;
1344}
1345
Abylay Ospan83808c22016-03-22 19:20:34 -03001346static int cxd2841er_read_packet_errors_i(
1347 struct cxd2841er_priv *priv, u32 *penum)
1348{
1349 u8 data[2];
1350
1351 *penum = 0;
1352 if (priv->state != STATE_ACTIVE_TC) {
1353 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1354 __func__, priv->state);
1355 return -EINVAL;
1356 }
1357 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1358 cxd2841er_read_regs(priv, I2C_SLVT, 0xA1, data, 1);
1359
1360 if (!(data[0] & 0x01))
1361 return 0;
1362
1363 /* Layer A */
1364 cxd2841er_read_regs(priv, I2C_SLVT, 0xA2, data, sizeof(data));
1365 *penum = ((u32)data[0] << 8) | (u32)data[1];
1366
1367 /* Layer B */
1368 cxd2841er_read_regs(priv, I2C_SLVT, 0xA4, data, sizeof(data));
1369 *penum += ((u32)data[0] << 8) | (u32)data[1];
1370
1371 /* Layer C */
1372 cxd2841er_read_regs(priv, I2C_SLVT, 0xA6, data, sizeof(data));
1373 *penum += ((u32)data[0] << 8) | (u32)data[1];
1374
1375 return 0;
1376}
1377
Abylay Ospana6f330c2016-07-15 15:34:22 -03001378static int cxd2841er_read_ber_c(struct cxd2841er_priv *priv,
1379 u32 *bit_error, u32 *bit_count)
1380{
1381 u8 data[3];
1382 u32 bit_err, period_exp;
1383
1384 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1385 if (priv->state != STATE_ACTIVE_TC) {
1386 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1387 __func__, priv->state);
1388 return -EINVAL;
1389 }
1390 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1391 cxd2841er_read_regs(priv, I2C_SLVT, 0x62, data, sizeof(data));
1392 if (!(data[0] & 0x80)) {
1393 dev_dbg(&priv->i2c->dev,
1394 "%s(): no valid BER data\n", __func__);
1395 return -EINVAL;
1396 }
1397 bit_err = ((u32)(data[0] & 0x3f) << 16) |
1398 ((u32)data[1] << 8) |
1399 (u32)data[2];
1400 cxd2841er_read_reg(priv, I2C_SLVT, 0x60, data);
1401 period_exp = data[0] & 0x1f;
1402
1403 if ((period_exp <= 11) && (bit_err > (1 << period_exp) * 204 * 8)) {
1404 dev_dbg(&priv->i2c->dev,
1405 "%s(): period_exp(%u) or bit_err(%u) not in range. no valid BER data\n",
1406 __func__, period_exp, bit_err);
1407 return -EINVAL;
1408 }
1409
1410 dev_dbg(&priv->i2c->dev,
1411 "%s(): period_exp(%u) or bit_err(%u) count=%d\n",
1412 __func__, period_exp, bit_err,
1413 ((1 << period_exp) * 204 * 8));
1414
1415 *bit_error = bit_err;
1416 *bit_count = ((1 << period_exp) * 204 * 8);
1417
1418 return 0;
1419}
1420
Abylay Ospan0854df72016-07-19 12:22:03 -03001421static int cxd2841er_read_ber_i(struct cxd2841er_priv *priv,
1422 u32 *bit_error, u32 *bit_count)
1423{
1424 u8 data[3];
1425 u8 pktnum[2];
1426
1427 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1428 if (priv->state != STATE_ACTIVE_TC) {
1429 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1430 __func__, priv->state);
1431 return -EINVAL;
1432 }
1433
1434 cxd2841er_freeze_regs(priv);
1435 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1436 cxd2841er_read_regs(priv, I2C_SLVT, 0x5B, pktnum, sizeof(pktnum));
1437 cxd2841er_read_regs(priv, I2C_SLVT, 0x16, data, sizeof(data));
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001438 cxd2841er_unfreeze_regs(priv);
Abylay Ospan0854df72016-07-19 12:22:03 -03001439
1440 if (!pktnum[0] && !pktnum[1]) {
1441 dev_dbg(&priv->i2c->dev,
1442 "%s(): no valid BER data\n", __func__);
Abylay Ospan0854df72016-07-19 12:22:03 -03001443 return -EINVAL;
1444 }
1445
1446 *bit_error = ((u32)(data[0] & 0x7F) << 16) |
1447 ((u32)data[1] << 8) | data[2];
1448 *bit_count = ((((u32)pktnum[0] << 8) | pktnum[1]) * 204 * 8);
1449 dev_dbg(&priv->i2c->dev, "%s(): bit_error=%u bit_count=%u\n",
1450 __func__, *bit_error, *bit_count);
1451
Abylay Ospan0854df72016-07-19 12:22:03 -03001452 return 0;
1453}
1454
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001455static int cxd2841er_mon_read_ber_s(struct cxd2841er_priv *priv,
1456 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001457{
1458 u8 data[11];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001459
1460 /* Set SLV-T Bank : 0xA0 */
1461 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1462 /*
1463 * slave Bank Addr Bit Signal name
1464 * <SLV-T> A0h 35h [0] IFVBER_VALID
1465 * <SLV-T> A0h 36h [5:0] IFVBER_BITERR[21:16]
1466 * <SLV-T> A0h 37h [7:0] IFVBER_BITERR[15:8]
1467 * <SLV-T> A0h 38h [7:0] IFVBER_BITERR[7:0]
1468 * <SLV-T> A0h 3Dh [5:0] IFVBER_BITNUM[21:16]
1469 * <SLV-T> A0h 3Eh [7:0] IFVBER_BITNUM[15:8]
1470 * <SLV-T> A0h 3Fh [7:0] IFVBER_BITNUM[7:0]
1471 */
1472 cxd2841er_read_regs(priv, I2C_SLVT, 0x35, data, 11);
1473 if (data[0] & 0x01) {
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001474 *bit_error = ((u32)(data[1] & 0x3F) << 16) |
1475 ((u32)(data[2] & 0xFF) << 8) |
1476 (u32)(data[3] & 0xFF);
1477 *bit_count = ((u32)(data[8] & 0x3F) << 16) |
1478 ((u32)(data[9] & 0xFF) << 8) |
1479 (u32)(data[10] & 0xFF);
1480 if ((*bit_count == 0) || (*bit_error > *bit_count)) {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001481 dev_dbg(&priv->i2c->dev,
1482 "%s(): invalid bit_error %d, bit_count %d\n",
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001483 __func__, *bit_error, *bit_count);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001484 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001485 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001486 return 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001487 }
1488 dev_dbg(&priv->i2c->dev, "%s(): no data available\n", __func__);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001489 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001490}
1491
1492
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001493static int cxd2841er_mon_read_ber_s2(struct cxd2841er_priv *priv,
1494 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001495{
1496 u8 data[5];
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001497 u32 period;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001498
1499 /* Set SLV-T Bank : 0xB2 */
1500 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xb2);
1501 /*
1502 * slave Bank Addr Bit Signal name
1503 * <SLV-T> B2h 30h [0] IFLBER_VALID
1504 * <SLV-T> B2h 31h [3:0] IFLBER_BITERR[27:24]
1505 * <SLV-T> B2h 32h [7:0] IFLBER_BITERR[23:16]
1506 * <SLV-T> B2h 33h [7:0] IFLBER_BITERR[15:8]
1507 * <SLV-T> B2h 34h [7:0] IFLBER_BITERR[7:0]
1508 */
1509 cxd2841er_read_regs(priv, I2C_SLVT, 0x30, data, 5);
1510 if (data[0] & 0x01) {
1511 /* Bit error count */
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001512 *bit_error = ((u32)(data[1] & 0x0F) << 24) |
1513 ((u32)(data[2] & 0xFF) << 16) |
1514 ((u32)(data[3] & 0xFF) << 8) |
1515 (u32)(data[4] & 0xFF);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001516
1517 /* Set SLV-T Bank : 0xA0 */
1518 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1519 cxd2841er_read_reg(priv, I2C_SLVT, 0x7a, data);
1520 /* Measurement period */
1521 period = (u32)(1 << (data[0] & 0x0F));
1522 if (period == 0) {
1523 dev_dbg(&priv->i2c->dev,
1524 "%s(): period is 0\n", __func__);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001525 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001526 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001527 if (*bit_error > (period * 64800)) {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001528 dev_dbg(&priv->i2c->dev,
1529 "%s(): invalid bit_err 0x%x period 0x%x\n",
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001530 __func__, *bit_error, period);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001531 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001532 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001533 *bit_count = period * 64800;
1534
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001535 return 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001536 } else {
1537 dev_dbg(&priv->i2c->dev,
1538 "%s(): no data available\n", __func__);
1539 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001540 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001541}
1542
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001543static int cxd2841er_read_ber_t2(struct cxd2841er_priv *priv,
1544 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001545{
1546 u8 data[4];
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001547 u32 period_exp, n_ldpc;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001548
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001549 if (priv->state != STATE_ACTIVE_TC) {
1550 dev_dbg(&priv->i2c->dev,
1551 "%s(): invalid state %d\n", __func__, priv->state);
1552 return -EINVAL;
1553 }
1554 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1555 cxd2841er_read_regs(priv, I2C_SLVT, 0x39, data, sizeof(data));
1556 if (!(data[0] & 0x10)) {
1557 dev_dbg(&priv->i2c->dev,
1558 "%s(): no valid BER data\n", __func__);
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001559 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001560 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001561 *bit_error = ((u32)(data[0] & 0x0f) << 24) |
1562 ((u32)data[1] << 16) |
1563 ((u32)data[2] << 8) |
1564 (u32)data[3];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001565 cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
1566 period_exp = data[0] & 0x0f;
1567 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x22);
1568 cxd2841er_read_reg(priv, I2C_SLVT, 0x5e, data);
1569 n_ldpc = ((data[0] & 0x03) == 0 ? 16200 : 64800);
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001570 if (*bit_error > ((1U << period_exp) * n_ldpc)) {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001571 dev_dbg(&priv->i2c->dev,
1572 "%s(): invalid BER value\n", __func__);
1573 return -EINVAL;
1574 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001575
1576 /*
1577 * FIXME: the right thing would be to return bit_error untouched,
1578 * but, as we don't know the scale returned by the counters, let's
1579 * at least preserver BER = bit_error/bit_count.
1580 */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001581 if (period_exp >= 4) {
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001582 *bit_count = (1U << (period_exp - 4)) * (n_ldpc / 200);
1583 *bit_error *= 3125ULL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001584 } else {
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001585 *bit_count = (1U << period_exp) * (n_ldpc / 200);
Abylay Ospana6f330c2016-07-15 15:34:22 -03001586 *bit_error *= 50000ULL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001587 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001588 return 0;
1589}
1590
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001591static int cxd2841er_read_ber_t(struct cxd2841er_priv *priv,
1592 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001593{
1594 u8 data[2];
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001595 u32 period;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001596
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001597 if (priv->state != STATE_ACTIVE_TC) {
1598 dev_dbg(&priv->i2c->dev,
1599 "%s(): invalid state %d\n", __func__, priv->state);
1600 return -EINVAL;
1601 }
1602 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1603 cxd2841er_read_reg(priv, I2C_SLVT, 0x39, data);
1604 if (!(data[0] & 0x01)) {
1605 dev_dbg(&priv->i2c->dev,
1606 "%s(): no valid BER data\n", __func__);
1607 return 0;
1608 }
1609 cxd2841er_read_regs(priv, I2C_SLVT, 0x22, data, sizeof(data));
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001610 *bit_error = ((u32)data[0] << 8) | (u32)data[1];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001611 cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
1612 period = ((data[0] & 0x07) == 0) ? 256 : (4096 << (data[0] & 0x07));
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001613
1614 /*
1615 * FIXME: the right thing would be to return bit_error untouched,
1616 * but, as we don't know the scale returned by the counters, let's
1617 * at least preserver BER = bit_error/bit_count.
1618 */
1619 *bit_count = period / 128;
1620 *bit_error *= 78125ULL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001621 return 0;
1622}
1623
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001624static int cxd2841er_freeze_regs(struct cxd2841er_priv *priv)
1625{
1626 /*
1627 * Freeze registers: ensure multiple separate register reads
1628 * are from the same snapshot
1629 */
1630 cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x01);
1631 return 0;
1632}
1633
1634static int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv)
1635{
1636 /*
1637 * un-freeze registers
1638 */
1639 cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x00);
1640 return 0;
1641}
1642
Abylay Ospane05b1872016-07-15 17:04:17 -03001643static u32 cxd2841er_dvbs_read_snr(struct cxd2841er_priv *priv,
1644 u8 delsys, u32 *snr)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001645{
1646 u8 data[3];
1647 u32 res = 0, value;
1648 int min_index, max_index, index;
1649 static const struct cxd2841er_cnr_data *cn_data;
1650
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001651 cxd2841er_freeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001652 /* Set SLV-T Bank : 0xA1 */
1653 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa1);
1654 /*
1655 * slave Bank Addr Bit Signal name
1656 * <SLV-T> A1h 10h [0] ICPM_QUICKRDY
1657 * <SLV-T> A1h 11h [4:0] ICPM_QUICKCNDT[12:8]
1658 * <SLV-T> A1h 12h [7:0] ICPM_QUICKCNDT[7:0]
1659 */
1660 cxd2841er_read_regs(priv, I2C_SLVT, 0x10, data, 3);
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001661 cxd2841er_unfreeze_regs(priv);
1662
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001663 if (data[0] & 0x01) {
1664 value = ((u32)(data[1] & 0x1F) << 8) | (u32)(data[2] & 0xFF);
1665 min_index = 0;
1666 if (delsys == SYS_DVBS) {
1667 cn_data = s_cn_data;
1668 max_index = sizeof(s_cn_data) /
1669 sizeof(s_cn_data[0]) - 1;
1670 } else {
1671 cn_data = s2_cn_data;
1672 max_index = sizeof(s2_cn_data) /
1673 sizeof(s2_cn_data[0]) - 1;
1674 }
1675 if (value >= cn_data[min_index].value) {
1676 res = cn_data[min_index].cnr_x1000;
1677 goto done;
1678 }
1679 if (value <= cn_data[max_index].value) {
1680 res = cn_data[max_index].cnr_x1000;
1681 goto done;
1682 }
1683 while ((max_index - min_index) > 1) {
1684 index = (max_index + min_index) / 2;
1685 if (value == cn_data[index].value) {
1686 res = cn_data[index].cnr_x1000;
1687 goto done;
1688 } else if (value > cn_data[index].value)
1689 max_index = index;
1690 else
1691 min_index = index;
1692 if ((max_index - min_index) <= 1) {
1693 if (value == cn_data[max_index].value) {
1694 res = cn_data[max_index].cnr_x1000;
1695 goto done;
1696 } else {
1697 res = cn_data[min_index].cnr_x1000;
1698 goto done;
1699 }
1700 }
1701 }
1702 } else {
1703 dev_dbg(&priv->i2c->dev,
1704 "%s(): no data available\n", __func__);
Abylay Ospane05b1872016-07-15 17:04:17 -03001705 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001706 }
1707done:
Abylay Ospane05b1872016-07-15 17:04:17 -03001708 *snr = res;
1709 return 0;
1710}
1711
1712static uint32_t sony_log(uint32_t x)
1713{
1714 return (((10000>>8)*(intlog2(x)>>16) + LOG2_E_100X/2)/LOG2_E_100X);
1715}
1716
1717static int cxd2841er_read_snr_c(struct cxd2841er_priv *priv, u32 *snr)
1718{
1719 u32 reg;
1720 u8 data[2];
1721 enum sony_dvbc_constellation_t qam = SONY_DVBC_CONSTELLATION_16QAM;
1722
1723 *snr = 0;
1724 if (priv->state != STATE_ACTIVE_TC) {
1725 dev_dbg(&priv->i2c->dev,
1726 "%s(): invalid state %d\n",
1727 __func__, priv->state);
1728 return -EINVAL;
1729 }
1730
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001731 cxd2841er_freeze_regs(priv);
Abylay Ospane05b1872016-07-15 17:04:17 -03001732 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1733 cxd2841er_read_regs(priv, I2C_SLVT, 0x19, data, 1);
1734 qam = (enum sony_dvbc_constellation_t) (data[0] & 0x07);
1735 cxd2841er_read_regs(priv, I2C_SLVT, 0x4C, data, 2);
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001736 cxd2841er_unfreeze_regs(priv);
Abylay Ospane05b1872016-07-15 17:04:17 -03001737
1738 reg = ((u32)(data[0]&0x1f) << 8) | (u32)data[1];
1739 if (reg == 0) {
1740 dev_dbg(&priv->i2c->dev,
1741 "%s(): reg value out of range\n", __func__);
1742 return 0;
1743 }
1744
1745 switch (qam) {
1746 case SONY_DVBC_CONSTELLATION_16QAM:
1747 case SONY_DVBC_CONSTELLATION_64QAM:
1748 case SONY_DVBC_CONSTELLATION_256QAM:
1749 /* SNR(dB) = -9.50 * ln(IREG_SNR_ESTIMATE / (24320)) */
1750 if (reg < 126)
1751 reg = 126;
1752 *snr = -95 * (int32_t)sony_log(reg) + 95941;
1753 break;
1754 case SONY_DVBC_CONSTELLATION_32QAM:
1755 case SONY_DVBC_CONSTELLATION_128QAM:
1756 /* SNR(dB) = -8.75 * ln(IREG_SNR_ESTIMATE / (20800)) */
1757 if (reg < 69)
1758 reg = 69;
1759 *snr = -88 * (int32_t)sony_log(reg) + 86999;
1760 break;
1761 default:
1762 return -EINVAL;
1763 }
1764
1765 return 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001766}
1767
1768static int cxd2841er_read_snr_t(struct cxd2841er_priv *priv, u32 *snr)
1769{
1770 u32 reg;
1771 u8 data[2];
1772
1773 *snr = 0;
1774 if (priv->state != STATE_ACTIVE_TC) {
1775 dev_dbg(&priv->i2c->dev,
1776 "%s(): invalid state %d\n", __func__, priv->state);
1777 return -EINVAL;
1778 }
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001779
1780 cxd2841er_freeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001781 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1782 cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001783 cxd2841er_unfreeze_regs(priv);
1784
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001785 reg = ((u32)data[0] << 8) | (u32)data[1];
1786 if (reg == 0) {
1787 dev_dbg(&priv->i2c->dev,
1788 "%s(): reg value out of range\n", __func__);
1789 return 0;
1790 }
1791 if (reg > 4996)
1792 reg = 4996;
1793 *snr = 10000 * ((intlog10(reg) - intlog10(5350 - reg)) >> 24) + 28500;
1794 return 0;
1795}
1796
Mauro Carvalho Chehabc8946c82015-08-11 15:08:47 -03001797static int cxd2841er_read_snr_t2(struct cxd2841er_priv *priv, u32 *snr)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001798{
1799 u32 reg;
1800 u8 data[2];
1801
1802 *snr = 0;
1803 if (priv->state != STATE_ACTIVE_TC) {
1804 dev_dbg(&priv->i2c->dev,
1805 "%s(): invalid state %d\n", __func__, priv->state);
1806 return -EINVAL;
1807 }
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001808
1809 cxd2841er_freeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001810 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1811 cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001812 cxd2841er_unfreeze_regs(priv);
1813
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001814 reg = ((u32)data[0] << 8) | (u32)data[1];
1815 if (reg == 0) {
1816 dev_dbg(&priv->i2c->dev,
1817 "%s(): reg value out of range\n", __func__);
1818 return 0;
1819 }
1820 if (reg > 10876)
1821 reg = 10876;
1822 *snr = 10000 * ((intlog10(reg) -
1823 intlog10(12600 - reg)) >> 24) + 32000;
1824 return 0;
1825}
1826
Abylay Ospan83808c22016-03-22 19:20:34 -03001827static int cxd2841er_read_snr_i(struct cxd2841er_priv *priv, u32 *snr)
1828{
1829 u32 reg;
1830 u8 data[2];
1831
1832 *snr = 0;
1833 if (priv->state != STATE_ACTIVE_TC) {
1834 dev_dbg(&priv->i2c->dev,
1835 "%s(): invalid state %d\n", __func__,
1836 priv->state);
1837 return -EINVAL;
1838 }
1839
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001840 cxd2841er_freeze_regs(priv);
Abylay Ospan83808c22016-03-22 19:20:34 -03001841 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1842 cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001843 cxd2841er_unfreeze_regs(priv);
1844
Abylay Ospan83808c22016-03-22 19:20:34 -03001845 reg = ((u32)data[0] << 8) | (u32)data[1];
1846 if (reg == 0) {
1847 dev_dbg(&priv->i2c->dev,
1848 "%s(): reg value out of range\n", __func__);
1849 return 0;
1850 }
Abylay Ospan0854df72016-07-19 12:22:03 -03001851 *snr = 10000 * (intlog10(reg) >> 24) - 9031;
Abylay Ospan83808c22016-03-22 19:20:34 -03001852 return 0;
1853}
1854
Abylay Ospand0998ce2016-06-30 23:09:48 -03001855static u16 cxd2841er_read_agc_gain_c(struct cxd2841er_priv *priv,
1856 u8 delsys)
1857{
1858 u8 data[2];
1859
1860 cxd2841er_write_reg(
1861 priv, I2C_SLVT, 0x00, 0x40);
1862 cxd2841er_read_regs(priv, I2C_SLVT, 0x49, data, 2);
1863 dev_dbg(&priv->i2c->dev,
1864 "%s(): AGC value=%u\n",
1865 __func__, (((u16)data[0] & 0x0F) << 8) |
1866 (u16)(data[1] & 0xFF));
1867 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1868}
1869
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001870static u16 cxd2841er_read_agc_gain_t_t2(struct cxd2841er_priv *priv,
1871 u8 delsys)
1872{
1873 u8 data[2];
1874
1875 cxd2841er_write_reg(
1876 priv, I2C_SLVT, 0x00, (delsys == SYS_DVBT ? 0x10 : 0x20));
1877 cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03001878 dev_dbg(&priv->i2c->dev,
1879 "%s(): AGC value=%u\n",
1880 __func__, (((u16)data[0] & 0x0F) << 8) |
1881 (u16)(data[1] & 0xFF));
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001882 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1883}
1884
Abylay Ospan83808c22016-03-22 19:20:34 -03001885static u16 cxd2841er_read_agc_gain_i(struct cxd2841er_priv *priv,
1886 u8 delsys)
1887{
1888 u8 data[2];
1889
1890 cxd2841er_write_reg(
1891 priv, I2C_SLVT, 0x00, 0x60);
1892 cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
1893
1894 dev_dbg(&priv->i2c->dev,
1895 "%s(): AGC value=%u\n",
1896 __func__, (((u16)data[0] & 0x0F) << 8) |
1897 (u16)(data[1] & 0xFF));
1898 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1899}
1900
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001901static u16 cxd2841er_read_agc_gain_s(struct cxd2841er_priv *priv)
1902{
1903 u8 data[2];
1904
1905 /* Set SLV-T Bank : 0xA0 */
1906 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1907 /*
1908 * slave Bank Addr Bit Signal name
1909 * <SLV-T> A0h 1Fh [4:0] IRFAGC_GAIN[12:8]
1910 * <SLV-T> A0h 20h [7:0] IRFAGC_GAIN[7:0]
1911 */
1912 cxd2841er_read_regs(priv, I2C_SLVT, 0x1f, data, 2);
1913 return ((((u16)data[0] & 0x1F) << 8) | (u16)(data[1] & 0xFF)) << 3;
1914}
1915
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001916static void cxd2841er_read_ber(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001917{
1918 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1919 struct cxd2841er_priv *priv = fe->demodulator_priv;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001920 u32 ret, bit_error = 0, bit_count = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001921
1922 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001923 switch (p->delivery_system) {
Abylay Ospana6f330c2016-07-15 15:34:22 -03001924 case SYS_DVBC_ANNEX_A:
1925 case SYS_DVBC_ANNEX_B:
1926 case SYS_DVBC_ANNEX_C:
1927 ret = cxd2841er_read_ber_c(priv, &bit_error, &bit_count);
1928 break;
Abylay Ospan0854df72016-07-19 12:22:03 -03001929 case SYS_ISDBT:
1930 ret = cxd2841er_read_ber_i(priv, &bit_error, &bit_count);
1931 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001932 case SYS_DVBS:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001933 ret = cxd2841er_mon_read_ber_s(priv, &bit_error, &bit_count);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001934 break;
1935 case SYS_DVBS2:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001936 ret = cxd2841er_mon_read_ber_s2(priv, &bit_error, &bit_count);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001937 break;
1938 case SYS_DVBT:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001939 ret = cxd2841er_read_ber_t(priv, &bit_error, &bit_count);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001940 break;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001941 case SYS_DVBT2:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001942 ret = cxd2841er_read_ber_t2(priv, &bit_error, &bit_count);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001943 break;
1944 default:
1945 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001946 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001947 return;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001948 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001949
1950 if (!ret) {
1951 p->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
Abylay Ospana6f330c2016-07-15 15:34:22 -03001952 p->post_bit_error.stat[0].uvalue += bit_error;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001953 p->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Abylay Ospana6f330c2016-07-15 15:34:22 -03001954 p->post_bit_count.stat[0].uvalue += bit_count;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001955 } else {
1956 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001957 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001958 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001959}
1960
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03001961static void cxd2841er_read_signal_strength(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001962{
1963 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1964 struct cxd2841er_priv *priv = fe->demodulator_priv;
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001965 s32 strength;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001966
1967 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1968 switch (p->delivery_system) {
1969 case SYS_DVBT:
1970 case SYS_DVBT2:
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03001971 strength = cxd2841er_read_agc_gain_t_t2(priv,
1972 p->delivery_system);
1973 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1974 /* Formula was empirically determinated @ 410 MHz */
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001975 p->strength.stat[0].uvalue = strength * 366 / 100 - 89520;
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03001976 break; /* Code moved out of the function */
Mauro Carvalho Chehab988bd282016-07-01 11:03:14 -03001977 case SYS_DVBC_ANNEX_A:
Abylay Ospan997bdc02016-07-15 14:59:37 -03001978 case SYS_DVBC_ANNEX_B:
1979 case SYS_DVBC_ANNEX_C:
1980 strength = cxd2841er_read_agc_gain_c(priv,
Mauro Carvalho Chehab988bd282016-07-01 11:03:14 -03001981 p->delivery_system);
Mauro Carvalho Chehabd12b7912016-07-01 11:03:16 -03001982 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1983 /*
1984 * Formula was empirically determinated via linear regression,
1985 * using frequencies: 175 MHz, 410 MHz and 800 MHz, and a
1986 * stream modulated with QAM64
1987 */
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001988 p->strength.stat[0].uvalue = strength * 4045 / 1000 - 85224;
Mauro Carvalho Chehab988bd282016-07-01 11:03:14 -03001989 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03001990 case SYS_ISDBT:
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001991 strength = cxd2841er_read_agc_gain_i(priv, p->delivery_system);
1992 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1993 /*
1994 * Formula was empirically determinated via linear regression,
1995 * using frequencies: 175 MHz, 410 MHz and 800 MHz.
1996 */
1997 p->strength.stat[0].uvalue = strength * 3775 / 1000 - 90185;
Abylay Ospan83808c22016-03-22 19:20:34 -03001998 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001999 case SYS_DVBS:
2000 case SYS_DVBS2:
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03002001 strength = 65535 - cxd2841er_read_agc_gain_s(priv);
2002 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
2003 p->strength.stat[0].uvalue = strength;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002004 break;
2005 default:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002006 p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002007 break;
2008 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002009}
2010
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002011static void cxd2841er_read_snr(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002012{
2013 u32 tmp = 0;
Abylay Ospane05b1872016-07-15 17:04:17 -03002014 int ret = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002015 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2016 struct cxd2841er_priv *priv = fe->demodulator_priv;
2017
2018 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2019 switch (p->delivery_system) {
Abylay Ospane05b1872016-07-15 17:04:17 -03002020 case SYS_DVBC_ANNEX_A:
2021 case SYS_DVBC_ANNEX_B:
2022 case SYS_DVBC_ANNEX_C:
2023 ret = cxd2841er_read_snr_c(priv, &tmp);
2024 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002025 case SYS_DVBT:
Abylay Ospane05b1872016-07-15 17:04:17 -03002026 ret = cxd2841er_read_snr_t(priv, &tmp);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002027 break;
2028 case SYS_DVBT2:
Abylay Ospane05b1872016-07-15 17:04:17 -03002029 ret = cxd2841er_read_snr_t2(priv, &tmp);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002030 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03002031 case SYS_ISDBT:
Abylay Ospane05b1872016-07-15 17:04:17 -03002032 ret = cxd2841er_read_snr_i(priv, &tmp);
Abylay Ospan83808c22016-03-22 19:20:34 -03002033 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002034 case SYS_DVBS:
2035 case SYS_DVBS2:
Abylay Ospane05b1872016-07-15 17:04:17 -03002036 ret = cxd2841er_dvbs_read_snr(priv, p->delivery_system, &tmp);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002037 break;
2038 default:
2039 dev_dbg(&priv->i2c->dev, "%s(): unknown delivery system %d\n",
2040 __func__, p->delivery_system);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002041 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2042 return;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002043 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002044
Abylay Ospan0854df72016-07-19 12:22:03 -03002045 dev_dbg(&priv->i2c->dev, "%s(): snr=%d\n",
2046 __func__, (int32_t)tmp);
2047
Abylay Ospane05b1872016-07-15 17:04:17 -03002048 if (!ret) {
2049 p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
2050 p->cnr.stat[0].svalue = tmp;
2051 } else {
2052 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2053 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002054}
2055
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002056static void cxd2841er_read_ucblocks(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002057{
2058 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2059 struct cxd2841er_priv *priv = fe->demodulator_priv;
Abylay Ospan4a86bc12016-07-19 00:10:20 -03002060 u32 ucblocks = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002061
2062 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2063 switch (p->delivery_system) {
Abylay Ospana6f330c2016-07-15 15:34:22 -03002064 case SYS_DVBC_ANNEX_A:
2065 case SYS_DVBC_ANNEX_B:
2066 case SYS_DVBC_ANNEX_C:
2067 cxd2841er_read_packet_errors_c(priv, &ucblocks);
2068 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002069 case SYS_DVBT:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002070 cxd2841er_read_packet_errors_t(priv, &ucblocks);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002071 break;
2072 case SYS_DVBT2:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002073 cxd2841er_read_packet_errors_t2(priv, &ucblocks);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002074 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03002075 case SYS_ISDBT:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002076 cxd2841er_read_packet_errors_i(priv, &ucblocks);
Abylay Ospan83808c22016-03-22 19:20:34 -03002077 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002078 default:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002079 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2080 return;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002081 }
Abylay Ospan4a86bc12016-07-19 00:10:20 -03002082 dev_dbg(&priv->i2c->dev, "%s() ucblocks=%u\n", __func__, ucblocks);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002083
2084 p->block_error.stat[0].scale = FE_SCALE_COUNTER;
2085 p->block_error.stat[0].uvalue = ucblocks;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002086}
2087
2088static int cxd2841er_dvbt2_set_profile(
2089 struct cxd2841er_priv *priv, enum cxd2841er_dvbt2_profile_t profile)
2090{
2091 u8 tune_mode;
2092 u8 seq_not2d_time;
2093
2094 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2095 switch (profile) {
2096 case DVBT2_PROFILE_BASE:
2097 tune_mode = 0x01;
Abylay Ospan6c771612016-05-16 11:43:25 -03002098 /* Set early unlock time */
2099 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x0E:0x0C;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002100 break;
2101 case DVBT2_PROFILE_LITE:
2102 tune_mode = 0x05;
Abylay Ospan6c771612016-05-16 11:43:25 -03002103 /* Set early unlock time */
2104 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002105 break;
2106 case DVBT2_PROFILE_ANY:
2107 tune_mode = 0x00;
Abylay Ospan6c771612016-05-16 11:43:25 -03002108 /* Set early unlock time */
2109 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002110 break;
2111 default:
2112 return -EINVAL;
2113 }
2114 /* Set SLV-T Bank : 0x2E */
2115 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2e);
2116 /* Set profile and tune mode */
2117 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x10, tune_mode, 0x07);
2118 /* Set SLV-T Bank : 0x2B */
2119 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2120 /* Set early unlock detection time */
2121 cxd2841er_write_reg(priv, I2C_SLVT, 0x9d, seq_not2d_time);
2122 return 0;
2123}
2124
2125static int cxd2841er_dvbt2_set_plp_config(struct cxd2841er_priv *priv,
2126 u8 is_auto, u8 plp_id)
2127{
2128 if (is_auto) {
2129 dev_dbg(&priv->i2c->dev,
2130 "%s() using auto PLP selection\n", __func__);
2131 } else {
2132 dev_dbg(&priv->i2c->dev,
2133 "%s() using manual PLP selection, ID %d\n",
2134 __func__, plp_id);
2135 }
2136 /* Set SLV-T Bank : 0x23 */
2137 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
2138 if (!is_auto) {
2139 /* Manual PLP selection mode. Set the data PLP Id. */
2140 cxd2841er_write_reg(priv, I2C_SLVT, 0xaf, plp_id);
2141 }
2142 /* Auto PLP select (Scanning mode = 0x00). Data PLP select = 0x01. */
2143 cxd2841er_write_reg(priv, I2C_SLVT, 0xad, (is_auto ? 0x00 : 0x01));
2144 return 0;
2145}
2146
2147static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
2148 u32 bandwidth)
2149{
2150 u32 iffreq;
Abylay Ospan6c771612016-05-16 11:43:25 -03002151 u8 data[MAX_WRITE_REGSIZE];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002152
Abylay Ospan6c771612016-05-16 11:43:25 -03002153 const uint8_t nominalRate8bw[3][5] = {
2154 /* TRCG Nominal Rate [37:0] */
2155 {0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2156 {0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2157 {0x11, 0xF0, 0x00, 0x00, 0x00} /* 41MHz XTal */
2158 };
2159
2160 const uint8_t nominalRate7bw[3][5] = {
2161 /* TRCG Nominal Rate [37:0] */
2162 {0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2163 {0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2164 {0x14, 0x80, 0x00, 0x00, 0x00} /* 41MHz XTal */
2165 };
2166
2167 const uint8_t nominalRate6bw[3][5] = {
2168 /* TRCG Nominal Rate [37:0] */
2169 {0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
2170 {0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2171 {0x17, 0xEA, 0xAA, 0xAA, 0xAA} /* 41MHz XTal */
2172 };
2173
2174 const uint8_t nominalRate5bw[3][5] = {
2175 /* TRCG Nominal Rate [37:0] */
2176 {0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
2177 {0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
2178 {0x1C, 0xB3, 0x33, 0x33, 0x33} /* 41MHz XTal */
2179 };
2180
2181 const uint8_t nominalRate17bw[3][5] = {
2182 /* TRCG Nominal Rate [37:0] */
2183 {0x58, 0xE2, 0xAF, 0xE0, 0xBC}, /* 20.5MHz XTal */
2184 {0x68, 0x0F, 0xA2, 0x32, 0xD0}, /* 24MHz XTal */
2185 {0x58, 0xE2, 0xAF, 0xE0, 0xBC} /* 41MHz XTal */
2186 };
2187
2188 const uint8_t itbCoef8bw[3][14] = {
2189 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
2190 0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
2191 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1,
2192 0x29, 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal */
2193 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
2194 0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8} /* 41MHz XTal */
2195 };
2196
2197 const uint8_t itbCoef7bw[3][14] = {
2198 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
2199 0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
2200 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0,
2201 0x29, 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal */
2202 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
2203 0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5} /* 41MHz XTal */
2204 };
2205
2206 const uint8_t itbCoef6bw[3][14] = {
2207 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2208 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2209 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
2210 0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2211 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2212 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2213 };
2214
2215 const uint8_t itbCoef5bw[3][14] = {
2216 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2217 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2218 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
2219 0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2220 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2221 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2222 };
2223
2224 const uint8_t itbCoef17bw[3][14] = {
2225 {0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
2226 0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99}, /* 20.5MHz XTal */
2227 {0x33, 0x8E, 0x2B, 0x97, 0x2D, 0x95, 0x37, 0x8B,
2228 0x30, 0x97, 0x2D, 0x9A, 0x21, 0xA4}, /* 24MHz XTal */
2229 {0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
2230 0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99} /* 41MHz XTal */
2231 };
2232
2233 /* Set SLV-T Bank : 0x20 */
2234 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
2235
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002236 switch (bandwidth) {
2237 case 8000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002238 /* <Timing Recovery setting> */
2239 cxd2841er_write_regs(priv, I2C_SLVT,
2240 0x9F, nominalRate8bw[priv->xtal], 5);
2241
2242 /* Set SLV-T Bank : 0x27 */
2243 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2244 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2245 0x7a, 0x00, 0x0f);
2246
2247 /* Set SLV-T Bank : 0x10 */
2248 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2249
2250 /* Group delay equaliser settings for
2251 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2252 */
2253 cxd2841er_write_regs(priv, I2C_SLVT,
2254 0xA6, itbCoef8bw[priv->xtal], 14);
2255 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002256 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4800000);
Abylay Ospan6c771612016-05-16 11:43:25 -03002257 data[0] = (u8) ((iffreq >> 16) & 0xff);
2258 data[1] = (u8)((iffreq >> 8) & 0xff);
2259 data[2] = (u8)(iffreq & 0xff);
2260 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2261 /* System bandwidth setting */
2262 cxd2841er_set_reg_bits(
2263 priv, I2C_SLVT, 0xD7, 0x00, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002264 break;
2265 case 7000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002266 /* <Timing Recovery setting> */
2267 cxd2841er_write_regs(priv, I2C_SLVT,
2268 0x9F, nominalRate7bw[priv->xtal], 5);
2269
2270 /* Set SLV-T Bank : 0x27 */
2271 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2272 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2273 0x7a, 0x00, 0x0f);
2274
2275 /* Set SLV-T Bank : 0x10 */
2276 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2277
2278 /* Group delay equaliser settings for
2279 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2280 */
2281 cxd2841er_write_regs(priv, I2C_SLVT,
2282 0xA6, itbCoef7bw[priv->xtal], 14);
2283 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002284 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4200000);
Abylay Ospan6c771612016-05-16 11:43:25 -03002285 data[0] = (u8) ((iffreq >> 16) & 0xff);
2286 data[1] = (u8)((iffreq >> 8) & 0xff);
2287 data[2] = (u8)(iffreq & 0xff);
2288 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2289 /* System bandwidth setting */
2290 cxd2841er_set_reg_bits(
2291 priv, I2C_SLVT, 0xD7, 0x02, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002292 break;
2293 case 6000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002294 /* <Timing Recovery setting> */
2295 cxd2841er_write_regs(priv, I2C_SLVT,
2296 0x9F, nominalRate6bw[priv->xtal], 5);
2297
2298 /* Set SLV-T Bank : 0x27 */
2299 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2300 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2301 0x7a, 0x00, 0x0f);
2302
2303 /* Set SLV-T Bank : 0x10 */
2304 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2305
2306 /* Group delay equaliser settings for
2307 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2308 */
2309 cxd2841er_write_regs(priv, I2C_SLVT,
2310 0xA6, itbCoef6bw[priv->xtal], 14);
2311 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002312 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3600000);
Abylay Ospan6c771612016-05-16 11:43:25 -03002313 data[0] = (u8) ((iffreq >> 16) & 0xff);
2314 data[1] = (u8)((iffreq >> 8) & 0xff);
2315 data[2] = (u8)(iffreq & 0xff);
2316 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2317 /* System bandwidth setting */
2318 cxd2841er_set_reg_bits(
2319 priv, I2C_SLVT, 0xD7, 0x04, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002320 break;
2321 case 5000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002322 /* <Timing Recovery setting> */
2323 cxd2841er_write_regs(priv, I2C_SLVT,
2324 0x9F, nominalRate5bw[priv->xtal], 5);
2325
2326 /* Set SLV-T Bank : 0x27 */
2327 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2328 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2329 0x7a, 0x00, 0x0f);
2330
2331 /* Set SLV-T Bank : 0x10 */
2332 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2333
2334 /* Group delay equaliser settings for
2335 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2336 */
2337 cxd2841er_write_regs(priv, I2C_SLVT,
2338 0xA6, itbCoef5bw[priv->xtal], 14);
2339 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002340 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3600000);
Abylay Ospan6c771612016-05-16 11:43:25 -03002341 data[0] = (u8) ((iffreq >> 16) & 0xff);
2342 data[1] = (u8)((iffreq >> 8) & 0xff);
2343 data[2] = (u8)(iffreq & 0xff);
2344 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2345 /* System bandwidth setting */
2346 cxd2841er_set_reg_bits(
2347 priv, I2C_SLVT, 0xD7, 0x06, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002348 break;
2349 case 1712000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002350 /* <Timing Recovery setting> */
2351 cxd2841er_write_regs(priv, I2C_SLVT,
2352 0x9F, nominalRate17bw[priv->xtal], 5);
2353
2354 /* Set SLV-T Bank : 0x27 */
2355 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2356 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2357 0x7a, 0x03, 0x0f);
2358
2359 /* Set SLV-T Bank : 0x10 */
2360 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2361
2362 /* Group delay equaliser settings for
2363 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2364 */
2365 cxd2841er_write_regs(priv, I2C_SLVT,
2366 0xA6, itbCoef17bw[priv->xtal], 14);
2367 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002368 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3500000);
Abylay Ospan6c771612016-05-16 11:43:25 -03002369 data[0] = (u8) ((iffreq >> 16) & 0xff);
2370 data[1] = (u8)((iffreq >> 8) & 0xff);
2371 data[2] = (u8)(iffreq & 0xff);
2372 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2373 /* System bandwidth setting */
2374 cxd2841er_set_reg_bits(
2375 priv, I2C_SLVT, 0xD7, 0x03, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002376 break;
2377 default:
2378 return -EINVAL;
2379 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002380 return 0;
2381}
2382
2383static int cxd2841er_sleep_tc_to_active_t_band(
2384 struct cxd2841er_priv *priv, u32 bandwidth)
2385{
Abylay Ospan83808c22016-03-22 19:20:34 -03002386 u8 data[MAX_WRITE_REGSIZE];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002387 u32 iffreq;
Abylay Ospan83808c22016-03-22 19:20:34 -03002388 u8 nominalRate8bw[3][5] = {
2389 /* TRCG Nominal Rate [37:0] */
2390 {0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2391 {0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2392 {0x11, 0xF0, 0x00, 0x00, 0x00} /* 41MHz XTal */
2393 };
2394 u8 nominalRate7bw[3][5] = {
2395 /* TRCG Nominal Rate [37:0] */
2396 {0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2397 {0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2398 {0x14, 0x80, 0x00, 0x00, 0x00} /* 41MHz XTal */
2399 };
2400 u8 nominalRate6bw[3][5] = {
2401 /* TRCG Nominal Rate [37:0] */
2402 {0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
2403 {0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2404 {0x17, 0xEA, 0xAA, 0xAA, 0xAA} /* 41MHz XTal */
2405 };
2406 u8 nominalRate5bw[3][5] = {
2407 /* TRCG Nominal Rate [37:0] */
2408 {0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
2409 {0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
2410 {0x1C, 0xB3, 0x33, 0x33, 0x33} /* 41MHz XTal */
2411 };
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002412
Abylay Ospan83808c22016-03-22 19:20:34 -03002413 u8 itbCoef8bw[3][14] = {
2414 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
2415 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
2416 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29, 0xA5,
2417 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal */
2418 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
2419 0x1F, 0xA8, 0x2C, 0xC8} /* 41MHz XTal */
2420 };
2421 u8 itbCoef7bw[3][14] = {
2422 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
2423 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
2424 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29, 0xA2,
2425 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal */
2426 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
2427 0x26, 0xA9, 0x21, 0xA5} /* 41MHz XTal */
2428 };
2429 u8 itbCoef6bw[3][14] = {
2430 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2431 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2432 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
2433 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2434 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2435 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2436 };
2437 u8 itbCoef5bw[3][14] = {
2438 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2439 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2440 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
2441 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2442 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2443 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2444 };
2445
2446 /* Set SLV-T Bank : 0x13 */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002447 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
2448 /* Echo performance optimization setting */
Abylay Ospan83808c22016-03-22 19:20:34 -03002449 data[0] = 0x01;
2450 data[1] = 0x14;
2451 cxd2841er_write_regs(priv, I2C_SLVT, 0x9C, data, 2);
2452
2453 /* Set SLV-T Bank : 0x10 */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002454 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2455
2456 switch (bandwidth) {
2457 case 8000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002458 /* <Timing Recovery setting> */
2459 cxd2841er_write_regs(priv, I2C_SLVT,
2460 0x9F, nominalRate8bw[priv->xtal], 5);
2461 /* Group delay equaliser settings for
2462 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2463 */
2464 cxd2841er_write_regs(priv, I2C_SLVT,
2465 0xA6, itbCoef8bw[priv->xtal], 14);
2466 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002467 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4800000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002468 data[0] = (u8) ((iffreq >> 16) & 0xff);
2469 data[1] = (u8)((iffreq >> 8) & 0xff);
2470 data[2] = (u8)(iffreq & 0xff);
2471 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2472 /* System bandwidth setting */
2473 cxd2841er_set_reg_bits(
2474 priv, I2C_SLVT, 0xD7, 0x00, 0x07);
2475
2476 /* Demod core latency setting */
2477 if (priv->xtal == SONY_XTAL_24000) {
2478 data[0] = 0x15;
2479 data[1] = 0x28;
2480 } else {
2481 data[0] = 0x01;
2482 data[1] = 0xE0;
2483 }
2484 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2485
2486 /* Notch filter setting */
2487 data[0] = 0x01;
2488 data[1] = 0x02;
2489 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2490 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002491 break;
2492 case 7000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002493 /* <Timing Recovery setting> */
2494 cxd2841er_write_regs(priv, I2C_SLVT,
2495 0x9F, nominalRate7bw[priv->xtal], 5);
2496 /* Group delay equaliser settings for
2497 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2498 */
2499 cxd2841er_write_regs(priv, I2C_SLVT,
2500 0xA6, itbCoef7bw[priv->xtal], 14);
2501 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002502 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4200000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002503 data[0] = (u8) ((iffreq >> 16) & 0xff);
2504 data[1] = (u8)((iffreq >> 8) & 0xff);
2505 data[2] = (u8)(iffreq & 0xff);
2506 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2507 /* System bandwidth setting */
2508 cxd2841er_set_reg_bits(
2509 priv, I2C_SLVT, 0xD7, 0x02, 0x07);
2510
2511 /* Demod core latency setting */
2512 if (priv->xtal == SONY_XTAL_24000) {
2513 data[0] = 0x1F;
2514 data[1] = 0xF8;
2515 } else {
2516 data[0] = 0x12;
2517 data[1] = 0xF8;
2518 }
2519 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2520
2521 /* Notch filter setting */
2522 data[0] = 0x00;
2523 data[1] = 0x03;
2524 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2525 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002526 break;
2527 case 6000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002528 /* <Timing Recovery setting> */
2529 cxd2841er_write_regs(priv, I2C_SLVT,
2530 0x9F, nominalRate6bw[priv->xtal], 5);
2531 /* Group delay equaliser settings for
2532 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2533 */
2534 cxd2841er_write_regs(priv, I2C_SLVT,
2535 0xA6, itbCoef6bw[priv->xtal], 14);
2536 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002537 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3600000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002538 data[0] = (u8) ((iffreq >> 16) & 0xff);
2539 data[1] = (u8)((iffreq >> 8) & 0xff);
2540 data[2] = (u8)(iffreq & 0xff);
2541 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2542 /* System bandwidth setting */
2543 cxd2841er_set_reg_bits(
2544 priv, I2C_SLVT, 0xD7, 0x04, 0x07);
2545
2546 /* Demod core latency setting */
2547 if (priv->xtal == SONY_XTAL_24000) {
2548 data[0] = 0x25;
2549 data[1] = 0x4C;
2550 } else {
2551 data[0] = 0x1F;
2552 data[1] = 0xDC;
2553 }
2554 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2555
2556 /* Notch filter setting */
2557 data[0] = 0x00;
2558 data[1] = 0x03;
2559 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2560 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002561 break;
2562 case 5000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002563 /* <Timing Recovery setting> */
2564 cxd2841er_write_regs(priv, I2C_SLVT,
2565 0x9F, nominalRate5bw[priv->xtal], 5);
2566 /* Group delay equaliser settings for
2567 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2568 */
2569 cxd2841er_write_regs(priv, I2C_SLVT,
2570 0xA6, itbCoef5bw[priv->xtal], 14);
2571 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002572 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3600000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002573 data[0] = (u8) ((iffreq >> 16) & 0xff);
2574 data[1] = (u8)((iffreq >> 8) & 0xff);
2575 data[2] = (u8)(iffreq & 0xff);
2576 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2577 /* System bandwidth setting */
2578 cxd2841er_set_reg_bits(
2579 priv, I2C_SLVT, 0xD7, 0x06, 0x07);
2580
2581 /* Demod core latency setting */
2582 if (priv->xtal == SONY_XTAL_24000) {
2583 data[0] = 0x2C;
2584 data[1] = 0xC2;
2585 } else {
2586 data[0] = 0x26;
2587 data[1] = 0x3C;
2588 }
2589 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2590
2591 /* Notch filter setting */
2592 data[0] = 0x00;
2593 data[1] = 0x03;
2594 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2595 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002596 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03002597 }
2598
2599 return 0;
2600}
2601
2602static int cxd2841er_sleep_tc_to_active_i_band(
2603 struct cxd2841er_priv *priv, u32 bandwidth)
2604{
2605 u32 iffreq;
2606 u8 data[3];
2607
2608 /* TRCG Nominal Rate */
2609 u8 nominalRate8bw[3][5] = {
2610 {0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2611 {0x11, 0xB8, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2612 {0x00, 0x00, 0x00, 0x00, 0x00} /* 41MHz XTal */
2613 };
2614
2615 u8 nominalRate7bw[3][5] = {
2616 {0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2617 {0x14, 0x40, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2618 {0x00, 0x00, 0x00, 0x00, 0x00} /* 41MHz XTal */
2619 };
2620
2621 u8 nominalRate6bw[3][5] = {
2622 {0x14, 0x2E, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2623 {0x17, 0xA0, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2624 {0x14, 0x2E, 0x00, 0x00, 0x00} /* 41MHz XTal */
2625 };
2626
2627 u8 itbCoef8bw[3][14] = {
2628 {0x00}, /* 20.5MHz XTal */
2629 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29,
2630 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz Xtal */
2631 {0x0}, /* 41MHz XTal */
2632 };
2633
2634 u8 itbCoef7bw[3][14] = {
2635 {0x00}, /* 20.5MHz XTal */
2636 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29,
2637 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz Xtal */
2638 {0x00}, /* 41MHz XTal */
2639 };
2640
2641 u8 itbCoef6bw[3][14] = {
2642 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
2643 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2644 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29,
2645 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz Xtal */
2646 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
2647 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 41MHz XTal */
2648 };
2649
2650 dev_dbg(&priv->i2c->dev, "%s() bandwidth=%u\n", __func__, bandwidth);
2651 /* Set SLV-T Bank : 0x10 */
2652 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2653
2654 /* 20.5/41MHz Xtal support is not available
2655 * on ISDB-T 7MHzBW and 8MHzBW
2656 */
2657 if (priv->xtal != SONY_XTAL_24000 && bandwidth > 6000000) {
2658 dev_err(&priv->i2c->dev,
2659 "%s(): bandwidth %d supported only for 24MHz xtal\n",
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002660 __func__, bandwidth);
2661 return -EINVAL;
2662 }
Abylay Ospan83808c22016-03-22 19:20:34 -03002663
2664 switch (bandwidth) {
2665 case 8000000:
2666 /* TRCG Nominal Rate */
2667 cxd2841er_write_regs(priv, I2C_SLVT,
2668 0x9F, nominalRate8bw[priv->xtal], 5);
2669 /* Group delay equaliser settings for ASCOT tuners optimized */
2670 cxd2841er_write_regs(priv, I2C_SLVT,
2671 0xA6, itbCoef8bw[priv->xtal], 14);
2672
2673 /* IF freq setting */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002674 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4750000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002675 data[0] = (u8) ((iffreq >> 16) & 0xff);
2676 data[1] = (u8)((iffreq >> 8) & 0xff);
2677 data[2] = (u8)(iffreq & 0xff);
2678 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2679
2680 /* System bandwidth setting */
2681 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x0, 0x7);
2682
2683 /* Demod core latency setting */
2684 data[0] = 0x13;
2685 data[1] = 0xFC;
2686 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2687
2688 /* Acquisition optimization setting */
2689 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2690 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
2691 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2692 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x03);
2693 break;
2694 case 7000000:
2695 /* TRCG Nominal Rate */
2696 cxd2841er_write_regs(priv, I2C_SLVT,
2697 0x9F, nominalRate7bw[priv->xtal], 5);
2698 /* Group delay equaliser settings for ASCOT tuners optimized */
2699 cxd2841er_write_regs(priv, I2C_SLVT,
2700 0xA6, itbCoef7bw[priv->xtal], 14);
2701
2702 /* IF freq setting */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002703 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4150000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002704 data[0] = (u8) ((iffreq >> 16) & 0xff);
2705 data[1] = (u8)((iffreq >> 8) & 0xff);
2706 data[2] = (u8)(iffreq & 0xff);
2707 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2708
2709 /* System bandwidth setting */
2710 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x02, 0x7);
2711
2712 /* Demod core latency setting */
2713 data[0] = 0x1A;
2714 data[1] = 0xFA;
2715 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2716
2717 /* Acquisition optimization setting */
2718 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2719 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
2720 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2721 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
2722 break;
2723 case 6000000:
2724 /* TRCG Nominal Rate */
2725 cxd2841er_write_regs(priv, I2C_SLVT,
2726 0x9F, nominalRate6bw[priv->xtal], 5);
2727 /* Group delay equaliser settings for ASCOT tuners optimized */
2728 cxd2841er_write_regs(priv, I2C_SLVT,
2729 0xA6, itbCoef6bw[priv->xtal], 14);
2730
2731 /* IF freq setting */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002732 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3550000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002733 data[0] = (u8) ((iffreq >> 16) & 0xff);
2734 data[1] = (u8)((iffreq >> 8) & 0xff);
2735 data[2] = (u8)(iffreq & 0xff);
2736 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2737
2738 /* System bandwidth setting */
2739 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x04, 0x7);
2740
2741 /* Demod core latency setting */
2742 if (priv->xtal == SONY_XTAL_24000) {
2743 data[0] = 0x1F;
2744 data[1] = 0x79;
2745 } else {
2746 data[0] = 0x1A;
2747 data[1] = 0xE2;
2748 }
2749 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2750
2751 /* Acquisition optimization setting */
2752 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2753 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x07, 0x07);
2754 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2755 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
2756 break;
2757 default:
2758 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
2759 __func__, bandwidth);
2760 return -EINVAL;
2761 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002762 return 0;
2763}
2764
2765static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
2766 u32 bandwidth)
2767{
2768 u8 bw7_8mhz_b10_a6[] = {
2769 0x2D, 0xC7, 0x04, 0xF4, 0x07, 0xC5, 0x2A, 0xB8,
2770 0x27, 0x9E, 0x27, 0xA4, 0x29, 0xAB };
2771 u8 bw6mhz_b10_a6[] = {
2772 0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2773 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4 };
2774 u8 b10_b6[3];
2775 u32 iffreq;
2776
Abylay Ospanaf4cc462016-07-21 10:56:25 -03002777 if (bandwidth != 6000000 &&
2778 bandwidth != 7000000 &&
2779 bandwidth != 8000000) {
2780 dev_info(&priv->i2c->dev, "%s(): unsupported bandwidth %d. Forcing 8Mhz!\n",
2781 __func__, bandwidth);
2782 bandwidth = 8000000;
2783 }
2784
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03002785 dev_dbg(&priv->i2c->dev, "%s() bw=%d\n", __func__, bandwidth);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002786 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2787 switch (bandwidth) {
2788 case 8000000:
2789 case 7000000:
2790 cxd2841er_write_regs(
2791 priv, I2C_SLVT, 0xa6,
2792 bw7_8mhz_b10_a6, sizeof(bw7_8mhz_b10_a6));
Daniel Schellercbc85a42017-04-09 16:38:14 -03002793 iffreq = cxd2841er_calc_iffreq(4900000);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002794 break;
2795 case 6000000:
2796 cxd2841er_write_regs(
2797 priv, I2C_SLVT, 0xa6,
2798 bw6mhz_b10_a6, sizeof(bw6mhz_b10_a6));
Daniel Schellercbc85a42017-04-09 16:38:14 -03002799 iffreq = cxd2841er_calc_iffreq(3700000);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002800 break;
2801 default:
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03002802 dev_err(&priv->i2c->dev, "%s(): unsupported bandwidth %d\n",
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002803 __func__, bandwidth);
2804 return -EINVAL;
2805 }
2806 /* <IF freq setting> */
2807 b10_b6[0] = (u8) ((iffreq >> 16) & 0xff);
2808 b10_b6[1] = (u8)((iffreq >> 8) & 0xff);
2809 b10_b6[2] = (u8)(iffreq & 0xff);
2810 cxd2841er_write_regs(priv, I2C_SLVT, 0xb6, b10_b6, sizeof(b10_b6));
2811 /* Set SLV-T Bank : 0x11 */
2812 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2813 switch (bandwidth) {
2814 case 8000000:
2815 case 7000000:
2816 cxd2841er_set_reg_bits(
2817 priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
2818 break;
2819 case 6000000:
2820 cxd2841er_set_reg_bits(
2821 priv, I2C_SLVT, 0xa3, 0x14, 0x1f);
2822 break;
2823 }
2824 /* Set SLV-T Bank : 0x40 */
2825 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
2826 switch (bandwidth) {
2827 case 8000000:
2828 cxd2841er_set_reg_bits(
2829 priv, I2C_SLVT, 0x26, 0x0b, 0x0f);
2830 cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0x3e);
2831 break;
2832 case 7000000:
2833 cxd2841er_set_reg_bits(
2834 priv, I2C_SLVT, 0x26, 0x09, 0x0f);
2835 cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0xd6);
2836 break;
2837 case 6000000:
2838 cxd2841er_set_reg_bits(
2839 priv, I2C_SLVT, 0x26, 0x08, 0x0f);
2840 cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0x6e);
2841 break;
2842 }
2843 return 0;
2844}
2845
2846static int cxd2841er_sleep_tc_to_active_t(struct cxd2841er_priv *priv,
2847 u32 bandwidth)
2848{
2849 u8 data[2] = { 0x09, 0x54 };
Abylay Ospan83808c22016-03-22 19:20:34 -03002850 u8 data24m[3] = {0xDC, 0x6C, 0x00};
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002851
2852 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2853 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
2854 /* Set SLV-X Bank : 0x00 */
2855 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
2856 /* Set demod mode */
2857 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
2858 /* Set SLV-T Bank : 0x00 */
2859 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2860 /* Enable demod clock */
2861 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
2862 /* Disable RF level monitor */
2863 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
2864 /* Enable ADC clock */
2865 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
2866 /* Enable ADC 1 */
2867 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
Abylay Ospan83808c22016-03-22 19:20:34 -03002868 /* Enable ADC 2 & 3 */
2869 if (priv->xtal == SONY_XTAL_41000) {
2870 data[0] = 0x0A;
2871 data[1] = 0xD4;
2872 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002873 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
2874 /* Enable ADC 4 */
2875 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
2876 /* Set SLV-T Bank : 0x10 */
2877 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2878 /* IFAGC gain settings */
2879 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
2880 /* Set SLV-T Bank : 0x11 */
2881 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2882 /* BBAGC TARGET level setting */
2883 cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
2884 /* Set SLV-T Bank : 0x10 */
2885 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2886 /* ASCOT setting ON */
2887 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
2888 /* Set SLV-T Bank : 0x18 */
2889 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
2890 /* Pre-RS BER moniter setting */
2891 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x36, 0x40, 0x07);
2892 /* FEC Auto Recovery setting */
2893 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
2894 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x01, 0x01);
2895 /* Set SLV-T Bank : 0x00 */
2896 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2897 /* TSIF setting */
2898 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
2899 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
Abylay Ospan83808c22016-03-22 19:20:34 -03002900
2901 if (priv->xtal == SONY_XTAL_24000) {
2902 /* Set SLV-T Bank : 0x10 */
2903 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2904 cxd2841er_write_reg(priv, I2C_SLVT, 0xBF, 0x60);
2905 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
2906 cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data24m, 3);
2907 }
2908
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002909 cxd2841er_sleep_tc_to_active_t_band(priv, bandwidth);
2910 /* Set SLV-T Bank : 0x00 */
2911 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2912 /* Disable HiZ Setting 1 */
2913 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
2914 /* Disable HiZ Setting 2 */
2915 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
2916 priv->state = STATE_ACTIVE_TC;
2917 return 0;
2918}
2919
2920static int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv,
2921 u32 bandwidth)
2922{
Abylay Ospan6c771612016-05-16 11:43:25 -03002923 u8 data[MAX_WRITE_REGSIZE];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002924
2925 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2926 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT2);
2927 /* Set SLV-X Bank : 0x00 */
2928 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
2929 /* Set demod mode */
2930 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x02);
2931 /* Set SLV-T Bank : 0x00 */
2932 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2933 /* Enable demod clock */
2934 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
2935 /* Disable RF level monitor */
Abylay Ospan6c771612016-05-16 11:43:25 -03002936 cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002937 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
2938 /* Enable ADC clock */
2939 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
2940 /* Enable ADC 1 */
2941 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
Abylay Ospan6c771612016-05-16 11:43:25 -03002942
2943 if (priv->xtal == SONY_XTAL_41000) {
2944 data[0] = 0x0A;
2945 data[1] = 0xD4;
2946 } else {
2947 data[0] = 0x09;
2948 data[1] = 0x54;
2949 }
2950
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002951 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
2952 /* Enable ADC 4 */
2953 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
2954 /* Set SLV-T Bank : 0x10 */
2955 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2956 /* IFAGC gain settings */
2957 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
2958 /* Set SLV-T Bank : 0x11 */
2959 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2960 /* BBAGC TARGET level setting */
2961 cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
2962 /* Set SLV-T Bank : 0x10 */
2963 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2964 /* ASCOT setting ON */
2965 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
2966 /* Set SLV-T Bank : 0x20 */
2967 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
2968 /* Acquisition optimization setting */
2969 cxd2841er_write_reg(priv, I2C_SLVT, 0x8b, 0x3c);
2970 /* Set SLV-T Bank : 0x2b */
2971 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2972 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x76, 0x20, 0x70);
Abylay Ospan6c771612016-05-16 11:43:25 -03002973 /* Set SLV-T Bank : 0x23 */
2974 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
2975 /* L1 Control setting */
2976 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE6, 0x00, 0x03);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002977 /* Set SLV-T Bank : 0x00 */
2978 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2979 /* TSIF setting */
2980 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
2981 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
2982 /* DVB-T2 initial setting */
2983 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
2984 cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x10);
2985 cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x34);
2986 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
2987 cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xd8);
2988 /* Set SLV-T Bank : 0x2a */
2989 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
2990 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x04, 0x0f);
2991 /* Set SLV-T Bank : 0x2b */
2992 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2993 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x20, 0x3f);
2994
Abylay Ospan6c771612016-05-16 11:43:25 -03002995 /* 24MHz Xtal setting */
2996 if (priv->xtal == SONY_XTAL_24000) {
2997 /* Set SLV-T Bank : 0x11 */
2998 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2999 data[0] = 0xEB;
3000 data[1] = 0x03;
3001 data[2] = 0x3B;
3002 cxd2841er_write_regs(priv, I2C_SLVT, 0x33, data, 3);
3003
3004 /* Set SLV-T Bank : 0x20 */
3005 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
3006 data[0] = 0x5E;
3007 data[1] = 0x5E;
3008 data[2] = 0x47;
3009 cxd2841er_write_regs(priv, I2C_SLVT, 0x95, data, 3);
3010
3011 cxd2841er_write_reg(priv, I2C_SLVT, 0x99, 0x18);
3012
3013 data[0] = 0x3F;
3014 data[1] = 0xFF;
3015 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
3016
3017 /* Set SLV-T Bank : 0x24 */
3018 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
3019 data[0] = 0x0B;
3020 data[1] = 0x72;
3021 cxd2841er_write_regs(priv, I2C_SLVT, 0x34, data, 2);
3022
3023 data[0] = 0x93;
3024 data[1] = 0xF3;
3025 data[2] = 0x00;
3026 cxd2841er_write_regs(priv, I2C_SLVT, 0xD2, data, 3);
3027
3028 data[0] = 0x05;
3029 data[1] = 0xB8;
3030 data[2] = 0xD8;
3031 cxd2841er_write_regs(priv, I2C_SLVT, 0xDD, data, 3);
3032
3033 cxd2841er_write_reg(priv, I2C_SLVT, 0xE0, 0x00);
3034
3035 /* Set SLV-T Bank : 0x25 */
3036 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x25);
3037 cxd2841er_write_reg(priv, I2C_SLVT, 0xED, 0x60);
3038
3039 /* Set SLV-T Bank : 0x27 */
3040 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
3041 cxd2841er_write_reg(priv, I2C_SLVT, 0xFA, 0x34);
3042
3043 /* Set SLV-T Bank : 0x2B */
3044 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2B);
3045 cxd2841er_write_reg(priv, I2C_SLVT, 0x4B, 0x2F);
3046 cxd2841er_write_reg(priv, I2C_SLVT, 0x9E, 0x0E);
3047
3048 /* Set SLV-T Bank : 0x2D */
3049 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2D);
3050 data[0] = 0x89;
3051 data[1] = 0x89;
3052 cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data, 2);
3053
3054 /* Set SLV-T Bank : 0x5E */
3055 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x5E);
3056 data[0] = 0x24;
3057 data[1] = 0x95;
3058 cxd2841er_write_regs(priv, I2C_SLVT, 0x8C, data, 2);
3059 }
3060
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003061 cxd2841er_sleep_tc_to_active_t2_band(priv, bandwidth);
3062
3063 /* Set SLV-T Bank : 0x00 */
3064 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3065 /* Disable HiZ Setting 1 */
3066 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3067 /* Disable HiZ Setting 2 */
3068 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3069 priv->state = STATE_ACTIVE_TC;
3070 return 0;
3071}
3072
Abylay Ospan83808c22016-03-22 19:20:34 -03003073/* ISDB-Tb part */
3074static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
3075 u32 bandwidth)
3076{
3077 u8 data[2] = { 0x09, 0x54 };
3078 u8 data24m[2] = {0x60, 0x00};
3079 u8 data24m2[3] = {0xB7, 0x1B, 0x00};
3080
3081 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3082 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
3083 /* Set SLV-X Bank : 0x00 */
3084 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
3085 /* Set demod mode */
3086 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x06);
3087 /* Set SLV-T Bank : 0x00 */
3088 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3089 /* Enable demod clock */
3090 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
3091 /* Enable RF level monitor */
3092 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x01);
3093 cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x01);
3094 /* Enable ADC clock */
3095 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
3096 /* Enable ADC 1 */
3097 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
3098 /* xtal freq 20.5MHz or 24M */
3099 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
3100 /* Enable ADC 4 */
3101 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
3102 /* ASCOT setting ON */
3103 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
3104 /* FEC Auto Recovery setting */
3105 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
3106 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x00, 0x01);
3107 /* ISDB-T initial setting */
3108 /* Set SLV-T Bank : 0x00 */
3109 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3110 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x00, 0x01);
3111 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x00, 0x01);
3112 /* Set SLV-T Bank : 0x10 */
3113 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3114 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x69, 0x04, 0x07);
3115 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x6B, 0x03, 0x07);
3116 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9D, 0x50, 0xFF);
3117 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xD3, 0x06, 0x1F);
3118 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xED, 0x00, 0x01);
3119 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE2, 0xCE, 0x80);
3120 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xF2, 0x13, 0x10);
3121 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x2E, 0x3F);
3122 /* Set SLV-T Bank : 0x15 */
3123 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
3124 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x02, 0x03);
3125 /* Set SLV-T Bank : 0x1E */
3126 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x1E);
3127 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x73, 0x68, 0xFF);
3128 /* Set SLV-T Bank : 0x63 */
3129 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x63);
3130 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x81, 0x00, 0x01);
3131
3132 /* for xtal 24MHz */
3133 /* Set SLV-T Bank : 0x10 */
3134 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3135 cxd2841er_write_regs(priv, I2C_SLVT, 0xBF, data24m, 2);
3136 /* Set SLV-T Bank : 0x60 */
3137 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
3138 cxd2841er_write_regs(priv, I2C_SLVT, 0xA8, data24m2, 3);
3139
3140 cxd2841er_sleep_tc_to_active_i_band(priv, bandwidth);
3141 /* Set SLV-T Bank : 0x00 */
3142 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3143 /* Disable HiZ Setting 1 */
3144 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3145 /* Disable HiZ Setting 2 */
3146 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3147 priv->state = STATE_ACTIVE_TC;
3148 return 0;
3149}
3150
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003151static int cxd2841er_sleep_tc_to_active_c(struct cxd2841er_priv *priv,
3152 u32 bandwidth)
3153{
3154 u8 data[2] = { 0x09, 0x54 };
3155
3156 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3157 cxd2841er_set_ts_clock_mode(priv, SYS_DVBC_ANNEX_A);
3158 /* Set SLV-X Bank : 0x00 */
3159 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
3160 /* Set demod mode */
3161 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x04);
3162 /* Set SLV-T Bank : 0x00 */
3163 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3164 /* Enable demod clock */
3165 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
3166 /* Disable RF level monitor */
Abylay Ospan4a86bc12016-07-19 00:10:20 -03003167 cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003168 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
3169 /* Enable ADC clock */
3170 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
3171 /* Enable ADC 1 */
3172 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
3173 /* xtal freq 20.5MHz */
3174 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
3175 /* Enable ADC 4 */
3176 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
3177 /* Set SLV-T Bank : 0x10 */
3178 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3179 /* IFAGC gain settings */
3180 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x09, 0x1f);
3181 /* Set SLV-T Bank : 0x11 */
3182 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
3183 /* BBAGC TARGET level setting */
3184 cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x48);
3185 /* Set SLV-T Bank : 0x10 */
3186 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3187 /* ASCOT setting ON */
3188 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
3189 /* Set SLV-T Bank : 0x40 */
3190 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
3191 /* Demod setting */
3192 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc3, 0x00, 0x04);
3193 /* Set SLV-T Bank : 0x00 */
3194 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3195 /* TSIF setting */
3196 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
3197 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
3198
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003199 cxd2841er_sleep_tc_to_active_c_band(priv, bandwidth);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003200 /* Set SLV-T Bank : 0x00 */
3201 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3202 /* Disable HiZ Setting 1 */
3203 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3204 /* Disable HiZ Setting 2 */
3205 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3206 priv->state = STATE_ACTIVE_TC;
3207 return 0;
3208}
3209
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02003210static int cxd2841er_get_frontend(struct dvb_frontend *fe,
3211 struct dtv_frontend_properties *p)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003212{
3213 enum fe_status status = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003214 struct cxd2841er_priv *priv = fe->demodulator_priv;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003215
3216 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3217 if (priv->state == STATE_ACTIVE_S)
3218 cxd2841er_read_status_s(fe, &status);
3219 else if (priv->state == STATE_ACTIVE_TC)
3220 cxd2841er_read_status_tc(fe, &status);
3221
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03003222 cxd2841er_read_signal_strength(fe);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003223
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003224 if (status & FE_HAS_LOCK) {
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03003225 cxd2841er_read_snr(fe);
3226 cxd2841er_read_ucblocks(fe);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003227
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03003228 cxd2841er_read_ber(fe);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003229 } else {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003230 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003231 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003232 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03003233 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003234 }
3235 return 0;
3236}
3237
3238static int cxd2841er_set_frontend_s(struct dvb_frontend *fe)
3239{
3240 int ret = 0, i, timeout, carr_offset;
3241 enum fe_status status;
3242 struct cxd2841er_priv *priv = fe->demodulator_priv;
3243 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3244 u32 symbol_rate = p->symbol_rate/1000;
3245
Abylay Ospan83808c22016-03-22 19:20:34 -03003246 dev_dbg(&priv->i2c->dev, "%s(): %s frequency=%d symbol_rate=%d xtal=%d\n",
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003247 __func__,
3248 (p->delivery_system == SYS_DVBS ? "DVB-S" : "DVB-S2"),
Abylay Ospan83808c22016-03-22 19:20:34 -03003249 p->frequency, symbol_rate, priv->xtal);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003250 switch (priv->state) {
3251 case STATE_SLEEP_S:
3252 ret = cxd2841er_sleep_s_to_active_s(
3253 priv, p->delivery_system, symbol_rate);
3254 break;
3255 case STATE_ACTIVE_S:
3256 ret = cxd2841er_retune_active(priv, p);
3257 break;
3258 default:
3259 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3260 __func__, priv->state);
3261 ret = -EINVAL;
3262 goto done;
3263 }
3264 if (ret) {
3265 dev_dbg(&priv->i2c->dev, "%s(): tune failed\n", __func__);
3266 goto done;
3267 }
Daniel Schellerc7518d12017-04-09 16:38:16 -03003268
3269 cxd2841er_tuner_set(fe);
3270
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003271 cxd2841er_tune_done(priv);
3272 timeout = ((3000000 + (symbol_rate - 1)) / symbol_rate) + 150;
3273 for (i = 0; i < timeout / CXD2841ER_DVBS_POLLING_INVL; i++) {
3274 usleep_range(CXD2841ER_DVBS_POLLING_INVL*1000,
3275 (CXD2841ER_DVBS_POLLING_INVL + 2) * 1000);
3276 cxd2841er_read_status_s(fe, &status);
3277 if (status & FE_HAS_LOCK)
3278 break;
3279 }
3280 if (status & FE_HAS_LOCK) {
3281 if (cxd2841er_get_carrier_offset_s_s2(
3282 priv, &carr_offset)) {
3283 ret = -EINVAL;
3284 goto done;
3285 }
3286 dev_dbg(&priv->i2c->dev, "%s(): carrier_offset=%d\n",
3287 __func__, carr_offset);
3288 }
3289done:
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003290 /* Reset stats */
3291 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
3292 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3293 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3294 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03003295 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003296
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003297 return ret;
3298}
3299
3300static int cxd2841er_set_frontend_tc(struct dvb_frontend *fe)
3301{
3302 int ret = 0, timeout;
3303 enum fe_status status;
3304 struct cxd2841er_priv *priv = fe->demodulator_priv;
3305 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3306
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003307 dev_dbg(&priv->i2c->dev, "%s() delivery_system=%d bandwidth_hz=%d\n",
3308 __func__, p->delivery_system, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003309 if (p->delivery_system == SYS_DVBT) {
3310 priv->system = SYS_DVBT;
3311 switch (priv->state) {
3312 case STATE_SLEEP_TC:
3313 ret = cxd2841er_sleep_tc_to_active_t(
3314 priv, p->bandwidth_hz);
3315 break;
3316 case STATE_ACTIVE_TC:
3317 ret = cxd2841er_retune_active(priv, p);
3318 break;
3319 default:
3320 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3321 __func__, priv->state);
3322 ret = -EINVAL;
3323 }
3324 } else if (p->delivery_system == SYS_DVBT2) {
3325 priv->system = SYS_DVBT2;
3326 cxd2841er_dvbt2_set_plp_config(priv,
3327 (int)(p->stream_id > 255), p->stream_id);
3328 cxd2841er_dvbt2_set_profile(priv, DVBT2_PROFILE_BASE);
3329 switch (priv->state) {
3330 case STATE_SLEEP_TC:
3331 ret = cxd2841er_sleep_tc_to_active_t2(priv,
3332 p->bandwidth_hz);
3333 break;
3334 case STATE_ACTIVE_TC:
3335 ret = cxd2841er_retune_active(priv, p);
3336 break;
3337 default:
3338 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3339 __func__, priv->state);
3340 ret = -EINVAL;
3341 }
Abylay Ospan83808c22016-03-22 19:20:34 -03003342 } else if (p->delivery_system == SYS_ISDBT) {
3343 priv->system = SYS_ISDBT;
3344 switch (priv->state) {
3345 case STATE_SLEEP_TC:
3346 ret = cxd2841er_sleep_tc_to_active_i(
3347 priv, p->bandwidth_hz);
3348 break;
3349 case STATE_ACTIVE_TC:
3350 ret = cxd2841er_retune_active(priv, p);
3351 break;
3352 default:
3353 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3354 __func__, priv->state);
3355 ret = -EINVAL;
3356 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003357 } else if (p->delivery_system == SYS_DVBC_ANNEX_A ||
3358 p->delivery_system == SYS_DVBC_ANNEX_C) {
3359 priv->system = SYS_DVBC_ANNEX_A;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003360 /* correct bandwidth */
3361 if (p->bandwidth_hz != 6000000 &&
3362 p->bandwidth_hz != 7000000 &&
3363 p->bandwidth_hz != 8000000) {
3364 p->bandwidth_hz = 8000000;
3365 dev_dbg(&priv->i2c->dev, "%s(): forcing bandwidth to %d\n",
3366 __func__, p->bandwidth_hz);
3367 }
3368
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003369 switch (priv->state) {
3370 case STATE_SLEEP_TC:
3371 ret = cxd2841er_sleep_tc_to_active_c(
3372 priv, p->bandwidth_hz);
3373 break;
3374 case STATE_ACTIVE_TC:
3375 ret = cxd2841er_retune_active(priv, p);
3376 break;
3377 default:
3378 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3379 __func__, priv->state);
3380 ret = -EINVAL;
3381 }
3382 } else {
3383 dev_dbg(&priv->i2c->dev,
3384 "%s(): invalid delivery system %d\n",
3385 __func__, p->delivery_system);
3386 ret = -EINVAL;
3387 }
3388 if (ret)
3389 goto done;
Daniel Schellerc7518d12017-04-09 16:38:16 -03003390
3391 cxd2841er_tuner_set(fe);
3392
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003393 cxd2841er_tune_done(priv);
3394 timeout = 2500;
3395 while (timeout > 0) {
3396 ret = cxd2841er_read_status_tc(fe, &status);
3397 if (ret)
3398 goto done;
3399 if (status & FE_HAS_LOCK)
3400 break;
3401 msleep(20);
3402 timeout -= 20;
3403 }
3404 if (timeout < 0)
3405 dev_dbg(&priv->i2c->dev,
3406 "%s(): LOCK wait timeout\n", __func__);
3407done:
3408 return ret;
3409}
3410
3411static int cxd2841er_tune_s(struct dvb_frontend *fe,
3412 bool re_tune,
3413 unsigned int mode_flags,
3414 unsigned int *delay,
3415 enum fe_status *status)
3416{
3417 int ret, carrier_offset;
3418 struct cxd2841er_priv *priv = fe->demodulator_priv;
3419 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3420
3421 dev_dbg(&priv->i2c->dev, "%s() re_tune=%d\n", __func__, re_tune);
3422 if (re_tune) {
3423 ret = cxd2841er_set_frontend_s(fe);
3424 if (ret)
3425 return ret;
3426 cxd2841er_read_status_s(fe, status);
3427 if (*status & FE_HAS_LOCK) {
3428 if (cxd2841er_get_carrier_offset_s_s2(
3429 priv, &carrier_offset))
3430 return -EINVAL;
3431 p->frequency += carrier_offset;
3432 ret = cxd2841er_set_frontend_s(fe);
3433 if (ret)
3434 return ret;
3435 }
3436 }
3437 *delay = HZ / 5;
3438 return cxd2841er_read_status_s(fe, status);
3439}
3440
3441static int cxd2841er_tune_tc(struct dvb_frontend *fe,
3442 bool re_tune,
3443 unsigned int mode_flags,
3444 unsigned int *delay,
3445 enum fe_status *status)
3446{
3447 int ret, carrier_offset;
3448 struct cxd2841er_priv *priv = fe->demodulator_priv;
3449 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3450
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003451 dev_dbg(&priv->i2c->dev, "%s(): re_tune %d bandwidth=%d\n", __func__,
3452 re_tune, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003453 if (re_tune) {
3454 ret = cxd2841er_set_frontend_tc(fe);
3455 if (ret)
3456 return ret;
3457 cxd2841er_read_status_tc(fe, status);
3458 if (*status & FE_HAS_LOCK) {
3459 switch (priv->system) {
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -03003460 case SYS_ISDBT:
3461 ret = cxd2841er_get_carrier_offset_i(
3462 priv, p->bandwidth_hz,
3463 &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003464 if (ret)
3465 return ret;
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -03003466 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003467 case SYS_DVBT:
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03003468 ret = cxd2841er_get_carrier_offset_t(
3469 priv, p->bandwidth_hz,
3470 &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003471 if (ret)
3472 return ret;
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03003473 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003474 case SYS_DVBT2:
3475 ret = cxd2841er_get_carrier_offset_t2(
3476 priv, p->bandwidth_hz,
3477 &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003478 if (ret)
3479 return ret;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003480 break;
3481 case SYS_DVBC_ANNEX_A:
3482 ret = cxd2841er_get_carrier_offset_c(
3483 priv, &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003484 if (ret)
3485 return ret;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003486 break;
3487 default:
3488 dev_dbg(&priv->i2c->dev,
3489 "%s(): invalid delivery system %d\n",
3490 __func__, priv->system);
3491 return -EINVAL;
3492 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003493 dev_dbg(&priv->i2c->dev, "%s(): carrier offset %d\n",
3494 __func__, carrier_offset);
3495 p->frequency += carrier_offset;
3496 ret = cxd2841er_set_frontend_tc(fe);
3497 if (ret)
3498 return ret;
3499 }
3500 }
3501 *delay = HZ / 5;
3502 return cxd2841er_read_status_tc(fe, status);
3503}
3504
3505static int cxd2841er_sleep_s(struct dvb_frontend *fe)
3506{
3507 struct cxd2841er_priv *priv = fe->demodulator_priv;
3508
3509 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3510 cxd2841er_active_s_to_sleep_s(fe->demodulator_priv);
3511 cxd2841er_sleep_s_to_shutdown(fe->demodulator_priv);
3512 return 0;
3513}
3514
3515static int cxd2841er_sleep_tc(struct dvb_frontend *fe)
3516{
3517 struct cxd2841er_priv *priv = fe->demodulator_priv;
3518
3519 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3520 if (priv->state == STATE_ACTIVE_TC) {
3521 switch (priv->system) {
3522 case SYS_DVBT:
3523 cxd2841er_active_t_to_sleep_tc(priv);
3524 break;
3525 case SYS_DVBT2:
3526 cxd2841er_active_t2_to_sleep_tc(priv);
3527 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03003528 case SYS_ISDBT:
3529 cxd2841er_active_i_to_sleep_tc(priv);
3530 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003531 case SYS_DVBC_ANNEX_A:
3532 cxd2841er_active_c_to_sleep_tc(priv);
3533 break;
3534 default:
3535 dev_warn(&priv->i2c->dev,
3536 "%s(): unknown delivery system %d\n",
3537 __func__, priv->system);
3538 }
3539 }
3540 if (priv->state != STATE_SLEEP_TC) {
3541 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
3542 __func__, priv->state);
3543 return -EINVAL;
3544 }
3545 cxd2841er_sleep_tc_to_shutdown(priv);
3546 return 0;
3547}
3548
3549static int cxd2841er_send_burst(struct dvb_frontend *fe,
3550 enum fe_sec_mini_cmd burst)
3551{
3552 u8 data;
3553 struct cxd2841er_priv *priv = fe->demodulator_priv;
3554
3555 dev_dbg(&priv->i2c->dev, "%s(): burst mode %s\n", __func__,
3556 (burst == SEC_MINI_A ? "A" : "B"));
3557 if (priv->state != STATE_SLEEP_S &&
3558 priv->state != STATE_ACTIVE_S) {
3559 dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3560 __func__, priv->state);
3561 return -EINVAL;
3562 }
3563 data = (burst == SEC_MINI_A ? 0 : 1);
3564 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3565 cxd2841er_write_reg(priv, I2C_SLVT, 0x34, 0x01);
3566 cxd2841er_write_reg(priv, I2C_SLVT, 0x35, data);
3567 return 0;
3568}
3569
3570static int cxd2841er_set_tone(struct dvb_frontend *fe,
3571 enum fe_sec_tone_mode tone)
3572{
3573 u8 data;
3574 struct cxd2841er_priv *priv = fe->demodulator_priv;
3575
3576 dev_dbg(&priv->i2c->dev, "%s(): tone %s\n", __func__,
3577 (tone == SEC_TONE_ON ? "On" : "Off"));
3578 if (priv->state != STATE_SLEEP_S &&
3579 priv->state != STATE_ACTIVE_S) {
3580 dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3581 __func__, priv->state);
3582 return -EINVAL;
3583 }
3584 data = (tone == SEC_TONE_ON ? 1 : 0);
3585 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3586 cxd2841er_write_reg(priv, I2C_SLVT, 0x36, data);
3587 return 0;
3588}
3589
3590static int cxd2841er_send_diseqc_msg(struct dvb_frontend *fe,
3591 struct dvb_diseqc_master_cmd *cmd)
3592{
3593 int i;
3594 u8 data[12];
3595 struct cxd2841er_priv *priv = fe->demodulator_priv;
3596
3597 if (priv->state != STATE_SLEEP_S &&
3598 priv->state != STATE_ACTIVE_S) {
3599 dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3600 __func__, priv->state);
3601 return -EINVAL;
3602 }
3603 dev_dbg(&priv->i2c->dev,
3604 "%s(): cmd->len %d\n", __func__, cmd->msg_len);
3605 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3606 /* DiDEqC enable */
3607 cxd2841er_write_reg(priv, I2C_SLVT, 0x33, 0x01);
3608 /* cmd1 length & data */
3609 cxd2841er_write_reg(priv, I2C_SLVT, 0x3d, cmd->msg_len);
3610 memset(data, 0, sizeof(data));
3611 for (i = 0; i < cmd->msg_len && i < sizeof(data); i++)
3612 data[i] = cmd->msg[i];
3613 cxd2841er_write_regs(priv, I2C_SLVT, 0x3e, data, sizeof(data));
3614 /* repeat count for cmd1 */
3615 cxd2841er_write_reg(priv, I2C_SLVT, 0x37, 1);
3616 /* repeat count for cmd2: always 0 */
3617 cxd2841er_write_reg(priv, I2C_SLVT, 0x38, 0);
3618 /* start transmit */
3619 cxd2841er_write_reg(priv, I2C_SLVT, 0x32, 0x01);
3620 /* wait for 1 sec timeout */
3621 for (i = 0; i < 50; i++) {
3622 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, data);
3623 if (!data[0]) {
3624 dev_dbg(&priv->i2c->dev,
3625 "%s(): DiSEqC cmd has been sent\n", __func__);
3626 return 0;
3627 }
3628 msleep(20);
3629 }
3630 dev_dbg(&priv->i2c->dev,
3631 "%s(): DiSEqC cmd transmit timeout\n", __func__);
3632 return -ETIMEDOUT;
3633}
3634
3635static void cxd2841er_release(struct dvb_frontend *fe)
3636{
3637 struct cxd2841er_priv *priv = fe->demodulator_priv;
3638
3639 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3640 kfree(priv);
3641}
3642
3643static int cxd2841er_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
3644{
3645 struct cxd2841er_priv *priv = fe->demodulator_priv;
3646
3647 dev_dbg(&priv->i2c->dev, "%s(): enable=%d\n", __func__, enable);
3648 cxd2841er_set_reg_bits(
3649 priv, I2C_SLVX, 0x8, (enable ? 0x01 : 0x00), 0x01);
3650 return 0;
3651}
3652
3653static enum dvbfe_algo cxd2841er_get_algo(struct dvb_frontend *fe)
3654{
3655 struct cxd2841er_priv *priv = fe->demodulator_priv;
3656
3657 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3658 return DVBFE_ALGO_HW;
3659}
3660
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003661static void cxd2841er_init_stats(struct dvb_frontend *fe)
3662{
3663 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3664
3665 p->strength.len = 1;
3666 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
3667 p->cnr.len = 1;
3668 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3669 p->block_error.len = 1;
3670 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3671 p->post_bit_error.len = 1;
3672 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03003673 p->post_bit_count.len = 1;
3674 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003675}
3676
3677
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003678static int cxd2841er_init_s(struct dvb_frontend *fe)
3679{
3680 struct cxd2841er_priv *priv = fe->demodulator_priv;
3681
Abylay Ospan30ae3302016-04-05 15:02:37 -03003682 /* sanity. force demod to SHUTDOWN state */
3683 if (priv->state == STATE_SLEEP_S) {
3684 dev_dbg(&priv->i2c->dev, "%s() forcing sleep->shutdown\n",
3685 __func__);
3686 cxd2841er_sleep_s_to_shutdown(priv);
3687 } else if (priv->state == STATE_ACTIVE_S) {
3688 dev_dbg(&priv->i2c->dev, "%s() forcing active->sleep->shutdown\n",
3689 __func__);
3690 cxd2841er_active_s_to_sleep_s(priv);
3691 cxd2841er_sleep_s_to_shutdown(priv);
3692 }
3693
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003694 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3695 cxd2841er_shutdown_to_sleep_s(priv);
3696 /* SONY_DEMOD_CONFIG_SAT_IFAGCNEG set to 1 */
3697 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
3698 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xb9, 0x01, 0x01);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003699
3700 cxd2841er_init_stats(fe);
3701
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003702 return 0;
3703}
3704
3705static int cxd2841er_init_tc(struct dvb_frontend *fe)
3706{
3707 struct cxd2841er_priv *priv = fe->demodulator_priv;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003708 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003709
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003710 dev_dbg(&priv->i2c->dev, "%s() bandwidth_hz=%d\n",
3711 __func__, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003712 cxd2841er_shutdown_to_sleep_tc(priv);
3713 /* SONY_DEMOD_CONFIG_IFAGCNEG = 1 */
3714 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3715 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcb, 0x40, 0x40);
3716 /* SONY_DEMOD_CONFIG_IFAGC_ADC_FS = 0 */
3717 cxd2841er_write_reg(priv, I2C_SLVT, 0xcd, 0x50);
3718 /* SONY_DEMOD_CONFIG_PARALLEL_SEL = 1 */
3719 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3720 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4, 0x00, 0x80);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003721
3722 cxd2841er_init_stats(fe);
3723
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003724 return 0;
3725}
3726
Max Kellermannbd336e62016-08-09 18:32:21 -03003727static const struct dvb_frontend_ops cxd2841er_dvbs_s2_ops;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003728static struct dvb_frontend_ops cxd2841er_t_c_ops;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003729
3730static struct dvb_frontend *cxd2841er_attach(struct cxd2841er_config *cfg,
3731 struct i2c_adapter *i2c,
3732 u8 system)
3733{
3734 u8 chip_id = 0;
3735 const char *type;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003736 const char *name;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003737 struct cxd2841er_priv *priv = NULL;
3738
3739 /* allocate memory for the internal state */
3740 priv = kzalloc(sizeof(struct cxd2841er_priv), GFP_KERNEL);
3741 if (!priv)
3742 return NULL;
3743 priv->i2c = i2c;
3744 priv->config = cfg;
3745 priv->i2c_addr_slvx = (cfg->i2c_addr + 4) >> 1;
3746 priv->i2c_addr_slvt = (cfg->i2c_addr) >> 1;
Abylay Ospan83808c22016-03-22 19:20:34 -03003747 priv->xtal = cfg->xtal;
Daniel Scheller050863a2017-04-09 16:38:15 -03003748 priv->flags = cfg->flags;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003749 priv->frontend.demodulator_priv = priv;
3750 dev_info(&priv->i2c->dev,
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003751 "%s(): I2C adapter %p SLVX addr %x SLVT addr %x\n",
3752 __func__, priv->i2c,
3753 priv->i2c_addr_slvx, priv->i2c_addr_slvt);
3754 chip_id = cxd2841er_chip_id(priv);
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003755 switch (chip_id) {
Daniel Scheller1ecda282017-04-09 16:38:13 -03003756 case CXD2837ER_CHIP_ID:
3757 snprintf(cxd2841er_t_c_ops.info.name, 128,
3758 "Sony CXD2837ER DVB-T/T2/C demodulator");
3759 name = "CXD2837ER";
3760 type = "C/T/T2";
3761 break;
3762 case CXD2838ER_CHIP_ID:
3763 snprintf(cxd2841er_t_c_ops.info.name, 128,
3764 "Sony CXD2838ER ISDB-T demodulator");
3765 cxd2841er_t_c_ops.delsys[0] = SYS_ISDBT;
3766 cxd2841er_t_c_ops.delsys[1] = SYS_UNDEFINED;
3767 cxd2841er_t_c_ops.delsys[2] = SYS_UNDEFINED;
3768 name = "CXD2838ER";
3769 type = "ISDB-T";
3770 break;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003771 case CXD2841ER_CHIP_ID:
3772 snprintf(cxd2841er_t_c_ops.info.name, 128,
3773 "Sony CXD2841ER DVB-T/T2/C demodulator");
3774 name = "CXD2841ER";
Daniel Scheller1ecda282017-04-09 16:38:13 -03003775 type = "T/T2/C/ISDB-T";
3776 break;
3777 case CXD2843ER_CHIP_ID:
3778 snprintf(cxd2841er_t_c_ops.info.name, 128,
3779 "Sony CXD2843ER DVB-T/T2/C/C2 demodulator");
3780 name = "CXD2843ER";
3781 type = "C/C2/T/T2";
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003782 break;
3783 case CXD2854ER_CHIP_ID:
3784 snprintf(cxd2841er_t_c_ops.info.name, 128,
3785 "Sony CXD2854ER DVB-T/T2/C and ISDB-T demodulator");
3786 cxd2841er_t_c_ops.delsys[3] = SYS_ISDBT;
3787 name = "CXD2854ER";
Daniel Scheller1ecda282017-04-09 16:38:13 -03003788 type = "C/C2/T/T2/ISDB-T";
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003789 break;
3790 default:
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003791 dev_err(&priv->i2c->dev, "%s(): invalid chip ID 0x%02x\n",
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003792 __func__, chip_id);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003793 priv->frontend.demodulator_priv = NULL;
3794 kfree(priv);
3795 return NULL;
3796 }
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003797
3798 /* create dvb_frontend */
3799 if (system == SYS_DVBS) {
3800 memcpy(&priv->frontend.ops,
3801 &cxd2841er_dvbs_s2_ops,
3802 sizeof(struct dvb_frontend_ops));
3803 type = "S/S2";
3804 } else {
3805 memcpy(&priv->frontend.ops,
3806 &cxd2841er_t_c_ops,
3807 sizeof(struct dvb_frontend_ops));
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003808 }
3809
3810 dev_info(&priv->i2c->dev,
3811 "%s(): attaching %s DVB-%s frontend\n",
3812 __func__, name, type);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003813 dev_info(&priv->i2c->dev, "%s(): chip ID 0x%02x OK.\n",
3814 __func__, chip_id);
3815 return &priv->frontend;
3816}
3817
3818struct dvb_frontend *cxd2841er_attach_s(struct cxd2841er_config *cfg,
3819 struct i2c_adapter *i2c)
3820{
3821 return cxd2841er_attach(cfg, i2c, SYS_DVBS);
3822}
3823EXPORT_SYMBOL(cxd2841er_attach_s);
3824
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003825struct dvb_frontend *cxd2841er_attach_t_c(struct cxd2841er_config *cfg,
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003826 struct i2c_adapter *i2c)
3827{
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003828 return cxd2841er_attach(cfg, i2c, 0);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003829}
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003830EXPORT_SYMBOL(cxd2841er_attach_t_c);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003831
Max Kellermannbd336e62016-08-09 18:32:21 -03003832static const struct dvb_frontend_ops cxd2841er_dvbs_s2_ops = {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003833 .delsys = { SYS_DVBS, SYS_DVBS2 },
3834 .info = {
3835 .name = "Sony CXD2841ER DVB-S/S2 demodulator",
3836 .frequency_min = 500000,
3837 .frequency_max = 2500000,
3838 .frequency_stepsize = 0,
3839 .symbol_rate_min = 1000000,
3840 .symbol_rate_max = 45000000,
3841 .symbol_rate_tolerance = 500,
3842 .caps = FE_CAN_INVERSION_AUTO |
3843 FE_CAN_FEC_AUTO |
3844 FE_CAN_QPSK,
3845 },
3846 .init = cxd2841er_init_s,
3847 .sleep = cxd2841er_sleep_s,
3848 .release = cxd2841er_release,
3849 .set_frontend = cxd2841er_set_frontend_s,
3850 .get_frontend = cxd2841er_get_frontend,
3851 .read_status = cxd2841er_read_status_s,
3852 .i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
3853 .get_frontend_algo = cxd2841er_get_algo,
3854 .set_tone = cxd2841er_set_tone,
3855 .diseqc_send_burst = cxd2841er_send_burst,
3856 .diseqc_send_master_cmd = cxd2841er_send_diseqc_msg,
3857 .tune = cxd2841er_tune_s
3858};
3859
Max Kellermannbd336e62016-08-09 18:32:21 -03003860static struct dvb_frontend_ops cxd2841er_t_c_ops = {
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003861 .delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A },
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003862 .info = {
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003863 .name = "", /* will set in attach function */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003864 .caps = FE_CAN_FEC_1_2 |
3865 FE_CAN_FEC_2_3 |
3866 FE_CAN_FEC_3_4 |
3867 FE_CAN_FEC_5_6 |
3868 FE_CAN_FEC_7_8 |
3869 FE_CAN_FEC_AUTO |
3870 FE_CAN_QPSK |
3871 FE_CAN_QAM_16 |
3872 FE_CAN_QAM_32 |
3873 FE_CAN_QAM_64 |
3874 FE_CAN_QAM_128 |
3875 FE_CAN_QAM_256 |
3876 FE_CAN_QAM_AUTO |
3877 FE_CAN_TRANSMISSION_MODE_AUTO |
3878 FE_CAN_GUARD_INTERVAL_AUTO |
3879 FE_CAN_HIERARCHY_AUTO |
3880 FE_CAN_MUTE_TS |
3881 FE_CAN_2G_MODULATION,
3882 .frequency_min = 42000000,
Daniel Scheller158f0322017-03-19 12:26:39 -03003883 .frequency_max = 1002000000,
3884 .symbol_rate_min = 870000,
3885 .symbol_rate_max = 11700000
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003886 },
3887 .init = cxd2841er_init_tc,
3888 .sleep = cxd2841er_sleep_tc,
3889 .release = cxd2841er_release,
3890 .set_frontend = cxd2841er_set_frontend_tc,
3891 .get_frontend = cxd2841er_get_frontend,
3892 .read_status = cxd2841er_read_status_tc,
3893 .tune = cxd2841er_tune_tc,
3894 .i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
3895 .get_frontend_algo = cxd2841er_get_algo
3896};
3897
Abylay Ospan83808c22016-03-22 19:20:34 -03003898MODULE_DESCRIPTION("Sony CXD2841ER/CXD2854ER DVB-C/C2/T/T2/S/S2 demodulator driver");
3899MODULE_AUTHOR("Sergey Kozlov <serjk@netup.ru>, Abylay Ospan <aospan@netup.ru>");
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003900MODULE_LICENSE("GPL");