blob: cfba41fb04de7beb82b098f001109635394faeff [file] [log] [blame]
Andres Salomon392a3252012-07-10 19:31:51 -07001/*
2 * Generic driver for the OLPC Embedded Controller.
3 *
4 * Copyright (C) 2011-2012 One Laptop per Child Foundation.
5 *
6 * Licensed under the GPL v2 or later.
7 */
Andres Salomon3d26c202012-07-11 17:40:25 -07008#include <linux/completion.h>
9#include <linux/spinlock.h>
10#include <linux/mutex.h>
Andres Salomonac250412012-07-13 05:57:17 -070011#include <linux/platform_device.h>
Andres Salomond278b7a2012-07-13 15:54:25 -070012#include <linux/slab.h>
Andres Salomon3d26c202012-07-11 17:40:25 -070013#include <linux/workqueue.h>
Andres Salomon392a3252012-07-10 19:31:51 -070014#include <linux/module.h>
Andres Salomon3d26c202012-07-11 17:40:25 -070015#include <linux/list.h>
16#include <linux/olpc-ec.h>
Andres Salomon392a3252012-07-10 19:31:51 -070017#include <asm/olpc.h>
18
Andres Salomon3d26c202012-07-11 17:40:25 -070019struct ec_cmd_desc {
20 u8 cmd;
21 u8 *inbuf, *outbuf;
22 size_t inlen, outlen;
23
24 int err;
25 struct completion finished;
26 struct list_head node;
27
28 void *priv;
29};
30
Andres Salomond278b7a2012-07-13 15:54:25 -070031struct olpc_ec_priv {
32 struct olpc_ec_driver *drv;
33
34 /*
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.
42 */
43 bool suspended;
44};
45
Andres Salomon3d26c202012-07-11 17:40:25 -070046static void olpc_ec_worker(struct work_struct *w);
47
48static DECLARE_WORK(ec_worker, olpc_ec_worker);
49static LIST_HEAD(ec_cmd_q);
50static DEFINE_SPINLOCK(ec_cmd_q_lock);
51
52static struct olpc_ec_driver *ec_driver;
Andres Salomond278b7a2012-07-13 15:54:25 -070053static struct olpc_ec_priv *ec_priv;
Andres Salomon3d26c202012-07-11 17:40:25 -070054static void *ec_cb_arg;
55static DEFINE_MUTEX(ec_cb_lock);
56
57void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
58{
59 ec_driver = drv;
60 ec_cb_arg = arg;
61}
62EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
63
64static void olpc_ec_worker(struct work_struct *w)
65{
66 struct ec_cmd_desc *desc = NULL;
67 unsigned long flags;
68
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);
74 }
75 spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
76
77 /* Do we actually have anything to do? */
78 if (!desc)
79 return;
80
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);
86
87 /* Finished, wake up olpc_ec_cmd() */
88 complete(&desc->finished);
89
90 /* Run the worker thread again in case there are more cmds pending */
91 schedule_work(&ec_worker);
92}
93
94/*
95 * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
96 * locking is pretty critical.
97 */
98static void queue_ec_descriptor(struct ec_cmd_desc *desc)
99{
100 unsigned long flags;
101
102 INIT_LIST_HEAD(&desc->node);
103
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);
107
108 schedule_work(&ec_worker);
109}
110
Andres Salomon392a3252012-07-10 19:31:51 -0700111int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
112{
Andres Salomond278b7a2012-07-13 15:54:25 -0700113 struct olpc_ec_priv *ec = ec_priv;
Andres Salomon3d26c202012-07-11 17:40:25 -0700114 struct ec_cmd_desc desc;
115
116 /* XXX: this will be removed in later patches */
117 /* Are we using old-style callers? */
118 if (!ec_driver || !ec_driver->ec_cmd)
119 return olpc_ec_cmd_x86(cmd, inbuf, inlen, outbuf, outlen);
120
121 /* Ensure a driver and ec hook have been registered */
122 if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
123 return -ENODEV;
124
Andres Salomond278b7a2012-07-13 15:54:25 -0700125 if (!ec)
126 return -ENOMEM;
127
128 /* Suspending in the middle of a command hoses things really badly */
129 if (WARN_ON(ec->suspended))
130 return -EBUSY;
131
Andres Salomon3d26c202012-07-11 17:40:25 -0700132 might_sleep();
133
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);
141
142 queue_ec_descriptor(&desc);
143
144 /* Timeouts must be handled in the platform-specific EC hook */
145 wait_for_completion(&desc.finished);
146
147 /* The worker thread dequeues the cmd; no need to do anything here */
148 return desc.err;
Andres Salomon392a3252012-07-10 19:31:51 -0700149}
150EXPORT_SYMBOL_GPL(olpc_ec_cmd);
Andres Salomonac250412012-07-13 05:57:17 -0700151
152static int olpc_ec_probe(struct platform_device *pdev)
153{
Andres Salomond278b7a2012-07-13 15:54:25 -0700154 struct olpc_ec_priv *ec;
Andres Salomonac250412012-07-13 05:57:17 -0700155 int err;
156
157 if (!ec_driver)
158 return -ENODEV;
159
Andres Salomond278b7a2012-07-13 15:54:25 -0700160 ec = kzalloc(sizeof(*ec), GFP_KERNEL);
161 if (!ec)
162 return -ENOMEM;
163 ec->drv = ec_driver;
164 ec_priv = ec;
165 platform_set_drvdata(pdev, ec);
166
Andres Salomonac250412012-07-13 05:57:17 -0700167 err = ec_driver->probe ? ec_driver->probe(pdev) : 0;
168
169 return err;
170}
171
172static int olpc_ec_suspend(struct device *dev)
173{
174 struct platform_device *pdev = to_platform_device(dev);
Andres Salomond278b7a2012-07-13 15:54:25 -0700175 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
176 int err = 0;
177
178 if (ec_driver->suspend)
179 err = ec_driver->suspend(pdev);
180 if (!err)
181 ec->suspended = true;
182
183 return err;
Andres Salomonac250412012-07-13 05:57:17 -0700184}
185
186static int olpc_ec_resume(struct device *dev)
187{
188 struct platform_device *pdev = to_platform_device(dev);
Andres Salomond278b7a2012-07-13 15:54:25 -0700189 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
190
191 ec->suspended = false;
Andres Salomonac250412012-07-13 05:57:17 -0700192 return ec_driver->resume ? ec_driver->resume(pdev) : 0;
193}
194
195static const struct dev_pm_ops olpc_ec_pm_ops = {
196 .suspend_late = olpc_ec_suspend,
197 .resume_early = olpc_ec_resume,
198};
199
200static struct platform_driver olpc_ec_plat_driver = {
201 .probe = olpc_ec_probe,
202 .driver = {
203 .name = "olpc-ec",
204 .pm = &olpc_ec_pm_ops,
205 },
206};
207
208static int __init olpc_ec_init_module(void)
209{
210 return platform_driver_register(&olpc_ec_plat_driver);
211}
212
213module_init(olpc_ec_init_module);
214
215MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
216MODULE_LICENSE("GPL");