Commit 6fd577cd authored by Linus Torvalds's avatar Linus Torvalds

Merge tag 'leds-for-4.20-rc1' of...

Merge tag 'leds-for-4.20-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewski/linux-leds

Pull LED updates from Jacek Anaszewski:
"We introduce LED pattern trigger - the idea that was proposed three
 years ago now received enough attention and determination to drive it
 to the successful end.

  There is also one new LED class driver and couple of improvements to
  the existing ones.

  New LED class driver:
   - add support for Panasonic AN30259A with related DT bindings

  New LED trigger:
   - introduce LED pattern trigger

  leds-sc27xx-bltc:
   - implement pattern_set/clear ops to enable support for pattern
     trigger's hw_pattern sysfs file

  Improvements to existing LED class drivers:
   - leds-pwm: don't print error message on -EPROBE_DEFER
   - leds-gpio: try to lookup gpiod from device
   - leds-as3645a: convert to using %pOFn instead of device_node.name"

* tag 'leds-for-4.20-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/j.anaszewski/linux-leds:
  leds: sc27xx: Add pattern_set/clear interfaces for LED controller
  leds: core: Introduce LED pattern trigger
  leds: add Panasonic AN30259A support
  dt-bindings: leds: document Panasonic AN30259A bindings
  leds: gpio: Try to lookup gpiod from device
  leds: pwm: silently error out on EPROBE_DEFER
  leds: Convert to using %pOFn instead of device_node.name
