1 // SPDX-License-Identifier: GPL-2.0-only
5 * Copyright 2012 Stefan Roese <sr@denx.de>
8 #include <linux/mod_devicetable.h>
9 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/property.h>
12 #include <linux/interrupt.h>
13 #include <linux/device.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
17 #include <linux/clk.h>
18 #include <linux/err.h>
19 #include <linux/completion.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/sysfs.h>
24 /* SPEAR registers definitions */
25 #define SPEAR600_ADC_SCAN_RATE_LO(x) ((x) & 0xFFFF)
26 #define SPEAR600_ADC_SCAN_RATE_HI(x) (((x) >> 0x10) & 0xFFFF)
27 #define SPEAR_ADC_CLK_LOW(x) (((x) & 0xf) << 0)
28 #define SPEAR_ADC_CLK_HIGH(x) (((x) & 0xf) << 4)
30 /* Bit definitions for SPEAR_ADC_STATUS */
31 #define SPEAR_ADC_STATUS_START_CONVERSION BIT(0)
32 #define SPEAR_ADC_STATUS_CHANNEL_NUM(x) ((x) << 1)
33 #define SPEAR_ADC_STATUS_ADC_ENABLE BIT(4)
34 #define SPEAR_ADC_STATUS_AVG_SAMPLE(x) ((x) << 5)
35 #define SPEAR_ADC_STATUS_VREF_INTERNAL BIT(9)
37 #define SPEAR_ADC_DATA_MASK 0x03ff
38 #define SPEAR_ADC_DATA_BITS 10
40 #define SPEAR_ADC_MOD_NAME "spear-adc"
42 #define SPEAR_ADC_CHANNEL_NUM 8
44 #define SPEAR_ADC_CLK_MIN 2500000
45 #define SPEAR_ADC_CLK_MAX 20000000
47 struct adc_regs_spear3xx
{
51 u32 clk
; /* Not avail for 1340 & 1310 */
52 u32 ch_ctrl
[SPEAR_ADC_CHANNEL_NUM
];
53 u32 ch_data
[SPEAR_ADC_CHANNEL_NUM
];
61 struct adc_regs_spear6xx
{
65 u32 ch_ctrl
[SPEAR_ADC_CHANNEL_NUM
];
66 struct chan_data ch_data
[SPEAR_ADC_CHANNEL_NUM
];
69 struct chan_data average
;
72 struct spear_adc_state
{
74 struct adc_regs_spear3xx __iomem
*adc_base_spear3xx
;
75 struct adc_regs_spear6xx __iomem
*adc_base_spear6xx
;
77 struct completion completion
;
79 * Lock to protect the device state during a potential concurrent
80 * read access from userspace. Reading a raw value requires a sequence
81 * of register writes, then a wait for a completion callback,
82 * and finally a register read, during which userspace could issue
83 * another read request. This lock protects a read access from
84 * ocurring before another one has finished.
95 * Functions to access some SPEAr ADC register. Abstracted into
96 * static inline functions, because of different register offsets
97 * on different SoC variants (SPEAr300 vs SPEAr600 etc).
99 static void spear_adc_set_status(struct spear_adc_state
*st
, u32 val
)
101 __raw_writel(val
, &st
->adc_base_spear6xx
->status
);
104 static void spear_adc_set_clk(struct spear_adc_state
*st
, u32 val
)
106 u32 clk_high
, clk_low
, count
;
107 u32 apb_clk
= clk_get_rate(st
->clk
);
109 count
= DIV_ROUND_UP(apb_clk
, val
);
111 clk_high
= count
- clk_low
;
112 st
->current_clk
= apb_clk
/ count
;
114 __raw_writel(SPEAR_ADC_CLK_LOW(clk_low
) | SPEAR_ADC_CLK_HIGH(clk_high
),
115 &st
->adc_base_spear6xx
->clk
);
118 static void spear_adc_set_ctrl(struct spear_adc_state
*st
, int n
,
121 __raw_writel(val
, &st
->adc_base_spear6xx
->ch_ctrl
[n
]);
124 static u32
spear_adc_get_average(struct spear_adc_state
*st
)
126 if (device_is_compatible(st
->dev
, "st,spear600-adc")) {
127 return __raw_readl(&st
->adc_base_spear6xx
->average
.msb
) &
130 return __raw_readl(&st
->adc_base_spear3xx
->average
) &
135 static void spear_adc_set_scanrate(struct spear_adc_state
*st
, u32 rate
)
137 if (device_is_compatible(st
->dev
, "st,spear600-adc")) {
138 __raw_writel(SPEAR600_ADC_SCAN_RATE_LO(rate
),
139 &st
->adc_base_spear6xx
->scan_rate_lo
);
140 __raw_writel(SPEAR600_ADC_SCAN_RATE_HI(rate
),
141 &st
->adc_base_spear6xx
->scan_rate_hi
);
143 __raw_writel(rate
, &st
->adc_base_spear3xx
->scan_rate
);
147 static int spear_adc_read_raw(struct iio_dev
*indio_dev
,
148 struct iio_chan_spec
const *chan
,
153 struct spear_adc_state
*st
= iio_priv(indio_dev
);
157 case IIO_CHAN_INFO_RAW
:
158 mutex_lock(&st
->lock
);
160 status
= SPEAR_ADC_STATUS_CHANNEL_NUM(chan
->channel
) |
161 SPEAR_ADC_STATUS_AVG_SAMPLE(st
->avg_samples
) |
162 SPEAR_ADC_STATUS_START_CONVERSION
|
163 SPEAR_ADC_STATUS_ADC_ENABLE
;
164 if (st
->vref_external
== 0)
165 status
|= SPEAR_ADC_STATUS_VREF_INTERNAL
;
167 spear_adc_set_status(st
, status
);
168 wait_for_completion(&st
->completion
); /* set by ISR */
171 mutex_unlock(&st
->lock
);
175 case IIO_CHAN_INFO_SCALE
:
176 *val
= st
->vref_external
;
177 *val2
= SPEAR_ADC_DATA_BITS
;
178 return IIO_VAL_FRACTIONAL_LOG2
;
179 case IIO_CHAN_INFO_SAMP_FREQ
:
180 *val
= st
->current_clk
;
187 static int spear_adc_write_raw(struct iio_dev
*indio_dev
,
188 struct iio_chan_spec
const *chan
,
193 struct spear_adc_state
*st
= iio_priv(indio_dev
);
196 if (mask
!= IIO_CHAN_INFO_SAMP_FREQ
)
199 mutex_lock(&st
->lock
);
201 if ((val
< SPEAR_ADC_CLK_MIN
) ||
202 (val
> SPEAR_ADC_CLK_MAX
) ||
208 spear_adc_set_clk(st
, val
);
211 mutex_unlock(&st
->lock
);
215 #define SPEAR_ADC_CHAN(idx) { \
216 .type = IIO_VOLTAGE, \
218 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
219 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
220 .info_mask_shared_by_all = BIT(IIO_CHAN_INFO_SAMP_FREQ),\
224 static const struct iio_chan_spec spear_adc_iio_channels
[] = {
235 static irqreturn_t
spear_adc_isr(int irq
, void *dev_id
)
237 struct spear_adc_state
*st
= dev_id
;
239 /* Read value to clear IRQ */
240 st
->value
= spear_adc_get_average(st
);
241 complete(&st
->completion
);
246 static int spear_adc_configure(struct spear_adc_state
*st
)
251 spear_adc_set_status(st
, 0);
252 __raw_writel(0, &st
->adc_base_spear6xx
->clk
);
253 for (i
= 0; i
< 8; i
++)
254 spear_adc_set_ctrl(st
, i
, 0);
255 spear_adc_set_scanrate(st
, 0);
257 spear_adc_set_clk(st
, st
->sampling_freq
);
262 static const struct iio_info spear_adc_info
= {
263 .read_raw
= &spear_adc_read_raw
,
264 .write_raw
= &spear_adc_write_raw
,
267 static int spear_adc_probe(struct platform_device
*pdev
)
269 struct device
*dev
= &pdev
->dev
;
270 struct spear_adc_state
*st
;
271 struct iio_dev
*indio_dev
= NULL
;
275 indio_dev
= devm_iio_device_alloc(dev
, sizeof(struct spear_adc_state
));
277 return dev_err_probe(dev
, -ENOMEM
,
278 "failed allocating iio device\n");
280 st
= iio_priv(indio_dev
);
283 mutex_init(&st
->lock
);
286 * SPEAr600 has a different register layout than other SPEAr SoC's
287 * (e.g. SPEAr3xx). Let's provide two register base addresses
288 * to support multi-arch kernels.
290 st
->adc_base_spear6xx
= devm_platform_ioremap_resource(pdev
, 0);
291 if (IS_ERR(st
->adc_base_spear6xx
))
292 return PTR_ERR(st
->adc_base_spear6xx
);
294 st
->adc_base_spear3xx
=
295 (struct adc_regs_spear3xx __iomem
*)st
->adc_base_spear6xx
;
297 st
->clk
= devm_clk_get_enabled(dev
, NULL
);
299 return dev_err_probe(dev
, PTR_ERR(st
->clk
),
300 "failed enabling clock\n");
302 irq
= platform_get_irq(pdev
, 0);
306 ret
= devm_request_irq(dev
, irq
, spear_adc_isr
, 0, SPEAR_ADC_MOD_NAME
,
309 return dev_err_probe(dev
, ret
, "failed requesting interrupt\n");
311 if (device_property_read_u32(dev
, "sampling-frequency", &st
->sampling_freq
))
312 return dev_err_probe(dev
, -EINVAL
,
313 "sampling-frequency missing in DT\n");
316 * Optional avg_samples defaults to 0, resulting in single data
319 device_property_read_u32(dev
, "average-samples", &st
->avg_samples
);
322 * Optional vref_external defaults to 0, resulting in internal vref
325 device_property_read_u32(dev
, "vref-external", &st
->vref_external
);
327 spear_adc_configure(st
);
329 init_completion(&st
->completion
);
331 indio_dev
->name
= SPEAR_ADC_MOD_NAME
;
332 indio_dev
->info
= &spear_adc_info
;
333 indio_dev
->modes
= INDIO_DIRECT_MODE
;
334 indio_dev
->channels
= spear_adc_iio_channels
;
335 indio_dev
->num_channels
= ARRAY_SIZE(spear_adc_iio_channels
);
337 ret
= devm_iio_device_register(dev
, indio_dev
);
341 dev_info(dev
, "SPEAR ADC driver loaded, IRQ %d\n", irq
);
346 static const struct of_device_id spear_adc_dt_ids
[] = {
347 { .compatible
= "st,spear600-adc", },
350 MODULE_DEVICE_TABLE(of
, spear_adc_dt_ids
);
352 static struct platform_driver spear_adc_driver
= {
353 .probe
= spear_adc_probe
,
355 .name
= SPEAR_ADC_MOD_NAME
,
356 .of_match_table
= spear_adc_dt_ids
,
360 module_platform_driver(spear_adc_driver
);
362 MODULE_AUTHOR("Stefan Roese <sr@denx.de>");
363 MODULE_DESCRIPTION("SPEAr ADC driver");
364 MODULE_LICENSE("GPL");