Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge tag 'for-v6.1' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply

Pull power supply and reset updates from Sebastian Reichel:

- new maintenance charging documentation

- mt6370: new charger driver

- bq25890: support input current limit

- added Qualcomm PMK8350 PON support

- misc minor fixes

* tag 'for-v6.1' of git://git.kernel.org/pub/scm/linux/kernel/git/sre/linux-power-supply: (22 commits)
power: supply: ab8500: remove unused static local variable
power: supply: mt6370: Fix return value check in mt6370_chg_probe()
power: supply: ab8500: Remove unused struct ab8500_chargalg_sysfs_entry
power: supply: mt6370: uses IIO interfaces, depends on IIO
power: supply: max1721x: Fix spelling mistake "Gauage" -> "Gauge"
power: supply: mt6370: Add MediaTek MT6370 charger driver
dt-bindings: power: supply: Add MediaTek MT6370 Charger
lib: add linear range index macro
power: supply: bq25890: Fix enum conversion in bq25890_power_supply_set_property()
power: supply: bq27xxx: fix NULL vs 0 warnings
power: supply: bq27xxx: fix __be16 warnings
power: supply: bq25890: Add support for setting IINLIM
power: supply: bq25890: Disable PUMPX_EN on errors
power: supply: Fix repeated word in comments
power: supply: adp5061: show unknown capacity_level as text
power: supply: adp5061: fix out-of-bounds read in adp5061_get_chg_type()
power: supply: tps65217: Fix comments typo
power: reset: qcom-pon: add support for qcom,pmk8350-pon compatible string
dt-bindings: power: reset: qcom-pon: Add new compatible "qcom,pmk8350-pon"
power: supply: cw2015: Use device managed API to simplify the code
...

