blob: f6ebbb47b9b2b9eedc62fa8789b969cae97fac04 [file] [log] [blame]
Antti Palosaari27cfc852011-04-07 16:27:43 -03001/*
2 * Sony CXD2820R demodulator driver
3 *
4 * Copyright (C) 2010 Antti Palosaari <crope@iki.fi>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21
22#include "cxd2820r_priv.h"
23
Antti Palosaari43e2ea62016-08-13 13:19:05 -030024/* Write register table */
25int cxd2820r_wr_reg_val_mask_tab(struct cxd2820r_priv *priv,
26 const struct reg_val_mask *tab, int tab_len)
Antti Palosaari27cfc852011-04-07 16:27:43 -030027{
Antti Palosaaric98975f2016-08-09 22:00:37 -030028 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -030029 int ret;
Antti Palosaari43e2ea62016-08-13 13:19:05 -030030 unsigned int i, reg, mask, val;
31 struct regmap *regmap;
Antti Palosaari27cfc852011-04-07 16:27:43 -030032
Antti Palosaari43e2ea62016-08-13 13:19:05 -030033 dev_dbg(&client->dev, "tab_len=%d\n", tab_len);
34
35 for (i = 0; i < tab_len; i++) {
36 if ((tab[i].reg >> 16) & 0x1)
37 regmap = priv->regmap[1];
38 else
39 regmap = priv->regmap[0];
40
41 reg = (tab[i].reg >> 0) & 0xffff;
42 val = tab[i].val;
43 mask = tab[i].mask;
44
45 if (mask == 0xff)
46 ret = regmap_write(regmap, reg, val);
47 else
48 ret = regmap_write_bits(regmap, reg, mask, val);
49 if (ret)
50 goto error;
Mauro Carvalho Chehab37ebaf62013-11-02 05:11:47 -030051 }
52
Antti Palosaari43e2ea62016-08-13 13:19:05 -030053 return 0;
54error:
55 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -030056 return ret;
57}
58
Antti Palosaari1e8f31f2012-07-19 21:10:36 -030059int cxd2820r_gpio(struct dvb_frontend *fe, u8 *gpio)
Antti Palosaari27cfc852011-04-07 16:27:43 -030060{
61 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -030062 struct i2c_client *client = priv->client[0];
63 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari27cfc852011-04-07 16:27:43 -030064 int ret, i;
Antti Palosaari1e8f31f2012-07-19 21:10:36 -030065 u8 tmp0, tmp1;
Antti Palosaari75aeafc2012-07-19 13:10:12 -030066
Antti Palosaaric98975f2016-08-09 22:00:37 -030067 dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
Antti Palosaari27cfc852011-04-07 16:27:43 -030068
Antti Palosaari27cfc852011-04-07 16:27:43 -030069 /* update GPIOs only when needed */
70 if (!memcmp(gpio, priv->gpio, sizeof(priv->gpio)))
71 return 0;
72
73 tmp0 = 0x00;
74 tmp1 = 0x00;
75 for (i = 0; i < sizeof(priv->gpio); i++) {
76 /* enable / disable */
77 if (gpio[i] & CXD2820R_GPIO_E)
78 tmp0 |= (2 << 6) >> (2 * i);
79 else
80 tmp0 |= (1 << 6) >> (2 * i);
81
82 /* input / output */
83 if (gpio[i] & CXD2820R_GPIO_I)
84 tmp1 |= (1 << (3 + i));
85 else
86 tmp1 |= (0 << (3 + i));
87
88 /* high / low */
89 if (gpio[i] & CXD2820R_GPIO_H)
90 tmp1 |= (1 << (0 + i));
91 else
92 tmp1 |= (0 << (0 + i));
93
Antti Palosaaric98975f2016-08-09 22:00:37 -030094 dev_dbg(&client->dev, "gpio i=%d %02x %02x\n", i, tmp0, tmp1);
Antti Palosaari27cfc852011-04-07 16:27:43 -030095 }
96
Antti Palosaaric98975f2016-08-09 22:00:37 -030097 dev_dbg(&client->dev, "wr gpio=%02x %02x\n", tmp0, tmp1);
Antti Palosaari27cfc852011-04-07 16:27:43 -030098
99 /* write bits [7:2] */
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300100 ret = regmap_update_bits(priv->regmap[0], 0x0089, 0xfc, tmp0);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300101 if (ret)
102 goto error;
103
104 /* write bits [5:0] */
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300105 ret = regmap_update_bits(priv->regmap[0], 0x008e, 0x3f, tmp1);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300106 if (ret)
107 goto error;
108
109 memcpy(priv->gpio, gpio, sizeof(priv->gpio));
110
111 return ret;
112error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300113 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300114 return ret;
115}
116
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300117static int cxd2820r_set_frontend(struct dvb_frontend *fe)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300118{
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300119 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300120 struct i2c_client *client = priv->client[0];
Antti Palosaari27cfc852011-04-07 16:27:43 -0300121 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
122 int ret;
Manu Abraham14c03862011-11-24 11:59:53 -0300123
Antti Palosaaric98975f2016-08-09 22:00:37 -0300124 dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300125
Manu Abraham14c03862011-11-24 11:59:53 -0300126 switch (c->delivery_system) {
127 case SYS_DVBT:
128 ret = cxd2820r_init_t(fe);
129 if (ret < 0)
130 goto err;
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300131 ret = cxd2820r_set_frontend_t(fe);
Manu Abraham14c03862011-11-24 11:59:53 -0300132 if (ret < 0)
133 goto err;
134 break;
135 case SYS_DVBT2:
136 ret = cxd2820r_init_t(fe);
137 if (ret < 0)
138 goto err;
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300139 ret = cxd2820r_set_frontend_t2(fe);
Manu Abraham14c03862011-11-24 11:59:53 -0300140 if (ret < 0)
141 goto err;
142 break;
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300143 case SYS_DVBC_ANNEX_A:
Manu Abraham14c03862011-11-24 11:59:53 -0300144 ret = cxd2820r_init_c(fe);
145 if (ret < 0)
146 goto err;
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300147 ret = cxd2820r_set_frontend_c(fe);
Manu Abraham14c03862011-11-24 11:59:53 -0300148 if (ret < 0)
149 goto err;
150 break;
151 default:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300152 dev_dbg(&client->dev, "invalid delivery_system\n");
Manu Abraham14c03862011-11-24 11:59:53 -0300153 ret = -EINVAL;
154 break;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300155 }
Manu Abraham14c03862011-11-24 11:59:53 -0300156err:
Antti Palosaari27cfc852011-04-07 16:27:43 -0300157 return ret;
158}
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300159
160static int cxd2820r_read_status(struct dvb_frontend *fe, enum fe_status *status)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300161{
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300162 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300163 struct i2c_client *client = priv->client[0];
164 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300165 int ret;
Manu Abraham14c03862011-11-24 11:59:53 -0300166
Antti Palosaaric98975f2016-08-09 22:00:37 -0300167 dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300168
Antti Palosaaric98975f2016-08-09 22:00:37 -0300169 switch (c->delivery_system) {
Manu Abraham14c03862011-11-24 11:59:53 -0300170 case SYS_DVBT:
171 ret = cxd2820r_read_status_t(fe, status);
172 break;
173 case SYS_DVBT2:
174 ret = cxd2820r_read_status_t2(fe, status);
175 break;
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300176 case SYS_DVBC_ANNEX_A:
Antti Palosaari27cfc852011-04-07 16:27:43 -0300177 ret = cxd2820r_read_status_c(fe, status);
Manu Abraham14c03862011-11-24 11:59:53 -0300178 break;
179 default:
180 ret = -EINVAL;
181 break;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300182 }
Antti Palosaari27cfc852011-04-07 16:27:43 -0300183 return ret;
184}
185
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200186static int cxd2820r_get_frontend(struct dvb_frontend *fe,
187 struct dtv_frontend_properties *p)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300188{
Antti Palosaari3b6a5672012-01-15 18:30:36 -0300189 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300190 struct i2c_client *client = priv->client[0];
191 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300192 int ret;
Manu Abraham14c03862011-11-24 11:59:53 -0300193
Antti Palosaaric98975f2016-08-09 22:00:37 -0300194 dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
Antti Palosaari3b6a5672012-01-15 18:30:36 -0300195
196 if (priv->delivery_system == SYS_UNDEFINED)
197 return 0;
198
Antti Palosaaric98975f2016-08-09 22:00:37 -0300199 switch (c->delivery_system) {
Manu Abraham14c03862011-11-24 11:59:53 -0300200 case SYS_DVBT:
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200201 ret = cxd2820r_get_frontend_t(fe, p);
Manu Abraham14c03862011-11-24 11:59:53 -0300202 break;
203 case SYS_DVBT2:
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200204 ret = cxd2820r_get_frontend_t2(fe, p);
Manu Abraham14c03862011-11-24 11:59:53 -0300205 break;
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300206 case SYS_DVBC_ANNEX_A:
Mauro Carvalho Chehab7e3e68b2016-02-04 12:58:30 -0200207 ret = cxd2820r_get_frontend_c(fe, p);
Manu Abraham14c03862011-11-24 11:59:53 -0300208 break;
209 default:
210 ret = -EINVAL;
211 break;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300212 }
Antti Palosaari27cfc852011-04-07 16:27:43 -0300213 return ret;
214}
215
216static int cxd2820r_read_ber(struct dvb_frontend *fe, u32 *ber)
217{
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300218 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300219 struct i2c_client *client = priv->client[0];
220 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Manu Abraham14c03862011-11-24 11:59:53 -0300221
Antti Palosaaric98975f2016-08-09 22:00:37 -0300222 dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300223
Antti Palosaari4aa4fd82016-08-09 14:58:21 -0300224 *ber = (priv->post_bit_error - priv->post_bit_error_prev_dvbv3);
225 priv->post_bit_error_prev_dvbv3 = priv->post_bit_error;
226
227 return 0;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300228}
229
230static int cxd2820r_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
231{
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300232 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300233 struct i2c_client *client = priv->client[0];
Antti Palosaari4aa4fd82016-08-09 14:58:21 -0300234 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Manu Abraham14c03862011-11-24 11:59:53 -0300235
Antti Palosaaric98975f2016-08-09 22:00:37 -0300236 dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300237
Antti Palosaari4aa4fd82016-08-09 14:58:21 -0300238 if (c->strength.stat[0].scale == FE_SCALE_RELATIVE)
239 *strength = c->strength.stat[0].uvalue;
240 else
241 *strength = 0;
242
243 return 0;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300244}
245
246static int cxd2820r_read_snr(struct dvb_frontend *fe, u16 *snr)
247{
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300248 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300249 struct i2c_client *client = priv->client[0];
Antti Palosaari4aa4fd82016-08-09 14:58:21 -0300250 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Manu Abraham14c03862011-11-24 11:59:53 -0300251
Antti Palosaaric98975f2016-08-09 22:00:37 -0300252 dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300253
Antti Palosaari4aa4fd82016-08-09 14:58:21 -0300254 if (c->cnr.stat[0].scale == FE_SCALE_DECIBEL)
255 *snr = div_s64(c->cnr.stat[0].svalue, 100);
256 else
257 *snr = 0;
258
259 return 0;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300260}
261
262static int cxd2820r_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
263{
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300264 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300265 struct i2c_client *client = priv->client[0];
266 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Manu Abraham14c03862011-11-24 11:59:53 -0300267
Antti Palosaaric98975f2016-08-09 22:00:37 -0300268 dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300269
Antti Palosaari4aa4fd82016-08-09 14:58:21 -0300270 *ucblocks = 0;
271
272 return 0;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300273}
274
275static int cxd2820r_init(struct dvb_frontend *fe)
276{
Manu Abraham14c03862011-11-24 11:59:53 -0300277 return 0;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300278}
279
280static int cxd2820r_sleep(struct dvb_frontend *fe)
281{
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300282 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300283 struct i2c_client *client = priv->client[0];
284 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300285 int ret;
Manu Abraham14c03862011-11-24 11:59:53 -0300286
Antti Palosaaric98975f2016-08-09 22:00:37 -0300287 dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300288
Antti Palosaaric98975f2016-08-09 22:00:37 -0300289 switch (c->delivery_system) {
Manu Abraham14c03862011-11-24 11:59:53 -0300290 case SYS_DVBT:
291 ret = cxd2820r_sleep_t(fe);
292 break;
293 case SYS_DVBT2:
294 ret = cxd2820r_sleep_t2(fe);
295 break;
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300296 case SYS_DVBC_ANNEX_A:
Antti Palosaari27cfc852011-04-07 16:27:43 -0300297 ret = cxd2820r_sleep_c(fe);
Manu Abraham14c03862011-11-24 11:59:53 -0300298 break;
299 default:
300 ret = -EINVAL;
301 break;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300302 }
Antti Palosaari27cfc852011-04-07 16:27:43 -0300303 return ret;
304}
305
306static int cxd2820r_get_tune_settings(struct dvb_frontend *fe,
Manu Abraham14c03862011-11-24 11:59:53 -0300307 struct dvb_frontend_tune_settings *s)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300308{
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300309 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300310 struct i2c_client *client = priv->client[0];
311 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300312 int ret;
Manu Abraham14c03862011-11-24 11:59:53 -0300313
Antti Palosaaric98975f2016-08-09 22:00:37 -0300314 dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300315
Antti Palosaaric98975f2016-08-09 22:00:37 -0300316 switch (c->delivery_system) {
Manu Abraham14c03862011-11-24 11:59:53 -0300317 case SYS_DVBT:
318 ret = cxd2820r_get_tune_settings_t(fe, s);
319 break;
320 case SYS_DVBT2:
321 ret = cxd2820r_get_tune_settings_t2(fe, s);
322 break;
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300323 case SYS_DVBC_ANNEX_A:
Antti Palosaari27cfc852011-04-07 16:27:43 -0300324 ret = cxd2820r_get_tune_settings_c(fe, s);
Manu Abraham14c03862011-11-24 11:59:53 -0300325 break;
326 default:
327 ret = -EINVAL;
328 break;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300329 }
Antti Palosaari27cfc852011-04-07 16:27:43 -0300330 return ret;
331}
332
Mauro Carvalho Chehab41da5322011-12-26 18:03:12 -0300333static enum dvbfe_search cxd2820r_search(struct dvb_frontend *fe)
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300334{
335 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300336 struct i2c_client *client = priv->client[0];
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300337 struct dtv_frontend_properties *c = &fe->dtv_property_cache;
338 int ret, i;
Mauro Carvalho Chehab0df289a2015-06-07 14:53:52 -0300339 enum fe_status status = 0;
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300340
Antti Palosaaric98975f2016-08-09 22:00:37 -0300341 dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300342
343 /* switch between DVB-T and DVB-T2 when tune fails */
Antti Palosaarid04ca8d2012-01-15 15:08:30 -0300344 if (priv->last_tune_failed) {
Antti Palosaari72565222012-01-20 19:48:28 -0300345 if (priv->delivery_system == SYS_DVBT) {
346 ret = cxd2820r_sleep_t(fe);
347 if (ret)
348 goto error;
349
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300350 c->delivery_system = SYS_DVBT2;
Antti Palosaari72565222012-01-20 19:48:28 -0300351 } else if (priv->delivery_system == SYS_DVBT2) {
352 ret = cxd2820r_sleep_t2(fe);
353 if (ret)
354 goto error;
355
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300356 c->delivery_system = SYS_DVBT;
Antti Palosaari72565222012-01-20 19:48:28 -0300357 }
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300358 }
359
360 /* set frontend */
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300361 ret = cxd2820r_set_frontend(fe);
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300362 if (ret)
363 goto error;
364
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300365 /* frontend lock wait loop count */
366 switch (priv->delivery_system) {
367 case SYS_DVBT:
Antti Palosaari1f649722012-01-15 16:50:11 -0300368 case SYS_DVBC_ANNEX_A:
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300369 i = 20;
370 break;
371 case SYS_DVBT2:
372 i = 40;
373 break;
374 case SYS_UNDEFINED:
375 default:
376 i = 0;
377 break;
378 }
379
380 /* wait frontend lock */
381 for (; i > 0; i--) {
Antti Palosaaric98975f2016-08-09 22:00:37 -0300382 dev_dbg(&client->dev, "loop=%d\n", i);
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300383 msleep(50);
384 ret = cxd2820r_read_status(fe, &status);
385 if (ret)
386 goto error;
387
Gianluca Gennarib115f402012-03-15 13:33:47 -0300388 if (status & FE_HAS_LOCK)
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300389 break;
390 }
391
392 /* check if we have a valid signal */
Gianluca Gennarib115f402012-03-15 13:33:47 -0300393 if (status & FE_HAS_LOCK) {
Mauro Carvalho Chehab285c0b02014-09-03 15:22:02 -0300394 priv->last_tune_failed = false;
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300395 return DVBFE_ALGO_SEARCH_SUCCESS;
396 } else {
Mauro Carvalho Chehab285c0b02014-09-03 15:22:02 -0300397 priv->last_tune_failed = true;
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300398 return DVBFE_ALGO_SEARCH_AGAIN;
399 }
400
401error:
Antti Palosaaric98975f2016-08-09 22:00:37 -0300402 dev_dbg(&client->dev, "failed=%d\n", ret);
Antti Palosaarie47b78f2011-05-03 20:31:36 -0300403 return DVBFE_ALGO_SEARCH_ERROR;
404}
405
406static int cxd2820r_get_frontend_algo(struct dvb_frontend *fe)
407{
408 return DVBFE_ALGO_CUSTOM;
409}
410
Antti Palosaari27cfc852011-04-07 16:27:43 -0300411static void cxd2820r_release(struct dvb_frontend *fe)
412{
413 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300414 struct i2c_client *client = priv->client[0];
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300415
Antti Palosaaric98975f2016-08-09 22:00:37 -0300416 dev_dbg(&client->dev, "\n");
Antti Palosaari27cfc852011-04-07 16:27:43 -0300417
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300418 i2c_unregister_device(client);
abdoulaye berthe88d5e522014-07-12 22:30:14 +0200419
Antti Palosaari27cfc852011-04-07 16:27:43 -0300420 return;
421}
422
Steve Kerrison0db4bf42011-08-09 07:16:21 -0300423static int cxd2820r_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300424{
425 struct cxd2820r_priv *priv = fe->demodulator_priv;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300426 struct i2c_client *client = priv->client[0];
Antti Palosaari75aeafc2012-07-19 13:10:12 -0300427
Antti Palosaaric98975f2016-08-09 22:00:37 -0300428 dev_dbg_ratelimited(&client->dev, "enable=%d\n", enable);
Steve Kerrison0db4bf42011-08-09 07:16:21 -0300429
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300430 return regmap_update_bits(priv->regmap[0], 0x00db, 0x01, enable ? 1 : 0);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300431}
Antti Palosaari27cfc852011-04-07 16:27:43 -0300432
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300433#ifdef CONFIG_GPIOLIB
434static int cxd2820r_gpio_direction_output(struct gpio_chip *chip, unsigned nr,
435 int val)
436{
Linus Walleij27524ff2015-12-09 11:46:55 -0200437 struct cxd2820r_priv *priv = gpiochip_get_data(chip);
Antti Palosaaric98975f2016-08-09 22:00:37 -0300438 struct i2c_client *client = priv->client[0];
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300439 u8 gpio[GPIO_COUNT];
440
Antti Palosaaric98975f2016-08-09 22:00:37 -0300441 dev_dbg(&client->dev, "nr=%u val=%d\n", nr, val);
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300442
443 memcpy(gpio, priv->gpio, sizeof(gpio));
444 gpio[nr] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | (val << 2);
445
446 return cxd2820r_gpio(&priv->fe, gpio);
447}
448
449static void cxd2820r_gpio_set(struct gpio_chip *chip, unsigned nr, int val)
450{
Linus Walleij27524ff2015-12-09 11:46:55 -0200451 struct cxd2820r_priv *priv = gpiochip_get_data(chip);
Antti Palosaaric98975f2016-08-09 22:00:37 -0300452 struct i2c_client *client = priv->client[0];
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300453 u8 gpio[GPIO_COUNT];
454
Antti Palosaaric98975f2016-08-09 22:00:37 -0300455 dev_dbg(&client->dev, "nr=%u val=%d\n", nr, val);
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300456
457 memcpy(gpio, priv->gpio, sizeof(gpio));
458 gpio[nr] = CXD2820R_GPIO_E | CXD2820R_GPIO_O | (val << 2);
459
460 (void) cxd2820r_gpio(&priv->fe, gpio);
461
462 return;
463}
464
465static int cxd2820r_gpio_get(struct gpio_chip *chip, unsigned nr)
466{
Linus Walleij27524ff2015-12-09 11:46:55 -0200467 struct cxd2820r_priv *priv = gpiochip_get_data(chip);
Antti Palosaaric98975f2016-08-09 22:00:37 -0300468 struct i2c_client *client = priv->client[0];
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300469
Antti Palosaaric98975f2016-08-09 22:00:37 -0300470 dev_dbg(&client->dev, "nr=%u\n", nr);
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300471
472 return (priv->gpio[nr] >> 2) & 0x01;
473}
474#endif
475
Manu Abraham14c03862011-11-24 11:59:53 -0300476static const struct dvb_frontend_ops cxd2820r_ops = {
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300477 .delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A },
Manu Abraham14c03862011-11-24 11:59:53 -0300478 /* default: DVB-T/T2 */
479 .info = {
Antti Palosaari9bf31ef2012-01-18 13:57:33 -0300480 .name = "Sony CXD2820R",
Manu Abraham14c03862011-11-24 11:59:53 -0300481
482 .caps = FE_CAN_FEC_1_2 |
483 FE_CAN_FEC_2_3 |
484 FE_CAN_FEC_3_4 |
485 FE_CAN_FEC_5_6 |
486 FE_CAN_FEC_7_8 |
487 FE_CAN_FEC_AUTO |
488 FE_CAN_QPSK |
489 FE_CAN_QAM_16 |
Antti Palosaari9bf31ef2012-01-18 13:57:33 -0300490 FE_CAN_QAM_32 |
Manu Abraham14c03862011-11-24 11:59:53 -0300491 FE_CAN_QAM_64 |
Antti Palosaari9bf31ef2012-01-18 13:57:33 -0300492 FE_CAN_QAM_128 |
Manu Abraham14c03862011-11-24 11:59:53 -0300493 FE_CAN_QAM_256 |
494 FE_CAN_QAM_AUTO |
495 FE_CAN_TRANSMISSION_MODE_AUTO |
496 FE_CAN_GUARD_INTERVAL_AUTO |
497 FE_CAN_HIERARCHY_AUTO |
498 FE_CAN_MUTE_TS |
Evgeny Plehov4a747722013-04-07 23:56:46 -0300499 FE_CAN_2G_MODULATION |
500 FE_CAN_MULTISTREAM
Manu Abraham14c03862011-11-24 11:59:53 -0300501 },
502
503 .release = cxd2820r_release,
504 .init = cxd2820r_init,
505 .sleep = cxd2820r_sleep,
506
507 .get_tune_settings = cxd2820r_get_tune_settings,
508 .i2c_gate_ctrl = cxd2820r_i2c_gate_ctrl,
509
Mauro Carvalho Chehabf311f682011-12-30 22:22:10 -0300510 .get_frontend = cxd2820r_get_frontend,
Manu Abraham14c03862011-11-24 11:59:53 -0300511
512 .get_frontend_algo = cxd2820r_get_frontend_algo,
513 .search = cxd2820r_search,
514
515 .read_status = cxd2820r_read_status,
516 .read_snr = cxd2820r_read_snr,
517 .read_ber = cxd2820r_read_ber,
518 .read_ucblocks = cxd2820r_read_ucblocks,
519 .read_signal_strength = cxd2820r_read_signal_strength,
Manu Abraham14c03862011-11-24 11:59:53 -0300520};
Antti Palosaari27cfc852011-04-07 16:27:43 -0300521
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300522/*
523 * XXX: That is wrapper to cxd2820r_probe() via driver core in order to provide
524 * proper I2C client for legacy media attach binding.
525 * New users must use I2C client binding directly!
526 */
527struct dvb_frontend *cxd2820r_attach(const struct cxd2820r_config *config,
528 struct i2c_adapter *adapter,
529 int *gpio_chip_base)
Antti Palosaari27cfc852011-04-07 16:27:43 -0300530{
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300531 struct i2c_client *client;
532 struct i2c_board_info board_info;
533 struct cxd2820r_platform_data pdata;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300534
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300535 pdata.ts_mode = config->ts_mode;
536 pdata.ts_clk_inv = config->ts_clock_inv;
537 pdata.if_agc_polarity = config->if_agc_polarity;
538 pdata.spec_inv = config->spec_inv;
539 pdata.gpio_chip_base = &gpio_chip_base;
540 pdata.attach_in_use = true;
541
542 memset(&board_info, 0, sizeof(board_info));
543 strlcpy(board_info.type, "cxd2820r", I2C_NAME_SIZE);
544 board_info.addr = config->i2c_address;
545 board_info.platform_data = &pdata;
546 client = i2c_new_device(adapter, &board_info);
547 if (!client || !client->dev.driver)
548 return NULL;
549
550 return pdata.get_dvb_frontend(client);
551}
552EXPORT_SYMBOL(cxd2820r_attach);
553
554static struct dvb_frontend *cxd2820r_get_dvb_frontend(struct i2c_client *client)
555{
556 struct cxd2820r_priv *priv = i2c_get_clientdata(client);
557
558 dev_dbg(&client->dev, "\n");
559
560 return &priv->fe;
561}
562
563static int cxd2820r_probe(struct i2c_client *client,
564 const struct i2c_device_id *id)
565{
566 struct cxd2820r_platform_data *pdata = client->dev.platform_data;
567 struct cxd2820r_priv *priv;
568 int ret, *gpio_chip_base;
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300569 unsigned int utmp;
570 static const struct regmap_range_cfg regmap_range_cfg0[] = {
571 {
572 .range_min = 0x0000,
573 .range_max = 0x3fff,
574 .selector_reg = 0x00,
575 .selector_mask = 0xff,
576 .selector_shift = 0,
577 .window_start = 0x00,
578 .window_len = 0x100,
579 },
580 };
581 static const struct regmap_range_cfg regmap_range_cfg1[] = {
582 {
583 .range_min = 0x0000,
584 .range_max = 0x01ff,
585 .selector_reg = 0x00,
586 .selector_mask = 0xff,
587 .selector_shift = 0,
588 .window_start = 0x00,
589 .window_len = 0x100,
590 },
591 };
592 static const struct regmap_config regmap_config0 = {
593 .reg_bits = 8,
594 .val_bits = 8,
595 .max_register = 0x3fff,
596 .ranges = regmap_range_cfg0,
597 .num_ranges = ARRAY_SIZE(regmap_range_cfg0),
598 .cache_type = REGCACHE_NONE,
599 };
600 static const struct regmap_config regmap_config1 = {
601 .reg_bits = 8,
602 .val_bits = 8,
603 .max_register = 0x01ff,
604 .ranges = regmap_range_cfg1,
605 .num_ranges = ARRAY_SIZE(regmap_range_cfg1),
606 .cache_type = REGCACHE_NONE,
607 };
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300608
609 dev_dbg(&client->dev, "\n");
610
611 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300612 if (!priv) {
613 ret = -ENOMEM;
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300614 goto err;
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300615 }
Antti Palosaari27cfc852011-04-07 16:27:43 -0300616
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300617 priv->client[0] = client;
Antti Palosaari238442c2017-01-16 19:27:41 -0200618 priv->fe.demodulator_priv = priv;
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300619 priv->i2c = client->adapter;
620 priv->ts_mode = pdata->ts_mode;
621 priv->ts_clk_inv = pdata->ts_clk_inv;
622 priv->if_agc_polarity = pdata->if_agc_polarity;
623 priv->spec_inv = pdata->spec_inv;
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300624 gpio_chip_base = *pdata->gpio_chip_base;
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300625 priv->regmap[0] = regmap_init_i2c(priv->client[0], &regmap_config0);
626 if (IS_ERR(priv->regmap[0])) {
627 ret = PTR_ERR(priv->regmap[0]);
628 goto err_kfree;
629 }
Antti Palosaari27cfc852011-04-07 16:27:43 -0300630
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300631 /* Check demod answers with correct chip id */
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300632 ret = regmap_read(priv->regmap[0], 0x00fd, &utmp);
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300633 if (ret)
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300634 goto err_regmap_0_regmap_exit;
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300635
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300636 dev_dbg(&client->dev, "chip_id=%02x\n", utmp);
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300637
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300638 if (utmp != 0xe1) {
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300639 ret = -ENODEV;
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300640 goto err_regmap_0_regmap_exit;
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300641 }
642
643 /*
644 * Chip has two I2C addresses for different register banks. We register
645 * one dummy I2C client in in order to get own I2C client for each
646 * register bank.
647 */
648 priv->client[1] = i2c_new_dummy(client->adapter, client->addr | (1 << 1));
649 if (!priv->client[1]) {
650 ret = -ENODEV;
651 dev_err(&client->dev, "I2C registration failed\n");
652 if (ret)
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300653 goto err_regmap_0_regmap_exit;
654 }
655
656 priv->regmap[1] = regmap_init_i2c(priv->client[1], &regmap_config1);
657 if (IS_ERR(priv->regmap[1])) {
658 ret = PTR_ERR(priv->regmap[1]);
659 goto err_client_1_i2c_unregister_device;
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300660 }
Antti Palosaari27cfc852011-04-07 16:27:43 -0300661
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300662 if (gpio_chip_base) {
Antti Palosaaria36a66d2012-08-16 21:07:23 -0300663#ifdef CONFIG_GPIOLIB
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300664 /* Add GPIOs */
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300665 priv->gpio_chip.label = KBUILD_MODNAME;
Antti Palosaaric98975f2016-08-09 22:00:37 -0300666 priv->gpio_chip.parent = &client->dev;
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300667 priv->gpio_chip.owner = THIS_MODULE;
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300668 priv->gpio_chip.direction_output = cxd2820r_gpio_direction_output;
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300669 priv->gpio_chip.set = cxd2820r_gpio_set;
670 priv->gpio_chip.get = cxd2820r_gpio_get;
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300671 priv->gpio_chip.base = -1; /* Dynamic allocation */
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300672 priv->gpio_chip.ngpio = GPIO_COUNT;
673 priv->gpio_chip.can_sleep = 1;
Linus Walleij27524ff2015-12-09 11:46:55 -0200674 ret = gpiochip_add_data(&priv->gpio_chip, priv);
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300675 if (ret)
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300676 goto err_regmap_1_regmap_exit;
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300677
Antti Palosaaric98975f2016-08-09 22:00:37 -0300678 dev_dbg(&client->dev, "gpio_chip.base=%d\n",
679 priv->gpio_chip.base);
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300680
681 *gpio_chip_base = priv->gpio_chip.base;
Antti Palosaaria36a66d2012-08-16 21:07:23 -0300682#else
683 /*
684 * Use static GPIO configuration if GPIOLIB is undefined.
685 * This is fallback condition.
686 */
Antti Palosaari44e0d7d2012-10-05 18:50:12 -0300687 u8 gpio[GPIO_COUNT];
Antti Palosaaria36a66d2012-08-16 21:07:23 -0300688 gpio[0] = (*gpio_chip_base >> 0) & 0x07;
689 gpio[1] = (*gpio_chip_base >> 3) & 0x07;
690 gpio[2] = 0;
691 ret = cxd2820r_gpio(&priv->fe, gpio);
692 if (ret)
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300693 goto err_regmap_1_regmap_exit;
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300694#endif
Antti Palosaaria36a66d2012-08-16 21:07:23 -0300695 }
Antti Palosaari1e8f31f2012-07-19 21:10:36 -0300696
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300697 /* Create dvb frontend */
698 memcpy(&priv->fe.ops, &cxd2820r_ops, sizeof(priv->fe.ops));
699 if (!pdata->attach_in_use)
700 priv->fe.ops.release = NULL;
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300701 i2c_set_clientdata(client, priv);
702
703 /* Setup callbacks */
704 pdata->get_dvb_frontend = cxd2820r_get_dvb_frontend;
705
706 dev_info(&client->dev, "Sony CXD2820R successfully identified\n");
707
708 return 0;
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300709err_regmap_1_regmap_exit:
710 regmap_exit(priv->regmap[1]);
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300711err_client_1_i2c_unregister_device:
712 i2c_unregister_device(priv->client[1]);
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300713err_regmap_0_regmap_exit:
714 regmap_exit(priv->regmap[0]);
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300715err_kfree:
Antti Palosaari27cfc852011-04-07 16:27:43 -0300716 kfree(priv);
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300717err:
718 dev_dbg(&client->dev, "failed=%d\n", ret);
719 return ret;
Antti Palosaari27cfc852011-04-07 16:27:43 -0300720}
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300721
722static int cxd2820r_remove(struct i2c_client *client)
723{
724 struct cxd2820r_priv *priv = i2c_get_clientdata(client);
725
726 dev_dbg(&client->dev, "\n");
727
728#ifdef CONFIG_GPIOLIB
729 if (priv->gpio_chip.label)
730 gpiochip_remove(&priv->gpio_chip);
731#endif
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300732 regmap_exit(priv->regmap[1]);
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300733 i2c_unregister_device(priv->client[1]);
Antti Palosaari43e2ea62016-08-13 13:19:05 -0300734
735 regmap_exit(priv->regmap[0]);
736
Antti Palosaari07fdf7d2016-08-09 20:49:09 -0300737 kfree(priv);
738
739 return 0;
740}
741
742static const struct i2c_device_id cxd2820r_id_table[] = {
743 {"cxd2820r", 0},
744 {}
745};
746MODULE_DEVICE_TABLE(i2c, cxd2820r_id_table);
747
748static struct i2c_driver cxd2820r_driver = {
749 .driver = {
750 .name = "cxd2820r",
751 .suppress_bind_attrs = true,
752 },
753 .probe = cxd2820r_probe,
754 .remove = cxd2820r_remove,
755 .id_table = cxd2820r_id_table,
756};
757
758module_i2c_driver(cxd2820r_driver);
Antti Palosaari27cfc852011-04-07 16:27:43 -0300759
Antti Palosaari27cfc852011-04-07 16:27:43 -0300760MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
761MODULE_DESCRIPTION("Sony CXD2820R demodulator driver");
762MODULE_LICENSE("GPL");