blob: 9660cd21996613b99fcf627318ab9c404d1172d6 [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2011, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12#define pr_fmt(fmt) "%s: " fmt, __func__
13
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/platform_device.h>
17#include <linux/leds.h>
18#include <linux/io.h>
19#include <linux/slab.h>
20#include <linux/err.h>
21#include <linux/delay.h>
22#include <linux/pm.h>
23#include <linux/pm_runtime.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070024#include <linux/module.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070025
26#ifdef CONFIG_HAS_EARLYSUSPEND
27#include <linux/earlysuspend.h>
28
29/* Early-suspend level */
30#define LED_SUSPEND_LEVEL 1
31#endif
32
33#define PDM_DUTY_MAXVAL BIT(16)
34#define PDM_DUTY_REFVAL BIT(15)
35
36struct pdm_led_data {
37 struct led_classdev cdev;
38 void __iomem *perph_base;
39 int pdm_offset;
40#ifdef CONFIG_HAS_EARLYSUSPEND
41 struct early_suspend early_suspend;
42#endif
43};
44
45static void msm_led_brightness_set_percent(struct pdm_led_data *led,
46 int duty_per)
47{
48 u16 duty_val;
49
50 duty_val = PDM_DUTY_REFVAL - ((PDM_DUTY_MAXVAL * duty_per) / 100);
51
52 if (!duty_per)
53 duty_val--;
54
55 writel_relaxed(duty_val, led->perph_base + led->pdm_offset);
56}
57
58static void msm_led_brightness_set(struct led_classdev *led_cdev,
59 enum led_brightness value)
60{
61 struct pdm_led_data *led =
62 container_of(led_cdev, struct pdm_led_data, cdev);
63
64 msm_led_brightness_set_percent(led, (value * 100) / LED_FULL);
65}
66
67#ifdef CONFIG_PM_SLEEP
68static int msm_led_pdm_suspend(struct device *dev)
69{
70 struct pdm_led_data *led = dev_get_drvdata(dev);
71
72 msm_led_brightness_set_percent(led, 0);
73
74 return 0;
75}
76
77#ifdef CONFIG_HAS_EARLYSUSPEND
78static void msm_led_pdm_early_suspend(struct early_suspend *h)
79{
80 struct pdm_led_data *led = container_of(h,
81 struct pdm_led_data, early_suspend);
82
83 msm_led_pdm_suspend(led->cdev.dev->parent);
84}
85
86#endif
87
88static const struct dev_pm_ops msm_led_pdm_pm_ops = {
89#ifndef CONFIG_HAS_EARLYSUSPEND
90 .suspend = msm_led_pdm_suspend,
91#endif
92};
93#endif
94
95static int __devinit msm_pdm_led_probe(struct platform_device *pdev)
96{
97 const struct led_info *pdata = pdev->dev.platform_data;
98 struct pdm_led_data *led;
99 struct resource *res, *ioregion;
100 u32 tcxo_pdm_ctl;
101 int rc;
102
103 if (!pdata) {
104 pr_err("platform data is invalid\n");
105 return -EINVAL;
106 }
107
108 if (pdev->id > 2) {
109 pr_err("pdm id is invalid\n");
110 return -EINVAL;
111 }
112
113 led = kzalloc(sizeof(struct pdm_led_data), GFP_KERNEL);
114 if (!led)
115 return -ENOMEM;
116
117 /* Enable runtime PM ops, start in ACTIVE mode */
118 rc = pm_runtime_set_active(&pdev->dev);
119 if (rc < 0)
120 dev_dbg(&pdev->dev, "unable to set runtime pm state\n");
121 pm_runtime_enable(&pdev->dev);
122
123 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
124 if (!res) {
125 pr_err("get resource failed\n");
126 rc = -EINVAL;
127 goto err_get_res;
128 }
129
130 ioregion = request_mem_region(res->start, resource_size(res),
131 pdev->name);
132 if (!ioregion) {
133 pr_err("request for mem region failed\n");
134 rc = -ENOMEM;
135 goto err_get_res;
136 }
137
138 led->perph_base = ioremap(res->start, resource_size(res));
139 if (!led->perph_base) {
140 pr_err("ioremap failed\n");
141 rc = -ENOMEM;
142 goto err_ioremap;
143 }
144
145 /* Pulse Density Modulation(PDM) ids start with 0 and
146 * every PDM register takes 4 bytes
147 */
148 led->pdm_offset = ((pdev->id) + 1) * 4;
149
150 /* program tcxo_pdm_ctl register to enable pdm*/
151 tcxo_pdm_ctl = readl_relaxed(led->perph_base);
152 tcxo_pdm_ctl |= (1 << pdev->id);
153 writel_relaxed(tcxo_pdm_ctl, led->perph_base);
154
155 /* Start with LED in off state */
156 msm_led_brightness_set_percent(led, 0);
157
158 led->cdev.brightness_set = msm_led_brightness_set;
159 led->cdev.name = pdata->name ? : "leds-msm-pdm";
160
161 rc = led_classdev_register(&pdev->dev, &led->cdev);
162 if (rc) {
163 pr_err("led class registration failed\n");
164 goto err_led_reg;
165 }
166
167#ifdef CONFIG_HAS_EARLYSUSPEND
168 led->early_suspend.level = EARLY_SUSPEND_LEVEL_BLANK_SCREEN +
169 LED_SUSPEND_LEVEL;
170 led->early_suspend.suspend = msm_led_pdm_early_suspend;
171 register_early_suspend(&led->early_suspend);
172#endif
173
174 platform_set_drvdata(pdev, led);
175 return 0;
176
177err_led_reg:
178 iounmap(led->perph_base);
179err_ioremap:
180 release_mem_region(res->start, resource_size(res));
181err_get_res:
182 pm_runtime_set_suspended(&pdev->dev);
183 pm_runtime_disable(&pdev->dev);
184 kfree(led);
185 return rc;
186}
187
188static int __devexit msm_pdm_led_remove(struct platform_device *pdev)
189{
190 struct pdm_led_data *led = platform_get_drvdata(pdev);
191 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
192
193#ifdef CONFIG_HAS_EARLYSUSPEND
194 unregister_early_suspend(&led->early_suspend);
195#endif
196 pm_runtime_set_suspended(&pdev->dev);
197 pm_runtime_disable(&pdev->dev);
198
199 led_classdev_unregister(&led->cdev);
200 msm_led_brightness_set_percent(led, 0);
201 iounmap(led->perph_base);
202 release_mem_region(res->start, resource_size(res));
203 kfree(led);
204
205 return 0;
206}
207
208static struct platform_driver msm_pdm_led_driver = {
209 .probe = msm_pdm_led_probe,
210 .remove = __devexit_p(msm_pdm_led_remove),
211 .driver = {
212 .name = "leds-msm-pdm",
213 .owner = THIS_MODULE,
214#ifdef CONFIG_PM_SLEEP
215 .pm = &msm_led_pdm_pm_ops,
216#endif
217 },
218};
219
220static int __init msm_pdm_led_init(void)
221{
222 return platform_driver_register(&msm_pdm_led_driver);
223}
224module_init(msm_pdm_led_init);
225
226static void __exit msm_pdm_led_exit(void)
227{
228 platform_driver_unregister(&msm_pdm_led_driver);
229}
230module_exit(msm_pdm_led_exit);
231
232MODULE_DESCRIPTION("MSM PDM LEDs driver");
233MODULE_LICENSE("GPL v2");
234MODULE_ALIAS("platform:leds-msm-pdm");