Re: [PATCH V2 3/5] backlight: qcom-wled: Add support for short circuit handling

2018-05-08 Thread kgunda

On 2018-05-08 16:05, Daniel Thompson wrote:

On Thu, May 03, 2018 at 03:42:30PM +0530, Kiran Gunda wrote:

Handle the short circuit interrupt and check if the short circuit
interrupt is valid. Re-enable the module to check if it goes
away. Disable the module altogether if the short circuit event
persists.

Signed-off-by: Kiran Gunda 
---
 drivers/video/backlight/qcom-wled.c | 134 
++--

 1 file changed, 130 insertions(+), 4 deletions(-)

diff --git a/drivers/video/backlight/qcom-wled.c 
b/drivers/video/backlight/qcom-wled.c

index be8b8d3..2cfba77 100644
--- a/drivers/video/backlight/qcom-wled.c
+++ b/drivers/video/backlight/qcom-wled.c
@@ -10,6 +10,9 @@
  * GNU General Public License for more details.
  */

+#include 
+#include 
+#include 
 #include 
 #include 
 #include 
@@ -23,7 +26,9 @@

 #define WLED3_SINK_REG_BRIGHT_MAX  0xFFF

-/* Control registers */
+/* WLED3 Control registers */
+#define WLED3_CTRL_REG_FAULT_STATUS0x08
+
 #define WLED3_CTRL_REG_MOD_EN  0x46
 #define  WLED3_CTRL_REG_MOD_EN_MASKBIT(7)

@@ -36,7 +41,17 @@
 #define WLED3_CTRL_REG_ILIMIT  0x4e
 #define  WLED3_CTRL_REG_ILIMIT_MASKGENMASK(2, 0)

-/* sink registers */
+/* WLED4 control registers */
+#define WLED4_CTRL_REG_SHORT_PROTECT   0x5e
+#define  WLED4_CTRL_REG_SHORT_EN_MASK  BIT(7)
+
+#define WLED4_CTRL_REG_SEC_ACCESS  0xd0
+#define  WLED4_CTRL_REG_SEC_UNLOCK 0xa5
+
+#define WLED4_CTRL_REG_TEST1   0xe2
+#define  WLED4_CTRL_REG_TEST1_EXT_FET_DTEST2   0x09
+
+/* WLED3 sink registers */
 #define WLED3_SINK_REG_SYNC0x47
 #define  WLED3_SINK_REG_SYNC_MASK  GENMASK(2, 0)
 #define  WLED4_SINK_REG_SYNC_MASK  GENMASK(3, 0)
@@ -130,17 +145,23 @@ struct wled_config {
bool cs_out_en;
bool ext_gen;
bool cabc;
+   bool external_pfet;
 };

 struct wled {
const char *name;
struct device *dev;
struct regmap *regmap;
+   struct mutex lock;  /* Lock to avoid race from ISR */
+   ktime_t last_short_event;
u16 ctrl_addr;
u16 sink_addr;
u32 brightness;
u32 max_brightness;
+   u32 short_count;
const int *version;
+   int short_irq;
+   bool force_mod_disable;

struct wled_config cfg;
int (*wled_set_brightness)(struct wled *wled, u16 brightness);
@@ -192,6 +213,9 @@ static int wled_module_enable(struct wled *wled, 
int val)

 {
int rc;

+   if (wled->force_mod_disable)
+   return 0;
+
rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
WLED3_CTRL_REG_MOD_EN,
WLED3_CTRL_REG_MOD_EN_MASK,
@@ -248,12 +272,13 @@ static int wled_update_status(struct 
backlight_device *bl)

bl->props.state & BL_CORE_FBBLANK)
brightness = 0;

+   mutex_lock(&wled->lock);
if (brightness) {
rc = wled->wled_set_brightness(wled, brightness);
if (rc < 0) {
dev_err(wled->dev, "wled failed to set brightness 
rc:%d\n",
rc);
-   return rc;
+   goto unlock_mutex;
}

rc = wled->wled_sync_toggle(wled);
@@ -267,15 +292,60 @@ static int wled_update_status(struct 
backlight_device *bl)

rc = wled_module_enable(wled, !!brightness);
if (rc < 0) {
dev_err(wled->dev, "wled enable failed rc:%d\n", rc);
-   return rc;
+   goto unlock_mutex;
}
}

wled->brightness = brightness;

+unlock_mutex:
+   mutex_unlock(&wled->lock);
+
return rc;
 }

+#define WLED_SHORT_DLY_MS  20
+#define WLED_SHORT_CNT_MAX 5
+#define WLED_SHORT_RESET_CNT_DLY_USHZ


So we think a short report every quarter second is harmless?


Yes. We wait for the short to report 5 times with in this time. If the 
short
still persists, we go and disable the WLED module to avoid the damage. 
This is

suggested by the HW team.


