2 * Freescale Vybrid vf610 ADC driver
4 * Copyright 2013 Freescale Semiconductor, Inc.
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.
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.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <linux/interrupt.h>
24 #include <linux/delay.h>
25 #include <linux/kernel.h>
26 #include <linux/slab.h>
28 #include <linux/clk.h>
29 #include <linux/completion.h>
31 #include <linux/of_irq.h>
32 #include <linux/regulator/consumer.h>
33 #include <linux/of_platform.h>
34 #include <linux/err.h>
36 #include <linux/iio/iio.h>
37 #include <linux/iio/sysfs.h>
38 #include <linux/iio/driver.h>
40 /* This will be the driver name the kernel reports */
41 #define DRIVER_NAME "vf610-adc"
43 /* Vybrid/IMX ADC registers */
44 #define VF610_REG_ADC_HC0 0x00
45 #define VF610_REG_ADC_HC1 0x04
46 #define VF610_REG_ADC_HS 0x08
47 #define VF610_REG_ADC_R0 0x0c
48 #define VF610_REG_ADC_R1 0x10
49 #define VF610_REG_ADC_CFG 0x14
50 #define VF610_REG_ADC_GC 0x18
51 #define VF610_REG_ADC_GS 0x1c
52 #define VF610_REG_ADC_CV 0x20
53 #define VF610_REG_ADC_OFS 0x24
54 #define VF610_REG_ADC_CAL 0x28
55 #define VF610_REG_ADC_PCTL 0x30
57 /* Configuration register field define */
58 #define VF610_ADC_MODE_BIT8 0x00
59 #define VF610_ADC_MODE_BIT10 0x04
60 #define VF610_ADC_MODE_BIT12 0x08
61 #define VF610_ADC_MODE_MASK 0x0c
62 #define VF610_ADC_BUSCLK2_SEL 0x01
63 #define VF610_ADC_ALTCLK_SEL 0x02
64 #define VF610_ADC_ADACK_SEL 0x03
65 #define VF610_ADC_ADCCLK_MASK 0x03
66 #define VF610_ADC_CLK_DIV2 0x20
67 #define VF610_ADC_CLK_DIV4 0x40
68 #define VF610_ADC_CLK_DIV8 0x60
69 #define VF610_ADC_CLK_MASK 0x60
70 #define VF610_ADC_ADLSMP_LONG 0x10
71 #define VF610_ADC_ADSTS_MASK 0x300
72 #define VF610_ADC_ADLPC_EN 0x80
73 #define VF610_ADC_ADHSC_EN 0x400
74 #define VF610_ADC_REFSEL_VALT 0x100
75 #define VF610_ADC_REFSEL_VBG 0x1000
76 #define VF610_ADC_ADTRG_HARD 0x2000
77 #define VF610_ADC_AVGS_8 0x4000
78 #define VF610_ADC_AVGS_16 0x8000
79 #define VF610_ADC_AVGS_32 0xC000
80 #define VF610_ADC_AVGS_MASK 0xC000
81 #define VF610_ADC_OVWREN 0x10000
83 /* General control register field define */
84 #define VF610_ADC_ADACKEN 0x1
85 #define VF610_ADC_DMAEN 0x2
86 #define VF610_ADC_ACREN 0x4
87 #define VF610_ADC_ACFGT 0x8
88 #define VF610_ADC_ACFE 0x10
89 #define VF610_ADC_AVGEN 0x20
90 #define VF610_ADC_ADCON 0x40
91 #define VF610_ADC_CAL 0x80
93 /* Other field define */
94 #define VF610_ADC_ADCHC(x) ((x) & 0x1F)
95 #define VF610_ADC_AIEN (0x1 << 7)
96 #define VF610_ADC_CONV_DISABLE 0x1F
97 #define VF610_ADC_HS_COCO0 0x1
98 #define VF610_ADC_CALF 0x2
99 #define VF610_ADC_TIMEOUT msecs_to_jiffies(100)
102 VF610_ADCIOC_BUSCLK_SET
,
103 VF610_ADCIOC_ALTCLK_SET
,
104 VF610_ADCIOC_ADACK_SET
,
108 VF610_ADCIOC_VR_VREF_SET
,
109 VF610_ADCIOC_VR_VALT_SET
,
110 VF610_ADCIOC_VR_VBG_SET
,
121 struct vf610_adc_feature
{
122 enum clk_sel clk_sel
;
123 enum vol_ref vol_ref
;
141 struct regulator
*vref
;
142 struct vf610_adc_feature adc_feature
;
144 struct completion completion
;
147 #define VF610_ADC_CHAN(_idx, _chan_type) { \
148 .type = (_chan_type), \
151 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
152 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
153 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
156 #define VF610_ADC_TEMPERATURE_CHAN(_idx, _chan_type) { \
157 .type = (_chan_type), \
159 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
162 static const struct iio_chan_spec vf610_adc_iio_channels
[] = {
163 VF610_ADC_CHAN(0, IIO_VOLTAGE
),
164 VF610_ADC_CHAN(1, IIO_VOLTAGE
),
165 VF610_ADC_CHAN(2, IIO_VOLTAGE
),
166 VF610_ADC_CHAN(3, IIO_VOLTAGE
),
167 VF610_ADC_CHAN(4, IIO_VOLTAGE
),
168 VF610_ADC_CHAN(5, IIO_VOLTAGE
),
169 VF610_ADC_CHAN(6, IIO_VOLTAGE
),
170 VF610_ADC_CHAN(7, IIO_VOLTAGE
),
171 VF610_ADC_CHAN(8, IIO_VOLTAGE
),
172 VF610_ADC_CHAN(9, IIO_VOLTAGE
),
173 VF610_ADC_CHAN(10, IIO_VOLTAGE
),
174 VF610_ADC_CHAN(11, IIO_VOLTAGE
),
175 VF610_ADC_CHAN(12, IIO_VOLTAGE
),
176 VF610_ADC_CHAN(13, IIO_VOLTAGE
),
177 VF610_ADC_CHAN(14, IIO_VOLTAGE
),
178 VF610_ADC_CHAN(15, IIO_VOLTAGE
),
179 VF610_ADC_TEMPERATURE_CHAN(26, IIO_TEMP
),
184 * ADC sample frequency, unit is ADCK cycles.
185 * ADC clk source is ipg clock, which is the same as bus clock.
187 * ADC conversion time = SFCAdder + AverageNum x (BCT + LSTAdder)
188 * SFCAdder: fixed to 6 ADCK cycles
189 * AverageNum: 1, 4, 8, 16, 32 samples for hardware average.
190 * BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode
191 * LSTAdder(Long Sample Time): fixed to 3 ADCK cycles
193 * By default, enable 12 bit resolution mode, clock source
194 * set to ipg clock, So get below frequency group:
196 static const u32 vf610_sample_freq_avail
[5] =
197 {1941176, 559332, 286957, 145374, 73171};
199 static inline void vf610_adc_cfg_init(struct vf610_adc
*info
)
201 /* set default Configuration for ADC controller */
202 info
->adc_feature
.clk_sel
= VF610_ADCIOC_BUSCLK_SET
;
203 info
->adc_feature
.vol_ref
= VF610_ADCIOC_VR_VREF_SET
;
205 info
->adc_feature
.calibration
= true;
206 info
->adc_feature
.ovwren
= true;
208 info
->adc_feature
.clk_div
= 1;
209 info
->adc_feature
.res_mode
= 12;
210 info
->adc_feature
.sample_rate
= 1;
211 info
->adc_feature
.lpm
= true;
214 static void vf610_adc_cfg_post_set(struct vf610_adc
*info
)
216 struct vf610_adc_feature
*adc_feature
= &info
->adc_feature
;
220 switch (adc_feature
->clk_sel
) {
221 case VF610_ADCIOC_ALTCLK_SET
:
222 cfg_data
|= VF610_ADC_ALTCLK_SEL
;
224 case VF610_ADCIOC_ADACK_SET
:
225 cfg_data
|= VF610_ADC_ADACK_SEL
;
231 /* low power set for calibration */
232 cfg_data
|= VF610_ADC_ADLPC_EN
;
234 /* enable high speed for calibration */
235 cfg_data
|= VF610_ADC_ADHSC_EN
;
237 /* voltage reference */
238 switch (adc_feature
->vol_ref
) {
239 case VF610_ADCIOC_VR_VREF_SET
:
241 case VF610_ADCIOC_VR_VALT_SET
:
242 cfg_data
|= VF610_ADC_REFSEL_VALT
;
244 case VF610_ADCIOC_VR_VBG_SET
:
245 cfg_data
|= VF610_ADC_REFSEL_VBG
;
248 dev_err(info
->dev
, "error voltage reference\n");
251 /* data overwrite enable */
252 if (adc_feature
->ovwren
)
253 cfg_data
|= VF610_ADC_OVWREN
;
255 writel(cfg_data
, info
->regs
+ VF610_REG_ADC_CFG
);
256 writel(gc_data
, info
->regs
+ VF610_REG_ADC_GC
);
259 static void vf610_adc_calibration(struct vf610_adc
*info
)
264 if (!info
->adc_feature
.calibration
)
267 /* enable calibration interrupt */
268 hc_cfg
= VF610_ADC_AIEN
| VF610_ADC_CONV_DISABLE
;
269 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
271 adc_gc
= readl(info
->regs
+ VF610_REG_ADC_GC
);
272 writel(adc_gc
| VF610_ADC_CAL
, info
->regs
+ VF610_REG_ADC_GC
);
274 timeout
= wait_for_completion_timeout
275 (&info
->completion
, VF610_ADC_TIMEOUT
);
277 dev_err(info
->dev
, "Timeout for adc calibration\n");
279 adc_gc
= readl(info
->regs
+ VF610_REG_ADC_GS
);
280 if (adc_gc
& VF610_ADC_CALF
)
281 dev_err(info
->dev
, "ADC calibration failed\n");
283 info
->adc_feature
.calibration
= false;
286 static void vf610_adc_cfg_set(struct vf610_adc
*info
)
288 struct vf610_adc_feature
*adc_feature
= &(info
->adc_feature
);
291 cfg_data
= readl(info
->regs
+ VF610_REG_ADC_CFG
);
293 /* low power configuration */
294 cfg_data
&= ~VF610_ADC_ADLPC_EN
;
295 if (adc_feature
->lpm
)
296 cfg_data
|= VF610_ADC_ADLPC_EN
;
298 /* disable high speed */
299 cfg_data
&= ~VF610_ADC_ADHSC_EN
;
301 writel(cfg_data
, info
->regs
+ VF610_REG_ADC_CFG
);
304 static void vf610_adc_sample_set(struct vf610_adc
*info
)
306 struct vf610_adc_feature
*adc_feature
= &(info
->adc_feature
);
307 int cfg_data
, gc_data
;
309 cfg_data
= readl(info
->regs
+ VF610_REG_ADC_CFG
);
310 gc_data
= readl(info
->regs
+ VF610_REG_ADC_GC
);
312 /* resolution mode */
313 cfg_data
&= ~VF610_ADC_MODE_MASK
;
314 switch (adc_feature
->res_mode
) {
316 cfg_data
|= VF610_ADC_MODE_BIT8
;
319 cfg_data
|= VF610_ADC_MODE_BIT10
;
322 cfg_data
|= VF610_ADC_MODE_BIT12
;
325 dev_err(info
->dev
, "error resolution mode\n");
329 /* clock select and clock divider */
330 cfg_data
&= ~(VF610_ADC_CLK_MASK
| VF610_ADC_ADCCLK_MASK
);
331 switch (adc_feature
->clk_div
) {
335 cfg_data
|= VF610_ADC_CLK_DIV2
;
338 cfg_data
|= VF610_ADC_CLK_DIV4
;
341 cfg_data
|= VF610_ADC_CLK_DIV8
;
344 switch (adc_feature
->clk_sel
) {
345 case VF610_ADCIOC_BUSCLK_SET
:
346 cfg_data
|= VF610_ADC_BUSCLK2_SEL
| VF610_ADC_CLK_DIV8
;
349 dev_err(info
->dev
, "error clk divider\n");
355 /* Use the short sample mode */
356 cfg_data
&= ~(VF610_ADC_ADLSMP_LONG
| VF610_ADC_ADSTS_MASK
);
358 /* update hardware average selection */
359 cfg_data
&= ~VF610_ADC_AVGS_MASK
;
360 gc_data
&= ~VF610_ADC_AVGEN
;
361 switch (adc_feature
->sample_rate
) {
362 case VF610_ADC_SAMPLE_1
:
364 case VF610_ADC_SAMPLE_4
:
365 gc_data
|= VF610_ADC_AVGEN
;
367 case VF610_ADC_SAMPLE_8
:
368 gc_data
|= VF610_ADC_AVGEN
;
369 cfg_data
|= VF610_ADC_AVGS_8
;
371 case VF610_ADC_SAMPLE_16
:
372 gc_data
|= VF610_ADC_AVGEN
;
373 cfg_data
|= VF610_ADC_AVGS_16
;
375 case VF610_ADC_SAMPLE_32
:
376 gc_data
|= VF610_ADC_AVGEN
;
377 cfg_data
|= VF610_ADC_AVGS_32
;
381 "error hardware sample average select\n");
384 writel(cfg_data
, info
->regs
+ VF610_REG_ADC_CFG
);
385 writel(gc_data
, info
->regs
+ VF610_REG_ADC_GC
);
388 static void vf610_adc_hw_init(struct vf610_adc
*info
)
390 /* CFG: Feature set */
391 vf610_adc_cfg_post_set(info
);
392 vf610_adc_sample_set(info
);
394 /* adc calibration */
395 vf610_adc_calibration(info
);
397 /* CFG: power and speed set */
398 vf610_adc_cfg_set(info
);
401 static int vf610_adc_read_data(struct vf610_adc
*info
)
405 result
= readl(info
->regs
+ VF610_REG_ADC_R0
);
407 switch (info
->adc_feature
.res_mode
) {
424 static irqreturn_t
vf610_adc_isr(int irq
, void *dev_id
)
426 struct vf610_adc
*info
= (struct vf610_adc
*)dev_id
;
429 coco
= readl(info
->regs
+ VF610_REG_ADC_HS
);
430 if (coco
& VF610_ADC_HS_COCO0
) {
431 info
->value
= vf610_adc_read_data(info
);
432 complete(&info
->completion
);
438 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1941176, 559332, 286957, 145374, 73171");
440 static struct attribute
*vf610_attributes
[] = {
441 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
445 static const struct attribute_group vf610_attribute_group
= {
446 .attrs
= vf610_attributes
,
449 static int vf610_read_raw(struct iio_dev
*indio_dev
,
450 struct iio_chan_spec
const *chan
,
455 struct vf610_adc
*info
= iio_priv(indio_dev
);
460 case IIO_CHAN_INFO_RAW
:
461 case IIO_CHAN_INFO_PROCESSED
:
462 mutex_lock(&indio_dev
->mlock
);
463 reinit_completion(&info
->completion
);
465 hc_cfg
= VF610_ADC_ADCHC(chan
->channel
);
466 hc_cfg
|= VF610_ADC_AIEN
;
467 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
468 ret
= wait_for_completion_interruptible_timeout
469 (&info
->completion
, VF610_ADC_TIMEOUT
);
471 mutex_unlock(&indio_dev
->mlock
);
475 mutex_unlock(&indio_dev
->mlock
);
479 switch (chan
->type
) {
485 * Calculate in degree Celsius times 1000
486 * Using sensor slope of 1.84 mV/°C and
487 * V at 25°C of 696 mV
489 *val
= 25000 - ((int)info
->value
- 864) * 1000000 / 1840;
492 mutex_unlock(&indio_dev
->mlock
);
496 mutex_unlock(&indio_dev
->mlock
);
499 case IIO_CHAN_INFO_SCALE
:
500 *val
= info
->vref_uv
/ 1000;
501 *val2
= info
->adc_feature
.res_mode
;
502 return IIO_VAL_FRACTIONAL_LOG2
;
504 case IIO_CHAN_INFO_SAMP_FREQ
:
505 *val
= vf610_sample_freq_avail
[info
->adc_feature
.sample_rate
];
516 static int vf610_write_raw(struct iio_dev
*indio_dev
,
517 struct iio_chan_spec
const *chan
,
522 struct vf610_adc
*info
= iio_priv(indio_dev
);
526 case IIO_CHAN_INFO_SAMP_FREQ
:
528 i
< ARRAY_SIZE(vf610_sample_freq_avail
);
530 if (val
== vf610_sample_freq_avail
[i
]) {
531 info
->adc_feature
.sample_rate
= i
;
532 vf610_adc_sample_set(info
);
544 static int vf610_adc_reg_access(struct iio_dev
*indio_dev
,
545 unsigned reg
, unsigned writeval
,
548 struct vf610_adc
*info
= iio_priv(indio_dev
);
550 if ((readval
== NULL
) ||
551 (!(reg
% 4) || (reg
> VF610_REG_ADC_PCTL
)))
554 *readval
= readl(info
->regs
+ reg
);
559 static const struct iio_info vf610_adc_iio_info
= {
560 .driver_module
= THIS_MODULE
,
561 .read_raw
= &vf610_read_raw
,
562 .write_raw
= &vf610_write_raw
,
563 .debugfs_reg_access
= &vf610_adc_reg_access
,
564 .attrs
= &vf610_attribute_group
,
567 static const struct of_device_id vf610_adc_match
[] = {
568 { .compatible
= "fsl,vf610-adc", },
571 MODULE_DEVICE_TABLE(of
, vf610_adc_match
);
573 static int vf610_adc_probe(struct platform_device
*pdev
)
575 struct vf610_adc
*info
;
576 struct iio_dev
*indio_dev
;
577 struct resource
*mem
;
581 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(struct vf610_adc
));
583 dev_err(&pdev
->dev
, "Failed allocating iio device\n");
587 info
= iio_priv(indio_dev
);
588 info
->dev
= &pdev
->dev
;
590 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
591 info
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
592 if (IS_ERR(info
->regs
))
593 return PTR_ERR(info
->regs
);
595 irq
= platform_get_irq(pdev
, 0);
597 dev_err(&pdev
->dev
, "no irq resource?\n");
601 ret
= devm_request_irq(info
->dev
, irq
,
603 dev_name(&pdev
->dev
), info
);
605 dev_err(&pdev
->dev
, "failed requesting irq, irq = %d\n", irq
);
609 info
->clk
= devm_clk_get(&pdev
->dev
, "adc");
610 if (IS_ERR(info
->clk
)) {
611 dev_err(&pdev
->dev
, "failed getting clock, err = %ld\n",
613 return PTR_ERR(info
->clk
);
616 info
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
617 if (IS_ERR(info
->vref
))
618 return PTR_ERR(info
->vref
);
620 ret
= regulator_enable(info
->vref
);
624 info
->vref_uv
= regulator_get_voltage(info
->vref
);
626 platform_set_drvdata(pdev
, indio_dev
);
628 init_completion(&info
->completion
);
630 indio_dev
->name
= dev_name(&pdev
->dev
);
631 indio_dev
->dev
.parent
= &pdev
->dev
;
632 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
633 indio_dev
->info
= &vf610_adc_iio_info
;
634 indio_dev
->modes
= INDIO_DIRECT_MODE
;
635 indio_dev
->channels
= vf610_adc_iio_channels
;
636 indio_dev
->num_channels
= ARRAY_SIZE(vf610_adc_iio_channels
);
638 ret
= clk_prepare_enable(info
->clk
);
641 "Could not prepare or enable the clock.\n");
642 goto error_adc_clk_enable
;
645 vf610_adc_cfg_init(info
);
646 vf610_adc_hw_init(info
);
648 ret
= iio_device_register(indio_dev
);
650 dev_err(&pdev
->dev
, "Couldn't register the device.\n");
651 goto error_iio_device_register
;
657 error_iio_device_register
:
658 clk_disable_unprepare(info
->clk
);
659 error_adc_clk_enable
:
660 regulator_disable(info
->vref
);
665 static int vf610_adc_remove(struct platform_device
*pdev
)
667 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
668 struct vf610_adc
*info
= iio_priv(indio_dev
);
670 iio_device_unregister(indio_dev
);
671 regulator_disable(info
->vref
);
672 clk_disable_unprepare(info
->clk
);
677 #ifdef CONFIG_PM_SLEEP
678 static int vf610_adc_suspend(struct device
*dev
)
680 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
681 struct vf610_adc
*info
= iio_priv(indio_dev
);
684 /* ADC controller enters to stop mode */
685 hc_cfg
= readl(info
->regs
+ VF610_REG_ADC_HC0
);
686 hc_cfg
|= VF610_ADC_CONV_DISABLE
;
687 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
689 clk_disable_unprepare(info
->clk
);
690 regulator_disable(info
->vref
);
695 static int vf610_adc_resume(struct device
*dev
)
697 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
698 struct vf610_adc
*info
= iio_priv(indio_dev
);
701 ret
= regulator_enable(info
->vref
);
705 ret
= clk_prepare_enable(info
->clk
);
709 vf610_adc_hw_init(info
);
714 regulator_disable(info
->vref
);
719 static SIMPLE_DEV_PM_OPS(vf610_adc_pm_ops
, vf610_adc_suspend
, vf610_adc_resume
);
721 static struct platform_driver vf610_adc_driver
= {
722 .probe
= vf610_adc_probe
,
723 .remove
= vf610_adc_remove
,
726 .of_match_table
= vf610_adc_match
,
727 .pm
= &vf610_adc_pm_ops
,
731 module_platform_driver(vf610_adc_driver
);
733 MODULE_AUTHOR("Fugang Duan <B38611@freescale.com>");
734 MODULE_DESCRIPTION("Freescale VF610 ADC driver");
735 MODULE_LICENSE("GPL v2");