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/mutex.h>
11 #include <linux/property.h>
12 #include <linux/platform_device.h>
13 #include <linux/interrupt.h>
14 #include <linux/delay.h>
15 #include <linux/kernel.h>
16 #include <linux/slab.h>
18 #include <linux/clk.h>
19 #include <linux/completion.h>
20 #include <linux/regulator/consumer.h>
21 #include <linux/err.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/sysfs.h>
26 #include <linux/iio/trigger.h>
27 #include <linux/iio/trigger_consumer.h>
28 #include <linux/iio/triggered_buffer.h>
30 /* This will be the driver name the kernel reports */
31 #define DRIVER_NAME "vf610-adc"
33 /* Vybrid/IMX ADC registers */
34 #define VF610_REG_ADC_HC0 0x00
35 #define VF610_REG_ADC_HC1 0x04
36 #define VF610_REG_ADC_HS 0x08
37 #define VF610_REG_ADC_R0 0x0c
38 #define VF610_REG_ADC_R1 0x10
39 #define VF610_REG_ADC_CFG 0x14
40 #define VF610_REG_ADC_GC 0x18
41 #define VF610_REG_ADC_GS 0x1c
42 #define VF610_REG_ADC_CV 0x20
43 #define VF610_REG_ADC_OFS 0x24
44 #define VF610_REG_ADC_CAL 0x28
45 #define VF610_REG_ADC_PCTL 0x30
47 /* Configuration register field define */
48 #define VF610_ADC_MODE_BIT8 0x00
49 #define VF610_ADC_MODE_BIT10 0x04
50 #define VF610_ADC_MODE_BIT12 0x08
51 #define VF610_ADC_MODE_MASK 0x0c
52 #define VF610_ADC_BUSCLK2_SEL 0x01
53 #define VF610_ADC_ALTCLK_SEL 0x02
54 #define VF610_ADC_ADACK_SEL 0x03
55 #define VF610_ADC_ADCCLK_MASK 0x03
56 #define VF610_ADC_CLK_DIV2 0x20
57 #define VF610_ADC_CLK_DIV4 0x40
58 #define VF610_ADC_CLK_DIV8 0x60
59 #define VF610_ADC_CLK_MASK 0x60
60 #define VF610_ADC_ADLSMP_LONG 0x10
61 #define VF610_ADC_ADSTS_SHORT 0x100
62 #define VF610_ADC_ADSTS_NORMAL 0x200
63 #define VF610_ADC_ADSTS_LONG 0x300
64 #define VF610_ADC_ADSTS_MASK 0x300
65 #define VF610_ADC_ADLPC_EN 0x80
66 #define VF610_ADC_ADHSC_EN 0x400
67 #define VF610_ADC_REFSEL_VALT 0x800
68 #define VF610_ADC_REFSEL_VBG 0x1000
69 #define VF610_ADC_ADTRG_HARD 0x2000
70 #define VF610_ADC_AVGS_8 0x4000
71 #define VF610_ADC_AVGS_16 0x8000
72 #define VF610_ADC_AVGS_32 0xC000
73 #define VF610_ADC_AVGS_MASK 0xC000
74 #define VF610_ADC_OVWREN 0x10000
76 /* General control register field define */
77 #define VF610_ADC_ADACKEN 0x1
78 #define VF610_ADC_DMAEN 0x2
79 #define VF610_ADC_ACREN 0x4
80 #define VF610_ADC_ACFGT 0x8
81 #define VF610_ADC_ACFE 0x10
82 #define VF610_ADC_AVGEN 0x20
83 #define VF610_ADC_ADCON 0x40
84 #define VF610_ADC_CAL 0x80
86 /* Other field define */
87 #define VF610_ADC_ADCHC(x) ((x) & 0x1F)
88 #define VF610_ADC_AIEN (0x1 << 7)
89 #define VF610_ADC_CONV_DISABLE 0x1F
90 #define VF610_ADC_HS_COCO0 0x1
91 #define VF610_ADC_CALF 0x2
92 #define VF610_ADC_TIMEOUT msecs_to_jiffies(100)
94 #define DEFAULT_SAMPLE_TIME 1000
96 /* V at 25°C of 696 mV */
97 #define VF610_VTEMP25_3V0 950
98 /* V at 25°C of 699 mV */
99 #define VF610_VTEMP25_3V3 867
100 /* Typical sensor slope coefficient at all temperatures */
101 #define VF610_TEMP_SLOPE_COEFF 1840
104 VF610_ADCIOC_BUSCLK_SET,
105 VF610_ADCIOC_ALTCLK_SET,
106 VF610_ADCIOC_ADACK_SET,
110 VF610_ADCIOC_VR_VREF_SET,
111 VF610_ADCIOC_VR_VALT_SET,
112 VF610_ADCIOC_VR_VBG_SET,
123 enum conversion_mode_sel {
124 VF610_ADC_CONV_NORMAL,
125 VF610_ADC_CONV_HIGH_SPEED,
126 VF610_ADC_CONV_LOW_POWER,
134 VF610_ADCK_CYCLES_13,
135 VF610_ADCK_CYCLES_17,
136 VF610_ADCK_CYCLES_21,
137 VF610_ADCK_CYCLES_25,
140 struct vf610_adc_feature {
141 enum clk_sel clk_sel;
142 enum vol_ref vol_ref;
143 enum conversion_mode_sel conv_mode;
149 u32 default_sample_time;
160 /* lock to protect against multiple access to the device */
165 struct regulator *vref;
167 u32 max_adck_rate[3];
168 struct vf610_adc_feature adc_feature;
170 u32 sample_freq_avail[5];
172 struct completion completion;
173 /* Ensure the timestamp is naturally aligned */
176 s64 timestamp __aligned(8);
180 static const u32 vf610_hw_avgs[] = { 1, 4, 8, 16, 32 };
181 static const u32 vf610_lst_adder[] = { 3, 5, 7, 9, 13, 17, 21, 25 };
183 static inline void vf610_adc_calculate_rates(struct vf610_adc *info)
185 struct vf610_adc_feature *adc_feature = &info->adc_feature;
186 unsigned long adck_rate, ipg_rate = clk_get_rate(info->clk);
187 u32 adck_period, lst_addr_min;
190 adck_rate = info->max_adck_rate[adc_feature->conv_mode];
193 /* calculate clk divider which is within specification */
194 divisor = ipg_rate / adck_rate;
195 adc_feature->clk_div = 1 << fls(divisor + 1);
197 /* fall-back value using a safe divisor */
198 adc_feature->clk_div = 8;
201 adck_rate = ipg_rate / adc_feature->clk_div;
204 * Determine the long sample time adder value to be used based
205 * on the default minimum sample time provided.
207 adck_period = NSEC_PER_SEC / adck_rate;
208 lst_addr_min = adc_feature->default_sample_time / adck_period;
209 for (i = 0; i < ARRAY_SIZE(vf610_lst_adder); i++) {
210 if (vf610_lst_adder[i] > lst_addr_min) {
211 adc_feature->lst_adder_index = i;
217 * Calculate ADC sample frequencies
218 * Sample time unit is ADCK cycles. ADCK clk source is ipg clock,
219 * which is the same as bus clock.
221 * ADC conversion time = SFCAdder + AverageNum x (BCT + LSTAdder)
222 * SFCAdder: fixed to 6 ADCK cycles
223 * AverageNum: 1, 4, 8, 16, 32 samples for hardware average.
224 * BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode
225 * LSTAdder(Long Sample Time): 3, 5, 7, 9, 13, 17, 21, 25 ADCK cycles
227 for (i = 0; i < ARRAY_SIZE(vf610_hw_avgs); i++)
228 info->sample_freq_avail[i] =
229 adck_rate / (6 + vf610_hw_avgs[i] *
230 (25 + vf610_lst_adder[adc_feature->lst_adder_index]));
233 static inline void vf610_adc_cfg_init(struct vf610_adc *info)
235 struct vf610_adc_feature *adc_feature = &info->adc_feature;
237 /* set default Configuration for ADC controller */
238 adc_feature->clk_sel = VF610_ADCIOC_BUSCLK_SET;
239 adc_feature->vol_ref = VF610_ADCIOC_VR_VREF_SET;
241 adc_feature->calibration = true;
242 adc_feature->ovwren = true;
244 adc_feature->res_mode = 12;
245 adc_feature->sample_rate = 1;
247 adc_feature->conv_mode = VF610_ADC_CONV_LOW_POWER;
249 vf610_adc_calculate_rates(info);
252 static void vf610_adc_cfg_post_set(struct vf610_adc *info)
254 struct vf610_adc_feature *adc_feature = &info->adc_feature;
258 switch (adc_feature->clk_sel) {
259 case VF610_ADCIOC_ALTCLK_SET:
260 cfg_data |= VF610_ADC_ALTCLK_SEL;
262 case VF610_ADCIOC_ADACK_SET:
263 cfg_data |= VF610_ADC_ADACK_SEL;
269 /* low power set for calibration */
270 cfg_data |= VF610_ADC_ADLPC_EN;
272 /* enable high speed for calibration */
273 cfg_data |= VF610_ADC_ADHSC_EN;
275 /* voltage reference */
276 switch (adc_feature->vol_ref) {
277 case VF610_ADCIOC_VR_VREF_SET:
279 case VF610_ADCIOC_VR_VALT_SET:
280 cfg_data |= VF610_ADC_REFSEL_VALT;
282 case VF610_ADCIOC_VR_VBG_SET:
283 cfg_data |= VF610_ADC_REFSEL_VBG;
286 dev_err(info->dev, "error voltage reference\n");
289 /* data overwrite enable */
290 if (adc_feature->ovwren)
291 cfg_data |= VF610_ADC_OVWREN;
293 writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
294 writel(gc_data, info->regs + VF610_REG_ADC_GC);
297 static void vf610_adc_calibration(struct vf610_adc *info)
301 if (!info->adc_feature.calibration)
304 /* enable calibration interrupt */
305 hc_cfg = VF610_ADC_AIEN | VF610_ADC_CONV_DISABLE;
306 writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
308 adc_gc = readl(info->regs + VF610_REG_ADC_GC);
309 writel(adc_gc | VF610_ADC_CAL, info->regs + VF610_REG_ADC_GC);
311 if (!wait_for_completion_timeout(&info->completion, VF610_ADC_TIMEOUT))
312 dev_err(info->dev, "Timeout for adc calibration\n");
314 adc_gc = readl(info->regs + VF610_REG_ADC_GS);
315 if (adc_gc & VF610_ADC_CALF)
316 dev_err(info->dev, "ADC calibration failed\n");
318 info->adc_feature.calibration = false;
321 static void vf610_adc_cfg_set(struct vf610_adc *info)
323 struct vf610_adc_feature *adc_feature = &(info->adc_feature);
326 cfg_data = readl(info->regs + VF610_REG_ADC_CFG);
328 cfg_data &= ~VF610_ADC_ADLPC_EN;
329 if (adc_feature->conv_mode == VF610_ADC_CONV_LOW_POWER)
330 cfg_data |= VF610_ADC_ADLPC_EN;
332 cfg_data &= ~VF610_ADC_ADHSC_EN;
333 if (adc_feature->conv_mode == VF610_ADC_CONV_HIGH_SPEED)
334 cfg_data |= VF610_ADC_ADHSC_EN;
336 writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
339 static void vf610_adc_sample_set(struct vf610_adc *info)
341 struct vf610_adc_feature *adc_feature = &(info->adc_feature);
342 int cfg_data, gc_data;
344 cfg_data = readl(info->regs + VF610_REG_ADC_CFG);
345 gc_data = readl(info->regs + VF610_REG_ADC_GC);
347 /* resolution mode */
348 cfg_data &= ~VF610_ADC_MODE_MASK;
349 switch (adc_feature->res_mode) {
351 cfg_data |= VF610_ADC_MODE_BIT8;
354 cfg_data |= VF610_ADC_MODE_BIT10;
357 cfg_data |= VF610_ADC_MODE_BIT12;
360 dev_err(info->dev, "error resolution mode\n");
364 /* clock select and clock divider */
365 cfg_data &= ~(VF610_ADC_CLK_MASK | VF610_ADC_ADCCLK_MASK);
366 switch (adc_feature->clk_div) {
370 cfg_data |= VF610_ADC_CLK_DIV2;
373 cfg_data |= VF610_ADC_CLK_DIV4;
376 cfg_data |= VF610_ADC_CLK_DIV8;
379 switch (adc_feature->clk_sel) {
380 case VF610_ADCIOC_BUSCLK_SET:
381 cfg_data |= VF610_ADC_BUSCLK2_SEL | VF610_ADC_CLK_DIV8;
384 dev_err(info->dev, "error clk divider\n");
391 * Set ADLSMP and ADSTS based on the Long Sample Time Adder value
394 switch (adc_feature->lst_adder_index) {
395 case VF610_ADCK_CYCLES_3:
397 case VF610_ADCK_CYCLES_5:
398 cfg_data |= VF610_ADC_ADSTS_SHORT;
400 case VF610_ADCK_CYCLES_7:
401 cfg_data |= VF610_ADC_ADSTS_NORMAL;
403 case VF610_ADCK_CYCLES_9:
404 cfg_data |= VF610_ADC_ADSTS_LONG;
406 case VF610_ADCK_CYCLES_13:
407 cfg_data |= VF610_ADC_ADLSMP_LONG;
409 case VF610_ADCK_CYCLES_17:
410 cfg_data |= VF610_ADC_ADLSMP_LONG;
411 cfg_data |= VF610_ADC_ADSTS_SHORT;
413 case VF610_ADCK_CYCLES_21:
414 cfg_data |= VF610_ADC_ADLSMP_LONG;
415 cfg_data |= VF610_ADC_ADSTS_NORMAL;
417 case VF610_ADCK_CYCLES_25:
418 cfg_data |= VF610_ADC_ADLSMP_LONG;
419 cfg_data |= VF610_ADC_ADSTS_NORMAL;
422 dev_err(info->dev, "error in sample time select\n");
425 /* update hardware average selection */
426 cfg_data &= ~VF610_ADC_AVGS_MASK;
427 gc_data &= ~VF610_ADC_AVGEN;
428 switch (adc_feature->sample_rate) {
429 case VF610_ADC_SAMPLE_1:
431 case VF610_ADC_SAMPLE_4:
432 gc_data |= VF610_ADC_AVGEN;
434 case VF610_ADC_SAMPLE_8:
435 gc_data |= VF610_ADC_AVGEN;
436 cfg_data |= VF610_ADC_AVGS_8;
438 case VF610_ADC_SAMPLE_16:
439 gc_data |= VF610_ADC_AVGEN;
440 cfg_data |= VF610_ADC_AVGS_16;
442 case VF610_ADC_SAMPLE_32:
443 gc_data |= VF610_ADC_AVGEN;
444 cfg_data |= VF610_ADC_AVGS_32;
448 "error hardware sample average select\n");
451 writel(cfg_data, info->regs + VF610_REG_ADC_CFG);
452 writel(gc_data, info->regs + VF610_REG_ADC_GC);
455 static void vf610_adc_hw_init(struct vf610_adc *info)
457 /* CFG: Feature set */
458 vf610_adc_cfg_post_set(info);
459 vf610_adc_sample_set(info);
461 /* adc calibration */
462 vf610_adc_calibration(info);
464 /* CFG: power and speed set */
465 vf610_adc_cfg_set(info);
468 static int vf610_set_conversion_mode(struct iio_dev *indio_dev,
469 const struct iio_chan_spec *chan,
472 struct vf610_adc *info = iio_priv(indio_dev);
474 mutex_lock(&info->lock);
475 info->adc_feature.conv_mode = mode;
476 vf610_adc_calculate_rates(info);
477 vf610_adc_hw_init(info);
478 mutex_unlock(&info->lock);
483 static int vf610_get_conversion_mode(struct iio_dev *indio_dev,
484 const struct iio_chan_spec *chan)
486 struct vf610_adc *info = iio_priv(indio_dev);
488 return info->adc_feature.conv_mode;
491 static const char * const vf610_conv_modes[] = { "normal", "high-speed",
494 static const struct iio_enum vf610_conversion_mode = {
495 .items = vf610_conv_modes,
496 .num_items = ARRAY_SIZE(vf610_conv_modes),
497 .get = vf610_get_conversion_mode,
498 .set = vf610_set_conversion_mode,
501 static const struct iio_chan_spec_ext_info vf610_ext_info[] = {
502 IIO_ENUM("conversion_mode", IIO_SHARED_BY_DIR, &vf610_conversion_mode),
506 #define VF610_ADC_CHAN(_idx, _chan_type) { \
507 .type = (_chan_type), \
510 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
511 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
512 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
513 .ext_info = vf610_ext_info, \
514 .scan_index = (_idx), \
522 #define VF610_ADC_TEMPERATURE_CHAN(_idx, _chan_type) { \
523 .type = (_chan_type), \
525 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
526 .scan_index = (_idx), \
534 static const struct iio_chan_spec vf610_adc_iio_channels[] = {
535 VF610_ADC_CHAN(0, IIO_VOLTAGE),
536 VF610_ADC_CHAN(1, IIO_VOLTAGE),
537 VF610_ADC_CHAN(2, IIO_VOLTAGE),
538 VF610_ADC_CHAN(3, IIO_VOLTAGE),
539 VF610_ADC_CHAN(4, IIO_VOLTAGE),
540 VF610_ADC_CHAN(5, IIO_VOLTAGE),
541 VF610_ADC_CHAN(6, IIO_VOLTAGE),
542 VF610_ADC_CHAN(7, IIO_VOLTAGE),
543 VF610_ADC_CHAN(8, IIO_VOLTAGE),
544 VF610_ADC_CHAN(9, IIO_VOLTAGE),
545 VF610_ADC_CHAN(10, IIO_VOLTAGE),
546 VF610_ADC_CHAN(11, IIO_VOLTAGE),
547 VF610_ADC_CHAN(12, IIO_VOLTAGE),
548 VF610_ADC_CHAN(13, IIO_VOLTAGE),
549 VF610_ADC_CHAN(14, IIO_VOLTAGE),
550 VF610_ADC_CHAN(15, IIO_VOLTAGE),
551 VF610_ADC_TEMPERATURE_CHAN(26, IIO_TEMP),
552 IIO_CHAN_SOFT_TIMESTAMP(32),
556 static int vf610_adc_read_data(struct vf610_adc *info)
560 result = readl(info->regs + VF610_REG_ADC_R0);
562 switch (info->adc_feature.res_mode) {
579 static irqreturn_t vf610_adc_isr(int irq, void *dev_id)
581 struct iio_dev *indio_dev = dev_id;
582 struct vf610_adc *info = iio_priv(indio_dev);
585 coco = readl(info->regs + VF610_REG_ADC_HS);
586 if (coco & VF610_ADC_HS_COCO0) {
587 info->value = vf610_adc_read_data(info);
588 if (iio_buffer_enabled(indio_dev)) {
589 info->scan.chan = info->value;
590 iio_push_to_buffers_with_timestamp(indio_dev,
592 iio_get_time_ns(indio_dev));
593 iio_trigger_notify_done(indio_dev->trig);
595 complete(&info->completion);
601 static ssize_t vf610_show_samp_freq_avail(struct device *dev,
602 struct device_attribute *attr, char *buf)
604 struct vf610_adc *info = iio_priv(dev_to_iio_dev(dev));
608 for (i = 0; i < ARRAY_SIZE(info->sample_freq_avail); i++)
609 len += scnprintf(buf + len, PAGE_SIZE - len,
610 "%u ", info->sample_freq_avail[i]);
612 /* replace trailing space by newline */
618 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(vf610_show_samp_freq_avail);
620 static struct attribute *vf610_attributes[] = {
621 &iio_dev_attr_sampling_frequency_available.dev_attr.attr,
625 static const struct attribute_group vf610_attribute_group = {
626 .attrs = vf610_attributes,
629 static int vf610_read_sample(struct iio_dev *indio_dev,
630 struct iio_chan_spec const *chan, int *val)
632 struct vf610_adc *info = iio_priv(indio_dev);
636 ret = iio_device_claim_direct_mode(indio_dev);
640 mutex_lock(&info->lock);
641 reinit_completion(&info->completion);
642 hc_cfg = VF610_ADC_ADCHC(chan->channel);
643 hc_cfg |= VF610_ADC_AIEN;
644 writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
645 ret = wait_for_completion_interruptible_timeout(&info->completion,
655 switch (chan->type) {
661 * Calculate in degree Celsius times 1000
662 * Using the typical sensor slope of 1.84 mV/°C
663 * and VREFH_ADC at 3.3V, V at 25°C of 699 mV
665 *val = 25000 - ((int)info->value - VF610_VTEMP25_3V3) *
666 1000000 / VF610_TEMP_SLOPE_COEFF;
675 mutex_unlock(&info->lock);
676 iio_device_release_direct_mode(indio_dev);
681 static int vf610_read_raw(struct iio_dev *indio_dev,
682 struct iio_chan_spec const *chan,
687 struct vf610_adc *info = iio_priv(indio_dev);
691 case IIO_CHAN_INFO_RAW:
692 case IIO_CHAN_INFO_PROCESSED:
693 ret = vf610_read_sample(indio_dev, chan, val);
699 case IIO_CHAN_INFO_SCALE:
700 *val = info->vref_uv / 1000;
701 *val2 = info->adc_feature.res_mode;
702 return IIO_VAL_FRACTIONAL_LOG2;
704 case IIO_CHAN_INFO_SAMP_FREQ:
705 *val = info->sample_freq_avail[info->adc_feature.sample_rate];
716 static int vf610_write_raw(struct iio_dev *indio_dev,
717 struct iio_chan_spec const *chan,
722 struct vf610_adc *info = iio_priv(indio_dev);
726 case IIO_CHAN_INFO_SAMP_FREQ:
728 i < ARRAY_SIZE(info->sample_freq_avail);
730 if (val == info->sample_freq_avail[i]) {
731 info->adc_feature.sample_rate = i;
732 vf610_adc_sample_set(info);
744 static int vf610_adc_buffer_postenable(struct iio_dev *indio_dev)
746 struct vf610_adc *info = iio_priv(indio_dev);
747 unsigned int channel;
750 val = readl(info->regs + VF610_REG_ADC_GC);
751 val |= VF610_ADC_ADCON;
752 writel(val, info->regs + VF610_REG_ADC_GC);
754 channel = find_first_bit(indio_dev->active_scan_mask,
755 indio_dev->masklength);
757 val = VF610_ADC_ADCHC(channel);
758 val |= VF610_ADC_AIEN;
760 writel(val, info->regs + VF610_REG_ADC_HC0);
765 static int vf610_adc_buffer_predisable(struct iio_dev *indio_dev)
767 struct vf610_adc *info = iio_priv(indio_dev);
768 unsigned int hc_cfg = 0;
771 val = readl(info->regs + VF610_REG_ADC_GC);
772 val &= ~VF610_ADC_ADCON;
773 writel(val, info->regs + VF610_REG_ADC_GC);
775 hc_cfg |= VF610_ADC_CONV_DISABLE;
776 hc_cfg &= ~VF610_ADC_AIEN;
778 writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
783 static const struct iio_buffer_setup_ops iio_triggered_buffer_setup_ops = {
784 .postenable = &vf610_adc_buffer_postenable,
785 .predisable = &vf610_adc_buffer_predisable,
786 .validate_scan_mask = &iio_validate_scan_mask_onehot,
789 static int vf610_adc_reg_access(struct iio_dev *indio_dev,
790 unsigned reg, unsigned writeval,
793 struct vf610_adc *info = iio_priv(indio_dev);
795 if ((readval == NULL) ||
796 ((reg % 4) || (reg > VF610_REG_ADC_PCTL)))
799 *readval = readl(info->regs + reg);
804 static const struct iio_info vf610_adc_iio_info = {
805 .read_raw = &vf610_read_raw,
806 .write_raw = &vf610_write_raw,
807 .debugfs_reg_access = &vf610_adc_reg_access,
808 .attrs = &vf610_attribute_group,
811 static const struct of_device_id vf610_adc_match[] = {
812 { .compatible = "fsl,vf610-adc", },
815 MODULE_DEVICE_TABLE(of, vf610_adc_match);
817 static int vf610_adc_probe(struct platform_device *pdev)
819 struct device *dev = &pdev->dev;
820 struct vf610_adc *info;
821 struct iio_dev *indio_dev;
825 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(struct vf610_adc));
827 dev_err(&pdev->dev, "Failed allocating iio device\n");
831 info = iio_priv(indio_dev);
832 info->dev = &pdev->dev;
834 info->regs = devm_platform_ioremap_resource(pdev, 0);
835 if (IS_ERR(info->regs))
836 return PTR_ERR(info->regs);
838 irq = platform_get_irq(pdev, 0);
842 ret = devm_request_irq(info->dev, irq,
844 dev_name(&pdev->dev), indio_dev);
846 dev_err(&pdev->dev, "failed requesting irq, irq = %d\n", irq);
850 info->clk = devm_clk_get(&pdev->dev, "adc");
851 if (IS_ERR(info->clk)) {
852 dev_err(&pdev->dev, "failed getting clock, err = %ld\n",
854 return PTR_ERR(info->clk);
857 info->vref = devm_regulator_get(&pdev->dev, "vref");
858 if (IS_ERR(info->vref))
859 return PTR_ERR(info->vref);
861 ret = regulator_enable(info->vref);
865 info->vref_uv = regulator_get_voltage(info->vref);
867 device_property_read_u32_array(dev, "fsl,adck-max-frequency", info->max_adck_rate, 3);
869 info->adc_feature.default_sample_time = DEFAULT_SAMPLE_TIME;
870 device_property_read_u32(dev, "min-sample-time", &info->adc_feature.default_sample_time);
872 platform_set_drvdata(pdev, indio_dev);
874 init_completion(&info->completion);
876 indio_dev->name = dev_name(&pdev->dev);
877 indio_dev->info = &vf610_adc_iio_info;
878 indio_dev->modes = INDIO_DIRECT_MODE;
879 indio_dev->channels = vf610_adc_iio_channels;
880 indio_dev->num_channels = ARRAY_SIZE(vf610_adc_iio_channels);
882 ret = clk_prepare_enable(info->clk);
885 "Could not prepare or enable the clock.\n");
886 goto error_adc_clk_enable;
889 vf610_adc_cfg_init(info);
890 vf610_adc_hw_init(info);
892 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
893 NULL, &iio_triggered_buffer_setup_ops);
895 dev_err(&pdev->dev, "Couldn't initialise the buffer\n");
896 goto error_iio_device_register;
899 mutex_init(&info->lock);
901 ret = iio_device_register(indio_dev);
903 dev_err(&pdev->dev, "Couldn't register the device.\n");
904 goto error_adc_buffer_init;
909 error_adc_buffer_init:
910 iio_triggered_buffer_cleanup(indio_dev);
911 error_iio_device_register:
912 clk_disable_unprepare(info->clk);
913 error_adc_clk_enable:
914 regulator_disable(info->vref);
919 static int vf610_adc_remove(struct platform_device *pdev)
921 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
922 struct vf610_adc *info = iio_priv(indio_dev);
924 iio_device_unregister(indio_dev);
925 iio_triggered_buffer_cleanup(indio_dev);
926 regulator_disable(info->vref);
927 clk_disable_unprepare(info->clk);
932 static int vf610_adc_suspend(struct device *dev)
934 struct iio_dev *indio_dev = dev_get_drvdata(dev);
935 struct vf610_adc *info = iio_priv(indio_dev);
938 /* ADC controller enters to stop mode */
939 hc_cfg = readl(info->regs + VF610_REG_ADC_HC0);
940 hc_cfg |= VF610_ADC_CONV_DISABLE;
941 writel(hc_cfg, info->regs + VF610_REG_ADC_HC0);
943 clk_disable_unprepare(info->clk);
944 regulator_disable(info->vref);
949 static int vf610_adc_resume(struct device *dev)
951 struct iio_dev *indio_dev = dev_get_drvdata(dev);
952 struct vf610_adc *info = iio_priv(indio_dev);
955 ret = regulator_enable(info->vref);
959 ret = clk_prepare_enable(info->clk);
963 vf610_adc_hw_init(info);
968 regulator_disable(info->vref);
972 static DEFINE_SIMPLE_DEV_PM_OPS(vf610_adc_pm_ops, vf610_adc_suspend,
975 static struct platform_driver vf610_adc_driver = {
976 .probe = vf610_adc_probe,
977 .remove = vf610_adc_remove,
980 .of_match_table = vf610_adc_match,
981 .pm = pm_sleep_ptr(&vf610_adc_pm_ops),
985 module_platform_driver(vf610_adc_driver);
988 MODULE_DESCRIPTION("Freescale VF610 ADC driver");
989 MODULE_LICENSE("GPL v2");