drm/panfrost: Move gpu_{write, read}() macros to panfrost_regs.h
[linux/fpc-iii.git] / drivers / platform / olpc / olpc-ec.c
blob7f25d6c661490276f5a33b145f051a5355e10227
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Generic driver for the OLPC Embedded Controller.
5 * Author: Andres Salomon <dilinger@queued.net>
7 * Copyright (C) 2011-2012 One Laptop per Child Foundation.
8 */
9 #include <linux/completion.h>
10 #include <linux/debugfs.h>
11 #include <linux/spinlock.h>
12 #include <linux/mutex.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
18 #include <linux/olpc-ec.h>
19 #include <asm/olpc.h>
21 struct ec_cmd_desc {
22 u8 cmd;
23 u8 *inbuf, *outbuf;
24 size_t inlen, outlen;
26 int err;
27 struct completion finished;
28 struct list_head node;
30 void *priv;
33 struct olpc_ec_priv {
34 struct olpc_ec_driver *drv;
35 struct work_struct worker;
36 struct mutex cmd_lock;
38 /* Pending EC commands */
39 struct list_head cmd_q;
40 spinlock_t cmd_q_lock;
42 struct dentry *dbgfs_dir;
45 * Running an EC command while suspending means we don't always finish
46 * the command before the machine suspends. This means that the EC
47 * is expecting the command protocol to finish, but we after a period
48 * of time (while the OS is asleep) the EC times out and restarts its
49 * idle loop. Meanwhile, the OS wakes up, thinks it's still in the
50 * middle of the command protocol, starts throwing random things at
51 * the EC... and everyone's uphappy.
53 bool suspended;
56 static struct olpc_ec_driver *ec_driver;
57 static struct olpc_ec_priv *ec_priv;
58 static void *ec_cb_arg;
60 void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
62 ec_driver = drv;
63 ec_cb_arg = arg;
65 EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
67 static void olpc_ec_worker(struct work_struct *w)
69 struct olpc_ec_priv *ec = container_of(w, struct olpc_ec_priv, worker);
70 struct ec_cmd_desc *desc = NULL;
71 unsigned long flags;
73 /* Grab the first pending command from the queue */
74 spin_lock_irqsave(&ec->cmd_q_lock, flags);
75 if (!list_empty(&ec->cmd_q)) {
76 desc = list_first_entry(&ec->cmd_q, struct ec_cmd_desc, node);
77 list_del(&desc->node);
79 spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
81 /* Do we actually have anything to do? */
82 if (!desc)
83 return;
85 /* Protect the EC hw with a mutex; only run one cmd at a time */
86 mutex_lock(&ec->cmd_lock);
87 desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
88 desc->outbuf, desc->outlen, ec_cb_arg);
89 mutex_unlock(&ec->cmd_lock);
91 /* Finished, wake up olpc_ec_cmd() */
92 complete(&desc->finished);
94 /* Run the worker thread again in case there are more cmds pending */
95 schedule_work(&ec->worker);
99 * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
100 * locking is pretty critical.
102 static void queue_ec_descriptor(struct ec_cmd_desc *desc,
103 struct olpc_ec_priv *ec)
105 unsigned long flags;
107 INIT_LIST_HEAD(&desc->node);
109 spin_lock_irqsave(&ec->cmd_q_lock, flags);
110 list_add_tail(&desc->node, &ec->cmd_q);
111 spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
113 schedule_work(&ec->worker);
116 int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
118 struct olpc_ec_priv *ec = ec_priv;
119 struct ec_cmd_desc desc;
121 /* Ensure a driver and ec hook have been registered */
122 if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
123 return -ENODEV;
125 if (!ec)
126 return -ENOMEM;
128 /* Suspending in the middle of a command hoses things really badly */
129 if (WARN_ON(ec->suspended))
130 return -EBUSY;
132 might_sleep();
134 desc.cmd = cmd;
135 desc.inbuf = inbuf;
136 desc.outbuf = outbuf;
137 desc.inlen = inlen;
138 desc.outlen = outlen;
139 desc.err = 0;
140 init_completion(&desc.finished);
142 queue_ec_descriptor(&desc, ec);
144 /* Timeouts must be handled in the platform-specific EC hook */
145 wait_for_completion(&desc.finished);
147 /* The worker thread dequeues the cmd; no need to do anything here */
148 return desc.err;
150 EXPORT_SYMBOL_GPL(olpc_ec_cmd);
152 #ifdef CONFIG_DEBUG_FS
155 * debugfs support for "generic commands", to allow sending
156 * arbitrary EC commands from userspace.
159 #define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */
160 #define EC_MAX_CMD_REPLY (8)
162 static DEFINE_MUTEX(ec_dbgfs_lock);
163 static unsigned char ec_dbgfs_resp[EC_MAX_CMD_REPLY];
164 static unsigned int ec_dbgfs_resp_bytes;
166 static ssize_t ec_dbgfs_cmd_write(struct file *file, const char __user *buf,
167 size_t size, loff_t *ppos)
169 int i, m;
170 unsigned char ec_cmd[EC_MAX_CMD_ARGS];
171 unsigned int ec_cmd_int[EC_MAX_CMD_ARGS];
172 char cmdbuf[64];
173 int ec_cmd_bytes;
175 mutex_lock(&ec_dbgfs_lock);
177 size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
179 m = sscanf(cmdbuf, "%x:%u %x %x %x %x %x", &ec_cmd_int[0],
180 &ec_dbgfs_resp_bytes, &ec_cmd_int[1], &ec_cmd_int[2],
181 &ec_cmd_int[3], &ec_cmd_int[4], &ec_cmd_int[5]);
182 if (m < 2 || ec_dbgfs_resp_bytes > EC_MAX_CMD_REPLY) {
183 /* reset to prevent overflow on read */
184 ec_dbgfs_resp_bytes = 0;
186 pr_debug("olpc-ec: bad ec cmd: cmd:response-count [arg1 [arg2 ...]]\n");
187 size = -EINVAL;
188 goto out;
191 /* convert scanf'd ints to char */
192 ec_cmd_bytes = m - 2;
193 for (i = 0; i <= ec_cmd_bytes; i++)
194 ec_cmd[i] = ec_cmd_int[i];
196 pr_debug("olpc-ec: debugfs cmd 0x%02x with %d args %5ph, want %d returns\n",
197 ec_cmd[0], ec_cmd_bytes, ec_cmd + 1,
198 ec_dbgfs_resp_bytes);
200 olpc_ec_cmd(ec_cmd[0], (ec_cmd_bytes == 0) ? NULL : &ec_cmd[1],
201 ec_cmd_bytes, ec_dbgfs_resp, ec_dbgfs_resp_bytes);
203 pr_debug("olpc-ec: response %8ph (%d bytes expected)\n",
204 ec_dbgfs_resp, ec_dbgfs_resp_bytes);
206 out:
207 mutex_unlock(&ec_dbgfs_lock);
208 return size;
211 static ssize_t ec_dbgfs_cmd_read(struct file *file, char __user *buf,
212 size_t size, loff_t *ppos)
214 unsigned int i, r;
215 char *rp;
216 char respbuf[64];
218 mutex_lock(&ec_dbgfs_lock);
219 rp = respbuf;
220 rp += sprintf(rp, "%02x", ec_dbgfs_resp[0]);
221 for (i = 1; i < ec_dbgfs_resp_bytes; i++)
222 rp += sprintf(rp, ", %02x", ec_dbgfs_resp[i]);
223 mutex_unlock(&ec_dbgfs_lock);
224 rp += sprintf(rp, "\n");
226 r = rp - respbuf;
227 return simple_read_from_buffer(buf, size, ppos, respbuf, r);
230 static const struct file_operations ec_dbgfs_ops = {
231 .write = ec_dbgfs_cmd_write,
232 .read = ec_dbgfs_cmd_read,
235 static struct dentry *olpc_ec_setup_debugfs(void)
237 struct dentry *dbgfs_dir;
239 dbgfs_dir = debugfs_create_dir("olpc-ec", NULL);
240 if (IS_ERR_OR_NULL(dbgfs_dir))
241 return NULL;
243 debugfs_create_file("cmd", 0600, dbgfs_dir, NULL, &ec_dbgfs_ops);
245 return dbgfs_dir;
248 #else
250 static struct dentry *olpc_ec_setup_debugfs(void)
252 return NULL;
255 #endif /* CONFIG_DEBUG_FS */
257 static int olpc_ec_probe(struct platform_device *pdev)
259 struct olpc_ec_priv *ec;
260 int err;
262 if (!ec_driver)
263 return -ENODEV;
265 ec = kzalloc(sizeof(*ec), GFP_KERNEL);
266 if (!ec)
267 return -ENOMEM;
269 ec->drv = ec_driver;
270 INIT_WORK(&ec->worker, olpc_ec_worker);
271 mutex_init(&ec->cmd_lock);
273 INIT_LIST_HEAD(&ec->cmd_q);
274 spin_lock_init(&ec->cmd_q_lock);
276 ec_priv = ec;
277 platform_set_drvdata(pdev, ec);
279 err = ec_driver->probe ? ec_driver->probe(pdev) : 0;
280 if (err) {
281 ec_priv = NULL;
282 kfree(ec);
283 } else {
284 ec->dbgfs_dir = olpc_ec_setup_debugfs();
287 return err;
290 static int olpc_ec_suspend(struct device *dev)
292 struct platform_device *pdev = to_platform_device(dev);
293 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
294 int err = 0;
296 if (ec_driver->suspend)
297 err = ec_driver->suspend(pdev);
298 if (!err)
299 ec->suspended = true;
301 return err;
304 static int olpc_ec_resume(struct device *dev)
306 struct platform_device *pdev = to_platform_device(dev);
307 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
309 ec->suspended = false;
310 return ec_driver->resume ? ec_driver->resume(pdev) : 0;
313 static const struct dev_pm_ops olpc_ec_pm_ops = {
314 .suspend_late = olpc_ec_suspend,
315 .resume_early = olpc_ec_resume,
318 static struct platform_driver olpc_ec_plat_driver = {
319 .probe = olpc_ec_probe,
320 .driver = {
321 .name = "olpc-ec",
322 .pm = &olpc_ec_pm_ops,
326 static int __init olpc_ec_init_module(void)
328 return platform_driver_register(&olpc_ec_plat_driver);
330 arch_initcall(olpc_ec_init_module);