xref: /openbmc/linux/drivers/iio/adc/mp2629_adc.c (revision 85250a24)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * MP2629 Driver for ADC
4  *
5  * Copyright 2020 Monolithic Power Systems, Inc
6  *
7  * Author: Saravanan Sekar <sravanhome@gmail.com>
8  */
9 
10 #include <linux/iio/driver.h>
11 #include <linux/iio/iio.h>
12 #include <linux/iio/machine.h>
13 #include <linux/mfd/mp2629.h>
14 #include <linux/mod_devicetable.h>
15 #include <linux/module.h>
16 #include <linux/mutex.h>
17 #include <linux/platform_device.h>
18 #include <linux/regmap.h>
19 
20 #define	MP2629_REG_ADC_CTRL		0x03
21 #define	MP2629_REG_BATT_VOLT		0x0e
22 #define	MP2629_REG_SYSTEM_VOLT		0x0f
23 #define	MP2629_REG_INPUT_VOLT		0x11
24 #define	MP2629_REG_BATT_CURRENT		0x12
25 #define	MP2629_REG_INPUT_CURRENT	0x13
26 
27 #define	MP2629_ADC_START		BIT(7)
28 #define	MP2629_ADC_CONTINUOUS		BIT(6)
29 
30 #define MP2629_MAP(_mp, _mpc) IIO_MAP(#_mp, "mp2629_charger", "mp2629-"_mpc)
31 
32 #define MP2629_ADC_CHAN(_ch, _type) {				\
33 	.type = _type,						\
34 	.indexed = 1,						\
35 	.datasheet_name = #_ch,					\
36 	.channel = MP2629_ ## _ch,				\
37 	.address = MP2629_REG_ ## _ch,				\
38 	.info_mask_separate = BIT(IIO_CHAN_INFO_RAW),		\
39 	.info_mask_shared_by_type = BIT(IIO_CHAN_INFO_SCALE),	\
40 }
41 
42 struct mp2629_adc {
43 	struct regmap *regmap;
44 	struct device *dev;
45 };
46 
47 static struct iio_chan_spec mp2629_channels[] = {
48 	MP2629_ADC_CHAN(BATT_VOLT, IIO_VOLTAGE),
49 	MP2629_ADC_CHAN(SYSTEM_VOLT, IIO_VOLTAGE),
50 	MP2629_ADC_CHAN(INPUT_VOLT, IIO_VOLTAGE),
51 	MP2629_ADC_CHAN(BATT_CURRENT, IIO_CURRENT),
52 	MP2629_ADC_CHAN(INPUT_CURRENT, IIO_CURRENT)
53 };
54 
55 static struct iio_map mp2629_adc_maps[] = {
56 	MP2629_MAP(BATT_VOLT, "batt-volt"),
57 	MP2629_MAP(SYSTEM_VOLT, "system-volt"),
58 	MP2629_MAP(INPUT_VOLT, "input-volt"),
59 	MP2629_MAP(BATT_CURRENT, "batt-current"),
60 	MP2629_MAP(INPUT_CURRENT, "input-current")
61 };
62 
63 static int mp2629_read_raw(struct iio_dev *indio_dev,
64 			struct iio_chan_spec const *chan,
65 			int *val, int *val2, long mask)
66 {
67 	struct mp2629_adc *info = iio_priv(indio_dev);
68 	unsigned int rval;
69 	int ret;
70 
71 	switch (mask) {
72 	case IIO_CHAN_INFO_RAW:
73 		ret = regmap_read(info->regmap, chan->address, &rval);
74 		if (ret)
75 			return ret;
76 
77 		if (chan->address == MP2629_INPUT_VOLT)
78 			rval &= GENMASK(6, 0);
79 		*val = rval;
80 		return IIO_VAL_INT;
81 
82 	case IIO_CHAN_INFO_SCALE:
83 		switch (chan->channel) {
84 		case MP2629_BATT_VOLT:
85 		case MP2629_SYSTEM_VOLT:
86 			*val = 20;
87 			return IIO_VAL_INT;
88 
89 		case MP2629_INPUT_VOLT:
90 			*val = 60;
91 			return IIO_VAL_INT;
92 
93 		case MP2629_BATT_CURRENT:
94 			*val = 175;
95 			*val2 = 10;
96 			return IIO_VAL_FRACTIONAL;
97 
98 		case MP2629_INPUT_CURRENT:
99 			*val = 133;
100 			*val2 = 10;
101 			return IIO_VAL_FRACTIONAL;
102 
103 		default:
104 			return -EINVAL;
105 		}
106 
107 	default:
108 		return -EINVAL;
109 	}
110 }
111 
112 static const struct iio_info mp2629_adc_info = {
113 	.read_raw = &mp2629_read_raw,
114 };
115 
116 static int mp2629_adc_probe(struct platform_device *pdev)
117 {
118 	struct device *dev = &pdev->dev;
119 	struct mp2629_data *ddata = dev_get_drvdata(dev->parent);
120 	struct mp2629_adc *info;
121 	struct iio_dev *indio_dev;
122 	int ret;
123 
124 	indio_dev = devm_iio_device_alloc(dev, sizeof(*info));
125 	if (!indio_dev)
126 		return -ENOMEM;
127 
128 	info = iio_priv(indio_dev);
129 	info->regmap = ddata->regmap;
130 	info->dev = dev;
131 	platform_set_drvdata(pdev, indio_dev);
132 
133 	ret = regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL,
134 				MP2629_ADC_START | MP2629_ADC_CONTINUOUS,
135 				MP2629_ADC_START | MP2629_ADC_CONTINUOUS);
136 	if (ret) {
137 		dev_err(dev, "adc enable fail: %d\n", ret);
138 		return ret;
139 	}
140 
141 	ret = iio_map_array_register(indio_dev, mp2629_adc_maps);
142 	if (ret) {
143 		dev_err(dev, "IIO maps register fail: %d\n", ret);
144 		goto fail_disable;
145 	}
146 
147 	indio_dev->name = "mp2629-adc";
148 	indio_dev->channels = mp2629_channels;
149 	indio_dev->num_channels = ARRAY_SIZE(mp2629_channels);
150 	indio_dev->modes = INDIO_DIRECT_MODE;
151 	indio_dev->info = &mp2629_adc_info;
152 
153 	ret = iio_device_register(indio_dev);
154 	if (ret) {
155 		dev_err(dev, "IIO device register fail: %d\n", ret);
156 		goto fail_map_unregister;
157 	}
158 
159 	return 0;
160 
161 fail_map_unregister:
162 	iio_map_array_unregister(indio_dev);
163 
164 fail_disable:
165 	regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL,
166 					 MP2629_ADC_CONTINUOUS, 0);
167 	regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL,
168 					 MP2629_ADC_START, 0);
169 
170 	return ret;
171 }
172 
173 static int mp2629_adc_remove(struct platform_device *pdev)
174 {
175 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
176 	struct mp2629_adc *info = iio_priv(indio_dev);
177 
178 	iio_device_unregister(indio_dev);
179 
180 	iio_map_array_unregister(indio_dev);
181 
182 	regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL,
183 					 MP2629_ADC_CONTINUOUS, 0);
184 	regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL,
185 					 MP2629_ADC_START, 0);
186 
187 	return 0;
188 }
189 
190 static const struct of_device_id mp2629_adc_of_match[] = {
191 	{ .compatible = "mps,mp2629_adc"},
192 	{}
193 };
194 MODULE_DEVICE_TABLE(of, mp2629_adc_of_match);
195 
196 static struct platform_driver mp2629_adc_driver = {
197 	.driver = {
198 		.name = "mp2629_adc",
199 		.of_match_table = mp2629_adc_of_match,
200 	},
201 	.probe		= mp2629_adc_probe,
202 	.remove		= mp2629_adc_remove,
203 };
204 module_platform_driver(mp2629_adc_driver);
205 
206 MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
207 MODULE_DESCRIPTION("MP2629 ADC driver");
208 MODULE_LICENSE("GPL");
209