xref: /linux/drivers/iio/adc/mp2629_adc.c (revision f7af616c632ee2ac3af0876fe33bf9e0232e665a)
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->dev.parent = dev;
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