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 ...@@ -58,6 +58,16 @@ config LEDS_AAT1290
help help
This option enables support for the LEDs on the AAT1290. 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 config LEDS_APU
tristate "Front panel LED support for PC Engines APU/APU2/APU3 boards" tristate "Front panel LED support for PC Engines APU/APU2/APU3 boards"
depends on LEDS_CLASS depends on LEDS_CLASS
......
...@@ -11,6 +11,7 @@ obj-$(CONFIG_LEDS_88PM860X) += leds-88pm860x.o ...@@ -11,6 +11,7 @@ obj-$(CONFIG_LEDS_88PM860X) += leds-88pm860x.o
obj-$(CONFIG_LEDS_AAT1290) += leds-aat1290.o obj-$(CONFIG_LEDS_AAT1290) += leds-aat1290.o
obj-$(CONFIG_LEDS_APU) += leds-apu.o obj-$(CONFIG_LEDS_APU) += leds-apu.o
obj-$(CONFIG_LEDS_AS3645A) += leds-as3645a.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_BCM6328) += leds-bcm6328.o
obj-$(CONFIG_LEDS_BCM6358) += leds-bcm6358.o obj-$(CONFIG_LEDS_BCM6358) += leds-bcm6358.o
obj-$(CONFIG_LEDS_BD2802) += leds-bd2802.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, ...@@ -529,7 +529,7 @@ static int as3645a_parse_node(struct as3645a *flash,
strlcpy(names->flash, name, sizeof(names->flash)); strlcpy(names->flash, name, sizeof(names->flash));
else else
snprintf(names->flash, sizeof(names->flash), snprintf(names->flash, sizeof(names->flash),
"%s:flash", node->name); "%pOFn:flash", node);
rval = of_property_read_u32(flash->flash_node, "flash-timeout-us", rval = of_property_read_u32(flash->flash_node, "flash-timeout-us",
&cfg->flash_timeout_us); &cfg->flash_timeout_us);
...@@ -573,7 +573,7 @@ static int as3645a_parse_node(struct as3645a *flash, ...@@ -573,7 +573,7 @@ static int as3645a_parse_node(struct as3645a *flash,
strlcpy(names->indicator, name, sizeof(names->indicator)); strlcpy(names->indicator, name, sizeof(names->indicator));
else else
snprintf(names->indicator, sizeof(names->indicator), snprintf(names->indicator, sizeof(names->indicator),
"%s:indicator", node->name); "%pOFn:indicator", node);
rval = of_property_read_u32(flash->indicator_node, "led-max-microamp", rval = of_property_read_u32(flash->indicator_node, "led-max-microamp",
&cfg->indicator_max_ua); &cfg->indicator_max_ua);
......
...@@ -81,35 +81,6 @@ static int create_gpio_led(const struct gpio_led *template, ...@@ -81,35 +81,6 @@ static int create_gpio_led(const struct gpio_led *template,
{ {
int ret, state; 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.name = template->name;
led_dat->cdev.default_trigger = template->default_trigger; led_dat->cdev.default_trigger = template->default_trigger;
led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod); led_dat->can_sleep = gpiod_cansleep(led_dat->gpiod);
...@@ -231,6 +202,52 @@ static const struct of_device_id of_gpio_leds_match[] = { ...@@ -231,6 +202,52 @@ static const struct of_device_id of_gpio_leds_match[] = {
MODULE_DEVICE_TABLE(of, 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) static int gpio_led_probe(struct platform_device *pdev)
{ {
struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev); struct gpio_led_platform_data *pdata = dev_get_platdata(&pdev->dev);
...@@ -246,7 +263,22 @@ static int gpio_led_probe(struct platform_device *pdev) ...@@ -246,7 +263,22 @@ static int gpio_led_probe(struct platform_device *pdev)
priv->num_leds = pdata->num_leds; priv->num_leds = pdata->num_leds;
for (i = 0; i < priv->num_leds; i++) { 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, &pdev->dev, NULL,
pdata->gpio_blink_set); pdata->gpio_blink_set);
if (ret < 0) if (ret < 0)
......
...@@ -100,8 +100,9 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv, ...@@ -100,8 +100,9 @@ static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv,
led_data->pwm = devm_pwm_get(dev, led->name); led_data->pwm = devm_pwm_get(dev, led->name);
if (IS_ERR(led_data->pwm)) { if (IS_ERR(led_data->pwm)) {
ret = PTR_ERR(led_data->pwm); ret = PTR_ERR(led_data->pwm);
dev_err(dev, "unable to request PWM for %s: %d\n", if (ret != -EPROBE_DEFER)
led->name, ret); dev_err(dev, "unable to request PWM for %s: %d\n",
led->name, ret);
return ret; return ret;
} }
......
...@@ -32,8 +32,18 @@ ...@@ -32,8 +32,18 @@
#define SC27XX_DUTY_MASK GENMASK(15, 0) #define SC27XX_DUTY_MASK GENMASK(15, 0)
#define SC27XX_MOD_MASK GENMASK(7, 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_OFFSET 0x10
#define SC27XX_LEDS_MAX 3 #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 { struct sc27xx_led {
char name[LED_MAX_NAME_SIZE]; char name[LED_MAX_NAME_SIZE];
...@@ -122,6 +132,113 @@ static int sc27xx_led_set(struct led_classdev *ldev, enum led_brightness value) ...@@ -122,6 +132,113 @@ static int sc27xx_led_set(struct led_classdev *ldev, enum led_brightness value)
return err; 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) static int sc27xx_led_register(struct device *dev, struct sc27xx_led_priv *priv)
{ {
int i, err; int i, err;
...@@ -140,6 +257,9 @@ static int sc27xx_led_register(struct device *dev, struct sc27xx_led_priv *priv) ...@@ -140,6 +257,9 @@ static int sc27xx_led_register(struct device *dev, struct sc27xx_led_priv *priv)
led->priv = priv; led->priv = priv;
led->ldev.name = led->name; led->ldev.name = led->name;
led->ldev.brightness_set_blocking = sc27xx_led_set; 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); err = devm_led_classdev_register(dev, &led->ldev);
if (err) if (err)
...@@ -241,4 +361,5 @@ module_platform_driver(sc27xx_led_driver); ...@@ -241,4 +361,5 @@ module_platform_driver(sc27xx_led_driver);
MODULE_DESCRIPTION("Spreadtrum SC27xx breathing light controller driver"); MODULE_DESCRIPTION("Spreadtrum SC27xx breathing light controller driver");
MODULE_AUTHOR("Xiaotong Lu <xiaotong.lu@spreadtrum.com>"); MODULE_AUTHOR("Xiaotong Lu <xiaotong.lu@spreadtrum.com>");
MODULE_AUTHOR("Baolin Wang <baolin.wang@linaro.org>");
MODULE_LICENSE("GPL v2"); MODULE_LICENSE("GPL v2");
...@@ -129,4 +129,11 @@ config LEDS_TRIGGER_NETDEV ...@@ -129,4 +129,11 @@ config LEDS_TRIGGER_NETDEV
This allows LEDs to be controlled by network device activity. This allows LEDs to be controlled by network device activity.
If unsure, say Y. 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 endif # LEDS_TRIGGERS
...@@ -13,3 +13,4 @@ obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o ...@@ -13,3 +13,4 @@ obj-$(CONFIG_LEDS_TRIGGER_TRANSIENT) += ledtrig-transient.o
obj-$(CONFIG_LEDS_TRIGGER_CAMERA) += ledtrig-camera.o obj-$(CONFIG_LEDS_TRIGGER_CAMERA) += ledtrig-camera.o
obj-$(CONFIG_LEDS_TRIGGER_PANIC) += ledtrig-panic.o obj-$(CONFIG_LEDS_TRIGGER_PANIC) += ledtrig-panic.o
obj-$(CONFIG_LEDS_TRIGGER_NETDEV) += ledtrig-netdev.o obj-$(CONFIG_LEDS_TRIGGER_NETDEV) += ledtrig-netdev.o
obj-$(CONFIG_LEDS_TRIGGER_PATTERN) += ledtrig-pattern.o
This diff is collapsed.
...@@ -22,6 +22,7 @@ ...@@ -22,6 +22,7 @@
#include <linux/workqueue.h> #include <linux/workqueue.h>
struct device; struct device;
struct led_pattern;
/* /*
* LED Core * LED Core
*/ */
...@@ -88,6 +89,10 @@ struct led_classdev { ...@@ -88,6 +89,10 @@ struct led_classdev {
unsigned long *delay_on, unsigned long *delay_on,
unsigned long *delay_off); 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; struct device *dev;
const struct attribute_group **groups; const struct attribute_group **groups;
...@@ -472,4 +477,14 @@ static inline void led_classdev_notify_brightness_hw_changed( ...@@ -472,4 +477,14 @@ static inline void led_classdev_notify_brightness_hw_changed(
struct led_classdev *led_cdev, enum led_brightness brightness) { } struct led_classdev *led_cdev, enum led_brightness brightness) { }
#endif #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 */ #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