leds-pwm.c 4.9 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0-only
2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * linux/drivers/leds-pwm.c
 *
 * simple PWM based LED control
 *
 * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de)
 *
 * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/platform_device.h>
15
#include <linux/of_platform.h>
16 17 18 19
#include <linux/leds.h>
#include <linux/err.h>
#include <linux/pwm.h>
#include <linux/leds_pwm.h>
20
#include <linux/slab.h>
21 22 23 24

struct led_pwm_data {
	struct led_classdev	cdev;
	struct pwm_device	*pwm;
25
	unsigned int		active_low;
26
	unsigned int		period;
27
	int			duty;
28 29
};

30 31 32 33 34
struct led_pwm_priv {
	int num_leds;
	struct led_pwm_data leds[0];
};

35 36 37 38 39 40 41 42 43 44 45 46
static void __led_pwm_set(struct led_pwm_data *led_dat)
{
	int new_duty = led_dat->duty;

	pwm_config(led_dat->pwm, new_duty, led_dat->period);

	if (new_duty == 0)
		pwm_disable(led_dat->pwm);
	else
		pwm_enable(led_dat->pwm);
}

47 48
static int led_pwm_set(struct led_classdev *led_cdev,
		       enum led_brightness brightness)
49 50 51
{
	struct led_pwm_data *led_dat =
		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;
54

55 56
	duty *= brightness;
	do_div(duty, max);
57 58 59 60

	if (led_dat->active_low)
		duty = led_dat->period - duty;

61
	led_dat->duty = duty;
62

J
Jacek Anaszewski 已提交
63 64 65
	__led_pwm_set(led_dat);

	return 0;
66 67
}

68 69 70 71 72 73
static inline size_t sizeof_pwm_leds_priv(int num_leds)
{
	return sizeof(struct led_pwm_priv) +
		      (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)
76
{
77
	struct led_pwm_data *led_data = &priv->leds[priv->num_leds];
78
	struct pwm_args pargs;
79
	int ret;
80

81 82 83 84 85 86
	led_data->active_low = led->active_low;
	led_data->cdev.name = led->name;
	led_data->cdev.default_trigger = led->default_trigger;
	led_data->cdev.brightness = LED_OFF;
	led_data->cdev.max_brightness = led->max_brightness;
	led_data->cdev.flags = LED_CORE_SUSPENDRESUME;
87

88 89
	if (fwnode)
		led_data->pwm = devm_fwnode_pwm_get(dev, fwnode, NULL);
90 91
	else
		led_data->pwm = devm_pwm_get(dev, led->name);
92 93
	if (IS_ERR(led_data->pwm)) {
		ret = PTR_ERR(led_data->pwm);
94 95 96
		if (ret != -EPROBE_DEFER)
			dev_err(dev, "unable to request PWM for %s: %d\n",
				led->name, ret);
97 98
		return ret;
	}
99

100
	led_data->cdev.brightness_set_blocking = led_pwm_set;
101

102 103 104 105 106 107 108 109 110
	/*
	 * FIXME: pwm_apply_args() should be removed when switching to the
	 * atomic PWM API.
	 */
	pwm_apply_args(led_data->pwm);

	pwm_get_args(led_data->pwm, &pargs);

	led_data->period = pargs.period;
111 112
	if (!led_data->period && (led->pwm_period_ns > 0))
		led_data->period = led->pwm_period_ns;
113

114 115
	ret = devm_of_led_classdev_register(dev, to_of_node(fwnode),
					    &led_data->cdev);
116 117
	if (ret == 0) {
		priv->num_leds++;
118
		led_pwm_set(&led_data->cdev, led_data->cdev.brightness);
119 120 121 122 123 124 125
	} else {
		dev_err(dev, "failed to register PWM led for %s: %d\n",
			led->name, ret);
	}

	return ret;
}
126

127
static int led_pwm_create_fwnode(struct device *dev, struct led_pwm_priv *priv)
128
{
129
	struct fwnode_handle *fwnode;
130 131
	struct led_pwm led;
	int ret = 0;
132

133
	memset(&led, 0, sizeof(led));
134

135 136 137 138
	device_for_each_child_node(dev, fwnode) {
		ret = fwnode_property_read_string(fwnode, "label", &led.name);
		if (ret && is_of_node(fwnode))
			led.name = to_of_node(fwnode)->name;
139

140 141 142 143 144 145 146 147 148 149 150 151
		if (!led.name) {
			fwnode_handle_put(fwnode);
			return -EINVAL;
		}

		fwnode_property_read_string(fwnode, "linux,default-trigger",
					    &led.default_trigger);

		led.active_low = fwnode_property_read_bool(fwnode,
							   "active-low");
		fwnode_property_read_u32(fwnode, "max-brightness",
					 &led.max_brightness);
152

153
		ret = led_pwm_add(dev, priv, &led, fwnode);
154
		if (ret) {
155
			fwnode_handle_put(fwnode);
156
			break;
157 158 159
		}
	}

160
	return ret;
161 162 163 164
}

static int led_pwm_probe(struct platform_device *pdev)
{
J
Jingoo Han 已提交
165
	struct led_pwm_platform_data *pdata = dev_get_platdata(&pdev->dev);
166
	struct led_pwm_priv *priv;
167 168 169 170 171 172
	int count, i;
	int ret = 0;

	if (pdata)
		count = pdata->num_leds;
	else
173
		count = device_get_child_node_count(&pdev->dev);
174 175 176

	if (!count)
		return -EINVAL;
177

178 179 180 181
	priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(count),
			    GFP_KERNEL);
	if (!priv)
		return -ENOMEM;
182

183 184
	if (pdata) {
		for (i = 0; i < count; i++) {
185 186
			ret = led_pwm_add(&pdev->dev, priv, &pdata->leds[i],
					  NULL);
187 188
			if (ret)
				break;
189 190
		}
	} else {
191
		ret = led_pwm_create_fwnode(&pdev->dev, priv);
192 193
	}

194
	if (ret)
195
		return ret;
196

197
	platform_set_drvdata(pdev, priv);
198 199 200 201

	return 0;
}

202 203 204 205 206 207
static const struct of_device_id of_pwm_leds_match[] = {
	{ .compatible = "pwm-leds", },
	{},
};
MODULE_DEVICE_TABLE(of, of_pwm_leds_match);

208 209 210 211
static struct platform_driver led_pwm_driver = {
	.probe		= led_pwm_probe,
	.driver		= {
		.name	= "leds_pwm",
212
		.of_match_table = of_pwm_leds_match,
213 214 215
	},
};

216
module_platform_driver(led_pwm_driver);
217 218

MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
219 220
MODULE_DESCRIPTION("generic PWM LED driver");
MODULE_LICENSE("GPL v2");
221
MODULE_ALIAS("platform:leds-pwm");