1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Generic driver for the OLPC Embedded Controller.
7 * Copyright (C) 2011-2012 One Laptop per Child Foundation.
9 #include <linux/completion.h>
10 #include <linux/debugfs.h>
11 #include <linux/spinlock.h>
12 #include <linux/mutex.h>
13 #include <linux/platform_device.h>
14 #include <linux/slab.h>
15 #include <linux/workqueue.h>
16 #include <linux/init.h>
17 #include <linux/list.h>
18 #include <linux/regulator/driver.h>
19 #include <linux/olpc-ec.h>
27 struct completion finished;
28 struct list_head node;
34 struct olpc_ec_driver *drv;
36 struct work_struct worker;
37 struct mutex cmd_lock;
42 /* Pending EC commands */
43 struct list_head cmd_q;
44 spinlock_t cmd_q_lock;
46 struct dentry *dbgfs_dir;
49 * EC event mask to be applied during suspend (defining wakeup
55 * Running an EC command while suspending means we don't always finish
56 * the command before the machine suspends. This means that the EC
57 * is expecting the command protocol to finish, but we after a period
58 * of time (while the OS is asleep) the EC times out and restarts its
59 * idle loop. Meanwhile, the OS wakes up, thinks it's still in the
60 * middle of the command protocol, starts throwing random things at
61 * the EC... and everyone's uphappy.
66 static struct olpc_ec_driver *ec_driver;
67 static struct olpc_ec_priv *ec_priv;
68 static void *ec_cb_arg;
70 void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
75 EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
77 static void olpc_ec_worker(struct work_struct *w)
79 struct olpc_ec_priv *ec = container_of(w, struct olpc_ec_priv, worker);
80 struct ec_cmd_desc *desc = NULL;
83 /* Grab the first pending command from the queue */
84 spin_lock_irqsave(&ec->cmd_q_lock, flags);
85 if (!list_empty(&ec->cmd_q)) {
86 desc = list_first_entry(&ec->cmd_q, struct ec_cmd_desc, node);
87 list_del(&desc->node);
89 spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
91 /* Do we actually have anything to do? */
95 /* Protect the EC hw with a mutex; only run one cmd at a time */
96 mutex_lock(&ec->cmd_lock);
97 desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
98 desc->outbuf, desc->outlen, ec_cb_arg);
99 mutex_unlock(&ec->cmd_lock);
101 /* Finished, wake up olpc_ec_cmd() */
102 complete(&desc->finished);
104 /* Run the worker thread again in case there are more cmds pending */
105 schedule_work(&ec->worker);
109 * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
110 * locking is pretty critical.
112 static void queue_ec_descriptor(struct ec_cmd_desc *desc,
113 struct olpc_ec_priv *ec)
117 INIT_LIST_HEAD(&desc->node);
119 spin_lock_irqsave(&ec->cmd_q_lock, flags);
120 list_add_tail(&desc->node, &ec->cmd_q);
121 spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
123 schedule_work(&ec->worker);
126 int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
128 struct olpc_ec_priv *ec = ec_priv;
129 struct ec_cmd_desc desc;
131 /* Driver not yet registered. */
133 return -EPROBE_DEFER;
135 if (WARN_ON(!ec_driver->ec_cmd))
141 /* Suspending in the middle of a command hoses things really badly */
142 if (WARN_ON(ec->suspended))
149 desc.outbuf = outbuf;
151 desc.outlen = outlen;
153 init_completion(&desc.finished);
155 queue_ec_descriptor(&desc, ec);
157 /* Timeouts must be handled in the platform-specific EC hook */
158 wait_for_completion(&desc.finished);
160 /* The worker thread dequeues the cmd; no need to do anything here */
163 EXPORT_SYMBOL_GPL(olpc_ec_cmd);
165 void olpc_ec_wakeup_set(u16 value)
167 struct olpc_ec_priv *ec = ec_priv;
172 ec->ec_wakeup_mask |= value;
174 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set);
176 void olpc_ec_wakeup_clear(u16 value)
178 struct olpc_ec_priv *ec = ec_priv;
183 ec->ec_wakeup_mask &= ~value;
185 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear);
187 int olpc_ec_mask_write(u16 bits)
189 struct olpc_ec_priv *ec = ec_priv;
194 /* EC version 0x5f adds support for wide SCI mask */
195 if (ec->version >= 0x5f) {
196 __be16 ec_word = cpu_to_be16(bits);
198 return olpc_ec_cmd(EC_WRITE_EXT_SCI_MASK, (void *)&ec_word, 2, NULL, 0);
200 u8 ec_byte = bits & 0xff;
202 return olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0);
205 EXPORT_SYMBOL_GPL(olpc_ec_mask_write);
208 * Returns true if the compile and runtime configurations allow for EC events
209 * to wake the system.
211 bool olpc_ec_wakeup_available(void)
213 if (WARN_ON(!ec_driver))
216 return ec_driver->wakeup_available;
218 EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available);
220 int olpc_ec_sci_query(u16 *sci_value)
222 struct olpc_ec_priv *ec = ec_priv;
228 /* EC version 0x5f adds support for wide SCI mask */
229 if (ec->version >= 0x5f) {
232 ret = olpc_ec_cmd(EC_EXT_SCI_QUERY, NULL, 0, (void *)&ec_word, 2);
234 *sci_value = be16_to_cpu(ec_word);
238 ret = olpc_ec_cmd(EC_SCI_QUERY, NULL, 0, &ec_byte, 1);
240 *sci_value = ec_byte;
245 EXPORT_SYMBOL_GPL(olpc_ec_sci_query);
247 #ifdef CONFIG_DEBUG_FS
250 * debugfs support for "generic commands", to allow sending
251 * arbitrary EC commands from userspace.
254 #define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */
255 #define EC_MAX_CMD_REPLY (8)
257 static DEFINE_MUTEX(ec_dbgfs_lock);
258 static unsigned char ec_dbgfs_resp[EC_MAX_CMD_REPLY];
259 static unsigned int ec_dbgfs_resp_bytes;
261 static ssize_t ec_dbgfs_cmd_write(struct file *file, const char __user *buf,
262 size_t size, loff_t *ppos)
265 unsigned char ec_cmd[EC_MAX_CMD_ARGS];
266 unsigned int ec_cmd_int[EC_MAX_CMD_ARGS];
267 char cmdbuf[64] = "";
270 mutex_lock(&ec_dbgfs_lock);
272 size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
274 m = sscanf(cmdbuf, "%x:%u %x %x %x %x %x", &ec_cmd_int[0],
275 &ec_dbgfs_resp_bytes, &ec_cmd_int[1], &ec_cmd_int[2],
276 &ec_cmd_int[3], &ec_cmd_int[4], &ec_cmd_int[5]);
277 if (m < 2 || ec_dbgfs_resp_bytes > EC_MAX_CMD_REPLY) {
278 /* reset to prevent overflow on read */
279 ec_dbgfs_resp_bytes = 0;
281 pr_debug("olpc-ec: bad ec cmd: cmd:response-count [arg1 [arg2 ...]]\n");
286 /* convert scanf'd ints to char */
287 ec_cmd_bytes = m - 2;
288 for (i = 0; i <= ec_cmd_bytes; i++)
289 ec_cmd[i] = ec_cmd_int[i];
291 pr_debug("olpc-ec: debugfs cmd 0x%02x with %d args %5ph, want %d returns\n",
292 ec_cmd[0], ec_cmd_bytes, ec_cmd + 1,
293 ec_dbgfs_resp_bytes);
295 olpc_ec_cmd(ec_cmd[0], (ec_cmd_bytes == 0) ? NULL : &ec_cmd[1],
296 ec_cmd_bytes, ec_dbgfs_resp, ec_dbgfs_resp_bytes);
298 pr_debug("olpc-ec: response %8ph (%d bytes expected)\n",
299 ec_dbgfs_resp, ec_dbgfs_resp_bytes);
302 mutex_unlock(&ec_dbgfs_lock);
306 static ssize_t ec_dbgfs_cmd_read(struct file *file, char __user *buf,
307 size_t size, loff_t *ppos)
313 mutex_lock(&ec_dbgfs_lock);
315 rp += sprintf(rp, "%02x", ec_dbgfs_resp[0]);
316 for (i = 1; i < ec_dbgfs_resp_bytes; i++)
317 rp += sprintf(rp, ", %02x", ec_dbgfs_resp[i]);
318 mutex_unlock(&ec_dbgfs_lock);
319 rp += sprintf(rp, "\n");
322 return simple_read_from_buffer(buf, size, ppos, respbuf, r);
325 static const struct file_operations ec_dbgfs_ops = {
326 .write = ec_dbgfs_cmd_write,
327 .read = ec_dbgfs_cmd_read,
330 static struct dentry *olpc_ec_setup_debugfs(void)
332 struct dentry *dbgfs_dir;
334 dbgfs_dir = debugfs_create_dir("olpc-ec", NULL);
335 if (IS_ERR_OR_NULL(dbgfs_dir))
338 debugfs_create_file("cmd", 0600, dbgfs_dir, NULL, &ec_dbgfs_ops);
345 static struct dentry *olpc_ec_setup_debugfs(void)
350 #endif /* CONFIG_DEBUG_FS */
352 static int olpc_ec_set_dcon_power(struct olpc_ec_priv *ec, bool state)
354 unsigned char ec_byte = state;
357 if (ec->dcon_enabled == state)
360 ret = olpc_ec_cmd(EC_DCON_POWER_MODE, &ec_byte, 1, NULL, 0);
364 ec->dcon_enabled = state;
368 static int dcon_regulator_enable(struct regulator_dev *rdev)
370 struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
372 return olpc_ec_set_dcon_power(ec, true);
375 static int dcon_regulator_disable(struct regulator_dev *rdev)
377 struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
379 return olpc_ec_set_dcon_power(ec, false);
382 static int dcon_regulator_is_enabled(struct regulator_dev *rdev)
384 struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
386 return ec->dcon_enabled ? 1 : 0;
389 static const struct regulator_ops dcon_regulator_ops = {
390 .enable = dcon_regulator_enable,
391 .disable = dcon_regulator_disable,
392 .is_enabled = dcon_regulator_is_enabled,
395 static const struct regulator_desc dcon_desc = {
398 .ops = &dcon_regulator_ops,
399 .type = REGULATOR_VOLTAGE,
400 .owner = THIS_MODULE,
401 .enable_time = 25000,
404 static int olpc_ec_probe(struct platform_device *pdev)
406 struct olpc_ec_priv *ec;
407 struct regulator_config config = { };
408 struct regulator_dev *regulator;
414 ec = kzalloc(sizeof(*ec), GFP_KERNEL);
419 INIT_WORK(&ec->worker, olpc_ec_worker);
420 mutex_init(&ec->cmd_lock);
422 INIT_LIST_HEAD(&ec->cmd_q);
423 spin_lock_init(&ec->cmd_q_lock);
426 platform_set_drvdata(pdev, ec);
428 /* get the EC revision */
429 err = olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, &ec->version, 1);
433 config.dev = pdev->dev.parent;
434 config.driver_data = ec;
435 ec->dcon_enabled = true;
436 regulator = devm_regulator_register(&pdev->dev, &dcon_desc, &config);
437 if (IS_ERR(regulator)) {
438 dev_err(&pdev->dev, "failed to register DCON regulator\n");
439 err = PTR_ERR(regulator);
443 ec->dbgfs_dir = olpc_ec_setup_debugfs();
453 static int olpc_ec_suspend(struct device *dev)
455 struct platform_device *pdev = to_platform_device(dev);
456 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
459 olpc_ec_mask_write(ec->ec_wakeup_mask);
461 if (ec_driver->suspend)
462 err = ec_driver->suspend(pdev);
464 ec->suspended = true;
469 static int olpc_ec_resume(struct device *dev)
471 struct platform_device *pdev = to_platform_device(dev);
472 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
474 ec->suspended = false;
475 return ec_driver->resume ? ec_driver->resume(pdev) : 0;
478 static const struct dev_pm_ops olpc_ec_pm_ops = {
479 .suspend_late = olpc_ec_suspend,
480 .resume_early = olpc_ec_resume,
483 static struct platform_driver olpc_ec_plat_driver = {
484 .probe = olpc_ec_probe,
487 .pm = &olpc_ec_pm_ops,
491 static int __init olpc_ec_init_module(void)
493 return platform_driver_register(&olpc_ec_plat_driver);
495 arch_initcall(olpc_ec_init_module);