Re: [PATCH 40/46] ARM: pxa: tosa: use gpio lookup for battery

2019-10-19 Thread Sebastian Reichel
Hi,

On Fri, Oct 18, 2019 at 05:41:55PM +0200, Arnd Bergmann wrote:
> The battery driver uses a lot of GPIO lines, hardcoded from a
> machine header file.
> 
> Change it to use a gpiod lookup table instead.
> 
> Cc: Sebastian Reichel 
> Cc: linux...@vger.kernel.org
> Signed-off-by: Arnd Bergmann 
> ---

That's a nice cleanup patch :) I assume, that this is also
merged together with the whole series through ARM, so:

Reviewed-by: Sebastian Reichel 
Acked-by: Sebastian Reichel 

-- Sebastian

>  arch/arm/mach-pxa/tosa.c|  23 +
>  drivers/power/supply/tosa_battery.c | 147 
>  2 files changed, 109 insertions(+), 61 deletions(-)
> 
> diff --git a/arch/arm/mach-pxa/tosa.c b/arch/arm/mach-pxa/tosa.c
> index 8329a2969b2b..ef3bbf6d158e 100644
> --- a/arch/arm/mach-pxa/tosa.c
> +++ b/arch/arm/mach-pxa/tosa.c
> @@ -365,6 +365,28 @@ static struct pxaficp_platform_data 
> tosa_ficp_platform_data = {
>   .shutdown   = tosa_irda_shutdown,
>  };
>  
> +static struct gpiod_lookup_table tosa_battery_gpio_table = {
> + .dev_id = "wm97xx-battery",
> + .table = {
> + GPIO_LOOKUP("tc6393xb", TOSA_GPIO_CHARGE_OFF - 
> TOSA_TC6393XB_GPIO_BASE,"main charge off", GPIO_ACTIVE_HIGH ),
> + GPIO_LOOKUP("tc6393xb", TOSA_GPIO_CHARGE_OFF_JC - 
> TOSA_TC6393XB_GPIO_BASE, "jacket charge off", GPIO_ACTIVE_HIGH ),
> + GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BAT_SW_ON - 
> TOSA_TC6393XB_GPIO_BASE, "battery switch", GPIO_ACTIVE_HIGH ),
> + GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BAT0_V_ON - 
> TOSA_TC6393XB_GPIO_BASE, "main battery", GPIO_ACTIVE_HIGH ),
> + GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BAT1_V_ON - 
> TOSA_TC6393XB_GPIO_BASE, "jacket battery", GPIO_ACTIVE_HIGH ),
> + GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BAT1_TH_ON - 
> TOSA_TC6393XB_GPIO_BASE,"main battery temp", GPIO_ACTIVE_HIGH ),
> + GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BAT0_TH_ON - 
> TOSA_TC6393XB_GPIO_BASE,"jacket battery temp", GPIO_ACTIVE_HIGH ),
> + GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BU_CHRG_ON - 
> TOSA_TC6393XB_GPIO_BASE,"backup battery", GPIO_ACTIVE_HIGH ),
> +
> + GPIO_LOOKUP("gpio-pxa", TOSA_GPIO_BAT0_CRG,  "main battery 
> full", GPIO_ACTIVE_HIGH ),
> + GPIO_LOOKUP("gpio-pxa", TOSA_GPIO_BAT1_CRG,  "jacket 
> battery full", GPIO_ACTIVE_HIGH ),
> + GPIO_LOOKUP("gpio-pxa", TOSA_GPIO_BAT0_LOW,  "main battery 
> low", GPIO_ACTIVE_HIGH ),
> + GPIO_LOOKUP("gpio-pxa", TOSA_GPIO_BAT1_LOW,  "jacket 
> battery low", GPIO_ACTIVE_HIGH ),
> + GPIO_LOOKUP("gpio-pxa", TOSA_GPIO_JACKET_DETECT, "jacket 
> detect", GPIO_ACTIVE_HIGH ),
> + { },
> + },
> +};
> +
> +
>  /*
>   * Tosa AC IN
>   */
> @@ -946,6 +968,7 @@ static void __init tosa_init(void)
>   /* enable batt_fault */
>   PMCR = 0x01;
>  
> + gpiod_add_lookup_table(_battery_gpio_table);
>   gpiod_add_lookup_table(_mci_gpio_table);
>   gpiod_add_lookup_table(_audio_gpio_table);
>   pxa_set_mci_info(_mci_platform_data);
> diff --git a/drivers/power/supply/tosa_battery.c 
> b/drivers/power/supply/tosa_battery.c
> index b26b0eca33e1..d10320f348d0 100644
> --- a/drivers/power/supply/tosa_battery.c
> +++ b/drivers/power/supply/tosa_battery.c
> @@ -15,11 +15,16 @@
>  #include 
>  
>  #include 
> -#include 
>  
>  static DEFINE_MUTEX(bat_lock); /* protects gpio pins */
>  static struct work_struct bat_work;
>  
> +struct tosa_gpio {
> + const char *con;
> + enum gpiod_flags flags;
> + struct gpio_desc *desc;
> +};
> +
>  struct tosa_bat {
>   int status;
>   struct power_supply *psy;
> @@ -28,38 +33,42 @@ struct tosa_bat {
>   struct mutex work_lock; /* protects data */
>  
>   bool (*is_present)(struct tosa_bat *bat);
> - int gpio_full;
> - int gpio_charge_off;
> + struct tosa_gpio gpio_full;
> + struct tosa_gpio gpio_charge_off;
>  
>   int technology;
>  
> - int gpio_bat;
> + struct tosa_gpio gpio_bat;
>   int adc_bat;
>   int adc_bat_divider;
>   int bat_max;
>   int bat_min;
>  
> - int gpio_temp;
> + struct tosa_gpio gpio_temp;
>   int adc_temp;
>   int adc_temp_divider;
>  };
>  
>  static struct tosa_bat tosa_bat_main;
>  static struct tosa_bat tosa_bat_jacket;
> +static struct tosa_gpio gpiod_jacket_det = { "jacket detect", GPIOD_IN };
> +static struct tosa_gpio gpiod_battery_switch = { "battery switch", 
> GPIOD_OUT_LOW };
> +static struct tosa_gpio gpiod_main_battery_low = { "main battery low", 
> GPIOD_IN };
> +static struct tosa_gpio gpiod_jacket_battery_low = { "jacket battery low", 
> GPIOD_IN };
>  
>  static unsigned long tosa_read_bat(struct tosa_bat *bat)
>  {
>   unsigned long value = 0;
>  
> - if (bat->gpio_bat < 0 || bat->adc_bat < 0)
> + if (!bat->gpio_bat.desc || bat->adc_bat < 0)
>   return 0;
>  
>   

[PATCH 40/46] ARM: pxa: tosa: use gpio lookup for battery

2019-10-18 Thread Arnd Bergmann
The battery driver uses a lot of GPIO lines, hardcoded from a
machine header file.

Change it to use a gpiod lookup table instead.

Cc: Sebastian Reichel 
Cc: linux...@vger.kernel.org
Signed-off-by: Arnd Bergmann 
---
 arch/arm/mach-pxa/tosa.c|  23 +
 drivers/power/supply/tosa_battery.c | 147 
 2 files changed, 109 insertions(+), 61 deletions(-)

diff --git a/arch/arm/mach-pxa/tosa.c b/arch/arm/mach-pxa/tosa.c
index 8329a2969b2b..ef3bbf6d158e 100644
--- a/arch/arm/mach-pxa/tosa.c
+++ b/arch/arm/mach-pxa/tosa.c
@@ -365,6 +365,28 @@ static struct pxaficp_platform_data 
tosa_ficp_platform_data = {
.shutdown   = tosa_irda_shutdown,
 };
 
+static struct gpiod_lookup_table tosa_battery_gpio_table = {
+   .dev_id = "wm97xx-battery",
+   .table = {
+   GPIO_LOOKUP("tc6393xb", TOSA_GPIO_CHARGE_OFF - 
TOSA_TC6393XB_GPIO_BASE,"main charge off", GPIO_ACTIVE_HIGH ),
+   GPIO_LOOKUP("tc6393xb", TOSA_GPIO_CHARGE_OFF_JC - 
TOSA_TC6393XB_GPIO_BASE, "jacket charge off", GPIO_ACTIVE_HIGH ),
+   GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BAT_SW_ON - 
TOSA_TC6393XB_GPIO_BASE, "battery switch", GPIO_ACTIVE_HIGH ),
+   GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BAT0_V_ON - 
TOSA_TC6393XB_GPIO_BASE, "main battery", GPIO_ACTIVE_HIGH ),
+   GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BAT1_V_ON - 
TOSA_TC6393XB_GPIO_BASE, "jacket battery", GPIO_ACTIVE_HIGH ),
+   GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BAT1_TH_ON - 
TOSA_TC6393XB_GPIO_BASE,"main battery temp", GPIO_ACTIVE_HIGH ),
+   GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BAT0_TH_ON - 
TOSA_TC6393XB_GPIO_BASE,"jacket battery temp", GPIO_ACTIVE_HIGH ),
+   GPIO_LOOKUP("tc6393xb", TOSA_GPIO_BU_CHRG_ON - 
TOSA_TC6393XB_GPIO_BASE,"backup battery", GPIO_ACTIVE_HIGH ),
+
+   GPIO_LOOKUP("gpio-pxa", TOSA_GPIO_BAT0_CRG,  "main battery 
full", GPIO_ACTIVE_HIGH ),
+   GPIO_LOOKUP("gpio-pxa", TOSA_GPIO_BAT1_CRG,  "jacket 
battery full", GPIO_ACTIVE_HIGH ),
+   GPIO_LOOKUP("gpio-pxa", TOSA_GPIO_BAT0_LOW,  "main battery 
low", GPIO_ACTIVE_HIGH ),
+   GPIO_LOOKUP("gpio-pxa", TOSA_GPIO_BAT1_LOW,  "jacket 
battery low", GPIO_ACTIVE_HIGH ),
+   GPIO_LOOKUP("gpio-pxa", TOSA_GPIO_JACKET_DETECT, "jacket 
detect", GPIO_ACTIVE_HIGH ),
+   { },
+   },
+};
+
+
 /*
  * Tosa AC IN
  */
@@ -946,6 +968,7 @@ static void __init tosa_init(void)
/* enable batt_fault */
PMCR = 0x01;
 
+   gpiod_add_lookup_table(_battery_gpio_table);
gpiod_add_lookup_table(_mci_gpio_table);
gpiod_add_lookup_table(_audio_gpio_table);
pxa_set_mci_info(_mci_platform_data);
diff --git a/drivers/power/supply/tosa_battery.c 
b/drivers/power/supply/tosa_battery.c
index b26b0eca33e1..d10320f348d0 100644
--- a/drivers/power/supply/tosa_battery.c
+++ b/drivers/power/supply/tosa_battery.c
@@ -15,11 +15,16 @@
 #include 
 
 #include 
-#include 
 
 static DEFINE_MUTEX(bat_lock); /* protects gpio pins */
 static struct work_struct bat_work;
 
+struct tosa_gpio {
+   const char *con;
+   enum gpiod_flags flags;
+   struct gpio_desc *desc;
+};
+
 struct tosa_bat {
int status;
struct power_supply *psy;
@@ -28,38 +33,42 @@ struct tosa_bat {
struct mutex work_lock; /* protects data */
 
bool (*is_present)(struct tosa_bat *bat);
-   int gpio_full;
-   int gpio_charge_off;
+   struct tosa_gpio gpio_full;
+   struct tosa_gpio gpio_charge_off;
 
int technology;
 
-   int gpio_bat;
+   struct tosa_gpio gpio_bat;
int adc_bat;
int adc_bat_divider;
int bat_max;
int bat_min;
 
-   int gpio_temp;
+   struct tosa_gpio gpio_temp;
int adc_temp;
int adc_temp_divider;
 };
 
 static struct tosa_bat tosa_bat_main;
 static struct tosa_bat tosa_bat_jacket;
+static struct tosa_gpio gpiod_jacket_det = { "jacket detect", GPIOD_IN };
+static struct tosa_gpio gpiod_battery_switch = { "battery switch", 
GPIOD_OUT_LOW };
+static struct tosa_gpio gpiod_main_battery_low = { "main battery low", 
GPIOD_IN };
+static struct tosa_gpio gpiod_jacket_battery_low = { "jacket battery low", 
GPIOD_IN };
 
 static unsigned long tosa_read_bat(struct tosa_bat *bat)
 {
unsigned long value = 0;
 
-   if (bat->gpio_bat < 0 || bat->adc_bat < 0)
+   if (!bat->gpio_bat.desc || bat->adc_bat < 0)
return 0;
 
mutex_lock(_lock);
-   gpio_set_value(bat->gpio_bat, 1);
+   gpiod_set_value(bat->gpio_bat.desc, 1);
msleep(5);
value = wm97xx_read_aux_adc(dev_get_drvdata(bat->psy->dev.parent),
bat->adc_bat);
-   gpio_set_value(bat->gpio_bat, 0);
+   gpiod_set_value(bat->gpio_bat.desc, 0);
mutex_unlock(_lock);
 
value = value * 100 / bat->adc_bat_divider;
@@ -71,15 +80,15 @@