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) & 0xF)
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 static const struct iio_chan_spec vf610_adc_iio_channels
[] = {
157 VF610_ADC_CHAN(0, IIO_VOLTAGE
),
158 VF610_ADC_CHAN(1, IIO_VOLTAGE
),
159 VF610_ADC_CHAN(2, IIO_VOLTAGE
),
160 VF610_ADC_CHAN(3, IIO_VOLTAGE
),
161 VF610_ADC_CHAN(4, IIO_VOLTAGE
),
162 VF610_ADC_CHAN(5, IIO_VOLTAGE
),
163 VF610_ADC_CHAN(6, IIO_VOLTAGE
),
164 VF610_ADC_CHAN(7, IIO_VOLTAGE
),
165 VF610_ADC_CHAN(8, IIO_VOLTAGE
),
166 VF610_ADC_CHAN(9, IIO_VOLTAGE
),
167 VF610_ADC_CHAN(10, IIO_VOLTAGE
),
168 VF610_ADC_CHAN(11, IIO_VOLTAGE
),
169 VF610_ADC_CHAN(12, IIO_VOLTAGE
),
170 VF610_ADC_CHAN(13, IIO_VOLTAGE
),
171 VF610_ADC_CHAN(14, IIO_VOLTAGE
),
172 VF610_ADC_CHAN(15, IIO_VOLTAGE
),
177 * ADC sample frequency, unit is ADCK cycles.
178 * ADC clk source is ipg clock, which is the same as bus clock.
180 * ADC conversion time = SFCAdder + AverageNum x (BCT + LSTAdder)
181 * SFCAdder: fixed to 6 ADCK cycles
182 * AverageNum: 1, 4, 8, 16, 32 samples for hardware average.
183 * BCT (Base Conversion Time): fixed to 25 ADCK cycles for 12 bit mode
184 * LSTAdder(Long Sample Time): fixed to 3 ADCK cycles
186 * By default, enable 12 bit resolution mode, clock source
187 * set to ipg clock, So get below frequency group:
189 static const u32 vf610_sample_freq_avail
[5] =
190 {1941176, 559332, 286957, 145374, 73171};
192 static inline void vf610_adc_cfg_init(struct vf610_adc
*info
)
194 /* set default Configuration for ADC controller */
195 info
->adc_feature
.clk_sel
= VF610_ADCIOC_BUSCLK_SET
;
196 info
->adc_feature
.vol_ref
= VF610_ADCIOC_VR_VREF_SET
;
198 info
->adc_feature
.calibration
= true;
199 info
->adc_feature
.ovwren
= true;
201 info
->adc_feature
.clk_div
= 1;
202 info
->adc_feature
.res_mode
= 12;
203 info
->adc_feature
.sample_rate
= 1;
204 info
->adc_feature
.lpm
= true;
207 static void vf610_adc_cfg_post_set(struct vf610_adc
*info
)
209 struct vf610_adc_feature
*adc_feature
= &info
->adc_feature
;
213 switch (adc_feature
->clk_sel
) {
214 case VF610_ADCIOC_ALTCLK_SET
:
215 cfg_data
|= VF610_ADC_ALTCLK_SEL
;
217 case VF610_ADCIOC_ADACK_SET
:
218 cfg_data
|= VF610_ADC_ADACK_SEL
;
224 /* low power set for calibration */
225 cfg_data
|= VF610_ADC_ADLPC_EN
;
227 /* enable high speed for calibration */
228 cfg_data
|= VF610_ADC_ADHSC_EN
;
230 /* voltage reference */
231 switch (adc_feature
->vol_ref
) {
232 case VF610_ADCIOC_VR_VREF_SET
:
234 case VF610_ADCIOC_VR_VALT_SET
:
235 cfg_data
|= VF610_ADC_REFSEL_VALT
;
237 case VF610_ADCIOC_VR_VBG_SET
:
238 cfg_data
|= VF610_ADC_REFSEL_VBG
;
241 dev_err(info
->dev
, "error voltage reference\n");
244 /* data overwrite enable */
245 if (adc_feature
->ovwren
)
246 cfg_data
|= VF610_ADC_OVWREN
;
248 writel(cfg_data
, info
->regs
+ VF610_REG_ADC_CFG
);
249 writel(gc_data
, info
->regs
+ VF610_REG_ADC_GC
);
252 static void vf610_adc_calibration(struct vf610_adc
*info
)
257 if (!info
->adc_feature
.calibration
)
260 /* enable calibration interrupt */
261 hc_cfg
= VF610_ADC_AIEN
| VF610_ADC_CONV_DISABLE
;
262 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
264 adc_gc
= readl(info
->regs
+ VF610_REG_ADC_GC
);
265 writel(adc_gc
| VF610_ADC_CAL
, info
->regs
+ VF610_REG_ADC_GC
);
267 timeout
= wait_for_completion_timeout
268 (&info
->completion
, VF610_ADC_TIMEOUT
);
270 dev_err(info
->dev
, "Timeout for adc calibration\n");
272 adc_gc
= readl(info
->regs
+ VF610_REG_ADC_GS
);
273 if (adc_gc
& VF610_ADC_CALF
)
274 dev_err(info
->dev
, "ADC calibration failed\n");
276 info
->adc_feature
.calibration
= false;
279 static void vf610_adc_cfg_set(struct vf610_adc
*info
)
281 struct vf610_adc_feature
*adc_feature
= &(info
->adc_feature
);
284 cfg_data
= readl(info
->regs
+ VF610_REG_ADC_CFG
);
286 /* low power configuration */
287 cfg_data
&= ~VF610_ADC_ADLPC_EN
;
288 if (adc_feature
->lpm
)
289 cfg_data
|= VF610_ADC_ADLPC_EN
;
291 /* disable high speed */
292 cfg_data
&= ~VF610_ADC_ADHSC_EN
;
294 writel(cfg_data
, info
->regs
+ VF610_REG_ADC_CFG
);
297 static void vf610_adc_sample_set(struct vf610_adc
*info
)
299 struct vf610_adc_feature
*adc_feature
= &(info
->adc_feature
);
300 int cfg_data
, gc_data
;
302 cfg_data
= readl(info
->regs
+ VF610_REG_ADC_CFG
);
303 gc_data
= readl(info
->regs
+ VF610_REG_ADC_GC
);
305 /* resolution mode */
306 cfg_data
&= ~VF610_ADC_MODE_MASK
;
307 switch (adc_feature
->res_mode
) {
309 cfg_data
|= VF610_ADC_MODE_BIT8
;
312 cfg_data
|= VF610_ADC_MODE_BIT10
;
315 cfg_data
|= VF610_ADC_MODE_BIT12
;
318 dev_err(info
->dev
, "error resolution mode\n");
322 /* clock select and clock divider */
323 cfg_data
&= ~(VF610_ADC_CLK_MASK
| VF610_ADC_ADCCLK_MASK
);
324 switch (adc_feature
->clk_div
) {
328 cfg_data
|= VF610_ADC_CLK_DIV2
;
331 cfg_data
|= VF610_ADC_CLK_DIV4
;
334 cfg_data
|= VF610_ADC_CLK_DIV8
;
337 switch (adc_feature
->clk_sel
) {
338 case VF610_ADCIOC_BUSCLK_SET
:
339 cfg_data
|= VF610_ADC_BUSCLK2_SEL
| VF610_ADC_CLK_DIV8
;
342 dev_err(info
->dev
, "error clk divider\n");
348 /* Use the short sample mode */
349 cfg_data
&= ~(VF610_ADC_ADLSMP_LONG
| VF610_ADC_ADSTS_MASK
);
351 /* update hardware average selection */
352 cfg_data
&= ~VF610_ADC_AVGS_MASK
;
353 gc_data
&= ~VF610_ADC_AVGEN
;
354 switch (adc_feature
->sample_rate
) {
355 case VF610_ADC_SAMPLE_1
:
357 case VF610_ADC_SAMPLE_4
:
358 gc_data
|= VF610_ADC_AVGEN
;
360 case VF610_ADC_SAMPLE_8
:
361 gc_data
|= VF610_ADC_AVGEN
;
362 cfg_data
|= VF610_ADC_AVGS_8
;
364 case VF610_ADC_SAMPLE_16
:
365 gc_data
|= VF610_ADC_AVGEN
;
366 cfg_data
|= VF610_ADC_AVGS_16
;
368 case VF610_ADC_SAMPLE_32
:
369 gc_data
|= VF610_ADC_AVGEN
;
370 cfg_data
|= VF610_ADC_AVGS_32
;
374 "error hardware sample average select\n");
377 writel(cfg_data
, info
->regs
+ VF610_REG_ADC_CFG
);
378 writel(gc_data
, info
->regs
+ VF610_REG_ADC_GC
);
381 static void vf610_adc_hw_init(struct vf610_adc
*info
)
383 /* CFG: Feature set */
384 vf610_adc_cfg_post_set(info
);
385 vf610_adc_sample_set(info
);
387 /* adc calibration */
388 vf610_adc_calibration(info
);
390 /* CFG: power and speed set */
391 vf610_adc_cfg_set(info
);
394 static int vf610_adc_read_data(struct vf610_adc
*info
)
398 result
= readl(info
->regs
+ VF610_REG_ADC_R0
);
400 switch (info
->adc_feature
.res_mode
) {
417 static irqreturn_t
vf610_adc_isr(int irq
, void *dev_id
)
419 struct vf610_adc
*info
= (struct vf610_adc
*)dev_id
;
422 coco
= readl(info
->regs
+ VF610_REG_ADC_HS
);
423 if (coco
& VF610_ADC_HS_COCO0
) {
424 info
->value
= vf610_adc_read_data(info
);
425 complete(&info
->completion
);
431 static IIO_CONST_ATTR_SAMP_FREQ_AVAIL("1941176, 559332, 286957, 145374, 73171");
433 static struct attribute
*vf610_attributes
[] = {
434 &iio_const_attr_sampling_frequency_available
.dev_attr
.attr
,
438 static const struct attribute_group vf610_attribute_group
= {
439 .attrs
= vf610_attributes
,
442 static int vf610_read_raw(struct iio_dev
*indio_dev
,
443 struct iio_chan_spec
const *chan
,
448 struct vf610_adc
*info
= iio_priv(indio_dev
);
453 case IIO_CHAN_INFO_RAW
:
454 mutex_lock(&indio_dev
->mlock
);
455 reinit_completion(&info
->completion
);
457 hc_cfg
= VF610_ADC_ADCHC(chan
->channel
);
458 hc_cfg
|= VF610_ADC_AIEN
;
459 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
460 ret
= wait_for_completion_interruptible_timeout
461 (&info
->completion
, VF610_ADC_TIMEOUT
);
463 mutex_unlock(&indio_dev
->mlock
);
467 mutex_unlock(&indio_dev
->mlock
);
472 mutex_unlock(&indio_dev
->mlock
);
475 case IIO_CHAN_INFO_SCALE
:
476 *val
= info
->vref_uv
/ 1000;
477 *val2
= info
->adc_feature
.res_mode
;
478 return IIO_VAL_FRACTIONAL_LOG2
;
480 case IIO_CHAN_INFO_SAMP_FREQ
:
481 *val
= vf610_sample_freq_avail
[info
->adc_feature
.sample_rate
];
492 static int vf610_write_raw(struct iio_dev
*indio_dev
,
493 struct iio_chan_spec
const *chan
,
498 struct vf610_adc
*info
= iio_priv(indio_dev
);
502 case IIO_CHAN_INFO_SAMP_FREQ
:
504 i
< ARRAY_SIZE(vf610_sample_freq_avail
);
506 if (val
== vf610_sample_freq_avail
[i
]) {
507 info
->adc_feature
.sample_rate
= i
;
508 vf610_adc_sample_set(info
);
520 static int vf610_adc_reg_access(struct iio_dev
*indio_dev
,
521 unsigned reg
, unsigned writeval
,
524 struct vf610_adc
*info
= iio_priv(indio_dev
);
526 if ((readval
== NULL
) ||
527 (!(reg
% 4) || (reg
> VF610_REG_ADC_PCTL
)))
530 *readval
= readl(info
->regs
+ reg
);
535 static const struct iio_info vf610_adc_iio_info
= {
536 .driver_module
= THIS_MODULE
,
537 .read_raw
= &vf610_read_raw
,
538 .write_raw
= &vf610_write_raw
,
539 .debugfs_reg_access
= &vf610_adc_reg_access
,
540 .attrs
= &vf610_attribute_group
,
543 static const struct of_device_id vf610_adc_match
[] = {
544 { .compatible
= "fsl,vf610-adc", },
547 MODULE_DEVICE_TABLE(of
, vf610_adc_match
);
549 static int vf610_adc_probe(struct platform_device
*pdev
)
551 struct vf610_adc
*info
;
552 struct iio_dev
*indio_dev
;
553 struct resource
*mem
;
557 indio_dev
= devm_iio_device_alloc(&pdev
->dev
, sizeof(struct vf610_adc
));
559 dev_err(&pdev
->dev
, "Failed allocating iio device\n");
563 info
= iio_priv(indio_dev
);
564 info
->dev
= &pdev
->dev
;
566 mem
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
567 info
->regs
= devm_ioremap_resource(&pdev
->dev
, mem
);
568 if (IS_ERR(info
->regs
))
569 return PTR_ERR(info
->regs
);
571 irq
= platform_get_irq(pdev
, 0);
573 dev_err(&pdev
->dev
, "no irq resource?\n");
577 ret
= devm_request_irq(info
->dev
, irq
,
579 dev_name(&pdev
->dev
), info
);
581 dev_err(&pdev
->dev
, "failed requesting irq, irq = %d\n", irq
);
585 info
->clk
= devm_clk_get(&pdev
->dev
, "adc");
586 if (IS_ERR(info
->clk
)) {
587 dev_err(&pdev
->dev
, "failed getting clock, err = %ld\n",
589 ret
= PTR_ERR(info
->clk
);
593 info
->vref
= devm_regulator_get(&pdev
->dev
, "vref");
594 if (IS_ERR(info
->vref
))
595 return PTR_ERR(info
->vref
);
597 ret
= regulator_enable(info
->vref
);
601 info
->vref_uv
= regulator_get_voltage(info
->vref
);
603 platform_set_drvdata(pdev
, indio_dev
);
605 init_completion(&info
->completion
);
607 indio_dev
->name
= dev_name(&pdev
->dev
);
608 indio_dev
->dev
.parent
= &pdev
->dev
;
609 indio_dev
->dev
.of_node
= pdev
->dev
.of_node
;
610 indio_dev
->info
= &vf610_adc_iio_info
;
611 indio_dev
->modes
= INDIO_DIRECT_MODE
;
612 indio_dev
->channels
= vf610_adc_iio_channels
;
613 indio_dev
->num_channels
= ARRAY_SIZE(vf610_adc_iio_channels
);
615 ret
= clk_prepare_enable(info
->clk
);
618 "Could not prepare or enable the clock.\n");
619 goto error_adc_clk_enable
;
622 vf610_adc_cfg_init(info
);
623 vf610_adc_hw_init(info
);
625 ret
= iio_device_register(indio_dev
);
627 dev_err(&pdev
->dev
, "Couldn't register the device.\n");
628 goto error_iio_device_register
;
634 error_iio_device_register
:
635 clk_disable_unprepare(info
->clk
);
636 error_adc_clk_enable
:
637 regulator_disable(info
->vref
);
642 static int vf610_adc_remove(struct platform_device
*pdev
)
644 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
645 struct vf610_adc
*info
= iio_priv(indio_dev
);
647 iio_device_unregister(indio_dev
);
648 regulator_disable(info
->vref
);
649 clk_disable_unprepare(info
->clk
);
654 #ifdef CONFIG_PM_SLEEP
655 static int vf610_adc_suspend(struct device
*dev
)
657 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
658 struct vf610_adc
*info
= iio_priv(indio_dev
);
661 /* ADC controller enters to stop mode */
662 hc_cfg
= readl(info
->regs
+ VF610_REG_ADC_HC0
);
663 hc_cfg
|= VF610_ADC_CONV_DISABLE
;
664 writel(hc_cfg
, info
->regs
+ VF610_REG_ADC_HC0
);
666 clk_disable_unprepare(info
->clk
);
667 regulator_disable(info
->vref
);
672 static int vf610_adc_resume(struct device
*dev
)
674 struct iio_dev
*indio_dev
= dev_get_drvdata(dev
);
675 struct vf610_adc
*info
= iio_priv(indio_dev
);
678 ret
= regulator_enable(info
->vref
);
682 ret
= clk_prepare_enable(info
->clk
);
686 vf610_adc_hw_init(info
);
692 static SIMPLE_DEV_PM_OPS(vf610_adc_pm_ops
,
696 static struct platform_driver vf610_adc_driver
= {
697 .probe
= vf610_adc_probe
,
698 .remove
= vf610_adc_remove
,
701 .owner
= THIS_MODULE
,
702 .of_match_table
= vf610_adc_match
,
703 .pm
= &vf610_adc_pm_ops
,
707 module_platform_driver(vf610_adc_driver
);
709 MODULE_AUTHOR("Fugang Duan <B38611@freescale.com>");
710 MODULE_DESCRIPTION("Freescale VF610 ADC driver");
711 MODULE_LICENSE("GPL v2");