+static irqreturn_t wled_short_irq_handler(int irq, void *_wled)
+{
+   struct wled *wled = _wled;
+   int rc;
+   s64 elapsed_time;
+
+   wled->short_count++;
+   mutex_lock(&wled->lock);
+   rc = wled_module_enable(wled, false);
+   if (rc < 0) {
+   dev_err(wled->dev, "wled disable failed rc:%d\n", rc);
+   goto unlock_mutex;
+   }
+
+   elapsed_time = ktime_us_delta(ktime_get(),
+ wled->last_short_event);
+   if (elapsed_time > WLED_SHORT_RESET_CNT_DLY_US)
+   wled->short_cou

Re: [PATCH V2 3/5] backlight: qcom-wled: Add support for short circuit handling

2018-05-08 Thread Daniel Thompson
On Thu, May 03, 2018 at 03:42:30PM +0530, Kiran Gunda wrote:
> Handle the short circuit interrupt and check if the short circuit
> interrupt is valid. Re-enable the module to check if it goes
> away. Disable the module altogether if the short circuit event
> persists.
> 
> Signed-off-by: Kiran Gunda 
> ---
>  drivers/video/backlight/qcom-wled.c | 134 
> ++--
>  1 file changed, 130 insertions(+), 4 deletions(-)
> 
> diff --git a/drivers/video/backlight/qcom-wled.c 
> b/drivers/video/backlight/qcom-wled.c
> index be8b8d3..2cfba77 100644
> --- a/drivers/video/backlight/qcom-wled.c
> +++ b/drivers/video/backlight/qcom-wled.c
> @@ -10,6 +10,9 @@
>   * GNU General Public License for more details.
>   */
>  
> +#include 
> +#include 
> +#include 
>  #include 
>  #include 
>  #include 
> @@ -23,7 +26,9 @@
>  
>  #define WLED3_SINK_REG_BRIGHT_MAX0xFFF
>  
> -/* Control registers */
> +/* WLED3 Control registers */
> +#define WLED3_CTRL_REG_FAULT_STATUS  0x08
> +
>  #define WLED3_CTRL_REG_MOD_EN0x46
>  #define  WLED3_CTRL_REG_MOD_EN_MASK  BIT(7)
>  
> @@ -36,7 +41,17 @@
>  #define WLED3_CTRL_REG_ILIMIT0x4e
>  #define  WLED3_CTRL_REG_ILIMIT_MASK  GENMASK(2, 0)
>  
> -/* sink registers */
> +/* WLED4 control registers */
> +#define WLED4_CTRL_REG_SHORT_PROTECT 0x5e
> +#define  WLED4_CTRL_REG_SHORT_EN_MASKBIT(7)
> +
> +#define WLED4_CTRL_REG_SEC_ACCESS0xd0
> +#define  WLED4_CTRL_REG_SEC_UNLOCK   0xa5
> +
> +#define WLED4_CTRL_REG_TEST1 0xe2
> +#define  WLED4_CTRL_REG_TEST1_EXT_FET_DTEST2 0x09
> +
> +/* WLED3 sink registers */
>  #define WLED3_SINK_REG_SYNC  0x47
>  #define  WLED3_SINK_REG_SYNC_MASKGENMASK(2, 0)
>  #define  WLED4_SINK_REG_SYNC_MASKGENMASK(3, 0)
> @@ -130,17 +145,23 @@ struct wled_config {
>   bool cs_out_en;
>   bool ext_gen;
>   bool cabc;
> + bool external_pfet;
>  };
>  
>  struct wled {
>   const char *name;
>   struct device *dev;
>   struct regmap *regmap;
> + struct mutex lock;  /* Lock to avoid race from ISR */
> + ktime_t last_short_event;
>   u16 ctrl_addr;
>   u16 sink_addr;
>   u32 brightness;
>   u32 max_brightness;
> + u32 short_count;
>   const int *version;
> + int short_irq;
> + bool force_mod_disable;
>  
>   struct wled_config cfg;
>   int (*wled_set_brightness)(struct wled *wled, u16 brightness);
> @@ -192,6 +213,9 @@ static int wled_module_enable(struct wled *wled, int val)
>  {
>   int rc;
>  
> + if (wled->force_mod_disable)
> + return 0;
> +
>   rc = regmap_update_bits(wled->regmap, wled->ctrl_addr +
>   WLED3_CTRL_REG_MOD_EN,
>   WLED3_CTRL_REG_MOD_EN_MASK,
> @@ -248,12 +272,13 @@ static int wled_update_status(struct backlight_device 
> *bl)
>   bl->props.state & BL_CORE_FBBLANK)
>   brightness = 0;
>  
> + mutex_lock(&wled->lock);
>   if (brightness) {
>   rc = wled->wled_set_brightness(wled, brightness);
>   if (rc < 0) {
>   dev_err(wled->dev, "wled failed to set brightness 
> rc:%d\n",
>   rc);
> - return rc;
> + goto unlock_mutex;
>   }
>  
>   rc = wled->wled_sync_toggle(wled);
> @@ -267,15 +292,60 @@ static int wled_update_status(struct backlight_device 
> *bl)
>   rc = wled_module_enable(wled, !!brightness);
>   if (rc < 0) {
>   dev_err(wled->dev, "wled enable failed rc:%d\n", rc);
> - return rc;
> + goto unlock_mutex;
>   }
>   }
>  
>   wled->brightness = brightness;
>  
> +unlock_mutex:
> + mutex_unlock(&wled->lock);
> +
>   return rc;
>  }
>  
> +#define WLED_SHORT_DLY_MS20
> +#define WLED_SHORT_CNT_MAX   5
> +#define WLED_SHORT_RESET_CNT_DLY_US  HZ

So we think a short report every quarter second is harmless?


> +static irqreturn_t wled_short_irq_handler(int irq, void *_wled)
> +{
> + struct wled *wled = _wled;
> + int rc;
> + s64 elapsed_time;
> +
> + wled->short_count++;
> + mutex_lock(&wled->lock);
> + rc = wled_module_enable(wled, false);
> + if (rc < 0) {
> + dev_err(wled->dev, "wled disable failed rc:%d\n", rc);
> + goto unlock_mutex;
> + }
> +
> + elapsed_time = ktime_us_delta(ktime_get(),
> +   wled->last_short_event);
> + if (elapsed_time > WLED_SHORT_RESET_CNT_DLY_US)
> + wled->short_count = 0;
> +
> + if (wled->short_count > WLED_SHORT_CNT_MAX) {
> +