[linux-sunxi] Re: [PATCH 1/2] sunxi: power: add AXP803 support

2017-01-05 Thread Maxime Ripard
On Fri, Dec 23, 2016 at 04:31:32PM +0800, Icenowy Zheng wrote:
> The A64 uses the AXP803 as its PMIC.
> 
> Signed-off-by: Icenowy Zheng 
> ---
>  arch/arm/mach-sunxi/Makefile   |   3 +
>  arch/arm/mach-sunxi/pmic_bus.c |   6 +-
>  arch/arm/mach-sunxi/rsb.c  |   2 +-
>  board/sunxi/board.c|  31 ++---
>  drivers/power/Kconfig  |  87 --
>  drivers/power/Makefile |   1 +
>  drivers/power/axp803.c | 255 
> +
>  drivers/power/axp818.c |   2 +-
>  include/axp803.h   |  73 
>  include/axp_pmic.h |   3 +
>  10 files changed, 413 insertions(+), 50 deletions(-)
>  create mode 100644 drivers/power/axp803.c
>  create mode 100644 include/axp803.h
> 
> diff --git a/arch/arm/mach-sunxi/Makefile b/arch/arm/mach-sunxi/Makefile
> index 128091e4e6..dcf2b5245d 100644
> --- a/arch/arm/mach-sunxi/Makefile
> +++ b/arch/arm/mach-sunxi/Makefile
> @@ -19,9 +19,11 @@ endif
>  obj-$(CONFIG_MACH_SUN6I) += prcm.o
>  obj-$(CONFIG_MACH_SUN8I) += prcm.o
>  obj-$(CONFIG_MACH_SUN9I) += prcm.o
> +obj-$(CONFIG_MACH_SUN50I)+= prcm.o

Introducing a PRCM config option that would be selected by the various
MACH_* seems like a better option.

>  obj-$(CONFIG_MACH_SUN6I) += p2wi.o
>  obj-$(CONFIG_MACH_SUN8I) += rsb.o
>  obj-$(CONFIG_MACH_SUN9I) += rsb.o
> +obj-$(CONFIG_MACH_SUN50I)+= rsb.o

Ditto.

>  obj-$(CONFIG_MACH_SUN4I) += clock_sun4i.o
>  obj-$(CONFIG_MACH_SUN5I) += clock_sun4i.o
>  obj-$(CONFIG_MACH_SUN6I) += clock_sun6i.o
> @@ -37,6 +39,7 @@ obj-$(CONFIG_MACH_SUN9I)+= clock_sun9i.o gtbus_sun9i.o
>  obj-$(CONFIG_AXP152_POWER)   += pmic_bus.o
>  obj-$(CONFIG_AXP209_POWER)   += pmic_bus.o
>  obj-$(CONFIG_AXP221_POWER)   += pmic_bus.o
> +obj-$(CONFIG_AXP803_POWER)   += pmic_bus.o
>  obj-$(CONFIG_AXP809_POWER)   += pmic_bus.o
>  obj-$(CONFIG_AXP818_POWER)   += pmic_bus.o
>  
> diff --git a/arch/arm/mach-sunxi/pmic_bus.c b/arch/arm/mach-sunxi/pmic_bus.c
> index 7c57f02792..0ac64a97cf 100644
> --- a/arch/arm/mach-sunxi/pmic_bus.c
> +++ b/arch/arm/mach-sunxi/pmic_bus.c
> @@ -36,7 +36,7 @@ int pmic_bus_init(void)
>   if (!needs_init)
>   return 0;
>  
> -#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined 
> CONFIG_AXP818_POWER
> +#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP803_POWER || defined 
> CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER

And you need to wrap your line here.

