Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / dvb-frontends / helene.c
bloba0d0b53c91d71e9743d7a655ccdc94dadfea9baa
1 /*
2 * helene.c
4 * Sony HELENE DVB-S/S2 DVB-T/T2 DVB-C/C2 ISDB-T/S tuner driver (CXD2858ER)
6 * Copyright 2012 Sony Corporation
7 * Copyright (C) 2014 NetUP Inc.
8 * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/dvb/frontend.h>
24 #include <linux/types.h>
25 #include "helene.h"
26 #include <media/dvb_frontend.h>
28 #define MAX_WRITE_REGSIZE 20
30 enum helene_state {
31 STATE_UNKNOWN,
32 STATE_SLEEP,
33 STATE_ACTIVE
36 struct helene_priv {
37 u32 frequency;
38 u8 i2c_address;
39 struct i2c_adapter *i2c;
40 enum helene_state state;
41 void *set_tuner_data;
42 int (*set_tuner)(void *, int);
43 enum helene_xtal xtal;
46 #define TERR_INTERNAL_LOOPFILTER_AVAILABLE(tv_system) \
47 (((tv_system) != SONY_HELENE_DTV_DVBC_6) && \
48 ((tv_system) != SONY_HELENE_DTV_DVBC_8)\
49 && ((tv_system) != SONY_HELENE_DTV_DVBC2_6) && \
50 ((tv_system) != SONY_HELENE_DTV_DVBC2_8))
52 #define HELENE_AUTO 0xff
53 #define HELENE_OFFSET(ofs) ((u8)(ofs) & 0x1F)
54 #define HELENE_BW_6 0x00
55 #define HELENE_BW_7 0x01
56 #define HELENE_BW_8 0x02
57 #define HELENE_BW_1_7 0x03
59 enum helene_tv_system_t {
60 SONY_HELENE_TV_SYSTEM_UNKNOWN,
61 /* Terrestrial Analog */
62 SONY_HELENE_ATV_MN_EIAJ,
63 /**< System-M (Japan) (IF: Fp=5.75MHz in default) */
64 SONY_HELENE_ATV_MN_SAP,
65 /**< System-M (US) (IF: Fp=5.75MHz in default) */
66 SONY_HELENE_ATV_MN_A2,
67 /**< System-M (Korea) (IF: Fp=5.9MHz in default) */
68 SONY_HELENE_ATV_BG,
69 /**< System-B/G (IF: Fp=7.3MHz in default) */
70 SONY_HELENE_ATV_I,
71 /**< System-I (IF: Fp=7.85MHz in default) */
72 SONY_HELENE_ATV_DK,
73 /**< System-D/K (IF: Fp=7.85MHz in default) */
74 SONY_HELENE_ATV_L,
75 /**< System-L (IF: Fp=7.85MHz in default) */
76 SONY_HELENE_ATV_L_DASH,
77 /**< System-L DASH (IF: Fp=2.2MHz in default) */
78 /* Terrestrial/Cable Digital */
79 SONY_HELENE_DTV_8VSB,
80 /**< ATSC 8VSB (IF: Fc=3.7MHz in default) */
81 SONY_HELENE_DTV_QAM,
82 /**< US QAM (IF: Fc=3.7MHz in default) */
83 SONY_HELENE_DTV_ISDBT_6,
84 /**< ISDB-T 6MHzBW (IF: Fc=3.55MHz in default) */
85 SONY_HELENE_DTV_ISDBT_7,
86 /**< ISDB-T 7MHzBW (IF: Fc=4.15MHz in default) */
87 SONY_HELENE_DTV_ISDBT_8,
88 /**< ISDB-T 8MHzBW (IF: Fc=4.75MHz in default) */
89 SONY_HELENE_DTV_DVBT_5,
90 /**< DVB-T 5MHzBW (IF: Fc=3.6MHz in default) */
91 SONY_HELENE_DTV_DVBT_6,
92 /**< DVB-T 6MHzBW (IF: Fc=3.6MHz in default) */
93 SONY_HELENE_DTV_DVBT_7,
94 /**< DVB-T 7MHzBW (IF: Fc=4.2MHz in default) */
95 SONY_HELENE_DTV_DVBT_8,
96 /**< DVB-T 8MHzBW (IF: Fc=4.8MHz in default) */
97 SONY_HELENE_DTV_DVBT2_1_7,
98 /**< DVB-T2 1.7MHzBW (IF: Fc=3.5MHz in default) */
99 SONY_HELENE_DTV_DVBT2_5,
100 /**< DVB-T2 5MHzBW (IF: Fc=3.6MHz in default) */
101 SONY_HELENE_DTV_DVBT2_6,
102 /**< DVB-T2 6MHzBW (IF: Fc=3.6MHz in default) */
103 SONY_HELENE_DTV_DVBT2_7,
104 /**< DVB-T2 7MHzBW (IF: Fc=4.2MHz in default) */
105 SONY_HELENE_DTV_DVBT2_8,
106 /**< DVB-T2 8MHzBW (IF: Fc=4.8MHz in default) */
107 SONY_HELENE_DTV_DVBC_6,
108 /**< DVB-C 6MHzBW (IF: Fc=3.7MHz in default) */
109 SONY_HELENE_DTV_DVBC_8,
110 /**< DVB-C 8MHzBW (IF: Fc=4.9MHz in default) */
111 SONY_HELENE_DTV_DVBC2_6,
112 /**< DVB-C2 6MHzBW (IF: Fc=3.7MHz in default) */
113 SONY_HELENE_DTV_DVBC2_8,
114 /**< DVB-C2 8MHzBW (IF: Fc=4.9MHz in default) */
115 SONY_HELENE_DTV_DTMB,
116 /**< DTMB (IF: Fc=5.1MHz in default) */
117 /* Satellite */
118 SONY_HELENE_STV_ISDBS,
119 /**< ISDB-S */
120 SONY_HELENE_STV_DVBS,
121 /**< DVB-S */
122 SONY_HELENE_STV_DVBS2,
123 /**< DVB-S2 */
125 SONY_HELENE_ATV_MIN = SONY_HELENE_ATV_MN_EIAJ,
126 /**< Minimum analog terrestrial system */
127 SONY_HELENE_ATV_MAX = SONY_HELENE_ATV_L_DASH,
128 /**< Maximum analog terrestrial system */
129 SONY_HELENE_DTV_MIN = SONY_HELENE_DTV_8VSB,
130 /**< Minimum digital terrestrial system */
131 SONY_HELENE_DTV_MAX = SONY_HELENE_DTV_DTMB,
132 /**< Maximum digital terrestrial system */
133 SONY_HELENE_TERR_TV_SYSTEM_NUM,
134 /**< Number of supported terrestrial broadcasting system */
135 SONY_HELENE_STV_MIN = SONY_HELENE_STV_ISDBS,
136 /**< Minimum satellite system */
137 SONY_HELENE_STV_MAX = SONY_HELENE_STV_DVBS2
138 /**< Maximum satellite system */
141 struct helene_terr_adjust_param_t {
142 /* < Addr:0x69 Bit[6:4] : RFVGA gain.
143 * 0xFF means Auto. (RF_GAIN_SEL = 1)
145 uint8_t RF_GAIN;
146 /* < Addr:0x69 Bit[3:0] : IF_BPF gain.
148 uint8_t IF_BPF_GC;
149 /* < Addr:0x6B Bit[3:0] : RF overload
150 * RF input detect level. (FRF <= 172MHz)
152 uint8_t RFOVLD_DET_LV1_VL;
153 /* < Addr:0x6B Bit[3:0] : RF overload
154 * RF input detect level. (172MHz < FRF <= 464MHz)
156 uint8_t RFOVLD_DET_LV1_VH;
157 /* < Addr:0x6B Bit[3:0] : RF overload
158 * RF input detect level. (FRF > 464MHz)
160 uint8_t RFOVLD_DET_LV1_U;
161 /* < Addr:0x6C Bit[2:0] :
162 * Internal RFAGC detect level. (FRF <= 172MHz)
164 uint8_t IFOVLD_DET_LV_VL;
165 /* < Addr:0x6C Bit[2:0] :
166 * Internal RFAGC detect level. (172MHz < FRF <= 464MHz)
168 uint8_t IFOVLD_DET_LV_VH;
169 /* < Addr:0x6C Bit[2:0] :
170 * Internal RFAGC detect level. (FRF > 464MHz)
172 uint8_t IFOVLD_DET_LV_U;
173 /* < Addr:0x6D Bit[5:4] :
174 * IF filter center offset.
176 uint8_t IF_BPF_F0;
177 /* < Addr:0x6D Bit[1:0] :
178 * 6MHzBW(0x00) or 7MHzBW(0x01)
179 * or 8MHzBW(0x02) or 1.7MHzBW(0x03)
181 uint8_t BW;
182 /* < Addr:0x6E Bit[4:0] :
183 * 5bit signed. IF offset (kHz) = FIF_OFFSET x 50
185 uint8_t FIF_OFFSET;
186 /* < Addr:0x6F Bit[4:0] :
187 * 5bit signed. BW offset (kHz) =
188 * BW_OFFSET x 50 (BW_OFFSET x 10 in 1.7MHzBW)
190 uint8_t BW_OFFSET;
191 /* < Addr:0x9C Bit[0] :
192 * Local polarity. (0: Upper Local, 1: Lower Local)
194 uint8_t IS_LOWERLOCAL;
197 static const struct helene_terr_adjust_param_t
198 terr_params[SONY_HELENE_TERR_TV_SYSTEM_NUM] = {
199 /*< SONY_HELENE_TV_SYSTEM_UNKNOWN */
200 {HELENE_AUTO, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
201 HELENE_BW_6, HELENE_OFFSET(0), HELENE_OFFSET(0), 0x00},
202 /* Analog */
203 /**< SONY_HELENE_ATV_MN_EIAJ (System-M (Japan)) */
204 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
205 HELENE_BW_6, HELENE_OFFSET(0), HELENE_OFFSET(1), 0x00},
206 /**< SONY_HELENE_ATV_MN_SAP (System-M (US)) */
207 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
208 HELENE_BW_6, HELENE_OFFSET(0), HELENE_OFFSET(1), 0x00},
209 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
210 HELENE_BW_6, HELENE_OFFSET(3), HELENE_OFFSET(1), 0x00},
211 /**< SONY_HELENE_ATV_MN_A2 (System-M (Korea)) */
212 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
213 HELENE_BW_7, HELENE_OFFSET(11), HELENE_OFFSET(5), 0x00},
214 /**< SONY_HELENE_ATV_BG (System-B/G) */
215 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
216 HELENE_BW_8, HELENE_OFFSET(2), HELENE_OFFSET(-3), 0x00},
217 /**< SONY_HELENE_ATV_I (System-I) */
218 {HELENE_AUTO, 0x05, 0x03, 0x06, 0x03, 0x01, 0x01, 0x01, 0x00,
219 HELENE_BW_8, HELENE_OFFSET(2), HELENE_OFFSET(-3), 0x00},
220 /**< SONY_HELENE_ATV_DK (System-D/K) */
221 {HELENE_AUTO, 0x03, 0x04, 0x0A, 0x04, 0x04, 0x04, 0x04, 0x00,
222 HELENE_BW_8, HELENE_OFFSET(2), HELENE_OFFSET(-3), 0x00},
223 /**< SONY_HELENE_ATV_L (System-L) */
224 {HELENE_AUTO, 0x03, 0x04, 0x0A, 0x04, 0x04, 0x04, 0x04, 0x00,
225 HELENE_BW_8, HELENE_OFFSET(-1), HELENE_OFFSET(4), 0x00},
226 /**< SONY_HELENE_ATV_L_DASH (System-L DASH) */
227 /* Digital */
228 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x03, 0x03, 0x03, 0x00,
229 HELENE_BW_6, HELENE_OFFSET(-6), HELENE_OFFSET(-3), 0x00},
230 /**< SONY_HELENE_DTV_8VSB (ATSC 8VSB) */
231 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
232 HELENE_BW_6, HELENE_OFFSET(-6), HELENE_OFFSET(-3), 0x00},
233 /**< SONY_HELENE_DTV_QAM (US QAM) */
234 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
235 HELENE_BW_6, HELENE_OFFSET(-9), HELENE_OFFSET(-5), 0x00},
236 /**< SONY_HELENE_DTV_ISDBT_6 (ISDB-T 6MHzBW) */
237 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
238 HELENE_BW_7, HELENE_OFFSET(-7), HELENE_OFFSET(-6), 0x00},
239 /**< SONY_HELENE_DTV_ISDBT_7 (ISDB-T 7MHzBW) */
240 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
241 HELENE_BW_8, HELENE_OFFSET(-5), HELENE_OFFSET(-7), 0x00},
242 /**< SONY_HELENE_DTV_ISDBT_8 (ISDB-T 8MHzBW) */
243 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
244 HELENE_BW_6, HELENE_OFFSET(-8), HELENE_OFFSET(-3), 0x00},
245 /**< SONY_HELENE_DTV_DVBT_5 (DVB-T 5MHzBW) */
246 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
247 HELENE_BW_6, HELENE_OFFSET(-8), HELENE_OFFSET(-3), 0x00},
248 /**< SONY_HELENE_DTV_DVBT_6 (DVB-T 6MHzBW) */
249 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
250 HELENE_BW_7, HELENE_OFFSET(-6), HELENE_OFFSET(-5), 0x00},
251 /**< SONY_HELENE_DTV_DVBT_7 (DVB-T 7MHzBW) */
252 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
253 HELENE_BW_8, HELENE_OFFSET(-4), HELENE_OFFSET(-6), 0x00},
254 /**< SONY_HELENE_DTV_DVBT_8 (DVB-T 8MHzBW) */
255 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
256 HELENE_BW_1_7, HELENE_OFFSET(-10), HELENE_OFFSET(-10), 0x00},
257 /**< SONY_HELENE_DTV_DVBT2_1_7 (DVB-T2 1.7MHzBW) */
258 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
259 HELENE_BW_6, HELENE_OFFSET(-8), HELENE_OFFSET(-3), 0x00},
260 /**< SONY_HELENE_DTV_DVBT2_5 (DVB-T2 5MHzBW) */
261 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
262 HELENE_BW_6, HELENE_OFFSET(-8), HELENE_OFFSET(-3), 0x00},
263 /**< SONY_HELENE_DTV_DVBT2_6 (DVB-T2 6MHzBW) */
264 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
265 HELENE_BW_7, HELENE_OFFSET(-6), HELENE_OFFSET(-5), 0x00},
266 /**< SONY_HELENE_DTV_DVBT2_7 (DVB-T2 7MHzBW) */
267 {HELENE_AUTO, 0x09, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
268 HELENE_BW_8, HELENE_OFFSET(-4), HELENE_OFFSET(-6), 0x00},
269 /**< SONY_HELENE_DTV_DVBT2_8 (DVB-T2 8MHzBW) */
270 {HELENE_AUTO, 0x05, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x00,
271 HELENE_BW_6, HELENE_OFFSET(-6), HELENE_OFFSET(-4), 0x00},
272 /**< SONY_HELENE_DTV_DVBC_6 (DVB-C 6MHzBW) */
273 {HELENE_AUTO, 0x05, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x00,
274 HELENE_BW_8, HELENE_OFFSET(-2), HELENE_OFFSET(-3), 0x00},
275 /**< SONY_HELENE_DTV_DVBC_8 (DVB-C 8MHzBW) */
276 {HELENE_AUTO, 0x03, 0x09, 0x09, 0x09, 0x02, 0x02, 0x02, 0x00,
277 HELENE_BW_6, HELENE_OFFSET(-6), HELENE_OFFSET(-2), 0x00},
278 /**< SONY_HELENE_DTV_DVBC2_6 (DVB-C2 6MHzBW) */
279 {HELENE_AUTO, 0x03, 0x09, 0x09, 0x09, 0x02, 0x02, 0x02, 0x00,
280 HELENE_BW_8, HELENE_OFFSET(-2), HELENE_OFFSET(0), 0x00},
281 /**< SONY_HELENE_DTV_DVBC2_8 (DVB-C2 8MHzBW) */
282 {HELENE_AUTO, 0x04, 0x0B, 0x0B, 0x0B, 0x02, 0x02, 0x02, 0x00,
283 HELENE_BW_8, HELENE_OFFSET(2), HELENE_OFFSET(1), 0x00}
284 /**< SONY_HELENE_DTV_DTMB (DTMB) */
287 static void helene_i2c_debug(struct helene_priv *priv,
288 u8 reg, u8 write, const u8 *data, u32 len)
290 dev_dbg(&priv->i2c->dev, "helene: I2C %s reg 0x%02x size %d\n",
291 (write == 0 ? "read" : "write"), reg, len);
292 print_hex_dump_bytes("helene: I2C data: ",
293 DUMP_PREFIX_OFFSET, data, len);
296 static int helene_write_regs(struct helene_priv *priv,
297 u8 reg, const u8 *data, u32 len)
299 int ret;
300 u8 buf[MAX_WRITE_REGSIZE + 1];
301 struct i2c_msg msg[1] = {
303 .addr = priv->i2c_address,
304 .flags = 0,
305 .len = len + 1,
306 .buf = buf,
310 if (len + 1 > sizeof(buf)) {
311 dev_warn(&priv->i2c->dev,
312 "wr reg=%04x: len=%d vs %zu is too big!\n",
313 reg, len + 1, sizeof(buf));
314 return -E2BIG;
317 helene_i2c_debug(priv, reg, 1, data, len);
318 buf[0] = reg;
319 memcpy(&buf[1], data, len);
320 ret = i2c_transfer(priv->i2c, msg, 1);
321 if (ret >= 0 && ret != 1)
322 ret = -EREMOTEIO;
323 if (ret < 0) {
324 dev_warn(&priv->i2c->dev,
325 "%s: i2c wr failed=%d reg=%02x len=%d\n",
326 KBUILD_MODNAME, ret, reg, len);
327 return ret;
329 return 0;
332 static int helene_write_reg(struct helene_priv *priv, u8 reg, u8 val)
334 u8 tmp = val; /* see gcc.gnu.org/bugzilla/show_bug.cgi?id=81715 */
336 return helene_write_regs(priv, reg, &tmp, 1);
339 static int helene_read_regs(struct helene_priv *priv,
340 u8 reg, u8 *val, u32 len)
342 int ret;
343 struct i2c_msg msg[2] = {
345 .addr = priv->i2c_address,
346 .flags = 0,
347 .len = 1,
348 .buf = &reg,
349 }, {
350 .addr = priv->i2c_address,
351 .flags = I2C_M_RD,
352 .len = len,
353 .buf = val,
357 ret = i2c_transfer(priv->i2c, &msg[0], 1);
358 if (ret >= 0 && ret != 1)
359 ret = -EREMOTEIO;
360 if (ret < 0) {
361 dev_warn(&priv->i2c->dev,
362 "%s: I2C rw failed=%d addr=%02x reg=%02x\n",
363 KBUILD_MODNAME, ret, priv->i2c_address, reg);
364 return ret;
366 ret = i2c_transfer(priv->i2c, &msg[1], 1);
367 if (ret >= 0 && ret != 1)
368 ret = -EREMOTEIO;
369 if (ret < 0) {
370 dev_warn(&priv->i2c->dev,
371 "%s: i2c rd failed=%d addr=%02x reg=%02x\n",
372 KBUILD_MODNAME, ret, priv->i2c_address, reg);
373 return ret;
375 helene_i2c_debug(priv, reg, 0, val, len);
376 return 0;
379 static int helene_read_reg(struct helene_priv *priv, u8 reg, u8 *val)
381 return helene_read_regs(priv, reg, val, 1);
384 static int helene_set_reg_bits(struct helene_priv *priv,
385 u8 reg, u8 data, u8 mask)
387 int res;
388 u8 rdata;
390 if (mask != 0xff) {
391 res = helene_read_reg(priv, reg, &rdata);
392 if (res != 0)
393 return res;
394 data = ((data & mask) | (rdata & (mask ^ 0xFF)));
396 return helene_write_reg(priv, reg, data);
399 static int helene_enter_power_save(struct helene_priv *priv)
401 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
402 if (priv->state == STATE_SLEEP)
403 return 0;
405 /* Standby setting for CPU */
406 helene_write_reg(priv, 0x88, 0x0);
408 /* Standby setting for internal logic block */
409 helene_write_reg(priv, 0x87, 0xC0);
411 priv->state = STATE_SLEEP;
412 return 0;
415 static int helene_leave_power_save(struct helene_priv *priv)
417 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
418 if (priv->state == STATE_ACTIVE)
419 return 0;
421 /* Standby setting for internal logic block */
422 helene_write_reg(priv, 0x87, 0xC4);
424 /* Standby setting for CPU */
425 helene_write_reg(priv, 0x88, 0x40);
427 priv->state = STATE_ACTIVE;
428 return 0;
431 static int helene_init(struct dvb_frontend *fe)
433 struct helene_priv *priv = fe->tuner_priv;
435 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
436 return helene_leave_power_save(priv);
439 static void helene_release(struct dvb_frontend *fe)
441 struct helene_priv *priv = fe->tuner_priv;
443 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
444 kfree(fe->tuner_priv);
445 fe->tuner_priv = NULL;
448 static int helene_sleep(struct dvb_frontend *fe)
450 struct helene_priv *priv = fe->tuner_priv;
452 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
453 helene_enter_power_save(priv);
454 return 0;
457 static enum helene_tv_system_t helene_get_tv_system(struct dvb_frontend *fe)
459 enum helene_tv_system_t system = SONY_HELENE_TV_SYSTEM_UNKNOWN;
460 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
461 struct helene_priv *priv = fe->tuner_priv;
463 if (p->delivery_system == SYS_DVBT) {
464 if (p->bandwidth_hz <= 5000000)
465 system = SONY_HELENE_DTV_DVBT_5;
466 else if (p->bandwidth_hz <= 6000000)
467 system = SONY_HELENE_DTV_DVBT_6;
468 else if (p->bandwidth_hz <= 7000000)
469 system = SONY_HELENE_DTV_DVBT_7;
470 else if (p->bandwidth_hz <= 8000000)
471 system = SONY_HELENE_DTV_DVBT_8;
472 else {
473 system = SONY_HELENE_DTV_DVBT_8;
474 p->bandwidth_hz = 8000000;
476 } else if (p->delivery_system == SYS_DVBT2) {
477 if (p->bandwidth_hz <= 5000000)
478 system = SONY_HELENE_DTV_DVBT2_5;
479 else if (p->bandwidth_hz <= 6000000)
480 system = SONY_HELENE_DTV_DVBT2_6;
481 else if (p->bandwidth_hz <= 7000000)
482 system = SONY_HELENE_DTV_DVBT2_7;
483 else if (p->bandwidth_hz <= 8000000)
484 system = SONY_HELENE_DTV_DVBT2_8;
485 else {
486 system = SONY_HELENE_DTV_DVBT2_8;
487 p->bandwidth_hz = 8000000;
489 } else if (p->delivery_system == SYS_DVBS) {
490 system = SONY_HELENE_STV_DVBS;
491 } else if (p->delivery_system == SYS_DVBS2) {
492 system = SONY_HELENE_STV_DVBS2;
493 } else if (p->delivery_system == SYS_ISDBS) {
494 system = SONY_HELENE_STV_ISDBS;
495 } else if (p->delivery_system == SYS_ISDBT) {
496 if (p->bandwidth_hz <= 6000000)
497 system = SONY_HELENE_DTV_ISDBT_6;
498 else if (p->bandwidth_hz <= 7000000)
499 system = SONY_HELENE_DTV_ISDBT_7;
500 else if (p->bandwidth_hz <= 8000000)
501 system = SONY_HELENE_DTV_ISDBT_8;
502 else {
503 system = SONY_HELENE_DTV_ISDBT_8;
504 p->bandwidth_hz = 8000000;
506 } else if (p->delivery_system == SYS_DVBC_ANNEX_A) {
507 if (p->bandwidth_hz <= 6000000)
508 system = SONY_HELENE_DTV_DVBC_6;
509 else if (p->bandwidth_hz <= 8000000)
510 system = SONY_HELENE_DTV_DVBC_8;
512 dev_dbg(&priv->i2c->dev,
513 "%s(): HELENE DTV system %d (delsys %d, bandwidth %d)\n",
514 __func__, (int)system, p->delivery_system,
515 p->bandwidth_hz);
516 return system;
519 static int helene_set_params_s(struct dvb_frontend *fe)
521 u8 data[MAX_WRITE_REGSIZE];
522 u32 frequency;
523 enum helene_tv_system_t tv_system;
524 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
525 struct helene_priv *priv = fe->tuner_priv;
526 int frequencykHz = p->frequency;
527 uint32_t frequency4kHz = 0;
528 u32 symbol_rate = p->symbol_rate/1000;
530 dev_dbg(&priv->i2c->dev, "%s(): tune frequency %dkHz sr=%uKsps\n",
531 __func__, frequencykHz, symbol_rate);
532 tv_system = helene_get_tv_system(fe);
534 if (tv_system == SONY_HELENE_TV_SYSTEM_UNKNOWN) {
535 dev_err(&priv->i2c->dev, "%s(): unknown DTV system\n",
536 __func__);
537 return -EINVAL;
539 /* RF switch turn to satellite */
540 if (priv->set_tuner)
541 priv->set_tuner(priv->set_tuner_data, 0);
542 frequency = roundup(p->frequency / 1000, 1);
544 /* Disable IF signal output */
545 helene_write_reg(priv, 0x15, 0x02);
547 /* RFIN matching in power save (Sat) reset */
548 helene_write_reg(priv, 0x43, 0x06);
550 /* Analog block setting (0x6A, 0x6B) */
551 data[0] = 0x00;
552 data[1] = 0x00;
553 helene_write_regs(priv, 0x6A, data, 2);
554 helene_write_reg(priv, 0x75, 0x99);
555 helene_write_reg(priv, 0x9D, 0x00);
557 /* Tuning setting for CPU (0x61) */
558 helene_write_reg(priv, 0x61, 0x07);
560 /* Satellite mode select (0x01) */
561 helene_write_reg(priv, 0x01, 0x01);
563 /* Clock enable for internal logic block, CPU wake-up (0x04, 0x05) */
564 data[0] = 0xC4;
565 data[1] = 0x40;
567 switch (priv->xtal) {
568 case SONY_HELENE_XTAL_16000:
569 data[2] = 0x02;
570 break;
571 case SONY_HELENE_XTAL_20500:
572 data[2] = 0x02;
573 break;
574 case SONY_HELENE_XTAL_24000:
575 data[2] = 0x03;
576 break;
577 case SONY_HELENE_XTAL_41000:
578 data[2] = 0x05;
579 break;
580 default:
581 dev_err(&priv->i2c->dev, "%s(): unknown xtal %d\n",
582 __func__, priv->xtal);
583 return -EINVAL;
586 /* Setting for analog block (0x07). LOOPFILTER INTERNAL */
587 data[3] = 0x80;
589 /* Tuning setting for analog block
590 * (0x08, 0x09, 0x0A, 0x0B). LOOPFILTER INTERNAL
592 if (priv->xtal == SONY_HELENE_XTAL_20500)
593 data[4] = 0x58;
594 else
595 data[4] = 0x70;
597 data[5] = 0x1E;
598 data[6] = 0x02;
599 data[7] = 0x24;
601 /* Enable for analog block (0x0C, 0x0D, 0x0E). SAT LNA ON */
602 data[8] = 0x0F;
603 data[8] |= 0xE0; /* POWERSAVE_TERR_RF_ACTIVE */
604 data[9] = 0x02;
605 data[10] = 0x1E;
607 /* Setting for LPF cutoff frequency (0x0F) */
608 switch (tv_system) {
609 case SONY_HELENE_STV_ISDBS:
610 data[11] = 0x22; /* 22MHz */
611 break;
612 case SONY_HELENE_STV_DVBS:
613 if (symbol_rate <= 4000)
614 data[11] = 0x05;
615 else if (symbol_rate <= 10000)
616 data[11] = (uint8_t)((symbol_rate * 47
617 + (40000-1)) / 40000);
618 else
619 data[11] = (uint8_t)((symbol_rate * 27
620 + (40000-1)) / 40000 + 5);
622 if (data[11] > 36)
623 data[11] = 36; /* 5 <= lpf_cutoff <= 36 is valid */
624 break;
625 case SONY_HELENE_STV_DVBS2:
626 if (symbol_rate <= 4000)
627 data[11] = 0x05;
628 else if (symbol_rate <= 10000)
629 data[11] = (uint8_t)((symbol_rate * 11
630 + (10000-1)) / 10000);
631 else
632 data[11] = (uint8_t)((symbol_rate * 3
633 + (5000-1)) / 5000 + 5);
635 if (data[11] > 36)
636 data[11] = 36; /* 5 <= lpf_cutoff <= 36 is valid */
637 break;
638 default:
639 dev_err(&priv->i2c->dev, "%s(): unknown standard %d\n",
640 __func__, tv_system);
641 return -EINVAL;
644 /* RF tuning frequency setting (0x10, 0x11, 0x12) */
645 frequency4kHz = (frequencykHz + 2) / 4;
646 data[12] = (uint8_t)(frequency4kHz & 0xFF); /* FRF_L */
647 data[13] = (uint8_t)((frequency4kHz >> 8) & 0xFF); /* FRF_M */
648 /* FRF_H (bit[3:0]) */
649 data[14] = (uint8_t)((frequency4kHz >> 16) & 0x0F);
651 /* Tuning command (0x13) */
652 data[15] = 0xFF;
654 /* Setting for IQOUT_LIMIT (0x14) 0.75Vpp */
655 data[16] = 0x00;
657 /* Enable IQ output (0x15) */
658 data[17] = 0x01;
660 helene_write_regs(priv, 0x04, data, 18);
662 dev_dbg(&priv->i2c->dev, "%s(): tune done\n",
663 __func__);
665 priv->frequency = frequency;
666 return 0;
669 static int helene_set_params(struct dvb_frontend *fe)
671 u8 data[MAX_WRITE_REGSIZE];
672 u32 frequency;
673 enum helene_tv_system_t tv_system;
674 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
675 struct helene_priv *priv = fe->tuner_priv;
676 int frequencykHz = p->frequency / 1000;
678 dev_dbg(&priv->i2c->dev, "%s(): tune frequency %dkHz\n",
679 __func__, frequencykHz);
680 tv_system = helene_get_tv_system(fe);
682 if (tv_system == SONY_HELENE_TV_SYSTEM_UNKNOWN) {
683 dev_dbg(&priv->i2c->dev, "%s(): unknown DTV system\n",
684 __func__);
685 return -EINVAL;
687 if (priv->set_tuner)
688 priv->set_tuner(priv->set_tuner_data, 1);
689 frequency = roundup(p->frequency / 1000, 25);
691 /* mode select */
692 helene_write_reg(priv, 0x01, 0x00);
694 /* Disable IF signal output */
695 helene_write_reg(priv, 0x74, 0x02);
697 if (priv->state == STATE_SLEEP)
698 helene_leave_power_save(priv);
700 /* Initial setting for internal analog block (0x91, 0x92) */
701 if ((tv_system == SONY_HELENE_DTV_DVBC_6) ||
702 (tv_system == SONY_HELENE_DTV_DVBC_8)) {
703 data[0] = 0x16;
704 data[1] = 0x26;
705 } else {
706 data[0] = 0x10;
707 data[1] = 0x20;
709 helene_write_regs(priv, 0x91, data, 2);
711 /* Setting for analog block */
712 if (TERR_INTERNAL_LOOPFILTER_AVAILABLE(tv_system))
713 data[0] = 0x90;
714 else
715 data[0] = 0x00;
717 /* Setting for local polarity (0x9D) */
718 data[1] = (uint8_t)(terr_params[tv_system].IS_LOWERLOCAL & 0x01);
719 helene_write_regs(priv, 0x9C, data, 2);
721 /* Enable for analog block */
722 data[0] = 0xEE;
723 data[1] = 0x02;
724 data[2] = 0x1E;
725 data[3] = 0x67; /* Tuning setting for CPU */
727 /* Setting for PLL reference divider for xtal=24MHz */
728 if ((tv_system == SONY_HELENE_DTV_DVBC_6) ||
729 (tv_system == SONY_HELENE_DTV_DVBC_8))
730 data[4] = 0x18;
731 else
732 data[4] = 0x03;
734 /* Tuning setting for analog block */
735 if (TERR_INTERNAL_LOOPFILTER_AVAILABLE(tv_system)) {
736 data[5] = 0x38;
737 data[6] = 0x1E;
738 data[7] = 0x02;
739 data[8] = 0x24;
740 } else if ((tv_system == SONY_HELENE_DTV_DVBC_6) ||
741 (tv_system == SONY_HELENE_DTV_DVBC_8)) {
742 data[5] = 0x1C;
743 data[6] = 0x78;
744 data[7] = 0x08;
745 data[8] = 0x1C;
746 } else {
747 data[5] = 0xB4;
748 data[6] = 0x78;
749 data[7] = 0x08;
750 data[8] = 0x30;
752 helene_write_regs(priv, 0x5E, data, 9);
754 /* LT_AMP_EN should be 0 */
755 helene_set_reg_bits(priv, 0x67, 0x0, 0x02);
757 /* Setting for IFOUT_LIMIT */
758 data[0] = 0x00; /* 1.5Vpp */
760 /* RF_GAIN setting */
761 if (terr_params[tv_system].RF_GAIN == HELENE_AUTO)
762 data[1] = 0x80; /* RF_GAIN_SEL = 1 */
763 else
764 data[1] = (uint8_t)((terr_params[tv_system].RF_GAIN
765 << 4) & 0x70);
767 /* IF_BPF_GC setting */
768 data[1] |= (uint8_t)(terr_params[tv_system].IF_BPF_GC & 0x0F);
770 /* Setting for internal RFAGC (0x6A, 0x6B, 0x6C) */
771 data[2] = 0x00;
772 if (frequencykHz <= 172000) {
773 data[3] = (uint8_t)(terr_params[tv_system].RFOVLD_DET_LV1_VL
774 & 0x0F);
775 data[4] = (uint8_t)(terr_params[tv_system].IFOVLD_DET_LV_VL
776 & 0x07);
777 } else if (frequencykHz <= 464000) {
778 data[3] = (uint8_t)(terr_params[tv_system].RFOVLD_DET_LV1_VH
779 & 0x0F);
780 data[4] = (uint8_t)(terr_params[tv_system].IFOVLD_DET_LV_VH
781 & 0x07);
782 } else {
783 data[3] = (uint8_t)(terr_params[tv_system].RFOVLD_DET_LV1_U
784 & 0x0F);
785 data[4] = (uint8_t)(terr_params[tv_system].IFOVLD_DET_LV_U
786 & 0x07);
788 data[4] |= 0x20;
790 /* Setting for IF frequency and bandwidth */
792 /* IF filter center frequency offset (IF_BPF_F0) (0x6D) */
793 data[5] = (uint8_t)((terr_params[tv_system].IF_BPF_F0 << 4) & 0x30);
795 /* IF filter band width (BW) (0x6D) */
796 data[5] |= (uint8_t)(terr_params[tv_system].BW & 0x03);
798 /* IF frequency offset value (FIF_OFFSET) (0x6E) */
799 data[6] = (uint8_t)(terr_params[tv_system].FIF_OFFSET & 0x1F);
801 /* IF band width offset value (BW_OFFSET) (0x6F) */
802 data[7] = (uint8_t)(terr_params[tv_system].BW_OFFSET & 0x1F);
804 /* RF tuning frequency setting (0x70, 0x71, 0x72) */
805 data[8] = (uint8_t)(frequencykHz & 0xFF); /* FRF_L */
806 data[9] = (uint8_t)((frequencykHz >> 8) & 0xFF); /* FRF_M */
807 data[10] = (uint8_t)((frequencykHz >> 16)
808 & 0x0F); /* FRF_H (bit[3:0]) */
810 /* Tuning command */
811 data[11] = 0xFF;
813 /* Enable IF output, AGC and IFOUT pin selection (0x74) */
814 data[12] = 0x01;
816 if ((tv_system == SONY_HELENE_DTV_DVBC_6) ||
817 (tv_system == SONY_HELENE_DTV_DVBC_8)) {
818 data[13] = 0xD9;
819 data[14] = 0x0F;
820 data[15] = 0x24;
821 data[16] = 0x87;
822 } else {
823 data[13] = 0x99;
824 data[14] = 0x00;
825 data[15] = 0x24;
826 data[16] = 0x87;
829 helene_write_regs(priv, 0x68, data, 17);
831 dev_dbg(&priv->i2c->dev, "%s(): tune done\n",
832 __func__);
834 priv->frequency = frequency;
835 return 0;
838 static int helene_get_frequency(struct dvb_frontend *fe, u32 *frequency)
840 struct helene_priv *priv = fe->tuner_priv;
842 *frequency = priv->frequency * 1000;
843 return 0;
846 static const struct dvb_tuner_ops helene_tuner_ops = {
847 .info = {
848 .name = "Sony HELENE Ter tuner",
849 .frequency_min = 1000000,
850 .frequency_max = 1200000000,
851 .frequency_step = 25000,
853 .init = helene_init,
854 .release = helene_release,
855 .sleep = helene_sleep,
856 .set_params = helene_set_params,
857 .get_frequency = helene_get_frequency,
860 static const struct dvb_tuner_ops helene_tuner_ops_s = {
861 .info = {
862 .name = "Sony HELENE Sat tuner",
863 .frequency_min = 500000,
864 .frequency_max = 2500000,
865 .frequency_step = 1000,
867 .init = helene_init,
868 .release = helene_release,
869 .sleep = helene_sleep,
870 .set_params = helene_set_params_s,
871 .get_frequency = helene_get_frequency,
874 /* power-on tuner
875 * call once after reset
877 static int helene_x_pon(struct helene_priv *priv)
879 /* RFIN matching in power save (terrestrial) = ACTIVE */
880 /* RFIN matching in power save (satellite) = ACTIVE */
881 u8 dataT[] = { 0x06, 0x00, 0x02, 0x00 };
882 /* SAT_RF_ACTIVE = true, lnaOff = false, terrRfActive = true */
883 u8 dataS[] = { 0x05, 0x06 };
884 u8 cdata[] = {0x7A, 0x01};
885 u8 data[20];
886 u8 rdata[2];
888 /* mode select */
889 helene_write_reg(priv, 0x01, 0x00);
891 helene_write_reg(priv, 0x67, dataT[3]);
892 helene_write_reg(priv, 0x43, dataS[1]);
893 helene_write_regs(priv, 0x5E, dataT, 3);
894 helene_write_reg(priv, 0x0C, dataS[0]);
896 /* Initial setting for internal logic block */
897 helene_write_regs(priv, 0x99, cdata, sizeof(cdata));
899 /* 0x81 - 0x94 */
900 data[0] = 0x18; /* xtal 24 MHz */
901 data[1] = (uint8_t)(0x80 | (0x04 & 0x1F)); /* 4 x 25 = 100uA */
902 data[2] = (uint8_t)(0x80 | (0x26 & 0x7F)); /* 38 x 0.25 = 9.5pF */
903 data[3] = 0x80; /* REFOUT signal output 500mVpp */
904 data[4] = 0x00; /* GPIO settings */
905 data[5] = 0x00; /* GPIO settings */
906 data[6] = 0xC4; /* Clock enable for internal logic block */
907 data[7] = 0x40; /* Start CPU boot-up */
908 data[8] = 0x10; /* For burst-write */
910 /* Setting for internal RFAGC */
911 data[9] = 0x00;
912 data[10] = 0x45;
913 data[11] = 0x75;
915 data[12] = 0x07; /* Setting for analog block */
917 /* Initial setting for internal analog block */
918 data[13] = 0x1C;
919 data[14] = 0x3F;
920 data[15] = 0x02;
921 data[16] = 0x10;
922 data[17] = 0x20;
923 data[18] = 0x0A;
924 data[19] = 0x00;
926 helene_write_regs(priv, 0x81, data, sizeof(data));
928 /* Setting for internal RFAGC */
929 helene_write_reg(priv, 0x9B, 0x00);
931 msleep(20);
933 /* Check CPU_STT/CPU_ERR */
934 helene_read_regs(priv, 0x1A, rdata, sizeof(rdata));
936 if (rdata[0] != 0x00) {
937 dev_err(&priv->i2c->dev,
938 "HELENE tuner CPU error 0x%x\n", rdata[0]);
939 return -EIO;
942 /* VCO current setting */
943 cdata[0] = 0x90;
944 cdata[1] = 0x06;
945 helene_write_regs(priv, 0x17, cdata, sizeof(cdata));
946 msleep(20);
947 helene_read_reg(priv, 0x19, data);
948 helene_write_reg(priv, 0x95, (uint8_t)((data[0] >> 4) & 0x0F));
950 /* Disable IF signal output */
951 helene_write_reg(priv, 0x74, 0x02);
953 /* Standby setting for CPU */
954 helene_write_reg(priv, 0x88, 0x00);
956 /* Standby setting for internal logic block */
957 helene_write_reg(priv, 0x87, 0xC0);
959 /* Load capacitance control setting for crystal oscillator */
960 helene_write_reg(priv, 0x80, 0x01);
962 /* Satellite initial setting */
963 cdata[0] = 0x07;
964 cdata[1] = 0x00;
965 helene_write_regs(priv, 0x41, cdata, sizeof(cdata));
967 dev_info(&priv->i2c->dev,
968 "HELENE tuner x_pon done\n");
970 return 0;
973 struct dvb_frontend *helene_attach_s(struct dvb_frontend *fe,
974 const struct helene_config *config,
975 struct i2c_adapter *i2c)
977 struct helene_priv *priv = NULL;
979 priv = kzalloc(sizeof(struct helene_priv), GFP_KERNEL);
980 if (priv == NULL)
981 return NULL;
982 priv->i2c_address = (config->i2c_address >> 1);
983 priv->i2c = i2c;
984 priv->set_tuner_data = config->set_tuner_priv;
985 priv->set_tuner = config->set_tuner_callback;
986 priv->xtal = config->xtal;
988 if (fe->ops.i2c_gate_ctrl)
989 fe->ops.i2c_gate_ctrl(fe, 1);
991 if (helene_x_pon(priv) != 0) {
992 kfree(priv);
993 return NULL;
996 if (fe->ops.i2c_gate_ctrl)
997 fe->ops.i2c_gate_ctrl(fe, 0);
999 memcpy(&fe->ops.tuner_ops, &helene_tuner_ops_s,
1000 sizeof(struct dvb_tuner_ops));
1001 fe->tuner_priv = priv;
1002 dev_info(&priv->i2c->dev,
1003 "Sony HELENE Sat attached on addr=%x at I2C adapter %p\n",
1004 priv->i2c_address, priv->i2c);
1005 return fe;
1007 EXPORT_SYMBOL(helene_attach_s);
1009 struct dvb_frontend *helene_attach(struct dvb_frontend *fe,
1010 const struct helene_config *config,
1011 struct i2c_adapter *i2c)
1013 struct helene_priv *priv = NULL;
1015 priv = kzalloc(sizeof(struct helene_priv), GFP_KERNEL);
1016 if (priv == NULL)
1017 return NULL;
1018 priv->i2c_address = (config->i2c_address >> 1);
1019 priv->i2c = i2c;
1020 priv->set_tuner_data = config->set_tuner_priv;
1021 priv->set_tuner = config->set_tuner_callback;
1022 priv->xtal = config->xtal;
1024 if (fe->ops.i2c_gate_ctrl)
1025 fe->ops.i2c_gate_ctrl(fe, 1);
1027 if (helene_x_pon(priv) != 0) {
1028 kfree(priv);
1029 return NULL;
1032 if (fe->ops.i2c_gate_ctrl)
1033 fe->ops.i2c_gate_ctrl(fe, 0);
1035 memcpy(&fe->ops.tuner_ops, &helene_tuner_ops,
1036 sizeof(struct dvb_tuner_ops));
1037 fe->tuner_priv = priv;
1038 dev_info(&priv->i2c->dev,
1039 "Sony HELENE Ter attached on addr=%x at I2C adapter %p\n",
1040 priv->i2c_address, priv->i2c);
1041 return fe;
1043 EXPORT_SYMBOL(helene_attach);
1045 MODULE_DESCRIPTION("Sony HELENE Sat/Ter tuner driver");
1046 MODULE_AUTHOR("Abylay Ospan <aospan@netup.ru>");
1047 MODULE_LICENSE("GPL");