blob: 6a8d78b6edaceb5c2452bbecf0f11fa3ffe66849 [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);
128 goto err;
129 }
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300130
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200131 utmp = div_u64((u64)clock_adc_lut[i].adc * 0x80000, 1000000);
132 buf[0] = (utmp >> 0) & 0xff;
133 buf[1] = (utmp >> 8) & 0xff;
134 buf[2] = (utmp >> 16) & 0xff;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200135 ret = regmap_bulk_write(dev->regmap, 0x80f1cd, buf, 3);
136 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300137 goto err;
138
Antti Palosaari81e19912016-11-06 18:55:36 -0200139 dev_dbg(&client->dev, "adc=%u adc_cw=%06x\n",
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200140 clock_adc_lut[i].adc, utmp);
141
Antti Palosaari81e19912016-11-06 18:55:36 -0200142 /* Config register table */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300143 for (i = 0; i < ARRAY_SIZE(tab); i++) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200144 ret = regmap_update_bits(dev->regmap, tab[i].reg, tab[i].mask,
145 tab[i].val);
146 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300147 goto err;
148 }
149
Antti Palosaari81e19912016-11-06 18:55:36 -0200150 /* Demod clk output */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300151 if (dev->cfg.dyn0_clk) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200152 ret = regmap_write(dev->regmap, 0x80fba8, 0x00);
153 if (ret)
Antti Palosaari9dc0f3f2014-08-09 01:03:14 -0300154 goto err;
155 }
156
Antti Palosaari81e19912016-11-06 18:55:36 -0200157 /* TS interface */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300158 if (dev->cfg.ts_mode == AF9033_TS_MODE_USB) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200159 ret = regmap_update_bits(dev->regmap, 0x80f9a5, 0x01, 0x00);
160 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300161 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200162 ret = regmap_update_bits(dev->regmap, 0x80f9b5, 0x01, 0x01);
163 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300164 goto err;
165 } else {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200166 ret = regmap_update_bits(dev->regmap, 0x80f990, 0x01, 0x00);
167 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300168 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200169 ret = regmap_update_bits(dev->regmap, 0x80f9b5, 0x01, 0x00);
170 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300171 goto err;
172 }
173
Antti Palosaari81e19912016-11-06 18:55:36 -0200174 /* Demod core settings */
175 dev_dbg(&client->dev, "load ofsm settings\n");
Antti Palosaari09611ca2014-08-31 02:18:34 -0300176 switch (dev->cfg.tuner) {
Antti Palosaarife8eece2013-02-03 13:39:55 -0300177 case AF9033_TUNER_IT9135_38:
178 case AF9033_TUNER_IT9135_51:
179 case AF9033_TUNER_IT9135_52:
Antti Palosaari463c3992013-03-07 00:03:06 -0300180 len = ARRAY_SIZE(ofsm_init_it9135_v1);
181 init = ofsm_init_it9135_v1;
182 break;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300183 case AF9033_TUNER_IT9135_60:
184 case AF9033_TUNER_IT9135_61:
185 case AF9033_TUNER_IT9135_62:
Antti Palosaari463c3992013-03-07 00:03:06 -0300186 len = ARRAY_SIZE(ofsm_init_it9135_v2);
187 init = ofsm_init_it9135_v2;
Antti Palosaarife8eece2013-02-03 13:39:55 -0300188 break;
189 default:
190 len = ARRAY_SIZE(ofsm_init);
191 init = ofsm_init;
192 break;
193 }
194
Antti Palosaari09611ca2014-08-31 02:18:34 -0300195 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200196 if (ret)
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300197 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300198
Antti Palosaari81e19912016-11-06 18:55:36 -0200199 /* Demod tuner specific settings */
200 dev_dbg(&client->dev, "load tuner specific settings\n");
Antti Palosaari09611ca2014-08-31 02:18:34 -0300201 switch (dev->cfg.tuner) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300202 case AF9033_TUNER_TUA9001:
203 len = ARRAY_SIZE(tuner_init_tua9001);
204 init = tuner_init_tua9001;
205 break;
Michael Büschffc501f2012-04-02 12:18:36 -0300206 case AF9033_TUNER_FC0011:
207 len = ARRAY_SIZE(tuner_init_fc0011);
208 init = tuner_init_fc0011;
209 break;
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300210 case AF9033_TUNER_MXL5007T:
211 len = ARRAY_SIZE(tuner_init_mxl5007t);
212 init = tuner_init_mxl5007t;
213 break;
Gianluca Gennarice1fe372012-04-02 17:25:14 -0300214 case AF9033_TUNER_TDA18218:
215 len = ARRAY_SIZE(tuner_init_tda18218);
216 init = tuner_init_tda18218;
217 break;
Oliver Schinagld67ceb32012-09-20 14:57:17 -0300218 case AF9033_TUNER_FC2580:
219 len = ARRAY_SIZE(tuner_init_fc2580);
220 init = tuner_init_fc2580;
221 break;
Antti Palosaarie713ad12012-12-02 18:47:00 -0300222 case AF9033_TUNER_FC0012:
223 len = ARRAY_SIZE(tuner_init_fc0012);
224 init = tuner_init_fc0012;
225 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300226 case AF9033_TUNER_IT9135_38:
Antti Palosaaria72cbb72013-03-07 21:17:55 -0300227 len = ARRAY_SIZE(tuner_init_it9135_38);
228 init = tuner_init_it9135_38;
229 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300230 case AF9033_TUNER_IT9135_51:
Antti Palosaaribb2e12a2013-03-07 21:25:16 -0300231 len = ARRAY_SIZE(tuner_init_it9135_51);
232 init = tuner_init_it9135_51;
233 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300234 case AF9033_TUNER_IT9135_52:
Antti Palosaari22d729f2013-03-07 21:29:05 -0300235 len = ARRAY_SIZE(tuner_init_it9135_52);
236 init = tuner_init_it9135_52;
237 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300238 case AF9033_TUNER_IT9135_60:
Antti Palosaaria49f53a2013-03-07 21:38:00 -0300239 len = ARRAY_SIZE(tuner_init_it9135_60);
240 init = tuner_init_it9135_60;
241 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300242 case AF9033_TUNER_IT9135_61:
Antti Palosaari85211322013-03-07 21:44:30 -0300243 len = ARRAY_SIZE(tuner_init_it9135_61);
244 init = tuner_init_it9135_61;
245 break;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300246 case AF9033_TUNER_IT9135_62:
Antti Palosaaridc4a2c42013-03-07 21:47:20 -0300247 len = ARRAY_SIZE(tuner_init_it9135_62);
248 init = tuner_init_it9135_62;
Antti Palosaari4902bb32013-01-07 09:48:03 -0300249 break;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300250 default:
Antti Palosaari81e19912016-11-06 18:55:36 -0200251 dev_dbg(&client->dev, "unsupported tuner ID=%d\n",
252 dev->cfg.tuner);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300253 ret = -ENODEV;
254 goto err;
255 }
256
Antti Palosaari09611ca2014-08-31 02:18:34 -0300257 ret = af9033_wr_reg_val_tab(dev, init, len);
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200258 if (ret)
Antti Palosaari3bf5e552013-03-08 16:54:09 -0300259 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300260
Antti Palosaari09611ca2014-08-31 02:18:34 -0300261 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200262 ret = regmap_update_bits(dev->regmap, 0x00d91c, 0x01, 0x01);
263 if (ret)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300264 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200265 ret = regmap_update_bits(dev->regmap, 0x00d917, 0x01, 0x00);
266 if (ret)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300267 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200268 ret = regmap_update_bits(dev->regmap, 0x00d916, 0x01, 0x00);
269 if (ret)
Jose Alberto Reguero98059922012-09-23 16:48:47 -0300270 goto err;
271 }
272
Antti Palosaari09611ca2014-08-31 02:18:34 -0300273 switch (dev->cfg.tuner) {
Antti Palosaari086991d2013-03-07 22:18:56 -0300274 case AF9033_TUNER_IT9135_60:
275 case AF9033_TUNER_IT9135_61:
276 case AF9033_TUNER_IT9135_62:
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200277 ret = regmap_write(dev->regmap, 0x800000, 0x01);
278 if (ret)
Antti Palosaari086991d2013-03-07 22:18:56 -0300279 goto err;
280 }
281
Antti Palosaari81e19912016-11-06 18:55:36 -0200282 dev->bandwidth_hz = 0; /* Force to program all parameters */
283 /* Init stats here in order signal app which stats are supported */
Antti Palosaari2db4d172014-09-06 11:24:14 -0300284 c->strength.len = 1;
285 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
286 c->cnr.len = 1;
287 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
288 c->block_count.len = 1;
289 c->block_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
290 c->block_error.len = 1;
291 c->block_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
292 c->post_bit_count.len = 1;
293 c->post_bit_count.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
294 c->post_bit_error.len = 1;
295 c->post_bit_error.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300296
297 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300298err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200299 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300300 return ret;
301}
302
303static int af9033_sleep(struct dvb_frontend *fe)
304{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300305 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200306 struct i2c_client *client = dev->client;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200307 int ret;
308 unsigned int utmp;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300309
Antti Palosaari81e19912016-11-06 18:55:36 -0200310 dev_dbg(&client->dev, "\n");
311
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200312 ret = regmap_write(dev->regmap, 0x80004c, 0x01);
313 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300314 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200315 ret = regmap_write(dev->regmap, 0x800000, 0x00);
316 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300317 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200318 ret = regmap_read_poll_timeout(dev->regmap, 0x80004c, utmp, utmp == 0,
319 5000, 1000000);
320 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300321 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200322 ret = regmap_update_bits(dev->regmap, 0x80fb24, 0x08, 0x08);
323 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300324 goto err;
325
Antti Palosaari81e19912016-11-06 18:55:36 -0200326 /* Prevent current leak by setting TS interface to parallel mode */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300327 if (dev->cfg.ts_mode == AF9033_TS_MODE_SERIAL) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200328 /* Enable parallel TS */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200329 ret = regmap_update_bits(dev->regmap, 0x00d917, 0x01, 0x00);
330 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300331 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200332 ret = regmap_update_bits(dev->regmap, 0x00d916, 0x01, 0x01);
333 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300334 goto err;
335 }
336
337 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300338err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200339 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300340 return ret;
341}
342
343static int af9033_get_tune_settings(struct dvb_frontend *fe,
Antti Palosaari81e19912016-11-06 18:55:36 -0200344 struct dvb_frontend_tune_settings *fesettings)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300345{
Antti Palosaarife8eece2013-02-03 13:39:55 -0300346 /* 800 => 2000 because IT9135 v2 is slow to gain lock */
347 fesettings->min_delay_ms = 2000;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300348 fesettings->step_size = 0;
349 fesettings->max_drift = 0;
350
351 return 0;
352}
353
354static int af9033_set_frontend(struct dvb_frontend *fe)
355{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300356 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200357 struct i2c_client *client = dev->client;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300358 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200359 int ret, i;
360 unsigned int utmp, adc_freq;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300361 u8 tmp, buf[3], bandwidth_reg_val;
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200362 u32 if_frequency;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300363
Antti Palosaari81e19912016-11-06 18:55:36 -0200364 dev_dbg(&client->dev, "frequency=%u bandwidth_hz=%u\n",
365 c->frequency, c->bandwidth_hz);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300366
Antti Palosaari81e19912016-11-06 18:55:36 -0200367 /* Check bandwidth */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300368 switch (c->bandwidth_hz) {
369 case 6000000:
370 bandwidth_reg_val = 0x00;
371 break;
372 case 7000000:
373 bandwidth_reg_val = 0x01;
374 break;
375 case 8000000:
376 bandwidth_reg_val = 0x02;
377 break;
378 default:
Antti Palosaari81e19912016-11-06 18:55:36 -0200379 dev_dbg(&client->dev, "invalid bandwidth_hz\n");
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300380 ret = -EINVAL;
381 goto err;
382 }
383
Antti Palosaari81e19912016-11-06 18:55:36 -0200384 /* Program tuner */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300385 if (fe->ops.tuner_ops.set_params)
386 fe->ops.tuner_ops.set_params(fe);
387
Antti Palosaari81e19912016-11-06 18:55:36 -0200388 /* Coefficients */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300389 if (c->bandwidth_hz != dev->bandwidth_hz) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300390 for (i = 0; i < ARRAY_SIZE(coeff_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300391 if (coeff_lut[i].clock == dev->cfg.clock &&
Antti Palosaari81e19912016-11-06 18:55:36 -0200392 coeff_lut[i].bandwidth_hz == c->bandwidth_hz) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300393 break;
394 }
395 }
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300396 if (i == ARRAY_SIZE(coeff_lut)) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200397 dev_err(&client->dev,
398 "Couldn't find config for clock %u\n",
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300399 dev->cfg.clock);
400 ret = -EINVAL;
401 goto err;
402 }
403
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200404 ret = regmap_bulk_write(dev->regmap, 0x800001, coeff_lut[i].val,
405 sizeof(coeff_lut[i].val));
406 if (ret)
407 goto err;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300408 }
409
Antti Palosaari81e19912016-11-06 18:55:36 -0200410 /* IF frequency control */
Antti Palosaari09611ca2014-08-31 02:18:34 -0300411 if (c->bandwidth_hz != dev->bandwidth_hz) {
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300412 for (i = 0; i < ARRAY_SIZE(clock_adc_lut); i++) {
Antti Palosaari09611ca2014-08-31 02:18:34 -0300413 if (clock_adc_lut[i].clock == dev->cfg.clock)
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300414 break;
415 }
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300416 if (i == ARRAY_SIZE(clock_adc_lut)) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200417 dev_err(&client->dev,
418 "Couldn't find ADC clock for clock %u\n",
Mauro Carvalho Chehab060f79d2014-11-04 17:35:07 -0300419 dev->cfg.clock);
420 ret = -EINVAL;
421 goto err;
422 }
Hans-Frieder Vogt540fd4b2012-04-02 14:18:16 -0300423 adc_freq = clock_adc_lut[i].adc;
424
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200425 if (dev->cfg.adc_multiplier == AF9033_ADC_MULTIPLIER_2X)
426 adc_freq = 2 * adc_freq;
427
Antti Palosaari81e19912016-11-06 18:55:36 -0200428 /* Get used IF frequency */
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300429 if (fe->ops.tuner_ops.get_if_frequency)
430 fe->ops.tuner_ops.get_if_frequency(fe, &if_frequency);
431 else
432 if_frequency = 0;
433
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200434 utmp = DIV_ROUND_CLOSEST_ULL((u64)if_frequency * 0x800000,
435 adc_freq);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300436
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200437 if (!dev->cfg.spec_inv && if_frequency)
438 utmp = 0x800000 - utmp;
Hans-Frieder Vogt182b9672012-10-03 05:25:40 -0300439
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200440 buf[0] = (utmp >> 0) & 0xff;
441 buf[1] = (utmp >> 8) & 0xff;
442 buf[2] = (utmp >> 16) & 0xff;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200443 ret = regmap_bulk_write(dev->regmap, 0x800029, buf, 3);
444 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300445 goto err;
446
Antti Palosaari81e19912016-11-06 18:55:36 -0200447 dev_dbg(&client->dev, "if_frequency_cw=%06x\n", utmp);
Antti Palosaaric2feb9f2016-11-06 15:27:16 -0200448
Antti Palosaari09611ca2014-08-31 02:18:34 -0300449 dev->bandwidth_hz = c->bandwidth_hz;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300450 }
451
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200452 ret = regmap_update_bits(dev->regmap, 0x80f904, 0x03,
453 bandwidth_reg_val);
454 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300455 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200456 ret = regmap_write(dev->regmap, 0x800040, 0x00);
457 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300458 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200459 ret = regmap_write(dev->regmap, 0x800047, 0x00);
460 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300461 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200462 ret = regmap_update_bits(dev->regmap, 0x80f999, 0x01, 0x00);
463 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300464 goto err;
465
466 if (c->frequency <= 230000000)
467 tmp = 0x00; /* VHF */
468 else
469 tmp = 0x01; /* UHF */
470
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200471 ret = regmap_write(dev->regmap, 0x80004b, tmp);
472 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300473 goto err;
Antti Palosaari81e19912016-11-06 18:55:36 -0200474 /* Reset FSM */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200475 ret = regmap_write(dev->regmap, 0x800000, 0x00);
476 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300477 goto err;
478
479 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300480err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200481 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300482 return ret;
483}
484
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200485static int af9033_get_frontend(struct dvb_frontend *fe,
486 struct dtv_frontend_properties *c)
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300487{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300488 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200489 struct i2c_client *client = dev->client;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300490 int ret;
491 u8 buf[8];
492
Antti Palosaari81e19912016-11-06 18:55:36 -0200493 dev_dbg(&client->dev, "\n");
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300494
Antti Palosaari81e19912016-11-06 18:55:36 -0200495 /* Read all needed TPS registers */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200496 ret = regmap_bulk_read(dev->regmap, 0x80f900, buf, 8);
497 if (ret)
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300498 goto err;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300499
500 switch ((buf[0] >> 0) & 3) {
501 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300502 c->transmission_mode = TRANSMISSION_MODE_2K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300503 break;
504 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300505 c->transmission_mode = TRANSMISSION_MODE_8K;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300506 break;
507 }
508
509 switch ((buf[1] >> 0) & 3) {
510 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300511 c->guard_interval = GUARD_INTERVAL_1_32;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300512 break;
513 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300514 c->guard_interval = GUARD_INTERVAL_1_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300515 break;
516 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300517 c->guard_interval = GUARD_INTERVAL_1_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300518 break;
519 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300520 c->guard_interval = GUARD_INTERVAL_1_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300521 break;
522 }
523
524 switch ((buf[2] >> 0) & 7) {
525 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300526 c->hierarchy = HIERARCHY_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300527 break;
528 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300529 c->hierarchy = HIERARCHY_1;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300530 break;
531 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300532 c->hierarchy = HIERARCHY_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300533 break;
534 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300535 c->hierarchy = HIERARCHY_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300536 break;
537 }
538
539 switch ((buf[3] >> 0) & 3) {
540 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300541 c->modulation = QPSK;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300542 break;
543 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300544 c->modulation = QAM_16;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300545 break;
546 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300547 c->modulation = QAM_64;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300548 break;
549 }
550
551 switch ((buf[4] >> 0) & 3) {
552 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300553 c->bandwidth_hz = 6000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300554 break;
555 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300556 c->bandwidth_hz = 7000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300557 break;
558 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300559 c->bandwidth_hz = 8000000;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300560 break;
561 }
562
563 switch ((buf[6] >> 0) & 7) {
564 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300565 c->code_rate_HP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300566 break;
567 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300568 c->code_rate_HP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300569 break;
570 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300571 c->code_rate_HP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300572 break;
573 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300574 c->code_rate_HP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300575 break;
576 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300577 c->code_rate_HP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300578 break;
579 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300580 c->code_rate_HP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300581 break;
582 }
583
584 switch ((buf[7] >> 0) & 7) {
585 case 0:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300586 c->code_rate_LP = FEC_1_2;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300587 break;
588 case 1:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300589 c->code_rate_LP = FEC_2_3;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300590 break;
591 case 2:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300592 c->code_rate_LP = FEC_3_4;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300593 break;
594 case 3:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300595 c->code_rate_LP = FEC_5_6;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300596 break;
597 case 4:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300598 c->code_rate_LP = FEC_7_8;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300599 break;
600 case 5:
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300601 c->code_rate_LP = FEC_NONE;
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300602 break;
603 }
604
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300605 return 0;
Antti Palosaaride7f14fc2012-04-05 21:14:32 -0300606err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200607 dev_dbg(&client->dev, "failed=%d\n", ret);
Gianluca Gennari0a4df232012-04-05 12:47:19 -0300608 return ret;
609}
610
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300611static int af9033_read_status(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300612{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300613 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200614 struct i2c_client *client = dev->client;
Antti Palosaari659a0992016-06-29 20:40:21 -0300615 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari239dd612016-12-19 01:42:37 -0200616 int ret, tmp = 0;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200617 u8 buf[7];
Antti Palosaari239dd612016-12-19 01:42:37 -0200618 unsigned int utmp, utmp1;
Antti Palosaari659a0992016-06-29 20:40:21 -0300619
Antti Palosaari81e19912016-11-06 18:55:36 -0200620 dev_dbg(&client->dev, "\n");
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300621
622 *status = 0;
623
Antti Palosaari81e19912016-11-06 18:55:36 -0200624 /* Radio channel status: 0=no result, 1=has signal, 2=no signal */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200625 ret = regmap_read(dev->regmap, 0x800047, &utmp);
626 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300627 goto err;
628
Antti Palosaari81e19912016-11-06 18:55:36 -0200629 /* Has signal */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200630 if (utmp == 0x01)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300631 *status |= FE_HAS_SIGNAL;
632
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200633 if (utmp != 0x02) {
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300634 /* TPS lock */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200635 ret = regmap_read(dev->regmap, 0x80f5a9, &utmp);
636 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300637 goto err;
638
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200639 if ((utmp >> 0) & 0x01)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300640 *status |= FE_HAS_SIGNAL | FE_HAS_CARRIER |
641 FE_HAS_VITERBI;
642
Antti Palosaari81e19912016-11-06 18:55:36 -0200643 /* Full lock */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200644 ret = regmap_read(dev->regmap, 0x80f999, &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 | FE_HAS_SYNC |
651 FE_HAS_LOCK;
652 }
653
Antti Palosaari83f11612014-09-02 03:55:21 -0300654 dev->fe_status = *status;
655
Antti Palosaari81e19912016-11-06 18:55:36 -0200656 /* Signal strength */
Antti Palosaari659a0992016-06-29 20:40:21 -0300657 if (dev->fe_status & FE_HAS_SIGNAL) {
658 if (dev->is_af9035) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200659 ret = regmap_read(dev->regmap, 0x80004a, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300660 if (ret)
661 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200662 tmp = -utmp * 1000;
Antti Palosaari659a0992016-06-29 20:40:21 -0300663 } else {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200664 ret = regmap_read(dev->regmap, 0x8000f7, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300665 if (ret)
666 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200667 tmp = (utmp - 100) * 1000;
Antti Palosaari659a0992016-06-29 20:40:21 -0300668 }
669
670 c->strength.len = 1;
671 c->strength.stat[0].scale = FE_SCALE_DECIBEL;
672 c->strength.stat[0].svalue = tmp;
673 } else {
674 c->strength.len = 1;
675 c->strength.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
676 }
677
678 /* CNR */
679 if (dev->fe_status & FE_HAS_VITERBI) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200680 /* Read raw SNR value */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200681 ret = regmap_bulk_read(dev->regmap, 0x80002c, buf, 3);
Antti Palosaari659a0992016-06-29 20:40:21 -0300682 if (ret)
683 goto err;
684
Antti Palosaari239dd612016-12-19 01:42:37 -0200685 utmp1 = buf[2] << 16 | buf[1] << 8 | buf[0] << 0;
Antti Palosaari659a0992016-06-29 20:40:21 -0300686
Antti Palosaari81e19912016-11-06 18:55:36 -0200687 /* Read superframe number */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200688 ret = regmap_read(dev->regmap, 0x80f78b, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300689 if (ret)
690 goto err;
691
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200692 if (utmp)
Antti Palosaari239dd612016-12-19 01:42:37 -0200693 utmp1 /= utmp;
Antti Palosaari659a0992016-06-29 20:40:21 -0300694
Antti Palosaari81e19912016-11-06 18:55:36 -0200695 /* Read current transmission mode */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200696 ret = regmap_read(dev->regmap, 0x80f900, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300697 if (ret)
698 goto err;
699
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200700 switch ((utmp >> 0) & 3) {
Antti Palosaari659a0992016-06-29 20:40:21 -0300701 case 0:
Antti Palosaari239dd612016-12-19 01:42:37 -0200702 /* 2k */
703 utmp1 *= 4;
Antti Palosaari659a0992016-06-29 20:40:21 -0300704 break;
705 case 1:
Antti Palosaari239dd612016-12-19 01:42:37 -0200706 /* 8k */
707 utmp1 *= 1;
Antti Palosaari659a0992016-06-29 20:40:21 -0300708 break;
709 case 2:
Antti Palosaari239dd612016-12-19 01:42:37 -0200710 /* 4k */
711 utmp1 *= 2;
Antti Palosaari659a0992016-06-29 20:40:21 -0300712 break;
713 default:
Antti Palosaari239dd612016-12-19 01:42:37 -0200714 utmp1 *= 0;
Antti Palosaari659a0992016-06-29 20:40:21 -0300715 break;
716 }
717
Antti Palosaari81e19912016-11-06 18:55:36 -0200718 /* Read current modulation */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200719 ret = regmap_read(dev->regmap, 0x80f903, &utmp);
Antti Palosaari659a0992016-06-29 20:40:21 -0300720 if (ret)
721 goto err;
722
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200723 switch ((utmp >> 0) & 3) {
Antti Palosaari659a0992016-06-29 20:40:21 -0300724 case 0:
Antti Palosaari239dd612016-12-19 01:42:37 -0200725 /*
726 * QPSK
727 * CNR[dB] 13 * -log10((1690000 - value) / value) + 2.6
728 * value [653799, 1689999], 2.6 / 13 = 3355443
729 */
730 utmp1 = clamp(utmp1, 653799U, 1689999U);
731 utmp1 = ((u64)(intlog10(utmp1)
732 - intlog10(1690000 - utmp1)
733 + 3355443) * 13 * 1000) >> 24;
Antti Palosaari659a0992016-06-29 20:40:21 -0300734 break;
735 case 1:
Antti Palosaari239dd612016-12-19 01:42:37 -0200736 /*
737 * QAM-16
738 * CNR[dB] 6 * log10((value - 370000) / (828000 - value)) + 15.7
739 * value [371105, 827999], 15.7 / 6 = 43900382
740 */
741 utmp1 = clamp(utmp1, 371105U, 827999U);
742 utmp1 = ((u64)(intlog10(utmp1 - 370000)
743 - intlog10(828000 - utmp1)
744 + 43900382) * 6 * 1000) >> 24;
Antti Palosaari659a0992016-06-29 20:40:21 -0300745 break;
746 case 2:
Antti Palosaari239dd612016-12-19 01:42:37 -0200747 /*
748 * QAM-64
749 * CNR[dB] 8 * log10((value - 193000) / (425000 - value)) + 23.8
750 * value [193246, 424999], 23.8 / 8 = 49912218
751 */
752 utmp1 = clamp(utmp1, 193246U, 424999U);
753 utmp1 = ((u64)(intlog10(utmp1 - 193000)
754 - intlog10(425000 - utmp1)
755 + 49912218) * 8 * 1000) >> 24;
Antti Palosaari659a0992016-06-29 20:40:21 -0300756 break;
757 default:
Antti Palosaari239dd612016-12-19 01:42:37 -0200758 utmp1 = 0;
Antti Palosaari659a0992016-06-29 20:40:21 -0300759 break;
760 }
761
Antti Palosaari239dd612016-12-19 01:42:37 -0200762 dev_dbg(&client->dev, "cnr=%u\n", utmp1);
Antti Palosaari659a0992016-06-29 20:40:21 -0300763
Antti Palosaari659a0992016-06-29 20:40:21 -0300764 c->cnr.stat[0].scale = FE_SCALE_DECIBEL;
Antti Palosaari239dd612016-12-19 01:42:37 -0200765 c->cnr.stat[0].svalue = utmp1;
Antti Palosaari659a0992016-06-29 20:40:21 -0300766 } else {
Antti Palosaari659a0992016-06-29 20:40:21 -0300767 c->cnr.stat[0].scale = FE_SCALE_NOT_AVAILABLE;
768 }
769
770 /* UCB/PER/BER */
771 if (dev->fe_status & FE_HAS_LOCK) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200772 /* Outer FEC, 204 byte packets */
Antti Palosaari659a0992016-06-29 20:40:21 -0300773 u16 abort_packet_count, rsd_packet_count;
Antti Palosaari81e19912016-11-06 18:55:36 -0200774 /* Inner FEC, bits */
Antti Palosaari659a0992016-06-29 20:40:21 -0300775 u32 rsd_bit_err_count;
776
777 /*
778 * Packet count used for measurement is 10000
779 * (rsd_packet_count). Maybe it should be increased?
780 */
781
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200782 ret = regmap_bulk_read(dev->regmap, 0x800032, buf, 7);
Antti Palosaari659a0992016-06-29 20:40:21 -0300783 if (ret)
784 goto err;
785
786 abort_packet_count = (buf[1] << 8) | (buf[0] << 0);
787 rsd_bit_err_count = (buf[4] << 16) | (buf[3] << 8) | buf[2];
788 rsd_packet_count = (buf[6] << 8) | (buf[5] << 0);
789
790 dev->error_block_count += abort_packet_count;
791 dev->total_block_count += rsd_packet_count;
792 dev->post_bit_error += rsd_bit_err_count;
793 dev->post_bit_count += rsd_packet_count * 204 * 8;
794
795 c->block_count.len = 1;
796 c->block_count.stat[0].scale = FE_SCALE_COUNTER;
797 c->block_count.stat[0].uvalue = dev->total_block_count;
798
799 c->block_error.len = 1;
800 c->block_error.stat[0].scale = FE_SCALE_COUNTER;
801 c->block_error.stat[0].uvalue = dev->error_block_count;
802
803 c->post_bit_count.len = 1;
804 c->post_bit_count.stat[0].scale = FE_SCALE_COUNTER;
805 c->post_bit_count.stat[0].uvalue = dev->post_bit_count;
806
807 c->post_bit_error.len = 1;
808 c->post_bit_error.stat[0].scale = FE_SCALE_COUNTER;
809 c->post_bit_error.stat[0].uvalue = dev->post_bit_error;
810 }
811
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300812 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300813err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200814 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300815 return ret;
816}
817
818static int af9033_read_snr(struct dvb_frontend *fe, u16 *snr)
819{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300820 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200821 struct i2c_client *client = dev->client;
Antti Palosaari6b457782014-09-02 05:03:21 -0300822 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300823 int ret;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200824 unsigned int utmp;
Antti Palosaarie898ef62012-04-01 12:50:02 -0300825
Antti Palosaari81e19912016-11-06 18:55:36 -0200826 dev_dbg(&client->dev, "\n");
827
828 /* Use DVBv5 CNR */
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300829 if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL) {
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300830 /* Return 0.1 dB for AF9030 and 0-0xffff for IT9130. */
831 if (dev->is_af9035) {
832 /* 1000x => 10x (0.1 dB) */
833 *snr = div_s64(c->cnr.stat[0].svalue, 100);
834 } else {
835 /* 1000x => 1x (1 dB) */
836 *snr = div_s64(c->cnr.stat[0].svalue, 1000);
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300837
Antti Palosaari81e19912016-11-06 18:55:36 -0200838 /* Read current modulation */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200839 ret = regmap_read(dev->regmap, 0x80f903, &utmp);
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300840 if (ret)
841 goto err;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300842
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300843 /* scale value to 0x0000-0xffff */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200844 switch ((utmp >> 0) & 3) {
Antti Palosaaric3a80cd2014-11-03 21:53:05 -0300845 case 0:
846 *snr = *snr * 0xffff / 23;
847 break;
848 case 1:
849 *snr = *snr * 0xffff / 26;
850 break;
851 case 2:
852 *snr = *snr * 0xffff / 32;
853 break;
854 default:
855 goto err;
856 }
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300857 }
858 } else {
Antti Palosaari6b457782014-09-02 05:03:21 -0300859 *snr = 0;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300860 }
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300861
862 return 0;
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300863err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200864 dev_dbg(&client->dev, "failed=%d\n", ret);
Bimow Chen6d03f6a2014-10-01 23:37:13 -0300865 return ret;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300866}
867
868static int af9033_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
869{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300870 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200871 struct i2c_client *client = dev->client;
Bimow Chen3adec272014-10-01 07:28:54 -0300872 struct dtv_frontend_properties *c = &dev->fe.dtv_property_cache;
873 int ret, tmp, power_real;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200874 unsigned int utmp;
875 u8 gain_offset, buf[7];
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300876
Antti Palosaari81e19912016-11-06 18:55:36 -0200877 dev_dbg(&client->dev, "\n");
878
Bimow Chen3adec272014-10-01 07:28:54 -0300879 if (dev->is_af9035) {
Antti Palosaari81e19912016-11-06 18:55:36 -0200880 /* Read signal strength of 0-100 scale */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200881 ret = regmap_read(dev->regmap, 0x800048, &utmp);
882 if (ret)
Antti Palosaari0b0d9622014-11-03 20:24:13 -0300883 goto err;
884
Antti Palosaari81e19912016-11-06 18:55:36 -0200885 /* Scale value to 0x0000-0xffff */
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200886 *strength = utmp * 0xffff / 100;
Bimow Chen3adec272014-10-01 07:28:54 -0300887 } else {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200888 ret = regmap_read(dev->regmap, 0x8000f7, &utmp);
889 if (ret)
Antti Palosaari1620d222014-11-03 21:23:26 -0300890 goto err;
891
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200892 ret = regmap_bulk_read(dev->regmap, 0x80f900, buf, 7);
893 if (ret)
Antti Palosaari1620d222014-11-03 21:23:26 -0300894 goto err;
Bimow Chen3adec272014-10-01 07:28:54 -0300895
896 if (c->frequency <= 300000000)
897 gain_offset = 7; /* VHF */
898 else
899 gain_offset = 4; /* UHF */
900
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200901 power_real = (utmp - 100 - gain_offset) -
Bimow Chen3adec272014-10-01 07:28:54 -0300902 power_reference[((buf[3] >> 0) & 3)][((buf[6] >> 0) & 7)];
903
904 if (power_real < -15)
905 tmp = 0;
906 else if ((power_real >= -15) && (power_real < 0))
907 tmp = (2 * (power_real + 15)) / 3;
908 else if ((power_real >= 0) && (power_real < 20))
909 tmp = 4 * power_real + 10;
910 else if ((power_real >= 20) && (power_real < 35))
911 tmp = (2 * (power_real - 20)) / 3 + 90;
912 else
913 tmp = 100;
914
Antti Palosaari81e19912016-11-06 18:55:36 -0200915 /* Scale value to 0x0000-0xffff */
Bimow Chen3adec272014-10-01 07:28:54 -0300916 *strength = tmp * 0xffff / 100;
917 }
918
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300919 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300920err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200921 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300922 return ret;
923}
924
925static int af9033_read_ber(struct dvb_frontend *fe, u32 *ber)
926{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300927 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300928
Antti Palosaarie53c4742014-09-03 23:22:53 -0300929 *ber = (dev->post_bit_error - dev->post_bit_error_prev);
930 dev->post_bit_error_prev = dev->post_bit_error;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300931
932 return 0;
933}
934
935static int af9033_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
936{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300937 struct af9033_dev *dev = fe->demodulator_priv;
Hans-Frieder Vogt47eafa52012-04-07 10:34:34 -0300938
Antti Palosaari1d0ceae2014-09-03 23:07:39 -0300939 *ucblocks = dev->error_block_count;
Antti Palosaari81e19912016-11-06 18:55:36 -0200940
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300941 return 0;
942}
943
944static int af9033_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
945{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300946 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200947 struct i2c_client *client = dev->client;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300948 int ret;
949
Antti Palosaari81e19912016-11-06 18:55:36 -0200950 dev_dbg(&client->dev, "enable=%d\n", enable);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300951
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200952 ret = regmap_update_bits(dev->regmap, 0x00fa04, 0x01, enable);
953 if (ret)
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300954 goto err;
955
956 return 0;
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300957err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200958 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari4b64bb22012-03-30 08:21:25 -0300959 return ret;
960}
961
Mauro Carvalho Chehabed97a6f2014-03-14 14:29:06 -0300962static int af9033_pid_filter_ctrl(struct dvb_frontend *fe, int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -0300963{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300964 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200965 struct i2c_client *client = dev->client;
Antti Palosaari040cf862014-02-13 15:40:59 -0300966 int ret;
967
Antti Palosaari81e19912016-11-06 18:55:36 -0200968 dev_dbg(&client->dev, "onoff=%d\n", onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300969
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200970 ret = regmap_update_bits(dev->regmap, 0x80f993, 0x01, onoff);
971 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -0300972 goto err;
973
974 return 0;
Antti Palosaari040cf862014-02-13 15:40:59 -0300975err:
Antti Palosaari81e19912016-11-06 18:55:36 -0200976 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari040cf862014-02-13 15:40:59 -0300977 return ret;
978}
Antti Palosaari040cf862014-02-13 15:40:59 -0300979
Antti Palosaari24e419a2014-08-09 14:59:53 -0300980static int af9033_pid_filter(struct dvb_frontend *fe, int index, u16 pid,
Antti Palosaari81e19912016-11-06 18:55:36 -0200981 int onoff)
Antti Palosaari040cf862014-02-13 15:40:59 -0300982{
Antti Palosaari09611ca2014-08-31 02:18:34 -0300983 struct af9033_dev *dev = fe->demodulator_priv;
Antti Palosaari81e19912016-11-06 18:55:36 -0200984 struct i2c_client *client = dev->client;
Antti Palosaari040cf862014-02-13 15:40:59 -0300985 int ret;
986 u8 wbuf[2] = {(pid >> 0) & 0xff, (pid >> 8) & 0xff};
987
Antti Palosaari81e19912016-11-06 18:55:36 -0200988 dev_dbg(&client->dev, "index=%d pid=%04x onoff=%d\n",
989 index, pid, onoff);
Antti Palosaari040cf862014-02-13 15:40:59 -0300990
991 if (pid > 0x1fff)
992 return 0;
993
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200994 ret = regmap_bulk_write(dev->regmap, 0x80f996, wbuf, 2);
995 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -0300996 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -0200997 ret = regmap_write(dev->regmap, 0x80f994, onoff);
998 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -0300999 goto err;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001000 ret = regmap_write(dev->regmap, 0x80f995, index);
1001 if (ret)
Antti Palosaari040cf862014-02-13 15:40:59 -03001002 goto err;
1003
1004 return 0;
Antti Palosaari040cf862014-02-13 15:40:59 -03001005err:
Antti Palosaari81e19912016-11-06 18:55:36 -02001006 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari040cf862014-02-13 15:40:59 -03001007 return ret;
1008}
Antti Palosaari040cf862014-02-13 15:40:59 -03001009
Max Kellermannbd336e62016-08-09 18:32:21 -03001010static const struct dvb_frontend_ops af9033_ops = {
Antti Palosaari81e19912016-11-06 18:55:36 -02001011 .delsys = {SYS_DVBT},
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001012 .info = {
1013 .name = "Afatech AF9033 (DVB-T)",
Mauro Carvalho Chehabf1b1eab2018-07-05 18:59:36 -04001014 .frequency_min_hz = 174 * MHz,
1015 .frequency_max_hz = 862 * MHz,
1016 .frequency_stepsize_hz = 250 * kHz,
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001017 .caps = FE_CAN_FEC_1_2 |
1018 FE_CAN_FEC_2_3 |
1019 FE_CAN_FEC_3_4 |
1020 FE_CAN_FEC_5_6 |
1021 FE_CAN_FEC_7_8 |
1022 FE_CAN_FEC_AUTO |
1023 FE_CAN_QPSK |
1024 FE_CAN_QAM_16 |
1025 FE_CAN_QAM_64 |
1026 FE_CAN_QAM_AUTO |
1027 FE_CAN_TRANSMISSION_MODE_AUTO |
1028 FE_CAN_GUARD_INTERVAL_AUTO |
1029 FE_CAN_HIERARCHY_AUTO |
1030 FE_CAN_RECOVER |
1031 FE_CAN_MUTE_TS
1032 },
1033
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001034 .init = af9033_init,
1035 .sleep = af9033_sleep,
1036
1037 .get_tune_settings = af9033_get_tune_settings,
1038 .set_frontend = af9033_set_frontend,
Gianluca Gennari0a4df232012-04-05 12:47:19 -03001039 .get_frontend = af9033_get_frontend,
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001040
1041 .read_status = af9033_read_status,
1042 .read_snr = af9033_read_snr,
1043 .read_signal_strength = af9033_read_signal_strength,
1044 .read_ber = af9033_read_ber,
1045 .read_ucblocks = af9033_read_ucblocks,
1046
1047 .i2c_gate_ctrl = af9033_i2c_gate_ctrl,
1048};
1049
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001050static int af9033_probe(struct i2c_client *client,
Antti Palosaari81e19912016-11-06 18:55:36 -02001051 const struct i2c_device_id *id)
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001052{
1053 struct af9033_config *cfg = client->dev.platform_data;
1054 struct af9033_dev *dev;
1055 int ret;
1056 u8 buf[8];
1057 u32 reg;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001058 static const struct regmap_config regmap_config = {
1059 .reg_bits = 24,
1060 .val_bits = 8,
1061 };
1062
Antti Palosaari81e19912016-11-06 18:55:36 -02001063 /* Allocate memory for the internal state */
1064 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1065 if (!dev) {
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001066 ret = -ENOMEM;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001067 goto err;
1068 }
1069
Antti Palosaari81e19912016-11-06 18:55:36 -02001070 /* Setup the state */
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001071 dev->client = client;
Antti Palosaari81e19912016-11-06 18:55:36 -02001072 memcpy(&dev->cfg, cfg, sizeof(dev->cfg));
1073 switch (dev->cfg.ts_mode) {
1074 case AF9033_TS_MODE_PARALLEL:
1075 dev->ts_mode_parallel = true;
1076 break;
1077 case AF9033_TS_MODE_SERIAL:
1078 dev->ts_mode_serial = true;
1079 break;
1080 case AF9033_TS_MODE_USB:
1081 /* USB mode for AF9035 */
1082 default:
1083 break;
1084 }
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001085
1086 if (dev->cfg.clock != 12000000) {
1087 ret = -ENODEV;
Antti Palosaari81e19912016-11-06 18:55:36 -02001088 dev_err(&client->dev,
1089 "Unsupported clock %u Hz. Only 12000000 Hz is supported currently\n",
1090 dev->cfg.clock);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001091 goto err_kfree;
1092 }
1093
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001094 /* Create regmap */
1095 dev->regmap = regmap_init_i2c(client, &regmap_config);
1096 if (IS_ERR(dev->regmap)) {
1097 ret = PTR_ERR(dev->regmap);
1098 goto err_kfree;
1099 }
1100
Antti Palosaari81e19912016-11-06 18:55:36 -02001101 /* Firmware version */
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001102 switch (dev->cfg.tuner) {
1103 case AF9033_TUNER_IT9135_38:
1104 case AF9033_TUNER_IT9135_51:
1105 case AF9033_TUNER_IT9135_52:
1106 case AF9033_TUNER_IT9135_60:
1107 case AF9033_TUNER_IT9135_61:
1108 case AF9033_TUNER_IT9135_62:
Antti Palosaari83f11612014-09-02 03:55:21 -03001109 dev->is_it9135 = true;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001110 reg = 0x004bfc;
1111 break;
1112 default:
Antti Palosaari83f11612014-09-02 03:55:21 -03001113 dev->is_af9035 = true;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001114 reg = 0x0083e9;
1115 break;
1116 }
1117
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001118 ret = regmap_bulk_read(dev->regmap, reg, &buf[0], 4);
1119 if (ret)
1120 goto err_regmap_exit;
1121 ret = regmap_bulk_read(dev->regmap, 0x804191, &buf[4], 4);
1122 if (ret)
1123 goto err_regmap_exit;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001124
Antti Palosaari81e19912016-11-06 18:55:36 -02001125 dev_info(&client->dev,
1126 "firmware version: LINK %d.%d.%d.%d - OFDM %d.%d.%d.%d\n",
1127 buf[0], buf[1], buf[2], buf[3],
1128 buf[4], buf[5], buf[6], buf[7]);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001129
Antti Palosaari81e19912016-11-06 18:55:36 -02001130 /* Sleep as chip seems to be partly active by default */
Victor Toso09a446d2018-10-30 12:14:48 -04001131 /* IT9135 did not like to sleep at that early */
1132 if (dev->is_af9035) {
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001133 ret = regmap_write(dev->regmap, 0x80004c, 0x01);
1134 if (ret)
1135 goto err_regmap_exit;
1136 ret = regmap_write(dev->regmap, 0x800000, 0x00);
1137 if (ret)
1138 goto err_regmap_exit;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001139 }
1140
Antti Palosaari81e19912016-11-06 18:55:36 -02001141 /* Create dvb frontend */
1142 memcpy(&dev->fe.ops, &af9033_ops, sizeof(dev->fe.ops));
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001143 dev->fe.demodulator_priv = dev;
1144 *cfg->fe = &dev->fe;
1145 if (cfg->ops) {
1146 cfg->ops->pid_filter = af9033_pid_filter;
1147 cfg->ops->pid_filter_ctrl = af9033_pid_filter_ctrl;
1148 }
Antti Palosaari34618312016-11-06 22:14:18 -02001149 cfg->regmap = dev->regmap;
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001150 i2c_set_clientdata(client, dev);
1151
Antti Palosaari81e19912016-11-06 18:55:36 -02001152 dev_info(&client->dev, "Afatech AF9033 successfully attached\n");
1153
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001154 return 0;
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001155err_regmap_exit:
1156 regmap_exit(dev->regmap);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001157err_kfree:
1158 kfree(dev);
1159err:
Antti Palosaari6a087f12014-08-31 21:08:09 -03001160 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001161 return ret;
1162}
1163
1164static int af9033_remove(struct i2c_client *client)
1165{
1166 struct af9033_dev *dev = i2c_get_clientdata(client);
1167
Antti Palosaari81e19912016-11-06 18:55:36 -02001168 dev_dbg(&client->dev, "\n");
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001169
Antti Palosaaribc85d5e2016-11-06 02:20:29 -02001170 regmap_exit(dev->regmap);
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001171 kfree(dev);
1172
1173 return 0;
1174}
1175
1176static const struct i2c_device_id af9033_id_table[] = {
1177 {"af9033", 0},
1178 {}
1179};
1180MODULE_DEVICE_TABLE(i2c, af9033_id_table);
1181
1182static struct i2c_driver af9033_driver = {
1183 .driver = {
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001184 .name = "af9033",
Antti Palosaari72812172016-06-29 20:40:22 -03001185 .suppress_bind_attrs = true,
Antti Palosaarif5b00a72014-08-31 20:57:05 -03001186 },
1187 .probe = af9033_probe,
1188 .remove = af9033_remove,
1189 .id_table = af9033_id_table,
1190};
1191
1192module_i2c_driver(af9033_driver);
1193
Antti Palosaari4b64bb22012-03-30 08:21:25 -03001194MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
1195MODULE_DESCRIPTION("Afatech AF9033 DVB-T demodulator driver");
1196MODULE_LICENSE("GPL");