Linux Audio

Check our new training course

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