1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Freescale Vybrid vf610 ADC driver
5 * Copyright 2013 Freescale Semiconductor, Inc.
8 #include <linux/mod_devicetable.h>
9 #include <linux/module.h>
10 #include <linux/property.h>
11 #include <linux/platform_device.h>
12 #include <linux/interrupt.h>
13 #include <linux/delay.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
17 #include <linux/clk.h>
18 #include <linux/completion.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/err.h>
22 #include <linux/iio/iio.h>
23 #include <linux/iio/buffer.h>
24 #include <linux/iio/sysfs.h>
25 #include <linux/iio/trigger.h>
26 #include <linux/iio/trigger_consumer.h>
27 #include <linux/iio/triggered_buffer.h>
29 /* This will be the driver name the kernel reports */
30 #define DRIVER_NAME "vf610-adc"
32 /* Vybrid/IMX ADC registers */
33 #define VF610_REG_ADC_HC0 0x00
34 #define VF610_REG_ADC_HC1 0x04
35 #define VF610_REG_ADC_HS 0x08
36 #define VF610_REG_ADC_R0 0x0c
37 #define VF610_REG_ADC_R1 0x10
38 #define VF610_REG_ADC_CFG 0x14
39 #define VF610_REG_ADC_GC 0x18
40 #define VF610_REG_ADC_GS 0x1c
41 #define VF610_REG_ADC_CV 0x20
42 #define VF610_REG_ADC_OFS 0x24
43 #define VF610_REG_ADC_CAL 0x28
44 #define VF610_REG_ADC_PCTL 0x30
46 /* Configuration register field define */
47 #define VF610_ADC_MODE_BIT8 0x00
48 #define VF610_ADC_MODE_BIT10 0x04
49 #define VF610_ADC_MODE_BIT12 0x08
50 #define VF610_ADC_MODE_MASK 0x0c
51 #define VF610_ADC_BUSCLK2_SEL 0x01
52 #define VF610_ADC_ALTCLK_SEL 0x02
53 #define VF610_ADC_ADACK_SEL 0x03
54 #define VF610_ADC_ADCCLK_MASK 0x03
55 #define VF610_ADC_CLK_DIV2 0x20
56 #define VF610_ADC_CLK_DIV4 0x40
57 #define VF610_ADC_CLK_DIV8 0x60
58 #define VF610_ADC_CLK_MASK 0x60
59 #define VF610_ADC_ADLSMP_LONG 0x10
60 #define VF610_ADC_ADSTS_SHORT 0x100
61 #define VF610_ADC_ADSTS_NORMAL 0x200
62 #define VF610_ADC_ADSTS_LONG 0x300
63 #define VF610_ADC_ADSTS_MASK 0x300
64 #define VF610_ADC_ADLPC_EN 0x80
65 #define VF610_ADC_ADHSC_EN 0x400
66 #define VF610_ADC_REFSEL_VALT 0x800
67 #define VF610_ADC_REFSEL_VBG 0x1000
68 #define VF610_ADC_ADTRG_HARD 0x2000
69 #define VF610_ADC_AVGS_8 0x4000
70 #define VF610_ADC_AVGS_16 0x8000
71 #define VF610_ADC_AVGS_32 0xC000
72 #define VF610_ADC_AVGS_MASK 0xC000
73 #define VF610_ADC_OVWREN 0x10000
75 /* General control register field define */
76 #define VF610_ADC_ADACKEN 0x1
77 #define VF610_ADC_DMAEN 0x2
78 #define VF610_ADC_ACREN 0x4
79 #define VF610_ADC_ACFGT 0x8
80 #define VF610_ADC_ACFE 0x10
81 #define VF610_ADC_AVGEN 0x20
82 #define VF610_ADC_ADCON 0x40
83 #define VF610_ADC_CAL 0x80
85 /* Other field define */
86 #define VF610_ADC_ADCHC(x) ((x) & 0x1F)
87 #define VF610_ADC_AIEN (0x1 << 7)
88 #define VF610_ADC_CONV_DISABLE 0x1F
89 #define VF610_ADC_HS_COCO0 0x1
90 #define VF610_ADC_CALF 0x2
91 #define VF610_ADC_TIMEOUT msecs_to_jiffies(100)
93 #define DEFAULT_SAMPLE_TIME 1000
95 /* V at 25°C of 696 mV */
96 #define VF610_VTEMP25_3V0 950
97 /* V at 25°C of 699 mV */
98 #define VF610_VTEMP25_3V3 867
99 /* Typical sensor slope coefficient at all temperatures */
100 #define VF610_TEMP_SLOPE_COEFF 1840
103 VF610_ADCIOC_BUSCLK_SET,
104 VF610_ADCIOC_ALTCLK_SET,
105 VF610_ADCIOC_ADACK_SET,
109 VF610_ADCIOC_VR_VREF_SET,
110 VF610_ADCIOC_VR_VALT_SET,
111 VF610_ADCIOC_VR_VBG_SET,
122 enum conversion_mode_sel {
123 VF610_ADC_CONV_NORMAL,
124 VF610_ADC_CONV_HIGH_SPEED,
125 VF610_ADC_CONV_LOW_POWER,
133 VF610_ADCK_CYCLES_13,
134 VF610_ADCK_CYCLES_17,
135 VF610_ADCK_CYCLES_21,
136 VF610_ADCK_CYCLES_25,
139 struct vf610_adc_feature {
140 enum clk_sel clk_sel;
141 enum vol_ref vol_ref;
142 enum conversion_mode_sel conv_mode;
148 u32 default_sample_time;
161 struct regulator *vref;
163 u32 max_adck_rate[3];
164 struct vf610_adc_feature adc_feature;
166 u32 sample_freq_avail[5];
168 struct completion completion;
169 /* Ensure the timestamp is naturally aligned */
172 s64 timestamp __aligned(8);
176 static const u32 vf610_hw_avgs[] = { 1, 4, 8, 16, 32 };
177 static const u32 vf610_lst_adder[] = { 3, 5, 7, 9, 13, 17, 21, 25 };
179 static inline void vf610_adc_calculate_rates(struct vf610_adc *info)
181 struct vf610_adc_feature *adc_feature = &info->adc_feature;
182 unsigned long adck_rate, ipg_rate = clk_get_rate(info->clk);
183 u32 adck_period, lst_addr_min;
186 adck_rate = info->max_adck_rate[adc_feature->conv_mode];
189 /* calculate clk divider which is within specification */
190 divisor = ipg_rate / adck_rate;
191 adc_feature->clk_div = 1 << fls(divisor + 1);
193 /* fall-back value using a safe divisor */
194 adc_feature->clk_div = 8;
197 adck_rate = ipg_rate / adc_feature->clk_div;
200 * Determine the long sample time adder value to be used based
201 * on the default minimum sample time provided.
203 adck_period = NSEC_PER_SEC / adck_rate;
204 lst_addr_min = adc_feature->default_sample_time / adck_period;
205 for (i = 0; i < ARRAY_SIZE(vf610_lst_adder); i++) {
206 if (vf610_lst_adder[i] > lst_addr_min) {
207 adc_feature->lst_adder_index = i;
213 * Calculate ADC sample frequencies
214 * Sample time unit is ADCK cycles. ADCK clk source is ipg clock,
215 * which is the same as bus clock.
217 * ADC conversion time = SFCAdder + AverageNum x (BCT + LSTAdder)
218 * SFCAdder: fixed to 6 ADCK cycles
219 * AverageNum: 1, 4, 8, 16, 32 samples for hardware average.
220 * BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode
221 * LSTAdder(Long Sample Time): 3, 5, 7, 9, 13, 17, 21, 25 ADCK cycles
223 for (i = 0; i < ARRAY_SIZE(vf610_hw_avgs); i++)
224 info->sample_freq_avail[i] =
225 adck_rate / (6 + vf610_hw_avgs[i] *
226 (25 + vf610_lst_adder[adc_feature->lst_adder_index]));
229 static inline void vf610_adc_cfg_init(struct vf610_adc *info)
231 struct vf610_adc_feature *adc_feature = &info->adc_feature;
233 /* set default Configuration for ADC controller */
234 adc_feature->clk_sel = VF610_ADCIOC_BUSCLK_SET;
235 adc_feature->vol_ref = VF610_ADCIOC_VR_VREF_SET;
237 adc_feature->calibration = true;
238 adc_feature->ovwren = true;
240 adc_feature->res_mode = 12;
241 adc_feature->sample_rate = 1;
243 adc_feature->conv_mode = VF610_ADC_CONV_LOW_POWER;
245 vf610_adc_calculate_rates(info);
248 static void vf610_adc_cfg_post_set(struct vf610_adc *info)
250 struct vf610_adc_feature *adc_feature = &info->adc_feature;
254 switch (adc_feature->clk_sel) {
255 case VF610_ADCIOC_ALTCLK_SET:
256 cfg_data |= VF610_ADC_ALTCLK_SEL;
258 case VF610_ADCIOC_ADACK_SET:
259 cfg_data |= VF610_ADC_ADACK_SEL;
265 /* low power set for calibration */
266 cfg_data |= VF610_ADC_ADLPC_EN;
268 /* enable high speed for calibration */
269 cfg_data |= VF610_ADC_ADHSC_EN;
271 /* voltage reference */
272 switch (adc_feature->vol_ref) {
273 case VF610_ADCIOC_VR_VREF_SET:
275 case VF610_ADCIOC_VR_VALT_SET:
276 cfg_data |= VF610_ADC_REFSEL_VALT;
278 case VF610_ADCIOC_VR_VBG_SET:
279 cfg_data |= VF610_ADC_REFSEL_VBG;
282 dev_err(info->dev, "error voltage reference\n");
285 /* data overwrite enable */
286 if (adc_feature->ovwren)
287 cfg_data |= VF610_ADC_OVWREN;
289 writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
290 writel(gc_data, info->regs + VF610_REG_ADC_GC);
293 static void vf610_adc_calibration(struct vf610_adc *info)
297 if (!info->adc_feature.calibration)
300 /* enable calibration interrupt */
301 hc_cfg = VF610_ADC_AIEN | VF610_ADC_CONV_DISABLE;
302 writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
304 adc_gc = readl(info->regs + VF610_REG_ADC_GC);
305 writel(adc_gc | VF610_ADC_CAL, info->regs + VF610_REG_ADC_GC);
307 if (!wait_for_completion_timeout(&info->completion, VF610_ADC_TIMEOUT))
308 dev_err(info->dev, "Timeout for adc calibration\n");
310 adc_gc = readl(info->regs + VF610_REG_ADC_GS);
311 if (adc_gc & VF610_ADC_CALF)
312 dev_err(info->dev, "ADC calibration failed\n");
314 info->adc_feature.calibration = false;
317 static void vf610_adc_cfg_set(struct vf610_adc *info)
319 struct vf610_adc_feature *adc_feature = &(info->adc_feature);
322 cfg_data = readl(info->regs + VF610_REG_ADC_CFG);
324 cfg_data &= ~VF610_ADC_ADLPC_EN;
325 if (adc_feature->conv_mode == VF610_ADC_CONV_LOW_POWER)
326 cfg_data |= VF610_ADC_ADLPC_EN;
328 cfg_data &= ~VF610_ADC_ADHSC_EN;
329 if (adc_feature->conv_mode == VF610_ADC_CONV_HIGH_SPEED)
330 cfg_data |= VF610_ADC_ADHSC_EN;
332 writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
335 static void vf610_adc_sample_set(struct vf610_adc *info)
337 struct vf610_adc_feature *adc_feature = &(info->adc_feature);
338 int cfg_data, gc_data;
340 cfg_data = readl(info->regs + VF610_REG_ADC_CFG);
341 gc_data = readl(info->regs + VF610_REG_ADC_GC);
343 /* resolution mode */
344 cfg_data &= ~VF610_ADC_MODE_MASK;
345 switch (adc_feature->res_mode) {
347 cfg_data |= VF610_ADC_MODE_BIT8;
350 cfg_data |= VF610_ADC_MODE_BIT10;
353 cfg_data |= VF610_ADC_MODE_BIT12;
356 dev_err(info->dev, "error resolution mode\n");
360 /* clock select and clock divider */
361 cfg_data &= ~(VF610_ADC_CLK_MASK | VF610_ADC_ADCCLK_MASK);
362 switch (adc_feature->clk_div) {
366 cfg_data |= VF610_ADC_CLK_DIV2;
369 cfg_data |= VF610_ADC_CLK_DIV4;
372 cfg_data |= VF610_ADC_CLK_DIV8;
375 switch (adc_feature->clk_sel) {
376 case VF610_ADCIOC_BUSCLK_SET:
377 cfg_data |= VF610_ADC_BUSCLK2_SEL | VF610_ADC_CLK_DIV8;
380 dev_err(info->dev, "error clk divider\n");
387 * Set ADLSMP and ADSTS based on the Long Sample Time Adder value
390 switch (adc_feature->lst_adder_index) {
391 case VF610_ADCK_CYCLES_3:
393 case VF610_ADCK_CYCLES_5:
394 cfg_data |= VF610_ADC_ADSTS_SHORT;
396 case VF610_ADCK_CYCLES_7:
397 cfg_data |= VF610_ADC_ADSTS_NORMAL;
399 case VF610_ADCK_CYCLES_9:
400 cfg_data |= VF610_ADC_ADSTS_LONG;
402 case VF610_ADCK_CYCLES_13:
403 cfg_data |= VF610_ADC_ADLSMP_LONG;
405 case VF610_ADCK_CYCLES_17:
406 cfg_data |= VF610_ADC_ADLSMP_LONG;
407 cfg_data |= VF610_ADC_ADSTS_SHORT;
409 case VF610_ADCK_CYCLES_21:
410 cfg_data |= VF610_ADC_ADLSMP_LONG;
411 cfg_data |= VF610_ADC_ADSTS_NORMAL;
413 case VF610_ADCK_CYCLES_25:
414 cfg_data |= VF610_ADC_ADLSMP_LONG;
415 cfg_data |= VF610_ADC_ADSTS_NORMAL;
418 dev_err(info->dev, "error in sample time select\n");
421 /* update hardware average selection */
422 cfg_data &= ~VF610_ADC_AVGS_MASK;
423 gc_data &= ~VF610_ADC_AVGEN;
424 switch (adc_feature->sample_rate) {
425 case VF610_ADC_SAMPLE_1:
427 case VF610_ADC_SAMPLE_4:
428 gc_data |= VF610_ADC_AVGEN;
430 case VF610_ADC_SAMPLE_8:
431 gc_data |= VF610_ADC_AVGEN;
432 cfg_data |= VF610_ADC_AVGS_8;
434 case VF610_ADC_SAMPLE_16:
435 gc_data |= VF610_ADC_AVGEN;
436 cfg_data |= VF610_ADC_AVGS_16;
438 case VF610_ADC_SAMPLE_32:
439 gc_data |= VF610_ADC_AVGEN;
440 cfg_data |= VF610_ADC_AVGS_32;
444 "error hardware sample average select\n");
447 writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
448 writel(gc_data, info->regs + VF610_REG_ADC_GC);
451 static void vf610_adc_hw_init(struct vf610_adc *info)
453 /* CFG: Feature set */
454 vf610_adc_cfg_post_set(info);
455 vf610_adc_sample_set(info);
457 /* adc calibration */
458 vf610_adc_calibration(info);
460 /* CFG: power and speed set */
461 vf610_adc_cfg_set(info);
464 static int vf610_set_conversion_mode(struct iio_dev *indio_dev,
465 const struct iio_chan_spec *chan,
468 struct vf610_adc *info = iio_priv(indio_dev);
470 mutex_lock(&indio_dev->mlock);
471 info->adc_feature.conv_mode = mode;
472 vf610_adc_calculate_rates(info);
473 vf610_adc_hw_init(info);
474 mutex_unlock(&indio_dev->mlock);
479 static int vf610_get_conversion_mode(struct iio_dev *indio_dev,
480 const struct iio_chan_spec *chan)
482 struct vf610_adc *info = iio_priv(indio_dev);
484 return info->adc_feature.conv_mode;
487 static const char * const vf610_conv_modes[] = { "normal", "high-speed",
490 static const struct iio_enum vf610_conversion_mode = {
491 .items = vf610_conv_modes,
492 .num_items = ARRAY_SIZE(vf610_conv_modes),
493 .get = vf610_get_conversion_mode,
494 .set = vf610_set_conversion_mode,
497 static const struct iio_chan_spec_ext_info vf610_ext_info[] = {
498 IIO_ENUM("conversion_mode", IIO_SHARED_BY_DIR, &vf610_conversion_mode),
502 #define VF610_ADC_CHAN(_idx, _chan_type) { \
503 .type = (_chan_type), \
506 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
507 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
508 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
509 .ext_info = vf610_ext_info, \
510 .scan_index = (_idx), \
518 #define VF610_ADC_TEMPERATURE_CHAN(_idx, _chan_type) { \
519 .type = (_chan_type), \
521 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
522 .scan_index = (_idx), \
530 static const struct iio_chan_spec vf610_adc_iio_channels[] = {
531 VF610_ADC_CHAN(0, IIO_VOLTAGE),
532 VF610_ADC_CHAN(1, IIO_VOLTAGE),
533 VF610_ADC_CHAN(2, IIO_VOLTAGE),
534 VF610_ADC_CHAN(3, IIO_VOLTAGE),
535 VF610_ADC_CHAN(4, IIO_VOLTAGE),
536 VF610_ADC_CHAN(5, IIO_VOLTAGE),
537 VF610_ADC_CHAN(6, IIO_VOLTAGE),
538 VF610_ADC_CHAN(7, IIO_VOLTAGE),
539 VF610_ADC_CHAN(8, IIO_VOLTAGE),
540 VF610_ADC_CHAN(9, IIO_VOLTAGE),
541 VF610_ADC_CHAN(10, IIO_VOLTAGE),
542 VF610_ADC_CHAN(11, IIO_VOLTAGE),
543 VF610_ADC_CHAN(12, IIO_VOLTAGE),
544 VF610_ADC_CHAN(13, IIO_VOLTAGE),
545 VF610_ADC_CHAN(14, IIO_VOLTAGE),
546 VF610_ADC_CHAN(15, IIO_VOLTAGE),
547 VF610_ADC_TEMPERATURE_CHAN(26, IIO_TEMP),
548 IIO_CHAN_SOFT_TIMESTAMP(32),
552 static int vf610_adc_read_data(struct vf610_adc *info)
556 result = readl(info->regs + VF610_REG_ADC_R0);
558 switch (info->adc_feature.res_mode) {
575 static irqreturn_t vf610_adc_isr(int irq, void *dev_id)
577 struct iio_dev *indio_dev = dev_id;
578 struct vf610_adc *info = iio_priv(indio_dev);
581 coco = readl(info->regs + VF610_REG_ADC_HS);
582 if (coco & VF610_ADC_HS_COCO0) {
583 info->value = vf610_adc_read_data(info);
584 if (iio_buffer_enabled(indio_dev)) {
585 info->scan.chan = info->value;
586 iio_push_to_buffers_with_timestamp(indio_dev,
588 iio_get_time_ns(indio_dev));
589 iio_trigger_notify_done(indio_dev->trig);
591 complete(&info->completion);
597 static ssize_t vf610_show_samp_freq_avail(struct device *dev,
598 struct device_attribute *attr, char *buf)
600 struct vf610_adc *info = iio_priv(dev_to_iio_dev(dev));
604 for (i = 0; i < ARRAY_SIZE(info->sample_freq_avail); i++)
605 len += scnprintf(buf + len, PAGE_SIZE - len,
606 "%u ", info->sample_freq_avail[i]);
608 /* replace trailing space by newline */
614 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(vf610_show_samp_freq_avail);
616 static struct attribute *vf610_attributes[] = {
617 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
621 static const struct attribute_group vf610_attribute_group = {
622 .attrs = vf610_attributes,
625 static int vf610_read_raw(struct iio_dev *indio_dev,
626 struct iio_chan_spec const *chan,
631 struct vf610_adc *info = iio_priv(indio_dev);
636 case IIO_CHAN_INFO_RAW:
637 case IIO_CHAN_INFO_PROCESSED:
638 mutex_lock(&indio_dev->mlock);
639 if (iio_buffer_enabled(indio_dev)) {
640 mutex_unlock(&indio_dev->mlock);
644 reinit_completion(&info->completion);
645 hc_cfg = VF610_ADC_ADCHC(chan->channel);
646 hc_cfg |= VF610_ADC_AIEN;
647 writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
648 ret = wait_for_completion_interruptible_timeout
649 (&info->completion, VF610_ADC_TIMEOUT);
651 mutex_unlock(&indio_dev->mlock);
655 mutex_unlock(&indio_dev->mlock);
659 switch (chan->type) {
665 * Calculate in degree Celsius times 1000
666 * Using the typical sensor slope of 1.84 mV/°C
667 * and VREFH_ADC at 3.3V, V at 25°C of 699 mV
669 *val = 25000 - ((int)info->value - VF610_VTEMP25_3V3) *
670 1000000 / VF610_TEMP_SLOPE_COEFF;
674 mutex_unlock(&indio_dev->mlock);
678 mutex_unlock(&indio_dev->mlock);
681 case IIO_CHAN_INFO_SCALE:
682 *val = info->vref_uv / 1000;
683 *val2 = info->adc_feature.res_mode;
684 return IIO_VAL_FRACTIONAL_LOG2;
686 case IIO_CHAN_INFO_SAMP_FREQ:
687 *val = info->sample_freq_avail[info->adc_feature.sample_rate];
698 static int vf610_write_raw(struct iio_dev *indio_dev,
699 struct iio_chan_spec const *chan,
704 struct vf610_adc *info = iio_priv(indio_dev);
708 case IIO_CHAN_INFO_SAMP_FREQ:
710 i < ARRAY_SIZE(info->sample_freq_avail);
712 if (val == info->sample_freq_avail[i]) {
713 info->adc_feature.sample_rate = i;
714 vf610_adc_sample_set(info);
726 static int vf610_adc_buffer_postenable(struct iio_dev *indio_dev)
728 struct vf610_adc *info = iio_priv(indio_dev);
729 unsigned int channel;
732 val = readl(info->regs + VF610_REG_ADC_GC);
733 val |= VF610_ADC_ADCON;
734 writel(val, info->regs + VF610_REG_ADC_GC);
736 channel = find_first_bit(indio_dev->active_scan_mask,
737 indio_dev->masklength);
739 val = VF610_ADC_ADCHC(channel);
740 val |= VF610_ADC_AIEN;
742 writel(val, info->regs + VF610_REG_ADC_HC0);
747 static int vf610_adc_buffer_predisable(struct iio_dev *indio_dev)
749 struct vf610_adc *info = iio_priv(indio_dev);
750 unsigned int hc_cfg = 0;
753 val = readl(info->regs + VF610_REG_ADC_GC);
754 val &= ~VF610_ADC_ADCON;
755 writel(val, info->regs + VF610_REG_ADC_GC);
757 hc_cfg |= VF610_ADC_CONV_DISABLE;
758 hc_cfg &= ~VF610_ADC_AIEN;
760 writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
765 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
766 .postenable = &vf610_adc_buffer_postenable,
767 .predisable = &vf610_adc_buffer_predisable,
768 .validate_scan_mask = &iio_validate_scan_mask_onehot,
771 static int vf610_adc_reg_access(struct iio_dev *indio_dev,
772 unsigned reg, unsigned writeval,
775 struct vf610_adc *info = iio_priv(indio_dev);
777 if ((readval == NULL) ||
778 ((reg % 4) || (reg > VF610_REG_ADC_PCTL)))
781 *readval = readl(info->regs + reg);
786 static const struct iio_info vf610_adc_iio_info = {
787 .read_raw = &vf610_read_raw,
788 .write_raw = &vf610_write_raw,
789 .debugfs_reg_access = &vf610_adc_reg_access,
790 .attrs = &vf610_attribute_group,
793 static const struct of_device_id vf610_adc_match[] = {
794 { .compatible = "fsl,vf610-adc", },
797 MODULE_DEVICE_TABLE(of, vf610_adc_match);
799 static int vf610_adc_probe(struct platform_device *pdev)
801 struct device *dev = &pdev->dev;
802 struct vf610_adc *info;
803 struct iio_dev *indio_dev;
807 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct vf610_adc));
809 dev_err(&pdev->dev, "Failed allocating iio device\n");
813 info = iio_priv(indio_dev);
814 info->dev = &pdev->dev;
816 info->regs = devm_platform_ioremap_resource(pdev, 0);
817 if (IS_ERR(info->regs))
818 return PTR_ERR(info->regs);
820 irq = platform_get_irq(pdev, 0);
824 ret = devm_request_irq(info->dev, irq,
826 dev_name(&pdev->dev), indio_dev);
828 dev_err(&pdev->dev, "failed requesting irq, irq = %d\n", irq);
832 info->clk = devm_clk_get(&pdev->dev, "adc");
833 if (IS_ERR(info->clk)) {
834 dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
836 return PTR_ERR(info->clk);
839 info->vref = devm_regulator_get(&pdev->dev, "vref");
840 if (IS_ERR(info->vref))
841 return PTR_ERR(info->vref);
843 ret = regulator_enable(info->vref);
847 info->vref_uv = regulator_get_voltage(info->vref);
849 device_property_read_u32_array(dev, "fsl,adck-max-frequency", info->max_adck_rate, 3);
851 info->adc_feature.default_sample_time = DEFAULT_SAMPLE_TIME;
852 device_property_read_u32(dev, "min-sample-time", &info->adc_feature.default_sample_time);
854 platform_set_drvdata(pdev, indio_dev);
856 init_completion(&info->completion);
858 indio_dev->name = dev_name(&pdev->dev);
859 indio_dev->info = &vf610_adc_iio_info;
860 indio_dev->modes = INDIO_DIRECT_MODE;
861 indio_dev->channels = vf610_adc_iio_channels;
862 indio_dev->num_channels = ARRAY_SIZE(vf610_adc_iio_channels);
864 ret = clk_prepare_enable(info->clk);
867 "Could not prepare or enable the clock.\n");
868 goto error_adc_clk_enable;
871 vf610_adc_cfg_init(info);
872 vf610_adc_hw_init(info);
874 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
875 NULL, &iio_triggered_buffer_setup_ops);
877 dev_err(&pdev->dev, "Couldn't initialise the buffer\n");
878 goto error_iio_device_register;
881 ret = iio_device_register(indio_dev);
883 dev_err(&pdev->dev, "Couldn't register the device.\n");
884 goto error_adc_buffer_init;
889 error_adc_buffer_init:
890 iio_triggered_buffer_cleanup(indio_dev);
891 error_iio_device_register:
892 clk_disable_unprepare(info->clk);
893 error_adc_clk_enable:
894 regulator_disable(info->vref);
899 static int vf610_adc_remove(struct platform_device *pdev)
901 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
902 struct vf610_adc *info = iio_priv(indio_dev);
904 iio_device_unregister(indio_dev);
905 iio_triggered_buffer_cleanup(indio_dev);
906 regulator_disable(info->vref);
907 clk_disable_unprepare(info->clk);
912 static int vf610_adc_suspend(struct device *dev)
914 struct iio_dev *indio_dev = dev_get_drvdata(dev);
915 struct vf610_adc *info = iio_priv(indio_dev);
918 /* ADC controller enters to stop mode */
919 hc_cfg = readl(info->regs + VF610_REG_ADC_HC0);
920 hc_cfg |= VF610_ADC_CONV_DISABLE;
921 writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
923 clk_disable_unprepare(info->clk);
924 regulator_disable(info->vref);
929 static int vf610_adc_resume(struct device *dev)
931 struct iio_dev *indio_dev = dev_get_drvdata(dev);
932 struct vf610_adc *info = iio_priv(indio_dev);
935 ret = regulator_enable(info->vref);
939 ret = clk_prepare_enable(info->clk);
943 vf610_adc_hw_init(info);
948 regulator_disable(info->vref);
952 static DEFINE_SIMPLE_DEV_PM_OPS(vf610_adc_pm_ops, vf610_adc_suspend,
955 static struct platform_driver vf610_adc_driver = {
956 .probe = vf610_adc_probe,
957 .remove = vf610_adc_remove,
960 .of_match_table = vf610_adc_match,
961 .pm = pm_sleep_ptr(&vf610_adc_pm_ops),
965 module_platform_driver(vf610_adc_driver);
967 MODULE_AUTHOR("Fugang Duan <B38611@freescale.com>");
968 MODULE_DESCRIPTION("Freescale VF610 ADC driver");
969 MODULE_LICENSE("GPL v2");