blob: 9f4234fb285fc0785d5d1e8400874bb2bc1f0c9d [file] [log] [blame]
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001/*
2 * cxd2841er.c
3 *
Abylay Ospan83808c22016-03-22 19:20:34 -03004 * Sony digital demodulator driver for
Abylay Ospan9ca17362016-05-16 11:57:04 -03005 * CXD2841ER - DVB-S/S2/T/T2/C/C2
6 * CXD2854ER - DVB-S/S2/T/T2/C/C2, ISDB-T/S
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03007 *
8 * Copyright 2012 Sony Corporation
9 * Copyright (C) 2014 NetUP Inc.
10 * Copyright (C) 2014 Sergey Kozlov <serjk@netup.ru>
11 * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#include <linux/module.h>
25#include <linux/init.h>
26#include <linux/string.h>
27#include <linux/slab.h>
28#include <linux/bitops.h>
29#include <linux/math64.h>
30#include <linux/log2.h>
31#include <linux/dynamic_debug.h>
32
33#include "dvb_math.h"
34#include "dvb_frontend.h"
35#include "cxd2841er.h"
36#include "cxd2841er_priv.h"
37
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -030038#define MAX_WRITE_REGSIZE 16
Abylay Ospana6f330c2016-07-15 15:34:22 -030039#define LOG2_E_100X 144
40
41/* DVB-C constellation */
42enum sony_dvbc_constellation_t {
43 SONY_DVBC_CONSTELLATION_16QAM,
44 SONY_DVBC_CONSTELLATION_32QAM,
45 SONY_DVBC_CONSTELLATION_64QAM,
46 SONY_DVBC_CONSTELLATION_128QAM,
47 SONY_DVBC_CONSTELLATION_256QAM
48};
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -030049
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -030050enum cxd2841er_state {
51 STATE_SHUTDOWN = 0,
52 STATE_SLEEP_S,
53 STATE_ACTIVE_S,
54 STATE_SLEEP_TC,
55 STATE_ACTIVE_TC
56};
57
58struct cxd2841er_priv {
59 struct dvb_frontend frontend;
60 struct i2c_adapter *i2c;
61 u8 i2c_addr_slvx;
62 u8 i2c_addr_slvt;
63 const struct cxd2841er_config *config;
64 enum cxd2841er_state state;
65 u8 system;
Abylay Ospan83808c22016-03-22 19:20:34 -030066 enum cxd2841er_xtal xtal;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -030067 enum fe_caps caps;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -030068};
69
70static const struct cxd2841er_cnr_data s_cn_data[] = {
71 { 0x033e, 0 }, { 0x0339, 100 }, { 0x0333, 200 },
72 { 0x032e, 300 }, { 0x0329, 400 }, { 0x0324, 500 },
73 { 0x031e, 600 }, { 0x0319, 700 }, { 0x0314, 800 },
74 { 0x030f, 900 }, { 0x030a, 1000 }, { 0x02ff, 1100 },
75 { 0x02f4, 1200 }, { 0x02e9, 1300 }, { 0x02de, 1400 },
76 { 0x02d4, 1500 }, { 0x02c9, 1600 }, { 0x02bf, 1700 },
77 { 0x02b5, 1800 }, { 0x02ab, 1900 }, { 0x02a1, 2000 },
78 { 0x029b, 2100 }, { 0x0295, 2200 }, { 0x0290, 2300 },
79 { 0x028a, 2400 }, { 0x0284, 2500 }, { 0x027f, 2600 },
80 { 0x0279, 2700 }, { 0x0274, 2800 }, { 0x026e, 2900 },
81 { 0x0269, 3000 }, { 0x0262, 3100 }, { 0x025c, 3200 },
82 { 0x0255, 3300 }, { 0x024f, 3400 }, { 0x0249, 3500 },
83 { 0x0242, 3600 }, { 0x023c, 3700 }, { 0x0236, 3800 },
84 { 0x0230, 3900 }, { 0x022a, 4000 }, { 0x0223, 4100 },
85 { 0x021c, 4200 }, { 0x0215, 4300 }, { 0x020e, 4400 },
86 { 0x0207, 4500 }, { 0x0201, 4600 }, { 0x01fa, 4700 },
87 { 0x01f4, 4800 }, { 0x01ed, 4900 }, { 0x01e7, 5000 },
88 { 0x01e0, 5100 }, { 0x01d9, 5200 }, { 0x01d2, 5300 },
89 { 0x01cb, 5400 }, { 0x01c4, 5500 }, { 0x01be, 5600 },
90 { 0x01b7, 5700 }, { 0x01b1, 5800 }, { 0x01aa, 5900 },
91 { 0x01a4, 6000 }, { 0x019d, 6100 }, { 0x0196, 6200 },
92 { 0x018f, 6300 }, { 0x0189, 6400 }, { 0x0182, 6500 },
93 { 0x017c, 6600 }, { 0x0175, 6700 }, { 0x016f, 6800 },
94 { 0x0169, 6900 }, { 0x0163, 7000 }, { 0x015c, 7100 },
95 { 0x0156, 7200 }, { 0x0150, 7300 }, { 0x014a, 7400 },
96 { 0x0144, 7500 }, { 0x013e, 7600 }, { 0x0138, 7700 },
97 { 0x0132, 7800 }, { 0x012d, 7900 }, { 0x0127, 8000 },
98 { 0x0121, 8100 }, { 0x011c, 8200 }, { 0x0116, 8300 },
99 { 0x0111, 8400 }, { 0x010b, 8500 }, { 0x0106, 8600 },
100 { 0x0101, 8700 }, { 0x00fc, 8800 }, { 0x00f7, 8900 },
101 { 0x00f2, 9000 }, { 0x00ee, 9100 }, { 0x00ea, 9200 },
102 { 0x00e6, 9300 }, { 0x00e2, 9400 }, { 0x00de, 9500 },
103 { 0x00da, 9600 }, { 0x00d7, 9700 }, { 0x00d3, 9800 },
104 { 0x00d0, 9900 }, { 0x00cc, 10000 }, { 0x00c7, 10100 },
105 { 0x00c3, 10200 }, { 0x00bf, 10300 }, { 0x00ba, 10400 },
106 { 0x00b6, 10500 }, { 0x00b2, 10600 }, { 0x00ae, 10700 },
107 { 0x00aa, 10800 }, { 0x00a7, 10900 }, { 0x00a3, 11000 },
108 { 0x009f, 11100 }, { 0x009c, 11200 }, { 0x0098, 11300 },
109 { 0x0094, 11400 }, { 0x0091, 11500 }, { 0x008e, 11600 },
110 { 0x008a, 11700 }, { 0x0087, 11800 }, { 0x0084, 11900 },
111 { 0x0081, 12000 }, { 0x007e, 12100 }, { 0x007b, 12200 },
112 { 0x0079, 12300 }, { 0x0076, 12400 }, { 0x0073, 12500 },
113 { 0x0071, 12600 }, { 0x006e, 12700 }, { 0x006c, 12800 },
114 { 0x0069, 12900 }, { 0x0067, 13000 }, { 0x0065, 13100 },
115 { 0x0062, 13200 }, { 0x0060, 13300 }, { 0x005e, 13400 },
116 { 0x005c, 13500 }, { 0x005a, 13600 }, { 0x0058, 13700 },
117 { 0x0056, 13800 }, { 0x0054, 13900 }, { 0x0052, 14000 },
118 { 0x0050, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
119 { 0x004b, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
120 { 0x0046, 14700 }, { 0x0044, 14800 }, { 0x0043, 14900 },
121 { 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
122 { 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
123 { 0x0037, 15700 }, { 0x0036, 15800 }, { 0x0034, 15900 },
124 { 0x0033, 16000 }, { 0x0032, 16100 }, { 0x0031, 16200 },
125 { 0x0030, 16300 }, { 0x002f, 16400 }, { 0x002e, 16500 },
126 { 0x002d, 16600 }, { 0x002c, 16700 }, { 0x002b, 16800 },
127 { 0x002a, 16900 }, { 0x0029, 17000 }, { 0x0028, 17100 },
128 { 0x0027, 17200 }, { 0x0026, 17300 }, { 0x0025, 17400 },
129 { 0x0024, 17500 }, { 0x0023, 17600 }, { 0x0022, 17800 },
130 { 0x0021, 17900 }, { 0x0020, 18000 }, { 0x001f, 18200 },
131 { 0x001e, 18300 }, { 0x001d, 18500 }, { 0x001c, 18700 },
132 { 0x001b, 18900 }, { 0x001a, 19000 }, { 0x0019, 19200 },
133 { 0x0018, 19300 }, { 0x0017, 19500 }, { 0x0016, 19700 },
134 { 0x0015, 19900 }, { 0x0014, 20000 },
135};
136
137static const struct cxd2841er_cnr_data s2_cn_data[] = {
138 { 0x05af, 0 }, { 0x0597, 100 }, { 0x057e, 200 },
139 { 0x0567, 300 }, { 0x0550, 400 }, { 0x0539, 500 },
140 { 0x0522, 600 }, { 0x050c, 700 }, { 0x04f6, 800 },
141 { 0x04e1, 900 }, { 0x04cc, 1000 }, { 0x04b6, 1100 },
142 { 0x04a1, 1200 }, { 0x048c, 1300 }, { 0x0477, 1400 },
143 { 0x0463, 1500 }, { 0x044f, 1600 }, { 0x043c, 1700 },
144 { 0x0428, 1800 }, { 0x0416, 1900 }, { 0x0403, 2000 },
145 { 0x03ef, 2100 }, { 0x03dc, 2200 }, { 0x03c9, 2300 },
146 { 0x03b6, 2400 }, { 0x03a4, 2500 }, { 0x0392, 2600 },
147 { 0x0381, 2700 }, { 0x036f, 2800 }, { 0x035f, 2900 },
148 { 0x034e, 3000 }, { 0x033d, 3100 }, { 0x032d, 3200 },
149 { 0x031d, 3300 }, { 0x030d, 3400 }, { 0x02fd, 3500 },
150 { 0x02ee, 3600 }, { 0x02df, 3700 }, { 0x02d0, 3800 },
151 { 0x02c2, 3900 }, { 0x02b4, 4000 }, { 0x02a6, 4100 },
152 { 0x0299, 4200 }, { 0x028c, 4300 }, { 0x027f, 4400 },
153 { 0x0272, 4500 }, { 0x0265, 4600 }, { 0x0259, 4700 },
154 { 0x024d, 4800 }, { 0x0241, 4900 }, { 0x0236, 5000 },
155 { 0x022b, 5100 }, { 0x0220, 5200 }, { 0x0215, 5300 },
156 { 0x020a, 5400 }, { 0x0200, 5500 }, { 0x01f6, 5600 },
157 { 0x01ec, 5700 }, { 0x01e2, 5800 }, { 0x01d8, 5900 },
158 { 0x01cf, 6000 }, { 0x01c6, 6100 }, { 0x01bc, 6200 },
159 { 0x01b3, 6300 }, { 0x01aa, 6400 }, { 0x01a2, 6500 },
160 { 0x0199, 6600 }, { 0x0191, 6700 }, { 0x0189, 6800 },
161 { 0x0181, 6900 }, { 0x0179, 7000 }, { 0x0171, 7100 },
162 { 0x0169, 7200 }, { 0x0161, 7300 }, { 0x015a, 7400 },
163 { 0x0153, 7500 }, { 0x014b, 7600 }, { 0x0144, 7700 },
164 { 0x013d, 7800 }, { 0x0137, 7900 }, { 0x0130, 8000 },
165 { 0x012a, 8100 }, { 0x0124, 8200 }, { 0x011e, 8300 },
166 { 0x0118, 8400 }, { 0x0112, 8500 }, { 0x010c, 8600 },
167 { 0x0107, 8700 }, { 0x0101, 8800 }, { 0x00fc, 8900 },
168 { 0x00f7, 9000 }, { 0x00f2, 9100 }, { 0x00ec, 9200 },
169 { 0x00e7, 9300 }, { 0x00e2, 9400 }, { 0x00dd, 9500 },
170 { 0x00d8, 9600 }, { 0x00d4, 9700 }, { 0x00cf, 9800 },
171 { 0x00ca, 9900 }, { 0x00c6, 10000 }, { 0x00c2, 10100 },
172 { 0x00be, 10200 }, { 0x00b9, 10300 }, { 0x00b5, 10400 },
173 { 0x00b1, 10500 }, { 0x00ae, 10600 }, { 0x00aa, 10700 },
174 { 0x00a6, 10800 }, { 0x00a3, 10900 }, { 0x009f, 11000 },
175 { 0x009b, 11100 }, { 0x0098, 11200 }, { 0x0095, 11300 },
176 { 0x0091, 11400 }, { 0x008e, 11500 }, { 0x008b, 11600 },
177 { 0x0088, 11700 }, { 0x0085, 11800 }, { 0x0082, 11900 },
178 { 0x007f, 12000 }, { 0x007c, 12100 }, { 0x007a, 12200 },
179 { 0x0077, 12300 }, { 0x0074, 12400 }, { 0x0072, 12500 },
180 { 0x006f, 12600 }, { 0x006d, 12700 }, { 0x006b, 12800 },
181 { 0x0068, 12900 }, { 0x0066, 13000 }, { 0x0064, 13100 },
182 { 0x0061, 13200 }, { 0x005f, 13300 }, { 0x005d, 13400 },
183 { 0x005b, 13500 }, { 0x0059, 13600 }, { 0x0057, 13700 },
184 { 0x0055, 13800 }, { 0x0053, 13900 }, { 0x0051, 14000 },
185 { 0x004f, 14100 }, { 0x004e, 14200 }, { 0x004c, 14300 },
186 { 0x004a, 14400 }, { 0x0049, 14500 }, { 0x0047, 14600 },
187 { 0x0045, 14700 }, { 0x0044, 14800 }, { 0x0042, 14900 },
188 { 0x0041, 15000 }, { 0x003f, 15100 }, { 0x003e, 15200 },
189 { 0x003c, 15300 }, { 0x003b, 15400 }, { 0x003a, 15500 },
190 { 0x0038, 15600 }, { 0x0037, 15700 }, { 0x0036, 15800 },
191 { 0x0034, 15900 }, { 0x0033, 16000 }, { 0x0032, 16100 },
192 { 0x0031, 16200 }, { 0x0030, 16300 }, { 0x002f, 16400 },
193 { 0x002e, 16500 }, { 0x002d, 16600 }, { 0x002c, 16700 },
194 { 0x002b, 16800 }, { 0x002a, 16900 }, { 0x0029, 17000 },
195 { 0x0028, 17100 }, { 0x0027, 17200 }, { 0x0026, 17300 },
196 { 0x0025, 17400 }, { 0x0024, 17500 }, { 0x0023, 17600 },
197 { 0x0022, 17800 }, { 0x0021, 17900 }, { 0x0020, 18000 },
198 { 0x001f, 18200 }, { 0x001e, 18300 }, { 0x001d, 18500 },
199 { 0x001c, 18700 }, { 0x001b, 18900 }, { 0x001a, 19000 },
200 { 0x0019, 19200 }, { 0x0018, 19300 }, { 0x0017, 19500 },
201 { 0x0016, 19700 }, { 0x0015, 19900 }, { 0x0014, 20000 },
202};
203
Abylay Ospan0854df72016-07-19 12:22:03 -0300204static int cxd2841er_freeze_regs(struct cxd2841er_priv *priv);
205static int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv);
206
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300207static void cxd2841er_i2c_debug(struct cxd2841er_priv *priv,
208 u8 addr, u8 reg, u8 write,
209 const u8 *data, u32 len)
210{
211 dev_dbg(&priv->i2c->dev,
Daniel Scheller5d6d93a2017-04-09 16:38:10 -0300212 "cxd2841er: I2C %s addr %02x reg 0x%02x size %d data %*ph\n",
213 (write == 0 ? "read" : "write"), addr, reg, len, len, data);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300214}
215
216static int cxd2841er_write_regs(struct cxd2841er_priv *priv,
217 u8 addr, u8 reg, const u8 *data, u32 len)
218{
219 int ret;
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300220 u8 buf[MAX_WRITE_REGSIZE + 1];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300221 u8 i2c_addr = (addr == I2C_SLVX ?
222 priv->i2c_addr_slvx : priv->i2c_addr_slvt);
223 struct i2c_msg msg[1] = {
224 {
225 .addr = i2c_addr,
226 .flags = 0,
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300227 .len = len + 1,
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300228 .buf = buf,
229 }
230 };
231
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300232 if (len + 1 >= sizeof(buf)) {
Abylay Ospan83808c22016-03-22 19:20:34 -0300233 dev_warn(&priv->i2c->dev, "wr reg=%04x: len=%d is too big!\n",
Mauro Carvalho Chehabd13a7b62015-08-11 15:22:36 -0300234 reg, len + 1);
235 return -E2BIG;
236 }
237
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300238 cxd2841er_i2c_debug(priv, i2c_addr, reg, 1, data, len);
239 buf[0] = reg;
240 memcpy(&buf[1], data, len);
241
242 ret = i2c_transfer(priv->i2c, msg, 1);
243 if (ret >= 0 && ret != 1)
244 ret = -EIO;
245 if (ret < 0) {
246 dev_warn(&priv->i2c->dev,
247 "%s: i2c wr failed=%d addr=%02x reg=%02x len=%d\n",
248 KBUILD_MODNAME, ret, i2c_addr, reg, len);
249 return ret;
250 }
251 return 0;
252}
253
254static int cxd2841er_write_reg(struct cxd2841er_priv *priv,
255 u8 addr, u8 reg, u8 val)
256{
257 return cxd2841er_write_regs(priv, addr, reg, &val, 1);
258}
259
260static int cxd2841er_read_regs(struct cxd2841er_priv *priv,
261 u8 addr, u8 reg, u8 *val, u32 len)
262{
263 int ret;
264 u8 i2c_addr = (addr == I2C_SLVX ?
265 priv->i2c_addr_slvx : priv->i2c_addr_slvt);
266 struct i2c_msg msg[2] = {
267 {
268 .addr = i2c_addr,
269 .flags = 0,
270 .len = 1,
271 .buf = &reg,
272 }, {
273 .addr = i2c_addr,
274 .flags = I2C_M_RD,
275 .len = len,
276 .buf = val,
277 }
278 };
279
Daniel Scheller725e93e2017-04-09 16:38:11 -0300280 ret = i2c_transfer(priv->i2c, msg, 2);
281 if (ret >= 0 && ret != 2)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300282 ret = -EIO;
283 if (ret < 0) {
284 dev_warn(&priv->i2c->dev,
285 "%s: i2c rd failed=%d addr=%02x reg=%02x\n",
286 KBUILD_MODNAME, ret, i2c_addr, reg);
287 return ret;
288 }
Abylay Ospan6c771612016-05-16 11:43:25 -0300289 cxd2841er_i2c_debug(priv, i2c_addr, reg, 0, val, len);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300290 return 0;
291}
292
293static int cxd2841er_read_reg(struct cxd2841er_priv *priv,
294 u8 addr, u8 reg, u8 *val)
295{
296 return cxd2841er_read_regs(priv, addr, reg, val, 1);
297}
298
299static int cxd2841er_set_reg_bits(struct cxd2841er_priv *priv,
300 u8 addr, u8 reg, u8 data, u8 mask)
301{
302 int res;
303 u8 rdata;
304
305 if (mask != 0xff) {
306 res = cxd2841er_read_reg(priv, addr, reg, &rdata);
307 if (res)
308 return res;
309 data = ((data & mask) | (rdata & (mask ^ 0xFF)));
310 }
311 return cxd2841er_write_reg(priv, addr, reg, data);
312}
313
Daniel Schellercbc85a42017-04-09 16:38:14 -0300314static u32 cxd2841er_calc_iffreq_xtal(enum cxd2841er_xtal xtal, u32 ifhz)
315{
316 u64 tmp;
317
318 tmp = (u64) ifhz * 16777216;
319 do_div(tmp, ((xtal == SONY_XTAL_24000) ? 48000000 : 41000000));
320
321 return (u32) tmp;
322}
323
324static u32 cxd2841er_calc_iffreq(u32 ifhz)
325{
326 return cxd2841er_calc_iffreq_xtal(SONY_XTAL_20500, ifhz);
327}
328
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300329static int cxd2841er_dvbs2_set_symbol_rate(struct cxd2841er_priv *priv,
330 u32 symbol_rate)
331{
332 u32 reg_value = 0;
333 u8 data[3] = {0, 0, 0};
334
335 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
336 /*
337 * regValue = (symbolRateKSps * 2^14 / 1000) + 0.5
338 * = ((symbolRateKSps * 2^14) + 500) / 1000
339 * = ((symbolRateKSps * 16384) + 500) / 1000
340 */
341 reg_value = DIV_ROUND_CLOSEST(symbol_rate * 16384, 1000);
342 if ((reg_value == 0) || (reg_value > 0xFFFFF)) {
343 dev_err(&priv->i2c->dev,
344 "%s(): reg_value is out of range\n", __func__);
345 return -EINVAL;
346 }
347 data[0] = (u8)((reg_value >> 16) & 0x0F);
348 data[1] = (u8)((reg_value >> 8) & 0xFF);
349 data[2] = (u8)(reg_value & 0xFF);
350 /* Set SLV-T Bank : 0xAE */
351 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
352 cxd2841er_write_regs(priv, I2C_SLVT, 0x20, data, 3);
353 return 0;
354}
355
356static void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
357 u8 system);
358
359static int cxd2841er_sleep_s_to_active_s(struct cxd2841er_priv *priv,
360 u8 system, u32 symbol_rate)
361{
362 int ret;
363 u8 data[4] = { 0, 0, 0, 0 };
364
365 if (priv->state != STATE_SLEEP_S) {
366 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
367 __func__, (int)priv->state);
368 return -EINVAL;
369 }
370 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
371 cxd2841er_set_ts_clock_mode(priv, SYS_DVBS);
372 /* Set demod mode */
373 if (system == SYS_DVBS) {
374 data[0] = 0x0A;
375 } else if (system == SYS_DVBS2) {
376 data[0] = 0x0B;
377 } else {
378 dev_err(&priv->i2c->dev, "%s(): invalid delsys %d\n",
379 __func__, system);
380 return -EINVAL;
381 }
382 /* Set SLV-X Bank : 0x00 */
383 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
384 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, data[0]);
385 /* DVB-S/S2 */
386 data[0] = 0x00;
387 /* Set SLV-T Bank : 0x00 */
388 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
389 /* Enable S/S2 auto detection 1 */
390 cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, data[0]);
391 /* Set SLV-T Bank : 0xAE */
392 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
393 /* Enable S/S2 auto detection 2 */
394 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, data[0]);
395 /* Set SLV-T Bank : 0x00 */
396 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
397 /* Enable demod clock */
398 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
399 /* Enable ADC clock */
400 cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x01);
401 /* Enable ADC 1 */
402 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
403 /* Enable ADC 2 */
404 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x3f);
405 /* Set SLV-X Bank : 0x00 */
406 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
407 /* Enable ADC 3 */
408 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
409 /* Set SLV-T Bank : 0xA3 */
410 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa3);
411 cxd2841er_write_reg(priv, I2C_SLVT, 0xac, 0x00);
412 data[0] = 0x07;
413 data[1] = 0x3B;
414 data[2] = 0x08;
415 data[3] = 0xC5;
416 /* Set SLV-T Bank : 0xAB */
417 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xab);
418 cxd2841er_write_regs(priv, I2C_SLVT, 0x98, data, 4);
419 data[0] = 0x05;
420 data[1] = 0x80;
421 data[2] = 0x0A;
422 data[3] = 0x80;
423 cxd2841er_write_regs(priv, I2C_SLVT, 0xa8, data, 4);
424 data[0] = 0x0C;
425 data[1] = 0xCC;
426 cxd2841er_write_regs(priv, I2C_SLVT, 0xc3, data, 2);
427 /* Set demod parameter */
428 ret = cxd2841er_dvbs2_set_symbol_rate(priv, symbol_rate);
429 if (ret != 0)
430 return ret;
431 /* Set SLV-T Bank : 0x00 */
432 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
433 /* disable Hi-Z setting 1 */
434 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x10);
435 /* disable Hi-Z setting 2 */
436 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
437 priv->state = STATE_ACTIVE_S;
438 return 0;
439}
440
441static int cxd2841er_sleep_tc_to_active_t_band(struct cxd2841er_priv *priv,
442 u32 bandwidth);
443
444static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
445 u32 bandwidth);
446
447static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
448 u32 bandwidth);
449
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -0300450static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
451 u32 bandwidth);
452
453static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv);
454
455static int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv);
456
457static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv);
458
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300459static int cxd2841er_retune_active(struct cxd2841er_priv *priv,
460 struct dtv_frontend_properties *p)
461{
462 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
463 if (priv->state != STATE_ACTIVE_S &&
464 priv->state != STATE_ACTIVE_TC) {
465 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
466 __func__, priv->state);
467 return -EINVAL;
468 }
469 /* Set SLV-T Bank : 0x00 */
470 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
471 /* disable TS output */
472 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
473 if (priv->state == STATE_ACTIVE_S)
474 return cxd2841er_dvbs2_set_symbol_rate(
475 priv, p->symbol_rate / 1000);
476 else if (priv->state == STATE_ACTIVE_TC) {
477 switch (priv->system) {
478 case SYS_DVBT:
479 return cxd2841er_sleep_tc_to_active_t_band(
480 priv, p->bandwidth_hz);
481 case SYS_DVBT2:
482 return cxd2841er_sleep_tc_to_active_t2_band(
483 priv, p->bandwidth_hz);
484 case SYS_DVBC_ANNEX_A:
485 return cxd2841er_sleep_tc_to_active_c_band(
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -0300486 priv, p->bandwidth_hz);
487 case SYS_ISDBT:
488 cxd2841er_active_i_to_sleep_tc(priv);
489 cxd2841er_sleep_tc_to_shutdown(priv);
490 cxd2841er_shutdown_to_sleep_tc(priv);
491 return cxd2841er_sleep_tc_to_active_i(
492 priv, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300493 }
494 }
495 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
496 __func__, priv->system);
497 return -EINVAL;
498}
499
500static int cxd2841er_active_s_to_sleep_s(struct cxd2841er_priv *priv)
501{
502 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
503 if (priv->state != STATE_ACTIVE_S) {
504 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
505 __func__, priv->state);
506 return -EINVAL;
507 }
508 /* Set SLV-T Bank : 0x00 */
509 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
510 /* disable TS output */
511 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
512 /* enable Hi-Z setting 1 */
513 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1f);
514 /* enable Hi-Z setting 2 */
515 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
516 /* Set SLV-X Bank : 0x00 */
517 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
518 /* disable ADC 1 */
519 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
520 /* Set SLV-T Bank : 0x00 */
521 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
522 /* disable ADC clock */
523 cxd2841er_write_reg(priv, I2C_SLVT, 0x31, 0x00);
524 /* disable ADC 2 */
525 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
526 /* disable ADC 3 */
527 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
528 /* SADC Bias ON */
529 cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
530 /* disable demod clock */
531 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
532 /* Set SLV-T Bank : 0xAE */
533 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xae);
534 /* disable S/S2 auto detection1 */
535 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
536 /* Set SLV-T Bank : 0x00 */
537 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
538 /* disable S/S2 auto detection2 */
539 cxd2841er_write_reg(priv, I2C_SLVT, 0x2d, 0x00);
540 priv->state = STATE_SLEEP_S;
541 return 0;
542}
543
544static int cxd2841er_sleep_s_to_shutdown(struct cxd2841er_priv *priv)
545{
546 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
547 if (priv->state != STATE_SLEEP_S) {
548 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
549 __func__, priv->state);
550 return -EINVAL;
551 }
552 /* Set SLV-T Bank : 0x00 */
553 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
554 /* Disable DSQOUT */
555 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
556 /* Disable DSQIN */
557 cxd2841er_write_reg(priv, I2C_SLVT, 0x9c, 0x00);
558 /* Set SLV-X Bank : 0x00 */
559 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
560 /* Disable oscillator */
561 cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
562 /* Set demod mode */
563 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
564 priv->state = STATE_SHUTDOWN;
565 return 0;
566}
567
568static int cxd2841er_sleep_tc_to_shutdown(struct cxd2841er_priv *priv)
569{
570 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
571 if (priv->state != STATE_SLEEP_TC) {
572 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
573 __func__, priv->state);
574 return -EINVAL;
575 }
576 /* Set SLV-X Bank : 0x00 */
577 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
578 /* Disable oscillator */
579 cxd2841er_write_reg(priv, I2C_SLVX, 0x15, 0x01);
580 /* Set demod mode */
581 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
582 priv->state = STATE_SHUTDOWN;
583 return 0;
584}
585
586static int cxd2841er_active_t_to_sleep_tc(struct cxd2841er_priv *priv)
587{
588 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
589 if (priv->state != STATE_ACTIVE_TC) {
590 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
591 __func__, priv->state);
592 return -EINVAL;
593 }
594 /* Set SLV-T Bank : 0x00 */
595 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
596 /* disable TS output */
597 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
598 /* enable Hi-Z setting 1 */
599 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
600 /* enable Hi-Z setting 2 */
601 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
602 /* Set SLV-X Bank : 0x00 */
603 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
604 /* disable ADC 1 */
605 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
606 /* Set SLV-T Bank : 0x00 */
607 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
608 /* Disable ADC 2 */
609 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
610 /* Disable ADC 3 */
611 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
612 /* Disable ADC clock */
613 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
614 /* Disable RF level monitor */
615 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
616 /* Disable demod clock */
617 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
618 priv->state = STATE_SLEEP_TC;
619 return 0;
620}
621
622static int cxd2841er_active_t2_to_sleep_tc(struct cxd2841er_priv *priv)
623{
624 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
625 if (priv->state != STATE_ACTIVE_TC) {
626 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
627 __func__, priv->state);
628 return -EINVAL;
629 }
630 /* Set SLV-T Bank : 0x00 */
631 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
632 /* disable TS output */
633 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
634 /* enable Hi-Z setting 1 */
635 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
636 /* enable Hi-Z setting 2 */
637 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
638 /* Cancel DVB-T2 setting */
639 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
640 cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x40);
641 cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x21);
642 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
643 cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xfb);
644 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
645 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x00, 0x0f);
646 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
647 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x00, 0x3f);
648 /* Set SLV-X Bank : 0x00 */
649 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
650 /* disable ADC 1 */
651 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
652 /* Set SLV-T Bank : 0x00 */
653 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
654 /* Disable ADC 2 */
655 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
656 /* Disable ADC 3 */
657 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
658 /* Disable ADC clock */
659 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
660 /* Disable RF level monitor */
661 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
662 /* Disable demod clock */
663 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
664 priv->state = STATE_SLEEP_TC;
665 return 0;
666}
667
668static int cxd2841er_active_c_to_sleep_tc(struct cxd2841er_priv *priv)
669{
670 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
671 if (priv->state != STATE_ACTIVE_TC) {
672 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
673 __func__, priv->state);
674 return -EINVAL;
675 }
676 /* Set SLV-T Bank : 0x00 */
677 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
678 /* disable TS output */
679 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
680 /* enable Hi-Z setting 1 */
681 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
682 /* enable Hi-Z setting 2 */
683 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
684 /* Cancel DVB-C setting */
685 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
686 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
687 /* Set SLV-X Bank : 0x00 */
688 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
689 /* disable ADC 1 */
690 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
691 /* Set SLV-T Bank : 0x00 */
692 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
693 /* Disable ADC 2 */
694 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
695 /* Disable ADC 3 */
696 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
697 /* Disable ADC clock */
698 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
699 /* Disable RF level monitor */
700 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
701 /* Disable demod clock */
702 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
703 priv->state = STATE_SLEEP_TC;
704 return 0;
705}
706
Abylay Ospan83808c22016-03-22 19:20:34 -0300707static int cxd2841er_active_i_to_sleep_tc(struct cxd2841er_priv *priv)
708{
709 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
710 if (priv->state != STATE_ACTIVE_TC) {
711 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
712 __func__, priv->state);
713 return -EINVAL;
714 }
715 /* Set SLV-T Bank : 0x00 */
716 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
717 /* disable TS output */
718 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x01);
719 /* enable Hi-Z setting 1 */
720 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x3f);
721 /* enable Hi-Z setting 2 */
722 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0xff);
723
724 /* TODO: Cancel demod parameter */
725
726 /* Set SLV-X Bank : 0x00 */
727 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
728 /* disable ADC 1 */
729 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x01);
730 /* Set SLV-T Bank : 0x00 */
731 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
732 /* Disable ADC 2 */
733 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
734 /* Disable ADC 3 */
735 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
736 /* Disable ADC clock */
737 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
738 /* Disable RF level monitor */
739 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
740 /* Disable demod clock */
741 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x00);
742 priv->state = STATE_SLEEP_TC;
743 return 0;
744}
745
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300746static int cxd2841er_shutdown_to_sleep_s(struct cxd2841er_priv *priv)
747{
748 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
749 if (priv->state != STATE_SHUTDOWN) {
750 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
751 __func__, priv->state);
752 return -EINVAL;
753 }
754 /* Set SLV-X Bank : 0x00 */
755 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
756 /* Clear all demodulator registers */
757 cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
758 usleep_range(3000, 5000);
759 /* Set SLV-X Bank : 0x00 */
760 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
761 /* Set demod SW reset */
762 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
Abylay Ospan83808c22016-03-22 19:20:34 -0300763
764 switch (priv->xtal) {
765 case SONY_XTAL_20500:
766 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x00);
767 break;
768 case SONY_XTAL_24000:
769 /* Select demod frequency */
770 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
771 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x03);
772 break;
773 case SONY_XTAL_41000:
774 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, 0x01);
775 break;
776 default:
777 dev_dbg(&priv->i2c->dev, "%s(): invalid demod xtal %d\n",
778 __func__, priv->xtal);
779 return -EINVAL;
780 }
781
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300782 /* Set demod mode */
783 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x0a);
784 /* Clear demod SW reset */
785 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
786 usleep_range(1000, 2000);
787 /* Set SLV-T Bank : 0x00 */
788 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
789 /* enable DSQOUT */
790 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x1F);
791 /* enable DSQIN */
792 cxd2841er_write_reg(priv, I2C_SLVT, 0x9C, 0x40);
793 /* TADC Bias On */
794 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
795 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
796 /* SADC Bias On */
797 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
798 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
799 cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
800 priv->state = STATE_SLEEP_S;
801 return 0;
802}
803
804static int cxd2841er_shutdown_to_sleep_tc(struct cxd2841er_priv *priv)
805{
Abylay Ospan6c771612016-05-16 11:43:25 -0300806 u8 data = 0;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -0300807
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300808 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
809 if (priv->state != STATE_SHUTDOWN) {
810 dev_dbg(&priv->i2c->dev, "%s(): invalid demod state %d\n",
811 __func__, priv->state);
812 return -EINVAL;
813 }
814 /* Set SLV-X Bank : 0x00 */
815 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
816 /* Clear all demodulator registers */
817 cxd2841er_write_reg(priv, I2C_SLVX, 0x02, 0x00);
818 usleep_range(3000, 5000);
819 /* Set SLV-X Bank : 0x00 */
820 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
821 /* Set demod SW reset */
822 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x01);
Abylay Ospan6c771612016-05-16 11:43:25 -0300823 /* Select ADC clock mode */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300824 cxd2841er_write_reg(priv, I2C_SLVX, 0x13, 0x00);
Abylay Ospan6c771612016-05-16 11:43:25 -0300825
826 switch (priv->xtal) {
827 case SONY_XTAL_20500:
828 data = 0x0;
829 break;
830 case SONY_XTAL_24000:
831 /* Select demod frequency */
832 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
833 data = 0x3;
834 break;
835 case SONY_XTAL_41000:
836 cxd2841er_write_reg(priv, I2C_SLVX, 0x12, 0x00);
837 data = 0x1;
838 break;
839 }
840 cxd2841er_write_reg(priv, I2C_SLVX, 0x14, data);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300841 /* Clear demod SW reset */
842 cxd2841er_write_reg(priv, I2C_SLVX, 0x10, 0x00);
843 usleep_range(1000, 2000);
844 /* Set SLV-T Bank : 0x00 */
845 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
846 /* TADC Bias On */
847 cxd2841er_write_reg(priv, I2C_SLVT, 0x43, 0x0a);
848 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x0a);
849 /* SADC Bias On */
850 cxd2841er_write_reg(priv, I2C_SLVT, 0x63, 0x16);
851 cxd2841er_write_reg(priv, I2C_SLVT, 0x65, 0x27);
852 cxd2841er_write_reg(priv, I2C_SLVT, 0x69, 0x06);
853 priv->state = STATE_SLEEP_TC;
854 return 0;
855}
856
857static int cxd2841er_tune_done(struct cxd2841er_priv *priv)
858{
859 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
860 /* Set SLV-T Bank : 0x00 */
861 cxd2841er_write_reg(priv, I2C_SLVT, 0, 0);
862 /* SW Reset */
863 cxd2841er_write_reg(priv, I2C_SLVT, 0xfe, 0x01);
864 /* Enable TS output */
865 cxd2841er_write_reg(priv, I2C_SLVT, 0xc3, 0x00);
866 return 0;
867}
868
869/* Set TS parallel mode */
870static void cxd2841er_set_ts_clock_mode(struct cxd2841er_priv *priv,
871 u8 system)
872{
873 u8 serial_ts, ts_rate_ctrl_off, ts_in_off;
874
875 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
876 /* Set SLV-T Bank : 0x00 */
877 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
878 cxd2841er_read_reg(priv, I2C_SLVT, 0xc4, &serial_ts);
879 cxd2841er_read_reg(priv, I2C_SLVT, 0xd3, &ts_rate_ctrl_off);
880 cxd2841er_read_reg(priv, I2C_SLVT, 0xde, &ts_in_off);
881 dev_dbg(&priv->i2c->dev, "%s(): ser_ts=0x%02x rate_ctrl_off=0x%02x in_off=0x%02x\n",
882 __func__, serial_ts, ts_rate_ctrl_off, ts_in_off);
883
884 /*
885 * slave Bank Addr Bit default Name
886 * <SLV-T> 00h D9h [7:0] 8'h08 OTSCKPERIOD
887 */
888 cxd2841er_write_reg(priv, I2C_SLVT, 0xd9, 0x08);
889 /*
890 * Disable TS IF Clock
891 * slave Bank Addr Bit default Name
892 * <SLV-T> 00h 32h [0] 1'b1 OREG_CK_TSIF_EN
893 */
894 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x00, 0x01);
895 /*
896 * slave Bank Addr Bit default Name
897 * <SLV-T> 00h 33h [1:0] 2'b01 OREG_CKSEL_TSIF
898 */
899 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x33, 0x00, 0x03);
900 /*
901 * Enable TS IF Clock
902 * slave Bank Addr Bit default Name
903 * <SLV-T> 00h 32h [0] 1'b1 OREG_CK_TSIF_EN
904 */
905 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x32, 0x01, 0x01);
906
907 if (system == SYS_DVBT) {
908 /* Enable parity period for DVB-T */
909 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
910 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
911 } else if (system == SYS_DVBC_ANNEX_A) {
912 /* Enable parity period for DVB-C */
913 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
914 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x66, 0x01, 0x01);
915 }
916}
917
918static u8 cxd2841er_chip_id(struct cxd2841er_priv *priv)
919{
Abylay Ospan83808c22016-03-22 19:20:34 -0300920 u8 chip_id = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300921
922 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
Abylay Ospan83808c22016-03-22 19:20:34 -0300923 if (cxd2841er_write_reg(priv, I2C_SLVT, 0, 0) == 0)
924 cxd2841er_read_reg(priv, I2C_SLVT, 0xfd, &chip_id);
925 else if (cxd2841er_write_reg(priv, I2C_SLVX, 0, 0) == 0)
926 cxd2841er_read_reg(priv, I2C_SLVX, 0xfd, &chip_id);
927
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -0300928 return chip_id;
929}
930
931static int cxd2841er_read_status_s(struct dvb_frontend *fe,
932 enum fe_status *status)
933{
934 u8 reg = 0;
935 struct cxd2841er_priv *priv = fe->demodulator_priv;
936
937 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
938 *status = 0;
939 if (priv->state != STATE_ACTIVE_S) {
940 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
941 __func__, priv->state);
942 return -EINVAL;
943 }
944 /* Set SLV-T Bank : 0xA0 */
945 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
946 /*
947 * slave Bank Addr Bit Signal name
948 * <SLV-T> A0h 11h [2] ITSLOCK
949 */
950 cxd2841er_read_reg(priv, I2C_SLVT, 0x11, &reg);
951 if (reg & 0x04) {
952 *status = FE_HAS_SIGNAL
953 | FE_HAS_CARRIER
954 | FE_HAS_VITERBI
955 | FE_HAS_SYNC
956 | FE_HAS_LOCK;
957 }
958 dev_dbg(&priv->i2c->dev, "%s(): result 0x%x\n", __func__, *status);
959 return 0;
960}
961
962static int cxd2841er_read_status_t_t2(struct cxd2841er_priv *priv,
963 u8 *sync, u8 *tslock, u8 *unlock)
964{
965 u8 data = 0;
966
967 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
968 if (priv->state != STATE_ACTIVE_TC)
969 return -EINVAL;
970 if (priv->system == SYS_DVBT) {
971 /* Set SLV-T Bank : 0x10 */
972 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
973 } else {
974 /* Set SLV-T Bank : 0x20 */
975 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
976 }
977 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
978 if ((data & 0x07) == 0x07) {
979 dev_dbg(&priv->i2c->dev,
980 "%s(): invalid hardware state detected\n", __func__);
981 *sync = 0;
982 *tslock = 0;
983 *unlock = 0;
984 } else {
985 *sync = ((data & 0x07) == 0x6 ? 1 : 0);
986 *tslock = ((data & 0x20) ? 1 : 0);
987 *unlock = ((data & 0x10) ? 1 : 0);
988 }
989 return 0;
990}
991
992static int cxd2841er_read_status_c(struct cxd2841er_priv *priv, u8 *tslock)
993{
994 u8 data;
995
996 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
997 if (priv->state != STATE_ACTIVE_TC)
998 return -EINVAL;
999 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1000 cxd2841er_read_reg(priv, I2C_SLVT, 0x88, &data);
1001 if ((data & 0x01) == 0) {
1002 *tslock = 0;
1003 } else {
1004 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
1005 *tslock = ((data & 0x20) ? 1 : 0);
1006 }
1007 return 0;
1008}
1009
Abylay Ospan83808c22016-03-22 19:20:34 -03001010static int cxd2841er_read_status_i(struct cxd2841er_priv *priv,
1011 u8 *sync, u8 *tslock, u8 *unlock)
1012{
1013 u8 data = 0;
1014
1015 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1016 if (priv->state != STATE_ACTIVE_TC)
1017 return -EINVAL;
1018 /* Set SLV-T Bank : 0x60 */
1019 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1020 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data);
1021 dev_dbg(&priv->i2c->dev,
1022 "%s(): lock=0x%x\n", __func__, data);
1023 *sync = ((data & 0x02) ? 1 : 0);
1024 *tslock = ((data & 0x01) ? 1 : 0);
1025 *unlock = ((data & 0x10) ? 1 : 0);
1026 return 0;
1027}
1028
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001029static int cxd2841er_read_status_tc(struct dvb_frontend *fe,
1030 enum fe_status *status)
1031{
1032 int ret = 0;
1033 u8 sync = 0;
1034 u8 tslock = 0;
1035 u8 unlock = 0;
1036 struct cxd2841er_priv *priv = fe->demodulator_priv;
1037
1038 *status = 0;
1039 if (priv->state == STATE_ACTIVE_TC) {
1040 if (priv->system == SYS_DVBT || priv->system == SYS_DVBT2) {
1041 ret = cxd2841er_read_status_t_t2(
1042 priv, &sync, &tslock, &unlock);
1043 if (ret)
1044 goto done;
1045 if (unlock)
1046 goto done;
1047 if (sync)
1048 *status = FE_HAS_SIGNAL |
1049 FE_HAS_CARRIER |
1050 FE_HAS_VITERBI |
1051 FE_HAS_SYNC;
1052 if (tslock)
1053 *status |= FE_HAS_LOCK;
Abylay Ospan83808c22016-03-22 19:20:34 -03001054 } else if (priv->system == SYS_ISDBT) {
1055 ret = cxd2841er_read_status_i(
1056 priv, &sync, &tslock, &unlock);
1057 if (ret)
1058 goto done;
1059 if (unlock)
1060 goto done;
1061 if (sync)
1062 *status = FE_HAS_SIGNAL |
1063 FE_HAS_CARRIER |
1064 FE_HAS_VITERBI |
1065 FE_HAS_SYNC;
1066 if (tslock)
1067 *status |= FE_HAS_LOCK;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001068 } else if (priv->system == SYS_DVBC_ANNEX_A) {
1069 ret = cxd2841er_read_status_c(priv, &tslock);
1070 if (ret)
1071 goto done;
1072 if (tslock)
1073 *status = FE_HAS_SIGNAL |
1074 FE_HAS_CARRIER |
1075 FE_HAS_VITERBI |
1076 FE_HAS_SYNC |
1077 FE_HAS_LOCK;
1078 }
1079 }
1080done:
1081 dev_dbg(&priv->i2c->dev, "%s(): status 0x%x\n", __func__, *status);
1082 return ret;
1083}
1084
1085static int cxd2841er_get_carrier_offset_s_s2(struct cxd2841er_priv *priv,
1086 int *offset)
1087{
1088 u8 data[3];
1089 u8 is_hs_mode;
1090 s32 cfrl_ctrlval;
1091 s32 temp_div, temp_q, temp_r;
1092
1093 if (priv->state != STATE_ACTIVE_S) {
1094 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1095 __func__, priv->state);
1096 return -EINVAL;
1097 }
1098 /*
1099 * Get High Sampling Rate mode
1100 * slave Bank Addr Bit Signal name
1101 * <SLV-T> A0h 10h [0] ITRL_LOCK
1102 */
1103 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1104 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, &data[0]);
1105 if (data[0] & 0x01) {
1106 /*
1107 * slave Bank Addr Bit Signal name
1108 * <SLV-T> A0h 50h [4] IHSMODE
1109 */
1110 cxd2841er_read_reg(priv, I2C_SLVT, 0x50, &data[0]);
1111 is_hs_mode = (data[0] & 0x10 ? 1 : 0);
1112 } else {
1113 dev_dbg(&priv->i2c->dev,
1114 "%s(): unable to detect sampling rate mode\n",
1115 __func__);
1116 return -EINVAL;
1117 }
1118 /*
1119 * slave Bank Addr Bit Signal name
1120 * <SLV-T> A0h 45h [4:0] ICFRL_CTRLVAL[20:16]
1121 * <SLV-T> A0h 46h [7:0] ICFRL_CTRLVAL[15:8]
1122 * <SLV-T> A0h 47h [7:0] ICFRL_CTRLVAL[7:0]
1123 */
1124 cxd2841er_read_regs(priv, I2C_SLVT, 0x45, data, 3);
1125 cfrl_ctrlval = sign_extend32((((u32)data[0] & 0x1F) << 16) |
1126 (((u32)data[1] & 0xFF) << 8) |
1127 ((u32)data[2] & 0xFF), 20);
1128 temp_div = (is_hs_mode ? 1048576 : 1572864);
1129 if (cfrl_ctrlval > 0) {
1130 temp_q = div_s64_rem(97375LL * cfrl_ctrlval,
1131 temp_div, &temp_r);
1132 } else {
1133 temp_q = div_s64_rem(-97375LL * cfrl_ctrlval,
1134 temp_div, &temp_r);
1135 }
1136 if (temp_r >= temp_div / 2)
1137 temp_q++;
1138 if (cfrl_ctrlval > 0)
1139 temp_q *= -1;
1140 *offset = temp_q;
1141 return 0;
1142}
1143
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -03001144static int cxd2841er_get_carrier_offset_i(struct cxd2841er_priv *priv,
1145 u32 bandwidth, int *offset)
1146{
1147 u8 data[4];
1148
1149 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1150 if (priv->state != STATE_ACTIVE_TC) {
1151 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1152 __func__, priv->state);
1153 return -EINVAL;
1154 }
1155 if (priv->system != SYS_ISDBT) {
1156 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1157 __func__, priv->system);
1158 return -EINVAL;
1159 }
1160 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1161 cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1162 *offset = -1 * sign_extend32(
1163 ((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
1164 ((u32)data[2] << 8) | (u32)data[3], 29);
1165
1166 switch (bandwidth) {
1167 case 6000000:
1168 *offset = -1 * ((*offset) * 8/264);
1169 break;
1170 case 7000000:
1171 *offset = -1 * ((*offset) * 8/231);
1172 break;
1173 case 8000000:
1174 *offset = -1 * ((*offset) * 8/198);
1175 break;
1176 default:
1177 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
1178 __func__, bandwidth);
1179 return -EINVAL;
1180 }
1181
1182 dev_dbg(&priv->i2c->dev, "%s(): bandwidth %d offset %d\n",
1183 __func__, bandwidth, *offset);
1184
1185 return 0;
1186}
1187
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03001188static int cxd2841er_get_carrier_offset_t(struct cxd2841er_priv *priv,
1189 u32 bandwidth, int *offset)
1190{
1191 u8 data[4];
1192
1193 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1194 if (priv->state != STATE_ACTIVE_TC) {
1195 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1196 __func__, priv->state);
1197 return -EINVAL;
1198 }
1199 if (priv->system != SYS_DVBT) {
1200 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1201 __func__, priv->system);
1202 return -EINVAL;
1203 }
1204 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1205 cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1206 *offset = -1 * sign_extend32(
1207 ((u32)(data[0] & 0x1F) << 24) | ((u32)data[1] << 16) |
1208 ((u32)data[2] << 8) | (u32)data[3], 29);
Abylay Ospan6c771612016-05-16 11:43:25 -03001209 *offset *= (bandwidth / 1000000);
1210 *offset /= 235;
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03001211 return 0;
1212}
1213
Mauro Carvalho Chehabc8946c82015-08-11 15:08:47 -03001214static int cxd2841er_get_carrier_offset_t2(struct cxd2841er_priv *priv,
1215 u32 bandwidth, int *offset)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001216{
1217 u8 data[4];
1218
1219 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1220 if (priv->state != STATE_ACTIVE_TC) {
1221 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1222 __func__, priv->state);
1223 return -EINVAL;
1224 }
1225 if (priv->system != SYS_DVBT2) {
1226 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1227 __func__, priv->system);
1228 return -EINVAL;
1229 }
1230 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1231 cxd2841er_read_regs(priv, I2C_SLVT, 0x4c, data, sizeof(data));
1232 *offset = -1 * sign_extend32(
1233 ((u32)(data[0] & 0x0F) << 24) | ((u32)data[1] << 16) |
1234 ((u32)data[2] << 8) | (u32)data[3], 27);
1235 switch (bandwidth) {
1236 case 1712000:
1237 *offset /= 582;
1238 break;
1239 case 5000000:
1240 case 6000000:
1241 case 7000000:
1242 case 8000000:
1243 *offset *= (bandwidth / 1000000);
1244 *offset /= 940;
1245 break;
1246 default:
1247 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
1248 __func__, bandwidth);
1249 return -EINVAL;
1250 }
1251 return 0;
1252}
1253
Mauro Carvalho Chehabc8946c82015-08-11 15:08:47 -03001254static int cxd2841er_get_carrier_offset_c(struct cxd2841er_priv *priv,
1255 int *offset)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001256{
1257 u8 data[2];
1258
1259 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1260 if (priv->state != STATE_ACTIVE_TC) {
1261 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1262 __func__, priv->state);
1263 return -EINVAL;
1264 }
1265 if (priv->system != SYS_DVBC_ANNEX_A) {
1266 dev_dbg(&priv->i2c->dev, "%s(): invalid delivery system %d\n",
1267 __func__, priv->system);
1268 return -EINVAL;
1269 }
1270 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1271 cxd2841er_read_regs(priv, I2C_SLVT, 0x15, data, sizeof(data));
1272 *offset = div_s64(41000LL * sign_extend32((((u32)data[0] & 0x3f) << 8)
1273 | (u32)data[1], 13), 16384);
1274 return 0;
1275}
1276
Abylay Ospana6f330c2016-07-15 15:34:22 -03001277static int cxd2841er_read_packet_errors_c(
1278 struct cxd2841er_priv *priv, u32 *penum)
1279{
1280 u8 data[3];
1281
1282 *penum = 0;
1283 if (priv->state != STATE_ACTIVE_TC) {
1284 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1285 __func__, priv->state);
1286 return -EINVAL;
1287 }
1288 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1289 cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
1290 if (data[2] & 0x01)
1291 *penum = ((u32)data[0] << 8) | (u32)data[1];
1292 return 0;
1293}
1294
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001295static int cxd2841er_read_packet_errors_t(
1296 struct cxd2841er_priv *priv, u32 *penum)
1297{
1298 u8 data[3];
1299
1300 *penum = 0;
1301 if (priv->state != STATE_ACTIVE_TC) {
1302 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1303 __func__, priv->state);
1304 return -EINVAL;
1305 }
1306 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1307 cxd2841er_read_regs(priv, I2C_SLVT, 0xea, data, sizeof(data));
1308 if (data[2] & 0x01)
1309 *penum = ((u32)data[0] << 8) | (u32)data[1];
1310 return 0;
1311}
1312
1313static int cxd2841er_read_packet_errors_t2(
1314 struct cxd2841er_priv *priv, u32 *penum)
1315{
1316 u8 data[3];
1317
1318 *penum = 0;
1319 if (priv->state != STATE_ACTIVE_TC) {
1320 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1321 __func__, priv->state);
1322 return -EINVAL;
1323 }
1324 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
1325 cxd2841er_read_regs(priv, I2C_SLVT, 0xfd, data, sizeof(data));
1326 if (data[0] & 0x01)
1327 *penum = ((u32)data[1] << 8) | (u32)data[2];
1328 return 0;
1329}
1330
Abylay Ospan83808c22016-03-22 19:20:34 -03001331static int cxd2841er_read_packet_errors_i(
1332 struct cxd2841er_priv *priv, u32 *penum)
1333{
1334 u8 data[2];
1335
1336 *penum = 0;
1337 if (priv->state != STATE_ACTIVE_TC) {
1338 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1339 __func__, priv->state);
1340 return -EINVAL;
1341 }
1342 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1343 cxd2841er_read_regs(priv, I2C_SLVT, 0xA1, data, 1);
1344
1345 if (!(data[0] & 0x01))
1346 return 0;
1347
1348 /* Layer A */
1349 cxd2841er_read_regs(priv, I2C_SLVT, 0xA2, data, sizeof(data));
1350 *penum = ((u32)data[0] << 8) | (u32)data[1];
1351
1352 /* Layer B */
1353 cxd2841er_read_regs(priv, I2C_SLVT, 0xA4, data, sizeof(data));
1354 *penum += ((u32)data[0] << 8) | (u32)data[1];
1355
1356 /* Layer C */
1357 cxd2841er_read_regs(priv, I2C_SLVT, 0xA6, data, sizeof(data));
1358 *penum += ((u32)data[0] << 8) | (u32)data[1];
1359
1360 return 0;
1361}
1362
Abylay Ospana6f330c2016-07-15 15:34:22 -03001363static int cxd2841er_read_ber_c(struct cxd2841er_priv *priv,
1364 u32 *bit_error, u32 *bit_count)
1365{
1366 u8 data[3];
1367 u32 bit_err, period_exp;
1368
1369 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1370 if (priv->state != STATE_ACTIVE_TC) {
1371 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1372 __func__, priv->state);
1373 return -EINVAL;
1374 }
1375 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1376 cxd2841er_read_regs(priv, I2C_SLVT, 0x62, data, sizeof(data));
1377 if (!(data[0] & 0x80)) {
1378 dev_dbg(&priv->i2c->dev,
1379 "%s(): no valid BER data\n", __func__);
1380 return -EINVAL;
1381 }
1382 bit_err = ((u32)(data[0] & 0x3f) << 16) |
1383 ((u32)data[1] << 8) |
1384 (u32)data[2];
1385 cxd2841er_read_reg(priv, I2C_SLVT, 0x60, data);
1386 period_exp = data[0] & 0x1f;
1387
1388 if ((period_exp <= 11) && (bit_err > (1 << period_exp) * 204 * 8)) {
1389 dev_dbg(&priv->i2c->dev,
1390 "%s(): period_exp(%u) or bit_err(%u) not in range. no valid BER data\n",
1391 __func__, period_exp, bit_err);
1392 return -EINVAL;
1393 }
1394
1395 dev_dbg(&priv->i2c->dev,
1396 "%s(): period_exp(%u) or bit_err(%u) count=%d\n",
1397 __func__, period_exp, bit_err,
1398 ((1 << period_exp) * 204 * 8));
1399
1400 *bit_error = bit_err;
1401 *bit_count = ((1 << period_exp) * 204 * 8);
1402
1403 return 0;
1404}
1405
Abylay Ospan0854df72016-07-19 12:22:03 -03001406static int cxd2841er_read_ber_i(struct cxd2841er_priv *priv,
1407 u32 *bit_error, u32 *bit_count)
1408{
1409 u8 data[3];
1410 u8 pktnum[2];
1411
1412 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1413 if (priv->state != STATE_ACTIVE_TC) {
1414 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
1415 __func__, priv->state);
1416 return -EINVAL;
1417 }
1418
1419 cxd2841er_freeze_regs(priv);
1420 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1421 cxd2841er_read_regs(priv, I2C_SLVT, 0x5B, pktnum, sizeof(pktnum));
1422 cxd2841er_read_regs(priv, I2C_SLVT, 0x16, data, sizeof(data));
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001423 cxd2841er_unfreeze_regs(priv);
Abylay Ospan0854df72016-07-19 12:22:03 -03001424
1425 if (!pktnum[0] && !pktnum[1]) {
1426 dev_dbg(&priv->i2c->dev,
1427 "%s(): no valid BER data\n", __func__);
Abylay Ospan0854df72016-07-19 12:22:03 -03001428 return -EINVAL;
1429 }
1430
1431 *bit_error = ((u32)(data[0] & 0x7F) << 16) |
1432 ((u32)data[1] << 8) | data[2];
1433 *bit_count = ((((u32)pktnum[0] << 8) | pktnum[1]) * 204 * 8);
1434 dev_dbg(&priv->i2c->dev, "%s(): bit_error=%u bit_count=%u\n",
1435 __func__, *bit_error, *bit_count);
1436
Abylay Ospan0854df72016-07-19 12:22:03 -03001437 return 0;
1438}
1439
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001440static int cxd2841er_mon_read_ber_s(struct cxd2841er_priv *priv,
1441 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001442{
1443 u8 data[11];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001444
1445 /* Set SLV-T Bank : 0xA0 */
1446 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1447 /*
1448 * slave Bank Addr Bit Signal name
1449 * <SLV-T> A0h 35h [0] IFVBER_VALID
1450 * <SLV-T> A0h 36h [5:0] IFVBER_BITERR[21:16]
1451 * <SLV-T> A0h 37h [7:0] IFVBER_BITERR[15:8]
1452 * <SLV-T> A0h 38h [7:0] IFVBER_BITERR[7:0]
1453 * <SLV-T> A0h 3Dh [5:0] IFVBER_BITNUM[21:16]
1454 * <SLV-T> A0h 3Eh [7:0] IFVBER_BITNUM[15:8]
1455 * <SLV-T> A0h 3Fh [7:0] IFVBER_BITNUM[7:0]
1456 */
1457 cxd2841er_read_regs(priv, I2C_SLVT, 0x35, data, 11);
1458 if (data[0] & 0x01) {
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001459 *bit_error = ((u32)(data[1] & 0x3F) << 16) |
1460 ((u32)(data[2] & 0xFF) << 8) |
1461 (u32)(data[3] & 0xFF);
1462 *bit_count = ((u32)(data[8] & 0x3F) << 16) |
1463 ((u32)(data[9] & 0xFF) << 8) |
1464 (u32)(data[10] & 0xFF);
1465 if ((*bit_count == 0) || (*bit_error > *bit_count)) {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001466 dev_dbg(&priv->i2c->dev,
1467 "%s(): invalid bit_error %d, bit_count %d\n",
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001468 __func__, *bit_error, *bit_count);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001469 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001470 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001471 return 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001472 }
1473 dev_dbg(&priv->i2c->dev, "%s(): no data available\n", __func__);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001474 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001475}
1476
1477
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001478static int cxd2841er_mon_read_ber_s2(struct cxd2841er_priv *priv,
1479 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001480{
1481 u8 data[5];
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001482 u32 period;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001483
1484 /* Set SLV-T Bank : 0xB2 */
1485 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xb2);
1486 /*
1487 * slave Bank Addr Bit Signal name
1488 * <SLV-T> B2h 30h [0] IFLBER_VALID
1489 * <SLV-T> B2h 31h [3:0] IFLBER_BITERR[27:24]
1490 * <SLV-T> B2h 32h [7:0] IFLBER_BITERR[23:16]
1491 * <SLV-T> B2h 33h [7:0] IFLBER_BITERR[15:8]
1492 * <SLV-T> B2h 34h [7:0] IFLBER_BITERR[7:0]
1493 */
1494 cxd2841er_read_regs(priv, I2C_SLVT, 0x30, data, 5);
1495 if (data[0] & 0x01) {
1496 /* Bit error count */
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001497 *bit_error = ((u32)(data[1] & 0x0F) << 24) |
1498 ((u32)(data[2] & 0xFF) << 16) |
1499 ((u32)(data[3] & 0xFF) << 8) |
1500 (u32)(data[4] & 0xFF);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001501
1502 /* Set SLV-T Bank : 0xA0 */
1503 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1504 cxd2841er_read_reg(priv, I2C_SLVT, 0x7a, data);
1505 /* Measurement period */
1506 period = (u32)(1 << (data[0] & 0x0F));
1507 if (period == 0) {
1508 dev_dbg(&priv->i2c->dev,
1509 "%s(): period is 0\n", __func__);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001510 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001511 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001512 if (*bit_error > (period * 64800)) {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001513 dev_dbg(&priv->i2c->dev,
1514 "%s(): invalid bit_err 0x%x period 0x%x\n",
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001515 __func__, *bit_error, period);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001516 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001517 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001518 *bit_count = period * 64800;
1519
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001520 return 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001521 } else {
1522 dev_dbg(&priv->i2c->dev,
1523 "%s(): no data available\n", __func__);
1524 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001525 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001526}
1527
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001528static int cxd2841er_read_ber_t2(struct cxd2841er_priv *priv,
1529 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001530{
1531 u8 data[4];
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001532 u32 period_exp, n_ldpc;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001533
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001534 if (priv->state != STATE_ACTIVE_TC) {
1535 dev_dbg(&priv->i2c->dev,
1536 "%s(): invalid state %d\n", __func__, priv->state);
1537 return -EINVAL;
1538 }
1539 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1540 cxd2841er_read_regs(priv, I2C_SLVT, 0x39, data, sizeof(data));
1541 if (!(data[0] & 0x10)) {
1542 dev_dbg(&priv->i2c->dev,
1543 "%s(): no valid BER data\n", __func__);
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001544 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001545 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001546 *bit_error = ((u32)(data[0] & 0x0f) << 24) |
1547 ((u32)data[1] << 16) |
1548 ((u32)data[2] << 8) |
1549 (u32)data[3];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001550 cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
1551 period_exp = data[0] & 0x0f;
1552 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x22);
1553 cxd2841er_read_reg(priv, I2C_SLVT, 0x5e, data);
1554 n_ldpc = ((data[0] & 0x03) == 0 ? 16200 : 64800);
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001555 if (*bit_error > ((1U << period_exp) * n_ldpc)) {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001556 dev_dbg(&priv->i2c->dev,
1557 "%s(): invalid BER value\n", __func__);
1558 return -EINVAL;
1559 }
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001560
1561 /*
1562 * FIXME: the right thing would be to return bit_error untouched,
1563 * but, as we don't know the scale returned by the counters, let's
1564 * at least preserver BER = bit_error/bit_count.
1565 */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001566 if (period_exp >= 4) {
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001567 *bit_count = (1U << (period_exp - 4)) * (n_ldpc / 200);
1568 *bit_error *= 3125ULL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001569 } else {
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001570 *bit_count = (1U << period_exp) * (n_ldpc / 200);
Abylay Ospana6f330c2016-07-15 15:34:22 -03001571 *bit_error *= 50000ULL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001572 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001573 return 0;
1574}
1575
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001576static int cxd2841er_read_ber_t(struct cxd2841er_priv *priv,
1577 u32 *bit_error, u32 *bit_count)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001578{
1579 u8 data[2];
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001580 u32 period;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001581
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001582 if (priv->state != STATE_ACTIVE_TC) {
1583 dev_dbg(&priv->i2c->dev,
1584 "%s(): invalid state %d\n", __func__, priv->state);
1585 return -EINVAL;
1586 }
1587 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1588 cxd2841er_read_reg(priv, I2C_SLVT, 0x39, data);
1589 if (!(data[0] & 0x01)) {
1590 dev_dbg(&priv->i2c->dev,
1591 "%s(): no valid BER data\n", __func__);
1592 return 0;
1593 }
1594 cxd2841er_read_regs(priv, I2C_SLVT, 0x22, data, sizeof(data));
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001595 *bit_error = ((u32)data[0] << 8) | (u32)data[1];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001596 cxd2841er_read_reg(priv, I2C_SLVT, 0x6f, data);
1597 period = ((data[0] & 0x07) == 0) ? 256 : (4096 << (data[0] & 0x07));
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001598
1599 /*
1600 * FIXME: the right thing would be to return bit_error untouched,
1601 * but, as we don't know the scale returned by the counters, let's
1602 * at least preserver BER = bit_error/bit_count.
1603 */
1604 *bit_count = period / 128;
1605 *bit_error *= 78125ULL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001606 return 0;
1607}
1608
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001609static int cxd2841er_freeze_regs(struct cxd2841er_priv *priv)
1610{
1611 /*
1612 * Freeze registers: ensure multiple separate register reads
1613 * are from the same snapshot
1614 */
1615 cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x01);
1616 return 0;
1617}
1618
1619static int cxd2841er_unfreeze_regs(struct cxd2841er_priv *priv)
1620{
1621 /*
1622 * un-freeze registers
1623 */
1624 cxd2841er_write_reg(priv, I2C_SLVT, 0x01, 0x00);
1625 return 0;
1626}
1627
Abylay Ospane05b1872016-07-15 17:04:17 -03001628static u32 cxd2841er_dvbs_read_snr(struct cxd2841er_priv *priv,
1629 u8 delsys, u32 *snr)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001630{
1631 u8 data[3];
1632 u32 res = 0, value;
1633 int min_index, max_index, index;
1634 static const struct cxd2841er_cnr_data *cn_data;
1635
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001636 cxd2841er_freeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001637 /* Set SLV-T Bank : 0xA1 */
1638 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa1);
1639 /*
1640 * slave Bank Addr Bit Signal name
1641 * <SLV-T> A1h 10h [0] ICPM_QUICKRDY
1642 * <SLV-T> A1h 11h [4:0] ICPM_QUICKCNDT[12:8]
1643 * <SLV-T> A1h 12h [7:0] ICPM_QUICKCNDT[7:0]
1644 */
1645 cxd2841er_read_regs(priv, I2C_SLVT, 0x10, data, 3);
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001646 cxd2841er_unfreeze_regs(priv);
1647
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001648 if (data[0] & 0x01) {
1649 value = ((u32)(data[1] & 0x1F) << 8) | (u32)(data[2] & 0xFF);
1650 min_index = 0;
1651 if (delsys == SYS_DVBS) {
1652 cn_data = s_cn_data;
1653 max_index = sizeof(s_cn_data) /
1654 sizeof(s_cn_data[0]) - 1;
1655 } else {
1656 cn_data = s2_cn_data;
1657 max_index = sizeof(s2_cn_data) /
1658 sizeof(s2_cn_data[0]) - 1;
1659 }
1660 if (value >= cn_data[min_index].value) {
1661 res = cn_data[min_index].cnr_x1000;
1662 goto done;
1663 }
1664 if (value <= cn_data[max_index].value) {
1665 res = cn_data[max_index].cnr_x1000;
1666 goto done;
1667 }
1668 while ((max_index - min_index) > 1) {
1669 index = (max_index + min_index) / 2;
1670 if (value == cn_data[index].value) {
1671 res = cn_data[index].cnr_x1000;
1672 goto done;
1673 } else if (value > cn_data[index].value)
1674 max_index = index;
1675 else
1676 min_index = index;
1677 if ((max_index - min_index) <= 1) {
1678 if (value == cn_data[max_index].value) {
1679 res = cn_data[max_index].cnr_x1000;
1680 goto done;
1681 } else {
1682 res = cn_data[min_index].cnr_x1000;
1683 goto done;
1684 }
1685 }
1686 }
1687 } else {
1688 dev_dbg(&priv->i2c->dev,
1689 "%s(): no data available\n", __func__);
Abylay Ospane05b1872016-07-15 17:04:17 -03001690 return -EINVAL;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001691 }
1692done:
Abylay Ospane05b1872016-07-15 17:04:17 -03001693 *snr = res;
1694 return 0;
1695}
1696
1697static uint32_t sony_log(uint32_t x)
1698{
1699 return (((10000>>8)*(intlog2(x)>>16) + LOG2_E_100X/2)/LOG2_E_100X);
1700}
1701
1702static int cxd2841er_read_snr_c(struct cxd2841er_priv *priv, u32 *snr)
1703{
1704 u32 reg;
1705 u8 data[2];
1706 enum sony_dvbc_constellation_t qam = SONY_DVBC_CONSTELLATION_16QAM;
1707
1708 *snr = 0;
1709 if (priv->state != STATE_ACTIVE_TC) {
1710 dev_dbg(&priv->i2c->dev,
1711 "%s(): invalid state %d\n",
1712 __func__, priv->state);
1713 return -EINVAL;
1714 }
1715
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001716 cxd2841er_freeze_regs(priv);
Abylay Ospane05b1872016-07-15 17:04:17 -03001717 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
1718 cxd2841er_read_regs(priv, I2C_SLVT, 0x19, data, 1);
1719 qam = (enum sony_dvbc_constellation_t) (data[0] & 0x07);
1720 cxd2841er_read_regs(priv, I2C_SLVT, 0x4C, data, 2);
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001721 cxd2841er_unfreeze_regs(priv);
Abylay Ospane05b1872016-07-15 17:04:17 -03001722
1723 reg = ((u32)(data[0]&0x1f) << 8) | (u32)data[1];
1724 if (reg == 0) {
1725 dev_dbg(&priv->i2c->dev,
1726 "%s(): reg value out of range\n", __func__);
1727 return 0;
1728 }
1729
1730 switch (qam) {
1731 case SONY_DVBC_CONSTELLATION_16QAM:
1732 case SONY_DVBC_CONSTELLATION_64QAM:
1733 case SONY_DVBC_CONSTELLATION_256QAM:
1734 /* SNR(dB) = -9.50 * ln(IREG_SNR_ESTIMATE / (24320)) */
1735 if (reg < 126)
1736 reg = 126;
1737 *snr = -95 * (int32_t)sony_log(reg) + 95941;
1738 break;
1739 case SONY_DVBC_CONSTELLATION_32QAM:
1740 case SONY_DVBC_CONSTELLATION_128QAM:
1741 /* SNR(dB) = -8.75 * ln(IREG_SNR_ESTIMATE / (20800)) */
1742 if (reg < 69)
1743 reg = 69;
1744 *snr = -88 * (int32_t)sony_log(reg) + 86999;
1745 break;
1746 default:
1747 return -EINVAL;
1748 }
1749
1750 return 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001751}
1752
1753static int cxd2841er_read_snr_t(struct cxd2841er_priv *priv, u32 *snr)
1754{
1755 u32 reg;
1756 u8 data[2];
1757
1758 *snr = 0;
1759 if (priv->state != STATE_ACTIVE_TC) {
1760 dev_dbg(&priv->i2c->dev,
1761 "%s(): invalid state %d\n", __func__, priv->state);
1762 return -EINVAL;
1763 }
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001764
1765 cxd2841er_freeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001766 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
1767 cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001768 cxd2841er_unfreeze_regs(priv);
1769
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001770 reg = ((u32)data[0] << 8) | (u32)data[1];
1771 if (reg == 0) {
1772 dev_dbg(&priv->i2c->dev,
1773 "%s(): reg value out of range\n", __func__);
1774 return 0;
1775 }
1776 if (reg > 4996)
1777 reg = 4996;
1778 *snr = 10000 * ((intlog10(reg) - intlog10(5350 - reg)) >> 24) + 28500;
1779 return 0;
1780}
1781
Mauro Carvalho Chehabc8946c82015-08-11 15:08:47 -03001782static int cxd2841er_read_snr_t2(struct cxd2841er_priv *priv, u32 *snr)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001783{
1784 u32 reg;
1785 u8 data[2];
1786
1787 *snr = 0;
1788 if (priv->state != STATE_ACTIVE_TC) {
1789 dev_dbg(&priv->i2c->dev,
1790 "%s(): invalid state %d\n", __func__, priv->state);
1791 return -EINVAL;
1792 }
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001793
1794 cxd2841er_freeze_regs(priv);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001795 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
1796 cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001797 cxd2841er_unfreeze_regs(priv);
1798
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001799 reg = ((u32)data[0] << 8) | (u32)data[1];
1800 if (reg == 0) {
1801 dev_dbg(&priv->i2c->dev,
1802 "%s(): reg value out of range\n", __func__);
1803 return 0;
1804 }
1805 if (reg > 10876)
1806 reg = 10876;
1807 *snr = 10000 * ((intlog10(reg) -
1808 intlog10(12600 - reg)) >> 24) + 32000;
1809 return 0;
1810}
1811
Abylay Ospan83808c22016-03-22 19:20:34 -03001812static int cxd2841er_read_snr_i(struct cxd2841er_priv *priv, u32 *snr)
1813{
1814 u32 reg;
1815 u8 data[2];
1816
1817 *snr = 0;
1818 if (priv->state != STATE_ACTIVE_TC) {
1819 dev_dbg(&priv->i2c->dev,
1820 "%s(): invalid state %d\n", __func__,
1821 priv->state);
1822 return -EINVAL;
1823 }
1824
Abylay Ospan4a86bc12016-07-19 00:10:20 -03001825 cxd2841er_freeze_regs(priv);
Abylay Ospan83808c22016-03-22 19:20:34 -03001826 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
1827 cxd2841er_read_regs(priv, I2C_SLVT, 0x28, data, sizeof(data));
Daniel Scheller6ccf8212017-04-09 16:38:12 -03001828 cxd2841er_unfreeze_regs(priv);
1829
Abylay Ospan83808c22016-03-22 19:20:34 -03001830 reg = ((u32)data[0] << 8) | (u32)data[1];
1831 if (reg == 0) {
1832 dev_dbg(&priv->i2c->dev,
1833 "%s(): reg value out of range\n", __func__);
1834 return 0;
1835 }
Abylay Ospan0854df72016-07-19 12:22:03 -03001836 *snr = 10000 * (intlog10(reg) >> 24) - 9031;
Abylay Ospan83808c22016-03-22 19:20:34 -03001837 return 0;
1838}
1839
Abylay Ospand0998ce2016-06-30 23:09:48 -03001840static u16 cxd2841er_read_agc_gain_c(struct cxd2841er_priv *priv,
1841 u8 delsys)
1842{
1843 u8 data[2];
1844
1845 cxd2841er_write_reg(
1846 priv, I2C_SLVT, 0x00, 0x40);
1847 cxd2841er_read_regs(priv, I2C_SLVT, 0x49, data, 2);
1848 dev_dbg(&priv->i2c->dev,
1849 "%s(): AGC value=%u\n",
1850 __func__, (((u16)data[0] & 0x0F) << 8) |
1851 (u16)(data[1] & 0xFF));
1852 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1853}
1854
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001855static u16 cxd2841er_read_agc_gain_t_t2(struct cxd2841er_priv *priv,
1856 u8 delsys)
1857{
1858 u8 data[2];
1859
1860 cxd2841er_write_reg(
1861 priv, I2C_SLVT, 0x00, (delsys == SYS_DVBT ? 0x10 : 0x20));
1862 cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03001863 dev_dbg(&priv->i2c->dev,
1864 "%s(): AGC value=%u\n",
1865 __func__, (((u16)data[0] & 0x0F) << 8) |
1866 (u16)(data[1] & 0xFF));
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001867 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1868}
1869
Abylay Ospan83808c22016-03-22 19:20:34 -03001870static u16 cxd2841er_read_agc_gain_i(struct cxd2841er_priv *priv,
1871 u8 delsys)
1872{
1873 u8 data[2];
1874
1875 cxd2841er_write_reg(
1876 priv, I2C_SLVT, 0x00, 0x60);
1877 cxd2841er_read_regs(priv, I2C_SLVT, 0x26, data, 2);
1878
1879 dev_dbg(&priv->i2c->dev,
1880 "%s(): AGC value=%u\n",
1881 __func__, (((u16)data[0] & 0x0F) << 8) |
1882 (u16)(data[1] & 0xFF));
1883 return ((((u16)data[0] & 0x0F) << 8) | (u16)(data[1] & 0xFF)) << 4;
1884}
1885
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001886static u16 cxd2841er_read_agc_gain_s(struct cxd2841er_priv *priv)
1887{
1888 u8 data[2];
1889
1890 /* Set SLV-T Bank : 0xA0 */
1891 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
1892 /*
1893 * slave Bank Addr Bit Signal name
1894 * <SLV-T> A0h 1Fh [4:0] IRFAGC_GAIN[12:8]
1895 * <SLV-T> A0h 20h [7:0] IRFAGC_GAIN[7:0]
1896 */
1897 cxd2841er_read_regs(priv, I2C_SLVT, 0x1f, data, 2);
1898 return ((((u16)data[0] & 0x1F) << 8) | (u16)(data[1] & 0xFF)) << 3;
1899}
1900
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001901static void cxd2841er_read_ber(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001902{
1903 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1904 struct cxd2841er_priv *priv = fe->demodulator_priv;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001905 u32 ret, bit_error = 0, bit_count = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001906
1907 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001908 switch (p->delivery_system) {
Abylay Ospana6f330c2016-07-15 15:34:22 -03001909 case SYS_DVBC_ANNEX_A:
1910 case SYS_DVBC_ANNEX_B:
1911 case SYS_DVBC_ANNEX_C:
1912 ret = cxd2841er_read_ber_c(priv, &bit_error, &bit_count);
1913 break;
Abylay Ospan0854df72016-07-19 12:22:03 -03001914 case SYS_ISDBT:
1915 ret = cxd2841er_read_ber_i(priv, &bit_error, &bit_count);
1916 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001917 case SYS_DVBS:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001918 ret = cxd2841er_mon_read_ber_s(priv, &bit_error, &bit_count);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001919 break;
1920 case SYS_DVBS2:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001921 ret = cxd2841er_mon_read_ber_s2(priv, &bit_error, &bit_count);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001922 break;
1923 case SYS_DVBT:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001924 ret = cxd2841er_read_ber_t(priv, &bit_error, &bit_count);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001925 break;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001926 case SYS_DVBT2:
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001927 ret = cxd2841er_read_ber_t2(priv, &bit_error, &bit_count);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001928 break;
1929 default:
1930 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001931 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001932 return;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001933 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001934
1935 if (!ret) {
1936 p->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
Abylay Ospana6f330c2016-07-15 15:34:22 -03001937 p->post_bit_error.stat[0].uvalue += bit_error;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001938 p->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
Abylay Ospana6f330c2016-07-15 15:34:22 -03001939 p->post_bit_count.stat[0].uvalue += bit_count;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001940 } else {
1941 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03001942 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001943 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001944}
1945
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03001946static void cxd2841er_read_signal_strength(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001947{
1948 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
1949 struct cxd2841er_priv *priv = fe->demodulator_priv;
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001950 s32 strength;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001951
1952 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
1953 switch (p->delivery_system) {
1954 case SYS_DVBT:
1955 case SYS_DVBT2:
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03001956 strength = cxd2841er_read_agc_gain_t_t2(priv,
1957 p->delivery_system);
1958 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1959 /* Formula was empirically determinated @ 410 MHz */
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001960 p->strength.stat[0].uvalue = strength * 366 / 100 - 89520;
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03001961 break; /* Code moved out of the function */
Mauro Carvalho Chehab988bd282016-07-01 11:03:14 -03001962 case SYS_DVBC_ANNEX_A:
Abylay Ospan997bdc02016-07-15 14:59:37 -03001963 case SYS_DVBC_ANNEX_B:
1964 case SYS_DVBC_ANNEX_C:
1965 strength = cxd2841er_read_agc_gain_c(priv,
Mauro Carvalho Chehab988bd282016-07-01 11:03:14 -03001966 p->delivery_system);
Mauro Carvalho Chehabd12b7912016-07-01 11:03:16 -03001967 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1968 /*
1969 * Formula was empirically determinated via linear regression,
1970 * using frequencies: 175 MHz, 410 MHz and 800 MHz, and a
1971 * stream modulated with QAM64
1972 */
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001973 p->strength.stat[0].uvalue = strength * 4045 / 1000 - 85224;
Mauro Carvalho Chehab988bd282016-07-01 11:03:14 -03001974 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03001975 case SYS_ISDBT:
Mauro Carvalho Chehab313a7df2016-07-01 15:41:38 -03001976 strength = cxd2841er_read_agc_gain_i(priv, p->delivery_system);
1977 p->strength.stat[0].scale = FE_SCALE_DECIBEL;
1978 /*
1979 * Formula was empirically determinated via linear regression,
1980 * using frequencies: 175 MHz, 410 MHz and 800 MHz.
1981 */
1982 p->strength.stat[0].uvalue = strength * 3775 / 1000 - 90185;
Abylay Ospan83808c22016-03-22 19:20:34 -03001983 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001984 case SYS_DVBS:
1985 case SYS_DVBS2:
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03001986 strength = 65535 - cxd2841er_read_agc_gain_s(priv);
1987 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
1988 p->strength.stat[0].uvalue = strength;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001989 break;
1990 default:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001991 p->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001992 break;
1993 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001994}
1995
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03001996static void cxd2841er_read_snr(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03001997{
1998 u32 tmp = 0;
Abylay Ospane05b1872016-07-15 17:04:17 -03001999 int ret = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002000 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2001 struct cxd2841er_priv *priv = fe->demodulator_priv;
2002
2003 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2004 switch (p->delivery_system) {
Abylay Ospane05b1872016-07-15 17:04:17 -03002005 case SYS_DVBC_ANNEX_A:
2006 case SYS_DVBC_ANNEX_B:
2007 case SYS_DVBC_ANNEX_C:
2008 ret = cxd2841er_read_snr_c(priv, &tmp);
2009 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002010 case SYS_DVBT:
Abylay Ospane05b1872016-07-15 17:04:17 -03002011 ret = cxd2841er_read_snr_t(priv, &tmp);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002012 break;
2013 case SYS_DVBT2:
Abylay Ospane05b1872016-07-15 17:04:17 -03002014 ret = cxd2841er_read_snr_t2(priv, &tmp);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002015 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03002016 case SYS_ISDBT:
Abylay Ospane05b1872016-07-15 17:04:17 -03002017 ret = cxd2841er_read_snr_i(priv, &tmp);
Abylay Ospan83808c22016-03-22 19:20:34 -03002018 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002019 case SYS_DVBS:
2020 case SYS_DVBS2:
Abylay Ospane05b1872016-07-15 17:04:17 -03002021 ret = cxd2841er_dvbs_read_snr(priv, p->delivery_system, &tmp);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002022 break;
2023 default:
2024 dev_dbg(&priv->i2c->dev, "%s(): unknown delivery system %d\n",
2025 __func__, p->delivery_system);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002026 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2027 return;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002028 }
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002029
Abylay Ospan0854df72016-07-19 12:22:03 -03002030 dev_dbg(&priv->i2c->dev, "%s(): snr=%d\n",
2031 __func__, (int32_t)tmp);
2032
Abylay Ospane05b1872016-07-15 17:04:17 -03002033 if (!ret) {
2034 p->cnr.stat[0].scale = FE_SCALE_DECIBEL;
2035 p->cnr.stat[0].svalue = tmp;
2036 } else {
2037 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2038 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002039}
2040
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002041static void cxd2841er_read_ucblocks(struct dvb_frontend *fe)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002042{
2043 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
2044 struct cxd2841er_priv *priv = fe->demodulator_priv;
Abylay Ospan4a86bc12016-07-19 00:10:20 -03002045 u32 ucblocks = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002046
2047 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2048 switch (p->delivery_system) {
Abylay Ospana6f330c2016-07-15 15:34:22 -03002049 case SYS_DVBC_ANNEX_A:
2050 case SYS_DVBC_ANNEX_B:
2051 case SYS_DVBC_ANNEX_C:
2052 cxd2841er_read_packet_errors_c(priv, &ucblocks);
2053 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002054 case SYS_DVBT:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002055 cxd2841er_read_packet_errors_t(priv, &ucblocks);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002056 break;
2057 case SYS_DVBT2:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002058 cxd2841er_read_packet_errors_t2(priv, &ucblocks);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002059 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03002060 case SYS_ISDBT:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002061 cxd2841er_read_packet_errors_i(priv, &ucblocks);
Abylay Ospan83808c22016-03-22 19:20:34 -03002062 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002063 default:
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002064 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
2065 return;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002066 }
Abylay Ospan4a86bc12016-07-19 00:10:20 -03002067 dev_dbg(&priv->i2c->dev, "%s() ucblocks=%u\n", __func__, ucblocks);
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03002068
2069 p->block_error.stat[0].scale = FE_SCALE_COUNTER;
2070 p->block_error.stat[0].uvalue = ucblocks;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002071}
2072
2073static int cxd2841er_dvbt2_set_profile(
2074 struct cxd2841er_priv *priv, enum cxd2841er_dvbt2_profile_t profile)
2075{
2076 u8 tune_mode;
2077 u8 seq_not2d_time;
2078
2079 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2080 switch (profile) {
2081 case DVBT2_PROFILE_BASE:
2082 tune_mode = 0x01;
Abylay Ospan6c771612016-05-16 11:43:25 -03002083 /* Set early unlock time */
2084 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x0E:0x0C;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002085 break;
2086 case DVBT2_PROFILE_LITE:
2087 tune_mode = 0x05;
Abylay Ospan6c771612016-05-16 11:43:25 -03002088 /* Set early unlock time */
2089 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002090 break;
2091 case DVBT2_PROFILE_ANY:
2092 tune_mode = 0x00;
Abylay Ospan6c771612016-05-16 11:43:25 -03002093 /* Set early unlock time */
2094 seq_not2d_time = (priv->xtal == SONY_XTAL_24000)?0x2E:0x28;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002095 break;
2096 default:
2097 return -EINVAL;
2098 }
2099 /* Set SLV-T Bank : 0x2E */
2100 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2e);
2101 /* Set profile and tune mode */
2102 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x10, tune_mode, 0x07);
2103 /* Set SLV-T Bank : 0x2B */
2104 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2105 /* Set early unlock detection time */
2106 cxd2841er_write_reg(priv, I2C_SLVT, 0x9d, seq_not2d_time);
2107 return 0;
2108}
2109
2110static int cxd2841er_dvbt2_set_plp_config(struct cxd2841er_priv *priv,
2111 u8 is_auto, u8 plp_id)
2112{
2113 if (is_auto) {
2114 dev_dbg(&priv->i2c->dev,
2115 "%s() using auto PLP selection\n", __func__);
2116 } else {
2117 dev_dbg(&priv->i2c->dev,
2118 "%s() using manual PLP selection, ID %d\n",
2119 __func__, plp_id);
2120 }
2121 /* Set SLV-T Bank : 0x23 */
2122 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
2123 if (!is_auto) {
2124 /* Manual PLP selection mode. Set the data PLP Id. */
2125 cxd2841er_write_reg(priv, I2C_SLVT, 0xaf, plp_id);
2126 }
2127 /* Auto PLP select (Scanning mode = 0x00). Data PLP select = 0x01. */
2128 cxd2841er_write_reg(priv, I2C_SLVT, 0xad, (is_auto ? 0x00 : 0x01));
2129 return 0;
2130}
2131
2132static int cxd2841er_sleep_tc_to_active_t2_band(struct cxd2841er_priv *priv,
2133 u32 bandwidth)
2134{
2135 u32 iffreq;
Abylay Ospan6c771612016-05-16 11:43:25 -03002136 u8 data[MAX_WRITE_REGSIZE];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002137
Abylay Ospan6c771612016-05-16 11:43:25 -03002138 const uint8_t nominalRate8bw[3][5] = {
2139 /* TRCG Nominal Rate [37:0] */
2140 {0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2141 {0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2142 {0x11, 0xF0, 0x00, 0x00, 0x00} /* 41MHz XTal */
2143 };
2144
2145 const uint8_t nominalRate7bw[3][5] = {
2146 /* TRCG Nominal Rate [37:0] */
2147 {0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2148 {0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2149 {0x14, 0x80, 0x00, 0x00, 0x00} /* 41MHz XTal */
2150 };
2151
2152 const uint8_t nominalRate6bw[3][5] = {
2153 /* TRCG Nominal Rate [37:0] */
2154 {0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
2155 {0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2156 {0x17, 0xEA, 0xAA, 0xAA, 0xAA} /* 41MHz XTal */
2157 };
2158
2159 const uint8_t nominalRate5bw[3][5] = {
2160 /* TRCG Nominal Rate [37:0] */
2161 {0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
2162 {0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
2163 {0x1C, 0xB3, 0x33, 0x33, 0x33} /* 41MHz XTal */
2164 };
2165
2166 const uint8_t nominalRate17bw[3][5] = {
2167 /* TRCG Nominal Rate [37:0] */
2168 {0x58, 0xE2, 0xAF, 0xE0, 0xBC}, /* 20.5MHz XTal */
2169 {0x68, 0x0F, 0xA2, 0x32, 0xD0}, /* 24MHz XTal */
2170 {0x58, 0xE2, 0xAF, 0xE0, 0xBC} /* 41MHz XTal */
2171 };
2172
2173 const uint8_t itbCoef8bw[3][14] = {
2174 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
2175 0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
2176 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1,
2177 0x29, 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal */
2178 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA,
2179 0x23, 0xA9, 0x1F, 0xA8, 0x2C, 0xC8} /* 41MHz XTal */
2180 };
2181
2182 const uint8_t itbCoef7bw[3][14] = {
2183 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
2184 0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
2185 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0,
2186 0x29, 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal */
2187 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6,
2188 0x29, 0xB0, 0x26, 0xA9, 0x21, 0xA5} /* 41MHz XTal */
2189 };
2190
2191 const uint8_t itbCoef6bw[3][14] = {
2192 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2193 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2194 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
2195 0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2196 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2197 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2198 };
2199
2200 const uint8_t itbCoef5bw[3][14] = {
2201 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2202 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2203 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E,
2204 0x29, 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2205 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2206 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2207 };
2208
2209 const uint8_t itbCoef17bw[3][14] = {
2210 {0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
2211 0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99}, /* 20.5MHz XTal */
2212 {0x33, 0x8E, 0x2B, 0x97, 0x2D, 0x95, 0x37, 0x8B,
2213 0x30, 0x97, 0x2D, 0x9A, 0x21, 0xA4}, /* 24MHz XTal */
2214 {0x25, 0xA0, 0x36, 0x8D, 0x2E, 0x94, 0x28, 0x9B,
2215 0x32, 0x90, 0x2C, 0x9D, 0x29, 0x99} /* 41MHz XTal */
2216 };
2217
2218 /* Set SLV-T Bank : 0x20 */
2219 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
2220
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002221 switch (bandwidth) {
2222 case 8000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002223 /* <Timing Recovery setting> */
2224 cxd2841er_write_regs(priv, I2C_SLVT,
2225 0x9F, nominalRate8bw[priv->xtal], 5);
2226
2227 /* Set SLV-T Bank : 0x27 */
2228 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2229 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2230 0x7a, 0x00, 0x0f);
2231
2232 /* Set SLV-T Bank : 0x10 */
2233 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2234
2235 /* Group delay equaliser settings for
2236 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2237 */
2238 cxd2841er_write_regs(priv, I2C_SLVT,
2239 0xA6, itbCoef8bw[priv->xtal], 14);
2240 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002241 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4800000);
Abylay Ospan6c771612016-05-16 11:43:25 -03002242 data[0] = (u8) ((iffreq >> 16) & 0xff);
2243 data[1] = (u8)((iffreq >> 8) & 0xff);
2244 data[2] = (u8)(iffreq & 0xff);
2245 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2246 /* System bandwidth setting */
2247 cxd2841er_set_reg_bits(
2248 priv, I2C_SLVT, 0xD7, 0x00, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002249 break;
2250 case 7000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002251 /* <Timing Recovery setting> */
2252 cxd2841er_write_regs(priv, I2C_SLVT,
2253 0x9F, nominalRate7bw[priv->xtal], 5);
2254
2255 /* Set SLV-T Bank : 0x27 */
2256 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2257 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2258 0x7a, 0x00, 0x0f);
2259
2260 /* Set SLV-T Bank : 0x10 */
2261 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2262
2263 /* Group delay equaliser settings for
2264 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2265 */
2266 cxd2841er_write_regs(priv, I2C_SLVT,
2267 0xA6, itbCoef7bw[priv->xtal], 14);
2268 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002269 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4200000);
Abylay Ospan6c771612016-05-16 11:43:25 -03002270 data[0] = (u8) ((iffreq >> 16) & 0xff);
2271 data[1] = (u8)((iffreq >> 8) & 0xff);
2272 data[2] = (u8)(iffreq & 0xff);
2273 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2274 /* System bandwidth setting */
2275 cxd2841er_set_reg_bits(
2276 priv, I2C_SLVT, 0xD7, 0x02, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002277 break;
2278 case 6000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002279 /* <Timing Recovery setting> */
2280 cxd2841er_write_regs(priv, I2C_SLVT,
2281 0x9F, nominalRate6bw[priv->xtal], 5);
2282
2283 /* Set SLV-T Bank : 0x27 */
2284 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2285 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2286 0x7a, 0x00, 0x0f);
2287
2288 /* Set SLV-T Bank : 0x10 */
2289 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2290
2291 /* Group delay equaliser settings for
2292 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2293 */
2294 cxd2841er_write_regs(priv, I2C_SLVT,
2295 0xA6, itbCoef6bw[priv->xtal], 14);
2296 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002297 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3600000);
Abylay Ospan6c771612016-05-16 11:43:25 -03002298 data[0] = (u8) ((iffreq >> 16) & 0xff);
2299 data[1] = (u8)((iffreq >> 8) & 0xff);
2300 data[2] = (u8)(iffreq & 0xff);
2301 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2302 /* System bandwidth setting */
2303 cxd2841er_set_reg_bits(
2304 priv, I2C_SLVT, 0xD7, 0x04, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002305 break;
2306 case 5000000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002307 /* <Timing Recovery setting> */
2308 cxd2841er_write_regs(priv, I2C_SLVT,
2309 0x9F, nominalRate5bw[priv->xtal], 5);
2310
2311 /* Set SLV-T Bank : 0x27 */
2312 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2313 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2314 0x7a, 0x00, 0x0f);
2315
2316 /* Set SLV-T Bank : 0x10 */
2317 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2318
2319 /* Group delay equaliser settings for
2320 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2321 */
2322 cxd2841er_write_regs(priv, I2C_SLVT,
2323 0xA6, itbCoef5bw[priv->xtal], 14);
2324 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002325 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3600000);
Abylay Ospan6c771612016-05-16 11:43:25 -03002326 data[0] = (u8) ((iffreq >> 16) & 0xff);
2327 data[1] = (u8)((iffreq >> 8) & 0xff);
2328 data[2] = (u8)(iffreq & 0xff);
2329 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2330 /* System bandwidth setting */
2331 cxd2841er_set_reg_bits(
2332 priv, I2C_SLVT, 0xD7, 0x06, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002333 break;
2334 case 1712000:
Abylay Ospan6c771612016-05-16 11:43:25 -03002335 /* <Timing Recovery setting> */
2336 cxd2841er_write_regs(priv, I2C_SLVT,
2337 0x9F, nominalRate17bw[priv->xtal], 5);
2338
2339 /* Set SLV-T Bank : 0x27 */
2340 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
2341 cxd2841er_set_reg_bits(priv, I2C_SLVT,
2342 0x7a, 0x03, 0x0f);
2343
2344 /* Set SLV-T Bank : 0x10 */
2345 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2346
2347 /* Group delay equaliser settings for
2348 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2349 */
2350 cxd2841er_write_regs(priv, I2C_SLVT,
2351 0xA6, itbCoef17bw[priv->xtal], 14);
2352 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002353 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3500000);
Abylay Ospan6c771612016-05-16 11:43:25 -03002354 data[0] = (u8) ((iffreq >> 16) & 0xff);
2355 data[1] = (u8)((iffreq >> 8) & 0xff);
2356 data[2] = (u8)(iffreq & 0xff);
2357 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2358 /* System bandwidth setting */
2359 cxd2841er_set_reg_bits(
2360 priv, I2C_SLVT, 0xD7, 0x03, 0x07);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002361 break;
2362 default:
2363 return -EINVAL;
2364 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002365 return 0;
2366}
2367
2368static int cxd2841er_sleep_tc_to_active_t_band(
2369 struct cxd2841er_priv *priv, u32 bandwidth)
2370{
Abylay Ospan83808c22016-03-22 19:20:34 -03002371 u8 data[MAX_WRITE_REGSIZE];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002372 u32 iffreq;
Abylay Ospan83808c22016-03-22 19:20:34 -03002373 u8 nominalRate8bw[3][5] = {
2374 /* TRCG Nominal Rate [37:0] */
2375 {0x11, 0xF0, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2376 {0x15, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2377 {0x11, 0xF0, 0x00, 0x00, 0x00} /* 41MHz XTal */
2378 };
2379 u8 nominalRate7bw[3][5] = {
2380 /* TRCG Nominal Rate [37:0] */
2381 {0x14, 0x80, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2382 {0x18, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2383 {0x14, 0x80, 0x00, 0x00, 0x00} /* 41MHz XTal */
2384 };
2385 u8 nominalRate6bw[3][5] = {
2386 /* TRCG Nominal Rate [37:0] */
2387 {0x17, 0xEA, 0xAA, 0xAA, 0xAA}, /* 20.5MHz XTal */
2388 {0x1C, 0x00, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2389 {0x17, 0xEA, 0xAA, 0xAA, 0xAA} /* 41MHz XTal */
2390 };
2391 u8 nominalRate5bw[3][5] = {
2392 /* TRCG Nominal Rate [37:0] */
2393 {0x1C, 0xB3, 0x33, 0x33, 0x33}, /* 20.5MHz XTal */
2394 {0x21, 0x99, 0x99, 0x99, 0x99}, /* 24MHz XTal */
2395 {0x1C, 0xB3, 0x33, 0x33, 0x33} /* 41MHz XTal */
2396 };
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002397
Abylay Ospan83808c22016-03-22 19:20:34 -03002398 u8 itbCoef8bw[3][14] = {
2399 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
2400 0x1F, 0xA8, 0x2C, 0xC8}, /* 20.5MHz XTal */
2401 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29, 0xA5,
2402 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz XTal */
2403 {0x26, 0xAF, 0x06, 0xCD, 0x13, 0xBB, 0x28, 0xBA, 0x23, 0xA9,
2404 0x1F, 0xA8, 0x2C, 0xC8} /* 41MHz XTal */
2405 };
2406 u8 itbCoef7bw[3][14] = {
2407 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
2408 0x26, 0xA9, 0x21, 0xA5}, /* 20.5MHz XTal */
2409 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29, 0xA2,
2410 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz XTal */
2411 {0x2C, 0xBD, 0x02, 0xCF, 0x04, 0xF8, 0x23, 0xA6, 0x29, 0xB0,
2412 0x26, 0xA9, 0x21, 0xA5} /* 41MHz XTal */
2413 };
2414 u8 itbCoef6bw[3][14] = {
2415 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2416 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2417 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
2418 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2419 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2420 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2421 };
2422 u8 itbCoef5bw[3][14] = {
2423 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2424 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2425 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29, 0xA4,
2426 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz XTal */
2427 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00, 0xCF,
2428 0x00, 0xE6, 0x23, 0xA4} /* 41MHz XTal */
2429 };
2430
2431 /* Set SLV-T Bank : 0x13 */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002432 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
2433 /* Echo performance optimization setting */
Abylay Ospan83808c22016-03-22 19:20:34 -03002434 data[0] = 0x01;
2435 data[1] = 0x14;
2436 cxd2841er_write_regs(priv, I2C_SLVT, 0x9C, data, 2);
2437
2438 /* Set SLV-T Bank : 0x10 */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002439 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2440
2441 switch (bandwidth) {
2442 case 8000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002443 /* <Timing Recovery setting> */
2444 cxd2841er_write_regs(priv, I2C_SLVT,
2445 0x9F, nominalRate8bw[priv->xtal], 5);
2446 /* Group delay equaliser settings for
2447 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2448 */
2449 cxd2841er_write_regs(priv, I2C_SLVT,
2450 0xA6, itbCoef8bw[priv->xtal], 14);
2451 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002452 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4800000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002453 data[0] = (u8) ((iffreq >> 16) & 0xff);
2454 data[1] = (u8)((iffreq >> 8) & 0xff);
2455 data[2] = (u8)(iffreq & 0xff);
2456 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2457 /* System bandwidth setting */
2458 cxd2841er_set_reg_bits(
2459 priv, I2C_SLVT, 0xD7, 0x00, 0x07);
2460
2461 /* Demod core latency setting */
2462 if (priv->xtal == SONY_XTAL_24000) {
2463 data[0] = 0x15;
2464 data[1] = 0x28;
2465 } else {
2466 data[0] = 0x01;
2467 data[1] = 0xE0;
2468 }
2469 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2470
2471 /* Notch filter setting */
2472 data[0] = 0x01;
2473 data[1] = 0x02;
2474 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2475 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002476 break;
2477 case 7000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002478 /* <Timing Recovery setting> */
2479 cxd2841er_write_regs(priv, I2C_SLVT,
2480 0x9F, nominalRate7bw[priv->xtal], 5);
2481 /* Group delay equaliser settings for
2482 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2483 */
2484 cxd2841er_write_regs(priv, I2C_SLVT,
2485 0xA6, itbCoef7bw[priv->xtal], 14);
2486 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002487 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4200000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002488 data[0] = (u8) ((iffreq >> 16) & 0xff);
2489 data[1] = (u8)((iffreq >> 8) & 0xff);
2490 data[2] = (u8)(iffreq & 0xff);
2491 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2492 /* System bandwidth setting */
2493 cxd2841er_set_reg_bits(
2494 priv, I2C_SLVT, 0xD7, 0x02, 0x07);
2495
2496 /* Demod core latency setting */
2497 if (priv->xtal == SONY_XTAL_24000) {
2498 data[0] = 0x1F;
2499 data[1] = 0xF8;
2500 } else {
2501 data[0] = 0x12;
2502 data[1] = 0xF8;
2503 }
2504 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2505
2506 /* Notch filter setting */
2507 data[0] = 0x00;
2508 data[1] = 0x03;
2509 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2510 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002511 break;
2512 case 6000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002513 /* <Timing Recovery setting> */
2514 cxd2841er_write_regs(priv, I2C_SLVT,
2515 0x9F, nominalRate6bw[priv->xtal], 5);
2516 /* Group delay equaliser settings for
2517 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2518 */
2519 cxd2841er_write_regs(priv, I2C_SLVT,
2520 0xA6, itbCoef6bw[priv->xtal], 14);
2521 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002522 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3600000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002523 data[0] = (u8) ((iffreq >> 16) & 0xff);
2524 data[1] = (u8)((iffreq >> 8) & 0xff);
2525 data[2] = (u8)(iffreq & 0xff);
2526 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2527 /* System bandwidth setting */
2528 cxd2841er_set_reg_bits(
2529 priv, I2C_SLVT, 0xD7, 0x04, 0x07);
2530
2531 /* Demod core latency setting */
2532 if (priv->xtal == SONY_XTAL_24000) {
2533 data[0] = 0x25;
2534 data[1] = 0x4C;
2535 } else {
2536 data[0] = 0x1F;
2537 data[1] = 0xDC;
2538 }
2539 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2540
2541 /* Notch filter setting */
2542 data[0] = 0x00;
2543 data[1] = 0x03;
2544 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2545 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002546 break;
2547 case 5000000:
Abylay Ospan83808c22016-03-22 19:20:34 -03002548 /* <Timing Recovery setting> */
2549 cxd2841er_write_regs(priv, I2C_SLVT,
2550 0x9F, nominalRate5bw[priv->xtal], 5);
2551 /* Group delay equaliser settings for
2552 * ASCOT2D, ASCOT2E and ASCOT3 tuners
2553 */
2554 cxd2841er_write_regs(priv, I2C_SLVT,
2555 0xA6, itbCoef5bw[priv->xtal], 14);
2556 /* <IF freq setting> */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002557 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3600000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002558 data[0] = (u8) ((iffreq >> 16) & 0xff);
2559 data[1] = (u8)((iffreq >> 8) & 0xff);
2560 data[2] = (u8)(iffreq & 0xff);
2561 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2562 /* System bandwidth setting */
2563 cxd2841er_set_reg_bits(
2564 priv, I2C_SLVT, 0xD7, 0x06, 0x07);
2565
2566 /* Demod core latency setting */
2567 if (priv->xtal == SONY_XTAL_24000) {
2568 data[0] = 0x2C;
2569 data[1] = 0xC2;
2570 } else {
2571 data[0] = 0x26;
2572 data[1] = 0x3C;
2573 }
2574 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2575
2576 /* Notch filter setting */
2577 data[0] = 0x00;
2578 data[1] = 0x03;
2579 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x17);
2580 cxd2841er_write_regs(priv, I2C_SLVT, 0x38, data, 2);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002581 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03002582 }
2583
2584 return 0;
2585}
2586
2587static int cxd2841er_sleep_tc_to_active_i_band(
2588 struct cxd2841er_priv *priv, u32 bandwidth)
2589{
2590 u32 iffreq;
2591 u8 data[3];
2592
2593 /* TRCG Nominal Rate */
2594 u8 nominalRate8bw[3][5] = {
2595 {0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2596 {0x11, 0xB8, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2597 {0x00, 0x00, 0x00, 0x00, 0x00} /* 41MHz XTal */
2598 };
2599
2600 u8 nominalRate7bw[3][5] = {
2601 {0x00, 0x00, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2602 {0x14, 0x40, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2603 {0x00, 0x00, 0x00, 0x00, 0x00} /* 41MHz XTal */
2604 };
2605
2606 u8 nominalRate6bw[3][5] = {
2607 {0x14, 0x2E, 0x00, 0x00, 0x00}, /* 20.5MHz XTal */
2608 {0x17, 0xA0, 0x00, 0x00, 0x00}, /* 24MHz XTal */
2609 {0x14, 0x2E, 0x00, 0x00, 0x00} /* 41MHz XTal */
2610 };
2611
2612 u8 itbCoef8bw[3][14] = {
2613 {0x00}, /* 20.5MHz XTal */
2614 {0x2F, 0xBA, 0x28, 0x9B, 0x28, 0x9D, 0x28, 0xA1, 0x29,
2615 0xA5, 0x2A, 0xAC, 0x29, 0xB5}, /* 24MHz Xtal */
2616 {0x0}, /* 41MHz XTal */
2617 };
2618
2619 u8 itbCoef7bw[3][14] = {
2620 {0x00}, /* 20.5MHz XTal */
2621 {0x30, 0xB1, 0x29, 0x9A, 0x28, 0x9C, 0x28, 0xA0, 0x29,
2622 0xA2, 0x2B, 0xA6, 0x2B, 0xAD}, /* 24MHz Xtal */
2623 {0x00}, /* 41MHz XTal */
2624 };
2625
2626 u8 itbCoef6bw[3][14] = {
2627 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
2628 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 20.5MHz XTal */
2629 {0x31, 0xA8, 0x29, 0x9B, 0x27, 0x9C, 0x28, 0x9E, 0x29,
2630 0xA4, 0x29, 0xA2, 0x29, 0xA8}, /* 24MHz Xtal */
2631 {0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8, 0x00,
2632 0xCF, 0x00, 0xE6, 0x23, 0xA4}, /* 41MHz XTal */
2633 };
2634
2635 dev_dbg(&priv->i2c->dev, "%s() bandwidth=%u\n", __func__, bandwidth);
2636 /* Set SLV-T Bank : 0x10 */
2637 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2638
2639 /* 20.5/41MHz Xtal support is not available
2640 * on ISDB-T 7MHzBW and 8MHzBW
2641 */
2642 if (priv->xtal != SONY_XTAL_24000 && bandwidth > 6000000) {
2643 dev_err(&priv->i2c->dev,
2644 "%s(): bandwidth %d supported only for 24MHz xtal\n",
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002645 __func__, bandwidth);
2646 return -EINVAL;
2647 }
Abylay Ospan83808c22016-03-22 19:20:34 -03002648
2649 switch (bandwidth) {
2650 case 8000000:
2651 /* TRCG Nominal Rate */
2652 cxd2841er_write_regs(priv, I2C_SLVT,
2653 0x9F, nominalRate8bw[priv->xtal], 5);
2654 /* Group delay equaliser settings for ASCOT tuners optimized */
2655 cxd2841er_write_regs(priv, I2C_SLVT,
2656 0xA6, itbCoef8bw[priv->xtal], 14);
2657
2658 /* IF freq setting */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002659 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4750000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002660 data[0] = (u8) ((iffreq >> 16) & 0xff);
2661 data[1] = (u8)((iffreq >> 8) & 0xff);
2662 data[2] = (u8)(iffreq & 0xff);
2663 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2664
2665 /* System bandwidth setting */
2666 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x0, 0x7);
2667
2668 /* Demod core latency setting */
2669 data[0] = 0x13;
2670 data[1] = 0xFC;
2671 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2672
2673 /* Acquisition optimization setting */
2674 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2675 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
2676 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2677 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x03);
2678 break;
2679 case 7000000:
2680 /* TRCG Nominal Rate */
2681 cxd2841er_write_regs(priv, I2C_SLVT,
2682 0x9F, nominalRate7bw[priv->xtal], 5);
2683 /* Group delay equaliser settings for ASCOT tuners optimized */
2684 cxd2841er_write_regs(priv, I2C_SLVT,
2685 0xA6, itbCoef7bw[priv->xtal], 14);
2686
2687 /* IF freq setting */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002688 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 4150000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002689 data[0] = (u8) ((iffreq >> 16) & 0xff);
2690 data[1] = (u8)((iffreq >> 8) & 0xff);
2691 data[2] = (u8)(iffreq & 0xff);
2692 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2693
2694 /* System bandwidth setting */
2695 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x02, 0x7);
2696
2697 /* Demod core latency setting */
2698 data[0] = 0x1A;
2699 data[1] = 0xFA;
2700 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2701
2702 /* Acquisition optimization setting */
2703 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2704 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x03, 0x07);
2705 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2706 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
2707 break;
2708 case 6000000:
2709 /* TRCG Nominal Rate */
2710 cxd2841er_write_regs(priv, I2C_SLVT,
2711 0x9F, nominalRate6bw[priv->xtal], 5);
2712 /* Group delay equaliser settings for ASCOT tuners optimized */
2713 cxd2841er_write_regs(priv, I2C_SLVT,
2714 0xA6, itbCoef6bw[priv->xtal], 14);
2715
2716 /* IF freq setting */
Daniel Schellercbc85a42017-04-09 16:38:14 -03002717 iffreq = cxd2841er_calc_iffreq_xtal(priv->xtal, 3550000);
Abylay Ospan83808c22016-03-22 19:20:34 -03002718 data[0] = (u8) ((iffreq >> 16) & 0xff);
2719 data[1] = (u8)((iffreq >> 8) & 0xff);
2720 data[2] = (u8)(iffreq & 0xff);
2721 cxd2841er_write_regs(priv, I2C_SLVT, 0xB6, data, 3);
2722
2723 /* System bandwidth setting */
2724 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd7, 0x04, 0x7);
2725
2726 /* Demod core latency setting */
2727 if (priv->xtal == SONY_XTAL_24000) {
2728 data[0] = 0x1F;
2729 data[1] = 0x79;
2730 } else {
2731 data[0] = 0x1A;
2732 data[1] = 0xE2;
2733 }
2734 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
2735
2736 /* Acquisition optimization setting */
2737 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x12);
2738 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x71, 0x07, 0x07);
2739 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
2740 cxd2841er_write_reg(priv, I2C_SLVT, 0xBE, 0x02);
2741 break;
2742 default:
2743 dev_dbg(&priv->i2c->dev, "%s(): invalid bandwidth %d\n",
2744 __func__, bandwidth);
2745 return -EINVAL;
2746 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002747 return 0;
2748}
2749
2750static int cxd2841er_sleep_tc_to_active_c_band(struct cxd2841er_priv *priv,
2751 u32 bandwidth)
2752{
2753 u8 bw7_8mhz_b10_a6[] = {
2754 0x2D, 0xC7, 0x04, 0xF4, 0x07, 0xC5, 0x2A, 0xB8,
2755 0x27, 0x9E, 0x27, 0xA4, 0x29, 0xAB };
2756 u8 bw6mhz_b10_a6[] = {
2757 0x27, 0xA7, 0x28, 0xB3, 0x02, 0xF0, 0x01, 0xE8,
2758 0x00, 0xCF, 0x00, 0xE6, 0x23, 0xA4 };
2759 u8 b10_b6[3];
2760 u32 iffreq;
2761
Abylay Ospanaf4cc462016-07-21 10:56:25 -03002762 if (bandwidth != 6000000 &&
2763 bandwidth != 7000000 &&
2764 bandwidth != 8000000) {
2765 dev_info(&priv->i2c->dev, "%s(): unsupported bandwidth %d. Forcing 8Mhz!\n",
2766 __func__, bandwidth);
2767 bandwidth = 8000000;
2768 }
2769
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03002770 dev_dbg(&priv->i2c->dev, "%s() bw=%d\n", __func__, bandwidth);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002771 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2772 switch (bandwidth) {
2773 case 8000000:
2774 case 7000000:
2775 cxd2841er_write_regs(
2776 priv, I2C_SLVT, 0xa6,
2777 bw7_8mhz_b10_a6, sizeof(bw7_8mhz_b10_a6));
Daniel Schellercbc85a42017-04-09 16:38:14 -03002778 iffreq = cxd2841er_calc_iffreq(4900000);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002779 break;
2780 case 6000000:
2781 cxd2841er_write_regs(
2782 priv, I2C_SLVT, 0xa6,
2783 bw6mhz_b10_a6, sizeof(bw6mhz_b10_a6));
Daniel Schellercbc85a42017-04-09 16:38:14 -03002784 iffreq = cxd2841er_calc_iffreq(3700000);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002785 break;
2786 default:
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03002787 dev_err(&priv->i2c->dev, "%s(): unsupported bandwidth %d\n",
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002788 __func__, bandwidth);
2789 return -EINVAL;
2790 }
2791 /* <IF freq setting> */
2792 b10_b6[0] = (u8) ((iffreq >> 16) & 0xff);
2793 b10_b6[1] = (u8)((iffreq >> 8) & 0xff);
2794 b10_b6[2] = (u8)(iffreq & 0xff);
2795 cxd2841er_write_regs(priv, I2C_SLVT, 0xb6, b10_b6, sizeof(b10_b6));
2796 /* Set SLV-T Bank : 0x11 */
2797 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2798 switch (bandwidth) {
2799 case 8000000:
2800 case 7000000:
2801 cxd2841er_set_reg_bits(
2802 priv, I2C_SLVT, 0xa3, 0x00, 0x1f);
2803 break;
2804 case 6000000:
2805 cxd2841er_set_reg_bits(
2806 priv, I2C_SLVT, 0xa3, 0x14, 0x1f);
2807 break;
2808 }
2809 /* Set SLV-T Bank : 0x40 */
2810 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
2811 switch (bandwidth) {
2812 case 8000000:
2813 cxd2841er_set_reg_bits(
2814 priv, I2C_SLVT, 0x26, 0x0b, 0x0f);
2815 cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0x3e);
2816 break;
2817 case 7000000:
2818 cxd2841er_set_reg_bits(
2819 priv, I2C_SLVT, 0x26, 0x09, 0x0f);
2820 cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0xd6);
2821 break;
2822 case 6000000:
2823 cxd2841er_set_reg_bits(
2824 priv, I2C_SLVT, 0x26, 0x08, 0x0f);
2825 cxd2841er_write_reg(priv, I2C_SLVT, 0x27, 0x6e);
2826 break;
2827 }
2828 return 0;
2829}
2830
2831static int cxd2841er_sleep_tc_to_active_t(struct cxd2841er_priv *priv,
2832 u32 bandwidth)
2833{
2834 u8 data[2] = { 0x09, 0x54 };
Abylay Ospan83808c22016-03-22 19:20:34 -03002835 u8 data24m[3] = {0xDC, 0x6C, 0x00};
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002836
2837 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2838 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
2839 /* Set SLV-X Bank : 0x00 */
2840 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
2841 /* Set demod mode */
2842 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x01);
2843 /* Set SLV-T Bank : 0x00 */
2844 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2845 /* Enable demod clock */
2846 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
2847 /* Disable RF level monitor */
2848 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
2849 /* Enable ADC clock */
2850 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
2851 /* Enable ADC 1 */
2852 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
Abylay Ospan83808c22016-03-22 19:20:34 -03002853 /* Enable ADC 2 & 3 */
2854 if (priv->xtal == SONY_XTAL_41000) {
2855 data[0] = 0x0A;
2856 data[1] = 0xD4;
2857 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002858 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
2859 /* Enable ADC 4 */
2860 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
2861 /* Set SLV-T Bank : 0x10 */
2862 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2863 /* IFAGC gain settings */
2864 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
2865 /* Set SLV-T Bank : 0x11 */
2866 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2867 /* BBAGC TARGET level setting */
2868 cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
2869 /* Set SLV-T Bank : 0x10 */
2870 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2871 /* ASCOT setting ON */
2872 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
2873 /* Set SLV-T Bank : 0x18 */
2874 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
2875 /* Pre-RS BER moniter setting */
2876 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x36, 0x40, 0x07);
2877 /* FEC Auto Recovery setting */
2878 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
2879 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x01, 0x01);
2880 /* Set SLV-T Bank : 0x00 */
2881 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2882 /* TSIF setting */
2883 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
2884 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
Abylay Ospan83808c22016-03-22 19:20:34 -03002885
2886 if (priv->xtal == SONY_XTAL_24000) {
2887 /* Set SLV-T Bank : 0x10 */
2888 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2889 cxd2841er_write_reg(priv, I2C_SLVT, 0xBF, 0x60);
2890 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x18);
2891 cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data24m, 3);
2892 }
2893
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002894 cxd2841er_sleep_tc_to_active_t_band(priv, bandwidth);
2895 /* Set SLV-T Bank : 0x00 */
2896 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2897 /* Disable HiZ Setting 1 */
2898 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
2899 /* Disable HiZ Setting 2 */
2900 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
2901 priv->state = STATE_ACTIVE_TC;
2902 return 0;
2903}
2904
2905static int cxd2841er_sleep_tc_to_active_t2(struct cxd2841er_priv *priv,
2906 u32 bandwidth)
2907{
Abylay Ospan6c771612016-05-16 11:43:25 -03002908 u8 data[MAX_WRITE_REGSIZE];
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002909
2910 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
2911 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT2);
2912 /* Set SLV-X Bank : 0x00 */
2913 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
2914 /* Set demod mode */
2915 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x02);
2916 /* Set SLV-T Bank : 0x00 */
2917 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2918 /* Enable demod clock */
2919 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
2920 /* Disable RF level monitor */
Abylay Ospan6c771612016-05-16 11:43:25 -03002921 cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002922 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
2923 /* Enable ADC clock */
2924 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
2925 /* Enable ADC 1 */
2926 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
Abylay Ospan6c771612016-05-16 11:43:25 -03002927
2928 if (priv->xtal == SONY_XTAL_41000) {
2929 data[0] = 0x0A;
2930 data[1] = 0xD4;
2931 } else {
2932 data[0] = 0x09;
2933 data[1] = 0x54;
2934 }
2935
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002936 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
2937 /* Enable ADC 4 */
2938 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
2939 /* Set SLV-T Bank : 0x10 */
2940 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2941 /* IFAGC gain settings */
2942 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x0c, 0x1f);
2943 /* Set SLV-T Bank : 0x11 */
2944 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2945 /* BBAGC TARGET level setting */
2946 cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x50);
2947 /* Set SLV-T Bank : 0x10 */
2948 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
2949 /* ASCOT setting ON */
2950 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
2951 /* Set SLV-T Bank : 0x20 */
2952 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
2953 /* Acquisition optimization setting */
2954 cxd2841er_write_reg(priv, I2C_SLVT, 0x8b, 0x3c);
2955 /* Set SLV-T Bank : 0x2b */
2956 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2957 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x76, 0x20, 0x70);
Abylay Ospan6c771612016-05-16 11:43:25 -03002958 /* Set SLV-T Bank : 0x23 */
2959 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x23);
2960 /* L1 Control setting */
2961 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE6, 0x00, 0x03);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03002962 /* Set SLV-T Bank : 0x00 */
2963 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
2964 /* TSIF setting */
2965 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
2966 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
2967 /* DVB-T2 initial setting */
2968 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x13);
2969 cxd2841er_write_reg(priv, I2C_SLVT, 0x83, 0x10);
2970 cxd2841er_write_reg(priv, I2C_SLVT, 0x86, 0x34);
2971 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9e, 0x09, 0x0f);
2972 cxd2841er_write_reg(priv, I2C_SLVT, 0x9f, 0xd8);
2973 /* Set SLV-T Bank : 0x2a */
2974 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2a);
2975 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x38, 0x04, 0x0f);
2976 /* Set SLV-T Bank : 0x2b */
2977 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2b);
2978 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x11, 0x20, 0x3f);
2979
Abylay Ospan6c771612016-05-16 11:43:25 -03002980 /* 24MHz Xtal setting */
2981 if (priv->xtal == SONY_XTAL_24000) {
2982 /* Set SLV-T Bank : 0x11 */
2983 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
2984 data[0] = 0xEB;
2985 data[1] = 0x03;
2986 data[2] = 0x3B;
2987 cxd2841er_write_regs(priv, I2C_SLVT, 0x33, data, 3);
2988
2989 /* Set SLV-T Bank : 0x20 */
2990 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x20);
2991 data[0] = 0x5E;
2992 data[1] = 0x5E;
2993 data[2] = 0x47;
2994 cxd2841er_write_regs(priv, I2C_SLVT, 0x95, data, 3);
2995
2996 cxd2841er_write_reg(priv, I2C_SLVT, 0x99, 0x18);
2997
2998 data[0] = 0x3F;
2999 data[1] = 0xFF;
3000 cxd2841er_write_regs(priv, I2C_SLVT, 0xD9, data, 2);
3001
3002 /* Set SLV-T Bank : 0x24 */
3003 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x24);
3004 data[0] = 0x0B;
3005 data[1] = 0x72;
3006 cxd2841er_write_regs(priv, I2C_SLVT, 0x34, data, 2);
3007
3008 data[0] = 0x93;
3009 data[1] = 0xF3;
3010 data[2] = 0x00;
3011 cxd2841er_write_regs(priv, I2C_SLVT, 0xD2, data, 3);
3012
3013 data[0] = 0x05;
3014 data[1] = 0xB8;
3015 data[2] = 0xD8;
3016 cxd2841er_write_regs(priv, I2C_SLVT, 0xDD, data, 3);
3017
3018 cxd2841er_write_reg(priv, I2C_SLVT, 0xE0, 0x00);
3019
3020 /* Set SLV-T Bank : 0x25 */
3021 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x25);
3022 cxd2841er_write_reg(priv, I2C_SLVT, 0xED, 0x60);
3023
3024 /* Set SLV-T Bank : 0x27 */
3025 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x27);
3026 cxd2841er_write_reg(priv, I2C_SLVT, 0xFA, 0x34);
3027
3028 /* Set SLV-T Bank : 0x2B */
3029 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2B);
3030 cxd2841er_write_reg(priv, I2C_SLVT, 0x4B, 0x2F);
3031 cxd2841er_write_reg(priv, I2C_SLVT, 0x9E, 0x0E);
3032
3033 /* Set SLV-T Bank : 0x2D */
3034 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x2D);
3035 data[0] = 0x89;
3036 data[1] = 0x89;
3037 cxd2841er_write_regs(priv, I2C_SLVT, 0x24, data, 2);
3038
3039 /* Set SLV-T Bank : 0x5E */
3040 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x5E);
3041 data[0] = 0x24;
3042 data[1] = 0x95;
3043 cxd2841er_write_regs(priv, I2C_SLVT, 0x8C, data, 2);
3044 }
3045
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003046 cxd2841er_sleep_tc_to_active_t2_band(priv, bandwidth);
3047
3048 /* Set SLV-T Bank : 0x00 */
3049 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3050 /* Disable HiZ Setting 1 */
3051 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3052 /* Disable HiZ Setting 2 */
3053 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3054 priv->state = STATE_ACTIVE_TC;
3055 return 0;
3056}
3057
Abylay Ospan83808c22016-03-22 19:20:34 -03003058/* ISDB-Tb part */
3059static int cxd2841er_sleep_tc_to_active_i(struct cxd2841er_priv *priv,
3060 u32 bandwidth)
3061{
3062 u8 data[2] = { 0x09, 0x54 };
3063 u8 data24m[2] = {0x60, 0x00};
3064 u8 data24m2[3] = {0xB7, 0x1B, 0x00};
3065
3066 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3067 cxd2841er_set_ts_clock_mode(priv, SYS_DVBT);
3068 /* Set SLV-X Bank : 0x00 */
3069 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
3070 /* Set demod mode */
3071 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x06);
3072 /* Set SLV-T Bank : 0x00 */
3073 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3074 /* Enable demod clock */
3075 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
3076 /* Enable RF level monitor */
3077 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x01);
3078 cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x01);
3079 /* Enable ADC clock */
3080 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
3081 /* Enable ADC 1 */
3082 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
3083 /* xtal freq 20.5MHz or 24M */
3084 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
3085 /* Enable ADC 4 */
3086 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
3087 /* ASCOT setting ON */
3088 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
3089 /* FEC Auto Recovery setting */
3090 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x30, 0x01, 0x01);
3091 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x31, 0x00, 0x01);
3092 /* ISDB-T initial setting */
3093 /* Set SLV-T Bank : 0x00 */
3094 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3095 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x00, 0x01);
3096 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x00, 0x01);
3097 /* Set SLV-T Bank : 0x10 */
3098 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3099 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x69, 0x04, 0x07);
3100 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x6B, 0x03, 0x07);
3101 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x9D, 0x50, 0xFF);
3102 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xD3, 0x06, 0x1F);
3103 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xED, 0x00, 0x01);
3104 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xE2, 0xCE, 0x80);
3105 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xF2, 0x13, 0x10);
3106 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x2E, 0x3F);
3107 /* Set SLV-T Bank : 0x15 */
3108 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x15);
3109 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xDE, 0x02, 0x03);
3110 /* Set SLV-T Bank : 0x1E */
3111 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x1E);
3112 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x73, 0x68, 0xFF);
3113 /* Set SLV-T Bank : 0x63 */
3114 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x63);
3115 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0x81, 0x00, 0x01);
3116
3117 /* for xtal 24MHz */
3118 /* Set SLV-T Bank : 0x10 */
3119 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3120 cxd2841er_write_regs(priv, I2C_SLVT, 0xBF, data24m, 2);
3121 /* Set SLV-T Bank : 0x60 */
3122 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x60);
3123 cxd2841er_write_regs(priv, I2C_SLVT, 0xA8, data24m2, 3);
3124
3125 cxd2841er_sleep_tc_to_active_i_band(priv, bandwidth);
3126 /* Set SLV-T Bank : 0x00 */
3127 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3128 /* Disable HiZ Setting 1 */
3129 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3130 /* Disable HiZ Setting 2 */
3131 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3132 priv->state = STATE_ACTIVE_TC;
3133 return 0;
3134}
3135
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003136static int cxd2841er_sleep_tc_to_active_c(struct cxd2841er_priv *priv,
3137 u32 bandwidth)
3138{
3139 u8 data[2] = { 0x09, 0x54 };
3140
3141 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3142 cxd2841er_set_ts_clock_mode(priv, SYS_DVBC_ANNEX_A);
3143 /* Set SLV-X Bank : 0x00 */
3144 cxd2841er_write_reg(priv, I2C_SLVX, 0x00, 0x00);
3145 /* Set demod mode */
3146 cxd2841er_write_reg(priv, I2C_SLVX, 0x17, 0x04);
3147 /* Set SLV-T Bank : 0x00 */
3148 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3149 /* Enable demod clock */
3150 cxd2841er_write_reg(priv, I2C_SLVT, 0x2c, 0x01);
3151 /* Disable RF level monitor */
Abylay Ospan4a86bc12016-07-19 00:10:20 -03003152 cxd2841er_write_reg(priv, I2C_SLVT, 0x59, 0x00);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003153 cxd2841er_write_reg(priv, I2C_SLVT, 0x2f, 0x00);
3154 /* Enable ADC clock */
3155 cxd2841er_write_reg(priv, I2C_SLVT, 0x30, 0x00);
3156 /* Enable ADC 1 */
3157 cxd2841er_write_reg(priv, I2C_SLVT, 0x41, 0x1a);
3158 /* xtal freq 20.5MHz */
3159 cxd2841er_write_regs(priv, I2C_SLVT, 0x43, data, 2);
3160 /* Enable ADC 4 */
3161 cxd2841er_write_reg(priv, I2C_SLVX, 0x18, 0x00);
3162 /* Set SLV-T Bank : 0x10 */
3163 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3164 /* IFAGC gain settings */
3165 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xd2, 0x09, 0x1f);
3166 /* Set SLV-T Bank : 0x11 */
3167 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x11);
3168 /* BBAGC TARGET level setting */
3169 cxd2841er_write_reg(priv, I2C_SLVT, 0x6a, 0x48);
3170 /* Set SLV-T Bank : 0x10 */
3171 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3172 /* ASCOT setting ON */
3173 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xa5, 0x01, 0x01);
3174 /* Set SLV-T Bank : 0x40 */
3175 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x40);
3176 /* Demod setting */
3177 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc3, 0x00, 0x04);
3178 /* Set SLV-T Bank : 0x00 */
3179 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3180 /* TSIF setting */
3181 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xce, 0x01, 0x01);
3182 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcf, 0x01, 0x01);
3183
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003184 cxd2841er_sleep_tc_to_active_c_band(priv, bandwidth);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003185 /* Set SLV-T Bank : 0x00 */
3186 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3187 /* Disable HiZ Setting 1 */
3188 cxd2841er_write_reg(priv, I2C_SLVT, 0x80, 0x28);
3189 /* Disable HiZ Setting 2 */
3190 cxd2841er_write_reg(priv, I2C_SLVT, 0x81, 0x00);
3191 priv->state = STATE_ACTIVE_TC;
3192 return 0;
3193}
3194
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -02003195static int cxd2841er_get_frontend(struct dvb_frontend *fe,
3196 struct dtv_frontend_properties *p)
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003197{
3198 enum fe_status status = 0;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003199 struct cxd2841er_priv *priv = fe->demodulator_priv;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003200
3201 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3202 if (priv->state == STATE_ACTIVE_S)
3203 cxd2841er_read_status_s(fe, &status);
3204 else if (priv->state == STATE_ACTIVE_TC)
3205 cxd2841er_read_status_tc(fe, &status);
3206
Mauro Carvalho Chehab5fda1b62016-06-30 19:41:45 -03003207 cxd2841er_read_signal_strength(fe);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003208
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003209 if (status & FE_HAS_LOCK) {
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03003210 cxd2841er_read_snr(fe);
3211 cxd2841er_read_ucblocks(fe);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003212
Mauro Carvalho Chehabf1b26622016-07-01 11:03:13 -03003213 cxd2841er_read_ber(fe);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003214 } else {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003215 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003216 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003217 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03003218 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003219 }
3220 return 0;
3221}
3222
3223static int cxd2841er_set_frontend_s(struct dvb_frontend *fe)
3224{
3225 int ret = 0, i, timeout, carr_offset;
3226 enum fe_status status;
3227 struct cxd2841er_priv *priv = fe->demodulator_priv;
3228 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3229 u32 symbol_rate = p->symbol_rate/1000;
3230
Abylay Ospan83808c22016-03-22 19:20:34 -03003231 dev_dbg(&priv->i2c->dev, "%s(): %s frequency=%d symbol_rate=%d xtal=%d\n",
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003232 __func__,
3233 (p->delivery_system == SYS_DVBS ? "DVB-S" : "DVB-S2"),
Abylay Ospan83808c22016-03-22 19:20:34 -03003234 p->frequency, symbol_rate, priv->xtal);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003235 switch (priv->state) {
3236 case STATE_SLEEP_S:
3237 ret = cxd2841er_sleep_s_to_active_s(
3238 priv, p->delivery_system, symbol_rate);
3239 break;
3240 case STATE_ACTIVE_S:
3241 ret = cxd2841er_retune_active(priv, p);
3242 break;
3243 default:
3244 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3245 __func__, priv->state);
3246 ret = -EINVAL;
3247 goto done;
3248 }
3249 if (ret) {
3250 dev_dbg(&priv->i2c->dev, "%s(): tune failed\n", __func__);
3251 goto done;
3252 }
3253 if (fe->ops.i2c_gate_ctrl)
3254 fe->ops.i2c_gate_ctrl(fe, 1);
3255 if (fe->ops.tuner_ops.set_params)
3256 fe->ops.tuner_ops.set_params(fe);
3257 if (fe->ops.i2c_gate_ctrl)
3258 fe->ops.i2c_gate_ctrl(fe, 0);
3259 cxd2841er_tune_done(priv);
3260 timeout = ((3000000 + (symbol_rate - 1)) / symbol_rate) + 150;
3261 for (i = 0; i < timeout / CXD2841ER_DVBS_POLLING_INVL; i++) {
3262 usleep_range(CXD2841ER_DVBS_POLLING_INVL*1000,
3263 (CXD2841ER_DVBS_POLLING_INVL + 2) * 1000);
3264 cxd2841er_read_status_s(fe, &status);
3265 if (status & FE_HAS_LOCK)
3266 break;
3267 }
3268 if (status & FE_HAS_LOCK) {
3269 if (cxd2841er_get_carrier_offset_s_s2(
3270 priv, &carr_offset)) {
3271 ret = -EINVAL;
3272 goto done;
3273 }
3274 dev_dbg(&priv->i2c->dev, "%s(): carrier_offset=%d\n",
3275 __func__, carr_offset);
3276 }
3277done:
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003278 /* Reset stats */
3279 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
3280 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3281 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3282 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03003283 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003284
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003285 return ret;
3286}
3287
3288static int cxd2841er_set_frontend_tc(struct dvb_frontend *fe)
3289{
3290 int ret = 0, timeout;
3291 enum fe_status status;
3292 struct cxd2841er_priv *priv = fe->demodulator_priv;
3293 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3294
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003295 dev_dbg(&priv->i2c->dev, "%s() delivery_system=%d bandwidth_hz=%d\n",
3296 __func__, p->delivery_system, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003297 if (p->delivery_system == SYS_DVBT) {
3298 priv->system = SYS_DVBT;
3299 switch (priv->state) {
3300 case STATE_SLEEP_TC:
3301 ret = cxd2841er_sleep_tc_to_active_t(
3302 priv, p->bandwidth_hz);
3303 break;
3304 case STATE_ACTIVE_TC:
3305 ret = cxd2841er_retune_active(priv, p);
3306 break;
3307 default:
3308 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3309 __func__, priv->state);
3310 ret = -EINVAL;
3311 }
3312 } else if (p->delivery_system == SYS_DVBT2) {
3313 priv->system = SYS_DVBT2;
3314 cxd2841er_dvbt2_set_plp_config(priv,
3315 (int)(p->stream_id > 255), p->stream_id);
3316 cxd2841er_dvbt2_set_profile(priv, DVBT2_PROFILE_BASE);
3317 switch (priv->state) {
3318 case STATE_SLEEP_TC:
3319 ret = cxd2841er_sleep_tc_to_active_t2(priv,
3320 p->bandwidth_hz);
3321 break;
3322 case STATE_ACTIVE_TC:
3323 ret = cxd2841er_retune_active(priv, p);
3324 break;
3325 default:
3326 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3327 __func__, priv->state);
3328 ret = -EINVAL;
3329 }
Abylay Ospan83808c22016-03-22 19:20:34 -03003330 } else if (p->delivery_system == SYS_ISDBT) {
3331 priv->system = SYS_ISDBT;
3332 switch (priv->state) {
3333 case STATE_SLEEP_TC:
3334 ret = cxd2841er_sleep_tc_to_active_i(
3335 priv, p->bandwidth_hz);
3336 break;
3337 case STATE_ACTIVE_TC:
3338 ret = cxd2841er_retune_active(priv, p);
3339 break;
3340 default:
3341 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3342 __func__, priv->state);
3343 ret = -EINVAL;
3344 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003345 } else if (p->delivery_system == SYS_DVBC_ANNEX_A ||
3346 p->delivery_system == SYS_DVBC_ANNEX_C) {
3347 priv->system = SYS_DVBC_ANNEX_A;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003348 /* correct bandwidth */
3349 if (p->bandwidth_hz != 6000000 &&
3350 p->bandwidth_hz != 7000000 &&
3351 p->bandwidth_hz != 8000000) {
3352 p->bandwidth_hz = 8000000;
3353 dev_dbg(&priv->i2c->dev, "%s(): forcing bandwidth to %d\n",
3354 __func__, p->bandwidth_hz);
3355 }
3356
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003357 switch (priv->state) {
3358 case STATE_SLEEP_TC:
3359 ret = cxd2841er_sleep_tc_to_active_c(
3360 priv, p->bandwidth_hz);
3361 break;
3362 case STATE_ACTIVE_TC:
3363 ret = cxd2841er_retune_active(priv, p);
3364 break;
3365 default:
3366 dev_dbg(&priv->i2c->dev, "%s(): invalid state %d\n",
3367 __func__, priv->state);
3368 ret = -EINVAL;
3369 }
3370 } else {
3371 dev_dbg(&priv->i2c->dev,
3372 "%s(): invalid delivery system %d\n",
3373 __func__, p->delivery_system);
3374 ret = -EINVAL;
3375 }
3376 if (ret)
3377 goto done;
3378 if (fe->ops.i2c_gate_ctrl)
3379 fe->ops.i2c_gate_ctrl(fe, 1);
3380 if (fe->ops.tuner_ops.set_params)
3381 fe->ops.tuner_ops.set_params(fe);
3382 if (fe->ops.i2c_gate_ctrl)
3383 fe->ops.i2c_gate_ctrl(fe, 0);
3384 cxd2841er_tune_done(priv);
3385 timeout = 2500;
3386 while (timeout > 0) {
3387 ret = cxd2841er_read_status_tc(fe, &status);
3388 if (ret)
3389 goto done;
3390 if (status & FE_HAS_LOCK)
3391 break;
3392 msleep(20);
3393 timeout -= 20;
3394 }
3395 if (timeout < 0)
3396 dev_dbg(&priv->i2c->dev,
3397 "%s(): LOCK wait timeout\n", __func__);
3398done:
3399 return ret;
3400}
3401
3402static int cxd2841er_tune_s(struct dvb_frontend *fe,
3403 bool re_tune,
3404 unsigned int mode_flags,
3405 unsigned int *delay,
3406 enum fe_status *status)
3407{
3408 int ret, carrier_offset;
3409 struct cxd2841er_priv *priv = fe->demodulator_priv;
3410 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3411
3412 dev_dbg(&priv->i2c->dev, "%s() re_tune=%d\n", __func__, re_tune);
3413 if (re_tune) {
3414 ret = cxd2841er_set_frontend_s(fe);
3415 if (ret)
3416 return ret;
3417 cxd2841er_read_status_s(fe, status);
3418 if (*status & FE_HAS_LOCK) {
3419 if (cxd2841er_get_carrier_offset_s_s2(
3420 priv, &carrier_offset))
3421 return -EINVAL;
3422 p->frequency += carrier_offset;
3423 ret = cxd2841er_set_frontend_s(fe);
3424 if (ret)
3425 return ret;
3426 }
3427 }
3428 *delay = HZ / 5;
3429 return cxd2841er_read_status_s(fe, status);
3430}
3431
3432static int cxd2841er_tune_tc(struct dvb_frontend *fe,
3433 bool re_tune,
3434 unsigned int mode_flags,
3435 unsigned int *delay,
3436 enum fe_status *status)
3437{
3438 int ret, carrier_offset;
3439 struct cxd2841er_priv *priv = fe->demodulator_priv;
3440 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3441
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003442 dev_dbg(&priv->i2c->dev, "%s(): re_tune %d bandwidth=%d\n", __func__,
3443 re_tune, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003444 if (re_tune) {
3445 ret = cxd2841er_set_frontend_tc(fe);
3446 if (ret)
3447 return ret;
3448 cxd2841er_read_status_tc(fe, status);
3449 if (*status & FE_HAS_LOCK) {
3450 switch (priv->system) {
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -03003451 case SYS_ISDBT:
3452 ret = cxd2841er_get_carrier_offset_i(
3453 priv, p->bandwidth_hz,
3454 &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003455 if (ret)
3456 return ret;
Mauro Carvalho Chehab76344a3f2016-05-04 18:25:38 -03003457 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003458 case SYS_DVBT:
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03003459 ret = cxd2841er_get_carrier_offset_t(
3460 priv, p->bandwidth_hz,
3461 &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003462 if (ret)
3463 return ret;
Abylay Ospanc5ea46d2016-04-02 23:31:50 -03003464 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003465 case SYS_DVBT2:
3466 ret = cxd2841er_get_carrier_offset_t2(
3467 priv, p->bandwidth_hz,
3468 &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003469 if (ret)
3470 return ret;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003471 break;
3472 case SYS_DVBC_ANNEX_A:
3473 ret = cxd2841er_get_carrier_offset_c(
3474 priv, &carrier_offset);
Arnd Bergmannbb9bd872016-08-03 13:46:21 -07003475 if (ret)
3476 return ret;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003477 break;
3478 default:
3479 dev_dbg(&priv->i2c->dev,
3480 "%s(): invalid delivery system %d\n",
3481 __func__, priv->system);
3482 return -EINVAL;
3483 }
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003484 dev_dbg(&priv->i2c->dev, "%s(): carrier offset %d\n",
3485 __func__, carrier_offset);
3486 p->frequency += carrier_offset;
3487 ret = cxd2841er_set_frontend_tc(fe);
3488 if (ret)
3489 return ret;
3490 }
3491 }
3492 *delay = HZ / 5;
3493 return cxd2841er_read_status_tc(fe, status);
3494}
3495
3496static int cxd2841er_sleep_s(struct dvb_frontend *fe)
3497{
3498 struct cxd2841er_priv *priv = fe->demodulator_priv;
3499
3500 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3501 cxd2841er_active_s_to_sleep_s(fe->demodulator_priv);
3502 cxd2841er_sleep_s_to_shutdown(fe->demodulator_priv);
3503 return 0;
3504}
3505
3506static int cxd2841er_sleep_tc(struct dvb_frontend *fe)
3507{
3508 struct cxd2841er_priv *priv = fe->demodulator_priv;
3509
3510 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3511 if (priv->state == STATE_ACTIVE_TC) {
3512 switch (priv->system) {
3513 case SYS_DVBT:
3514 cxd2841er_active_t_to_sleep_tc(priv);
3515 break;
3516 case SYS_DVBT2:
3517 cxd2841er_active_t2_to_sleep_tc(priv);
3518 break;
Abylay Ospan83808c22016-03-22 19:20:34 -03003519 case SYS_ISDBT:
3520 cxd2841er_active_i_to_sleep_tc(priv);
3521 break;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003522 case SYS_DVBC_ANNEX_A:
3523 cxd2841er_active_c_to_sleep_tc(priv);
3524 break;
3525 default:
3526 dev_warn(&priv->i2c->dev,
3527 "%s(): unknown delivery system %d\n",
3528 __func__, priv->system);
3529 }
3530 }
3531 if (priv->state != STATE_SLEEP_TC) {
3532 dev_err(&priv->i2c->dev, "%s(): invalid state %d\n",
3533 __func__, priv->state);
3534 return -EINVAL;
3535 }
3536 cxd2841er_sleep_tc_to_shutdown(priv);
3537 return 0;
3538}
3539
3540static int cxd2841er_send_burst(struct dvb_frontend *fe,
3541 enum fe_sec_mini_cmd burst)
3542{
3543 u8 data;
3544 struct cxd2841er_priv *priv = fe->demodulator_priv;
3545
3546 dev_dbg(&priv->i2c->dev, "%s(): burst mode %s\n", __func__,
3547 (burst == SEC_MINI_A ? "A" : "B"));
3548 if (priv->state != STATE_SLEEP_S &&
3549 priv->state != STATE_ACTIVE_S) {
3550 dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3551 __func__, priv->state);
3552 return -EINVAL;
3553 }
3554 data = (burst == SEC_MINI_A ? 0 : 1);
3555 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3556 cxd2841er_write_reg(priv, I2C_SLVT, 0x34, 0x01);
3557 cxd2841er_write_reg(priv, I2C_SLVT, 0x35, data);
3558 return 0;
3559}
3560
3561static int cxd2841er_set_tone(struct dvb_frontend *fe,
3562 enum fe_sec_tone_mode tone)
3563{
3564 u8 data;
3565 struct cxd2841er_priv *priv = fe->demodulator_priv;
3566
3567 dev_dbg(&priv->i2c->dev, "%s(): tone %s\n", __func__,
3568 (tone == SEC_TONE_ON ? "On" : "Off"));
3569 if (priv->state != STATE_SLEEP_S &&
3570 priv->state != STATE_ACTIVE_S) {
3571 dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3572 __func__, priv->state);
3573 return -EINVAL;
3574 }
3575 data = (tone == SEC_TONE_ON ? 1 : 0);
3576 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3577 cxd2841er_write_reg(priv, I2C_SLVT, 0x36, data);
3578 return 0;
3579}
3580
3581static int cxd2841er_send_diseqc_msg(struct dvb_frontend *fe,
3582 struct dvb_diseqc_master_cmd *cmd)
3583{
3584 int i;
3585 u8 data[12];
3586 struct cxd2841er_priv *priv = fe->demodulator_priv;
3587
3588 if (priv->state != STATE_SLEEP_S &&
3589 priv->state != STATE_ACTIVE_S) {
3590 dev_err(&priv->i2c->dev, "%s(): invalid demod state %d\n",
3591 __func__, priv->state);
3592 return -EINVAL;
3593 }
3594 dev_dbg(&priv->i2c->dev,
3595 "%s(): cmd->len %d\n", __func__, cmd->msg_len);
3596 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xbb);
3597 /* DiDEqC enable */
3598 cxd2841er_write_reg(priv, I2C_SLVT, 0x33, 0x01);
3599 /* cmd1 length & data */
3600 cxd2841er_write_reg(priv, I2C_SLVT, 0x3d, cmd->msg_len);
3601 memset(data, 0, sizeof(data));
3602 for (i = 0; i < cmd->msg_len && i < sizeof(data); i++)
3603 data[i] = cmd->msg[i];
3604 cxd2841er_write_regs(priv, I2C_SLVT, 0x3e, data, sizeof(data));
3605 /* repeat count for cmd1 */
3606 cxd2841er_write_reg(priv, I2C_SLVT, 0x37, 1);
3607 /* repeat count for cmd2: always 0 */
3608 cxd2841er_write_reg(priv, I2C_SLVT, 0x38, 0);
3609 /* start transmit */
3610 cxd2841er_write_reg(priv, I2C_SLVT, 0x32, 0x01);
3611 /* wait for 1 sec timeout */
3612 for (i = 0; i < 50; i++) {
3613 cxd2841er_read_reg(priv, I2C_SLVT, 0x10, data);
3614 if (!data[0]) {
3615 dev_dbg(&priv->i2c->dev,
3616 "%s(): DiSEqC cmd has been sent\n", __func__);
3617 return 0;
3618 }
3619 msleep(20);
3620 }
3621 dev_dbg(&priv->i2c->dev,
3622 "%s(): DiSEqC cmd transmit timeout\n", __func__);
3623 return -ETIMEDOUT;
3624}
3625
3626static void cxd2841er_release(struct dvb_frontend *fe)
3627{
3628 struct cxd2841er_priv *priv = fe->demodulator_priv;
3629
3630 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3631 kfree(priv);
3632}
3633
3634static int cxd2841er_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
3635{
3636 struct cxd2841er_priv *priv = fe->demodulator_priv;
3637
3638 dev_dbg(&priv->i2c->dev, "%s(): enable=%d\n", __func__, enable);
3639 cxd2841er_set_reg_bits(
3640 priv, I2C_SLVX, 0x8, (enable ? 0x01 : 0x00), 0x01);
3641 return 0;
3642}
3643
3644static enum dvbfe_algo cxd2841er_get_algo(struct dvb_frontend *fe)
3645{
3646 struct cxd2841er_priv *priv = fe->demodulator_priv;
3647
3648 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3649 return DVBFE_ALGO_HW;
3650}
3651
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003652static void cxd2841er_init_stats(struct dvb_frontend *fe)
3653{
3654 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
3655
3656 p->strength.len = 1;
3657 p->strength.stat[0].scale = FE_SCALE_RELATIVE;
3658 p->cnr.len = 1;
3659 p->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3660 p->block_error.len = 1;
3661 p->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
3662 p->post_bit_error.len = 1;
3663 p->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehab4216be12016-07-01 11:03:15 -03003664 p->post_bit_count.len = 1;
3665 p->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003666}
3667
3668
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003669static int cxd2841er_init_s(struct dvb_frontend *fe)
3670{
3671 struct cxd2841er_priv *priv = fe->demodulator_priv;
3672
Abylay Ospan30ae3302016-04-05 15:02:37 -03003673 /* sanity. force demod to SHUTDOWN state */
3674 if (priv->state == STATE_SLEEP_S) {
3675 dev_dbg(&priv->i2c->dev, "%s() forcing sleep->shutdown\n",
3676 __func__);
3677 cxd2841er_sleep_s_to_shutdown(priv);
3678 } else if (priv->state == STATE_ACTIVE_S) {
3679 dev_dbg(&priv->i2c->dev, "%s() forcing active->sleep->shutdown\n",
3680 __func__);
3681 cxd2841er_active_s_to_sleep_s(priv);
3682 cxd2841er_sleep_s_to_shutdown(priv);
3683 }
3684
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003685 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
3686 cxd2841er_shutdown_to_sleep_s(priv);
3687 /* SONY_DEMOD_CONFIG_SAT_IFAGCNEG set to 1 */
3688 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0xa0);
3689 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xb9, 0x01, 0x01);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003690
3691 cxd2841er_init_stats(fe);
3692
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003693 return 0;
3694}
3695
3696static int cxd2841er_init_tc(struct dvb_frontend *fe)
3697{
3698 struct cxd2841er_priv *priv = fe->demodulator_priv;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003699 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003700
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003701 dev_dbg(&priv->i2c->dev, "%s() bandwidth_hz=%d\n",
3702 __func__, p->bandwidth_hz);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003703 cxd2841er_shutdown_to_sleep_tc(priv);
3704 /* SONY_DEMOD_CONFIG_IFAGCNEG = 1 */
3705 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x10);
3706 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xcb, 0x40, 0x40);
3707 /* SONY_DEMOD_CONFIG_IFAGC_ADC_FS = 0 */
3708 cxd2841er_write_reg(priv, I2C_SLVT, 0xcd, 0x50);
3709 /* SONY_DEMOD_CONFIG_PARALLEL_SEL = 1 */
3710 cxd2841er_write_reg(priv, I2C_SLVT, 0x00, 0x00);
3711 cxd2841er_set_reg_bits(priv, I2C_SLVT, 0xc4, 0x00, 0x80);
Mauro Carvalho Chehabd0e20e12016-06-30 00:34:59 -03003712
3713 cxd2841er_init_stats(fe);
3714
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003715 return 0;
3716}
3717
Max Kellermannbd336e62016-08-09 18:32:21 -03003718static const struct dvb_frontend_ops cxd2841er_dvbs_s2_ops;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003719static struct dvb_frontend_ops cxd2841er_t_c_ops;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003720
3721static struct dvb_frontend *cxd2841er_attach(struct cxd2841er_config *cfg,
3722 struct i2c_adapter *i2c,
3723 u8 system)
3724{
3725 u8 chip_id = 0;
3726 const char *type;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003727 const char *name;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003728 struct cxd2841er_priv *priv = NULL;
3729
3730 /* allocate memory for the internal state */
3731 priv = kzalloc(sizeof(struct cxd2841er_priv), GFP_KERNEL);
3732 if (!priv)
3733 return NULL;
3734 priv->i2c = i2c;
3735 priv->config = cfg;
3736 priv->i2c_addr_slvx = (cfg->i2c_addr + 4) >> 1;
3737 priv->i2c_addr_slvt = (cfg->i2c_addr) >> 1;
Abylay Ospan83808c22016-03-22 19:20:34 -03003738 priv->xtal = cfg->xtal;
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003739 priv->frontend.demodulator_priv = priv;
3740 dev_info(&priv->i2c->dev,
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003741 "%s(): I2C adapter %p SLVX addr %x SLVT addr %x\n",
3742 __func__, priv->i2c,
3743 priv->i2c_addr_slvx, priv->i2c_addr_slvt);
3744 chip_id = cxd2841er_chip_id(priv);
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003745 switch (chip_id) {
Daniel Scheller1ecda282017-04-09 16:38:13 -03003746 case CXD2837ER_CHIP_ID:
3747 snprintf(cxd2841er_t_c_ops.info.name, 128,
3748 "Sony CXD2837ER DVB-T/T2/C demodulator");
3749 name = "CXD2837ER";
3750 type = "C/T/T2";
3751 break;
3752 case CXD2838ER_CHIP_ID:
3753 snprintf(cxd2841er_t_c_ops.info.name, 128,
3754 "Sony CXD2838ER ISDB-T demodulator");
3755 cxd2841er_t_c_ops.delsys[0] = SYS_ISDBT;
3756 cxd2841er_t_c_ops.delsys[1] = SYS_UNDEFINED;
3757 cxd2841er_t_c_ops.delsys[2] = SYS_UNDEFINED;
3758 name = "CXD2838ER";
3759 type = "ISDB-T";
3760 break;
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003761 case CXD2841ER_CHIP_ID:
3762 snprintf(cxd2841er_t_c_ops.info.name, 128,
3763 "Sony CXD2841ER DVB-T/T2/C demodulator");
3764 name = "CXD2841ER";
Daniel Scheller1ecda282017-04-09 16:38:13 -03003765 type = "T/T2/C/ISDB-T";
3766 break;
3767 case CXD2843ER_CHIP_ID:
3768 snprintf(cxd2841er_t_c_ops.info.name, 128,
3769 "Sony CXD2843ER DVB-T/T2/C/C2 demodulator");
3770 name = "CXD2843ER";
3771 type = "C/C2/T/T2";
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003772 break;
3773 case CXD2854ER_CHIP_ID:
3774 snprintf(cxd2841er_t_c_ops.info.name, 128,
3775 "Sony CXD2854ER DVB-T/T2/C and ISDB-T demodulator");
3776 cxd2841er_t_c_ops.delsys[3] = SYS_ISDBT;
3777 name = "CXD2854ER";
Daniel Scheller1ecda282017-04-09 16:38:13 -03003778 type = "C/C2/T/T2/ISDB-T";
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003779 break;
3780 default:
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003781 dev_err(&priv->i2c->dev, "%s(): invalid chip ID 0x%02x\n",
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003782 __func__, chip_id);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003783 priv->frontend.demodulator_priv = NULL;
3784 kfree(priv);
3785 return NULL;
3786 }
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003787
3788 /* create dvb_frontend */
3789 if (system == SYS_DVBS) {
3790 memcpy(&priv->frontend.ops,
3791 &cxd2841er_dvbs_s2_ops,
3792 sizeof(struct dvb_frontend_ops));
3793 type = "S/S2";
3794 } else {
3795 memcpy(&priv->frontend.ops,
3796 &cxd2841er_t_c_ops,
3797 sizeof(struct dvb_frontend_ops));
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003798 }
3799
3800 dev_info(&priv->i2c->dev,
3801 "%s(): attaching %s DVB-%s frontend\n",
3802 __func__, name, type);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003803 dev_info(&priv->i2c->dev, "%s(): chip ID 0x%02x OK.\n",
3804 __func__, chip_id);
3805 return &priv->frontend;
3806}
3807
3808struct dvb_frontend *cxd2841er_attach_s(struct cxd2841er_config *cfg,
3809 struct i2c_adapter *i2c)
3810{
3811 return cxd2841er_attach(cfg, i2c, SYS_DVBS);
3812}
3813EXPORT_SYMBOL(cxd2841er_attach_s);
3814
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003815struct dvb_frontend *cxd2841er_attach_t_c(struct cxd2841er_config *cfg,
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003816 struct i2c_adapter *i2c)
3817{
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003818 return cxd2841er_attach(cfg, i2c, 0);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003819}
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003820EXPORT_SYMBOL(cxd2841er_attach_t_c);
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003821
Max Kellermannbd336e62016-08-09 18:32:21 -03003822static const struct dvb_frontend_ops cxd2841er_dvbs_s2_ops = {
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003823 .delsys = { SYS_DVBS, SYS_DVBS2 },
3824 .info = {
3825 .name = "Sony CXD2841ER DVB-S/S2 demodulator",
3826 .frequency_min = 500000,
3827 .frequency_max = 2500000,
3828 .frequency_stepsize = 0,
3829 .symbol_rate_min = 1000000,
3830 .symbol_rate_max = 45000000,
3831 .symbol_rate_tolerance = 500,
3832 .caps = FE_CAN_INVERSION_AUTO |
3833 FE_CAN_FEC_AUTO |
3834 FE_CAN_QPSK,
3835 },
3836 .init = cxd2841er_init_s,
3837 .sleep = cxd2841er_sleep_s,
3838 .release = cxd2841er_release,
3839 .set_frontend = cxd2841er_set_frontend_s,
3840 .get_frontend = cxd2841er_get_frontend,
3841 .read_status = cxd2841er_read_status_s,
3842 .i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
3843 .get_frontend_algo = cxd2841er_get_algo,
3844 .set_tone = cxd2841er_set_tone,
3845 .diseqc_send_burst = cxd2841er_send_burst,
3846 .diseqc_send_master_cmd = cxd2841er_send_diseqc_msg,
3847 .tune = cxd2841er_tune_s
3848};
3849
Max Kellermannbd336e62016-08-09 18:32:21 -03003850static struct dvb_frontend_ops cxd2841er_t_c_ops = {
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003851 .delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A },
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003852 .info = {
Abylay Ospan3f3b48a2016-05-14 00:08:40 -03003853 .name = "", /* will set in attach function */
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003854 .caps = FE_CAN_FEC_1_2 |
3855 FE_CAN_FEC_2_3 |
3856 FE_CAN_FEC_3_4 |
3857 FE_CAN_FEC_5_6 |
3858 FE_CAN_FEC_7_8 |
3859 FE_CAN_FEC_AUTO |
3860 FE_CAN_QPSK |
3861 FE_CAN_QAM_16 |
3862 FE_CAN_QAM_32 |
3863 FE_CAN_QAM_64 |
3864 FE_CAN_QAM_128 |
3865 FE_CAN_QAM_256 |
3866 FE_CAN_QAM_AUTO |
3867 FE_CAN_TRANSMISSION_MODE_AUTO |
3868 FE_CAN_GUARD_INTERVAL_AUTO |
3869 FE_CAN_HIERARCHY_AUTO |
3870 FE_CAN_MUTE_TS |
3871 FE_CAN_2G_MODULATION,
3872 .frequency_min = 42000000,
Daniel Scheller158f0322017-03-19 12:26:39 -03003873 .frequency_max = 1002000000,
3874 .symbol_rate_min = 870000,
3875 .symbol_rate_max = 11700000
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003876 },
3877 .init = cxd2841er_init_tc,
3878 .sleep = cxd2841er_sleep_tc,
3879 .release = cxd2841er_release,
3880 .set_frontend = cxd2841er_set_frontend_tc,
3881 .get_frontend = cxd2841er_get_frontend,
3882 .read_status = cxd2841er_read_status_tc,
3883 .tune = cxd2841er_tune_tc,
3884 .i2c_gate_ctrl = cxd2841er_i2c_gate_ctrl,
3885 .get_frontend_algo = cxd2841er_get_algo
3886};
3887
Abylay Ospan83808c22016-03-22 19:20:34 -03003888MODULE_DESCRIPTION("Sony CXD2841ER/CXD2854ER DVB-C/C2/T/T2/S/S2 demodulator driver");
3889MODULE_AUTHOR("Sergey Kozlov <serjk@netup.ru>, Abylay Ospan <aospan@netup.ru>");
Kozlov Sergeya6dc60ff2015-07-28 11:33:03 -03003890MODULE_LICENSE("GPL");