3 * Maxim max11100 ADC Driver with IIO interface
5 * Copyright (C) 2016-17 Renesas Electronics Corporation
6 * Copyright (C) 2016-17 Jacopo Mondi
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
12 #include <linux/delay.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/regulator/consumer.h>
16 #include <linux/spi/spi.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/driver.h>
22 * LSB is the ADC single digital step
23 * 1 LSB = (vref_mv / 2 ^ 16)
25 * LSB is used to calculate analog voltage value
26 * from the number of ADC steps count
30 #define MAX11100_LSB_DIV (1 << 16)
32 struct max11100_state
{
33 struct regulator
*vref_reg
;
34 struct spi_device
*spi
;
37 * DMA (thus cache coherency maintenance) requires the
38 * transfer buffers to live in their own cache lines.
40 u8 buffer
[3] ____cacheline_aligned
;
43 static struct iio_chan_spec max11100_channels
[] = {
46 .info_mask_separate
= BIT(IIO_CHAN_INFO_RAW
) |
47 BIT(IIO_CHAN_INFO_SCALE
),
51 static int max11100_read_single(struct iio_dev
*indio_dev
, int *val
)
54 struct max11100_state
*state
= iio_priv(indio_dev
);
56 ret
= spi_read(state
->spi
, state
->buffer
, sizeof(state
->buffer
));
58 dev_err(&indio_dev
->dev
, "SPI transfer failed\n");
62 /* the first 8 bits sent out from ADC must be 0s */
63 if (state
->buffer
[0]) {
64 dev_err(&indio_dev
->dev
, "Invalid value: buffer[0] != 0\n");
68 *val
= (state
->buffer
[1] << 8) | state
->buffer
[2];
73 static int max11100_read_raw(struct iio_dev
*indio_dev
,
74 struct iio_chan_spec
const *chan
,
75 int *val
, int *val2
, long info
)
78 struct max11100_state
*state
= iio_priv(indio_dev
);
81 case IIO_CHAN_INFO_RAW
:
82 ret
= max11100_read_single(indio_dev
, val
);
88 case IIO_CHAN_INFO_SCALE
:
89 vref_uv
= regulator_get_voltage(state
->vref_reg
);
91 /* dummy regulator "get_voltage" returns -EINVAL */
94 *val
= vref_uv
/ 1000;
95 *val2
= MAX11100_LSB_DIV
;
96 return IIO_VAL_FRACTIONAL
;
102 static const struct iio_info max11100_info
= {
103 .driver_module
= THIS_MODULE
,
104 .read_raw
= max11100_read_raw
,
107 static int max11100_probe(struct spi_device
*spi
)
110 struct iio_dev
*indio_dev
;
111 struct max11100_state
*state
;
113 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*state
));
117 spi_set_drvdata(spi
, indio_dev
);
119 state
= iio_priv(indio_dev
);
122 indio_dev
->dev
.parent
= &spi
->dev
;
123 indio_dev
->dev
.of_node
= spi
->dev
.of_node
;
124 indio_dev
->name
= "max11100";
125 indio_dev
->info
= &max11100_info
;
126 indio_dev
->modes
= INDIO_DIRECT_MODE
;
127 indio_dev
->channels
= max11100_channels
,
128 indio_dev
->num_channels
= ARRAY_SIZE(max11100_channels
),
130 state
->vref_reg
= devm_regulator_get(&spi
->dev
, "vref");
131 if (IS_ERR(state
->vref_reg
))
132 return PTR_ERR(state
->vref_reg
);
134 ret
= regulator_enable(state
->vref_reg
);
138 ret
= iio_device_register(indio_dev
);
140 goto disable_regulator
;
145 regulator_disable(state
->vref_reg
);
150 static int max11100_remove(struct spi_device
*spi
)
152 struct iio_dev
*indio_dev
= spi_get_drvdata(spi
);
153 struct max11100_state
*state
= iio_priv(indio_dev
);
155 iio_device_unregister(indio_dev
);
156 regulator_disable(state
->vref_reg
);
161 static const struct of_device_id max11100_ids
[] = {
162 {.compatible
= "maxim,max11100"},
165 MODULE_DEVICE_TABLE(of
, max11100_ids
);
167 static struct spi_driver max11100_driver
= {
170 .owner
= THIS_MODULE
,
171 .of_match_table
= of_match_ptr(max11100_ids
),
173 .probe
= max11100_probe
,
174 .remove
= max11100_remove
,
177 module_spi_driver(max11100_driver
);
179 MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
180 MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
181 MODULE_LICENSE("GPL v2");