blob: 60d46815235745ef402860c040b381ce368c19a0 [file] [log] [blame]
Tingting Yangd09bf672013-06-13 21:08:58 +08001
2/* Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/leds.h>
18#include <linux/platform_device.h>
19#include <linux/of_gpio.h>
20#include <linux/gpio.h>
21#include <linux/of.h>
22#include <linux/printk.h>
23
24#define LED_GPIO_FLASH_DRIVER_NAME "qcom,leds-gpio-flash"
25#define LED_TRIGGER_DEFAULT "none"
26
27struct led_gpio_flash_data {
28 int flash_en;
29 int flash_now;
30 int brightness;
31 struct led_classdev cdev;
32};
33
34static struct of_device_id led_gpio_flash_of_match[] = {
35 {.compatible = LED_GPIO_FLASH_DRIVER_NAME,},
36 {},
37};
38
39static void led_gpio_brightness_set(struct led_classdev *led_cdev,
40 enum led_brightness value)
41{
42 int rc = 0;
43 struct led_gpio_flash_data *flash_led =
44 container_of(led_cdev, struct led_gpio_flash_data, cdev);
45
46 int brightness = value;
47 int flash_en = 0, flash_now = 0;
48
49 if (brightness > LED_HALF) {
50 flash_en = 0;
51 flash_now = 1;
52 } else if (brightness > LED_OFF) {
53 flash_en = 1;
54 flash_now = 0;
55 } else {
56 flash_en = 0;
57 flash_now = 0;
58 }
59
60 rc = gpio_direction_output(flash_led->flash_en, flash_en);
61 if (rc) {
62 pr_err("%s: Failed to set gpio %d\n", __func__,
63 flash_led->flash_en);
64 goto err;
65 }
66 rc = gpio_direction_output(flash_led->flash_now, flash_now);
67 if (rc) {
68 pr_err("%s: Failed to set gpio %d\n", __func__,
69 flash_led->flash_now);
70 goto err;
71 }
72
73 flash_led->brightness = brightness;
74err:
75 return;
76}
77
78static enum led_brightness led_gpio_brightness_get(struct led_classdev
79 *led_cdev)
80{
81 struct led_gpio_flash_data *flash_led =
82 container_of(led_cdev, struct led_gpio_flash_data, cdev);
83 return flash_led->brightness;
84}
85
86int __devinit led_gpio_flash_probe(struct platform_device *pdev)
87{
88 int rc = 0;
89 const char *temp_str;
90 struct led_gpio_flash_data *flash_led = NULL;
91 struct device_node *node = pdev->dev.of_node;
92
93 flash_led = devm_kzalloc(&pdev->dev, sizeof(struct led_gpio_flash_data),
94 GFP_KERNEL);
95 if (flash_led == NULL) {
96 dev_err(&pdev->dev, "%s:%d Unable to allocate memory\n",
97 __func__, __LINE__);
98 return -ENOMEM;
99 }
100
101 flash_led->cdev.default_trigger = LED_TRIGGER_DEFAULT;
102 rc = of_property_read_string(node, "linux,default-trigger", &temp_str);
103 if (!rc)
104 flash_led->cdev.default_trigger = temp_str;
105
106 flash_led->flash_en = of_get_named_gpio(node, "qcom,flash-en", 0);
107
108 if (flash_led->flash_en < 0) {
109 dev_err(&pdev->dev,
110 "Looking up %s property in node %s failed. rc = %d\n",
111 "flash-en", node->full_name, flash_led->flash_en);
112 goto error;
113 } else {
114 rc = gpio_request(flash_led->flash_en, "FLASH_EN");
115 if (rc) {
116 dev_err(&pdev->dev,
117 "%s: Failed to request gpio %d,rc = %d\n",
118 __func__, flash_led->flash_en, rc);
119
120 goto error;
121 }
122 }
123
124 flash_led->flash_now = of_get_named_gpio(node, "qcom,flash-now", 0);
125 if (flash_led->flash_now < 0) {
126 dev_err(&pdev->dev,
127 "Looking up %s property in node %s failed. rc = %d\n",
128 "flash-now", node->full_name, flash_led->flash_now);
129 goto error;
130 } else {
131 rc = gpio_request(flash_led->flash_now, "FLASH_NOW");
132 if (rc) {
133 dev_err(&pdev->dev,
134 "%s: Failed to request gpio %d,rc = %d\n",
135 __func__, flash_led->flash_now, rc);
136
137 goto error;
138 }
139 }
140
141 gpio_tlmm_config(GPIO_CFG(flash_led->flash_en, 0,
142 GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL,
143 GPIO_CFG_2MA), GPIO_CFG_ENABLE);
144 gpio_tlmm_config(GPIO_CFG(flash_led->flash_now, 0,
145 GPIO_CFG_OUTPUT, GPIO_CFG_NO_PULL,
146 GPIO_CFG_2MA), GPIO_CFG_ENABLE);
147
148 rc = of_property_read_string(node, "linux,name", &flash_led->cdev.name);
149 if (rc) {
150 dev_err(&pdev->dev, "%s: Failed to read linux name. rc = %d\n",
151 __func__, rc);
152 goto error;
153 }
154
155 platform_set_drvdata(pdev, flash_led);
156 flash_led->cdev.max_brightness = LED_FULL;
157 flash_led->cdev.brightness_set = led_gpio_brightness_set;
158 flash_led->cdev.brightness_get = led_gpio_brightness_get;
159
160 rc = led_classdev_register(&pdev->dev, &flash_led->cdev);
161 if (rc) {
162 dev_err(&pdev->dev, "%s: Failed to register led dev. rc = %d\n",
163 __func__, rc);
164 goto error;
165 }
166 return 0;
167
168error:
169 devm_kfree(&pdev->dev, flash_led);
170 return rc;
171}
172
173int __devexit led_gpio_flash_remove(struct platform_device *pdev)
174{
175 struct led_gpio_flash_data *flash_led =
176 (struct led_gpio_flash_data *)platform_get_drvdata(pdev);
177
178 led_classdev_unregister(&flash_led->cdev);
179 devm_kfree(&pdev->dev, flash_led);
180 return 0;
181}
182
183static struct platform_driver led_gpio_flash_driver = {
184 .probe = led_gpio_flash_probe,
185 .remove = __devexit_p(led_gpio_flash_remove),
186 .driver = {
187 .name = LED_GPIO_FLASH_DRIVER_NAME,
188 .owner = THIS_MODULE,
189 .of_match_table = led_gpio_flash_of_match,
190 }
191};
192
193static int __init led_gpio_flash_init(void)
194{
195 return platform_driver_register(&led_gpio_flash_driver);
196}
197
198static void __exit led_gpio_flash_exit(void)
199{
200 return platform_driver_unregister(&led_gpio_flash_driver);
201}
202
203late_initcall(led_gpio_flash_init);
204module_exit(led_gpio_flash_exit);
205
206MODULE_DESCRIPTION("QCOM GPIO LEDs driver");
207MODULE_LICENSE("GPL v2");
208MODULE_ALIAS("leds:leds-msm-gpio-flash");