1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 2022 Analog Devices, Inc.
4 * ADI MAX77541 ADC Driver with IIO interface
7 #include <linux/bitfield.h>
8 #include <linux/iio/iio.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/platform_device.h>
11 #include <linux/regmap.h>
12 #include <linux/units.h>
14 #include <linux/mfd/max77541.h>
16 enum max77541_adc_range
{
22 enum max77541_adc_channel
{
29 static int max77541_adc_offset(struct iio_dev
*indio_dev
,
30 struct iio_chan_spec
const *chan
,
33 switch (chan
->channel
) {
34 case MAX77541_ADC_TEMP
:
35 *val
= DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS
, 1725);
42 static int max77541_adc_scale(struct iio_dev
*indio_dev
,
43 struct iio_chan_spec
const *chan
,
46 struct regmap
**regmap
= iio_priv(indio_dev
);
50 switch (chan
->channel
) {
51 case MAX77541_ADC_VSYS_V
:
54 case MAX77541_ADC_VOUT1_V
:
55 case MAX77541_ADC_VOUT2_V
:
56 ret
= regmap_read(*regmap
, MAX77541_REG_M2_CFG1
, ®_val
);
60 reg_val
= FIELD_GET(MAX77541_BITS_MX_CFG1_RNG
, reg_val
);
77 return IIO_VAL_INT_PLUS_MICRO
;
78 case MAX77541_ADC_TEMP
:
86 static int max77541_adc_raw(struct iio_dev
*indio_dev
,
87 struct iio_chan_spec
const *chan
,
90 struct regmap
**regmap
= iio_priv(indio_dev
);
93 ret
= regmap_read(*regmap
, chan
->address
, val
);
100 #define MAX77541_ADC_CHANNEL_V(_channel, _name, _type, _reg) \
104 .channel = _channel, \
106 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
107 BIT(IIO_CHAN_INFO_SCALE), \
108 .datasheet_name = _name, \
111 #define MAX77541_ADC_CHANNEL_TEMP(_channel, _name, _type, _reg) \
115 .channel = _channel, \
117 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
118 BIT(IIO_CHAN_INFO_SCALE) |\
119 BIT(IIO_CHAN_INFO_OFFSET),\
120 .datasheet_name = _name, \
123 static const struct iio_chan_spec max77541_adc_channels
[] = {
124 MAX77541_ADC_CHANNEL_V(MAX77541_ADC_VSYS_V
, "vsys_v", IIO_VOLTAGE
,
125 MAX77541_REG_ADC_DATA_CH1
),
126 MAX77541_ADC_CHANNEL_V(MAX77541_ADC_VOUT1_V
, "vout1_v", IIO_VOLTAGE
,
127 MAX77541_REG_ADC_DATA_CH2
),
128 MAX77541_ADC_CHANNEL_V(MAX77541_ADC_VOUT2_V
, "vout2_v", IIO_VOLTAGE
,
129 MAX77541_REG_ADC_DATA_CH3
),
130 MAX77541_ADC_CHANNEL_TEMP(MAX77541_ADC_TEMP
, "temp", IIO_TEMP
,
131 MAX77541_REG_ADC_DATA_CH6
),
134 static int max77541_adc_read_raw(struct iio_dev
*indio_dev
,
135 struct iio_chan_spec
const *chan
,
136 int *val
, int *val2
, long mask
)
139 case IIO_CHAN_INFO_OFFSET
:
140 return max77541_adc_offset(indio_dev
, chan
, val
, val2
);
141 case IIO_CHAN_INFO_SCALE
:
142 return max77541_adc_scale(indio_dev
, chan
, val
, val2
);
143 case IIO_CHAN_INFO_RAW
:
144 return max77541_adc_raw(indio_dev
, chan
, val
);
150 static const struct iio_info max77541_adc_info
= {
151 .read_raw
= max77541_adc_read_raw
,
154 static int max77541_adc_probe(struct platform_device
*pdev
)
156 struct device
*dev
= &pdev
->dev
;
157 struct iio_dev
*indio_dev
;
158 struct regmap
**regmap
;
160 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*regmap
));
164 regmap
= iio_priv(indio_dev
);
166 *regmap
= dev_get_regmap(dev
->parent
, NULL
);
167 indio_dev
->modes
= INDIO_DIRECT_MODE
;
169 indio_dev
->name
= "max77541";
170 indio_dev
->info
= &max77541_adc_info
;
171 indio_dev
->channels
= max77541_adc_channels
;
172 indio_dev
->num_channels
= ARRAY_SIZE(max77541_adc_channels
);
174 return devm_iio_device_register(dev
, indio_dev
);
177 static const struct platform_device_id max77541_adc_platform_id
[] = {
181 MODULE_DEVICE_TABLE(platform
, max77541_adc_platform_id
);
183 static struct platform_driver max77541_adc_driver
= {
185 .name
= "max77541-adc",
187 .probe
= max77541_adc_probe
,
188 .id_table
= max77541_adc_platform_id
,
190 module_platform_driver(max77541_adc_driver
);
192 MODULE_AUTHOR("Okan Sahin <Okan.Sahin@analog.com>");
193 MODULE_DESCRIPTION("MAX77541 ADC driver");
194 MODULE_LICENSE("GPL");