1 /*
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 
24 /* Write register table */
25 int cxd2820r_wr_reg_val_mask_tab(struct cxd2820r_priv *priv,
26 				 const struct reg_val_mask *tab, int tab_len)
27 {
28 	struct i2c_client *client = priv->client[0];
29 	int ret;
30 	unsigned int i, reg, mask, val;
31 	struct regmap *regmap;
32 
33 	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;
51 	}
52 
53 	return 0;
54 error:
55 	dev_dbg(&client->dev, "failed=%d\n", ret);
56 	return ret;
57 }
58 
59 int cxd2820r_gpio(struct dvb_frontend *fe, u8 *gpio)
60 {
61 	struct cxd2820r_priv *priv = fe->demodulator_priv;
62 	struct i2c_client *client = priv->client[0];
63 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
64 	int ret, i;
65 	u8 tmp0, tmp1;
66 
67 	dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
68 
69 	/* 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 
94 		dev_dbg(&client->dev, "gpio i=%d %02x %02x\n", i, tmp0, tmp1);
95 	}
96 
97 	dev_dbg(&client->dev, "wr gpio=%02x %02x\n", tmp0, tmp1);
98 
99 	/* write bits [7:2] */
100 	ret = regmap_update_bits(priv->regmap[0], 0x0089, 0xfc, tmp0);
101 	if (ret)
102 		goto error;
103 
104 	/* write bits [5:0] */
105 	ret = regmap_update_bits(priv->regmap[0], 0x008e, 0x3f, tmp1);
106 	if (ret)
107 		goto error;
108 
109 	memcpy(priv->gpio, gpio, sizeof(priv->gpio));
110 
111 	return ret;
112 error:
113 	dev_dbg(&client->dev, "failed=%d\n", ret);
114 	return ret;
115 }
116 
117 static int cxd2820r_set_frontend(struct dvb_frontend *fe)
118 {
119 	struct cxd2820r_priv *priv = fe->demodulator_priv;
120 	struct i2c_client *client = priv->client[0];
121 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
122 	int ret;
123 
124 	dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
125 
126 	switch (c->delivery_system) {
127 	case SYS_DVBT:
128 		ret = cxd2820r_init_t(fe);
129 		if (ret < 0)
130 			goto err;
131 		ret = cxd2820r_set_frontend_t(fe);
132 		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;
139 		ret = cxd2820r_set_frontend_t2(fe);
140 		if (ret < 0)
141 			goto err;
142 		break;
143 	case SYS_DVBC_ANNEX_A:
144 		ret = cxd2820r_init_c(fe);
145 		if (ret < 0)
146 			goto err;
147 		ret = cxd2820r_set_frontend_c(fe);
148 		if (ret < 0)
149 			goto err;
150 		break;
151 	default:
152 		dev_dbg(&client->dev, "invalid delivery_system\n");
153 		ret = -EINVAL;
154 		break;
155 	}
156 err:
157 	return ret;
158 }
159 
160 static int cxd2820r_read_status(struct dvb_frontend *fe, enum fe_status *status)
161 {
162 	struct cxd2820r_priv *priv = fe->demodulator_priv;
163 	struct i2c_client *client = priv->client[0];
164 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
165 	int ret;
166 
167 	dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
168 
169 	switch (c->delivery_system) {
170 	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;
176 	case SYS_DVBC_ANNEX_A:
177 		ret = cxd2820r_read_status_c(fe, status);
178 		break;
179 	default:
180 		ret = -EINVAL;
181 		break;
182 	}
183 	return ret;
184 }
185 
186 static int cxd2820r_get_frontend(struct dvb_frontend *fe,
187 				 struct dtv_frontend_properties *p)
188 {
189 	struct cxd2820r_priv *priv = fe->demodulator_priv;
190 	struct i2c_client *client = priv->client[0];
191 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
192 	int ret;
193 
194 	dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
195 
196 	if (priv->delivery_system == SYS_UNDEFINED)
197 		return 0;
198 
199 	switch (c->delivery_system) {
200 	case SYS_DVBT:
201 		ret = cxd2820r_get_frontend_t(fe, p);
202 		break;
203 	case SYS_DVBT2:
204 		ret = cxd2820r_get_frontend_t2(fe, p);
205 		break;
206 	case SYS_DVBC_ANNEX_A:
207 		ret = cxd2820r_get_frontend_c(fe, p);
208 		break;
209 	default:
210 		ret = -EINVAL;
211 		break;
212 	}
213 	return ret;
214 }
215 
216 static int cxd2820r_read_ber(struct dvb_frontend *fe, u32 *ber)
217 {
218 	struct cxd2820r_priv *priv = fe->demodulator_priv;
219 	struct i2c_client *client = priv->client[0];
220 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
221 
222 	dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
223 
224 	*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;
228 }
229 
230 static int cxd2820r_read_signal_strength(struct dvb_frontend *fe, u16 *strength)
231 {
232 	struct cxd2820r_priv *priv = fe->demodulator_priv;
233 	struct i2c_client *client = priv->client[0];
234 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
235 
236 	dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
237 
238 	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;
244 }
245 
246 static int cxd2820r_read_snr(struct dvb_frontend *fe, u16 *snr)
247 {
248 	struct cxd2820r_priv *priv = fe->demodulator_priv;
249 	struct i2c_client *client = priv->client[0];
250 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
251 
252 	dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
253 
254 	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;
260 }
261 
262 static int cxd2820r_read_ucblocks(struct dvb_frontend *fe, u32 *ucblocks)
263 {
264 	struct cxd2820r_priv *priv = fe->demodulator_priv;
265 	struct i2c_client *client = priv->client[0];
266 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
267 
268 	dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
269 
270 	*ucblocks = 0;
271 
272 	return 0;
273 }
274 
275 static int cxd2820r_init(struct dvb_frontend *fe)
276 {
277 	return 0;
278 }
279 
280 static int cxd2820r_sleep(struct dvb_frontend *fe)
281 {
282 	struct cxd2820r_priv *priv = fe->demodulator_priv;
283 	struct i2c_client *client = priv->client[0];
284 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
285 	int ret;
286 
287 	dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
288 
289 	switch (c->delivery_system) {
290 	case SYS_DVBT:
291 		ret = cxd2820r_sleep_t(fe);
292 		break;
293 	case SYS_DVBT2:
294 		ret = cxd2820r_sleep_t2(fe);
295 		break;
296 	case SYS_DVBC_ANNEX_A:
297 		ret = cxd2820r_sleep_c(fe);
298 		break;
299 	default:
300 		ret = -EINVAL;
301 		break;
302 	}
303 	return ret;
304 }
305 
306 static int cxd2820r_get_tune_settings(struct dvb_frontend *fe,
307 				      struct dvb_frontend_tune_settings *s)
308 {
309 	struct cxd2820r_priv *priv = fe->demodulator_priv;
310 	struct i2c_client *client = priv->client[0];
311 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
312 	int ret;
313 
314 	dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
315 
316 	switch (c->delivery_system) {
317 	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;
323 	case SYS_DVBC_ANNEX_A:
324 		ret = cxd2820r_get_tune_settings_c(fe, s);
325 		break;
326 	default:
327 		ret = -EINVAL;
328 		break;
329 	}
330 	return ret;
331 }
332 
333 static enum dvbfe_search cxd2820r_search(struct dvb_frontend *fe)
334 {
335 	struct cxd2820r_priv *priv = fe->demodulator_priv;
336 	struct i2c_client *client = priv->client[0];
337 	struct dtv_frontend_properties *c = &fe->dtv_property_cache;
338 	int ret, i;
339 	enum fe_status status = 0;
340 
341 	dev_dbg(&client->dev, "delivery_system=%d\n", c->delivery_system);
342 
343 	/* switch between DVB-T and DVB-T2 when tune fails */
344 	if (priv->last_tune_failed) {
345 		if (priv->delivery_system == SYS_DVBT) {
346 			ret = cxd2820r_sleep_t(fe);
347 			if (ret)
348 				goto error;
349 
350 			c->delivery_system = SYS_DVBT2;
351 		} else if (priv->delivery_system == SYS_DVBT2) {
352 			ret = cxd2820r_sleep_t2(fe);
353 			if (ret)
354 				goto error;
355 
356 			c->delivery_system = SYS_DVBT;
357 		}
358 	}
359 
360 	/* set frontend */
361 	ret = cxd2820r_set_frontend(fe);
362 	if (ret)
363 		goto error;
364 
365 	/* frontend lock wait loop count */
366 	switch (priv->delivery_system) {
367 	case SYS_DVBT:
368 	case SYS_DVBC_ANNEX_A:
369 		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--) {
382 		dev_dbg(&client->dev, "loop=%d\n", i);
383 		msleep(50);
384 		ret = cxd2820r_read_status(fe, &status);
385 		if (ret)
386 			goto error;
387 
388 		if (status & FE_HAS_LOCK)
389 			break;
390 	}
391 
392 	/* check if we have a valid signal */
393 	if (status & FE_HAS_LOCK) {
394 		priv->last_tune_failed = false;
395 		return DVBFE_ALGO_SEARCH_SUCCESS;
396 	} else {
397 		priv->last_tune_failed = true;
398 		return DVBFE_ALGO_SEARCH_AGAIN;
399 	}
400 
401 error:
402 	dev_dbg(&client->dev, "failed=%d\n", ret);
403 	return DVBFE_ALGO_SEARCH_ERROR;
404 }
405 
406 static int cxd2820r_get_frontend_algo(struct dvb_frontend *fe)
407 {
408 	return DVBFE_ALGO_CUSTOM;
409 }
410 
411 static void cxd2820r_release(struct dvb_frontend *fe)
412 {
413 	struct cxd2820r_priv *priv = fe->demodulator_priv;
414 	struct i2c_client *client = priv->client[0];
415 
416 	dev_dbg(&client->dev, "\n");
417 
418 	i2c_unregister_device(client);
419 
420 	return;
421 }
422 
423 static int cxd2820r_i2c_gate_ctrl(struct dvb_frontend *fe, int enable)
424 {
425 	struct cxd2820r_priv *priv = fe->demodulator_priv;
426 	struct i2c_client *client = priv->client[0];
427 
428 	dev_dbg_ratelimited(&client->dev, "enable=%d\n", enable);
429 
430 	return regmap_update_bits(priv->regmap[0], 0x00db, 0x01, enable ? 1 : 0);
431 }
432 
433 #ifdef CONFIG_GPIOLIB
434 static int cxd2820r_gpio_direction_output(struct gpio_chip *chip, unsigned nr,
435 		int val)
436 {
437 	struct cxd2820r_priv *priv = gpiochip_get_data(chip);
438 	struct i2c_client *client = priv->client[0];
439 	u8 gpio[GPIO_COUNT];
440 
441 	dev_dbg(&client->dev, "nr=%u val=%d\n", nr, val);
442 
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 
449 static void cxd2820r_gpio_set(struct gpio_chip *chip, unsigned nr, int val)
450 {
451 	struct cxd2820r_priv *priv = gpiochip_get_data(chip);
452 	struct i2c_client *client = priv->client[0];
453 	u8 gpio[GPIO_COUNT];
454 
455 	dev_dbg(&client->dev, "nr=%u val=%d\n", nr, val);
456 
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 
465 static int cxd2820r_gpio_get(struct gpio_chip *chip, unsigned nr)
466 {
467 	struct cxd2820r_priv *priv = gpiochip_get_data(chip);
468 	struct i2c_client *client = priv->client[0];
469 
470 	dev_dbg(&client->dev, "nr=%u\n", nr);
471 
472 	return (priv->gpio[nr] >> 2) & 0x01;
473 }
474 #endif
475 
476 static const struct dvb_frontend_ops cxd2820r_ops = {
477 	.delsys = { SYS_DVBT, SYS_DVBT2, SYS_DVBC_ANNEX_A },
478 	/* default: DVB-T/T2 */
479 	.info = {
480 		.name = "Sony CXD2820R",
481 
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			|
490 			FE_CAN_QAM_32			|
491 			FE_CAN_QAM_64			|
492 			FE_CAN_QAM_128			|
493 			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			|
499 			FE_CAN_2G_MODULATION		|
500 			FE_CAN_MULTISTREAM
501 		},
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 
510 	.get_frontend		= cxd2820r_get_frontend,
511 
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,
520 };
521 
522 /*
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  */
527 struct dvb_frontend *cxd2820r_attach(const struct cxd2820r_config *config,
528 				     struct i2c_adapter *adapter,
529 				     int *gpio_chip_base)
530 {
531 	struct i2c_client *client;
532 	struct i2c_board_info board_info;
533 	struct cxd2820r_platform_data pdata;
534 
535 	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 }
552 EXPORT_SYMBOL(cxd2820r_attach);
553 
554 static 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 
563 static 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;
569 	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 	};
608 
609 	dev_dbg(&client->dev, "\n");
610 
611 	priv = kzalloc(sizeof(*priv), GFP_KERNEL);
612 	if (!priv) {
613 		ret = -ENOMEM;
614 		goto err;
615 	}
616 
617 	priv->client[0] = client;
618 	priv->fe.demodulator_priv = priv;
619 	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;
624 	gpio_chip_base = *pdata->gpio_chip_base;
625 	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 	}
630 
631 	/* Check demod answers with correct chip id */
632 	ret = regmap_read(priv->regmap[0], 0x00fd, &utmp);
633 	if (ret)
634 		goto err_regmap_0_regmap_exit;
635 
636 	dev_dbg(&client->dev, "chip_id=%02x\n", utmp);
637 
638 	if (utmp != 0xe1) {
639 		ret = -ENODEV;
640 		goto err_regmap_0_regmap_exit;
641 	}
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)
653 			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;
660 	}
661 
662 	if (gpio_chip_base) {
663 #ifdef CONFIG_GPIOLIB
664 		/* Add GPIOs */
665 		priv->gpio_chip.label = KBUILD_MODNAME;
666 		priv->gpio_chip.parent = &client->dev;
667 		priv->gpio_chip.owner = THIS_MODULE;
668 		priv->gpio_chip.direction_output = cxd2820r_gpio_direction_output;
669 		priv->gpio_chip.set = cxd2820r_gpio_set;
670 		priv->gpio_chip.get = cxd2820r_gpio_get;
671 		priv->gpio_chip.base = -1; /* Dynamic allocation */
672 		priv->gpio_chip.ngpio = GPIO_COUNT;
673 		priv->gpio_chip.can_sleep = 1;
674 		ret = gpiochip_add_data(&priv->gpio_chip, priv);
675 		if (ret)
676 			goto err_regmap_1_regmap_exit;
677 
678 		dev_dbg(&client->dev, "gpio_chip.base=%d\n",
679 			priv->gpio_chip.base);
680 
681 		*gpio_chip_base = priv->gpio_chip.base;
682 #else
683 		/*
684 		 * Use static GPIO configuration if GPIOLIB is undefined.
685 		 * This is fallback condition.
686 		 */
687 		u8 gpio[GPIO_COUNT];
688 		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)
693 			goto err_regmap_1_regmap_exit;
694 #endif
695 	}
696 
697 	/* 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;
701 	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;
709 err_regmap_1_regmap_exit:
710 	regmap_exit(priv->regmap[1]);
711 err_client_1_i2c_unregister_device:
712 	i2c_unregister_device(priv->client[1]);
713 err_regmap_0_regmap_exit:
714 	regmap_exit(priv->regmap[0]);
715 err_kfree:
716 	kfree(priv);
717 err:
718 	dev_dbg(&client->dev, "failed=%d\n", ret);
719 	return ret;
720 }
721 
722 static 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
732 	regmap_exit(priv->regmap[1]);
733 	i2c_unregister_device(priv->client[1]);
734 
735 	regmap_exit(priv->regmap[0]);
736 
737 	kfree(priv);
738 
739 	return 0;
740 }
741 
742 static const struct i2c_device_id cxd2820r_id_table[] = {
743 	{"cxd2820r", 0},
744 	{}
745 };
746 MODULE_DEVICE_TABLE(i2c, cxd2820r_id_table);
747 
748 static 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 
758 module_i2c_driver(cxd2820r_driver);
759 
760 MODULE_AUTHOR("Antti Palosaari <crope@iki.fi>");
761 MODULE_DESCRIPTION("Sony CXD2820R demodulator driver");
762 MODULE_LICENSE("GPL");
763