-
Notifications
You must be signed in to change notification settings - Fork 4
/
mn88436.c
380 lines (309 loc) · 8.19 KB
/
mn88436.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/*
* Driver for the Panasonic MN88436 ATSC demodulator
*
* Copyright (C) 2014 Sasa Savic <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/firmware.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/i2c.h>
#include <linux/version.h>
#include <linux/dvb/frontend.h>
#include "media/dvb_frontend.h"
#include "mn88436.h"
struct mn88436_state {
struct dvb_frontend frontend;
struct i2c_adapter *i2c;
enum fe_modulation current_modulation;
u32 current_frequency;
u8 mn88436_bank[DMD_REG_BANK];
bool boot;
};
static int mn88436_write_reg(struct mn88436_state *state, u8 id, u8 reg, u8 val)
{
int ret;
u8 buf[] = { reg, val };
struct i2c_msg msg = { .addr = state->mn88436_bank[id],
.flags = 0,
.buf = buf,
.len = 2
};
ret = i2c_transfer(state->i2c, &msg, 1);
if (ret == 1) {
ret = 0;
} else {
dev_warn(&state->i2c->dev, "i2c wr failed=%d reg=%02x "
, ret, reg);
ret = -EREMOTEIO;
}
return ret;
}
static int mn88436_read_reg(struct mn88436_state *state, u8 id, u8 reg, u8 *val)
{
int ret;
u8 buf[] = { reg };
struct i2c_msg msg[] = {
{ .addr = state->mn88436_bank[id],
.flags = 0,
.buf = buf,
.len = 1
},
{ .addr = state->mn88436_bank[id],
.flags = I2C_M_RD,
.buf = val,
.len = 1
},
};
ret = i2c_transfer(state->i2c, msg, 2);
if (ret == 2) {
ret = 0;
} else {
dev_warn(&state->i2c->dev, "i2c rd failed=%d reg=%02x "
, ret, reg);
ret = -EREMOTEIO;
}
return ret;
}
static int mn88436_write_reg_mask(struct mn88436_state *state, u8 id,
u8 reg , u8 mask , u8 data)
{
int ret;
u8 rd;
ret = mn88436_read_reg(state, id, reg, &rd);
if (ret)
goto err;
rd |= mask & data;
rd &= (mask ^ 0xff) | data;
ret = mn88436_write_reg(state, id, reg, rd);
if (ret)
goto err;
return 0;
err:
dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
return ret;
}
static int mn88436_read_status(struct dvb_frontend* fe, enum fe_status* status)
{
struct mn88436_state* state = fe->demodulator_priv;
int ret;
u8 locked;
*status = 0;
ret = mn88436_read_reg(state, 0, DMD_MAIN_STSMON1, &locked);
if (ret)
goto err;
if (locked & 1)
*status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
return 0;
err:
dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
return ret;
}
static int mn88436_set_frontend(struct dvb_frontend* fe)
{
struct dtv_frontend_properties *p = &fe->dtv_property_cache;
struct mn88436_state* state = fe->demodulator_priv;
int cnt = 50, ret;
u8 locked;
if (!state->boot) {
ret = -EAGAIN;
goto err;
}
if (fe->ops.tuner_ops.set_params) {
ret = fe->ops.tuner_ops.set_params(fe);
if (ret)
goto err;
}
ret = mn88436_write_reg(state, 0, DMD_MAIN_RSTSET1, 0x77);
if (ret)
goto err;
do {
ret = mn88436_read_reg(state, 0, DMD_MAIN_STSMON1, &locked);
if (!ret && (locked & 1))
break;
msleep(10);
} while (--cnt);
if (!cnt) {
ret = -EAGAIN;
goto err;
}
dev_dbg(&state->i2c->dev, "Service locked!!!\n");
state->current_frequency = p->frequency;
state->current_modulation = p->modulation;
return 0;
err:
dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
return ret;
}
static int mn88436_get_frontend(struct dvb_frontend *fe, struct dtv_frontend_properties *p)
{
// struct dtv_frontend_properties *p = &fe->dtv_property_cache;
struct mn88436_state *state = fe->demodulator_priv;
p->modulation = state->current_modulation;
p->frequency = state->current_frequency;
return 0;
}
static int mn88436_init(struct dvb_frontend* fe)
{
struct mn88436_state* state = fe->demodulator_priv;
const struct firmware *fw = NULL;
int ret, i;
u8 d;
if (state->boot)
return 0;
dev_dbg(&state->i2c->dev, "Uploading demod firmware (%s)...\n", MN88436_DEMOD_ATSC);
ret = request_firmware(&fw, MN88436_DEMOD_ATSC, &state->i2c->dev);
if (ret) {
dev_dbg(&state->i2c->dev, "Firmware upload failed. Timeout or file not found\n");
goto err1;
}
for (i = 0;;) {
if (fw->data[i] == 0xff)
break;
ret = mn88436_write_reg(state, fw->data[i], fw->data[i + 1], fw->data[i + 2]);
if (ret)
goto err2;
i = i + 3;
}
release_firmware(fw);
fw = NULL;
dev_dbg(&state->i2c->dev, "Uploading demod pseq (%s)...\n", MN88436_DEMOD_PSEQ);
ret = request_firmware(&fw, MN88436_DEMOD_PSEQ, &state->i2c->dev);
if (ret) {
dev_dbg(&state->i2c->dev, "Pseq upload failed. Timeout or file not found\n");
goto err1;
}
/* Load PSEQ Program */
ret = mn88436_write_reg(state, 0, DMD_MAIN_PSEQSET , 0x03);
if (ret)
goto err2;
for (i = 0; i < fw->size; i++) {
ret = mn88436_write_reg(state, 0, DMD_MAIN_PSEQPRG , fw->data[i]);
if (ret)
goto err2;
}
release_firmware(fw);
fw = NULL;
/* Check Parity bit */
ret = mn88436_read_reg(state, 0, DMD_MAIN_PSEQSET , &d);
if (ret)
goto err1;
if (d & 0x20) {
ret = -EAGAIN;
goto err1;
}
ret = mn88436_write_reg(state, 0, DMD_MAIN_PSEQSET , 0x00);
if (ret)
goto err1;
/* TS parallel (Fixed clock mode) */
ret = mn88436_write_reg(state, 0, DMD_MAIN_CPOSET2, 0xc1);
if (ret)
goto err1;
ret = mn88436_write_reg(state, 0, DMD_MAIN_GPSET1, 0xff);
if (ret)
goto err1;
/* Set TCB Through Mode */
ret = mn88436_write_reg_mask(state, 0, DMD_MAIN_TCBSET, 0x7f, 0x53);
if (ret)
goto err1;
ret = mn88436_write_reg(state, 0, DMD_MAIN_TCBADR, 0x00);
if (ret)
goto err1;
ret = mn88436_write_reg(state, 0, DMD_MAIN_VEQSET2, 0x80);
if (ret)
goto err1;
state->boot = true;
return 0;
err2:
release_firmware(fw);
fw = NULL;
err1:
dev_dbg(&state->i2c->dev, "%s: failed=%d\n", __func__, ret);
return ret;
}
static void mn88436_release(struct dvb_frontend* fe)
{
struct mn88436_state* state = fe->demodulator_priv;
kfree(state);
}
static struct dvb_frontend_ops mn88436_ops = {
.delsys = { SYS_ATSC },
.info = {
.name = "Panasonic MN88436",
.frequency_min_hz = 51 * MHz,
.frequency_max_hz = 858 * MHz,
.caps = FE_CAN_8VSB
},
.init = mn88436_init,
.release = mn88436_release,
.set_frontend = mn88436_set_frontend,
.get_frontend = mn88436_get_frontend,
.read_status = mn88436_read_status,
};
struct dvb_frontend *mn88436_attach(struct i2c_adapter *i2c,
u8 device_id)
{
struct mn88436_state *state = NULL;
int ret;
state = kzalloc(sizeof(struct mn88436_state), GFP_KERNEL);
if (!state) {
ret = -ENOMEM;
dev_err(&i2c->dev, "kzalloc() failed\n");
goto err1;
}
state->i2c = i2c;
switch (device_id) {
case 0:
default:
state->mn88436_bank[0] = 0x18;
state->mn88436_bank[1] = 0x10;
break;
case 1:
state->mn88436_bank[0] = 0x19;
state->mn88436_bank[1] = 0x11;
break;
case 2:
state->mn88436_bank[0] = 0x1A;
state->mn88436_bank[1] = 0x12;
break;
case 3:
state->mn88436_bank[0] = 0x1B;
state->mn88436_bank[1] = 0x13;
break;
}
/* Try SOFT reset */
ret = mn88436_write_reg(state, 0, DMD_MAIN_RSTSET1, 0x77);
if (ret)
goto err2;
dev_info(&i2c->dev, "MN88436 ATSC successfully attached\n");
memcpy(&state->frontend.ops, &mn88436_ops,
sizeof(struct dvb_frontend_ops));
state->frontend.demodulator_priv = state;
return &state->frontend;
err2:
kfree(state);
err1:
dev_dbg(&i2c->dev, "%s: failed=%d\n", __func__, ret);
return NULL;
}
EXPORT_SYMBOL(mn88436_attach);
MODULE_DESCRIPTION("Panasonic MN88436 ATSC demod driver");
MODULE_AUTHOR("Sasa Savic <[email protected]>");
MODULE_LICENSE("GPL");