aboutsummaryrefslogtreecommitdiff
path: root/drivers/power/supply/ip5xxx_power.c
diff options
context:
space:
mode:
authorLibravatar Linus Torvalds <torvalds@linux-foundation.org>2023-02-21 18:24:12 -0800
committerLibravatar Linus Torvalds <torvalds@linux-foundation.org>2023-02-21 18:24:12 -0800
commit5b7c4cabbb65f5c469464da6c5f614cbd7f730f2 (patch)
treecc5c2d0a898769fd59549594fedb3ee6f84e59a0 /drivers/power/supply/ip5xxx_power.c
downloadlinux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.tar.gz
linux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.zip
Merge tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-nextgrafted
Pull networking updates from Jakub Kicinski: "Core: - Add dedicated kmem_cache for typical/small skb->head, avoid having to access struct page at kfree time, and improve memory use. - Introduce sysctl to set default RPS configuration for new netdevs. - Define Netlink protocol specification format which can be used to describe messages used by each family and auto-generate parsers. Add tools for generating kernel data structures and uAPI headers. - Expose all net/core sysctls inside netns. - Remove 4s sleep in netpoll if carrier is instantly detected on boot. - Add configurable limit of MDB entries per port, and port-vlan. - Continue populating drop reasons throughout the stack. - Retire a handful of legacy Qdiscs and classifiers. Protocols: - Support IPv4 big TCP (TSO frames larger than 64kB). - Add IP_LOCAL_PORT_RANGE socket option, to control local port range on socket by socket basis. - Track and report in procfs number of MPTCP sockets used. - Support mixing IPv4 and IPv6 flows in the in-kernel MPTCP path manager. - IPv6: don't check net.ipv6.route.max_size and rely on garbage collection to free memory (similarly to IPv4). - Support Penultimate Segment Pop (PSP) flavor in SRv6 (RFC8986). - ICMP: add per-rate limit counters. - Add support for user scanning requests in ieee802154. - Remove static WEP support. - Support minimal Wi-Fi 7 Extremely High Throughput (EHT) rate reporting. - WiFi 7 EHT channel puncturing support (client & AP). BPF: - Add a rbtree data structure following the "next-gen data structure" precedent set by recently added linked list, that is, by using kfunc + kptr instead of adding a new BPF map type. - Expose XDP hints via kfuncs with initial support for RX hash and timestamp metadata. - Add BPF_F_NO_TUNNEL_KEY extension to bpf_skb_set_tunnel_key to better support decap on GRE tunnel devices not operating in collect metadata. - Improve x86 JIT's codegen for PROBE_MEM runtime error checks. - Remove the need for trace_printk_lock for bpf_trace_printk and bpf_trace_vprintk helpers. - Extend libbpf's bpf_tracing.h support for tracing arguments of kprobes/uprobes and syscall as a special case. - Significantly reduce the search time for module symbols by livepatch and BPF. - Enable cpumasks to be used as kptrs, which is useful for tracing programs tracking which tasks end up running on which CPUs in different time intervals. - Add support for BPF trampoline on s390x and riscv64. - Add capability to export the XDP features supported by the NIC. - Add __bpf_kfunc tag for marking kernel functions as kfuncs. - Add cgroup.memory=nobpf kernel parameter option to disable BPF memory accounting for container environments. Netfilter: - Remove the CLUSTERIP target. It has been marked as obsolete for years, and we still have WARN splats wrt races of the out-of-band /proc interface installed by this target. - Add 'destroy' commands to nf_tables. They are identical to the existing 'delete' commands, but do not return an error if the referenced object (set, chain, rule...) did not exist. Driver API: - Improve cpumask_local_spread() locality to help NICs set the right IRQ affinity on AMD platforms. - Separate C22 and C45 MDIO bus transactions more clearly. - Introduce new DCB table to control DSCP rewrite on egress. - Support configuration of Physical Layer Collision Avoidance (PLCA) Reconciliation Sublayer (RS) (802.3cg-2019). Modern version of shared medium Ethernet. - Support for MAC Merge layer (IEEE 802.3-2018 clause 99). Allowing preemption of low priority frames by high priority frames. - Add support for controlling MACSec offload using netlink SET. - Rework devlink instance refcounts to allow registration and de-registration under the instance lock. Split the code into multiple files, drop some of the unnecessarily granular locks and factor out common parts of netlink operation handling. - Add TX frame aggregation parameters (for USB drivers). - Add a new attr TCA_EXT_WARN_MSG to report TC (offload) warning messages with notifications for debug. - Allow offloading of UDP NEW connections via act_ct. - Add support for per action HW stats in TC. - Support hardware miss to TC action (continue processing in SW from a specific point in the action chain). - Warn if old Wireless Extension user space interface is used with modern cfg80211/mac80211 drivers. Do not support Wireless Extensions for Wi-Fi 7 devices at all. Everyone should switch to using nl80211 interface instead. - Improve the CAN bit timing configuration. Use extack to return error messages directly to user space, update the SJW handling, including the definition of a new default value that will benefit CAN-FD controllers, by increasing their oscillator tolerance. New hardware / drivers: - Ethernet: - nVidia BlueField-3 support (control traffic driver) - Ethernet support for imx93 SoCs - Motorcomm yt8531 gigabit Ethernet PHY - onsemi NCN26000 10BASE-T1S PHY (with support for PLCA) - Microchip LAN8841 PHY (incl. cable diagnostics and PTP) - Amlogic gxl MDIO mux - WiFi: - RealTek RTL8188EU (rtl8xxxu) - Qualcomm Wi-Fi 7 devices (ath12k) - CAN: - Renesas R-Car V4H Drivers: - Bluetooth: - Set Per Platform Antenna Gain (PPAG) for Intel controllers. - Ethernet NICs: - Intel (1G, igc): - support TSN / Qbv / packet scheduling features of i226 model - Intel (100G, ice): - use GNSS subsystem instead of TTY - multi-buffer XDP support - extend support for GPIO pins to E823 devices - nVidia/Mellanox: - update the shared buffer configuration on PFC commands - implement PTP adjphase function for HW offset control - TC support for Geneve and GRE with VF tunnel offload - more efficient crypto key management method - multi-port eswitch support - Netronome/Corigine: - add DCB IEEE support - support IPsec offloading for NFP3800 - Freescale/NXP (enetc): - support XDP_REDIRECT for XDP non-linear buffers - improve reconfig, avoid link flap and waiting for idle - support MAC Merge layer - Other NICs: - sfc/ef100: add basic devlink support for ef100 - ionic: rx_push mode operation (writing descriptors via MMIO) - bnxt: use the auxiliary bus abstraction for RDMA - r8169: disable ASPM and reset bus in case of tx timeout - cpsw: support QSGMII mode for J721e CPSW9G - cpts: support pulse-per-second output - ngbe: add an mdio bus driver - usbnet: optimize usbnet_bh() by avoiding unnecessary queuing - r8152: handle devices with FW with NCM support - amd-xgbe: support 10Mbps, 2.5GbE speeds and rx-adaptation - virtio-net: support multi buffer XDP - virtio/vsock: replace virtio_vsock_pkt with sk_buff - tsnep: XDP support - Ethernet high-speed switches: - nVidia/Mellanox (mlxsw): - add support for latency TLV (in FW control messages) - Microchip (sparx5): - separate explicit and implicit traffic forwarding rules, make the implicit rules always active - add support for egress DSCP rewrite - IS0 VCAP support (Ingress Classification) - IS2 VCAP filters (protos, L3 addrs, L4 ports, flags, ToS etc.) - ES2 VCAP support (Egress Access Control) - support for Per-Stream Filtering and Policing (802.1Q, 8.6.5.1) - Ethernet embedded switches: - Marvell (mv88e6xxx): - add MAB (port auth) offload support - enable PTP receive for mv88e6390 - NXP (ocelot): - support MAC Merge layer - support for the the vsc7512 internal copper phys - Microchip: - lan9303: convert to PHYLINK - lan966x: support TC flower filter statistics - lan937x: PTP support for KSZ9563/KSZ8563 and LAN937x - lan937x: support Credit Based Shaper configuration - ksz9477: support Energy Efficient Ethernet - other: - qca8k: convert to regmap read/write API, use bulk operations - rswitch: Improve TX timestamp accuracy - Intel WiFi (iwlwifi): - EHT (Wi-Fi 7) rate reporting - STEP equalizer support: transfer some STEP (connection to radio on platforms with integrated wifi) related parameters from the BIOS to the firmware. - Qualcomm 802.11ax WiFi (ath11k): - IPQ5018 support - Fine Timing Measurement (FTM) responder role support - channel 177 support - MediaTek WiFi (mt76): - per-PHY LED support - mt7996: EHT (Wi-Fi 7) support - Wireless Ethernet Dispatch (WED) reset support - switch to using page pool allocator - RealTek WiFi (rtw89): - support new version of Bluetooth co-existance - Mobile: - rmnet: support TX aggregation" * tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next: (1872 commits) page_pool: add a comment explaining the fragment counter usage net: ethtool: fix __ethtool_dev_mm_supported() implementation ethtool: pse-pd: Fix double word in comments xsk: add linux/vmalloc.h to xsk.c sefltests: netdevsim: wait for devlink instance after netns removal selftest: fib_tests: Always cleanup before exit net/mlx5e: Align IPsec ASO result memory to be as required by hardware net/mlx5e: TC, Set CT miss to the specific ct action instance net/mlx5e: Rename CHAIN_TO_REG to MAPPED_OBJ_TO_REG net/mlx5: Refactor tc miss handling to a single function net/mlx5: Kconfig: Make tc offload depend on tc skb extension net/sched: flower: Support hardware miss to tc action net/sched: flower: Move filter handle initialization earlier net/sched: cls_api: Support hardware miss to tc action net/sched: Rename user cookie and act cookie sfc: fix builds without CONFIG_RTC_LIB sfc: clean up some inconsistent indentings net/mlx4_en: Introduce flexible array to silence overflow warning net: lan966x: Fix possible deadlock inside PTP net/ulp: Remove redundant ->clone() test in inet_clone_ulp(). ...
Diffstat (limited to '')
-rw-r--r--drivers/power/supply/ip5xxx_power.c638
1 files changed, 638 insertions, 0 deletions
diff --git a/drivers/power/supply/ip5xxx_power.c b/drivers/power/supply/ip5xxx_power.c
new file mode 100644
index 000000000..00221e9c0
--- /dev/null
+++ b/drivers/power/supply/ip5xxx_power.c
@@ -0,0 +1,638 @@
+// SPDX-License-Identifier: GPL-2.0
+//
+// Copyright (C) 2021 Samuel Holland <samuel@sholland.org>
+
+#include <linux/i2c.h>
+#include <linux/module.h>
+#include <linux/power_supply.h>
+#include <linux/regmap.h>
+
+#define IP5XXX_SYS_CTL0 0x01
+#define IP5XXX_SYS_CTL0_WLED_DET_EN BIT(4)
+#define IP5XXX_SYS_CTL0_WLED_EN BIT(3)
+#define IP5XXX_SYS_CTL0_BOOST_EN BIT(2)
+#define IP5XXX_SYS_CTL0_CHARGER_EN BIT(1)
+#define IP5XXX_SYS_CTL1 0x02
+#define IP5XXX_SYS_CTL1_LIGHT_SHDN_EN BIT(1)
+#define IP5XXX_SYS_CTL1_LOAD_PWRUP_EN BIT(0)
+#define IP5XXX_SYS_CTL2 0x0c
+#define IP5XXX_SYS_CTL2_LIGHT_SHDN_TH GENMASK(7, 3)
+#define IP5XXX_SYS_CTL3 0x03
+#define IP5XXX_SYS_CTL3_LONG_PRESS_TIME_SEL GENMASK(7, 6)
+#define IP5XXX_SYS_CTL3_BTN_SHDN_EN BIT(5)
+#define IP5XXX_SYS_CTL4 0x04
+#define IP5XXX_SYS_CTL4_SHDN_TIME_SEL GENMASK(7, 6)
+#define IP5XXX_SYS_CTL4_VIN_PULLOUT_BOOST_EN BIT(5)
+#define IP5XXX_SYS_CTL5 0x07
+#define IP5XXX_SYS_CTL5_NTC_DIS BIT(6)
+#define IP5XXX_SYS_CTL5_WLED_MODE_SEL BIT(1)
+#define IP5XXX_SYS_CTL5_BTN_SHDN_SEL BIT(0)
+#define IP5XXX_CHG_CTL1 0x22
+#define IP5XXX_CHG_CTL1_BOOST_UVP_SEL GENMASK(3, 2)
+#define IP5XXX_CHG_CTL2 0x24
+#define IP5XXX_CHG_CTL2_BAT_TYPE_SEL GENMASK(6, 5)
+#define IP5XXX_CHG_CTL2_BAT_TYPE_SEL_4_2V (0x0 << 5)
+#define IP5XXX_CHG_CTL2_BAT_TYPE_SEL_4_3V (0x1 << 5)
+#define IP5XXX_CHG_CTL2_BAT_TYPE_SEL_4_35V (0x2 << 5)
+#define IP5XXX_CHG_CTL2_CONST_VOLT_SEL GENMASK(2, 1)
+#define IP5XXX_CHG_CTL4 0x26
+#define IP5XXX_CHG_CTL4_BAT_TYPE_SEL_EN BIT(6)
+#define IP5XXX_CHG_CTL4A 0x25
+#define IP5XXX_CHG_CTL4A_CONST_CUR_SEL GENMASK(4, 0)
+#define IP5XXX_MFP_CTL0 0x51
+#define IP5XXX_MFP_CTL1 0x52
+#define IP5XXX_GPIO_CTL2 0x53
+#define IP5XXX_GPIO_CTL2A 0x54
+#define IP5XXX_GPIO_CTL3 0x55
+#define IP5XXX_READ0 0x71
+#define IP5XXX_READ0_CHG_STAT GENMASK(7, 5)
+#define IP5XXX_READ0_CHG_STAT_IDLE (0x0 << 5)
+#define IP5XXX_READ0_CHG_STAT_TRICKLE (0x1 << 5)
+#define IP5XXX_READ0_CHG_STAT_CONST_VOLT (0x2 << 5)
+#define IP5XXX_READ0_CHG_STAT_CONST_CUR (0x3 << 5)
+#define IP5XXX_READ0_CHG_STAT_CONST_VOLT_STOP (0x4 << 5)
+#define IP5XXX_READ0_CHG_STAT_FULL (0x5 << 5)
+#define IP5XXX_READ0_CHG_STAT_TIMEOUT (0x6 << 5)
+#define IP5XXX_READ0_CHG_OP BIT(4)
+#define IP5XXX_READ0_CHG_END BIT(3)
+#define IP5XXX_READ0_CONST_VOLT_TIMEOUT BIT(2)
+#define IP5XXX_READ0_CHG_TIMEOUT BIT(1)
+#define IP5XXX_READ0_TRICKLE_TIMEOUT BIT(0)
+#define IP5XXX_READ0_TIMEOUT GENMASK(2, 0)
+#define IP5XXX_READ1 0x72
+#define IP5XXX_READ1_WLED_PRESENT BIT(7)
+#define IP5XXX_READ1_LIGHT_LOAD BIT(6)
+#define IP5XXX_READ1_VIN_OVERVOLT BIT(5)
+#define IP5XXX_READ2 0x77
+#define IP5XXX_READ2_BTN_PRESS BIT(3)
+#define IP5XXX_READ2_BTN_LONG_PRESS BIT(1)
+#define IP5XXX_READ2_BTN_SHORT_PRESS BIT(0)
+#define IP5XXX_BATVADC_DAT0 0xa2
+#define IP5XXX_BATVADC_DAT1 0xa3
+#define IP5XXX_BATIADC_DAT0 0xa4
+#define IP5XXX_BATIADC_DAT1 0xa5
+#define IP5XXX_BATOCV_DAT0 0xa8
+#define IP5XXX_BATOCV_DAT1 0xa9
+
+struct ip5xxx {
+ struct regmap *regmap;
+ bool initialized;
+};
+
+/*
+ * The IP5xxx charger only responds on I2C when it is "awake". The charger is
+ * generally only awake when VIN is powered or when its boost converter is
+ * enabled. Going into shutdown resets all register values. To handle this:
+ * 1) When any bus error occurs, assume the charger has gone into shutdown.
+ * 2) Attempt the initialization sequence on each subsequent register access
+ * until it succeeds.
+ */
+static int ip5xxx_read(struct ip5xxx *ip5xxx, unsigned int reg,
+ unsigned int *val)
+{
+ int ret;
+
+ ret = regmap_read(ip5xxx->regmap, reg, val);
+ if (ret)
+ ip5xxx->initialized = false;
+
+ return ret;
+}
+
+static int ip5xxx_update_bits(struct ip5xxx *ip5xxx, unsigned int reg,
+ unsigned int mask, unsigned int val)
+{
+ int ret;
+
+ ret = regmap_update_bits(ip5xxx->regmap, reg, mask, val);
+ if (ret)
+ ip5xxx->initialized = false;
+
+ return ret;
+}
+
+static int ip5xxx_initialize(struct power_supply *psy)
+{
+ struct ip5xxx *ip5xxx = power_supply_get_drvdata(psy);
+ int ret;
+
+ if (ip5xxx->initialized)
+ return 0;
+
+ /*
+ * Disable shutdown under light load.
+ * Enable power on when under load.
+ */
+ ret = ip5xxx_update_bits(ip5xxx, IP5XXX_SYS_CTL1,
+ IP5XXX_SYS_CTL1_LIGHT_SHDN_EN |
+ IP5XXX_SYS_CTL1_LOAD_PWRUP_EN,
+ IP5XXX_SYS_CTL1_LOAD_PWRUP_EN);
+ if (ret)
+ return ret;
+
+ /*
+ * Enable shutdown after a long button press (as configured below).
+ */
+ ret = ip5xxx_update_bits(ip5xxx, IP5XXX_SYS_CTL3,
+ IP5XXX_SYS_CTL3_BTN_SHDN_EN,
+ IP5XXX_SYS_CTL3_BTN_SHDN_EN);
+ if (ret)
+ return ret;
+
+ /*
+ * Power on automatically when VIN is removed.
+ */
+ ret = ip5xxx_update_bits(ip5xxx, IP5XXX_SYS_CTL4,
+ IP5XXX_SYS_CTL4_VIN_PULLOUT_BOOST_EN,
+ IP5XXX_SYS_CTL4_VIN_PULLOUT_BOOST_EN);
+ if (ret)
+ return ret;
+
+ /*
+ * Enable the NTC.
+ * Configure the button for two presses => LED, long press => shutdown.
+ */
+ ret = ip5xxx_update_bits(ip5xxx, IP5XXX_SYS_CTL5,
+ IP5XXX_SYS_CTL5_NTC_DIS |
+ IP5XXX_SYS_CTL5_WLED_MODE_SEL |
+ IP5XXX_SYS_CTL5_BTN_SHDN_SEL,
+ IP5XXX_SYS_CTL5_WLED_MODE_SEL |
+ IP5XXX_SYS_CTL5_BTN_SHDN_SEL);
+ if (ret)
+ return ret;
+
+ ip5xxx->initialized = true;
+ dev_dbg(psy->dev.parent, "Initialized after power on\n");
+
+ return 0;
+}
+
+static const enum power_supply_property ip5xxx_battery_properties[] = {
+ POWER_SUPPLY_PROP_STATUS,
+ POWER_SUPPLY_PROP_CHARGE_TYPE,
+ POWER_SUPPLY_PROP_HEALTH,
+ POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN,
+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
+ POWER_SUPPLY_PROP_VOLTAGE_OCV,
+ POWER_SUPPLY_PROP_CURRENT_NOW,
+ POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT,
+ POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX,
+ POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
+ POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX,
+};
+
+static int ip5xxx_battery_get_status(struct ip5xxx *ip5xxx, int *val)
+{
+ unsigned int rval;
+ int ret;
+
+ ret = ip5xxx_read(ip5xxx, IP5XXX_READ0, &rval);
+ if (ret)
+ return ret;
+
+ switch (rval & IP5XXX_READ0_CHG_STAT) {
+ case IP5XXX_READ0_CHG_STAT_IDLE:
+ *val = POWER_SUPPLY_STATUS_DISCHARGING;
+ break;
+ case IP5XXX_READ0_CHG_STAT_TRICKLE:
+ case IP5XXX_READ0_CHG_STAT_CONST_CUR:
+ case IP5XXX_READ0_CHG_STAT_CONST_VOLT:
+ *val = POWER_SUPPLY_STATUS_CHARGING;
+ break;
+ case IP5XXX_READ0_CHG_STAT_CONST_VOLT_STOP:
+ case IP5XXX_READ0_CHG_STAT_FULL:
+ *val = POWER_SUPPLY_STATUS_FULL;
+ break;
+ case IP5XXX_READ0_CHG_STAT_TIMEOUT:
+ *val = POWER_SUPPLY_STATUS_NOT_CHARGING;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int ip5xxx_battery_get_charge_type(struct ip5xxx *ip5xxx, int *val)
+{
+ unsigned int rval;
+ int ret;
+
+ ret = ip5xxx_read(ip5xxx, IP5XXX_READ0, &rval);
+ if (ret)
+ return ret;
+
+ switch (rval & IP5XXX_READ0_CHG_STAT) {
+ case IP5XXX_READ0_CHG_STAT_IDLE:
+ case IP5XXX_READ0_CHG_STAT_CONST_VOLT_STOP:
+ case IP5XXX_READ0_CHG_STAT_FULL:
+ case IP5XXX_READ0_CHG_STAT_TIMEOUT:
+ *val = POWER_SUPPLY_CHARGE_TYPE_NONE;
+ break;
+ case IP5XXX_READ0_CHG_STAT_TRICKLE:
+ *val = POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
+ break;
+ case IP5XXX_READ0_CHG_STAT_CONST_CUR:
+ case IP5XXX_READ0_CHG_STAT_CONST_VOLT:
+ *val = POWER_SUPPLY_CHARGE_TYPE_STANDARD;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int ip5xxx_battery_get_health(struct ip5xxx *ip5xxx, int *val)
+{
+ unsigned int rval;
+ int ret;
+
+ ret = ip5xxx_read(ip5xxx, IP5XXX_READ0, &rval);
+ if (ret)
+ return ret;
+
+ if (rval & IP5XXX_READ0_TIMEOUT)
+ *val = POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
+ else
+ *val = POWER_SUPPLY_HEALTH_GOOD;
+
+ return 0;
+}
+
+static int ip5xxx_battery_get_voltage_max(struct ip5xxx *ip5xxx, int *val)
+{
+ unsigned int rval;
+ int ret;
+
+ ret = ip5xxx_read(ip5xxx, IP5XXX_CHG_CTL2, &rval);
+ if (ret)
+ return ret;
+
+ /*
+ * It is not clear what this will return if
+ * IP5XXX_CHG_CTL4_BAT_TYPE_SEL_EN is not set...
+ */
+ switch (rval & IP5XXX_CHG_CTL2_BAT_TYPE_SEL) {
+ case IP5XXX_CHG_CTL2_BAT_TYPE_SEL_4_2V:
+ *val = 4200000;
+ break;
+ case IP5XXX_CHG_CTL2_BAT_TYPE_SEL_4_3V:
+ *val = 4300000;
+ break;
+ case IP5XXX_CHG_CTL2_BAT_TYPE_SEL_4_35V:
+ *val = 4350000;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
+static int ip5xxx_battery_read_adc(struct ip5xxx *ip5xxx,
+ u8 lo_reg, u8 hi_reg, int *val)
+{
+ unsigned int hi, lo;
+ int ret;
+
+ ret = ip5xxx_read(ip5xxx, lo_reg, &lo);
+ if (ret)
+ return ret;
+
+ ret = ip5xxx_read(ip5xxx, hi_reg, &hi);
+ if (ret)
+ return ret;
+
+ *val = sign_extend32(hi << 8 | lo, 13);
+
+ return 0;
+}
+
+static int ip5xxx_battery_get_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ union power_supply_propval *val)
+{
+ struct ip5xxx *ip5xxx = power_supply_get_drvdata(psy);
+ int raw, ret, vmax;
+ unsigned int rval;
+
+ ret = ip5xxx_initialize(psy);
+ if (ret)
+ return ret;
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_STATUS:
+ return ip5xxx_battery_get_status(ip5xxx, &val->intval);
+
+ case POWER_SUPPLY_PROP_CHARGE_TYPE:
+ return ip5xxx_battery_get_charge_type(ip5xxx, &val->intval);
+
+ case POWER_SUPPLY_PROP_HEALTH:
+ return ip5xxx_battery_get_health(ip5xxx, &val->intval);
+
+ case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+ return ip5xxx_battery_get_voltage_max(ip5xxx, &val->intval);
+
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+ ret = ip5xxx_battery_read_adc(ip5xxx, IP5XXX_BATVADC_DAT0,
+ IP5XXX_BATVADC_DAT1, &raw);
+
+ val->intval = 2600000 + DIV_ROUND_CLOSEST(raw * 26855, 100);
+ return 0;
+
+ case POWER_SUPPLY_PROP_VOLTAGE_OCV:
+ ret = ip5xxx_battery_read_adc(ip5xxx, IP5XXX_BATOCV_DAT0,
+ IP5XXX_BATOCV_DAT1, &raw);
+
+ val->intval = 2600000 + DIV_ROUND_CLOSEST(raw * 26855, 100);
+ return 0;
+
+ case POWER_SUPPLY_PROP_CURRENT_NOW:
+ ret = ip5xxx_battery_read_adc(ip5xxx, IP5XXX_BATIADC_DAT0,
+ IP5XXX_BATIADC_DAT1, &raw);
+
+ val->intval = DIV_ROUND_CLOSEST(raw * 149197, 200);
+ return 0;
+
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
+ ret = ip5xxx_read(ip5xxx, IP5XXX_CHG_CTL4A, &rval);
+ if (ret)
+ return ret;
+
+ rval &= IP5XXX_CHG_CTL4A_CONST_CUR_SEL;
+ val->intval = 100000 * rval;
+ return 0;
+
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+ val->intval = 100000 * 0x1f;
+ return 0;
+
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+ ret = ip5xxx_battery_get_voltage_max(ip5xxx, &vmax);
+ if (ret)
+ return ret;
+
+ ret = ip5xxx_read(ip5xxx, IP5XXX_CHG_CTL2, &rval);
+ if (ret)
+ return ret;
+
+ rval &= IP5XXX_CHG_CTL2_CONST_VOLT_SEL;
+ val->intval = vmax + 14000 * (rval >> 1);
+ return 0;
+
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
+ ret = ip5xxx_battery_get_voltage_max(ip5xxx, &vmax);
+ if (ret)
+ return ret;
+
+ val->intval = vmax + 14000 * 3;
+ return 0;
+
+ default:
+ return -EINVAL;
+ }
+}
+
+static int ip5xxx_battery_set_voltage_max(struct ip5xxx *ip5xxx, int val)
+{
+ unsigned int rval;
+ int ret;
+
+ switch (val) {
+ case 4200000:
+ rval = IP5XXX_CHG_CTL2_BAT_TYPE_SEL_4_2V;
+ break;
+ case 4300000:
+ rval = IP5XXX_CHG_CTL2_BAT_TYPE_SEL_4_3V;
+ break;
+ case 4350000:
+ rval = IP5XXX_CHG_CTL2_BAT_TYPE_SEL_4_35V;
+ break;
+ default:
+ return -EINVAL;
+ }
+
+ ret = ip5xxx_update_bits(ip5xxx, IP5XXX_CHG_CTL2,
+ IP5XXX_CHG_CTL2_BAT_TYPE_SEL, rval);
+ if (ret)
+ return ret;
+
+ ret = ip5xxx_update_bits(ip5xxx, IP5XXX_CHG_CTL4,
+ IP5XXX_CHG_CTL4_BAT_TYPE_SEL_EN,
+ IP5XXX_CHG_CTL4_BAT_TYPE_SEL_EN);
+ if (ret)
+ return ret;
+
+ return 0;
+}
+
+static int ip5xxx_battery_set_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ const union power_supply_propval *val)
+{
+ struct ip5xxx *ip5xxx = power_supply_get_drvdata(psy);
+ unsigned int rval;
+ int ret, vmax;
+
+ ret = ip5xxx_initialize(psy);
+ if (ret)
+ return ret;
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_STATUS:
+ switch (val->intval) {
+ case POWER_SUPPLY_STATUS_CHARGING:
+ rval = IP5XXX_SYS_CTL0_CHARGER_EN;
+ break;
+ case POWER_SUPPLY_STATUS_DISCHARGING:
+ case POWER_SUPPLY_STATUS_NOT_CHARGING:
+ rval = 0;
+ break;
+ default:
+ return -EINVAL;
+ }
+ return ip5xxx_update_bits(ip5xxx, IP5XXX_SYS_CTL0,
+ IP5XXX_SYS_CTL0_CHARGER_EN, rval);
+
+ case POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN:
+ return ip5xxx_battery_set_voltage_max(ip5xxx, val->intval);
+
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
+ rval = val->intval / 100000;
+ return ip5xxx_update_bits(ip5xxx, IP5XXX_CHG_CTL4A,
+ IP5XXX_CHG_CTL4A_CONST_CUR_SEL, rval);
+
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+ ret = ip5xxx_battery_get_voltage_max(ip5xxx, &vmax);
+ if (ret)
+ return ret;
+
+ rval = ((val->intval - vmax) / 14000) << 1;
+ return ip5xxx_update_bits(ip5xxx, IP5XXX_CHG_CTL2,
+ IP5XXX_CHG_CTL2_CONST_VOLT_SEL, rval);
+
+ default:
+ return -EINVAL;
+ }
+}
+
+static int ip5xxx_battery_property_is_writeable(struct power_supply *psy,
+ enum power_supply_property psp)
+{
+ return psp == POWER_SUPPLY_PROP_STATUS ||
+ psp == POWER_SUPPLY_PROP_VOLTAGE_MAX_DESIGN ||
+ psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT ||
+ psp == POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE;
+}
+
+static const struct power_supply_desc ip5xxx_battery_desc = {
+ .name = "ip5xxx-battery",
+ .type = POWER_SUPPLY_TYPE_BATTERY,
+ .properties = ip5xxx_battery_properties,
+ .num_properties = ARRAY_SIZE(ip5xxx_battery_properties),
+ .get_property = ip5xxx_battery_get_property,
+ .set_property = ip5xxx_battery_set_property,
+ .property_is_writeable = ip5xxx_battery_property_is_writeable,
+};
+
+static const enum power_supply_property ip5xxx_boost_properties[] = {
+ POWER_SUPPLY_PROP_ONLINE,
+ POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
+};
+
+static int ip5xxx_boost_get_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ union power_supply_propval *val)
+{
+ struct ip5xxx *ip5xxx = power_supply_get_drvdata(psy);
+ unsigned int rval;
+ int ret;
+
+ ret = ip5xxx_initialize(psy);
+ if (ret)
+ return ret;
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_ONLINE:
+ ret = ip5xxx_read(ip5xxx, IP5XXX_SYS_CTL0, &rval);
+ if (ret)
+ return ret;
+
+ val->intval = !!(rval & IP5XXX_SYS_CTL0_BOOST_EN);
+ return 0;
+
+ case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+ ret = ip5xxx_read(ip5xxx, IP5XXX_CHG_CTL1, &rval);
+ if (ret)
+ return ret;
+
+ rval &= IP5XXX_CHG_CTL1_BOOST_UVP_SEL;
+ val->intval = 4530000 + 100000 * (rval >> 2);
+ return 0;
+
+ default:
+ return -EINVAL;
+ }
+}
+
+static int ip5xxx_boost_set_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ const union power_supply_propval *val)
+{
+ struct ip5xxx *ip5xxx = power_supply_get_drvdata(psy);
+ unsigned int rval;
+ int ret;
+
+ ret = ip5xxx_initialize(psy);
+ if (ret)
+ return ret;
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_ONLINE:
+ rval = val->intval ? IP5XXX_SYS_CTL0_BOOST_EN : 0;
+ return ip5xxx_update_bits(ip5xxx, IP5XXX_SYS_CTL0,
+ IP5XXX_SYS_CTL0_BOOST_EN, rval);
+
+ case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
+ rval = ((val->intval - 4530000) / 100000) << 2;
+ return ip5xxx_update_bits(ip5xxx, IP5XXX_CHG_CTL1,
+ IP5XXX_CHG_CTL1_BOOST_UVP_SEL, rval);
+
+ default:
+ return -EINVAL;
+ }
+}
+
+static int ip5xxx_boost_property_is_writeable(struct power_supply *psy,
+ enum power_supply_property psp)
+{
+ return true;
+}
+
+static const struct power_supply_desc ip5xxx_boost_desc = {
+ .name = "ip5xxx-boost",
+ .type = POWER_SUPPLY_TYPE_USB,
+ .properties = ip5xxx_boost_properties,
+ .num_properties = ARRAY_SIZE(ip5xxx_boost_properties),
+ .get_property = ip5xxx_boost_get_property,
+ .set_property = ip5xxx_boost_set_property,
+ .property_is_writeable = ip5xxx_boost_property_is_writeable,
+};
+
+static const struct regmap_config ip5xxx_regmap_config = {
+ .reg_bits = 8,
+ .val_bits = 8,
+ .max_register = IP5XXX_BATOCV_DAT1,
+};
+
+static int ip5xxx_power_probe(struct i2c_client *client)
+{
+ struct power_supply_config psy_cfg = {};
+ struct device *dev = &client->dev;
+ struct power_supply *psy;
+ struct ip5xxx *ip5xxx;
+
+ ip5xxx = devm_kzalloc(dev, sizeof(*ip5xxx), GFP_KERNEL);
+ if (!ip5xxx)
+ return -ENOMEM;
+
+ ip5xxx->regmap = devm_regmap_init_i2c(client, &ip5xxx_regmap_config);
+ if (IS_ERR(ip5xxx->regmap))
+ return PTR_ERR(ip5xxx->regmap);
+
+ psy_cfg.of_node = dev->of_node;
+ psy_cfg.drv_data = ip5xxx;
+
+ psy = devm_power_supply_register(dev, &ip5xxx_battery_desc, &psy_cfg);
+ if (IS_ERR(psy))
+ return PTR_ERR(psy);
+
+ psy = devm_power_supply_register(dev, &ip5xxx_boost_desc, &psy_cfg);
+ if (IS_ERR(psy))
+ return PTR_ERR(psy);
+
+ return 0;
+}
+
+static const struct of_device_id ip5xxx_power_of_match[] = {
+ { .compatible = "injoinic,ip5108" },
+ { .compatible = "injoinic,ip5109" },
+ { .compatible = "injoinic,ip5207" },
+ { .compatible = "injoinic,ip5209" },
+ { }
+};
+MODULE_DEVICE_TABLE(of, ip5xxx_power_of_match);
+
+static struct i2c_driver ip5xxx_power_driver = {
+ .probe_new = ip5xxx_power_probe,
+ .driver = {
+ .name = "ip5xxx-power",
+ .of_match_table = ip5xxx_power_of_match,
+ }
+};
+module_i2c_driver(ip5xxx_power_driver);
+
+MODULE_AUTHOR("Samuel Holland <samuel@sholland.org>");
+MODULE_DESCRIPTION("Injoinic IP5xxx power bank IC driver");
+MODULE_LICENSE("GPL");