4 * Sony Horus3A DVB-S/S2 tuner driver
6 * Copyright 2012 Sony Corporation
7 * Copyright (C) 2014 NetUP Inc.
8 * Copyright (C) 2014 Sergey Kozlov <serjk@netup.ru>
9 * Copyright (C) 2014 Abylay Ospan <aospan@netup.ru>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
22 #include <linux/slab.h>
23 #include <linux/module.h>
24 #include <linux/dvb/frontend.h>
25 #include <linux/types.h>
27 #include "dvb_frontend.h"
29 #define MAX_WRITE_REGSIZE 5
40 struct i2c_adapter *i2c;
41 enum horus3a_state state;
43 int (*set_tuner)(void *, int);
46 static void horus3a_i2c_debug(struct horus3a_priv *priv,
47 u8 reg, u8 write, const u8 *data, u32 len)
49 dev_dbg(&priv->i2c->dev, "horus3a: I2C %s reg 0x%02x size %d\n",
50 (write == 0 ? "read" : "write"), reg, len);
51 print_hex_dump_bytes("horus3a: I2C data: ",
52 DUMP_PREFIX_OFFSET, data, len);
55 static int horus3a_write_regs(struct horus3a_priv *priv,
56 u8 reg, const u8 *data, u32 len)
59 u8 buf[MAX_WRITE_REGSIZE + 1];
60 struct i2c_msg msg[1] = {
62 .addr = priv->i2c_address,
69 if (len + 1 > sizeof(buf)) {
70 dev_warn(&priv->i2c->dev,"wr reg=%04x: len=%d is too big!\n",
75 horus3a_i2c_debug(priv, reg, 1, data, len);
77 memcpy(&buf[1], data, len);
78 ret = i2c_transfer(priv->i2c, msg, 1);
79 if (ret >= 0 && ret != 1)
82 dev_warn(&priv->i2c->dev,
83 "%s: i2c wr failed=%d reg=%02x len=%d\n",
84 KBUILD_MODNAME, ret, reg, len);
90 static int horus3a_write_reg(struct horus3a_priv *priv, u8 reg, u8 val)
92 return horus3a_write_regs(priv, reg, &val, 1);
95 static int horus3a_enter_power_save(struct horus3a_priv *priv)
99 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
100 if (priv->state == STATE_SLEEP)
102 /* IQ Generator disable */
103 horus3a_write_reg(priv, 0x2a, 0x79);
105 horus3a_write_reg(priv, 0x29, 0x70);
106 /* VCO disable preparation */
107 horus3a_write_reg(priv, 0x28, 0x3e);
108 /* VCO buffer disable */
109 horus3a_write_reg(priv, 0x2a, 0x19);
110 /* VCO calibration disable */
111 horus3a_write_reg(priv, 0x1c, 0x00);
112 /* Power save setting (xtal is not stopped) */
114 /* LNA is Disabled */
117 horus3a_write_regs(priv, 0x11, data, sizeof(data));
118 priv->state = STATE_SLEEP;
122 static int horus3a_leave_power_save(struct horus3a_priv *priv)
126 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
127 if (priv->state == STATE_ACTIVE)
129 /* Leave power save */
131 /* LNA is Disabled */
134 horus3a_write_regs(priv, 0x11, data, sizeof(data));
135 /* VCO buffer enable */
136 horus3a_write_reg(priv, 0x2a, 0x79);
137 /* VCO calibration enable */
138 horus3a_write_reg(priv, 0x1c, 0xc0);
140 horus3a_write_reg(priv, 0x29, 0x71);
141 usleep_range(5000, 7000);
142 priv->state = STATE_ACTIVE;
146 static int horus3a_init(struct dvb_frontend *fe)
148 struct horus3a_priv *priv = fe->tuner_priv;
150 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
154 static void horus3a_release(struct dvb_frontend *fe)
156 struct horus3a_priv *priv = fe->tuner_priv;
158 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
159 kfree(fe->tuner_priv);
160 fe->tuner_priv = NULL;
163 static int horus3a_sleep(struct dvb_frontend *fe)
165 struct horus3a_priv *priv = fe->tuner_priv;
167 dev_dbg(&priv->i2c->dev, "%s()\n", __func__);
168 horus3a_enter_power_save(priv);
172 static int horus3a_set_params(struct dvb_frontend *fe)
174 struct dtv_frontend_properties *p = &fe->dtv_property_cache;
175 struct horus3a_priv *priv = fe->tuner_priv;
176 u32 frequency = p->frequency;
177 u32 symbol_rate = p->symbol_rate/1000;
186 dev_dbg(&priv->i2c->dev, "%s(): frequency %dkHz symbol_rate %dksps\n",
187 __func__, frequency, symbol_rate);
189 priv->set_tuner(priv->set_tuner_data, 0);
190 if (priv->state == STATE_SLEEP)
191 horus3a_leave_power_save(priv);
193 /* frequency should be X MHz (X : integer) */
194 frequency = DIV_ROUND_CLOSEST(frequency, 1000) * 1000;
195 if (frequency <= 1155000) {
202 /* Assumed that fREF == 1MHz (1000kHz) */
203 ms = DIV_ROUND_CLOSEST((frequency * mixdiv) / 2, 1000);
204 if (ms > 0x7FFF) { /* 15 bit */
205 dev_err(&priv->i2c->dev, "horus3a: invalid frequency %d\n",
209 if (frequency < 975000) {
210 /* F_CTL=11100 G_CTL=001 */
213 } else if (frequency < 1050000) {
214 /* F_CTL=11000 G_CTL=010 */
217 } else if (frequency < 1150000) {
218 /* F_CTL=10100 G_CTL=010 */
221 } else if (frequency < 1250000) {
222 /* F_CTL=10000 G_CTL=011 */
225 } else if (frequency < 1350000) {
226 /* F_CTL=01100 G_CTL=100 */
229 } else if (frequency < 1450000) {
230 /* F_CTL=01010 G_CTL=100 */
233 } else if (frequency < 1600000) {
234 /* F_CTL=00111 G_CTL=101 */
237 } else if (frequency < 1800000) {
238 /* F_CTL=00100 G_CTL=010 */
241 } else if (frequency < 2000000) {
242 /* F_CTL=00010 G_CTL=001 */
246 /* F_CTL=00000 G_CTL=000 */
250 /* LPF cutoff frequency setting */
251 if (p->delivery_system == SYS_DVBS) {
257 * fc_lpf = SR * (1 + rolloff) / 2 + SR / 2 =
258 * SR * 1.175 = SR * (47/40)
260 * fc_lpf = SR * (1 + rolloff) / 2 + 5 =
261 * SR * 0.675 + 5 = SR * (27/40) + 5
262 * NOTE: The result should be round up.
264 if (symbol_rate <= 4300)
266 else if (symbol_rate <= 10000)
267 fc_lpf = (u8)DIV_ROUND_UP(symbol_rate * 47, 40000);
269 fc_lpf = (u8)DIV_ROUND_UP(symbol_rate * 27, 40000) + 5;
270 /* 5 <= fc_lpf <= 36 */
273 } else if (p->delivery_system == SYS_DVBS2) {
278 * fc_lpf = SR * (1 + rolloff) / 2 + SR / 2
280 * fc_lpf = SR * (1 + rolloff) / 2 + 5
281 * NOTE: The result should be round up.
283 if (symbol_rate <= 4500)
285 else if (symbol_rate <= 10000)
286 fc_lpf = (u8)((symbol_rate * 11 + (10000-1)) / 10000);
288 fc_lpf = (u8)((symbol_rate * 3 + (5000-1)) / 5000 + 5);
289 /* 5 <= fc_lpf <= 36 is valid */
293 dev_err(&priv->i2c->dev,
294 "horus3a: invalid delivery system %d\n",
299 data[0] = (u8)((ms >> 7) & 0xFF);
300 data[1] = (u8)((ms << 1) & 0xFF);
303 data[4] = (u8)(mdiv << 7);
304 horus3a_write_regs(priv, 0x00, data, sizeof(data));
305 /* Write G_CTL, F_CTL */
306 horus3a_write_reg(priv, 0x09, (u8)((g_ctl << 5) | f_ctl));
307 /* Write LPF cutoff frequency */
308 horus3a_write_reg(priv, 0x37, (u8)(0x80 | (fc_lpf << 1)));
309 /* Start Calibration */
310 horus3a_write_reg(priv, 0x05, 0x80);
311 /* IQ Generator enable */
312 horus3a_write_reg(priv, 0x2a, 0x7b);
313 /* tuner stabilization time */
315 /* Store tuned frequency to the struct */
316 priv->frequency = ms * 2 * 1000 / mixdiv;
320 static int horus3a_get_frequency(struct dvb_frontend *fe, u32 *frequency)
322 struct horus3a_priv *priv = fe->tuner_priv;
324 *frequency = priv->frequency;
328 static const struct dvb_tuner_ops horus3a_tuner_ops = {
330 .name = "Sony Horus3a",
331 .frequency_min = 950000,
332 .frequency_max = 2150000,
333 .frequency_step = 1000,
335 .init = horus3a_init,
336 .release = horus3a_release,
337 .sleep = horus3a_sleep,
338 .set_params = horus3a_set_params,
339 .get_frequency = horus3a_get_frequency,
342 struct dvb_frontend *horus3a_attach(struct dvb_frontend *fe,
343 const struct horus3a_config *config,
344 struct i2c_adapter *i2c)
347 struct horus3a_priv *priv = NULL;
349 priv = kzalloc(sizeof(struct horus3a_priv), GFP_KERNEL);
352 priv->i2c_address = (config->i2c_address >> 1);
354 priv->set_tuner_data = config->set_tuner_priv;
355 priv->set_tuner = config->set_tuner_callback;
357 if (fe->ops.i2c_gate_ctrl)
358 fe->ops.i2c_gate_ctrl(fe, 1);
360 /* wait 4ms after power on */
361 usleep_range(4000, 6000);
362 /* IQ Generator disable */
363 horus3a_write_reg(priv, 0x2a, 0x79);
364 /* REF_R = Xtal Frequency */
365 buf[0] = config->xtal_freq_mhz;
366 buf[1] = config->xtal_freq_mhz;
369 horus3a_write_regs(priv, 0x6, buf, 3);
370 /* IQ Out = Single Ended */
371 horus3a_write_reg(priv, 0x0a, 0x40);
372 switch (config->xtal_freq_mhz) {
384 dev_warn(&priv->i2c->dev,
385 "horus3a: invalid xtal frequency %dMHz\n",
386 config->xtal_freq_mhz);
390 horus3a_write_reg(priv, 0x0e, val);
391 horus3a_enter_power_save(priv);
392 usleep_range(3000, 5000);
394 if (fe->ops.i2c_gate_ctrl)
395 fe->ops.i2c_gate_ctrl(fe, 0);
397 memcpy(&fe->ops.tuner_ops, &horus3a_tuner_ops,
398 sizeof(struct dvb_tuner_ops));
399 fe->tuner_priv = priv;
400 dev_info(&priv->i2c->dev,
401 "Sony HORUS3A attached on addr=%x at I2C adapter %p\n",
402 priv->i2c_address, priv->i2c);
405 EXPORT_SYMBOL(horus3a_attach);
407 MODULE_DESCRIPTION("Sony HORUS3A sattelite tuner driver");
408 MODULE_AUTHOR("Sergey Kozlov <serjk@netup.ru>");
409 MODULE_LICENSE("GPL");