powernv-op-panel.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * OPAL Operator Panel Display Driver
  4. *
  5. * Copyright 2016, Suraj Jitindar Singh, IBM Corporation.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/init.h>
  9. #include <linux/module.h>
  10. #include <linux/kernel.h>
  11. #include <linux/fs.h>
  12. #include <linux/device.h>
  13. #include <linux/errno.h>
  14. #include <linux/mutex.h>
  15. #include <linux/of.h>
  16. #include <linux/slab.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/miscdevice.h>
  19. #include <asm/opal.h>
  20. /*
  21. * This driver creates a character device (/dev/op_panel) which exposes the
  22. * operator panel (character LCD display) on IBM Power Systems machines
  23. * with FSPs.
  24. * A character buffer written to the device will be displayed on the
  25. * operator panel.
  26. */
  27. static DEFINE_MUTEX(oppanel_mutex);
  28. static u32 num_lines, oppanel_size;
  29. static oppanel_line_t *oppanel_lines;
  30. static char *oppanel_data;
  31. static loff_t oppanel_llseek(struct file *filp, loff_t offset, int whence)
  32. {
  33. return fixed_size_llseek(filp, offset, whence, oppanel_size);
  34. }
  35. static ssize_t oppanel_read(struct file *filp, char __user *userbuf, size_t len,
  36. loff_t *f_pos)
  37. {
  38. return simple_read_from_buffer(userbuf, len, f_pos, oppanel_data,
  39. oppanel_size);
  40. }
  41. static int __op_panel_update_display(void)
  42. {
  43. struct opal_msg msg;
  44. int rc, token;
  45. token = opal_async_get_token_interruptible();
  46. if (token < 0) {
  47. if (token != -ERESTARTSYS)
  48. pr_debug("Couldn't get OPAL async token [token=%d]\n",
  49. token);
  50. return token;
  51. }
  52. rc = opal_write_oppanel_async(token, oppanel_lines, num_lines);
  53. switch (rc) {
  54. case OPAL_ASYNC_COMPLETION:
  55. rc = opal_async_wait_response(token, &msg);
  56. if (rc) {
  57. pr_debug("Failed to wait for async response [rc=%d]\n",
  58. rc);
  59. break;
  60. }
  61. rc = opal_get_async_rc(msg);
  62. if (rc != OPAL_SUCCESS) {
  63. pr_debug("OPAL async call returned failed [rc=%d]\n",
  64. rc);
  65. break;
  66. }
  67. break;
  68. case OPAL_SUCCESS:
  69. break;
  70. default:
  71. pr_debug("OPAL write op-panel call failed [rc=%d]\n", rc);
  72. }
  73. opal_async_release_token(token);
  74. return rc;
  75. }
  76. static ssize_t oppanel_write(struct file *filp, const char __user *userbuf,
  77. size_t len, loff_t *f_pos)
  78. {
  79. loff_t f_pos_prev = *f_pos;
  80. ssize_t ret;
  81. int rc;
  82. if (!*f_pos)
  83. memset(oppanel_data, ' ', oppanel_size);
  84. else if (*f_pos >= oppanel_size)
  85. return -EFBIG;
  86. ret = simple_write_to_buffer(oppanel_data, oppanel_size, f_pos, userbuf,
  87. len);
  88. if (ret > 0) {
  89. rc = __op_panel_update_display();
  90. if (rc != OPAL_SUCCESS) {
  91. pr_err_ratelimited("OPAL call failed to write to op panel display [rc=%d]\n",
  92. rc);
  93. *f_pos = f_pos_prev;
  94. return -EIO;
  95. }
  96. }
  97. return ret;
  98. }
  99. static int oppanel_open(struct inode *inode, struct file *filp)
  100. {
  101. if (!mutex_trylock(&oppanel_mutex)) {
  102. pr_debug("Device Busy\n");
  103. return -EBUSY;
  104. }
  105. return 0;
  106. }
  107. static int oppanel_release(struct inode *inode, struct file *filp)
  108. {
  109. mutex_unlock(&oppanel_mutex);
  110. return 0;
  111. }
  112. static const struct file_operations oppanel_fops = {
  113. .owner = THIS_MODULE,
  114. .llseek = oppanel_llseek,
  115. .read = oppanel_read,
  116. .write = oppanel_write,
  117. .open = oppanel_open,
  118. .release = oppanel_release
  119. };
  120. static struct miscdevice oppanel_dev = {
  121. .minor = MISC_DYNAMIC_MINOR,
  122. .name = "op_panel",
  123. .fops = &oppanel_fops
  124. };
  125. static int oppanel_probe(struct platform_device *pdev)
  126. {
  127. struct device_node *np = pdev->dev.of_node;
  128. u32 line_len;
  129. int rc, i;
  130. rc = of_property_read_u32(np, "#length", &line_len);
  131. if (rc) {
  132. pr_err_ratelimited("Operator panel length property not found\n");
  133. return rc;
  134. }
  135. rc = of_property_read_u32(np, "#lines", &num_lines);
  136. if (rc) {
  137. pr_err_ratelimited("Operator panel lines property not found\n");
  138. return rc;
  139. }
  140. oppanel_size = line_len * num_lines;
  141. pr_devel("Operator panel of size %u found with %u lines of length %u\n",
  142. oppanel_size, num_lines, line_len);
  143. oppanel_data = kcalloc(oppanel_size, sizeof(*oppanel_data), GFP_KERNEL);
  144. if (!oppanel_data)
  145. return -ENOMEM;
  146. oppanel_lines = kcalloc(num_lines, sizeof(oppanel_line_t), GFP_KERNEL);
  147. if (!oppanel_lines) {
  148. rc = -ENOMEM;
  149. goto free_oppanel_data;
  150. }
  151. memset(oppanel_data, ' ', oppanel_size);
  152. for (i = 0; i < num_lines; i++) {
  153. oppanel_lines[i].line_len = cpu_to_be64(line_len);
  154. oppanel_lines[i].line = cpu_to_be64(__pa(&oppanel_data[i *
  155. line_len]));
  156. }
  157. rc = misc_register(&oppanel_dev);
  158. if (rc) {
  159. pr_err_ratelimited("Failed to register as misc device\n");
  160. goto free_oppanel;
  161. }
  162. return 0;
  163. free_oppanel:
  164. kfree(oppanel_lines);
  165. free_oppanel_data:
  166. kfree(oppanel_data);
  167. return rc;
  168. }
  169. static int oppanel_remove(struct platform_device *pdev)
  170. {
  171. misc_deregister(&oppanel_dev);
  172. kfree(oppanel_lines);
  173. kfree(oppanel_data);
  174. return 0;
  175. }
  176. static const struct of_device_id oppanel_match[] = {
  177. { .compatible = "ibm,opal-oppanel" },
  178. { },
  179. };
  180. static struct platform_driver oppanel_driver = {
  181. .driver = {
  182. .name = "powernv-op-panel",
  183. .of_match_table = oppanel_match,
  184. },
  185. .probe = oppanel_probe,
  186. .remove = oppanel_remove,
  187. };
  188. module_platform_driver(oppanel_driver);
  189. MODULE_DEVICE_TABLE(of, oppanel_match);
  190. MODULE_LICENSE("GPL v2");
  191. MODULE_DESCRIPTION("PowerNV Operator Panel LCD Display Driver");
  192. MODULE_AUTHOR("Suraj Jitindar Singh <[email protected]>");