2 * Generic driver for the OLPC Embedded Controller.
4 * Author: Andres Salomon <dilinger@queued.net>
6 * Copyright (C) 2011-2012 One Laptop per Child Foundation.
8 * Licensed under the GPL v2 or later.
10 #include <linux/completion.h>
11 #include <linux/debugfs.h>
12 #include <linux/spinlock.h>
13 #include <linux/mutex.h>
14 #include <linux/platform_device.h>
15 #include <linux/slab.h>
16 #include <linux/workqueue.h>
17 #include <linux/init.h>
18 #include <linux/list.h>
19 #include <linux/olpc-ec.h>
28 struct completion finished
;
29 struct list_head node
;
35 struct olpc_ec_driver
*drv
;
36 struct work_struct worker
;
37 struct mutex cmd_lock
;
39 /* Pending EC commands */
40 struct list_head cmd_q
;
41 spinlock_t cmd_q_lock
;
43 struct dentry
*dbgfs_dir
;
46 * Running an EC command while suspending means we don't always finish
47 * the command before the machine suspends. This means that the EC
48 * is expecting the command protocol to finish, but we after a period
49 * of time (while the OS is asleep) the EC times out and restarts its
50 * idle loop. Meanwhile, the OS wakes up, thinks it's still in the
51 * middle of the command protocol, starts throwing random things at
52 * the EC... and everyone's uphappy.
57 static struct olpc_ec_driver
*ec_driver
;
58 static struct olpc_ec_priv
*ec_priv
;
59 static void *ec_cb_arg
;
61 void olpc_ec_driver_register(struct olpc_ec_driver
*drv
, void *arg
)
66 EXPORT_SYMBOL_GPL(olpc_ec_driver_register
);
68 static void olpc_ec_worker(struct work_struct
*w
)
70 struct olpc_ec_priv
*ec
= container_of(w
, struct olpc_ec_priv
, worker
);
71 struct ec_cmd_desc
*desc
= NULL
;
74 /* Grab the first pending command from the queue */
75 spin_lock_irqsave(&ec
->cmd_q_lock
, flags
);
76 if (!list_empty(&ec
->cmd_q
)) {
77 desc
= list_first_entry(&ec
->cmd_q
, struct ec_cmd_desc
, node
);
78 list_del(&desc
->node
);
80 spin_unlock_irqrestore(&ec
->cmd_q_lock
, flags
);
82 /* Do we actually have anything to do? */
86 /* Protect the EC hw with a mutex; only run one cmd at a time */
87 mutex_lock(&ec
->cmd_lock
);
88 desc
->err
= ec_driver
->ec_cmd(desc
->cmd
, desc
->inbuf
, desc
->inlen
,
89 desc
->outbuf
, desc
->outlen
, ec_cb_arg
);
90 mutex_unlock(&ec
->cmd_lock
);
92 /* Finished, wake up olpc_ec_cmd() */
93 complete(&desc
->finished
);
95 /* Run the worker thread again in case there are more cmds pending */
96 schedule_work(&ec
->worker
);
100 * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
101 * locking is pretty critical.
103 static void queue_ec_descriptor(struct ec_cmd_desc
*desc
,
104 struct olpc_ec_priv
*ec
)
108 INIT_LIST_HEAD(&desc
->node
);
110 spin_lock_irqsave(&ec
->cmd_q_lock
, flags
);
111 list_add_tail(&desc
->node
, &ec
->cmd_q
);
112 spin_unlock_irqrestore(&ec
->cmd_q_lock
, flags
);
114 schedule_work(&ec
->worker
);
117 int olpc_ec_cmd(u8 cmd
, u8
*inbuf
, size_t inlen
, u8
*outbuf
, size_t outlen
)
119 struct olpc_ec_priv
*ec
= ec_priv
;
120 struct ec_cmd_desc desc
;
122 /* Ensure a driver and ec hook have been registered */
123 if (WARN_ON(!ec_driver
|| !ec_driver
->ec_cmd
))
129 /* Suspending in the middle of a command hoses things really badly */
130 if (WARN_ON(ec
->suspended
))
137 desc
.outbuf
= outbuf
;
139 desc
.outlen
= outlen
;
141 init_completion(&desc
.finished
);
143 queue_ec_descriptor(&desc
, ec
);
145 /* Timeouts must be handled in the platform-specific EC hook */
146 wait_for_completion(&desc
.finished
);
148 /* The worker thread dequeues the cmd; no need to do anything here */
151 EXPORT_SYMBOL_GPL(olpc_ec_cmd
);
153 #ifdef CONFIG_DEBUG_FS
156 * debugfs support for "generic commands", to allow sending
157 * arbitrary EC commands from userspace.
160 #define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */
161 #define EC_MAX_CMD_REPLY (8)
163 static DEFINE_MUTEX(ec_dbgfs_lock
);
164 static unsigned char ec_dbgfs_resp
[EC_MAX_CMD_REPLY
];
165 static unsigned int ec_dbgfs_resp_bytes
;
167 static ssize_t
ec_dbgfs_cmd_write(struct file
*file
, const char __user
*buf
,
168 size_t size
, loff_t
*ppos
)
171 unsigned char ec_cmd
[EC_MAX_CMD_ARGS
];
172 unsigned int ec_cmd_int
[EC_MAX_CMD_ARGS
];
176 mutex_lock(&ec_dbgfs_lock
);
178 size
= simple_write_to_buffer(cmdbuf
, sizeof(cmdbuf
), ppos
, buf
, size
);
180 m
= sscanf(cmdbuf
, "%x:%u %x %x %x %x %x", &ec_cmd_int
[0],
181 &ec_dbgfs_resp_bytes
, &ec_cmd_int
[1], &ec_cmd_int
[2],
182 &ec_cmd_int
[3], &ec_cmd_int
[4], &ec_cmd_int
[5]);
183 if (m
< 2 || ec_dbgfs_resp_bytes
> EC_MAX_CMD_REPLY
) {
184 /* reset to prevent overflow on read */
185 ec_dbgfs_resp_bytes
= 0;
187 pr_debug("olpc-ec: bad ec cmd: cmd:response-count [arg1 [arg2 ...]]\n");
192 /* convert scanf'd ints to char */
193 ec_cmd_bytes
= m
- 2;
194 for (i
= 0; i
<= ec_cmd_bytes
; i
++)
195 ec_cmd
[i
] = ec_cmd_int
[i
];
197 pr_debug("olpc-ec: debugfs cmd 0x%02x with %d args %5ph, want %d returns\n",
198 ec_cmd
[0], ec_cmd_bytes
, ec_cmd
+ 1,
199 ec_dbgfs_resp_bytes
);
201 olpc_ec_cmd(ec_cmd
[0], (ec_cmd_bytes
== 0) ? NULL
: &ec_cmd
[1],
202 ec_cmd_bytes
, ec_dbgfs_resp
, ec_dbgfs_resp_bytes
);
204 pr_debug("olpc-ec: response %8ph (%d bytes expected)\n",
205 ec_dbgfs_resp
, ec_dbgfs_resp_bytes
);
208 mutex_unlock(&ec_dbgfs_lock
);
212 static ssize_t
ec_dbgfs_cmd_read(struct file
*file
, char __user
*buf
,
213 size_t size
, loff_t
*ppos
)
219 mutex_lock(&ec_dbgfs_lock
);
221 rp
+= sprintf(rp
, "%02x", ec_dbgfs_resp
[0]);
222 for (i
= 1; i
< ec_dbgfs_resp_bytes
; i
++)
223 rp
+= sprintf(rp
, ", %02x", ec_dbgfs_resp
[i
]);
224 mutex_unlock(&ec_dbgfs_lock
);
225 rp
+= sprintf(rp
, "\n");
228 return simple_read_from_buffer(buf
, size
, ppos
, respbuf
, r
);
231 static const struct file_operations ec_dbgfs_ops
= {
232 .write
= ec_dbgfs_cmd_write
,
233 .read
= ec_dbgfs_cmd_read
,
236 static struct dentry
*olpc_ec_setup_debugfs(void)
238 struct dentry
*dbgfs_dir
;
240 dbgfs_dir
= debugfs_create_dir("olpc-ec", NULL
);
241 if (IS_ERR_OR_NULL(dbgfs_dir
))
244 debugfs_create_file("cmd", 0600, dbgfs_dir
, NULL
, &ec_dbgfs_ops
);
251 static struct dentry
*olpc_ec_setup_debugfs(void)
256 #endif /* CONFIG_DEBUG_FS */
258 static int olpc_ec_probe(struct platform_device
*pdev
)
260 struct olpc_ec_priv
*ec
;
266 ec
= kzalloc(sizeof(*ec
), GFP_KERNEL
);
271 INIT_WORK(&ec
->worker
, olpc_ec_worker
);
272 mutex_init(&ec
->cmd_lock
);
274 INIT_LIST_HEAD(&ec
->cmd_q
);
275 spin_lock_init(&ec
->cmd_q_lock
);
278 platform_set_drvdata(pdev
, ec
);
280 err
= ec_driver
->probe
? ec_driver
->probe(pdev
) : 0;
285 ec
->dbgfs_dir
= olpc_ec_setup_debugfs();
291 static int olpc_ec_suspend(struct device
*dev
)
293 struct platform_device
*pdev
= to_platform_device(dev
);
294 struct olpc_ec_priv
*ec
= platform_get_drvdata(pdev
);
297 if (ec_driver
->suspend
)
298 err
= ec_driver
->suspend(pdev
);
300 ec
->suspended
= true;
305 static int olpc_ec_resume(struct device
*dev
)
307 struct platform_device
*pdev
= to_platform_device(dev
);
308 struct olpc_ec_priv
*ec
= platform_get_drvdata(pdev
);
310 ec
->suspended
= false;
311 return ec_driver
->resume
? ec_driver
->resume(pdev
) : 0;
314 static const struct dev_pm_ops olpc_ec_pm_ops
= {
315 .suspend_late
= olpc_ec_suspend
,
316 .resume_early
= olpc_ec_resume
,
319 static struct platform_driver olpc_ec_plat_driver
= {
320 .probe
= olpc_ec_probe
,
323 .pm
= &olpc_ec_pm_ops
,
327 static int __init
olpc_ec_init_module(void)
329 return platform_driver_register(&olpc_ec_plat_driver
);
331 arch_initcall(olpc_ec_init_module
);