Linus Torvalds cb9b4c3403 * pca955x: Add HW blink support, utilizing PWM0. It supports one frequency
across all blinking LEDs and falls back to software blink if different
     frequencies are requested.
 
   * trigger: netdev: Allow configuring LED blink interval via .blink_set
     even when HW offload (.hw_control) is enabled.
   * led-core: Fix a race condition where a quick LED_OFF followed by another
     brightness set could leave the LED off incorrectly, especially
     noticeable after the introduction of the ordered workqueue.
   * qcom-lpg: Add support for 6-bit PWM resolution alongside the existing
     9-bit support.
   * qcom-lpg: Fix PWM value capping to respect the selected resolution
     (6-bit or 9-bit) for normal PWMs.
   * qcom-lpg: Fix PWM value capping to respect the selected resolution for
     Hi-Res PWMs.
   * qcom-lpg: Fix calculation of the best period for Hi-Res PWMs to prevent
     requested duty cycles from exceeding the maximum allowed by the
     selected resolution.
   * st1202: Add a check for the error code returned by devm_mutex_init().
   * pwm-multicolor: Add a check for the return value of
     fwnode_property_read_u32().
   * st1202: Ensure hardware initialization (st1202_setup) happens before DT
     node processing (st1202_dt_init).
   * Kconfig: leds-st1202: Add select LEDS_TRIGGER_PATTERN as it's required
     by the driver.
 
   * lp8860: Drop unneeded explicit assignment to REGCACHE_NONE.
   * pca955x: Refactor code with helper functions and rename some
     functions/variables for clarity.
   * pca955x: Pass driver data pointers instead of the I2C client to helper
     functions.
   * pca955x: Optimize probe LED selection logic to reduce I2C operations.
   * pca955x: Revert the removal of pca95xx_num_led_regs() (renaming it to
     pca955x_num_led_regs) as it's needed for HW blink support.
   * st1202: Refactor st1202_led_set() to use the !! operator for boolean
     conversion.
   * st1202: Minor spacing and proofreading edits in comments.
   * Directory Rename: Rename the drivers/leds/simple directory to
     drivers/leds/simatic as the drivers within are not simple.
   * mlxcpld: Remove unused include of acpi.h.
   * nic78bx: Tidy up the ACPI ID table (remove ACPI_PTR, use
     mod_devicetable.h, remove explicit driver_data initializer).
 
   * tlc591xx: Convert text binding to YAML format, add child node
     constraints, and fix typos/formatting in the example.
   * qcom-lpg: Document the qcom,pm8937-pwm compatible string as a
     fallback for qcom,pm8916-pwm.
 -----BEGIN PGP SIGNATURE-----
 
 iQIzBAABCgAdFiEEdrbJNaO+IJqU8IdIUa+KL4f8d2EFAmfmqRcACgkQUa+KL4f8
 d2EIjg//cmG4xqmhs5u38pAVL30ZYphGL3AlSVnAtMbBgP3u6AzzppjrDHQuPbZC
 Iw8VGRbl1JTu1kazlQ4B4VR1b0KBAScz0oIrLqb/qJUWFw9DsIeKxCsTjukRr+/O
 FaQJRwhSPd4x+RW6+yGrug0ciR+f6ZfaQl7SLP7Puo2TqQukaA28aVG+KPeVxUzi
 CdVNDyMLSpDBNvWq1zRzlGEHDUprFO+lwV5kVy9V5qT7t5WkayMkOE4qsopJVQqh
 jkbxpKfxfWh7Mi3BxACqiVVTZRlzPu8hCmm+9OwT08m+coXGkSNzhXni3dSNugOL
 XMFQatfmsRgqSt68icHA993xhNytkLN8yj98mzUcpky8VfwUMNXKN2JhTJT+QG19
 +W4/Xt1WROMr/FTi4JBLzQe3dmyXPiVpONtUuO4vVtPeXyUMOUxKU9opVF6KHu/v
 +9xF4qDYVbxvV0NysB6unsqyEL+su+//wBhR+7gSpc7Gg8gquE9kFeP+1jkK5K4d
 mKhijFr1BGP1f6nJA5wivnKJ9EIie7wnJcrLFCDDYV2uACJDwmaBIx+VLb2yf/FY
 usd1bj/4mMobtSGrYnOZf4IK8erDt/+ozm0t7pqshmI/SM54xMvF3L7CttOPPh6K
 j69dTkKJ9FzfSqy2RZ9gvbga+WoCD3++lhw5ivGJjF9lRaQQo7I=
 =cEhV
 -----END PGP SIGNATURE-----

