aboutsummaryrefslogtreecommitdiff
path: root/drivers/power/supply/ltc4162-l-charger.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/ltc4162-l-charger.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 'drivers/power/supply/ltc4162-l-charger.c')
-rw-r--r--drivers/power/supply/ltc4162-l-charger.c930
1 files changed, 930 insertions, 0 deletions
diff --git a/drivers/power/supply/ltc4162-l-charger.c b/drivers/power/supply/ltc4162-l-charger.c
new file mode 100644
index 000000000..db2bb5233
--- /dev/null
+++ b/drivers/power/supply/ltc4162-l-charger.c
@@ -0,0 +1,930 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Driver for Analog Devices (Linear Technology) LTC4162-L charger IC.
+ * Copyright (C) 2020, Topic Embedded Products
+ */
+
+#include <linux/module.h>
+#include <linux/delay.h>
+#include <linux/of_device.h>
+#include <linux/pm_runtime.h>
+#include <linux/power_supply.h>
+#include <linux/i2c.h>
+#include <linux/regmap.h>
+
+/* Registers (names based on what datasheet uses) */
+#define LTC4162L_EN_LIMIT_ALERTS_REG 0x0D
+#define LTC4162L_EN_CHARGER_STATE_ALERTS_REG 0x0E
+#define LTC4162L_EN_CHARGE_STATUS_ALERTS_REG 0x0F
+#define LTC4162L_CONFIG_BITS_REG 0x14
+#define LTC4162L_IIN_LIMIT_TARGET 0x15
+#define LTC4162L_ARM_SHIP_MODE 0x19
+#define LTC4162L_CHARGE_CURRENT_SETTING 0X1A
+#define LTC4162L_VCHARGE_SETTING 0X1B
+#define LTC4162L_C_OVER_X_THRESHOLD 0x1C
+#define LTC4162L_MAX_CV_TIME 0X1D
+#define LTC4162L_MAX_CHARGE_TIME 0X1E
+#define LTC4162L_CHARGER_CONFIG_BITS 0x29
+#define LTC4162L_CHARGER_STATE 0x34
+#define LTC4162L_CHARGE_STATUS 0x35
+#define LTC4162L_LIMIT_ALERTS_REG 0x36
+#define LTC4162L_CHARGER_STATE_ALERTS_REG 0x37
+#define LTC4162L_CHARGE_STATUS_ALERTS_REG 0x38
+#define LTC4162L_SYSTEM_STATUS_REG 0x39
+#define LTC4162L_VBAT 0x3A
+#define LTC4162L_VIN 0x3B
+#define LTC4162L_VOUT 0x3C
+#define LTC4162L_IBAT 0x3D
+#define LTC4162L_IIN 0x3E
+#define LTC4162L_DIE_TEMPERATURE 0x3F
+#define LTC4162L_THERMISTOR_VOLTAGE 0x40
+#define LTC4162L_BSR 0x41
+#define LTC4162L_JEITA_REGION 0x42
+#define LTC4162L_CHEM_CELLS_REG 0x43
+#define LTC4162L_ICHARGE_DAC 0x44
+#define LTC4162L_VCHARGE_DAC 0x45
+#define LTC4162L_IIN_LIMIT_DAC 0x46
+#define LTC4162L_VBAT_FILT 0x47
+#define LTC4162L_INPUT_UNDERVOLTAGE_DAC 0x4B
+
+/* Enumeration as in datasheet. Individual bits are mutually exclusive. */
+enum ltc4162l_state {
+ battery_detection = 2048,
+ charger_suspended = 256,
+ precharge = 128, /* trickle on low bat voltage */
+ cc_cv_charge = 64, /* normal charge */
+ ntc_pause = 32,
+ timer_term = 16,
+ c_over_x_term = 8, /* battery is full */
+ max_charge_time_fault = 4,
+ bat_missing_fault = 2,
+ bat_short_fault = 1
+};
+
+/* Individual bits are mutually exclusive. Only active in charging states.*/
+enum ltc4162l_charge_status {
+ ilim_reg_active = 32,
+ thermal_reg_active = 16,
+ vin_uvcl_active = 8,
+ iin_limit_active = 4,
+ constant_current = 2,
+ constant_voltage = 1,
+ charger_off = 0
+};
+
+/* Magic number to write to ARM_SHIP_MODE register */
+#define LTC4162L_ARM_SHIP_MODE_MAGIC 21325
+
+struct ltc4162l_info {
+ struct i2c_client *client;
+ struct regmap *regmap;
+ struct power_supply *charger;
+ u32 rsnsb; /* Series resistor that sets charge current, microOhm */
+ u32 rsnsi; /* Series resistor to measure input current, microOhm */
+ u8 cell_count; /* Number of connected cells, 0 while unknown */
+};
+
+static u8 ltc4162l_get_cell_count(struct ltc4162l_info *info)
+{
+ int ret;
+ unsigned int val;
+
+ /* Once read successfully */
+ if (info->cell_count)
+ return info->cell_count;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHEM_CELLS_REG, &val);
+ if (ret)
+ return 0;
+
+ /* Lower 4 bits is the cell count, or 0 if the chip doesn't know yet */
+ val &= 0x0f;
+ if (!val)
+ return 0;
+
+ /* Once determined, keep the value */
+ info->cell_count = val;
+
+ return val;
+};
+
+/* Convert enum value to POWER_SUPPLY_STATUS value */
+static int ltc4162l_state_decode(enum ltc4162l_state value)
+{
+ switch (value) {
+ case precharge:
+ case cc_cv_charge:
+ return POWER_SUPPLY_STATUS_CHARGING;
+ case c_over_x_term:
+ return POWER_SUPPLY_STATUS_FULL;
+ case bat_missing_fault:
+ case bat_short_fault:
+ return POWER_SUPPLY_STATUS_UNKNOWN;
+ default:
+ return POWER_SUPPLY_STATUS_NOT_CHARGING;
+ }
+};
+
+static int ltc4162l_get_status(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHARGER_STATE, &regval);
+ if (ret) {
+ dev_err(&info->client->dev, "Failed to read CHARGER_STATE\n");
+ return ret;
+ }
+
+ val->intval = ltc4162l_state_decode(regval);
+
+ return 0;
+}
+
+static int ltc4162l_charge_status_decode(enum ltc4162l_charge_status value)
+{
+ if (!value)
+ return POWER_SUPPLY_CHARGE_TYPE_NONE;
+
+ /* constant voltage/current and input_current limit are "fast" modes */
+ if (value <= iin_limit_active)
+ return POWER_SUPPLY_CHARGE_TYPE_FAST;
+
+ /* Anything that's not fast we'll return as trickle */
+ return POWER_SUPPLY_CHARGE_TYPE_TRICKLE;
+}
+
+static int ltc4162l_get_charge_type(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHARGE_STATUS, &regval);
+ if (ret)
+ return ret;
+
+ val->intval = ltc4162l_charge_status_decode(regval);
+
+ return 0;
+}
+
+static int ltc4162l_state_to_health(enum ltc4162l_state value)
+{
+ switch (value) {
+ case ntc_pause:
+ return POWER_SUPPLY_HEALTH_OVERHEAT;
+ case timer_term:
+ return POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE;
+ case max_charge_time_fault:
+ return POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE;
+ case bat_missing_fault:
+ return POWER_SUPPLY_HEALTH_UNSPEC_FAILURE;
+ case bat_short_fault:
+ return POWER_SUPPLY_HEALTH_DEAD;
+ default:
+ return POWER_SUPPLY_HEALTH_GOOD;
+ }
+}
+
+static int ltc4162l_get_health(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHARGER_STATE, &regval);
+ if (ret)
+ return ret;
+
+ val->intval = ltc4162l_state_to_health(regval);
+
+ return 0;
+}
+
+static int ltc4162l_get_online(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_SYSTEM_STATUS_REG, &regval);
+ if (ret)
+ return ret;
+
+ /* BIT(2) indicates if input voltage is sufficient to charge */
+ val->intval = !!(regval & BIT(2));
+
+ return 0;
+}
+
+static int ltc4162l_get_vbat(struct ltc4162l_info *info,
+ unsigned int reg,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, reg, &regval);
+ if (ret)
+ return ret;
+
+ /* cell_count × 192.4μV/LSB */
+ regval *= 1924;
+ regval *= ltc4162l_get_cell_count(info);
+ regval /= 10;
+ val->intval = regval;
+
+ return 0;
+}
+
+static int ltc4162l_get_ibat(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_IBAT, &regval);
+ if (ret)
+ return ret;
+
+ /* Signed 16-bit number, 1.466μV / RSNSB amperes/LSB. */
+ ret = (s16)(regval & 0xFFFF);
+ val->intval = 100 * mult_frac(ret, 14660, (int)info->rsnsb);
+
+ return 0;
+}
+
+
+static int ltc4162l_get_input_voltage(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_VIN, &regval);
+ if (ret)
+ return ret;
+
+ /* 1.649mV/LSB */
+ val->intval = regval * 1694;
+
+ return 0;
+}
+
+static int ltc4162l_get_input_current(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_IIN, &regval);
+ if (ret)
+ return ret;
+
+ /* Signed 16-bit number, 1.466μV / RSNSI amperes/LSB. */
+ ret = (s16)(regval & 0xFFFF);
+ ret *= 14660;
+ ret /= info->rsnsi;
+ ret *= 100;
+
+ val->intval = ret;
+
+ return 0;
+}
+
+static int ltc4162l_get_icharge(struct ltc4162l_info *info,
+ unsigned int reg,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, reg, &regval);
+ if (ret)
+ return ret;
+
+ regval &= BIT(6) - 1; /* Only the lower 5 bits */
+
+ /* The charge current servo level: (icharge_dac + 1) × 1mV/RSNSB */
+ ++regval;
+ val->intval = 10000u * mult_frac(regval, 100000u, info->rsnsb);
+
+ return 0;
+}
+
+static int ltc4162l_set_icharge(struct ltc4162l_info *info,
+ unsigned int reg,
+ unsigned int value)
+{
+ value = mult_frac(value, info->rsnsb, 100000u);
+ value /= 10000u;
+
+ /* Round to lowest possible */
+ if (value)
+ --value;
+
+ if (value > 31)
+ return -EINVAL;
+
+ return regmap_write(info->regmap, reg, value);
+}
+
+
+static int ltc4162l_get_vcharge(struct ltc4162l_info *info,
+ unsigned int reg,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+ u32 voltage;
+
+ ret = regmap_read(info->regmap, reg, &regval);
+ if (ret)
+ return ret;
+
+ regval &= BIT(6) - 1; /* Only the lower 5 bits */
+
+ /*
+ * charge voltage setting can be computed from
+ * cell_count × (vcharge_setting × 12.5mV + 3.8125V)
+ * where vcharge_setting ranges from 0 to 31 (4.2V max).
+ */
+ voltage = 3812500 + (regval * 12500);
+ voltage *= ltc4162l_get_cell_count(info);
+ val->intval = voltage;
+
+ return 0;
+}
+
+static int ltc4162l_set_vcharge(struct ltc4162l_info *info,
+ unsigned int reg,
+ unsigned int value)
+{
+ u8 cell_count = ltc4162l_get_cell_count(info);
+
+ if (!cell_count)
+ return -EBUSY; /* Not available yet, try again later */
+
+ value /= cell_count;
+
+ if (value < 3812500)
+ return -EINVAL;
+
+ value -= 3812500;
+ value /= 12500;
+
+ if (value > 31)
+ return -EINVAL;
+
+ return regmap_write(info->regmap, reg, value);
+}
+
+static int ltc4162l_get_iin_limit_dac(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_IIN_LIMIT_DAC, &regval);
+ if (ret)
+ return ret;
+
+ regval &= BIT(6) - 1; /* Only 6 bits */
+
+ /* (iin_limit_dac + 1) × 500μV / RSNSI */
+ ++regval;
+ regval *= 5000000u;
+ regval /= info->rsnsi;
+ val->intval = 100u * regval;
+
+ return 0;
+}
+
+static int ltc4162l_set_iin_limit(struct ltc4162l_info *info,
+ unsigned int value)
+{
+ unsigned int regval;
+
+ regval = mult_frac(value, info->rsnsi, 50000u);
+ regval /= 10000u;
+ if (regval)
+ --regval;
+ if (regval > 63)
+ regval = 63;
+
+ return regmap_write(info->regmap, LTC4162L_IIN_LIMIT_TARGET, regval);
+}
+
+static int ltc4162l_get_die_temp(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_DIE_TEMPERATURE, &regval);
+ if (ret)
+ return ret;
+
+ /* die_temp × 0.0215°C/LSB - 264.4°C */
+ ret = (s16)(regval & 0xFFFF);
+ ret *= 215;
+ ret /= 100; /* Centidegrees scale */
+ ret -= 26440;
+ val->intval = ret;
+
+ return 0;
+}
+
+static int ltc4162l_get_term_current(struct ltc4162l_info *info,
+ union power_supply_propval *val)
+{
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHARGER_CONFIG_BITS, &regval);
+ if (ret)
+ return ret;
+
+ /* Check if C_OVER_X_THRESHOLD is enabled */
+ if (!(regval & BIT(2))) {
+ val->intval = 0;
+ return 0;
+ }
+
+ ret = regmap_read(info->regmap, LTC4162L_C_OVER_X_THRESHOLD, &regval);
+ if (ret)
+ return ret;
+
+ /* 1.466μV / RSNSB amperes/LSB */
+ regval *= 14660u;
+ regval /= info->rsnsb;
+ val->intval = 100 * regval;
+
+ return 0;
+}
+
+static int ltc4162l_set_term_current(struct ltc4162l_info *info,
+ unsigned int value)
+{
+ int ret;
+ unsigned int regval;
+
+ if (!value) {
+ /* Disable en_c_over_x_term when set to zero */
+ return regmap_update_bits(info->regmap,
+ LTC4162L_CHARGER_CONFIG_BITS,
+ BIT(2), 0);
+ }
+
+ regval = mult_frac(value, info->rsnsb, 14660u);
+ regval /= 100u;
+
+ ret = regmap_write(info->regmap, LTC4162L_C_OVER_X_THRESHOLD, regval);
+ if (ret)
+ return ret;
+
+ /* Set en_c_over_x_term after changing the threshold value */
+ return regmap_update_bits(info->regmap, LTC4162L_CHARGER_CONFIG_BITS,
+ BIT(2), BIT(2));
+}
+
+/* Custom properties */
+static const char * const ltc4162l_charge_status_name[] = {
+ "ilim_reg_active", /* 32 */
+ "thermal_reg_active",
+ "vin_uvcl_active",
+ "iin_limit_active",
+ "constant_current",
+ "constant_voltage",
+ "charger_off" /* 0 */
+};
+
+static ssize_t charge_status_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ const char *result = ltc4162l_charge_status_name[
+ ARRAY_SIZE(ltc4162l_charge_status_name) - 1];
+ unsigned int regval;
+ unsigned int mask;
+ unsigned int index;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CHARGE_STATUS, &regval);
+ if (ret)
+ return ret;
+
+ /* Only one bit is set according to datasheet, let's be safe here */
+ for (mask = 32, index = 0; mask != 0; mask >>= 1, ++index) {
+ if (regval & mask) {
+ result = ltc4162l_charge_status_name[index];
+ break;
+ }
+ }
+
+ return sprintf(buf, "%s\n", result);
+}
+static DEVICE_ATTR_RO(charge_status);
+
+static ssize_t vbat_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ union power_supply_propval val;
+ int ret;
+
+ ret = ltc4162l_get_vbat(info, LTC4162L_VBAT, &val);
+ if (ret)
+ return ret;
+
+ return sprintf(buf, "%d\n", val.intval);
+}
+static DEVICE_ATTR_RO(vbat);
+
+static ssize_t vbat_avg_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ union power_supply_propval val;
+ int ret;
+
+ ret = ltc4162l_get_vbat(info, LTC4162L_VBAT_FILT, &val);
+ if (ret)
+ return ret;
+
+ return sprintf(buf, "%d\n", val.intval);
+}
+static DEVICE_ATTR_RO(vbat_avg);
+
+static ssize_t ibat_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ union power_supply_propval val;
+ int ret;
+
+ ret = ltc4162l_get_ibat(info, &val);
+ if (ret)
+ return ret;
+
+ return sprintf(buf, "%d\n", val.intval);
+}
+static DEVICE_ATTR_RO(ibat);
+
+static ssize_t force_telemetry_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_CONFIG_BITS_REG, &regval);
+ if (ret)
+ return ret;
+
+ return sprintf(buf, "%u\n", regval & BIT(2) ? 1 : 0);
+}
+
+static ssize_t force_telemetry_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf,
+ size_t count)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ int ret;
+ unsigned int value;
+
+ ret = kstrtouint(buf, 0, &value);
+ if (ret < 0)
+ return ret;
+
+ ret = regmap_update_bits(info->regmap, LTC4162L_CONFIG_BITS_REG,
+ BIT(2), value ? BIT(2) : 0);
+ if (ret < 0)
+ return ret;
+
+ return count;
+}
+
+static DEVICE_ATTR_RW(force_telemetry);
+
+static ssize_t arm_ship_mode_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ unsigned int regval;
+ int ret;
+
+ ret = regmap_read(info->regmap, LTC4162L_ARM_SHIP_MODE, &regval);
+ if (ret)
+ return ret;
+
+ return sprintf(buf, "%u\n",
+ regval == LTC4162L_ARM_SHIP_MODE_MAGIC ? 1 : 0);
+}
+
+static ssize_t arm_ship_mode_store(struct device *dev,
+ struct device_attribute *attr,
+ const char *buf,
+ size_t count)
+{
+ struct power_supply *psy = to_power_supply(dev);
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+ int ret;
+ unsigned int value;
+
+ ret = kstrtouint(buf, 0, &value);
+ if (ret < 0)
+ return ret;
+
+ ret = regmap_write(info->regmap, LTC4162L_ARM_SHIP_MODE,
+ value ? LTC4162L_ARM_SHIP_MODE_MAGIC : 0);
+ if (ret < 0)
+ return ret;
+
+ return count;
+}
+
+static DEVICE_ATTR_RW(arm_ship_mode);
+
+static struct attribute *ltc4162l_sysfs_entries[] = {
+ &dev_attr_charge_status.attr,
+ &dev_attr_ibat.attr,
+ &dev_attr_vbat.attr,
+ &dev_attr_vbat_avg.attr,
+ &dev_attr_force_telemetry.attr,
+ &dev_attr_arm_ship_mode.attr,
+ NULL,
+};
+
+static const struct attribute_group ltc4162l_attr_group = {
+ .name = NULL, /* put in device directory */
+ .attrs = ltc4162l_sysfs_entries,
+};
+
+static const struct attribute_group *ltc4162l_attr_groups[] = {
+ &ltc4162l_attr_group,
+ NULL,
+};
+
+static int ltc4162l_get_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ union power_supply_propval *val)
+{
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_STATUS:
+ return ltc4162l_get_status(info, val);
+ case POWER_SUPPLY_PROP_CHARGE_TYPE:
+ return ltc4162l_get_charge_type(info, val);
+ case POWER_SUPPLY_PROP_HEALTH:
+ return ltc4162l_get_health(info, val);
+ case POWER_SUPPLY_PROP_ONLINE:
+ return ltc4162l_get_online(info, val);
+ case POWER_SUPPLY_PROP_VOLTAGE_NOW:
+ return ltc4162l_get_input_voltage(info, val);
+ case POWER_SUPPLY_PROP_CURRENT_NOW:
+ return ltc4162l_get_input_current(info, val);
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT:
+ return ltc4162l_get_icharge(info,
+ LTC4162L_ICHARGE_DAC, val);
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+ return ltc4162l_get_icharge(info,
+ LTC4162L_CHARGE_CURRENT_SETTING, val);
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
+ return ltc4162l_get_vcharge(info,
+ LTC4162L_VCHARGE_DAC, val);
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
+ return ltc4162l_get_vcharge(info,
+ LTC4162L_VCHARGE_SETTING, val);
+ case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+ return ltc4162l_get_iin_limit_dac(info, val);
+ case POWER_SUPPLY_PROP_TEMP:
+ return ltc4162l_get_die_temp(info, val);
+ case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
+ return ltc4162l_get_term_current(info, val);
+ default:
+ return -EINVAL;
+ }
+}
+
+static int ltc4162l_set_property(struct power_supply *psy,
+ enum power_supply_property psp,
+ const union power_supply_propval *val)
+{
+ struct ltc4162l_info *info = power_supply_get_drvdata(psy);
+
+ switch (psp) {
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+ return ltc4162l_set_icharge(info,
+ LTC4162L_CHARGE_CURRENT_SETTING, val->intval);
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
+ return ltc4162l_set_vcharge(info,
+ LTC4162L_VCHARGE_SETTING, val->intval);
+ case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+ return ltc4162l_set_iin_limit(info, val->intval);
+ case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
+ return ltc4162l_set_term_current(info, val->intval);
+ default:
+ return -EINVAL;
+ }
+}
+
+static int ltc4162l_property_is_writeable(struct power_supply *psy,
+ enum power_supply_property psp)
+{
+ switch (psp) {
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_CURRENT_MAX:
+ case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE_MAX:
+ case POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT:
+ case POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT:
+ return 1;
+ default:
+ return 0;
+ }
+}
+
+/* Charger power supply property routines */
+static enum power_supply_property ltc4162l_properties[] = {
+ POWER_SUPPLY_PROP_STATUS,
+ POWER_SUPPLY_PROP_CHARGE_TYPE,
+ POWER_SUPPLY_PROP_HEALTH,
+ POWER_SUPPLY_PROP_ONLINE,
+ POWER_SUPPLY_PROP_VOLTAGE_NOW,
+ 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,
+ POWER_SUPPLY_PROP_INPUT_CURRENT_LIMIT,
+ POWER_SUPPLY_PROP_TEMP,
+ POWER_SUPPLY_PROP_CHARGE_TERM_CURRENT,
+};
+
+static const struct power_supply_desc ltc4162l_desc = {
+ .name = "ltc4162-l",
+ .type = POWER_SUPPLY_TYPE_MAINS,
+ .properties = ltc4162l_properties,
+ .num_properties = ARRAY_SIZE(ltc4162l_properties),
+ .get_property = ltc4162l_get_property,
+ .set_property = ltc4162l_set_property,
+ .property_is_writeable = ltc4162l_property_is_writeable,
+};
+
+static bool ltc4162l_is_writeable_reg(struct device *dev, unsigned int reg)
+{
+ /* all registers up to this one are writeable */
+ if (reg <= LTC4162L_CHARGER_CONFIG_BITS)
+ return true;
+
+ /* The ALERTS registers can be written to clear alerts */
+ if (reg >= LTC4162L_LIMIT_ALERTS_REG &&
+ reg <= LTC4162L_CHARGE_STATUS_ALERTS_REG)
+ return true;
+
+ return false;
+}
+
+static bool ltc4162l_is_volatile_reg(struct device *dev, unsigned int reg)
+{
+ /* all registers after this one are read-only status registers */
+ return reg > LTC4162L_CHARGER_CONFIG_BITS;
+}
+
+static const struct regmap_config ltc4162l_regmap_config = {
+ .reg_bits = 8,
+ .val_bits = 16,
+ .val_format_endian = REGMAP_ENDIAN_LITTLE,
+ .writeable_reg = ltc4162l_is_writeable_reg,
+ .volatile_reg = ltc4162l_is_volatile_reg,
+ .max_register = LTC4162L_INPUT_UNDERVOLTAGE_DAC,
+ .cache_type = REGCACHE_RBTREE,
+};
+
+static void ltc4162l_clear_interrupts(struct ltc4162l_info *info)
+{
+ /* Acknowledge interrupt to chip by clearing all events */
+ regmap_write(info->regmap, LTC4162L_LIMIT_ALERTS_REG, 0);
+ regmap_write(info->regmap, LTC4162L_CHARGER_STATE_ALERTS_REG, 0);
+ regmap_write(info->regmap, LTC4162L_CHARGE_STATUS_ALERTS_REG, 0);
+}
+
+static int ltc4162l_probe(struct i2c_client *client)
+{
+ struct i2c_adapter *adapter = client->adapter;
+ struct device *dev = &client->dev;
+ struct ltc4162l_info *info;
+ struct power_supply_config ltc4162l_config = {};
+ u32 value;
+ int ret;
+
+ if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WORD_DATA)) {
+ dev_err(dev, "No support for SMBUS_WORD_DATA\n");
+ return -ENODEV;
+ }
+ info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL);
+ if (!info)
+ return -ENOMEM;
+
+ info->client = client;
+ i2c_set_clientdata(client, info);
+
+ info->regmap = devm_regmap_init_i2c(client, &ltc4162l_regmap_config);
+ if (IS_ERR(info->regmap)) {
+ dev_err(dev, "Failed to initialize register map\n");
+ return PTR_ERR(info->regmap);
+ }
+
+ ret = device_property_read_u32(dev, "lltc,rsnsb-micro-ohms",
+ &info->rsnsb);
+ if (ret) {
+ dev_err(dev, "Missing lltc,rsnsb-micro-ohms property\n");
+ return ret;
+ }
+ if (!info->rsnsb)
+ return -EINVAL;
+
+ ret = device_property_read_u32(dev, "lltc,rsnsi-micro-ohms",
+ &info->rsnsi);
+ if (ret) {
+ dev_err(dev, "Missing lltc,rsnsi-micro-ohms property\n");
+ return ret;
+ }
+ if (!info->rsnsi)
+ return -EINVAL;
+
+ if (!device_property_read_u32(dev, "lltc,cell-count", &value))
+ info->cell_count = value;
+
+ ltc4162l_config.of_node = dev->of_node;
+ ltc4162l_config.drv_data = info;
+ ltc4162l_config.attr_grp = ltc4162l_attr_groups;
+
+ info->charger = devm_power_supply_register(dev, &ltc4162l_desc,
+ &ltc4162l_config);
+ if (IS_ERR(info->charger)) {
+ dev_err(dev, "Failed to register charger\n");
+ return PTR_ERR(info->charger);
+ }
+
+ /* Disable the threshold alerts, we're not using them */
+ regmap_write(info->regmap, LTC4162L_EN_LIMIT_ALERTS_REG, 0);
+
+ /* Enable interrupts on all status changes */
+ regmap_write(info->regmap, LTC4162L_EN_CHARGER_STATE_ALERTS_REG,
+ 0x1fff);
+ regmap_write(info->regmap, LTC4162L_EN_CHARGE_STATUS_ALERTS_REG, 0x1f);
+
+ ltc4162l_clear_interrupts(info);
+
+ return 0;
+}
+
+static void ltc4162l_alert(struct i2c_client *client,
+ enum i2c_alert_protocol type, unsigned int flag)
+{
+ struct ltc4162l_info *info = i2c_get_clientdata(client);
+
+ if (type != I2C_PROTOCOL_SMBUS_ALERT)
+ return;
+
+ ltc4162l_clear_interrupts(info);
+ power_supply_changed(info->charger);
+}
+
+static const struct i2c_device_id ltc4162l_i2c_id_table[] = {
+ { "ltc4162-l", 0 },
+ { },
+};
+MODULE_DEVICE_TABLE(i2c, ltc4162l_i2c_id_table);
+
+static const struct of_device_id ltc4162l_of_match[] = {
+ { .compatible = "lltc,ltc4162-l", },
+ { },
+};
+MODULE_DEVICE_TABLE(of, ltc4162l_of_match);
+
+static struct i2c_driver ltc4162l_driver = {
+ .probe_new = ltc4162l_probe,
+ .alert = ltc4162l_alert,
+ .id_table = ltc4162l_i2c_id_table,
+ .driver = {
+ .name = "ltc4162-l-charger",
+ .of_match_table = of_match_ptr(ltc4162l_of_match),
+ },
+};
+module_i2c_driver(ltc4162l_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Mike Looijmans <mike.looijmans@topic.nl>");
+MODULE_DESCRIPTION("LTC4162-L charger driver");