blob: 785c49b3d3072bb4737c82af72ebe17af1bee67b [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Antti Palosaari4b64bb22012-03-30 08:21:25 -03002/*
3 * Afatech AF9033 demodulator driver
4 *
5 * Copyright (C) 2009 Antti Palosaari <crope@iki.fi>
6 * Copyright (C) 2012 Antti Palosaari <crope@iki.fi>
Antti Palosaari4b64bb22012-03-30 08:21:25 -03007 */
8
9#include "af9033_priv.h"
10
Antti Palosaari09611ca2014-08-31 02:18:34 -030011struct af9033_dev {
Antti Palosaarif5b00a72014-08-31 20:57:05 -030012 struct i2c_client *client;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -020013 struct regmap *regmap;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030014 struct dvb_frontend fe;
15 struct af9033_config cfg;
Antti Palosaari83f11612014-09-02 03:55:21 -030016 bool is_af9035;
17 bool is_it9135;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030018
19 u32 bandwidth_hz;
20 bool ts_mode_parallel;
21 bool ts_mode_serial;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -030022
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -030023 enum fe_status fe_status;
Antti Palosaarie53c4742014-09-03 23:22:53 -030024 u64 post_bit_error_prev; /* for old read_ber we return (curr - prev) */
Antti Palosaari6bb096c2014-09-02 08:29:46 -030025 u64 post_bit_error;
26 u64 post_bit_count;
Antti Palosaari204f4312014-09-02 08:01:10 -030027 u64 error_block_count;
28 u64 total_block_count;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030029};
30
Antti Palosaari81e19912016-11-06 18:55:36 -020031/* Write reg val table using reg addr auto increment */
Antti Palosaari09611ca2014-08-31 02:18:34 -030032static int af9033_wr_reg_val_tab(struct af9033_dev *dev,
Antti Palosaari81e19912016-11-06 18:55:36 -020033 const struct reg_val *tab, int tab_len)
Antti Palosaari3bf5e552013-03-08 16:54:09 -030034{
Antti Palosaari81e19912016-11-06 18:55:36 -020035 struct i2c_client *client = dev->client;
Antti Palosaarid18a88b2013-11-27 17:17:43 -030036#define MAX_TAB_LEN 212
Antti Palosaari3bf5e552013-03-08 16:54:09 -030037 int ret, i, j;
Antti Palosaarid18a88b2013-11-27 17:17:43 -030038 u8 buf[1 + MAX_TAB_LEN];
Antti Palosaari3bf5e552013-03-08 16:54:09 -030039
Antti Palosaari81e19912016-11-06 18:55:36 -020040 dev_dbg(&client->dev, "tab_len=%d\n", tab_len);
Antti Palosaari3bf5e552013-03-08 16:54:09 -030041
Antti Palosaarid18a88b2013-11-27 17:17:43 -030042 if (tab_len > sizeof(buf)) {
Antti Palosaari81e19912016-11-06 18:55:36 -020043 dev_warn(&client->dev, "tab len %d is too big\n", tab_len);
Antti Palosaarid18a88b2013-11-27 17:17:43 -030044 return -EINVAL;
45 }
46
Antti Palosaari3bf5e552013-03-08 16:54:09 -030047 for (i = 0, j = 0; i < tab_len; i++) {
48 buf[j] = tab[i].val;
49
50 if (i == tab_len - 1 || tab[i].reg != tab[i + 1].reg - 1) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -020051 ret = regmap_bulk_write(dev->regmap, tab[i].reg - j,
52 buf, j + 1);
53 if (ret)
Antti Palosaari3bf5e552013-03-08 16:54:09 -030054 goto err;
55
56 j = 0;
57 } else {
58 j++;
59 }
60 }
61
62 return 0;
Antti Palosaari3bf5e552013-03-08 16:54:09 -030063err:
Antti Palosaari81e19912016-11-06 18:55:36 -020064 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari3bf5e552013-03-08 16:54:09 -030065 return ret;
66}
67
Antti Palosaari4b64bb22012-03-30 08:21:25 -030068static int af9033_init(struct dvb_frontend *fe)
69{
Antti Palosaari09611ca2014-08-31 02:18:34 -030070 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -020071 struct i2c_client *client = dev->client;
Antti Palosaari2db4d172014-09-06 11:24:14 -030072 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030073 int ret, i, len;
Antti Palosaaric2feb9f2016-11-06 15:27:16 -020074 unsigned int utmp;
Antti Palosaari4b64bb22012-03-30 08:21:25 -030075 const struct reg_val *init;
76 u8 buf[4];
Antti Palosaari4b64bb22012-03-30 08:21:25 -030077 struct reg_val_mask tab[] = {
78 { 0x80fb24, 0x00, 0x08 },
79 { 0x80004c, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -030080 { 0x00f641, dev->cfg.tuner, 0xff },
Antti Palosaari4b64bb22012-03-30 08:21:25 -030081 { 0x80f5ca, 0x01, 0x01 },
82 { 0x80f715, 0x01, 0x01 },
83 { 0x00f41f, 0x04, 0x04 },
84 { 0x00f41a, 0x01, 0x01 },
85 { 0x80f731, 0x00, 0x01 },
86 { 0x00d91e, 0x00, 0x01 },
87 { 0x00d919, 0x00, 0x01 },
88 { 0x80f732, 0x00, 0x01 },
89 { 0x00d91f, 0x00, 0x01 },
90 { 0x00d91a, 0x00, 0x01 },
91 { 0x80f730, 0x00, 0x01 },
92 { 0x80f778, 0x00, 0xff },
93 { 0x80f73c, 0x01, 0x01 },
94 { 0x80f776, 0x00, 0x01 },
95 { 0x00d8fd, 0x01, 0xff },
96 { 0x00d830, 0x01, 0xff },
97 { 0x00d831, 0x00, 0xff },
98 { 0x00d832, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -030099 { 0x80f985, dev->ts_mode_serial, 0x01 },
100 { 0x80f986, dev->ts_mode_parallel, 0x01 },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300101 { 0x00d827, 0x00, 0xff },
102 { 0x00d829, 0x00, 0xff },
Antti Palosaari09611ca2014-08-31 02:18:34 -0300103 { 0x800045, dev->cfg.adc_multiplier, 0xff },
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300104 };
105
Antti Palosaari81e19912016-11-06 18:55:36 -0200106 dev_dbg(&client->dev, "\n");
107
108 /* Main clk control */
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200109 utmp = div_u64((u64)dev->cfg.clock * 0x80000, 1000000);
110 buf[0] = (utmp >> 0) & 0xff;
111 buf[1] = (utmp >> 8) & 0xff;
112 buf[2] = (utmp >> 16) & 0xff;
113 buf[3] = (utmp >> 24) & 0xff;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200114 ret = regmap_bulk_write(dev->regmap, 0x800025, buf, 4);
115 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300116 goto err;
117
Antti Palosaari81e19912016-11-06 18:55:36 -0200118 dev_dbg(&client->dev, "clk=%u clk_cw=%08x\n", dev->cfg.clock, utmp);
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200119
Antti Palosaari81e19912016-11-06 18:55:36 -0200120 /* ADC clk control */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300121 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300122 if (clock_adc_lut[i].clock == dev->cfg.clock)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300123 break;
124 }
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300125 if (i == ARRAY_SIZE(clock_adc_lut)) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200126 dev_err(&client->dev, "Couldn't find ADC config for clock %d\n",
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300127 dev->cfg.clock);
Hans Verkuile1219932021-01-20 10:09:05 +0100128 ret = -ENODEV;
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300129 goto err;
130 }
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300131
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200132 utmp = div_u64((u64)clock_adc_lut[i].adc * 0x80000, 1000000);
133 buf[0] = (utmp >> 0) & 0xff;
134 buf[1] = (utmp >> 8) & 0xff;
135 buf[2] = (utmp >> 16) & 0xff;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200136 ret = regmap_bulk_write(dev->regmap, 0x80f1cd, buf, 3);
137 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300138 goto err;
139
Antti Palosaari81e19912016-11-06 18:55:36 -0200140 dev_dbg(&client->dev, "adc=%u adc_cw=%06x\n",
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200141 clock_adc_lut[i].adc, utmp);
142
Antti Palosaari81e19912016-11-06 18:55:36 -0200143 /* Config register table */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300144 for (i = 0; i < ARRAY_SIZE(tab); i++) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200145 ret = regmap_update_bits(dev->regmap, tab[i].reg, tab[i].mask,
146 tab[i].val);
147 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300148 goto err;
149 }
150
Antti Palosaari81e19912016-11-06 18:55:36 -0200151 /* Demod clk output */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300152 if (dev->cfg.dyn0_clk) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200153 ret = regmap_write(dev->regmap, 0x80fba8, 0x00);
154 if (ret)
Antti Palosaari9dc0f3f2014-08-09 01:03:14 -0300155 goto err;
156 }
157
Antti Palosaari81e19912016-11-06 18:55:36 -0200158 /* TS interface */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300159 if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200160 ret = regmap_update_bits(dev->regmap, 0x80f9a5, 0x01, 0x00);
161 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300162 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200163 ret = regmap_update_bits(dev->regmap, 0x80f9b5, 0x01, 0x01);
164 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300165 goto err;
166 } else {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200167 ret = regmap_update_bits(dev->regmap, 0x80f990, 0x01, 0x00);
168 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300169 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200170 ret = regmap_update_bits(dev->regmap, 0x80f9b5, 0x01, 0x00);
171 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300172 goto err;
173 }
174
Antti Palosaari81e19912016-11-06 18:55:36 -0200175 /* Demod core settings */
176 dev_dbg(&client->dev, "load ofsm settings\n");
Antti Palosaari09611ca2014-08-31 02:18:34 -0300177 switch (dev->cfg.tuner) {
Antti Palosaarife8eece2013-02-03 13:39:55 -0300178 case AF9033_TUNER_IT9135_38:
179 case AF9033_TUNER_IT9135_51:
180 case AF9033_TUNER_IT9135_52:
Antti Palosaari463c3992013-03-07 00:03:06 -0300181 len = ARRAY_SIZE(ofsm_init_it9135_v1);
182 init = ofsm_init_it9135_v1;
183 break;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300184 case AF9033_TUNER_IT9135_60:
185 case AF9033_TUNER_IT9135_61:
186 case AF9033_TUNER_IT9135_62:
Antti Palosaari463c3992013-03-07 00:03:06 -0300187 len = ARRAY_SIZE(ofsm_init_it9135_v2);
188 init = ofsm_init_it9135_v2;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300189 break;
190 default:
191 len = ARRAY_SIZE(ofsm_init);
192 init = ofsm_init;
193 break;
194 }
195
Antti Palosaari09611ca2014-08-31 02:18:34 -0300196 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200197 if (ret)
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300198 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300199
Antti Palosaari81e19912016-11-06 18:55:36 -0200200 /* Demod tuner specific settings */
201 dev_dbg(&client->dev, "load tuner specific settings\n");
Antti Palosaari09611ca2014-08-31 02:18:34 -0300202 switch (dev->cfg.tuner) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300203 case AF9033_TUNER_TUA9001:
204 len = ARRAY_SIZE(tuner_init_tua9001);
205 init = tuner_init_tua9001;
206 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300207 case AF9033_TUNER_FC0011:
208 len = ARRAY_SIZE(tuner_init_fc0011);
209 init = tuner_init_fc0011;
210 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300211 case AF9033_TUNER_MXL5007T:
212 len = ARRAY_SIZE(tuner_init_mxl5007t);
213 init = tuner_init_mxl5007t;
214 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300215 case AF9033_TUNER_TDA18218:
216 len = ARRAY_SIZE(tuner_init_tda18218);
217 init = tuner_init_tda18218;
218 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300219 case AF9033_TUNER_FC2580:
220 len = ARRAY_SIZE(tuner_init_fc2580);
221 init = tuner_init_fc2580;
222 break;
Antti Palosaarie713ad12012-12-02 18:47:00 -0300223 case AF9033_TUNER_FC0012:
224 len = ARRAY_SIZE(tuner_init_fc0012);
225 init = tuner_init_fc0012;
226 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300227 case AF9033_TUNER_IT9135_38:
Antti Palosaaria72cbb72013-03-07 21:17:55 -0300228 len = ARRAY_SIZE(tuner_init_it9135_38);
229 init = tuner_init_it9135_38;
230 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300231 case AF9033_TUNER_IT9135_51:
Antti Palosaaribb2e12a2013-03-07 21:25:16 -0300232 len = ARRAY_SIZE(tuner_init_it9135_51);
233 init = tuner_init_it9135_51;
234 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300235 case AF9033_TUNER_IT9135_52:
Antti Palosaari22d729f2013-03-07 21:29:05 -0300236 len = ARRAY_SIZE(tuner_init_it9135_52);
237 init = tuner_init_it9135_52;
238 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300239 case AF9033_TUNER_IT9135_60:
Antti Palosaaria49f53a2013-03-07 21:38:00 -0300240 len = ARRAY_SIZE(tuner_init_it9135_60);
241 init = tuner_init_it9135_60;
242 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300243 case AF9033_TUNER_IT9135_61:
Antti Palosaari85211322013-03-07 21:44:30 -0300244 len = ARRAY_SIZE(tuner_init_it9135_61);
245 init = tuner_init_it9135_61;
246 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300247 case AF9033_TUNER_IT9135_62:
Antti Palosaaridc4a2c42013-03-07 21:47:20 -0300248 len = ARRAY_SIZE(tuner_init_it9135_62);
249 init = tuner_init_it9135_62;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300250 break;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300251 default:
Antti Palosaari81e19912016-11-06 18:55:36 -0200252 dev_dbg(&client->dev, "unsupported tuner ID=%d\n",
253 dev->cfg.tuner);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300254 ret = -ENODEV;
255 goto err;
256 }
257
Antti Palosaari09611ca2014-08-31 02:18:34 -0300258 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200259 if (ret)
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300260 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300261
Antti Palosaari09611ca2014-08-31 02:18:34 -0300262 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200263 ret = regmap_update_bits(dev->regmap, 0x00d91c, 0x01, 0x01);
264 if (ret)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300265 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200266 ret = regmap_update_bits(dev->regmap, 0x00d917, 0x01, 0x00);
267 if (ret)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300268 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200269 ret = regmap_update_bits(dev->regmap, 0x00d916, 0x01, 0x00);
270 if (ret)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300271 goto err;
272 }
273
Antti Palosaari09611ca2014-08-31 02:18:34 -0300274 switch (dev->cfg.tuner) {
Antti Palosaari086991d2013-03-07 22:18:56 -0300275 case AF9033_TUNER_IT9135_60:
276 case AF9033_TUNER_IT9135_61:
277 case AF9033_TUNER_IT9135_62:
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200278 ret = regmap_write(dev->regmap, 0x800000, 0x01);
279 if (ret)
Antti Palosaari086991d2013-03-07 22:18:56 -0300280 goto err;
281 }
282
Antti Palosaari81e19912016-11-06 18:55:36 -0200283 dev->bandwidth_hz = 0; /* Force to program all parameters */
284 /* Init stats here in order signal app which stats are supported */
Antti Palosaari2db4d172014-09-06 11:24:14 -0300285 c->strength.len = 1;
286 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
287 c->cnr.len = 1;
288 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
289 c->block_count.len = 1;
290 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
291 c->block_error.len = 1;
292 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
293 c->post_bit_count.len = 1;
294 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
295 c->post_bit_error.len = 1;
296 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300297
298 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300299err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200300 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300301 return ret;
302}
303
304static int af9033_sleep(struct dvb_frontend *fe)
305{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300306 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200307 struct i2c_client *client = dev->client;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200308 int ret;
309 unsigned int utmp;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300310
Antti Palosaari81e19912016-11-06 18:55:36 -0200311 dev_dbg(&client->dev, "\n");
312
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200313 ret = regmap_write(dev->regmap, 0x80004c, 0x01);
314 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300315 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200316 ret = regmap_write(dev->regmap, 0x800000, 0x00);
317 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300318 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200319 ret = regmap_read_poll_timeout(dev->regmap, 0x80004c, utmp, utmp == 0,
320 5000, 1000000);
321 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300322 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200323 ret = regmap_update_bits(dev->regmap, 0x80fb24, 0x08, 0x08);
324 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300325 goto err;
326
Antti Palosaari81e19912016-11-06 18:55:36 -0200327 /* Prevent current leak by setting TS interface to parallel mode */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300328 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200329 /* Enable parallel TS */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200330 ret = regmap_update_bits(dev->regmap, 0x00d917, 0x01, 0x00);
331 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300332 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200333 ret = regmap_update_bits(dev->regmap, 0x00d916, 0x01, 0x01);
334 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300335 goto err;
336 }
337
338 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300339err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200340 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300341 return ret;
342}
343
344static int af9033_get_tune_settings(struct dvb_frontend *fe,
Antti Palosaari81e19912016-11-06 18:55:36 -0200345 struct dvb_frontend_tune_settings *fesettings)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300346{
Antti Palosaarife8eece2013-02-03 13:39:55 -0300347 /* 800 => 2000 because IT9135 v2 is slow to gain lock */
348 fesettings->min_delay_ms = 2000;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300349 fesettings->step_size = 0;
350 fesettings->max_drift = 0;
351
352 return 0;
353}
354
355static int af9033_set_frontend(struct dvb_frontend *fe)
356{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300357 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200358 struct i2c_client *client = dev->client;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300359 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200360 int ret, i;
361 unsigned int utmp, adc_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300362 u8 tmp, buf[3], bandwidth_reg_val;
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200363 u32 if_frequency;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300364
Antti Palosaari81e19912016-11-06 18:55:36 -0200365 dev_dbg(&client->dev, "frequency=%u bandwidth_hz=%u\n",
366 c->frequency, c->bandwidth_hz);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300367
Antti Palosaari81e19912016-11-06 18:55:36 -0200368 /* Check bandwidth */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300369 switch (c->bandwidth_hz) {
370 case 6000000:
371 bandwidth_reg_val = 0x00;
372 break;
373 case 7000000:
374 bandwidth_reg_val = 0x01;
375 break;
376 case 8000000:
377 bandwidth_reg_val = 0x02;
378 break;
379 default:
Antti Palosaari81e19912016-11-06 18:55:36 -0200380 dev_dbg(&client->dev, "invalid bandwidth_hz\n");
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300381 ret = -EINVAL;
382 goto err;
383 }
384
Antti Palosaari81e19912016-11-06 18:55:36 -0200385 /* Program tuner */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300386 if (fe->ops.tuner_ops.set_params)
387 fe->ops.tuner_ops.set_params(fe);
388
Antti Palosaari81e19912016-11-06 18:55:36 -0200389 /* Coefficients */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300390 if (c->bandwidth_hz != dev->bandwidth_hz) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300391 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300392 if (coeff_lut[i].clock == dev->cfg.clock &&
Antti Palosaari81e19912016-11-06 18:55:36 -0200393 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300394 break;
395 }
396 }
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300397 if (i == ARRAY_SIZE(coeff_lut)) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200398 dev_err(&client->dev,
399 "Couldn't find config for clock %u\n",
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300400 dev->cfg.clock);
401 ret = -EINVAL;
402 goto err;
403 }
404
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200405 ret = regmap_bulk_write(dev->regmap, 0x800001, coeff_lut[i].val,
406 sizeof(coeff_lut[i].val));
407 if (ret)
408 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300409 }
410
Antti Palosaari81e19912016-11-06 18:55:36 -0200411 /* IF frequency control */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300412 if (c->bandwidth_hz != dev->bandwidth_hz) {
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300413 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300414 if (clock_adc_lut[i].clock == dev->cfg.clock)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300415 break;
416 }
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300417 if (i == ARRAY_SIZE(clock_adc_lut)) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200418 dev_err(&client->dev,
419 "Couldn't find ADC clock for clock %u\n",
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300420 dev->cfg.clock);
421 ret = -EINVAL;
422 goto err;
423 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300424 adc_freq = clock_adc_lut[i].adc;
425
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200426 if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
427 adc_freq = 2 * adc_freq;
428
Antti Palosaari81e19912016-11-06 18:55:36 -0200429 /* Get used IF frequency */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300430 if (fe->ops.tuner_ops.get_if_frequency)
431 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
432 else
433 if_frequency = 0;
434
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200435 utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x800000,
436 adc_freq);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300437
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200438 if (!dev->cfg.spec_inv && if_frequency)
439 utmp = 0x800000 - utmp;
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300440
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200441 buf[0] = (utmp >> 0) & 0xff;
442 buf[1] = (utmp >> 8) & 0xff;
443 buf[2] = (utmp >> 16) & 0xff;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200444 ret = regmap_bulk_write(dev->regmap, 0x800029, buf, 3);
445 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300446 goto err;
447
Antti Palosaari81e19912016-11-06 18:55:36 -0200448 dev_dbg(&client->dev, "if_frequency_cw=%06x\n", utmp);
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200449
Antti Palosaari09611ca2014-08-31 02:18:34 -0300450 dev->bandwidth_hz = c->bandwidth_hz;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300451 }
452
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200453 ret = regmap_update_bits(dev->regmap, 0x80f904, 0x03,
454 bandwidth_reg_val);
455 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300456 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200457 ret = regmap_write(dev->regmap, 0x800040, 0x00);
458 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300459 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200460 ret = regmap_write(dev->regmap, 0x800047, 0x00);
461 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300462 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200463 ret = regmap_update_bits(dev->regmap, 0x80f999, 0x01, 0x00);
464 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300465 goto err;
466
467 if (c->frequency <= 230000000)
468 tmp = 0x00; /* VHF */
469 else
470 tmp = 0x01; /* UHF */
471
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200472 ret = regmap_write(dev->regmap, 0x80004b, tmp);
473 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300474 goto err;
Antti Palosaari81e19912016-11-06 18:55:36 -0200475 /* Reset FSM */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200476 ret = regmap_write(dev->regmap, 0x800000, 0x00);
477 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300478 goto err;
479
480 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300481err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200482 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300483 return ret;
484}
485
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200486static int af9033_get_frontend(struct dvb_frontend *fe,
487 struct dtv_frontend_properties *c)
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300488{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300489 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200490 struct i2c_client *client = dev->client;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300491 int ret;
492 u8 buf[8];
493
Antti Palosaari81e19912016-11-06 18:55:36 -0200494 dev_dbg(&client->dev, "\n");
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300495
Antti Palosaari81e19912016-11-06 18:55:36 -0200496 /* Read all needed TPS registers */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200497 ret = regmap_bulk_read(dev->regmap, 0x80f900, buf, 8);
498 if (ret)
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300499 goto err;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300500
501 switch ((buf[0] >> 0) & 3) {
502 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300503 c->transmission_mode = TRANSMISSION_MODE_2K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300504 break;
505 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300506 c->transmission_mode = TRANSMISSION_MODE_8K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300507 break;
508 }
509
510 switch ((buf[1] >> 0) & 3) {
511 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300512 c->guard_interval = GUARD_INTERVAL_1_32;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300513 break;
514 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300515 c->guard_interval = GUARD_INTERVAL_1_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300516 break;
517 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300518 c->guard_interval = GUARD_INTERVAL_1_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300519 break;
520 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300521 c->guard_interval = GUARD_INTERVAL_1_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300522 break;
523 }
524
525 switch ((buf[2] >> 0) & 7) {
526 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300527 c->hierarchy = HIERARCHY_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300528 break;
529 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300530 c->hierarchy = HIERARCHY_1;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300531 break;
532 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300533 c->hierarchy = HIERARCHY_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300534 break;
535 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300536 c->hierarchy = HIERARCHY_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300537 break;
538 }
539
540 switch ((buf[3] >> 0) & 3) {
541 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300542 c->modulation = QPSK;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300543 break;
544 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300545 c->modulation = QAM_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300546 break;
547 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300548 c->modulation = QAM_64;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300549 break;
550 }
551
552 switch ((buf[4] >> 0) & 3) {
553 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300554 c->bandwidth_hz = 6000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300555 break;
556 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300557 c->bandwidth_hz = 7000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300558 break;
559 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300560 c->bandwidth_hz = 8000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300561 break;
562 }
563
564 switch ((buf[6] >> 0) & 7) {
565 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300566 c->code_rate_HP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300567 break;
568 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300569 c->code_rate_HP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300570 break;
571 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300572 c->code_rate_HP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300573 break;
574 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300575 c->code_rate_HP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300576 break;
577 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300578 c->code_rate_HP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300579 break;
580 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300581 c->code_rate_HP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300582 break;
583 }
584
585 switch ((buf[7] >> 0) & 7) {
586 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300587 c->code_rate_LP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300588 break;
589 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300590 c->code_rate_LP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300591 break;
592 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300593 c->code_rate_LP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300594 break;
595 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300596 c->code_rate_LP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300597 break;
598 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300599 c->code_rate_LP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300600 break;
601 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300602 c->code_rate_LP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300603 break;
604 }
605
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300606 return 0;
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300607err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200608 dev_dbg(&client->dev, "failed=%d\n", ret);
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300609 return ret;
610}
611
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300612static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300613{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300614 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200615 struct i2c_client *client = dev->client;
Antti Palosaari659a0992016-06-29 20:40:21 -0300616 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari239dd612016-12-19 01:42:37 -0200617 int ret, tmp = 0;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200618 u8 buf[7];
Antti Palosaari239dd612016-12-19 01:42:37 -0200619 unsigned int utmp, utmp1;
Antti Palosaari659a0992016-06-29 20:40:21 -0300620
Antti Palosaari81e19912016-11-06 18:55:36 -0200621 dev_dbg(&client->dev, "\n");
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300622
623 *status = 0;
624
Antti Palosaari81e19912016-11-06 18:55:36 -0200625 /* Radio channel status: 0=no result, 1=has signal, 2=no signal */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200626 ret = regmap_read(dev->regmap, 0x800047, &utmp);
627 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300628 goto err;
629
Antti Palosaari81e19912016-11-06 18:55:36 -0200630 /* Has signal */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200631 if (utmp == 0x01)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300632 *status |= FE_HAS_SIGNAL;
633
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200634 if (utmp != 0x02) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300635 /* TPS lock */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200636 ret = regmap_read(dev->regmap, 0x80f5a9, &utmp);
637 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300638 goto err;
639
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200640 if ((utmp >> 0) & 0x01)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300641 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
642 FE_HAS_VITERBI;
643
Antti Palosaari81e19912016-11-06 18:55:36 -0200644 /* Full lock */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200645 ret = regmap_read(dev->regmap, 0x80f999, &utmp);
646 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300647 goto err;
648
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200649 if ((utmp >> 0) & 0x01)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300650 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
651 FE_HAS_VITERBI | FE_HAS_SYNC |
652 FE_HAS_LOCK;
653 }
654
Antti Palosaari83f11612014-09-02 03:55:21 -0300655 dev->fe_status = *status;
656
Antti Palosaari81e19912016-11-06 18:55:36 -0200657 /* Signal strength */
Antti Palosaari659a0992016-06-29 20:40:21 -0300658 if (dev->fe_status & FE_HAS_SIGNAL) {
659 if (dev->is_af9035) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200660 ret = regmap_read(dev->regmap, 0x80004a, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300661 if (ret)
662 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200663 tmp = -utmp * 1000;
Antti Palosaari659a0992016-06-29 20:40:21 -0300664 } else {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200665 ret = regmap_read(dev->regmap, 0x8000f7, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300666 if (ret)
667 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200668 tmp = (utmp - 100) * 1000;
Antti Palosaari659a0992016-06-29 20:40:21 -0300669 }
670
671 c->strength.len = 1;
672 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
673 c->strength.stat[0].svalue = tmp;
674 } else {
675 c->strength.len = 1;
676 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
677 }
678
679 /* CNR */
680 if (dev->fe_status & FE_HAS_VITERBI) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200681 /* Read raw SNR value */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200682 ret = regmap_bulk_read(dev->regmap, 0x80002c, buf, 3);
Antti Palosaari659a0992016-06-29 20:40:21 -0300683 if (ret)
684 goto err;
685
Antti Palosaari239dd612016-12-19 01:42:37 -0200686 utmp1 = buf[2] << 16 | buf[1] << 8 | buf[0] << 0;
Antti Palosaari659a0992016-06-29 20:40:21 -0300687
Antti Palosaari81e19912016-11-06 18:55:36 -0200688 /* Read superframe number */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200689 ret = regmap_read(dev->regmap, 0x80f78b, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300690 if (ret)
691 goto err;
692
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200693 if (utmp)
Antti Palosaari239dd612016-12-19 01:42:37 -0200694 utmp1 /= utmp;
Antti Palosaari659a0992016-06-29 20:40:21 -0300695
Antti Palosaari81e19912016-11-06 18:55:36 -0200696 /* Read current transmission mode */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200697 ret = regmap_read(dev->regmap, 0x80f900, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300698 if (ret)
699 goto err;
700
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200701 switch ((utmp >> 0) & 3) {
Antti Palosaari659a0992016-06-29 20:40:21 -0300702 case 0:
Antti Palosaari239dd612016-12-19 01:42:37 -0200703 /* 2k */
704 utmp1 *= 4;
Antti Palosaari659a0992016-06-29 20:40:21 -0300705 break;
706 case 1:
Antti Palosaari239dd612016-12-19 01:42:37 -0200707 /* 8k */
708 utmp1 *= 1;
Antti Palosaari659a0992016-06-29 20:40:21 -0300709 break;
710 case 2:
Antti Palosaari239dd612016-12-19 01:42:37 -0200711 /* 4k */
712 utmp1 *= 2;
Antti Palosaari659a0992016-06-29 20:40:21 -0300713 break;
714 default:
Antti Palosaari239dd612016-12-19 01:42:37 -0200715 utmp1 *= 0;
Antti Palosaari659a0992016-06-29 20:40:21 -0300716 break;
717 }
718
Antti Palosaari81e19912016-11-06 18:55:36 -0200719 /* Read current modulation */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200720 ret = regmap_read(dev->regmap, 0x80f903, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300721 if (ret)
722 goto err;
723
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200724 switch ((utmp >> 0) & 3) {
Antti Palosaari659a0992016-06-29 20:40:21 -0300725 case 0:
Antti Palosaari239dd612016-12-19 01:42:37 -0200726 /*
727 * QPSK
728 * CNR[dB] 13 * -log10((1690000 - value) / value) + 2.6
729 * value [653799, 1689999], 2.6 / 13 = 3355443
730 */
731 utmp1 = clamp(utmp1, 653799U, 1689999U);
732 utmp1 = ((u64)(intlog10(utmp1)
733 - intlog10(1690000 - utmp1)
734 + 3355443) * 13 * 1000) >> 24;
Antti Palosaari659a0992016-06-29 20:40:21 -0300735 break;
736 case 1:
Antti Palosaari239dd612016-12-19 01:42:37 -0200737 /*
738 * QAM-16
739 * CNR[dB] 6 * log10((value - 370000) / (828000 - value)) + 15.7
740 * value [371105, 827999], 15.7 / 6 = 43900382
741 */
742 utmp1 = clamp(utmp1, 371105U, 827999U);
743 utmp1 = ((u64)(intlog10(utmp1 - 370000)
744 - intlog10(828000 - utmp1)
745 + 43900382) * 6 * 1000) >> 24;
Antti Palosaari659a0992016-06-29 20:40:21 -0300746 break;
747 case 2:
Antti Palosaari239dd612016-12-19 01:42:37 -0200748 /*
749 * QAM-64
750 * CNR[dB] 8 * log10((value - 193000) / (425000 - value)) + 23.8
751 * value [193246, 424999], 23.8 / 8 = 49912218
752 */
753 utmp1 = clamp(utmp1, 193246U, 424999U);
754 utmp1 = ((u64)(intlog10(utmp1 - 193000)
755 - intlog10(425000 - utmp1)
756 + 49912218) * 8 * 1000) >> 24;
Antti Palosaari659a0992016-06-29 20:40:21 -0300757 break;
758 default:
Antti Palosaari239dd612016-12-19 01:42:37 -0200759 utmp1 = 0;
Antti Palosaari659a0992016-06-29 20:40:21 -0300760 break;
761 }
762
Antti Palosaari239dd612016-12-19 01:42:37 -0200763 dev_dbg(&client->dev, "cnr=%u\n", utmp1);
Antti Palosaari659a0992016-06-29 20:40:21 -0300764
Antti Palosaari659a0992016-06-29 20:40:21 -0300765 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
Antti Palosaari239dd612016-12-19 01:42:37 -0200766 c->cnr.stat[0].svalue = utmp1;
Antti Palosaari659a0992016-06-29 20:40:21 -0300767 } else {
Antti Palosaari659a0992016-06-29 20:40:21 -0300768 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
769 }
770
771 /* UCB/PER/BER */
772 if (dev->fe_status & FE_HAS_LOCK) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200773 /* Outer FEC, 204 byte packets */
Antti Palosaari659a0992016-06-29 20:40:21 -0300774 u16 abort_packet_count, rsd_packet_count;
Antti Palosaari81e19912016-11-06 18:55:36 -0200775 /* Inner FEC, bits */
Antti Palosaari659a0992016-06-29 20:40:21 -0300776 u32 rsd_bit_err_count;
777
778 /*
779 * Packet count used for measurement is 10000
780 * (rsd_packet_count). Maybe it should be increased?
781 */
782
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200783 ret = regmap_bulk_read(dev->regmap, 0x800032, buf, 7);
Antti Palosaari659a0992016-06-29 20:40:21 -0300784 if (ret)
785 goto err;
786
787 abort_packet_count = (buf[1] << 8) | (buf[0] << 0);
788 rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2];
789 rsd_packet_count = (buf[6] << 8) | (buf[5] << 0);
790
791 dev->error_block_count += abort_packet_count;
792 dev->total_block_count += rsd_packet_count;
793 dev->post_bit_error += rsd_bit_err_count;
794 dev->post_bit_count += rsd_packet_count * 204 * 8;
795
796 c->block_count.len = 1;
797 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
798 c->block_count.stat[0].uvalue = dev->total_block_count;
799
800 c->block_error.len = 1;
801 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
802 c->block_error.stat[0].uvalue = dev->error_block_count;
803
804 c->post_bit_count.len = 1;
805 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
806 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
807
808 c->post_bit_error.len = 1;
809 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
810 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
811 }
812
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300813 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300814err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200815 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300816 return ret;
817}
818
819static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
820{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300821 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200822 struct i2c_client *client = dev->client;
Antti Palosaari6b457782014-09-02 05:03:21 -0300823 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300824 int ret;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200825 unsigned int utmp;
Antti Palosaarie898ef62012-04-01 12:50:02 -0300826
Antti Palosaari81e19912016-11-06 18:55:36 -0200827 dev_dbg(&client->dev, "\n");
828
829 /* Use DVBv5 CNR */
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300830 if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) {
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300831 /* Return 0.1 dB for AF9030 and 0-0xffff for IT9130. */
832 if (dev->is_af9035) {
833 /* 1000x => 10x (0.1 dB) */
834 *snr = div_s64(c->cnr.stat[0].svalue, 100);
835 } else {
836 /* 1000x => 1x (1 dB) */
837 *snr = div_s64(c->cnr.stat[0].svalue, 1000);
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300838
Antti Palosaari81e19912016-11-06 18:55:36 -0200839 /* Read current modulation */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200840 ret = regmap_read(dev->regmap, 0x80f903, &utmp);
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300841 if (ret)
842 goto err;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300843
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300844 /* scale value to 0x0000-0xffff */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200845 switch ((utmp >> 0) & 3) {
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300846 case 0:
847 *snr = *snr * 0xffff / 23;
848 break;
849 case 1:
850 *snr = *snr * 0xffff / 26;
851 break;
852 case 2:
853 *snr = *snr * 0xffff / 32;
854 break;
855 default:
Hans Verkuile1219932021-01-20 10:09:05 +0100856 ret = -EINVAL;
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300857 goto err;
858 }
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300859 }
860 } else {
Antti Palosaari6b457782014-09-02 05:03:21 -0300861 *snr = 0;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300862 }
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300863
864 return 0;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300865err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200866 dev_dbg(&client->dev, "failed=%d\n", ret);
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300867 return ret;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300868}
869
870static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
871{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300872 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200873 struct i2c_client *client = dev->client;
Bimow Chen3adec272014-10-01 07:28:54 -0300874 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
875 int ret, tmp, power_real;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200876 unsigned int utmp;
877 u8 gain_offset, buf[7];
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300878
Antti Palosaari81e19912016-11-06 18:55:36 -0200879 dev_dbg(&client->dev, "\n");
880
Bimow Chen3adec272014-10-01 07:28:54 -0300881 if (dev->is_af9035) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200882 /* Read signal strength of 0-100 scale */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200883 ret = regmap_read(dev->regmap, 0x800048, &utmp);
884 if (ret)
Antti Palosaari0b0d9622014-11-03 20:24:13 -0300885 goto err;
886
Antti Palosaari81e19912016-11-06 18:55:36 -0200887 /* Scale value to 0x0000-0xffff */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200888 *strength = utmp * 0xffff / 100;
Bimow Chen3adec272014-10-01 07:28:54 -0300889 } else {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200890 ret = regmap_read(dev->regmap, 0x8000f7, &utmp);
891 if (ret)
Antti Palosaari1620d222014-11-03 21:23:26 -0300892 goto err;
893
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200894 ret = regmap_bulk_read(dev->regmap, 0x80f900, buf, 7);
895 if (ret)
Antti Palosaari1620d222014-11-03 21:23:26 -0300896 goto err;
Bimow Chen3adec272014-10-01 07:28:54 -0300897
898 if (c->frequency <= 300000000)
899 gain_offset = 7; /* VHF */
900 else
901 gain_offset = 4; /* UHF */
902
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200903 power_real = (utmp - 100 - gain_offset) -
Bimow Chen3adec272014-10-01 07:28:54 -0300904 power_reference[((buf[3] >> 0) & 3)][((buf[6] >> 0) & 7)];
905
906 if (power_real < -15)
907 tmp = 0;
908 else if ((power_real >= -15) && (power_real < 0))
909 tmp = (2 * (power_real + 15)) / 3;
910 else if ((power_real >= 0) && (power_real < 20))
911 tmp = 4 * power_real + 10;
912 else if ((power_real >= 20) && (power_real < 35))
913 tmp = (2 * (power_real - 20)) / 3 + 90;
914 else
915 tmp = 100;
916
Antti Palosaari81e19912016-11-06 18:55:36 -0200917 /* Scale value to 0x0000-0xffff */
Bimow Chen3adec272014-10-01 07:28:54 -0300918 *strength = tmp * 0xffff / 100;
919 }
920
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300921 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300922err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200923 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300924 return ret;
925}
926
927static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
928{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300929 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300930
Antti Palosaarie53c4742014-09-03 23:22:53 -0300931 *ber = (dev->post_bit_error - dev->post_bit_error_prev);
932 dev->post_bit_error_prev = dev->post_bit_error;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300933
934 return 0;
935}
936
937static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
938{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300939 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300940
Antti Palosaari1d0ceae2014-09-03 23:07:39 -0300941 *ucblocks = dev->error_block_count;
Antti Palosaari81e19912016-11-06 18:55:36 -0200942
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300943 return 0;
944}
945
946static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
947{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300948 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200949 struct i2c_client *client = dev->client;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300950 int ret;
951
Antti Palosaari81e19912016-11-06 18:55:36 -0200952 dev_dbg(&client->dev, "enable=%d\n", enable);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300953
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200954 ret = regmap_update_bits(dev->regmap, 0x00fa04, 0x01, enable);
955 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300956 goto err;
957
958 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300959err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200960 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300961 return ret;
962}
963
Mauro Carvalho Chehabed97a6f2014-03-14 14:29:06 -0300964static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -0300965{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300966 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200967 struct i2c_client *client = dev->client;
Antti Palosaari040cf862014-02-13 15:40:59 -0300968 int ret;
969
Antti Palosaari81e19912016-11-06 18:55:36 -0200970 dev_dbg(&client->dev, "onoff=%d\n", onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300971
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200972 ret = regmap_update_bits(dev->regmap, 0x80f993, 0x01, onoff);
973 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -0300974 goto err;
975
976 return 0;
Antti Palosaari040cf862014-02-13 15:40:59 -0300977err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200978 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari040cf862014-02-13 15:40:59 -0300979 return ret;
980}
Antti Palosaari040cf862014-02-13 15:40:59 -0300981
Antti Palosaari24e419a2014-08-09 14:59:53 -0300982static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
Antti Palosaari81e19912016-11-06 18:55:36 -0200983 int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -0300984{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300985 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200986 struct i2c_client *client = dev->client;
Antti Palosaari040cf862014-02-13 15:40:59 -0300987 int ret;
988 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
989
Antti Palosaari81e19912016-11-06 18:55:36 -0200990 dev_dbg(&client->dev, "index=%d pid=%04x onoff=%d\n",
991 index, pid, onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300992
993 if (pid > 0x1fff)
994 return 0;
995
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200996 ret = regmap_bulk_write(dev->regmap, 0x80f996, wbuf, 2);
997 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -0300998 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200999 ret = regmap_write(dev->regmap, 0x80f994, onoff);
1000 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -03001001 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001002 ret = regmap_write(dev->regmap, 0x80f995, index);
1003 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -03001004 goto err;
1005
1006 return 0;
Antti Palosaari040cf862014-02-13 15:40:59 -03001007err:
Antti Palosaari81e19912016-11-06 18:55:36 -02001008 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari040cf862014-02-13 15:40:59 -03001009 return ret;
1010}
Antti Palosaari040cf862014-02-13 15:40:59 -03001011
Max Kellermannbd336e62016-08-09 18:32:21 -03001012static const struct dvb_frontend_ops af9033_ops = {
Antti Palosaari81e19912016-11-06 18:55:36 -02001013 .delsys = {SYS_DVBT},
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001014 .info = {
1015 .name = "Afatech AF9033 (DVB-T)",
Mauro Carvalho Chehabf1b1eab2018-07-05 18:59:36 -04001016 .frequency_min_hz = 174 * MHz,
1017 .frequency_max_hz = 862 * MHz,
1018 .frequency_stepsize_hz = 250 * kHz,
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001019 .caps = FE_CAN_FEC_1_2 |
1020 FE_CAN_FEC_2_3 |
1021 FE_CAN_FEC_3_4 |
1022 FE_CAN_FEC_5_6 |
1023 FE_CAN_FEC_7_8 |
1024 FE_CAN_FEC_AUTO |
1025 FE_CAN_QPSK |
1026 FE_CAN_QAM_16 |
1027 FE_CAN_QAM_64 |
1028 FE_CAN_QAM_AUTO |
1029 FE_CAN_TRANSMISSION_MODE_AUTO |
1030 FE_CAN_GUARD_INTERVAL_AUTO |
1031 FE_CAN_HIERARCHY_AUTO |
1032 FE_CAN_RECOVER |
1033 FE_CAN_MUTE_TS
1034 },
1035
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001036 .init = af9033_init,
1037 .sleep = af9033_sleep,
1038
1039 .get_tune_settings = af9033_get_tune_settings,
1040 .set_frontend = af9033_set_frontend,
Gianluca Gennari0a4df232012-04-05 12:47:19 -03001041 .get_frontend = af9033_get_frontend,
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001042
1043 .read_status = af9033_read_status,
1044 .read_snr = af9033_read_snr,
1045 .read_signal_strength = af9033_read_signal_strength,
1046 .read_ber = af9033_read_ber,
1047 .read_ucblocks = af9033_read_ucblocks,
1048
1049 .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
1050};
1051
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001052static int af9033_probe(struct i2c_client *client,
Antti Palosaari81e19912016-11-06 18:55:36 -02001053 const struct i2c_device_id *id)
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001054{
1055 struct af9033_config *cfg = client->dev.platform_data;
1056 struct af9033_dev *dev;
1057 int ret;
1058 u8 buf[8];
1059 u32 reg;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001060 static const struct regmap_config regmap_config = {
1061 .reg_bits = 24,
1062 .val_bits = 8,
1063 };
1064
Antti Palosaari81e19912016-11-06 18:55:36 -02001065 /* Allocate memory for the internal state */
1066 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1067 if (!dev) {
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001068 ret = -ENOMEM;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001069 goto err;
1070 }
1071
Antti Palosaari81e19912016-11-06 18:55:36 -02001072 /* Setup the state */
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001073 dev->client = client;
Antti Palosaari81e19912016-11-06 18:55:36 -02001074 memcpy(&dev->cfg, cfg, sizeof(dev->cfg));
1075 switch (dev->cfg.ts_mode) {
1076 case AF9033_TS_MODE_PARALLEL:
1077 dev->ts_mode_parallel = true;
1078 break;
1079 case AF9033_TS_MODE_SERIAL:
1080 dev->ts_mode_serial = true;
1081 break;
1082 case AF9033_TS_MODE_USB:
1083 /* USB mode for AF9035 */
1084 default:
1085 break;
1086 }
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001087
1088 if (dev->cfg.clock != 12000000) {
1089 ret = -ENODEV;
Antti Palosaari81e19912016-11-06 18:55:36 -02001090 dev_err(&client->dev,
1091 "Unsupported clock %u Hz. Only 12000000 Hz is supported currently\n",
1092 dev->cfg.clock);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001093 goto err_kfree;
1094 }
1095
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001096 /* Create regmap */
1097 dev->regmap = regmap_init_i2c(client, &regmap_config);
1098 if (IS_ERR(dev->regmap)) {
1099 ret = PTR_ERR(dev->regmap);
1100 goto err_kfree;
1101 }
1102
Antti Palosaari81e19912016-11-06 18:55:36 -02001103 /* Firmware version */
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001104 switch (dev->cfg.tuner) {
1105 case AF9033_TUNER_IT9135_38:
1106 case AF9033_TUNER_IT9135_51:
1107 case AF9033_TUNER_IT9135_52:
1108 case AF9033_TUNER_IT9135_60:
1109 case AF9033_TUNER_IT9135_61:
1110 case AF9033_TUNER_IT9135_62:
Antti Palosaari83f11612014-09-02 03:55:21 -03001111 dev->is_it9135 = true;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001112 reg = 0x004bfc;
1113 break;
1114 default:
Antti Palosaari83f11612014-09-02 03:55:21 -03001115 dev->is_af9035 = true;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001116 reg = 0x0083e9;
1117 break;
1118 }
1119
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001120 ret = regmap_bulk_read(dev->regmap, reg, &buf[0], 4);
1121 if (ret)
1122 goto err_regmap_exit;
1123 ret = regmap_bulk_read(dev->regmap, 0x804191, &buf[4], 4);
1124 if (ret)
1125 goto err_regmap_exit;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001126
Antti Palosaari81e19912016-11-06 18:55:36 -02001127 dev_info(&client->dev,
1128 "firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n",
1129 buf[0], buf[1], buf[2], buf[3],
1130 buf[4], buf[5], buf[6], buf[7]);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001131
Antti Palosaari81e19912016-11-06 18:55:36 -02001132 /* Sleep as chip seems to be partly active by default */
Victor Toso09a446d2018-10-30 12:14:48 -04001133 /* IT9135 did not like to sleep at that early */
1134 if (dev->is_af9035) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001135 ret = regmap_write(dev->regmap, 0x80004c, 0x01);
1136 if (ret)
1137 goto err_regmap_exit;
1138 ret = regmap_write(dev->regmap, 0x800000, 0x00);
1139 if (ret)
1140 goto err_regmap_exit;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001141 }
1142
Antti Palosaari81e19912016-11-06 18:55:36 -02001143 /* Create dvb frontend */
1144 memcpy(&dev->fe.ops, &af9033_ops, sizeof(dev->fe.ops));
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001145 dev->fe.demodulator_priv = dev;
1146 *cfg->fe = &dev->fe;
1147 if (cfg->ops) {
1148 cfg->ops->pid_filter = af9033_pid_filter;
1149 cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl;
1150 }
Antti Palosaari34618312016-11-06 22:14:18 -02001151 cfg->regmap = dev->regmap;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001152 i2c_set_clientdata(client, dev);
1153
Antti Palosaari81e19912016-11-06 18:55:36 -02001154 dev_info(&client->dev, "Afatech AF9033 successfully attached\n");
1155
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001156 return 0;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001157err_regmap_exit:
1158 regmap_exit(dev->regmap);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001159err_kfree:
1160 kfree(dev);
1161err:
Antti Palosaari6a087f12014-08-31 21:08:09 -03001162 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001163 return ret;
1164}
1165
1166static int af9033_remove(struct i2c_client *client)
1167{
1168 struct af9033_dev *dev = i2c_get_clientdata(client);
1169
Antti Palosaari81e19912016-11-06 18:55:36 -02001170 dev_dbg(&client->dev, "\n");
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001171
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001172 regmap_exit(dev->regmap);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001173 kfree(dev);
1174
1175 return 0;
1176}
1177
1178static const struct i2c_device_id af9033_id_table[] = {
1179 {"af9033", 0},
1180 {}
1181};
1182MODULE_DEVICE_TABLE(i2c, af9033_id_table);
1183
1184static struct i2c_driver af9033_driver = {
1185 .driver = {
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001186 .name = "af9033",
Antti Palosaari72812172016-06-29 20:40:22 -03001187 .suppress_bind_attrs = true,
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001188 },
1189 .probe = af9033_probe,
1190 .remove = af9033_remove,
1191 .id_table = af9033_id_table,
1192};
1193
1194module_i2c_driver(af9033_driver);
1195
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001196MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1197MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
1198MODULE_LICENSE("GPL");