^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Motorola CPCAP PMIC battery charger driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2017 Tony Lindgren <tony@atomide.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Rewritten for Linux power framework with some parts based on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * on earlier driver found in the Motorola Linux kernel:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (C) 2009-2010 Motorola, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/atomic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/of_platform.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/regmap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/gpio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/usb/phy_companion.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/phy/omap_usb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <linux/usb/otg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #include <linux/iio/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <linux/mfd/motorola-cpcap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * CPCAP_REG_CRM register bits. For documentation of somewhat similar hardware,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * see NXP "MC13783 Power Management and Audio Circuit Users's Guide"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) * MC13783UG.pdf chapter "8.5 Battery Interface Register Summary". The registers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * and values for CPCAP are different, but some of the internal components seem
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * similar. Also see the Motorola Linux kernel cpcap-regbits.h. CPCAP_REG_CHRGR_1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * bits that seem to describe the CRM register.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define CPCAP_REG_CRM_UNUSED_641_15 BIT(15) /* 641 = register number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #define CPCAP_REG_CRM_UNUSED_641_14 BIT(14) /* 641 = register number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define CPCAP_REG_CRM_CHRG_LED_EN BIT(13) /* Charger LED */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define CPCAP_REG_CRM_RVRSMODE BIT(12) /* USB VBUS output enable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define CPCAP_REG_CRM_ICHRG_TR1 BIT(11) /* Trickle charge current */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define CPCAP_REG_CRM_ICHRG_TR0 BIT(10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define CPCAP_REG_CRM_FET_OVRD BIT(9) /* 0 = hardware, 1 = FET_CTRL */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) #define CPCAP_REG_CRM_FET_CTRL BIT(8) /* BPFET 1 if FET_OVRD set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define CPCAP_REG_CRM_VCHRG3 BIT(7) /* Charge voltage bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define CPCAP_REG_CRM_VCHRG2 BIT(6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) #define CPCAP_REG_CRM_VCHRG1 BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) #define CPCAP_REG_CRM_VCHRG0 BIT(4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define CPCAP_REG_CRM_ICHRG3 BIT(3) /* Charge current bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) #define CPCAP_REG_CRM_ICHRG2 BIT(2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) #define CPCAP_REG_CRM_ICHRG1 BIT(1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) #define CPCAP_REG_CRM_ICHRG0 BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) /* CPCAP_REG_CRM trickle charge voltages */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) #define CPCAP_REG_CRM_TR(val) (((val) & 0x3) << 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #define CPCAP_REG_CRM_TR_0A00 CPCAP_REG_CRM_TR(0x0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define CPCAP_REG_CRM_TR_0A24 CPCAP_REG_CRM_TR(0x1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define CPCAP_REG_CRM_TR_0A48 CPCAP_REG_CRM_TR(0x2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) #define CPCAP_REG_CRM_TR_0A72 CPCAP_REG_CRM_TR(0x4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * CPCAP_REG_CRM charge voltages based on the ADC channel 1 values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * Note that these register bits don't match MC13783UG.pdf VCHRG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) * register bits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) #define CPCAP_REG_CRM_VCHRG(val) (((val) & 0xf) << 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) #define CPCAP_REG_CRM_VCHRG_3V80 CPCAP_REG_CRM_VCHRG(0x0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) #define CPCAP_REG_CRM_VCHRG_4V10 CPCAP_REG_CRM_VCHRG(0x1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) #define CPCAP_REG_CRM_VCHRG_4V12 CPCAP_REG_CRM_VCHRG(0x2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #define CPCAP_REG_CRM_VCHRG_4V15 CPCAP_REG_CRM_VCHRG(0x3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #define CPCAP_REG_CRM_VCHRG_4V17 CPCAP_REG_CRM_VCHRG(0x4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) #define CPCAP_REG_CRM_VCHRG_4V20 CPCAP_REG_CRM_VCHRG(0x5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #define CPCAP_REG_CRM_VCHRG_4V23 CPCAP_REG_CRM_VCHRG(0x6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) #define CPCAP_REG_CRM_VCHRG_4V25 CPCAP_REG_CRM_VCHRG(0x7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) #define CPCAP_REG_CRM_VCHRG_4V27 CPCAP_REG_CRM_VCHRG(0x8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) #define CPCAP_REG_CRM_VCHRG_4V30 CPCAP_REG_CRM_VCHRG(0x9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) #define CPCAP_REG_CRM_VCHRG_4V33 CPCAP_REG_CRM_VCHRG(0xa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) #define CPCAP_REG_CRM_VCHRG_4V35 CPCAP_REG_CRM_VCHRG(0xb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) #define CPCAP_REG_CRM_VCHRG_4V38 CPCAP_REG_CRM_VCHRG(0xc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) #define CPCAP_REG_CRM_VCHRG_4V40 CPCAP_REG_CRM_VCHRG(0xd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) #define CPCAP_REG_CRM_VCHRG_4V42 CPCAP_REG_CRM_VCHRG(0xe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) #define CPCAP_REG_CRM_VCHRG_4V44 CPCAP_REG_CRM_VCHRG(0xf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) * CPCAP_REG_CRM charge currents. These seem to match MC13783UG.pdf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) * values in "Table 8-3. Charge Path Regulator Current Limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * Characteristics" for the nominal values.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #define CPCAP_REG_CRM_ICHRG(val) (((val) & 0xf) << 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #define CPCAP_REG_CRM_ICHRG_0A000 CPCAP_REG_CRM_ICHRG(0x0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #define CPCAP_REG_CRM_ICHRG_0A070 CPCAP_REG_CRM_ICHRG(0x1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #define CPCAP_REG_CRM_ICHRG_0A177 CPCAP_REG_CRM_ICHRG(0x2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #define CPCAP_REG_CRM_ICHRG_0A266 CPCAP_REG_CRM_ICHRG(0x3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #define CPCAP_REG_CRM_ICHRG_0A355 CPCAP_REG_CRM_ICHRG(0x4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #define CPCAP_REG_CRM_ICHRG_0A443 CPCAP_REG_CRM_ICHRG(0x5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #define CPCAP_REG_CRM_ICHRG_0A532 CPCAP_REG_CRM_ICHRG(0x6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #define CPCAP_REG_CRM_ICHRG_0A621 CPCAP_REG_CRM_ICHRG(0x7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #define CPCAP_REG_CRM_ICHRG_0A709 CPCAP_REG_CRM_ICHRG(0x8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #define CPCAP_REG_CRM_ICHRG_0A798 CPCAP_REG_CRM_ICHRG(0x9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #define CPCAP_REG_CRM_ICHRG_0A886 CPCAP_REG_CRM_ICHRG(0xa)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #define CPCAP_REG_CRM_ICHRG_0A975 CPCAP_REG_CRM_ICHRG(0xb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #define CPCAP_REG_CRM_ICHRG_1A064 CPCAP_REG_CRM_ICHRG(0xc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #define CPCAP_REG_CRM_ICHRG_1A152 CPCAP_REG_CRM_ICHRG(0xd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #define CPCAP_REG_CRM_ICHRG_1A596 CPCAP_REG_CRM_ICHRG(0xe)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #define CPCAP_REG_CRM_ICHRG_NO_LIMIT CPCAP_REG_CRM_ICHRG(0xf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) /* CPCAP_REG_VUSBC register bits needed for VBUS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #define CPCAP_BIT_VBUS_SWITCH BIT(0) /* VBUS boost to 5V */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) CPCAP_CHARGER_IIO_BATTDET,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) CPCAP_CHARGER_IIO_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) CPCAP_CHARGER_IIO_VBUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) CPCAP_CHARGER_IIO_CHRG_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) CPCAP_CHARGER_IIO_BATT_CURRENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) CPCAP_CHARGER_IIO_NR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) CPCAP_CHARGER_DISCONNECTED,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) CPCAP_CHARGER_DETECTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) CPCAP_CHARGER_CHARGING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) CPCAP_CHARGER_DONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) struct cpcap_charger_ddata {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) struct regmap *reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) struct list_head irq_list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) struct delayed_work detect_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) struct delayed_work vbus_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) struct gpio_desc *gpio[2]; /* gpio_reven0 & 1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) struct iio_channel *channels[CPCAP_CHARGER_IIO_NR];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) struct power_supply *usb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) struct phy_companion comparator; /* For USB VBUS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) unsigned int vbus_enabled:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) unsigned int feeding_vbus:1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) atomic_t active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) int state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) int voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) struct cpcap_interrupt_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) int irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) struct list_head node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) struct cpcap_charger_ints_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) bool chrg_det;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) bool rvrs_chrg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) bool vbusov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) bool chrg_se1b;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) bool rvrs_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) bool chrgcurr2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) bool chrgcurr1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) bool vbusvld;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) bool battdetb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) static enum power_supply_property cpcap_charger_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) POWER_SUPPLY_PROP_VOLTAGE_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) POWER_SUPPLY_PROP_CURRENT_NOW,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* No battery always shows temperature of -40000 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static bool cpcap_charger_battery_found(struct cpcap_charger_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) struct iio_channel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) int error, temperature;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) channel = ddata->channels[CPCAP_CHARGER_IIO_BATTDET];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) error = iio_read_channel_processed(channel, &temperature);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return temperature > -20000 && temperature < 60000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static int cpcap_charger_get_charge_voltage(struct cpcap_charger_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct iio_channel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) int error, value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) channel = ddata->channels[CPCAP_CHARGER_IIO_VOLTAGE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) error = iio_read_channel_processed(channel, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) static int cpcap_charger_get_charge_current(struct cpcap_charger_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) struct iio_channel *channel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) int error, value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) channel = ddata->channels[CPCAP_CHARGER_IIO_CHRG_CURRENT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) error = iio_read_channel_processed(channel, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) if (error < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) dev_warn(ddata->dev, "%s failed: %i\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) return value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static int cpcap_charger_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) val->intval = ddata->status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) val->intval = ddata->voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) case POWER_SUPPLY_PROP_VOLTAGE_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) val->intval = cpcap_charger_get_charge_voltage(ddata) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) case POWER_SUPPLY_PROP_CURRENT_NOW:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) if (ddata->status == POWER_SUPPLY_STATUS_CHARGING)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) val->intval = cpcap_charger_get_charge_current(ddata) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) val->intval = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) val->intval = ddata->status == POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) static int cpcap_charger_match_voltage(int voltage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) switch (voltage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) case 0 ... 4100000 - 1: return 3800000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) case 4100000 ... 4120000 - 1: return 4100000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) case 4120000 ... 4150000 - 1: return 4120000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) case 4150000 ... 4170000 - 1: return 4150000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) case 4170000 ... 4200000 - 1: return 4170000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) case 4200000 ... 4230000 - 1: return 4200000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) case 4230000 ... 4250000 - 1: return 4230000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) case 4250000 ... 4270000 - 1: return 4250000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) case 4270000 ... 4300000 - 1: return 4270000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) case 4300000 ... 4330000 - 1: return 4300000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) case 4330000 ... 4350000 - 1: return 4330000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) case 4350000 ... 4380000 - 1: return 4350000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) case 4380000 ... 4400000 - 1: return 4380000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) case 4400000 ... 4420000 - 1: return 4400000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) case 4420000 ... 4440000 - 1: return 4420000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) case 4440000: return 4440000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) default: return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) static int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) cpcap_charger_get_bat_const_charge_voltage(struct cpcap_charger_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) union power_supply_propval prop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) struct power_supply *battery;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) int voltage = ddata->voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) battery = power_supply_get_by_name("battery");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (battery) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) error = power_supply_get_property(battery,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) &prop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (!error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) voltage = prop.intval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) power_supply_put(battery);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static int cpcap_charger_set_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) const union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) struct cpcap_charger_ddata *ddata = dev_get_drvdata(psy->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) int voltage, batvolt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) voltage = cpcap_charger_match_voltage(val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) batvolt = cpcap_charger_get_bat_const_charge_voltage(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (voltage > batvolt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) voltage = batvolt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) ddata->voltage = voltage;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) schedule_delayed_work(&ddata->detect_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) static int cpcap_charger_property_is_writeable(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) enum power_supply_property psp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) case POWER_SUPPLY_PROP_CONSTANT_CHARGE_VOLTAGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static void cpcap_charger_set_cable_path(struct cpcap_charger_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) bool enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (!ddata->gpio[0])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) gpiod_set_value(ddata->gpio[0], enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) static void cpcap_charger_set_inductive_path(struct cpcap_charger_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) bool enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) if (!ddata->gpio[1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) gpiod_set_value(ddata->gpio[1], enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static int cpcap_charger_set_state(struct cpcap_charger_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) int max_voltage, int charge_current,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) int trickle_current)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) bool enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) enable = (charge_current || trickle_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) dev_dbg(ddata->dev, "%s enable: %i\n", __func__, enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) if (!enable) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 0x3fff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) CPCAP_REG_CRM_FET_OVRD |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) CPCAP_REG_CRM_FET_CTRL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) ddata->status = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM, 0x3fff,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) CPCAP_REG_CRM_CHRG_LED_EN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) trickle_current |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) CPCAP_REG_CRM_FET_OVRD |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) CPCAP_REG_CRM_FET_CTRL |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) max_voltage |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) charge_current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) ddata->status = POWER_SUPPLY_STATUS_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) ddata->status = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) static bool cpcap_charger_vbus_valid(struct cpcap_charger_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) int error, value = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) struct iio_channel *channel =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) ddata->channels[CPCAP_CHARGER_IIO_VBUS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) error = iio_read_channel_processed(channel, &value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) if (error >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) return value > 3900 ? true : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) dev_err(ddata->dev, "error reading VBUS: %i\n", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) /* VBUS control functions for the USB PHY companion */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static void cpcap_charger_vbus_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) struct cpcap_charger_ddata *ddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) bool vbus = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) ddata = container_of(work, struct cpcap_charger_ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) vbus_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) if (ddata->vbus_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) vbus = cpcap_charger_vbus_valid(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) if (vbus) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) dev_info(ddata->dev, "VBUS already provided\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) ddata->feeding_vbus = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) cpcap_charger_set_cable_path(ddata, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) cpcap_charger_set_inductive_path(ddata, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) error = cpcap_charger_set_state(ddata, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) CPCAP_BIT_VBUS_SWITCH,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) CPCAP_BIT_VBUS_SWITCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) CPCAP_REG_CRM_RVRSMODE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) CPCAP_REG_CRM_RVRSMODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) error = regmap_update_bits(ddata->reg, CPCAP_REG_VUSBC,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) CPCAP_BIT_VBUS_SWITCH, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) error = regmap_update_bits(ddata->reg, CPCAP_REG_CRM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) CPCAP_REG_CRM_RVRSMODE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) cpcap_charger_set_cable_path(ddata, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) cpcap_charger_set_inductive_path(ddata, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) ddata->feeding_vbus = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) dev_err(ddata->dev, "%s could not %s vbus: %i\n", __func__,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) ddata->vbus_enabled ? "enable" : "disable", error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) static int cpcap_charger_set_vbus(struct phy_companion *comparator,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) bool enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) struct cpcap_charger_ddata *ddata =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) container_of(comparator, struct cpcap_charger_ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) comparator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) ddata->vbus_enabled = enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) schedule_delayed_work(&ddata->vbus_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) /* Charger interrupt handling functions */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) static int cpcap_charger_get_ints_state(struct cpcap_charger_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) struct cpcap_charger_ints_state *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) int val, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) error = regmap_read(ddata->reg, CPCAP_REG_INTS1, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) s->chrg_det = val & BIT(13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) s->rvrs_chrg = val & BIT(12);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) s->vbusov = val & BIT(11);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) error = regmap_read(ddata->reg, CPCAP_REG_INTS2, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) s->chrg_se1b = val & BIT(13);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) s->rvrs_mode = val & BIT(6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) s->chrgcurr2 = val & BIT(5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) s->chrgcurr1 = val & BIT(4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) s->vbusvld = val & BIT(3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) error = regmap_read(ddata->reg, CPCAP_REG_INTS4, &val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) s->battdetb = val & BIT(6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) static void cpcap_charger_update_state(struct cpcap_charger_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) int state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) const char *status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) if (state > CPCAP_CHARGER_DONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) dev_warn(ddata->dev, "unknown state: %i\n", state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) ddata->state = state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) switch (state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) case CPCAP_CHARGER_DISCONNECTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) status = "DISCONNECTED";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) case CPCAP_CHARGER_DETECTING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) status = "DETECTING";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) case CPCAP_CHARGER_CHARGING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) status = "CHARGING";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) case CPCAP_CHARGER_DONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) status = "DONE";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) dev_dbg(ddata->dev, "state: %s\n", status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) static int cpcap_charger_voltage_to_regval(int voltage)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) switch (voltage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) case 0 ... 4100000 - 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) case 4100000 ... 4200000 - 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) offset = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) case 4200000 ... 4300000 - 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) case 4300000 ... 4380000 - 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) offset = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) case 4380000 ... 4440000:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) offset = -2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) return ((voltage - 4100000) / 20000) + offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) static void cpcap_charger_disconnect(struct cpcap_charger_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) int state, unsigned long delay)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) error = cpcap_charger_set_state(ddata, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) cpcap_charger_update_state(ddata, state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) power_supply_changed(ddata->usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) schedule_delayed_work(&ddata->detect_work, delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) static void cpcap_usb_detect(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) struct cpcap_charger_ddata *ddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) struct cpcap_charger_ints_state s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) ddata = container_of(work, struct cpcap_charger_ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) detect_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) error = cpcap_charger_get_ints_state(ddata, &s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) /* Just init the state if a charger is connected with no chrg_det set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) if (!s.chrg_det && s.chrgcurr1 && s.vbusvld) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) cpcap_charger_update_state(ddata, CPCAP_CHARGER_DETECTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) * If battery voltage is higher than charge voltage, it may have been
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) * charged to 4.35V by Android. Try again in 10 minutes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) if (cpcap_charger_get_charge_voltage(ddata) > ddata->voltage) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) HZ * 60 * 10);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) /* Delay for 80ms to avoid vbus bouncing when usb cable is plugged in */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) usleep_range(80000, 120000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) /* Throttle chrgcurr2 interrupt for charger done and retry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640) switch (ddata->state) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) case CPCAP_CHARGER_CHARGING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) if (s.chrgcurr2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) if (s.chrgcurr1 && s.vbusvld) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DONE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) HZ * 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) case CPCAP_CHARGER_DONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (!s.chrgcurr2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) cpcap_charger_disconnect(ddata, CPCAP_CHARGER_DETECTING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) HZ * 5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) if (!ddata->feeding_vbus && cpcap_charger_vbus_valid(ddata) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) s.chrgcurr1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) int max_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) int vchrg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) if (cpcap_charger_battery_found(ddata))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) max_current = CPCAP_REG_CRM_ICHRG_1A596;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) max_current = CPCAP_REG_CRM_ICHRG_0A532;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) vchrg = cpcap_charger_voltage_to_regval(ddata->voltage);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) error = cpcap_charger_set_state(ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) CPCAP_REG_CRM_VCHRG(vchrg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) max_current, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) cpcap_charger_update_state(ddata, CPCAP_CHARGER_CHARGING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) error = cpcap_charger_set_state(ddata, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) cpcap_charger_update_state(ddata, CPCAP_CHARGER_DISCONNECTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) power_supply_changed(ddata->usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) dev_err(ddata->dev, "%s failed with %i\n", __func__, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) static irqreturn_t cpcap_charger_irq_thread(int irq, void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) struct cpcap_charger_ddata *ddata = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) if (!atomic_read(&ddata->active))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) schedule_delayed_work(&ddata->detect_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) static int cpcap_usb_init_irq(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) struct cpcap_charger_ddata *ddata,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) struct cpcap_interrupt_desc *d;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) int irq, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) irq = platform_get_irq_byname(pdev, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) if (irq < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) error = devm_request_threaded_irq(ddata->dev, irq, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) cpcap_charger_irq_thread,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) IRQF_SHARED | IRQF_ONESHOT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) name, ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) if (error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) dev_err(ddata->dev, "could not get irq %s: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) name, error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) d = devm_kzalloc(ddata->dev, sizeof(*d), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) if (!d)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) d->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) d->irq = irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) list_add(&d->node, &ddata->irq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) static const char * const cpcap_charger_irqs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) /* REG_INT_0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) "chrg_det", "rvrs_chrg",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) /* REG_INT1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) "chrg_se1b", "se0conn", "rvrs_mode", "chrgcurr2", "chrgcurr1", "vbusvld",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) /* REG_INT_3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) "battdetb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) static int cpcap_usb_init_interrupts(struct platform_device *pdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) struct cpcap_charger_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) int i, error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) for (i = 0; i < ARRAY_SIZE(cpcap_charger_irqs); i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) error = cpcap_usb_init_irq(pdev, ddata, cpcap_charger_irqs[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) static void cpcap_charger_init_optional_gpios(struct cpcap_charger_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) ddata->gpio[i] = devm_gpiod_get_index(ddata->dev, "mode",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) i, GPIOD_OUT_HIGH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) if (IS_ERR(ddata->gpio[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) dev_info(ddata->dev, "no mode change GPIO%i: %li\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) i, PTR_ERR(ddata->gpio[i]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) ddata->gpio[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) static int cpcap_charger_init_iio(struct cpcap_charger_ddata *ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) const char * const names[CPCAP_CHARGER_IIO_NR] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) "battdetb", "battp", "vbus", "chg_isense", "batti",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) int error, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) for (i = 0; i < CPCAP_CHARGER_IIO_NR; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) ddata->channels[i] = devm_iio_channel_get(ddata->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) names[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) if (IS_ERR(ddata->channels[i])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) error = PTR_ERR(ddata->channels[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) if (!ddata->channels[i]->indio_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) error = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793) goto out_err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) out_err:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) if (error != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) dev_err(ddata->dev, "could not initialize VBUS or ID IIO: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) static const struct power_supply_desc cpcap_charger_usb_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) .name = "usb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) .type = POWER_SUPPLY_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) .properties = cpcap_charger_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) .num_properties = ARRAY_SIZE(cpcap_charger_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) .get_property = cpcap_charger_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) .set_property = cpcap_charger_set_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) .property_is_writeable = cpcap_charger_property_is_writeable,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) static const struct of_device_id cpcap_charger_id_table[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) .compatible = "motorola,mapphone-cpcap-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) {},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) MODULE_DEVICE_TABLE(of, cpcap_charger_id_table);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) static int cpcap_charger_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) struct cpcap_charger_ddata *ddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) const struct of_device_id *of_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) of_id = of_match_device(of_match_ptr(cpcap_charger_id_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) &pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) if (!of_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) ddata = devm_kzalloc(&pdev->dev, sizeof(*ddata), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840) if (!ddata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) ddata->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) ddata->voltage = 4200000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) ddata->reg = dev_get_regmap(ddata->dev->parent, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) if (!ddata->reg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) INIT_LIST_HEAD(&ddata->irq_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) INIT_DELAYED_WORK(&ddata->detect_work, cpcap_usb_detect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) INIT_DELAYED_WORK(&ddata->vbus_work, cpcap_charger_vbus_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) platform_set_drvdata(pdev, ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) error = cpcap_charger_init_iio(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) atomic_set(&ddata->active, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) psy_cfg.of_node = pdev->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) psy_cfg.drv_data = ddata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) ddata->usb = devm_power_supply_register(ddata->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) &cpcap_charger_usb_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) if (IS_ERR(ddata->usb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) error = PTR_ERR(ddata->usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) dev_err(ddata->dev, "failed to register USB charger: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) error = cpcap_usb_init_interrupts(pdev, ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) ddata->comparator.set_vbus = cpcap_charger_set_vbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) error = omap_usb2_set_comparator(&ddata->comparator);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) if (error == -ENODEV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) dev_info(ddata->dev, "charger needs phy, deferring probe\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) return -EPROBE_DEFER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) cpcap_charger_init_optional_gpios(ddata);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) schedule_delayed_work(&ddata->detect_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) static int cpcap_charger_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) struct cpcap_charger_ddata *ddata = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) int error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) atomic_set(&ddata->active, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899) error = omap_usb2_set_comparator(NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) dev_warn(ddata->dev, "could not clear USB comparator: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) error = cpcap_charger_set_state(ddata, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) dev_warn(ddata->dev, "could not clear charger: %i\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) error);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) cancel_delayed_work_sync(&ddata->vbus_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) cancel_delayed_work_sync(&ddata->detect_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) static struct platform_driver cpcap_charger_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) .probe = cpcap_charger_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) .name = "cpcap-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) .of_match_table = of_match_ptr(cpcap_charger_id_table),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) .remove = cpcap_charger_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) module_platform_driver(cpcap_charger_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) MODULE_AUTHOR("Tony Lindgren <tony@atomide.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) MODULE_DESCRIPTION("CPCAP Battery Charger Interface driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) MODULE_ALIAS("platform:cpcap-charger");