xref: /openbmc/linux/drivers/iio/adc/mp2629_adc.c (revision ca1547ab)
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 
mp2629_read_raw(struct iio_dev * indio_dev,struct iio_chan_spec const * chan,int * val,int * val2,long mask)64 static int mp2629_read_raw(struct iio_dev *indio_dev,
65 			struct iio_chan_spec const *chan,
66 			int *val, int *val2, long mask)
67 {
68 	struct mp2629_adc *info = iio_priv(indio_dev);
69 	unsigned int rval;
70 	int ret;
71 
72 	switch (mask) {
73 	case IIO_CHAN_INFO_RAW:
74 		ret = regmap_read(info->regmap, chan->address, &rval);
75 		if (ret)
76 			return ret;
77 
78 		if (chan->channel == MP2629_INPUT_VOLT)
79 			rval &= GENMASK(6, 0);
80 		*val = rval;
81 		return IIO_VAL_INT;
82 
83 	case IIO_CHAN_INFO_SCALE:
84 		switch (chan->channel) {
85 		case MP2629_BATT_VOLT:
86 		case MP2629_SYSTEM_VOLT:
87 			*val = 20;
88 			return IIO_VAL_INT;
89 
90 		case MP2629_INPUT_VOLT:
91 			*val = 60;
92 			return IIO_VAL_INT;
93 
94 		case MP2629_BATT_CURRENT:
95 			*val = 175;
96 			*val2 = 10;
97 			return IIO_VAL_FRACTIONAL;
98 
99 		case MP2629_INPUT_CURRENT:
100 			*val = 133;
101 			*val2 = 10;
102 			return IIO_VAL_FRACTIONAL;
103 
104 		default:
105 			return -EINVAL;
106 		}
107 
108 	default:
109 		return -EINVAL;
110 	}
111 }
112 
113 static const struct iio_info mp2629_adc_info = {
114 	.read_raw = &mp2629_read_raw,
115 };
116 
mp2629_adc_probe(struct platform_device * pdev)117 static int mp2629_adc_probe(struct platform_device *pdev)
118 {
119 	struct device *dev = &pdev->dev;
120 	struct mp2629_data *ddata = dev_get_drvdata(dev->parent);
121 	struct mp2629_adc *info;
122 	struct iio_dev *indio_dev;
123 	int ret;
124 
125 	indio_dev = devm_iio_device_alloc(dev, sizeof(*info));
126 	if (!indio_dev)
127 		return -ENOMEM;
128 
129 	info = iio_priv(indio_dev);
130 	info->regmap = ddata->regmap;
131 	info->dev = dev;
132 	platform_set_drvdata(pdev, indio_dev);
133 
134 	ret = regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL,
135 				MP2629_ADC_START | MP2629_ADC_CONTINUOUS,
136 				MP2629_ADC_START | MP2629_ADC_CONTINUOUS);
137 	if (ret) {
138 		dev_err(dev, "adc enable fail: %d\n", ret);
139 		return ret;
140 	}
141 
142 	ret = iio_map_array_register(indio_dev, mp2629_adc_maps);
143 	if (ret) {
144 		dev_err(dev, "IIO maps register fail: %d\n", ret);
145 		goto fail_disable;
146 	}
147 
148 	indio_dev->name = "mp2629-adc";
149 	indio_dev->channels = mp2629_channels;
150 	indio_dev->num_channels = ARRAY_SIZE(mp2629_channels);
151 	indio_dev->modes = INDIO_DIRECT_MODE;
152 	indio_dev->info = &mp2629_adc_info;
153 
154 	ret = iio_device_register(indio_dev);
155 	if (ret) {
156 		dev_err(dev, "IIO device register fail: %d\n", ret);
157 		goto fail_map_unregister;
158 	}
159 
160 	return 0;
161 
162 fail_map_unregister:
163 	iio_map_array_unregister(indio_dev);
164 
165 fail_disable:
166 	regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL,
167 					 MP2629_ADC_CONTINUOUS, 0);
168 	regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL,
169 					 MP2629_ADC_START, 0);
170 
171 	return ret;
172 }
173 
mp2629_adc_remove(struct platform_device * pdev)174 static int mp2629_adc_remove(struct platform_device *pdev)
175 {
176 	struct iio_dev *indio_dev = platform_get_drvdata(pdev);
177 	struct mp2629_adc *info = iio_priv(indio_dev);
178 
179 	iio_device_unregister(indio_dev);
180 
181 	iio_map_array_unregister(indio_dev);
182 
183 	regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL,
184 					 MP2629_ADC_CONTINUOUS, 0);
185 	regmap_update_bits(info->regmap, MP2629_REG_ADC_CTRL,
186 					 MP2629_ADC_START, 0);
187 
188 	return 0;
189 }
190 
191 static const struct of_device_id mp2629_adc_of_match[] = {
192 	{ .compatible = "mps,mp2629_adc"},
193 	{}
194 };
195 MODULE_DEVICE_TABLE(of, mp2629_adc_of_match);
196 
197 static struct platform_driver mp2629_adc_driver = {
198 	.driver = {
199 		.name = "mp2629_adc",
200 		.of_match_table = mp2629_adc_of_match,
201 	},
202 	.probe		= mp2629_adc_probe,
203 	.remove		= mp2629_adc_remove,
204 };
205 module_platform_driver(mp2629_adc_driver);
206 
207 MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
208 MODULE_DESCRIPTION("MP2629 ADC driver");
209 MODULE_LICENSE("GPL");
210