multi led support
Patch #2 introduces the multi LED support. It is based on the "single" led-pwm support. Since it is RFC only I haven't introduced DT-binding documentation.
I assumed to it to be "little" but due to the way the LED layer stores things it got a little larger than expected. Hints how to accomplish this in a different / shorter way are greatly welcome, this is however what I use right now.
Sebastian
RFC PATCH 1/2 pwm: allow to fetch pwm via index
This patch introduces of_pwm_get_index() (and its devm based friend) which works like of_pwm_get() except that you are able to specify which pwm you need instead using a name (which is optional anyway). This comes handy if you have multiple pwms specified in device tree and you need to grab one by one (in a loop) and you don't have any specific names for them.
--- drivers/pwm/core.c | 101 +++++++++++++++++++++++++++++++++++++--------------- include/linux/pwm.h | 16 +++++++++ 2 files changed, 88 insertions(+), 29 deletions(-)
drivers/pwm/core.c +72 -29
@@ -493,35 +493,24 @@ static struct pwm_chip *of_node_to_pwmchip(struct device_node *np) }/** -* of_pwm_get() - request a PWM via the PWM framework +* of_pwm_get_index() - request a PWM via the PWM framework * @np: device node to get the PWM from -* @con_id: consumer name +* @index: The number of the PWN within the property * * Returns the PWM device parsed from the phandle and index specified in the * "pwms" property of a device tree node or a negative error-code on failure. * Values parsed from the device tree are stored in the returned PWM device * object. * -* If con_id is NULL, the first PWM device listed in the "pwms" property will -* be requested. Otherwise the "pwm-names" property is used to do a reverse -* lookup of the PWM index. This also means that the "pwm-names" property -* becomes mandatory for devices that look up the PWM device via the con_id -* parameter. */ -struct pwm_device *of_pwm_get (struct device_node *np,const char *con_id) +struct pwm_device *of_pwm_get_index (struct device_node *np,int index) { -struct pwm_device *pwm =NULL ; +struct pwm_device *pwm;struct of_phandle_args args; +const char *con_id;struct pwm_chip *pc; -int index =0 ;int err; -if (con_id) { - index =of_property_match_string (np,"pwm-names" , con_id); -if (index <0 ) -return ERR_PTR (index); - } - err =of_parse_phandle_with_args (np,"pwms" ,"#pwm-cells" , index, &args);if (err) {
@@ -547,23 +536,47 @@ struct pwm_device *of_pwm_get(struct device_node *np, const char *con_id)if (IS_ERR (pwm)) goto put; -/* -* If a consumer name was not given, try to look it up from the -* "pwm-names" property if it exists. Otherwise use the name of -* the user device node. -*/ -if (!con_id) { - err =of_property_read_string_index (np,"pwm-names" , index, - &con_id); -if (err <0 ) - con_id = np->name; + err =of_property_read_string_index (np,"pwm-names" , index, + &con_id); +if (err <0 ) { + con_id = np->name; + pwm->label = con_id; } - - pwm->label = con_id; - put:of_node_put (args.np); +return pwm; +} +EXPORT_SYMBOL_GPL (of_pwm_get_index); +/** +* of_pwm_get() - request a PWM via the PWM framework +* @np: device node to get the PWM from +* @con_id: consumer name +* +* Returns the PWM device parsed from the phandle and index specified in the +* "pwms" property of a device tree node or a negative error-code on failure. +* Values parsed from the device tree are stored in the returned PWM device +* object. +* +* If con_id is NULL, the first PWM device listed in the "pwms" property will +* be requested. Otherwise the "pwm-names" property is used to do a reverse +* lookup of the PWM index. This also means that the "pwm-names" property +* becomes mandatory for devices that look up the PWM device via the con_id +* parameter. +*/ +struct pwm_device *of_pwm_get (struct device_node *np,const char *con_id) +{ +struct pwm_device *pwm; +int index =0 ; + +if (con_id) { + index =of_property_match_string (np,"pwm-names" , con_id); +if (index <0 ) +return ERR_PTR (index); + } + + pwm =of_pwm_get_index (np, index); + pwm->label = con_id;return pwm; }EXPORT_SYMBOL_GPL (of_pwm_get);
@@ -740,6 +753,36 @@ struct pwm_device *devm_pwm_get(struct device *dev, const char *con_id)EXPORT_SYMBOL_GPL (devm_pwm_get);/** +* devm_of_pwm_get_index() - resource managed of_pwm_get_index() +* @dev: device for PWM consumer +* @np: device node to get the PWM from +* @index: pwm index +* +* This function performs like of_pwm_get_index() but the acquired PWM device +* will automatically be released on driver detach. +*/ +struct pwm_device *devm_of_pwm_get_index (struct device *dev, +struct device_node *np,int index) +{ +struct pwm_device **ptr, *pwm; + + ptr =devres_alloc (devm_pwm_release,sizeof (*ptr),GFP_KERNEL ); +if (!ptr) +return ERR_PTR (-ENOMEM ); + + pwm =of_pwm_get_index (np, index); +if (!IS_ERR (pwm)) { + *ptr = pwm; +devres_add (dev, ptr); + }else { +devres_free (ptr); + } + +return pwm; +} +EXPORT_SYMBOL_GPL (devm_of_pwm_get_index); + +/** * devm_of_pwm_get() - resource managed of_pwm_get() * @dev: device for PWM consumer * @np: device node to get the PWM from
include/linux/pwm.h +16 -0
@@ -192,10 +192,13 @@ struct pwm_device *of_pwm_xlate_with_flags(struct pwm_chip *pc, const struct of_phandle_args *args);struct pwm_device *pwm_get (struct device *dev,const char *con_id); +struct pwm_device *of_pwm_get_index (struct device_node *np,int index);struct pwm_device *of_pwm_get (struct device_node *np,const char *con_id);void pwm_put (struct pwm_device *pwm);struct pwm_device *devm_pwm_get (struct device *dev,const char *con_id); +struct pwm_device *devm_of_pwm_get_index (struct device *dev, +struct device_node *np,int index);struct pwm_device *devm_of_pwm_get (struct device *dev,struct device_node *np,const char *con_id);void devm_pwm_put (struct device *dev,struct pwm_device *pwm);
@@ -235,6 +238,12 @@ static inline struct pwm_device *pwm_get(struct device *dev,return ERR_PTR (-ENODEV ); } +static inline struct pwm_device *of_pwm_get_index (struct device_node *np, +int index) +{ +return ERR_PTR (-ENODEV ); +} +static inline struct pwm_device *of_pwm_get (struct device_node *np,const char *con_id) {
@@ -251,6 +260,13 @@ static inline struct pwm_device *devm_pwm_get(struct device *dev,return ERR_PTR (-ENODEV ); } +static inline struct pwm_device *devm_of_pwm_get_index (struct device *dev, +struct device_node *np, +int index) +{ +return ERR_PTR (-ENODEV ); +} +static inline struct pwm_device *devm_of_pwm_get (struct device *dev,struct device_node *np,const char *con_id)
Signature · 1 line
2.1.4
RFC PATCH 2/2 leds: Add multi-LED driver for PWM driven multi-LED
In reply to Sebastian Andrzej SiewiorA multi-LED (I've seen so far) have three inputs: red, green and blue. You can hook them with three PWMs. Then you can program the red and green PWM to on and make it shine yellow. This works in generall. If a trigger is not exactly programmed then you might see two colors for a short period of time which looks odd.
Therefore this patch introduces a multi-led driver which programms all three PWMs as required within one IRQ disabled section.
The way this is currently used, is to programm the three colors via sysfs ("echo 255 255 0 > brightness" for yellow for instance) and then use a trigger like timer.
--- drivers/leds/leds-multi-pwm.c | 291 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 291 insertions(+) create mode 100644 drivers/leds/leds-multi-pwm.c
drivers/leds/leds-multi-pwm.c new file +291 -0
@@ -0,0 +1,291 @@ +/* +* multi PWM based LED control +* +* based on leds-pwm.c by Luotao Fu @ Pengutronix ([email protected]) +* based on leds-gpio.c by Raphael Assenat <[email protected]> +* +*/ + +#include <linux/module.h> +#include <linux/kernel.h> +#include <linux/platform_device.h> +#include <linux/of_platform.h> +#include <linux/leds.h> +#include <linux/err.h> +#include <linux/pwm.h> +#include <linux/leds_pwm.h> +#include <linux/slab.h> +#include <linux/workqueue.h> + +struct single_pwm { +struct pwm_device *pwm; +u32 duty; +bool enabled; +bool trigger_enabled; +bool active_low; +}; + +struct mled_pwm_data { +struct led_classdev cdev; +struct work_struct work; +bool can_sleep; +struct single_pwm *spwm; +}; + +struct mled_pwm_priv { +int num_leds; +struct mled_pwm_data leds[0 ]; +}; + +static int of_pwm_count (struct device_node *np) +{ +return of_count_phandle_with_args (np,"pwms" ,"#pwm-cells" ); +} + +static void __mled_pwm_set (struct mled_pwm_data *led_dat) +{ +struct single_pwm *spwm; +unsigned long flags; +int i; + +for (i =0 ; i < led_dat->cdev.num_leds; i++) { + spwm = &led_dat->spwm[i]; +pwm_config (spwm->pwm, spwm->duty,pwm_get_period (spwm->pwm)); + } + +local_irq_save (flags); +for (i =0 ; i < led_dat->cdev.num_leds; i++) { + spwm = &led_dat->spwm[i]; + +if (spwm->trigger_enabled) +pwm_enable (spwm->pwm); +else +pwm_disable (spwm->pwm); + } +local_irq_restore (flags); +} + +static void mled_pwm_work (struct work_struct *work) +{ +struct mled_pwm_data *led_dat = + container_of(work, struct mled_pwm_data, work); + +__mled_pwm_set (led_dat); +} + +static void mled_pwm_compute (struct single_pwm *spwm, +enum led_brightness brightness, +unsigned int max_brightness) +{ +unsigned long long duty; + + duty =pwm_get_period (spwm->pwm); + + duty *= brightness; +do_div (duty, max_brightness); + +if (spwm->active_low) + duty =pwm_get_period (spwm->pwm) - duty; + + spwm->duty = duty; + spwm->enabled = brightness ==LED_OFF ? false : true; + spwm->trigger_enabled = spwm->enabled; +} + +static void mled_pwm_set_multi (struct led_classdev *led_cdev, +enum led_brightness *brightness) +{ +struct mled_pwm_data *led_dat = + container_of(led_cdev, struct mled_pwm_data, cdev); +unsigned int max_brightness = led_dat->cdev.max_brightness; +int i; + +for (i =0 ; i < led_dat->cdev.num_leds; i++) +mled_pwm_compute (&led_dat->spwm[i], brightness[i], + max_brightness); + +if (led_dat->can_sleep) +schedule_work (&led_dat->work); +else +__mled_pwm_set (led_dat); +} + +static void mled_pwm_set_single (struct led_classdev *led_cdev, +enum led_brightness brightness) +{ +int i; +struct mled_pwm_data *led_dat = + container_of(led_cdev, struct mled_pwm_data, cdev); + +for (i =0 ; i < led_dat->cdev.num_leds; i++) { +struct single_pwm *spwm; + + spwm = &led_dat->spwm[i]; +if (spwm->enabled && brightness !=LED_OFF ) + spwm->trigger_enabled = true; +else + spwm->trigger_enabled = false; + } + +if (led_dat->can_sleep) +schedule_work (&led_dat->work); +else +__mled_pwm_set (led_dat); +} + +static inline size_t sizeof_pwm_leds_priv (int count) +{ +return sizeof (struct mled_pwm_priv ) + + (sizeof (struct mled_pwm_data ) * count); +} + +static void mled_pwm_cleanup (struct mled_pwm_priv *priv) +{ +while (priv->num_leds--) { +led_classdev_unregister (&priv->leds[priv->num_leds].cdev); +if (priv->leds[priv->num_leds].can_sleep) +cancel_work_sync (&priv->leds[priv->num_leds].work); + } +} + +static int mled_pwm_add (struct device *dev,struct mled_pwm_data *led_dat, +struct device_node *child) +{ +unsigned int max_brightness = led_dat->cdev.max_brightness; +u32 def_brightness[3 ] = {0 , }; +int ret; +u32 num_pwms; +int i; + +of_property_read_u32_array (child,"linux,default-brightness" , + def_brightness,3 ); + + num_pwms =of_pwm_count (child); +for (i =0 ; i < num_pwms; i++) { +struct single_pwm *spwm; +enum led_brightness brightness; + + spwm = &led_dat->spwm[i]; + + spwm->active_low =of_property_read_bool (child,"active-low" ); + + spwm->pwm =devm_of_pwm_get_index (dev, child, i); +if (IS_ERR (spwm->pwm)) { + ret =PTR_ERR (spwm->pwm); +dev_err (dev,"unable to request PWM for [%s:%d]: %d\n" , + child->name, i, ret); +return ret; + } + brightness = min_t(unsigned int, def_brightness[i], LED_FULL); +mled_pwm_compute (spwm, brightness, max_brightness); + led_dat->can_sleep |=pwm_can_sleep (spwm->pwm); + } + +if (led_dat->can_sleep) +INIT_WORK (&led_dat->work, mled_pwm_work); +return 0 ; +} + +static int mled_pwm_create_of (struct device *dev,struct mled_pwm_priv *priv) +{ +struct device_node *child; +int ret = -EINVAL ; + +for_each_child_of_node (dev->of_node, child) { +struct mled_pwm_data *led_dat = &priv->leds[priv->num_leds]; +int num_pwms; +u32 max_brightness =0 ; + + num_pwms =of_pwm_count (child); +if (num_pwms !=3 ) { +pr_err ("Need atleast three pwms.\n" ); + ret = -EINVAL ; + goto err; + } + led_dat->spwm =devm_kzalloc (dev,sizeof (struct single_pwm ) * + num_pwms,GFP_KERNEL ); + +of_property_read_u32 (child,"max-brightness" , &max_brightness); + led_dat->cdev.name =of_get_property (child,"label" ,NULL ) ? : + child->name; + led_dat->cdev.default_trigger =of_get_property (child, +"linux,default-trigger" ,NULL ); + led_dat->cdev.brightness_set = mled_pwm_set_single; + led_dat->cdev.brightness_set_multi = mled_pwm_set_multi; + led_dat->cdev.brightness =LED_OFF ; + led_dat->cdev.max_brightness = max_brightness; + led_dat->cdev.flags =LED_CORE_SUSPENDRESUME |LED_IS_MULTI ; + led_dat->cdev.num_leds = num_pwms; + + ret =mled_pwm_add (dev, led_dat, child); +if (ret) + goto err; + ret =led_classdev_register (dev, &led_dat->cdev); +if (ret ==0 ) { + priv->num_leds++; + }else { +dev_err (dev,"failed to register PWM led for %s: %d\n" , + child->name, ret); + goto err; + } + } + +return ret; +err: +of_node_put (child); +return ret; +} + +static int mled_pwm_probe (struct platform_device *pdev) +{ +struct mled_pwm_priv *priv; +int count; +int ret; + + count =of_get_child_count (pdev->dev.of_node); +if (!count) +return -EINVAL ; + + + priv =devm_kzalloc (&pdev->dev,sizeof_pwm_leds_priv (count), +GFP_KERNEL ); +if (!priv) +return -ENOMEM ; + + ret =mled_pwm_create_of (&pdev->dev, priv); +if (ret) { +mled_pwm_cleanup (priv); +return ret; + } + +platform_set_drvdata (pdev, priv); +return 0 ; +} + +static int mled_pwm_remove (struct platform_device *pdev) +{ +struct mled_pwm_priv *priv =platform_get_drvdata (pdev); + +mled_pwm_cleanup (priv); +return 0 ; +} + +static const struct of_device_id of_pwm_leds_match[] = { + { .compatible ="pwm-multi-leds" , }, + {}, +}; +MODULE_DEVICE_TABLE (of, of_pwm_leds_match); + +static struct platform_driver mled_pwm_driver = { + .probe = mled_pwm_probe, + .remove = mled_pwm_remove, + .driver = { + .name ="multi-leds_pwm" , + .of_match_table = of_pwm_leds_match, + }, +}; + +module_platform_driver (mled_pwm_driver); + +MODULE_DESCRIPTION ("Multi-LED PWM driver" ); +MODULE_LICENSE ("GPL" );
Signature · 1 line
2.1.4