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