summaryrefslogtreecommitdiff
path: root/drivers/leds/leds-stmp378x-pwm.c
blob: f0865db4eb9017b8a856d5ea48bf72d77077794f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Freescale STMP378X PWM LED driver
 *
 * Author: Drew Benedetti <drewb@embeddedalley.com>
 *
 * Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
 * Copyright 2008 Embedded Alley Solutions, Inc All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/leds.h>
#include <linux/err.h>
#include <linux/clk.h>
#include <mach/hardware.h>
#include <mach/regs-pwm.h>
#include <mach/regs-clkctrl.h>
#include <mach/pwm-led.h>
#include <mach/stmp3xxx.h>

/* Up to 5 PWM lines are available. */
#define PWM_MAX 5

/* PWM enables are the lowest PWM_MAX bits of HW_PWM_CTRL register */
#define BM_PWM_CTRL_PWM_ENABLE(n)	((1<<(n)) & ((1<<(PWM_MAX))-1))
#define BF_PWM_PERIODn_SETTINGS					\
		(BF_PWM_PERIODn_CDIV(5) | /* divide by 64 */ 	\
		BF_PWM_PERIODn_INACTIVE_STATE(2) | /* low */ 	\
		BF_PWM_PERIODn_ACTIVE_STATE(3) | /* high */ 	\
		BF_PWM_PERIODn_PERIOD(LED_FULL)) /* 255 cycles */

struct stmp378x_led {
	struct led_classdev led_dev;
	int in_use;
};

static struct stmp378x_led leds[PWM_MAX];

static struct clk *pwm_clk;

static void stmp378x_pwm_led_brightness_set(struct led_classdev *pled,
					    enum led_brightness value)
{
	unsigned int pwmn;

	pwmn = container_of(pled, struct stmp378x_led, led_dev) - leds;

	if (pwmn < PWM_MAX && leds[pwmn].in_use) {
		HW_PWM_CTRL_CLR(BM_PWM_CTRL_PWM_ENABLE(pwmn));
		HW_PWM_ACTIVEn_WR(pwmn, BF_PWM_ACTIVEn_INACTIVE(value) |
				BF_PWM_ACTIVEn_ACTIVE(0));
		HW_PWM_PERIODn_WR(pwmn, BF_PWM_PERIODn_SETTINGS);
		HW_PWM_CTRL_SET(BM_PWM_CTRL_PWM_ENABLE(pwmn));
	}
}

static int stmp378x_pwm_led_probe(struct platform_device *pdev)
{
	struct led_classdev *led;
	unsigned int pwmn;
	int leds_in_use = 0, rc = 0;
	int i;

	stmp3xxx_reset_block(REGS_PWM_BASE, 1);

	pwm_clk = clk_get(&pdev->dev, "pwm");
	if (IS_ERR(pwm_clk)) {
		rc = PTR_ERR(pwm_clk);
		return rc;
	}

	clk_enable(pwm_clk);

	for (i = 0; i < pdev->num_resources; i++) {

		if (pdev->resource[i].flags & IORESOURCE_DISABLED)
			continue;

		pwmn = pdev->resource[i].start;
		if (pwmn >= PWM_MAX) {
			dev_err(&pdev->dev, "PWM %d doesn't exist\n", pwmn);
			continue;
		}

		rc = pwm_led_pinmux_request(pwmn, "stmp378x_pwm_led");
		if (rc) {
			dev_err(&pdev->dev,
				"PWM %d is not available (err=%d)\n",
				pwmn, rc);
			continue;
		}

		led = &leds[pwmn].led_dev;

		led->flags = pdev->resource[i].flags;
		led->name = pdev->resource[i].name;
		led->brightness = LED_HALF;
		led->flags = 0;
		led->brightness_set = stmp378x_pwm_led_brightness_set;
		led->default_trigger = 0;

		rc = led_classdev_register(&pdev->dev, led);
		if (rc < 0) {
			dev_err(&pdev->dev,
				"Unable to register LED device %d (err=%d)\n",
				pwmn, rc);
			pwm_led_pinmux_free(pwmn, "stmp378x_pwm_led");
			continue;
		}

		/* PWM LED is available now */
		leds[pwmn].in_use = !0;
		leds_in_use++;

		/* Set default brightness */
		stmp378x_pwm_led_brightness_set(led, LED_HALF);
	}

	if (leds_in_use == 0) {
		dev_info(&pdev->dev, "No PWM LEDs available\n");
		clk_disable(pwm_clk);
		clk_put(pwm_clk);
		return -ENODEV;
	}

	return 0;
}

static int stmp378x_pwm_led_remove(struct platform_device *pdev)
{
	unsigned int pwmn;

	for (pwmn = 0; pwmn < PWM_MAX; pwmn++) {

		if (!leds[pwmn].in_use)
			continue;

		/* Disable LED */
		HW_PWM_CTRL_CLR(BM_PWM_CTRL_PWM_ENABLE(pwmn));
		HW_PWM_ACTIVEn_WR(pwmn, BF_PWM_ACTIVEn_INACTIVE(0) |
				BF_PWM_ACTIVEn_ACTIVE(0));
		HW_PWM_PERIODn_WR(pwmn, BF_PWM_PERIODn_SETTINGS);

		led_classdev_unregister(&leds[pwmn].led_dev);
		pwm_led_pinmux_free(pwmn, "stmp378x_pwm_led");

		leds[pwmn].led_dev.name = 0;
		leds[pwmn].in_use = 0;
	}

	clk_disable(pwm_clk);
	clk_put(pwm_clk);

	return 0;
}


static struct platform_driver stmp378x_pwm_led_driver = {
	.probe   = stmp378x_pwm_led_probe,
	.remove  = stmp378x_pwm_led_remove,
	.driver  = {
		.name = "stmp378x-pwm-led",
	},
};

static int __init stmp378x_pwm_led_init(void)
{
	return platform_driver_register(&stmp378x_pwm_led_driver);
}

static void __exit stmp378x_pwm_led_exit(void)
{
	platform_driver_unregister(&stmp378x_pwm_led_driver);
}

module_init(stmp378x_pwm_led_init);
module_exit(stmp378x_pwm_led_exit);

MODULE_AUTHOR("Drew Benedetti <drewb@embeddedalley.com>");
MODULE_DESCRIPTION("STMP378X PWM LED driver");
MODULE_LICENSE("GPL");