summaryrefslogtreecommitdiff
path: root/drivers/iio/adc/max77541-adc.c
blob: 21d024bde16ba00332ee7ae77f87425f169dec20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 * Copyright (c) 2022 Analog Devices, Inc.
 * ADI MAX77541 ADC Driver with IIO interface
 */

#include <linux/bitfield.h>
#include <linux/iio/iio.h>
#include <linux/mod_devicetable.h>
#include <linux/platform_device.h>
#include <linux/regmap.h>
#include <linux/units.h>

#include <linux/mfd/max77541.h>

enum max77541_adc_range {
	LOW_RANGE,
	MID_RANGE,
	HIGH_RANGE,
};

enum max77541_adc_channel {
	MAX77541_ADC_VSYS_V,
	MAX77541_ADC_VOUT1_V,
	MAX77541_ADC_VOUT2_V,
	MAX77541_ADC_TEMP,
};

static int max77541_adc_offset(struct iio_dev *indio_dev,
			       struct iio_chan_spec const *chan,
			       int *val, int *val2)
{
	switch (chan->channel) {
	case MAX77541_ADC_TEMP:
		*val = DIV_ROUND_CLOSEST(ABSOLUTE_ZERO_MILLICELSIUS, 1725);
		return IIO_VAL_INT;
	default:
		return -EINVAL;
	}
}

static int max77541_adc_scale(struct iio_dev *indio_dev,
			      struct iio_chan_spec const *chan,
			      int *val, int *val2)
{
	struct regmap **regmap = iio_priv(indio_dev);
	unsigned int reg_val;
	int ret;

	switch (chan->channel) {
	case MAX77541_ADC_VSYS_V:
		*val = 25;
		return IIO_VAL_INT;
	case MAX77541_ADC_VOUT1_V:
	case MAX77541_ADC_VOUT2_V:
		ret = regmap_read(*regmap, MAX77541_REG_M2_CFG1, &reg_val);
		if (ret)
			return ret;

		reg_val = FIELD_GET(MAX77541_BITS_MX_CFG1_RNG, reg_val);
		switch (reg_val) {
		case LOW_RANGE:
			*val = 6;
			*val2 = 250000;
			break;
		case MID_RANGE:
			*val = 12;
			*val2 = 500000;
			break;
		case HIGH_RANGE:
			*val = 25;
			return IIO_VAL_INT;
		default:
			return -EINVAL;
		}

		return IIO_VAL_INT_PLUS_MICRO;
	case MAX77541_ADC_TEMP:
		*val = 1725;
		return IIO_VAL_INT;
	default:
		return -EINVAL;
	}
}

static int max77541_adc_raw(struct iio_dev *indio_dev,
			    struct iio_chan_spec const *chan,
			    int *val)
{
	struct regmap **regmap = iio_priv(indio_dev);
	int ret;

	ret = regmap_read(*regmap, chan->address, val);
	if (ret)
		return ret;

	return IIO_VAL_INT;
}

#define MAX77541_ADC_CHANNEL_V(_channel, _name, _type, _reg) \
	{							\
		.type = _type,					\
		.indexed = 1,					\
		.channel = _channel,				\
		.address = _reg,				\
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
				      BIT(IIO_CHAN_INFO_SCALE), \
		.datasheet_name = _name,			\
	}

#define MAX77541_ADC_CHANNEL_TEMP(_channel, _name, _type, _reg) \
	{							\
		.type = _type,					\
		.indexed = 1,					\
		.channel = _channel,				\
		.address = _reg,				\
		.info_mask_separate = BIT(IIO_CHAN_INFO_RAW) |	\
				      BIT(IIO_CHAN_INFO_SCALE) |\
				      BIT(IIO_CHAN_INFO_OFFSET),\
		.datasheet_name = _name,			\
	}

static const struct iio_chan_spec max77541_adc_channels[] = {
	MAX77541_ADC_CHANNEL_V(MAX77541_ADC_VSYS_V, "vsys_v", IIO_VOLTAGE,
			       MAX77541_REG_ADC_DATA_CH1),
	MAX77541_ADC_CHANNEL_V(MAX77541_ADC_VOUT1_V, "vout1_v", IIO_VOLTAGE,
			       MAX77541_REG_ADC_DATA_CH2),
	MAX77541_ADC_CHANNEL_V(MAX77541_ADC_VOUT2_V, "vout2_v", IIO_VOLTAGE,
			       MAX77541_REG_ADC_DATA_CH3),
	MAX77541_ADC_CHANNEL_TEMP(MAX77541_ADC_TEMP, "temp", IIO_TEMP,
				  MAX77541_REG_ADC_DATA_CH6),
};

static int max77541_adc_read_raw(struct iio_dev *indio_dev,
				 struct iio_chan_spec const *chan,
				 int *val, int *val2, long mask)
{
	switch (mask) {
	case IIO_CHAN_INFO_OFFSET:
		return max77541_adc_offset(indio_dev, chan, val, val2);
	case IIO_CHAN_INFO_SCALE:
		return max77541_adc_scale(indio_dev, chan, val, val2);
	case IIO_CHAN_INFO_RAW:
		return max77541_adc_raw(indio_dev, chan, val);
	default:
		return -EINVAL;
	}
}

static const struct iio_info max77541_adc_info = {
	.read_raw = max77541_adc_read_raw,
};

static int max77541_adc_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct iio_dev *indio_dev;
	struct regmap **regmap;

	indio_dev = devm_iio_device_alloc(dev, sizeof(*regmap));
	if (!indio_dev)
		return -ENOMEM;

	regmap = iio_priv(indio_dev);

	*regmap = dev_get_regmap(dev->parent, NULL);
	indio_dev->modes = INDIO_DIRECT_MODE;

	indio_dev->name = "max77541";
	indio_dev->info = &max77541_adc_info;
	indio_dev->channels = max77541_adc_channels;
	indio_dev->num_channels = ARRAY_SIZE(max77541_adc_channels);

	return devm_iio_device_register(dev, indio_dev);
}

static const struct platform_device_id max77541_adc_platform_id[] = {
	{ "max77541-adc" },
	{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(platform, max77541_adc_platform_id);

static struct platform_driver max77541_adc_driver = {
	.driver = {
		.name = "max77541-adc",
	},
	.probe = max77541_adc_probe,
	.id_table = max77541_adc_platform_id,
};
module_platform_driver(max77541_adc_driver);

MODULE_AUTHOR("Okan Sahin <Okan.Sahin@analog.com>");
MODULE_DESCRIPTION("MAX77541 ADC driver");
MODULE_LICENSE("GPL");