blob: b4ea34c2d09fd209fd30f6308810f70f61a6df14 [file] [log] [blame]
Antti Palosaaria15c7b42009-11-13 22:33:45 -03001/*
2 * E3C EC100 demodulator driver
3 *
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 *
20 */
21
22#include "dvb_frontend.h"
Antti Palosaaria15c7b42009-11-13 22:33:45 -030023#include "ec100.h"
24
Antti Palosaaria15c7b42009-11-13 22:33:45 -030025struct ec100_state {
26 struct i2c_adapter *i2c;
27 struct dvb_frontend frontend;
28 struct ec100_config config;
29
30 u16 ber;
31};
32
33/* write single register */
34static int ec100_write_reg(struct ec100_state *state, u8 reg, u8 val)
35{
36 u8 buf[2] = {reg, val};
37 struct i2c_msg msg = {
38 .addr = state->config.demod_address,
39 .flags = 0,
40 .len = 2,
41 .buf = buf};
42
43 if (i2c_transfer(state->i2c, &msg, 1) != 1) {
Antti Palosaari20399b32012-09-12 20:23:44 -030044 dev_warn(&state->i2c->dev, "%s: i2c wr failed reg=%02x\n",
45 KBUILD_MODNAME, reg);
Antti Palosaaria15c7b42009-11-13 22:33:45 -030046 return -EREMOTEIO;
47 }
48 return 0;
49}
50
51/* read single register */
52static int ec100_read_reg(struct ec100_state *state, u8 reg, u8 *val)
53{
54 struct i2c_msg msg[2] = {
55 {
56 .addr = state->config.demod_address,
57 .flags = 0,
58 .len = 1,
59 .buf = &reg
60 }, {
61 .addr = state->config.demod_address,
62 .flags = I2C_M_RD,
63 .len = 1,
64 .buf = val
65 }
66 };
67
68 if (i2c_transfer(state->i2c, msg, 2) != 2) {
Antti Palosaari20399b32012-09-12 20:23:44 -030069 dev_warn(&state->i2c->dev, "%s: i2c rd failed reg=%02x\n",
70 KBUILD_MODNAME, reg);
Antti Palosaaria15c7b42009-11-13 22:33:45 -030071 return -EREMOTEIO;
72 }
73 return 0;
74}
75
Mauro Carvalho Chehab41943ea2011-12-26 10:15:30 -030076static int ec100_set_frontend(struct dvb_frontend *fe)
Antti Palosaaria15c7b42009-11-13 22:33:45 -030077{
Mauro Carvalho Chehab41943ea2011-12-26 10:15:30 -030078 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaaria15c7b42009-11-13 22:33:45 -030079 struct ec100_state *state = fe->demodulator_priv;
80 int ret;
81 u8 tmp, tmp2;
82
Antti Palosaari20399b32012-09-12 20:23:44 -030083 dev_dbg(&state->i2c->dev, "%s: frequency=%d bandwidth_hz=%d\n",
84 __func__, c->frequency, c->bandwidth_hz);
Antti Palosaaria15c7b42009-11-13 22:33:45 -030085
86 /* program tuner */
87 if (fe->ops.tuner_ops.set_params)
Mauro Carvalho Chehab14d24d12011-12-24 12:24:33 -030088 fe->ops.tuner_ops.set_params(fe);
Antti Palosaaria15c7b42009-11-13 22:33:45 -030089
90 ret = ec100_write_reg(state, 0x04, 0x06);
91 if (ret)
92 goto error;
93 ret = ec100_write_reg(state, 0x67, 0x58);
94 if (ret)
95 goto error;
96 ret = ec100_write_reg(state, 0x05, 0x18);
97 if (ret)
98 goto error;
99
100 /* reg/bw | 6 | 7 | 8
101 -------+------+------+------
102 A 0x1b | 0xa1 | 0xe7 | 0x2c
103 A 0x1c | 0x55 | 0x63 | 0x72
104 -------+------+------+------
105 B 0x1b | 0xb7 | 0x00 | 0x49
106 B 0x1c | 0x55 | 0x64 | 0x72 */
107
Mauro Carvalho Chehab41943ea2011-12-26 10:15:30 -0300108 switch (c->bandwidth_hz) {
109 case 6000000:
Antti Palosaaria15c7b42009-11-13 22:33:45 -0300110 tmp = 0xb7;
111 tmp2 = 0x55;
112 break;
Mauro Carvalho Chehab41943ea2011-12-26 10:15:30 -0300113 case 7000000:
Antti Palosaaria15c7b42009-11-13 22:33:45 -0300114 tmp = 0x00;
115 tmp2 = 0x64;
116 break;
Mauro Carvalho Chehab41943ea2011-12-26 10:15:30 -0300117 case 8000000:
Antti Palosaaria15c7b42009-11-13 22:33:45 -0300118 default:
119 tmp = 0x49;
120 tmp2 = 0x72;
121 }
122
123 ret = ec100_write_reg(state, 0x1b, tmp);
124 if (ret)
125 goto error;
126 ret = ec100_write_reg(state, 0x1c, tmp2);
127 if (ret)
128 goto error;
129
130 ret = ec100_write_reg(state, 0x0c, 0xbb); /* if freq */
131 if (ret)
132 goto error;
133 ret = ec100_write_reg(state, 0x0d, 0x31); /* if freq */
134 if (ret)
135 goto error;
136
137 ret = ec100_write_reg(state, 0x08, 0x24);
138 if (ret)
139 goto error;
140
141 ret = ec100_write_reg(state, 0x00, 0x00); /* go */
142 if (ret)
143 goto error;
144 ret = ec100_write_reg(state, 0x00, 0x20); /* go */
145 if (ret)
146 goto error;
147
148 return ret;
149error:
Antti Palosaari20399b32012-09-12 20:23:44 -0300150 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaria15c7b42009-11-13 22:33:45 -0300151 return ret;
152}
153
154static int ec100_get_tune_settings(struct dvb_frontend *fe,
155 struct dvb_frontend_tune_settings *fesettings)
156{
157 fesettings->min_delay_ms = 300;
158 fesettings->step_size = 0;
159 fesettings->max_drift = 0;
160
161 return 0;
162}
163
164static int ec100_read_status(struct dvb_frontend *fe, fe_status_t *status)
165{
166 struct ec100_state *state = fe->demodulator_priv;
167 int ret;
168 u8 tmp;
169 *status = 0;
170
171 ret = ec100_read_reg(state, 0x42, &tmp);
172 if (ret)
173 goto error;
174
175 if (tmp & 0x80) {
176 /* bit7 set - have lock */
177 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER | FE_HAS_VITERBI |
178 FE_HAS_SYNC | FE_HAS_LOCK;
179 } else {
180 ret = ec100_read_reg(state, 0x01, &tmp);
181 if (ret)
182 goto error;
183
184 if (tmp & 0x10) {
185 /* bit4 set - have signal */
186 *status |= FE_HAS_SIGNAL;
187 if (!(tmp & 0x01)) {
188 /* bit0 clear - have ~valid signal */
189 *status |= FE_HAS_CARRIER | FE_HAS_VITERBI;
190 }
191 }
192 }
193
194 return ret;
195error:
Antti Palosaari20399b32012-09-12 20:23:44 -0300196 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaria15c7b42009-11-13 22:33:45 -0300197 return ret;
198}
199
200static int ec100_read_ber(struct dvb_frontend *fe, u32 *ber)
201{
202 struct ec100_state *state = fe->demodulator_priv;
203 int ret;
204 u8 tmp, tmp2;
205 u16 ber2;
206
207 *ber = 0;
208
209 ret = ec100_read_reg(state, 0x65, &tmp);
210 if (ret)
211 goto error;
212 ret = ec100_read_reg(state, 0x66, &tmp2);
213 if (ret)
214 goto error;
215
216 ber2 = (tmp2 << 8) | tmp;
217
218 /* if counter overflow or clear */
219 if (ber2 < state->ber)
220 *ber = ber2;
221 else
222 *ber = ber2 - state->ber;
223
224 state->ber = ber2;
225
226 return ret;
227error:
Antti Palosaari20399b32012-09-12 20:23:44 -0300228 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaria15c7b42009-11-13 22:33:45 -0300229 return ret;
230}
231
232static int ec100_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
233{
234 struct ec100_state *state = fe->demodulator_priv;
235 int ret;
236 u8 tmp;
237
238 ret = ec100_read_reg(state, 0x24, &tmp);
239 if (ret) {
240 *strength = 0;
241 goto error;
242 }
243
244 *strength = ((tmp << 8) | tmp);
245
246 return ret;
247error:
Antti Palosaari20399b32012-09-12 20:23:44 -0300248 dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
Antti Palosaaria15c7b42009-11-13 22:33:45 -0300249 return ret;
250}
251
252static int ec100_read_snr(struct dvb_frontend *fe, u16 *snr)
253{
254 *snr = 0;
255 return 0;
256}
257
258static int ec100_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
259{
260 *ucblocks = 0;
261 return 0;
262}
263
264static void ec100_release(struct dvb_frontend *fe)
265{
266 struct ec100_state *state = fe->demodulator_priv;
267 kfree(state);
268}
269
270static struct dvb_frontend_ops ec100_ops;
271
272struct dvb_frontend *ec100_attach(const struct ec100_config *config,
273 struct i2c_adapter *i2c)
274{
275 int ret;
276 struct ec100_state *state = NULL;
277 u8 tmp;
278
279 /* allocate memory for the internal state */
280 state = kzalloc(sizeof(struct ec100_state), GFP_KERNEL);
281 if (state == NULL)
282 goto error;
283
284 /* setup the state */
285 state->i2c = i2c;
286 memcpy(&state->config, config, sizeof(struct ec100_config));
287
288 /* check if the demod is there */
289 ret = ec100_read_reg(state, 0x33, &tmp);
290 if (ret || tmp != 0x0b)
291 goto error;
292
293 /* create dvb_frontend */
294 memcpy(&state->frontend.ops, &ec100_ops,
295 sizeof(struct dvb_frontend_ops));
296 state->frontend.demodulator_priv = state;
297
298 return &state->frontend;
299error:
300 kfree(state);
301 return NULL;
302}
303EXPORT_SYMBOL(ec100_attach);
304
305static struct dvb_frontend_ops ec100_ops = {
Mauro Carvalho Chehab41943ea2011-12-26 10:15:30 -0300306 .delsys = { SYS_DVBT },
Antti Palosaaria15c7b42009-11-13 22:33:45 -0300307 .info = {
308 .name = "E3C EC100 DVB-T",
Antti Palosaaria15c7b42009-11-13 22:33:45 -0300309 .caps =
310 FE_CAN_FEC_1_2 | FE_CAN_FEC_2_3 | FE_CAN_FEC_3_4 |
311 FE_CAN_FEC_5_6 | FE_CAN_FEC_7_8 | FE_CAN_FEC_AUTO |
312 FE_CAN_QPSK | FE_CAN_QAM_16 |
313 FE_CAN_QAM_64 | FE_CAN_QAM_AUTO |
314 FE_CAN_TRANSMISSION_MODE_AUTO |
315 FE_CAN_GUARD_INTERVAL_AUTO |
316 FE_CAN_HIERARCHY_AUTO |
317 FE_CAN_MUTE_TS
318 },
319
320 .release = ec100_release,
Mauro Carvalho Chehab41943ea2011-12-26 10:15:30 -0300321 .set_frontend = ec100_set_frontend,
Antti Palosaaria15c7b42009-11-13 22:33:45 -0300322 .get_tune_settings = ec100_get_tune_settings,
323 .read_status = ec100_read_status,
324 .read_ber = ec100_read_ber,
325 .read_signal_strength = ec100_read_signal_strength,
326 .read_snr = ec100_read_snr,
327 .read_ucblocks = ec100_read_ucblocks,
328};
329
330MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
331MODULE_DESCRIPTION("E3C EC100 DVB-T demodulator driver");
332MODULE_LICENSE("GPL");