blob: f2e44c7194374f135906fc3e9aa4ac87ca33a6bb [file] [log] [blame]
Luotao Fu41c42ff2009-02-11 13:24:40 -08001/*
2 * linux/drivers/leds-pwm.c
3 *
4 * simple PWM based LED control
5 *
6 * Copyright 2009 Luotao Fu @ Pengutronix (l.fu@pengutronix.de)
7 *
8 * based on leds-gpio.c by Raphael Assenat <raph@8d.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License version 2 as
12 * published by the Free Software Foundation.
13 */
14
15#include <linux/module.h>
16#include <linux/kernel.h>
17#include <linux/init.h>
18#include <linux/platform_device.h>
19#include <linux/fb.h>
20#include <linux/leds.h>
21#include <linux/err.h>
22#include <linux/pwm.h>
23#include <linux/leds_pwm.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Luotao Fu41c42ff2009-02-11 13:24:40 -080025
26struct led_pwm_data {
27 struct led_classdev cdev;
28 struct pwm_device *pwm;
29 unsigned int active_low;
30 unsigned int period;
Luotao Fu41c42ff2009-02-11 13:24:40 -080031};
32
33static void led_pwm_set(struct led_classdev *led_cdev,
34 enum led_brightness brightness)
35{
36 struct led_pwm_data *led_dat =
37 container_of(led_cdev, struct led_pwm_data, cdev);
Lars-Peter Clausene4590622009-11-27 06:17:38 +010038 unsigned int max = led_dat->cdev.max_brightness;
Luotao Fu41c42ff2009-02-11 13:24:40 -080039 unsigned int period = led_dat->period;
40
41 if (brightness == 0) {
42 pwm_config(led_dat->pwm, 0, period);
43 pwm_disable(led_dat->pwm);
44 } else {
45 pwm_config(led_dat->pwm, brightness * period / max, period);
46 pwm_enable(led_dat->pwm);
47 }
48}
49
50static int led_pwm_probe(struct platform_device *pdev)
51{
52 struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
53 struct led_pwm *cur_led;
54 struct led_pwm_data *leds_data, *led_dat;
55 int i, ret = 0;
56
57 if (!pdata)
58 return -EBUSY;
59
Sachin Kamat0b7da742012-07-03 19:47:53 +080060 leds_data = devm_kzalloc(&pdev->dev,
61 sizeof(struct led_pwm_data) * pdata->num_leds,
Luotao Fu41c42ff2009-02-11 13:24:40 -080062 GFP_KERNEL);
63 if (!leds_data)
64 return -ENOMEM;
65
66 for (i = 0; i < pdata->num_leds; i++) {
67 cur_led = &pdata->leds[i];
68 led_dat = &leds_data[i];
69
70 led_dat->pwm = pwm_request(cur_led->pwm_id,
71 cur_led->name);
72 if (IS_ERR(led_dat->pwm)) {
Axel Lind8cc6672011-01-25 15:07:14 -080073 ret = PTR_ERR(led_dat->pwm);
Luotao Fu41c42ff2009-02-11 13:24:40 -080074 dev_err(&pdev->dev, "unable to request PWM %d\n",
75 cur_led->pwm_id);
76 goto err;
77 }
78
79 led_dat->cdev.name = cur_led->name;
80 led_dat->cdev.default_trigger = cur_led->default_trigger;
81 led_dat->active_low = cur_led->active_low;
Luotao Fu41c42ff2009-02-11 13:24:40 -080082 led_dat->period = cur_led->pwm_period_ns;
83 led_dat->cdev.brightness_set = led_pwm_set;
84 led_dat->cdev.brightness = LED_OFF;
Lars-Peter Clausene4590622009-11-27 06:17:38 +010085 led_dat->cdev.max_brightness = cur_led->max_brightness;
Luotao Fu41c42ff2009-02-11 13:24:40 -080086 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
87
88 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
89 if (ret < 0) {
90 pwm_free(led_dat->pwm);
91 goto err;
92 }
93 }
94
95 platform_set_drvdata(pdev, leds_data);
96
97 return 0;
98
99err:
100 if (i > 0) {
101 for (i = i - 1; i >= 0; i--) {
102 led_classdev_unregister(&leds_data[i].cdev);
103 pwm_free(leds_data[i].pwm);
104 }
105 }
106
Luotao Fu41c42ff2009-02-11 13:24:40 -0800107 return ret;
108}
109
110static int __devexit led_pwm_remove(struct platform_device *pdev)
111{
112 int i;
113 struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
114 struct led_pwm_data *leds_data;
115
116 leds_data = platform_get_drvdata(pdev);
117
118 for (i = 0; i < pdata->num_leds; i++) {
119 led_classdev_unregister(&leds_data[i].cdev);
120 pwm_free(leds_data[i].pwm);
121 }
122
Luotao Fu41c42ff2009-02-11 13:24:40 -0800123 return 0;
124}
125
126static struct platform_driver led_pwm_driver = {
127 .probe = led_pwm_probe,
128 .remove = __devexit_p(led_pwm_remove),
129 .driver = {
130 .name = "leds_pwm",
131 .owner = THIS_MODULE,
132 },
133};
134
Axel Lin892a8842012-01-10 15:09:24 -0800135module_platform_driver(led_pwm_driver);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800136
137MODULE_AUTHOR("Luotao Fu <l.fu@pengutronix.de>");
138MODULE_DESCRIPTION("PWM LED driver for PXA");
139MODULE_LICENSE("GPL");
140MODULE_ALIAS("platform:leds-pwm");