44e6a4fae79b60c36bcac75c0a74b327ff7d4dd0
[profile/ivi/kernel-adaptation-intel-automotive.git] / drivers / platform / olpc / olpc-ec.c
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  */
8 #include <linux/completion.h>
9 #include <linux/spinlock.h>
10 #include <linux/mutex.h>
11 #include <linux/workqueue.h>
12 #include <linux/module.h>
13 #include <linux/list.h>
14 #include <linux/olpc-ec.h>
15 #include <asm/olpc.h>
16
17 struct ec_cmd_desc {
18         u8 cmd;
19         u8 *inbuf, *outbuf;
20         size_t inlen, outlen;
21
22         int err;
23         struct completion finished;
24         struct list_head node;
25
26         void *priv;
27 };
28
29 static void olpc_ec_worker(struct work_struct *w);
30
31 static DECLARE_WORK(ec_worker, olpc_ec_worker);
32 static LIST_HEAD(ec_cmd_q);
33 static DEFINE_SPINLOCK(ec_cmd_q_lock);
34
35 static struct olpc_ec_driver *ec_driver;
36 static void *ec_cb_arg;
37 static DEFINE_MUTEX(ec_cb_lock);
38
39 void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
40 {
41         ec_driver = drv;
42         ec_cb_arg = arg;
43 }
44 EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
45
46 static void olpc_ec_worker(struct work_struct *w)
47 {
48         struct ec_cmd_desc *desc = NULL;
49         unsigned long flags;
50
51         /* Grab the first pending command from the queue */
52         spin_lock_irqsave(&ec_cmd_q_lock, flags);
53         if (!list_empty(&ec_cmd_q)) {
54                 desc = list_first_entry(&ec_cmd_q, struct ec_cmd_desc, node);
55                 list_del(&desc->node);
56         }
57         spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
58
59         /* Do we actually have anything to do? */
60         if (!desc)
61                 return;
62
63         /* Protect the EC hw with a mutex; only run one cmd at a time */
64         mutex_lock(&ec_cb_lock);
65         desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
66                         desc->outbuf, desc->outlen, ec_cb_arg);
67         mutex_unlock(&ec_cb_lock);
68
69         /* Finished, wake up olpc_ec_cmd() */
70         complete(&desc->finished);
71
72         /* Run the worker thread again in case there are more cmds pending */
73         schedule_work(&ec_worker);
74 }
75
76 /*
77  * Throw a cmd descripter onto the list.  We now have SMP OLPC machines, so
78  * locking is pretty critical.
79  */
80 static void queue_ec_descriptor(struct ec_cmd_desc *desc)
81 {
82         unsigned long flags;
83
84         INIT_LIST_HEAD(&desc->node);
85
86         spin_lock_irqsave(&ec_cmd_q_lock, flags);
87         list_add_tail(&desc->node, &ec_cmd_q);
88         spin_unlock_irqrestore(&ec_cmd_q_lock, flags);
89
90         schedule_work(&ec_worker);
91 }
92
93 int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
94 {
95         struct ec_cmd_desc desc;
96
97         /* XXX: this will be removed in later patches */
98         /* Are we using old-style callers? */
99         if (!ec_driver || !ec_driver->ec_cmd)
100                 return olpc_ec_cmd_x86(cmd, inbuf, inlen, outbuf, outlen);
101
102         /* Ensure a driver and ec hook have been registered */
103         if (WARN_ON(!ec_driver || !ec_driver->ec_cmd))
104                 return -ENODEV;
105
106         might_sleep();
107
108         desc.cmd = cmd;
109         desc.inbuf = inbuf;
110         desc.outbuf = outbuf;
111         desc.inlen = inlen;
112         desc.outlen = outlen;
113         desc.err = 0;
114         init_completion(&desc.finished);
115
116         queue_ec_descriptor(&desc);
117
118         /* Timeouts must be handled in the platform-specific EC hook */
119         wait_for_completion(&desc.finished);
120
121         /* The worker thread dequeues the cmd; no need to do anything here */
122         return desc.err;
123 }
124 EXPORT_SYMBOL_GPL(olpc_ec_cmd);