1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2014 Philippe Reynes
6 * based on linux/drivers/iio/ad7923.c
7 * Copyright 2011 Analog Devices Inc (from AD7923 Driver)
8 * Copyright 2012 CS Systemes d'Information
12 * Partial support for max1027 and similar chips.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/spi/spi.h>
19 #include <linux/delay.h>
21 #include <linux/iio/iio.h>
22 #include <linux/iio/buffer.h>
23 #include <linux/iio/trigger.h>
24 #include <linux/iio/trigger_consumer.h>
25 #include <linux/iio/triggered_buffer.h>
27 #define MAX1027_CONV_REG BIT(7)
28 #define MAX1027_SETUP_REG BIT(6)
29 #define MAX1027_AVG_REG BIT(5)
30 #define MAX1027_RST_REG BIT(4)
32 /* conversion register */
33 #define MAX1027_TEMP BIT(0)
34 #define MAX1027_SCAN_0_N (0x00 << 1)
35 #define MAX1027_SCAN_N_M (0x01 << 1)
36 #define MAX1027_SCAN_N (0x02 << 1)
37 #define MAX1027_NOSCAN (0x03 << 1)
38 #define MAX1027_CHAN(n) ((n) << 3)
41 #define MAX1027_UNIPOLAR 0x02
42 #define MAX1027_BIPOLAR 0x03
43 #define MAX1027_REF_MODE0 (0x00 << 2)
44 #define MAX1027_REF_MODE1 (0x01 << 2)
45 #define MAX1027_REF_MODE2 (0x02 << 2)
46 #define MAX1027_REF_MODE3 (0x03 << 2)
47 #define MAX1027_CKS_MODE0 (0x00 << 4)
48 #define MAX1027_CKS_MODE1 (0x01 << 4)
49 #define MAX1027_CKS_MODE2 (0x02 << 4)
50 #define MAX1027_CKS_MODE3 (0x03 << 4)
52 /* averaging register */
53 #define MAX1027_NSCAN_4 0x00
54 #define MAX1027_NSCAN_8 0x01
55 #define MAX1027_NSCAN_12 0x02
56 #define MAX1027_NSCAN_16 0x03
57 #define MAX1027_NAVG_4 (0x00 << 2)
58 #define MAX1027_NAVG_8 (0x01 << 2)
59 #define MAX1027_NAVG_16 (0x02 << 2)
60 #define MAX1027_NAVG_32 (0x03 << 2)
61 #define MAX1027_AVG_EN BIT(4)
72 static const struct spi_device_id max1027_id
[] = {
81 MODULE_DEVICE_TABLE(spi
, max1027_id
);
83 static const struct of_device_id max1027_adc_dt_ids
[] = {
84 { .compatible
= "maxim,max1027" },
85 { .compatible
= "maxim,max1029" },
86 { .compatible
= "maxim,max1031" },
87 { .compatible
= "maxim,max1227" },
88 { .compatible
= "maxim,max1229" },
89 { .compatible
= "maxim,max1231" },
92 MODULE_DEVICE_TABLE(of
, max1027_adc_dt_ids
);
94 #define MAX1027_V_CHAN(index, depth) \
96 .type = IIO_VOLTAGE, \
99 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
100 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
101 .scan_index = index + 1, \
107 .endianness = IIO_BE, \
111 #define MAX1027_T_CHAN \
115 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
116 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
122 .endianness = IIO_BE, \
126 #define MAX1X27_CHANNELS(depth) \
128 MAX1027_V_CHAN(0, depth), \
129 MAX1027_V_CHAN(1, depth), \
130 MAX1027_V_CHAN(2, depth), \
131 MAX1027_V_CHAN(3, depth), \
132 MAX1027_V_CHAN(4, depth), \
133 MAX1027_V_CHAN(5, depth), \
134 MAX1027_V_CHAN(6, depth), \
135 MAX1027_V_CHAN(7, depth)
137 #define MAX1X29_CHANNELS(depth) \
138 MAX1X27_CHANNELS(depth), \
139 MAX1027_V_CHAN(8, depth), \
140 MAX1027_V_CHAN(9, depth), \
141 MAX1027_V_CHAN(10, depth), \
142 MAX1027_V_CHAN(11, depth)
144 #define MAX1X31_CHANNELS(depth) \
145 MAX1X27_CHANNELS(depth), \
146 MAX1X29_CHANNELS(depth), \
147 MAX1027_V_CHAN(12, depth), \
148 MAX1027_V_CHAN(13, depth), \
149 MAX1027_V_CHAN(14, depth), \
150 MAX1027_V_CHAN(15, depth)
152 static const struct iio_chan_spec max1027_channels
[] = {
153 MAX1X27_CHANNELS(10),
156 static const struct iio_chan_spec max1029_channels
[] = {
157 MAX1X29_CHANNELS(10),
160 static const struct iio_chan_spec max1031_channels
[] = {
161 MAX1X31_CHANNELS(10),
164 static const struct iio_chan_spec max1227_channels
[] = {
165 MAX1X27_CHANNELS(12),
168 static const struct iio_chan_spec max1229_channels
[] = {
169 MAX1X29_CHANNELS(12),
172 static const struct iio_chan_spec max1231_channels
[] = {
173 MAX1X31_CHANNELS(12),
176 static const unsigned long max1027_available_scan_masks
[] = {
181 static const unsigned long max1029_available_scan_masks
[] = {
186 static const unsigned long max1031_available_scan_masks
[] = {
191 struct max1027_chip_info
{
192 const struct iio_chan_spec
*channels
;
193 unsigned int num_channels
;
194 const unsigned long *available_scan_masks
;
197 static const struct max1027_chip_info max1027_chip_info_tbl
[] = {
199 .channels
= max1027_channels
,
200 .num_channels
= ARRAY_SIZE(max1027_channels
),
201 .available_scan_masks
= max1027_available_scan_masks
,
204 .channels
= max1029_channels
,
205 .num_channels
= ARRAY_SIZE(max1029_channels
),
206 .available_scan_masks
= max1029_available_scan_masks
,
209 .channels
= max1031_channels
,
210 .num_channels
= ARRAY_SIZE(max1031_channels
),
211 .available_scan_masks
= max1031_available_scan_masks
,
214 .channels
= max1227_channels
,
215 .num_channels
= ARRAY_SIZE(max1227_channels
),
216 .available_scan_masks
= max1027_available_scan_masks
,
219 .channels
= max1229_channels
,
220 .num_channels
= ARRAY_SIZE(max1229_channels
),
221 .available_scan_masks
= max1029_available_scan_masks
,
224 .channels
= max1231_channels
,
225 .num_channels
= ARRAY_SIZE(max1231_channels
),
226 .available_scan_masks
= max1031_available_scan_masks
,
230 struct max1027_state
{
231 const struct max1027_chip_info
*info
;
232 struct spi_device
*spi
;
233 struct iio_trigger
*trig
;
237 u8 reg ____cacheline_aligned
;
240 static int max1027_read_single_value(struct iio_dev
*indio_dev
,
241 struct iio_chan_spec
const *chan
,
245 struct max1027_state
*st
= iio_priv(indio_dev
);
247 if (iio_buffer_enabled(indio_dev
)) {
248 dev_warn(&indio_dev
->dev
, "trigger mode already enabled");
252 /* Start acquisition on conversion register write */
253 st
->reg
= MAX1027_SETUP_REG
| MAX1027_REF_MODE2
| MAX1027_CKS_MODE2
;
254 ret
= spi_write(st
->spi
, &st
->reg
, 1);
256 dev_err(&indio_dev
->dev
,
257 "Failed to configure setup register\n");
261 /* Configure conversion register with the requested chan */
262 st
->reg
= MAX1027_CONV_REG
| MAX1027_CHAN(chan
->channel
) |
264 if (chan
->type
== IIO_TEMP
)
265 st
->reg
|= MAX1027_TEMP
;
266 ret
= spi_write(st
->spi
, &st
->reg
, 1);
268 dev_err(&indio_dev
->dev
,
269 "Failed to configure conversion register\n");
274 * For an unknown reason, when we use the mode "10" (write
275 * conversion register), the interrupt doesn't occur every time.
276 * So we just wait 1 ms.
281 ret
= spi_read(st
->spi
, st
->buffer
, (chan
->type
== IIO_TEMP
) ? 4 : 2);
285 *val
= be16_to_cpu(st
->buffer
[0]);
290 static int max1027_read_raw(struct iio_dev
*indio_dev
,
291 struct iio_chan_spec
const *chan
,
292 int *val
, int *val2
, long mask
)
295 struct max1027_state
*st
= iio_priv(indio_dev
);
297 mutex_lock(&st
->lock
);
300 case IIO_CHAN_INFO_RAW
:
301 ret
= max1027_read_single_value(indio_dev
, chan
, val
);
303 case IIO_CHAN_INFO_SCALE
:
304 switch (chan
->type
) {
308 ret
= IIO_VAL_FRACTIONAL
;
312 *val2
= chan
->scan_type
.realbits
;
313 ret
= IIO_VAL_FRACTIONAL_LOG2
;
325 mutex_unlock(&st
->lock
);
330 static int max1027_debugfs_reg_access(struct iio_dev
*indio_dev
,
331 unsigned reg
, unsigned writeval
,
334 struct max1027_state
*st
= iio_priv(indio_dev
);
335 u8
*val
= (u8
*)st
->buffer
;
338 int ret
= spi_read(st
->spi
, val
, 2);
339 *readval
= be16_to_cpu(st
->buffer
[0]);
344 return spi_write(st
->spi
, val
, 1);
347 static int max1027_validate_trigger(struct iio_dev
*indio_dev
,
348 struct iio_trigger
*trig
)
350 struct max1027_state
*st
= iio_priv(indio_dev
);
352 if (st
->trig
!= trig
)
358 static int max1027_set_trigger_state(struct iio_trigger
*trig
, bool state
)
360 struct iio_dev
*indio_dev
= iio_trigger_get_drvdata(trig
);
361 struct max1027_state
*st
= iio_priv(indio_dev
);
365 /* Start acquisition on cnvst */
366 st
->reg
= MAX1027_SETUP_REG
| MAX1027_CKS_MODE0
|
368 ret
= spi_write(st
->spi
, &st
->reg
, 1);
372 /* Scan from 0 to max */
373 st
->reg
= MAX1027_CONV_REG
| MAX1027_CHAN(0) |
374 MAX1027_SCAN_N_M
| MAX1027_TEMP
;
375 ret
= spi_write(st
->spi
, &st
->reg
, 1);
379 /* Start acquisition on conversion register write */
380 st
->reg
= MAX1027_SETUP_REG
| MAX1027_CKS_MODE2
|
382 ret
= spi_write(st
->spi
, &st
->reg
, 1);
390 static irqreturn_t
max1027_trigger_handler(int irq
, void *private)
392 struct iio_poll_func
*pf
= private;
393 struct iio_dev
*indio_dev
= pf
->indio_dev
;
394 struct max1027_state
*st
= iio_priv(indio_dev
);
396 pr_debug("%s(irq=%d, private=0x%p)\n", __func__
, irq
, private);
398 /* fill buffer with all channel */
399 spi_read(st
->spi
, st
->buffer
, indio_dev
->masklength
* 2);
401 iio_push_to_buffers(indio_dev
, st
->buffer
);
403 iio_trigger_notify_done(indio_dev
->trig
);
408 static const struct iio_trigger_ops max1027_trigger_ops
= {
409 .validate_device
= &iio_trigger_validate_own_device
,
410 .set_trigger_state
= &max1027_set_trigger_state
,
413 static const struct iio_info max1027_info
= {
414 .read_raw
= &max1027_read_raw
,
415 .validate_trigger
= &max1027_validate_trigger
,
416 .debugfs_reg_access
= &max1027_debugfs_reg_access
,
419 static int max1027_probe(struct spi_device
*spi
)
422 struct iio_dev
*indio_dev
;
423 struct max1027_state
*st
;
425 pr_debug("%s: probe(spi = 0x%p)\n", __func__
, spi
);
427 indio_dev
= devm_iio_device_alloc(&spi
->dev
, sizeof(*st
));
428 if (indio_dev
== NULL
) {
429 pr_err("Can't allocate iio device\n");
433 spi_set_drvdata(spi
, indio_dev
);
435 st
= iio_priv(indio_dev
);
437 st
->info
= &max1027_chip_info_tbl
[spi_get_device_id(spi
)->driver_data
];
439 mutex_init(&st
->lock
);
441 indio_dev
->name
= spi_get_device_id(spi
)->name
;
442 indio_dev
->info
= &max1027_info
;
443 indio_dev
->modes
= INDIO_DIRECT_MODE
;
444 indio_dev
->channels
= st
->info
->channels
;
445 indio_dev
->num_channels
= st
->info
->num_channels
;
446 indio_dev
->available_scan_masks
= st
->info
->available_scan_masks
;
448 st
->buffer
= devm_kmalloc_array(&indio_dev
->dev
,
449 indio_dev
->num_channels
, 2,
451 if (st
->buffer
== NULL
) {
452 dev_err(&indio_dev
->dev
, "Can't allocate buffer\n");
457 ret
= devm_iio_triggered_buffer_setup(&spi
->dev
, indio_dev
,
458 &iio_pollfunc_store_time
,
459 &max1027_trigger_handler
,
462 dev_err(&indio_dev
->dev
, "Failed to setup buffer\n");
466 st
->trig
= devm_iio_trigger_alloc(&spi
->dev
, "%s-trigger",
468 if (st
->trig
== NULL
) {
470 dev_err(&indio_dev
->dev
,
471 "Failed to allocate iio trigger\n");
475 st
->trig
->ops
= &max1027_trigger_ops
;
476 st
->trig
->dev
.parent
= &spi
->dev
;
477 iio_trigger_set_drvdata(st
->trig
, indio_dev
);
478 ret
= devm_iio_trigger_register(&indio_dev
->dev
,
481 dev_err(&indio_dev
->dev
,
482 "Failed to register iio trigger\n");
486 ret
= devm_request_threaded_irq(&spi
->dev
, spi
->irq
,
487 iio_trigger_generic_data_rdy_poll
,
489 IRQF_TRIGGER_FALLING
,
490 spi
->dev
.driver
->name
,
493 dev_err(&indio_dev
->dev
, "Failed to allocate IRQ.\n");
499 st
->reg
= MAX1027_RST_REG
;
500 ret
= spi_write(st
->spi
, &st
->reg
, 1);
502 dev_err(&indio_dev
->dev
, "Failed to reset the ADC\n");
506 /* Disable averaging */
507 st
->reg
= MAX1027_AVG_REG
;
508 ret
= spi_write(st
->spi
, &st
->reg
, 1);
510 dev_err(&indio_dev
->dev
, "Failed to configure averaging register\n");
514 return devm_iio_device_register(&spi
->dev
, indio_dev
);
517 static struct spi_driver max1027_driver
= {
520 .of_match_table
= max1027_adc_dt_ids
,
522 .probe
= max1027_probe
,
523 .id_table
= max1027_id
,
525 module_spi_driver(max1027_driver
);
527 MODULE_AUTHOR("Philippe Reynes <tremyfr@yahoo.fr>");
528 MODULE_DESCRIPTION("MAX1X27/MAX1X29/MAX1X31 ADC");
529 MODULE_LICENSE("GPL v2");