xref: /openbmc/linux/drivers/iio/adc/sc27xx_adc.c (revision c059ee9d)
1 // SPDX-License-Identifier: GPL-2.0
2 // Copyright (C) 2018 Spreadtrum Communications Inc.
3 
4 #include <linux/hwspinlock.h>
5 #include <linux/iio/iio.h>
6 #include <linux/module.h>
7 #include <linux/nvmem-consumer.h>
8 #include <linux/of.h>
9 #include <linux/of_device.h>
10 #include <linux/platform_device.h>
11 #include <linux/regmap.h>
12 #include <linux/regulator/consumer.h>
13 #include <linux/slab.h>
14 
15 /* PMIC global registers definition */
16 #define SC2730_MODULE_EN		0x1808
17 #define SC2731_MODULE_EN		0xc08
18 #define SC27XX_MODULE_ADC_EN		BIT(5)
19 #define SC2721_ARM_CLK_EN		0xc0c
20 #define SC2730_ARM_CLK_EN		0x180c
21 #define SC2731_ARM_CLK_EN		0xc10
22 #define SC27XX_CLK_ADC_EN		BIT(5)
23 #define SC27XX_CLK_ADC_CLK_EN		BIT(6)
24 
25 /* ADC controller registers definition */
26 #define SC27XX_ADC_CTL			0x0
27 #define SC27XX_ADC_CH_CFG		0x4
28 #define SC27XX_ADC_DATA			0x4c
29 #define SC27XX_ADC_INT_EN		0x50
30 #define SC27XX_ADC_INT_CLR		0x54
31 #define SC27XX_ADC_INT_STS		0x58
32 #define SC27XX_ADC_INT_RAW		0x5c
33 
34 /* Bits and mask definition for SC27XX_ADC_CTL register */
35 #define SC27XX_ADC_EN			BIT(0)
36 #define SC27XX_ADC_CHN_RUN		BIT(1)
37 #define SC27XX_ADC_12BIT_MODE		BIT(2)
38 #define SC27XX_ADC_RUN_NUM_MASK		GENMASK(7, 4)
39 #define SC27XX_ADC_RUN_NUM_SHIFT	4
40 
41 /* Bits and mask definition for SC27XX_ADC_CH_CFG register */
42 #define SC27XX_ADC_CHN_ID_MASK		GENMASK(4, 0)
43 #define SC27XX_ADC_SCALE_MASK		GENMASK(10, 9)
44 #define SC2721_ADC_SCALE_MASK		BIT(5)
45 #define SC27XX_ADC_SCALE_SHIFT		9
46 #define SC2721_ADC_SCALE_SHIFT		5
47 
48 /* Bits definitions for SC27XX_ADC_INT_EN registers */
49 #define SC27XX_ADC_IRQ_EN		BIT(0)
50 
51 /* Bits definitions for SC27XX_ADC_INT_CLR registers */
52 #define SC27XX_ADC_IRQ_CLR		BIT(0)
53 
54 /* Bits definitions for SC27XX_ADC_INT_RAW registers */
55 #define SC27XX_ADC_IRQ_RAW		BIT(0)
56 
57 /* Mask definition for SC27XX_ADC_DATA register */
58 #define SC27XX_ADC_DATA_MASK		GENMASK(11, 0)
59 
60 /* Timeout (ms) for the trylock of hardware spinlocks */
61 #define SC27XX_ADC_HWLOCK_TIMEOUT	5000
62 
63 /* Timeout (us) for ADC data conversion according to ADC datasheet */
64 #define SC27XX_ADC_RDY_TIMEOUT		1000000
65 #define SC27XX_ADC_POLL_RAW_STATUS	500
66 
67 /* Maximum ADC channel number */
68 #define SC27XX_ADC_CHANNEL_MAX		32
69 
70 /* ADC voltage ratio definition */
71 #define SC27XX_VOLT_RATIO(n, d)		\
72 	(((n) << SC27XX_RATIO_NUMERATOR_OFFSET) | (d))
73 #define SC27XX_RATIO_NUMERATOR_OFFSET	16
74 #define SC27XX_RATIO_DENOMINATOR_MASK	GENMASK(15, 0)
75 
76 /* ADC specific channel reference voltage 3.5V */
77 #define SC27XX_ADC_REFVOL_VDD35		3500000
78 
79 /* ADC default channel reference voltage is 2.8V */
80 #define SC27XX_ADC_REFVOL_VDD28		2800000
81 
82 struct sc27xx_adc_data {
83 	struct device *dev;
84 	struct regulator *volref;
85 	struct regmap *regmap;
86 	/*
87 	 * One hardware spinlock to synchronize between the multiple
88 	 * subsystems which will access the unique ADC controller.
89 	 */
90 	struct hwspinlock *hwlock;
91 	int channel_scale[SC27XX_ADC_CHANNEL_MAX];
92 	u32 base;
93 	int irq;
94 	const struct sc27xx_adc_variant_data *var_data;
95 };
96 
97 /*
98  * Since different PMICs of SC27xx series can have different
99  * address and ratio, we should save ratio config and base
100  * in the device data structure.
101  */
102 struct sc27xx_adc_variant_data {
103 	u32 module_en;
104 	u32 clk_en;
105 	u32 scale_shift;
106 	u32 scale_mask;
107 	const struct sc27xx_adc_linear_graph *bscale_cal;
108 	const struct sc27xx_adc_linear_graph *sscale_cal;
109 	void (*init_scale)(struct sc27xx_adc_data *data);
110 	int (*get_ratio)(int channel, int scale);
111 	bool set_volref;
112 };
113 
114 struct sc27xx_adc_linear_graph {
115 	int volt0;
116 	int adc0;
117 	int volt1;
118 	int adc1;
119 };
120 
121 /*
122  * According to the datasheet, we can convert one ADC value to one voltage value
123  * through 2 points in the linear graph. If the voltage is less than 1.2v, we
124  * should use the small-scale graph, and if more than 1.2v, we should use the
125  * big-scale graph.
126  */
127 static struct sc27xx_adc_linear_graph big_scale_graph = {
128 	4200, 3310,
129 	3600, 2832,
130 };
131 
132 static struct sc27xx_adc_linear_graph small_scale_graph = {
133 	1000, 3413,
134 	100, 341,
135 };
136 
137 static const struct sc27xx_adc_linear_graph sc2731_big_scale_graph_calib = {
138 	4200, 850,
139 	3600, 728,
140 };
141 
142 static const struct sc27xx_adc_linear_graph sc2731_small_scale_graph_calib = {
143 	1000, 838,
144 	100, 84,
145 };
146 
147 static const struct sc27xx_adc_linear_graph big_scale_graph_calib = {
148 	4200, 856,
149 	3600, 733,
150 };
151 
152 static const struct sc27xx_adc_linear_graph small_scale_graph_calib = {
153 	1000, 833,
154 	100, 80,
155 };
156 
157 static int sc27xx_adc_get_calib_data(u32 calib_data, int calib_adc)
158 {
159 	return ((calib_data & 0xff) + calib_adc - 128) * 4;
160 }
161 
162 /* get the adc nvmem cell calibration data */
163 static int adc_nvmem_cell_calib_data(struct sc27xx_adc_data *data, const char *cell_name)
164 {
165 	struct nvmem_cell *cell;
166 	void *buf;
167 	u32 origin_calib_data = 0;
168 	size_t len;
169 
170 	if (!data)
171 		return -EINVAL;
172 
173 	cell = nvmem_cell_get(data->dev, cell_name);
174 	if (IS_ERR(cell))
175 		return PTR_ERR(cell);
176 
177 	buf = nvmem_cell_read(cell, &len);
178 	if (IS_ERR(buf)) {
179 		nvmem_cell_put(cell);
180 		return PTR_ERR(buf);
181 	}
182 
183 	memcpy(&origin_calib_data, buf, min(len, sizeof(u32)));
184 
185 	kfree(buf);
186 	nvmem_cell_put(cell);
187 	return origin_calib_data;
188 }
189 
190 static int sc27xx_adc_scale_calibration(struct sc27xx_adc_data *data,
191 					bool big_scale)
192 {
193 	const struct sc27xx_adc_linear_graph *calib_graph;
194 	struct sc27xx_adc_linear_graph *graph;
195 	const char *cell_name;
196 	u32 calib_data = 0;
197 
198 	if (big_scale) {
199 		calib_graph = data->var_data->bscale_cal;
200 		graph = &big_scale_graph;
201 		cell_name = "big_scale_calib";
202 	} else {
203 		calib_graph = data->var_data->sscale_cal;
204 		graph = &small_scale_graph;
205 		cell_name = "small_scale_calib";
206 	}
207 
208 	calib_data = adc_nvmem_cell_calib_data(data, cell_name);
209 
210 	/* Only need to calibrate the adc values in the linear graph. */
211 	graph->adc0 = sc27xx_adc_get_calib_data(calib_data, calib_graph->adc0);
212 	graph->adc1 = sc27xx_adc_get_calib_data(calib_data >> 8,
213 						calib_graph->adc1);
214 
215 	return 0;
216 }
217 
218 static int sc2720_adc_get_ratio(int channel, int scale)
219 {
220 	switch (channel) {
221 	case 14:
222 		switch (scale) {
223 		case 0:
224 			return SC27XX_VOLT_RATIO(68, 900);
225 		case 1:
226 			return SC27XX_VOLT_RATIO(68, 1760);
227 		case 2:
228 			return SC27XX_VOLT_RATIO(68, 2327);
229 		case 3:
230 			return SC27XX_VOLT_RATIO(68, 3654);
231 		default:
232 			return SC27XX_VOLT_RATIO(1, 1);
233 		}
234 	case 16:
235 		switch (scale) {
236 		case 0:
237 			return SC27XX_VOLT_RATIO(48, 100);
238 		case 1:
239 			return SC27XX_VOLT_RATIO(480, 1955);
240 		case 2:
241 			return SC27XX_VOLT_RATIO(480, 2586);
242 		case 3:
243 			return SC27XX_VOLT_RATIO(48, 406);
244 		default:
245 			return SC27XX_VOLT_RATIO(1, 1);
246 		}
247 	case 21:
248 	case 22:
249 	case 23:
250 		switch (scale) {
251 		case 0:
252 			return SC27XX_VOLT_RATIO(3, 8);
253 		case 1:
254 			return SC27XX_VOLT_RATIO(375, 1955);
255 		case 2:
256 			return SC27XX_VOLT_RATIO(375, 2586);
257 		case 3:
258 			return SC27XX_VOLT_RATIO(300, 3248);
259 		default:
260 			return SC27XX_VOLT_RATIO(1, 1);
261 		}
262 	default:
263 		switch (scale) {
264 		case 0:
265 			return SC27XX_VOLT_RATIO(1, 1);
266 		case 1:
267 			return SC27XX_VOLT_RATIO(1000, 1955);
268 		case 2:
269 			return SC27XX_VOLT_RATIO(1000, 2586);
270 		case 3:
271 			return SC27XX_VOLT_RATIO(100, 406);
272 		default:
273 			return SC27XX_VOLT_RATIO(1, 1);
274 		}
275 	}
276 	return SC27XX_VOLT_RATIO(1, 1);
277 }
278 
279 static int sc2721_adc_get_ratio(int channel, int scale)
280 {
281 	switch (channel) {
282 	case 1:
283 	case 2:
284 	case 3:
285 	case 4:
286 		return scale ? SC27XX_VOLT_RATIO(400, 1025) :
287 			SC27XX_VOLT_RATIO(1, 1);
288 	case 5:
289 		return SC27XX_VOLT_RATIO(7, 29);
290 	case 7:
291 	case 9:
292 		return scale ? SC27XX_VOLT_RATIO(100, 125) :
293 			SC27XX_VOLT_RATIO(1, 1);
294 	case 14:
295 		return SC27XX_VOLT_RATIO(68, 900);
296 	case 16:
297 		return SC27XX_VOLT_RATIO(48, 100);
298 	case 19:
299 		return SC27XX_VOLT_RATIO(1, 3);
300 	default:
301 		return SC27XX_VOLT_RATIO(1, 1);
302 	}
303 	return SC27XX_VOLT_RATIO(1, 1);
304 }
305 
306 static int sc2730_adc_get_ratio(int channel, int scale)
307 {
308 	switch (channel) {
309 	case 14:
310 		switch (scale) {
311 		case 0:
312 			return SC27XX_VOLT_RATIO(68, 900);
313 		case 1:
314 			return SC27XX_VOLT_RATIO(68, 1760);
315 		case 2:
316 			return SC27XX_VOLT_RATIO(68, 2327);
317 		case 3:
318 			return SC27XX_VOLT_RATIO(68, 3654);
319 		default:
320 			return SC27XX_VOLT_RATIO(1, 1);
321 		}
322 	case 15:
323 		switch (scale) {
324 		case 0:
325 			return SC27XX_VOLT_RATIO(1, 3);
326 		case 1:
327 			return SC27XX_VOLT_RATIO(1000, 5865);
328 		case 2:
329 			return SC27XX_VOLT_RATIO(500, 3879);
330 		case 3:
331 			return SC27XX_VOLT_RATIO(500, 6090);
332 		default:
333 			return SC27XX_VOLT_RATIO(1, 1);
334 		}
335 	case 16:
336 		switch (scale) {
337 		case 0:
338 			return SC27XX_VOLT_RATIO(48, 100);
339 		case 1:
340 			return SC27XX_VOLT_RATIO(480, 1955);
341 		case 2:
342 			return SC27XX_VOLT_RATIO(480, 2586);
343 		case 3:
344 			return SC27XX_VOLT_RATIO(48, 406);
345 		default:
346 			return SC27XX_VOLT_RATIO(1, 1);
347 		}
348 	case 21:
349 	case 22:
350 	case 23:
351 		switch (scale) {
352 		case 0:
353 			return SC27XX_VOLT_RATIO(3, 8);
354 		case 1:
355 			return SC27XX_VOLT_RATIO(375, 1955);
356 		case 2:
357 			return SC27XX_VOLT_RATIO(375, 2586);
358 		case 3:
359 			return SC27XX_VOLT_RATIO(300, 3248);
360 		default:
361 			return SC27XX_VOLT_RATIO(1, 1);
362 		}
363 	default:
364 		switch (scale) {
365 		case 0:
366 			return SC27XX_VOLT_RATIO(1, 1);
367 		case 1:
368 			return SC27XX_VOLT_RATIO(1000, 1955);
369 		case 2:
370 			return SC27XX_VOLT_RATIO(1000, 2586);
371 		case 3:
372 			return SC27XX_VOLT_RATIO(1000, 4060);
373 		default:
374 			return SC27XX_VOLT_RATIO(1, 1);
375 		}
376 	}
377 	return SC27XX_VOLT_RATIO(1, 1);
378 }
379 
380 static int sc2731_adc_get_ratio(int channel, int scale)
381 {
382 	switch (channel) {
383 	case 1:
384 	case 2:
385 	case 3:
386 	case 4:
387 		return scale ? SC27XX_VOLT_RATIO(400, 1025) :
388 			SC27XX_VOLT_RATIO(1, 1);
389 	case 5:
390 		return SC27XX_VOLT_RATIO(7, 29);
391 	case 6:
392 		return SC27XX_VOLT_RATIO(375, 9000);
393 	case 7:
394 	case 8:
395 		return scale ? SC27XX_VOLT_RATIO(100, 125) :
396 			SC27XX_VOLT_RATIO(1, 1);
397 	case 19:
398 		return SC27XX_VOLT_RATIO(1, 3);
399 	default:
400 		return SC27XX_VOLT_RATIO(1, 1);
401 	}
402 	return SC27XX_VOLT_RATIO(1, 1);
403 }
404 
405 /*
406  * According to the datasheet set specific value on some channel.
407  */
408 static void sc2720_adc_scale_init(struct sc27xx_adc_data *data)
409 {
410 	int i;
411 
412 	for (i = 0; i < SC27XX_ADC_CHANNEL_MAX; i++) {
413 		switch (i) {
414 		case 5:
415 			data->channel_scale[i] = 3;
416 			break;
417 		case 7:
418 		case 9:
419 			data->channel_scale[i] = 2;
420 			break;
421 		case 13:
422 			data->channel_scale[i] = 1;
423 			break;
424 		case 19:
425 		case 30:
426 		case 31:
427 			data->channel_scale[i] = 3;
428 			break;
429 		default:
430 			data->channel_scale[i] = 0;
431 			break;
432 		}
433 	}
434 }
435 
436 static void sc2730_adc_scale_init(struct sc27xx_adc_data *data)
437 {
438 	int i;
439 
440 	for (i = 0; i < SC27XX_ADC_CHANNEL_MAX; i++) {
441 		switch (i) {
442 		case 5:
443 		case 10:
444 		case 19:
445 		case 30:
446 		case 31:
447 			data->channel_scale[i] = 3;
448 			break;
449 		case 7:
450 		case 9:
451 			data->channel_scale[i] = 2;
452 			break;
453 		case 13:
454 			data->channel_scale[i] = 1;
455 			break;
456 		default:
457 			data->channel_scale[i] = 0;
458 			break;
459 		}
460 	}
461 }
462 
463 static void sc2731_adc_scale_init(struct sc27xx_adc_data *data)
464 {
465 	int i;
466 	/*
467 	 * In the current software design, SC2731 support 2 scales,
468 	 * channels 5 uses big scale, others use smale.
469 	 */
470 	for (i = 0; i < SC27XX_ADC_CHANNEL_MAX; i++) {
471 		switch (i) {
472 		case 5:
473 			data->channel_scale[i] = 1;
474 			break;
475 		default:
476 			data->channel_scale[i] = 0;
477 			break;
478 		}
479 	}
480 }
481 
482 static int sc27xx_adc_read(struct sc27xx_adc_data *data, int channel,
483 			   int scale, int *val)
484 {
485 	int ret, ret_volref;
486 	u32 tmp, value, status;
487 
488 	ret = hwspin_lock_timeout_raw(data->hwlock, SC27XX_ADC_HWLOCK_TIMEOUT);
489 	if (ret) {
490 		dev_err(data->dev, "timeout to get the hwspinlock\n");
491 		return ret;
492 	}
493 
494 	/*
495 	 * According to the sc2721 chip data sheet, the reference voltage of
496 	 * specific channel 30 and channel 31 in ADC module needs to be set from
497 	 * the default 2.8v to 3.5v.
498 	 */
499 	if ((data->var_data->set_volref) && (channel == 30 || channel == 31)) {
500 		ret = regulator_set_voltage(data->volref,
501 					SC27XX_ADC_REFVOL_VDD35,
502 					SC27XX_ADC_REFVOL_VDD35);
503 		if (ret) {
504 			dev_err(data->dev, "failed to set the volref 3.5v\n");
505 			goto unlock_adc;
506 		}
507 	}
508 
509 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_CTL,
510 				 SC27XX_ADC_EN, SC27XX_ADC_EN);
511 	if (ret)
512 		goto regulator_restore;
513 
514 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_INT_CLR,
515 				 SC27XX_ADC_IRQ_CLR, SC27XX_ADC_IRQ_CLR);
516 	if (ret)
517 		goto disable_adc;
518 
519 	/* Configure the channel id and scale */
520 	tmp = (scale << data->var_data->scale_shift) & data->var_data->scale_mask;
521 	tmp |= channel & SC27XX_ADC_CHN_ID_MASK;
522 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_CH_CFG,
523 				 SC27XX_ADC_CHN_ID_MASK |
524 				 data->var_data->scale_mask,
525 				 tmp);
526 	if (ret)
527 		goto disable_adc;
528 
529 	/* Select 12bit conversion mode, and only sample 1 time */
530 	tmp = SC27XX_ADC_12BIT_MODE;
531 	tmp |= (0 << SC27XX_ADC_RUN_NUM_SHIFT) & SC27XX_ADC_RUN_NUM_MASK;
532 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_CTL,
533 				 SC27XX_ADC_RUN_NUM_MASK | SC27XX_ADC_12BIT_MODE,
534 				 tmp);
535 	if (ret)
536 		goto disable_adc;
537 
538 	ret = regmap_update_bits(data->regmap, data->base + SC27XX_ADC_CTL,
539 				 SC27XX_ADC_CHN_RUN, SC27XX_ADC_CHN_RUN);
540 	if (ret)
541 		goto disable_adc;
542 
543 	ret = regmap_read_poll_timeout(data->regmap,
544 				       data->base + SC27XX_ADC_INT_RAW,
545 				       status, (status & SC27XX_ADC_IRQ_RAW),
546 				       SC27XX_ADC_POLL_RAW_STATUS,
547 				       SC27XX_ADC_RDY_TIMEOUT);
548 	if (ret) {
549 		dev_err(data->dev, "read adc timeout, status = 0x%x\n", status);
550 		goto disable_adc;
551 	}
552 
553 	ret = regmap_read(data->regmap, data->base + SC27XX_ADC_DATA, &value);
554 	if (ret)
555 		goto disable_adc;
556 
557 	value &= SC27XX_ADC_DATA_MASK;
558 
559 disable_adc:
560 	regmap_update_bits(data->regmap, data->base + SC27XX_ADC_CTL,
561 			   SC27XX_ADC_EN, 0);
562 regulator_restore:
563 	if ((data->var_data->set_volref) && (channel == 30 || channel == 31)) {
564 		ret_volref = regulator_set_voltage(data->volref,
565 					    SC27XX_ADC_REFVOL_VDD28,
566 					    SC27XX_ADC_REFVOL_VDD28);
567 		if (ret_volref) {
568 			dev_err(data->dev, "failed to set the volref 2.8v,ret_volref = 0x%x\n",
569 					 ret_volref);
570 			ret = ret || ret_volref;
571 		}
572 	}
573 unlock_adc:
574 	hwspin_unlock_raw(data->hwlock);
575 
576 	if (!ret)
577 		*val = value;
578 
579 	return ret;
580 }
581 
582 static void sc27xx_adc_volt_ratio(struct sc27xx_adc_data *data,
583 				  int channel, int scale,
584 				  u32 *div_numerator, u32 *div_denominator)
585 {
586 	u32 ratio;
587 
588 	ratio = data->var_data->get_ratio(channel, scale);
589 	*div_numerator = ratio >> SC27XX_RATIO_NUMERATOR_OFFSET;
590 	*div_denominator = ratio & SC27XX_RATIO_DENOMINATOR_MASK;
591 }
592 
593 static int adc_to_volt(struct sc27xx_adc_linear_graph *graph,
594 			      int raw_adc)
595 {
596 	int tmp;
597 
598 	tmp = (graph->volt0 - graph->volt1) * (raw_adc - graph->adc1);
599 	tmp /= (graph->adc0 - graph->adc1);
600 	tmp += graph->volt1;
601 
602 	return tmp;
603 }
604 
605 static int sc27xx_adc_to_volt(struct sc27xx_adc_linear_graph *graph,
606 			      int raw_adc)
607 {
608 	int tmp;
609 
610 	tmp = adc_to_volt(graph, raw_adc);
611 
612 	return tmp < 0 ? 0 : tmp;
613 }
614 
615 static int sc27xx_adc_convert_volt(struct sc27xx_adc_data *data, int channel,
616 				   int scale, int raw_adc)
617 {
618 	u32 numerator, denominator;
619 	u32 volt;
620 
621 	/*
622 	 * Convert ADC values to voltage values according to the linear graph,
623 	 * and channel 5 and channel 1 has been calibrated, so we can just
624 	 * return the voltage values calculated by the linear graph. But other
625 	 * channels need be calculated to the real voltage values with the
626 	 * voltage ratio.
627 	 */
628 	switch (channel) {
629 	case 5:
630 		return sc27xx_adc_to_volt(&big_scale_graph, raw_adc);
631 
632 	case 1:
633 		return sc27xx_adc_to_volt(&small_scale_graph, raw_adc);
634 
635 	default:
636 		volt = sc27xx_adc_to_volt(&small_scale_graph, raw_adc);
637 		break;
638 	}
639 
640 	sc27xx_adc_volt_ratio(data, channel, scale, &numerator, &denominator);
641 
642 	return DIV_ROUND_CLOSEST(volt * denominator, numerator);
643 }
644 
645 static int sc27xx_adc_read_processed(struct sc27xx_adc_data *data,
646 				     int channel, int scale, int *val)
647 {
648 	int ret, raw_adc;
649 
650 	ret = sc27xx_adc_read(data, channel, scale, &raw_adc);
651 	if (ret)
652 		return ret;
653 
654 	*val = sc27xx_adc_convert_volt(data, channel, scale, raw_adc);
655 	return 0;
656 }
657 
658 static int sc27xx_adc_read_raw(struct iio_dev *indio_dev,
659 			       struct iio_chan_spec const *chan,
660 			       int *val, int *val2, long mask)
661 {
662 	struct sc27xx_adc_data *data = iio_priv(indio_dev);
663 	int scale = data->channel_scale[chan->channel];
664 	int ret, tmp;
665 
666 	switch (mask) {
667 	case IIO_CHAN_INFO_RAW:
668 		mutex_lock(&indio_dev->mlock);
669 		ret = sc27xx_adc_read(data, chan->channel, scale, &tmp);
670 		mutex_unlock(&indio_dev->mlock);
671 
672 		if (ret)
673 			return ret;
674 
675 		*val = tmp;
676 		return IIO_VAL_INT;
677 
678 	case IIO_CHAN_INFO_PROCESSED:
679 		mutex_lock(&indio_dev->mlock);
680 		ret = sc27xx_adc_read_processed(data, chan->channel, scale,
681 						&tmp);
682 		mutex_unlock(&indio_dev->mlock);
683 
684 		if (ret)
685 			return ret;
686 
687 		*val = tmp;
688 		return IIO_VAL_INT;
689 
690 	case IIO_CHAN_INFO_SCALE:
691 		*val = scale;
692 		return IIO_VAL_INT;
693 
694 	default:
695 		return -EINVAL;
696 	}
697 }
698 
699 static int sc27xx_adc_write_raw(struct iio_dev *indio_dev,
700 				struct iio_chan_spec const *chan,
701 				int val, int val2, long mask)
702 {
703 	struct sc27xx_adc_data *data = iio_priv(indio_dev);
704 
705 	switch (mask) {
706 	case IIO_CHAN_INFO_SCALE:
707 		data->channel_scale[chan->channel] = val;
708 		return IIO_VAL_INT;
709 
710 	default:
711 		return -EINVAL;
712 	}
713 }
714 
715 static const struct iio_info sc27xx_info = {
716 	.read_raw = &sc27xx_adc_read_raw,
717 	.write_raw = &sc27xx_adc_write_raw,
718 };
719 
720 #define SC27XX_ADC_CHANNEL(index, mask) {			\
721 	.type = IIO_VOLTAGE,					\
722 	.channel = index,					\
723 	.info_mask_separate = mask | BIT(IIO_CHAN_INFO_SCALE),	\
724 	.datasheet_name = "CH##index",				\
725 	.indexed = 1,						\
726 }
727 
728 static const struct iio_chan_spec sc27xx_channels[] = {
729 	SC27XX_ADC_CHANNEL(0, BIT(IIO_CHAN_INFO_PROCESSED)),
730 	SC27XX_ADC_CHANNEL(1, BIT(IIO_CHAN_INFO_PROCESSED)),
731 	SC27XX_ADC_CHANNEL(2, BIT(IIO_CHAN_INFO_PROCESSED)),
732 	SC27XX_ADC_CHANNEL(3, BIT(IIO_CHAN_INFO_PROCESSED)),
733 	SC27XX_ADC_CHANNEL(4, BIT(IIO_CHAN_INFO_PROCESSED)),
734 	SC27XX_ADC_CHANNEL(5, BIT(IIO_CHAN_INFO_PROCESSED)),
735 	SC27XX_ADC_CHANNEL(6, BIT(IIO_CHAN_INFO_PROCESSED)),
736 	SC27XX_ADC_CHANNEL(7, BIT(IIO_CHAN_INFO_PROCESSED)),
737 	SC27XX_ADC_CHANNEL(8, BIT(IIO_CHAN_INFO_PROCESSED)),
738 	SC27XX_ADC_CHANNEL(9, BIT(IIO_CHAN_INFO_PROCESSED)),
739 	SC27XX_ADC_CHANNEL(10, BIT(IIO_CHAN_INFO_PROCESSED)),
740 	SC27XX_ADC_CHANNEL(11, BIT(IIO_CHAN_INFO_PROCESSED)),
741 	SC27XX_ADC_CHANNEL(12, BIT(IIO_CHAN_INFO_PROCESSED)),
742 	SC27XX_ADC_CHANNEL(13, BIT(IIO_CHAN_INFO_PROCESSED)),
743 	SC27XX_ADC_CHANNEL(14, BIT(IIO_CHAN_INFO_PROCESSED)),
744 	SC27XX_ADC_CHANNEL(15, BIT(IIO_CHAN_INFO_PROCESSED)),
745 	SC27XX_ADC_CHANNEL(16, BIT(IIO_CHAN_INFO_PROCESSED)),
746 	SC27XX_ADC_CHANNEL(17, BIT(IIO_CHAN_INFO_PROCESSED)),
747 	SC27XX_ADC_CHANNEL(18, BIT(IIO_CHAN_INFO_PROCESSED)),
748 	SC27XX_ADC_CHANNEL(19, BIT(IIO_CHAN_INFO_PROCESSED)),
749 	SC27XX_ADC_CHANNEL(20, BIT(IIO_CHAN_INFO_RAW)),
750 	SC27XX_ADC_CHANNEL(21, BIT(IIO_CHAN_INFO_PROCESSED)),
751 	SC27XX_ADC_CHANNEL(22, BIT(IIO_CHAN_INFO_PROCESSED)),
752 	SC27XX_ADC_CHANNEL(23, BIT(IIO_CHAN_INFO_PROCESSED)),
753 	SC27XX_ADC_CHANNEL(24, BIT(IIO_CHAN_INFO_PROCESSED)),
754 	SC27XX_ADC_CHANNEL(25, BIT(IIO_CHAN_INFO_PROCESSED)),
755 	SC27XX_ADC_CHANNEL(26, BIT(IIO_CHAN_INFO_PROCESSED)),
756 	SC27XX_ADC_CHANNEL(27, BIT(IIO_CHAN_INFO_PROCESSED)),
757 	SC27XX_ADC_CHANNEL(28, BIT(IIO_CHAN_INFO_PROCESSED)),
758 	SC27XX_ADC_CHANNEL(29, BIT(IIO_CHAN_INFO_PROCESSED)),
759 	SC27XX_ADC_CHANNEL(30, BIT(IIO_CHAN_INFO_PROCESSED)),
760 	SC27XX_ADC_CHANNEL(31, BIT(IIO_CHAN_INFO_PROCESSED)),
761 };
762 
763 static int sc27xx_adc_enable(struct sc27xx_adc_data *data)
764 {
765 	int ret;
766 
767 	ret = regmap_update_bits(data->regmap, data->var_data->module_en,
768 				 SC27XX_MODULE_ADC_EN, SC27XX_MODULE_ADC_EN);
769 	if (ret)
770 		return ret;
771 
772 	/* Enable ADC work clock and controller clock */
773 	ret = regmap_update_bits(data->regmap, data->var_data->clk_en,
774 				 SC27XX_CLK_ADC_EN | SC27XX_CLK_ADC_CLK_EN,
775 				 SC27XX_CLK_ADC_EN | SC27XX_CLK_ADC_CLK_EN);
776 	if (ret)
777 		goto disable_adc;
778 
779 	/* ADC channel scales' calibration from nvmem device */
780 	ret = sc27xx_adc_scale_calibration(data, true);
781 	if (ret)
782 		goto disable_clk;
783 
784 	ret = sc27xx_adc_scale_calibration(data, false);
785 	if (ret)
786 		goto disable_clk;
787 
788 	return 0;
789 
790 disable_clk:
791 	regmap_update_bits(data->regmap, data->var_data->clk_en,
792 			   SC27XX_CLK_ADC_EN | SC27XX_CLK_ADC_CLK_EN, 0);
793 disable_adc:
794 	regmap_update_bits(data->regmap, data->var_data->module_en,
795 			   SC27XX_MODULE_ADC_EN, 0);
796 
797 	return ret;
798 }
799 
800 static void sc27xx_adc_disable(void *_data)
801 {
802 	struct sc27xx_adc_data *data = _data;
803 
804 	/* Disable ADC work clock and controller clock */
805 	regmap_update_bits(data->regmap, data->var_data->clk_en,
806 			   SC27XX_CLK_ADC_EN | SC27XX_CLK_ADC_CLK_EN, 0);
807 
808 	regmap_update_bits(data->regmap, data->var_data->module_en,
809 			   SC27XX_MODULE_ADC_EN, 0);
810 }
811 
812 static const struct sc27xx_adc_variant_data sc2731_data = {
813 	.module_en = SC2731_MODULE_EN,
814 	.clk_en = SC2731_ARM_CLK_EN,
815 	.scale_shift = SC27XX_ADC_SCALE_SHIFT,
816 	.scale_mask = SC27XX_ADC_SCALE_MASK,
817 	.bscale_cal = &sc2731_big_scale_graph_calib,
818 	.sscale_cal = &sc2731_small_scale_graph_calib,
819 	.init_scale = sc2731_adc_scale_init,
820 	.get_ratio = sc2731_adc_get_ratio,
821 	.set_volref = false,
822 };
823 
824 static const struct sc27xx_adc_variant_data sc2730_data = {
825 	.module_en = SC2730_MODULE_EN,
826 	.clk_en = SC2730_ARM_CLK_EN,
827 	.scale_shift = SC27XX_ADC_SCALE_SHIFT,
828 	.scale_mask = SC27XX_ADC_SCALE_MASK,
829 	.bscale_cal = &big_scale_graph_calib,
830 	.sscale_cal = &small_scale_graph_calib,
831 	.init_scale = sc2730_adc_scale_init,
832 	.get_ratio = sc2730_adc_get_ratio,
833 	.set_volref = false,
834 };
835 
836 static const struct sc27xx_adc_variant_data sc2721_data = {
837 	.module_en = SC2731_MODULE_EN,
838 	.clk_en = SC2721_ARM_CLK_EN,
839 	.scale_shift = SC2721_ADC_SCALE_SHIFT,
840 	.scale_mask = SC2721_ADC_SCALE_MASK,
841 	.bscale_cal = &sc2731_big_scale_graph_calib,
842 	.sscale_cal = &sc2731_small_scale_graph_calib,
843 	.init_scale = sc2731_adc_scale_init,
844 	.get_ratio = sc2721_adc_get_ratio,
845 	.set_volref = true,
846 };
847 
848 static const struct sc27xx_adc_variant_data sc2720_data = {
849 	.module_en = SC2731_MODULE_EN,
850 	.clk_en = SC2721_ARM_CLK_EN,
851 	.scale_shift = SC27XX_ADC_SCALE_SHIFT,
852 	.scale_mask = SC27XX_ADC_SCALE_MASK,
853 	.bscale_cal = &big_scale_graph_calib,
854 	.sscale_cal = &small_scale_graph_calib,
855 	.init_scale = sc2720_adc_scale_init,
856 	.get_ratio = sc2720_adc_get_ratio,
857 	.set_volref = false,
858 };
859 
860 static int sc27xx_adc_probe(struct platform_device *pdev)
861 {
862 	struct device *dev = &pdev->dev;
863 	struct device_node *np = dev->of_node;
864 	struct sc27xx_adc_data *sc27xx_data;
865 	const struct sc27xx_adc_variant_data *pdata;
866 	struct iio_dev *indio_dev;
867 	int ret;
868 
869 	pdata = of_device_get_match_data(dev);
870 	if (!pdata) {
871 		dev_err(dev, "No matching driver data found\n");
872 		return -EINVAL;
873 	}
874 
875 	indio_dev = devm_iio_device_alloc(dev, sizeof(*sc27xx_data));
876 	if (!indio_dev)
877 		return -ENOMEM;
878 
879 	sc27xx_data = iio_priv(indio_dev);
880 
881 	sc27xx_data->regmap = dev_get_regmap(dev->parent, NULL);
882 	if (!sc27xx_data->regmap) {
883 		dev_err(dev, "failed to get ADC regmap\n");
884 		return -ENODEV;
885 	}
886 
887 	ret = of_property_read_u32(np, "reg", &sc27xx_data->base);
888 	if (ret) {
889 		dev_err(dev, "failed to get ADC base address\n");
890 		return ret;
891 	}
892 
893 	sc27xx_data->irq = platform_get_irq(pdev, 0);
894 	if (sc27xx_data->irq < 0)
895 		return sc27xx_data->irq;
896 
897 	ret = of_hwspin_lock_get_id(np, 0);
898 	if (ret < 0) {
899 		dev_err(dev, "failed to get hwspinlock id\n");
900 		return ret;
901 	}
902 
903 	sc27xx_data->hwlock = devm_hwspin_lock_request_specific(dev, ret);
904 	if (!sc27xx_data->hwlock) {
905 		dev_err(dev, "failed to request hwspinlock\n");
906 		return -ENXIO;
907 	}
908 
909 	sc27xx_data->dev = dev;
910 	if (pdata->set_volref) {
911 		sc27xx_data->volref = devm_regulator_get(dev, "vref");
912 		if (IS_ERR(sc27xx_data->volref)) {
913 			ret = PTR_ERR(sc27xx_data->volref);
914 			return dev_err_probe(dev, ret, "failed to get ADC volref\n");
915 		}
916 	}
917 
918 	sc27xx_data->var_data = pdata;
919 	sc27xx_data->var_data->init_scale(sc27xx_data);
920 
921 	ret = sc27xx_adc_enable(sc27xx_data);
922 	if (ret) {
923 		dev_err(dev, "failed to enable ADC module\n");
924 		return ret;
925 	}
926 
927 	ret = devm_add_action_or_reset(dev, sc27xx_adc_disable, sc27xx_data);
928 	if (ret) {
929 		dev_err(dev, "failed to add ADC disable action\n");
930 		return ret;
931 	}
932 
933 	indio_dev->name = dev_name(dev);
934 	indio_dev->modes = INDIO_DIRECT_MODE;
935 	indio_dev->info = &sc27xx_info;
936 	indio_dev->channels = sc27xx_channels;
937 	indio_dev->num_channels = ARRAY_SIZE(sc27xx_channels);
938 	ret = devm_iio_device_register(dev, indio_dev);
939 	if (ret)
940 		dev_err(dev, "could not register iio (ADC)");
941 
942 	return ret;
943 }
944 
945 static const struct of_device_id sc27xx_adc_of_match[] = {
946 	{ .compatible = "sprd,sc2731-adc", .data = &sc2731_data},
947 	{ .compatible = "sprd,sc2730-adc", .data = &sc2730_data},
948 	{ .compatible = "sprd,sc2721-adc", .data = &sc2721_data},
949 	{ .compatible = "sprd,sc2720-adc", .data = &sc2720_data},
950 	{ }
951 };
952 MODULE_DEVICE_TABLE(of, sc27xx_adc_of_match);
953 
954 static struct platform_driver sc27xx_adc_driver = {
955 	.probe = sc27xx_adc_probe,
956 	.driver = {
957 		.name = "sc27xx-adc",
958 		.of_match_table = sc27xx_adc_of_match,
959 	},
960 };
961 
962 module_platform_driver(sc27xx_adc_driver);
963 
964 MODULE_AUTHOR("Freeman Liu <freeman.liu@spreadtrum.com>");
965 MODULE_DESCRIPTION("Spreadtrum SC27XX ADC Driver");
966 MODULE_LICENSE("GPL v2");
967