Merge tag 'leds-next-6.15' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/leds

Pull LED updates from Lee Jones:

 - pca955x: Add HW blink support, utilizing PWM0. It supports one
   frequency across all blinking LEDs and falls back to software blink
   if different frequencies are requested.

 - trigger: netdev: Allow configuring LED blink interval via .blink_set
   even when HW offload (.hw_control) is enabled.

 - led-core: Fix a race condition where a quick LED_OFF followed by
   another brightness set could leave the LED off incorrectly,
   especially noticeable after the introduction of the ordered
   workqueue.

 - qcom-lpg: Add support for 6-bit PWM resolution alongside the existing
   9-bit support.

 - qcom-lpg: Fix PWM value capping to respect the selected resolution
   (6-bit or 9-bit) for normal PWMs.

 - qcom-lpg: Fix PWM value capping to respect the selected resolution
   for Hi-Res PWMs.

 - qcom-lpg: Fix calculation of the best period for Hi-Res PWMs to
   prevent requested duty cycles from exceeding the maximum allowed by
   the selected resolution.

 - st1202: Add a check for the error code returned by devm_mutex_init().

 - pwm-multicolor: Add a check for the return value of
   fwnode_property_read_u32().

 - st1202: Ensure hardware initialization (st1202_setup) happens before
   DT node processing (st1202_dt_init).

 - Kconfig: leds-st1202: Add select LEDS_TRIGGER_PATTERN as it's
   required by the driver.

 - lp8860: Drop unneeded explicit assignment to REGCACHE_NONE.

 - pca955x: Refactor code with helper functions and rename some
   functions/variables for clarity.

 - pca955x: Pass driver data pointers instead of the I2C client to
   helper functions.

 - pca955x: Optimize probe LED selection logic to reduce I2C operations.

 - pca955x: Revert the removal of pca95xx_num_led_regs() (renaming it to
   pca955x_num_led_regs) as it's needed for HW blink support.

 - st1202: Refactor st1202_led_set() to use the !! operator for boolean
   conversion.

 - st1202: Minor spacing and proofreading edits in comments.

 - Directory Rename: Rename the drivers/leds/simple directory to
   drivers/leds/simatic as the drivers within are not simple.

 - mlxcpld: Remove unused include of acpi.h.

 - nic78bx: Tidy up the ACPI ID table (remove ACPI_PTR, use
   mod_devicetable.h, remove explicit driver_data initializer).

 - tlc591xx: Convert text binding to YAML format, add child node
   constraints, and fix typos/formatting in the example.

 - qcom-lpg: Document the qcom,pm8937-pwm compatible string as a
   fallback for qcom,pm8916-pwm.

* tag 'leds-next-6.15' of git://git.kernel.org/pub/scm/linux/kernel/git/lee/leds: (23 commits)
  leds: nic78bx: Tidy up ACPI ID table
  leds: mlxcpld: Remove unused ACPI header inclusion
  leds: rgb: leds-qcom-lpg: Fix calculation of best period Hi-Res PWMs
  leds: rgb: leds-qcom-lpg: Fix pwm resolution max for Hi-Res PWMs
  leds: rgb: leds-qcom-lpg: Fix pwm resolution max for normal PWMs
  leds: Rename simple directory to simatic
  leds: Kconfig: leds-st1202: Add select for required LEDS_TRIGGER_PATTERN
  leds: leds-st1202: Spacing and proofreading editing
  leds: leds-st1202: Initialize hardware before DT node child operations
  leds: pwm-multicolor: Add check for fwnode_property_read_u32
  leds: rgb: leds-qcom-lpg: Add support for 6-bit PWM resolution
  leds: Fix LED_OFF brightness race
  Revert "leds-pca955x: Remove the unused function pca95xx_num_led_regs()"
  leds: st1202: Refactor st1202_led_set() to use !! operator for boolean conversion
  dt-bindings: leds: qcom-lpg: Document PM8937 PWM compatible
  leds: pca955x: Add HW blink support
  leds: pca955x: Optimize probe LED selection
  leds: pca955x: Use pointers to driver data rather than I2C client
  leds: pca955x: Refactor with helper functions and renaming
  dt-bindings: leds: Convert leds-tlc591xx.txt to yaml format
  ...
