blob: dfb8bd390125357fd6e1660d4257ba564a5ab72c [file] [log] [blame]
Vasant Hegde84ad6e52015-08-19 22:19:54 +05301/*
2 * PowerNV LED Driver
3 *
4 * Copyright IBM Corp. 2015
5 *
6 * Author: Vasant Hegde <hegdevasant@linux.vnet.ibm.com>
7 * Author: Anshuman Khandual <khandual@linux.vnet.ibm.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 */
14
15#include <linux/leds.h>
16#include <linux/module.h>
17#include <linux/of.h>
18#include <linux/platform_device.h>
19#include <linux/slab.h>
20#include <linux/types.h>
21
22#include <asm/opal.h>
23
24/* Map LED type to description. */
25struct led_type_map {
26 const int type;
27 const char *desc;
28};
29static const struct led_type_map led_type_map[] = {
Michael Ellerman5d53be72015-08-22 09:33:55 +100030 {OPAL_SLOT_LED_TYPE_ID, "identify"},
31 {OPAL_SLOT_LED_TYPE_FAULT, "fault"},
32 {OPAL_SLOT_LED_TYPE_ATTN, "attention"},
Vasant Hegde84ad6e52015-08-19 22:19:54 +053033 {-1, NULL},
34};
35
36struct powernv_led_common {
37 /*
38 * By default unload path resets all the LEDs. But on PowerNV
39 * platform we want to retain LED state across reboot as these
40 * are controlled by firmware. Also service processor can modify
41 * the LEDs independent of OS. Hence avoid resetting LEDs in
42 * unload path.
43 */
44 bool led_disabled;
45
46 /* Max supported LED type */
47 __be64 max_led_type;
48
49 /* glabal lock */
50 struct mutex lock;
51};
52
53/* PowerNV LED data */
54struct powernv_led_data {
55 struct led_classdev cdev;
56 char *loc_code; /* LED location code */
57 int led_type; /* OPAL_SLOT_LED_TYPE_* */
58
59 struct powernv_led_common *common;
60};
61
62
63/* Returns OPAL_SLOT_LED_TYPE_* for given led type string */
64static int powernv_get_led_type(const char *led_type_desc)
65{
66 int i;
67
68 for (i = 0; i < ARRAY_SIZE(led_type_map); i++)
69 if (!strcmp(led_type_map[i].desc, led_type_desc))
70 return led_type_map[i].type;
71
72 return -1;
73}
74
75/*
76 * This commits the state change of the requested LED through an OPAL call.
77 * This function is called from work queue task context when ever it gets
78 * scheduled. This function can sleep at opal_async_wait_response call.
79 */
Jacek Anaszewskif5db6b82015-11-20 16:38:50 +010080static int powernv_led_set(struct powernv_led_data *powernv_led,
Vasant Hegde84ad6e52015-08-19 22:19:54 +053081 enum led_brightness value)
82{
83 int rc, token;
84 u64 led_mask, led_value = 0;
85 __be64 max_type;
86 struct opal_msg msg;
87 struct device *dev = powernv_led->cdev.dev;
88 struct powernv_led_common *powernv_led_common = powernv_led->common;
89
90 /* Prepare for the OPAL call */
91 max_type = powernv_led_common->max_led_type;
92 led_mask = OPAL_SLOT_LED_STATE_ON << powernv_led->led_type;
93 if (value)
94 led_value = led_mask;
95
96 /* OPAL async call */
97 token = opal_async_get_token_interruptible();
98 if (token < 0) {
99 if (token != -ERESTARTSYS)
100 dev_err(dev, "%s: Couldn't get OPAL async token\n",
101 __func__);
Jacek Anaszewskif5db6b82015-11-20 16:38:50 +0100102 return token;
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530103 }
104
105 rc = opal_leds_set_ind(token, powernv_led->loc_code,
106 led_mask, led_value, &max_type);
107 if (rc != OPAL_ASYNC_COMPLETION) {
108 dev_err(dev, "%s: OPAL set LED call failed for %s [rc=%d]\n",
109 __func__, powernv_led->loc_code, rc);
110 goto out_token;
111 }
112
113 rc = opal_async_wait_response(token, &msg);
114 if (rc) {
115 dev_err(dev,
116 "%s: Failed to wait for the async response [rc=%d]\n",
117 __func__, rc);
118 goto out_token;
119 }
120
121 rc = be64_to_cpu(msg.params[1]);
122 if (rc != OPAL_SUCCESS)
123 dev_err(dev, "%s : OAPL async call returned failed [rc=%d]\n",
124 __func__, rc);
125
126out_token:
127 opal_async_release_token(token);
Jacek Anaszewskif5db6b82015-11-20 16:38:50 +0100128 return rc;
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530129}
130
131/*
132 * This function fetches the LED state for a given LED type for
133 * mentioned LED classdev structure.
134 */
135static enum led_brightness powernv_led_get(struct powernv_led_data *powernv_led)
136{
137 int rc;
138 __be64 mask, value, max_type;
139 u64 led_mask, led_value;
140 struct device *dev = powernv_led->cdev.dev;
141 struct powernv_led_common *powernv_led_common = powernv_led->common;
142
143 /* Fetch all LED status */
144 mask = cpu_to_be64(0);
145 value = cpu_to_be64(0);
146 max_type = powernv_led_common->max_led_type;
147
148 rc = opal_leds_get_ind(powernv_led->loc_code,
149 &mask, &value, &max_type);
150 if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
151 dev_err(dev, "%s: OPAL get led call failed [rc=%d]\n",
152 __func__, rc);
153 return LED_OFF;
154 }
155
156 led_mask = be64_to_cpu(mask);
157 led_value = be64_to_cpu(value);
158
159 /* LED status available */
160 if (!((led_mask >> powernv_led->led_type) & OPAL_SLOT_LED_STATE_ON)) {
161 dev_err(dev, "%s: LED status not available for %s\n",
162 __func__, powernv_led->cdev.name);
163 return LED_OFF;
164 }
165
166 /* LED status value */
167 if ((led_value >> powernv_led->led_type) & OPAL_SLOT_LED_STATE_ON)
168 return LED_FULL;
169
170 return LED_OFF;
171}
172
173/*
174 * LED classdev 'brightness_get' function. This schedules work
175 * to update LED state.
176 */
Jacek Anaszewskif5db6b82015-11-20 16:38:50 +0100177static int powernv_brightness_set(struct led_classdev *led_cdev,
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530178 enum led_brightness value)
179{
180 struct powernv_led_data *powernv_led =
181 container_of(led_cdev, struct powernv_led_data, cdev);
182 struct powernv_led_common *powernv_led_common = powernv_led->common;
Jacek Anaszewskif5db6b82015-11-20 16:38:50 +0100183 int rc;
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530184
185 /* Do not modify LED in unload path */
186 if (powernv_led_common->led_disabled)
Jacek Anaszewskif5db6b82015-11-20 16:38:50 +0100187 return 0;
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530188
189 mutex_lock(&powernv_led_common->lock);
Jacek Anaszewskif5db6b82015-11-20 16:38:50 +0100190 rc = powernv_led_set(powernv_led, value);
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530191 mutex_unlock(&powernv_led_common->lock);
Jacek Anaszewskif5db6b82015-11-20 16:38:50 +0100192
193 return rc;
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530194}
195
196/* LED classdev 'brightness_get' function */
197static enum led_brightness powernv_brightness_get(struct led_classdev *led_cdev)
198{
199 struct powernv_led_data *powernv_led =
200 container_of(led_cdev, struct powernv_led_data, cdev);
201
202 return powernv_led_get(powernv_led);
203}
204
205/*
206 * This function registers classdev structure for any given type of LED on
207 * a given child LED device node.
208 */
209static int powernv_led_create(struct device *dev,
210 struct powernv_led_data *powernv_led,
211 const char *led_type_desc)
212{
213 int rc;
214
215 /* Make sure LED type is supported */
216 powernv_led->led_type = powernv_get_led_type(led_type_desc);
217 if (powernv_led->led_type == -1) {
218 dev_warn(dev, "%s: No support for led type : %s\n",
219 __func__, led_type_desc);
220 return -EINVAL;
221 }
222
223 /* Create the name for classdev */
224 powernv_led->cdev.name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s",
225 powernv_led->loc_code,
226 led_type_desc);
227 if (!powernv_led->cdev.name) {
228 dev_err(dev,
229 "%s: Memory allocation failed for classdev name\n",
230 __func__);
231 return -ENOMEM;
232 }
233
Jacek Anaszewskif5db6b82015-11-20 16:38:50 +0100234 powernv_led->cdev.brightness_set_blocking = powernv_brightness_set;
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530235 powernv_led->cdev.brightness_get = powernv_brightness_get;
236 powernv_led->cdev.brightness = LED_OFF;
237 powernv_led->cdev.max_brightness = LED_FULL;
238
239 /* Register the classdev */
240 rc = devm_led_classdev_register(dev, &powernv_led->cdev);
241 if (rc) {
242 dev_err(dev, "%s: Classdev registration failed for %s\n",
243 __func__, powernv_led->cdev.name);
244 }
245
246 return rc;
247}
248
249/* Go through LED device tree node and register LED classdev structure */
250static int powernv_led_classdev(struct platform_device *pdev,
251 struct device_node *led_node,
252 struct powernv_led_common *powernv_led_common)
253{
254 const char *cur = NULL;
255 int rc = -1;
256 struct property *p;
257 struct device_node *np;
258 struct powernv_led_data *powernv_led;
259 struct device *dev = &pdev->dev;
260
261 for_each_child_of_node(led_node, np) {
262 p = of_find_property(np, "led-types", NULL);
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530263
264 while ((cur = of_prop_next_string(p, cur)) != NULL) {
265 powernv_led = devm_kzalloc(dev, sizeof(*powernv_led),
266 GFP_KERNEL);
Julia Lawalldde4afe2015-10-24 16:42:27 +0200267 if (!powernv_led) {
268 of_node_put(np);
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530269 return -ENOMEM;
Julia Lawalldde4afe2015-10-24 16:42:27 +0200270 }
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530271
272 powernv_led->common = powernv_led_common;
273 powernv_led->loc_code = (char *)np->name;
274
275 rc = powernv_led_create(dev, powernv_led, cur);
Julia Lawalldde4afe2015-10-24 16:42:27 +0200276 if (rc) {
277 of_node_put(np);
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530278 return rc;
Julia Lawalldde4afe2015-10-24 16:42:27 +0200279 }
Vasant Hegde84ad6e52015-08-19 22:19:54 +0530280 } /* while end */
281 }
282
283 return rc;
284}
285
286/* Platform driver probe */
287static int powernv_led_probe(struct platform_device *pdev)
288{
289 struct device_node *led_node;
290 struct powernv_led_common *powernv_led_common;
291 struct device *dev = &pdev->dev;
292
293 led_node = of_find_node_by_path("/ibm,opal/leds");
294 if (!led_node) {
295 dev_err(dev, "%s: LED parent device node not found\n",
296 __func__);
297 return -EINVAL;
298 }
299
300 powernv_led_common = devm_kzalloc(dev, sizeof(*powernv_led_common),
301 GFP_KERNEL);
302 if (!powernv_led_common)
303 return -ENOMEM;
304
305 mutex_init(&powernv_led_common->lock);
306 powernv_led_common->max_led_type = cpu_to_be64(OPAL_SLOT_LED_TYPE_MAX);
307
308 platform_set_drvdata(pdev, powernv_led_common);
309
310 return powernv_led_classdev(pdev, led_node, powernv_led_common);
311}
312
313/* Platform driver remove */
314static int powernv_led_remove(struct platform_device *pdev)
315{
316 struct powernv_led_common *powernv_led_common;
317
318 /* Disable LED operation */
319 powernv_led_common = platform_get_drvdata(pdev);
320 powernv_led_common->led_disabled = true;
321
322 /* Destroy lock */
323 mutex_destroy(&powernv_led_common->lock);
324
325 dev_info(&pdev->dev, "PowerNV led module unregistered\n");
326 return 0;
327}
328
329/* Platform driver property match */
330static const struct of_device_id powernv_led_match[] = {
331 {
332 .compatible = "ibm,opal-v3-led",
333 },
334 {},
335};
336MODULE_DEVICE_TABLE(of, powernv_led_match);
337
338static struct platform_driver powernv_led_driver = {
339 .probe = powernv_led_probe,
340 .remove = powernv_led_remove,
341 .driver = {
342 .name = "powernv-led-driver",
343 .of_match_table = powernv_led_match,
344 },
345};
346
347module_platform_driver(powernv_led_driver);
348
349MODULE_LICENSE("GPL v2");
350MODULE_DESCRIPTION("PowerNV LED driver");
351MODULE_AUTHOR("Vasant Hegde <hegdevasant@linux.vnet.ibm.com>");