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