1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * MP2629 Driver for ADC
5 * Copyright 2020 Monolithic Power Systems, Inc
7 * Author: Saravanan Sekar <sravanhome@gmail.com>
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>
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
26 #define MP2629_ADC_START BIT(7)
27 #define MP2629_ADC_CONTINUOUS BIT(6)
29 #define MP2629_MAP(_mp, _mpc) IIO_MAP(#_mp, "mp2629_charger", "mp2629-"_mpc)
31 #define MP2629_ADC_CHAN(_ch, _type) { \
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), \
42 struct regmap
*regmap
;
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
)
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")
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
)
66 struct mp2629_adc
*info
= iio_priv(indio_dev
);
71 case IIO_CHAN_INFO_RAW
:
72 ret
= regmap_read(info
->regmap
, chan
->address
, &rval
);
76 if (chan
->address
== MP2629_INPUT_VOLT
)
77 rval
&= GENMASK(6, 0);
81 case IIO_CHAN_INFO_SCALE
:
82 switch (chan
->channel
) {
83 case MP2629_BATT_VOLT
:
84 case MP2629_SYSTEM_VOLT
:
88 case MP2629_INPUT_VOLT
:
92 case MP2629_BATT_CURRENT
:
95 return IIO_VAL_FRACTIONAL
;
97 case MP2629_INPUT_CURRENT
:
100 return IIO_VAL_FRACTIONAL
;
111 static const struct iio_info mp2629_adc_info
= {
112 .read_raw
= &mp2629_read_raw
,
115 static int mp2629_adc_probe(struct platform_device
*pdev
)
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
;
123 indio_dev
= devm_iio_device_alloc(dev
, sizeof(*info
));
127 info
= iio_priv(indio_dev
);
128 info
->regmap
= ddata
->regmap
;
130 platform_set_drvdata(pdev
, indio_dev
);
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
);
136 dev_err(dev
, "adc enable fail: %d\n", ret
);
140 ret
= iio_map_array_register(indio_dev
, mp2629_adc_maps
);
142 dev_err(dev
, "IIO maps register fail: %d\n", ret
);
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
;
153 ret
= iio_device_register(indio_dev
);
155 dev_err(dev
, "IIO device register fail: %d\n", ret
);
156 goto fail_map_unregister
;
162 iio_map_array_unregister(indio_dev
);
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);
173 static int mp2629_adc_remove(struct platform_device
*pdev
)
175 struct iio_dev
*indio_dev
= platform_get_drvdata(pdev
);
176 struct mp2629_adc
*info
= iio_priv(indio_dev
);
178 iio_device_unregister(indio_dev
);
180 iio_map_array_unregister(indio_dev
);
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);
190 static const struct of_device_id mp2629_adc_of_match
[] = {
191 { .compatible
= "mps,mp2629_adc"},
194 MODULE_DEVICE_TABLE(of
, mp2629_adc_of_match
);
196 static struct platform_driver mp2629_adc_driver
= {
198 .name
= "mp2629_adc",
199 .of_match_table
= mp2629_adc_of_match
,
201 .probe
= mp2629_adc_probe
,
202 .remove
= mp2629_adc_remove
,
204 module_platform_driver(mp2629_adc_driver
);
206 MODULE_AUTHOR("Saravanan Sekar <sravanhome@gmail.com>");
207 MODULE_DESCRIPTION("MP2629 ADC driver");
208 MODULE_LICENSE("GPL");