1 // SPDX-License-Identifier: GPL-2.0
3 * IIO driver for Maxim MAX34409/34408 ADC, 4-Channels/2-Channels, 8bits, I2C
5 * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/MAX34408-MAX34409.pdf
7 * TODO: ALERT interrupt, Overcurrent delay, Shutdown delay
10 #include <linux/bitfield.h>
11 #include <linux/init.h>
12 #include <linux/i2c.h>
13 #include <linux/module.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/property.h>
16 #include <linux/regmap.h>
18 #include <linux/iio/iio.h>
19 #include <linux/iio/types.h>
21 #define MAX34408_STATUS_REG 0x0
22 #define MAX34408_CONTROL_REG 0x1
23 #define MAX34408_OCDELAY_REG 0x2
24 #define MAX34408_SDDELAY_REG 0x3
26 #define MAX34408_ADC1_REG 0x4
27 #define MAX34408_ADC2_REG 0x5
28 /* ADC3 & ADC4 always returns 0x0 on 34408 */
29 #define MAX34409_ADC3_REG 0x6
30 #define MAX34409_ADC4_REG 0x7
32 #define MAX34408_OCT1_REG 0x8
33 #define MAX34408_OCT2_REG 0x9
34 #define MAX34409_OCT3_REG 0xA
35 #define MAX34409_OCT4_REG 0xB
37 #define MAX34408_DID_REG 0xC
38 #define MAX34408_DCYY_REG 0xD
39 #define MAX34408_DCWW_REG 0xE
41 /* Bit masks for status register */
42 #define MAX34408_STATUS_OC_MSK GENMASK(1, 0)
43 #define MAX34409_STATUS_OC_MSK GENMASK(3, 0)
44 #define MAX34408_STATUS_SHTDN BIT(4)
45 #define MAX34408_STATUS_ENA BIT(5)
47 /* Bit masks for control register */
48 #define MAX34408_CONTROL_AVG0 BIT(0)
49 #define MAX34408_CONTROL_AVG1 BIT(1)
50 #define MAX34408_CONTROL_AVG2 BIT(2)
51 #define MAX34408_CONTROL_ALERT BIT(3)
53 #define MAX34408_DEFAULT_AVG 0x4
55 /* Bit masks for over current delay */
56 #define MAX34408_OCDELAY_OCD_MSK GENMASK(6, 0)
57 #define MAX34408_OCDELAY_RESET BIT(7)
59 /* Bit masks for shutdown delay */
60 #define MAX34408_SDDELAY_SHD_MSK GENMASK(6, 0)
61 #define MAX34408_SDDELAY_RESET BIT(7)
63 #define MAX34408_DEFAULT_RSENSE 1000
66 * struct max34408_data - max34408/max34409 specific data.
67 * @regmap: device register map.
68 * @dev: max34408 device.
69 * @lock: lock for protecting access to device hardware registers, mostly
70 * for read modify write cycles for control registers.
71 * @input_rsense: Rsense values in uOhm, will be overwritten by
72 * values from channel nodes.
74 struct max34408_data
{
75 struct regmap
*regmap
;
81 static const struct regmap_config max34408_regmap_config
= {
84 .max_register
= MAX34408_DCWW_REG
,
87 struct max34408_adc_model_data
{
88 const char *model_name
;
89 const struct iio_chan_spec
*channels
;
90 const int num_channels
;
93 #define MAX34008_CHANNEL(_index, _address) \
95 .type = IIO_CURRENT, \
96 .info_mask_separate = BIT(IIO_CHAN_INFO_RAW) | \
97 BIT(IIO_CHAN_INFO_SCALE) | \
98 BIT(IIO_CHAN_INFO_OFFSET), \
99 .channel = (_index), \
100 .address = (_address), \
104 static const struct iio_chan_spec max34408_channels
[] = {
105 MAX34008_CHANNEL(0, MAX34408_ADC1_REG
),
106 MAX34008_CHANNEL(1, MAX34408_ADC2_REG
),
109 static const struct iio_chan_spec max34409_channels
[] = {
110 MAX34008_CHANNEL(0, MAX34408_ADC1_REG
),
111 MAX34008_CHANNEL(1, MAX34408_ADC2_REG
),
112 MAX34008_CHANNEL(2, MAX34409_ADC3_REG
),
113 MAX34008_CHANNEL(3, MAX34409_ADC4_REG
),
116 static int max34408_read_adc_avg(struct max34408_data
*max34408
,
117 const struct iio_chan_spec
*chan
, int *val
)
122 guard(mutex
)(&max34408
->lock
);
123 rc
= regmap_read(max34408
->regmap
, MAX34408_CONTROL_REG
, (u32
*)&ctrl
);
127 /* set averaging (0b100) default values*/
128 rc
= regmap_write(max34408
->regmap
, MAX34408_CONTROL_REG
,
129 MAX34408_DEFAULT_AVG
);
131 dev_err(max34408
->dev
,
132 "Error (%d) writing control register\n", rc
);
136 rc
= regmap_read(max34408
->regmap
, chan
->address
, val
);
140 /* back to old values */
141 rc
= regmap_write(max34408
->regmap
, MAX34408_CONTROL_REG
, ctrl
);
143 dev_err(max34408
->dev
,
144 "Error (%d) writing control register\n", rc
);
149 static int max34408_read_raw(struct iio_dev
*indio_dev
,
150 struct iio_chan_spec
const *chan
,
151 int *val
, int *val2
, long mask
)
153 struct max34408_data
*max34408
= iio_priv(indio_dev
);
157 case IIO_CHAN_INFO_RAW
:
158 rc
= max34408_read_adc_avg(max34408
, chan
, val
);
162 case IIO_CHAN_INFO_SCALE
:
164 * calculate current for 8bit ADC with Rsense
166 * 10 mV * 1000 / Rsense uOhm = max current
167 * (max current * adc val * 1000) / (2^8 - 1) mA
169 *val
= 10000 / max34408
->input_rsense
[chan
->channel
];
171 return IIO_VAL_FRACTIONAL_LOG2
;
177 static const struct iio_info max34408_info
= {
178 .read_raw
= max34408_read_raw
,
181 static const struct max34408_adc_model_data max34408_model_data
= {
182 .model_name
= "max34408",
183 .channels
= max34408_channels
,
187 static const struct max34408_adc_model_data max34409_model_data
= {
188 .model_name
= "max34409",
189 .channels
= max34409_channels
,
193 static int max34408_probe(struct i2c_client
*client
)
195 const struct max34408_adc_model_data
*model_data
;
196 struct device
*dev
= &client
->dev
;
197 struct max34408_data
*max34408
;
198 struct fwnode_handle
*node
;
199 struct iio_dev
*indio_dev
;
200 struct regmap
*regmap
;
203 model_data
= i2c_get_match_data(client
);
207 regmap
= devm_regmap_init_i2c(client
, &max34408_regmap_config
);
208 if (IS_ERR(regmap
)) {
209 dev_err_probe(dev
, PTR_ERR(regmap
),
210 "regmap_init failed\n");
211 return PTR_ERR(regmap
);
214 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*max34408
));
218 max34408
= iio_priv(indio_dev
);
219 max34408
->regmap
= regmap
;
221 mutex_init(&max34408
->lock
);
223 device_for_each_child_node(dev
, node
) {
224 fwnode_property_read_u32(node
, "maxim,rsense-val-micro-ohms",
225 &max34408
->input_rsense
[i
]);
229 /* disable ALERT and averaging */
230 rc
= regmap_write(max34408
->regmap
, MAX34408_CONTROL_REG
, 0x0);
234 indio_dev
->channels
= model_data
->channels
;
235 indio_dev
->num_channels
= model_data
->num_channels
;
236 indio_dev
->name
= model_data
->model_name
;
238 indio_dev
->info
= &max34408_info
;
239 indio_dev
->modes
= INDIO_DIRECT_MODE
;
241 return devm_iio_device_register(dev
, indio_dev
);
244 static const struct of_device_id max34408_of_match
[] = {
246 .compatible
= "maxim,max34408",
247 .data
= &max34408_model_data
,
250 .compatible
= "maxim,max34409",
251 .data
= &max34409_model_data
,
255 MODULE_DEVICE_TABLE(of
, max34408_of_match
);
257 static const struct i2c_device_id max34408_id
[] = {
258 { "max34408", (kernel_ulong_t
)&max34408_model_data
},
259 { "max34409", (kernel_ulong_t
)&max34409_model_data
},
262 MODULE_DEVICE_TABLE(i2c
, max34408_id
);
264 static struct i2c_driver max34408_driver
= {
267 .of_match_table
= max34408_of_match
,
269 .probe
= max34408_probe
,
270 .id_table
= max34408_id
,
272 module_i2c_driver(max34408_driver
);
274 MODULE_AUTHOR("Ivan Mikhaylov <fr0st61te@gmail.com>");
275 MODULE_DESCRIPTION("Maxim MAX34408/34409 ADC driver");
276 MODULE_LICENSE("GPL");