Re: [PATCH v5 2/2] leds: lm3601x: Introduce the lm3601x LED driver

From: Dan Murphy
Date: Fri May 11 2018 - 07:57:07 EST


Jacek

Thanks for the review

On 05/10/2018 03:48 PM, Jacek Anaszewski wrote:
> Hi Dan,
>
> Thank you for the patch.
>
> On 05/10/2018 07:47 PM, Dan Murphy wrote:
>> Introduce the family of LED devices that can
>> drive a torch, strobe or IR LED.
>>
>> The LED driver can be configured with a strobe
>> timer to execute a strobe flash. The IR LED
>> brightness is controlled via the torch brightness
>> register.
>>
>> The data sheet for each the LM36010 and LM36011
>> LED drivers can be found here:
>> http://www.ti.com/product/LM36010
>> http://www.ti.com/product/LM36011
>>
>> Signed-off-by: Dan Murphy <dmurphy@xxxxxx>
>> ---
>>
>> v5 - Fixed magic numbers, change reg cache type, added of_put_node to release
>> the dt node ref, and I did not change the remove function to leave the LED in its
>> state on driver removal - https://patchwork.kernel.org/patch/10391741/
>>
>> v4 - Fixed Cocci issue using ARRAY_SIZE - https://patchwork.kernel.org/patch/10389259/
>> v3 - removed wildcard dt compatible, fixed copyright, fixed struct doc, removed
>> RO registers from default, added regmap volatile for FLAGS_REG, updated regmap cache type,
>> fixed unlock and extra semi colon in strobe_set, removed unnecessary out label
>> in led register and fixed checking of the ret in brightness_set - https://patchwork.kernel.org/patch/10386243/
>> v2 - Fixed kbuild issue and removed unused cdev_strobe - https://patchwork.kernel.org/patch/10384585/
>>
>>
>> Â drivers/leds/KconfigÂÂÂÂÂÂÂ |ÂÂ 9 +
>> Â drivers/leds/MakefileÂÂÂÂÂÂ |ÂÂ 1 +
>> Â drivers/leds/leds-lm3601x.c | 622 ++++++++++++++++++++++++++++++++++++
>> Â 3 files changed, 632 insertions(+)
>> Â create mode 100644 drivers/leds/leds-lm3601x.c
>>
>> diff --git a/drivers/leds/Kconfig b/drivers/leds/Kconfig
>> index 2c896c0e69e1..50ae536f343f 100644
>> --- a/drivers/leds/Kconfig
>> +++ b/drivers/leds/Kconfig
>> @@ -145,6 +145,15 @@ config LEDS_LM3692X
>> ÂÂÂÂÂÂÂ This option enables support for the TI LM3692x family
>> ÂÂÂÂÂÂÂ of white LED string drivers used for backlighting.
>> Â +config LEDS_LM3601X
>> +ÂÂÂ tristate "LED support for LM3601x Chips"
>> +ÂÂÂ depends on LEDS_CLASS && I2C && OF
>> +ÂÂÂ depends on LEDS_CLASS_FLASH
>> +ÂÂÂ select REGMAP_I2C
>> +ÂÂÂ help
>> +ÂÂÂÂÂ This option enables support for the TI LM3601x family
>> +ÂÂÂÂÂ of flash, torch and indicator classes.
>> +
>> Â config LEDS_LOCOMO
>> ÂÂÂÂÂ tristate "LED Support for Locomo device"
>> ÂÂÂÂÂ depends on LEDS_CLASS
>> diff --git a/drivers/leds/Makefile b/drivers/leds/Makefile
>> index 91eca81cae82..b79807fe1b67 100644
>> --- a/drivers/leds/Makefile
>> +++ b/drivers/leds/Makefile
>> @@ -76,6 +76,7 @@ obj-$(CONFIG_LEDS_MLXREG)ÂÂÂÂÂÂÂ += leds-mlxreg.o
>> Â obj-$(CONFIG_LEDS_NIC78BX)ÂÂÂÂÂÂÂ += leds-nic78bx.o
>> Â obj-$(CONFIG_LEDS_MT6323)ÂÂÂÂÂÂÂ += leds-mt6323.o
>> Â obj-$(CONFIG_LEDS_LM3692X)ÂÂÂÂÂÂÂ += leds-lm3692x.o
>> +obj-$(CONFIG_LEDS_LM3601X)ÂÂÂÂÂÂÂ += leds-lm3601x.o
>> Â Â # LED SPI Drivers
>> Â obj-$(CONFIG_LEDS_DAC124S085)ÂÂÂÂÂÂÂ += leds-dac124s085.o
>> diff --git a/drivers/leds/leds-lm3601x.c b/drivers/leds/leds-lm3601x.c
>> new file mode 100644
>> index 000000000000..3c00886fbc7a
>> --- /dev/null
>> +++ b/drivers/leds/leds-lm3601x.c
>> @@ -0,0 +1,622 @@
>> +// SPDX-License-Identifier: GPL-2.0
>> +/*
>> + * Flash and torch driver for Texas Instruments LM3601X LED
>> + * Flash driver chip family
>> + * Copyright (C) 2018 Texas Instruments Incorporated - http://www.ti.com/
>> + */
>
> Please use uniform "//" comment style here.

