blob: 357e6c21378487ac6f9663766a79de3050209d3a [file] [log] [blame]
Phani Movva1664f6a2015-01-06 17:47:35 -03001/*
2 * Copyright (c) 2014-2015 Imagination Technologies Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published by
6 * the Free Software Foundation.
7 *
8 */
9
10#include <linux/clk.h>
11#include <linux/delay.h>
12#include <linux/err.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/of.h>
16#include <linux/of_device.h>
17#include <linux/platform_device.h>
18#include <linux/regulator/consumer.h>
19#include <linux/slab.h>
20
21#include <linux/iio/buffer.h>
22#include <linux/iio/iio.h>
23#include <linux/iio/sysfs.h>
24#include <linux/iio/trigger.h>
25#include <linux/iio/trigger_consumer.h>
26#include <linux/iio/triggered_buffer.h>
27
28/* Registers */
29#define CC10001_ADC_CONFIG 0x00
30#define CC10001_ADC_START_CONV BIT(4)
31#define CC10001_ADC_MODE_SINGLE_CONV BIT(5)
32
33#define CC10001_ADC_DDATA_OUT 0x04
34#define CC10001_ADC_EOC 0x08
35#define CC10001_ADC_EOC_SET BIT(0)
36
37#define CC10001_ADC_CHSEL_SAMPLED 0x0c
38#define CC10001_ADC_POWER_UP 0x10
39#define CC10001_ADC_POWER_UP_SET BIT(0)
40#define CC10001_ADC_DEBUG 0x14
41#define CC10001_ADC_DATA_COUNT 0x20
42
43#define CC10001_ADC_DATA_MASK GENMASK(9, 0)
44#define CC10001_ADC_NUM_CHANNELS 8
45#define CC10001_ADC_CH_MASK GENMASK(2, 0)
46
47#define CC10001_INVALID_SAMPLED 0xffff
48#define CC10001_MAX_POLL_COUNT 20
49
50/*
51 * As per device specification, wait six clock cycles after power-up to
52 * activate START. Since adding two more clock cycles delay does not
53 * impact the performance too much, we are adding two additional cycles delay
54 * intentionally here.
55 */
56#define CC10001_WAIT_CYCLES 8
57
58struct cc10001_adc_device {
59 void __iomem *reg_base;
60 struct clk *adc_clk;
61 struct regulator *reg;
62 u16 *buf;
63
64 struct mutex lock;
Phani Movva1664f6a2015-01-06 17:47:35 -030065 unsigned int start_delay_ns;
66 unsigned int eoc_delay_ns;
67};
68
69static inline void cc10001_adc_write_reg(struct cc10001_adc_device *adc_dev,
70 u32 reg, u32 val)
71{
72 writel(val, adc_dev->reg_base + reg);
73}
74
75static inline u32 cc10001_adc_read_reg(struct cc10001_adc_device *adc_dev,
76 u32 reg)
77{
78 return readl(adc_dev->reg_base + reg);
79}
80
81static void cc10001_adc_start(struct cc10001_adc_device *adc_dev,
82 unsigned int channel)
83{
84 u32 val;
85
86 /* Channel selection and mode of operation */
87 val = (channel & CC10001_ADC_CH_MASK) | CC10001_ADC_MODE_SINGLE_CONV;
88 cc10001_adc_write_reg(adc_dev, CC10001_ADC_CONFIG, val);
89
90 val = cc10001_adc_read_reg(adc_dev, CC10001_ADC_CONFIG);
91 val = val | CC10001_ADC_START_CONV;
92 cc10001_adc_write_reg(adc_dev, CC10001_ADC_CONFIG, val);
93}
94
95static u16 cc10001_adc_poll_done(struct iio_dev *indio_dev,
96 unsigned int channel,
97 unsigned int delay)
98{
99 struct cc10001_adc_device *adc_dev = iio_priv(indio_dev);
100 unsigned int poll_count = 0;
101
102 while (!(cc10001_adc_read_reg(adc_dev, CC10001_ADC_EOC) &
103 CC10001_ADC_EOC_SET)) {
104
105 ndelay(delay);
106 if (poll_count++ == CC10001_MAX_POLL_COUNT)
107 return CC10001_INVALID_SAMPLED;
108 }
109
110 poll_count = 0;
111 while ((cc10001_adc_read_reg(adc_dev, CC10001_ADC_CHSEL_SAMPLED) &
112 CC10001_ADC_CH_MASK) != channel) {
113
114 ndelay(delay);
115 if (poll_count++ == CC10001_MAX_POLL_COUNT)
116 return CC10001_INVALID_SAMPLED;
117 }
118
119 /* Read the 10 bit output register */
120 return cc10001_adc_read_reg(adc_dev, CC10001_ADC_DDATA_OUT) &
121 CC10001_ADC_DATA_MASK;
122}
123
124static irqreturn_t cc10001_adc_trigger_h(int irq, void *p)
125{
126 struct cc10001_adc_device *adc_dev;
127 struct iio_poll_func *pf = p;
128 struct iio_dev *indio_dev;
129 unsigned int delay_ns;
130 unsigned int channel;
Naidu Tellapati13415a992015-05-07 18:22:17 -0300131 unsigned int scan_idx;
Phani Movva1664f6a2015-01-06 17:47:35 -0300132 bool sample_invalid;
133 u16 *data;
134 int i;
135
136 indio_dev = pf->indio_dev;
137 adc_dev = iio_priv(indio_dev);
138 data = adc_dev->buf;
139
140 mutex_lock(&adc_dev->lock);
141
142 cc10001_adc_write_reg(adc_dev, CC10001_ADC_POWER_UP,
143 CC10001_ADC_POWER_UP_SET);
144
145 /* Wait for 8 (6+2) clock cycles before activating START */
146 ndelay(adc_dev->start_delay_ns);
147
148 /* Calculate delay step for eoc and sampled data */
149 delay_ns = adc_dev->eoc_delay_ns / CC10001_MAX_POLL_COUNT;
150
151 i = 0;
152 sample_invalid = false;
Naidu Tellapati13415a992015-05-07 18:22:17 -0300153 for_each_set_bit(scan_idx, indio_dev->active_scan_mask,
Phani Movva1664f6a2015-01-06 17:47:35 -0300154 indio_dev->masklength) {
155
Naidu Tellapati13415a992015-05-07 18:22:17 -0300156 channel = indio_dev->channels[scan_idx].channel;
Phani Movva1664f6a2015-01-06 17:47:35 -0300157 cc10001_adc_start(adc_dev, channel);
158
159 data[i] = cc10001_adc_poll_done(indio_dev, channel, delay_ns);
160 if (data[i] == CC10001_INVALID_SAMPLED) {
161 dev_warn(&indio_dev->dev,
162 "invalid sample on channel %d\n", channel);
163 sample_invalid = true;
164 goto done;
165 }
166 i++;
167 }
168
169done:
170 cc10001_adc_write_reg(adc_dev, CC10001_ADC_POWER_UP, 0);
171
172 mutex_unlock(&adc_dev->lock);
173
174 if (!sample_invalid)
175 iio_push_to_buffers_with_timestamp(indio_dev, data,
176 iio_get_time_ns());
177 iio_trigger_notify_done(indio_dev->trig);
178
179 return IRQ_HANDLED;
180}
181
182static u16 cc10001_adc_read_raw_voltage(struct iio_dev *indio_dev,
183 struct iio_chan_spec const *chan)
184{
185 struct cc10001_adc_device *adc_dev = iio_priv(indio_dev);
186 unsigned int delay_ns;
187 u16 val;
188
189 cc10001_adc_write_reg(adc_dev, CC10001_ADC_POWER_UP,
190 CC10001_ADC_POWER_UP_SET);
191
192 /* Wait for 8 (6+2) clock cycles before activating START */
193 ndelay(adc_dev->start_delay_ns);
194
195 /* Calculate delay step for eoc and sampled data */
196 delay_ns = adc_dev->eoc_delay_ns / CC10001_MAX_POLL_COUNT;
197
198 cc10001_adc_start(adc_dev, chan->channel);
199
200 val = cc10001_adc_poll_done(indio_dev, chan->channel, delay_ns);
201
202 cc10001_adc_write_reg(adc_dev, CC10001_ADC_POWER_UP, 0);
203
204 return val;
205}
206
207static int cc10001_adc_read_raw(struct iio_dev *indio_dev,
208 struct iio_chan_spec const *chan,
209 int *val, int *val2, long mask)
210{
211 struct cc10001_adc_device *adc_dev = iio_priv(indio_dev);
212 int ret;
213
214 switch (mask) {
215 case IIO_CHAN_INFO_RAW:
216 if (iio_buffer_enabled(indio_dev))
217 return -EBUSY;
218 mutex_lock(&adc_dev->lock);
219 *val = cc10001_adc_read_raw_voltage(indio_dev, chan);
220 mutex_unlock(&adc_dev->lock);
221
222 if (*val == CC10001_INVALID_SAMPLED)
223 return -EIO;
224 return IIO_VAL_INT;
225
226 case IIO_CHAN_INFO_SCALE:
227 ret = regulator_get_voltage(adc_dev->reg);
228 if (ret)
229 return ret;
230
231 *val = ret / 1000;
232 *val2 = chan->scan_type.realbits;
233 return IIO_VAL_FRACTIONAL_LOG2;
234
235 default:
236 return -EINVAL;
237 }
238}
239
240static int cc10001_update_scan_mode(struct iio_dev *indio_dev,
241 const unsigned long *scan_mask)
242{
243 struct cc10001_adc_device *adc_dev = iio_priv(indio_dev);
244
245 kfree(adc_dev->buf);
246 adc_dev->buf = kmalloc(indio_dev->scan_bytes, GFP_KERNEL);
247 if (!adc_dev->buf)
248 return -ENOMEM;
249
250 return 0;
251}
252
253static const struct iio_info cc10001_adc_info = {
254 .driver_module = THIS_MODULE,
255 .read_raw = &cc10001_adc_read_raw,
256 .update_scan_mode = &cc10001_update_scan_mode,
257};
258
Naidu Tellapati13415a992015-05-07 18:22:17 -0300259static int cc10001_adc_channel_init(struct iio_dev *indio_dev,
260 unsigned long channel_map)
Phani Movva1664f6a2015-01-06 17:47:35 -0300261{
Phani Movva1664f6a2015-01-06 17:47:35 -0300262 struct iio_chan_spec *chan_array, *timestamp;
263 unsigned int bit, idx = 0;
264
Naidu Tellapati13415a992015-05-07 18:22:17 -0300265 indio_dev->num_channels = bitmap_weight(&channel_map,
266 CC10001_ADC_NUM_CHANNELS) + 1;
Phani Movva1664f6a2015-01-06 17:47:35 -0300267
Naidu Tellapati13415a992015-05-07 18:22:17 -0300268 chan_array = devm_kcalloc(&indio_dev->dev, indio_dev->num_channels,
Phani Movva1664f6a2015-01-06 17:47:35 -0300269 sizeof(struct iio_chan_spec),
270 GFP_KERNEL);
271 if (!chan_array)
272 return -ENOMEM;
273
Naidu Tellapati13415a992015-05-07 18:22:17 -0300274 for_each_set_bit(bit, &channel_map, CC10001_ADC_NUM_CHANNELS) {
Phani Movva1664f6a2015-01-06 17:47:35 -0300275 struct iio_chan_spec *chan = &chan_array[idx];
276
277 chan->type = IIO_VOLTAGE;
278 chan->indexed = 1;
279 chan->channel = bit;
280 chan->scan_index = idx;
281 chan->scan_type.sign = 'u';
282 chan->scan_type.realbits = 10;
283 chan->scan_type.storagebits = 16;
284 chan->info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE);
285 chan->info_mask_separate = BIT(IIO_CHAN_INFO_RAW);
286 idx++;
287 }
288
289 timestamp = &chan_array[idx];
290 timestamp->type = IIO_TIMESTAMP;
291 timestamp->channel = -1;
292 timestamp->scan_index = idx;
293 timestamp->scan_type.sign = 's';
294 timestamp->scan_type.realbits = 64;
295 timestamp->scan_type.storagebits = 64;
296
297 indio_dev->channels = chan_array;
298
299 return 0;
300}
301
302static int cc10001_adc_probe(struct platform_device *pdev)
303{
304 struct device_node *node = pdev->dev.of_node;
305 struct cc10001_adc_device *adc_dev;
306 unsigned long adc_clk_rate;
307 struct resource *res;
308 struct iio_dev *indio_dev;
Naidu Tellapati13415a992015-05-07 18:22:17 -0300309 unsigned long channel_map;
Phani Movva1664f6a2015-01-06 17:47:35 -0300310 int ret;
311
312 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*adc_dev));
313 if (indio_dev == NULL)
314 return -ENOMEM;
315
316 adc_dev = iio_priv(indio_dev);
317
Naidu Tellapati13415a992015-05-07 18:22:17 -0300318 channel_map = GENMASK(CC10001_ADC_NUM_CHANNELS - 1, 0);
Phani Movva1664f6a2015-01-06 17:47:35 -0300319 if (!of_property_read_u32(node, "adc-reserved-channels", &ret))
Naidu Tellapati13415a992015-05-07 18:22:17 -0300320 channel_map &= ~ret;
Phani Movva1664f6a2015-01-06 17:47:35 -0300321
322 adc_dev->reg = devm_regulator_get(&pdev->dev, "vref");
323 if (IS_ERR(adc_dev->reg))
324 return PTR_ERR(adc_dev->reg);
325
326 ret = regulator_enable(adc_dev->reg);
327 if (ret)
328 return ret;
329
330 indio_dev->dev.parent = &pdev->dev;
331 indio_dev->name = dev_name(&pdev->dev);
332 indio_dev->info = &cc10001_adc_info;
333 indio_dev->modes = INDIO_DIRECT_MODE;
334
335 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
336 adc_dev->reg_base = devm_ioremap_resource(&pdev->dev, res);
337 if (IS_ERR(adc_dev->reg_base)) {
338 ret = PTR_ERR(adc_dev->reg_base);
339 goto err_disable_reg;
340 }
341
342 adc_dev->adc_clk = devm_clk_get(&pdev->dev, "adc");
343 if (IS_ERR(adc_dev->adc_clk)) {
344 dev_err(&pdev->dev, "failed to get the clock\n");
345 ret = PTR_ERR(adc_dev->adc_clk);
346 goto err_disable_reg;
347 }
348
349 ret = clk_prepare_enable(adc_dev->adc_clk);
350 if (ret) {
351 dev_err(&pdev->dev, "failed to enable the clock\n");
352 goto err_disable_reg;
353 }
354
355 adc_clk_rate = clk_get_rate(adc_dev->adc_clk);
356 if (!adc_clk_rate) {
357 ret = -EINVAL;
358 dev_err(&pdev->dev, "null clock rate!\n");
359 goto err_disable_clk;
360 }
361
362 adc_dev->eoc_delay_ns = NSEC_PER_SEC / adc_clk_rate;
363 adc_dev->start_delay_ns = adc_dev->eoc_delay_ns * CC10001_WAIT_CYCLES;
364
365 /* Setup the ADC channels available on the device */
Naidu Tellapati13415a992015-05-07 18:22:17 -0300366 ret = cc10001_adc_channel_init(indio_dev, channel_map);
Phani Movva1664f6a2015-01-06 17:47:35 -0300367 if (ret < 0)
368 goto err_disable_clk;
369
370 mutex_init(&adc_dev->lock);
371
372 ret = iio_triggered_buffer_setup(indio_dev, NULL,
373 &cc10001_adc_trigger_h, NULL);
374 if (ret < 0)
375 goto err_disable_clk;
376
377 ret = iio_device_register(indio_dev);
378 if (ret < 0)
379 goto err_cleanup_buffer;
380
381 platform_set_drvdata(pdev, indio_dev);
382
383 return 0;
384
385err_cleanup_buffer:
386 iio_triggered_buffer_cleanup(indio_dev);
387err_disable_clk:
388 clk_disable_unprepare(adc_dev->adc_clk);
389err_disable_reg:
390 regulator_disable(adc_dev->reg);
391 return ret;
392}
393
394static int cc10001_adc_remove(struct platform_device *pdev)
395{
396 struct iio_dev *indio_dev = platform_get_drvdata(pdev);
397 struct cc10001_adc_device *adc_dev = iio_priv(indio_dev);
398
399 iio_device_unregister(indio_dev);
400 iio_triggered_buffer_cleanup(indio_dev);
401 clk_disable_unprepare(adc_dev->adc_clk);
402 regulator_disable(adc_dev->reg);
403
404 return 0;
405}
406
407static const struct of_device_id cc10001_adc_dt_ids[] = {
408 { .compatible = "cosmic,10001-adc", },
409 { }
410};
411MODULE_DEVICE_TABLE(of, cc10001_adc_dt_ids);
412
413static struct platform_driver cc10001_adc_driver = {
414 .driver = {
415 .name = "cc10001-adc",
416 .of_match_table = cc10001_adc_dt_ids,
417 },
418 .probe = cc10001_adc_probe,
419 .remove = cc10001_adc_remove,
420};
421module_platform_driver(cc10001_adc_driver);
422
423MODULE_AUTHOR("Phani Movva <Phani.Movva@imgtec.com>");
424MODULE_DESCRIPTION("Cosmic Circuits ADC driver");
425MODULE_LICENSE("GPL v2");