blob: 23b831ce343911d117c8633a3f01014320c4b5ed [file] [log] [blame]
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001/*
2 * Afatech AF9033 demodulator driver
3 *
4 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
5 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
Antti Palosaari4b64bb22012-03-30 08:21:25 -030016 */
17
18#include "af9033_priv.h"
19
Antti Palosaari09611ca2014-08-31 02:18:34 -030020struct af9033_dev {
Antti Palosaarif5b00a72014-08-31 20:57:05 -030021 struct i2c_client *client;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -020022 struct regmap *regmap;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030023 struct dvb_frontend fe;
24 struct af9033_config cfg;
Antti Palosaari83f11612014-09-02 03:55:21 -030025 bool is_af9035;
26 bool is_it9135;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030027
28 u32 bandwidth_hz;
29 bool ts_mode_parallel;
30 bool ts_mode_serial;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -030031
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030032 enum fe_status fe_status;
Antti Palosaarie53c4742014-09-03 23:22:53 -030033 u64 post_bit_error_prev; /* for old read_ber we return (curr - prev) */
Antti Palosaari6bb096c2014-09-02 08:29:46 -030034 u64 post_bit_error;
35 u64 post_bit_count;
Antti Palosaari204f4312014-09-02 08:01:10 -030036 u64 error_block_count;
37 u64 total_block_count;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030038};
39
Antti Palosaari81e19912016-11-06 18:55:36 -020040/* Write reg val table using reg addr auto increment */
Antti Palosaari09611ca2014-08-31 02:18:34 -030041static int af9033_wr_reg_val_tab(struct af9033_dev *dev,
Antti Palosaari81e19912016-11-06 18:55:36 -020042 const struct reg_val *tab, int tab_len)
Antti Palosaari3bf5e552013-03-08 16:54:09 -030043{
Antti Palosaari81e19912016-11-06 18:55:36 -020044 struct i2c_client *client = dev->client;
Antti Palosaarid18a88b2013-11-27 17:17:43 -030045#define MAX_TAB_LEN 212
Antti Palosaari3bf5e552013-03-08 16:54:09 -030046 int ret, i, j;
Antti Palosaarid18a88b2013-11-27 17:17:43 -030047 u8 buf[1 + MAX_TAB_LEN];
Antti Palosaari3bf5e552013-03-08 16:54:09 -030048
Antti Palosaari81e19912016-11-06 18:55:36 -020049 dev_dbg(&client->dev, "tab_len=%d\n", tab_len);
Antti Palosaari3bf5e552013-03-08 16:54:09 -030050
Antti Palosaarid18a88b2013-11-27 17:17:43 -030051 if (tab_len > sizeof(buf)) {
Antti Palosaari81e19912016-11-06 18:55:36 -020052 dev_warn(&client->dev, "tab len %d is too big\n", tab_len);
Antti Palosaarid18a88b2013-11-27 17:17:43 -030053 return -EINVAL;
54 }
55
Antti Palosaari3bf5e552013-03-08 16:54:09 -030056 for (i = 0, j = 0; i < tab_len; i++) {
57 buf[j] = tab[i].val;
58
59 if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -020060 ret = regmap_bulk_write(dev->regmap, tab[i].reg - j,
61 buf, j + 1);
62 if (ret)
Antti Palosaari3bf5e552013-03-08 16:54:09 -030063 goto err;
64
65 j = 0;
66 } else {
67 j++;
68 }
69 }
70
71 return 0;
Antti Palosaari3bf5e552013-03-08 16:54:09 -030072err:
Antti Palosaari81e19912016-11-06 18:55:36 -020073 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari3bf5e552013-03-08 16:54:09 -030074 return ret;
75}
76
Antti Palosaari4b64bb22012-03-30 08:21:25 -030077static int af9033_init(struct dvb_frontend *fe)
78{
Antti Palosaari09611ca2014-08-31 02:18:34 -030079 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -020080 struct i2c_client *client = dev->client;
Antti Palosaari2db4d172014-09-06 11:24:14 -030081 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030082 int ret, i, len;
Antti Palosaaric2feb9f2016-11-06 15:27:16 -020083 unsigned int utmp;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030084 const struct reg_val *init;
85 u8 buf[4];
Antti Palosaari4b64bb22012-03-30 08:21:25 -030086 struct reg_val_mask tab[] = {
87 { 0x80fb24, 0x00, 0x08 },
88 { 0x80004c, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -030089 { 0x00f641, dev->cfg.tuner, 0xff },
Antti Palosaari4b64bb22012-03-30 08:21:25 -030090 { 0x80f5ca, 0x01, 0x01 },
91 { 0x80f715, 0x01, 0x01 },
92 { 0x00f41f, 0x04, 0x04 },
93 { 0x00f41a, 0x01, 0x01 },
94 { 0x80f731, 0x00, 0x01 },
95 { 0x00d91e, 0x00, 0x01 },
96 { 0x00d919, 0x00, 0x01 },
97 { 0x80f732, 0x00, 0x01 },
98 { 0x00d91f, 0x00, 0x01 },
99 { 0x00d91a, 0x00, 0x01 },
100 { 0x80f730, 0x00, 0x01 },
101 { 0x80f778, 0x00, 0xff },
102 { 0x80f73c, 0x01, 0x01 },
103 { 0x80f776, 0x00, 0x01 },
104 { 0x00d8fd, 0x01, 0xff },
105 { 0x00d830, 0x01, 0xff },
106 { 0x00d831, 0x00, 0xff },
107 { 0x00d832, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300108 { 0x80f985, dev->ts_mode_serial, 0x01 },
109 { 0x80f986, dev->ts_mode_parallel, 0x01 },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300110 { 0x00d827, 0x00, 0xff },
111 { 0x00d829, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300112 { 0x800045, dev->cfg.adc_multiplier, 0xff },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300113 };
114
Antti Palosaari81e19912016-11-06 18:55:36 -0200115 dev_dbg(&client->dev, "\n");
116
117 /* Main clk control */
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200118 utmp = div_u64((u64)dev->cfg.clock * 0x80000, 1000000);
119 buf[0] = (utmp >> 0) & 0xff;
120 buf[1] = (utmp >> 8) & 0xff;
121 buf[2] = (utmp >> 16) & 0xff;
122 buf[3] = (utmp >> 24) & 0xff;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200123 ret = regmap_bulk_write(dev->regmap, 0x800025, buf, 4);
124 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300125 goto err;
126
Antti Palosaari81e19912016-11-06 18:55:36 -0200127 dev_dbg(&client->dev, "clk=%u clk_cw=%08x\n", dev->cfg.clock, utmp);
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200128
Antti Palosaari81e19912016-11-06 18:55:36 -0200129 /* ADC clk control */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300130 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300131 if (clock_adc_lut[i].clock == dev->cfg.clock)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300132 break;
133 }
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300134 if (i == ARRAY_SIZE(clock_adc_lut)) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200135 dev_err(&client->dev, "Couldn't find ADC config for clock %d\n",
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300136 dev->cfg.clock);
137 goto err;
138 }
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300139
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200140 utmp = div_u64((u64)clock_adc_lut[i].adc * 0x80000, 1000000);
141 buf[0] = (utmp >> 0) & 0xff;
142 buf[1] = (utmp >> 8) & 0xff;
143 buf[2] = (utmp >> 16) & 0xff;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200144 ret = regmap_bulk_write(dev->regmap, 0x80f1cd, buf, 3);
145 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300146 goto err;
147
Antti Palosaari81e19912016-11-06 18:55:36 -0200148 dev_dbg(&client->dev, "adc=%u adc_cw=%06x\n",
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200149 clock_adc_lut[i].adc, utmp);
150
Antti Palosaari81e19912016-11-06 18:55:36 -0200151 /* Config register table */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300152 for (i = 0; i < ARRAY_SIZE(tab); i++) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200153 ret = regmap_update_bits(dev->regmap, tab[i].reg, tab[i].mask,
154 tab[i].val);
155 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300156 goto err;
157 }
158
Antti Palosaari81e19912016-11-06 18:55:36 -0200159 /* Demod clk output */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300160 if (dev->cfg.dyn0_clk) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200161 ret = regmap_write(dev->regmap, 0x80fba8, 0x00);
162 if (ret)
Antti Palosaari9dc0f3f2014-08-09 01:03:14 -0300163 goto err;
164 }
165
Antti Palosaari81e19912016-11-06 18:55:36 -0200166 /* TS interface */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300167 if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200168 ret = regmap_update_bits(dev->regmap, 0x80f9a5, 0x01, 0x00);
169 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300170 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200171 ret = regmap_update_bits(dev->regmap, 0x80f9b5, 0x01, 0x01);
172 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300173 goto err;
174 } else {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200175 ret = regmap_update_bits(dev->regmap, 0x80f990, 0x01, 0x00);
176 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300177 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200178 ret = regmap_update_bits(dev->regmap, 0x80f9b5, 0x01, 0x00);
179 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300180 goto err;
181 }
182
Antti Palosaari81e19912016-11-06 18:55:36 -0200183 /* Demod core settings */
184 dev_dbg(&client->dev, "load ofsm settings\n");
Antti Palosaari09611ca2014-08-31 02:18:34 -0300185 switch (dev->cfg.tuner) {
Antti Palosaarife8eece2013-02-03 13:39:55 -0300186 case AF9033_TUNER_IT9135_38:
187 case AF9033_TUNER_IT9135_51:
188 case AF9033_TUNER_IT9135_52:
Antti Palosaari463c3992013-03-07 00:03:06 -0300189 len = ARRAY_SIZE(ofsm_init_it9135_v1);
190 init = ofsm_init_it9135_v1;
191 break;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300192 case AF9033_TUNER_IT9135_60:
193 case AF9033_TUNER_IT9135_61:
194 case AF9033_TUNER_IT9135_62:
Antti Palosaari463c3992013-03-07 00:03:06 -0300195 len = ARRAY_SIZE(ofsm_init_it9135_v2);
196 init = ofsm_init_it9135_v2;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300197 break;
198 default:
199 len = ARRAY_SIZE(ofsm_init);
200 init = ofsm_init;
201 break;
202 }
203
Antti Palosaari09611ca2014-08-31 02:18:34 -0300204 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200205 if (ret)
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300206 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300207
Antti Palosaari81e19912016-11-06 18:55:36 -0200208 /* Demod tuner specific settings */
209 dev_dbg(&client->dev, "load tuner specific settings\n");
Antti Palosaari09611ca2014-08-31 02:18:34 -0300210 switch (dev->cfg.tuner) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300211 case AF9033_TUNER_TUA9001:
212 len = ARRAY_SIZE(tuner_init_tua9001);
213 init = tuner_init_tua9001;
214 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300215 case AF9033_TUNER_FC0011:
216 len = ARRAY_SIZE(tuner_init_fc0011);
217 init = tuner_init_fc0011;
218 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300219 case AF9033_TUNER_MXL5007T:
220 len = ARRAY_SIZE(tuner_init_mxl5007t);
221 init = tuner_init_mxl5007t;
222 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300223 case AF9033_TUNER_TDA18218:
224 len = ARRAY_SIZE(tuner_init_tda18218);
225 init = tuner_init_tda18218;
226 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300227 case AF9033_TUNER_FC2580:
228 len = ARRAY_SIZE(tuner_init_fc2580);
229 init = tuner_init_fc2580;
230 break;
Antti Palosaarie713ad12012-12-02 18:47:00 -0300231 case AF9033_TUNER_FC0012:
232 len = ARRAY_SIZE(tuner_init_fc0012);
233 init = tuner_init_fc0012;
234 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300235 case AF9033_TUNER_IT9135_38:
Antti Palosaaria72cbb72013-03-07 21:17:55 -0300236 len = ARRAY_SIZE(tuner_init_it9135_38);
237 init = tuner_init_it9135_38;
238 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300239 case AF9033_TUNER_IT9135_51:
Antti Palosaaribb2e12a2013-03-07 21:25:16 -0300240 len = ARRAY_SIZE(tuner_init_it9135_51);
241 init = tuner_init_it9135_51;
242 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300243 case AF9033_TUNER_IT9135_52:
Antti Palosaari22d729f2013-03-07 21:29:05 -0300244 len = ARRAY_SIZE(tuner_init_it9135_52);
245 init = tuner_init_it9135_52;
246 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300247 case AF9033_TUNER_IT9135_60:
Antti Palosaaria49f53a2013-03-07 21:38:00 -0300248 len = ARRAY_SIZE(tuner_init_it9135_60);
249 init = tuner_init_it9135_60;
250 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300251 case AF9033_TUNER_IT9135_61:
Antti Palosaari85211322013-03-07 21:44:30 -0300252 len = ARRAY_SIZE(tuner_init_it9135_61);
253 init = tuner_init_it9135_61;
254 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300255 case AF9033_TUNER_IT9135_62:
Antti Palosaaridc4a2c42013-03-07 21:47:20 -0300256 len = ARRAY_SIZE(tuner_init_it9135_62);
257 init = tuner_init_it9135_62;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300258 break;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300259 default:
Antti Palosaari81e19912016-11-06 18:55:36 -0200260 dev_dbg(&client->dev, "unsupported tuner ID=%d\n",
261 dev->cfg.tuner);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300262 ret = -ENODEV;
263 goto err;
264 }
265
Antti Palosaari09611ca2014-08-31 02:18:34 -0300266 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200267 if (ret)
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300268 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300269
Antti Palosaari09611ca2014-08-31 02:18:34 -0300270 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200271 ret = regmap_update_bits(dev->regmap, 0x00d91c, 0x01, 0x01);
272 if (ret)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300273 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200274 ret = regmap_update_bits(dev->regmap, 0x00d917, 0x01, 0x00);
275 if (ret)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300276 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200277 ret = regmap_update_bits(dev->regmap, 0x00d916, 0x01, 0x00);
278 if (ret)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300279 goto err;
280 }
281
Antti Palosaari09611ca2014-08-31 02:18:34 -0300282 switch (dev->cfg.tuner) {
Antti Palosaari086991d2013-03-07 22:18:56 -0300283 case AF9033_TUNER_IT9135_60:
284 case AF9033_TUNER_IT9135_61:
285 case AF9033_TUNER_IT9135_62:
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200286 ret = regmap_write(dev->regmap, 0x800000, 0x01);
287 if (ret)
Antti Palosaari086991d2013-03-07 22:18:56 -0300288 goto err;
289 }
290
Antti Palosaari81e19912016-11-06 18:55:36 -0200291 dev->bandwidth_hz = 0; /* Force to program all parameters */
292 /* Init stats here in order signal app which stats are supported */
Antti Palosaari2db4d172014-09-06 11:24:14 -0300293 c->strength.len = 1;
294 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
295 c->cnr.len = 1;
296 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
297 c->block_count.len = 1;
298 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
299 c->block_error.len = 1;
300 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
301 c->post_bit_count.len = 1;
302 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
303 c->post_bit_error.len = 1;
304 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300305
306 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300307err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200308 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300309 return ret;
310}
311
312static int af9033_sleep(struct dvb_frontend *fe)
313{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300314 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200315 struct i2c_client *client = dev->client;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200316 int ret;
317 unsigned int utmp;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300318
Antti Palosaari81e19912016-11-06 18:55:36 -0200319 dev_dbg(&client->dev, "\n");
320
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200321 ret = regmap_write(dev->regmap, 0x80004c, 0x01);
322 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300323 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200324 ret = regmap_write(dev->regmap, 0x800000, 0x00);
325 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300326 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200327 ret = regmap_read_poll_timeout(dev->regmap, 0x80004c, utmp, utmp == 0,
328 5000, 1000000);
329 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300330 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200331 ret = regmap_update_bits(dev->regmap, 0x80fb24, 0x08, 0x08);
332 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300333 goto err;
334
Antti Palosaari81e19912016-11-06 18:55:36 -0200335 /* Prevent current leak by setting TS interface to parallel mode */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300336 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200337 /* Enable parallel TS */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200338 ret = regmap_update_bits(dev->regmap, 0x00d917, 0x01, 0x00);
339 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300340 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200341 ret = regmap_update_bits(dev->regmap, 0x00d916, 0x01, 0x01);
342 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300343 goto err;
344 }
345
346 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300347err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200348 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300349 return ret;
350}
351
352static int af9033_get_tune_settings(struct dvb_frontend *fe,
Antti Palosaari81e19912016-11-06 18:55:36 -0200353 struct dvb_frontend_tune_settings *fesettings)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300354{
Antti Palosaarife8eece2013-02-03 13:39:55 -0300355 /* 800 => 2000 because IT9135 v2 is slow to gain lock */
356 fesettings->min_delay_ms = 2000;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300357 fesettings->step_size = 0;
358 fesettings->max_drift = 0;
359
360 return 0;
361}
362
363static int af9033_set_frontend(struct dvb_frontend *fe)
364{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300365 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200366 struct i2c_client *client = dev->client;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300367 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200368 int ret, i;
369 unsigned int utmp, adc_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300370 u8 tmp, buf[3], bandwidth_reg_val;
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200371 u32 if_frequency;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300372
Antti Palosaari81e19912016-11-06 18:55:36 -0200373 dev_dbg(&client->dev, "frequency=%u bandwidth_hz=%u\n",
374 c->frequency, c->bandwidth_hz);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300375
Antti Palosaari81e19912016-11-06 18:55:36 -0200376 /* Check bandwidth */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300377 switch (c->bandwidth_hz) {
378 case 6000000:
379 bandwidth_reg_val = 0x00;
380 break;
381 case 7000000:
382 bandwidth_reg_val = 0x01;
383 break;
384 case 8000000:
385 bandwidth_reg_val = 0x02;
386 break;
387 default:
Antti Palosaari81e19912016-11-06 18:55:36 -0200388 dev_dbg(&client->dev, "invalid bandwidth_hz\n");
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300389 ret = -EINVAL;
390 goto err;
391 }
392
Antti Palosaari81e19912016-11-06 18:55:36 -0200393 /* Program tuner */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300394 if (fe->ops.tuner_ops.set_params)
395 fe->ops.tuner_ops.set_params(fe);
396
Antti Palosaari81e19912016-11-06 18:55:36 -0200397 /* Coefficients */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300398 if (c->bandwidth_hz != dev->bandwidth_hz) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300399 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300400 if (coeff_lut[i].clock == dev->cfg.clock &&
Antti Palosaari81e19912016-11-06 18:55:36 -0200401 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300402 break;
403 }
404 }
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300405 if (i == ARRAY_SIZE(coeff_lut)) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200406 dev_err(&client->dev,
407 "Couldn't find config for clock %u\n",
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300408 dev->cfg.clock);
409 ret = -EINVAL;
410 goto err;
411 }
412
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200413 ret = regmap_bulk_write(dev->regmap, 0x800001, coeff_lut[i].val,
414 sizeof(coeff_lut[i].val));
415 if (ret)
416 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300417 }
418
Antti Palosaari81e19912016-11-06 18:55:36 -0200419 /* IF frequency control */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300420 if (c->bandwidth_hz != dev->bandwidth_hz) {
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300421 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300422 if (clock_adc_lut[i].clock == dev->cfg.clock)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300423 break;
424 }
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300425 if (i == ARRAY_SIZE(clock_adc_lut)) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200426 dev_err(&client->dev,
427 "Couldn't find ADC clock for clock %u\n",
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300428 dev->cfg.clock);
429 ret = -EINVAL;
430 goto err;
431 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300432 adc_freq = clock_adc_lut[i].adc;
433
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200434 if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
435 adc_freq = 2 * adc_freq;
436
Antti Palosaari81e19912016-11-06 18:55:36 -0200437 /* Get used IF frequency */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300438 if (fe->ops.tuner_ops.get_if_frequency)
439 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
440 else
441 if_frequency = 0;
442
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200443 utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x800000,
444 adc_freq);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300445
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200446 if (!dev->cfg.spec_inv && if_frequency)
447 utmp = 0x800000 - utmp;
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300448
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200449 buf[0] = (utmp >> 0) & 0xff;
450 buf[1] = (utmp >> 8) & 0xff;
451 buf[2] = (utmp >> 16) & 0xff;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200452 ret = regmap_bulk_write(dev->regmap, 0x800029, buf, 3);
453 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300454 goto err;
455
Antti Palosaari81e19912016-11-06 18:55:36 -0200456 dev_dbg(&client->dev, "if_frequency_cw=%06x\n", utmp);
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200457
Antti Palosaari09611ca2014-08-31 02:18:34 -0300458 dev->bandwidth_hz = c->bandwidth_hz;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300459 }
460
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200461 ret = regmap_update_bits(dev->regmap, 0x80f904, 0x03,
462 bandwidth_reg_val);
463 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300464 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200465 ret = regmap_write(dev->regmap, 0x800040, 0x00);
466 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300467 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200468 ret = regmap_write(dev->regmap, 0x800047, 0x00);
469 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300470 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200471 ret = regmap_update_bits(dev->regmap, 0x80f999, 0x01, 0x00);
472 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300473 goto err;
474
475 if (c->frequency <= 230000000)
476 tmp = 0x00; /* VHF */
477 else
478 tmp = 0x01; /* UHF */
479
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200480 ret = regmap_write(dev->regmap, 0x80004b, tmp);
481 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300482 goto err;
Antti Palosaari81e19912016-11-06 18:55:36 -0200483 /* Reset FSM */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200484 ret = regmap_write(dev->regmap, 0x800000, 0x00);
485 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300486 goto err;
487
488 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300489err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200490 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300491 return ret;
492}
493
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200494static int af9033_get_frontend(struct dvb_frontend *fe,
495 struct dtv_frontend_properties *c)
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300496{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300497 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200498 struct i2c_client *client = dev->client;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300499 int ret;
500 u8 buf[8];
501
Antti Palosaari81e19912016-11-06 18:55:36 -0200502 dev_dbg(&client->dev, "\n");
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300503
Antti Palosaari81e19912016-11-06 18:55:36 -0200504 /* Read all needed TPS registers */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200505 ret = regmap_bulk_read(dev->regmap, 0x80f900, buf, 8);
506 if (ret)
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300507 goto err;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300508
509 switch ((buf[0] >> 0) & 3) {
510 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300511 c->transmission_mode = TRANSMISSION_MODE_2K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300512 break;
513 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300514 c->transmission_mode = TRANSMISSION_MODE_8K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300515 break;
516 }
517
518 switch ((buf[1] >> 0) & 3) {
519 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300520 c->guard_interval = GUARD_INTERVAL_1_32;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300521 break;
522 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300523 c->guard_interval = GUARD_INTERVAL_1_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300524 break;
525 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300526 c->guard_interval = GUARD_INTERVAL_1_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300527 break;
528 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300529 c->guard_interval = GUARD_INTERVAL_1_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300530 break;
531 }
532
533 switch ((buf[2] >> 0) & 7) {
534 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300535 c->hierarchy = HIERARCHY_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300536 break;
537 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300538 c->hierarchy = HIERARCHY_1;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300539 break;
540 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300541 c->hierarchy = HIERARCHY_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300542 break;
543 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300544 c->hierarchy = HIERARCHY_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300545 break;
546 }
547
548 switch ((buf[3] >> 0) & 3) {
549 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300550 c->modulation = QPSK;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300551 break;
552 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300553 c->modulation = QAM_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300554 break;
555 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300556 c->modulation = QAM_64;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300557 break;
558 }
559
560 switch ((buf[4] >> 0) & 3) {
561 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300562 c->bandwidth_hz = 6000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300563 break;
564 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300565 c->bandwidth_hz = 7000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300566 break;
567 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300568 c->bandwidth_hz = 8000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300569 break;
570 }
571
572 switch ((buf[6] >> 0) & 7) {
573 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300574 c->code_rate_HP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300575 break;
576 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300577 c->code_rate_HP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300578 break;
579 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300580 c->code_rate_HP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300581 break;
582 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300583 c->code_rate_HP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300584 break;
585 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300586 c->code_rate_HP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300587 break;
588 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300589 c->code_rate_HP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300590 break;
591 }
592
593 switch ((buf[7] >> 0) & 7) {
594 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300595 c->code_rate_LP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300596 break;
597 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300598 c->code_rate_LP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300599 break;
600 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300601 c->code_rate_LP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300602 break;
603 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300604 c->code_rate_LP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300605 break;
606 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300607 c->code_rate_LP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300608 break;
609 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300610 c->code_rate_LP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300611 break;
612 }
613
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300614 return 0;
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300615err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200616 dev_dbg(&client->dev, "failed=%d\n", ret);
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300617 return ret;
618}
619
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300620static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300621{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300622 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200623 struct i2c_client *client = dev->client;
Antti Palosaari659a0992016-06-29 20:40:21 -0300624 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari239dd612016-12-19 01:42:37 -0200625 int ret, tmp = 0;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200626 u8 buf[7];
Antti Palosaari239dd612016-12-19 01:42:37 -0200627 unsigned int utmp, utmp1;
Antti Palosaari659a0992016-06-29 20:40:21 -0300628
Antti Palosaari81e19912016-11-06 18:55:36 -0200629 dev_dbg(&client->dev, "\n");
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300630
631 *status = 0;
632
Antti Palosaari81e19912016-11-06 18:55:36 -0200633 /* Radio channel status: 0=no result, 1=has signal, 2=no signal */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200634 ret = regmap_read(dev->regmap, 0x800047, &utmp);
635 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300636 goto err;
637
Antti Palosaari81e19912016-11-06 18:55:36 -0200638 /* Has signal */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200639 if (utmp == 0x01)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300640 *status |= FE_HAS_SIGNAL;
641
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200642 if (utmp != 0x02) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300643 /* TPS lock */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200644 ret = regmap_read(dev->regmap, 0x80f5a9, &utmp);
645 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300646 goto err;
647
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200648 if ((utmp >> 0) & 0x01)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300649 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
650 FE_HAS_VITERBI;
651
Antti Palosaari81e19912016-11-06 18:55:36 -0200652 /* Full lock */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200653 ret = regmap_read(dev->regmap, 0x80f999, &utmp);
654 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300655 goto err;
656
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200657 if ((utmp >> 0) & 0x01)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300658 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
659 FE_HAS_VITERBI | FE_HAS_SYNC |
660 FE_HAS_LOCK;
661 }
662
Antti Palosaari83f11612014-09-02 03:55:21 -0300663 dev->fe_status = *status;
664
Antti Palosaari81e19912016-11-06 18:55:36 -0200665 /* Signal strength */
Antti Palosaari659a0992016-06-29 20:40:21 -0300666 if (dev->fe_status & FE_HAS_SIGNAL) {
667 if (dev->is_af9035) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200668 ret = regmap_read(dev->regmap, 0x80004a, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300669 if (ret)
670 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200671 tmp = -utmp * 1000;
Antti Palosaari659a0992016-06-29 20:40:21 -0300672 } else {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200673 ret = regmap_read(dev->regmap, 0x8000f7, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300674 if (ret)
675 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200676 tmp = (utmp - 100) * 1000;
Antti Palosaari659a0992016-06-29 20:40:21 -0300677 }
678
679 c->strength.len = 1;
680 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
681 c->strength.stat[0].svalue = tmp;
682 } else {
683 c->strength.len = 1;
684 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
685 }
686
687 /* CNR */
688 if (dev->fe_status & FE_HAS_VITERBI) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200689 /* Read raw SNR value */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200690 ret = regmap_bulk_read(dev->regmap, 0x80002c, buf, 3);
Antti Palosaari659a0992016-06-29 20:40:21 -0300691 if (ret)
692 goto err;
693
Antti Palosaari239dd612016-12-19 01:42:37 -0200694 utmp1 = buf[2] << 16 | buf[1] << 8 | buf[0] << 0;
Antti Palosaari659a0992016-06-29 20:40:21 -0300695
Antti Palosaari81e19912016-11-06 18:55:36 -0200696 /* Read superframe number */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200697 ret = regmap_read(dev->regmap, 0x80f78b, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300698 if (ret)
699 goto err;
700
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200701 if (utmp)
Antti Palosaari239dd612016-12-19 01:42:37 -0200702 utmp1 /= utmp;
Antti Palosaari659a0992016-06-29 20:40:21 -0300703
Antti Palosaari81e19912016-11-06 18:55:36 -0200704 /* Read current transmission mode */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200705 ret = regmap_read(dev->regmap, 0x80f900, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300706 if (ret)
707 goto err;
708
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200709 switch ((utmp >> 0) & 3) {
Antti Palosaari659a0992016-06-29 20:40:21 -0300710 case 0:
Antti Palosaari239dd612016-12-19 01:42:37 -0200711 /* 2k */
712 utmp1 *= 4;
Antti Palosaari659a0992016-06-29 20:40:21 -0300713 break;
714 case 1:
Antti Palosaari239dd612016-12-19 01:42:37 -0200715 /* 8k */
716 utmp1 *= 1;
Antti Palosaari659a0992016-06-29 20:40:21 -0300717 break;
718 case 2:
Antti Palosaari239dd612016-12-19 01:42:37 -0200719 /* 4k */
720 utmp1 *= 2;
Antti Palosaari659a0992016-06-29 20:40:21 -0300721 break;
722 default:
Antti Palosaari239dd612016-12-19 01:42:37 -0200723 utmp1 *= 0;
Antti Palosaari659a0992016-06-29 20:40:21 -0300724 break;
725 }
726
Antti Palosaari81e19912016-11-06 18:55:36 -0200727 /* Read current modulation */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200728 ret = regmap_read(dev->regmap, 0x80f903, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300729 if (ret)
730 goto err;
731
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200732 switch ((utmp >> 0) & 3) {
Antti Palosaari659a0992016-06-29 20:40:21 -0300733 case 0:
Antti Palosaari239dd612016-12-19 01:42:37 -0200734 /*
735 * QPSK
736 * CNR[dB] 13 * -log10((1690000 - value) / value) + 2.6
737 * value [653799, 1689999], 2.6 / 13 = 3355443
738 */
739 utmp1 = clamp(utmp1, 653799U, 1689999U);
740 utmp1 = ((u64)(intlog10(utmp1)
741 - intlog10(1690000 - utmp1)
742 + 3355443) * 13 * 1000) >> 24;
Antti Palosaari659a0992016-06-29 20:40:21 -0300743 break;
744 case 1:
Antti Palosaari239dd612016-12-19 01:42:37 -0200745 /*
746 * QAM-16
747 * CNR[dB] 6 * log10((value - 370000) / (828000 - value)) + 15.7
748 * value [371105, 827999], 15.7 / 6 = 43900382
749 */
750 utmp1 = clamp(utmp1, 371105U, 827999U);
751 utmp1 = ((u64)(intlog10(utmp1 - 370000)
752 - intlog10(828000 - utmp1)
753 + 43900382) * 6 * 1000) >> 24;
Antti Palosaari659a0992016-06-29 20:40:21 -0300754 break;
755 case 2:
Antti Palosaari239dd612016-12-19 01:42:37 -0200756 /*
757 * QAM-64
758 * CNR[dB] 8 * log10((value - 193000) / (425000 - value)) + 23.8
759 * value [193246, 424999], 23.8 / 8 = 49912218
760 */
761 utmp1 = clamp(utmp1, 193246U, 424999U);
762 utmp1 = ((u64)(intlog10(utmp1 - 193000)
763 - intlog10(425000 - utmp1)
764 + 49912218) * 8 * 1000) >> 24;
Antti Palosaari659a0992016-06-29 20:40:21 -0300765 break;
766 default:
Antti Palosaari239dd612016-12-19 01:42:37 -0200767 utmp1 = 0;
Antti Palosaari659a0992016-06-29 20:40:21 -0300768 break;
769 }
770
Antti Palosaari239dd612016-12-19 01:42:37 -0200771 dev_dbg(&client->dev, "cnr=%u\n", utmp1);
Antti Palosaari659a0992016-06-29 20:40:21 -0300772
Antti Palosaari659a0992016-06-29 20:40:21 -0300773 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
Antti Palosaari239dd612016-12-19 01:42:37 -0200774 c->cnr.stat[0].svalue = utmp1;
Antti Palosaari659a0992016-06-29 20:40:21 -0300775 } else {
Antti Palosaari659a0992016-06-29 20:40:21 -0300776 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
777 }
778
779 /* UCB/PER/BER */
780 if (dev->fe_status & FE_HAS_LOCK) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200781 /* Outer FEC, 204 byte packets */
Antti Palosaari659a0992016-06-29 20:40:21 -0300782 u16 abort_packet_count, rsd_packet_count;
Antti Palosaari81e19912016-11-06 18:55:36 -0200783 /* Inner FEC, bits */
Antti Palosaari659a0992016-06-29 20:40:21 -0300784 u32 rsd_bit_err_count;
785
786 /*
787 * Packet count used for measurement is 10000
788 * (rsd_packet_count). Maybe it should be increased?
789 */
790
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200791 ret = regmap_bulk_read(dev->regmap, 0x800032, buf, 7);
Antti Palosaari659a0992016-06-29 20:40:21 -0300792 if (ret)
793 goto err;
794
795 abort_packet_count = (buf[1] << 8) | (buf[0] << 0);
796 rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2];
797 rsd_packet_count = (buf[6] << 8) | (buf[5] << 0);
798
799 dev->error_block_count += abort_packet_count;
800 dev->total_block_count += rsd_packet_count;
801 dev->post_bit_error += rsd_bit_err_count;
802 dev->post_bit_count += rsd_packet_count * 204 * 8;
803
804 c->block_count.len = 1;
805 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
806 c->block_count.stat[0].uvalue = dev->total_block_count;
807
808 c->block_error.len = 1;
809 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
810 c->block_error.stat[0].uvalue = dev->error_block_count;
811
812 c->post_bit_count.len = 1;
813 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
814 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
815
816 c->post_bit_error.len = 1;
817 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
818 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
819 }
820
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300821 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300822err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200823 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300824 return ret;
825}
826
827static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
828{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300829 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200830 struct i2c_client *client = dev->client;
Antti Palosaari6b457782014-09-02 05:03:21 -0300831 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300832 int ret;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200833 unsigned int utmp;
Antti Palosaarie898ef62012-04-01 12:50:02 -0300834
Antti Palosaari81e19912016-11-06 18:55:36 -0200835 dev_dbg(&client->dev, "\n");
836
837 /* Use DVBv5 CNR */
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300838 if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) {
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300839 /* Return 0.1 dB for AF9030 and 0-0xffff for IT9130. */
840 if (dev->is_af9035) {
841 /* 1000x => 10x (0.1 dB) */
842 *snr = div_s64(c->cnr.stat[0].svalue, 100);
843 } else {
844 /* 1000x => 1x (1 dB) */
845 *snr = div_s64(c->cnr.stat[0].svalue, 1000);
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300846
Antti Palosaari81e19912016-11-06 18:55:36 -0200847 /* Read current modulation */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200848 ret = regmap_read(dev->regmap, 0x80f903, &utmp);
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300849 if (ret)
850 goto err;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300851
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300852 /* scale value to 0x0000-0xffff */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200853 switch ((utmp >> 0) & 3) {
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300854 case 0:
855 *snr = *snr * 0xffff / 23;
856 break;
857 case 1:
858 *snr = *snr * 0xffff / 26;
859 break;
860 case 2:
861 *snr = *snr * 0xffff / 32;
862 break;
863 default:
864 goto err;
865 }
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300866 }
867 } else {
Antti Palosaari6b457782014-09-02 05:03:21 -0300868 *snr = 0;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300869 }
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300870
871 return 0;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300872err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200873 dev_dbg(&client->dev, "failed=%d\n", ret);
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300874 return ret;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300875}
876
877static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
878{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300879 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200880 struct i2c_client *client = dev->client;
Bimow Chen3adec272014-10-01 07:28:54 -0300881 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
882 int ret, tmp, power_real;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200883 unsigned int utmp;
884 u8 gain_offset, buf[7];
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300885
Antti Palosaari81e19912016-11-06 18:55:36 -0200886 dev_dbg(&client->dev, "\n");
887
Bimow Chen3adec272014-10-01 07:28:54 -0300888 if (dev->is_af9035) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200889 /* Read signal strength of 0-100 scale */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200890 ret = regmap_read(dev->regmap, 0x800048, &utmp);
891 if (ret)
Antti Palosaari0b0d9622014-11-03 20:24:13 -0300892 goto err;
893
Antti Palosaari81e19912016-11-06 18:55:36 -0200894 /* Scale value to 0x0000-0xffff */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200895 *strength = utmp * 0xffff / 100;
Bimow Chen3adec272014-10-01 07:28:54 -0300896 } else {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200897 ret = regmap_read(dev->regmap, 0x8000f7, &utmp);
898 if (ret)
Antti Palosaari1620d222014-11-03 21:23:26 -0300899 goto err;
900
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200901 ret = regmap_bulk_read(dev->regmap, 0x80f900, buf, 7);
902 if (ret)
Antti Palosaari1620d222014-11-03 21:23:26 -0300903 goto err;
Bimow Chen3adec272014-10-01 07:28:54 -0300904
905 if (c->frequency <= 300000000)
906 gain_offset = 7; /* VHF */
907 else
908 gain_offset = 4; /* UHF */
909
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200910 power_real = (utmp - 100 - gain_offset) -
Bimow Chen3adec272014-10-01 07:28:54 -0300911 power_reference[((buf[3] >> 0) & 3)][((buf[6] >> 0) & 7)];
912
913 if (power_real < -15)
914 tmp = 0;
915 else if ((power_real >= -15) && (power_real < 0))
916 tmp = (2 * (power_real + 15)) / 3;
917 else if ((power_real >= 0) && (power_real < 20))
918 tmp = 4 * power_real + 10;
919 else if ((power_real >= 20) && (power_real < 35))
920 tmp = (2 * (power_real - 20)) / 3 + 90;
921 else
922 tmp = 100;
923
Antti Palosaari81e19912016-11-06 18:55:36 -0200924 /* Scale value to 0x0000-0xffff */
Bimow Chen3adec272014-10-01 07:28:54 -0300925 *strength = tmp * 0xffff / 100;
926 }
927
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300928 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300929err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200930 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300931 return ret;
932}
933
934static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
935{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300936 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300937
Antti Palosaarie53c4742014-09-03 23:22:53 -0300938 *ber = (dev->post_bit_error - dev->post_bit_error_prev);
939 dev->post_bit_error_prev = dev->post_bit_error;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300940
941 return 0;
942}
943
944static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
945{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300946 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300947
Antti Palosaari1d0ceae2014-09-03 23:07:39 -0300948 *ucblocks = dev->error_block_count;
Antti Palosaari81e19912016-11-06 18:55:36 -0200949
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300950 return 0;
951}
952
953static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
954{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300955 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200956 struct i2c_client *client = dev->client;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300957 int ret;
958
Antti Palosaari81e19912016-11-06 18:55:36 -0200959 dev_dbg(&client->dev, "enable=%d\n", enable);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300960
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200961 ret = regmap_update_bits(dev->regmap, 0x00fa04, 0x01, enable);
962 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300963 goto err;
964
965 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300966err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200967 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300968 return ret;
969}
970
Mauro Carvalho Chehabed97a6f2014-03-14 14:29:06 -0300971static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -0300972{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300973 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200974 struct i2c_client *client = dev->client;
Antti Palosaari040cf862014-02-13 15:40:59 -0300975 int ret;
976
Antti Palosaari81e19912016-11-06 18:55:36 -0200977 dev_dbg(&client->dev, "onoff=%d\n", onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300978
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200979 ret = regmap_update_bits(dev->regmap, 0x80f993, 0x01, onoff);
980 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -0300981 goto err;
982
983 return 0;
Antti Palosaari040cf862014-02-13 15:40:59 -0300984err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200985 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari040cf862014-02-13 15:40:59 -0300986 return ret;
987}
Antti Palosaari040cf862014-02-13 15:40:59 -0300988
Antti Palosaari24e419a2014-08-09 14:59:53 -0300989static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
Antti Palosaari81e19912016-11-06 18:55:36 -0200990 int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -0300991{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300992 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200993 struct i2c_client *client = dev->client;
Antti Palosaari040cf862014-02-13 15:40:59 -0300994 int ret;
995 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
996
Antti Palosaari81e19912016-11-06 18:55:36 -0200997 dev_dbg(&client->dev, "index=%d pid=%04x onoff=%d\n",
998 index, pid, onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300999
1000 if (pid > 0x1fff)
1001 return 0;
1002
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001003 ret = regmap_bulk_write(dev->regmap, 0x80f996, wbuf, 2);
1004 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -03001005 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001006 ret = regmap_write(dev->regmap, 0x80f994, onoff);
1007 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -03001008 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001009 ret = regmap_write(dev->regmap, 0x80f995, index);
1010 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -03001011 goto err;
1012
1013 return 0;
Antti Palosaari040cf862014-02-13 15:40:59 -03001014err:
Antti Palosaari81e19912016-11-06 18:55:36 -02001015 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari040cf862014-02-13 15:40:59 -03001016 return ret;
1017}
Antti Palosaari040cf862014-02-13 15:40:59 -03001018
Max Kellermannbd336e62016-08-09 18:32:21 -03001019static const struct dvb_frontend_ops af9033_ops = {
Antti Palosaari81e19912016-11-06 18:55:36 -02001020 .delsys = {SYS_DVBT},
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001021 .info = {
1022 .name = "Afatech AF9033 (DVB-T)",
Mauro Carvalho Chehabf1b1eab2018-07-05 18:59:36 -04001023 .frequency_min_hz = 174 * MHz,
1024 .frequency_max_hz = 862 * MHz,
1025 .frequency_stepsize_hz = 250 * kHz,
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001026 .caps = FE_CAN_FEC_1_2 |
1027 FE_CAN_FEC_2_3 |
1028 FE_CAN_FEC_3_4 |
1029 FE_CAN_FEC_5_6 |
1030 FE_CAN_FEC_7_8 |
1031 FE_CAN_FEC_AUTO |
1032 FE_CAN_QPSK |
1033 FE_CAN_QAM_16 |
1034 FE_CAN_QAM_64 |
1035 FE_CAN_QAM_AUTO |
1036 FE_CAN_TRANSMISSION_MODE_AUTO |
1037 FE_CAN_GUARD_INTERVAL_AUTO |
1038 FE_CAN_HIERARCHY_AUTO |
1039 FE_CAN_RECOVER |
1040 FE_CAN_MUTE_TS
1041 },
1042
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001043 .init = af9033_init,
1044 .sleep = af9033_sleep,
1045
1046 .get_tune_settings = af9033_get_tune_settings,
1047 .set_frontend = af9033_set_frontend,
Gianluca Gennari0a4df232012-04-05 12:47:19 -03001048 .get_frontend = af9033_get_frontend,
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001049
1050 .read_status = af9033_read_status,
1051 .read_snr = af9033_read_snr,
1052 .read_signal_strength = af9033_read_signal_strength,
1053 .read_ber = af9033_read_ber,
1054 .read_ucblocks = af9033_read_ucblocks,
1055
1056 .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
1057};
1058
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001059static int af9033_probe(struct i2c_client *client,
Antti Palosaari81e19912016-11-06 18:55:36 -02001060 const struct i2c_device_id *id)
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001061{
1062 struct af9033_config *cfg = client->dev.platform_data;
1063 struct af9033_dev *dev;
1064 int ret;
1065 u8 buf[8];
1066 u32 reg;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001067 static const struct regmap_config regmap_config = {
1068 .reg_bits = 24,
1069 .val_bits = 8,
1070 };
1071
Antti Palosaari81e19912016-11-06 18:55:36 -02001072 /* Allocate memory for the internal state */
1073 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1074 if (!dev) {
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001075 ret = -ENOMEM;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001076 goto err;
1077 }
1078
Antti Palosaari81e19912016-11-06 18:55:36 -02001079 /* Setup the state */
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001080 dev->client = client;
Antti Palosaari81e19912016-11-06 18:55:36 -02001081 memcpy(&dev->cfg, cfg, sizeof(dev->cfg));
1082 switch (dev->cfg.ts_mode) {
1083 case AF9033_TS_MODE_PARALLEL:
1084 dev->ts_mode_parallel = true;
1085 break;
1086 case AF9033_TS_MODE_SERIAL:
1087 dev->ts_mode_serial = true;
1088 break;
1089 case AF9033_TS_MODE_USB:
1090 /* USB mode for AF9035 */
1091 default:
1092 break;
1093 }
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001094
1095 if (dev->cfg.clock != 12000000) {
1096 ret = -ENODEV;
Antti Palosaari81e19912016-11-06 18:55:36 -02001097 dev_err(&client->dev,
1098 "Unsupported clock %u Hz. Only 12000000 Hz is supported currently\n",
1099 dev->cfg.clock);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001100 goto err_kfree;
1101 }
1102
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001103 /* Create regmap */
1104 dev->regmap = regmap_init_i2c(client, &regmap_config);
1105 if (IS_ERR(dev->regmap)) {
1106 ret = PTR_ERR(dev->regmap);
1107 goto err_kfree;
1108 }
1109
Antti Palosaari81e19912016-11-06 18:55:36 -02001110 /* Firmware version */
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001111 switch (dev->cfg.tuner) {
1112 case AF9033_TUNER_IT9135_38:
1113 case AF9033_TUNER_IT9135_51:
1114 case AF9033_TUNER_IT9135_52:
1115 case AF9033_TUNER_IT9135_60:
1116 case AF9033_TUNER_IT9135_61:
1117 case AF9033_TUNER_IT9135_62:
Antti Palosaari83f11612014-09-02 03:55:21 -03001118 dev->is_it9135 = true;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001119 reg = 0x004bfc;
1120 break;
1121 default:
Antti Palosaari83f11612014-09-02 03:55:21 -03001122 dev->is_af9035 = true;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001123 reg = 0x0083e9;
1124 break;
1125 }
1126
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001127 ret = regmap_bulk_read(dev->regmap, reg, &buf[0], 4);
1128 if (ret)
1129 goto err_regmap_exit;
1130 ret = regmap_bulk_read(dev->regmap, 0x804191, &buf[4], 4);
1131 if (ret)
1132 goto err_regmap_exit;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001133
Antti Palosaari81e19912016-11-06 18:55:36 -02001134 dev_info(&client->dev,
1135 "firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n",
1136 buf[0], buf[1], buf[2], buf[3],
1137 buf[4], buf[5], buf[6], buf[7]);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001138
Antti Palosaari81e19912016-11-06 18:55:36 -02001139 /* Sleep as chip seems to be partly active by default */
Victor Toso09a446d2018-10-30 12:14:48 -04001140 /* IT9135 did not like to sleep at that early */
1141 if (dev->is_af9035) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001142 ret = regmap_write(dev->regmap, 0x80004c, 0x01);
1143 if (ret)
1144 goto err_regmap_exit;
1145 ret = regmap_write(dev->regmap, 0x800000, 0x00);
1146 if (ret)
1147 goto err_regmap_exit;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001148 }
1149
Antti Palosaari81e19912016-11-06 18:55:36 -02001150 /* Create dvb frontend */
1151 memcpy(&dev->fe.ops, &af9033_ops, sizeof(dev->fe.ops));
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001152 dev->fe.demodulator_priv = dev;
1153 *cfg->fe = &dev->fe;
1154 if (cfg->ops) {
1155 cfg->ops->pid_filter = af9033_pid_filter;
1156 cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl;
1157 }
Antti Palosaari34618312016-11-06 22:14:18 -02001158 cfg->regmap = dev->regmap;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001159 i2c_set_clientdata(client, dev);
1160
Antti Palosaari81e19912016-11-06 18:55:36 -02001161 dev_info(&client->dev, "Afatech AF9033 successfully attached\n");
1162
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001163 return 0;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001164err_regmap_exit:
1165 regmap_exit(dev->regmap);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001166err_kfree:
1167 kfree(dev);
1168err:
Antti Palosaari6a087f12014-08-31 21:08:09 -03001169 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001170 return ret;
1171}
1172
1173static int af9033_remove(struct i2c_client *client)
1174{
1175 struct af9033_dev *dev = i2c_get_clientdata(client);
1176
Antti Palosaari81e19912016-11-06 18:55:36 -02001177 dev_dbg(&client->dev, "\n");
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001178
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001179 regmap_exit(dev->regmap);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001180 kfree(dev);
1181
1182 return 0;
1183}
1184
1185static const struct i2c_device_id af9033_id_table[] = {
1186 {"af9033", 0},
1187 {}
1188};
1189MODULE_DEVICE_TABLE(i2c, af9033_id_table);
1190
1191static struct i2c_driver af9033_driver = {
1192 .driver = {
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001193 .name = "af9033",
Antti Palosaari72812172016-06-29 20:40:22 -03001194 .suppress_bind_attrs = true,
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001195 },
1196 .probe = af9033_probe,
1197 .remove = af9033_remove,
1198 .id_table = af9033_id_table,
1199};
1200
1201module_i2c_driver(af9033_driver);
1202
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001203MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1204MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
1205MODULE_LICENSE("GPL");