dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / drivers / iio / adc / ad_sigma_delta.c
blob22c4c17cd9969486fe1ef80b68eaa99e51c43032
1 /*
2 * Support code for Analog Devices Sigma-Delta ADCs
4 * Copyright 2012 Analog Devices Inc.
5 * Author: Lars-Peter Clausen <lars@metafoo.de>
7 * Licensed under the GPL-2.
8 */
10 #include <linux/interrupt.h>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
15 #include <linux/err.h>
16 #include <linux/module.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/sysfs.h>
20 #include <linux/iio/buffer.h>
21 #include <linux/iio/trigger.h>
22 #include <linux/iio/trigger_consumer.h>
23 #include <linux/iio/triggered_buffer.h>
24 #include <linux/iio/adc/ad_sigma_delta.h>
26 #include <asm/unaligned.h>
29 #define AD_SD_COMM_CHAN_MASK 0x3
31 #define AD_SD_REG_COMM 0x00
32 #define AD_SD_REG_DATA 0x03
34 /**
35 * ad_sd_set_comm() - Set communications register
37 * @sigma_delta: The sigma delta device
38 * @comm: New value for the communications register
40 void ad_sd_set_comm(struct ad_sigma_delta *sigma_delta, uint8_t comm)
42 /* Some variants use the lower two bits of the communications register
43 * to select the channel */
44 sigma_delta->comm = comm & AD_SD_COMM_CHAN_MASK;
46 EXPORT_SYMBOL_GPL(ad_sd_set_comm);
48 /**
49 * ad_sd_write_reg() - Write a register
51 * @sigma_delta: The sigma delta device
52 * @reg: Address of the register
53 * @size: Size of the register (0-3)
54 * @val: Value to write to the register
56 * Returns 0 on success, an error code otherwise.
57 **/
58 int ad_sd_write_reg(struct ad_sigma_delta *sigma_delta, unsigned int reg,
59 unsigned int size, unsigned int val)
61 uint8_t *data = sigma_delta->data;
62 struct spi_transfer t = {
63 .tx_buf = data,
64 .len = size + 1,
65 .cs_change = sigma_delta->bus_locked,
67 struct spi_message m;
68 int ret;
70 data[0] = (reg << sigma_delta->info->addr_shift) | sigma_delta->comm;
72 switch (size) {
73 case 3:
74 data[1] = val >> 16;
75 data[2] = val >> 8;
76 data[3] = val;
77 break;
78 case 2:
79 put_unaligned_be16(val, &data[1]);
80 break;
81 case 1:
82 data[1] = val;
83 break;
84 case 0:
85 break;
86 default:
87 return -EINVAL;
90 spi_message_init(&m);
91 spi_message_add_tail(&t, &m);
93 if (sigma_delta->bus_locked)
94 ret = spi_sync_locked(sigma_delta->spi, &m);
95 else
96 ret = spi_sync(sigma_delta->spi, &m);
98 return ret;
100 EXPORT_SYMBOL_GPL(ad_sd_write_reg);
102 static int ad_sd_read_reg_raw(struct ad_sigma_delta *sigma_delta,
103 unsigned int reg, unsigned int size, uint8_t *val)
105 uint8_t *data = sigma_delta->data;
106 int ret;
107 struct spi_transfer t[] = {
109 .tx_buf = data,
110 .len = 1,
111 }, {
112 .rx_buf = val,
113 .len = size,
114 .cs_change = sigma_delta->bus_locked,
117 struct spi_message m;
119 spi_message_init(&m);
121 if (sigma_delta->info->has_registers) {
122 data[0] = reg << sigma_delta->info->addr_shift;
123 data[0] |= sigma_delta->info->read_mask;
124 spi_message_add_tail(&t[0], &m);
126 spi_message_add_tail(&t[1], &m);
128 if (sigma_delta->bus_locked)
129 ret = spi_sync_locked(sigma_delta->spi, &m);
130 else
131 ret = spi_sync(sigma_delta->spi, &m);
133 return ret;
137 * ad_sd_read_reg() - Read a register
139 * @sigma_delta: The sigma delta device
140 * @reg: Address of the register
141 * @size: Size of the register (1-4)
142 * @val: Read value
144 * Returns 0 on success, an error code otherwise.
146 int ad_sd_read_reg(struct ad_sigma_delta *sigma_delta,
147 unsigned int reg, unsigned int size, unsigned int *val)
149 int ret;
151 ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data);
152 if (ret < 0)
153 goto out;
155 switch (size) {
156 case 4:
157 *val = get_unaligned_be32(sigma_delta->data);
158 break;
159 case 3:
160 *val = (sigma_delta->data[0] << 16) |
161 (sigma_delta->data[1] << 8) |
162 sigma_delta->data[2];
163 break;
164 case 2:
165 *val = get_unaligned_be16(sigma_delta->data);
166 break;
167 case 1:
168 *val = sigma_delta->data[0];
169 break;
170 default:
171 ret = -EINVAL;
172 break;
175 out:
176 return ret;
178 EXPORT_SYMBOL_GPL(ad_sd_read_reg);
181 * ad_sd_reset() - Reset the serial interface
183 * @sigma_delta: The sigma delta device
184 * @reset_length: Number of SCLKs with DIN = 1
186 * Returns 0 on success, an error code otherwise.
188 int ad_sd_reset(struct ad_sigma_delta *sigma_delta,
189 unsigned int reset_length)
191 uint8_t *buf;
192 unsigned int size;
193 int ret;
195 size = DIV_ROUND_UP(reset_length, 8);
196 buf = kcalloc(size, sizeof(*buf), GFP_KERNEL);
197 if (!buf)
198 return -ENOMEM;
200 memset(buf, 0xff, size);
201 ret = spi_write(sigma_delta->spi, buf, size);
202 kfree(buf);
204 return ret;
206 EXPORT_SYMBOL_GPL(ad_sd_reset);
208 static int ad_sd_calibrate(struct ad_sigma_delta *sigma_delta,
209 unsigned int mode, unsigned int channel)
211 int ret;
213 ret = ad_sigma_delta_set_channel(sigma_delta, channel);
214 if (ret)
215 return ret;
217 spi_bus_lock(sigma_delta->spi->master);
218 sigma_delta->bus_locked = true;
219 reinit_completion(&sigma_delta->completion);
221 ret = ad_sigma_delta_set_mode(sigma_delta, mode);
222 if (ret < 0)
223 goto out;
225 sigma_delta->irq_dis = false;
226 enable_irq(sigma_delta->spi->irq);
227 ret = wait_for_completion_timeout(&sigma_delta->completion, 2*HZ);
228 if (ret == 0) {
229 sigma_delta->irq_dis = true;
230 disable_irq_nosync(sigma_delta->spi->irq);
231 ret = -EIO;
232 } else {
233 ret = 0;
235 out:
236 sigma_delta->bus_locked = false;
237 spi_bus_unlock(sigma_delta->spi->master);
238 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
240 return ret;
244 * ad_sd_calibrate_all() - Performs channel calibration
245 * @sigma_delta: The sigma delta device
246 * @cb: Array of channels and calibration type to perform
247 * @n: Number of items in cb
249 * Returns 0 on success, an error code otherwise.
251 int ad_sd_calibrate_all(struct ad_sigma_delta *sigma_delta,
252 const struct ad_sd_calib_data *cb, unsigned int n)
254 unsigned int i;
255 int ret;
257 for (i = 0; i < n; i++) {
258 ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel);
259 if (ret)
260 return ret;
263 return 0;
265 EXPORT_SYMBOL_GPL(ad_sd_calibrate_all);
268 * ad_sigma_delta_single_conversion() - Performs a single data conversion
269 * @indio_dev: The IIO device
270 * @chan: The conversion is done for this channel
271 * @val: Pointer to the location where to store the read value
273 * Returns: 0 on success, an error value otherwise.
275 int ad_sigma_delta_single_conversion(struct iio_dev *indio_dev,
276 const struct iio_chan_spec *chan, int *val)
278 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
279 unsigned int sample, raw_sample;
280 int ret = 0;
282 if (iio_buffer_enabled(indio_dev))
283 return -EBUSY;
285 mutex_lock(&indio_dev->mlock);
286 ad_sigma_delta_set_channel(sigma_delta, chan->address);
288 spi_bus_lock(sigma_delta->spi->master);
289 sigma_delta->bus_locked = true;
290 reinit_completion(&sigma_delta->completion);
292 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_SINGLE);
294 sigma_delta->irq_dis = false;
295 enable_irq(sigma_delta->spi->irq);
296 ret = wait_for_completion_interruptible_timeout(
297 &sigma_delta->completion, HZ);
299 sigma_delta->bus_locked = false;
300 spi_bus_unlock(sigma_delta->spi->master);
302 if (ret == 0)
303 ret = -EIO;
304 if (ret < 0)
305 goto out;
307 ret = ad_sd_read_reg(sigma_delta, AD_SD_REG_DATA,
308 DIV_ROUND_UP(chan->scan_type.realbits + chan->scan_type.shift, 8),
309 &raw_sample);
311 out:
312 if (!sigma_delta->irq_dis) {
313 disable_irq_nosync(sigma_delta->spi->irq);
314 sigma_delta->irq_dis = true;
317 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
318 mutex_unlock(&indio_dev->mlock);
320 if (ret)
321 return ret;
323 sample = raw_sample >> chan->scan_type.shift;
324 sample &= (1 << chan->scan_type.realbits) - 1;
325 *val = sample;
327 ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
328 if (ret)
329 return ret;
331 return IIO_VAL_INT;
333 EXPORT_SYMBOL_GPL(ad_sigma_delta_single_conversion);
335 static int ad_sd_buffer_postenable(struct iio_dev *indio_dev)
337 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
338 unsigned int channel;
339 int ret;
341 ret = iio_triggered_buffer_postenable(indio_dev);
342 if (ret < 0)
343 return ret;
345 channel = find_first_bit(indio_dev->active_scan_mask,
346 indio_dev->masklength);
347 ret = ad_sigma_delta_set_channel(sigma_delta,
348 indio_dev->channels[channel].address);
349 if (ret)
350 goto err_predisable;
352 spi_bus_lock(sigma_delta->spi->master);
353 sigma_delta->bus_locked = true;
354 ret = ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_CONTINUOUS);
355 if (ret)
356 goto err_unlock;
358 sigma_delta->irq_dis = false;
359 enable_irq(sigma_delta->spi->irq);
361 return 0;
363 err_unlock:
364 spi_bus_unlock(sigma_delta->spi->master);
365 err_predisable:
367 return ret;
370 static int ad_sd_buffer_postdisable(struct iio_dev *indio_dev)
372 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
374 reinit_completion(&sigma_delta->completion);
375 wait_for_completion_timeout(&sigma_delta->completion, HZ);
377 if (!sigma_delta->irq_dis) {
378 disable_irq_nosync(sigma_delta->spi->irq);
379 sigma_delta->irq_dis = true;
382 ad_sigma_delta_set_mode(sigma_delta, AD_SD_MODE_IDLE);
384 sigma_delta->bus_locked = false;
385 return spi_bus_unlock(sigma_delta->spi->master);
388 static irqreturn_t ad_sd_trigger_handler(int irq, void *p)
390 struct iio_poll_func *pf = p;
391 struct iio_dev *indio_dev = pf->indio_dev;
392 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
393 unsigned int reg_size;
394 uint8_t data[16];
395 int ret;
397 memset(data, 0x00, 16);
399 reg_size = indio_dev->channels[0].scan_type.realbits +
400 indio_dev->channels[0].scan_type.shift;
401 reg_size = DIV_ROUND_UP(reg_size, 8);
403 switch (reg_size) {
404 case 4:
405 case 2:
406 case 1:
407 ret = ad_sd_read_reg_raw(sigma_delta, AD_SD_REG_DATA,
408 reg_size, &data[0]);
409 break;
410 case 3:
411 /* We store 24 bit samples in a 32 bit word. Keep the upper
412 * byte set to zero. */
413 ret = ad_sd_read_reg_raw(sigma_delta, AD_SD_REG_DATA,
414 reg_size, &data[1]);
415 break;
418 iio_push_to_buffers_with_timestamp(indio_dev, data, pf->timestamp);
420 iio_trigger_notify_done(indio_dev->trig);
421 sigma_delta->irq_dis = false;
422 enable_irq(sigma_delta->spi->irq);
424 return IRQ_HANDLED;
427 static const struct iio_buffer_setup_ops ad_sd_buffer_setup_ops = {
428 .postenable = &ad_sd_buffer_postenable,
429 .predisable = &iio_triggered_buffer_predisable,
430 .postdisable = &ad_sd_buffer_postdisable,
431 .validate_scan_mask = &iio_validate_scan_mask_onehot,
434 static irqreturn_t ad_sd_data_rdy_trig_poll(int irq, void *private)
436 struct ad_sigma_delta *sigma_delta = private;
438 complete(&sigma_delta->completion);
439 disable_irq_nosync(irq);
440 sigma_delta->irq_dis = true;
441 iio_trigger_poll(sigma_delta->trig);
443 return IRQ_HANDLED;
447 * ad_sd_validate_trigger() - validate_trigger callback for ad_sigma_delta devices
448 * @indio_dev: The IIO device
449 * @trig: The new trigger
451 * Returns: 0 if the 'trig' matches the trigger registered by the ad_sigma_delta
452 * device, -EINVAL otherwise.
454 int ad_sd_validate_trigger(struct iio_dev *indio_dev, struct iio_trigger *trig)
456 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
458 if (sigma_delta->trig != trig)
459 return -EINVAL;
461 return 0;
463 EXPORT_SYMBOL_GPL(ad_sd_validate_trigger);
465 static const struct iio_trigger_ops ad_sd_trigger_ops = {
466 .owner = THIS_MODULE,
469 static int ad_sd_probe_trigger(struct iio_dev *indio_dev)
471 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
472 int ret;
474 sigma_delta->trig = iio_trigger_alloc("%s-dev%d", indio_dev->name,
475 indio_dev->id);
476 if (sigma_delta->trig == NULL) {
477 ret = -ENOMEM;
478 goto error_ret;
480 sigma_delta->trig->ops = &ad_sd_trigger_ops;
481 init_completion(&sigma_delta->completion);
483 ret = request_irq(sigma_delta->spi->irq,
484 ad_sd_data_rdy_trig_poll,
485 IRQF_TRIGGER_LOW,
486 indio_dev->name,
487 sigma_delta);
488 if (ret)
489 goto error_free_trig;
491 if (!sigma_delta->irq_dis) {
492 sigma_delta->irq_dis = true;
493 disable_irq_nosync(sigma_delta->spi->irq);
495 sigma_delta->trig->dev.parent = &sigma_delta->spi->dev;
496 iio_trigger_set_drvdata(sigma_delta->trig, sigma_delta);
498 ret = iio_trigger_register(sigma_delta->trig);
499 if (ret)
500 goto error_free_irq;
502 /* select default trigger */
503 indio_dev->trig = iio_trigger_get(sigma_delta->trig);
505 return 0;
507 error_free_irq:
508 free_irq(sigma_delta->spi->irq, sigma_delta);
509 error_free_trig:
510 iio_trigger_free(sigma_delta->trig);
511 error_ret:
512 return ret;
515 static void ad_sd_remove_trigger(struct iio_dev *indio_dev)
517 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
519 iio_trigger_unregister(sigma_delta->trig);
520 free_irq(sigma_delta->spi->irq, sigma_delta);
521 iio_trigger_free(sigma_delta->trig);
525 * ad_sd_setup_buffer_and_trigger() -
526 * @indio_dev: The IIO device
528 int ad_sd_setup_buffer_and_trigger(struct iio_dev *indio_dev)
530 int ret;
532 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
533 &ad_sd_trigger_handler, &ad_sd_buffer_setup_ops);
534 if (ret)
535 return ret;
537 ret = ad_sd_probe_trigger(indio_dev);
538 if (ret) {
539 iio_triggered_buffer_cleanup(indio_dev);
540 return ret;
543 return 0;
545 EXPORT_SYMBOL_GPL(ad_sd_setup_buffer_and_trigger);
548 * ad_sd_cleanup_buffer_and_trigger() -
549 * @indio_dev: The IIO device
551 void ad_sd_cleanup_buffer_and_trigger(struct iio_dev *indio_dev)
553 ad_sd_remove_trigger(indio_dev);
554 iio_triggered_buffer_cleanup(indio_dev);
556 EXPORT_SYMBOL_GPL(ad_sd_cleanup_buffer_and_trigger);
559 * ad_sd_init() - Initializes a ad_sigma_delta struct
560 * @sigma_delta: The ad_sigma_delta device
561 * @indio_dev: The IIO device which the Sigma Delta device is used for
562 * @spi: The SPI device for the ad_sigma_delta device
563 * @info: Device specific callbacks and options
565 * This function needs to be called before any other operations are performed on
566 * the ad_sigma_delta struct.
568 int ad_sd_init(struct ad_sigma_delta *sigma_delta, struct iio_dev *indio_dev,
569 struct spi_device *spi, const struct ad_sigma_delta_info *info)
571 sigma_delta->spi = spi;
572 sigma_delta->info = info;
573 iio_device_set_drvdata(indio_dev, sigma_delta);
575 return 0;
577 EXPORT_SYMBOL_GPL(ad_sd_init);
579 MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
580 MODULE_DESCRIPTION("Analog Devices Sigma-Delta ADCs");
581 MODULE_LICENSE("GPL v2");