1 // SPDX-License-Identifier: GPL-2.0
3 * Generic battery driver using IIO
4 * Copyright (C) 2012, Anish Kumar <yesanishhere@gmail.com>
5 * Copyright (c) 2023, Sebastian Reichel <sre@kernel.org>
7 #include <linux/interrupt.h>
8 #include <linux/platform_device.h>
9 #include <linux/power_supply.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/err.h>
12 #include <linux/timer.h>
13 #include <linux/jiffies.h>
14 #include <linux/errno.h>
15 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/slab.h>
18 #include <linux/iio/consumer.h>
19 #include <linux/iio/types.h>
21 #include <linux/devm-helpers.h>
23 #define JITTER_DEFAULT 10 /* hope 10ms is enough */
34 * gab_chan_name suggests the standard channel names for commonly used
37 static const char *const gab_chan_name
[] = {
38 [GAB_VOLTAGE
] = "voltage",
39 [GAB_CURRENT
] = "current",
40 [GAB_POWER
] = "power",
41 [GAB_TEMP
] = "temperature",
45 struct power_supply
*psy
;
46 struct power_supply_desc psy_desc
;
47 struct iio_channel
*channel
[GAB_MAX_CHAN_TYPE
];
48 struct delayed_work bat_work
;
50 struct gpio_desc
*charge_finished
;
53 static struct gab
*to_generic_bat(struct power_supply
*psy
)
55 return power_supply_get_drvdata(psy
);
58 static void gab_ext_power_changed(struct power_supply
*psy
)
60 struct gab
*adc_bat
= to_generic_bat(psy
);
62 schedule_delayed_work(&adc_bat
->bat_work
, msecs_to_jiffies(0));
65 static const enum power_supply_property gab_props
[] = {
66 POWER_SUPPLY_PROP_STATUS
,
70 * This properties are set based on the received platform data and this
71 * should correspond one-to-one with enum chan_type.
73 static const enum power_supply_property gab_dyn_props
[] = {
74 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
75 POWER_SUPPLY_PROP_CURRENT_NOW
,
76 POWER_SUPPLY_PROP_POWER_NOW
,
77 POWER_SUPPLY_PROP_TEMP
,
80 static bool gab_charge_finished(struct gab
*adc_bat
)
82 if (!adc_bat
->charge_finished
)
84 return gpiod_get_value(adc_bat
->charge_finished
);
87 static int gab_read_channel(struct gab
*adc_bat
, enum gab_chan_type channel
,
92 ret
= iio_read_channel_processed(adc_bat
->channel
[channel
], result
);
94 dev_err(&adc_bat
->psy
->dev
, "read channel error: %d\n", ret
);
101 static int gab_get_property(struct power_supply
*psy
,
102 enum power_supply_property psp
, union power_supply_propval
*val
)
104 struct gab
*adc_bat
= to_generic_bat(psy
);
107 case POWER_SUPPLY_PROP_STATUS
:
108 val
->intval
= adc_bat
->status
;
110 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
111 return gab_read_channel(adc_bat
, GAB_VOLTAGE
, &val
->intval
);
112 case POWER_SUPPLY_PROP_CURRENT_NOW
:
113 return gab_read_channel(adc_bat
, GAB_CURRENT
, &val
->intval
);
114 case POWER_SUPPLY_PROP_POWER_NOW
:
115 return gab_read_channel(adc_bat
, GAB_POWER
, &val
->intval
);
116 case POWER_SUPPLY_PROP_TEMP
:
117 return gab_read_channel(adc_bat
, GAB_TEMP
, &val
->intval
);
123 static void gab_work(struct work_struct
*work
)
126 struct delayed_work
*delayed_work
;
129 delayed_work
= to_delayed_work(work
);
130 adc_bat
= container_of(delayed_work
, struct gab
, bat_work
);
131 status
= adc_bat
->status
;
133 if (!power_supply_am_i_supplied(adc_bat
->psy
))
134 adc_bat
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
135 else if (gab_charge_finished(adc_bat
))
136 adc_bat
->status
= POWER_SUPPLY_STATUS_NOT_CHARGING
;
138 adc_bat
->status
= POWER_SUPPLY_STATUS_CHARGING
;
140 if (status
!= adc_bat
->status
)
141 power_supply_changed(adc_bat
->psy
);
144 static irqreturn_t
gab_charged(int irq
, void *dev_id
)
146 struct gab
*adc_bat
= dev_id
;
148 schedule_delayed_work(&adc_bat
->bat_work
,
149 msecs_to_jiffies(JITTER_DEFAULT
));
154 static int gab_probe(struct platform_device
*pdev
)
157 struct power_supply_desc
*psy_desc
;
158 struct power_supply_config psy_cfg
= {};
159 enum power_supply_property
*properties
;
162 int index
= ARRAY_SIZE(gab_props
);
165 adc_bat
= devm_kzalloc(&pdev
->dev
, sizeof(*adc_bat
), GFP_KERNEL
);
169 psy_cfg
.of_node
= pdev
->dev
.of_node
;
170 psy_cfg
.drv_data
= adc_bat
;
171 psy_desc
= &adc_bat
->psy_desc
;
172 psy_desc
->name
= dev_name(&pdev
->dev
);
174 /* bootup default values for the battery */
175 adc_bat
->status
= POWER_SUPPLY_STATUS_DISCHARGING
;
176 psy_desc
->type
= POWER_SUPPLY_TYPE_BATTERY
;
177 psy_desc
->get_property
= gab_get_property
;
178 psy_desc
->external_power_changed
= gab_ext_power_changed
;
181 * copying the static properties and allocating extra memory for holding
182 * the extra configurable properties received from platform data.
184 properties
= devm_kcalloc(&pdev
->dev
,
185 ARRAY_SIZE(gab_props
) +
186 ARRAY_SIZE(gab_chan_name
),
192 memcpy(properties
, gab_props
, sizeof(gab_props
));
195 * getting channel from iio and copying the battery properties
196 * based on the channel supported by consumer device.
198 for (chan
= 0; chan
< ARRAY_SIZE(gab_chan_name
); chan
++) {
199 adc_bat
->channel
[chan
] = devm_iio_channel_get(&pdev
->dev
, gab_chan_name
[chan
]);
200 if (IS_ERR(adc_bat
->channel
[chan
])) {
201 ret
= PTR_ERR(adc_bat
->channel
[chan
]);
203 return dev_err_probe(&pdev
->dev
, ret
, "Failed to get ADC channel %s\n", gab_chan_name
[chan
]);
204 adc_bat
->channel
[chan
] = NULL
;
205 } else if (adc_bat
->channel
[chan
]) {
206 /* copying properties for supported channels only */
209 for (index2
= 0; index2
< index
; index2
++) {
210 if (properties
[index2
] == gab_dyn_props
[chan
])
211 break; /* already known */
213 if (index2
== index
) /* really new */
214 properties
[index
++] = gab_dyn_props
[chan
];
219 /* none of the channels are supported so let's bail out */
221 return dev_err_probe(&pdev
->dev
, -ENODEV
, "Failed to get any ADC channel\n");
224 * Total number of properties is equal to static properties
225 * plus the dynamic properties.Some properties may not be set
226 * as come channels may be not be supported by the device.So
227 * we need to take care of that.
229 psy_desc
->properties
= properties
;
230 psy_desc
->num_properties
= index
;
232 adc_bat
->psy
= devm_power_supply_register(&pdev
->dev
, psy_desc
, &psy_cfg
);
233 if (IS_ERR(adc_bat
->psy
))
234 return dev_err_probe(&pdev
->dev
, PTR_ERR(adc_bat
->psy
), "Failed to register power-supply device\n");
236 ret
= devm_delayed_work_autocancel(&pdev
->dev
, &adc_bat
->bat_work
, gab_work
);
238 return dev_err_probe(&pdev
->dev
, ret
, "Failed to register delayed work\n");
240 adc_bat
->charge_finished
= devm_gpiod_get_optional(&pdev
->dev
, "charged", GPIOD_IN
);
241 if (adc_bat
->charge_finished
) {
244 irq
= gpiod_to_irq(adc_bat
->charge_finished
);
245 ret
= devm_request_any_context_irq(&pdev
->dev
, irq
, gab_charged
,
246 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
,
247 "battery charged", adc_bat
);
249 return dev_err_probe(&pdev
->dev
, ret
, "Failed to register irq\n");
252 platform_set_drvdata(pdev
, adc_bat
);
254 /* Schedule timer to check current status */
255 schedule_delayed_work(&adc_bat
->bat_work
,
256 msecs_to_jiffies(0));
260 static int __maybe_unused
gab_suspend(struct device
*dev
)
262 struct gab
*adc_bat
= dev_get_drvdata(dev
);
264 cancel_delayed_work_sync(&adc_bat
->bat_work
);
265 adc_bat
->status
= POWER_SUPPLY_STATUS_UNKNOWN
;
269 static int __maybe_unused
gab_resume(struct device
*dev
)
271 struct gab
*adc_bat
= dev_get_drvdata(dev
);
273 /* Schedule timer to check current status */
274 schedule_delayed_work(&adc_bat
->bat_work
,
275 msecs_to_jiffies(JITTER_DEFAULT
));
280 static SIMPLE_DEV_PM_OPS(gab_pm_ops
, gab_suspend
, gab_resume
);
282 static const struct of_device_id gab_match
[] = {
283 { .compatible
= "adc-battery" },
286 MODULE_DEVICE_TABLE(of
, gab_match
);
288 static struct platform_driver gab_driver
= {
290 .name
= "generic-adc-battery",
292 .of_match_table
= gab_match
,
296 module_platform_driver(gab_driver
);
298 MODULE_AUTHOR("anish kumar <yesanishhere@gmail.com>");
299 MODULE_DESCRIPTION("generic battery driver using IIO");
300 MODULE_LICENSE("GPL");