From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.3 required=3.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY, SPF_PASS,URIBL_BLOCKED,USER_AGENT_MUTT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D54ADC43218 for ; Fri, 26 Apr 2019 16:10:45 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 90D65208CB for ; Fri, 26 Apr 2019 16:10:45 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="R5wQVQMi" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726403AbfDZQKn (ORCPT ); Fri, 26 Apr 2019 12:10:43 -0400 Received: from mail-pf1-f193.google.com ([209.85.210.193]:42149 "EHLO mail-pf1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726039AbfDZQKn (ORCPT ); Fri, 26 Apr 2019 12:10:43 -0400 Received: by mail-pf1-f193.google.com with SMTP id w25so1945010pfi.9; Fri, 26 Apr 2019 09:10:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=IBy6ja0AP8r54q4wsYB3apS9KiZ5NEEMqGKMCMGCZzU=; b=R5wQVQMiKmd/6NtOCDh9t/O/mcKLvJlhJmGN3D3xCGE16uQd81D+rRERgkmZo89tLi ivupm/mMYP/c+HOqbl0PACfVAhDi943Q0WL20DiOo/83z4mBvRk6CumVvkAkLF6Mxye1 T6mSEqD/Qb3ZWtP7Vva1YNZDWPlH+JTtfj5XcCSghF/DygPmXuwQx4dbhocW0cl8JqO8 fV5lIiFk5HBK5NC3UP82mg+1NORFHQ2XSp+P6GdZONheEV8a3jwe8751OaWrS2V0vudS 95JNAPWrauzIeLen4bKVx1CH9DE8Jl4FGK8pegmtrJHXWEh3jDkv6mrDbGXq7Pjo9F8K WWzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=IBy6ja0AP8r54q4wsYB3apS9KiZ5NEEMqGKMCMGCZzU=; b=jEtCnW8Po01mb9gAqIE1WbjIp4MfZefOVKBXqxjwZjYR1SFAbtqlh15Md1wQK9AuLY GGPmLZWIHKXnQrdWUM5+U1SfCj+D48d3qUoSZ5aeGm97xhsKkBpSuoeuOk+XamZ/WVD2 OdlRiVn2TR8c8tQYUqKcN/i5CPtcGAAkfqBaFSyVW1Y012ZDCrzXU8pCm1Qtjbcogncq MeReHj1mYMAw0Eo3UNpFj+Pwhr2xQNG5pdo03mIyOgAwrkI9Qy3aWnfKFgLhJkBcGGfu rA3jNe7YyGbwEtMmSLs55Esiic4hn++KI2YSimh7v2lJkmXGcF6jledIi0R1Joc8vQ6F 2fNg== X-Gm-Message-State: APjAAAV+GH8dNYAk+3wUJtD/BXicuHjT/+iaqsZZm4upswV3+GAW+yAS Ng4AfC12EdUDJZ/70k3BvRRjdQ/h X-Google-Smtp-Source: APXvYqwvcIVZoeQCDS+oLiVLAYlZa7lcDHfrS6O8bhTCQF+4vuAoXUMsVbwnjqFtMy3xDpQlJZ8fuQ== X-Received: by 2002:a63:cf:: with SMTP id 198mr42769796pga.228.1556295042217; Fri, 26 Apr 2019 09:10:42 -0700 (PDT) Received: from localhost ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id y23sm38036474pfn.25.2019.04.26.09.10.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 26 Apr 2019 09:10:41 -0700 (PDT) Date: Fri, 26 Apr 2019 09:10:39 -0700 From: Guenter Roeck To: Andrey Smirnov Cc: linux-pm@vger.kernel.org, Chris Health , Lucas Stach , Fabio Estevam , Sebastian Reichel , linux-kernel@vger.kernel.org Subject: Re: [PATCH 2/3] power: supply: Add driver for Microchip UCS1002 Message-ID: <20190426161039.GA6341@roeck-us.net> References: <20190417084457.28747-1-andrew.smirnov@gmail.com> <20190417084457.28747-3-andrew.smirnov@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20190417084457.28747-3-andrew.smirnov@gmail.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 17, 2019 at 01:44:56AM -0700, Andrey Smirnov wrote: > Add driver for Microchip UCS1002 Programmable USB Port Power > Controller with Charger Emulation. The driver exposed a power supply > device to control/monitor various parameter of the device as well as a > regulator to allow controlling VBUS line. > > Signed-off-by: Andrey Smirnov > Cc: Chris Health > Cc: Lucas Stach > Cc: Fabio Estevam > Cc: Guenter Roeck > Cc: Sebastian Reichel > Cc: linux-kernel@vger.kernel.org > Cc: linux-pm@vger.kernel.org > --- > drivers/power/supply/Kconfig | 9 + > drivers/power/supply/Makefile | 1 + > drivers/power/supply/ucs1002_power.c | 639 +++++++++++++++++++++++++++ > 3 files changed, 649 insertions(+) > create mode 100644 drivers/power/supply/ucs1002_power.c > > diff --git a/drivers/power/supply/Kconfig b/drivers/power/supply/Kconfig > index e901b9879e7e..c614c8a196f3 100644 > --- a/drivers/power/supply/Kconfig > +++ b/drivers/power/supply/Kconfig > @@ -660,4 +660,13 @@ config FUEL_GAUGE_SC27XX > Say Y here to enable support for fuel gauge with SC27XX > PMIC chips. > > +config CHARGER_UCS1002 > + tristate "Microchip UCS1002 USB Port Power Controller" > + depends on I2C > + depends on OF > + select REGMAP_I2C > + help > + Say Y to enable support for Microchip UCS1002 Programmable > + USB Port Power Controller with Charger Emulation. > + > endif # POWER_SUPPLY > diff --git a/drivers/power/supply/Makefile b/drivers/power/supply/Makefile > index b731c2a9b695..c56803a9e4fe 100644 > --- a/drivers/power/supply/Makefile > +++ b/drivers/power/supply/Makefile > @@ -87,3 +87,4 @@ obj-$(CONFIG_AXP288_CHARGER) += axp288_charger.o > obj-$(CONFIG_CHARGER_CROS_USBPD) += cros_usbpd-charger.o > obj-$(CONFIG_CHARGER_SC2731) += sc2731_charger.o > obj-$(CONFIG_FUEL_GAUGE_SC27XX) += sc27xx_fuel_gauge.o > +obj-$(CONFIG_CHARGER_UCS1002) += ucs1002_power.o > diff --git a/drivers/power/supply/ucs1002_power.c b/drivers/power/supply/ucs1002_power.c > new file mode 100644 > index 000000000000..b0b81d62e003 > --- /dev/null > +++ b/drivers/power/supply/ucs1002_power.c > @@ -0,0 +1,639 @@ > +// SPDX-License-Identifier: GPL-2.0+ > +/* > + * Driver for UCS1002 Programmable USB Port Power Controller > + * > + * Copyright (C) 2019 Zodiac Inflight Innovations > + */ > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + You probably want to include linux/bits.h as well. > +/* UCS1002 Registers */ > +#define UCS1002_REG_CURRENT_MEASUREMENT 0x00 > + > +/* > + * The Total Accumulated Charge registers store the total accumulated > + * charge delivered from the VS source to a portable device. The total > + * value is calculated using four registers, from 01h to 04h. The bit > + * weighting of the registers is given in mA/hrs. > + */ > +#define UCS1002_REG_TOTAL_ACC_CHARGE 0x01 > + > +/* Other Status Register */ > +#define UCS1002_REG_OTHER_STATUS 0x0f > +# define F_ADET_PIN BIT(4) > +# define F_CHG_ACT BIT(3) > + > +/* Interrupt Status */ > +#define UCS1002_REG_INTERRUPT_STATUS 0x10 > +# define F_DISCHARGE_ERR BIT(6) > +# define F_RESET BIT(5) > +# define F_MIN_KEEP_OUT BIT(4) > +# define F_TSD BIT(3) > +# define F_OVER_VOLT BIT(2) > +# define F_BACK_VOLT BIT(1) > +# define F_OVER_ILIM BIT(0) > + > +/* Pin Status Register */ > +#define UCS1002_REG_PIN_STATUS 0x14 > +# define UCS1002_PWR_STATE_MASK 0x03 > +# define F_PWR_EN_PIN BIT(6) > +# define F_M2_PIN BIT(5) > +# define F_M1_PIN BIT(4) > +# define F_EM_EN_PIN BIT(3) > +# define F_SEL_PIN BIT(2) > +# define F_ACTIVE_MODE_MASK GENMASK(5, 3) > +# define F_ACTIVE_MODE_PASSTHROUGH F_M2_PIN > +# define F_ACTIVE_MODE_DEDICATED F_EM_EN_PIN > +# define F_ACTIVE_MODE_BC12_DCP (F_M2_PIN | F_EM_EN_PIN) > +# define F_ACTIVE_MODE_BC12_SDP F_M1_PIN > +# define F_ACTIVE_MODE_BC12_CDP (F_M1_PIN | F_M2_PIN | F_EM_EN_PIN) > + > +/* General Configuration Register */ > +#define UCS1002_REG_GENERAL_CFG 0x15 > +# define F_RATION_EN BIT(3) > + > +/* Emulation Configuration Register */ > +#define UCS1002_REG_EMU_CFG 0x16 > + > +/* Switch Configuration Register */ > +#define UCS1002_REG_SWITCH_CFG 0x17 > +# define F_PIN_IGNORE BIT(7) > +# define F_EM_EN_SET BIT(5) > +# define F_M2_SET BIT(4) > +# define F_M1_SET BIT(3) > +# define F_S0_SET BIT(2) > +# define F_PWR_EN_SET BIT(1) > +# define F_LATCH_SET BIT(0) > +# define V_SET_ACTIVE_MODE_MASK GENMASK(5, 3) > +# define V_SET_ACTIVE_MODE_PASSTHROUGH F_M2_SET > +# define V_SET_ACTIVE_MODE_DEDICATED F_EM_EN_SET > +# define V_SET_ACTIVE_MODE_BC12_DCP (F_M2_SET | F_EM_EN_SET) > +# define V_SET_ACTIVE_MODE_BC12_SDP F_M1_SET > +# define V_SET_ACTIVE_MODE_BC12_CDP (F_M1_SET | F_M2_SET | F_EM_EN_SET) > + > +/* Current Limit Register */ > +#define UCS1002_REG_ILIMIT 0x19 > +# define UCS1002_ILIM_SW_MASK GENMASK(3, 0) > + > +/* Product ID */ > +#define UCS1002_REG_PRODUCT_ID 0xfd > +# define UCS1002_PRODUCT_ID 0x4e > + > +/* Manufacture name */ > +#define UCS1002_MANUFACTURER "SMSC" > + > +struct ucs1002_info { > + struct power_supply *charger; > + struct i2c_client *client; > + struct regmap *regmap; > + struct regulator_desc *regulator_descriptor; > + bool present; > +}; > + > +static enum power_supply_property ucs1002_props[] = { > + POWER_SUPPLY_PROP_ONLINE, > + POWER_SUPPLY_PROP_CHARGE_NOW, > + POWER_SUPPLY_PROP_CURRENT_NOW, > + POWER_SUPPLY_PROP_CURRENT_MAX, > + POWER_SUPPLY_PROP_PRESENT, /* the presence of PED */ > + POWER_SUPPLY_PROP_MANUFACTURER, > + POWER_SUPPLY_PROP_USB_TYPE, > + POWER_SUPPLY_PROP_HEALTH, > +}; > + > +static int ucs1002_get_online(struct ucs1002_info *info, > + union power_supply_propval *val) > +{ > + unsigned int reg; > + int ret; > + > + ret = regmap_read(info->regmap, UCS1002_REG_OTHER_STATUS, ®); > + if (ret) > + return ret; > + > + val->intval = !!(reg & F_CHG_ACT); > + > + return 0; > +} > + > +static int ucs1002_get_charge(struct ucs1002_info *info, > + union power_supply_propval *val) > +{ > + /* > + * To fit within 32 bits some values are rounded (uA/h) > + * > + * For Total Accumulated Charge Middle Low Byte register, addr > + * 03h, byte 2 > + * > + * B0: 0.01084 mA/h rounded to 11 uA/h > + * B1: 0.02169 mA/h rounded to 22 uA/h > + * B2: 0.04340 mA/h rounded to 43 uA/h > + * B3: 0.08676 mA/h rounded to 87 uA/h > + * B4: 0.17350 mA/h rounded to 173 uÁ/h > + * > + * For Total Accumulated Charge Low Byte register, addr 04h, > + * byte 3 > + * > + * B6: 0.00271 mA/h rounded to 3 uA/h > + * B7: 0.005422 mA/h rounded to 5 uA/h > + */ > + static const int bit_weights_uAh[BITS_PER_TYPE(u32)] = { > + 0, 0, 0, 0, 0, 0, 3, 5, > + > + 11, 22, 43, 87, 173, 347, 694, 1388, > + > + 2776, 5552, 11105, 22210, 44420, 88840, 177700, 355400, > + > + 710700, 1421000, 2843000, 5685000, 11371000, 22742000, > + 45484000, 90968000, > + }; > + unsigned long total_acc_charger; > + unsigned int reg; > + int i, ret; > + > + ret = regmap_bulk_read(info->regmap, UCS1002_REG_TOTAL_ACC_CHARGE, > + ®, sizeof(u32)); > + if (ret) > + return ret; > + > + total_acc_charger = be32_to_cpu(reg); > + val->intval = 0; > + > + for_each_set_bit(i, &total_acc_charger, ARRAY_SIZE(bit_weights_uAh)) > + val->intval += bit_weights_uAh[i]; > + > + return 0; > +} > + > +static int ucs1002_get_current(struct ucs1002_info *info, > + union power_supply_propval *val) > +{ > + /* > + * The Current Measurement register stores the measured > + * current value delivered to the portable device. The range > + * is from 9.76 mA to 2.5 A. > + */ > + static const int bit_weights_uA[BITS_PER_TYPE(u8)] = { > + 9760, 19500, 39000, 78100, 156200, 312300, 624600, 1249300, > + }; > + unsigned long current_measurement; > + unsigned int reg; > + int i, ret; > + > + ret = regmap_read(info->regmap, UCS1002_REG_CURRENT_MEASUREMENT, ®); > + if (ret) > + return ret; > + > + current_measurement = reg; > + val->intval = 0; > + > + for_each_set_bit(i, ¤t_measurement, ARRAY_SIZE(bit_weights_uA)) > + val->intval += bit_weights_uA[i]; > + > + return 0; > +} > + > +/* > + * The Current Limit register stores the maximum current used by the > + * port switch. The range is from 500mA to 2.5 A. > + */ > +static const u32 ucs1002_current_limit_uA[] = { > + 500000, 900000, 1000000, 1200000, 1500000, 1800000, 2000000, 2500000, > +}; > + > +static int ucs1002_get_max_current(struct ucs1002_info *info, > + union power_supply_propval *val) > +{ > + unsigned int reg; > + int ret; > + > + ret = regmap_read(info->regmap, UCS1002_REG_ILIMIT, ®); > + if (ret) > + return ret; > + > + val->intval = ucs1002_current_limit_uA[reg & UCS1002_ILIM_SW_MASK]; > + > + return 0; > +} > + > +static int ucs1002_set_max_current(struct ucs1002_info *info, u32 val) > +{ > + unsigned int reg; > + int ret, idx; > + > + for (idx = 0; idx < ARRAY_SIZE(ucs1002_current_limit_uA); idx++) { > + if (val == ucs1002_current_limit_uA[idx]) > + break; > + } > + > + if (idx == ARRAY_SIZE(ucs1002_current_limit_uA)) { > + dev_err(&info->client->dev, > + "%d is an invalid max current value\n", val); Here the error message is displayed as result of an attempt to set the current limit, which presumably can happen from userspace. This is inconsistent with the error returns below. I am not sure I understand why this would warrant an error log, but exceeding the limit set with the ILIM pin doesn't. I would suggest to drop this error message. > + return -EINVAL; > + } > + > + ret = regmap_write(info->regmap, UCS1002_REG_ILIMIT, idx); > + if (ret) > + return ret; > + /* > + * Any current limit setting exceeding the one set via ILIM > + * pin will be rejected, so we read out freshly changed limit > + * to make sure that it took effect. > + */ > + ret = regmap_read(info->regmap, UCS1002_REG_ILIMIT, ®); > + if (ret) > + return ret; > + > + if (reg != idx) > + return -EINVAL; > + > + return 0; > +} > + > +static enum power_supply_usb_type ucs1002_usb_types[] = { > + POWER_SUPPLY_USB_TYPE_PD, > + POWER_SUPPLY_USB_TYPE_SDP, > + POWER_SUPPLY_USB_TYPE_DCP, > + POWER_SUPPLY_USB_TYPE_CDP, > + POWER_SUPPLY_USB_TYPE_UNKNOWN, > +}; > + > +static int ucs1002_set_usb_type(struct ucs1002_info *info, int val) > +{ > + unsigned int mode; > + > + if (val >= ARRAY_SIZE(ucs1002_usb_types)) > + return -EINVAL; > + > + switch (ucs1002_usb_types[val]) { > + case POWER_SUPPLY_USB_TYPE_PD: > + mode = V_SET_ACTIVE_MODE_DEDICATED; > + break; > + case POWER_SUPPLY_USB_TYPE_SDP: > + mode = V_SET_ACTIVE_MODE_BC12_SDP; > + break; > + case POWER_SUPPLY_USB_TYPE_DCP: > + mode = V_SET_ACTIVE_MODE_BC12_DCP; > + break; > + case POWER_SUPPLY_USB_TYPE_CDP: > + mode = V_SET_ACTIVE_MODE_BC12_CDP; > + break; > + default: > + return -EINVAL; > + } > + > + return regmap_update_bits(info->regmap, UCS1002_REG_SWITCH_CFG, > + V_SET_ACTIVE_MODE_MASK, mode); > +} > + > +static int ucs1002_get_usb_type(struct ucs1002_info *info, > + union power_supply_propval *val) > +{ > + enum power_supply_usb_type type; > + unsigned int reg; > + int ret; > + > + ret = regmap_read(info->regmap, UCS1002_REG_PIN_STATUS, ®); > + if (ret) > + return ret; > + > + switch (reg & F_ACTIVE_MODE_MASK) { > + default: > + type = POWER_SUPPLY_USB_TYPE_UNKNOWN; > + break; > + case F_ACTIVE_MODE_DEDICATED: > + type = POWER_SUPPLY_USB_TYPE_PD; > + break; > + case F_ACTIVE_MODE_BC12_SDP: > + type = POWER_SUPPLY_USB_TYPE_SDP; > + break; > + case F_ACTIVE_MODE_BC12_DCP: > + type = POWER_SUPPLY_USB_TYPE_DCP; > + break; > + case F_ACTIVE_MODE_BC12_CDP: > + type = POWER_SUPPLY_USB_TYPE_CDP; > + break; > + }; > + > + val->intval = type; > + > + return 0; > +} > + > +static int ucs1002_get_health(struct ucs1002_info *info, > + union power_supply_propval *val) > +{ > + unsigned int reg; > + int ret, health; > + > + ret = regmap_read(info->regmap, UCS1002_REG_INTERRUPT_STATUS, ®); > + if (ret) > + return ret; > + > + if (reg & F_TSD) > + health = POWER_SUPPLY_HEALTH_OVERHEAT; > + else if (reg & (F_OVER_VOLT | F_BACK_VOLT)) > + health = POWER_SUPPLY_HEALTH_OVERVOLTAGE; > + else if (reg & F_OVER_ILIM) > + health = POWER_SUPPLY_HEALTH_OVERCURRENT; > + else if (reg & (F_DISCHARGE_ERR | F_MIN_KEEP_OUT)) > + health = POWER_SUPPLY_HEALTH_UNSPEC_FAILURE; > + else > + health = POWER_SUPPLY_HEALTH_GOOD; > + > + val->intval = health; > + > + return 0; > +} > + > +static int ucs1002_get_property(struct power_supply *psy, > + enum power_supply_property psp, > + union power_supply_propval *val) > +{ > + struct ucs1002_info *info = power_supply_get_drvdata(psy); > + > + switch (psp) { > + case POWER_SUPPLY_PROP_ONLINE: > + return ucs1002_get_online(info, val); > + case POWER_SUPPLY_PROP_CHARGE_NOW: > + return ucs1002_get_charge(info, val); > + case POWER_SUPPLY_PROP_CURRENT_NOW: > + return ucs1002_get_current(info, val); > + case POWER_SUPPLY_PROP_CURRENT_MAX: > + return ucs1002_get_max_current(info, val); > + case POWER_SUPPLY_PROP_USB_TYPE: > + return ucs1002_get_usb_type(info, val); > + case POWER_SUPPLY_PROP_HEALTH: > + return ucs1002_get_health(info, val); > + case POWER_SUPPLY_PROP_PRESENT: > + val->intval = info->present; > + return 0; > + case POWER_SUPPLY_PROP_MANUFACTURER: > + val->strval = UCS1002_MANUFACTURER; > + return 0; > + default: > + return -EINVAL; > + } > +} > + > +static int ucs1002_set_property(struct power_supply *psy, > + enum power_supply_property psp, > + const union power_supply_propval *val) > +{ > + struct ucs1002_info *info = power_supply_get_drvdata(psy); > + > + switch (psp) { > + case POWER_SUPPLY_PROP_CURRENT_MAX: > + return ucs1002_set_max_current(info, val->intval); > + case POWER_SUPPLY_PROP_USB_TYPE: > + return ucs1002_set_usb_type(info, val->intval); > + default: > + return -EINVAL; > + } > +} > + > +static int ucs1002_property_is_writeable(struct power_supply *psy, > + enum power_supply_property psp) > +{ > + switch (psp) { > + case POWER_SUPPLY_PROP_CURRENT_MAX: > + case POWER_SUPPLY_PROP_USB_TYPE: > + return true; > + default: > + return false; > + } > +} > + > +static const struct power_supply_desc ucs1002_charger_desc = { > + .name = "ucs1002", > + .type = POWER_SUPPLY_TYPE_USB, > + .usb_types = ucs1002_usb_types, > + .num_usb_types = ARRAY_SIZE(ucs1002_usb_types), > + .get_property = ucs1002_get_property, > + .set_property = ucs1002_set_property, > + .property_is_writeable = ucs1002_property_is_writeable, > + .properties = ucs1002_props, > + .num_properties = ARRAY_SIZE(ucs1002_props), > +}; > + > +static irqreturn_t ucs1002_charger_irq(int irq, void *data) > +{ > + int ret, regval; > + bool present; > + struct ucs1002_info *info = data; > + > + present = info->present; > + > + ret = regmap_read(info->regmap, UCS1002_REG_OTHER_STATUS, ®val); > + if (ret) > + return IRQ_HANDLED; > + > + /* update attached status */ > + info->present = (regval & F_ADET_PIN) ? true : false; !!(regval & F_ADET_PIN); would do. > + > + /* notify the change */ > + if (present != info->present) > + power_supply_changed(info->charger); > + > + return IRQ_HANDLED; > +} > + > +static irqreturn_t ucs1002_alert_irq(int irq, void *data) > +{ > + struct ucs1002_info *info = data; > + > + power_supply_changed(info->charger); > + > + return IRQ_HANDLED; > +} > + > +static const struct regulator_ops ucs1002_regulator_ops = { > + .is_enabled = regulator_is_enabled_regmap, > + .enable = regulator_enable_regmap, > + .disable = regulator_disable_regmap, > +}; > + > +static const struct regulator_desc ucs1002_regulator_descriptor = { > + .name = "ucs1002-vbus", > + .ops = &ucs1002_regulator_ops, > + .type = REGULATOR_VOLTAGE, > + .owner = THIS_MODULE, > + .enable_reg = UCS1002_REG_SWITCH_CFG, > + .enable_mask = F_PWR_EN_SET, > + .enable_val = F_PWR_EN_SET, > + .fixed_uV = 5000000, > + .n_voltages = 1, > +}; > + > +static int ucs1002_probe(struct i2c_client *client, > + const struct i2c_device_id *dev_id) > +{ > + struct device *dev = &client->dev; > + struct power_supply_config charger_config = {}; > + const struct regmap_config regmap_config = { > + .reg_bits = 8, > + .val_bits = 8, > + }; > + struct regulator_config regulator_config = {}; > + int irq_a_det, irq_alert, ret; > + struct regulator_dev *rdev; > + struct ucs1002_info *info; > + unsigned int regval; > + > + info = devm_kzalloc(dev, sizeof(*info), GFP_KERNEL); > + if (!info) > + return -ENOMEM; > + > + info->regmap = devm_regmap_init_i2c(client, ®map_config); > + if (IS_ERR(info->regmap)) { > + ret = PTR_ERR(info->regmap); > + dev_err(dev, "regmap initialization failed: %d\n", ret); > + return ret; > + } > + > + info->client = client; > + > + irq_a_det = of_irq_get_byname(dev->of_node, "a_det"); > + irq_alert = of_irq_get_byname(dev->of_node, "alert"); > + > + charger_config.of_node = dev->of_node; > + charger_config.drv_data = info; > + > + ret = regmap_read(info->regmap, UCS1002_REG_PRODUCT_ID, ®val); > + if (ret) > + return ret; Silent error ? > + > + if (regval != UCS1002_PRODUCT_ID) { > + dev_err(dev, > + "Product ID does not match (0x%02x != 0x%02x)\n", > + regval, UCS1002_PRODUCT_ID); > + return -ENODEV; > + } > + > + dev_info(dev, "registered with product id 0x%02x\n", regval); > + Does that add any value ? After all, the displayed value is known to be UCS1002_PRODUCT_ID. It is also not really registered yet at this point. > + /* Enable charge rationing by default */ > + ret = regmap_update_bits(info->regmap, UCS1002_REG_GENERAL_CFG, > + F_RATION_EN, F_RATION_EN); > + if (ret) > + return ret; Another silent error ? Seems odd, especially since we just above already claimed that the device was registered. > + > + dev_dbg(dev, "set active mode selection through i2c\n"); > + /* > + * Ignore the M1, M2, PWR_EN, and EM_EN pin states. Set active > + * mode selection to Dedicated Charger Emulation Cycle. > + * > + * #M1 #M2 EM_EN > + * 0 0 1 - Dedicated Charger Emulation Cycle > + * > + */ > + ret = regmap_update_bits(info->regmap, UCS1002_REG_SWITCH_CFG, > + F_PIN_IGNORE | F_EM_EN_SET | F_M2_SET | > + F_M1_SET, F_PIN_IGNORE | F_EM_EN_SET); > + if (ret) > + return ret; And another one. > + /* > + * Be safe and set initial current limit to 500mA > + */ > + ret = ucs1002_set_max_current(info, 500000); > + if (ret) { > + dev_err(dev, "Failed to read max current default\n"); read ? > + return ret; > + } > + > + info->charger = devm_power_supply_register(dev, &ucs1002_charger_desc, > + &charger_config); > + if (IS_ERR(info->charger)) { > + dev_err(dev, "failed to register power supply\n"); > + return PTR_ERR(info->charger); > + } > + > + ret = regmap_read(info->regmap, UCS1002_REG_PIN_STATUS, ®val); > + if (ret) > + return ret; > + > + info->regulator_descriptor = > + devm_kmemdup(dev, &ucs1002_regulator_descriptor, > + sizeof(ucs1002_regulator_descriptor), > + GFP_KERNEL); What if devm_kmemdup() returns NULL ? > + info->regulator_descriptor->enable_is_inverted = !(regval & F_SEL_PIN); > + > + regulator_config.dev = dev; > + regulator_config.of_node = dev->of_node; > + regulator_config.regmap = info->regmap; > + > + rdev = devm_regulator_register(dev, info->regulator_descriptor, > + ®ulator_config); > + if (IS_ERR(rdev)) { > + dev_err(dev, "failed to register VBUS regulator\n"); > + return PTR_ERR(rdev); > + } > + > + if (irq_a_det > 0) { > + ret = devm_request_threaded_irq(dev, irq_a_det, NULL, > + ucs1002_charger_irq, > + IRQF_TRIGGER_FALLING | > + IRQF_TRIGGER_RISING | > + IRQF_ONESHOT, > + "ucs1002-a_det", info); > + if (ret) { > + dev_err(dev, "failed to request A_DET threaded irq\n"); > + return ret; > + } > + } > + > + if (irq_alert > 0) { > + ret = devm_request_threaded_irq(dev, irq_alert, NULL, > + ucs1002_alert_irq, > + IRQF_TRIGGER_FALLING | > + IRQF_TRIGGER_RISING | > + IRQF_ONESHOT, > + "ucs1002-alert", info); > + if (ret) { > + dev_err(dev, "failed to request ALERT threaded irq\n"); > + return ret; > + } > + } > + > + return 0; > +} > + > +#if IS_ENABLED(CONFIG_OF) > +static const struct of_device_id ucs1002_of_match[] = { > + { .compatible = "microchip,ucs1002", }, > + { /* sentinel */ }, > +}; > +MODULE_DEVICE_TABLE(of, ucs1002_of_match); > +#endif > + > +static const struct i2c_device_id ucs1002_ids[] = { > + {"ucs1002", 0}, > + { /* sentinel */ }, > +}; > +MODULE_DEVICE_TABLE(i2c, ucs1002_ids); > + > +static struct i2c_driver ucs1002_driver = { > + .driver = { > + .name = "ucs1002", > + .of_match_table = of_match_ptr(ucs1002_of_match), > + }, > + .probe = ucs1002_probe, > +}; > +module_i2c_driver(ucs1002_driver); > + > +MODULE_DESCRIPTION("Microchip UCS1002 Programmable USB Port Power Controller"); > +MODULE_AUTHOR("Enric Balletbo Serra "); > +MODULE_AUTHOR("Andrey Smirnov "); > +MODULE_LICENSE("GPL v2"); > -- > 2.20.1 >