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 u32 sample_freq_avail
[5];
146 struct completion completion
;
149 static const u32 vf610_hw_avgs
[] = { 1, 4, 8, 16, 32 };
151 #define VF610_ADC_CHAN(_idx, _chan_type) { \
152 .type = (_chan_type), \
155 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
156 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE) | \
157 BIT(IIO_CHAN_INFO_SAMP_FREQ), \
160 #define VF610_ADC_TEMPERATURE_CHAN(_idx, _chan_type) { \
161 .type = (_chan_type), \
163 .info_mask_separate = BIT(IIO_CHAN_INFO_PROCESSED), \
166 static const struct iio_chan_spec vf610_adc_iio_channels
[] = {
167 VF610_ADC_CHAN(0, IIO_VOLTAGE
),
168 VF610_ADC_CHAN(1, IIO_VOLTAGE
),
169 VF610_ADC_CHAN(2, IIO_VOLTAGE
),
170 VF610_ADC_CHAN(3, IIO_VOLTAGE
),
171 VF610_ADC_CHAN(4, IIO_VOLTAGE
),
172 VF610_ADC_CHAN(5, IIO_VOLTAGE
),
173 VF610_ADC_CHAN(6, IIO_VOLTAGE
),
174 VF610_ADC_CHAN(7, IIO_VOLTAGE
),
175 VF610_ADC_CHAN(8, IIO_VOLTAGE
),
176 VF610_ADC_CHAN(9, IIO_VOLTAGE
),
177 VF610_ADC_CHAN(10, IIO_VOLTAGE
),
178 VF610_ADC_CHAN(11, IIO_VOLTAGE
),
179 VF610_ADC_CHAN(12, IIO_VOLTAGE
),
180 VF610_ADC_CHAN(13, IIO_VOLTAGE
),
181 VF610_ADC_CHAN(14, IIO_VOLTAGE
),
182 VF610_ADC_CHAN(15, IIO_VOLTAGE
),
183 VF610_ADC_TEMPERATURE_CHAN(26, IIO_TEMP
),
187 static inline void vf610_adc_calculate_rates(struct vf610_adc
*info
)
189 unsigned long adck_rate
, ipg_rate
= clk_get_rate(info
->clk
);
193 * Calculate ADC sample frequencies
194 * Sample time unit is ADCK cycles. ADCK clk source is ipg clock,
195 * which is the same as bus clock.
197 * ADC conversion time = SFCAdder + AverageNum x (BCT + LSTAdder)
198 * SFCAdder: fixed to 6 ADCK cycles
199 * AverageNum: 1, 4, 8, 16, 32 samples for hardware average.
200 * BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode
201 * LSTAdder(Long Sample Time): fixed to 3 ADCK cycles
203 adck_rate
= ipg_rate
/ info
->adc_feature
.clk_div
;
204 for (i
= 0; i
< ARRAY_SIZE(vf610_hw_avgs
); i
++)
205 info
->sample_freq_avail
[i
] =
206 adck_rate
/ (6 + vf610_hw_avgs
[i
] * (25 + 3));
209 static inline void vf610_adc_cfg_init(struct vf610_adc
*info
)
211 struct vf610_adc_feature
*adc_feature
= &info
->adc_feature
;
213 /* set default Configuration for ADC controller */
214 adc_feature
->clk_sel
= VF610_ADCIOC_BUSCLK_SET
;
215 adc_feature
->vol_ref
= VF610_ADCIOC_VR_VREF_SET
;
217 adc_feature
->calibration
= true;
218 adc_feature
->ovwren
= true;
220 adc_feature
->res_mode
= 12;
221 adc_feature
->sample_rate
= 1;
222 adc_feature
->lpm
= true;
224 /* Use a save ADCK which is below 20MHz on all devices */
225 adc_feature
->clk_div
= 8;
227 vf610_adc_calculate_rates(info
);
230 static void vf610_adc_cfg_post_set(struct vf610_adc
*info
)
232 struct vf610_adc_feature
*adc_feature
= &info
->adc_feature
;
236 switch (adc_feature
->clk_sel
) {
237 case VF610_ADCIOC_ALTCLK_SET
:
238 cfg_data
|= VF610_ADC_ALTCLK_SEL
;
240 case VF610_ADCIOC_ADACK_SET
:
241 cfg_data
|= VF610_ADC_ADACK_SEL
;
247 /* low power set for calibration */
248 cfg_data
|= VF610_ADC_ADLPC_EN
;
250 /* enable high speed for calibration */
251 cfg_data
|= VF610_ADC_ADHSC_EN
;
253 /* voltage reference */
254 switch (adc_feature
->vol_ref
) {
255 case VF610_ADCIOC_VR_VREF_SET
:
257 case VF610_ADCIOC_VR_VALT_SET
:
258 cfg_data
|= VF610_ADC_REFSEL_VALT
;
260 case VF610_ADCIOC_VR_VBG_SET
:
261 cfg_data
|= VF610_ADC_REFSEL_VBG
;
264 dev_err(info
->dev
, "error voltage reference\n");
267 /* data overwrite enable */
268 if (adc_feature
->ovwren
)
269 cfg_data
|= VF610_ADC_OVWREN
;
271 writel(cfg_data
, info
->regs
+ VF610_REG_ADC_CFG
);
272 writel(gc_data
, info
->regs
+ VF610_REG_ADC_GC
);
275 static void vf610_adc_calibration(struct vf610_adc
*info
)
279 if (!info
->adc_feature
.calibration
)
282 /* enable calibration interrupt */
283 hc_cfg
= VF610_ADC_AIEN
| VF610_ADC_CONV_DISABLE
;
284 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
286 adc_gc
= readl(info
->regs
+ VF610_REG_ADC_GC
);
287 writel(adc_gc
| VF610_ADC_CAL
, info
->regs
+ VF610_REG_ADC_GC
);
289 if (!wait_for_completion_timeout(&info
->completion
, VF610_ADC_TIMEOUT
))
290 dev_err(info
->dev
, "Timeout for adc calibration\n");
292 adc_gc
= readl(info
->regs
+ VF610_REG_ADC_GS
);
293 if (adc_gc
& VF610_ADC_CALF
)
294 dev_err(info
->dev
, "ADC calibration failed\n");
296 info
->adc_feature
.calibration
= false;
299 static void vf610_adc_cfg_set(struct vf610_adc
*info
)
301 struct vf610_adc_feature
*adc_feature
= &(info
->adc_feature
);
304 cfg_data
= readl(info
->regs
+ VF610_REG_ADC_CFG
);
306 cfg_data
&= ~VF610_ADC_ADLPC_EN
;
307 if (adc_feature
->lpm
)
308 cfg_data
|= VF610_ADC_ADLPC_EN
;
310 cfg_data
&= ~VF610_ADC_ADHSC_EN
;
312 writel(cfg_data
, info
->regs
+ VF610_REG_ADC_CFG
);
315 static void vf610_adc_sample_set(struct vf610_adc
*info
)
317 struct vf610_adc_feature
*adc_feature
= &(info
->adc_feature
);
318 int cfg_data
, gc_data
;
320 cfg_data
= readl(info
->regs
+ VF610_REG_ADC_CFG
);
321 gc_data
= readl(info
->regs
+ VF610_REG_ADC_GC
);
323 /* resolution mode */
324 cfg_data
&= ~VF610_ADC_MODE_MASK
;
325 switch (adc_feature
->res_mode
) {
327 cfg_data
|= VF610_ADC_MODE_BIT8
;
330 cfg_data
|= VF610_ADC_MODE_BIT10
;
333 cfg_data
|= VF610_ADC_MODE_BIT12
;
336 dev_err(info
->dev
, "error resolution mode\n");
340 /* clock select and clock divider */
341 cfg_data
&= ~(VF610_ADC_CLK_MASK
| VF610_ADC_ADCCLK_MASK
);
342 switch (adc_feature
->clk_div
) {
346 cfg_data
|= VF610_ADC_CLK_DIV2
;
349 cfg_data
|= VF610_ADC_CLK_DIV4
;
352 cfg_data
|= VF610_ADC_CLK_DIV8
;
355 switch (adc_feature
->clk_sel
) {
356 case VF610_ADCIOC_BUSCLK_SET
:
357 cfg_data
|= VF610_ADC_BUSCLK2_SEL
| VF610_ADC_CLK_DIV8
;
360 dev_err(info
->dev
, "error clk divider\n");
366 /* Use the short sample mode */
367 cfg_data
&= ~(VF610_ADC_ADLSMP_LONG
| VF610_ADC_ADSTS_MASK
);
369 /* update hardware average selection */
370 cfg_data
&= ~VF610_ADC_AVGS_MASK
;
371 gc_data
&= ~VF610_ADC_AVGEN
;
372 switch (adc_feature
->sample_rate
) {
373 case VF610_ADC_SAMPLE_1
:
375 case VF610_ADC_SAMPLE_4
:
376 gc_data
|= VF610_ADC_AVGEN
;
378 case VF610_ADC_SAMPLE_8
:
379 gc_data
|= VF610_ADC_AVGEN
;
380 cfg_data
|= VF610_ADC_AVGS_8
;
382 case VF610_ADC_SAMPLE_16
:
383 gc_data
|= VF610_ADC_AVGEN
;
384 cfg_data
|= VF610_ADC_AVGS_16
;
386 case VF610_ADC_SAMPLE_32
:
387 gc_data
|= VF610_ADC_AVGEN
;
388 cfg_data
|= VF610_ADC_AVGS_32
;
392 "error hardware sample average select\n");
395 writel(cfg_data
, info
->regs
+ VF610_REG_ADC_CFG
);
396 writel(gc_data
, info
->regs
+ VF610_REG_ADC_GC
);
399 static void vf610_adc_hw_init(struct vf610_adc
*info
)
401 /* CFG: Feature set */
402 vf610_adc_cfg_post_set(info
);
403 vf610_adc_sample_set(info
);
405 /* adc calibration */
406 vf610_adc_calibration(info
);
408 /* CFG: power and speed set */
409 vf610_adc_cfg_set(info
);
412 static int vf610_adc_read_data(struct vf610_adc
*info
)
416 result
= readl(info
->regs
+ VF610_REG_ADC_R0
);
418 switch (info
->adc_feature
.res_mode
) {
435 static irqreturn_t
vf610_adc_isr(int irq
, void *dev_id
)
437 struct vf610_adc
*info
= (struct vf610_adc
*)dev_id
;
440 coco
= readl(info
->regs
+ VF610_REG_ADC_HS
);
441 if (coco
& VF610_ADC_HS_COCO0
) {
442 info
->value
= vf610_adc_read_data(info
);
443 complete(&info
->completion
);
449 static ssize_t
vf610_show_samp_freq_avail(struct device
*dev
,
450 struct device_attribute
*attr
, char *buf
)
452 struct vf610_adc
*info
= iio_priv(dev_to_iio_dev(dev
));
456 for (i
= 0; i
< ARRAY_SIZE(info
->sample_freq_avail
); i
++)
457 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
458 "%u ", info
->sample_freq_avail
[i
]);
460 /* replace trailing space by newline */
466 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(vf610_show_samp_freq_avail
);
468 static struct attribute
*vf610_attributes
[] = {
469 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
473 static const struct attribute_group vf610_attribute_group
= {
474 .attrs
= vf610_attributes
,
477 static int vf610_read_raw(struct iio_dev
*indio_dev
,
478 struct iio_chan_spec
const *chan
,
483 struct vf610_adc
*info
= iio_priv(indio_dev
);
488 case IIO_CHAN_INFO_RAW
:
489 case IIO_CHAN_INFO_PROCESSED
:
490 mutex_lock(&indio_dev
->mlock
);
491 reinit_completion(&info
->completion
);
493 hc_cfg
= VF610_ADC_ADCHC(chan
->channel
);
494 hc_cfg
|= VF610_ADC_AIEN
;
495 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
496 ret
= wait_for_completion_interruptible_timeout
497 (&info
->completion
, VF610_ADC_TIMEOUT
);
499 mutex_unlock(&indio_dev
->mlock
);
503 mutex_unlock(&indio_dev
->mlock
);
507 switch (chan
->type
) {
513 * Calculate in degree Celsius times 1000
514 * Using sensor slope of 1.84 mV/°C and
515 * V at 25°C of 696 mV
517 *val
= 25000 - ((int)info
->value
- 864) * 1000000 / 1840;
520 mutex_unlock(&indio_dev
->mlock
);
524 mutex_unlock(&indio_dev
->mlock
);
527 case IIO_CHAN_INFO_SCALE
:
528 *val
= info
->vref_uv
/ 1000;
529 *val2
= info
->adc_feature
.res_mode
;
530 return IIO_VAL_FRACTIONAL_LOG2
;
532 case IIO_CHAN_INFO_SAMP_FREQ
:
533 *val
= info
->sample_freq_avail
[info
->adc_feature
.sample_rate
];
544 static int vf610_write_raw(struct iio_dev
*indio_dev
,
545 struct iio_chan_spec
const *chan
,
550 struct vf610_adc
*info
= iio_priv(indio_dev
);
554 case IIO_CHAN_INFO_SAMP_FREQ
:
556 i
< ARRAY_SIZE(info
->sample_freq_avail
);
558 if (val
== info
->sample_freq_avail
[i
]) {
559 info
->adc_feature
.sample_rate
= i
;
560 vf610_adc_sample_set(info
);
572 static int vf610_adc_reg_access(struct iio_dev
*indio_dev
,
573 unsigned reg
, unsigned writeval
,
576 struct vf610_adc
*info
= iio_priv(indio_dev
);
578 if ((readval
== NULL
) ||
579 (!(reg
% 4) || (reg
> VF610_REG_ADC_PCTL
)))
582 *readval
= readl(info
->regs
+ reg
);
587 static const struct iio_info vf610_adc_iio_info
= {
588 .driver_module
= THIS_MODULE
,
589 .read_raw
= &vf610_read_raw
,
590 .write_raw
= &vf610_write_raw
,
591 .debugfs_reg_access
= &vf610_adc_reg_access
,
592 .attrs
= &vf610_attribute_group
,
595 static const struct of_device_id vf610_adc_match
[] = {
596 { .compatible
= "fsl,vf610-adc", },
599 MODULE_DEVICE_TABLE(of
, vf610_adc_match
);
601 static int vf610_adc_probe(struct platform_device
*pdev
)
603 struct vf610_adc
*info
;
604 struct iio_dev
*indio_dev
;
605 struct resource
*mem
;
609 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(struct vf610_adc
));
611 dev_err(&pdev
->dev
, "Failed allocating iio device\n");
615 info
= iio_priv(indio_dev
);
616 info
->dev
= &pdev
->dev
;
618 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
619 info
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
620 if (IS_ERR(info
->regs
))
621 return PTR_ERR(info
->regs
);
623 irq
= platform_get_irq(pdev
, 0);
625 dev_err(&pdev
->dev
, "no irq resource?\n");
629 ret
= devm_request_irq(info
->dev
, irq
,
631 dev_name(&pdev
->dev
), info
);
633 dev_err(&pdev
->dev
, "failed requesting irq, irq = %d\n", irq
);
637 info
->clk
= devm_clk_get(&pdev
->dev
, "adc");
638 if (IS_ERR(info
->clk
)) {
639 dev_err(&pdev
->dev
, "failed getting clock, err = %ld\n",
641 return PTR_ERR(info
->clk
);
644 info
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
645 if (IS_ERR(info
->vref
))
646 return PTR_ERR(info
->vref
);
648 ret
= regulator_enable(info
->vref
);
652 info
->vref_uv
= regulator_get_voltage(info
->vref
);
654 platform_set_drvdata(pdev
, indio_dev
);
656 init_completion(&info
->completion
);
658 indio_dev
->name
= dev_name(&pdev
->dev
);
659 indio_dev
->dev
.parent
= &pdev
->dev
;
660 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
661 indio_dev
->info
= &vf610_adc_iio_info
;
662 indio_dev
->modes
= INDIO_DIRECT_MODE
;
663 indio_dev
->channels
= vf610_adc_iio_channels
;
664 indio_dev
->num_channels
= ARRAY_SIZE(vf610_adc_iio_channels
);
666 ret
= clk_prepare_enable(info
->clk
);
669 "Could not prepare or enable the clock.\n");
670 goto error_adc_clk_enable
;
673 vf610_adc_cfg_init(info
);
674 vf610_adc_hw_init(info
);
676 ret
= iio_device_register(indio_dev
);
678 dev_err(&pdev
->dev
, "Couldn't register the device.\n");
679 goto error_iio_device_register
;
685 error_iio_device_register
:
686 clk_disable_unprepare(info
->clk
);
687 error_adc_clk_enable
:
688 regulator_disable(info
->vref
);
693 static int vf610_adc_remove(struct platform_device
*pdev
)
695 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
696 struct vf610_adc
*info
= iio_priv(indio_dev
);
698 iio_device_unregister(indio_dev
);
699 regulator_disable(info
->vref
);
700 clk_disable_unprepare(info
->clk
);
705 #ifdef CONFIG_PM_SLEEP
706 static int vf610_adc_suspend(struct device
*dev
)
708 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
709 struct vf610_adc
*info
= iio_priv(indio_dev
);
712 /* ADC controller enters to stop mode */
713 hc_cfg
= readl(info
->regs
+ VF610_REG_ADC_HC0
);
714 hc_cfg
|= VF610_ADC_CONV_DISABLE
;
715 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
717 clk_disable_unprepare(info
->clk
);
718 regulator_disable(info
->vref
);
723 static int vf610_adc_resume(struct device
*dev
)
725 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
726 struct vf610_adc
*info
= iio_priv(indio_dev
);
729 ret
= regulator_enable(info
->vref
);
733 ret
= clk_prepare_enable(info
->clk
);
737 vf610_adc_hw_init(info
);
742 regulator_disable(info
->vref
);
747 static SIMPLE_DEV_PM_OPS(vf610_adc_pm_ops
, vf610_adc_suspend
, vf610_adc_resume
);
749 static struct platform_driver vf610_adc_driver
= {
750 .probe
= vf610_adc_probe
,
751 .remove
= vf610_adc_remove
,
754 .of_match_table
= vf610_adc_match
,
755 .pm
= &vf610_adc_pm_ops
,
759 module_platform_driver(vf610_adc_driver
);
761 MODULE_AUTHOR("Fugang Duan <B38611@freescale.com>");
762 MODULE_DESCRIPTION("Freescale VF610 ADC driver");
763 MODULE_LICENSE("GPL v2");