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/slab.h>
13 #include <linux/workqueue.h>
14 #include <linux/module.h>
15 #include <linux/list.h>
16 #include <linux/olpc-ec.h>
25 struct completion finished;
26 struct list_head node;
32 struct olpc_ec_driver *drv;
35 * Running an EC command while suspending means we don't always finish
36 * the command before the machine suspends. This means that the EC
37 * is expecting the command protocol to finish, but we after a period
38 * of time (while the OS is asleep) the EC times out and restarts its
39 * idle loop. Meanwhile, the OS wakes up, thinks it's still in the
40 * middle of the command protocol, starts throwing random things at
41 * the EC... and everyone's uphappy.
46 static void olpc_ec_worker(struct work_struct *w);
48 static DECLARE_WORK(ec_worker, olpc_ec_worker);
49 static LIST_HEAD(ec_cmd_q);
50 static DEFINE_SPINLOCK(ec_cmd_q_lock);
52 static struct olpc_ec_driver *ec_driver;
53 static struct olpc_ec_priv *ec_priv;
54 static void *ec_cb_arg;
55 static DEFINE_MUTEX(ec_cb_lock);
57 void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
62 EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
64 static void olpc_ec_worker(struct work_struct *w)
66 struct ec_cmd_desc *desc = NULL;
69 /* Grab the first pending command from the queue */
70 spin_lock_irqsave(&ec_cmd_q_lock, flags);
71 if (!list_empty(&ec_cmd_q)) {
72 desc = list_first_entry(&ec_cmd_q, struct ec_cmd_desc, node);
73 list_del(&desc->node);
75 spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
77 /* Do we actually have anything to do? */
81 /* Protect the EC hw with a mutex; only run one cmd at a time */
82 mutex_lock(&ec_cb_lock);
83 desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
84 desc->outbuf, desc->outlen, ec_cb_arg);
85 mutex_unlock(&ec_cb_lock);
87 /* Finished, wake up olpc_ec_cmd() */
88 complete(&desc->finished);
90 /* Run the worker thread again in case there are more cmds pending */
91 schedule_work(&ec_worker);
95 * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
96 * locking is pretty critical.
98 static void queue_ec_descriptor(struct ec_cmd_desc *desc)
102 INIT_LIST_HEAD(&desc->node);
104 spin_lock_irqsave(&ec_cmd_q_lock, flags);
105 list_add_tail(&desc->node, &ec_cmd_q);
106 spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
108 schedule_work(&ec_worker);
111 int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
113 struct olpc_ec_priv *ec = ec_priv;
114 struct ec_cmd_desc desc;
116 /* Ensure a driver and ec hook have been registered */
117 if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
123 /* Suspending in the middle of a command hoses things really badly */
124 if (WARN_ON(ec->suspended))
131 desc.outbuf = outbuf;
133 desc.outlen = outlen;
135 init_completion(&desc.finished);
137 queue_ec_descriptor(&desc);
139 /* Timeouts must be handled in the platform-specific EC hook */
140 wait_for_completion(&desc.finished);
142 /* The worker thread dequeues the cmd; no need to do anything here */
145 EXPORT_SYMBOL_GPL(olpc_ec_cmd);
147 static int olpc_ec_probe(struct platform_device *pdev)
149 struct olpc_ec_priv *ec;
155 ec = kzalloc(sizeof(*ec), GFP_KERNEL);
160 platform_set_drvdata(pdev, ec);
162 err = ec_driver->probe ? ec_driver->probe(pdev) : 0;
167 static int olpc_ec_suspend(struct device *dev)
169 struct platform_device *pdev = to_platform_device(dev);
170 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
173 if (ec_driver->suspend)
174 err = ec_driver->suspend(pdev);
176 ec->suspended = true;
181 static int olpc_ec_resume(struct device *dev)
183 struct platform_device *pdev = to_platform_device(dev);
184 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
186 ec->suspended = false;
187 return ec_driver->resume ? ec_driver->resume(pdev) : 0;
190 static const struct dev_pm_ops olpc_ec_pm_ops = {
191 .suspend_late = olpc_ec_suspend,
192 .resume_early = olpc_ec_resume,
195 static struct platform_driver olpc_ec_plat_driver = {
196 .probe = olpc_ec_probe,
199 .pm = &olpc_ec_pm_ops,
203 static int __init olpc_ec_init_module(void)
205 return platform_driver_register(&olpc_ec_plat_driver);
208 module_init(olpc_ec_init_module);
210 MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
211 MODULE_LICENSE("GPL");