leds-powernv.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  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. #include <linux/leds.h>
  15. #include <linux/module.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #include <linux/types.h>
  20. #include <asm/opal.h>
  21. /* Map LED type to description. */
  22. struct led_type_map {
  23. const int type;
  24. const char *desc;
  25. };
  26. static const struct led_type_map led_type_map[] = {
  27. {OPAL_SLOT_LED_TYPE_ID, "identify"},
  28. {OPAL_SLOT_LED_TYPE_FAULT, "fault"},
  29. {OPAL_SLOT_LED_TYPE_ATTN, "attention"},
  30. {-1, NULL},
  31. };
  32. struct powernv_led_common {
  33. /*
  34. * By default unload path resets all the LEDs. But on PowerNV
  35. * platform we want to retain LED state across reboot as these
  36. * are controlled by firmware. Also service processor can modify
  37. * the LEDs independent of OS. Hence avoid resetting LEDs in
  38. * unload path.
  39. */
  40. bool led_disabled;
  41. /* Max supported LED type */
  42. __be64 max_led_type;
  43. /* glabal lock */
  44. struct mutex lock;
  45. };
  46. /* PowerNV LED data */
  47. struct powernv_led_data {
  48. struct led_classdev cdev;
  49. char *loc_code; /* LED location code */
  50. int led_type; /* OPAL_SLOT_LED_TYPE_* */
  51. struct powernv_led_common *common;
  52. };
  53. /* Returns OPAL_SLOT_LED_TYPE_* for given led type string */
  54. static int powernv_get_led_type(const char *led_type_desc)
  55. {
  56. int i;
  57. for (i = 0; i < ARRAY_SIZE(led_type_map); i++)
  58. if (!strcmp(led_type_map[i].desc, led_type_desc))
  59. return led_type_map[i].type;
  60. return -1;
  61. }
  62. /*
  63. * This commits the state change of the requested LED through an OPAL call.
  64. * This function is called from work queue task context when ever it gets
  65. * scheduled. This function can sleep at opal_async_wait_response call.
  66. */
  67. static void powernv_led_set(struct powernv_led_data *powernv_led,
  68. enum led_brightness value)
  69. {
  70. int rc, token;
  71. u64 led_mask, led_value = 0;
  72. __be64 max_type;
  73. struct opal_msg msg;
  74. struct device *dev = powernv_led->cdev.dev;
  75. struct powernv_led_common *powernv_led_common = powernv_led->common;
  76. /* Prepare for the OPAL call */
  77. max_type = powernv_led_common->max_led_type;
  78. led_mask = OPAL_SLOT_LED_STATE_ON << powernv_led->led_type;
  79. if (value)
  80. led_value = led_mask;
  81. /* OPAL async call */
  82. token = opal_async_get_token_interruptible();
  83. if (token < 0) {
  84. if (token != -ERESTARTSYS)
  85. dev_err(dev, "%s: Couldn't get OPAL async token\n",
  86. __func__);
  87. return;
  88. }
  89. rc = opal_leds_set_ind(token, powernv_led->loc_code,
  90. led_mask, led_value, &max_type);
  91. if (rc != OPAL_ASYNC_COMPLETION) {
  92. dev_err(dev, "%s: OPAL set LED call failed for %s [rc=%d]\n",
  93. __func__, powernv_led->loc_code, rc);
  94. goto out_token;
  95. }
  96. rc = opal_async_wait_response(token, &msg);
  97. if (rc) {
  98. dev_err(dev,
  99. "%s: Failed to wait for the async response [rc=%d]\n",
  100. __func__, rc);
  101. goto out_token;
  102. }
  103. rc = be64_to_cpu(msg.params[1]);
  104. if (rc != OPAL_SUCCESS)
  105. dev_err(dev, "%s : OAPL async call returned failed [rc=%d]\n",
  106. __func__, rc);
  107. out_token:
  108. opal_async_release_token(token);
  109. }
  110. /*
  111. * This function fetches the LED state for a given LED type for
  112. * mentioned LED classdev structure.
  113. */
  114. static enum led_brightness powernv_led_get(struct powernv_led_data *powernv_led)
  115. {
  116. int rc;
  117. __be64 mask, value, max_type;
  118. u64 led_mask, led_value;
  119. struct device *dev = powernv_led->cdev.dev;
  120. struct powernv_led_common *powernv_led_common = powernv_led->common;
  121. /* Fetch all LED status */
  122. mask = cpu_to_be64(0);
  123. value = cpu_to_be64(0);
  124. max_type = powernv_led_common->max_led_type;
  125. rc = opal_leds_get_ind(powernv_led->loc_code,
  126. &mask, &value, &max_type);
  127. if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
  128. dev_err(dev, "%s: OPAL get led call failed [rc=%d]\n",
  129. __func__, rc);
  130. return LED_OFF;
  131. }
  132. led_mask = be64_to_cpu(mask);
  133. led_value = be64_to_cpu(value);
  134. /* LED status available */
  135. if (!((led_mask >> powernv_led->led_type) & OPAL_SLOT_LED_STATE_ON)) {
  136. dev_err(dev, "%s: LED status not available for %s\n",
  137. __func__, powernv_led->cdev.name);
  138. return LED_OFF;
  139. }
  140. /* LED status value */
  141. if ((led_value >> powernv_led->led_type) & OPAL_SLOT_LED_STATE_ON)
  142. return LED_FULL;
  143. return LED_OFF;
  144. }
  145. /*
  146. * LED classdev 'brightness_get' function. This schedules work
  147. * to update LED state.
  148. */
  149. static void powernv_brightness_set(struct led_classdev *led_cdev,
  150. enum led_brightness value)
  151. {
  152. struct powernv_led_data *powernv_led =
  153. container_of(led_cdev, struct powernv_led_data, cdev);
  154. struct powernv_led_common *powernv_led_common = powernv_led->common;
  155. /* Do not modify LED in unload path */
  156. if (powernv_led_common->led_disabled)
  157. return;
  158. mutex_lock(&powernv_led_common->lock);
  159. powernv_led_set(powernv_led, value);
  160. mutex_unlock(&powernv_led_common->lock);
  161. }
  162. /* LED classdev 'brightness_get' function */
  163. static enum led_brightness powernv_brightness_get(struct led_classdev *led_cdev)
  164. {
  165. struct powernv_led_data *powernv_led =
  166. container_of(led_cdev, struct powernv_led_data, cdev);
  167. return powernv_led_get(powernv_led);
  168. }
  169. /*
  170. * This function registers classdev structure for any given type of LED on
  171. * a given child LED device node.
  172. */
  173. static int powernv_led_create(struct device *dev,
  174. struct powernv_led_data *powernv_led,
  175. const char *led_type_desc)
  176. {
  177. int rc;
  178. /* Make sure LED type is supported */
  179. powernv_led->led_type = powernv_get_led_type(led_type_desc);
  180. if (powernv_led->led_type == -1) {
  181. dev_warn(dev, "%s: No support for led type : %s\n",
  182. __func__, led_type_desc);
  183. return -EINVAL;
  184. }
  185. /* Create the name for classdev */
  186. powernv_led->cdev.name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s",
  187. powernv_led->loc_code,
  188. led_type_desc);
  189. if (!powernv_led->cdev.name) {
  190. dev_err(dev,
  191. "%s: Memory allocation failed for classdev name\n",
  192. __func__);
  193. return -ENOMEM;
  194. }
  195. powernv_led->cdev.brightness_set = powernv_brightness_set;
  196. powernv_led->cdev.brightness_get = powernv_brightness_get;
  197. powernv_led->cdev.brightness = LED_OFF;
  198. powernv_led->cdev.max_brightness = LED_FULL;
  199. /* Register the classdev */
  200. rc = devm_led_classdev_register(dev, &powernv_led->cdev);
  201. if (rc) {
  202. dev_err(dev, "%s: Classdev registration failed for %s\n",
  203. __func__, powernv_led->cdev.name);
  204. }
  205. return rc;
  206. }
  207. /* Go through LED device tree node and register LED classdev structure */
  208. static int powernv_led_classdev(struct platform_device *pdev,
  209. struct device_node *led_node,
  210. struct powernv_led_common *powernv_led_common)
  211. {
  212. const char *cur = NULL;
  213. int rc = -1;
  214. struct property *p;
  215. struct device_node *np;
  216. struct powernv_led_data *powernv_led;
  217. struct device *dev = &pdev->dev;
  218. for_each_child_of_node(led_node, np) {
  219. p = of_find_property(np, "led-types", NULL);
  220. if (!p)
  221. continue;
  222. while ((cur = of_prop_next_string(p, cur)) != NULL) {
  223. powernv_led = devm_kzalloc(dev, sizeof(*powernv_led),
  224. GFP_KERNEL);
  225. if (!powernv_led) {
  226. of_node_put(np);
  227. return -ENOMEM;
  228. }
  229. powernv_led->common = powernv_led_common;
  230. powernv_led->loc_code = (char *)np->name;
  231. rc = powernv_led_create(dev, powernv_led, cur);
  232. if (rc) {
  233. of_node_put(np);
  234. return rc;
  235. }
  236. } /* while end */
  237. }
  238. return rc;
  239. }
  240. /* Platform driver probe */
  241. static int powernv_led_probe(struct platform_device *pdev)
  242. {
  243. struct device_node *led_node;
  244. struct powernv_led_common *powernv_led_common;
  245. struct device *dev = &pdev->dev;
  246. led_node = of_find_node_by_path("/ibm,opal/leds");
  247. if (!led_node) {
  248. dev_err(dev, "%s: LED parent device node not found\n",
  249. __func__);
  250. return -EINVAL;
  251. }
  252. powernv_led_common = devm_kzalloc(dev, sizeof(*powernv_led_common),
  253. GFP_KERNEL);
  254. if (!powernv_led_common)
  255. return -ENOMEM;
  256. mutex_init(&powernv_led_common->lock);
  257. powernv_led_common->max_led_type = cpu_to_be64(OPAL_SLOT_LED_TYPE_MAX);
  258. platform_set_drvdata(pdev, powernv_led_common);
  259. return powernv_led_classdev(pdev, led_node, powernv_led_common);
  260. }
  261. /* Platform driver remove */
  262. static int powernv_led_remove(struct platform_device *pdev)
  263. {
  264. struct powernv_led_common *powernv_led_common;
  265. /* Disable LED operation */
  266. powernv_led_common = platform_get_drvdata(pdev);
  267. powernv_led_common->led_disabled = true;
  268. /* Destroy lock */
  269. mutex_destroy(&powernv_led_common->lock);
  270. dev_info(&pdev->dev, "PowerNV led module unregistered\n");
  271. return 0;
  272. }
  273. /* Platform driver property match */
  274. static const struct of_device_id powernv_led_match[] = {
  275. {
  276. .compatible = "ibm,opal-v3-led",
  277. },
  278. {},
  279. };
  280. MODULE_DEVICE_TABLE(of, powernv_led_match);
  281. static struct platform_driver powernv_led_driver = {
  282. .probe = powernv_led_probe,
  283. .remove = powernv_led_remove,
  284. .driver = {
  285. .name = "powernv-led-driver",
  286. .of_match_table = powernv_led_match,
  287. },
  288. };
  289. module_platform_driver(powernv_led_driver);
  290. MODULE_LICENSE("GPL v2");
  291. MODULE_DESCRIPTION("PowerNV LED driver");
  292. MODULE_AUTHOR("Vasant Hegde <hegdevasant@linux.vnet.ibm.com>");