2 * Generic driver for the OLPC Embedded Controller.
4 * Copyright (C) 2011-2012 One Laptop per Child Foundation.
6 * Licensed under the GPL v2 or later.
8 #include <linux/completion.h>
9 #include <linux/spinlock.h>
10 #include <linux/mutex.h>
11 #include <linux/platform_device.h>
12 #include <linux/workqueue.h>
13 #include <linux/module.h>
14 #include <linux/list.h>
15 #include <linux/olpc-ec.h>
24 struct completion finished;
25 struct list_head node;
30 static void olpc_ec_worker(struct work_struct *w);
32 static DECLARE_WORK(ec_worker, olpc_ec_worker);
33 static LIST_HEAD(ec_cmd_q);
34 static DEFINE_SPINLOCK(ec_cmd_q_lock);
36 static struct olpc_ec_driver *ec_driver;
37 static void *ec_cb_arg;
38 static DEFINE_MUTEX(ec_cb_lock);
40 void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
45 EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
47 static void olpc_ec_worker(struct work_struct *w)
49 struct ec_cmd_desc *desc = NULL;
52 /* Grab the first pending command from the queue */
53 spin_lock_irqsave(&ec_cmd_q_lock, flags);
54 if (!list_empty(&ec_cmd_q)) {
55 desc = list_first_entry(&ec_cmd_q, struct ec_cmd_desc, node);
56 list_del(&desc->node);
58 spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
60 /* Do we actually have anything to do? */
64 /* Protect the EC hw with a mutex; only run one cmd at a time */
65 mutex_lock(&ec_cb_lock);
66 desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
67 desc->outbuf, desc->outlen, ec_cb_arg);
68 mutex_unlock(&ec_cb_lock);
70 /* Finished, wake up olpc_ec_cmd() */
71 complete(&desc->finished);
73 /* Run the worker thread again in case there are more cmds pending */
74 schedule_work(&ec_worker);
78 * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
79 * locking is pretty critical.
81 static void queue_ec_descriptor(struct ec_cmd_desc *desc)
85 INIT_LIST_HEAD(&desc->node);
87 spin_lock_irqsave(&ec_cmd_q_lock, flags);
88 list_add_tail(&desc->node, &ec_cmd_q);
89 spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
91 schedule_work(&ec_worker);
94 int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
96 struct ec_cmd_desc desc;
98 /* XXX: this will be removed in later patches */
99 /* Are we using old-style callers? */
100 if (!ec_driver || !ec_driver->ec_cmd)
101 return olpc_ec_cmd_x86(cmd, inbuf, inlen, outbuf, outlen);
103 /* Ensure a driver and ec hook have been registered */
104 if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
111 desc.outbuf = outbuf;
113 desc.outlen = outlen;
115 init_completion(&desc.finished);
117 queue_ec_descriptor(&desc);
119 /* Timeouts must be handled in the platform-specific EC hook */
120 wait_for_completion(&desc.finished);
122 /* The worker thread dequeues the cmd; no need to do anything here */
125 EXPORT_SYMBOL_GPL(olpc_ec_cmd);
127 static int olpc_ec_probe(struct platform_device *pdev)
134 err = ec_driver->probe ? ec_driver->probe(pdev) : 0;
139 static int olpc_ec_suspend(struct device *dev)
141 struct platform_device *pdev = to_platform_device(dev);
142 return ec_driver->suspend ? ec_driver->suspend(pdev) : 0;
145 static int olpc_ec_resume(struct device *dev)
147 struct platform_device *pdev = to_platform_device(dev);
148 return ec_driver->resume ? ec_driver->resume(pdev) : 0;
151 static const struct dev_pm_ops olpc_ec_pm_ops = {
152 .suspend_late = olpc_ec_suspend,
153 .resume_early = olpc_ec_resume,
156 static struct platform_driver olpc_ec_plat_driver = {
157 .probe = olpc_ec_probe,
160 .pm = &olpc_ec_pm_ops,
164 static int __init olpc_ec_init_module(void)
166 return platform_driver_register(&olpc_ec_plat_driver);
169 module_init(olpc_ec_init_module);
171 MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
172 MODULE_LICENSE("GPL");