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