blob: c767837522fe15a92b01d3157dd0c2832eb93cad [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;
Sachin Kamatdcba9102012-11-25 12:24:55 +053029 unsigned int active_low;
Luotao Fu41c42ff2009-02-11 13:24:40 -080030 unsigned int period;
Luotao Fu41c42ff2009-02-11 13:24:40 -080031};
32
Peter Ujfalusi0f868152012-12-21 01:43:56 -080033struct led_pwm_priv {
34 int num_leds;
35 struct led_pwm_data leds[0];
36};
37
Luotao Fu41c42ff2009-02-11 13:24:40 -080038static void led_pwm_set(struct led_classdev *led_cdev,
39 enum led_brightness brightness)
40{
41 struct led_pwm_data *led_dat =
42 container_of(led_cdev, struct led_pwm_data, cdev);
Lars-Peter Clausene4590622009-11-27 06:17:38 +010043 unsigned int max = led_dat->cdev.max_brightness;
Luotao Fu41c42ff2009-02-11 13:24:40 -080044 unsigned int period = led_dat->period;
45
46 if (brightness == 0) {
47 pwm_config(led_dat->pwm, 0, period);
48 pwm_disable(led_dat->pwm);
49 } else {
50 pwm_config(led_dat->pwm, brightness * period / max, period);
51 pwm_enable(led_dat->pwm);
52 }
53}
54
Peter Ujfalusi0f868152012-12-21 01:43:56 -080055static inline size_t sizeof_pwm_leds_priv(int num_leds)
56{
57 return sizeof(struct led_pwm_priv) +
58 (sizeof(struct led_pwm_data) * num_leds);
59}
60
Luotao Fu41c42ff2009-02-11 13:24:40 -080061static int led_pwm_probe(struct platform_device *pdev)
62{
63 struct led_pwm_platform_data *pdata = pdev->dev.platform_data;
Peter Ujfalusi0f868152012-12-21 01:43:56 -080064 struct led_pwm_priv *priv;
Luotao Fu41c42ff2009-02-11 13:24:40 -080065 int i, ret = 0;
66
67 if (!pdata)
68 return -EBUSY;
69
Peter Ujfalusi0f868152012-12-21 01:43:56 -080070 priv = devm_kzalloc(&pdev->dev, sizeof_pwm_leds_priv(pdata->num_leds),
71 GFP_KERNEL);
72 if (!priv)
Luotao Fu41c42ff2009-02-11 13:24:40 -080073 return -ENOMEM;
74
75 for (i = 0; i < pdata->num_leds; i++) {
Peter Ujfalusi0f868152012-12-21 01:43:56 -080076 struct led_pwm *cur_led = &pdata->leds[i];
77 struct led_pwm_data *led_dat = &priv->leds[i];
Luotao Fu41c42ff2009-02-11 13:24:40 -080078
Peter Ujfalusi9ea6cda2012-12-21 01:43:55 -080079 led_dat->pwm = devm_pwm_get(&pdev->dev, cur_led->name);
Luotao Fu41c42ff2009-02-11 13:24:40 -080080 if (IS_ERR(led_dat->pwm)) {
Axel Lind8cc6672011-01-25 15:07:14 -080081 ret = PTR_ERR(led_dat->pwm);
Peter Ujfalusi9ea6cda2012-12-21 01:43:55 -080082 dev_err(&pdev->dev, "unable to request PWM for %s\n",
83 cur_led->name);
Luotao Fu41c42ff2009-02-11 13:24:40 -080084 goto err;
85 }
86
87 led_dat->cdev.name = cur_led->name;
88 led_dat->cdev.default_trigger = cur_led->default_trigger;
89 led_dat->active_low = cur_led->active_low;
Luotao Fu41c42ff2009-02-11 13:24:40 -080090 led_dat->period = cur_led->pwm_period_ns;
91 led_dat->cdev.brightness_set = led_pwm_set;
92 led_dat->cdev.brightness = LED_OFF;
Lars-Peter Clausene4590622009-11-27 06:17:38 +010093 led_dat->cdev.max_brightness = cur_led->max_brightness;
Luotao Fu41c42ff2009-02-11 13:24:40 -080094 led_dat->cdev.flags |= LED_CORE_SUSPENDRESUME;
95
96 ret = led_classdev_register(&pdev->dev, &led_dat->cdev);
Peter Ujfalusi9ea6cda2012-12-21 01:43:55 -080097 if (ret < 0)
Luotao Fu41c42ff2009-02-11 13:24:40 -080098 goto err;
Luotao Fu41c42ff2009-02-11 13:24:40 -080099 }
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800100 priv->num_leds = pdata->num_leds;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800101
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800102 platform_set_drvdata(pdev, priv);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800103
104 return 0;
105
106err:
107 if (i > 0) {
Peter Ujfalusi9ea6cda2012-12-21 01:43:55 -0800108 for (i = i - 1; i >= 0; i--)
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800109 led_classdev_unregister(&priv->leds[i].cdev);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800110 }
111
Luotao Fu41c42ff2009-02-11 13:24:40 -0800112 return ret;
113}
114
Bill Pemberton678e8a62012-11-19 13:26:00 -0500115static int led_pwm_remove(struct platform_device *pdev)
Luotao Fu41c42ff2009-02-11 13:24:40 -0800116{
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800117 struct led_pwm_priv *priv = platform_get_drvdata(pdev);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800118 int i;
Luotao Fu41c42ff2009-02-11 13:24:40 -0800119
Peter Ujfalusi0f868152012-12-21 01:43:56 -0800120 for (i = 0; i < priv->num_leds; i++)
121 led_classdev_unregister(&priv->leds[i].cdev);
Luotao Fu41c42ff2009-02-11 13:24:40 -0800122
Luotao Fu41c42ff2009-02-11 13:24:40 -0800123 return 0;
124}
125
126static struct platform_driver led_pwm_driver = {
127 .probe = led_pwm_probe,
Bill Pembertondf07cf82012-11-19 13:20:20 -0500128 .remove = led_pwm_remove,
Luotao Fu41c42ff2009-02-11 13:24:40 -0800129 .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");