1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/drivers/leds-pwm.c
5 * simple PWM based LED control
7 * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de)
9 * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/platform_device.h>
15 #include <linux/of_platform.h>
16 #include <linux/leds.h>
17 #include <linux/err.h>
18 #include <linux/pwm.h>
19 #include <linux/leds_pwm.h>
20 #include <linux/slab.h>
23 struct led_classdev cdev
;
24 struct pwm_device
*pwm
;
25 unsigned int active_low
;
32 struct led_pwm_data leds
[0];
35 static void __led_pwm_set(struct led_pwm_data
*led_dat
)
37 int new_duty
= led_dat
->duty
;
39 pwm_config(led_dat
->pwm
, new_duty
, led_dat
->period
);
42 pwm_disable(led_dat
->pwm
);
44 pwm_enable(led_dat
->pwm
);
47 static int led_pwm_set(struct led_classdev
*led_cdev
,
48 enum led_brightness brightness
)
50 struct led_pwm_data
*led_dat
=
51 container_of(led_cdev
, struct led_pwm_data
, cdev
);
52 unsigned int max
= led_dat
->cdev
.max_brightness
;
53 unsigned long long duty
= led_dat
->period
;
58 if (led_dat
->active_low
)
59 duty
= led_dat
->period
- duty
;
63 __led_pwm_set(led_dat
);
68 static inline size_t sizeof_pwm_leds_priv(int num_leds
)
70 return sizeof(struct led_pwm_priv
) +
71 (sizeof(struct led_pwm_data
) * num_leds
);
74 static int led_pwm_add(struct device
*dev
, struct led_pwm_priv
*priv
,
75 struct led_pwm
*led
, struct fwnode_handle
*fwnode
)
77 struct led_pwm_data
*led_data
= &priv
->leds
[priv
->num_leds
];
78 struct pwm_args pargs
;
81 led_data
->active_low
= led
->active_low
;
82 led_data
->cdev
.name
= led
->name
;
83 led_data
->cdev
.default_trigger
= led
->default_trigger
;
84 led_data
->cdev
.brightness
= LED_OFF
;
85 led_data
->cdev
.max_brightness
= led
->max_brightness
;
86 led_data
->cdev
.flags
= LED_CORE_SUSPENDRESUME
;
89 led_data
->pwm
= devm_fwnode_pwm_get(dev
, fwnode
, NULL
);
91 led_data
->pwm
= devm_pwm_get(dev
, led
->name
);
92 if (IS_ERR(led_data
->pwm
)) {
93 ret
= PTR_ERR(led_data
->pwm
);
94 if (ret
!= -EPROBE_DEFER
)
95 dev_err(dev
, "unable to request PWM for %s: %d\n",
100 led_data
->cdev
.brightness_set_blocking
= led_pwm_set
;
103 * FIXME: pwm_apply_args() should be removed when switching to the
106 pwm_apply_args(led_data
->pwm
);
108 pwm_get_args(led_data
->pwm
, &pargs
);
110 led_data
->period
= pargs
.period
;
111 if (!led_data
->period
&& (led
->pwm_period_ns
> 0))
112 led_data
->period
= led
->pwm_period_ns
;
114 ret
= devm_of_led_classdev_register(dev
, to_of_node(fwnode
),
118 led_pwm_set(&led_data
->cdev
, led_data
->cdev
.brightness
);
120 dev_err(dev
, "failed to register PWM led for %s: %d\n",
127 static int led_pwm_create_fwnode(struct device
*dev
, struct led_pwm_priv
*priv
)
129 struct fwnode_handle
*fwnode
;
133 memset(&led
, 0, sizeof(led
));
135 device_for_each_child_node(dev
, fwnode
) {
136 ret
= fwnode_property_read_string(fwnode
, "label", &led
.name
);
137 if (ret
&& is_of_node(fwnode
))
138 led
.name
= to_of_node(fwnode
)->name
;
141 fwnode_handle_put(fwnode
);
145 fwnode_property_read_string(fwnode
, "linux,default-trigger",
146 &led
.default_trigger
);
148 led
.active_low
= fwnode_property_read_bool(fwnode
,
150 fwnode_property_read_u32(fwnode
, "max-brightness",
151 &led
.max_brightness
);
153 ret
= led_pwm_add(dev
, priv
, &led
, fwnode
);
155 fwnode_handle_put(fwnode
);
163 static int led_pwm_probe(struct platform_device
*pdev
)
165 struct led_pwm_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
166 struct led_pwm_priv
*priv
;
171 count
= pdata
->num_leds
;
173 count
= device_get_child_node_count(&pdev
->dev
);
178 priv
= devm_kzalloc(&pdev
->dev
, sizeof_pwm_leds_priv(count
),
184 for (i
= 0; i
< count
; i
++) {
185 ret
= led_pwm_add(&pdev
->dev
, priv
, &pdata
->leds
[i
],
191 ret
= led_pwm_create_fwnode(&pdev
->dev
, priv
);
197 platform_set_drvdata(pdev
, priv
);
202 static const struct of_device_id of_pwm_leds_match
[] = {
203 { .compatible
= "pwm-leds", },
206 MODULE_DEVICE_TABLE(of
, of_pwm_leds_match
);
208 static struct platform_driver led_pwm_driver
= {
209 .probe
= led_pwm_probe
,
212 .of_match_table
= of_pwm_leds_match
,
216 module_platform_driver(led_pwm_driver
);
218 MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
219 MODULE_DESCRIPTION("generic PWM LED driver");
220 MODULE_LICENSE("GPL v2");
221 MODULE_ALIAS("platform:leds-pwm");