x86: OLPC: switch over to using new EC driver on x86
[linux-2.6-block.git] / drivers / platform / olpc / olpc-ec.c
CommitLineData
392a325c
AS
1/*
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 */
3d26c20b
AS
8#include <linux/completion.h>
9#include <linux/spinlock.h>
10#include <linux/mutex.h>
ac250415 11#include <linux/platform_device.h>
d278b7a2 12#include <linux/slab.h>
3d26c20b 13#include <linux/workqueue.h>
392a325c 14#include <linux/module.h>
3d26c20b
AS
15#include <linux/list.h>
16#include <linux/olpc-ec.h>
392a325c
AS
17#include <asm/olpc.h>
18
3d26c20b
AS
19struct 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
d278b7a2
AS
31struct 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
3d26c20b
AS
46static 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;
d278b7a2 53static struct olpc_ec_priv *ec_priv;
3d26c20b
AS
54static 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
392a325c
AS
111int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
112{
d278b7a2 113 struct olpc_ec_priv *ec = ec_priv;
3d26c20b
AS
114 struct ec_cmd_desc desc;
115
3d26c20b
AS
116 /* Ensure a driver and ec hook have been registered */
117 if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
118 return -ENODEV;
119
d278b7a2
AS
120 if (!ec)
121 return -ENOMEM;
122
123 /* Suspending in the middle of a command hoses things really badly */
124 if (WARN_ON(ec->suspended))
125 return -EBUSY;
126
3d26c20b
AS
127 might_sleep();
128
129 desc.cmd = cmd;
130 desc.inbuf = inbuf;
131 desc.outbuf = outbuf;
132 desc.inlen = inlen;
133 desc.outlen = outlen;
134 desc.err = 0;
135 init_completion(&desc.finished);
136
137 queue_ec_descriptor(&desc);
138
139 /* Timeouts must be handled in the platform-specific EC hook */
140 wait_for_completion(&desc.finished);
141
142 /* The worker thread dequeues the cmd; no need to do anything here */
143 return desc.err;
392a325c
AS
144}
145EXPORT_SYMBOL_GPL(olpc_ec_cmd);
ac250415
AS
146
147static int olpc_ec_probe(struct platform_device *pdev)
148{
d278b7a2 149 struct olpc_ec_priv *ec;
ac250415
AS
150 int err;
151
152 if (!ec_driver)
153 return -ENODEV;
154
d278b7a2
AS
155 ec = kzalloc(sizeof(*ec), GFP_KERNEL);
156 if (!ec)
157 return -ENOMEM;
158 ec->drv = ec_driver;
159 ec_priv = ec;
160 platform_set_drvdata(pdev, ec);
161
ac250415
AS
162 err = ec_driver->probe ? ec_driver->probe(pdev) : 0;
163
164 return err;
165}
166
167static int olpc_ec_suspend(struct device *dev)
168{
169 struct platform_device *pdev = to_platform_device(dev);
d278b7a2
AS
170 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
171 int err = 0;
172
173 if (ec_driver->suspend)
174 err = ec_driver->suspend(pdev);
175 if (!err)
176 ec->suspended = true;
177
178 return err;
ac250415
AS
179}
180
181static int olpc_ec_resume(struct device *dev)
182{
183 struct platform_device *pdev = to_platform_device(dev);
d278b7a2
AS
184 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
185
186 ec->suspended = false;
ac250415
AS
187 return ec_driver->resume ? ec_driver->resume(pdev) : 0;
188}
189
190static const struct dev_pm_ops olpc_ec_pm_ops = {
191 .suspend_late = olpc_ec_suspend,
192 .resume_early = olpc_ec_resume,
193};
194
195static struct platform_driver olpc_ec_plat_driver = {
196 .probe = olpc_ec_probe,
197 .driver = {
198 .name = "olpc-ec",
199 .pm = &olpc_ec_pm_ops,
200 },
201};
202
203static int __init olpc_ec_init_module(void)
204{
205 return platform_driver_register(&olpc_ec_plat_driver);
206}
207
208module_init(olpc_ec_init_module);
209
210MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
211MODULE_LICENSE("GPL");