// SPDX-License-Identifier: GPL-2.0-or-later
/*
* AXP20x PMIC USB power supply status driver
*
* Copyright (C) 2015 Hans de Goede <hdegoede@redhat.com>
* Copyright (C) 2014 Bruno Prémont <bonbons@linux-vserver.org>
*/
#include <linux/bitops.h>
#include <linux/device.h>
#include <linux/devm-helpers.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/mfd/axp20x.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/pm.h>
#include <linux/power_supply.h>
#include <linux/regmap.h>
#include <linux/slab.h>
#include <linux/iio/consumer.h>
#include <linux/workqueue.h>
#define DRVNAME "axp20x-usb-power-supply"
#define AXP192_USB_OTG_STATUS 0x04
#define AXP20X_PWR_STATUS_VBUS_PRESENT BIT(5)
#define AXP20X_PWR_STATUS_VBUS_USED BIT(4)
#define AXP717_PWR_STATUS_VBUS_GOOD BIT(5)
#define AXP20X_USB_STATUS_VBUS_VALID BIT(2)
#define AXP717_PMU_FAULT_VBUS BIT(5)
#define AXP717_PMU_FAULT_VSYS BIT(3)
#define AXP20X_VBUS_VHOLD_uV(b) (4000000 + (((b) >> 3) & 7) * 100000)
#define AXP20X_VBUS_VHOLD_MASK GENMASK(5, 3)
#define AXP20X_VBUS_VHOLD_OFFSET 3
#define AXP20X_ADC_EN1_VBUS_CURR BIT(2)
#define AXP20X_ADC_EN1_VBUS_VOLT BIT(3)
#define AXP717_INPUT_VOL_LIMIT_MASK GENMASK(3, 0)
#define AXP717_INPUT_CUR_LIMIT_MASK GENMASK(5, 0)
#define AXP717_ADC_DATA_MASK GENMASK(14, 0)
#define AXP717_ADC_EN_VBUS_VOLT BIT(2)
/*
* Note do not raise the debounce time, we must report Vusb high within
* 100ms otherwise we get Vbus errors in musb.
*/
#define DEBOUNCE_TIME msecs_to_jiffies(50)
struct axp20x_usb_power;
struct axp_data {
const struct power_supply_desc *power_desc;
const char * const *irq_names;
unsigned int num_irq_names;
const int *curr_lim_table;
int curr_lim_table_size;
struct reg_field curr_lim_fld;
struct reg_field vbus_valid_bit;
struct reg_field vbus_mon_bit;
struct reg_field usb_bc_en_bit;
struct reg_field usb_bc_det_fld;
struct reg_field vbus_disable_bit;
bool vbus_needs_polling: 1;
void (*axp20x_read_vbus)(struct work_struct *work);
int (*axp20x_cfg_iio_chan)(struct platform_device *pdev,
struct axp20x_usb_power *power);
int (*axp20x_cfg_adc_reg)(struct axp20x_usb_power *power);
};
struct axp20x_usb_power {
struct device *dev;
struct regmap *regmap;
struct regmap_field *curr_lim_fld;
struct regmap_field *vbus_valid_bit;
struct regmap_field *vbus_mon_bit;
struct regmap_field *usb_bc_en_bit;
struct regmap_field *usb_bc_det_fld;
struct regmap_field *vbus_disable_bit;
struct power_supply *supply;
const struct axp_data *axp_data;
struct iio_channel *vbus_v;
struct iio_channel *vbus_i;
struct delayed_work vbus_detect;
int max_input_cur;
unsigned int old_status;
unsigned int online;
unsigned int num_irqs;
unsigned int irqs[] __counted_by(num_irqs);
};
static bool axp20x_usb_vbus_needs_polling(struct axp20x_usb_power *power)
{
/*
* Polling is only necessary while VBUS is offline. While online, a
* present->absent transition implies an online->offline transition
* and will trigger the VBUS_REMOVAL IRQ.
*/
if (power->axp_data->vbus_needs_polling && !power->online)
return true;
return false;
}
static irqreturn_t axp20x_usb_power_irq(int irq, void *devid)
{
struct axp20x_usb_power *power = devid;
power_supply_changed(power->supply);
mod_delayed_work(system_power_efficient_wq, &power->vbus_detect, DEBOUNCE_TIME);
return IRQ_HANDLED;
}
static void axp20x_usb_power_poll_vbus(struct work_struct *work)
{
struct axp20x_usb_power *power =
container_of(work, struct axp20x_usb_power, vbus_detect.work);
unsigned int val;
int ret;
ret = regmap_read(power->regmap, AXP20X_PWR_INPUT_STATUS, &val);
if (ret)
goto out;
val &= (AXP20X_PWR_STATUS_VBUS_PRESENT | AXP20X_PWR_STATUS_VBUS_USED);
if (val != power->old_status)
power_supply_changed(power->supply);
if (power->usb_bc_en_bit && (val & AXP20X_PWR_STATUS_VBUS_PRESENT) !=
(power->old_status & AXP20X_PWR_STATUS_VBUS_PRESENT)) {
dev_dbg(power->dev, "Cable status changed, re-enabling USB BC");
ret = regmap_field_write(power->usb_bc_en_bit, 1);
if (ret)
dev_err(power->dev, "failed to enable USB BC: errno %d",
ret);
}
power->old_status = val;
power->online = val & AXP20X_PWR_STATUS_VBUS_USED;
out:
if (axp20x_usb_vbus_needs_polling(power))
mod_delayed_work(system_power_efficient_wq, &power->vbus_detect, DEBOUNCE_TIME);
}
static void axp717_usb_power_poll_vbus(struct work_struct *work)
{
struct axp20x_usb_power *power =
container_of(work, struct axp20x_usb_power, vbus_detect.work);
unsigned int val;
int ret;
ret = regmap_read(power->regmap, AXP717_ON_INDICATE, &val);
if (ret)
return;
val &= AXP717_PWR_STATUS_VBUS_GOOD;
if (val != power->old_status)
power_supply_changed(power->supply);
power->old_status = val;
}
static int axp20x_get_usb_type(struct axp20x_usb_power *power,
union power_supply_propval *val)
{
unsigned int reg;
int ret;
if (!power->usb_bc_det_fld)
return -EINVAL;
ret = regmap_field_read(power