2025-03-29 14:42:59 -07:00

417 lines
11 KiB
C

// SPDX-License-Identifier: GPL-2.0-only
/*
* LED driver for STMicroelectronics LED1202 chip
*
* Copyright (C) 2024 Remote-Tech Ltd. UK
*/
#include <linux/cleanup.h>
#include <linux/ctype.h>
#include <linux/delay.h>
#include <linux/err.h>
#include <linux/gpio.h>
#include <linux/i2c.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/string.h>
#define ST1202_CHAN_DISABLE_ALL 0x00
#define ST1202_CHAN_ENABLE_HIGH 0x03
#define ST1202_CHAN_ENABLE_LOW 0x02
#define ST1202_CONFIG_REG 0x04
/* PATS: Pattern sequence feature enable */
#define ST1202_CONFIG_REG_PATS BIT(7)
/* PATSR: Pattern sequence runs (self-clear when sequence is finished) */
#define ST1202_CONFIG_REG_PATSR BIT(6)
#define ST1202_CONFIG_REG_SHFT BIT(3)
#define ST1202_DEV_ENABLE 0x01
#define ST1202_DEV_ENABLE_ON BIT(0)
#define ST1202_DEV_ENABLE_RESET BIT(7)
#define ST1202_DEVICE_ID 0x00
#define ST1202_ILED_REG0 0x09
#define ST1202_MAX_LEDS 12
#define ST1202_MAX_PATTERNS 8
#define ST1202_MILLIS_PATTERN_DUR_MAX 5660
#define ST1202_MILLIS_PATTERN_DUR_MIN 22
#define ST1202_PATTERN_DUR 0x16
#define ST1202_PATTERN_PWM 0x1E
#define ST1202_PATTERN_REP 0x15
struct st1202_led {
struct fwnode_handle *fwnode;
struct led_classdev led_cdev;
struct st1202_chip *chip;
bool is_active;
int led_num;
};
struct st1202_chip {
struct i2c_client *client;
struct mutex lock;
struct st1202_led leds[ST1202_MAX_LEDS];
};
static struct st1202_led *cdev_to_st1202_led(struct led_classdev *cdev)
{
return container_of(cdev, struct st1202_led, led_cdev);
}
static int st1202_read_reg(struct st1202_chip *chip, int reg, uint8_t *val)
{
struct device *dev = &chip->client->dev;
int ret;
ret = i2c_smbus_read_byte_data(chip->client, reg);
if (ret < 0) {
dev_err(dev, "Failed to read register [0x%x]: %d\n", reg, ret);
return ret;
}
*val = (uint8_t)ret;
return 0;
}
static int st1202_write_reg(struct st1202_chip *chip, int reg, uint8_t val)
{
struct device *dev = &chip->client->dev;
int ret;
ret = i2c_smbus_write_byte_data(chip->client, reg, val);
if (ret != 0)
dev_err(dev, "Failed to write %d to register [0x%x]: %d\n", val, reg, ret);
return ret;
}
static uint8_t st1202_prescalar_to_miliseconds(unsigned int value)
{
return value / ST1202_MILLIS_PATTERN_DUR_MIN - 1;
}
static int st1202_pwm_pattern_write(struct st1202_chip *chip, int led_num,
int pattern, unsigned int value)
{
u8 value_l, value_h;
int ret;
value_l = (u8)value;
value_h = (u8)(value >> 8);
/*
* Datasheet: Register address low = 1Eh + 2*(xh) + 18h*(yh),
* where x is the channel number (led number) in hexadecimal (x = 00h .. 0Bh)
* and y is the pattern number in hexadecimal (y = 00h .. 07h)
*/
ret = st1202_write_reg(chip, (ST1202_PATTERN_PWM + (led_num * 2) + 0x18 * pattern),
value_l);
if (ret != 0)
return ret;
/*
* Datasheet: Register address high = 1Eh + 01h + 2(xh) +18h*(yh),
* where x is the channel number in hexadecimal (x = 00h .. 0Bh)
* and y is the pattern number in hexadecimal (y = 00h .. 07h)
*/
ret = st1202_write_reg(chip, (ST1202_PATTERN_PWM + 0x1 + (led_num * 2) + 0x18 * pattern),
value_h);
if (ret != 0)
return ret;
return 0;
}
static int st1202_duration_pattern_write(struct st1202_chip *chip, int pattern,
unsigned int value)
{
return st1202_write_reg(chip, (ST1202_PATTERN_DUR + pattern),
st1202_prescalar_to_miliseconds(value));
}
static void st1202_brightness_set(struct led_classdev *led_cdev,
enum led_brightness value)
{
struct st1202_led *led = cdev_to_st1202_led(led_cdev);
struct st1202_chip *chip = led->chip;
guard(mutex)(&chip->lock);
st1202_write_reg(chip, ST1202_ILED_REG0 + led->led_num, value);
}
static enum led_brightness st1202_brightness_get(struct led_classdev *led_cdev)
{
struct st1202_led *led = cdev_to_st1202_led(led_cdev);
struct st1202_chip *chip = led->chip;
u8 value = 0;
guard(mutex)(&chip->lock);
st1202_read_reg(chip, ST1202_ILED_REG0 + led->led_num, &value);
return value;
}
static int st1202_channel_set(struct st1202_chip *chip, int led_num, bool active)
{
u8 chan_low, chan_high;
int ret;
guard(mutex)(&chip->lock);
if (led_num <= 7) {
ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_LOW, &chan_low);
if (ret < 0)
return ret;
chan_low = active ? chan_low | BIT(led_num) : chan_low & ~BIT(led_num);
ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, chan_low);
if (ret < 0)
return ret;
} else {
ret = st1202_read_reg(chip, ST1202_CHAN_ENABLE_HIGH, &chan_high);
if (ret < 0)
return ret;
chan_high = active ? chan_high | (BIT(led_num) >> 8) :
chan_high & ~(BIT(led_num) >> 8);
ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, chan_high);
if (ret < 0)
return ret;
}
return 0;
}
static int st1202_led_set(struct led_classdev *ldev, enum led_brightness value)
{
struct st1202_led *led = cdev_to_st1202_led(ldev);
return st1202_channel_set(led->chip, led->led_num, !!value);
}
static int st1202_led_pattern_clear(struct led_classdev *ldev)
{
struct st1202_led *led = cdev_to_st1202_led(ldev);
struct st1202_chip *chip = led->chip;
int ret;
guard(mutex)(&chip->lock);
for (int patt = 0; patt < ST1202_MAX_PATTERNS; patt++) {
ret = st1202_pwm_pattern_write(chip, led->led_num, patt, LED_OFF);
if (ret != 0)
return ret;
ret = st1202_duration_pattern_write(chip, patt, ST1202_MILLIS_PATTERN_DUR_MIN);
if (ret != 0)
return ret;
}
return 0;
}
static int st1202_led_pattern_set(struct led_classdev *ldev,
struct led_pattern *pattern,
u32 len, int repeat)
{
struct st1202_led *led = cdev_to_st1202_led(ldev);
struct st1202_chip *chip = led->chip;
int ret;
if (len > ST1202_MAX_PATTERNS)
return -EINVAL;
guard(mutex)(&chip->lock);
for (int patt = 0; patt < len; patt++) {
if (pattern[patt].delta_t < ST1202_MILLIS_PATTERN_DUR_MIN ||
pattern[patt].delta_t > ST1202_MILLIS_PATTERN_DUR_MAX)
return -EINVAL;
ret = st1202_pwm_pattern_write(chip, led->led_num, patt, pattern[patt].brightness);
if (ret != 0)
return ret;
ret = st1202_duration_pattern_write(chip, patt, pattern[patt].delta_t);
if (ret != 0)
return ret;
}
ret = st1202_write_reg(chip, ST1202_PATTERN_REP, repeat);
if (ret != 0)
return ret;
ret = st1202_write_reg(chip, ST1202_CONFIG_REG, (ST1202_CONFIG_REG_PATSR |
ST1202_CONFIG_REG_PATS | ST1202_CONFIG_REG_SHFT));
if (ret != 0)
return ret;
return 0;
}
static int st1202_dt_init(struct st1202_chip *chip)
{
struct device *dev = &chip->client->dev;
struct st1202_led *led;
int err, reg;
for_each_available_child_of_node_scoped(dev_of_node(dev), child) {
err = of_property_read_u32(child, "reg", &reg);
if (err)
return dev_err_probe(dev, err, "Invalid register\n");
led = &chip->leds[reg];
led->is_active = true;
led->fwnode = of_fwnode_handle(child);
led->led_cdev.max_brightness = U8_MAX;
led->led_cdev.brightness_set_blocking = st1202_led_set;
led->led_cdev.pattern_set = st1202_led_pattern_set;
led->led_cdev.pattern_clear = st1202_led_pattern_clear;
led->led_cdev.default_trigger = "pattern";
led->led_cdev.brightness_set = st1202_brightness_set;
led->led_cdev.brightness_get = st1202_brightness_get;
}
return 0;
}
static int st1202_setup(struct st1202_chip *chip)
{
int ret;
guard(mutex)(&chip->lock);
/*
* Once the supply voltage is applied, the LED1202 executes some internal checks.
* Afterwards, it stops the oscillator and puts the internal LDO in quiescent mode.
* To start the device, EN bit must be set inside the “Device Enable” register at
* address 01h. As soon as EN is set, the LED1202 loads the adjustment parameters
* from the internal non-volatile memory and performs an auto-calibration procedure
* in order to increase the output current precision.
* Such initialization lasts about 6.5 ms.
*/
/* Reset the chip during setup */
ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_RESET);
if (ret < 0)
return ret;
/* Enable phase-shift delay feature */
ret = st1202_write_reg(chip, ST1202_CONFIG_REG, ST1202_CONFIG_REG_SHFT);
if (ret < 0)
return ret;
/* Enable the device */
ret = st1202_write_reg(chip, ST1202_DEV_ENABLE, ST1202_DEV_ENABLE_ON);
if (ret < 0)
return ret;
/* Duration of initialization */
usleep_range(6500, 10000);
/* Deactivate all LEDS (channels) and activate only the ones found in Device Tree */
ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_LOW, ST1202_CHAN_DISABLE_ALL);
if (ret < 0)
return ret;
ret = st1202_write_reg(chip, ST1202_CHAN_ENABLE_HIGH, ST1202_CHAN_DISABLE_ALL);
if (ret < 0)
return ret;
ret = st1202_write_reg(chip, ST1202_CONFIG_REG,
ST1202_CONFIG_REG_PATS | ST1202_CONFIG_REG_PATSR);
if (ret < 0)
return ret;
return 0;
}
static int st1202_probe(struct i2c_client *client)
{
struct st1202_chip *chip;
struct st1202_led *led;
int ret;
if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
return dev_err_probe(&client->dev, -EIO, "SMBUS Byte Data not Supported\n");
chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
if (!chip)
return -ENOMEM;
ret = devm_mutex_init(&client->dev, &chip->lock);
if (ret < 0)
return ret;
chip->client = client;
ret = st1202_setup(chip);
if (ret < 0)
return ret;
ret = st1202_dt_init(chip);
if (ret < 0)
return ret;
for (int i = 0; i < ST1202_MAX_LEDS; i++) {
struct led_init_data init_data = {};
led = &chip->leds[i];
led->chip = chip;
led->led_num = i;
if (!led->is_active)
continue;
ret = st1202_channel_set(led->chip, led->led_num, true);
if (ret < 0)
return dev_err_probe(&client->dev, ret,
"Failed to activate LED channel\n");
ret = st1202_led_pattern_clear(&led->led_cdev);
if (ret < 0)
return dev_err_probe(&client->dev, ret,
"Failed to clear LED pattern\n");
init_data.fwnode = led->fwnode;
init_data.devicename = "st1202";
init_data.default_label = ":";
ret = devm_led_classdev_register_ext(&client->dev, &led->led_cdev, &init_data);
if (ret < 0)
return dev_err_probe(&client->dev, ret,
"Failed to register LED class device\n");
}
return 0;
}
static const struct i2c_device_id st1202_id[] = {
{ "st1202-i2c" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(i2c, st1202_id);
static const struct of_device_id st1202_dt_ids[] = {
{ .compatible = "st,led1202" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, st1202_dt_ids);
static struct i2c_driver st1202_driver = {
.driver = {
.name = "leds-st1202",
.of_match_table = of_match_ptr(st1202_dt_ids),
},
.probe = st1202_probe,
.id_table = st1202_id,
};
module_i2c_driver(st1202_driver);
MODULE_AUTHOR("Remote Tech LTD");
MODULE_DESCRIPTION("STMicroelectronics LED1202 : 12-channel constant current LED driver");
MODULE_LICENSE("GPL");