>  # ifdef CONFIG_MACH_SUN6I
>   p2wi_init();
>   ret = p2wi_change_to_p2wi_mode(AXP221_CHIP_ADDR, AXP221_CTRL_ADDR,
> @@ -62,7 +62,7 @@ int pmic_bus_read(u8 reg, u8 *data)
>   return i2c_read(AXP152_I2C_ADDR, reg, 1, data, 1);
>  #elif defined CONFIG_AXP209_POWER
>   return i2c_read(AXP209_I2C_ADDR, reg, 1, data, 1);
> -#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined 
> CONFIG_AXP818_POWER
> +#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP803_POWER || defined 
> CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER

Ditto

>  # ifdef CONFIG_MACH_SUN6I
>   return p2wi_read(reg, data);
>  # else
> @@ -77,7 +77,7 @@ int pmic_bus_write(u8 reg, u8 data)
>   return i2c_write(AXP152_I2C_ADDR, reg, 1, &data, 1);
>  #elif defined CONFIG_AXP209_POWER
>   return i2c_write(AXP209_I2C_ADDR, reg, 1, &data, 1);
> -#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || defined 
> CONFIG_AXP818_POWER
> +#elif defined CONFIG_AXP221_POWER || defined CONFIG_AXP803_POWER || defined 
> CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER

Ditto

>  # ifdef CONFIG_MACH_SUN6I
>   return p2wi_write(reg, data);
>  # else
> diff --git a/arch/arm/mach-sunxi/rsb.c b/arch/arm/mach-sunxi/rsb.c
> index 6fd11f1529..28d05e962a 100644
> --- a/arch/arm/mach-sunxi/rsb.c
> +++ b/arch/arm/mach-sunxi/rsb.c
> @@ -20,7 +20,7 @@ static int rsb_set_device_mode(void);
>  
>  static void rsb_cfg_io(void)
>  {
> -#ifdef CONFIG_MACH_SUN8I
> +#if defined CONFIG_MACH_SUN8I || defined CONFIG_MACH_SUN50I
>   sunxi_gpio_set_cfgpin(SUNXI_GPL(0), SUN8I_GPL_R_RSB);
>   sunxi_gpio_set_cfgpin(SUNXI_GPL(1), SUN8I_GPL_R_RSB);
>   sunxi_gpio_set_pull(SUNXI_GPL(0), 1);
> diff --git a/board/sunxi/board.c b/board/sunxi/board.c
> index 53656383d5..3435b80c14 100644
> --- a/board/sunxi/board.c
> +++ b/board/sunxi/board.c
> @@ -487,26 +487,27 @@ void sunxi_board_init(void)
>  #endif
>  
>  #if defined CONFIG_AXP152_POWER || defined CONFIG_AXP209_POWER || \
> - defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
> - defined CONFIG_AXP818_POWER
> + defined CONFIG_AXP221_POWER || defined CONFIG_AXP803_POWER || \
> + defined CONFIG_AXP809_POWER || defined CONFIG_AXP818_POWER
>   power_failed = axp_init();
>  
> -#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP809_POWER || \
> - defined CONFIG_AXP818_POWER
> +#if defined CONFIG_AXP221_POWER || defined CONFIG_AXP803_POWER || \
> + 

Re: [linux-sunxi] Re: [PATCH 1/2] sunxi: power: add AXP803 support

2017-01-08 Thread Maxime Ripard
On Fri, Jan 06, 2017 at 06:47:30AM +0800, Icenowy Zheng wrote:
> > > +++ b/drivers/power/axp803.c 
> > > @@ -0,0 +1,255 @@ 
> > > +/* 
> > > + * AXP803 driver based on AXP818 driver 
> > > + * 
> > > + * Based on axp818.c 
> > > + * (C) Copyright 2015 Vishnu Patekar  
> > > + * 
> > > + * Based on axp221.c 
> > > + * (C) Copyright 2014 Hans de Goede  
> > > + * (C) Copyright 2013 Oliver Schinagl  
> > > + * 
> > > + * SPDX-License-Identifier: GPL-2.0+ 
> > > + */ 
> > > + 
> > > +#include  
> > > +#include  
> > > +#include  
> > > +#include  
> > > +#include  
> > > + 
> > > +static u8 axp803_mvolt_to_cfg(int mvolt, int min, int max, int div) 
> > > +{ 
> > > + if (mvolt < min) 
> > > + mvolt = min; 
> > > + else if (mvolt > max) 
> > > + mvolt = max; 
> > > + 
> > > + returnĀ  (mvolt - min) / div; 
> > > +} 
> > > + 
> > > +int axp_set_dcdc1(unsigned int mvolt) 
> > > +{ 
> > > + int ret; 
> > > + u8 cfg = axp803_mvolt_to_cfg(mvolt, 1600, 3400, 100); 
> > > + 
> > > + if (mvolt == 0) 
> > > + return pmic_bus_clrbits(AXP803_OUTPUT_CTRL1, 
> > > + AXP803_OUTPUT_CTRL1_DCDC1_EN); 
> > > + 
> > > + ret = pmic_bus_write(AXP803_DCDC1_CTRL, cfg); 
> > > + if (ret) 
> > > + return ret; 
> > > + 
> > > + return pmic_bus_setbits(AXP803_OUTPUT_CTRL1, 
> > > + AXP803_OUTPUT_CTRL1_DCDC1_EN); 
> > > +} 
> > > + 
> > > +int axp_set_dcdc2(unsigned int mvolt) 
> > > +{ 
> > > + int ret; 
> > > + u8 cfg; 
> > > + 
> > > + if (mvolt >= 1220) 
> > > + cfg = 70 + axp803_mvolt_to_cfg(mvolt, 1220, 1300, 20); 
> > > + else 
> > > + cfg = axp803_mvolt_to_cfg(mvolt, 500, 1200, 10); 
> > > + 
> > > + if (mvolt == 0) 
> > > + return pmic_bus_clrbits(AXP803_OUTPUT_CTRL1, 
> > > + AXP803_OUTPUT_CTRL1_DCDC2_EN); 
> > > + 
> > > + ret = pmic_bus_write(AXP803_DCDC2_CTRL, cfg); 
> > > + if (ret) 
> > > + return ret; 
> > > + 
> > > + return pmic_bus_setbits(AXP803_OUTPUT_CTRL1, 
> > > + AXP803_OUTPUT_CTRL1_DCDC2_EN); 
> > > +} 
> > > + 
> > > +int axp_set_dcdc3(unsigned int mvolt) 
> > > +{ 
> > > + int ret; 
> > > + u8 cfg; 
> > > + 
> > > + if (mvolt >= 1220) 
> > > + cfg = 70 + axp803_mvolt_to_cfg(mvolt, 1220, 1300, 20); 
> > > + else 
> > > + cfg = axp803_mvolt_to_cfg(mvolt, 500, 1200, 10); 
> > > + 
> > > + if (mvolt == 0) 
> > > + return pmic_bus_clrbits(AXP803_OUTPUT_CTRL1, 
> > > + AXP803_OUTPUT_CTRL1_DCDC3_EN); 
> > > + 
> > > + ret = pmic_bus_write(AXP803_DCDC3_CTRL, cfg); 
> > > + if (ret) 
> > > + return ret; 
> > > + 
> > > + return pmic_bus_setbits(AXP803_OUTPUT_CTRL1, 
> > > + AXP803_OUTPUT_CTRL1_DCDC3_EN); 
> > > +} 
> > > + 
> > > +int axp_set_dcdc5(unsigned int mvolt) 
> > > +{ 
> > > + int ret; 
> > > + u8 cfg; 
> > > + 
> > > + if (mvolt >= 1140) 
> > > + cfg = 32 + axp803_mvolt_to_cfg(mvolt, 1140, 1840, 20); 
> > > + else 
> > > + cfg = axp803_mvolt_to_cfg(mvolt, 800, 1120, 10); 
> > > + 
> > > + if (mvolt == 0) 
> > > + return pmic_bus_clrbits(AXP803_OUTPUT_CTRL1, 
> > > + AXP803_OUTPUT_CTRL1_DCDC5_EN); 
> > > + 
> > > + ret = pmic_bus_write(AXP803_DCDC5_CTRL, cfg); 
> > > + if (ret) 
> > > + return ret; 
> > > + 
> > > + return pmic_bus_setbits(AXP803_OUTPUT_CTRL1, 
> > > + AXP803_OUTPUT_CTRL1_DCDC5_EN); 
> > > +} 
> > > + 
> > > +int axp_set_aldo(int aldo_num, unsigned int mvolt) 
> > > +{ 
> > > + int ret; 
> > > + u8 cfg; 
> > > + 
> > > + if (aldo_num < 1 || aldo_num > 3) 
> > > + return -EINVAL; 
> > > + 
> > > + if (mvolt == 0) 
> > > + return pmic_bus_clrbits(AXP803_OUTPUT_CTRL3, 
> > > + AXP803_OUTPUT_CTRL3_ALDO1_EN << (aldo_num - 1)); 
> > > + 
> > > + cfg = axp803_mvolt_to_cfg(mvolt, 700, 3300, 100); 
> > > + ret = pmic_bus_write(AXP803_ALDO1_CTRL + (aldo_num - 1), cfg); 
> > > + if (ret) 
> > > + return ret; 
> > > + 
> > > + return pmic_bus_setbits(AXP803_OUTPUT_CTRL3, 
> > > + AXP803_OUTPUT_CTRL3_ALDO1_EN << (aldo_num - 1)); 
> > > +} 
> > > + 
> > > +/* TODO: re-work other AXP drivers to consolidate ALDO functions. */ 
> > > +int axp_set_aldo1(unsigned int mvolt) 
> > > +{ 
> > > + return axp_set_aldo(1, mvolt); 
> > > +} 
> > > + 
> > > +int axp_set_aldo2(unsigned int mvolt) 
> > > +{ 
> > > + return axp_set_aldo(2, mvolt); 
> > > +} 
> > > + 
> > > +int axp_set_aldo3(unsigned int mvolt) 
> > > +{ 
> > > + return axp_set_aldo(3, mvolt); 
> > > +} 
> > > + 
> > > +int axp_set_dldo(int dldo_num, unsigned int mvolt) 
> > > +{ 
> > > + int ret; 
> > > + u8 cfg; 
> > > + 
> > > + if (dldo_num < 1 || dldo_num > 4) 
> > > + return -EINVAL; 
> > > + 
> > > + if (mvolt == 0) 
> > > + return pmic_bus_clrbits(AXP803_OUTPUT_CTRL2, 
> > > + AXP803_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1)); 
> > > + 
> > > + cfg = axp803_mvolt_to_cfg(mvolt, 700, 3300, 100); 
> > > + if (dldo_num == 2 && mvolt > 3300) 
> > > + cfg += 1 + axp803_mvolt_to_cfg(mvolt, 3400, 4200, 200); 
> > > + ret = pmic_bus_write(AXP803_DLDO1_CTRL + (dldo_num - 1), cfg); 
> > > + if (ret) 
> > > + return ret; 
> > > + 
> > > + return pmic_bus_setbits(AXP803_OUTPUT_CTRL2, 
> > > + AXP803_OUTPUT_CTRL2_DLDO1_EN << (dldo_num - 1)); 
> > > +} 
> > > + 
> > >