3 * Copyright (C) 2014 Philippe Reynes
5 * based on linux/drivers/iio/ad7923.c
6 * Copyright 2011 Analog Devices Inc (from AD7923 Driver)
7 * Copyright 2012 CS Systemes d'Information
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
15 * Partial support for max1027 and similar chips.
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/spi/spi.h>
21 #include <linux/delay.h>
23 #include <linux/iio/iio.h>
24 #include <linux/iio/buffer.h>
25 #include <linux/iio/trigger.h>
26 #include <linux/iio/trigger_consumer.h>
27 #include <linux/iio/triggered_buffer.h>
29 #define MAX1027_CONV_REG BIT(7)
30 #define MAX1027_SETUP_REG BIT(6)
31 #define MAX1027_AVG_REG BIT(5)
32 #define MAX1027_RST_REG BIT(4)
34 /* conversion register */
35 #define MAX1027_TEMP BIT(0)
36 #define MAX1027_SCAN_0_N (0x00 << 1)
37 #define MAX1027_SCAN_N_M (0x01 << 1)
38 #define MAX1027_SCAN_N (0x02 << 1)
39 #define MAX1027_NOSCAN (0x03 << 1)
40 #define MAX1027_CHAN(n) ((n) << 3)
43 #define MAX1027_UNIPOLAR 0x02
44 #define MAX1027_BIPOLAR 0x03
45 #define MAX1027_REF_MODE0 (0x00 << 2)
46 #define MAX1027_REF_MODE1 (0x01 << 2)
47 #define MAX1027_REF_MODE2 (0x02 << 2)
48 #define MAX1027_REF_MODE3 (0x03 << 2)
49 #define MAX1027_CKS_MODE0 (0x00 << 4)
50 #define MAX1027_CKS_MODE1 (0x01 << 4)
51 #define MAX1027_CKS_MODE2 (0x02 << 4)
52 #define MAX1027_CKS_MODE3 (0x03 << 4)
54 /* averaging register */
55 #define MAX1027_NSCAN_4 0x00
56 #define MAX1027_NSCAN_8 0x01
57 #define MAX1027_NSCAN_12 0x02
58 #define MAX1027_NSCAN_16 0x03
59 #define MAX1027_NAVG_4 (0x00 << 2)
60 #define MAX1027_NAVG_8 (0x01 << 2)
61 #define MAX1027_NAVG_16 (0x02 << 2)
62 #define MAX1027_NAVG_32 (0x03 << 2)
63 #define MAX1027_AVG_EN BIT(4)
71 static const struct spi_device_id max1027_id[] = {
77 MODULE_DEVICE_TABLE(spi, max1027_id);
80 static const struct of_device_id max1027_adc_dt_ids[] = {
81 { .compatible = "maxim,max1027" },
82 { .compatible = "maxim,max1029" },
83 { .compatible = "maxim,max1031" },
86 MODULE_DEVICE_TABLE(of, max1027_adc_dt_ids);
89 #define MAX1027_V_CHAN(index) \
91 .type = IIO_VOLTAGE, \
94 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
95 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
96 .scan_index = index + 1, \
102 .endianness = IIO_BE, \
106 #define MAX1027_T_CHAN \
110 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW), \
111 .info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE), \
117 .endianness = IIO_BE, \
121 static const struct iio_chan_spec max1027_channels[] = {
133 static const struct iio_chan_spec max1029_channels[] = {
149 static const struct iio_chan_spec max1031_channels[] = {
169 static const unsigned long max1027_available_scan_masks[] = {
174 static const unsigned long max1029_available_scan_masks[] = {
179 static const unsigned long max1031_available_scan_masks[] = {
184 struct max1027_chip_info {
185 const struct iio_chan_spec *channels;
186 unsigned int num_channels;
187 const unsigned long *available_scan_masks;
190 static const struct max1027_chip_info max1027_chip_info_tbl[] = {
192 .channels = max1027_channels,
193 .num_channels = ARRAY_SIZE(max1027_channels),
194 .available_scan_masks = max1027_available_scan_masks,
197 .channels = max1029_channels,
198 .num_channels = ARRAY_SIZE(max1029_channels),
199 .available_scan_masks = max1029_available_scan_masks,
202 .channels = max1031_channels,
203 .num_channels = ARRAY_SIZE(max1031_channels),
204 .available_scan_masks = max1031_available_scan_masks,
208 struct max1027_state {
209 const struct max1027_chip_info *info;
210 struct spi_device *spi;
211 struct iio_trigger *trig;
215 u8 reg ____cacheline_aligned;
218 static int max1027_read_single_value(struct iio_dev *indio_dev,
219 struct iio_chan_spec const *chan,
223 struct max1027_state *st = iio_priv(indio_dev);
225 if (iio_buffer_enabled(indio_dev)) {
226 dev_warn(&indio_dev->dev, "trigger mode already enabled");
230 /* Start acquisition on conversion register write */
231 st->reg = MAX1027_SETUP_REG | MAX1027_REF_MODE2 | MAX1027_CKS_MODE2;
232 ret = spi_write(st->spi, &st->reg, 1);
234 dev_err(&indio_dev->dev,
235 "Failed to configure setup register\n");
239 /* Configure conversion register with the requested chan */
240 st->reg = MAX1027_CONV_REG | MAX1027_CHAN(chan->channel) |
241 MAX1027_NOSCAN | !!(chan->type == IIO_TEMP);
242 ret = spi_write(st->spi, &st->reg, 1);
244 dev_err(&indio_dev->dev,
245 "Failed to configure conversion register\n");
250 * For an unknown reason, when we use the mode "10" (write
251 * conversion register), the interrupt doesn't occur every time.
252 * So we just wait 1 ms.
257 ret = spi_read(st->spi, st->buffer, (chan->type == IIO_TEMP) ? 4 : 2);
261 *val = be16_to_cpu(st->buffer[0]);
266 static int max1027_read_raw(struct iio_dev *indio_dev,
267 struct iio_chan_spec const *chan,
268 int *val, int *val2, long mask)
271 struct max1027_state *st = iio_priv(indio_dev);
273 mutex_lock(&st->lock);
276 case IIO_CHAN_INFO_RAW:
277 ret = max1027_read_single_value(indio_dev, chan, val);
279 case IIO_CHAN_INFO_SCALE:
280 switch (chan->type) {
284 ret = IIO_VAL_FRACTIONAL;
289 ret = IIO_VAL_FRACTIONAL_LOG2;
301 mutex_unlock(&st->lock);
306 static int max1027_debugfs_reg_access(struct iio_dev *indio_dev,
307 unsigned reg, unsigned writeval,
310 struct max1027_state *st = iio_priv(indio_dev);
311 u8 *val = (u8 *)st->buffer;
317 return spi_write(st->spi, val, 1);
320 static int max1027_validate_trigger(struct iio_dev *indio_dev,
321 struct iio_trigger *trig)
323 struct max1027_state *st = iio_priv(indio_dev);
325 if (st->trig != trig)
331 static int max1027_set_trigger_state(struct iio_trigger *trig, bool state)
333 struct iio_dev *indio_dev = iio_trigger_get_drvdata(trig);
334 struct max1027_state *st = iio_priv(indio_dev);
338 /* Start acquisition on cnvst */
339 st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE0 |
341 ret = spi_write(st->spi, &st->reg, 1);
345 /* Scan from 0 to max */
346 st->reg = MAX1027_CONV_REG | MAX1027_CHAN(0) |
347 MAX1027_SCAN_N_M | MAX1027_TEMP;
348 ret = spi_write(st->spi, &st->reg, 1);
352 /* Start acquisition on conversion register write */
353 st->reg = MAX1027_SETUP_REG | MAX1027_CKS_MODE2 |
355 ret = spi_write(st->spi, &st->reg, 1);
363 static int max1027_validate_device(struct iio_trigger *trig,
364 struct iio_dev *indio_dev)
366 struct iio_dev *indio = iio_trigger_get_drvdata(trig);
368 if (indio != indio_dev)
374 static irqreturn_t max1027_trigger_handler(int irq, void *private)
376 struct iio_poll_func *pf = (struct iio_poll_func *)private;
377 struct iio_dev *indio_dev = pf->indio_dev;
378 struct max1027_state *st = iio_priv(indio_dev);
380 pr_debug("%s(irq=%d, private=0x%p)\n", __func__, irq, private);
382 /* fill buffer with all channel */
383 spi_read(st->spi, st->buffer, indio_dev->masklength * 2);
385 iio_push_to_buffers(indio_dev, st->buffer);
387 iio_trigger_notify_done(indio_dev->trig);
392 static const struct iio_trigger_ops max1027_trigger_ops = {
393 .owner = THIS_MODULE,
394 .validate_device = &max1027_validate_device,
395 .set_trigger_state = &max1027_set_trigger_state,
398 static const struct iio_info max1027_info = {
399 .driver_module = THIS_MODULE,
400 .read_raw = &max1027_read_raw,
401 .validate_trigger = &max1027_validate_trigger,
402 .debugfs_reg_access = &max1027_debugfs_reg_access,
405 static int max1027_probe(struct spi_device *spi)
408 struct iio_dev *indio_dev;
409 struct max1027_state *st;
411 pr_debug("%s: probe(spi = 0x%p)\n", __func__, spi);
413 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*st));
414 if (indio_dev == NULL) {
415 pr_err("Can't allocate iio device\n");
419 spi_set_drvdata(spi, indio_dev);
421 st = iio_priv(indio_dev);
423 st->info = &max1027_chip_info_tbl[spi_get_device_id(spi)->driver_data];
425 mutex_init(&st->lock);
427 indio_dev->name = spi_get_device_id(spi)->name;
428 indio_dev->dev.parent = &spi->dev;
429 indio_dev->dev.of_node = spi->dev.of_node;
430 indio_dev->info = &max1027_info;
431 indio_dev->modes = INDIO_DIRECT_MODE;
432 indio_dev->channels = st->info->channels;
433 indio_dev->num_channels = st->info->num_channels;
434 indio_dev->available_scan_masks = st->info->available_scan_masks;
436 st->buffer = devm_kmalloc(&indio_dev->dev,
437 indio_dev->num_channels * 2,
439 if (st->buffer == NULL) {
440 dev_err(&indio_dev->dev, "Can't allocate buffer\n");
444 ret = iio_triggered_buffer_setup(indio_dev, &iio_pollfunc_store_time,
445 &max1027_trigger_handler, NULL);
447 dev_err(&indio_dev->dev, "Failed to setup buffer\n");
451 st->trig = devm_iio_trigger_alloc(&spi->dev, "%s-trigger",
453 if (st->trig == NULL) {
455 dev_err(&indio_dev->dev, "Failed to allocate iio trigger\n");
456 goto fail_trigger_alloc;
459 st->trig->ops = &max1027_trigger_ops;
460 st->trig->dev.parent = &spi->dev;
461 iio_trigger_set_drvdata(st->trig, indio_dev);
462 iio_trigger_register(st->trig);
464 ret = devm_request_threaded_irq(&spi->dev, spi->irq,
465 iio_trigger_generic_data_rdy_poll,
467 IRQF_TRIGGER_FALLING,
468 spi->dev.driver->name, st->trig);
470 dev_err(&indio_dev->dev, "Failed to allocate IRQ.\n");
471 goto fail_dev_register;
475 st->reg = MAX1027_RST_REG;
476 ret = spi_write(st->spi, &st->reg, 1);
478 dev_err(&indio_dev->dev, "Failed to reset the ADC\n");
482 /* Disable averaging */
483 st->reg = MAX1027_AVG_REG;
484 ret = spi_write(st->spi, &st->reg, 1);
486 dev_err(&indio_dev->dev, "Failed to configure averaging register\n");
487 goto fail_dev_register;
490 ret = iio_device_register(indio_dev);
492 dev_err(&indio_dev->dev, "Failed to register iio device\n");
493 goto fail_dev_register;
500 iio_triggered_buffer_cleanup(indio_dev);
505 static int max1027_remove(struct spi_device *spi)
507 struct iio_dev *indio_dev = spi_get_drvdata(spi);
509 pr_debug("%s: remove(spi = 0x%p)\n", __func__, spi);
511 iio_device_unregister(indio_dev);
512 iio_triggered_buffer_cleanup(indio_dev);
517 static struct spi_driver max1027_driver = {
520 .of_match_table = of_match_ptr(max1027_adc_dt_ids),
522 .probe = max1027_probe,
523 .remove = max1027_remove,
524 .id_table = max1027_id,
526 module_spi_driver(max1027_driver);
528 MODULE_AUTHOR("Philippe Reynes <tremyfr@yahoo.fr>");
529 MODULE_DESCRIPTION("MAX1027/MAX1029/MAX1031 ADC");
530 MODULE_LICENSE("GPL v2");