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> |
| 19 | #include <linux/leds_pwm.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 21 | |
| 22 | struct led_pwm_data { |
| 23 | struct led_classdev cdev; |
| 24 | struct pwm_device *pwm; |
Uwe Kleine-König | dd47a83 | 2020-01-24 17:54:08 +0100 | [diff] [blame^] | 25 | struct pwm_state pwmstate; |
Sachin Kamat | dcba910 | 2012-11-25 12:24:55 +0530 | [diff] [blame] | 26 | unsigned int active_low; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 27 | }; |
| 28 | |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 29 | struct led_pwm_priv { |
| 30 | int num_leds; |
| 31 | struct led_pwm_data leds[0]; |
| 32 | }; |
| 33 | |
Thierry Reding | 247bde1 | 2017-01-04 09:37:56 +0100 | [diff] [blame] | 34 | static int led_pwm_set(struct led_classdev *led_cdev, |
| 35 | enum led_brightness brightness) |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 36 | { |
| 37 | struct led_pwm_data *led_dat = |
| 38 | container_of(led_cdev, struct led_pwm_data, cdev); |
Lars-Peter Clausen | e459062 | 2009-11-27 06:17:38 +0100 | [diff] [blame] | 39 | unsigned int max = led_dat->cdev.max_brightness; |
Uwe Kleine-König | dd47a83 | 2020-01-24 17:54:08 +0100 | [diff] [blame^] | 40 | unsigned long long duty = led_dat->pwmstate.period; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 41 | |
Xiubo Li | fc1aee0 | 2013-12-11 01:19:42 -0800 | [diff] [blame] | 42 | duty *= brightness; |
| 43 | do_div(duty, max); |
Russell King | d19a8a7 | 2014-04-06 15:20:18 -0700 | [diff] [blame] | 44 | |
| 45 | if (led_dat->active_low) |
Uwe Kleine-König | dd47a83 | 2020-01-24 17:54:08 +0100 | [diff] [blame^] | 46 | duty = led_dat->pwmstate.period - duty; |
Russell King | d19a8a7 | 2014-04-06 15:20:18 -0700 | [diff] [blame] | 47 | |
Uwe Kleine-König | dd47a83 | 2020-01-24 17:54:08 +0100 | [diff] [blame^] | 48 | led_dat->pwmstate.duty_cycle = duty; |
| 49 | led_dat->pwmstate.enabled = duty > 0; |
| 50 | return pwm_apply_state(led_dat->pwm, &led_dat->pwmstate); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 51 | } |
| 52 | |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 53 | 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] | 54 | struct led_pwm *led, struct fwnode_handle *fwnode) |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 55 | { |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 56 | struct led_pwm_data *led_data = &priv->leds[priv->num_leds]; |
Peter Ujfalusi | aa1a6d6 | 2013-11-28 01:06:38 -0800 | [diff] [blame] | 57 | int ret; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 58 | |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 59 | led_data->active_low = led->active_low; |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 60 | led_data->cdev.name = led->name; |
| 61 | led_data->cdev.default_trigger = led->default_trigger; |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 62 | led_data->cdev.brightness = LED_OFF; |
| 63 | led_data->cdev.max_brightness = led->max_brightness; |
| 64 | led_data->cdev.flags = LED_CORE_SUSPENDRESUME; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 65 | |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 66 | if (fwnode) |
| 67 | led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL); |
Russell King | b795e6d | 2014-04-06 15:20:13 -0700 | [diff] [blame] | 68 | else |
| 69 | led_data->pwm = devm_pwm_get(dev, led->name); |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 70 | if (IS_ERR(led_data->pwm)) { |
| 71 | ret = PTR_ERR(led_data->pwm); |
Jerome Brunet | 9aec303 | 2018-09-06 15:59:04 +0200 | [diff] [blame] | 72 | if (ret != -EPROBE_DEFER) |
| 73 | dev_err(dev, "unable to request PWM for %s: %d\n", |
| 74 | led->name, ret); |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 75 | return ret; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 76 | } |
| 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 | |
Uwe Kleine-König | dd47a83 | 2020-01-24 17:54:08 +0100 | [diff] [blame^] | 82 | if (!led_data->pwmstate.period) |
| 83 | led_data->pwmstate.period = led->pwm_period_ns; |
Linus Torvalds | 7c574cf | 2014-06-12 13:08:09 -0700 | [diff] [blame] | 84 | |
Jacek Anaszewski | b2b998c | 2019-06-09 20:19:03 +0200 | [diff] [blame] | 85 | ret = devm_led_classdev_register(dev, &led_data->cdev); |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 86 | if (ret == 0) { |
| 87 | priv->num_leds++; |
Markus Hofstaetter | f167033 | 2015-11-11 12:40:29 +0100 | [diff] [blame] | 88 | led_pwm_set(&led_data->cdev, led_data->cdev.brightness); |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 89 | } else { |
| 90 | dev_err(dev, "failed to register PWM led for %s: %d\n", |
| 91 | led->name, ret); |
| 92 | } |
| 93 | |
| 94 | return ret; |
| 95 | } |
| 96 | |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 97 | 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] | 98 | { |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 99 | struct fwnode_handle *fwnode; |
Russell King | b795e6d | 2014-04-06 15:20:13 -0700 | [diff] [blame] | 100 | struct led_pwm led; |
| 101 | int ret = 0; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 102 | |
Russell King | b795e6d | 2014-04-06 15:20:13 -0700 | [diff] [blame] | 103 | memset(&led, 0, sizeof(led)); |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 104 | |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 105 | device_for_each_child_node(dev, fwnode) { |
| 106 | ret = fwnode_property_read_string(fwnode, "label", &led.name); |
| 107 | if (ret && is_of_node(fwnode)) |
| 108 | led.name = to_of_node(fwnode)->name; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 109 | |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 110 | if (!led.name) { |
| 111 | fwnode_handle_put(fwnode); |
| 112 | return -EINVAL; |
| 113 | } |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 114 | |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 115 | fwnode_property_read_string(fwnode, "linux,default-trigger", |
| 116 | &led.default_trigger); |
| 117 | |
| 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); |
Russell King | b795e6d | 2014-04-06 15:20:13 -0700 | [diff] [blame] | 124 | if (ret) { |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 125 | fwnode_handle_put(fwnode); |
Russell King | b795e6d | 2014-04-06 15:20:13 -0700 | [diff] [blame] | 126 | break; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 127 | } |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 128 | } |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 129 | |
Peter Ujfalusi | aa1a6d6 | 2013-11-28 01:06:38 -0800 | [diff] [blame] | 130 | return ret; |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 133 | static int led_pwm_probe(struct platform_device *pdev) |
| 134 | { |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 135 | struct led_pwm_platform_data *pdata = dev_get_platdata(&pdev->dev); |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 136 | struct led_pwm_priv *priv; |
Peter Ujfalusi | aa1a6d6 | 2013-11-28 01:06:38 -0800 | [diff] [blame] | 137 | int count, i; |
| 138 | int ret = 0; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 139 | |
Peter Ujfalusi | aa1a6d6 | 2013-11-28 01:06:38 -0800 | [diff] [blame] | 140 | if (pdata) |
| 141 | count = pdata->num_leds; |
| 142 | else |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 143 | count = device_get_child_node_count(&pdev->dev); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 144 | |
Peter Ujfalusi | aa1a6d6 | 2013-11-28 01:06:38 -0800 | [diff] [blame] | 145 | if (!count) |
| 146 | return -EINVAL; |
| 147 | |
Gustavo A. R. Silva | d4b0220 | 2019-08-29 19:53:20 -0500 | [diff] [blame] | 148 | priv = devm_kzalloc(&pdev->dev, struct_size(priv, leds, count), |
Peter Ujfalusi | aa1a6d6 | 2013-11-28 01:06:38 -0800 | [diff] [blame] | 149 | GFP_KERNEL); |
| 150 | if (!priv) |
| 151 | return -ENOMEM; |
| 152 | |
| 153 | if (pdata) { |
| 154 | for (i = 0; i < count; i++) { |
Russell King | b795e6d | 2014-04-06 15:20:13 -0700 | [diff] [blame] | 155 | ret = led_pwm_add(&pdev->dev, priv, &pdata->leds[i], |
| 156 | NULL); |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 157 | if (ret) |
| 158 | break; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 159 | } |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 160 | } else { |
Nikolaus Voss | 3f467eb | 2019-06-12 10:36:08 +0200 | [diff] [blame] | 161 | ret = led_pwm_create_fwnode(&pdev->dev, priv); |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Krzysztof Kozlowski | e5a0436 | 2018-12-07 13:32:51 +0100 | [diff] [blame] | 164 | if (ret) |
Russell King | 5f7b03d | 2014-04-06 15:20:08 -0700 | [diff] [blame] | 165 | return ret; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 166 | |
Peter Ujfalusi | 0f86815 | 2012-12-21 01:43:56 -0800 | [diff] [blame] | 167 | platform_set_drvdata(pdev, priv); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 168 | |
| 169 | return 0; |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Peter Ujfalusi | 08541cb | 2012-12-21 01:44:01 -0800 | [diff] [blame] | 172 | static const struct of_device_id of_pwm_leds_match[] = { |
| 173 | { .compatible = "pwm-leds", }, |
| 174 | {}, |
| 175 | }; |
| 176 | MODULE_DEVICE_TABLE(of, of_pwm_leds_match); |
| 177 | |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 178 | static struct platform_driver led_pwm_driver = { |
| 179 | .probe = led_pwm_probe, |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 180 | .driver = { |
| 181 | .name = "leds_pwm", |
Sachin Kamat | 1e08f72 | 2013-09-28 04:38:31 -0700 | [diff] [blame] | 182 | .of_match_table = of_pwm_leds_match, |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 183 | }, |
| 184 | }; |
| 185 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 186 | module_platform_driver(led_pwm_driver); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 187 | |
| 188 | MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>"); |
Uwe Kleine-König | 49651c6 | 2013-11-25 21:43:45 +0100 | [diff] [blame] | 189 | MODULE_DESCRIPTION("generic PWM LED driver"); |
| 190 | MODULE_LICENSE("GPL v2"); |
Luotao Fu | 41c42ff | 2009-02-11 13:24:40 -0800 | [diff] [blame] | 191 | MODULE_ALIAS("platform:leds-pwm"); |