Linux Audio

Check our new training course

Loading...
v5.4
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * iio/adc/max11100.c
  4 * Maxim max11100 ADC Driver with IIO interface
  5 *
  6 * Copyright (C) 2016-17 Renesas Electronics Corporation
  7 * Copyright (C) 2016-17 Jacopo Mondi
  8 */
  9#include <linux/delay.h>
 10#include <linux/kernel.h>
 
 11#include <linux/module.h>
 12#include <linux/regulator/consumer.h>
 13#include <linux/spi/spi.h>
 14
 15#include <linux/iio/iio.h>
 16#include <linux/iio/driver.h>
 17
 18/*
 19 * LSB is the ADC single digital step
 20 * 1 LSB = (vref_mv / 2 ^ 16)
 21 *
 22 * LSB is used to calculate analog voltage value
 23 * from the number of ADC steps count
 24 *
 25 * Ain = (count * LSB)
 26 */
 27#define MAX11100_LSB_DIV		(1 << 16)
 28
 29struct max11100_state {
 30	struct regulator *vref_reg;
 31	struct spi_device *spi;
 32
 33	/*
 34	 * DMA (thus cache coherency maintenance) requires the
 35	 * transfer buffers to live in their own cache lines.
 36	 */
 37	u8 buffer[3] ____cacheline_aligned;
 38};
 39
 40static struct iio_chan_spec max11100_channels[] = {
 41	{ /* [0] */
 42		.type = IIO_VOLTAGE,
 43		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
 44				      BIT(IIO_CHAN_INFO_SCALE),
 45	},
 46};
 47
 48static int max11100_read_single(struct iio_dev *indio_dev, int *val)
 49{
 50	int ret;
 51	struct max11100_state *state = iio_priv(indio_dev);
 52
 53	ret = spi_read(state->spi, state->buffer, sizeof(state->buffer));
 54	if (ret) {
 55		dev_err(&indio_dev->dev, "SPI transfer failed\n");
 56		return ret;
 57	}
 58
 59	/* the first 8 bits sent out from ADC must be 0s */
 60	if (state->buffer[0]) {
 61		dev_err(&indio_dev->dev, "Invalid value: buffer[0] != 0\n");
 62		return -EINVAL;
 63	}
 64
 65	*val = (state->buffer[1] << 8) | state->buffer[2];
 66
 67	return 0;
 68}
 69
 70static int max11100_read_raw(struct iio_dev *indio_dev,
 71			     struct iio_chan_spec const *chan,
 72			     int *val, int *val2, long info)
 73{
 74	int ret, vref_uv;
 75	struct max11100_state *state = iio_priv(indio_dev);
 76
 77	switch (info) {
 78	case IIO_CHAN_INFO_RAW:
 79		ret = max11100_read_single(indio_dev, val);
 80		if (ret)
 81			return ret;
 82
 83		return IIO_VAL_INT;
 84
 85	case IIO_CHAN_INFO_SCALE:
 86		vref_uv = regulator_get_voltage(state->vref_reg);
 87		if (vref_uv < 0)
 88			/* dummy regulator "get_voltage" returns -EINVAL */
 89			return -EINVAL;
 90
 91		*val =  vref_uv / 1000;
 92		*val2 = MAX11100_LSB_DIV;
 93		return IIO_VAL_FRACTIONAL;
 94	}
 95
 96	return -EINVAL;
 97}
 98
 99static const struct iio_info max11100_info = {
100	.read_raw = max11100_read_raw,
101};
102
103static int max11100_probe(struct spi_device *spi)
104{
105	int ret;
106	struct iio_dev *indio_dev;
107	struct max11100_state *state;
108
109	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
110	if (!indio_dev)
111		return -ENOMEM;
112
113	spi_set_drvdata(spi, indio_dev);
114
115	state = iio_priv(indio_dev);
116	state->spi = spi;
117
118	indio_dev->dev.parent = &spi->dev;
119	indio_dev->dev.of_node = spi->dev.of_node;
120	indio_dev->name = "max11100";
121	indio_dev->info = &max11100_info;
122	indio_dev->modes = INDIO_DIRECT_MODE;
123	indio_dev->channels = max11100_channels;
124	indio_dev->num_channels = ARRAY_SIZE(max11100_channels);
125
126	state->vref_reg = devm_regulator_get(&spi->dev, "vref");
127	if (IS_ERR(state->vref_reg))
128		return PTR_ERR(state->vref_reg);
129
130	ret = regulator_enable(state->vref_reg);
131	if (ret)
132		return ret;
133
134	ret = iio_device_register(indio_dev);
135	if (ret)
136		goto disable_regulator;
137
138	return 0;
139
140disable_regulator:
141	regulator_disable(state->vref_reg);
142
143	return ret;
144}
145
146static int max11100_remove(struct spi_device *spi)
147{
148	struct iio_dev *indio_dev = spi_get_drvdata(spi);
149	struct max11100_state *state = iio_priv(indio_dev);
150
151	iio_device_unregister(indio_dev);
152	regulator_disable(state->vref_reg);
153
154	return 0;
155}
156
157static const struct of_device_id max11100_ids[] = {
158	{.compatible = "maxim,max11100"},
159	{ },
160};
161MODULE_DEVICE_TABLE(of, max11100_ids);
162
163static struct spi_driver max11100_driver = {
164	.driver = {
165		.name	= "max11100",
166		.of_match_table = of_match_ptr(max11100_ids),
167	},
168	.probe		= max11100_probe,
169	.remove		= max11100_remove,
170};
171
172module_spi_driver(max11100_driver);
173
174MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
175MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
176MODULE_LICENSE("GPL v2");
v5.9
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * iio/adc/max11100.c
  4 * Maxim max11100 ADC Driver with IIO interface
  5 *
  6 * Copyright (C) 2016-17 Renesas Electronics Corporation
  7 * Copyright (C) 2016-17 Jacopo Mondi
  8 */
  9#include <linux/delay.h>
 10#include <linux/kernel.h>
 11#include <linux/mod_devicetable.h>
 12#include <linux/module.h>
 13#include <linux/regulator/consumer.h>
 14#include <linux/spi/spi.h>
 15
 16#include <linux/iio/iio.h>
 17#include <linux/iio/driver.h>
 18
 19/*
 20 * LSB is the ADC single digital step
 21 * 1 LSB = (vref_mv / 2 ^ 16)
 22 *
 23 * LSB is used to calculate analog voltage value
 24 * from the number of ADC steps count
 25 *
 26 * Ain = (count * LSB)
 27 */
 28#define MAX11100_LSB_DIV		(1 << 16)
 29
 30struct max11100_state {
 31	struct regulator *vref_reg;
 32	struct spi_device *spi;
 33
 34	/*
 35	 * DMA (thus cache coherency maintenance) requires the
 36	 * transfer buffers to live in their own cache lines.
 37	 */
 38	u8 buffer[3] ____cacheline_aligned;
 39};
 40
 41static const struct iio_chan_spec max11100_channels[] = {
 42	{ /* [0] */
 43		.type = IIO_VOLTAGE,
 44		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |
 45				      BIT(IIO_CHAN_INFO_SCALE),
 46	},
 47};
 48
 49static int max11100_read_single(struct iio_dev *indio_dev, int *val)
 50{
 51	int ret;
 52	struct max11100_state *state = iio_priv(indio_dev);
 53
 54	ret = spi_read(state->spi, state->buffer, sizeof(state->buffer));
 55	if (ret) {
 56		dev_err(&indio_dev->dev, "SPI transfer failed\n");
 57		return ret;
 58	}
 59
 60	/* the first 8 bits sent out from ADC must be 0s */
 61	if (state->buffer[0]) {
 62		dev_err(&indio_dev->dev, "Invalid value: buffer[0] != 0\n");
 63		return -EINVAL;
 64	}
 65
 66	*val = (state->buffer[1] << 8) | state->buffer[2];
 67
 68	return 0;
 69}
 70
 71static int max11100_read_raw(struct iio_dev *indio_dev,
 72			     struct iio_chan_spec const *chan,
 73			     int *val, int *val2, long info)
 74{
 75	int ret, vref_uv;
 76	struct max11100_state *state = iio_priv(indio_dev);
 77
 78	switch (info) {
 79	case IIO_CHAN_INFO_RAW:
 80		ret = max11100_read_single(indio_dev, val);
 81		if (ret)
 82			return ret;
 83
 84		return IIO_VAL_INT;
 85
 86	case IIO_CHAN_INFO_SCALE:
 87		vref_uv = regulator_get_voltage(state->vref_reg);
 88		if (vref_uv < 0)
 89			/* dummy regulator "get_voltage" returns -EINVAL */
 90			return -EINVAL;
 91
 92		*val =  vref_uv / 1000;
 93		*val2 = MAX11100_LSB_DIV;
 94		return IIO_VAL_FRACTIONAL;
 95	}
 96
 97	return -EINVAL;
 98}
 99
