^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) * Common power driver for PDAs and phones with one or two external
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * power supplies (AC/USB) connected to main and backup batteries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * and optional builtin charger.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/pda_power.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/regulator/consumer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/timer.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/jiffies.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/usb/otg.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) static inline unsigned int get_irq_flags(struct resource *res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) return IRQF_SHARED | (res->flags & IRQF_TRIGGER_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) static struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static struct pda_power_pdata *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) static struct resource *ac_irq, *usb_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) static struct delayed_work charger_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static struct delayed_work polling_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static struct delayed_work supply_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static int polling;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) static struct power_supply *pda_psy_ac, *pda_psy_usb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) static struct usb_phy *transceiver;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static struct notifier_block otg_nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) static struct regulator *ac_draw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) enum {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) PDA_PSY_OFFLINE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) PDA_PSY_ONLINE = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) PDA_PSY_TO_CHANGE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static int new_ac_status = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int new_usb_status = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static int ac_status = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static int usb_status = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) static int pda_power_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (psy->desc->type == POWER_SUPPLY_TYPE_MAINS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) val->intval = pdata->is_ac_online ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) pdata->is_ac_online() : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) val->intval = pdata->is_usb_online ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) pdata->is_usb_online() : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) static enum power_supply_property pda_power_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) static char *pda_power_supplied_to[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) "main-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) "backup-battery",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static const struct power_supply_desc pda_psy_ac_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) .name = "ac",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) .type = POWER_SUPPLY_TYPE_MAINS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) .properties = pda_power_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) .num_properties = ARRAY_SIZE(pda_power_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) .get_property = pda_power_get_property,
^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) static const struct power_supply_desc pda_psy_usb_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .name = "usb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .type = POWER_SUPPLY_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) .properties = pda_power_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) .num_properties = ARRAY_SIZE(pda_power_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) .get_property = pda_power_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) static void update_status(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) if (pdata->is_ac_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) new_ac_status = !!pdata->is_ac_online();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) if (pdata->is_usb_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) new_usb_status = !!pdata->is_usb_online();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static void update_charger(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) static int regulator_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int max_uA = pdata->ac_max_uA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (pdata->set_charge) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) if (new_ac_status > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) dev_dbg(dev, "charger on (AC)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) pdata->set_charge(PDA_POWER_CHARGE_AC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) } else if (new_usb_status > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) dev_dbg(dev, "charger on (USB)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) pdata->set_charge(PDA_POWER_CHARGE_USB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) dev_dbg(dev, "charger off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) pdata->set_charge(0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) } else if (ac_draw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (new_ac_status > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) regulator_set_current_limit(ac_draw, max_uA, max_uA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (!regulator_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) dev_dbg(dev, "charger on (AC)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) WARN_ON(regulator_enable(ac_draw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) regulator_enabled = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) if (regulator_enabled) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) dev_dbg(dev, "charger off\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) WARN_ON(regulator_disable(ac_draw));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) regulator_enabled = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) static void supply_work_func(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if (ac_status == PDA_PSY_TO_CHANGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) ac_status = new_ac_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) power_supply_changed(pda_psy_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) if (usb_status == PDA_PSY_TO_CHANGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) usb_status = new_usb_status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) power_supply_changed(pda_psy_usb);
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) static void psy_changed(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) update_charger();
^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) * Okay, charger set. Now wait a bit before notifying supplicants,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) * charge power should stabilize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) cancel_delayed_work(&supply_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) schedule_delayed_work(&supply_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) msecs_to_jiffies(pdata->wait_for_charger));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) static void charger_work_func(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) update_status();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) psy_changed();
^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 irqreturn_t power_changed_isr(int irq, void *power_supply)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) if (power_supply == pda_psy_ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) ac_status = PDA_PSY_TO_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) else if (power_supply == pda_psy_usb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) usb_status = PDA_PSY_TO_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) return IRQ_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * Wait a bit before reading ac/usb line status and setting charger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * because ac/usb status readings may lag from irq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) cancel_delayed_work(&charger_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) schedule_delayed_work(&charger_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) msecs_to_jiffies(pdata->wait_for_status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) static void polling_work_func(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) int changed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) dev_dbg(dev, "polling...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) update_status();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (!ac_irq && new_ac_status != ac_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) ac_status = PDA_PSY_TO_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) changed = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) if (!usb_irq && new_usb_status != usb_status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) usb_status = PDA_PSY_TO_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) changed = 1;
^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) if (changed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) psy_changed();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) cancel_delayed_work(&polling_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) schedule_delayed_work(&polling_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) msecs_to_jiffies(pdata->polling_interval));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static int otg_is_usb_online(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) return (transceiver->last_event == USB_EVENT_VBUS ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) transceiver->last_event == USB_EVENT_ENUMERATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) static int otg_is_ac_online(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return (transceiver->last_event == USB_EVENT_CHARGER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) static int otg_handle_notification(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) unsigned long event, void *unused)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) case USB_EVENT_CHARGER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) ac_status = PDA_PSY_TO_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) case USB_EVENT_VBUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) case USB_EVENT_ENUMERATED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) usb_status = PDA_PSY_TO_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) case USB_EVENT_NONE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) ac_status = PDA_PSY_TO_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) usb_status = PDA_PSY_TO_CHANGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) * Wait a bit before reading ac/usb line status and setting charger,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) * because ac/usb status readings may lag from irq.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) cancel_delayed_work(&charger_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) schedule_delayed_work(&charger_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) msecs_to_jiffies(pdata->wait_for_status));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) return NOTIFY_OK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) static int pda_power_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) struct power_supply_config psy_cfg = {};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (pdev->id != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) dev_err(dev, "it's meaningless to register several "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) "pda_powers; use id = -1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) goto wrongid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) pdata = pdev->dev.platform_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) if (pdata->init) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) ret = pdata->init(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) goto init_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) ac_draw = regulator_get(dev, "ac_draw");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) if (IS_ERR(ac_draw)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) dev_dbg(dev, "couldn't get ac_draw regulator\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) ac_draw = NULL;
^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) update_status();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) update_charger();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (!pdata->wait_for_status)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) pdata->wait_for_status = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (!pdata->wait_for_charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) pdata->wait_for_charger = 500;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) if (!pdata->polling_interval)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) pdata->polling_interval = 2000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) if (!pdata->ac_max_uA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) pdata->ac_max_uA = 500000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) INIT_DELAYED_WORK(&charger_work, charger_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) INIT_DELAYED_WORK(&supply_work, supply_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) ac_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "ac");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) usb_irq = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "usb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (pdata->supplied_to) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) psy_cfg.supplied_to = pdata->supplied_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) psy_cfg.num_supplicants = pdata->num_supplicants;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) psy_cfg.supplied_to = pda_power_supplied_to;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) psy_cfg.num_supplicants = ARRAY_SIZE(pda_power_supplied_to);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) if (!IS_ERR_OR_NULL(transceiver)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) if (!pdata->is_usb_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) pdata->is_usb_online = otg_is_usb_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) if (!pdata->is_ac_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) pdata->is_ac_online = otg_is_ac_online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) if (pdata->is_ac_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) pda_psy_ac = power_supply_register(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) &pda_psy_ac_desc, &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (IS_ERR(pda_psy_ac)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) dev_err(dev, "failed to register %s power supply\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) pda_psy_ac_desc.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) ret = PTR_ERR(pda_psy_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) goto ac_supply_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) if (ac_irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) ret = request_irq(ac_irq->start, power_changed_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) get_irq_flags(ac_irq), ac_irq->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) pda_psy_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) dev_err(dev, "request ac irq failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) goto ac_irq_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) polling = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) if (pdata->is_usb_online) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) pda_psy_usb = power_supply_register(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) &pda_psy_usb_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (IS_ERR(pda_psy_usb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) dev_err(dev, "failed to register %s power supply\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) pda_psy_usb_desc.name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) ret = PTR_ERR(pda_psy_usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) goto usb_supply_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) if (usb_irq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) ret = request_irq(usb_irq->start, power_changed_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) get_irq_flags(usb_irq),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) usb_irq->name, pda_psy_usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) dev_err(dev, "request usb irq failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) goto usb_irq_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) polling = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (!IS_ERR_OR_NULL(transceiver) && pdata->use_otg_notifier) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) otg_nb.notifier_call = otg_handle_notification;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) ret = usb_register_notifier(transceiver, &otg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) dev_err(dev, "failure to register otg notifier\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) goto otg_reg_notifier_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) polling = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) if (polling) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) dev_dbg(dev, "will poll for status\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) INIT_DELAYED_WORK(&polling_work, polling_work_func);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) cancel_delayed_work(&polling_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) schedule_delayed_work(&polling_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) msecs_to_jiffies(pdata->polling_interval));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) if (ac_irq || usb_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) device_init_wakeup(&pdev->dev, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) otg_reg_notifier_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) if (pdata->is_usb_online && usb_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) free_irq(usb_irq->start, pda_psy_usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) usb_irq_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) if (pdata->is_usb_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) power_supply_unregister(pda_psy_usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) usb_supply_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (pdata->is_ac_online && ac_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) free_irq(ac_irq->start, pda_psy_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) if (!IS_ERR_OR_NULL(transceiver))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) usb_put_phy(transceiver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) ac_irq_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) if (pdata->is_ac_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) power_supply_unregister(pda_psy_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) ac_supply_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (ac_draw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) regulator_put(ac_draw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) ac_draw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) if (pdata->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) pdata->exit(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) init_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) wrongid:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) static int pda_power_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) if (!IS_ERR_OR_NULL(transceiver) && pdata->use_otg_notifier)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) usb_unregister_notifier(transceiver, &otg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) if (pdata->is_usb_online && usb_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) free_irq(usb_irq->start, pda_psy_usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) if (pdata->is_ac_online && ac_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) free_irq(ac_irq->start, pda_psy_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) if (polling)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) cancel_delayed_work_sync(&polling_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) cancel_delayed_work_sync(&charger_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) cancel_delayed_work_sync(&supply_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) if (pdata->is_usb_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) power_supply_unregister(pda_psy_usb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448) if (pdata->is_ac_online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) power_supply_unregister(pda_psy_ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) #if IS_ENABLED(CONFIG_USB_PHY)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) if (!IS_ERR_OR_NULL(transceiver))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) usb_put_phy(transceiver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) if (ac_draw) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) regulator_put(ac_draw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) ac_draw = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) if (pdata->exit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) pdata->exit(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) #ifdef CONFIG_PM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) static int ac_wakeup_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) static int usb_wakeup_enabled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) static int pda_power_suspend(struct platform_device *pdev, pm_message_t state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (pdata->suspend) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) int ret = pdata->suspend(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) if (device_may_wakeup(&pdev->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) if (ac_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) ac_wakeup_enabled = !enable_irq_wake(ac_irq->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (usb_irq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) usb_wakeup_enabled = !enable_irq_wake(usb_irq->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) static int pda_power_resume(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) if (device_may_wakeup(&pdev->dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) if (usb_irq && usb_wakeup_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) disable_irq_wake(usb_irq->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) if (ac_irq && ac_wakeup_enabled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) disable_irq_wake(ac_irq->start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) if (pdata->resume)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) return pdata->resume();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) #define pda_power_suspend NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) #define pda_power_resume NULL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) #endif /* CONFIG_PM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) static struct platform_driver pda_power_pdrv = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) .name = "pda-power",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) .probe = pda_power_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) .remove = pda_power_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) .suspend = pda_power_suspend,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) .resume = pda_power_resume,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) module_platform_driver(pda_power_pdrv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) MODULE_AUTHOR("Anton Vorontsov <cbou@mail.ru>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) MODULE_ALIAS("platform:pda-power");