summaryrefslogtreecommitdiff
path: root/drivers/leds/leds-mxs-pwm.c
blob: c76821770446e1989c923b9173a815bb9cb6f2fc (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
191
192
/*
 * Freescale MXS PWM LED driver
 *
 * Author: Drew Benedetti <drewb@embeddedalley.com>
 *
 * Copyright 2008-2010 Freescale Semiconductor, Inc.
 * 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/io.h>
#include <linux/clk.h>

#include <mach/hardware.h>
#include <mach/system.h>
#include <mach/device.h>
#include <mach/regs-pwm.h>


/*
 * PWM enables are the lowest bits of HW_PWM_CTRL register
 */
#define BM_PWM_CTRL_PWM_ENABLE	((1<<(CONFIG_MXS_PWM_CHANNELS)) - 1)
#define BF_PWM_CTRL_PWM_ENABLE(n) ((1<<(n)) & BM_PWM_CTRL_PWM_ENABLE)

#define BF_PWM_PERIODn_SETTINGS					\
		(BF_PWM_PERIODn_CDIV(5) | /* divide by 64 */ 	\
		BF_PWM_PERIODn_INACTIVE_STATE(3) | /* low */ 	\
		BF_PWM_PERIODn_ACTIVE_STATE(2) | /* high */ 	\
		BF_PWM_PERIODn_PERIOD(LED_FULL)) /* 255 cycles */

struct mxs_pwm_leds {
	struct clk *pwm_clk;
	unsigned int base;
	unsigned int led_num;
	struct mxs_pwm_led *leds;
};

static struct mxs_pwm_leds leds;

static void mxs_pwm_led_brightness_set(struct led_classdev *pled,
					    enum led_brightness value)
{
	struct mxs_pwm_led *pwm_led;

	pwm_led = container_of(pled, struct mxs_pwm_led, dev);

	if (pwm_led->pwm < CONFIG_MXS_PWM_CHANNELS) {
		__raw_writel(BF_PWM_CTRL_PWM_ENABLE(pwm_led->pwm),
			     leds.base + HW_PWM_CTRL_CLR);
		__raw_writel(BF_PWM_ACTIVEn_INACTIVE(LED_FULL) |
				BF_PWM_ACTIVEn_ACTIVE(value),
			     leds.base + HW_PWM_ACTIVEn(pwm_led->pwm));
		__raw_writel(BF_PWM_PERIODn_SETTINGS,
			     leds.base + HW_PWM_PERIODn(pwm_led->pwm));
		__raw_writel(BF_PWM_CTRL_PWM_ENABLE(pwm_led->pwm),
			     leds.base + HW_PWM_CTRL_SET);
	}
}

static int __devinit mxs_pwm_led_probe(struct platform_device *pdev)
{
	struct mxs_pwm_leds_plat_data *plat_data;
	struct resource *res;
	struct led_classdev *led;
	unsigned int pwmn;
	int leds_in_use = 0, rc = 0;
	int i;

	plat_data = (struct mxs_pwm_leds_plat_data *)pdev->dev.platform_data;
	if (plat_data == NULL)
		return -ENODEV;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (res == NULL)
		return -ENODEV;
	leds.base = (unsigned int)IO_ADDRESS(res->start);

	mxs_reset_block((void __iomem *)leds.base, 1);

	leds.led_num = plat_data->num;
	if (leds.led_num <= 0 || leds.led_num > CONFIG_MXS_PWM_CHANNELS)
		return -EFAULT;
	leds.leds = plat_data->leds;
	if (leds.leds == NULL)
		return -EFAULT;

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

	clk_enable(leds.pwm_clk);

	for (i = 0; i < leds.led_num; i++) {
		pwmn = leds.leds[i].pwm;
		if (pwmn >= CONFIG_MXS_PWM_CHANNELS) {
			dev_err(&pdev->dev,
				"[led-pwm%d]:PWM %d doesn't exist\n",
				i, pwmn);
			continue;
		}
		led = &(leds.leds[i].dev);
		led->name = leds.leds[i].name;
		led->brightness = LED_HALF;
		led->flags = 0;
		led->brightness_set = mxs_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",
				i, rc);
			continue;
		}

		leds_in_use++;

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

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

static int __devexit mxs_pwm_led_remove(struct platform_device *pdev)
{
	int i;
	unsigned int pwm;
	for (i = 0; i < leds.led_num; i++) {
		pwm = leds.leds[i].pwm;
		__raw_writel(BF_PWM_CTRL_PWM_ENABLE(pwm),
			     leds.base + HW_PWM_CTRL_CLR);
		__raw_writel(BF_PWM_ACTIVEn_INACTIVE(0) |
				BF_PWM_ACTIVEn_ACTIVE(0),
			     leds.base + HW_PWM_ACTIVEn(pwm));
		__raw_writel(BF_PWM_PERIODn_SETTINGS,
			     leds.base + HW_PWM_PERIODn(pwm));
		led_classdev_unregister(&leds.leds[i].dev);
	}

	clk_disable(leds.pwm_clk);
	clk_put(leds.pwm_clk);

	return 0;
}


static struct platform_driver mxs_pwm_led_driver = {
	.probe   = mxs_pwm_led_probe,
	.remove  = __devexit_p(mxs_pwm_led_remove),
	.driver  = {
		.name = "mxs-leds",
	},
};

static int __init mxs_pwm_led_init(void)
{
	return platform_driver_register(&mxs_pwm_led_driver);
}

static void __exit mxs_pwm_led_exit(void)
{
	platform_driver_unregister(&mxs_pwm_led_driver);
}

module_init(mxs_pwm_led_init);
module_exit(mxs_pwm_led_exit);

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