Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / power / supply / s3c_adc_battery.c
bloba2addc24ee8b83b8448c7998267ef0a35b1a93fa
1 /*
2 * iPAQ h1930/h1940/rx1950 battery controller driver
3 * Copyright (c) Vasily Khoruzhick
4 * Based on h1940_battery.c by Arnaud Patard
6 * This file is subject to the terms and conditions of the GNU General Public
7 * License. See the file COPYING in the main directory of this archive for
8 * more details.
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/power_supply.h>
15 #include <linux/leds.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/err.h>
18 #include <linux/timer.h>
19 #include <linux/jiffies.h>
20 #include <linux/s3c_adc_battery.h>
21 #include <linux/errno.h>
22 #include <linux/init.h>
23 #include <linux/module.h>
25 #include <linux/soc/samsung/s3c-adc.h>
27 #define BAT_POLL_INTERVAL 10000 /* ms */
28 #define JITTER_DELAY 500 /* ms */
30 struct s3c_adc_bat {
31 struct power_supply *psy;
32 struct s3c_adc_client *client;
33 struct s3c_adc_bat_pdata *pdata;
34 struct gpio_desc *charge_finished;
35 int volt_value;
36 int cur_value;
37 unsigned int timestamp;
38 int level;
39 int status;
40 int cable_plugged:1;
43 static struct delayed_work bat_work;
45 static void s3c_adc_bat_ext_power_changed(struct power_supply *psy)
47 schedule_delayed_work(&bat_work,
48 msecs_to_jiffies(JITTER_DELAY));
51 static int gather_samples(struct s3c_adc_client *client, int num, int channel)
53 int value, i;
55 /* default to 1 if nothing is set */
56 if (num < 1)
57 num = 1;
59 value = 0;
60 for (i = 0; i < num; i++)
61 value += s3c_adc_read(client, channel);
62 value /= num;
64 return value;
67 static enum power_supply_property s3c_adc_backup_bat_props[] = {
68 POWER_SUPPLY_PROP_VOLTAGE_NOW,
69 POWER_SUPPLY_PROP_VOLTAGE_MIN,
70 POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
73 static int s3c_adc_backup_bat_get_property(struct power_supply *psy,
74 enum power_supply_property psp,
75 union power_supply_propval *val)
77 struct s3c_adc_bat *bat = power_supply_get_drvdata(psy);
79 if (!bat) {
80 dev_err(&psy->dev, "%s: no battery infos ?!\n", __func__);
81 return -EINVAL;
84 if (bat->volt_value < 0 ||
85 jiffies_to_msecs(jiffies - bat->timestamp) >
86 BAT_POLL_INTERVAL) {
87 bat->volt_value = gather_samples(bat->client,
88 bat->pdata->backup_volt_samples,
89 bat->pdata->backup_volt_channel);
90 bat->volt_value *= bat->pdata->backup_volt_mult;
91 bat->timestamp = jiffies;
94 switch (psp) {
95 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
96 val->intval = bat->volt_value;
97 return 0;
98 case POWER_SUPPLY_PROP_VOLTAGE_MIN:
99 val->intval = bat->pdata->backup_volt_min;
100 return 0;
101 case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
102 val->intval = bat->pdata->backup_volt_max;
103 return 0;
104 default:
105 return -EINVAL;
109 static const struct power_supply_desc backup_bat_desc = {
110 .name = "backup-battery",
111 .type = POWER_SUPPLY_TYPE_BATTERY,
112 .properties = s3c_adc_backup_bat_props,
113 .num_properties = ARRAY_SIZE(s3c_adc_backup_bat_props),
114 .get_property = s3c_adc_backup_bat_get_property,
115 .use_for_apm = 1,
118 static struct s3c_adc_bat backup_bat;
120 static enum power_supply_property s3c_adc_main_bat_props[] = {
121 POWER_SUPPLY_PROP_STATUS,
122 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
123 POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN,
124 POWER_SUPPLY_PROP_CHARGE_NOW,
125 POWER_SUPPLY_PROP_VOLTAGE_NOW,
126 POWER_SUPPLY_PROP_CURRENT_NOW,
129 static int calc_full_volt(int volt_val, int cur_val, int impedance)
131 return volt_val + cur_val * impedance / 1000;
134 static int charge_finished(struct s3c_adc_bat *bat)
136 return gpiod_get_value(bat->charge_finished);
139 static int s3c_adc_bat_get_property(struct power_supply *psy,
140 enum power_supply_property psp,
141 union power_supply_propval *val)
143 struct s3c_adc_bat *bat = power_supply_get_drvdata(psy);
145 int new_level;
146 int full_volt;
147 const struct s3c_adc_bat_thresh *lut;
148 unsigned int lut_size;
150 if (!bat) {
151 dev_err(&psy->dev, "no battery infos ?!\n");
152 return -EINVAL;
155 lut = bat->pdata->lut_noac;
156 lut_size = bat->pdata->lut_noac_cnt;
158 if (bat->volt_value < 0 || bat->cur_value < 0 ||
159 jiffies_to_msecs(jiffies - bat->timestamp) >
160 BAT_POLL_INTERVAL) {
161 bat->volt_value = gather_samples(bat->client,
162 bat->pdata->volt_samples,
163 bat->pdata->volt_channel) * bat->pdata->volt_mult;
164 bat->cur_value = gather_samples(bat->client,
165 bat->pdata->current_samples,
166 bat->pdata->current_channel) * bat->pdata->current_mult;
167 bat->timestamp = jiffies;
170 if (bat->cable_plugged &&
171 (!bat->charge_finished ||
172 !charge_finished(bat))) {
173 lut = bat->pdata->lut_acin;
174 lut_size = bat->pdata->lut_acin_cnt;
177 new_level = 100000;
178 full_volt = calc_full_volt((bat->volt_value / 1000),
179 (bat->cur_value / 1000), bat->pdata->internal_impedance);
181 if (full_volt < calc_full_volt(lut->volt, lut->cur,
182 bat->pdata->internal_impedance)) {
183 lut_size--;
184 while (lut_size--) {
185 int lut_volt1;
186 int lut_volt2;
188 lut_volt1 = calc_full_volt(lut[0].volt, lut[0].cur,
189 bat->pdata->internal_impedance);
190 lut_volt2 = calc_full_volt(lut[1].volt, lut[1].cur,
191 bat->pdata->internal_impedance);
192 if (full_volt < lut_volt1 && full_volt >= lut_volt2) {
193 new_level = (lut[1].level +
194 (lut[0].level - lut[1].level) *
195 (full_volt - lut_volt2) /
196 (lut_volt1 - lut_volt2)) * 1000;
197 break;
199 new_level = lut[1].level * 1000;
200 lut++;
204 bat->level = new_level;
206 switch (psp) {
207 case POWER_SUPPLY_PROP_STATUS:
208 if (!bat->charge_finished)
209 val->intval = bat->level == 100000 ?
210 POWER_SUPPLY_STATUS_FULL : bat->status;
211 else
212 val->intval = bat->status;
213 return 0;
214 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
215 val->intval = 100000;
216 return 0;
217 case POWER_SUPPLY_PROP_CHARGE_EMPTY_DESIGN:
218 val->intval = 0;
219 return 0;
220 case POWER_SUPPLY_PROP_CHARGE_NOW:
221 val->intval = bat->level;
222 return 0;
223 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
224 val->intval = bat->volt_value;
225 return 0;
226 case POWER_SUPPLY_PROP_CURRENT_NOW:
227 val->intval = bat->cur_value;
228 return 0;
229 default:
230 return -EINVAL;
234 static const struct power_supply_desc main_bat_desc = {
235 .name = "main-battery",
236 .type = POWER_SUPPLY_TYPE_BATTERY,
237 .properties = s3c_adc_main_bat_props,
238 .num_properties = ARRAY_SIZE(s3c_adc_main_bat_props),
239 .get_property = s3c_adc_bat_get_property,
240 .external_power_changed = s3c_adc_bat_ext_power_changed,
241 .use_for_apm = 1,
244 static struct s3c_adc_bat main_bat;
246 static void s3c_adc_bat_work(struct work_struct *work)
248 struct s3c_adc_bat *bat = &main_bat;
249 int is_charged;
250 int is_plugged;
251 static int was_plugged;
253 is_plugged = power_supply_am_i_supplied(bat->psy);
254 bat->cable_plugged = is_plugged;
255 if (is_plugged != was_plugged) {
256 was_plugged = is_plugged;
257 if (is_plugged) {
258 if (bat->pdata->enable_charger)
259 bat->pdata->enable_charger();
260 bat->status = POWER_SUPPLY_STATUS_CHARGING;
261 } else {
262 if (bat->pdata->disable_charger)
263 bat->pdata->disable_charger();
264 bat->status = POWER_SUPPLY_STATUS_DISCHARGING;
266 } else {
267 if (bat->charge_finished && is_plugged) {
268 is_charged = charge_finished(&main_bat);
269 if (is_charged) {
270 if (bat->pdata->disable_charger)
271 bat->pdata->disable_charger();
272 bat->status = POWER_SUPPLY_STATUS_FULL;
273 } else {
274 if (bat->pdata->enable_charger)
275 bat->pdata->enable_charger();
276 bat->status = POWER_SUPPLY_STATUS_CHARGING;
281 power_supply_changed(bat->psy);
284 static irqreturn_t s3c_adc_bat_charged(int irq, void *dev_id)
286 schedule_delayed_work(&bat_work,
287 msecs_to_jiffies(JITTER_DELAY));
288 return IRQ_HANDLED;
291 static int s3c_adc_bat_probe(struct platform_device *pdev)
293 struct s3c_adc_client *client;
294 struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
295 struct power_supply_config psy_cfg = {};
296 struct gpio_desc *gpiod;
297 int ret;
299 client = s3c_adc_register(pdev, NULL, NULL, 0);
300 if (IS_ERR(client)) {
301 dev_err(&pdev->dev, "cannot register adc\n");
302 return PTR_ERR(client);
305 platform_set_drvdata(pdev, client);
307 gpiod = devm_gpiod_get_optional(&pdev->dev, "charge-status", GPIOD_IN);
308 if (IS_ERR(gpiod)) {
309 /* Could be probe deferral etc */
310 ret = PTR_ERR(gpiod);
311 dev_err(&pdev->dev, "no GPIO %d\n", ret);
312 return ret;
315 main_bat.client = client;
316 main_bat.pdata = pdata;
317 main_bat.charge_finished = gpiod;
318 main_bat.volt_value = -1;
319 main_bat.cur_value = -1;
320 main_bat.cable_plugged = 0;
321 main_bat.status = POWER_SUPPLY_STATUS_DISCHARGING;
322 psy_cfg.drv_data = &main_bat;
324 main_bat.psy = power_supply_register(&pdev->dev, &main_bat_desc, &psy_cfg);
325 if (IS_ERR(main_bat.psy)) {
326 ret = PTR_ERR(main_bat.psy);
327 goto err_reg_main;
329 if (pdata->backup_volt_mult) {
330 const struct power_supply_config backup_psy_cfg
331 = { .drv_data = &backup_bat, };
333 backup_bat.client = client;
334 backup_bat.pdata = pdev->dev.platform_data;
335 backup_bat.charge_finished = gpiod;
336 backup_bat.volt_value = -1;
337 backup_bat.psy = power_supply_register(&pdev->dev,
338 &backup_bat_desc,
339 &backup_psy_cfg);
340 if (IS_ERR(backup_bat.psy)) {
341 ret = PTR_ERR(backup_bat.psy);
342 goto err_reg_backup;
346 INIT_DELAYED_WORK(&bat_work, s3c_adc_bat_work);
348 if (gpiod) {
349 ret = request_irq(gpiod_to_irq(gpiod),
350 s3c_adc_bat_charged,
351 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
352 "battery charged", NULL);
353 if (ret)
354 goto err_irq;
357 if (pdata->init) {
358 ret = pdata->init();
359 if (ret)
360 goto err_platform;
363 dev_info(&pdev->dev, "successfully loaded\n");
364 device_init_wakeup(&pdev->dev, 1);
366 /* Schedule timer to check current status */
367 schedule_delayed_work(&bat_work,
368 msecs_to_jiffies(JITTER_DELAY));
370 return 0;
372 err_platform:
373 if (gpiod)
374 free_irq(gpiod_to_irq(gpiod), NULL);
375 err_irq:
376 if (pdata->backup_volt_mult)
377 power_supply_unregister(backup_bat.psy);
378 err_reg_backup:
379 power_supply_unregister(main_bat.psy);
380 err_reg_main:
381 return ret;
384 static int s3c_adc_bat_remove(struct platform_device *pdev)
386 struct s3c_adc_client *client = platform_get_drvdata(pdev);
387 struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
389 power_supply_unregister(main_bat.psy);
390 if (pdata->backup_volt_mult)
391 power_supply_unregister(backup_bat.psy);
393 s3c_adc_release(client);
395 if (main_bat.charge_finished)
396 free_irq(gpiod_to_irq(main_bat.charge_finished), NULL);
398 cancel_delayed_work(&bat_work);
400 if (pdata->exit)
401 pdata->exit();
403 return 0;
406 #ifdef CONFIG_PM
407 static int s3c_adc_bat_suspend(struct platform_device *pdev,
408 pm_message_t state)
410 struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
412 if (main_bat.charge_finished) {
413 if (device_may_wakeup(&pdev->dev))
414 enable_irq_wake(
415 gpiod_to_irq(main_bat.charge_finished));
416 else {
417 disable_irq(gpiod_to_irq(main_bat.charge_finished));
418 main_bat.pdata->disable_charger();
422 return 0;
425 static int s3c_adc_bat_resume(struct platform_device *pdev)
427 struct s3c_adc_bat_pdata *pdata = pdev->dev.platform_data;
429 if (main_bat.charge_finished) {
430 if (device_may_wakeup(&pdev->dev))
431 disable_irq_wake(
432 gpiod_to_irq(main_bat.charge_finished));
433 else
434 enable_irq(gpiod_to_irq(main_bat.charge_finished));
437 /* Schedule timer to check current status */
438 schedule_delayed_work(&bat_work,
439 msecs_to_jiffies(JITTER_DELAY));
441 return 0;
443 #else
444 #define s3c_adc_bat_suspend NULL
445 #define s3c_adc_bat_resume NULL
446 #endif
448 static struct platform_driver s3c_adc_bat_driver = {
449 .driver = {
450 .name = "s3c-adc-battery",
452 .probe = s3c_adc_bat_probe,
453 .remove = s3c_adc_bat_remove,
454 .suspend = s3c_adc_bat_suspend,
455 .resume = s3c_adc_bat_resume,
458 module_platform_driver(s3c_adc_bat_driver);
460 MODULE_AUTHOR("Vasily Khoruzhick <anarsoul@gmail.com>");
461 MODULE_DESCRIPTION("iPAQ H1930/H1940/RX1950 battery controller driver");
462 MODULE_LICENSE("GPL");