leds-powernv.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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 int 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 token;
  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 = opal_get_async_rc(msg);
  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. return rc;
  110. }
  111. /*
  112. * This function fetches the LED state for a given LED type for
  113. * mentioned LED classdev structure.
  114. */
  115. static enum led_brightness powernv_led_get(struct powernv_led_data *powernv_led)
  116. {
  117. int rc;
  118. __be64 mask, value, max_type;
  119. u64 led_mask, led_value;
  120. struct device *dev = powernv_led->cdev.dev;
  121. struct powernv_led_common *powernv_led_common = powernv_led->common;
  122. /* Fetch all LED status */
  123. mask = cpu_to_be64(0);
  124. value = cpu_to_be64(0);
  125. max_type = powernv_led_common->max_led_type;
  126. rc = opal_leds_get_ind(powernv_led->loc_code,
  127. &mask, &value, &max_type);
  128. if (rc != OPAL_SUCCESS && rc != OPAL_PARTIAL) {
  129. dev_err(dev, "%s: OPAL get led call failed [rc=%d]\n",
  130. __func__, rc);
  131. return LED_OFF;
  132. }
  133. led_mask = be64_to_cpu(mask);
  134. led_value = be64_to_cpu(value);
  135. /* LED status available */
  136. if (!((led_mask >> powernv_led->led_type) & OPAL_SLOT_LED_STATE_ON)) {
  137. dev_err(dev, "%s: LED status not available for %s\n",
  138. __func__, powernv_led->cdev.name);
  139. return LED_OFF;
  140. }
  141. /* LED status value */
  142. if ((led_value >> powernv_led->led_type) & OPAL_SLOT_LED_STATE_ON)
  143. return LED_FULL;
  144. return LED_OFF;
  145. }
  146. /*
  147. * LED classdev 'brightness_get' function. This schedules work
  148. * to update LED state.
  149. */
  150. static int powernv_brightness_set(struct led_classdev *led_cdev,
  151. enum led_brightness value)
  152. {
  153. struct powernv_led_data *powernv_led =
  154. container_of(led_cdev, struct powernv_led_data, cdev);
  155. struct powernv_led_common *powernv_led_common = powernv_led->common;
  156. int rc;
  157. /* Do not modify LED in unload path */
  158. if (powernv_led_common->led_disabled)
  159. return 0;
  160. mutex_lock(&powernv_led_common->lock);
  161. rc = powernv_led_set(powernv_led, value);
  162. mutex_unlock(&powernv_led_common->lock);
  163. return rc;
  164. }
  165. /* LED classdev 'brightness_get' function */
  166. static enum led_brightness powernv_brightness_get(struct led_classdev *led_cdev)
  167. {
  168. struct powernv_led_data *powernv_led =
  169. container_of(led_cdev, struct powernv_led_data, cdev);
  170. return powernv_led_get(powernv_led);
  171. }
  172. /*
  173. * This function registers classdev structure for any given type of LED on
  174. * a given child LED device node.
  175. */
  176. static int powernv_led_create(struct device *dev,
  177. struct powernv_led_data *powernv_led,
  178. const char *led_type_desc)
  179. {
  180. int rc;
  181. /* Make sure LED type is supported */
  182. powernv_led->led_type = powernv_get_led_type(led_type_desc);
  183. if (powernv_led->led_type == -1) {
  184. dev_warn(dev, "%s: No support for led type : %s\n",
  185. __func__, led_type_desc);
  186. return -EINVAL;
  187. }
  188. /* Create the name for classdev */
  189. powernv_led->cdev.name = devm_kasprintf(dev, GFP_KERNEL, "%s:%s",
  190. powernv_led->loc_code,
  191. led_type_desc);
  192. if (!powernv_led->cdev.name) {
  193. dev_err(dev,
  194. "%s: Memory allocation failed for classdev name\n",
  195. __func__);
  196. return -ENOMEM;
  197. }
  198. powernv_led->cdev.brightness_set_blocking = powernv_brightness_set;
  199. powernv_led->cdev.brightness_get = powernv_brightness_get;
  200. powernv_led->cdev.brightness = LED_OFF;
  201. powernv_led->cdev.max_brightness = LED_FULL;
  202. /* Register the classdev */
  203. rc = devm_led_classdev_register(dev, &powernv_led->cdev);
  204. if (rc) {
  205. dev_err(dev, "%s: Classdev registration failed for %s\n",
  206. __func__, powernv_led->cdev.name);
  207. }
  208. return rc;
  209. }
  210. /* Go through LED device tree node and register LED classdev structure */
  211. static int powernv_led_classdev(struct platform_device *pdev,
  212. struct device_node *led_node,
  213. struct powernv_led_common *powernv_led_common)
  214. {
  215. const char *cur = NULL;
  216. int rc = -1;
  217. struct property *p;
  218. struct device_node *np;
  219. struct powernv_led_data *powernv_led;
  220. struct device *dev = &pdev->dev;
  221. for_each_child_of_node(led_node, np) {
  222. p = of_find_property(np, "led-types", NULL);
  223. while ((cur = of_prop_next_string(p, cur)) != NULL) {
  224. powernv_led = devm_kzalloc(dev, sizeof(*powernv_led),
  225. GFP_KERNEL);
  226. if (!powernv_led) {
  227. of_node_put(np);
  228. return -ENOMEM;
  229. }
  230. powernv_led->common = powernv_led_common;
  231. powernv_led->loc_code = (char *)np->name;
  232. rc = powernv_led_create(dev, powernv_led, cur);
  233. if (rc) {
  234. of_node_put(np);
  235. return rc;
  236. }
  237. } /* while end */
  238. }
  239. return rc;
  240. }
  241. /* Platform driver probe */
  242. static int powernv_led_probe(struct platform_device *pdev)
  243. {
  244. struct device_node *led_node;
  245. struct powernv_led_common *powernv_led_common;
  246. struct device *dev = &pdev->dev;
  247. led_node = of_find_node_by_path("/ibm,opal/leds");
  248. if (!led_node) {
  249. dev_err(dev, "%s: LED parent device node not found\n",
  250. __func__);
  251. return -EINVAL;
  252. }
  253. powernv_led_common = devm_kzalloc(dev, sizeof(*powernv_led_common),
  254. GFP_KERNEL);
  255. if (!powernv_led_common)
  256. return -ENOMEM;
  257. mutex_init(&powernv_led_common->lock);
  258. powernv_led_common->max_led_type = cpu_to_be64(OPAL_SLOT_LED_TYPE_MAX);
  259. platform_set_drvdata(pdev, powernv_led_common);
  260. return powernv_led_classdev(pdev, led_node, powernv_led_common);
  261. }
  262. /* Platform driver remove */
  263. static int powernv_led_remove(struct platform_device *pdev)
  264. {
  265. struct powernv_led_common *powernv_led_common;
  266. /* Disable LED operation */
  267. powernv_led_common = platform_get_drvdata(pdev);
  268. powernv_led_common->led_disabled = true;
  269. /* Destroy lock */
  270. mutex_destroy(&powernv_led_common->lock);
  271. dev_info(&pdev->dev, "PowerNV led module unregistered\n");
  272. return 0;
  273. }
  274. /* Platform driver property match */
  275. static const struct of_device_id powernv_led_match[] = {
  276. {
  277. .compatible = "ibm,opal-v3-led",
  278. },
  279. {},
  280. };
  281. MODULE_DEVICE_TABLE(of, powernv_led_match);
  282. static struct platform_driver powernv_led_driver = {
  283. .probe = powernv_led_probe,
  284. .remove = powernv_led_remove,
  285. .driver = {
  286. .name = "powernv-led-driver",
  287. .of_match_table = powernv_led_match,
  288. },
  289. };
  290. module_platform_driver(powernv_led_driver);
  291. MODULE_LICENSE("GPL v2");
  292. MODULE_DESCRIPTION("PowerNV LED driver");
  293. MODULE_AUTHOR("Vasant Hegde <hegdevasant@linux.vnet.ibm.com>");