Is this a LEDs only requirement because I checked a few other files and this is
how the license and header is presented.

Unless I missed some new change to source code headers. And the documentation
does not dictate any change to this.

>
>> +#include <linux/delay.h>
>> +#include <linux/i2c.h>
>> +#include <linux/leds.h>
>> +#include <linux/led-class-flash.h>
>> +#include <linux/module.h>
>> +#include <linux/regmap.h>
>> +#include <linux/slab.h>
>> +#include <uapi/linux/uleds.h>
>> +
>> +#define LM3601X_LED_TORCHÂÂÂ 0x0
>> +#define LM3601X_LED_STROBEÂÂÂ 0x1
>> +#define LM3601X_LED_IRÂÂÂÂÂÂÂ 0x2
>> +
>> +/* Registers */
>> +#define LM3601X_ENABLE_REGÂÂÂ 0x01
>> +#define LM3601X_CFG_REGÂÂÂÂÂÂÂ 0x02
>> +#define LM3601X_LED_FLASH_REGÂÂÂ 0x03
>> +#define LM3601X_LED_TORCH_REGÂÂÂ 0x04
>> +#define LM3601X_FLAGS_REGÂÂÂ 0x05
>> +#define LM3601X_DEV_ID_REGÂÂÂ 0x06
>> +
>> +#define LM3601X_SW_RESETÂÂÂ BIT(7)
>> +
>> +/* Enable Mode bits */
>> +#define LM3601X_MODE_STANDBYÂÂÂ 0x00
>> +#define LM3601X_MODE_IR_DRVÂÂÂ BIT(0)
>> +#define LM3601X_MODE_TORCHÂÂÂ BIT(1)
>> +#define LM3601X_MODE_STROBEÂÂÂ (BIT(0) | BIT(1))
>> +#define LM3601X_STRB_ENÂÂÂÂÂÂÂ BIT(2)
>> +#define LM3601X_STRB_LVL_TRIGÂÂÂ ~BIT(3)
>> +#define LM3601X_STRB_EDGE_TRIGÂÂÂ BIT(3)
>> +#define LM3601X_IVFM_ENÂÂÂÂÂÂÂ BIT(4)
>> +
>> +#define LM36010_BOOST_LIMIT_19ÂÂÂ ~BIT(5)
>> +#define LM36010_BOOST_LIMIT_28ÂÂÂ BIT(5)
>> +#define LM36010_BOOST_FREQ_2MHZÂÂÂ ~BIT(6)
>> +#define LM36010_BOOST_FREQ_4MHZÂÂÂ BIT(6)
>> +#define LM36010_BOOST_MODE_NORMÂÂÂ ~BIT(7)
>> +#define LM36010_BOOST_MODE_PASSÂÂÂ BIT(7)
>> +
>> +/* Flag Mask */
>> +#define LM3601X_FLASH_TIME_OUTÂÂÂ BIT(0)
>> +#define LM3601X_UVLO_FAULTÂÂÂ BIT(1)
>> +#define LM3601X_THERM_SHUTDOWNÂÂÂ BIT(2)
>> +#define LM3601X_THERM_CURRÂÂÂ BIT(3)
>> +#define LM36010_CURR_LIMITÂÂÂ BIT(4)
>> +#define LM3601X_SHORT_FAULTÂÂÂ BIT(5)
>> +#define LM3601X_IVFM_TRIPÂÂÂ BIT(6)
>> +#define LM36010_OVP_FAULTÂÂÂ BIT(7)
>> +
>> +#define LM3601X_MIN_TORCH_I_UAÂÂÂ 2400
>> +#define LM3601X_MIN_STROBE_I_MAÂÂÂ 11
>> +
>> +#define LM3601X_TIMEOUT_MASKÂÂÂ 0x1e
>> +#define LM3601X_ENABLE_MASKÂÂÂ 0x03
>> +
>> +enum lm3601x_type {
>> +ÂÂÂ CHIP_LM36010 = 0,
>> +ÂÂÂ CHIP_LM36011,
>> +};
>> +
>> +/**
>> + * struct lm3601x_max_timeouts -
>> + * @timeout: timeout value in ms
>> + * @regval: the value of the register to write
>> + */
>> +struct lm3601x_max_timeouts {
>> +ÂÂÂ int timeout;
>> +ÂÂÂ int reg_val;
>> +};
>> +
>> +/**
>> + * struct lm3601x_led -
>> + * @lock: Lock for reading/writing the device
>> + * @regmap: Devices register map
>> + * @client: Pointer to the I2C client
>> + * @cdev_torch: led class device pointer for the torch
>> + * @cdev_ir: led class device pointer for infrared
>> + * @fled_cdev: flash led class device pointer
>> + * @strobe_node: DT device node for the strobe
>> + * @torch: LED label for the torch
>> + * @strobe: LED label for the strobe
>> + * @ir: LED label for the infrared
>> + * @last_flag: last known flags register value
>> + * @strobe_timeout: the timeout for the strobe
>> + * @torch_current_max: maximum current for the torch
>> + * @strobe_current_max: maximum current for the strobe
>> + * @max_strobe_timeout: maximum timeout for the strobe
>> + */
>> +struct lm3601x_led {
>> +ÂÂÂ struct mutex lock;
>> +ÂÂÂ struct regmap *regmap;
>> +ÂÂÂ struct i2c_client *client;
>> +
>> +ÂÂÂ struct led_classdev cdev_torch;
>> +ÂÂÂ struct led_classdev cdev_ir;
>> +
>> +ÂÂÂ struct led_classdev_flash fled_cdev;
>> +
>> +ÂÂÂ struct device_node *strobe_node;
>> +ÂÂÂ struct device_node *torch_node;
>> +ÂÂÂ struct device_node *ir_node;
>> +
>> +ÂÂÂ char torch[LED_MAX_NAME_SIZE];
>> +ÂÂÂ char strobe[LED_MAX_NAME_SIZE];
>> +ÂÂÂ char ir[LED_MAX_NAME_SIZE];
>> +
>> +ÂÂÂ unsigned int last_flag;
>> +ÂÂÂ unsigned int strobe_timeout;
>> +
>> +ÂÂÂ u32 torch_current_max;
>> +ÂÂÂ u32 strobe_current_max;
>> +ÂÂÂ u32 max_strobe_timeout;
>> +};
>> +
>> +static const struct lm3601x_max_timeouts strobe_timeouts[] = {
>> +ÂÂÂ { 40, 0x00 },
>> +ÂÂÂ { 80, 0x01 },
>> +ÂÂÂ { 120, 0x02 },
>> +ÂÂÂ { 160, 0x03 },
>> +ÂÂÂ { 200, 0x04 },
>> +ÂÂÂ { 240, 0x05 },
>> +ÂÂÂ { 280, 0x06 },
>> +ÂÂÂ { 320, 0x07 },
>> +ÂÂÂ { 360, 0x08 },
>> +ÂÂÂ { 400, 0x09 },
>> +ÂÂÂ { 600, 0x0a },
>> +ÂÂÂ { 800, 0x0b },
>> +ÂÂÂ { 1000, 0x0c },
>> +ÂÂÂ { 1200, 0x0d },
>> +ÂÂÂ { 1400, 0x0e },
>> +ÂÂÂ { 1600, 0x0f },
>> +};
>> +
>> +static const struct reg_default lm3601x_regmap_defs[] = {
>> +ÂÂÂ { LM3601X_ENABLE_REG, 0x20 },
>> +ÂÂÂ { LM3601X_CFG_REG, 0x15 },
>> +ÂÂÂ { LM3601X_LED_FLASH_REG, 0x00 },
>> +ÂÂÂ { LM3601X_LED_TORCH_REG, 0x00 },
>> +};
>> +
>> +static bool lm3601x_volatile_reg(struct device *dev, unsigned int reg)
>> +{
>> +ÂÂÂ switch (reg) {
>> +ÂÂÂ case LM3601X_FLAGS_REG:
>> +ÂÂÂÂÂÂÂ return true;
>> +ÂÂÂ default:
>> +ÂÂÂÂÂÂÂ return false;
>> +ÂÂÂ }
>> +}
>> +
>> +static const struct regmap_config lm3601x_regmap = {
>> +ÂÂÂ .reg_bits = 8,
>> +ÂÂÂ .val_bits = 8,
>> +
>> +ÂÂÂ .max_register = LM3601X_DEV_ID_REG,
>> +ÂÂÂ .reg_defaults = lm3601x_regmap_defs,
>> +ÂÂÂ .num_reg_defaults = ARRAY_SIZE(lm3601x_regmap_defs),
>> +ÂÂÂ .cache_type = REGCACHE_RBTREE,
>> +ÂÂÂ .volatile_reg = lm3601x_volatile_reg,
>> +};
>> +
>> +static struct lm3601x_led *fled_cdev_to_led(
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct led_classdev_flash *fled_cdev)
>> +{
>> +ÂÂÂ return container_of(fled_cdev, struct lm3601x_led, fled_cdev);
>> +}
>> +
>> +static int lm3601x_read_faults(struct lm3601x_led *led)
>> +{
>> +ÂÂÂ int flags_val;
>> +ÂÂÂ int ret;
>> +
>> +ÂÂÂ ret = regmap_read(led->regmap, LM3601X_FLAGS_REG, &flags_val);
>> +ÂÂÂ if (ret < 0)
>> +ÂÂÂÂÂÂÂ return -EIO;
>> +
>> +ÂÂÂ led->last_flag = flags_val;
>> +
>> +ÂÂÂ return led->last_flag;
>> +}
>> +
>> +static int lm3601x_torch_brightness_set(struct led_classdev *cdev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ enum led_brightness brightness)
>> +{
>> +ÂÂÂ struct lm3601x_led *led =
>> +ÂÂÂÂÂÂÂ container_of(cdev, struct lm3601x_led, cdev_torch);
>> +ÂÂÂ int ret;
>> +ÂÂÂ u8 brightness_val;
>> +
>> +ÂÂÂ mutex_lock(&led->lock);
>> +
>> +ÂÂÂ ret = lm3601x_read_faults(led);
>> +ÂÂÂ if (ret < 0)
>> +ÂÂÂÂÂÂÂ goto out;
>> +
>> +ÂÂÂ if (brightness == LED_OFF) {
>> +ÂÂÂÂÂÂÂ ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_MODE_TORCH, LED_OFF);
>> +ÂÂÂÂÂÂÂ goto out;
>> +ÂÂÂ }
>> +
>> +ÂÂÂ if (brightness == LED_ON)
>> +ÂÂÂÂÂÂÂ brightness_val = LED_ON;
>> +ÂÂÂ else
>> +ÂÂÂÂÂÂÂ brightness_val = (brightness/2);
>> +
>> +ÂÂÂ ret = regmap_write(led->regmap, LM3601X_LED_TORCH_REG, brightness_val);
>> +ÂÂÂ if (ret < 0)
>> +ÂÂÂÂÂÂÂ goto out;
>> +
>> +ÂÂÂ ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_MODE_TORCH,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_MODE_TORCH);
>> +
>> +out:
>> +ÂÂÂ mutex_unlock(&led->lock);
>> +ÂÂÂ return ret;
>> +}
>> +
>> +static int lm3601x_strobe_set(struct led_classdev_flash *fled_cdev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ bool state)
>> +{
>> +ÂÂÂ struct lm3601x_led *led = fled_cdev_to_led(fled_cdev);
>> +ÂÂÂ int ret;
>> +ÂÂÂ int current_timeout;
>> +ÂÂÂ int reg_count;
>> +ÂÂÂ int i;
>> +ÂÂÂ int timeout_reg_val = 0;
>> +
>> +ÂÂÂ mutex_lock(&led->lock);
>> +
>> +ÂÂÂ ret = regmap_read(led->regmap, LM3601X_CFG_REG, &current_timeout);
>> +ÂÂÂ if (ret < 0)
>> +ÂÂÂÂÂÂÂ goto out;
>> +
>> +ÂÂÂ reg_count = ARRAY_SIZE(strobe_timeouts);
>> +ÂÂÂ for (i = 0; i < reg_count; i++) {
>> +ÂÂÂÂÂÂÂ if (led->strobe_timeout > strobe_timeouts[i].timeout)
>> +ÂÂÂÂÂÂÂÂÂÂÂ continue;
>> +
>> +ÂÂÂÂÂÂÂ if (led->strobe_timeout <= strobe_timeouts[i].timeout) {
>> +ÂÂÂÂÂÂÂÂÂÂÂ timeout_reg_val = (strobe_timeouts[i].reg_val << 1);
>> +ÂÂÂÂÂÂÂÂÂÂÂ break;
>> +ÂÂÂÂÂÂÂ }
>> +
>> +ÂÂÂÂÂÂÂ ret = -EINVAL;
>> +ÂÂÂÂÂÂÂ goto out;
>> +ÂÂÂ }
>> +
>> +ÂÂÂ if (led->strobe_timeout != current_timeout)
>> +ÂÂÂÂÂÂÂ ret = regmap_update_bits(led->regmap, LM3601X_CFG_REG,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_TIMEOUT_MASK, timeout_reg_val);
>> +
>> +ÂÂÂ if (state)
>> +ÂÂÂÂÂÂÂ ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_MODE_STROBE,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_MODE_STROBE);
>> +ÂÂÂ else
>> +ÂÂÂÂÂÂÂ ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_MODE_STROBE, LED_OFF);
>> +
>> +ÂÂÂ ret = lm3601x_read_faults(led);
>> +out:
>> +ÂÂÂ mutex_unlock(&led->lock);
>> +ÂÂÂ return ret;
>> +}
>> +
>> +static int lm3601x_strobe_brightness_set(struct led_classdev *cdev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ enum led_brightness brightness)
>> +{
>> +ÂÂÂ struct led_classdev_flash *fled_cdev = lcdev_to_flcdev(cdev);
>> +ÂÂÂ struct lm3601x_led *led = fled_cdev_to_led(fled_cdev);
>> +ÂÂÂ int ret;
>> +ÂÂÂ u8 brightness_val;
>> +
>> +ÂÂÂ mutex_lock(&led->lock);
>> +ÂÂÂ ret = lm3601x_read_faults(led);
>> +ÂÂÂ if (ret < 0)
>> +ÂÂÂÂÂÂÂ goto out;
>> +
>> +ÂÂÂ if (brightness == LED_OFF) {
>> +ÂÂÂÂÂÂÂ ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_MODE_STROBE, LED_OFF);
>> +ÂÂÂÂÂÂÂ goto out;
>> +ÂÂÂ }
>> +
>> +ÂÂÂ if (brightness == LED_ON)
>> +ÂÂÂÂÂÂÂ brightness_val = LED_ON;
>> +ÂÂÂ else
>> +ÂÂÂÂÂÂÂ brightness_val = (brightness/2);
>> +
>> +ÂÂÂ ret = regmap_write(led->regmap, LM3601X_LED_FLASH_REG, brightness_val);
>> +
>> +out:
>> +ÂÂÂ mutex_unlock(&led->lock);
>> +ÂÂÂ return ret;
>> +}
>> +
>> +static int lm3601x_strobe_timeout_set(struct led_classdev_flash *fled_cdev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ u32 timeout)
>> +{
>> +ÂÂÂ struct lm3601x_led *led = fled_cdev_to_led(fled_cdev);
>> +ÂÂÂ int ret = 0;
>> +
>> +ÂÂÂ mutex_lock(&led->lock);
>> +
>> +ÂÂÂ if (timeout > led->max_strobe_timeout)
>> +ÂÂÂÂÂÂÂ ret = -EINVAL;
>
> LED flash core takes care of clamping timeout setting
> according to the defined constraints.

OK

>
>> +ÂÂÂ else
>> +ÂÂÂÂÂÂÂ led->strobe_timeout = timeout;
>> +
>> +ÂÂÂ mutex_unlock(&led->lock);
>> +
>> +ÂÂÂ return ret;
>> +}
>> +
>> +static int lm3601x_strobe_get(struct led_classdev_flash *fled_cdev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ bool *state)
>> +{
>> +ÂÂÂ struct lm3601x_led *led = fled_cdev_to_led(fled_cdev);
>> +ÂÂÂ int ret;
>> +ÂÂÂ int strobe_state;
>> +
>> +ÂÂÂ mutex_lock(&led->lock);
>> +
>> +ÂÂÂ ret = regmap_read(led->regmap, LM3601X_ENABLE_REG, &strobe_state);
>> +ÂÂÂ if (ret < 0)
>> +ÂÂÂÂÂÂÂ goto out;
>> +
>> +ÂÂÂ *state = strobe_state & LM3601X_MODE_STROBE;
>> +
>> +out:
>> +ÂÂÂ mutex_unlock(&led->lock);
>> +ÂÂÂ return ret;
>> +}
>> +
>> +static int lm3601x_ir_brightness_set(struct led_classdev *cdev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ enum led_brightness brightness)
>> +{
>> +ÂÂÂ struct lm3601x_led *led =
>> +ÂÂÂÂÂÂÂ container_of(cdev, struct lm3601x_led, cdev_ir);
>> +ÂÂÂ int ret;
>> +ÂÂÂ u8 brightness_val;
>> +
>> +ÂÂÂ mutex_lock(&led->lock);
>> +ÂÂÂ ret = lm3601x_read_faults(led);
>> +ÂÂÂ if (ret < 0)
>> +ÂÂÂÂÂÂÂ goto out;
>> +
>> +ÂÂÂ if (brightness == LED_OFF) {
>> +ÂÂÂÂÂÂÂ ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_LED_IR,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LED_OFF);
>> +ÂÂÂÂÂÂÂ goto out;
>> +ÂÂÂ }
>> +
>> +ÂÂÂ if (brightness == LED_ON)
>> +ÂÂÂÂÂÂÂ brightness_val = LED_ON;
>> +ÂÂÂ else
>> +ÂÂÂÂÂÂÂ brightness_val = (brightness/2);
>> +
>> +ÂÂÂ ret = regmap_write(led->regmap, LM3601X_LED_FLASH_REG, brightness_val);
>> +ÂÂÂ if (ret < 0)
>> +ÂÂÂÂÂÂÂ goto out;
>> +
>> +ÂÂÂ ret = regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_MODE_IR_DRV,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_MODE_IR_DRV);
>> +
>> +out:
>> +ÂÂÂ mutex_unlock(&led->lock);
>> +ÂÂÂ return ret;
>> +}
>> +
>> +static int lm3601x_strobe_fault_get(struct led_classdev_flash *fled_cdev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ u32 *fault)
>
> Does the device report only strobe related faults?

