Loading...
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 | // SPDX-License-Identifier: GPL-2.0-or-later /* * PowerNV LED Driver * * Copyright IBM Corp. 2015 * * Author: Vasant Hegde <hegdevasant@linux.vnet.ibm.com> * Author: Anshuman Khandual <khandual@linux.vnet.ibm.com> */ #include <linux/leds.h> #include <linux/module.h> #include <linux/of.h> #include <linux/platform_device.h> #include <linux/slab.h> #include <linux/types.h> #include <asm/opal.h> /* Map LED type to description. */ struct led_type_map { const int type; const char *desc; }; static const struct led_type_map led_type_map[] = { {OPAL_SLOT_LED_TYPE_ID, "identify"}, {OPAL_SLOT_LED_TYPE_FAULT, "fault"}, {OPAL_SLOT_LED_TYPE_ATTN, "attention"}, {-1, NULL}, }; struct powernv_led_common { /* * By default unload path resets all the LEDs. But on PowerNV * platform we want to retain LED state across reboot as these * are controlled by firmware. Also service processor can modify * the LEDs independent of OS. Hence avoid resetting LEDs in * unload path. */ bool led_disabled; /* Max supported LED type */ __be64 max_led_type; /* glabal lock */ struct mutex lock; }; /* PowerNV LED data */ struct powernv_led_data { struct led_classdev cdev; char *loc_code; /* LED location code */ int led_type; /* OPAL_SLOT_LED_TYPE_* */ struct powernv_led_common *common; }; /* Returns OPAL_SLOT_LED_TYPE_* for given led type string */ static int powernv_get_led_type(const char *led_type_desc) { int i; for (i = 0; i < ARRAY_SIZE(led_type_map); i++) if (!strcmp(led_type_map[i].desc, led_type_desc)) return led_type_map[i].type; return -1; } /* * This commits the state change of the requested LED through an OPAL call. * This function is called from work queue task context when ever it gets * scheduled. This function can sleep at opal_async_wait_response call. */ static int powernv_led_set(struct powernv_led_data *powernv_led, enum led_brightness value) { int rc, token; u64 led_mask, led_value = 0; __be64 max_type; struct opal_msg msg; struct device *dev = powernv_led->cdev.dev; struct powernv_led_common *powernv_led_common = powernv_led->common; /* Prepare for the OPAL call */ max_type = powernv_led_common->max_led_type; led_mask = OPAL_SLOT_LED_STATE_ON << powernv_led->led_type; if (value) led_value = led_mask; /* OPAL async call */ token = opal_async_get_token_interruptible(); if (token < 0) { if (token != -ERESTARTSYS) dev_err(dev, "%s: Couldn't get OPAL async token\n", __func__); return token; } rc = opal_leds_set_ind(token, powernv_led->loc_code, led_mask, led_value, &max_type); if (rc != OPAL_ASYNC_COMPLETION) { dev_err(dev, "%s: OPAL set LED call failed for %s [rc=%d]\n", __func__, powernv_led->loc_code, rc); goto out_token; } rc = opal_async_wait_response(token, &msg); if (rc) { dev_err(dev, "%s: Failed to wait for the async response [rc=%d]\n", __func__, rc); goto out_token; } rc = opal_get_async_rc(msg); if (rc != OPAL_SUCCESS) dev_err(dev, "%s : OAPL async call returned failed [rc=%d]\n", __func__, rc); out_token: opal_async_release_token(token); return rc; } /* * This function fetches the LED state for a given LED type for * mentioned LED classdev structure. */ static enum led_brightness powernv_led_get(struct powernv_led_data *powernv_led) { int rc; __be64 mask, value, max_type; u64 led_mask, led_value; struct device *dev = powernv_led->cdev.dev; struct powernv_led_common *powernv_led_common = powernv_led->common; /* Fetch all LED status */ mask = cpu_to_be64(0); value = cpu_to_be64(0); max_type = powernv_led_common->max_led_type; rc = opal_leds_get_ind(powernv_led->loc_code, &mask, &value, &max_type); if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) { dev_err(dev, "%s: OPAL get led call failed [rc=%d]\n", __func__, rc); return LED_OFF; } led_mask = be64_to_cpu(mask); led_value = be64_to_cpu(value); /* LED status available */ if (!((led_mask >> powernv_led->led_type) & OPAL_SLOT_LED_STATE_ON)) { dev_err(dev, "%s: LED status not available for %s\n", __func__, powernv_led->cdev.name); return LED_OFF; } /* LED status value */ if ((led_value >> powernv_led->led_type) & OPAL_SLOT_LED_STATE_ON) return LED_FULL; return LED_OFF; } /* * LED classdev 'brightness_get' function. This schedules work * to update LED state. */ static int powernv_brightness_set(struct led_classdev *led_cdev, enum led_brightness value) { struct powernv_led_data *powernv_led = container_of(led_cdev, struct powernv_led_data, cdev); struct powernv_led_common *powernv_led_common = powernv_led->common; int rc; /* Do not modify LED in unload path */ if (powernv_led_common->led_disabled) return 0; mutex_lock(&powernv_led_common->lock); rc = powernv_led_set(powernv_led, value); mutex_unlock(&powernv_led_common->lock); return rc; } /* LED classdev 'brightness_get' function */ static enum led_brightness powernv_brightness_get(struct led_classdev *led_cdev) { struct powernv_led_data *powernv_led = container_of(led_cdev, struct powernv_led_data, cdev); return powernv_led_get(powernv_led); } /* * This function registers classdev structure for any given type of LED on * a given child LED device node. */ static int powernv_led_create(struct device *dev, struct powernv_led_data *powernv_led, const char *led_type_desc) { int rc; /* Make sure LED type is supported */ powernv_led->led_type = powernv_get_led_type(led_type_desc); if (powernv_led->led_type == -1) { dev_warn(dev, "%s: No support for led type : %s\n", __func__, led_type_desc); return -EINVAL; } /* Create the name for classdev */ powernv_led->cdev.name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s", powernv_led->loc_code, led_type_desc); if (!powernv_led->cdev.name) return -ENOMEM; powernv_led->cdev.brightness_set_blocking = powernv_brightness_set; powernv_led->cdev.brightness_get = powernv_brightness_get; powernv_led->cdev.brightness = LED_OFF; powernv_led->cdev.max_brightness = LED_FULL; /* Register the classdev */ rc = devm_led_classdev_register(dev, &powernv_led->cdev); if (rc) { dev_err(dev, "%s: Classdev registration failed for %s\n", __func__, powernv_led->cdev.name); } return rc; } /* Go through LED device tree node and register LED classdev structure */ static int powernv_led_classdev(struct platform_device *pdev, struct device_node *led_node, struct powernv_led_common *powernv_led_common) { const char *cur = NULL; int rc = -1; struct property *p; struct device_node *np; struct powernv_led_data *powernv_led; struct device *dev = &pdev->dev; for_each_available_child_of_node(led_node, np) { p = of_find_property(np, "led-types", NULL); while ((cur = of_prop_next_string(p, cur)) != NULL) { powernv_led = devm_kzalloc(dev, sizeof(*powernv_led), GFP_KERNEL); if (!powernv_led) { of_node_put(np); return -ENOMEM; } powernv_led->common = powernv_led_common; powernv_led->loc_code = (char *)np->name; rc = powernv_led_create(dev, powernv_led, cur); if (rc) { of_node_put(np); return rc; } } /* while end */ } return rc; } /* Platform driver probe */ static int powernv_led_probe(struct platform_device *pdev) { struct device_node *led_node; struct powernv_led_common *powernv_led_common; struct device *dev = &pdev->dev; int rc; led_node = of_find_node_by_path("/ibm,opal/leds"); if (!led_node) { dev_err(dev, "%s: LED parent device node not found\n", __func__); return -EINVAL; } powernv_led_common = devm_kzalloc(dev, sizeof(*powernv_led_common), GFP_KERNEL); if (!powernv_led_common) { rc = -ENOMEM; goto out; } mutex_init(&powernv_led_common->lock); powernv_led_common->max_led_type = cpu_to_be64(OPAL_SLOT_LED_TYPE_MAX); platform_set_drvdata(pdev, powernv_led_common); rc = powernv_led_classdev(pdev, led_node, powernv_led_common); out: of_node_put(led_node); return rc; } /* Platform driver remove */ static int powernv_led_remove(struct platform_device *pdev) { struct powernv_led_common *powernv_led_common; /* Disable LED operation */ powernv_led_common = platform_get_drvdata(pdev); powernv_led_common->led_disabled = true; /* Destroy lock */ mutex_destroy(&powernv_led_common->lock); dev_info(&pdev->dev, "PowerNV led module unregistered\n"); return 0; } /* Platform driver property match */ static const struct of_device_id powernv_led_match[] = { { .compatible = "ibm,opal-v3-led", }, {}, }; MODULE_DEVICE_TABLE(of, powernv_led_match); static struct platform_driver powernv_led_driver = { .probe = powernv_led_probe, .remove = powernv_led_remove, .driver = { .name = "powernv-led-driver", .of_match_table = powernv_led_match, }, }; module_platform_driver(powernv_led_driver); MODULE_LICENSE("GPL v2"); MODULE_DESCRIPTION("PowerNV LED driver"); MODULE_AUTHOR("Vasant Hegde <hegdevasant@linux.vnet.ibm.com>"); |