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_set_bits(info->regmap, MP2629_REG_ADC_CTRL,
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_clear_bits(info->regmap, MP2629_REG_ADC_CTRL,
166 MP2629_ADC_CONTINUOUS);
167 regmap_clear_bits(info->regmap, MP2629_REG_ADC_CTRL, MP2629_ADC_START);
168
169 return ret;
170 }
171
mp2629_adc_remove(struct platform_device * pdev)172 static void 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_clear_bits(info->regmap, MP2629_REG_ADC_CTRL,
182 MP2629_ADC_CONTINUOUS);
183 regmap_clear_bits(info->regmap, MP2629_REG_ADC_CTRL, MP2629_ADC_START);
184 }
185
186 static const struct of_device_id mp2629_adc_of_match[] = {
187 { .compatible = "mps,mp2629_adc" },
188 { }
189 };
190 MODULE_DEVICE_TABLE(of, mp2629_adc_of_match);
191
192 static struct platform_driver mp2629_adc_driver = {
193 .driver = {
194 .name = "mp2629_adc",
195 .of_match_table = mp2629_adc_of_match,
196 },
197 .probe = mp2629_adc_probe,
198 .remove_new = mp2629_adc_remove,
199 };
200 module_platform_driver(mp2629_adc_driver);
201
202 MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
203 MODULE_DESCRIPTION("MP2629 ADC driver");
204 MODULE_LICENSE("GPL");
205