Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / power / supply / rn5t618_power.c
blobdee520f0fdf5532cb0bb9324ef234afa470b778f
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * Power supply driver for the RICOH RN5T618 power management chip family
5 * Copyright (C) 2020 Andreas Kemnade
6 */
8 #include <linux/kernel.h>
9 #include <linux/device.h>
10 #include <linux/bitops.h>
11 #include <linux/errno.h>
12 #include <linux/init.h>
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/mfd/rn5t618.h>
16 #include <linux/platform_device.h>
17 #include <linux/power_supply.h>
18 #include <linux/regmap.h>
19 #include <linux/slab.h>
21 #define CHG_STATE_ADP_INPUT 0x40
22 #define CHG_STATE_USB_INPUT 0x80
23 #define CHG_STATE_MASK 0x1f
24 #define CHG_STATE_CHG_OFF 0
25 #define CHG_STATE_CHG_READY_VADP 1
26 #define CHG_STATE_CHG_TRICKLE 2
27 #define CHG_STATE_CHG_RAPID 3
28 #define CHG_STATE_CHG_COMPLETE 4
29 #define CHG_STATE_SUSPEND 5
30 #define CHG_STATE_VCHG_OVER_VOL 6
31 #define CHG_STATE_BAT_ERROR 7
32 #define CHG_STATE_NO_BAT 8
33 #define CHG_STATE_BAT_OVER_VOL 9
34 #define CHG_STATE_BAT_TEMP_ERR 10
35 #define CHG_STATE_DIE_ERR 11
36 #define CHG_STATE_DIE_SHUTDOWN 12
37 #define CHG_STATE_NO_BAT2 13
38 #define CHG_STATE_CHG_READY_VUSB 14
40 #define FG_ENABLE 1
42 struct rn5t618_power_info {
43 struct rn5t618 *rn5t618;
44 struct platform_device *pdev;
45 struct power_supply *battery;
46 struct power_supply *usb;
47 struct power_supply *adp;
48 int irq;
51 static enum power_supply_property rn5t618_usb_props[] = {
52 POWER_SUPPLY_PROP_STATUS,
53 POWER_SUPPLY_PROP_ONLINE,
56 static enum power_supply_property rn5t618_adp_props[] = {
57 POWER_SUPPLY_PROP_STATUS,
58 POWER_SUPPLY_PROP_ONLINE,
62 static enum power_supply_property rn5t618_battery_props[] = {
63 POWER_SUPPLY_PROP_STATUS,
64 POWER_SUPPLY_PROP_PRESENT,
65 POWER_SUPPLY_PROP_VOLTAGE_NOW,
66 POWER_SUPPLY_PROP_CURRENT_NOW,
67 POWER_SUPPLY_PROP_CAPACITY,
68 POWER_SUPPLY_PROP_TEMP,
69 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
70 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
71 POWER_SUPPLY_PROP_TECHNOLOGY,
72 POWER_SUPPLY_PROP_CHARGE_FULL,
73 POWER_SUPPLY_PROP_CHARGE_NOW,
76 static int rn5t618_battery_read_doublereg(struct rn5t618_power_info *info,
77 u8 reg, u16 *result)
79 int ret, i;
80 u8 data[2];
81 u16 old, new;
83 old = 0;
84 /* Prevent races when registers are changing. */
85 for (i = 0; i < 3; i++) {
86 ret = regmap_bulk_read(info->rn5t618->regmap,
87 reg, data, sizeof(data));
88 if (ret)
89 return ret;
91 new = data[0] << 8;
92 new |= data[1];
93 if (new == old)
94 break;
96 old = new;
99 *result = new;
101 return 0;
104 static int rn5t618_decode_status(unsigned int status)
106 switch (status & CHG_STATE_MASK) {
107 case CHG_STATE_CHG_OFF:
108 case CHG_STATE_SUSPEND:
109 case CHG_STATE_VCHG_OVER_VOL:
110 case CHG_STATE_DIE_SHUTDOWN:
111 return POWER_SUPPLY_STATUS_DISCHARGING;
113 case CHG_STATE_CHG_TRICKLE:
114 case CHG_STATE_CHG_RAPID:
115 return POWER_SUPPLY_STATUS_CHARGING;
117 case CHG_STATE_CHG_COMPLETE:
118 return POWER_SUPPLY_STATUS_FULL;
120 default:
121 return POWER_SUPPLY_STATUS_NOT_CHARGING;
125 static int rn5t618_battery_status(struct rn5t618_power_info *info,
126 union power_supply_propval *val)
128 unsigned int v;
129 int ret;
131 ret = regmap_read(info->rn5t618->regmap, RN5T618_CHGSTATE, &v);
132 if (ret)
133 return ret;
135 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
137 if (v & 0xc0) { /* USB or ADP plugged */
138 val->intval = rn5t618_decode_status(v);
139 } else
140 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
142 return ret;
145 static int rn5t618_battery_present(struct rn5t618_power_info *info,
146 union power_supply_propval *val)
148 unsigned int v;
149 int ret;
151 ret = regmap_read(info->rn5t618->regmap, RN5T618_CHGSTATE, &v);
152 if (ret)
153 return ret;
155 v &= CHG_STATE_MASK;
156 if ((v == CHG_STATE_NO_BAT) || (v == CHG_STATE_NO_BAT2))
157 val->intval = 0;
158 else
159 val->intval = 1;
161 return ret;
164 static int rn5t618_battery_voltage_now(struct rn5t618_power_info *info,
165 union power_supply_propval *val)
167 u16 res;
168 int ret;
170 ret = rn5t618_battery_read_doublereg(info, RN5T618_VOLTAGE_1, &res);
171 if (ret)
172 return ret;
174 val->intval = res * 2 * 2500 / 4095 * 1000;
176 return 0;
179 static int rn5t618_battery_current_now(struct rn5t618_power_info *info,
180 union power_supply_propval *val)
182 u16 res;
183 int ret;
185 ret = rn5t618_battery_read_doublereg(info, RN5T618_CC_AVEREG1, &res);
186 if (ret)
187 return ret;
189 /* current is negative when discharging */
190 val->intval = sign_extend32(res, 13) * 1000;
192 return 0;
195 static int rn5t618_battery_capacity(struct rn5t618_power_info *info,
196 union power_supply_propval *val)
198 unsigned int v;
199 int ret;
201 ret = regmap_read(info->rn5t618->regmap, RN5T618_SOC, &v);
202 if (ret)
203 return ret;
205 val->intval = v;
207 return 0;
210 static int rn5t618_battery_temp(struct rn5t618_power_info *info,
211 union power_supply_propval *val)
213 u16 res;
214 int ret;
216 ret = rn5t618_battery_read_doublereg(info, RN5T618_TEMP_1, &res);
217 if (ret)
218 return ret;
220 val->intval = sign_extend32(res, 11) * 10 / 16;
222 return 0;
225 static int rn5t618_battery_tte(struct rn5t618_power_info *info,
226 union power_supply_propval *val)
228 u16 res;
229 int ret;
231 ret = rn5t618_battery_read_doublereg(info, RN5T618_TT_EMPTY_H, &res);
232 if (ret)
233 return ret;
235 if (res == 65535)
236 return -ENODATA;
238 val->intval = res * 60;
240 return 0;
243 static int rn5t618_battery_ttf(struct rn5t618_power_info *info,
244 union power_supply_propval *val)
246 u16 res;
247 int ret;
249 ret = rn5t618_battery_read_doublereg(info, RN5T618_TT_FULL_H, &res);
250 if (ret)
251 return ret;
253 if (res == 65535)
254 return -ENODATA;
256 val->intval = res * 60;
258 return 0;
261 static int rn5t618_battery_charge_full(struct rn5t618_power_info *info,
262 union power_supply_propval *val)
264 u16 res;
265 int ret;
267 ret = rn5t618_battery_read_doublereg(info, RN5T618_FA_CAP_H, &res);
268 if (ret)
269 return ret;
271 val->intval = res * 1000;
273 return 0;
276 static int rn5t618_battery_charge_now(struct rn5t618_power_info *info,
277 union power_supply_propval *val)
279 u16 res;
280 int ret;
282 ret = rn5t618_battery_read_doublereg(info, RN5T618_RE_CAP_H, &res);
283 if (ret)
284 return ret;
286 val->intval = res * 1000;
288 return 0;
291 static int rn5t618_battery_get_property(struct power_supply *psy,
292 enum power_supply_property psp,
293 union power_supply_propval *val)
295 int ret = 0;
296 struct rn5t618_power_info *info = power_supply_get_drvdata(psy);
298 switch (psp) {
299 case POWER_SUPPLY_PROP_STATUS:
300 ret = rn5t618_battery_status(info, val);
301 break;
302 case POWER_SUPPLY_PROP_PRESENT:
303 ret = rn5t618_battery_present(info, val);
304 break;
305 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
306 ret = rn5t618_battery_voltage_now(info, val);
307 break;
308 case POWER_SUPPLY_PROP_CURRENT_NOW:
309 ret = rn5t618_battery_current_now(info, val);
310 break;
311 case POWER_SUPPLY_PROP_CAPACITY:
312 ret = rn5t618_battery_capacity(info, val);
313 break;
314 case POWER_SUPPLY_PROP_TEMP:
315 ret = rn5t618_battery_temp(info, val);
316 break;
317 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
318 ret = rn5t618_battery_tte(info, val);
319 break;
320 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
321 ret = rn5t618_battery_ttf(info, val);
322 break;
323 case POWER_SUPPLY_PROP_TECHNOLOGY:
324 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
325 break;
326 case POWER_SUPPLY_PROP_CHARGE_FULL:
327 ret = rn5t618_battery_charge_full(info, val);
328 break;
329 case POWER_SUPPLY_PROP_CHARGE_NOW:
330 ret = rn5t618_battery_charge_now(info, val);
331 break;
332 default:
333 return -EINVAL;
336 return ret;
339 static int rn5t618_adp_get_property(struct power_supply *psy,
340 enum power_supply_property psp,
341 union power_supply_propval *val)
343 struct rn5t618_power_info *info = power_supply_get_drvdata(psy);
344 unsigned int chgstate;
345 bool online;
346 int ret;
348 ret = regmap_read(info->rn5t618->regmap, RN5T618_CHGSTATE, &chgstate);
349 if (ret)
350 return ret;
352 online = !!(chgstate & CHG_STATE_ADP_INPUT);
354 switch (psp) {
355 case POWER_SUPPLY_PROP_ONLINE:
356 val->intval = online;
357 break;
358 case POWER_SUPPLY_PROP_STATUS:
359 if (!online) {
360 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
361 break;
363 val->intval = rn5t618_decode_status(chgstate);
364 if (val->intval != POWER_SUPPLY_STATUS_CHARGING)
365 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
367 break;
368 default:
369 return -EINVAL;
372 return 0;
375 static int rn5t618_usb_get_property(struct power_supply *psy,
376 enum power_supply_property psp,
377 union power_supply_propval *val)
379 struct rn5t618_power_info *info = power_supply_get_drvdata(psy);
380 unsigned int chgstate;
381 bool online;
382 int ret;
384 ret = regmap_read(info->rn5t618->regmap, RN5T618_CHGSTATE, &chgstate);
385 if (ret)
386 return ret;
388 online = !!(chgstate & CHG_STATE_USB_INPUT);
390 switch (psp) {
391 case POWER_SUPPLY_PROP_ONLINE:
392 val->intval = online;
393 break;
394 case POWER_SUPPLY_PROP_STATUS:
395 if (!online) {
396 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
397 break;
399 val->intval = rn5t618_decode_status(chgstate);
400 if (val->intval != POWER_SUPPLY_STATUS_CHARGING)
401 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
403 break;
404 default:
405 return -EINVAL;
408 return 0;
411 static const struct power_supply_desc rn5t618_battery_desc = {
412 .name = "rn5t618-battery",
413 .type = POWER_SUPPLY_TYPE_BATTERY,
414 .properties = rn5t618_battery_props,
415 .num_properties = ARRAY_SIZE(rn5t618_battery_props),
416 .get_property = rn5t618_battery_get_property,
419 static const struct power_supply_desc rn5t618_adp_desc = {
420 .name = "rn5t618-adp",
421 .type = POWER_SUPPLY_TYPE_MAINS,
422 .properties = rn5t618_adp_props,
423 .num_properties = ARRAY_SIZE(rn5t618_adp_props),
424 .get_property = rn5t618_adp_get_property,
427 static const struct power_supply_desc rn5t618_usb_desc = {
428 .name = "rn5t618-usb",
429 .type = POWER_SUPPLY_TYPE_USB,
430 .properties = rn5t618_usb_props,
431 .num_properties = ARRAY_SIZE(rn5t618_usb_props),
432 .get_property = rn5t618_usb_get_property,
435 static irqreturn_t rn5t618_charger_irq(int irq, void *data)
437 struct device *dev = data;
438 struct rn5t618_power_info *info = dev_get_drvdata(dev);
440 unsigned int ctrl, stat1, stat2, err;
442 regmap_read(info->rn5t618->regmap, RN5T618_CHGERR_IRR, &err);
443 regmap_read(info->rn5t618->regmap, RN5T618_CHGCTRL_IRR, &ctrl);
444 regmap_read(info->rn5t618->regmap, RN5T618_CHGSTAT_IRR1, &stat1);
445 regmap_read(info->rn5t618->regmap, RN5T618_CHGSTAT_IRR2, &stat2);
447 regmap_write(info->rn5t618->regmap, RN5T618_CHGERR_IRR, 0);
448 regmap_write(info->rn5t618->regmap, RN5T618_CHGCTRL_IRR, 0);
449 regmap_write(info->rn5t618->regmap, RN5T618_CHGSTAT_IRR1, 0);
450 regmap_write(info->rn5t618->regmap, RN5T618_CHGSTAT_IRR2, 0);
452 dev_dbg(dev, "chgerr: %x chgctrl: %x chgstat: %x chgstat2: %x\n",
453 err, ctrl, stat1, stat2);
455 power_supply_changed(info->usb);
456 power_supply_changed(info->adp);
457 power_supply_changed(info->battery);
459 return IRQ_HANDLED;
462 static int rn5t618_power_probe(struct platform_device *pdev)
464 int ret = 0;
465 unsigned int v;
466 struct power_supply_config psy_cfg = {};
467 struct rn5t618_power_info *info;
469 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
470 if (!info)
471 return -ENOMEM;
473 info->pdev = pdev;
474 info->rn5t618 = dev_get_drvdata(pdev->dev.parent);
475 info->irq = -1;
477 platform_set_drvdata(pdev, info);
479 ret = regmap_read(info->rn5t618->regmap, RN5T618_CONTROL, &v);
480 if (ret)
481 return ret;
483 if (!(v & FG_ENABLE)) {
484 /* E.g. the vendor kernels of various Kobo and Tolino Ebook
485 * readers disable the fuel gauge on shutdown. If a kernel
486 * without fuel gauge support is booted after that, the fuel
487 * gauge will get decalibrated.
489 dev_info(&pdev->dev, "Fuel gauge not enabled, enabling now\n");
490 dev_info(&pdev->dev, "Expect imprecise results\n");
491 regmap_update_bits(info->rn5t618->regmap, RN5T618_CONTROL,
492 FG_ENABLE, FG_ENABLE);
495 psy_cfg.drv_data = info;
496 info->battery = devm_power_supply_register(&pdev->dev,
497 &rn5t618_battery_desc,
498 &psy_cfg);
499 if (IS_ERR(info->battery)) {
500 ret = PTR_ERR(info->battery);
501 dev_err(&pdev->dev, "failed to register battery: %d\n", ret);
502 return ret;
505 info->adp = devm_power_supply_register(&pdev->dev,
506 &rn5t618_adp_desc,
507 &psy_cfg);
508 if (IS_ERR(info->adp)) {
509 ret = PTR_ERR(info->adp);
510 dev_err(&pdev->dev, "failed to register adp: %d\n", ret);
511 return ret;
514 info->usb = devm_power_supply_register(&pdev->dev,
515 &rn5t618_usb_desc,
516 &psy_cfg);
517 if (IS_ERR(info->usb)) {
518 ret = PTR_ERR(info->usb);
519 dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
520 return ret;
523 if (info->rn5t618->irq_data)
524 info->irq = regmap_irq_get_virq(info->rn5t618->irq_data,
525 RN5T618_IRQ_CHG);
527 if (info->irq < 0)
528 info->irq = -1;
529 else {
530 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
531 rn5t618_charger_irq,
532 IRQF_ONESHOT,
533 "rn5t618_power",
534 &pdev->dev);
536 if (ret < 0) {
537 dev_err(&pdev->dev, "request IRQ:%d fail\n",
538 info->irq);
539 info->irq = -1;
543 return 0;
546 static struct platform_driver rn5t618_power_driver = {
547 .driver = {
548 .name = "rn5t618-power",
550 .probe = rn5t618_power_probe,
553 module_platform_driver(rn5t618_power_driver);
554 MODULE_ALIAS("platform:rn5t618-power");
555 MODULE_DESCRIPTION("Power supply driver for RICOH RN5T618");
556 MODULE_LICENSE("GPL");