blob: 5e8fd63832e95628ecea5d34676733de275f4e20 [file] [log] [blame]
Antti Palosaarica25cb52013-11-29 16:19:50 -03001/*
2 * Panasonic MN88472 DVB-T/T2/C demodulator driver
3 *
4 * Copyright (C) 2013 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
17#include "mn88472_priv.h"
18
Antti Palosaari8e0d8572013-12-02 20:28:58 -030019static int mn88472_get_tune_settings(struct dvb_frontend *fe,
Antti Palosaari8d1f38f2016-05-13 12:19:19 -030020 struct dvb_frontend_tune_settings *s)
Antti Palosaari8e0d8572013-12-02 20:28:58 -030021{
Antti Palosaari8d1f38f2016-05-13 12:19:19 -030022 s->min_delay_ms = 1000;
Antti Palosaari8e0d8572013-12-02 20:28:58 -030023 return 0;
24}
25
Antti Palosaari8d1f38f2016-05-13 12:19:19 -030026static int mn88472_read_status(struct dvb_frontend *fe, enum fe_status *status)
27{
28 struct i2c_client *client = fe->demodulator_priv;
29 struct mn88472_dev *dev = i2c_get_clientdata(client);
30 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaaric635a5e2017-03-17 13:34:44 -030031 int ret, i, stmp;
32 unsigned int utmp, utmp1, utmp2;
33 u8 buf[5];
Antti Palosaari8d1f38f2016-05-13 12:19:19 -030034
35 if (!dev->active) {
36 ret = -EAGAIN;
37 goto err;
38 }
39
40 switch (c->delivery_system) {
41 case SYS_DVBT:
42 ret = regmap_read(dev->regmap[0], 0x7f, &utmp);
43 if (ret)
44 goto err;
45 if ((utmp & 0x0f) >= 0x09)
46 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
47 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
48 else
49 *status = 0;
50 break;
51 case SYS_DVBT2:
52 ret = regmap_read(dev->regmap[2], 0x92, &utmp);
53 if (ret)
54 goto err;
55 if ((utmp & 0x0f) >= 0x0d)
56 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
57 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
58 else if ((utmp & 0x0f) >= 0x0a)
59 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
60 FE_HAS_VITERBI;
61 else if ((utmp & 0x0f) >= 0x07)
62 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER;
63 else
64 *status = 0;
65 break;
66 case SYS_DVBC_ANNEX_A:
67 ret = regmap_read(dev->regmap[1], 0x84, &utmp);
68 if (ret)
69 goto err;
70 if ((utmp & 0x0f) >= 0x08)
71 *status = FE_HAS_SIGNAL | FE_HAS_CARRIER |
72 FE_HAS_VITERBI | FE_HAS_SYNC | FE_HAS_LOCK;
73 else
74 *status = 0;
75 break;
76 default:
77 ret = -EINVAL;
78 goto err;
79 }
80
Antti Palosaari61d7c6a2017-03-17 12:43:06 -030081 /* Signal strength */
82 if (*status & FE_HAS_SIGNAL) {
83 for (i = 0; i < 2; i++) {
84 ret = regmap_bulk_read(dev->regmap[2], 0x8e + i,
85 &buf[i], 1);
86 if (ret)
87 goto err;
88 }
89
90 utmp1 = buf[0] << 8 | buf[1] << 0 | buf[0] >> 2;
91 dev_dbg(&client->dev, "strength=%u\n", utmp1);
92
93 c->strength.stat[0].scale = FE_SCALE_RELATIVE;
94 c->strength.stat[0].uvalue = utmp1;
95 } else {
96 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
97 }
98
Antti Palosaaric635a5e2017-03-17 13:34:44 -030099 /* CNR */
100 if (*status & FE_HAS_VITERBI && c->delivery_system == SYS_DVBT) {
101 /* DVB-T CNR */
102 ret = regmap_bulk_read(dev->regmap[0], 0x9c, buf, 2);
103 if (ret)
104 goto err;
105
106 utmp = buf[0] << 8 | buf[1] << 0;
107 if (utmp) {
108 /* CNR[dB]: 10 * log10(65536 / value) + 2 */
109 /* log10(65536) = 80807124, 0.2 = 3355443 */
110 stmp = ((u64)80807124 - intlog10(utmp) + 3355443)
111 * 10000 >> 24;
112
113 dev_dbg(&client->dev, "cnr=%d value=%u\n", stmp, utmp);
114 } else {
115 stmp = 0;
116 }
117
118 c->cnr.stat[0].svalue = stmp;
119 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
120 } else if (*status & FE_HAS_VITERBI &&
121 c->delivery_system == SYS_DVBT2) {
122 /* DVB-T2 CNR */
123 for (i = 0; i < 3; i++) {
124 ret = regmap_bulk_read(dev->regmap[2], 0xbc + i,
125 &buf[i], 1);
126 if (ret)
127 goto err;
128 }
129
130 utmp = buf[1] << 8 | buf[2] << 0;
131 utmp1 = (buf[0] >> 2) & 0x01; /* 0=SISO, 1=MISO */
132 if (utmp) {
133 if (utmp1) {
134 /* CNR[dB]: 10 * log10(16384 / value) - 6 */
135 /* log10(16384) = 70706234, 0.6 = 10066330 */
136 stmp = ((u64)70706234 - intlog10(utmp)
137 - 10066330) * 10000 >> 24;
138 dev_dbg(&client->dev, "cnr=%d value=%u MISO\n",
139 stmp, utmp);
140 } else {
141 /* CNR[dB]: 10 * log10(65536 / value) + 2 */
142 /* log10(65536) = 80807124, 0.2 = 3355443 */
143 stmp = ((u64)80807124 - intlog10(utmp)
144 + 3355443) * 10000 >> 24;
145
146 dev_dbg(&client->dev, "cnr=%d value=%u SISO\n",
147 stmp, utmp);
148 }
149 } else {
150 stmp = 0;
151 }
152
153 c->cnr.stat[0].svalue = stmp;
154 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
155 } else if (*status & FE_HAS_VITERBI &&
156 c->delivery_system == SYS_DVBC_ANNEX_A) {
157 /* DVB-C CNR */
158 ret = regmap_bulk_read(dev->regmap[1], 0xa1, buf, 4);
159 if (ret)
160 goto err;
161
162 utmp1 = buf[0] << 8 | buf[1] << 0; /* signal */
163 utmp2 = buf[2] << 8 | buf[3] << 0; /* noise */
164 if (utmp1 && utmp2) {
165 /* CNR[dB]: 10 * log10(8 * (signal / noise)) */
166 /* log10(8) = 15151336 */
167 stmp = ((u64)15151336 + intlog10(utmp1)
168 - intlog10(utmp2)) * 10000 >> 24;
169
170 dev_dbg(&client->dev, "cnr=%d signal=%u noise=%u\n",
171 stmp, utmp1, utmp2);
172 } else {
173 stmp = 0;
174 }
175
176 c->cnr.stat[0].svalue = stmp;
177 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
178 } else {
179 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
180 }
181
Antti Palosaari0fc66c12017-03-17 13:45:48 -0300182 /* PER */
183 if (*status & FE_HAS_SYNC) {
184 ret = regmap_bulk_read(dev->regmap[0], 0xe1, buf, 4);
185 if (ret)
186 goto err;
187
188 utmp1 = buf[0] << 8 | buf[1] << 0;
189 utmp2 = buf[2] << 8 | buf[3] << 0;
190 dev_dbg(&client->dev, "block_error=%u block_count=%u\n",
191 utmp1, utmp2);
192
193 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
194 c->block_error.stat[0].uvalue += utmp1;
195 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
196 c->block_count.stat[0].uvalue += utmp2;
197 } else {
198 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
199 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
200 }
201
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300202 return 0;
203err:
204 dev_dbg(&client->dev, "failed=%d\n", ret);
205 return ret;
206}
207
Antti Palosaari5ef1ad32014-10-25 22:23:09 -0300208static int mn88472_set_frontend(struct dvb_frontend *fe)
Antti Palosaarica25cb52013-11-29 16:19:50 -0300209{
Antti Palosaari528af192014-10-26 23:01:07 -0300210 struct i2c_client *client = fe->demodulator_priv;
211 struct mn88472_dev *dev = i2c_get_clientdata(client);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300212 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari50489072014-10-27 20:39:40 -0300213 int ret, i;
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300214 unsigned int utmp;
215 u32 if_frequency;
216 u8 buf[3], delivery_system_val, bandwidth_val, *bandwidth_vals_ptr;
217 u8 reg_bank0_b4_val, reg_bank0_cd_val, reg_bank0_d4_val;
218 u8 reg_bank0_d6_val;
Antti Palosaari528af192014-10-26 23:01:07 -0300219
220 dev_dbg(&client->dev,
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300221 "delivery_system=%u modulation=%u frequency=%u bandwidth_hz=%u symbol_rate=%u inversion=%d stream_id=%d\n",
222 c->delivery_system, c->modulation, c->frequency,
223 c->bandwidth_hz, c->symbol_rate, c->inversion, c->stream_id);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300224
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300225 if (!dev->active) {
Antti Palosaarica25cb52013-11-29 16:19:50 -0300226 ret = -EAGAIN;
227 goto err;
228 }
229
Antti Palosaari50489072014-10-27 20:39:40 -0300230 switch (c->delivery_system) {
231 case SYS_DVBT:
232 delivery_system_val = 0x02;
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300233 reg_bank0_b4_val = 0x00;
234 reg_bank0_cd_val = 0x1f;
235 reg_bank0_d4_val = 0x0a;
236 reg_bank0_d6_val = 0x48;
Antti Palosaari50489072014-10-27 20:39:40 -0300237 break;
238 case SYS_DVBT2:
239 delivery_system_val = 0x03;
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300240 reg_bank0_b4_val = 0xf6;
241 reg_bank0_cd_val = 0x01;
242 reg_bank0_d4_val = 0x09;
243 reg_bank0_d6_val = 0x46;
Antti Palosaari50489072014-10-27 20:39:40 -0300244 break;
245 case SYS_DVBC_ANNEX_A:
246 delivery_system_val = 0x04;
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300247 reg_bank0_b4_val = 0x00;
248 reg_bank0_cd_val = 0x17;
249 reg_bank0_d4_val = 0x09;
250 reg_bank0_d6_val = 0x48;
Antti Palosaari50489072014-10-27 20:39:40 -0300251 break;
252 default:
253 ret = -EINVAL;
254 goto err;
255 }
256
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300257 switch (c->delivery_system) {
258 case SYS_DVBT:
259 case SYS_DVBT2:
260 switch (c->bandwidth_hz) {
261 case 5000000:
262 bandwidth_vals_ptr = "\xe5\x99\x9a\x1b\xa9\x1b\xa9";
263 bandwidth_val = 0x03;
264 break;
265 case 6000000:
266 bandwidth_vals_ptr = "\xbf\x55\x55\x15\x6b\x15\x6b";
267 bandwidth_val = 0x02;
268 break;
269 case 7000000:
270 bandwidth_vals_ptr = "\xa4\x00\x00\x0f\x2c\x0f\x2c";
271 bandwidth_val = 0x01;
272 break;
273 case 8000000:
274 bandwidth_vals_ptr = "\x8f\x80\x00\x08\xee\x08\xee";
275 bandwidth_val = 0x00;
276 break;
277 default:
278 ret = -EINVAL;
279 goto err;
280 }
281 break;
282 case SYS_DVBC_ANNEX_A:
283 bandwidth_vals_ptr = NULL;
284 bandwidth_val = 0x00;
285 break;
286 default:
287 break;
Antti Palosaari50489072014-10-27 20:39:40 -0300288 }
289
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300290 /* Program tuner */
Antti Palosaarica25cb52013-11-29 16:19:50 -0300291 if (fe->ops.tuner_ops.set_params) {
292 ret = fe->ops.tuner_ops.set_params(fe);
293 if (ret)
294 goto err;
295 }
296
297 if (fe->ops.tuner_ops.get_if_frequency) {
298 ret = fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
299 if (ret)
300 goto err;
301
Antti Palosaari528af192014-10-26 23:01:07 -0300302 dev_dbg(&client->dev, "get_if_frequency=%d\n", if_frequency);
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300303 } else {
304 ret = -EINVAL;
Antti Palosaarica25cb52013-11-29 16:19:50 -0300305 goto err;
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300306 }
Antti Palosaarica25cb52013-11-29 16:19:50 -0300307
Antti Palosaari50489072014-10-27 20:39:40 -0300308 ret = regmap_write(dev->regmap[2], 0x00, 0x66);
309 if (ret)
310 goto err;
311 ret = regmap_write(dev->regmap[2], 0x01, 0x00);
312 if (ret)
313 goto err;
314 ret = regmap_write(dev->regmap[2], 0x02, 0x01);
315 if (ret)
316 goto err;
317 ret = regmap_write(dev->regmap[2], 0x03, delivery_system_val);
318 if (ret)
319 goto err;
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300320 ret = regmap_write(dev->regmap[2], 0x04, bandwidth_val);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300321 if (ret)
322 goto err;
323
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300324 /* IF */
325 utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x1000000, dev->clk);
326 buf[0] = (utmp >> 16) & 0xff;
327 buf[1] = (utmp >> 8) & 0xff;
328 buf[2] = (utmp >> 0) & 0xff;
329 for (i = 0; i < 3; i++) {
330 ret = regmap_write(dev->regmap[2], 0x10 + i, buf[i]);
Antti Palosaari50489072014-10-27 20:39:40 -0300331 if (ret)
332 goto err;
333 }
Antti Palosaarica25cb52013-11-29 16:19:50 -0300334
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300335 /* Bandwidth */
336 if (bandwidth_vals_ptr) {
337 for (i = 0; i < 7; i++) {
338 ret = regmap_write(dev->regmap[2], 0x13 + i,
339 bandwidth_vals_ptr[i]);
340 if (ret)
341 goto err;
342 }
Antti Palosaari50489072014-10-27 20:39:40 -0300343 }
Antti Palosaarica25cb52013-11-29 16:19:50 -0300344
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300345 ret = regmap_write(dev->regmap[0], 0xb4, reg_bank0_b4_val);
346 if (ret)
347 goto err;
348 ret = regmap_write(dev->regmap[0], 0xcd, reg_bank0_cd_val);
349 if (ret)
350 goto err;
351 ret = regmap_write(dev->regmap[0], 0xd4, reg_bank0_d4_val);
352 if (ret)
353 goto err;
354 ret = regmap_write(dev->regmap[0], 0xd6, reg_bank0_d6_val);
355 if (ret)
356 goto err;
357
Antti Palosaari50489072014-10-27 20:39:40 -0300358 switch (c->delivery_system) {
359 case SYS_DVBT:
360 ret = regmap_write(dev->regmap[0], 0x07, 0x26);
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300361 if (ret)
362 goto err;
Antti Palosaari50489072014-10-27 20:39:40 -0300363 ret = regmap_write(dev->regmap[0], 0x00, 0xba);
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300364 if (ret)
365 goto err;
Antti Palosaari50489072014-10-27 20:39:40 -0300366 ret = regmap_write(dev->regmap[0], 0x01, 0x13);
367 if (ret)
368 goto err;
369 break;
370 case SYS_DVBT2:
371 ret = regmap_write(dev->regmap[2], 0x2b, 0x13);
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300372 if (ret)
373 goto err;
Antti Palosaari50489072014-10-27 20:39:40 -0300374 ret = regmap_write(dev->regmap[2], 0x4f, 0x05);
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300375 if (ret)
376 goto err;
Antti Palosaari50489072014-10-27 20:39:40 -0300377 ret = regmap_write(dev->regmap[1], 0xf6, 0x05);
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300378 if (ret)
379 goto err;
Olli Salonen07d45a42017-07-30 08:34:48 -0400380 ret = regmap_write(dev->regmap[2], 0x32,
381 (c->stream_id == NO_STREAM_ID_FILTER) ? 0 :
382 c->stream_id );
Antti Palosaari50489072014-10-27 20:39:40 -0300383 if (ret)
384 goto err;
385 break;
386 case SYS_DVBC_ANNEX_A:
Antti Palosaari50489072014-10-27 20:39:40 -0300387 break;
388 default:
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300389 break;
Antti Palosaari50489072014-10-27 20:39:40 -0300390 }
Antti Palosaarica25cb52013-11-29 16:19:50 -0300391
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300392 /* Reset FSM */
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300393 ret = regmap_write(dev->regmap[2], 0xf8, 0x9f);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300394 if (ret)
395 goto err;
396
Antti Palosaarica25cb52013-11-29 16:19:50 -0300397 return 0;
398err:
Antti Palosaari528af192014-10-26 23:01:07 -0300399 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300400 return ret;
401}
402
Antti Palosaari5ef1ad32014-10-25 22:23:09 -0300403static int mn88472_init(struct dvb_frontend *fe)
Antti Palosaarica25cb52013-11-29 16:19:50 -0300404{
Antti Palosaari528af192014-10-26 23:01:07 -0300405 struct i2c_client *client = fe->demodulator_priv;
406 struct mn88472_dev *dev = i2c_get_clientdata(client);
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300407 int ret, len, rem;
408 unsigned int utmp;
409 const struct firmware *firmware;
410 const char *name = MN88472_FIRMWARE;
Antti Palosaari528af192014-10-26 23:01:07 -0300411
412 dev_dbg(&client->dev, "\n");
Antti Palosaarica25cb52013-11-29 16:19:50 -0300413
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300414 /* Power up */
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300415 ret = regmap_write(dev->regmap[2], 0x05, 0x00);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300416 if (ret)
417 goto err;
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300418 ret = regmap_write(dev->regmap[2], 0x0b, 0x00);
419 if (ret)
420 goto err;
421 ret = regmap_write(dev->regmap[2], 0x0c, 0x00);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300422 if (ret)
423 goto err;
424
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300425 /* Check if firmware is already running */
426 ret = regmap_read(dev->regmap[0], 0xf5, &utmp);
Benjamin Larsson4752eb22015-03-15 19:57:54 -0300427 if (ret)
428 goto err;
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300429 if (!(utmp & 0x01))
430 goto warm;
Benjamin Larsson4752eb22015-03-15 19:57:54 -0300431
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300432 ret = request_firmware(&firmware, name, &client->dev);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300433 if (ret) {
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300434 dev_err(&client->dev, "firmware file '%s' not found\n", name);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300435 goto err;
436 }
437
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300438 dev_info(&client->dev, "downloading firmware from file '%s'\n", name);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300439
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300440 ret = regmap_write(dev->regmap[0], 0xf5, 0x03);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300441 if (ret)
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300442 goto err_release_firmware;
Antti Palosaarica25cb52013-11-29 16:19:50 -0300443
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300444 for (rem = firmware->size; rem > 0; rem -= (dev->i2c_write_max - 1)) {
445 len = min(dev->i2c_write_max - 1, rem);
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300446 ret = regmap_bulk_write(dev->regmap[0], 0xf6,
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300447 &firmware->data[firmware->size - rem],
448 len);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300449 if (ret) {
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300450 dev_err(&client->dev, "firmware download failed %d\n",
451 ret);
452 goto err_release_firmware;
Antti Palosaarica25cb52013-11-29 16:19:50 -0300453 }
454 }
455
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300456 /* Parity check of firmware */
457 ret = regmap_read(dev->regmap[0], 0xf8, &utmp);
458 if (ret)
459 goto err_release_firmware;
460 if (utmp & 0x10) {
461 ret = -EINVAL;
462 dev_err(&client->dev, "firmware did not run\n");
463 goto err_release_firmware;
Benjamin Larsson307e95c2015-03-15 19:57:51 -0300464 }
Benjamin Larsson307e95c2015-03-15 19:57:51 -0300465
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300466 ret = regmap_write(dev->regmap[0], 0xf5, 0x00);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300467 if (ret)
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300468 goto err_release_firmware;
Antti Palosaarica25cb52013-11-29 16:19:50 -0300469
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300470 release_firmware(firmware);
471warm:
472 /* TS config */
473 switch (dev->ts_mode) {
474 case SERIAL_TS_MODE:
475 utmp = 0x1d;
476 break;
477 case PARALLEL_TS_MODE:
478 utmp = 0x00;
479 break;
480 default:
481 ret = -EINVAL;
482 goto err;
483 }
484 ret = regmap_write(dev->regmap[2], 0x08, utmp);
485 if (ret)
486 goto err;
Antti Palosaarica25cb52013-11-29 16:19:50 -0300487
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300488 switch (dev->ts_clk) {
489 case VARIABLE_TS_CLOCK:
490 utmp = 0xe3;
491 break;
492 case FIXED_TS_CLOCK:
493 utmp = 0xe1;
494 break;
495 default:
496 ret = -EINVAL;
497 goto err;
498 }
499 ret = regmap_write(dev->regmap[0], 0xd9, utmp);
500 if (ret)
501 goto err;
502
503 dev->active = true;
Antti Palosaarica25cb52013-11-29 16:19:50 -0300504
505 return 0;
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300506err_release_firmware:
507 release_firmware(firmware);
Markus Elfringbf104c22015-02-01 15:34:37 -0300508err:
Antti Palosaari528af192014-10-26 23:01:07 -0300509 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300510 return ret;
511}
512
Antti Palosaari5ef1ad32014-10-25 22:23:09 -0300513static int mn88472_sleep(struct dvb_frontend *fe)
Antti Palosaarica25cb52013-11-29 16:19:50 -0300514{
Antti Palosaari528af192014-10-26 23:01:07 -0300515 struct i2c_client *client = fe->demodulator_priv;
516 struct mn88472_dev *dev = i2c_get_clientdata(client);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300517 int ret;
Antti Palosaari528af192014-10-26 23:01:07 -0300518
519 dev_dbg(&client->dev, "\n");
Antti Palosaarica25cb52013-11-29 16:19:50 -0300520
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300521 /* Power down */
522 ret = regmap_write(dev->regmap[2], 0x0c, 0x30);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300523 if (ret)
524 goto err;
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300525 ret = regmap_write(dev->regmap[2], 0x0b, 0x30);
526 if (ret)
527 goto err;
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300528 ret = regmap_write(dev->regmap[2], 0x05, 0x3e);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300529 if (ret)
530 goto err;
531
Antti Palosaarica25cb52013-11-29 16:19:50 -0300532 return 0;
533err:
Antti Palosaari528af192014-10-26 23:01:07 -0300534 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarica25cb52013-11-29 16:19:50 -0300535 return ret;
536}
537
Max Kellermannbd336e62016-08-09 18:32:21 -0300538static const struct dvb_frontend_ops mn88472_ops = {
Antti Palosaari50489072014-10-27 20:39:40 -0300539 .delsys = {SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A},
Antti Palosaarica25cb52013-11-29 16:19:50 -0300540 .info = {
541 .name = "Panasonic MN88472",
Antti Palosaarid73ab402015-03-16 19:01:53 -0300542 .symbol_rate_min = 1000000,
543 .symbol_rate_max = 7200000,
Antti Palosaari528af192014-10-26 23:01:07 -0300544 .caps = FE_CAN_FEC_1_2 |
545 FE_CAN_FEC_2_3 |
546 FE_CAN_FEC_3_4 |
547 FE_CAN_FEC_5_6 |
548 FE_CAN_FEC_7_8 |
549 FE_CAN_FEC_AUTO |
550 FE_CAN_QPSK |
551 FE_CAN_QAM_16 |
552 FE_CAN_QAM_32 |
553 FE_CAN_QAM_64 |
554 FE_CAN_QAM_128 |
555 FE_CAN_QAM_256 |
556 FE_CAN_QAM_AUTO |
557 FE_CAN_TRANSMISSION_MODE_AUTO |
558 FE_CAN_GUARD_INTERVAL_AUTO |
559 FE_CAN_HIERARCHY_AUTO |
560 FE_CAN_MUTE_TS |
561 FE_CAN_2G_MODULATION |
Antti Palosaarica25cb52013-11-29 16:19:50 -0300562 FE_CAN_MULTISTREAM
563 },
564
Antti Palosaari8e0d8572013-12-02 20:28:58 -0300565 .get_tune_settings = mn88472_get_tune_settings,
566
Antti Palosaari5ef1ad32014-10-25 22:23:09 -0300567 .init = mn88472_init,
568 .sleep = mn88472_sleep,
Antti Palosaarica25cb52013-11-29 16:19:50 -0300569
Antti Palosaari5ef1ad32014-10-25 22:23:09 -0300570 .set_frontend = mn88472_set_frontend,
Antti Palosaarica25cb52013-11-29 16:19:50 -0300571
Antti Palosaari5ef1ad32014-10-25 22:23:09 -0300572 .read_status = mn88472_read_status,
Antti Palosaarica25cb52013-11-29 16:19:50 -0300573};
574
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300575static struct dvb_frontend *mn88472_get_dvb_frontend(struct i2c_client *client)
Antti Palosaari528af192014-10-26 23:01:07 -0300576{
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300577 struct mn88472_dev *dev = i2c_get_clientdata(client);
578
579 dev_dbg(&client->dev, "\n");
580
581 return &dev->fe;
582}
583
584static int mn88472_probe(struct i2c_client *client,
585 const struct i2c_device_id *id)
586{
587 struct mn88472_config *pdata = client->dev.platform_data;
Antti Palosaari528af192014-10-26 23:01:07 -0300588 struct mn88472_dev *dev;
Antti Palosaari61d7c6a2017-03-17 12:43:06 -0300589 struct dtv_frontend_properties *c;
Antti Palosaari528af192014-10-26 23:01:07 -0300590 int ret;
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300591 unsigned int utmp;
592 static const struct regmap_config regmap_config = {
593 .reg_bits = 8,
594 .val_bits = 8,
595 };
Antti Palosaari528af192014-10-26 23:01:07 -0300596
597 dev_dbg(&client->dev, "\n");
598
Antti Palosaari528af192014-10-26 23:01:07 -0300599 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
Eva Rachel Retuya4a1e81b2016-02-27 20:39:22 +0800600 if (!dev) {
Antti Palosaari528af192014-10-26 23:01:07 -0300601 ret = -ENOMEM;
602 goto err;
603 }
604
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300605 dev->i2c_write_max = pdata->i2c_wr_max ? pdata->i2c_wr_max : ~0;
606 dev->clk = pdata->xtal;
607 dev->ts_mode = pdata->ts_mode;
608 dev->ts_clk = pdata->ts_clock;
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300609 dev->client[0] = client;
610 dev->regmap[0] = regmap_init_i2c(dev->client[0], &regmap_config);
611 if (IS_ERR(dev->regmap[0])) {
612 ret = PTR_ERR(dev->regmap[0]);
613 goto err_kfree;
614 }
Antti Palosaari528af192014-10-26 23:01:07 -0300615
Antti Palosaari528af192014-10-26 23:01:07 -0300616 /*
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300617 * Chip has three I2C addresses for different register banks. Used
Antti Palosaari528af192014-10-26 23:01:07 -0300618 * addresses are 0x18, 0x1a and 0x1c. We register two dummy clients,
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300619 * 0x1a and 0x1c, in order to get own I2C client for each register bank.
620 *
621 * Also, register bank 2 do not support sequential I/O. Only single
622 * register write or read is allowed to that bank.
Antti Palosaari528af192014-10-26 23:01:07 -0300623 */
624 dev->client[1] = i2c_new_dummy(client->adapter, 0x1a);
Eva Rachel Retuya4a1e81b2016-02-27 20:39:22 +0800625 if (!dev->client[1]) {
Antti Palosaari528af192014-10-26 23:01:07 -0300626 ret = -ENODEV;
627 dev_err(&client->dev, "I2C registration failed\n");
628 if (ret)
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300629 goto err_regmap_0_regmap_exit;
630 }
631 dev->regmap[1] = regmap_init_i2c(dev->client[1], &regmap_config);
632 if (IS_ERR(dev->regmap[1])) {
633 ret = PTR_ERR(dev->regmap[1]);
634 goto err_client_1_i2c_unregister_device;
Antti Palosaari528af192014-10-26 23:01:07 -0300635 }
636 i2c_set_clientdata(dev->client[1], dev);
637
638 dev->client[2] = i2c_new_dummy(client->adapter, 0x1c);
Eva Rachel Retuya4a1e81b2016-02-27 20:39:22 +0800639 if (!dev->client[2]) {
Antti Palosaari528af192014-10-26 23:01:07 -0300640 ret = -ENODEV;
641 dev_err(&client->dev, "2nd I2C registration failed\n");
642 if (ret)
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300643 goto err_regmap_1_regmap_exit;
644 }
645 dev->regmap[2] = regmap_init_i2c(dev->client[2], &regmap_config);
646 if (IS_ERR(dev->regmap[2])) {
647 ret = PTR_ERR(dev->regmap[2]);
648 goto err_client_2_i2c_unregister_device;
Antti Palosaari528af192014-10-26 23:01:07 -0300649 }
650 i2c_set_clientdata(dev->client[2], dev);
651
Antti Palosaari365fe4e2016-11-30 22:08:27 -0200652 /* Check demod answers with correct chip id */
653 ret = regmap_read(dev->regmap[2], 0xff, &utmp);
654 if (ret)
655 goto err_regmap_2_regmap_exit;
656
657 dev_dbg(&client->dev, "chip id=%02x\n", utmp);
658
659 if (utmp != 0x02) {
660 ret = -ENODEV;
661 goto err_regmap_2_regmap_exit;
662 }
663
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300664 /* Sleep because chip is active by default */
665 ret = regmap_write(dev->regmap[2], 0x05, 0x3e);
666 if (ret)
667 goto err_regmap_2_regmap_exit;
668
669 /* Create dvb frontend */
Antti Palosaari528af192014-10-26 23:01:07 -0300670 memcpy(&dev->fe.ops, &mn88472_ops, sizeof(struct dvb_frontend_ops));
671 dev->fe.demodulator_priv = client;
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300672 *pdata->fe = &dev->fe;
Antti Palosaari528af192014-10-26 23:01:07 -0300673 i2c_set_clientdata(client, dev);
674
Antti Palosaari61d7c6a2017-03-17 12:43:06 -0300675 /* Init stats to indicate which stats are supported */
676 c = &dev->fe.dtv_property_cache;
677 c->strength.len = 1;
Antti Palosaaric635a5e2017-03-17 13:34:44 -0300678 c->cnr.len = 1;
Antti Palosaari0fc66c12017-03-17 13:45:48 -0300679 c->block_error.len = 1;
680 c->block_count.len = 1;
Antti Palosaari61d7c6a2017-03-17 12:43:06 -0300681
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300682 /* Setup callbacks */
683 pdata->get_dvb_frontend = mn88472_get_dvb_frontend;
Antti Palosaari528af192014-10-26 23:01:07 -0300684
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300685 dev_info(&client->dev, "Panasonic MN88472 successfully identified\n");
686
687 return 0;
688err_regmap_2_regmap_exit:
689 regmap_exit(dev->regmap[2]);
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300690err_client_2_i2c_unregister_device:
691 i2c_unregister_device(dev->client[2]);
692err_regmap_1_regmap_exit:
693 regmap_exit(dev->regmap[1]);
Antti Palosaari528af192014-10-26 23:01:07 -0300694err_client_1_i2c_unregister_device:
695 i2c_unregister_device(dev->client[1]);
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300696err_regmap_0_regmap_exit:
697 regmap_exit(dev->regmap[0]);
Antti Palosaari528af192014-10-26 23:01:07 -0300698err_kfree:
699 kfree(dev);
700err:
701 dev_dbg(&client->dev, "failed=%d\n", ret);
702 return ret;
703}
704
705static int mn88472_remove(struct i2c_client *client)
706{
707 struct mn88472_dev *dev = i2c_get_clientdata(client);
708
709 dev_dbg(&client->dev, "\n");
710
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300711 regmap_exit(dev->regmap[2]);
Antti Palosaari528af192014-10-26 23:01:07 -0300712 i2c_unregister_device(dev->client[2]);
713
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300714 regmap_exit(dev->regmap[1]);
Antti Palosaari528af192014-10-26 23:01:07 -0300715 i2c_unregister_device(dev->client[1]);
716
Antti Palosaaric1011fb2014-10-26 23:26:04 -0300717 regmap_exit(dev->regmap[0]);
718
Antti Palosaari528af192014-10-26 23:01:07 -0300719 kfree(dev);
720
721 return 0;
722}
723
724static const struct i2c_device_id mn88472_id_table[] = {
725 {"mn88472", 0},
726 {}
727};
728MODULE_DEVICE_TABLE(i2c, mn88472_id_table);
729
730static struct i2c_driver mn88472_driver = {
731 .driver = {
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300732 .name = "mn88472",
733 .suppress_bind_attrs = true,
Antti Palosaari528af192014-10-26 23:01:07 -0300734 },
Antti Palosaari8d1f38f2016-05-13 12:19:19 -0300735 .probe = mn88472_probe,
736 .remove = mn88472_remove,
737 .id_table = mn88472_id_table,
Antti Palosaari528af192014-10-26 23:01:07 -0300738};
739
740module_i2c_driver(mn88472_driver);
741
Antti Palosaarica25cb52013-11-29 16:19:50 -0300742MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
743MODULE_DESCRIPTION("Panasonic MN88472 DVB-T/T2/C demodulator driver");
744MODULE_LICENSE("GPL");
745MODULE_FIRMWARE(MN88472_FIRMWARE);