No. It has under/over voltage and thermal.

>
>> +{
>> +ÂÂÂ struct lm3601x_led *led = fled_cdev_to_led(fled_cdev);
>> +
>> +ÂÂÂ lm3601x_read_faults(led);
>> +
>> +ÂÂÂ *fault = led->last_flag;
>
> You should translate device specific faults to LED_FAULT* flags.
> Please compare e.g. drivers/leds/leds-max77693.c or
> driver/leds/leds-as3645a.c.

Hmm. This code is in the max77693.c. I think the translation should be in the
read_faults function in this file and not here.

I will add that translation above

>
>> +
>> +ÂÂÂ return 0;
>> +}
>> +
>> +static const struct led_flash_ops strobe_ops = {
>> +ÂÂÂ .strobe_setÂÂÂÂÂÂÂ = lm3601x_strobe_set,
>> +ÂÂÂ .strobe_getÂÂÂÂÂÂÂ = lm3601x_strobe_get,
>> +ÂÂÂ .timeout_setÂÂÂÂÂÂÂ = lm3601x_strobe_timeout_set,
>> +ÂÂÂ .fault_getÂÂÂÂÂÂÂ = lm3601x_strobe_fault_get,
>> +};
>> +
>> +static int lm3601x_register_leds(struct lm3601x_led *led)
>> +{
>> +ÂÂÂ struct led_classdev_flash *fled_cdev;
>> +ÂÂÂ struct led_classdev *led_cdev;
>> +ÂÂÂ int err = -ENODEV;
>> +
>> +ÂÂÂ if (led->torch_node) {
>> +ÂÂÂÂÂÂÂ led->cdev_torch.name = led->torch;
>> +ÂÂÂÂÂÂÂ led->cdev_torch.max_brightness = LED_FULL;
>> +ÂÂÂÂÂÂÂ led->cdev_torch.brightness_set_blocking = lm3601x_torch_brightness_set;
>> +ÂÂÂÂÂÂÂ err = devm_led_classdev_register(&led->client->dev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &led->cdev_torch);
>> +ÂÂÂÂÂÂÂ if (err < 0)
>> +ÂÂÂÂÂÂÂÂÂÂÂ return err;
>> +ÂÂÂ }
>> +
>> +ÂÂÂ if (led->strobe_node) {
>> +ÂÂÂÂÂÂÂ fled_cdev = &led->fled_cdev;
>> +ÂÂÂÂÂÂÂ fled_cdev->ops = &strobe_ops;
>> +
>> +ÂÂÂÂÂÂÂ led_cdev = &fled_cdev->led_cdev;
>> +ÂÂÂÂÂÂÂ led_cdev->name = led->strobe;
>> +ÂÂÂÂÂÂÂ led_cdev->max_brightness = LED_FULL;
>> +ÂÂÂÂÂÂÂ led_cdev->brightness_set_blocking = lm3601x_strobe_brightness_set;
>> +ÂÂÂÂÂÂÂ led_cdev->flags |= LED_DEV_CAP_FLASH;
>> +
>> +ÂÂÂÂÂÂÂ err = led_classdev_flash_register(&led->client->dev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ fled_cdev);
>> +ÂÂÂÂÂÂÂ if (err < 0)
>> +ÂÂÂÂÂÂÂÂÂÂÂ return err;
>> +ÂÂÂ }
>> +
>> +ÂÂÂ if (led->ir_node) {
>> +ÂÂÂÂÂÂÂ led->cdev_ir.name = led->ir;
>> +ÂÂÂÂÂÂÂ led->cdev_ir.max_brightness = LED_FULL;
>> +ÂÂÂÂÂÂÂ led->cdev_ir.brightness_set_blocking =
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ lm3601x_ir_brightness_set;
>> +
>> +ÂÂÂÂÂÂÂ err = devm_led_classdev_register(&led->client->dev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &led->cdev_ir);
>> +ÂÂÂ }
>> +
>> +ÂÂÂ return err;
>> +}
>> +
>> +static void lm3601x_init_flash_timeout(struct lm3601x_led *led)
>> +{
>> +ÂÂÂ struct led_flash_setting *setting;
>> +
>> +ÂÂÂ setting = &led->fled_cdev.timeout;
>> +ÂÂÂ setting->min = strobe_timeouts[0].timeout;
>> +ÂÂÂ setting->max = led->max_strobe_timeout;
>> +ÂÂÂ setting->step = 40;
>> +ÂÂÂ setting->val = led->max_strobe_timeout;
>> +}
>> +
>> +static int lm3601x_parse_node(struct lm3601x_led *led,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ struct device_node *node)
>> +{
>> +ÂÂÂ struct device_node *child_node;
>> +ÂÂÂ const char *name;
>> +ÂÂÂ int ret = -ENODEV;
>> +
>> +ÂÂÂ for_each_available_child_of_node(node, child_node) {
>> +ÂÂÂÂÂÂÂ u32 id = 0;
>> +
>> +ÂÂÂÂÂÂÂ of_property_read_u32(child_node, "reg", &id);
>> +
>> +ÂÂÂÂÂÂÂ switch (id) {
>> +ÂÂÂÂÂÂÂ case LM3601X_LED_TORCH:
>> +ÂÂÂÂÂÂÂÂÂÂÂ led->torch_node = of_node_get(child_node);
>> +ÂÂÂÂÂÂÂÂÂÂÂ break;
>> +ÂÂÂÂÂÂÂ case LM3601X_LED_STROBE:
>> +ÂÂÂÂÂÂÂÂÂÂÂ led->strobe_node = of_node_get(child_node);
>> +ÂÂÂÂÂÂÂÂÂÂÂ break;
>> +ÂÂÂÂÂÂÂ case LM3601X_LED_IR:
>> +ÂÂÂÂÂÂÂÂÂÂÂ led->ir_node = of_node_get(child_node);
>> +ÂÂÂÂÂÂÂÂÂÂÂ break;
>> +ÂÂÂÂÂÂÂ default:
>> +ÂÂÂÂÂÂÂÂÂÂÂ dev_warn(&led->client->dev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "unknown LED %u encountered, ignoring\n", id);
>> +ÂÂÂÂÂÂÂÂÂÂÂ break;
>> +ÂÂÂÂÂÂÂ }
>> +ÂÂÂ }
>> +
>> +ÂÂÂ if (led->torch_node) {
>> +ÂÂÂÂÂÂÂ ret = of_property_read_string(led->torch_node, "label", &name);
>> +ÂÂÂÂÂÂÂ if (!ret)
>> +ÂÂÂÂÂÂÂÂÂÂÂ snprintf(led->torch, sizeof(led->torch),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "%s:%s", led->torch_node->name, name);
>> +ÂÂÂÂÂÂÂ else
>> +ÂÂÂÂÂÂÂÂÂÂÂ snprintf(led->torch, sizeof(led->torch),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "%s:torch", led->torch_node->name);
>> +ÂÂÂÂÂÂÂ ret = of_property_read_u32(led->torch_node, "led-max-microamp",
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &led->torch_current_max);
>> +ÂÂÂÂÂÂÂ if (ret < 0) {
>> +ÂÂÂÂÂÂÂÂÂÂÂ led->torch_current_max = LM3601X_MIN_TORCH_I_UA;
>> +ÂÂÂÂÂÂÂÂÂÂÂ dev_warn(&led->client->dev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "led-max-microamp DT property missing\n");
>> +ÂÂÂÂÂÂÂ }
>> +
>> +ÂÂÂÂÂÂÂ of_node_put(led->torch_node);
>> +ÂÂÂ }
>> +
>> +ÂÂÂ if (led->strobe_node) {
>> +ÂÂÂÂÂÂÂ ret = of_property_read_string(led->strobe_node, "label", &name);
>> +ÂÂÂÂÂÂÂ if (!ret)
>> +ÂÂÂÂÂÂÂÂÂÂÂ snprintf(led->strobe, sizeof(led->strobe),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "%s:%s", led->strobe_node->name, name);
>> +ÂÂÂÂÂÂÂ else
>> +ÂÂÂÂÂÂÂÂÂÂÂ snprintf(led->strobe, sizeof(led->strobe),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "%s::strobe", led->strobe_node->name);
>> +
>> +ÂÂÂÂÂÂÂ ret = of_property_read_u32(led->strobe_node,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "flash-max-microamp",
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &led->strobe_current_max);
>> +ÂÂÂÂÂÂÂ if (ret < 0) {
>> +ÂÂÂÂÂÂÂÂÂÂÂ led->strobe_current_max = LM3601X_MIN_STROBE_I_MA;
>> +ÂÂÂÂÂÂÂÂÂÂÂ dev_warn(&led->client->dev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "flash-max-microamp DT property missing\n");
>> +ÂÂÂÂÂÂÂ }
>> +
>> +ÂÂÂÂÂÂÂ ret = of_property_read_u32(led->strobe_node,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "flash-max-timeout-us",
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ &led->max_strobe_timeout);
>> +ÂÂÂÂÂÂÂ if (ret < 0) {
>> +ÂÂÂÂÂÂÂÂÂÂÂ led->max_strobe_timeout = strobe_timeouts[0].reg_val;
>> +ÂÂÂÂÂÂÂÂÂÂÂ dev_warn(&led->client->dev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "flash-max-timeout-us DT property missing\n");
>> +ÂÂÂÂÂÂÂ }
>
> Common LED bindings state that flash-max-microamp and
> flash-max-timeout-us properties are mandatory.

OK.

>
>> +
>> +ÂÂÂÂÂÂÂ lm3601x_init_flash_timeout(led);
>> +
>> +ÂÂÂÂÂÂÂ of_node_put(led->strobe_node);
>> +ÂÂÂ }
>> +
>> +ÂÂÂ if (led->ir_node) {
>> +ÂÂÂÂÂÂÂ ret = of_property_read_string(led->ir_node, "label", &name);
>> +ÂÂÂÂÂÂÂ if (!ret)
>> +ÂÂÂÂÂÂÂÂÂÂÂ snprintf(led->ir, sizeof(led->ir),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "%s:%s", led->ir_node->name, name);
>> +ÂÂÂÂÂÂÂ else
>> +ÂÂÂÂÂÂÂÂÂÂÂ snprintf(led->ir, sizeof(led->ir),
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ "%s::infrared", led->ir_node->name);
>> +
>> +ÂÂÂÂÂÂÂ of_node_put(led->ir_node);
>> +ÂÂÂ }
>> +
>> +ÂÂÂ return ret;
>> +}
>> +
>> +static int lm3601x_probe(struct i2c_client *client,
>> +ÂÂÂÂÂÂÂÂÂÂÂ const struct i2c_device_id *id)
>> +{
>> +ÂÂÂ struct lm3601x_led *led;
>> +ÂÂÂ int err;
>> +
>> +ÂÂÂ led = devm_kzalloc(&client->dev,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ sizeof(struct lm3601x_led), GFP_KERNEL);
>> +ÂÂÂ if (!led)
>> +ÂÂÂÂÂÂÂ return -ENOMEM;
>> +
>> +ÂÂÂ err = lm3601x_parse_node(led, client->dev.of_node);
>> +ÂÂÂ if (err < 0)
>> +ÂÂÂÂÂÂÂ return -ENODEV;
>> +
>> +ÂÂÂ led->client = client;
>> +ÂÂÂ led->regmap = devm_regmap_init_i2c(client, &lm3601x_regmap);
>> +ÂÂÂ if (IS_ERR(led->regmap)) {
>> +ÂÂÂÂÂÂÂ err = PTR_ERR(led->regmap);
>> +ÂÂÂÂÂÂÂ dev_err(&client->dev,
>> +ÂÂÂÂÂÂÂÂÂÂÂ "Failed to allocate register map: %d\n", err);
>> +ÂÂÂÂÂÂÂ return err;
>> +ÂÂÂ }
>> +
>> +ÂÂÂ mutex_init(&led->lock);
>> +ÂÂÂ i2c_set_clientdata(client, led);
>> +ÂÂÂ err = lm3601x_register_leds(led);
>> +
>> +ÂÂÂ return err;
>> +}
>> +
>> +static int lm3601x_remove(struct i2c_client *client)
>> +{
>> +ÂÂÂ struct lm3601x_led *led = i2c_get_clientdata(client);
>> +
>> +ÂÂÂ regmap_update_bits(led->regmap, LM3601X_ENABLE_REG,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_ENABLE_MASK,
>> +ÂÂÂÂÂÂÂÂÂÂÂÂÂÂ LM3601X_MODE_STANDBY);
>> +
>> +ÂÂÂ return 0;
>> +}
>> +
>> +static const struct i2c_device_id lm3601x_id[] = {
>> +ÂÂÂ { "LM36010", CHIP_LM36010 },
>> +ÂÂÂ { "LM36011", CHIP_LM36011 },
>> +ÂÂÂ { },
>> +};
>> +MODULE_DEVICE_TABLE(i2c, lm3601x_id);
>> +
>> +static const struct of_device_id of_lm3601x_leds_match[] = {
>> +ÂÂÂ { .compatible = "ti,lm36010", },
>> +ÂÂÂ { .compatible = "ti,lm36011", },
>> +ÂÂÂ {},
>> +};
>> +MODULE_DEVICE_TABLE(of, of_lm3601x_leds_match);
>> +
>> +static struct i2c_driver lm3601x_i2c_driver = {
>> +ÂÂÂ .driver = {
>> +ÂÂÂÂÂÂÂ .name = "lm3601x",
>> +ÂÂÂÂÂÂÂ .of_match_table = of_lm3601x_leds_match,
>> +ÂÂÂ },
>> +ÂÂÂ .probe = lm3601x_probe,
>> +ÂÂÂ .remove = lm3601x_remove,
>> +ÂÂÂ .id_table = lm3601x_id,
>> +};
>> +module_i2c_driver(lm3601x_i2c_driver);
>> +
>> +MODULE_DESCRIPTION("Texas Instruments Flash Lighting driver for LM3601X");
>> +MODULE_AUTHOR("Dan Murphy <dmurphy@xxxxxx>");
>> +MODULE_LICENSE("GPL v2");
>>
>


--
------------------
Dan Murphy