Re: [PATCH v3 2/3] power: supply: Add driver for Microchip UCS1002

2019-04-29 Thread Andrey Smirnov
On Mon, Apr 29, 2019 at 1:36 PM Guenter Roeck  wrote:
>
> On Mon, Apr 29, 2019 at 12:53:48PM -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: Enric Balletbo Serra 
> > Signed-off-by: Andrey Smirnov 
> > Cc: Chris Healy 
> > Cc: Lucas Stach 
> > Cc: Fabio Estevam 
> > Cc: Guenter Roeck 
> > Cc: Sebastian Reichel 
> > Cc: linux-kernel@vger.kernel.org
> > Cc: linux...@vger.kernel.org
> > ---
> >  drivers/power/supply/Kconfig |   9 +
> >  drivers/power/supply/Makefile|   1 +
> >  drivers/power/supply/ucs1002_power.c | 646 +++
> >  3 files changed, 656 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 ..677f20a4d76f
> > --- /dev/null
> > +++ b/drivers/power/supply/ucs1002_power.c
> > @@ -0,0 +1,646 @@
> ...
> > +
> > +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;
> > +
> I hate to bring it up that late, but I don't see a check
> against val being negative anywhere in the calling code.
>

Sure, I'll add it in v4

Thanks,
Andrey Smirnov


Re: [PATCH v3 2/3] power: supply: Add driver for Microchip UCS1002

2019-04-29 Thread Guenter Roeck
On Mon, Apr 29, 2019 at 12:53:48PM -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: Enric Balletbo Serra 
> Signed-off-by: Andrey Smirnov 
> Cc: Chris Healy 
> Cc: Lucas Stach 
> Cc: Fabio Estevam 
> Cc: Guenter Roeck 
> Cc: Sebastian Reichel 
> Cc: linux-kernel@vger.kernel.org
> Cc: linux...@vger.kernel.org
> ---
>  drivers/power/supply/Kconfig |   9 +
>  drivers/power/supply/Makefile|   1 +
>  drivers/power/supply/ucs1002_power.c | 646 +++
>  3 files changed, 656 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 ..677f20a4d76f
> --- /dev/null
> +++ b/drivers/power/supply/ucs1002_power.c
> @@ -0,0 +1,646 @@
...
> +
> +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;
> +
I hate to bring it up that late, but I don't see a check
against val being negative anywhere in the calling code.

Guenter


[PATCH v3 2/3] power: supply: Add driver for Microchip UCS1002

2019-04-29 Thread Andrey Smirnov
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: Enric Balletbo Serra 
Signed-off-by: Andrey Smirnov 
Cc: Chris Healy 
Cc: Lucas Stach 
Cc: Fabio Estevam 
Cc: Guenter Roeck 
Cc: Sebastian Reichel 
Cc: linux-kernel@vger.kernel.org
Cc: linux...@vger.kernel.org
---
 drivers/power/supply/Kconfig |   9 +
 drivers/power/supply/Makefile|   1 +
 drivers/power/supply/ucs1002_power.c | 646 +++
 3 files changed, 656 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 ..677f20a4d76f
--- /dev/null
+++ b/drivers/power/supply/ucs1002_power.c
@@ -0,0 +1,646 @@
+// 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 
+#include 
+
+/* UCS1002 Registers */
+#define UCS1002_REG_CURRENT_MEASUREMENT0x00
+
+/*
+ * 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_ACTBIT(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_TSDBIT(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_PINBIT(2)
+#  define F_ACTIVE_MODE_MASK   GENMASK(5, 3)
+#  define F_ACTIVE_MODE_PASSTHROUGHF_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_CFG0x15
+#  define F_RATION_EN  BIT(3)
+
+/* Emulation Configuration Register */
+#define UCS1002_REG_EMU_CFG0x16
+
+/* 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_PASSTHROUGHF_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