11 #include <linux/device.h>
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
16 #include <linux/module.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
59 unsigned int size,
unsigned int val)
70 data[0] = (reg << sigma_delta->
info->addr_shift) | sigma_delta->
comm;
79 put_unaligned_be16(val, &data[1]);
91 spi_message_add_tail(&t, &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);
147 unsigned int reg,
unsigned int size,
unsigned int *val)
151 ret = ad_sd_read_reg_raw(sigma_delta, reg, size, sigma_delta->data);
160 *val = (sigma_delta->data[0] << 16) |
161 (sigma_delta->data[1] << 8) |
162 sigma_delta->data[2];
165 *val = get_unaligned_be16(sigma_delta->data);
168 *val = sigma_delta->data[0];
185 ret = ad_sigma_delta_set_channel(sigma_delta, channel);
193 ret = ad_sigma_delta_set_mode(sigma_delta, mode);
229 for (i = 0; i <
n; i++) {
230 ret = ad_sd_calibrate(sigma_delta, cb[i].mode, cb[i].channel);
250 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
251 unsigned int sample, raw_sample;
254 if (iio_buffer_enabled(indio_dev))
258 ad_sigma_delta_set_channel(sigma_delta, chan->
address);
295 sample = raw_sample >> chan->
scan_type.shift;
296 sample &= (1 << chan->
scan_type.realbits) - 1;
299 ret = ad_sigma_delta_postprocess_sample(sigma_delta, raw_sample);
307 static int ad_sd_buffer_postenable(
struct iio_dev *indio_dev)
309 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
319 ret = ad_sigma_delta_set_channel(sigma_delta,
320 indio_dev->
channels[channel].address);
342 static int ad_sd_buffer_postdisable(
struct iio_dev *indio_dev)
344 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
360 static irqreturn_t ad_sd_trigger_handler(
int irq,
void *
p)
364 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
365 unsigned int reg_size;
375 reg_size = indio_dev->
channels[0].scan_type.realbits +
376 indio_dev->
channels[0].scan_type.shift;
405 .postenable = &ad_sd_buffer_postenable,
407 .postdisable = &ad_sd_buffer_postdisable,
411 static irqreturn_t ad_sd_data_rdy_trig_poll(
int irq,
void *
private)
433 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
435 if (sigma_delta->
trig != trig)
446 static int ad_sd_probe_trigger(
struct iio_dev *indio_dev)
448 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
457 sigma_delta->
trig->ops = &ad_sd_trigger_ops;
461 ad_sd_data_rdy_trig_poll,
466 goto error_free_trig;
472 sigma_delta->
trig->dev.parent = &sigma_delta->
spi->dev;
473 sigma_delta->
trig->private_data = sigma_delta;
480 indio_dev->
trig = sigma_delta->
trig;
492 static void ad_sd_remove_trigger(
struct iio_dev *indio_dev)
494 struct ad_sigma_delta *sigma_delta = iio_device_get_drvdata(indio_dev);
510 &ad_sd_trigger_handler, &ad_sd_buffer_setup_ops);
514 ret = ad_sd_probe_trigger(indio_dev);
530 ad_sd_remove_trigger(indio_dev);
550 iio_device_set_drvdata(indio_dev, sigma_delta);