100static const struct iio_info max11100_info = {
101	.read_raw = max11100_read_raw,
102};
103
104static int max11100_probe(struct spi_device *spi)
105{
106	int ret;
107	struct iio_dev *indio_dev;
108	struct max11100_state *state;
109
110	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*state));
111	if (!indio_dev)
112		return -ENOMEM;
113
114	spi_set_drvdata(spi, indio_dev);
115
116	state = iio_priv(indio_dev);
117	state->spi = spi;
118
 
 
119	indio_dev->name = "max11100";
120	indio_dev->info = &max11100_info;
121	indio_dev->modes = INDIO_DIRECT_MODE;
122	indio_dev->channels = max11100_channels;
123	indio_dev->num_channels = ARRAY_SIZE(max11100_channels);
124
125	state->vref_reg = devm_regulator_get(&spi->dev, "vref");
126	if (IS_ERR(state->vref_reg))
127		return PTR_ERR(state->vref_reg);
128
129	ret = regulator_enable(state->vref_reg);
130	if (ret)
131		return ret;
132
133	ret = iio_device_register(indio_dev);
134	if (ret)
135		goto disable_regulator;
136
137	return 0;
138
139disable_regulator:
140	regulator_disable(state->vref_reg);
141
142	return ret;
143}
144
145static int max11100_remove(struct spi_device *spi)
146{
147	struct iio_dev *indio_dev = spi_get_drvdata(spi);
148	struct max11100_state *state = iio_priv(indio_dev);
149
150	iio_device_unregister(indio_dev);
151	regulator_disable(state->vref_reg);
152
153	return 0;
154}
155
156static const struct of_device_id max11100_ids[] = {
157	{.compatible = "maxim,max11100"},
158	{ },
159};
160MODULE_DEVICE_TABLE(of, max11100_ids);
161
162static struct spi_driver max11100_driver = {
163	.driver = {
164		.name	= "max11100",
165		.of_match_table = max11100_ids,
166	},
167	.probe		= max11100_probe,
168	.remove		= max11100_remove,
169};
170
171module_spi_driver(max11100_driver);
172
173MODULE_AUTHOR("Jacopo Mondi <jacopo@jmondi.org>");
174MODULE_DESCRIPTION("Maxim max11100 ADC Driver");
175MODULE_LICENSE("GPL v2");