Andres Salomon | 392a325 | 2012-07-10 19:31:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Generic driver for the OLPC Embedded Controller. |
| 3 | * |
Paul Gortmaker | f48d149 | 2016-08-15 18:25:17 -0400 | [diff] [blame] | 4 | * Author: Andres Salomon <dilinger@queued.net> |
| 5 | * |
Andres Salomon | 392a325 | 2012-07-10 19:31:51 -0700 | [diff] [blame] | 6 | * Copyright (C) 2011-2012 One Laptop per Child Foundation. |
| 7 | * |
| 8 | * Licensed under the GPL v2 or later. |
| 9 | */ |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 10 | #include <linux/completion.h> |
Andres Salomon | 6cca83d4 | 2012-07-12 20:45:14 -0700 | [diff] [blame] | 11 | #include <linux/debugfs.h> |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/mutex.h> |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 14 | #include <linux/platform_device.h> |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 15 | #include <linux/slab.h> |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 16 | #include <linux/workqueue.h> |
Paul Gortmaker | f48d149 | 2016-08-15 18:25:17 -0400 | [diff] [blame] | 17 | #include <linux/init.h> |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 18 | #include <linux/list.h> |
| 19 | #include <linux/olpc-ec.h> |
Andres Salomon | 392a325 | 2012-07-10 19:31:51 -0700 | [diff] [blame] | 20 | #include <asm/olpc.h> |
| 21 | |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 22 | struct ec_cmd_desc { |
| 23 | u8 cmd; |
| 24 | u8 *inbuf, *outbuf; |
| 25 | size_t inlen, outlen; |
| 26 | |
| 27 | int err; |
| 28 | struct completion finished; |
| 29 | struct list_head node; |
| 30 | |
| 31 | void *priv; |
| 32 | }; |
| 33 | |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 34 | struct olpc_ec_priv { |
| 35 | struct olpc_ec_driver *drv; |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 36 | struct work_struct worker; |
| 37 | struct mutex cmd_lock; |
| 38 | |
| 39 | /* Pending EC commands */ |
| 40 | struct list_head cmd_q; |
| 41 | spinlock_t cmd_q_lock; |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 42 | |
Andres Salomon | 6cca83d4 | 2012-07-12 20:45:14 -0700 | [diff] [blame] | 43 | struct dentry *dbgfs_dir; |
| 44 | |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 45 | /* |
| 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. |
| 53 | */ |
| 54 | bool suspended; |
| 55 | }; |
| 56 | |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 57 | static struct olpc_ec_driver *ec_driver; |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 58 | static struct olpc_ec_priv *ec_priv; |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 59 | static void *ec_cb_arg; |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 60 | |
| 61 | void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg) |
| 62 | { |
| 63 | ec_driver = drv; |
| 64 | ec_cb_arg = arg; |
| 65 | } |
| 66 | EXPORT_SYMBOL_GPL(olpc_ec_driver_register); |
| 67 | |
| 68 | static void olpc_ec_worker(struct work_struct *w) |
| 69 | { |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 70 | struct olpc_ec_priv *ec = container_of(w, struct olpc_ec_priv, worker); |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 71 | struct ec_cmd_desc *desc = NULL; |
| 72 | unsigned long flags; |
| 73 | |
| 74 | /* Grab the first pending command from the queue */ |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 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); |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 78 | list_del(&desc->node); |
| 79 | } |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 80 | spin_unlock_irqrestore(&ec->cmd_q_lock, flags); |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 81 | |
| 82 | /* Do we actually have anything to do? */ |
| 83 | if (!desc) |
| 84 | return; |
| 85 | |
| 86 | /* Protect the EC hw with a mutex; only run one cmd at a time */ |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 87 | mutex_lock(&ec->cmd_lock); |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 88 | desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen, |
| 89 | desc->outbuf, desc->outlen, ec_cb_arg); |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 90 | mutex_unlock(&ec->cmd_lock); |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 91 | |
| 92 | /* Finished, wake up olpc_ec_cmd() */ |
| 93 | complete(&desc->finished); |
| 94 | |
| 95 | /* Run the worker thread again in case there are more cmds pending */ |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 96 | schedule_work(&ec->worker); |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so |
| 101 | * locking is pretty critical. |
| 102 | */ |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 103 | static void queue_ec_descriptor(struct ec_cmd_desc *desc, |
| 104 | struct olpc_ec_priv *ec) |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 105 | { |
| 106 | unsigned long flags; |
| 107 | |
| 108 | INIT_LIST_HEAD(&desc->node); |
| 109 | |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 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); |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 113 | |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 114 | schedule_work(&ec->worker); |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Andres Salomon | 392a325 | 2012-07-10 19:31:51 -0700 | [diff] [blame] | 117 | int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen) |
| 118 | { |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 119 | struct olpc_ec_priv *ec = ec_priv; |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 120 | struct ec_cmd_desc desc; |
| 121 | |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 122 | /* Ensure a driver and ec hook have been registered */ |
| 123 | if (WARN_ON(!ec_driver || !ec_driver->ec_cmd)) |
| 124 | return -ENODEV; |
| 125 | |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 126 | if (!ec) |
| 127 | return -ENOMEM; |
| 128 | |
| 129 | /* Suspending in the middle of a command hoses things really badly */ |
| 130 | if (WARN_ON(ec->suspended)) |
| 131 | return -EBUSY; |
| 132 | |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 133 | might_sleep(); |
| 134 | |
| 135 | desc.cmd = cmd; |
| 136 | desc.inbuf = inbuf; |
| 137 | desc.outbuf = outbuf; |
| 138 | desc.inlen = inlen; |
| 139 | desc.outlen = outlen; |
| 140 | desc.err = 0; |
| 141 | init_completion(&desc.finished); |
| 142 | |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 143 | queue_ec_descriptor(&desc, ec); |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 144 | |
| 145 | /* Timeouts must be handled in the platform-specific EC hook */ |
| 146 | wait_for_completion(&desc.finished); |
| 147 | |
| 148 | /* The worker thread dequeues the cmd; no need to do anything here */ |
| 149 | return desc.err; |
Andres Salomon | 392a325 | 2012-07-10 19:31:51 -0700 | [diff] [blame] | 150 | } |
| 151 | EXPORT_SYMBOL_GPL(olpc_ec_cmd); |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 152 | |
Andres Salomon | 6cca83d4 | 2012-07-12 20:45:14 -0700 | [diff] [blame] | 153 | #ifdef CONFIG_DEBUG_FS |
| 154 | |
| 155 | /* |
| 156 | * debugfs support for "generic commands", to allow sending |
| 157 | * arbitrary EC commands from userspace. |
| 158 | */ |
| 159 | |
| 160 | #define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */ |
| 161 | #define EC_MAX_CMD_REPLY (8) |
| 162 | |
| 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; |
| 166 | |
| 167 | static ssize_t ec_dbgfs_cmd_write(struct file *file, const char __user *buf, |
| 168 | size_t size, loff_t *ppos) |
| 169 | { |
| 170 | int i, m; |
| 171 | unsigned char ec_cmd[EC_MAX_CMD_ARGS]; |
| 172 | unsigned int ec_cmd_int[EC_MAX_CMD_ARGS]; |
| 173 | char cmdbuf[64]; |
| 174 | int ec_cmd_bytes; |
| 175 | |
| 176 | mutex_lock(&ec_dbgfs_lock); |
| 177 | |
| 178 | size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size); |
| 179 | |
| 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; |
| 186 | |
| 187 | pr_debug("olpc-ec: bad ec cmd: cmd:response-count [arg1 [arg2 ...]]\n"); |
| 188 | size = -EINVAL; |
| 189 | goto out; |
| 190 | } |
| 191 | |
| 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]; |
| 196 | |
Andy Shevchenko | 7d3777d | 2014-12-29 14:26:50 +0200 | [diff] [blame] | 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); |
Andres Salomon | 6cca83d4 | 2012-07-12 20:45:14 -0700 | [diff] [blame] | 200 | |
| 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); |
| 203 | |
Andy Shevchenko | 7d3777d | 2014-12-29 14:26:50 +0200 | [diff] [blame] | 204 | pr_debug("olpc-ec: response %8ph (%d bytes expected)\n", |
| 205 | ec_dbgfs_resp, ec_dbgfs_resp_bytes); |
Andres Salomon | 6cca83d4 | 2012-07-12 20:45:14 -0700 | [diff] [blame] | 206 | |
| 207 | out: |
| 208 | mutex_unlock(&ec_dbgfs_lock); |
| 209 | return size; |
| 210 | } |
| 211 | |
| 212 | static ssize_t ec_dbgfs_cmd_read(struct file *file, char __user *buf, |
| 213 | size_t size, loff_t *ppos) |
| 214 | { |
| 215 | unsigned int i, r; |
| 216 | char *rp; |
| 217 | char respbuf[64]; |
| 218 | |
| 219 | mutex_lock(&ec_dbgfs_lock); |
| 220 | rp = respbuf; |
| 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"); |
| 226 | |
| 227 | r = rp - respbuf; |
| 228 | return simple_read_from_buffer(buf, size, ppos, respbuf, r); |
| 229 | } |
| 230 | |
| 231 | static const struct file_operations ec_dbgfs_ops = { |
| 232 | .write = ec_dbgfs_cmd_write, |
| 233 | .read = ec_dbgfs_cmd_read, |
| 234 | }; |
| 235 | |
| 236 | static struct dentry *olpc_ec_setup_debugfs(void) |
| 237 | { |
| 238 | struct dentry *dbgfs_dir; |
| 239 | |
| 240 | dbgfs_dir = debugfs_create_dir("olpc-ec", NULL); |
| 241 | if (IS_ERR_OR_NULL(dbgfs_dir)) |
| 242 | return NULL; |
| 243 | |
| 244 | debugfs_create_file("cmd", 0600, dbgfs_dir, NULL, &ec_dbgfs_ops); |
| 245 | |
| 246 | return dbgfs_dir; |
| 247 | } |
| 248 | |
| 249 | #else |
| 250 | |
| 251 | static struct dentry *olpc_ec_setup_debugfs(void) |
| 252 | { |
| 253 | return NULL; |
| 254 | } |
| 255 | |
| 256 | #endif /* CONFIG_DEBUG_FS */ |
| 257 | |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 258 | static int olpc_ec_probe(struct platform_device *pdev) |
| 259 | { |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 260 | struct olpc_ec_priv *ec; |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 261 | int err; |
| 262 | |
| 263 | if (!ec_driver) |
| 264 | return -ENODEV; |
| 265 | |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 266 | ec = kzalloc(sizeof(*ec), GFP_KERNEL); |
| 267 | if (!ec) |
| 268 | return -ENOMEM; |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 269 | |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 270 | ec->drv = ec_driver; |
Andres Salomon | 99ecb01 | 2012-07-13 17:10:45 -0700 | [diff] [blame] | 271 | INIT_WORK(&ec->worker, olpc_ec_worker); |
| 272 | mutex_init(&ec->cmd_lock); |
| 273 | |
| 274 | INIT_LIST_HEAD(&ec->cmd_q); |
| 275 | spin_lock_init(&ec->cmd_q_lock); |
| 276 | |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 277 | ec_priv = ec; |
| 278 | platform_set_drvdata(pdev, ec); |
| 279 | |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 280 | err = ec_driver->probe ? ec_driver->probe(pdev) : 0; |
Andres Salomon | 6cca83d4 | 2012-07-12 20:45:14 -0700 | [diff] [blame] | 281 | if (err) { |
| 282 | ec_priv = NULL; |
| 283 | kfree(ec); |
| 284 | } else { |
| 285 | ec->dbgfs_dir = olpc_ec_setup_debugfs(); |
| 286 | } |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 287 | |
| 288 | return err; |
| 289 | } |
| 290 | |
| 291 | static int olpc_ec_suspend(struct device *dev) |
| 292 | { |
| 293 | struct platform_device *pdev = to_platform_device(dev); |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 294 | struct olpc_ec_priv *ec = platform_get_drvdata(pdev); |
| 295 | int err = 0; |
| 296 | |
| 297 | if (ec_driver->suspend) |
| 298 | err = ec_driver->suspend(pdev); |
| 299 | if (!err) |
| 300 | ec->suspended = true; |
| 301 | |
| 302 | return err; |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static int olpc_ec_resume(struct device *dev) |
| 306 | { |
| 307 | struct platform_device *pdev = to_platform_device(dev); |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 308 | struct olpc_ec_priv *ec = platform_get_drvdata(pdev); |
| 309 | |
| 310 | ec->suspended = false; |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 311 | return ec_driver->resume ? ec_driver->resume(pdev) : 0; |
| 312 | } |
| 313 | |
| 314 | static const struct dev_pm_ops olpc_ec_pm_ops = { |
| 315 | .suspend_late = olpc_ec_suspend, |
| 316 | .resume_early = olpc_ec_resume, |
| 317 | }; |
| 318 | |
| 319 | static struct platform_driver olpc_ec_plat_driver = { |
| 320 | .probe = olpc_ec_probe, |
| 321 | .driver = { |
| 322 | .name = "olpc-ec", |
| 323 | .pm = &olpc_ec_pm_ops, |
| 324 | }, |
| 325 | }; |
| 326 | |
| 327 | static int __init olpc_ec_init_module(void) |
| 328 | { |
| 329 | return platform_driver_register(&olpc_ec_plat_driver); |
| 330 | } |
Daniel Drake | 93dbc1b | 2013-08-22 16:35:43 -0700 | [diff] [blame] | 331 | arch_initcall(olpc_ec_init_module); |