Platform: OLPC: turn EC driver into a platform_driver
[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>
3d26c20b 12#include <linux/workqueue.h>
392a325c 13#include <linux/module.h>
3d26c20b
AS
14#include <linux/list.h>
15#include <linux/olpc-ec.h>
392a325c
AS
16#include <asm/olpc.h>
17
3d26c20b
AS
18struct ec_cmd_desc {
19 u8 cmd;
20 u8 *inbuf, *outbuf;
21 size_t inlen, outlen;
22
23 int err;
24 struct completion finished;
25 struct list_head node;
26
27 void *priv;
28};
29
30static void olpc_ec_worker(struct work_struct *w);
31
32static DECLARE_WORK(ec_worker, olpc_ec_worker);
33static LIST_HEAD(ec_cmd_q);
34static DEFINE_SPINLOCK(ec_cmd_q_lock);
35
36static struct olpc_ec_driver *ec_driver;
37static void *ec_cb_arg;
38static DEFINE_MUTEX(ec_cb_lock);
39
40void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
41{
42 ec_driver = drv;
43 ec_cb_arg = arg;
44}
45EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
46
47static void olpc_ec_worker(struct work_struct *w)
48{
49 struct ec_cmd_desc *desc = NULL;
50 unsigned long flags;
51
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);
57 }
58 spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
59
60 /* Do we actually have anything to do? */
61 if (!desc)
62 return;
63
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);
69
70 /* Finished, wake up olpc_ec_cmd() */
71 complete(&desc->finished);
72
73 /* Run the worker thread again in case there are more cmds pending */
74 schedule_work(&ec_worker);
75}
76
77/*
78 * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
79 * locking is pretty critical.
80 */
81static void queue_ec_descriptor(struct ec_cmd_desc *desc)
82{
83 unsigned long flags;
84
85 INIT_LIST_HEAD(&desc->node);
86
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);
90
91 schedule_work(&ec_worker);
92}
93
392a325c
AS
94int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
95{
3d26c20b
AS
96 struct ec_cmd_desc desc;
97
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);
102
103 /* Ensure a driver and ec hook have been registered */
104 if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
105 return -ENODEV;
106
107 might_sleep();
108
109 desc.cmd = cmd;
110 desc.inbuf = inbuf;
111 desc.outbuf = outbuf;
112 desc.inlen = inlen;
113 desc.outlen = outlen;
114 desc.err = 0;
115 init_completion(&desc.finished);
116
117 queue_ec_descriptor(&desc);
118
119 /* Timeouts must be handled in the platform-specific EC hook */
120 wait_for_completion(&desc.finished);
121
122 /* The worker thread dequeues the cmd; no need to do anything here */
123 return desc.err;
392a325c
AS
124}
125EXPORT_SYMBOL_GPL(olpc_ec_cmd);
ac250415
AS
126
127static int olpc_ec_probe(struct platform_device *pdev)
128{
129 int err;
130
131 if (!ec_driver)
132 return -ENODEV;
133
134 err = ec_driver->probe ? ec_driver->probe(pdev) : 0;
135
136 return err;
137}
138
139static int olpc_ec_suspend(struct device *dev)
140{
141 struct platform_device *pdev = to_platform_device(dev);
142 return ec_driver->suspend ? ec_driver->suspend(pdev) : 0;
143}
144
145static int olpc_ec_resume(struct device *dev)
146{
147 struct platform_device *pdev = to_platform_device(dev);
148 return ec_driver->resume ? ec_driver->resume(pdev) : 0;
149}
150
151static const struct dev_pm_ops olpc_ec_pm_ops = {
152 .suspend_late = olpc_ec_suspend,
153 .resume_early = olpc_ec_resume,
154};
155
156static struct platform_driver olpc_ec_plat_driver = {
157 .probe = olpc_ec_probe,
158 .driver = {
159 .name = "olpc-ec",
160 .pm = &olpc_ec_pm_ops,
161 },
162};
163
164static int __init olpc_ec_init_module(void)
165{
166 return platform_driver_register(&olpc_ec_plat_driver);
167}
168
169module_init(olpc_ec_init_module);
170
171MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>");
172MODULE_LICENSE("GPL");