blob: c80261748d8fdc36229363d29e68837bc044b4ea [file] [log] [blame]
Johannes Thumshirn74aeac42014-02-26 17:29:07 +01001/*
2 * MEN 16z188 Analog to Digial Converter
3 *
4 * Copyright (C) 2014 MEN Mikroelektronik GmbH (www.men.de)
5 * Author: Johannes Thumshirn <johannes.thumshirn@men.de>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; version 2 of the License.
10 */
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/mcb.h>
Johannes Thumshirn35272752014-03-03 21:46:49 +010015#include <linux/io.h>
Johannes Thumshirn74aeac42014-02-26 17:29:07 +010016#include <linux/iio/iio.h>
17
18#define Z188_ADC_MAX_CHAN 8
19#define Z188_ADC_GAIN 0x0700000
20#define Z188_MODE_VOLTAGE BIT(27)
21#define Z188_CFG_AUTO 0x1
22#define Z188_CTRL_REG 0x40
23
24#define ADC_DATA(x) (((x) >> 2) & 0x7ffffc)
25#define ADC_OVR(x) ((x) & 0x1)
26
27struct z188_adc {
28 struct resource *mem;
29 void __iomem *base;
30};
31
32#define Z188_ADC_CHANNEL(idx) { \
33 .type = IIO_VOLTAGE, \
34 .indexed = 1, \
35 .channel = (idx), \
36 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
37}
38
39static const struct iio_chan_spec z188_adc_iio_channels[] = {
40 Z188_ADC_CHANNEL(0),
41 Z188_ADC_CHANNEL(1),
42 Z188_ADC_CHANNEL(2),
43 Z188_ADC_CHANNEL(3),
44 Z188_ADC_CHANNEL(4),
45 Z188_ADC_CHANNEL(5),
46 Z188_ADC_CHANNEL(6),
47 Z188_ADC_CHANNEL(7),
48};
49
50static int z188_iio_read_raw(struct iio_dev *iio_dev,
51 struct iio_chan_spec const *chan,
52 int *val,
53 int *val2,
54 long info)
55{
56 struct z188_adc *adc = iio_priv(iio_dev);
57 int ret;
58 u16 tmp;
59
60 switch (info) {
61 case IIO_CHAN_INFO_RAW:
62 tmp = readw(adc->base + chan->channel * 4);
63
64 if (ADC_OVR(tmp)) {
65 dev_info(&iio_dev->dev,
66 "Oversampling error on ADC channel %d\n",
67 chan->channel);
68 return -EIO;
69 }
70 *val = ADC_DATA(tmp);
71 ret = IIO_VAL_INT;
72 break;
73 default:
74 ret = -EINVAL;
75 break;
76 }
77
78 return ret;
79}
80
Julia Lawall52ceb772016-09-16 12:55:25 +020081static const struct iio_info z188_adc_info = {
Johannes Thumshirn74aeac42014-02-26 17:29:07 +010082 .read_raw = &z188_iio_read_raw,
Johannes Thumshirn74aeac42014-02-26 17:29:07 +010083};
84
85static void men_z188_config_channels(void __iomem *addr)
86{
87 int i;
88 u32 cfg;
89 u32 ctl;
90
91 ctl = readl(addr + Z188_CTRL_REG);
92 ctl |= Z188_CFG_AUTO;
93 writel(ctl, addr + Z188_CTRL_REG);
94
95 for (i = 0; i < Z188_ADC_MAX_CHAN; i++) {
96 cfg = readl(addr + i);
97 cfg &= ~Z188_ADC_GAIN;
98 cfg |= Z188_MODE_VOLTAGE;
99 writel(cfg, addr + i);
100 }
101}
102
103static int men_z188_probe(struct mcb_device *dev,
104 const struct mcb_device_id *id)
105{
106 struct z188_adc *adc;
107 struct iio_dev *indio_dev;
108 struct resource *mem;
109
110 indio_dev = devm_iio_device_alloc(&dev->dev, sizeof(struct z188_adc));
111 if (!indio_dev)
112 return -ENOMEM;
113
114 adc = iio_priv(indio_dev);
115 indio_dev->name = "z188-adc";
116 indio_dev->dev.parent = &dev->dev;
117 indio_dev->info = &z188_adc_info;
118 indio_dev->modes = INDIO_DIRECT_MODE;
119 indio_dev->channels = z188_adc_iio_channels;
120 indio_dev->num_channels = ARRAY_SIZE(z188_adc_iio_channels);
121
122 mem = mcb_request_mem(dev, "z188-adc");
Dan Carpentere94f62e2014-03-28 08:33:00 +0000123 if (IS_ERR(mem))
124 return PTR_ERR(mem);
Johannes Thumshirn74aeac42014-02-26 17:29:07 +0100125
126 adc->base = ioremap(mem->start, resource_size(mem));
127 if (adc->base == NULL)
128 goto err;
129
130 men_z188_config_channels(adc->base);
131
132 adc->mem = mem;
133 mcb_set_drvdata(dev, indio_dev);
134
135 return iio_device_register(indio_dev);
136
137err:
138 mcb_release_mem(mem);
139 return -ENXIO;
140}
141
142static void men_z188_remove(struct mcb_device *dev)
143{
144 struct iio_dev *indio_dev = mcb_get_drvdata(dev);
145 struct z188_adc *adc = iio_priv(indio_dev);
146
147 iio_device_unregister(indio_dev);
148 iounmap(adc->base);
149 mcb_release_mem(adc->mem);
150}
151
152static const struct mcb_device_id men_z188_ids[] = {
153 { .device = 0xbc },
Axel Linfbbba1f2014-11-10 16:04:06 +0800154 { }
Johannes Thumshirn74aeac42014-02-26 17:29:07 +0100155};
156MODULE_DEVICE_TABLE(mcb, men_z188_ids);
157
158static struct mcb_driver men_z188_driver = {
159 .driver = {
160 .name = "z188-adc",
161 .owner = THIS_MODULE,
162 },
163 .probe = men_z188_probe,
164 .remove = men_z188_remove,
165 .id_table = men_z188_ids,
166};
167module_mcb_driver(men_z188_driver);
168
169MODULE_AUTHOR("Johannes Thumshirn <johannes.thumshirn@men.de>");
170MODULE_LICENSE("GPL");
171MODULE_DESCRIPTION("IIO ADC driver for MEN 16z188 ADC Core");
172MODULE_ALIAS("mcb:16z188");