1 // SPDX-License-Identifier: GPL-2.0+
3 * Power supply driver for the RICOH RN5T618 power management chip family
5 * Copyright (C) 2020 Andreas Kemnade
8 #include <linux/kernel.h>
9 #include <linux/device.h>
10 #include <linux/bitops.h>
11 #include <linux/errno.h>
12 #include <linux/iio/consumer.h>
13 #include <linux/init.h>
14 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/mfd/rn5t618.h>
17 #include <linux/platform_device.h>
18 #include <linux/power_supply.h>
19 #include <linux/regmap.h>
20 #include <linux/slab.h>
22 #define CHG_STATE_ADP_INPUT 0x40
23 #define CHG_STATE_USB_INPUT 0x80
24 #define CHG_STATE_MASK 0x1f
25 #define CHG_STATE_CHG_OFF 0
26 #define CHG_STATE_CHG_READY_VADP 1
27 #define CHG_STATE_CHG_TRICKLE 2
28 #define CHG_STATE_CHG_RAPID 3
29 #define CHG_STATE_CHG_COMPLETE 4
30 #define CHG_STATE_SUSPEND 5
31 #define CHG_STATE_VCHG_OVER_VOL 6
32 #define CHG_STATE_BAT_ERROR 7
33 #define CHG_STATE_NO_BAT 8
34 #define CHG_STATE_BAT_OVER_VOL 9
35 #define CHG_STATE_BAT_TEMP_ERR 10
36 #define CHG_STATE_DIE_ERR 11
37 #define CHG_STATE_DIE_SHUTDOWN 12
38 #define CHG_STATE_NO_BAT2 13
39 #define CHG_STATE_CHG_READY_VUSB 14
41 #define GCHGDET_TYPE_MASK 0x30
42 #define GCHGDET_TYPE_SDP 0x00
43 #define GCHGDET_TYPE_CDP 0x10
44 #define GCHGDET_TYPE_DCP 0x20
49 * Formula seems accurate for battery current, but for USB current around 70mA
50 * per step was seen on Kobo Clara HD but all sources show the same formula
51 * also fur USB current. To avoid accidentially unwanted high currents we stick
54 #define TO_CUR_REG(x) ((x) / 100000 - 1)
55 #define FROM_CUR_REG(x) ((((x) & 0x1f) + 1) * 100000)
56 #define CHG_MIN_CUR 100000
57 #define CHG_MAX_CUR 1800000
58 #define ADP_MAX_CUR 2500000
59 #define USB_MAX_CUR 1400000
62 struct rn5t618_power_info {
63 struct rn5t618 *rn5t618;
64 struct platform_device *pdev;
65 struct power_supply *battery;
66 struct power_supply *usb;
67 struct power_supply *adp;
68 struct iio_channel *channel_vusb;
69 struct iio_channel *channel_vadp;
73 static enum power_supply_usb_type rn5t618_usb_types[] = {
74 POWER_SUPPLY_USB_TYPE_SDP,
75 POWER_SUPPLY_USB_TYPE_DCP,
76 POWER_SUPPLY_USB_TYPE_CDP,
77 POWER_SUPPLY_USB_TYPE_UNKNOWN
80 static enum power_supply_property rn5t618_usb_props[] = {
81 /* input current limit is not very accurate */
82 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
83 POWER_SUPPLY_PROP_VOLTAGE_NOW,
84 POWER_SUPPLY_PROP_STATUS,
85 POWER_SUPPLY_PROP_USB_TYPE,
86 POWER_SUPPLY_PROP_ONLINE,
89 static enum power_supply_property rn5t618_adp_props[] = {
90 /* input current limit is not very accurate */
91 POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
92 POWER_SUPPLY_PROP_VOLTAGE_NOW,
93 POWER_SUPPLY_PROP_STATUS,
94 POWER_SUPPLY_PROP_ONLINE,
98 static enum power_supply_property rn5t618_battery_props[] = {
99 POWER_SUPPLY_PROP_STATUS,
100 POWER_SUPPLY_PROP_PRESENT,
101 POWER_SUPPLY_PROP_VOLTAGE_NOW,
102 POWER_SUPPLY_PROP_CURRENT_NOW,
103 POWER_SUPPLY_PROP_CAPACITY,
104 POWER_SUPPLY_PROP_TEMP,
105 POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW,
106 POWER_SUPPLY_PROP_TIME_TO_FULL_NOW,
107 POWER_SUPPLY_PROP_TECHNOLOGY,
108 POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT,
109 POWER_SUPPLY_PROP_CHARGE_FULL,
110 POWER_SUPPLY_PROP_CHARGE_NOW,
113 static int rn5t618_battery_read_doublereg(struct rn5t618_power_info *info,
121 /* Prevent races when registers are changing. */
122 for (i = 0; i < 3; i++) {
123 ret = regmap_bulk_read(info->rn5t618->regmap,
124 reg, data, sizeof(data));
141 static int rn5t618_decode_status(unsigned int status)
143 switch (status & CHG_STATE_MASK) {
144 case CHG_STATE_CHG_OFF:
145 case CHG_STATE_SUSPEND:
146 case CHG_STATE_VCHG_OVER_VOL:
147 case CHG_STATE_DIE_SHUTDOWN:
148 return POWER_SUPPLY_STATUS_DISCHARGING;
150 case CHG_STATE_CHG_TRICKLE:
151 case CHG_STATE_CHG_RAPID:
152 return POWER_SUPPLY_STATUS_CHARGING;
154 case CHG_STATE_CHG_COMPLETE:
155 return POWER_SUPPLY_STATUS_FULL;
158 return POWER_SUPPLY_STATUS_NOT_CHARGING;
162 static int rn5t618_battery_status(struct rn5t618_power_info *info,
163 union power_supply_propval *val)
168 ret = regmap_read(info->rn5t618->regmap, RN5T618_CHGSTATE, &v);
172 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
174 if (v & 0xc0) { /* USB or ADP plugged */
175 val->intval = rn5t618_decode_status(v);
177 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
182 static int rn5t618_battery_present(struct rn5t618_power_info *info,
183 union power_supply_propval *val)
188 ret = regmap_read(info->rn5t618->regmap, RN5T618_CHGSTATE, &v);
193 if ((v == CHG_STATE_NO_BAT) || (v == CHG_STATE_NO_BAT2))
201 static int rn5t618_battery_voltage_now(struct rn5t618_power_info *info,
202 union power_supply_propval *val)
207 ret = rn5t618_battery_read_doublereg(info, RN5T618_VOLTAGE_1, &res);
211 val->intval = res * 2 * 2500 / 4095 * 1000;
216 static int rn5t618_battery_current_now(struct rn5t618_power_info *info,
217 union power_supply_propval *val)
222 ret = rn5t618_battery_read_doublereg(info, RN5T618_CC_AVEREG1, &res);
226 /* current is negative when discharging */
227 val->intval = sign_extend32(res, 13) * 1000;
232 static int rn5t618_battery_capacity(struct rn5t618_power_info *info,
233 union power_supply_propval *val)
238 ret = regmap_read(info->rn5t618->regmap, RN5T618_SOC, &v);
247 static int rn5t618_battery_temp(struct rn5t618_power_info *info,
248 union power_supply_propval *val)
253 ret = rn5t618_battery_read_doublereg(info, RN5T618_TEMP_1, &res);
257 val->intval = sign_extend32(res, 11) * 10 / 16;
262 static int rn5t618_battery_tte(struct rn5t618_power_info *info,
263 union power_supply_propval *val)
268 ret = rn5t618_battery_read_doublereg(info, RN5T618_TT_EMPTY_H, &res);
275 val->intval = res * 60;
280 static int rn5t618_battery_ttf(struct rn5t618_power_info *info,
281 union power_supply_propval *val)
286 ret = rn5t618_battery_read_doublereg(info, RN5T618_TT_FULL_H, &res);
293 val->intval = res * 60;
298 static int rn5t618_battery_set_current_limit(struct rn5t618_power_info *info,
299 const union power_supply_propval *val)
301 if (val->intval < CHG_MIN_CUR)
304 if (val->intval >= CHG_MAX_CUR)
307 return regmap_update_bits(info->rn5t618->regmap,
309 0x1F, TO_CUR_REG(val->intval));
312 static int rn5t618_battery_get_current_limit(struct rn5t618_power_info *info,
313 union power_supply_propval *val)
318 ret = regmap_read(info->rn5t618->regmap, RN5T618_CHGISET,
323 val->intval = FROM_CUR_REG(regval);
328 static int rn5t618_battery_charge_full(struct rn5t618_power_info *info,
329 union power_supply_propval *val)
334 ret = rn5t618_battery_read_doublereg(info, RN5T618_FA_CAP_H, &res);
338 val->intval = res * 1000;
343 static int rn5t618_battery_charge_now(struct rn5t618_power_info *info,
344 union power_supply_propval *val)
349 ret = rn5t618_battery_read_doublereg(info, RN5T618_RE_CAP_H, &res);
353 val->intval = res * 1000;
358 static int rn5t618_battery_get_property(struct power_supply *psy,
359 enum power_supply_property psp,
360 union power_supply_propval *val)
363 struct rn5t618_power_info *info = power_supply_get_drvdata(psy);
366 case POWER_SUPPLY_PROP_STATUS:
367 ret = rn5t618_battery_status(info, val);
369 case POWER_SUPPLY_PROP_PRESENT:
370 ret = rn5t618_battery_present(info, val);
372 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
373 ret = rn5t618_battery_voltage_now(info, val);
375 case POWER_SUPPLY_PROP_CURRENT_NOW:
376 ret = rn5t618_battery_current_now(info, val);
378 case POWER_SUPPLY_PROP_CAPACITY:
379 ret = rn5t618_battery_capacity(info, val);
381 case POWER_SUPPLY_PROP_TEMP:
382 ret = rn5t618_battery_temp(info, val);
384 case POWER_SUPPLY_PROP_TIME_TO_EMPTY_NOW:
385 ret = rn5t618_battery_tte(info, val);
387 case POWER_SUPPLY_PROP_TIME_TO_FULL_NOW:
388 ret = rn5t618_battery_ttf(info, val);
390 case POWER_SUPPLY_PROP_TECHNOLOGY:
391 val->intval = POWER_SUPPLY_TECHNOLOGY_LION;
393 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
394 ret = rn5t618_battery_get_current_limit(info, val);
396 case POWER_SUPPLY_PROP_CHARGE_FULL:
397 ret = rn5t618_battery_charge_full(info, val);
399 case POWER_SUPPLY_PROP_CHARGE_NOW:
400 ret = rn5t618_battery_charge_now(info, val);
409 static int rn5t618_battery_set_property(struct power_supply *psy,
410 enum power_supply_property psp,
411 const union power_supply_propval *val)
413 struct rn5t618_power_info *info = power_supply_get_drvdata(psy);
416 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
417 return rn5t618_battery_set_current_limit(info, val);
423 static int rn5t618_battery_property_is_writeable(struct power_supply *psy,
424 enum power_supply_property psp)
427 case POWER_SUPPLY_PROP_CHARGE_CONTROL_LIMIT:
434 static int rn5t618_adp_get_property(struct power_supply *psy,
435 enum power_supply_property psp,
436 union power_supply_propval *val)
438 struct rn5t618_power_info *info = power_supply_get_drvdata(psy);
439 unsigned int chgstate;
444 ret = regmap_read(info->rn5t618->regmap, RN5T618_CHGSTATE, &chgstate);
448 online = !!(chgstate & CHG_STATE_ADP_INPUT);
451 case POWER_SUPPLY_PROP_ONLINE:
452 val->intval = online;
454 case POWER_SUPPLY_PROP_STATUS:
456 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
459 val->intval = rn5t618_decode_status(chgstate);
460 if (val->intval != POWER_SUPPLY_STATUS_CHARGING)
461 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
464 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
465 ret = regmap_read(info->rn5t618->regmap,
466 RN5T618_REGISET1, ®val);
470 val->intval = FROM_CUR_REG(regval);
472 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
473 if (!info->channel_vadp)
476 ret = iio_read_channel_processed_scale(info->channel_vadp, &val->intval, 1000);
488 static int rn5t618_adp_set_property(struct power_supply *psy,
489 enum power_supply_property psp,
490 const union power_supply_propval *val)
492 struct rn5t618_power_info *info = power_supply_get_drvdata(psy);
496 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
497 if (val->intval > ADP_MAX_CUR)
500 if (val->intval < CHG_MIN_CUR)
503 ret = regmap_write(info->rn5t618->regmap, RN5T618_REGISET1,
504 TO_CUR_REG(val->intval));
516 static int rn5t618_adp_property_is_writeable(struct power_supply *psy,
517 enum power_supply_property psp)
520 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
527 static int rc5t619_usb_get_type(struct rn5t618_power_info *info,
528 union power_supply_propval *val)
533 ret = regmap_read(info->rn5t618->regmap, RN5T618_GCHGDET, ®val);
537 switch (regval & GCHGDET_TYPE_MASK) {
538 case GCHGDET_TYPE_SDP:
539 val->intval = POWER_SUPPLY_USB_TYPE_SDP;
541 case GCHGDET_TYPE_CDP:
542 val->intval = POWER_SUPPLY_USB_TYPE_CDP;
544 case GCHGDET_TYPE_DCP:
545 val->intval = POWER_SUPPLY_USB_TYPE_DCP;
548 val->intval = POWER_SUPPLY_USB_TYPE_UNKNOWN;
554 static int rn5t618_usb_get_property(struct power_supply *psy,
555 enum power_supply_property psp,
556 union power_supply_propval *val)
558 struct rn5t618_power_info *info = power_supply_get_drvdata(psy);
559 unsigned int chgstate;
564 ret = regmap_read(info->rn5t618->regmap, RN5T618_CHGSTATE, &chgstate);
568 online = !!(chgstate & CHG_STATE_USB_INPUT);
571 case POWER_SUPPLY_PROP_ONLINE:
572 val->intval = online;
574 case POWER_SUPPLY_PROP_STATUS:
576 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
579 val->intval = rn5t618_decode_status(chgstate);
580 if (val->intval != POWER_SUPPLY_STATUS_CHARGING)
581 val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING;
584 case POWER_SUPPLY_PROP_USB_TYPE:
585 if (!online || (info->rn5t618->variant != RC5T619))
588 return rc5t619_usb_get_type(info, val);
589 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
590 ret = regmap_read(info->rn5t618->regmap, RN5T618_CHGCTL1,
597 ret = regmap_read(info->rn5t618->regmap,
603 val->intval = FROM_CUR_REG(regval);
606 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
607 if (!info->channel_vusb)
610 ret = iio_read_channel_processed_scale(info->channel_vusb, &val->intval, 1000);
622 static int rn5t618_usb_set_property(struct power_supply *psy,
623 enum power_supply_property psp,
624 const union power_supply_propval *val)
626 struct rn5t618_power_info *info = power_supply_get_drvdata(psy);
630 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
631 if (val->intval > USB_MAX_CUR)
634 if (val->intval < CHG_MIN_CUR)
637 ret = regmap_write(info->rn5t618->regmap, RN5T618_REGISET2,
638 0xE0 | TO_CUR_REG(val->intval));
650 static int rn5t618_usb_property_is_writeable(struct power_supply *psy,
651 enum power_supply_property psp)
654 case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
661 static const struct power_supply_desc rn5t618_battery_desc = {
662 .name = "rn5t618-battery",
663 .type = POWER_SUPPLY_TYPE_BATTERY,
664 .properties = rn5t618_battery_props,
665 .num_properties = ARRAY_SIZE(rn5t618_battery_props),
666 .get_property = rn5t618_battery_get_property,
667 .set_property = rn5t618_battery_set_property,
668 .property_is_writeable = rn5t618_battery_property_is_writeable,
671 static const struct power_supply_desc rn5t618_adp_desc = {
672 .name = "rn5t618-adp",
673 .type = POWER_SUPPLY_TYPE_MAINS,
674 .properties = rn5t618_adp_props,
675 .num_properties = ARRAY_SIZE(rn5t618_adp_props),
676 .get_property = rn5t618_adp_get_property,
677 .set_property = rn5t618_adp_set_property,
678 .property_is_writeable = rn5t618_adp_property_is_writeable,
681 static const struct power_supply_desc rn5t618_usb_desc = {
682 .name = "rn5t618-usb",
683 .type = POWER_SUPPLY_TYPE_USB,
684 .usb_types = rn5t618_usb_types,
685 .num_usb_types = ARRAY_SIZE(rn5t618_usb_types),
686 .properties = rn5t618_usb_props,
687 .num_properties = ARRAY_SIZE(rn5t618_usb_props),
688 .get_property = rn5t618_usb_get_property,
689 .set_property = rn5t618_usb_set_property,
690 .property_is_writeable = rn5t618_usb_property_is_writeable,
693 static irqreturn_t rn5t618_charger_irq(int irq, void *data)
695 struct device *dev = data;
696 struct rn5t618_power_info *info = dev_get_drvdata(dev);
698 unsigned int ctrl, stat1, stat2, err;
700 regmap_read(info->rn5t618->regmap, RN5T618_CHGERR_IRR, &err);
701 regmap_read(info->rn5t618->regmap, RN5T618_CHGCTRL_IRR, &ctrl);
702 regmap_read(info->rn5t618->regmap, RN5T618_CHGSTAT_IRR1, &stat1);
703 regmap_read(info->rn5t618->regmap, RN5T618_CHGSTAT_IRR2, &stat2);
705 regmap_write(info->rn5t618->regmap, RN5T618_CHGERR_IRR, 0);
706 regmap_write(info->rn5t618->regmap, RN5T618_CHGCTRL_IRR, 0);
707 regmap_write(info->rn5t618->regmap, RN5T618_CHGSTAT_IRR1, 0);
708 regmap_write(info->rn5t618->regmap, RN5T618_CHGSTAT_IRR2, 0);
710 dev_dbg(dev, "chgerr: %x chgctrl: %x chgstat: %x chgstat2: %x\n",
711 err, ctrl, stat1, stat2);
713 power_supply_changed(info->usb);
714 power_supply_changed(info->adp);
715 power_supply_changed(info->battery);
720 static int rn5t618_power_probe(struct platform_device *pdev)
724 struct power_supply_config psy_cfg = {};
725 struct rn5t618_power_info *info;
727 info = devm_kzalloc(&pdev->dev, sizeof(*info), GFP_KERNEL);
732 info->rn5t618 = dev_get_drvdata(pdev->dev.parent);
735 platform_set_drvdata(pdev, info);
737 info->channel_vusb = devm_iio_channel_get(&pdev->dev, "vusb");
738 if (IS_ERR(info->channel_vusb)) {
739 if (PTR_ERR(info->channel_vusb) == -ENODEV)
740 return -EPROBE_DEFER;
741 return PTR_ERR(info->channel_vusb);
744 info->channel_vadp = devm_iio_channel_get(&pdev->dev, "vadp");
745 if (IS_ERR(info->channel_vadp)) {
746 if (PTR_ERR(info->channel_vadp) == -ENODEV)
747 return -EPROBE_DEFER;
748 return PTR_ERR(info->channel_vadp);
751 ret = regmap_read(info->rn5t618->regmap, RN5T618_CONTROL, &v);
755 if (!(v & FG_ENABLE)) {
756 /* E.g. the vendor kernels of various Kobo and Tolino Ebook
757 * readers disable the fuel gauge on shutdown. If a kernel
758 * without fuel gauge support is booted after that, the fuel
759 * gauge will get decalibrated.
761 dev_info(&pdev->dev, "Fuel gauge not enabled, enabling now\n");
762 dev_info(&pdev->dev, "Expect imprecise results\n");
763 regmap_update_bits(info->rn5t618->regmap, RN5T618_CONTROL,
764 FG_ENABLE, FG_ENABLE);
767 psy_cfg.drv_data = info;
768 info->battery = devm_power_supply_register(&pdev->dev,
769 &rn5t618_battery_desc,
771 if (IS_ERR(info->battery)) {
772 ret = PTR_ERR(info->battery);
773 dev_err(&pdev->dev, "failed to register battery: %d\n", ret);
777 info->adp = devm_power_supply_register(&pdev->dev,
780 if (IS_ERR(info->adp)) {
781 ret = PTR_ERR(info->adp);
782 dev_err(&pdev->dev, "failed to register adp: %d\n", ret);
786 info->usb = devm_power_supply_register(&pdev->dev,
789 if (IS_ERR(info->usb)) {
790 ret = PTR_ERR(info->usb);
791 dev_err(&pdev->dev, "failed to register usb: %d\n", ret);
795 if (info->rn5t618->irq_data)
796 info->irq = regmap_irq_get_virq(info->rn5t618->irq_data,
802 ret = devm_request_threaded_irq(&pdev->dev, info->irq, NULL,
809 dev_err(&pdev->dev, "request IRQ:%d fail\n",
818 static struct platform_driver rn5t618_power_driver = {
820 .name = "rn5t618-power",
822 .probe = rn5t618_power_probe,
825 module_platform_driver(rn5t618_power_driver);
826 MODULE_ALIAS("platform:rn5t618-power");
827 MODULE_DESCRIPTION("Power supply driver for RICOH RN5T618");
828 MODULE_LICENSE("GPL");