parents 114b5f8f 8dbac65f
What: /sys/class/leds/<led>/hw_pattern
Date: September 2018
KernelVersion: 4.20
Description:
Specify a hardware pattern for the SC27XX LED. For the SC27XX
LED controller, it only supports 4 stages to make a single
hardware pattern, which is used to configure the rise time,
high time, fall time and low time for the breathing mode.
For the breathing mode, the SC27XX LED only expects one brightness
for the high stage. To be compatible with the hardware pattern
format, we should set brightness as 0 for rise stage, fall
stage and low stage.
Min stage duration: 125 ms
Max stage duration: 31875 ms
Since the stage duration step is 125 ms, the duration should be
a multiplier of 125, like 125ms, 250ms, 375ms, 500ms ... 31875ms.
Thus the format of the hardware pattern values should be:
"0 rise_duration brightness high_duration 0 fall_duration 0 low_duration".
What: /sys/class/leds/<led>/pattern
Date: September 2018
KernelVersion: 4.20
Description:
Specify a software pattern for the LED, that supports altering
the brightness for the specified duration with one software
timer. It can do gradual dimming and step change of brightness.
The pattern is given by a series of tuples, of brightness and
duration (ms). The LED is expected to traverse the series and
each brightness value for the specified duration. Duration of
0 means brightness should immediately change to new value, and
writing malformed pattern deactivates any active one.
1. For gradual dimming, the dimming interval now is set as 50
milliseconds. So the tuple with duration less than dimming
interval (50ms) is treated as a step change of brightness,
i.e. the subsequent brightness will be applied without adding
intervening dimming intervals.
The gradual dimming format of the software pattern values should be:
"brightness_1 duration_1 brightness_2 duration_2 brightness_3
duration_3 ...". For example:
echo 0 1000 255 2000 > pattern
It will make the LED go gradually from zero-intensity to max (255)
intensity in 1000 milliseconds, then back to zero intensity in 2000
milliseconds:
LED brightness
^
255-| / \ / \ /
| / \ / \ /
| / \ / \ /
| / \ / \ /
0-| / \/ \/
+---0----1----2----3----4----5----6------------> time (s)
2. To make the LED go instantly from one brigntess value to another,
we should use use zero-time lengths (the brightness must be same as
the previous tuple's). So the format should be:
"brightness_1 duration_1 brightness_1 0 brightness_2 duration_2
brightness_2 0 ...". For example:
echo 0 1000 0 0 255 2000 255 0 > pattern
It will make the LED stay off for one second, then stay at max brightness
for two seconds:
LED brightness
^
255-| +---------+ +---------+
| | | | |
| | | | |
| | | | |
0-| -----+ +----+ +----
+---0----1----2----3----4----5----6------------> time (s)
What: /sys/class/leds/<led>/hw_pattern
Date: September 2018
KernelVersion: 4.20
Description:
Specify a hardware pattern for the LED, for LED hardware that
supports autonomously controlling brightness over time, according
to some preprogrammed hardware patterns. It deactivates any active
software pattern.
Since different LED hardware can have different semantics of
hardware patterns, each driver is expected to provide its own
description for the hardware patterns in their ABI documentation
file.
What: /sys/class/leds/<led>/repeat
Date: September 2018
KernelVersion: 4.20
Description:
Specify a pattern repeat number. -1 means repeat indefinitely,
other negative numbers and number 0 are invalid.
This file will always return the originally written repeat
number.
* Panasonic AN30259A 3-channel LED driver
The AN30259A is a LED controller capable of driving three LEDs independently. It supports
constant current output and sloping current output modes. The chip is connected over I2C.
Required properties:
- compatible: Must be "panasonic,an30259a".
- reg: I2C slave address.
- #address-cells: Must be 1.
- #size-cells: Must be 0.
Each LED is represented as a sub-node of the panasonic,an30259a node.
Required sub-node properties:
- reg: Pin that the LED is connected to. Must be 1, 2, or 3.
Optional sub-node properties:
- label: see Documentation/devicetree/bindings/leds/common.txt
- linux,default-trigger: see Documentation/devicetree/bindings/leds/common.txt
Example:
led-controller@30 {
compatible = "panasonic,an30259a";
reg = <0x30>;
#address-cells = <1>;
#size-cells = <0>;
led@1 {
reg = <1>;
linux,default-trigger = "heartbeat";
label = "red:indicator";
};
led@2 {
reg = <2>;
label = "green:indicator";
};
led@3 {
reg = <3>;
label = "blue:indicator";
};
};
......@@ -58,6 +58,16 @@ config LEDS_AAT1290
help
This option enables support for the LEDs on the AAT1290.
config LEDS_AN30259A
tristate "LED support for Panasonic AN30259A"
depends on LEDS_CLASS && I2C && OF
help
This option enables support for the AN30259A 3-channel
LED driver.
To compile this driver as a module, choose M here: the module
will be called leds-an30259a.
config LEDS_APU
tristate "Front panel LED support for PC Engines APU/APU2/APU3 boards"
depends on LEDS_CLASS
......
......@@ -11,6 +11,7 @@ obj-$(CONFIG_LEDS_88PM860X) += leds-88pm860x.o
obj-$(CONFIG_LEDS_AAT1290) += leds-aat1290.o
obj-$(CONFIG_LEDS_APU) += leds-apu.o
obj-$(CONFIG_LEDS_AS3645A) += leds-as3645a.o
obj-$(CONFIG_LEDS_AN30259A) += leds-an30259a.o
obj-$(CONFIG_LEDS_BCM6328) += leds-bcm6328.o
obj-$(CONFIG_LEDS_BCM6358) += leds-bcm6358.o
obj-$(CONFIG_LEDS_BD2802) += leds-bd2802.o
......
// SPDX-License-Identifier: GPL-2.0+
//
// Driver for Panasonic AN30259A 3-channel LED driver
//
// Copyright (c) 2018 Simon Shields <simon@lineageos.org>
//
// Datasheet:
// https://www.alliedelec.com/m/d/a9d2b3ee87c2d1a535a41dd747b1c247.pdf
#include <linux/i2c.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/of.h>
#include <linux/regmap.h>
#include <uapi/linux/uleds.h>
#define AN30259A_MAX_LEDS 3
#define AN30259A_REG_SRESET 0x00
#define AN30259A_LED_SRESET BIT(0)
/* LED power registers */
#define AN30259A_REG_LED_ON 0x01
#define AN30259A_LED_EN(x) BIT((x) - 1)
#define AN30259A_LED_SLOPE(x) BIT(((x) - 1) + 4)
#define AN30259A_REG_LEDCC(x) (0x03 + ((x) - 1))
/* slope control registers */
#define AN30259A_REG_SLOPE(x) (0x06 + ((x) - 1))
#define AN30259A_LED_SLOPETIME1(x) (x)
#define AN30259A_LED_SLOPETIME2(x) ((x) << 4)
#define AN30259A_REG_LEDCNT1(x) (0x09 + (4 * ((x) - 1)))
#define AN30259A_LED_DUTYMAX(x) ((x) << 4)
#define AN30259A_LED_DUTYMID(x) (x)
#define AN30259A_REG_LEDCNT2(x) (0x0A + (4 * ((x) - 1)))
#define AN30259A_LED_DELAY(x) ((x) << 4)
#define AN30259A_LED_DUTYMIN(x) (x)
/* detention time control (length of each slope step) */
#define AN30259A_REG_LEDCNT3(x) (0x0B + (4 * ((x) - 1)))
#define AN30259A_LED_DT1(x) (x)
#define AN30259A_LED_DT2(x) ((x) << 4)
#define AN30259A_REG_LEDCNT4(x) (0x0C + (4 * ((x) - 1)))
#define AN30259A_LED_DT3(x) (x)
#define AN30259A_LED_DT4(x) ((x) << 4)
#define AN30259A_REG_MAX 0x14
#define AN30259A_BLINK_MAX_TIME 7500 /* ms */
#define AN30259A_SLOPE_RESOLUTION 500 /* ms */
#define STATE_OFF 0
#define STATE_KEEP 1
#define STATE_ON 2
struct an30259a;
struct an30259a_led {
struct an30259a *chip;
struct led_classdev cdev;
u32 num;
u32 default_state;
bool sloping;
char label[LED_MAX_NAME_SIZE];
};
struct an30259a {
struct mutex mutex; /* held when writing to registers */
struct i2c_client *client;
struct an30259a_led leds[AN30259A_MAX_LEDS];
struct regmap *regmap;
int num_leds;
};
static int an30259a_brightness_set(struct led_classdev *cdev,
enum led_brightness brightness)
{
struct an30259a_led *led;
int ret;
unsigned int led_on;
led = container_of(cdev, struct an30259a_led, cdev);
mutex_lock(&led->chip->mutex);
ret = regmap_read(led->chip->regmap, AN30259A_REG_LED_ON, &led_on);
if (ret)
goto error;
switch (brightness) {
case LED_OFF:
led_on &= ~AN30259A_LED_EN(led->num);
led_on &= ~AN30259A_LED_SLOPE(led->num);
led->sloping = false;
break;
default:
led_on |= AN30259A_LED_EN(led->num);
if (led->sloping)
led_on |= AN30259A_LED_SLOPE(led->num);
ret = regmap_write(led->chip->regmap,
AN30259A_REG_LEDCNT1(led->num),
AN30259A_LED_DUTYMAX(0xf) |
AN30259A_LED_DUTYMID(0xf));
if (ret)
goto error;
break;
}
ret = regmap_write(led->chip->regmap, AN30259A_REG_LED_ON, led_on);
if (ret)
goto error;
ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCC(led->num),
brightness);
error:
mutex_unlock(&led->chip->mutex);
return ret;
}
static int an30259a_blink_set(struct led_classdev *cdev,
unsigned long *delay_off, unsigned long *delay_on)
{
struct an30259a_led *led;
int ret, num;
unsigned int led_on;
unsigned long off = *delay_off, on = *delay_on;
led = container_of(cdev, struct an30259a_led, cdev);
mutex_lock(&led->chip->mutex);
num = led->num;
/* slope time can only be a multiple of 500ms. */
if (off % AN30259A_SLOPE_RESOLUTION || on % AN30259A_SLOPE_RESOLUTION) {
ret = -EINVAL;
goto error;
}
/* up to a maximum of 7500ms. */
if (off > AN30259A_BLINK_MAX_TIME || on > AN30259A_BLINK_MAX_TIME) {
ret = -EINVAL;
goto error;
}
/* if no blink specified, default to 1 Hz. */
if (!off && !on) {
*delay_off = off = 500;
*delay_on = on = 500;
}
/* convert into values the HW will understand. */
off /= AN30259A_SLOPE_RESOLUTION;
on /= AN30259A_SLOPE_RESOLUTION;
/* duty min should be zero (=off), delay should be zero. */
ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCNT2(num),
AN30259A_LED_DELAY(0) | AN30259A_LED_DUTYMIN(0));
if (ret)
goto error;
/* reset detention time (no "breathing" effect). */
ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCNT3(num),
AN30259A_LED_DT1(0) | AN30259A_LED_DT2(0));
if (ret)
goto error;
ret = regmap_write(led->chip->regmap, AN30259A_REG_LEDCNT4(num),
AN30259A_LED_DT3(0) | AN30259A_LED_DT4(0));
if (ret)
goto error;
/* slope time controls on/off cycle length. */
ret = regmap_write(led->chip->regmap, AN30259A_REG_SLOPE(num),
AN30259A_LED_SLOPETIME1(off) |
AN30259A_LED_SLOPETIME2(on));
if (ret)
goto error;
/* Finally, enable slope mode. */
ret = regmap_read(led->chip->regmap, AN30259A_REG_LED_ON, &led_on);
if (ret)
goto error;
led_on |= AN30259A_LED_SLOPE(num) | AN30259A_LED_EN(led->num);
ret = regmap_write(led->chip->regmap, AN30259A_REG_LED_ON, led_on);
if (!ret)
led->sloping = true;
error:
mutex_unlock(&led->chip->mutex);
return ret;
}
static int an30259a_dt_init(struct i2c_client *client,
struct an30259a *chip)
{
struct device_node *np = client->dev.of_node, *child;
int count, ret;
int i = 0;
const char *str;
struct an30259a_led *led;
count = of_get_child_count(np);
if (!count || count > AN30259A_MAX_LEDS)
return -EINVAL;
for_each_available_child_of_node(np, child) {
u32 source;
ret = of_property_read_u32(child, "reg", &source);
if (ret != 0 || !source || source > AN30259A_MAX_LEDS) {
dev_err(&client->dev, "Couldn't read LED address: %d\n",
ret);
count--;
continue;
}
led = &chip->leds[i];
led->num = source;
led->chip = chip;
if (of_property_read_string(child, "label", &str))
snprintf(led->label, sizeof(led->label), "an30259a::");
else
snprintf(led->label, sizeof(led->label), "an30259a:%s",
str);
led->cdev.name = led->label;
if (!of_property_read_string(child, "default-state", &str)) {
if (!strcmp(str, "on"))
led->default_state = STATE_ON;
else if (!strcmp(str, "keep"))
led->default_state = STATE_KEEP;
else
led->default_state = STATE_OFF;
}
of_property_read_string(child, "linux,default-trigger",
&led->cdev.default_trigger);
i++;
}
if (!count)
return -EINVAL;
chip->num_leds = i;
return 0;
}
static const struct regmap_config an30259a_regmap_config = {
.reg_bits = 8,
.val_bits = 8,
.max_register = AN30259A_REG_MAX,
};
static void an30259a_init_default_state(struct an30259a_led *led)
{
struct an30259a *chip = led->chip;
int led_on, err;
switch (led->default_state) {
case STATE_ON:
led->cdev.brightness = LED_FULL;
break;
case STATE_KEEP:
err = regmap_read(chip->regmap, AN30259A_REG_LED_ON, &led_on);
if (err)
break;
if (!(led_on & AN30259A_LED_EN(led->num))) {
led->cdev.brightness = LED_OFF;
break;
}
regmap_read(chip->regmap, AN30259A_REG_LEDCC(led->num),
&led->cdev.brightness);
break;
default:
led->cdev.brightness = LED_OFF;
}
an30259a_brightness_set(&led->cdev, led->cdev.brightness);
}
static int an30259a_probe(struct i2c_client *client)
{
struct an30259a *chip;
int i, err;
chip = devm_kzalloc(&client->dev, sizeof(*chip), GFP_KERNEL);
if (!chip)
return -ENOMEM;
err = an30259a_dt_init(client, chip);
if (err < 0)
return err;
mutex_init(&chip->mutex);
chip->client = client;
i2c_set_clientdata(client, chip);
chip->regmap = devm_regmap_init_i2c(client, &an30259a_regmap_config);
for (i = 0; i < chip->num_leds; i++) {
an30259a_init_default_state(&chip->leds[i]);
chip->leds[i].cdev.brightness_set_blocking =
an30259a_brightness_set;
chip->leds[i].cdev.blink_set = an30259a_blink_set;
err = devm_led_classdev_register(&client->dev,
&chip->leds[i].cdev);
if (err < 0)
goto exit;
}
return 0;
exit:
mutex_destroy(&chip->mutex);
return err;
}
static int an30259a_remove(struct i2c_client *client)
{
struct an30259a *chip = i2c_get_clientdata(client);
mutex_destroy(&chip->mutex);
return 0;
}
static const struct of_device_id an30259a_match_table[] = {
{ .compatible = "panasonic,an30259a", },
{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(of, an30259a_match_table);
static const struct i2c_device_id an30259a_id[] = {
{ "an30259a", 0 },
{ /* sentinel */ },
};
MODULE_DEVICE_TABLE(i2c, an30259a_id);
static struct i2c_driver an30259a_driver = {
.driver = {
.name = "leds-an32059a",
.of_match_table = of_match_ptr(an30259a_match_table),
},
.probe_new = an30259a_probe,
.remove = an30259a_remove,
.id_table = an30259a_id,
};
module_i2c_driver(an30259a_driver);
MODULE_AUTHOR("Simon Shields <simon@lineageos.org>");
MODULE_DESCRIPTION("AN32059A LED driver");
MODULE_LICENSE("GPL v2");
......@@ -529,7 +529,7 @@ static int as3645a_parse_node(struct as3645a *flash,
strlcpy(names->flash, name, sizeof(names->flash));
else
snprintf(names->flash, sizeof(names->flash),
"%s:flash", node->name);
"%pOFn:flash", node);
rval = of_property_read_u32(flash->flash_node, "flash-timeout-us",
&cfg->flash_timeout_us);
......@@ -573,7 +573,7 @@ static int as3645a_parse_node(struct as3645a *flash,
strlcpy(names->indicator, name, sizeof(names->indicator));
else
snprintf(names->indicator, sizeof(names->indicator),
"%s:indicator", node->name);
"%pOFn:indicator", node);
rval = of_property_read_u32(flash->indicator_node, "led-max-microamp",
&cfg->indicator_max_ua);
......
......@@ -81,35 +81,6 @@ static int create_gpio_led(const struct gpio_led *template,
{
int ret, state;
led_dat->gpiod = template->gpiod;
if (!led_dat->gpiod) {
/*
* This is the legacy code path for platform code that
* still uses GPIO numbers. Ultimately we would like to get
* rid of this block completely.
*/
unsigned long flags = GPIOF_OUT_INIT_LOW;
/* skip leds that aren't available */
if (!gpio_is_valid(template->gpio)) {
dev_info(parent, "Skipping unavailable LED gpio %d (%s)\n",
template->gpio, template->name);
return 0;
}
if (template->active_low)
flags |= GPIOF_ACTIVE_LOW;
ret = devm_gpio_request_one(parent, template->gpio, flags,
template->name);
if (ret < 0)
return ret;
led_dat->gpiod = gpio_to_desc(template->gpio);
if (!led_dat->gpiod)
return -EINVAL;
}
led_dat->cdev.name = template->name;
led_dat->cdev.default_trigger = template->default_trigger;
led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod);
......@@ -231,6 +202,52 @@ static const struct of_device_id of_gpio_leds_match[] = {
MODULE_DEVICE_TABLE(of, of_gpio_leds_match);
static struct gpio_desc *gpio_led_get_gpiod(struct device *dev, int idx,
const struct gpio_led *template)
{
struct gpio_desc *gpiod;
unsigned long flags = GPIOF_OUT_INIT_LOW;
int ret;
/*
* This means the LED does not come from the device tree
* or ACPI, so let's try just getting it by index from the
* device, this will hit the board file, if any and get
* the GPIO from there.
*/
gpiod = devm_gpiod_get_index(dev, NULL, idx, flags);
if (!IS_ERR(gpiod)) {
gpiod_set_consumer_name(gpiod, template->name);
return gpiod;
}
if (PTR_ERR(gpiod) != -ENOENT)
return gpiod;
/*
* This is the legacy code path for platform code that
* still uses GPIO numbers. Ultimately we would like to get
* rid of this block completely.
*/
/* skip leds that aren't available */
if (!gpio_is_valid(template->gpio))
return ERR_PTR(-ENOENT);
if (template->active_low)
flags |= GPIOF_ACTIVE_LOW;
ret = devm_gpio_request_one(dev, template->gpio, flags,
template->name);
if (ret < 0)
return ERR_PTR(ret);
gpiod = gpio_to_desc(template->gpio);
if (!gpiod)
return ERR_PTR(-EINVAL);
return gpiod;
}
static int gpio_led_probe(struct platform_device *pdev)
{
struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
......@@ -246,7 +263,22 @@ static int gpio_led_probe(struct platform_device *pdev)
priv->num_leds = pdata->num_leds;
for (i = 0; i < priv->num_leds; i++) {
ret = create_gpio_led(&pdata->leds[i], &priv->leds[i],
const struct gpio_led *template = &pdata->leds[i];
struct gpio_led_data *led_dat = &priv->leds[i];
if (template->gpiod)
led_dat->gpiod = template->gpiod;
else
led_dat->gpiod =
gpio_led_get_gpiod(&pdev->dev,
i, template);
if (IS_ERR(led_dat->gpiod)) {
dev_info(&pdev->dev, "Skipping unavailable LED gpio %d (%s)\n",
template->gpio, template->name);
continue;
}
ret = create_gpio_led(template, led_dat,
&pdev->dev, NULL,
pdata->gpio_blink_set);
if (ret < 0)
......
......@@ -100,6 +100,7 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
led_data->pwm = devm_pwm_get(dev, led->name);
if (IS_ERR(led_data->pwm)) {
ret = PTR_ERR(led_data->pwm);
if (ret != -EPROBE_DEFER)
dev_err(dev, "unable to request PWM for %s: %d\n",
led->name, ret);
return ret;
......
......@@ -32,8 +32,18 @@
#define SC27XX_DUTY_MASK GENMASK(15, 0)
#define SC27XX_MOD_MASK GENMASK(7, 0)
#define SC27XX_CURVE_SHIFT 8
#define SC27XX_CURVE_L_MASK GENMASK(7, 0)
#define SC27XX_CURVE_H_MASK GENMASK(15, 8)
#define SC27XX_LEDS_OFFSET 0x10
#define SC27XX_LEDS_MAX 3
#define SC27XX_LEDS_PATTERN_CNT 4
/* Stage duration step, in milliseconds */
#define SC27XX_LEDS_STEP 125
/* Minimum and maximum duration, in milliseconds */
#define SC27XX_DELTA_T_MIN SC27XX_LEDS_STEP
#define SC27XX_DELTA_T_MAX (SC27XX_LEDS_STEP * 255)
struct sc27xx_led {
char name[LED_MAX_NAME_SIZE];
......@@ -122,6 +132,113 @@ static int sc27xx_led_set(struct led_classdev *ldev, enum led_brightness value)
return err;
}
static void sc27xx_led_clamp_align_delta_t(u32 *delta_t)
{
u32 v, offset, t = *delta_t;
v = t + SC27XX_LEDS_STEP / 2;
v = clamp_t(u32, v, SC27XX_DELTA_T_MIN, SC27XX_DELTA_T_MAX);
offset = v - SC27XX_DELTA_T_MIN;
offset = SC27XX_LEDS_STEP * (offset / SC27XX_LEDS_STEP);
*delta_t = SC27XX_DELTA_T_MIN + offset;
}
static int sc27xx_led_pattern_clear(struct led_classdev *ldev)
{
struct sc27xx_led *leds = to_sc27xx_led(ldev);
struct regmap *regmap = leds->priv->regmap;
u32 base = sc27xx_led_get_offset(leds);
u32 ctrl_base = leds->priv->base + SC27XX_LEDS_CTRL;
u8 ctrl_shift = SC27XX_CTRL_SHIFT * leds->line;
int err;
mutex_lock(&leds->priv->lock);
/* Reset the rise, high, fall and low time to zero. */
regmap_write(regmap, base + SC27XX_LEDS_CURVE0, 0);
regmap_write(regmap, base + SC27XX_LEDS_CURVE1, 0);
err = regmap_update_bits(regmap, ctrl_base,
(SC27XX_LED_RUN | SC27XX_LED_TYPE) << ctrl_shift, 0);
ldev->brightness = LED_OFF;
mutex_unlock(&leds->priv->lock);
return err;
}
static int sc27xx_led_pattern_set(struct led_classdev *ldev,
struct led_pattern *pattern,
u32 len, int repeat)
{
struct sc27xx_led *leds = to_sc27xx_led(ldev);
u32 base = sc27xx_led_get_offset(leds);
u32 ctrl_base = leds->priv->base + SC27XX_LEDS_CTRL;
u8 ctrl_shift = SC27XX_CTRL_SHIFT * leds->line;
struct regmap *regmap = leds->priv->regmap;
int err;
/*
* Must contain 4 tuples to configure the rise time, high time, fall
* time and low time to enable the breathing mode.
*/
if (len != SC27XX_LEDS_PATTERN_CNT)
return -EINVAL;
mutex_lock(&leds->priv->lock);
sc27xx_led_clamp_align_delta_t(&pattern[0].delta_t);
err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE0,
SC27XX_CURVE_L_MASK,
pattern[0].delta_t / SC27XX_LEDS_STEP);
if (err)
goto out;
sc27xx_led_clamp_align_delta_t(&pattern[1].delta_t);
err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE1,
SC27XX_CURVE_L_MASK,
pattern[1].delta_t / SC27XX_LEDS_STEP);
if (err)
goto out;
sc27xx_led_clamp_align_delta_t(&pattern[2].delta_t);
err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE0,
SC27XX_CURVE_H_MASK,
(pattern[2].delta_t / SC27XX_LEDS_STEP) <<
SC27XX_CURVE_SHIFT);
if (err)
goto out;
sc27xx_led_clamp_align_delta_t(&pattern[3].delta_t);
err = regmap_update_bits(regmap, base + SC27XX_LEDS_CURVE1,
SC27XX_CURVE_H_MASK,
(pattern[3].delta_t / SC27XX_LEDS_STEP) <<
SC27XX_CURVE_SHIFT);
if (err)
goto out;
err = regmap_update_bits(regmap, base + SC27XX_LEDS_DUTY,
SC27XX_DUTY_MASK,
(pattern[1].brightness << SC27XX_DUTY_SHIFT) |
SC27XX_MOD_MASK);
if (err)
goto out;
/* Enable the LED breathing mode */
err = regmap_update_bits(regmap, ctrl_base,
SC27XX_LED_RUN << ctrl_shift,
SC27XX_LED_RUN << ctrl_shift);
if (!err)
ldev->brightness = pattern[1].brightness;
out:
mutex_unlock(&leds->priv->lock);
return err;
}
static int sc27xx_led_register(struct device *dev, struct sc27xx_led_priv *priv)
{
int i, err;
......@@ -140,6 +257,9 @@ static int sc27xx_led_register(struct device *dev, struct sc27xx_led_priv *priv)
led->priv = priv;
led->ldev.name = led->name;
led->ldev.brightness_set_blocking = sc27xx_led_set;
led->ldev.pattern_set = sc27xx_led_pattern_set;
led->ldev.pattern_clear = sc27xx_led_pattern_clear;
led->ldev.default_trigger = "pattern";
err = devm_led_classdev_register(dev, &led->ldev);
if (err)
......@@ -241,4 +361,5 @@ module_platform_driver(sc27xx_led_driver);
MODULE_DESCRIPTION("Spreadtrum SC27xx breathing light controller driver");
MODULE_AUTHOR("Xiaotong Lu <xiaotong.lu@spreadtrum.com>");
MODULE_AUTHOR("Baolin Wang <baolin.wang@linaro.org>");
MODULE_LICENSE("GPL v2");
......@@ -129,4 +129,11 @@ config LEDS_TRIGGER_NETDEV
This allows LEDs to be controlled by network device activity.
If unsure, say Y.
config LEDS_TRIGGER_PATTERN
tristate "LED Pattern Trigger"
help
This allows LEDs to be controlled by a software or hardware pattern
which is a series of tuples, of brightness and duration (ms).
If unsure, say N
endif # LEDS_TRIGGERS
......@@ -13,3 +13,4 @@ obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o
obj-$(CONFIG_LEDS_TRIGGER_CAMERA) += ledtrig-camera.o
obj-$(CONFIG_LEDS_TRIGGER_PANIC) += ledtrig-panic.o
obj-$(CONFIG_LEDS_TRIGGER_NETDEV) += ledtrig-netdev.o
obj-$(CONFIG_LEDS_TRIGGER_PATTERN) += ledtrig-pattern.o
// SPDX-License-Identifier: GPL-2.0
/*
* LED pattern trigger
*
* Idea discussed with Pavel Machek. Raphael Teysseyre implemented
* the first version, Baolin Wang simplified and improved the approach.
*/
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/mutex.h>
#include <linux/slab.h>
#include <linux/timer.h>
#define MAX_PATTERNS 1024
/*
* When doing gradual dimming, the led brightness will be updated
* every 50 milliseconds.
*/
#define UPDATE_INTERVAL 50
struct pattern_trig_data {
struct led_classdev *led_cdev;
struct led_pattern patterns[MAX_PATTERNS];
struct led_pattern *curr;
struct led_pattern *next;
struct mutex lock;
u32 npatterns;
int repeat;
int last_repeat;
int delta_t;
bool is_indefinite;
bool is_hw_pattern;
struct timer_list timer;
};
static void pattern_trig_update_patterns(struct pattern_trig_data *data)
{
data->curr = data->next;
if (!data->is_indefinite && data->curr == data->patterns)
data->repeat--;
if (data->next == data->patterns + data->npatterns - 1)
data->next = data->patterns;
else
data->next++;
data->delta_t = 0;
}
static int pattern_trig_compute_brightness(struct pattern_trig_data *data)
{
int step_brightness;
/*
* If current tuple's duration is less than the dimming interval,
* we should treat it as a step change of brightness instead of
* doing gradual dimming.
*/
if (data->delta_t == 0 || data->curr->delta_t < UPDATE_INTERVAL)
return data->curr->brightness;
step_brightness = abs(data->next->brightness - data->curr->brightness);
step_brightness = data->delta_t * step_brightness / data->curr->delta_t;
if (data->next->brightness > data->curr->brightness)
return data->curr->brightness + step_brightness;
else
return data->curr->brightness - step_brightness;
}
static void pattern_trig_timer_function(struct timer_list *t)
{
struct pattern_trig_data *data = from_timer(data, t, timer);
mutex_lock(&data->lock);
for (;;) {
if (!data->is_indefinite && !data->repeat)
break;
if (data->curr->brightness == data->next->brightness) {
/* Step change of brightness */
led_set_brightness(data->led_cdev,
data->curr->brightness);
mod_timer(&data->timer,
jiffies + msecs_to_jiffies(data->curr->delta_t));
/* Skip the tuple with zero duration */
pattern_trig_update_patterns(data);
/* Select next tuple */
pattern_trig_update_patterns(data);
} else {
/* Gradual dimming */
/*
* If the accumulation time is larger than current
* tuple's duration, we should go next one and re-check
* if we repeated done.
*/
if (data->delta_t > data->curr->delta_t) {
pattern_trig_update_patterns(data);
continue;
}
led_set_brightness(data->led_cdev,
pattern_trig_compute_brightness(data));
mod_timer(&data->timer,
jiffies + msecs_to_jiffies(UPDATE_INTERVAL));
/* Accumulate the gradual dimming time */
data->delta_t += UPDATE_INTERVAL;
}
break;
}
mutex_unlock(&data->lock);
}
static int pattern_trig_start_pattern(struct led_classdev *led_cdev)
{
struct pattern_trig_data *data = led_cdev->trigger_data;
if (!data->npatterns)
return 0;
if (data->is_hw_pattern) {
return led_cdev->pattern_set(led_cdev, data->patterns,
data->npatterns, data->repeat);
}
/* At least 2 tuples for software pattern. */
if (data->npatterns < 2)
return -EINVAL;
data->delta_t = 0;
data->curr = data->patterns;
data->next = data->patterns + 1;
data->timer.expires = jiffies;
add_timer(&data->timer);
return 0;
}
static ssize_t repeat_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
struct pattern_trig_data *data = led_cdev->trigger_data;
int repeat;
mutex_lock(&data->lock);
repeat = data->last_repeat;
mutex_unlock(&data->lock);
return scnprintf(buf, PAGE_SIZE, "%d\n", repeat);
}
static ssize_t repeat_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
struct pattern_trig_data *data = led_cdev->trigger_data;
int err, res;
err = kstrtos32(buf, 10, &res);
if (err)
return err;
/* Number 0 and negative numbers except -1 are invalid. */
if (res < -1 || res == 0)
return -EINVAL;
/*
* Clear previous patterns' performence firstly, and remove the timer
* without mutex lock to avoid dead lock.
*/
del_timer_sync(&data->timer);
mutex_lock(&data->lock);
if (data->is_hw_pattern)
led_cdev->pattern_clear(led_cdev);
data->last_repeat = data->repeat = res;
/* -1 means repeat indefinitely */
if (data->repeat == -1)
data->is_indefinite = true;
else
data->is_indefinite = false;
err = pattern_trig_start_pattern(led_cdev);
mutex_unlock(&data->lock);
return err < 0 ? err : count;
}
static DEVICE_ATTR_RW(repeat);
static ssize_t pattern_trig_show_patterns(struct pattern_trig_data *data,
char *buf, bool hw_pattern)
{
ssize_t count = 0;
int i;
mutex_lock(&data->lock);
if (!data->npatterns || (data->is_hw_pattern ^ hw_pattern))
goto out;
for (i = 0; i < data->npatterns; i++) {
count += scnprintf(buf + count, PAGE_SIZE - count,
"%d %u ",
data->patterns[i].brightness,
data->patterns[i].delta_t);
}
buf[count - 1] = '\n';
out:
mutex_unlock(&data->lock);
return count;
}
static ssize_t pattern_trig_store_patterns(struct led_classdev *led_cdev,
const char *buf, size_t count,
bool hw_pattern)
{
struct pattern_trig_data *data = led_cdev->trigger_data;
int ccount, cr, offset = 0, err = 0;
/*
* Clear previous patterns' performence firstly, and remove the timer
* without mutex lock to avoid dead lock.
*/
del_timer_sync(&data->timer);
mutex_lock(&data->lock);
if (data->is_hw_pattern)
led_cdev->pattern_clear(led_cdev);
data->is_hw_pattern = hw_pattern;
data->npatterns = 0;
while (offset < count - 1 && data->npatterns < MAX_PATTERNS) {
cr = 0;
ccount = sscanf(buf + offset, "%d %u %n",
&data->patterns[data->npatterns].brightness,
&data->patterns[data->npatterns].delta_t, &cr);
if (ccount != 2) {
data->npatterns = 0;
err = -EINVAL;
goto out;
}
offset += cr;
data->npatterns++;
}
err = pattern_trig_start_pattern(led_cdev);
if (err)
data->npatterns = 0;
out:
mutex_unlock(&data->lock);
return err < 0 ? err : count;
}
static ssize_t pattern_show(struct device *dev, struct device_attribute *attr,
char *buf)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
struct pattern_trig_data *data = led_cdev->trigger_data;
return pattern_trig_show_patterns(data, buf, false);
}
static ssize_t pattern_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
return pattern_trig_store_patterns(led_cdev, buf, count, false);
}
static DEVICE_ATTR_RW(pattern);
static ssize_t hw_pattern_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
struct pattern_trig_data *data = led_cdev->trigger_data;
return pattern_trig_show_patterns(data, buf, true);
}
static ssize_t hw_pattern_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
return pattern_trig_store_patterns(led_cdev, buf, count, true);
}
static DEVICE_ATTR_RW(hw_pattern);
static umode_t pattern_trig_attrs_mode(struct kobject *kobj,
struct attribute *attr, int index)
{
struct device *dev = container_of(kobj, struct device, kobj);
struct led_classdev *led_cdev = dev_get_drvdata(dev);
if (attr == &dev_attr_repeat.attr || attr == &dev_attr_pattern.attr)
return attr->mode;
else if (attr == &dev_attr_hw_pattern.attr && led_cdev->pattern_set)
return attr->mode;
return 0;
}
static struct attribute *pattern_trig_attrs[] = {
&dev_attr_pattern.attr,
&dev_attr_hw_pattern.attr,
&dev_attr_repeat.attr,
NULL
};
static const struct attribute_group pattern_trig_group = {
.attrs = pattern_trig_attrs,
.is_visible = pattern_trig_attrs_mode,
};
static const struct attribute_group *pattern_trig_groups[] = {
&pattern_trig_group,
NULL,
};
static int pattern_trig_activate(struct led_classdev *led_cdev)
{
struct pattern_trig_data *data;
data = kzalloc(sizeof(*data), GFP_KERNEL);
if (!data)
return -ENOMEM;
if (!!led_cdev->pattern_set ^ !!led_cdev->pattern_clear) {
dev_warn(led_cdev->dev,
"Hardware pattern ops validation failed\n");
led_cdev->pattern_set = NULL;
led_cdev->pattern_clear = NULL;
}
data->is_indefinite = true;
data->last_repeat = -1;
mutex_init(&data->lock);
data->led_cdev = led_cdev;
led_set_trigger_data(led_cdev, data);
timer_setup(&data->timer, pattern_trig_timer_function, 0);
led_cdev->activated = true;
return 0;
}
static void pattern_trig_deactivate(struct led_classdev *led_cdev)
{
struct pattern_trig_data *data = led_cdev->trigger_data;
if (!led_cdev->activated)
return;
if (led_cdev->pattern_clear)
led_cdev->pattern_clear(led_cdev);
del_timer_sync(&data->timer);
led_set_brightness(led_cdev, LED_OFF);
kfree(data);
led_cdev->activated = false;
}
static struct led_trigger pattern_led_trigger = {
.name = "pattern",
.activate = pattern_trig_activate,
.deactivate = pattern_trig_deactivate,
.groups = pattern_trig_groups,
};
static int __init pattern_trig_init(void)
{
return led_trigger_register(&pattern_led_trigger);
}
static void __exit pattern_trig_exit(void)
{
led_trigger_unregister(&pattern_led_trigger);
}
module_init(pattern_trig_init);
module_exit(pattern_trig_exit);
MODULE_AUTHOR("Raphael Teysseyre <rteysseyre@gmail.com");
MODULE_AUTHOR("Baolin Wang <baolin.wang@linaro.org");
MODULE_DESCRIPTION("LED Pattern trigger");
MODULE_LICENSE("GPL v2");
......@@ -22,6 +22,7 @@
#include <linux/workqueue.h>
struct device;
struct led_pattern;
/*
* LED Core
*/
......@@ -88,6 +89,10 @@ struct led_classdev {
unsigned long *delay_on,
unsigned long *delay_off);
int (*pattern_set)(struct led_classdev *led_cdev,
struct led_pattern *pattern, u32 len, int repeat);
int (*pattern_clear)(struct led_classdev *led_cdev);
struct device *dev;
const struct attribute_group **groups;
......@@ -472,4 +477,14 @@ static inline void led_classdev_notify_brightness_hw_changed(
struct led_classdev *led_cdev, enum led_brightness brightness) { }
#endif
/**
* struct led_pattern - pattern interval settings
* @delta_t: pattern interval delay, in milliseconds
* @brightness: pattern interval brightness
*/
struct led_pattern {
u32 delta_t;
int brightness;
};
#endif /* __LINUX_LEDS_H_INCLUDED */
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment