Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 2 | /* |
| 3 | * linux/drivers/leds-pwm.c |
| 4 | * |
| 5 | * simple PWM based LED control |
| 6 | * |
| 7 | * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de) |
| 8 | * |
| 9 | * based on leds-gpio.c by Raphael Assenat <raph@8d.com> |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <linux/module.h> |
| 13 | #include <linux/kernel.h> |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 15 | #include <linux/of_platform.h> |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 16 | #include <linux/leds.h> |
| 17 | #include <linux/err.h> |
| 18 | #include <linux/pwm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 20 | |
Denis Osterland-Heim | 141f15c | 2020-03-21 08:15:53 +0000 | [diff] [blame] | 21 | struct led_pwm { |
| 22 | const char *name; |
Denis Osterland-Heim | 141f15c | 2020-03-21 08:15:53 +0000 | [diff] [blame] | 23 | u8 active_low; |
| 24 | unsigned int max_brightness; |
Denis Osterland-Heim | 141f15c | 2020-03-21 08:15:53 +0000 | [diff] [blame] | 25 | }; |
| 26 | |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 27 | struct led_pwm_data { |
| 28 | struct led_classdev cdev; |
| 29 | struct pwm_device *pwm; |
Uwe Kleine-König | dd47a83 | 2020-01-24 17:54:08 +0100 | [diff] [blame] | 30 | struct pwm_state pwmstate; |
Sachin Kamat | dcba910 | 2012-11-25 12:24:55 +0530 | [diff] [blame] | 31 | unsigned int active_low; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 32 | }; |
| 33 | |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 34 | struct led_pwm_priv { |
| 35 | int num_leds; |
Gustavo A. R. Silva | 7bbec6c | 2020-03-19 16:51:46 -0500 | [diff] [blame] | 36 | struct led_pwm_data leds[]; |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 37 | }; |
| 38 | |
Thierry Reding | 247bde1 | 2017-01-04 09:37:56 +0100 | [diff] [blame] | 39 | static int led_pwm_set(struct led_classdev *led_cdev, |
| 40 | enum led_brightness brightness) |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 41 | { |
| 42 | struct led_pwm_data *led_dat = |
| 43 | container_of(led_cdev, struct led_pwm_data, cdev); |
Lars-Peter Clausen | e459062 | 2009-11-27 06:17:38 +0100 | [diff] [blame] | 44 | unsigned int max = led_dat->cdev.max_brightness; |
Uwe Kleine-König | dd47a83 | 2020-01-24 17:54:08 +0100 | [diff] [blame] | 45 | unsigned long long duty = led_dat->pwmstate.period; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 46 | |
Xiubo Li | fc1aee0 | 2013-12-11 01:19:42 -0800 | [diff] [blame] | 47 | duty *= brightness; |
| 48 | do_div(duty, max); |
Russell King | d19a8a7 | 2014-04-06 15:20:18 -0700 | [diff] [blame] | 49 | |
| 50 | if (led_dat->active_low) |
Uwe Kleine-König | dd47a83 | 2020-01-24 17:54:08 +0100 | [diff] [blame] | 51 | duty = led_dat->pwmstate.period - duty; |
Russell King | d19a8a7 | 2014-04-06 15:20:18 -0700 | [diff] [blame] | 52 | |
Uwe Kleine-König | dd47a83 | 2020-01-24 17:54:08 +0100 | [diff] [blame] | 53 | led_dat->pwmstate.duty_cycle = duty; |
| 54 | led_dat->pwmstate.enabled = duty > 0; |
| 55 | return pwm_apply_state(led_dat->pwm, &led_dat->pwmstate); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 56 | } |
| 57 | |
Alexander Dahl | 19d2e0c | 2020-10-05 22:34:40 +0200 | [diff] [blame] | 58 | __attribute__((nonnull)) |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 59 | static int led_pwm_add(struct device *dev, struct led_pwm_priv *priv, |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 60 | struct led_pwm *led, struct fwnode_handle *fwnode) |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 61 | { |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 62 | struct led_pwm_data *led_data = &priv->leds[priv->num_leds]; |
Alexander Dahl | de73f27 | 2020-09-19 07:31:44 +0200 | [diff] [blame] | 63 | struct led_init_data init_data = { .fwnode = fwnode }; |
Peter Ujfalusi | aa1a6d6 | 2013-11-28 01:06:38 -0800 | [diff] [blame] | 64 | int ret; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 65 | |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 66 | led_data->active_low = led->active_low; |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 67 | led_data->cdev.name = led->name; |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 68 | led_data->cdev.brightness = LED_OFF; |
| 69 | led_data->cdev.max_brightness = led->max_brightness; |
| 70 | led_data->cdev.flags = LED_CORE_SUSPENDRESUME; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 71 | |
Alexander Dahl | 19d2e0c | 2020-10-05 22:34:40 +0200 | [diff] [blame] | 72 | led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL); |
Krzysztof Kozlowski | 7e8da60 | 2020-08-26 16:50:11 +0200 | [diff] [blame] | 73 | if (IS_ERR(led_data->pwm)) |
| 74 | return dev_err_probe(dev, PTR_ERR(led_data->pwm), |
| 75 | "unable to request PWM for %s\n", |
| 76 | led->name); |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 77 | |
Thierry Reding | 247bde1 | 2017-01-04 09:37:56 +0100 | [diff] [blame] | 78 | led_data->cdev.brightness_set_blocking = led_pwm_set; |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 79 | |
Uwe Kleine-König | dd47a83 | 2020-01-24 17:54:08 +0100 | [diff] [blame] | 80 | pwm_init_state(led_data->pwm, &led_data->pwmstate); |
Boris Brezillon | 1b50673 | 2016-04-14 21:17:26 +0200 | [diff] [blame] | 81 | |
Alexander Dahl | de73f27 | 2020-09-19 07:31:44 +0200 | [diff] [blame] | 82 | ret = devm_led_classdev_register_ext(dev, &led_data->cdev, &init_data); |
Denis Osterland-Heim | 44c606b | 2020-04-21 13:09:14 +0000 | [diff] [blame] | 83 | if (ret) { |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 84 | dev_err(dev, "failed to register PWM led for %s: %d\n", |
| 85 | led->name, ret); |
Denis Osterland-Heim | 44c606b | 2020-04-21 13:09:14 +0000 | [diff] [blame] | 86 | return ret; |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 87 | } |
| 88 | |
Denis Osterland-Heim | 44c606b | 2020-04-21 13:09:14 +0000 | [diff] [blame] | 89 | ret = led_pwm_set(&led_data->cdev, led_data->cdev.brightness); |
| 90 | if (ret) { |
| 91 | dev_err(dev, "failed to set led PWM value for %s: %d", |
| 92 | led->name, ret); |
| 93 | return ret; |
| 94 | } |
| 95 | |
| 96 | priv->num_leds++; |
| 97 | return 0; |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 100 | static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv) |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 101 | { |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 102 | struct fwnode_handle *fwnode; |
Russell King | b795e6d | 2014-04-06 15:20:13 -0700 | [diff] [blame] | 103 | struct led_pwm led; |
Andy Shevchenko | 95138e0 | 2021-05-10 12:50:43 +0300 | [diff] [blame] | 104 | int ret; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 105 | |
Russell King | b795e6d | 2014-04-06 15:20:13 -0700 | [diff] [blame] | 106 | memset(&led, 0, sizeof(led)); |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 107 | |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 108 | device_for_each_child_node(dev, fwnode) { |
| 109 | ret = fwnode_property_read_string(fwnode, "label", &led.name); |
| 110 | if (ret && is_of_node(fwnode)) |
| 111 | led.name = to_of_node(fwnode)->name; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 112 | |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 113 | if (!led.name) { |
Andy Shevchenko | 95138e0 | 2021-05-10 12:50:43 +0300 | [diff] [blame] | 114 | ret = EINVAL; |
| 115 | goto err_child_out; |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 116 | } |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 117 | |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 118 | led.active_low = fwnode_property_read_bool(fwnode, |
| 119 | "active-low"); |
| 120 | fwnode_property_read_u32(fwnode, "max-brightness", |
| 121 | &led.max_brightness); |
| 122 | |
| 123 | ret = led_pwm_add(dev, priv, &led, fwnode); |
Andy Shevchenko | 95138e0 | 2021-05-10 12:50:43 +0300 | [diff] [blame] | 124 | if (ret) |
| 125 | goto err_child_out; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 126 | } |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 127 | |
Andy Shevchenko | 95138e0 | 2021-05-10 12:50:43 +0300 | [diff] [blame] | 128 | return 0; |
| 129 | |
| 130 | err_child_out: |
| 131 | fwnode_handle_put(fwnode); |
Peter Ujfalusi | aa1a6d6 | 2013-11-28 01:06:38 -0800 | [diff] [blame] | 132 | return ret; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 135 | static int led_pwm_probe(struct platform_device *pdev) |
| 136 | { |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 137 | struct led_pwm_priv *priv; |
Peter Ujfalusi | aa1a6d6 | 2013-11-28 01:06:38 -0800 | [diff] [blame] | 138 | int ret = 0; |
Alexander Dahl | 19d2e0c | 2020-10-05 22:34:40 +0200 | [diff] [blame] | 139 | int count; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 140 | |
Alexander Dahl | 19d2e0c | 2020-10-05 22:34:40 +0200 | [diff] [blame] | 141 | count = device_get_child_node_count(&pdev->dev); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 142 | |
Peter Ujfalusi | aa1a6d6 | 2013-11-28 01:06:38 -0800 | [diff] [blame] | 143 | if (!count) |
| 144 | return -EINVAL; |
| 145 | |
Gustavo A. R. Silva | d4b0220 | 2019-08-29 19:53:20 -0500 | [diff] [blame] | 146 | priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count), |
Peter Ujfalusi | aa1a6d6 | 2013-11-28 01:06:38 -0800 | [diff] [blame] | 147 | GFP_KERNEL); |
| 148 | if (!priv) |
| 149 | return -ENOMEM; |
| 150 | |
Alexander Dahl | 19d2e0c | 2020-10-05 22:34:40 +0200 | [diff] [blame] | 151 | ret = led_pwm_create_fwnode(&pdev->dev, priv); |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 152 | |
Krzysztof Kozlowski | e5a0436 | 2018-12-07 13:32:51 +0100 | [diff] [blame] | 153 | if (ret) |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 154 | return ret; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 155 | |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 156 | platform_set_drvdata(pdev, priv); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 157 | |
| 158 | return 0; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 161 | static const struct of_device_id of_pwm_leds_match[] = { |
| 162 | { .compatible = "pwm-leds", }, |
| 163 | {}, |
| 164 | }; |
| 165 | MODULE_DEVICE_TABLE(of, of_pwm_leds_match); |
| 166 | |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 167 | static struct platform_driver led_pwm_driver = { |
| 168 | .probe = led_pwm_probe, |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 169 | .driver = { |
| 170 | .name = "leds_pwm", |
Sachin Kamat | 1e08f72 | 2013-09-28 04:38:31 -0700 | [diff] [blame] | 171 | .of_match_table = of_pwm_leds_match, |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 172 | }, |
| 173 | }; |
| 174 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 175 | module_platform_driver(led_pwm_driver); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 176 | |
| 177 | MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>"); |
Uwe Kleine-König | 49651c6 | 2013-11-25 21:43:45 +0100 | [diff] [blame] | 178 | MODULE_DESCRIPTION("generic PWM LED driver"); |
| 179 | MODULE_LICENSE("GPL v2"); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 180 | MODULE_ALIAS("platform:leds-pwm"); |