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
)
280 if (!info
->adc_feature
.calibration
)
283 /* enable calibration interrupt */
284 hc_cfg
= VF610_ADC_AIEN
| VF610_ADC_CONV_DISABLE
;
285 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
287 adc_gc
= readl(info
->regs
+ VF610_REG_ADC_GC
);
288 writel(adc_gc
| VF610_ADC_CAL
, info
->regs
+ VF610_REG_ADC_GC
);
290 timeout
= wait_for_completion_timeout
291 (&info
->completion
, VF610_ADC_TIMEOUT
);
293 dev_err(info
->dev
, "Timeout for adc calibration\n");
295 adc_gc
= readl(info
->regs
+ VF610_REG_ADC_GS
);
296 if (adc_gc
& VF610_ADC_CALF
)
297 dev_err(info
->dev
, "ADC calibration failed\n");
299 info
->adc_feature
.calibration
= false;
302 static void vf610_adc_cfg_set(struct vf610_adc
*info
)
304 struct vf610_adc_feature
*adc_feature
= &(info
->adc_feature
);
307 cfg_data
= readl(info
->regs
+ VF610_REG_ADC_CFG
);
309 cfg_data
&= ~VF610_ADC_ADLPC_EN
;
310 if (adc_feature
->lpm
)
311 cfg_data
|= VF610_ADC_ADLPC_EN
;
313 cfg_data
&= ~VF610_ADC_ADHSC_EN
;
315 writel(cfg_data
, info
->regs
+ VF610_REG_ADC_CFG
);
318 static void vf610_adc_sample_set(struct vf610_adc
*info
)
320 struct vf610_adc_feature
*adc_feature
= &(info
->adc_feature
);
321 int cfg_data
, gc_data
;
323 cfg_data
= readl(info
->regs
+ VF610_REG_ADC_CFG
);
324 gc_data
= readl(info
->regs
+ VF610_REG_ADC_GC
);
326 /* resolution mode */
327 cfg_data
&= ~VF610_ADC_MODE_MASK
;
328 switch (adc_feature
->res_mode
) {
330 cfg_data
|= VF610_ADC_MODE_BIT8
;
333 cfg_data
|= VF610_ADC_MODE_BIT10
;
336 cfg_data
|= VF610_ADC_MODE_BIT12
;
339 dev_err(info
->dev
, "error resolution mode\n");
343 /* clock select and clock divider */
344 cfg_data
&= ~(VF610_ADC_CLK_MASK
| VF610_ADC_ADCCLK_MASK
);
345 switch (adc_feature
->clk_div
) {
349 cfg_data
|= VF610_ADC_CLK_DIV2
;
352 cfg_data
|= VF610_ADC_CLK_DIV4
;
355 cfg_data
|= VF610_ADC_CLK_DIV8
;
358 switch (adc_feature
->clk_sel
) {
359 case VF610_ADCIOC_BUSCLK_SET
:
360 cfg_data
|= VF610_ADC_BUSCLK2_SEL
| VF610_ADC_CLK_DIV8
;
363 dev_err(info
->dev
, "error clk divider\n");
369 /* Use the short sample mode */
370 cfg_data
&= ~(VF610_ADC_ADLSMP_LONG
| VF610_ADC_ADSTS_MASK
);
372 /* update hardware average selection */
373 cfg_data
&= ~VF610_ADC_AVGS_MASK
;
374 gc_data
&= ~VF610_ADC_AVGEN
;
375 switch (adc_feature
->sample_rate
) {
376 case VF610_ADC_SAMPLE_1
:
378 case VF610_ADC_SAMPLE_4
:
379 gc_data
|= VF610_ADC_AVGEN
;
381 case VF610_ADC_SAMPLE_8
:
382 gc_data
|= VF610_ADC_AVGEN
;
383 cfg_data
|= VF610_ADC_AVGS_8
;
385 case VF610_ADC_SAMPLE_16
:
386 gc_data
|= VF610_ADC_AVGEN
;
387 cfg_data
|= VF610_ADC_AVGS_16
;
389 case VF610_ADC_SAMPLE_32
:
390 gc_data
|= VF610_ADC_AVGEN
;
391 cfg_data
|= VF610_ADC_AVGS_32
;
395 "error hardware sample average select\n");
398 writel(cfg_data
, info
->regs
+ VF610_REG_ADC_CFG
);
399 writel(gc_data
, info
->regs
+ VF610_REG_ADC_GC
);
402 static void vf610_adc_hw_init(struct vf610_adc
*info
)
404 /* CFG: Feature set */
405 vf610_adc_cfg_post_set(info
);
406 vf610_adc_sample_set(info
);
408 /* adc calibration */
409 vf610_adc_calibration(info
);
411 /* CFG: power and speed set */
412 vf610_adc_cfg_set(info
);
415 static int vf610_adc_read_data(struct vf610_adc
*info
)
419 result
= readl(info
->regs
+ VF610_REG_ADC_R0
);
421 switch (info
->adc_feature
.res_mode
) {
438 static irqreturn_t
vf610_adc_isr(int irq
, void *dev_id
)
440 struct vf610_adc
*info
= (struct vf610_adc
*)dev_id
;
443 coco
= readl(info
->regs
+ VF610_REG_ADC_HS
);
444 if (coco
& VF610_ADC_HS_COCO0
) {
445 info
->value
= vf610_adc_read_data(info
);
446 complete(&info
->completion
);
452 static ssize_t
vf610_show_samp_freq_avail(struct device
*dev
,
453 struct device_attribute
*attr
, char *buf
)
455 struct vf610_adc
*info
= iio_priv(dev_to_iio_dev(dev
));
459 for (i
= 0; i
< ARRAY_SIZE(info
->sample_freq_avail
); i
++)
460 len
+= scnprintf(buf
+ len
, PAGE_SIZE
- len
,
461 "%u ", info
->sample_freq_avail
[i
]);
463 /* replace trailing space by newline */
469 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(vf610_show_samp_freq_avail
);
471 static struct attribute
*vf610_attributes
[] = {
472 &iio_dev_attr_sampling_frequency_available
.dev_attr
.attr
,
476 static const struct attribute_group vf610_attribute_group
= {
477 .attrs
= vf610_attributes
,
480 static int vf610_read_raw(struct iio_dev
*indio_dev
,
481 struct iio_chan_spec
const *chan
,
486 struct vf610_adc
*info
= iio_priv(indio_dev
);
491 case IIO_CHAN_INFO_RAW
:
492 case IIO_CHAN_INFO_PROCESSED
:
493 mutex_lock(&indio_dev
->mlock
);
494 reinit_completion(&info
->completion
);
496 hc_cfg
= VF610_ADC_ADCHC(chan
->channel
);
497 hc_cfg
|= VF610_ADC_AIEN
;
498 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
499 ret
= wait_for_completion_interruptible_timeout
500 (&info
->completion
, VF610_ADC_TIMEOUT
);
502 mutex_unlock(&indio_dev
->mlock
);
506 mutex_unlock(&indio_dev
->mlock
);
510 switch (chan
->type
) {
516 * Calculate in degree Celsius times 1000
517 * Using sensor slope of 1.84 mV/°C and
518 * V at 25°C of 696 mV
520 *val
= 25000 - ((int)info
->value
- 864) * 1000000 / 1840;
523 mutex_unlock(&indio_dev
->mlock
);
527 mutex_unlock(&indio_dev
->mlock
);
530 case IIO_CHAN_INFO_SCALE
:
531 *val
= info
->vref_uv
/ 1000;
532 *val2
= info
->adc_feature
.res_mode
;
533 return IIO_VAL_FRACTIONAL_LOG2
;
535 case IIO_CHAN_INFO_SAMP_FREQ
:
536 *val
= info
->sample_freq_avail
[info
->adc_feature
.sample_rate
];
547 static int vf610_write_raw(struct iio_dev
*indio_dev
,
548 struct iio_chan_spec
const *chan
,
553 struct vf610_adc
*info
= iio_priv(indio_dev
);
557 case IIO_CHAN_INFO_SAMP_FREQ
:
559 i
< ARRAY_SIZE(info
->sample_freq_avail
);
561 if (val
== info
->sample_freq_avail
[i
]) {
562 info
->adc_feature
.sample_rate
= i
;
563 vf610_adc_sample_set(info
);
575 static int vf610_adc_reg_access(struct iio_dev
*indio_dev
,
576 unsigned reg
, unsigned writeval
,
579 struct vf610_adc
*info
= iio_priv(indio_dev
);
581 if ((readval
== NULL
) ||
582 (!(reg
% 4) || (reg
> VF610_REG_ADC_PCTL
)))
585 *readval
= readl(info
->regs
+ reg
);
590 static const struct iio_info vf610_adc_iio_info
= {
591 .driver_module
= THIS_MODULE
,
592 .read_raw
= &vf610_read_raw
,
593 .write_raw
= &vf610_write_raw
,
594 .debugfs_reg_access
= &vf610_adc_reg_access
,
595 .attrs
= &vf610_attribute_group
,
598 static const struct of_device_id vf610_adc_match
[] = {
599 { .compatible
= "fsl,vf610-adc", },
602 MODULE_DEVICE_TABLE(of
, vf610_adc_match
);
604 static int vf610_adc_probe(struct platform_device
*pdev
)
606 struct vf610_adc
*info
;
607 struct iio_dev
*indio_dev
;
608 struct resource
*mem
;
612 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(struct vf610_adc
));
614 dev_err(&pdev
->dev
, "Failed allocating iio device\n");
618 info
= iio_priv(indio_dev
);
619 info
->dev
= &pdev
->dev
;
621 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
622 info
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
623 if (IS_ERR(info
->regs
))
624 return PTR_ERR(info
->regs
);
626 irq
= platform_get_irq(pdev
, 0);
628 dev_err(&pdev
->dev
, "no irq resource?\n");
632 ret
= devm_request_irq(info
->dev
, irq
,
634 dev_name(&pdev
->dev
), info
);
636 dev_err(&pdev
->dev
, "failed requesting irq, irq = %d\n", irq
);
640 info
->clk
= devm_clk_get(&pdev
->dev
, "adc");
641 if (IS_ERR(info
->clk
)) {
642 dev_err(&pdev
->dev
, "failed getting clock, err = %ld\n",
644 return PTR_ERR(info
->clk
);
647 info
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
648 if (IS_ERR(info
->vref
))
649 return PTR_ERR(info
->vref
);
651 ret
= regulator_enable(info
->vref
);
655 info
->vref_uv
= regulator_get_voltage(info
->vref
);
657 platform_set_drvdata(pdev
, indio_dev
);
659 init_completion(&info
->completion
);
661 indio_dev
->name
= dev_name(&pdev
->dev
);
662 indio_dev
->dev
.parent
= &pdev
->dev
;
663 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
664 indio_dev
->info
= &vf610_adc_iio_info
;
665 indio_dev
->modes
= INDIO_DIRECT_MODE
;
666 indio_dev
->channels
= vf610_adc_iio_channels
;
667 indio_dev
->num_channels
= ARRAY_SIZE(vf610_adc_iio_channels
);
669 ret
= clk_prepare_enable(info
->clk
);
672 "Could not prepare or enable the clock.\n");
673 goto error_adc_clk_enable
;
676 vf610_adc_cfg_init(info
);
677 vf610_adc_hw_init(info
);
679 ret
= iio_device_register(indio_dev
);
681 dev_err(&pdev
->dev
, "Couldn't register the device.\n");
682 goto error_iio_device_register
;
688 error_iio_device_register
:
689 clk_disable_unprepare(info
->clk
);
690 error_adc_clk_enable
:
691 regulator_disable(info
->vref
);
696 static int vf610_adc_remove(struct platform_device
*pdev
)
698 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
699 struct vf610_adc
*info
= iio_priv(indio_dev
);
701 iio_device_unregister(indio_dev
);
702 regulator_disable(info
->vref
);
703 clk_disable_unprepare(info
->clk
);
708 #ifdef CONFIG_PM_SLEEP
709 static int vf610_adc_suspend(struct device
*dev
)
711 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
712 struct vf610_adc
*info
= iio_priv(indio_dev
);
715 /* ADC controller enters to stop mode */
716 hc_cfg
= readl(info
->regs
+ VF610_REG_ADC_HC0
);
717 hc_cfg
|= VF610_ADC_CONV_DISABLE
;
718 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
720 clk_disable_unprepare(info
->clk
);
721 regulator_disable(info
->vref
);
726 static int vf610_adc_resume(struct device
*dev
)
728 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
729 struct vf610_adc
*info
= iio_priv(indio_dev
);
732 ret
= regulator_enable(info
->vref
);
736 ret
= clk_prepare_enable(info
->clk
);
740 vf610_adc_hw_init(info
);
745 regulator_disable(info
->vref
);
750 static SIMPLE_DEV_PM_OPS(vf610_adc_pm_ops
, vf610_adc_suspend
, vf610_adc_resume
);
752 static struct platform_driver vf610_adc_driver
= {
753 .probe
= vf610_adc_probe
,
754 .remove
= vf610_adc_remove
,
757 .of_match_table
= vf610_adc_match
,
758 .pm
= &vf610_adc_pm_ops
,
762 module_platform_driver(vf610_adc_driver
);
764 MODULE_AUTHOR("Fugang Duan <B38611@freescale.com>");
765 MODULE_DESCRIPTION("Freescale VF610 ADC driver");
766 MODULE_LICENSE("GPL v2");