+1246 -65
+46 -4
Documentation/devicetree/bindings/power/reset/qcom,pon.yaml
··· 15 15 16 16 This DT node has pwrkey and resin as sub nodes. 17 17 18 - allOf: 19 - - $ref: reboot-mode.yaml# 20 - 21 18 properties: 22 19 compatible: 23 20 enum: 24 21 - qcom,pm8916-pon 25 22 - qcom,pms405-pon 26 23 - qcom,pm8998-pon 24 + - qcom,pmk8350-pon 27 25 28 26 reg: 29 - maxItems: 1 27 + description: | 28 + Specifies the SPMI base address for the PON (power-on) peripheral. For 29 + PMICs that have the PON peripheral (GEN3) split into PON_HLOS and PON_PBS 30 + (e.g. PMK8350), this can hold addresses of both PON_HLOS and PON_PBS 31 + peripherals. In that case, the PON_PBS address needs to be specified to 32 + facilitate software debouncing on some PMIC. 33 + minItems: 1 34 + maxItems: 2 35 + 36 + reg-names: 37 + minItems: 1 38 + maxItems: 2 30 39 31 40 pwrkey: 32 41 type: object ··· 54 45 - reg 55 46 56 47 unevaluatedProperties: false 48 + 49 + allOf: 50 + - $ref: reboot-mode.yaml# 51 + - if: 52 + properties: 53 + compatible: 54 + contains: 55 + enum: 56 + - qcom,pm8916-pon 57 + - qcom,pms405-pon 58 + - qcom,pm8998-pon 59 + then: 60 + properties: 61 + reg: 62 + maxItems: 1 63 + reg-names: 64 + items: 65 + - const: pon 66 + - if: 67 + properties: 68 + compatible: 69 + contains: 70 + const: qcom,pmk8350-pon 71 + then: 72 + properties: 73 + reg: 74 + minItems: 1 75 + maxItems: 2 76 + reg-names: 77 + minItems: 1 78 + items: 79 + - const: hlos 80 + - const: pbs 57 81 58 82 examples: 59 83 - |
+96
Documentation/devicetree/bindings/power/supply/mediatek,mt6370-charger.yaml
··· 1 + # SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 + %YAML 1.2 3 + --- 4 + $id: http://devicetree.org/schemas/power/supply/mediatek,mt6370-charger.yaml# 5 + $schema: http://devicetree.org/meta-schemas/core.yaml# 6 + 7 + title: MediaTek MT6370 Battery Charger 8 + 9 + maintainers: 10 + - ChiaEn Wu <chiaen_wu@richtek.com> 11 + 12 + description: | 13 + This module is part of the MT6370 MFD device. 14 + Provides Battery Charger, Boost for OTG devices and BC1.2 detection. 15 + 16 + properties: 17 + compatible: 18 + const: mediatek,mt6370-charger 19 + 20 + interrupts: 21 + description: | 22 + Specify what irqs are needed to be handled by MT6370 Charger driver. 23 + We need to use the IRQ "MT6370_IRQ_OVPCTRL_UVP_D" to know when USB 24 + is plugged in, and then the driver will enable BC1.2 detection. 25 + After the hardware of MT6370 completes the BC1.2 detection, 26 + IRQ "MT6370_IRQ_ATTACH" will be triggered, and the driver will know 27 + the result of BC1.2 detection. 28 + When the IRQ "MT6370_IRQ_CHG_MIVR" is triggered, it means that the 29 + hardware enters the "Minimum Input Voltage Regulation loop" and 30 + a workaround needs to be applied at this time. 31 + In summary, "MT6370_IRQ_OVPCTRL_UVP_D", "MT6370_IRQ_ATTACH" and 32 + "MT6370_IRQ_CHG_MIVR" are required in this charger driver. 33 + items: 34 + - description: irq of "USB is plugged in" 35 + - description: irq of "BC1.2 is done" 36 + - description: irq of "Minimum Input Voltage Regulation loop is active" 37 + 38 + interrupt-names: 39 + items: 40 + - const: uvp_d_evt 41 + - const: attach_i 42 + - const: mivr 43 + 44 + io-channels: 45 + description: | 46 + Use ADC channel to read VBUS, IBUS, IBAT, etc., info. 47 + minItems: 1 48 + items: 49 + - description: | 50 + VBUS voltage with lower accuracy (+-75mV) but higher measure 51 + range (1~22V) 52 + - description: | 53 + VBUS voltage with higher accuracy (+-30mV) but lower measure 54 + range (1~9.76V) 55 + - description: the main system input voltage 56 + - description: battery voltage 57 + - description: battery temperature-sense input voltage 58 + - description: IBUS current (required) 59 + - description: battery current 60 + - description: | 61 + regulated output voltage to supply for the PWM low-side gate driver 62 + and the bootstrap capacitor 63 + - description: IC junction temperature 64 + 65 + io-channel-names: 66 + minItems: 1 67 + items: 68 + - const: vbusdiv5 69 + - const: vbusdiv2 70 + - const: vsys 71 + - const: vbat 72 + - const: ts_bat 73 + - const: ibus 74 + - const: ibat 75 + - const: chg_vddp 76 + - const: temp_jc 77 + 78 + usb-otg-vbus-regulator: 79 + type: object 80 + description: OTG boost regulator. 81 + unevaluatedProperties: false 82 + $ref: /schemas/regulator/regulator.yaml# 83 + 84 + properties: 85 + enable-gpios: 86 + maxItems: 1 87 + 88 + required: 89 + - compatible 90 + - interrupts 91 + - interrupt-names 92 + - io-channels 93 + 94 + additionalProperties: false 95 + 96 + ...
+1
drivers/power/reset/qcom-pon.c
··· 82 82 { .compatible = "qcom,pm8916-pon", .data = (void *)GEN1_REASON_SHIFT }, 83 83 { .compatible = "qcom,pms405-pon", .data = (void *)GEN1_REASON_SHIFT }, 84 84 { .compatible = "qcom,pm8998-pon", .data = (void *)GEN2_REASON_SHIFT }, 85 + { .compatible = "qcom,pmk8350-pon", .data = (void *)GEN2_REASON_SHIFT }, 85 86 { } 86 87 }; 87 88 MODULE_DEVICE_TABLE(of, pm8916_pon_id_table);
+15
drivers/power/supply/Kconfig
··· 619 619 Average Input Current Regulation, Battery Temperature Sensing, 620 620 Over-Temperature Protection, DPDM Detection for BC1.2. 621 621 622 + config CHARGER_MT6370 623 + tristate "MediaTek MT6370 Charger Driver" 624 + depends on MFD_MT6370 625 + depends on REGULATOR 626 + depends on IIO 627 + select LINEAR_RANGES 628 + help 629 + Say Y here to enable MT6370 Charger Part. 630 + The device supports High-Accuracy Voltage/Current Regulation, 631 + Average Input Current Regulation, Battery Temperature Sensing, 632 + Over-Temperature Protection, DPDM Detection for BC1.2. 633 + 634 + This driver can also be built as a module. If so, the module 635 + will be called "mt6370-charger". 636 + 622 637 config CHARGER_QCOM_SMBB 623 638 tristate "Qualcomm Switch-Mode Battery Charger and Boost" 624 639 depends on MFD_SPMI_PMIC || COMPILE_TEST
+1
drivers/power/supply/Makefile
··· 82 82 obj-$(CONFIG_CHARGER_MAX8998) += max8998_charger.o 83 83 obj-$(CONFIG_CHARGER_MP2629) += mp2629_charger.o 84 84 obj-$(CONFIG_CHARGER_MT6360) += mt6360_charger.o 85 + obj-$(CONFIG_CHARGER_MT6370) += mt6370-charger.o 85 86 obj-$(CONFIG_CHARGER_QCOM_SMBB) += qcom_smbb.o 86 87 obj-$(CONFIG_CHARGER_BQ2415X) += bq2415x_charger.o 87 88 obj-$(CONFIG_CHARGER_BQ24190) += bq24190_charger.o
-8
drivers/power/supply/ab8500_chargalg.c
··· 252 252 POWER_SUPPLY_PROP_HEALTH, 253 253 }; 254 254 255 - struct ab8500_chargalg_sysfs_entry { 256 - struct attribute attr; 257 - ssize_t (*show)(struct ab8500_chargalg *di, char *buf); 258 - ssize_t (*store)(struct ab8500_chargalg *di, const char *buf, size_t length); 259 - }; 260 - 261 255 /** 262 256 * ab8500_chargalg_safety_timer_expired() - Expiration of the safety timer 263 257 * @timer: pointer to the hrtimer structure ··· 484 490 static int ab8500_chargalg_ac_en(struct ab8500_chargalg *di, int enable, 485 491 int vset_uv, int iset_ua) 486 492 { 487 - static int ab8500_chargalg_ex_ac_enable_toggle; 488 - 489 493 if (!di->ac_chg || !di->ac_chg->ops.enable) 490 494 return -ENXIO; 491 495
+6 -3
drivers/power/supply/adp5061.c
··· 427 427 if (ret < 0) 428 428 return ret; 429 429 430 - chg_type = adp5061_chg_type[ADP5061_CHG_STATUS_1_CHG_STATUS(status1)]; 431 - if (chg_type > ADP5061_CHG_FAST_CV) 430 + chg_type = ADP5061_CHG_STATUS_1_CHG_STATUS(status1); 431 + if (chg_type >= ARRAY_SIZE(adp5061_chg_type)) 432 432 val->intval = POWER_SUPPLY_STATUS_UNKNOWN; 433 433 else 434 - val->intval = chg_type; 434 + val->intval = adp5061_chg_type[chg_type]; 435 435 436 436 return ret; 437 437 } ··· 492 492 break; 493 493 case 0x4: /* VBAT_SNS > VWEAK */ 494 494 val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL; 495 + break; 496 + default: 497 + val->intval = POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN; 495 498 break; 496 499 } 497 500
+30
drivers/power/supply/bq25890_charger.c
··· 613 613 return 0; 614 614 } 615 615 616 + static int bq25890_power_supply_set_property(struct power_supply *psy, 617 + enum power_supply_property psp, 618 + const union power_supply_propval *val) 619 + { 620 + struct bq25890_device *bq = power_supply_get_drvdata(psy); 621 + u8 lval; 622 + 623 + switch (psp) { 624 + case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 625 + lval = bq25890_find_idx(val->intval, TBL_IINLIM); 626 + return bq25890_field_write(bq, F_IINLIM, lval); 627 + default: 628 + return -EINVAL; 629 + } 630 + } 631 + 632 + static int bq25890_power_supply_property_is_writeable(struct power_supply *psy, 633 + enum power_supply_property psp) 634 + { 635 + switch (psp) { 636 + case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 637 + return true; 638 + default: 639 + return false; 640 + } 641 + } 642 + 616 643 /* On the BQ25892 try to get charger-type info from our supplier */ 617 644 static void bq25890_charger_external_power_changed(struct power_supply *psy) 618 645 { ··· 901 874 .properties = bq25890_power_supply_props, 902 875 .num_properties = ARRAY_SIZE(bq25890_power_supply_props), 903 876 .get_property = bq25890_power_supply_get_property, 877 + .set_property = bq25890_power_supply_set_property, 878 + .property_is_writeable = bq25890_power_supply_property_is_writeable, 904 879 .external_power_changed = bq25890_charger_external_power_changed, 905 880 }; 906 881 ··· 975 946 976 947 return; 977 948 error_print: 949 + bq25890_field_write(bq, F_PUMPX_EN, 0); 978 950 dev_err(bq->dev, "Failed to request hi-voltage charging\n"); 979 951 } 980 952
+30 -30
drivers/power/supply/bq27xxx_battery.c
··· 868 868 BQ27XXX_DM_TERMINATE_VOLTAGE, 869 869 }; 870 870 871 - #define bq27000_dm_regs 0 872 - #define bq27010_dm_regs 0 873 - #define bq2750x_dm_regs 0 874 - #define bq2751x_dm_regs 0 875 - #define bq2752x_dm_regs 0 871 + #define bq27000_dm_regs NULL 872 + #define bq27010_dm_regs NULL 873 + #define bq2750x_dm_regs NULL 874 + #define bq2751x_dm_regs NULL 875 + #define bq2752x_dm_regs NULL 876 876 877 877 #if 0 /* not yet tested */ 878 878 static struct bq27xxx_dm_reg bq27500_dm_regs[] = { ··· 881 881 [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 80, 48, 2, 1000, 32767 }, 882 882 }; 883 883 #else 884 - #define bq27500_dm_regs 0 884 + #define bq27500_dm_regs NULL 885 885 #endif 886 886 887 887 /* todo create data memory definitions from datasheets and test on chips */ 888 - #define bq27510g1_dm_regs 0 889 - #define bq27510g2_dm_regs 0 890 - #define bq27510g3_dm_regs 0 891 - #define bq27520g1_dm_regs 0 892 - #define bq27520g2_dm_regs 0 893 - #define bq27520g3_dm_regs 0 894 - #define bq27520g4_dm_regs 0 895 - #define bq27521_dm_regs 0 896 - #define bq27530_dm_regs 0 897 - #define bq27531_dm_regs 0 898 - #define bq27541_dm_regs 0 899 - #define bq27542_dm_regs 0 900 - #define bq27546_dm_regs 0 901 - #define bq27742_dm_regs 0 888 + #define bq27510g1_dm_regs NULL 889 + #define bq27510g2_dm_regs NULL 890 + #define bq27510g3_dm_regs NULL 891 + #define bq27520g1_dm_regs NULL 892 + #define bq27520g2_dm_regs NULL 893 + #define bq27520g3_dm_regs NULL 894 + #define bq27520g4_dm_regs NULL 895 + #define bq27521_dm_regs NULL 896 + #define bq27530_dm_regs NULL 897 + #define bq27531_dm_regs NULL 898 + #define bq27541_dm_regs NULL 899 + #define bq27542_dm_regs NULL 900 + #define bq27546_dm_regs NULL 901 + #define bq27742_dm_regs NULL 902 902 903 903 #if 0 /* not yet tested */ 904 904 static struct bq27xxx_dm_reg bq27545_dm_regs[] = { ··· 907 907 [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 80, 67, 2, 2800, 3700 }, 908 908 }; 909 909 #else 910 - #define bq27545_dm_regs 0 910 + #define bq27545_dm_regs NULL 911 911 #endif 912 912 913 913 static struct bq27xxx_dm_reg bq27411_dm_regs[] = { ··· 937 937 #if 0 /* not yet tested */ 938 938 #define bq27441_dm_regs bq27421_dm_regs 939 939 #else 940 - #define bq27441_dm_regs 0 940 + #define bq27441_dm_regs NULL 941 941 #endif 942 942 943 943 #if 0 /* not yet tested */ ··· 947 947 [BQ27XXX_DM_TERMINATE_VOLTAGE] = { 82, 9, 2, 2500, 3700 }, 948 948 }; 949 949 #else 950 - #define bq27621_dm_regs 0 950 + #define bq27621_dm_regs NULL 951 951 #endif 952 952 953 - #define bq27z561_dm_regs 0 954 - #define bq28z610_dm_regs 0 955 - #define bq34z100_dm_regs 0 956 - #define bq78z100_dm_regs 0 953 + #define bq27z561_dm_regs NULL 954 + #define bq28z610_dm_regs NULL 955 + #define bq34z100_dm_regs NULL 956 + #define bq78z100_dm_regs NULL 957 957 958 958 #define BQ27XXX_O_ZERO BIT(0) 959 959 #define BQ27XXX_O_OTDC BIT(1) /* has OTC/OTD overtemperature flags */ ··· 1044 1044 .block = (di)->dm_regs[i].offset / BQ27XXX_DM_SZ, \ 1045 1045 } 1046 1046 1047 - static inline u16 *bq27xxx_dm_reg_ptr(struct bq27xxx_dm_buf *buf, 1047 + static inline __be16 *bq27xxx_dm_reg_ptr(struct bq27xxx_dm_buf *buf, 1048 1048 struct bq27xxx_dm_reg *reg) 1049 1049 { 1050 1050 if (buf->class == reg->subclass_id && 1051 1051 buf->block == reg->offset / BQ27XXX_DM_SZ) 1052 - return (u16 *) (buf->data + reg->offset % BQ27XXX_DM_SZ); 1052 + return (__be16 *) (buf->data + reg->offset % BQ27XXX_DM_SZ); 1053 1053 1054 1054 return NULL; 1055 1055 } ··· 1275 1275 { 1276 1276 struct bq27xxx_dm_reg *reg = &di->dm_regs[reg_id]; 1277 1277 const char *str = bq27xxx_dm_reg_name[reg_id]; 1278 - u16 *prev = bq27xxx_dm_reg_ptr(buf, reg); 1278 + __be16 *prev = bq27xxx_dm_reg_ptr(buf, reg); 1279 1279 1280 1280 if (prev == NULL) { 1281 1281 dev_warn(di->dev, "buffer does not match %s dm spec\n", str);
+1 -1
drivers/power/supply/cpcap-charger.c
··· 5 5 * Copyright (C) 2017 Tony Lindgren <tony@atomide.com> 6 6 * 7 7 * Rewritten for Linux power framework with some parts based on 8 - * on earlier driver found in the Motorola Linux kernel: 8 + * earlier driver found in the Motorola Linux kernel: 9 9 * 10 10 * Copyright (C) 2009-2010 Motorola, Inc. 11 11 */
+3 -10
drivers/power/supply/cw2015_battery.c
··· 21 21 #include <linux/regmap.h> 22 22 #include <linux/time.h> 23 23 #include <linux/workqueue.h> 24 + #include <linux/devm-helpers.h> 24 25 25 26 #define CW2015_SIZE_BATINFO 64 26 27 ··· 699 698 } 700 699 701 700 cw_bat->battery_workqueue = create_singlethread_workqueue("rk_battery"); 702 - INIT_DELAYED_WORK(&cw_bat->battery_delay_work, cw_bat_work); 701 + devm_delayed_work_autocancel(&client->dev, 702 + &cw_bat->battery_delay_work, cw_bat_work); 703 703 queue_delayed_work(cw_bat->battery_workqueue, 704 704 &cw_bat->battery_delay_work, msecs_to_jiffies(10)); 705 705 return 0; ··· 727 725 728 726 static SIMPLE_DEV_PM_OPS(cw_bat_pm_ops, cw_bat_suspend, cw_bat_resume); 729 727 730 - static void cw_bat_remove(struct i2c_client *client) 731 - { 732 - struct cw_battery *cw_bat = i2c_get_clientdata(client); 733 - 734 - cancel_delayed_work_sync(&cw_bat->battery_delay_work); 735 - power_supply_put_battery_info(cw_bat->rk_bat, cw_bat->battery); 736 - } 737 - 738 728 static const struct i2c_device_id cw_bat_id_table[] = { 739 729 { "cw2015", 0 }, 740 730 { } ··· 745 751 .pm = &cw_bat_pm_ops, 746 752 }, 747 753 .probe_new = cw_bat_probe, 748 - .remove = cw_bat_remove, 749 754 .id_table = cw_bat_id_table, 750 755 }; 751 756
+1 -1
drivers/power/supply/max1721x_battery.c
··· 444 444 445 445 MODULE_LICENSE("GPL"); 446 446 MODULE_AUTHOR("Alex A. Mihaylov <minimumlaw@rambler.ru>"); 447 - MODULE_DESCRIPTION("Maxim MAX17211/MAX17215 Fuel Gauage IC driver"); 447 + MODULE_DESCRIPTION("Maxim MAX17211/MAX17215 Fuel Gauge IC driver"); 448 448 MODULE_ALIAS("w1-family-" __stringify(W1_MAX1721X_FAMILY_ID));
+961
drivers/power/supply/mt6370-charger.c
··· 1 + // SPDX-License-Identifier: GPL-2.0-only 2 + /* 3 + * Copyright (C) 2022 Richtek Technology Corp. 4 + * 5 + * Author: ChiaEn Wu <chiaen_wu@richtek.com> 6 + */ 7 + 8 + #include <linux/bitfield.h> 9 + #include <linux/bits.h> 10 + #include <linux/devm-helpers.h> 11 + #include <linux/gpio/consumer.h> 12 + #include <linux/iio/consumer.h> 13 + #include <linux/init.h> 14 + #include <linux/interrupt.h> 15 + #include <linux/kernel.h> 16 + #include <linux/linear_range.h> 17 + #include <linux/module.h> 18 + #include <linux/of.h> 19 + #include <linux/platform_device.h> 20 + #include <linux/power_supply.h> 21 + #include <linux/regmap.h> 22 + #include <linux/regulator/driver.h> 23 + #include <linux/workqueue.h> 24 + 25 + #define MT6370_REG_CHG_CTRL1 0x111 26 + #define MT6370_REG_CHG_CTRL2 0x112 27 + #define MT6370_REG_CHG_CTRL3 0x113 28 + #define MT6370_REG_CHG_CTRL4 0x114 29 + #define MT6370_REG_CHG_CTRL5 0x115 30 + #define MT6370_REG_CHG_CTRL6 0x116 31 + #define MT6370_REG_CHG_CTRL7 0x117 32 + #define MT6370_REG_CHG_CTRL8 0x118 33 + #define MT6370_REG_CHG_CTRL9 0x119 34 + #define MT6370_REG_CHG_CTRL10 0x11A 35 + #define MT6370_REG_DEVICE_TYPE 0x122 36 + #define MT6370_REG_USB_STATUS1 0x127 37 + #define MT6370_REG_CHG_STAT 0x14A 38 + #define MT6370_REG_FLED_EN 0x17E 39 + #define MT6370_REG_CHG_STAT1 0X1D0 40 + #define MT6370_REG_OVPCTRL_STAT 0x1D8 41 + 42 + #define MT6370_VOBST_MASK GENMASK(7, 2) 43 + #define MT6370_OTG_PIN_EN_MASK BIT(1) 44 + #define MT6370_OPA_MODE_MASK BIT(0) 45 + #define MT6370_OTG_OC_MASK GENMASK(2, 0) 46 + 47 + #define MT6370_MIVR_IBUS_TH_100_mA 100000 48 + #define MT6370_ADC_CHAN_IBUS 5 49 + #define MT6370_ADC_CHAN_MAX 9 50 + 51 + enum mt6370_chg_reg_field { 52 + /* MT6370_REG_CHG_CTRL2 */ 53 + F_IINLMTSEL, F_CFO_EN, F_CHG_EN, 54 + /* MT6370_REG_CHG_CTRL3 */ 55 + F_IAICR, F_AICR_EN, F_ILIM_EN, 56 + /* MT6370_REG_CHG_CTRL4 */ 57 + F_VOREG, 58 + /* MT6370_REG_CHG_CTRL6 */ 59 + F_VMIVR, 60 + /* MT6370_REG_CHG_CTRL7 */ 61 + F_ICHG, 62 + /* MT6370_REG_CHG_CTRL8 */ 63 + F_IPREC, 64 + /* MT6370_REG_CHG_CTRL9 */ 65 + F_IEOC, 66 + /* MT6370_REG_DEVICE_TYPE */ 67 + F_USBCHGEN, 68 + /* MT6370_REG_USB_STATUS1 */ 69 + F_USB_STAT, F_CHGDET, 70 + /* MT6370_REG_CHG_STAT */ 71 + F_CHG_STAT, F_BOOST_STAT, F_VBAT_LVL, 72 + /* MT6370_REG_FLED_EN */ 73 + F_FL_STROBE, 74 + /* MT6370_REG_CHG_STAT1 */ 75 + F_CHG_MIVR_STAT, 76 + /* MT6370_REG_OVPCTRL_STAT */ 77 + F_UVP_D_STAT, 78 + F_MAX 79 + }; 80 + 81 + enum mt6370_irq { 82 + MT6370_IRQ_ATTACH_I = 0, 83 + MT6370_IRQ_UVP_D_EVT, 84 + MT6370_IRQ_MIVR, 85 + MT6370_IRQ_MAX 86 + }; 87 + 88 + struct mt6370_priv { 89 + struct device *dev; 90 + struct iio_channel *iio_adcs; 91 + struct mutex attach_lock; 92 + struct power_supply *psy; 93 + struct regmap *regmap; 94 + struct regmap_field *rmap_fields[F_MAX]; 95 + struct regulator_dev *rdev; 96 + struct workqueue_struct *wq; 97 + struct work_struct bc12_work; 98 + struct delayed_work mivr_dwork; 99 + unsigned int irq_nums[MT6370_IRQ_MAX]; 100 + int attach; 101 + int psy_usb_type; 102 + bool pwr_rdy; 103 + }; 104 + 105 + enum mt6370_usb_status { 106 + MT6370_USB_STAT_NO_VBUS = 0, 107 + MT6370_USB_STAT_VBUS_FLOW_IS_UNDER_GOING, 108 + MT6370_USB_STAT_SDP, 109 + MT6370_USB_STAT_SDP_NSTD, 110 + MT6370_USB_STAT_DCP, 111 + MT6370_USB_STAT_CDP, 112 + MT6370_USB_STAT_MAX 113 + }; 114 + 115 + struct mt6370_chg_field { 116 + const char *name; 117 + const struct linear_range *range; 118 + struct reg_field field; 119 + }; 120 + 121 + enum { 122 + MT6370_RANGE_F_IAICR = 0, 123 + MT6370_RANGE_F_VOREG, 124 + MT6370_RANGE_F_VMIVR, 125 + MT6370_RANGE_F_ICHG, 126 + MT6370_RANGE_F_IPREC, 127 + MT6370_RANGE_F_IEOC, 128 + MT6370_RANGE_F_MAX 129 + }; 130 + 131 + static const struct linear_range mt6370_chg_ranges[MT6370_RANGE_F_MAX] = { 132 + LINEAR_RANGE_IDX(MT6370_RANGE_F_IAICR, 100000, 0x0, 0x3F, 50000), 133 + LINEAR_RANGE_IDX(MT6370_RANGE_F_VOREG, 3900000, 0x0, 0x51, 10000), 134 + LINEAR_RANGE_IDX(MT6370_RANGE_F_VMIVR, 3900000, 0x0, 0x5F, 100000), 135 + LINEAR_RANGE_IDX(MT6370_RANGE_F_ICHG, 900000, 0x08, 0x31, 100000), 136 + LINEAR_RANGE_IDX(MT6370_RANGE_F_IPREC, 100000, 0x0, 0x0F, 50000), 137 + LINEAR_RANGE_IDX(MT6370_RANGE_F_IEOC, 100000, 0x0, 0x0F, 50000), 138 + }; 139 + 140 + #define MT6370_CHG_FIELD(_fd, _reg, _lsb, _msb) \ 141 + [_fd] = { \ 142 + .name = #_fd, \ 143 + .range = NULL, \ 144 + .field = REG_FIELD(_reg, _lsb, _msb), \ 145 + } 146 + 147 + #define MT6370_CHG_FIELD_RANGE(_fd, _reg, _lsb, _msb) \ 148 + [_fd] = { \ 149 + .name = #_fd, \ 150 + .range = &mt6370_chg_ranges[MT6370_RANGE_##_fd], \ 151 + .field = REG_FIELD(_reg, _lsb, _msb), \ 152 + } 153 + 154 + static const struct mt6370_chg_field mt6370_chg_fields[F_MAX] = { 155 + MT6370_CHG_FIELD(F_IINLMTSEL, MT6370_REG_CHG_CTRL2, 2, 3), 156 + MT6370_CHG_FIELD(F_CFO_EN, MT6370_REG_CHG_CTRL2, 1, 1), 157 + MT6370_CHG_FIELD(F_CHG_EN, MT6370_REG_CHG_CTRL2, 0, 0), 158 + MT6370_CHG_FIELD_RANGE(F_IAICR, MT6370_REG_CHG_CTRL3, 2, 7), 159 + MT6370_CHG_FIELD(F_AICR_EN, MT6370_REG_CHG_CTRL3, 1, 1), 160 + MT6370_CHG_FIELD(F_ILIM_EN, MT6370_REG_CHG_CTRL3, 0, 0), 161 + MT6370_CHG_FIELD_RANGE(F_VOREG, MT6370_REG_CHG_CTRL4, 1, 7), 162 + MT6370_CHG_FIELD_RANGE(F_VMIVR, MT6370_REG_CHG_CTRL6, 1, 7), 163 + MT6370_CHG_FIELD_RANGE(F_ICHG, MT6370_REG_CHG_CTRL7, 2, 7), 164 + MT6370_CHG_FIELD_RANGE(F_IPREC, MT6370_REG_CHG_CTRL8, 0, 3), 165 + MT6370_CHG_FIELD_RANGE(F_IEOC, MT6370_REG_CHG_CTRL9, 4, 7), 166 + MT6370_CHG_FIELD(F_USBCHGEN, MT6370_REG_DEVICE_TYPE, 7, 7), 167 + MT6370_CHG_FIELD(F_USB_STAT, MT6370_REG_USB_STATUS1, 4, 6), 168 + MT6370_CHG_FIELD(F_CHGDET, MT6370_REG_USB_STATUS1, 3, 3), 169 + MT6370_CHG_FIELD(F_CHG_STAT, MT6370_REG_CHG_STAT, 6, 7), 170 + MT6370_CHG_FIELD(F_BOOST_STAT, MT6370_REG_CHG_STAT, 3, 3), 171 + MT6370_CHG_FIELD(F_VBAT_LVL, MT6370_REG_CHG_STAT, 5, 5), 172 + MT6370_CHG_FIELD(F_FL_STROBE, MT6370_REG_FLED_EN, 2, 2), 173 + MT6370_CHG_FIELD(F_CHG_MIVR_STAT, MT6370_REG_CHG_STAT1, 6, 6), 174 + MT6370_CHG_FIELD(F_UVP_D_STAT, MT6370_REG_OVPCTRL_STAT, 4, 4), 175 + }; 176 + 177 + static inline int mt6370_chg_field_get(struct mt6370_priv *priv, 178 + enum mt6370_chg_reg_field fd, 179 + unsigned int *val) 180 + { 181 + int ret; 182 + unsigned int reg_val; 183 + 184 + ret = regmap_field_read(priv->rmap_fields[fd], &reg_val); 185 + if (ret) 186 + return ret; 187 + 188 + if (mt6370_chg_fields[fd].range) 189 + return linear_range_get_value(mt6370_chg_fields[fd].range, 190 + reg_val, val); 191 + 192 + *val = reg_val; 193 + return 0; 194 + } 195 + 196 + static inline int mt6370_chg_field_set(struct mt6370_priv *priv, 197 + enum mt6370_chg_reg_field fd, 198 + unsigned int val) 199 + { 200 + int ret; 201 + bool f; 202 + const struct linear_range *r; 203 + 204 + if (mt6370_chg_fields[fd].range) { 205 + r = mt6370_chg_fields[fd].range; 206 + 207 + if (fd == F_VMIVR) { 208 + ret = linear_range_get_selector_high(r, val, &val, &f); 209 + if (ret) 210 + val = r->max_sel; 211 + } else { 212 + linear_range_get_selector_within(r, val, &val); 213 + } 214 + } 215 + 216 + return regmap_field_write(priv->rmap_fields[fd], val); 217 + } 218 + 219 + enum { 220 + MT6370_CHG_STAT_READY = 0, 221 + MT6370_CHG_STAT_CHARGE_IN_PROGRESS, 222 + MT6370_CHG_STAT_DONE, 223 + MT6370_CHG_STAT_FAULT, 224 + MT6370_CHG_STAT_MAX 225 + }; 226 + 227 + enum { 228 + MT6370_ATTACH_STAT_DETACH = 0, 229 + MT6370_ATTACH_STAT_ATTACH_WAIT_FOR_BC12, 230 + MT6370_ATTACH_STAT_ATTACH_BC12_DONE, 231 + MT6370_ATTACH_STAT_ATTACH_MAX 232 + }; 233 + 234 + static int mt6370_chg_otg_of_parse_cb(struct device_node *of, 235 + const struct regulator_desc *rdesc, 236 + struct regulator_config *rcfg) 237 + { 238 + struct mt6370_priv *priv = rcfg->driver_data; 239 + 240 + rcfg->ena_gpiod = fwnode_gpiod_get_index(of_fwnode_handle(of), 241 + "enable", 0, GPIOD_OUT_LOW | 242 + GPIOD_FLAGS_BIT_NONEXCLUSIVE, 243 + rdesc->name); 244 + if (IS_ERR(rcfg->ena_gpiod)) { 245 + rcfg->ena_gpiod = NULL; 246 + return 0; 247 + } 248 + 249 + return regmap_update_bits(priv->regmap, MT6370_REG_CHG_CTRL1, 250 + MT6370_OTG_PIN_EN_MASK, 251 + MT6370_OTG_PIN_EN_MASK); 252 + } 253 + 254 + static void mt6370_chg_bc12_work_func(struct work_struct *work) 255 + { 256 + struct mt6370_priv *priv = container_of(work, struct mt6370_priv, 257 + bc12_work); 258 + int ret; 259 + bool rpt_psy = false; 260 + unsigned int attach, usb_stat; 261 + 262 + mutex_lock(&priv->attach_lock); 263 + attach = priv->attach; 264 + 265 + switch (attach) { 266 + case MT6370_ATTACH_STAT_DETACH: 267 + usb_stat = 0; 268 + break; 269 + case MT6370_ATTACH_STAT_ATTACH_WAIT_FOR_BC12: 270 + ret = mt6370_chg_field_set(priv, F_USBCHGEN, attach); 271 + if (ret) 272 + dev_err(priv->dev, "Failed to enable USB CHG EN\n"); 273 + goto bc12_work_func_out; 274 + case MT6370_ATTACH_STAT_ATTACH_BC12_DONE: 275 + ret = mt6370_chg_field_get(priv, F_USB_STAT, &usb_stat); 276 + if (ret) { 277 + dev_err(priv->dev, "Failed to get USB status\n"); 278 + goto bc12_work_func_out; 279 + } 280 + break; 281 + default: 282 + dev_err(priv->dev, "Invalid attach state\n"); 283 + goto bc12_work_func_out; 284 + } 285 + 286 + rpt_psy = true; 287 + 288 + switch (usb_stat) { 289 + case MT6370_USB_STAT_SDP: 290 + case MT6370_USB_STAT_SDP_NSTD: 291 + priv->psy_usb_type = POWER_SUPPLY_USB_TYPE_SDP; 292 + break; 293 + case MT6370_USB_STAT_DCP: 294 + priv->psy_usb_type = POWER_SUPPLY_USB_TYPE_DCP; 295 + break; 296 + case MT6370_USB_STAT_CDP: 297 + priv->psy_usb_type = POWER_SUPPLY_USB_TYPE_CDP; 298 + break; 299 + case MT6370_USB_STAT_NO_VBUS: 300 + case MT6370_USB_STAT_VBUS_FLOW_IS_UNDER_GOING: 301 + default: 302 + priv->psy_usb_type = POWER_SUPPLY_USB_TYPE_UNKNOWN; 303 + break; 304 + } 305 + 306 + bc12_work_func_out: 307 + mutex_unlock(&priv->attach_lock); 308 + 309 + if (rpt_psy) 310 + power_supply_changed(priv->psy); 311 + } 312 + 313 + static int mt6370_chg_toggle_cfo(struct mt6370_priv *priv) 314 + { 315 + int ret; 316 + unsigned int fl_strobe; 317 + 318 + /* check if flash led in strobe mode */ 319 + ret = mt6370_chg_field_get(priv, F_FL_STROBE, &fl_strobe); 320 + if (ret) { 321 + dev_err(priv->dev, "Failed to get FL_STROBE_EN\n"); 322 + return ret; 323 + } 324 + 325 + if (fl_strobe) { 326 + dev_err(priv->dev, "Flash led is still in strobe mode\n"); 327 + return ret; 328 + } 329 + 330 + /* cfo off */ 331 + ret = mt6370_chg_field_set(priv, F_CFO_EN, 0); 332 + if (ret) { 333 + dev_err(priv->dev, "Failed to disable CFO_EN\n"); 334 + return ret; 335 + } 336 + 337 + /* cfo on */ 338 + ret = mt6370_chg_field_set(priv, F_CFO_EN, 1); 339 + if (ret) 340 + dev_err(priv->dev, "Failed to enable CFO_EN\n"); 341 + 342 + return ret; 343 + } 344 + 345 + static int mt6370_chg_read_adc_chan(struct mt6370_priv *priv, unsigned int chan, 346 + int *val) 347 + { 348 + int ret; 349 + 350 + if (chan >= MT6370_ADC_CHAN_MAX) 351 + return -EINVAL; 352 + 353 + ret = iio_read_channel_processed(&priv->iio_adcs[chan], val); 354 + if (ret) 355 + dev_err(priv->dev, "Failed to read ADC\n"); 356 + 357 + return ret; 358 + } 359 + 360 + static void mt6370_chg_mivr_dwork_func(struct work_struct *work) 361 + { 362 + struct mt6370_priv *priv = container_of(work, struct mt6370_priv, 363 + mivr_dwork.work); 364 + int ret; 365 + unsigned int mivr_stat, ibus; 366 + 367 + ret = mt6370_chg_field_get(priv, F_CHG_MIVR_STAT, &mivr_stat); 368 + if (ret) { 369 + dev_err(priv->dev, "Failed to get mivr state\n"); 370 + goto mivr_handler_out; 371 + } 372 + 373 + if (!mivr_stat) 374 + goto mivr_handler_out; 375 + 376 + ret = mt6370_chg_read_adc_chan(priv, MT6370_ADC_CHAN_IBUS, &ibus); 377 + if (ret) { 378 + dev_err(priv->dev, "Failed to get ibus\n"); 379 + goto mivr_handler_out; 380 + } 381 + 382 + if (ibus < MT6370_MIVR_IBUS_TH_100_mA) { 383 + ret = mt6370_chg_toggle_cfo(priv); 384 + if (ret) 385 + dev_err(priv->dev, "Failed to toggle cfo\n"); 386 + } 387 + 388 + mivr_handler_out: 389 + enable_irq(priv->irq_nums[MT6370_IRQ_MIVR]); 390 + pm_relax(priv->dev); 391 + } 392 + 393 + static void mt6370_chg_pwr_rdy_check(struct mt6370_priv *priv) 394 + { 395 + int ret; 396 + unsigned int opposite_pwr_rdy, otg_en; 397 + union power_supply_propval val; 398 + 399 + /* Check in OTG mode or not */ 400 + ret = mt6370_chg_field_get(priv, F_BOOST_STAT, &otg_en); 401 + if (ret) { 402 + dev_err(priv->dev, "Failed to get OTG state\n"); 403 + return; 404 + } 405 + 406 + if (otg_en) 407 + return; 408 + 409 + ret = mt6370_chg_field_get(priv, F_UVP_D_STAT, &opposite_pwr_rdy); 410 + if (ret) { 411 + dev_err(priv->dev, "Failed to get opposite power ready state\n"); 412 + return; 413 + } 414 + 415 + val.intval = opposite_pwr_rdy ? 416 + MT6370_ATTACH_STAT_DETACH : 417 + MT6370_ATTACH_STAT_ATTACH_WAIT_FOR_BC12; 418 + 419 + ret = power_supply_set_property(priv->psy, POWER_SUPPLY_PROP_ONLINE, 420 + &val); 421 + if (ret) 422 + dev_err(priv->dev, "Failed to start attach/detach flow\n"); 423 + } 424 + 425 + static int mt6370_chg_get_online(struct mt6370_priv *priv, 426 + union power_supply_propval *val) 427 + { 428 + mutex_lock(&priv->attach_lock); 429 + val->intval = !!priv->attach; 430 + mutex_unlock(&priv->attach_lock); 431 + 432 + return 0; 433 + } 434 + 435 + static int mt6370_chg_get_status(struct mt6370_priv *priv, 436 + union power_supply_propval *val) 437 + { 438 + int ret; 439 + unsigned int chg_stat; 440 + union power_supply_propval online; 441 + 442 + ret = power_supply_get_property(priv->psy, POWER_SUPPLY_PROP_ONLINE, 443 + &online); 444 + if (ret) { 445 + dev_err(priv->dev, "Failed to get online status\n"); 446 + return ret; 447 + } 448 + 449 + if (!online.intval) { 450 + val->intval = POWER_SUPPLY_STATUS_DISCHARGING; 451 + return 0; 452 + } 453 + 454 + ret = mt6370_chg_field_get(priv, F_CHG_STAT, &chg_stat); 455 + if (ret) 456 + return ret; 457 + 458 + switch (chg_stat) { 459 + case MT6370_CHG_STAT_READY: 460 + case MT6370_CHG_STAT_FAULT: 461 + val->intval = POWER_SUPPLY_STATUS_NOT_CHARGING; 462 + return ret; 463 + case MT6370_CHG_STAT_CHARGE_IN_PROGRESS: 464 + val->intval = POWER_SUPPLY_STATUS_CHARGING; 465 + return ret; 466 + case MT6370_CHG_STAT_DONE: 467 + val->intval = POWER_SUPPLY_STATUS_FULL; 468 + return ret; 469 + default: 470 + val->intval = POWER_SUPPLY_STATUS_UNKNOWN; 471 + return ret; 472 + } 473 + } 474 + 475 + static int mt6370_chg_get_charge_type(struct mt6370_priv *priv, 476 + union power_supply_propval *val) 477 + { 478 + int type, ret; 479 + unsigned int chg_stat, vbat_lvl; 480 + 481 + ret = mt6370_chg_field_get(priv, F_CHG_STAT, &chg_stat); 482 + if (ret) 483 + return ret; 484 + 485 + ret = mt6370_chg_field_get(priv, F_VBAT_LVL, &vbat_lvl); 486 + if (ret) 487 + return ret; 488 + 489 + switch (chg_stat) { 490 + case MT6370_CHG_STAT_CHARGE_IN_PROGRESS: 491 + if (vbat_lvl) 492 + type = POWER_SUPPLY_CHARGE_TYPE_FAST; 493 + else 494 + type = POWER_SUPPLY_CHARGE_TYPE_TRICKLE; 495 + break; 496 + case MT6370_CHG_STAT_READY: 497 + case MT6370_CHG_STAT_DONE: 498 + case MT6370_CHG_STAT_FAULT: 499 + default: 500 + type = POWER_SUPPLY_CHARGE_TYPE_NONE; 501 + break; 502 + } 503 + 504 + val->intval = type; 505 + 506 + return 0; 507 + } 508 + 509 + static int mt6370_chg_set_online(struct mt6370_priv *priv, 510 + const union power_supply_propval *val) 511 + { 512 + bool pwr_rdy = !!val->intval; 513 + 514 + mutex_lock(&priv->attach_lock); 515 + if (pwr_rdy == !!priv->attach) { 516 + dev_err(priv->dev, "pwr_rdy is same(%d)\n", pwr_rdy); 517 + mutex_unlock(&priv->attach_lock); 518 + return 0; 519 + } 520 + 521 + priv->attach = pwr_rdy; 522 + mutex_unlock(&priv->attach_lock); 523 + 524 + if (!queue_work(priv->wq, &priv->bc12_work)) 525 + dev_err(priv->dev, "bc12 work has already queued\n"); 526 + 527 + return 0; 528 + } 529 + 530 + static int mt6370_chg_get_property(struct power_supply *psy, 531 + enum power_supply_property psp, 532 + union power_supply_propval *val) 533 + { 534 + struct mt6370_priv *priv = power_supply_get_drvdata(psy); 535 + 536 + switch (psp) { 537 + case POWER_SUPPLY_PROP_ONLINE: 538 + return mt6370_chg_get_online(priv, val); 539 + case POWER_SUPPLY_PROP_STATUS: 540 + return mt6370_chg_get_status(priv, val); 541 + case POWER_SUPPLY_PROP_CHARGE_TYPE: 542 + return mt6370_chg_get_charge_type(priv, val); 543 + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 544 + return mt6370_chg_field_get(priv, F_ICHG, &val->intval); 545 + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX: 546 + val->intval = linear_range_get_max_value(&mt6370_chg_ranges[MT6370_RANGE_F_ICHG]); 547 + return 0; 548 + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 549 + return mt6370_chg_field_get(priv, F_VOREG, &val->intval); 550 + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX: 551 + val->intval = linear_range_get_max_value(&mt6370_chg_ranges[MT6370_RANGE_F_VOREG]); 552 + return 0; 553 + case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 554 + return mt6370_chg_field_get(priv, F_IAICR, &val->intval); 555 + case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT: 556 + return mt6370_chg_field_get(priv, F_VMIVR, &val->intval); 557 + case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 558 + return mt6370_chg_field_get(priv, F_IPREC, &val->intval); 559 + case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 560 + return mt6370_chg_field_get(priv, F_IEOC, &val->intval); 561 + case POWER_SUPPLY_PROP_USB_TYPE: 562 + val->intval = priv->psy_usb_type; 563 + return 0; 564 + default: 565 + return -EINVAL; 566 + } 567 + } 568 + 569 + static int mt6370_chg_set_property(struct power_supply *psy, 570 + enum power_supply_property psp, 571 + const union power_supply_propval *val) 572 + { 573 + struct mt6370_priv *priv = power_supply_get_drvdata(psy); 574 + 575 + switch (psp) { 576 + case POWER_SUPPLY_PROP_ONLINE: 577 + return mt6370_chg_set_online(priv, val); 578 + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 579 + return mt6370_chg_field_set(priv, F_ICHG, val->intval); 580 + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 581 + return mt6370_chg_field_set(priv, F_VOREG, val->intval); 582 + case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 583 + return mt6370_chg_field_set(priv, F_IAICR, val->intval); 584 + case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT: 585 + return mt6370_chg_field_set(priv, F_VMIVR, val->intval); 586 + case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 587 + return mt6370_chg_field_set(priv, F_IPREC, val->intval); 588 + case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 589 + return mt6370_chg_field_set(priv, F_IEOC, val->intval); 590 + default: 591 + return -EINVAL; 592 + } 593 + } 594 + 595 + static int mt6370_chg_property_is_writeable(struct power_supply *psy, 596 + enum power_supply_property psp) 597 + { 598 + switch (psp) { 599 + case POWER_SUPPLY_PROP_ONLINE: 600 + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT: 601 + case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE: 602 + case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT: 603 + case POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT: 604 + case POWER_SUPPLY_PROP_PRECHARGE_CURRENT: 605 + case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT: 606 + return 1; 607 + default: 608 + return 0; 609 + } 610 + } 611 + 612 + static enum power_supply_property mt6370_chg_properties[] = { 613 + POWER_SUPPLY_PROP_ONLINE, 614 + POWER_SUPPLY_PROP_STATUS, 615 + POWER_SUPPLY_PROP_CHARGE_TYPE, 616 + POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT, 617 + POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX, 618 + POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE, 619 + POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX, 620 + POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT, 621 + POWER_SUPPLY_PROP_INPUT_VOLTAGE_LIMIT, 622 + POWER_SUPPLY_PROP_PRECHARGE_CURRENT, 623 + POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT, 624 + POWER_SUPPLY_PROP_USB_TYPE, 625 + }; 626 + 627 + static enum power_supply_usb_type mt6370_chg_usb_types[] = { 628 + POWER_SUPPLY_USB_TYPE_UNKNOWN, 629 + POWER_SUPPLY_USB_TYPE_SDP, 630 + POWER_SUPPLY_USB_TYPE_CDP, 631 + POWER_SUPPLY_USB_TYPE_DCP, 632 + }; 633 + 634 + static const struct power_supply_desc mt6370_chg_psy_desc = { 635 + .name = "mt6370-charger", 636 + .type = POWER_SUPPLY_TYPE_USB, 637 + .properties = mt6370_chg_properties, 638 + .num_properties = ARRAY_SIZE(mt6370_chg_properties), 639 + .get_property = mt6370_chg_get_property, 640 + .set_property = mt6370_chg_set_property, 641 + .property_is_writeable = mt6370_chg_property_is_writeable, 642 + .usb_types = mt6370_chg_usb_types, 643 + .num_usb_types = ARRAY_SIZE(mt6370_chg_usb_types), 644 + }; 645 + 646 + static const struct regulator_ops mt6370_chg_otg_ops = { 647 + .list_voltage = regulator_list_voltage_linear, 648 + .enable = regulator_enable_regmap, 649 + .disable = regulator_disable_regmap, 650 + .is_enabled = regulator_is_enabled_regmap, 651 + .set_voltage_sel = regulator_set_voltage_sel_regmap, 652 + .get_voltage_sel = regulator_get_voltage_sel_regmap, 653 + .set_current_limit = regulator_set_current_limit_regmap, 654 + .get_current_limit = regulator_get_current_limit_regmap, 655 + }; 656 + 657 + static const u32 mt6370_chg_otg_oc_ma[] = { 658 + 500000, 700000, 1100000, 1300000, 1800000, 2100000, 2400000, 659 + }; 660 + 661 + static const struct regulator_desc mt6370_chg_otg_rdesc = { 662 + .of_match = "usb-otg-vbus-regulator", 663 + .of_parse_cb = mt6370_chg_otg_of_parse_cb, 664 + .name = "mt6370-usb-otg-vbus", 665 + .ops = &mt6370_chg_otg_ops, 666 + .owner = THIS_MODULE, 667 + .type = REGULATOR_VOLTAGE, 668 + .min_uV = 4425000, 669 + .uV_step = 25000, 670 + .n_voltages = 57, 671 + .vsel_reg = MT6370_REG_CHG_CTRL5, 672 + .vsel_mask = MT6370_VOBST_MASK, 673 + .enable_reg = MT6370_REG_CHG_CTRL1, 674 + .enable_mask = MT6370_OPA_MODE_MASK, 675 + .curr_table = mt6370_chg_otg_oc_ma, 676 + .n_current_limits = ARRAY_SIZE(mt6370_chg_otg_oc_ma), 677 + .csel_reg = MT6370_REG_CHG_CTRL10, 678 + .csel_mask = MT6370_OTG_OC_MASK, 679 + }; 680 + 681 + static int mt6370_chg_init_rmap_fields(struct mt6370_priv *priv) 682 + { 683 + int i; 684 + const struct mt6370_chg_field *fds = mt6370_chg_fields; 685 + 686 + for (i = 0; i < F_MAX; i++) { 687 + priv->rmap_fields[i] = devm_regmap_field_alloc(priv->dev, 688 + priv->regmap, 689 + fds[i].field); 690 + if (IS_ERR(priv->rmap_fields[i])) 691 + return dev_err_probe(priv->dev, 692 + PTR_ERR(priv->rmap_fields[i]), 693 + "Failed to allocate regmapfield[%s]\n", 694 + fds[i].name); 695 + } 696 + 697 + return 0; 698 + } 699 + 700 + static int mt6370_chg_init_setting(struct mt6370_priv *priv) 701 + { 702 + int ret; 703 + 704 + /* Disable usb_chg_en */ 705 + ret = mt6370_chg_field_set(priv, F_USBCHGEN, 0); 706 + if (ret) { 707 + dev_err(priv->dev, "Failed to disable usb_chg_en\n"); 708 + return ret; 709 + } 710 + 711 + /* Disable input current limit */ 712 + ret = mt6370_chg_field_set(priv, F_ILIM_EN, 0); 713 + if (ret) { 714 + dev_err(priv->dev, "Failed to disable input current limit\n"); 715 + return ret; 716 + } 717 + 718 + /* ICHG/IEOC Workaround, ICHG can not be set less than 900mA */ 719 + ret = mt6370_chg_field_set(priv, F_ICHG, 900000); 720 + if (ret) { 721 + dev_err(priv->dev, "Failed to set ICHG to 900mA"); 722 + return ret; 723 + } 724 + 725 + /* Change input current limit selection to using IAICR results */ 726 + ret = mt6370_chg_field_set(priv, F_IINLMTSEL, 2); 727 + if (ret) { 728 + dev_err(priv->dev, "Failed to set IINLMTSEL\n"); 729 + return ret; 730 + } 731 + 732 + return 0; 733 + } 734 + 735 + #define MT6370_CHG_DT_PROP_DECL(_name, _type, _field) \ 736 + { \ 737 + .name = "mediatek,chg-" #_name, \ 738 + .type = MT6370_PARSE_TYPE_##_type, \ 739 + .fd = _field, \ 740 + } 741 + 742 + static int mt6370_chg_init_otg_regulator(struct mt6370_priv *priv) 743 + { 744 + struct regulator_config rcfg = { 745 + .dev = priv->dev, 746 + .regmap = priv->regmap, 747 + .driver_data = priv, 748 + }; 749 + 750 + priv->rdev = devm_regulator_register(priv->dev, &mt6370_chg_otg_rdesc, 751 + &rcfg); 752 + 753 + return PTR_ERR_OR_ZERO(priv->rdev); 754 + } 755 + 756 + static int mt6370_chg_init_psy(struct mt6370_priv *priv) 757 + { 758 + struct power_supply_config cfg = { 759 + .drv_data = priv, 760 + .of_node = dev_of_node(priv->dev), 761 + }; 762 + 763 + priv->psy = devm_power_supply_register(priv->dev, &mt6370_chg_psy_desc, 764 + &cfg); 765 + 766 + return PTR_ERR_OR_ZERO(priv->psy); 767 + } 768 + 769 + static void mt6370_chg_destroy_attach_lock(void *data) 770 + { 771 + struct mutex *attach_lock = data; 772 + 773 + mutex_destroy(attach_lock); 774 + } 775 + 776 + static void mt6370_chg_destroy_wq(void *data) 777 + { 778 + struct workqueue_struct *wq = data; 779 + 780 + flush_workqueue(wq); 781 + destroy_workqueue(wq); 782 + } 783 + 784 + static irqreturn_t mt6370_attach_i_handler(int irq, void *data) 785 + { 786 + struct mt6370_priv *priv = data; 787 + unsigned int otg_en; 788 + int ret; 789 + 790 + /* Check in OTG mode or not */ 791 + ret = mt6370_chg_field_get(priv, F_BOOST_STAT, &otg_en); 792 + if (ret) { 793 + dev_err(priv->dev, "Failed to get OTG state\n"); 794 + return IRQ_NONE; 795 + } 796 + 797 + if (otg_en) 798 + return IRQ_HANDLED; 799 + 800 + mutex_lock(&priv->attach_lock); 801 + priv->attach = MT6370_ATTACH_STAT_ATTACH_BC12_DONE; 802 + mutex_unlock(&priv->attach_lock); 803 + 804 + if (!queue_work(priv->wq, &priv->bc12_work)) 805 + dev_err(priv->dev, "bc12 work has already queued\n"); 806 + 807 + return IRQ_HANDLED; 808 + } 809 + 810 + static irqreturn_t mt6370_uvp_d_evt_handler(int irq, void *data) 811 + { 812 + struct mt6370_priv *priv = data; 813 + 814 + mt6370_chg_pwr_rdy_check(priv); 815 + 816 + return IRQ_HANDLED; 817 + } 818 + 819 + static irqreturn_t mt6370_mivr_handler(int irq, void *data) 820 + { 821 + struct mt6370_priv *priv = data; 822 + 823 + pm_stay_awake(priv->dev); 824 + disable_irq_nosync(priv->irq_nums[MT6370_IRQ_MIVR]); 825 + schedule_delayed_work(&priv->mivr_dwork, msecs_to_jiffies(200)); 826 + 827 + return IRQ_HANDLED; 828 + } 829 + 830 + #define MT6370_CHG_IRQ(_name) \ 831 + { \ 832 + .name = #_name, \ 833 + .handler = mt6370_##_name##_handler, \ 834 + } 835 + 836 + static int mt6370_chg_init_irq(struct mt6370_priv *priv) 837 + { 838 + int i, ret; 839 + const struct { 840 + char *name; 841 + irq_handler_t handler; 842 + } mt6370_chg_irqs[] = { 843 + MT6370_CHG_IRQ(attach_i), 844 + MT6370_CHG_IRQ(uvp_d_evt), 845 + MT6370_CHG_IRQ(mivr), 846 + }; 847 + 848 + for (i = 0; i < ARRAY_SIZE(mt6370_chg_irqs); i++) { 849 + ret = platform_get_irq_byname(to_platform_device(priv->dev), 850 + mt6370_chg_irqs[i].name); 851 + if (ret < 0) 852 + return dev_err_probe(priv->dev, ret, 853 + "Failed to get irq %s\n", 854 + mt6370_chg_irqs[i].name); 855 + 856 + priv->irq_nums[i] = ret; 857 + ret = devm_request_threaded_irq(priv->dev, ret, NULL, 858 + mt6370_chg_irqs[i].handler, 859 + IRQF_TRIGGER_FALLING, 860 + dev_name(priv->dev), priv); 861 + if (ret) 862 + return dev_err_probe(priv->dev, ret, 863 + "Failed to request irq %s\n", 864 + mt6370_chg_irqs[i].name); 865 + } 866 + 867 + return 0; 868 + } 869 + 870 + static int mt6370_chg_probe(struct platform_device *pdev) 871 + { 872 + struct device *dev = &pdev->dev; 873 + struct mt6370_priv *priv; 874 + int ret; 875 + 876 + priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 877 + if (!priv) 878 + return -ENOMEM; 879 + 880 + priv->dev = &pdev->dev; 881 + 882 + priv->regmap = dev_get_regmap(pdev->dev.parent, NULL); 883 + if (!priv->regmap) 884 + return dev_err_probe(dev, -ENODEV, "Failed to get regmap\n"); 885 + 886 + ret = mt6370_chg_init_rmap_fields(priv); 887 + if (ret) 888 + return dev_err_probe(dev, ret, "Failed to init regmap fields\n"); 889 + 890 + platform_set_drvdata(pdev, priv); 891 + 892 + priv->iio_adcs = devm_iio_channel_get_all(priv->dev); 893 + if (IS_ERR(priv->iio_adcs)) 894 + return dev_err_probe(dev, PTR_ERR(priv->iio_adcs), 895 + "Failed to get iio adc\n"); 896 + 897 + ret = mt6370_chg_init_otg_regulator(priv); 898 + if (ret) 899 + return dev_err_probe(dev, ret, "Failed to init OTG regulator\n"); 900 + 901 + ret = mt6370_chg_init_psy(priv); 902 + if (ret) 903 + return dev_err_probe(dev, ret, "Failed to init psy\n"); 904 + 905 + mutex_init(&priv->attach_lock); 906 + ret = devm_add_action_or_reset(dev, mt6370_chg_destroy_attach_lock, 907 + &priv->attach_lock); 908 + if (ret) 909 + return dev_err_probe(dev, ret, "Failed to init attach lock\n"); 910 + 911 + priv->attach = MT6370_ATTACH_STAT_DETACH; 912 + 913 + priv->wq = create_singlethread_workqueue(dev_name(priv->dev)); 914 + if (!priv->wq) 915 + return dev_err_probe(dev, -ENOMEM, 916 + "Failed to create workqueue\n"); 917 + 918 + ret = devm_add_action_or_reset(dev, mt6370_chg_destroy_wq, priv->wq); 919 + if (ret) 920 + return dev_err_probe(dev, ret, "Failed to init wq\n"); 921 + 922 + ret = devm_work_autocancel(dev, &priv->bc12_work, mt6370_chg_bc12_work_func); 923 + if (ret) 924 + return dev_err_probe(dev, ret, "Failed to init bc12 work\n"); 925 + 926 + ret = devm_delayed_work_autocancel(dev, &priv->mivr_dwork, mt6370_chg_mivr_dwork_func); 927 + if (ret) 928 + return dev_err_probe(dev, ret, "Failed to init mivr delayed work\n"); 929 + 930 + ret = mt6370_chg_init_setting(priv); 931 + if (ret) 932 + return dev_err_probe(dev, ret, 933 + "Failed to init mt6370 charger setting\n"); 934 + 935 + ret = mt6370_chg_init_irq(priv); 936 + if (ret) 937 + return ret; 938 + 939 + mt6370_chg_pwr_rdy_check(priv); 940 + 941 + return 0; 942 + } 943 + 944 + static const struct of_device_id mt6370_chg_of_match[] = { 945 + { .compatible = "mediatek,mt6370-charger", }, 946 + {} 947 + }; 948 + MODULE_DEVICE_TABLE(of, mt6370_chg_of_match); 949 + 950 + static struct platform_driver mt6370_chg_driver = { 951 + .probe = mt6370_chg_probe, 952 + .driver = { 953 + .name = "mt6370-charger", 954 + .of_match_table = mt6370_chg_of_match, 955 + }, 956 + }; 957 + module_platform_driver(mt6370_chg_driver); 958 + 959 + MODULE_AUTHOR("ChiaEn Wu <chiaen_wu@richtek.com>"); 960 + MODULE_DESCRIPTION("MediaTek MT6370 Charger Driver"); 961 + MODULE_LICENSE("GPL v2");
+1 -1
drivers/power/supply/power_supply_sysfs.c
··· 442 442 if (ret == -ENODEV || ret == -ENODATA) { 443 443 /* 444 444 * When a battery is absent, we expect -ENODEV. Don't abort; 445 - * send the uevent with at least the the PRESENT=0 property 445 + * send the uevent with at least the PRESENT=0 property 446 446 */ 447 447 return 0; 448 448 }
+1 -1
drivers/power/supply/tps65217_charger.c
··· 50 50 * tps65217 rev. G, p. 31 (see p. 32 for NTC schematic) 51 51 * 52 52 * The device can be configured to support a 100k NTC (B = 3960) by 53 - * setting the the NTC_TYPE bit in register CHGCONFIG1 to 1. However it 53 + * setting the NTC_TYPE bit in register CHGCONFIG1 to 1. However it 54 54 * is not recommended to do so. In sleep mode, the charger continues 55 55 * charging the battery, but all register values are reset to default 56 56 * values. Therefore, the charger would get the wrong temperature
+11
include/linux/linear_range.h
··· 26 26 unsigned int step; 27 27 }; 28 28 29 + #define LINEAR_RANGE(_min, _min_sel, _max_sel, _step) \ 30 + { \ 31 + .min = _min, \ 32 + .min_sel = _min_sel, \ 33 + .max_sel = _max_sel, \ 34 + .step = _step, \ 35 + } 36 + 37 + #define LINEAR_RANGE_IDX(_idx, _min, _min_sel, _max_sel, _step) \ 38 + [_idx] = LINEAR_RANGE(_min, _min_sel, _max_sel, _step) 39 + 29 40 unsigned int linear_range_values_in_range(const struct linear_range *r); 30 41 unsigned int linear_range_values_in_range_array(const struct linear_range *r, 31 42 int ranges);
+42 -6
include/linux/power_supply.h
··· 374 374 * These timers should be chosen to align with the typical discharge curve 375 375 * for the battery. 376 376 * 377 - * When the main CC/CV charging is complete the battery can optionally be 378 - * maintenance charged at the voltages from this table: a table of settings is 379 - * traversed using a slightly lower current and voltage than what is used for 377 + * Ordinary CC/CV charging will stop charging when the charge current goes 378 + * below charge_term_current_ua, and then restart it (if the device is still 379 + * plugged into the charger) at charge_restart_voltage_uv. This happens in most 380 + * consumer products because the power usage while connected to a charger is 381 + * not zero, and devices are not manufactured to draw power directly from the 382 + * charger: instead they will at all times dissipate the battery a little, like 383 + * the power used in standby mode. This will over time give a charge graph 384 + * such as this: 385 + * 386 + * Energy 387 + * ^ ... ... ... ... ... ... ... 388 + * | . . . . . . . . . . . . . 389 + * | .. . .. . .. . .. . .. . .. . .. 390 + * |. .. .. .. .. .. .. 391 + * +-------------------------------------------------------------------> t 392 + * 393 + * Practically this means that the Li-ions are wandering back and forth in the 394 + * battery and this causes degeneration of the battery anode and cathode. 395 + * To prolong the life of the battery, maintenance charging is applied after 396 + * reaching charge_term_current_ua to hold up the charge in the battery while 397 + * consuming power, thus lowering the wear on the battery: 398 + * 399 + * Energy 400 + * ^ ....................................... 401 + * | . ...................... 402 + * | .. 403 + * |. 404 + * +-------------------------------------------------------------------> t 405 + * 406 + * Maintenance charging uses the voltages from this table: a table of settings 407 + * is traversed using a slightly lower current and voltage than what is used for 380 408 * CC/CV charging. The maintenance charging will for safety reasons not go on 381 409 * indefinately: we lower the current and voltage with successive maintenance 382 410 * settings, then disable charging completely after we reach the last one, ··· 413 385 * ordinary CC/CV charging from there. 414 386 * 415 387 * As an example, a Samsung EB425161LA Lithium-Ion battery is CC/CV charged 416 - * at 900mA to 4340mV, then maintenance charged at 600mA and 4150mV for 417 - * 60 hours, then maintenance charged at 600mA and 4100mV for 200 hours. 388 + * at 900mA to 4340mV, then maintenance charged at 600mA and 4150mV for up to 389 + * 60 hours, then maintenance charged at 600mA and 4100mV for up to 200 hours. 418 390 * After this the charge cycle is restarted waiting for 419 391 * charge_restart_voltage_uv. 420 392 * 421 393 * For most mobile electronics this type of maintenance charging is enough for 422 394 * the user to disconnect the device and make use of it before both maintenance 423 - * charging cycles are complete. 395 + * charging cycles are complete, if the current and voltage has been chosen 396 + * appropriately. These need to be determined from battery discharge curves 397 + * and expected standby current. 398 + * 399 + * If the voltage anyway drops to charge_restart_voltage_uv during maintenance 400 + * charging, ordinary CC/CV charging is restarted. This can happen if the 401 + * device is e.g. actively used during charging, so more current is drawn than 402 + * the expected stand-by current. Also overvoltage protection will be applied 403 + * as usual. 424 404 */ 425 405 struct power_supply_maintenance_charge_table { 426 406 int charge_current_max_ua;