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> |
| 9 | #include <linux/spinlock.h> |
| 10 | #include <linux/mutex.h> |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 11 | #include <linux/platform_device.h> |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 12 | #include <linux/slab.h> |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 13 | #include <linux/workqueue.h> |
Andres Salomon | 392a325 | 2012-07-10 19:31:51 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 15 | #include <linux/list.h> |
| 16 | #include <linux/olpc-ec.h> |
Andres Salomon | 392a325 | 2012-07-10 19:31:51 -0700 | [diff] [blame] | 17 | #include <asm/olpc.h> |
| 18 | |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 19 | struct ec_cmd_desc { |
| 20 | u8 cmd; |
| 21 | u8 *inbuf, *outbuf; |
| 22 | size_t inlen, outlen; |
| 23 | |
| 24 | int err; |
| 25 | struct completion finished; |
| 26 | struct list_head node; |
| 27 | |
| 28 | void *priv; |
| 29 | }; |
| 30 | |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 31 | struct olpc_ec_priv { |
| 32 | struct olpc_ec_driver *drv; |
| 33 | |
| 34 | /* |
| 35 | * Running an EC command while suspending means we don't always finish |
| 36 | * the command before the machine suspends. This means that the EC |
| 37 | * is expecting the command protocol to finish, but we after a period |
| 38 | * of time (while the OS is asleep) the EC times out and restarts its |
| 39 | * idle loop. Meanwhile, the OS wakes up, thinks it's still in the |
| 40 | * middle of the command protocol, starts throwing random things at |
| 41 | * the EC... and everyone's uphappy. |
| 42 | */ |
| 43 | bool suspended; |
| 44 | }; |
| 45 | |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 46 | static void olpc_ec_worker(struct work_struct *w); |
| 47 | |
| 48 | static DECLARE_WORK(ec_worker, olpc_ec_worker); |
| 49 | static LIST_HEAD(ec_cmd_q); |
| 50 | static DEFINE_SPINLOCK(ec_cmd_q_lock); |
| 51 | |
| 52 | static struct olpc_ec_driver *ec_driver; |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 53 | static struct olpc_ec_priv *ec_priv; |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 54 | static void *ec_cb_arg; |
| 55 | static DEFINE_MUTEX(ec_cb_lock); |
| 56 | |
| 57 | void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg) |
| 58 | { |
| 59 | ec_driver = drv; |
| 60 | ec_cb_arg = arg; |
| 61 | } |
| 62 | EXPORT_SYMBOL_GPL(olpc_ec_driver_register); |
| 63 | |
| 64 | static void olpc_ec_worker(struct work_struct *w) |
| 65 | { |
| 66 | struct ec_cmd_desc *desc = NULL; |
| 67 | unsigned long flags; |
| 68 | |
| 69 | /* Grab the first pending command from the queue */ |
| 70 | spin_lock_irqsave(&ec_cmd_q_lock, flags); |
| 71 | if (!list_empty(&ec_cmd_q)) { |
| 72 | desc = list_first_entry(&ec_cmd_q, struct ec_cmd_desc, node); |
| 73 | list_del(&desc->node); |
| 74 | } |
| 75 | spin_unlock_irqrestore(&ec_cmd_q_lock, flags); |
| 76 | |
| 77 | /* Do we actually have anything to do? */ |
| 78 | if (!desc) |
| 79 | return; |
| 80 | |
| 81 | /* Protect the EC hw with a mutex; only run one cmd at a time */ |
| 82 | mutex_lock(&ec_cb_lock); |
| 83 | desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen, |
| 84 | desc->outbuf, desc->outlen, ec_cb_arg); |
| 85 | mutex_unlock(&ec_cb_lock); |
| 86 | |
| 87 | /* Finished, wake up olpc_ec_cmd() */ |
| 88 | complete(&desc->finished); |
| 89 | |
| 90 | /* Run the worker thread again in case there are more cmds pending */ |
| 91 | schedule_work(&ec_worker); |
| 92 | } |
| 93 | |
| 94 | /* |
| 95 | * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so |
| 96 | * locking is pretty critical. |
| 97 | */ |
| 98 | static void queue_ec_descriptor(struct ec_cmd_desc *desc) |
| 99 | { |
| 100 | unsigned long flags; |
| 101 | |
| 102 | INIT_LIST_HEAD(&desc->node); |
| 103 | |
| 104 | spin_lock_irqsave(&ec_cmd_q_lock, flags); |
| 105 | list_add_tail(&desc->node, &ec_cmd_q); |
| 106 | spin_unlock_irqrestore(&ec_cmd_q_lock, flags); |
| 107 | |
| 108 | schedule_work(&ec_worker); |
| 109 | } |
| 110 | |
Andres Salomon | 392a325 | 2012-07-10 19:31:51 -0700 | [diff] [blame] | 111 | int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen) |
| 112 | { |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 113 | struct olpc_ec_priv *ec = ec_priv; |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 114 | struct ec_cmd_desc desc; |
| 115 | |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 116 | /* Ensure a driver and ec hook have been registered */ |
| 117 | if (WARN_ON(!ec_driver || !ec_driver->ec_cmd)) |
| 118 | return -ENODEV; |
| 119 | |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 120 | if (!ec) |
| 121 | return -ENOMEM; |
| 122 | |
| 123 | /* Suspending in the middle of a command hoses things really badly */ |
| 124 | if (WARN_ON(ec->suspended)) |
| 125 | return -EBUSY; |
| 126 | |
Andres Salomon | 3d26c20 | 2012-07-11 17:40:25 -0700 | [diff] [blame] | 127 | might_sleep(); |
| 128 | |
| 129 | desc.cmd = cmd; |
| 130 | desc.inbuf = inbuf; |
| 131 | desc.outbuf = outbuf; |
| 132 | desc.inlen = inlen; |
| 133 | desc.outlen = outlen; |
| 134 | desc.err = 0; |
| 135 | init_completion(&desc.finished); |
| 136 | |
| 137 | queue_ec_descriptor(&desc); |
| 138 | |
| 139 | /* Timeouts must be handled in the platform-specific EC hook */ |
| 140 | wait_for_completion(&desc.finished); |
| 141 | |
| 142 | /* The worker thread dequeues the cmd; no need to do anything here */ |
| 143 | return desc.err; |
Andres Salomon | 392a325 | 2012-07-10 19:31:51 -0700 | [diff] [blame] | 144 | } |
| 145 | EXPORT_SYMBOL_GPL(olpc_ec_cmd); |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 146 | |
| 147 | static int olpc_ec_probe(struct platform_device *pdev) |
| 148 | { |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 149 | struct olpc_ec_priv *ec; |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 150 | int err; |
| 151 | |
| 152 | if (!ec_driver) |
| 153 | return -ENODEV; |
| 154 | |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 155 | ec = kzalloc(sizeof(*ec), GFP_KERNEL); |
| 156 | if (!ec) |
| 157 | return -ENOMEM; |
| 158 | ec->drv = ec_driver; |
| 159 | ec_priv = ec; |
| 160 | platform_set_drvdata(pdev, ec); |
| 161 | |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 162 | err = ec_driver->probe ? ec_driver->probe(pdev) : 0; |
| 163 | |
| 164 | return err; |
| 165 | } |
| 166 | |
| 167 | static int olpc_ec_suspend(struct device *dev) |
| 168 | { |
| 169 | struct platform_device *pdev = to_platform_device(dev); |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 170 | struct olpc_ec_priv *ec = platform_get_drvdata(pdev); |
| 171 | int err = 0; |
| 172 | |
| 173 | if (ec_driver->suspend) |
| 174 | err = ec_driver->suspend(pdev); |
| 175 | if (!err) |
| 176 | ec->suspended = true; |
| 177 | |
| 178 | return err; |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static int olpc_ec_resume(struct device *dev) |
| 182 | { |
| 183 | struct platform_device *pdev = to_platform_device(dev); |
Andres Salomon | d278b7a | 2012-07-13 15:54:25 -0700 | [diff] [blame] | 184 | struct olpc_ec_priv *ec = platform_get_drvdata(pdev); |
| 185 | |
| 186 | ec->suspended = false; |
Andres Salomon | ac25041 | 2012-07-13 05:57:17 -0700 | [diff] [blame] | 187 | return ec_driver->resume ? ec_driver->resume(pdev) : 0; |
| 188 | } |
| 189 | |
| 190 | static const struct dev_pm_ops olpc_ec_pm_ops = { |
| 191 | .suspend_late = olpc_ec_suspend, |
| 192 | .resume_early = olpc_ec_resume, |
| 193 | }; |
| 194 | |
| 195 | static struct platform_driver olpc_ec_plat_driver = { |
| 196 | .probe = olpc_ec_probe, |
| 197 | .driver = { |
| 198 | .name = "olpc-ec", |
| 199 | .pm = &olpc_ec_pm_ops, |
| 200 | }, |
| 201 | }; |
| 202 | |
| 203 | static int __init olpc_ec_init_module(void) |
| 204 | { |
| 205 | return platform_driver_register(&olpc_ec_plat_driver); |
| 206 | } |
| 207 | |
| 208 | module_init(olpc_ec_init_module); |
| 209 | |
| 210 | MODULE_AUTHOR("Andres Salomon <dilinger@queued.net>"); |
| 211 | MODULE_LICENSE("GPL"); |