9 #include <linux/module.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
29 #define SCAN_RATE_LO(x) ((x) & 0xFFFF)
30 #define SCAN_RATE_HI(x) (((x) >> 0x10) & 0xFFFF)
31 #define CLK_LOW(x) (((x) & 0xf) << 0)
32 #define CLK_HIGH(x) (((x) & 0xf) << 4)
35 #define START_CONVERSION (1 << 0)
36 #define CHANNEL_NUM(x) ((x) << 1)
37 #define ADC_ENABLE (1 << 4)
38 #define AVG_SAMPLE(x) ((x) << 5)
39 #define VREF_INTERNAL (1 << 9)
41 #define DATA_MASK 0x03ff
44 #define MOD_NAME "spear-adc"
46 #define ADC_CHANNEL_NUM 8
48 #define CLK_MIN 2500000
49 #define CLK_MAX 20000000
104 count = (apb_clk + val - 1) / val;
106 clk_high = count - clk_low;
142 static int spear_read_raw(
struct iio_dev *indio_dev,
162 spear_adc_set_status(info, status);
172 *val = scale_mv / 1000;
173 *val2 = (scale_mv % 1000) * 1000;
180 #define SPEAR_ADC_CHAN(idx) { \
181 .type = IIO_VOLTAGE, \
183 .info_mask = IIO_CHAN_INFO_RAW_SEPARATE_BIT | \
184 IIO_CHAN_INFO_SCALE_SHARED_BIT, \
192 static const struct iio_chan_spec spear_adc_iio_channels[] = {
208 info->
value = spear_adc_get_average(info);
219 spear_adc_set_status(info, 0);
221 for (i = 0; i < 8; i++)
222 spear_adc_set_ctrl(info, i, 0);
223 spear_adc_set_scanrate(info, 0);
234 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
245 struct iio_dev *indio_dev = dev_to_iio_dev(dev);
252 ret = kstrtoul(buf, 10, &lval);
263 count = (apb_clk + lval - 1) / lval;
265 clk_high = count - clk_low;
267 spear_adc_set_clk(info, lval);
272 return ret ? ret : len;
276 spear_adc_read_frequency,
277 spear_adc_write_frequency);
279 static struct attribute *spear_attributes[] = {
280 &iio_dev_attr_sampling_frequency.dev_attr.attr,
285 .attrs = spear_attributes,
288 static const struct iio_info spear_adc_iio_info = {
289 .read_raw = &spear_read_raw,
290 .attrs = &spear_attribute_group,
305 dev_err(dev,
"failed allocating iio device\n");
310 info = iio_priv(iodev);
320 dev_err(dev,
"failed mapping memory\n");
328 if (IS_ERR(info->
clk)) {
329 dev_err(dev,
"failed getting clock\n");
333 ret = clk_prepare_enable(info->
clk);
335 dev_err(dev,
"failed enabling clock\n");
340 if ((irq < 0) || (irq >=
NR_IRQS)) {
341 dev_err(dev,
"failed getting interrupt resource\n");
346 ret = devm_request_irq(dev, irq, spear_adc_isr, 0,
MOD_NAME, info);
348 dev_err(dev,
"failed requesting interrupt\n");
352 if (of_property_read_u32(np,
"sampling-frequency",
354 dev_err(dev,
"sampling-frequency missing in DT\n");
363 of_property_read_u32(np,
"average-samples", &info->
avg_samples);
369 of_property_read_u32(np,
"vref-external", &info->
vref_external);
371 spear_adc_configure(info);
373 platform_set_drvdata(pdev, iodev);
379 iodev->
info = &spear_adc_iio_info;
381 iodev->
channels = spear_adc_iio_channels;
388 dev_info(dev,
"SPEAR ADC driver loaded, IRQ %d\n", irq);
393 clk_disable_unprepare(info->
clk);
406 struct iio_dev *iodev = platform_get_drvdata(pdev);
410 platform_set_drvdata(pdev,
NULL);
411 clk_disable_unprepare(info->
clk);
420 { .compatible =
"st,spear600-adc", },
426 .probe = spear_adc_probe,