1 // SPDX-License-Identifier: GPL-2.0
3 * Driver for Microchip MCP3911, Two-channel Analog Front End
5 * Copyright (C) 2018 Marcus Folkesson <marcus.folkesson@gmail.com>
6 * Copyright (C) 2018 Kent Gustavsson <kent@minoris.se>
8 #include <linux/bitfield.h>
9 #include <linux/bits.h>
10 #include <linux/cleanup.h>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/err.h>
14 #include <linux/module.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/property.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/spi/spi.h>
20 #include <linux/iio/iio.h>
21 #include <linux/iio/buffer.h>
22 #include <linux/iio/triggered_buffer.h>
23 #include <linux/iio/trigger_consumer.h>
24 #include <linux/iio/trigger.h>
26 #include <linux/unaligned.h>
28 #define MCP3911_REG_CHANNEL0 0x00
29 #define MCP3911_REG_CHANNEL1 0x03
30 #define MCP3911_REG_MOD 0x06
31 #define MCP3911_REG_PHASE 0x07
32 #define MCP3911_REG_GAIN 0x09
33 #define MCP3911_GAIN_MASK(ch) (GENMASK(2, 0) << 3 * (ch))
34 #define MCP3911_GAIN_VAL(ch, val) ((val << 3 * (ch)) & MCP3911_GAIN_MASK(ch))
36 #define MCP3911_REG_STATUSCOM 0x0a
37 #define MCP3911_STATUSCOM_DRHIZ BIT(12)
38 #define MCP3911_STATUSCOM_READ GENMASK(7, 6)
39 #define MCP3911_STATUSCOM_CH1_24WIDTH BIT(4)
40 #define MCP3911_STATUSCOM_CH0_24WIDTH BIT(3)
41 #define MCP3911_STATUSCOM_EN_OFFCAL BIT(2)
42 #define MCP3911_STATUSCOM_EN_GAINCAL BIT(1)
44 #define MCP3911_REG_CONFIG 0x0c
45 #define MCP3911_CONFIG_CLKEXT BIT(1)
46 #define MCP3911_CONFIG_VREFEXT BIT(2)
47 #define MCP3911_CONFIG_OSR GENMASK(13, 11)
49 #define MCP3911_REG_OFFCAL_CH0 0x0e
50 #define MCP3911_REG_GAINCAL_CH0 0x11
51 #define MCP3911_REG_OFFCAL_CH1 0x14
52 #define MCP3911_REG_GAINCAL_CH1 0x17
53 #define MCP3911_REG_VREFCAL 0x1a
55 #define MCP3911_CHANNEL(ch) (MCP3911_REG_CHANNEL0 + (ch) * 3)
56 #define MCP3911_OFFCAL(ch) (MCP3911_REG_OFFCAL_CH0 + (ch) * 6)
58 /* Internal voltage reference in mV */
59 #define MCP3911_INT_VREF_MV 1200
61 #define MCP3911_REG_READ(reg, id) ((((reg) << 1) | ((id) << 6) | (1 << 0)) & 0xff)
62 #define MCP3911_REG_WRITE(reg, id) ((((reg) << 1) | ((id) << 6) | (0 << 0)) & 0xff)
63 #define MCP3911_REG_MASK GENMASK(4, 1)
65 #define MCP3911_NUM_SCALES 6
67 /* Registers compatible with MCP3910 */
68 #define MCP3910_REG_STATUSCOM 0x0c
69 #define MCP3910_STATUSCOM_READ GENMASK(23, 22)
70 #define MCP3910_STATUSCOM_DRHIZ BIT(20)
72 #define MCP3910_REG_GAIN 0x0b
74 #define MCP3910_REG_CONFIG0 0x0d
75 #define MCP3910_CONFIG0_EN_OFFCAL BIT(23)
76 #define MCP3910_CONFIG0_OSR GENMASK(15, 13)
78 #define MCP3910_REG_CONFIG1 0x0e
79 #define MCP3910_CONFIG1_CLKEXT BIT(6)
80 #define MCP3910_CONFIG1_VREFEXT BIT(7)
82 #define MCP3910_REG_OFFCAL_CH0 0x0f
83 #define MCP3910_OFFCAL(ch) (MCP3910_REG_OFFCAL_CH0 + (ch) * 6)
85 /* Maximal number of channels used by the MCP39XX family */
86 #define MCP39XX_MAX_NUM_CHANNELS 8
88 static const int mcp3911_osr_table
[] = { 32, 64, 128, 256, 512, 1024, 2048, 4096 };
89 static u32 mcp3911_scale_table
[MCP3911_NUM_SCALES
][2];
102 struct mcp3911_chip_info
{
103 const struct iio_chan_spec
*channels
;
104 unsigned int num_channels
;
106 int (*config
)(struct mcp3911
*adc
, bool external_vref
);
107 int (*get_osr
)(struct mcp3911
*adc
, u32
*val
);
108 int (*set_osr
)(struct mcp3911
*adc
, u32 val
);
109 int (*enable_offset
)(struct mcp3911
*adc
, bool enable
);
110 int (*get_offset
)(struct mcp3911
*adc
, int channel
, int *val
);
111 int (*set_offset
)(struct mcp3911
*adc
, int channel
, int val
);
112 int (*set_scale
)(struct mcp3911
*adc
, int channel
, u32 val
);
116 struct spi_device
*spi
;
120 struct iio_trigger
*trig
;
121 u32 gain
[MCP39XX_MAX_NUM_CHANNELS
];
122 const struct mcp3911_chip_info
*chip
;
124 u32 channels
[MCP39XX_MAX_NUM_CHANNELS
];
128 u8 tx_buf
__aligned(IIO_DMA_MINALIGN
);
129 u8 rx_buf
[MCP39XX_MAX_NUM_CHANNELS
* 3];
132 static int mcp3911_read(struct mcp3911
*adc
, u8 reg
, u32
*val
, u8 len
)
136 reg
= MCP3911_REG_READ(reg
, adc
->dev_addr
);
137 ret
= spi_write_then_read(adc
->spi
, ®
, 1, val
, len
);
142 *val
>>= ((4 - len
) * 8);
143 dev_dbg(&adc
->spi
->dev
, "reading 0x%x from register 0x%lx\n", *val
,
144 FIELD_GET(MCP3911_REG_MASK
, reg
));
148 static int mcp3911_write(struct mcp3911
*adc
, u8 reg
, u32 val
, u8 len
)
150 dev_dbg(&adc
->spi
->dev
, "writing 0x%x to register 0x%x\n", val
, reg
);
152 val
<<= (3 - len
) * 8;
154 val
|= MCP3911_REG_WRITE(reg
, adc
->dev_addr
);
156 return spi_write(adc
->spi
, &val
, len
+ 1);
159 static int mcp3911_update(struct mcp3911
*adc
, u8 reg
, u32 mask
, u32 val
, u8 len
)
164 ret
= mcp3911_read(adc
, reg
, &tmp
, len
);
170 return mcp3911_write(adc
, reg
, val
, len
);
173 static int mcp3910_enable_offset(struct mcp3911
*adc
, bool enable
)
175 unsigned int mask
= MCP3910_CONFIG0_EN_OFFCAL
;
176 unsigned int value
= enable
? mask
: 0;
178 return mcp3911_update(adc
, MCP3910_REG_CONFIG0
, mask
, value
, 3);
181 static int mcp3910_get_offset(struct mcp3911
*adc
, int channel
, int *val
)
183 return mcp3911_read(adc
, MCP3910_OFFCAL(channel
), val
, 3);
186 static int mcp3910_set_offset(struct mcp3911
*adc
, int channel
, int val
)
190 ret
= mcp3911_write(adc
, MCP3910_OFFCAL(channel
), val
, 3);
194 return adc
->chip
->enable_offset(adc
, 1);
197 static int mcp3911_enable_offset(struct mcp3911
*adc
, bool enable
)
199 unsigned int mask
= MCP3911_STATUSCOM_EN_OFFCAL
;
200 unsigned int value
= enable
? mask
: 0;
202 return mcp3911_update(adc
, MCP3911_REG_STATUSCOM
, mask
, value
, 2);
205 static int mcp3911_get_offset(struct mcp3911
*adc
, int channel
, int *val
)
207 return mcp3911_read(adc
, MCP3911_OFFCAL(channel
), val
, 3);
210 static int mcp3911_set_offset(struct mcp3911
*adc
, int channel
, int val
)
214 ret
= mcp3911_write(adc
, MCP3911_OFFCAL(channel
), val
, 3);
218 return adc
->chip
->enable_offset(adc
, 1);
221 static int mcp3910_get_osr(struct mcp3911
*adc
, u32
*val
)
226 ret
= mcp3911_read(adc
, MCP3910_REG_CONFIG0
, val
, 3);
230 osr
= FIELD_GET(MCP3910_CONFIG0_OSR
, *val
);
235 static int mcp3910_set_osr(struct mcp3911
*adc
, u32 val
)
237 unsigned int osr
= FIELD_PREP(MCP3910_CONFIG0_OSR
, val
);
238 unsigned int mask
= MCP3910_CONFIG0_OSR
;
240 return mcp3911_update(adc
, MCP3910_REG_CONFIG0
, mask
, osr
, 3);
243 static int mcp3911_set_osr(struct mcp3911
*adc
, u32 val
)
245 unsigned int osr
= FIELD_PREP(MCP3911_CONFIG_OSR
, val
);
246 unsigned int mask
= MCP3911_CONFIG_OSR
;
248 return mcp3911_update(adc
, MCP3911_REG_CONFIG
, mask
, osr
, 2);
251 static int mcp3911_get_osr(struct mcp3911
*adc
, u32
*val
)
256 ret
= mcp3911_read(adc
, MCP3911_REG_CONFIG
, val
, 2);
260 osr
= FIELD_GET(MCP3911_CONFIG_OSR
, *val
);
265 static int mcp3910_set_scale(struct mcp3911
*adc
, int channel
, u32 val
)
267 return mcp3911_update(adc
, MCP3910_REG_GAIN
,
268 MCP3911_GAIN_MASK(channel
),
269 MCP3911_GAIN_VAL(channel
, val
), 3);
272 static int mcp3911_set_scale(struct mcp3911
*adc
, int channel
, u32 val
)
274 return mcp3911_update(adc
, MCP3911_REG_GAIN
,
275 MCP3911_GAIN_MASK(channel
),
276 MCP3911_GAIN_VAL(channel
, val
), 1);
279 static int mcp3911_write_raw_get_fmt(struct iio_dev
*indio_dev
,
280 struct iio_chan_spec
const *chan
,
284 case IIO_CHAN_INFO_SCALE
:
285 return IIO_VAL_INT_PLUS_NANO
;
286 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
289 return IIO_VAL_INT_PLUS_NANO
;
293 static int mcp3911_read_avail(struct iio_dev
*indio_dev
,
294 struct iio_chan_spec
const *chan
,
295 const int **vals
, int *type
, int *length
,
299 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
301 *vals
= mcp3911_osr_table
;
302 *length
= ARRAY_SIZE(mcp3911_osr_table
);
303 return IIO_AVAIL_LIST
;
304 case IIO_CHAN_INFO_SCALE
:
305 *type
= IIO_VAL_INT_PLUS_NANO
;
306 *vals
= (int *)mcp3911_scale_table
;
307 *length
= ARRAY_SIZE(mcp3911_scale_table
) * 2;
308 return IIO_AVAIL_LIST
;
314 static int mcp3911_read_raw(struct iio_dev
*indio_dev
,
315 struct iio_chan_spec
const *channel
, int *val
,
316 int *val2
, long mask
)
318 struct mcp3911
*adc
= iio_priv(indio_dev
);
321 guard(mutex
)(&adc
->lock
);
323 case IIO_CHAN_INFO_RAW
:
324 ret
= mcp3911_read(adc
,
325 MCP3911_CHANNEL(channel
->channel
), val
, 3);
329 *val
= sign_extend32(*val
, 23);
331 case IIO_CHAN_INFO_OFFSET
:
332 ret
= adc
->chip
->get_offset(adc
, channel
->channel
, val
);
337 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
338 ret
= adc
->chip
->get_osr(adc
, val
);
343 case IIO_CHAN_INFO_SCALE
:
344 *val
= mcp3911_scale_table
[ilog2(adc
->gain
[channel
->channel
])][0];
345 *val2
= mcp3911_scale_table
[ilog2(adc
->gain
[channel
->channel
])][1];
346 return IIO_VAL_INT_PLUS_NANO
;
352 static int mcp3911_write_raw(struct iio_dev
*indio_dev
,
353 struct iio_chan_spec
const *channel
, int val
,
356 struct mcp3911
*adc
= iio_priv(indio_dev
);
358 guard(mutex
)(&adc
->lock
);
360 case IIO_CHAN_INFO_SCALE
:
361 for (int i
= 0; i
< MCP3911_NUM_SCALES
; i
++) {
362 if (val
== mcp3911_scale_table
[i
][0] &&
363 val2
== mcp3911_scale_table
[i
][1]) {
365 adc
->gain
[channel
->channel
] = BIT(i
);
366 return adc
->chip
->set_scale(adc
, channel
->channel
, i
);
370 case IIO_CHAN_INFO_OFFSET
:
374 return adc
->chip
->set_offset(adc
, channel
->channel
, val
);
375 case IIO_CHAN_INFO_OVERSAMPLING_RATIO
:
376 for (int i
= 0; i
< ARRAY_SIZE(mcp3911_osr_table
); i
++) {
377 if (val
== mcp3911_osr_table
[i
]) {
378 return adc
->chip
->set_osr(adc
, i
);
387 static int mcp3911_calc_scale_table(u32 vref_mv
)
393 * For 24-bit Conversion
394 * Raw = ((Voltage)/(Vref) * 2^23 * Gain * 1.5
395 * Voltage = Raw * (Vref)/(2^23 * Gain * 1.5)
397 * ref = Reference voltage
398 * div = (2^23 * 1.5 * gain) = 12582912 * gain
400 for (int i
= 0; i
< MCP3911_NUM_SCALES
; i
++) {
401 div
= 12582912 * BIT(i
);
402 tmp
= div_s64((s64
)vref_mv
* 1000000000LL, div
);
404 mcp3911_scale_table
[i
][0] = 0;
405 mcp3911_scale_table
[i
][1] = tmp
;
411 #define MCP3911_CHAN(idx) { \
412 .type = IIO_VOLTAGE, \
416 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
417 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
418 BIT(IIO_CHAN_INFO_OFFSET) | \
419 BIT(IIO_CHAN_INFO_SCALE), \
420 .info_mask_shared_by_type_available = \
421 BIT(IIO_CHAN_INFO_OVERSAMPLING_RATIO), \
422 .info_mask_separate_available = \
423 BIT(IIO_CHAN_INFO_SCALE), \
428 .endianness = IIO_BE, \
432 static const struct iio_chan_spec mcp3910_channels
[] = {
435 IIO_CHAN_SOFT_TIMESTAMP(2),
438 static const struct iio_chan_spec mcp3911_channels
[] = {
441 IIO_CHAN_SOFT_TIMESTAMP(2),
444 static const struct iio_chan_spec mcp3912_channels
[] = {
449 IIO_CHAN_SOFT_TIMESTAMP(4),
452 static const struct iio_chan_spec mcp3913_channels
[] = {
459 IIO_CHAN_SOFT_TIMESTAMP(6),
462 static const struct iio_chan_spec mcp3914_channels
[] = {
471 IIO_CHAN_SOFT_TIMESTAMP(8),
474 static const struct iio_chan_spec mcp3918_channels
[] = {
476 IIO_CHAN_SOFT_TIMESTAMP(1),
479 static const struct iio_chan_spec mcp3919_channels
[] = {
483 IIO_CHAN_SOFT_TIMESTAMP(3),
486 static irqreturn_t
mcp3911_trigger_handler(int irq
, void *p
)
488 struct iio_poll_func
*pf
= p
;
489 struct iio_dev
*indio_dev
= pf
->indio_dev
;
490 struct mcp3911
*adc
= iio_priv(indio_dev
);
491 struct device
*dev
= &adc
->spi
->dev
;
492 struct spi_transfer xfer
[] = {
494 .tx_buf
= &adc
->tx_buf
,
497 .rx_buf
= adc
->rx_buf
,
498 .len
= (adc
->chip
->num_channels
- 1) * 3,
505 guard(mutex
)(&adc
->lock
);
506 adc
->tx_buf
= MCP3911_REG_READ(MCP3911_CHANNEL(0), adc
->dev_addr
);
507 ret
= spi_sync_transfer(adc
->spi
, xfer
, ARRAY_SIZE(xfer
));
509 dev_warn(dev
, "failed to get conversion data\n");
513 iio_for_each_active_channel(indio_dev
, scan_index
) {
514 const struct iio_chan_spec
*scan_chan
= &indio_dev
->channels
[scan_index
];
516 adc
->scan
.channels
[i
] = get_unaligned_be24(&adc
->rx_buf
[scan_chan
->channel
* 3]);
519 iio_push_to_buffers_with_timestamp(indio_dev
, &adc
->scan
,
520 iio_get_time_ns(indio_dev
));
522 iio_trigger_notify_done(indio_dev
->trig
);
527 static const struct iio_info mcp3911_info
= {
528 .read_raw
= mcp3911_read_raw
,
529 .write_raw
= mcp3911_write_raw
,
530 .read_avail
= mcp3911_read_avail
,
531 .write_raw_get_fmt
= mcp3911_write_raw_get_fmt
,
534 static int mcp3911_config(struct mcp3911
*adc
, bool external_vref
)
536 struct device
*dev
= &adc
->spi
->dev
;
540 ret
= mcp3911_read(adc
, MCP3911_REG_CONFIG
, ®val
, 2);
544 regval
&= ~MCP3911_CONFIG_VREFEXT
;
546 dev_dbg(dev
, "use external voltage reference\n");
547 regval
|= FIELD_PREP(MCP3911_CONFIG_VREFEXT
, 1);
549 dev_dbg(dev
, "use internal voltage reference (1.2V)\n");
550 regval
|= FIELD_PREP(MCP3911_CONFIG_VREFEXT
, 0);
553 regval
&= ~MCP3911_CONFIG_CLKEXT
;
555 dev_dbg(dev
, "use external clock as clocksource\n");
556 regval
|= FIELD_PREP(MCP3911_CONFIG_CLKEXT
, 1);
558 dev_dbg(dev
, "use crystal oscillator as clocksource\n");
559 regval
|= FIELD_PREP(MCP3911_CONFIG_CLKEXT
, 0);
562 ret
= mcp3911_write(adc
, MCP3911_REG_CONFIG
, regval
, 2);
566 ret
= mcp3911_read(adc
, MCP3911_REG_STATUSCOM
, ®val
, 2);
570 /* Address counter incremented, cycle through register types */
571 regval
&= ~MCP3911_STATUSCOM_READ
;
572 regval
|= FIELD_PREP(MCP3911_STATUSCOM_READ
, 0x02);
574 regval
&= ~MCP3911_STATUSCOM_DRHIZ
;
575 if (device_property_read_bool(dev
, "microchip,data-ready-hiz"))
576 regval
|= FIELD_PREP(MCP3911_STATUSCOM_DRHIZ
, 0);
578 regval
|= FIELD_PREP(MCP3911_STATUSCOM_DRHIZ
, 1);
580 /* Disable offset to ignore any old values in offset register */
581 regval
&= ~MCP3911_STATUSCOM_EN_OFFCAL
;
583 ret
= mcp3911_write(adc
, MCP3911_REG_STATUSCOM
, regval
, 2);
587 /* Set gain to 1 for all channels */
588 ret
= mcp3911_read(adc
, MCP3911_REG_GAIN
, ®val
, 1);
592 for (int i
= 0; i
< adc
->chip
->num_channels
- 1; i
++) {
594 regval
&= ~MCP3911_GAIN_MASK(i
);
597 return mcp3911_write(adc
, MCP3911_REG_GAIN
, regval
, 1);
600 static int mcp3910_config(struct mcp3911
*adc
, bool external_vref
)
602 struct device
*dev
= &adc
->spi
->dev
;
606 ret
= mcp3911_read(adc
, MCP3910_REG_CONFIG1
, ®val
, 3);
610 regval
&= ~MCP3910_CONFIG1_VREFEXT
;
612 dev_dbg(dev
, "use external voltage reference\n");
613 regval
|= FIELD_PREP(MCP3910_CONFIG1_VREFEXT
, 1);
615 dev_dbg(dev
, "use internal voltage reference (1.2V)\n");
616 regval
|= FIELD_PREP(MCP3910_CONFIG1_VREFEXT
, 0);
619 regval
&= ~MCP3910_CONFIG1_CLKEXT
;
621 dev_dbg(dev
, "use external clock as clocksource\n");
622 regval
|= FIELD_PREP(MCP3910_CONFIG1_CLKEXT
, 1);
624 dev_dbg(dev
, "use crystal oscillator as clocksource\n");
625 regval
|= FIELD_PREP(MCP3910_CONFIG1_CLKEXT
, 0);
628 ret
= mcp3911_write(adc
, MCP3910_REG_CONFIG1
, regval
, 3);
632 ret
= mcp3911_read(adc
, MCP3910_REG_STATUSCOM
, ®val
, 3);
636 /* Address counter incremented, cycle through register types */
637 regval
&= ~MCP3910_STATUSCOM_READ
;
638 regval
|= FIELD_PREP(MCP3910_STATUSCOM_READ
, 0x02);
640 regval
&= ~MCP3910_STATUSCOM_DRHIZ
;
641 if (device_property_read_bool(dev
, "microchip,data-ready-hiz"))
642 regval
|= FIELD_PREP(MCP3910_STATUSCOM_DRHIZ
, 0);
644 regval
|= FIELD_PREP(MCP3910_STATUSCOM_DRHIZ
, 1);
646 ret
= mcp3911_write(adc
, MCP3910_REG_STATUSCOM
, regval
, 3);
650 /* Set gain to 1 for all channels */
651 ret
= mcp3911_read(adc
, MCP3910_REG_GAIN
, ®val
, 3);
655 for (int i
= 0; i
< adc
->chip
->num_channels
- 1; i
++) {
657 regval
&= ~MCP3911_GAIN_MASK(i
);
659 ret
= mcp3911_write(adc
, MCP3910_REG_GAIN
, regval
, 3);
663 /* Disable offset to ignore any old values in offset register */
664 return adc
->chip
->enable_offset(adc
, 0);
667 static int mcp3911_set_trigger_state(struct iio_trigger
*trig
, bool enable
)
669 struct mcp3911
*adc
= iio_trigger_get_drvdata(trig
);
672 enable_irq(adc
->spi
->irq
);
674 disable_irq(adc
->spi
->irq
);
679 static const struct iio_trigger_ops mcp3911_trigger_ops
= {
680 .validate_device
= iio_trigger_validate_own_device
,
681 .set_trigger_state
= mcp3911_set_trigger_state
,
684 static int mcp3911_probe(struct spi_device
*spi
)
686 struct device
*dev
= &spi
->dev
;
687 struct iio_dev
*indio_dev
;
693 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*adc
));
697 adc
= iio_priv(indio_dev
);
699 adc
->chip
= spi_get_device_match_data(spi
);
701 ret
= devm_regulator_get_enable_read_voltage(dev
, "vref");
702 if (ret
< 0 && ret
!= -ENODEV
)
703 return dev_err_probe(dev
, ret
, "failed to get vref voltage\n");
705 external_vref
= ret
!= -ENODEV
;
706 vref_mv
= external_vref
? ret
/ 1000 : MCP3911_INT_VREF_MV
;
708 adc
->clki
= devm_clk_get_enabled(dev
, NULL
);
709 if (IS_ERR(adc
->clki
)) {
710 if (PTR_ERR(adc
->clki
) == -ENOENT
) {
713 return dev_err_probe(dev
, PTR_ERR(adc
->clki
), "failed to get adc clk\n");
718 * Fallback to "device-addr" due to historical mismatch between
719 * dt-bindings and implementation.
721 ret
= device_property_read_u32(dev
, "microchip,device-addr", &adc
->dev_addr
);
723 device_property_read_u32(dev
, "device-addr", &adc
->dev_addr
);
724 if (adc
->dev_addr
> 3) {
725 return dev_err_probe(dev
, -EINVAL
,
726 "invalid device address (%i). Must be in range 0-3.\n",
729 dev_dbg(dev
, "use device address %i\n", adc
->dev_addr
);
731 ret
= adc
->chip
->config(adc
, external_vref
);
735 ret
= mcp3911_calc_scale_table(vref_mv
);
739 /* Set gain to 1 for all channels */
740 for (int i
= 0; i
< adc
->chip
->num_channels
- 1; i
++) {
742 ret
= mcp3911_update(adc
, MCP3911_REG_GAIN
,
743 MCP3911_GAIN_MASK(i
),
744 MCP3911_GAIN_VAL(i
, 0), 1);
749 indio_dev
->name
= spi_get_device_id(spi
)->name
;
750 indio_dev
->modes
= INDIO_DIRECT_MODE
;
751 indio_dev
->info
= &mcp3911_info
;
752 spi_set_drvdata(spi
, indio_dev
);
754 indio_dev
->channels
= adc
->chip
->channels
;
755 indio_dev
->num_channels
= adc
->chip
->num_channels
;
757 mutex_init(&adc
->lock
);
760 adc
->trig
= devm_iio_trigger_alloc(dev
, "%s-dev%d", indio_dev
->name
,
761 iio_device_id(indio_dev
));
765 adc
->trig
->ops
= &mcp3911_trigger_ops
;
766 iio_trigger_set_drvdata(adc
->trig
, adc
);
767 ret
= devm_iio_trigger_register(dev
, adc
->trig
);
772 * The device generates interrupts as long as it is powered up.
773 * Some platforms might not allow the option to power it down so
774 * don't enable the interrupt to avoid extra load on the system.
776 ret
= devm_request_irq(dev
, spi
->irq
, &iio_trigger_generic_data_rdy_poll
,
777 IRQF_NO_AUTOEN
| IRQF_ONESHOT
,
778 indio_dev
->name
, adc
->trig
);
783 ret
= devm_iio_triggered_buffer_setup(dev
, indio_dev
, NULL
,
784 mcp3911_trigger_handler
, NULL
);
788 return devm_iio_device_register(dev
, indio_dev
);
791 static const struct mcp3911_chip_info mcp3911_chip_info
[] = {
793 .channels
= mcp3910_channels
,
794 .num_channels
= ARRAY_SIZE(mcp3910_channels
),
795 .config
= mcp3910_config
,
796 .get_osr
= mcp3910_get_osr
,
797 .set_osr
= mcp3910_set_osr
,
798 .enable_offset
= mcp3910_enable_offset
,
799 .get_offset
= mcp3910_get_offset
,
800 .set_offset
= mcp3910_set_offset
,
801 .set_scale
= mcp3910_set_scale
,
804 .channels
= mcp3911_channels
,
805 .num_channels
= ARRAY_SIZE(mcp3911_channels
),
806 .config
= mcp3911_config
,
807 .get_osr
= mcp3911_get_osr
,
808 .set_osr
= mcp3911_set_osr
,
809 .enable_offset
= mcp3911_enable_offset
,
810 .get_offset
= mcp3911_get_offset
,
811 .set_offset
= mcp3911_set_offset
,
812 .set_scale
= mcp3911_set_scale
,
815 .channels
= mcp3912_channels
,
816 .num_channels
= ARRAY_SIZE(mcp3912_channels
),
817 .config
= mcp3910_config
,
818 .get_osr
= mcp3910_get_osr
,
819 .set_osr
= mcp3910_set_osr
,
820 .enable_offset
= mcp3910_enable_offset
,
821 .get_offset
= mcp3910_get_offset
,
822 .set_offset
= mcp3910_set_offset
,
823 .set_scale
= mcp3910_set_scale
,
826 .channels
= mcp3913_channels
,
827 .num_channels
= ARRAY_SIZE(mcp3913_channels
),
828 .config
= mcp3910_config
,
829 .get_osr
= mcp3910_get_osr
,
830 .set_osr
= mcp3910_set_osr
,
831 .enable_offset
= mcp3910_enable_offset
,
832 .get_offset
= mcp3910_get_offset
,
833 .set_offset
= mcp3910_set_offset
,
834 .set_scale
= mcp3910_set_scale
,
837 .channels
= mcp3914_channels
,
838 .num_channels
= ARRAY_SIZE(mcp3914_channels
),
839 .config
= mcp3910_config
,
840 .get_osr
= mcp3910_get_osr
,
841 .set_osr
= mcp3910_set_osr
,
842 .enable_offset
= mcp3910_enable_offset
,
843 .get_offset
= mcp3910_get_offset
,
844 .set_offset
= mcp3910_set_offset
,
845 .set_scale
= mcp3910_set_scale
,
848 .channels
= mcp3918_channels
,
849 .num_channels
= ARRAY_SIZE(mcp3918_channels
),
850 .config
= mcp3910_config
,
851 .get_osr
= mcp3910_get_osr
,
852 .set_osr
= mcp3910_set_osr
,
853 .enable_offset
= mcp3910_enable_offset
,
854 .get_offset
= mcp3910_get_offset
,
855 .set_offset
= mcp3910_set_offset
,
856 .set_scale
= mcp3910_set_scale
,
859 .channels
= mcp3919_channels
,
860 .num_channels
= ARRAY_SIZE(mcp3919_channels
),
861 .config
= mcp3910_config
,
862 .get_osr
= mcp3910_get_osr
,
863 .set_osr
= mcp3910_set_osr
,
864 .enable_offset
= mcp3910_enable_offset
,
865 .get_offset
= mcp3910_get_offset
,
866 .set_offset
= mcp3910_set_offset
,
867 .set_scale
= mcp3910_set_scale
,
870 static const struct of_device_id mcp3911_dt_ids
[] = {
871 { .compatible
= "microchip,mcp3910", .data
= &mcp3911_chip_info
[MCP3910
] },
872 { .compatible
= "microchip,mcp3911", .data
= &mcp3911_chip_info
[MCP3911
] },
873 { .compatible
= "microchip,mcp3912", .data
= &mcp3911_chip_info
[MCP3912
] },
874 { .compatible
= "microchip,mcp3913", .data
= &mcp3911_chip_info
[MCP3913
] },
875 { .compatible
= "microchip,mcp3914", .data
= &mcp3911_chip_info
[MCP3914
] },
876 { .compatible
= "microchip,mcp3918", .data
= &mcp3911_chip_info
[MCP3918
] },
877 { .compatible
= "microchip,mcp3919", .data
= &mcp3911_chip_info
[MCP3919
] },
880 MODULE_DEVICE_TABLE(of
, mcp3911_dt_ids
);
882 static const struct spi_device_id mcp3911_id
[] = {
883 { "mcp3910", (kernel_ulong_t
)&mcp3911_chip_info
[MCP3910
] },
884 { "mcp3911", (kernel_ulong_t
)&mcp3911_chip_info
[MCP3911
] },
885 { "mcp3912", (kernel_ulong_t
)&mcp3911_chip_info
[MCP3912
] },
886 { "mcp3913", (kernel_ulong_t
)&mcp3911_chip_info
[MCP3913
] },
887 { "mcp3914", (kernel_ulong_t
)&mcp3911_chip_info
[MCP3914
] },
888 { "mcp3918", (kernel_ulong_t
)&mcp3911_chip_info
[MCP3918
] },
889 { "mcp3919", (kernel_ulong_t
)&mcp3911_chip_info
[MCP3919
] },
892 MODULE_DEVICE_TABLE(spi
, mcp3911_id
);
894 static struct spi_driver mcp3911_driver
= {
897 .of_match_table
= mcp3911_dt_ids
,
899 .probe
= mcp3911_probe
,
900 .id_table
= mcp3911_id
,
902 module_spi_driver(mcp3911_driver
);
904 MODULE_AUTHOR("Marcus Folkesson <marcus.folkesson@gmail.com>");
905 MODULE_AUTHOR("Kent Gustavsson <kent@minoris.se>");
906 MODULE_DESCRIPTION("Microchip Technology MCP3911");
907 MODULE_LICENSE("GPL v2");