^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * sy6982c/sy6982e charger driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Copyright (C) 2016 Fuzhou Rockchip Electronics Co., Ltd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * This program is free software; you can redistribute it and/or modify
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * it under the terms of the GNU General Public License as published by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * the Free Software Foundation; either version 2 of the License, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * (at your option) any later version.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * This program is distributed in the hope that it will be useful,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * but WITHOUT ANY WARRANTY; without even the implied warranty of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * GNU General Public License for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/extcon.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/irq.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/of_gpio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <linux/power_supply.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <linux/power/rk_usbbc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/property.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/rk_keys.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/workqueue.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) enum charger_t {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) USB_TYPE_UNKNOWN_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) USB_TYPE_NONE_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) USB_TYPE_USB_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) USB_TYPE_AC_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) USB_TYPE_CDP_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) DC_TYPE_DC_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) DC_TYPE_NONE_CHARGER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct sy6982c_charger {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct power_supply *usb_psy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct workqueue_struct *usb_charger_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct delayed_work usb_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct workqueue_struct *dc_charger_wq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct delayed_work dc_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) struct delayed_work discnt_work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct notifier_block cable_cg_nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) struct notifier_block cable_discnt_nb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) unsigned int bc_event;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) enum charger_t usb_charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) enum charger_t dc_charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) bool extcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct extcon_dev *cable_edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct gpio_desc *dc_det_pin;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) bool support_dc_det;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static void sy6982c_cg_bc_evt_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct sy6982c_charger *cg = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct sy6982c_charger, usb_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) struct extcon_dev *edev = cg->cable_edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) enum charger_t charger = USB_TYPE_UNKNOWN_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) const char *event[5] = {"UN", "NONE", "USB", "AC", "CDP1.5A"};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) /* Determine cable/charger type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (extcon_get_cable_state_(edev, EXTCON_CHG_USB_SDP) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) charger = USB_TYPE_USB_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) else if (extcon_get_cable_state_(edev, EXTCON_CHG_USB_DCP) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) charger = USB_TYPE_AC_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) else if (extcon_get_cable_state_(edev, EXTCON_CHG_USB_CDP) > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) charger = USB_TYPE_CDP_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) else if (extcon_get_cable_state_(edev, EXTCON_CHG_USB_DCP) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) charger = USB_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) else if (extcon_get_cable_state_(edev, EXTCON_CHG_USB_CDP) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) charger = USB_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (charger != USB_TYPE_UNKNOWN_CHARGER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) dev_info(cg->dev, "receive type-c notifier event: %s...\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) event[charger]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) cg->usb_charger = charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) static int sy6982c_cg_charger_evt_notifier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) unsigned long event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) struct sy6982c_charger *cg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) container_of(nb, struct sy6982c_charger, cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) queue_delayed_work(cg->usb_charger_wq, &cg->usb_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) static void sy6982c_cg_discnt_evt_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) struct sy6982c_charger *cg = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) struct sy6982c_charger, discnt_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) if (extcon_get_cable_state_(cg->cable_edev, EXTCON_USB) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) dev_info(cg->dev, "receive type-c notifier event: DISCNT...\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) cg->usb_charger = USB_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) static int sy6982c_cg_discnt_evt_notfier(struct notifier_block *nb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct sy6982c_charger *cg =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) container_of(nb, struct sy6982c_charger, cable_discnt_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) queue_delayed_work(cg->usb_charger_wq, &cg->discnt_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) static int sy6982c_cg_init_usb(struct sy6982c_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct device *dev = cg->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct extcon_dev *edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!cg->extcon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) edev = extcon_get_edev_by_phandle(dev, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (IS_ERR(edev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (PTR_ERR(edev) != -EPROBE_DEFER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) dev_err(dev, "Invalid or missing extcon\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) return PTR_ERR(edev);
^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) cg->usb_charger_wq = alloc_ordered_workqueue("%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) WQ_MEM_RECLAIM | WQ_FREEZABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) "sy6982c-usb-wq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) cg->usb_charger = USB_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) /* Register chargers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) INIT_DELAYED_WORK(&cg->usb_work, sy6982c_cg_bc_evt_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) cg->cable_cg_nb.notifier_call = sy6982c_cg_charger_evt_notifier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ret = devm_extcon_register_notifier(dev, edev, EXTCON_CHG_USB_SDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) dev_err(dev, "failed to register notifier for SDP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) goto __fail;
^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) ret = devm_extcon_register_notifier(dev, edev, EXTCON_CHG_USB_DCP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) dev_err(dev, "failed to register notifier for DCP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) goto __fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) ret = devm_extcon_register_notifier(dev, edev, EXTCON_CHG_USB_CDP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) &cg->cable_cg_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) dev_err(dev, "failed to register notifier for CDP\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) goto __fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* Register discnt usb */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) INIT_DELAYED_WORK(&cg->discnt_work, sy6982c_cg_discnt_evt_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) cg->cable_discnt_nb.notifier_call = sy6982c_cg_discnt_evt_notfier;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) ret = devm_extcon_register_notifier(dev, edev, EXTCON_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) &cg->cable_discnt_nb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) dev_err(dev, "failed to register notifier for HOST\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) goto __fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) cg->cable_edev = edev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) schedule_delayed_work(&cg->usb_work, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) dev_info(cg->dev, "register extcon evt notifier\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) __fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) destroy_workqueue(cg->usb_charger_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static enum power_supply_property sy6982c_usb_props[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) POWER_SUPPLY_PROP_ONLINE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) POWER_SUPPLY_PROP_STATUS,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) static int sy6982c_cg_usb_get_property(struct power_supply *psy,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) enum power_supply_property psp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) union power_supply_propval *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct sy6982c_charger *cg = power_supply_get_drvdata(psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int online = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) if (cg->usb_charger != USB_TYPE_UNKNOWN_CHARGER &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) cg->usb_charger != USB_TYPE_NONE_CHARGER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) if (cg->dc_charger != DC_TYPE_NONE_CHARGER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) online = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) switch (psp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) case POWER_SUPPLY_PROP_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) val->intval = online;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) dev_dbg(cg->dev, "report online: %d\n", val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) case POWER_SUPPLY_PROP_STATUS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (online)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) val->intval = POWER_SUPPLY_STATUS_CHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) dev_dbg(cg->dev, "report prop: %d\n", val->intval);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) break;
^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) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) static const struct power_supply_desc sy6982c_usb_desc = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) .name = "sy6982c_usb",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) .type = POWER_SUPPLY_TYPE_USB,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) .properties = sy6982c_usb_props,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) .num_properties = ARRAY_SIZE(sy6982c_usb_props),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) .get_property = sy6982c_cg_usb_get_property,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) static int sy6982c_cg_init_power_supply(struct sy6982c_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct power_supply_config psy_cfg = { .drv_data = cg, };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) cg->usb_psy = devm_power_supply_register(cg->dev, &sy6982c_usb_desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) &psy_cfg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) if (IS_ERR(cg->usb_psy)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) dev_err(cg->dev, "register usb power supply fail\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) return PTR_ERR(cg->usb_psy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) return 0;
^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) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) static int sy6982c_charger_parse_dt(struct sy6982c_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) struct device *dev = cg->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) cg->dc_det_pin = devm_gpiod_get_optional(dev, "dc-det",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) GPIOD_IN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) if (!IS_ERR_OR_NULL(cg->dc_det_pin)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) cg->support_dc_det = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) dev_err(dev, "invalid dc det gpio!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) cg->support_dc_det = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) static int sy6982c_charger_parse_dt(struct sy6982c_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) static enum charger_t sy6982c_charger_get_dc_state(struct sy6982c_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) return (gpiod_get_value(cg->dc_det_pin)) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) DC_TYPE_DC_CHARGER : DC_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static void sy6982c_charger_dc_det_worker(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) enum charger_t charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) struct sy6982c_charger *cg = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct sy6982c_charger, dc_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) charger = sy6982c_charger_get_dc_state(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (charger == DC_TYPE_DC_CHARGER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) cg->dc_charger = charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) cg->dc_charger = DC_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) rk_send_wakeup_key();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) static irqreturn_t sy6982c_charger_dc_det_isr(int irq, void *charger)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) struct sy6982c_charger *cg = (struct sy6982c_charger *)charger;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) queue_delayed_work(cg->dc_charger_wq, &cg->dc_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) msecs_to_jiffies(10));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) return IRQ_HANDLED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) static int sy6982c_charger_init_dc(struct sy6982c_charger *cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) unsigned long irq_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) unsigned int dc_det_irq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) if (!cg->support_dc_det)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) cg->dc_charger_wq = alloc_ordered_workqueue("%s",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) WQ_MEM_RECLAIM | WQ_FREEZABLE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) "sy6982c-dc-wq");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (!cg->dc_charger_wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) INIT_DELAYED_WORK(&cg->dc_work, sy6982c_charger_dc_det_worker);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) cg->dc_charger = DC_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (gpiod_get_value(cg->dc_det_pin))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) cg->dc_charger = DC_TYPE_DC_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) cg->dc_charger = DC_TYPE_NONE_CHARGER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) irq_flags = IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) dc_det_irq = gpiod_to_irq(cg->dc_det_pin);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) ret = devm_request_irq(cg->dev, dc_det_irq, sy6982c_charger_dc_det_isr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) irq_flags, "sy6982c_dc_det", cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (ret != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) dev_err(cg->dev, "sy6982c_dc_det_irq request failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) enable_irq_wake(dc_det_irq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) static int sy6982c_charger_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) struct sy6982c_charger *cg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) cg = devm_kzalloc(&pdev->dev, sizeof(*cg), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) if (!cg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) cg->dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) sy6982c_charger_parse_dt(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) sy6982c_charger_init_dc(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) cg->extcon = device_property_read_bool(cg->dev, "extcon");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) ret = sy6982c_cg_init_usb(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) dev_err(cg->dev, "init usb failed!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) ret = sy6982c_cg_init_power_supply(cg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) dev_err(cg->dev, "init power supply fail!\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) dev_info(cg->dev, "driver registered\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) static int sy6982c_charger_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) struct sy6982c_charger *cg = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) if (cg->usb_charger_wq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) destroy_workqueue(cg->usb_charger_wq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) static const struct of_device_id sy6982c_charger_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) .compatible = "sy6982c-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) {},
^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) static struct platform_driver sy6982c_charger_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) .probe = sy6982c_charger_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) .remove = sy6982c_charger_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) .name = "sy6982c-charger",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) .of_match_table = sy6982c_charger_match,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) module_platform_driver(sy6982c_charger_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) MODULE_ALIAS("platform:sy6982c-charger");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) MODULE_AUTHOR("chen Shunqing<csq@rock-chips.com>");