]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blame - drivers/platform/olpc/olpc-ec.c
Platform: OLPC: Fix probe error handling
[mirror_ubuntu-focal-kernel.git] / drivers / platform / olpc / olpc-ec.c
CommitLineData
ff206db4 1// SPDX-License-Identifier: GPL-2.0-or-later
392a325c
AS
2/*
3 * Generic driver for the OLPC Embedded Controller.
4 *
f48d1496
PG
5 * Author: Andres Salomon <dilinger@queued.net>
6 *
392a325c 7 * Copyright (C) 2011-2012 One Laptop per Child Foundation.
392a325c 8 */
3d26c20b 9#include <linux/completion.h>
6cca83d4 10#include <linux/debugfs.h>
3d26c20b
AS
11#include <linux/spinlock.h>
12#include <linux/mutex.h>
ac250415 13#include <linux/platform_device.h>
d278b7a2 14#include <linux/slab.h>
3d26c20b 15#include <linux/workqueue.h>
f48d1496 16#include <linux/init.h>
3d26c20b 17#include <linux/list.h>
231c0c21 18#include <linux/regulator/driver.h>
3d26c20b 19#include <linux/olpc-ec.h>
392a325c 20
3d26c20b
AS
21struct ec_cmd_desc {
22 u8 cmd;
23 u8 *inbuf, *outbuf;
24 size_t inlen, outlen;
25
26 int err;
27 struct completion finished;
28 struct list_head node;
29
30 void *priv;
31};
32
d278b7a2
AS
33struct olpc_ec_priv {
34 struct olpc_ec_driver *drv;
ec9964b4 35 u8 version;
99ecb01c
AS
36 struct work_struct worker;
37 struct mutex cmd_lock;
38
231c0c21
LR
39 /* DCON regulator */
40 struct regulator_dev *dcon_rdev;
41 bool dcon_enabled;
42
99ecb01c
AS
43 /* Pending EC commands */
44 struct list_head cmd_q;
45 spinlock_t cmd_q_lock;
d278b7a2 46
6cca83d4
AS
47 struct dentry *dbgfs_dir;
48
ec9964b4
LR
49 /*
50 * EC event mask to be applied during suspend (defining wakeup
51 * sources).
52 */
53 u16 ec_wakeup_mask;
54
d278b7a2
AS
55 /*
56 * Running an EC command while suspending means we don't always finish
57 * the command before the machine suspends. This means that the EC
58 * is expecting the command protocol to finish, but we after a period
59 * of time (while the OS is asleep) the EC times out and restarts its
60 * idle loop. Meanwhile, the OS wakes up, thinks it's still in the
61 * middle of the command protocol, starts throwing random things at
62 * the EC... and everyone's uphappy.
63 */
64 bool suspended;
65};
66
3d26c20b 67static struct olpc_ec_driver *ec_driver;
d278b7a2 68static struct olpc_ec_priv *ec_priv;
3d26c20b 69static void *ec_cb_arg;
3d26c20b
AS
70
71void olpc_ec_driver_register(struct olpc_ec_driver *drv, void *arg)
72{
73 ec_driver = drv;
74 ec_cb_arg = arg;
75}
76EXPORT_SYMBOL_GPL(olpc_ec_driver_register);
77
78static void olpc_ec_worker(struct work_struct *w)
79{
99ecb01c 80 struct olpc_ec_priv *ec = container_of(w, struct olpc_ec_priv, worker);
3d26c20b
AS
81 struct ec_cmd_desc *desc = NULL;
82 unsigned long flags;
83
84 /* Grab the first pending command from the queue */
99ecb01c
AS
85 spin_lock_irqsave(&ec->cmd_q_lock, flags);
86 if (!list_empty(&ec->cmd_q)) {
87 desc = list_first_entry(&ec->cmd_q, struct ec_cmd_desc, node);
3d26c20b
AS
88 list_del(&desc->node);
89 }
99ecb01c 90 spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
3d26c20b
AS
91
92 /* Do we actually have anything to do? */
93 if (!desc)
94 return;
95
96 /* Protect the EC hw with a mutex; only run one cmd at a time */
99ecb01c 97 mutex_lock(&ec->cmd_lock);
3d26c20b
AS
98 desc->err = ec_driver->ec_cmd(desc->cmd, desc->inbuf, desc->inlen,
99 desc->outbuf, desc->outlen, ec_cb_arg);
99ecb01c 100 mutex_unlock(&ec->cmd_lock);
3d26c20b
AS
101
102 /* Finished, wake up olpc_ec_cmd() */
103 complete(&desc->finished);
104
105 /* Run the worker thread again in case there are more cmds pending */
99ecb01c 106 schedule_work(&ec->worker);
3d26c20b
AS
107}
108
109/*
110 * Throw a cmd descripter onto the list. We now have SMP OLPC machines, so
111 * locking is pretty critical.
112 */
99ecb01c
AS
113static void queue_ec_descriptor(struct ec_cmd_desc *desc,
114 struct olpc_ec_priv *ec)
3d26c20b
AS
115{
116 unsigned long flags;
117
118 INIT_LIST_HEAD(&desc->node);
119
99ecb01c
AS
120 spin_lock_irqsave(&ec->cmd_q_lock, flags);
121 list_add_tail(&desc->node, &ec->cmd_q);
122 spin_unlock_irqrestore(&ec->cmd_q_lock, flags);
3d26c20b 123
99ecb01c 124 schedule_work(&ec->worker);
3d26c20b
AS
125}
126
392a325c
AS
127int olpc_ec_cmd(u8 cmd, u8 *inbuf, size_t inlen, u8 *outbuf, size_t outlen)
128{
d278b7a2 129 struct olpc_ec_priv *ec = ec_priv;
3d26c20b
AS
130 struct ec_cmd_desc desc;
131
560331ea
LR
132 /* Driver not yet registered. */
133 if (!ec_driver)
134 return -EPROBE_DEFER;
135
136 if (WARN_ON(!ec_driver->ec_cmd))
3d26c20b
AS
137 return -ENODEV;
138
d278b7a2
AS
139 if (!ec)
140 return -ENOMEM;
141
142 /* Suspending in the middle of a command hoses things really badly */
143 if (WARN_ON(ec->suspended))
144 return -EBUSY;
145
3d26c20b
AS
146 might_sleep();
147
148 desc.cmd = cmd;
149 desc.inbuf = inbuf;
150 desc.outbuf = outbuf;
151 desc.inlen = inlen;
152 desc.outlen = outlen;
153 desc.err = 0;
154 init_completion(&desc.finished);
155
99ecb01c 156 queue_ec_descriptor(&desc, ec);
3d26c20b
AS
157
158 /* Timeouts must be handled in the platform-specific EC hook */
159 wait_for_completion(&desc.finished);
160
161 /* The worker thread dequeues the cmd; no need to do anything here */
162 return desc.err;
392a325c
AS
163}
164EXPORT_SYMBOL_GPL(olpc_ec_cmd);
ac250415 165
ec9964b4
LR
166void olpc_ec_wakeup_set(u16 value)
167{
168 struct olpc_ec_priv *ec = ec_priv;
169
170 if (WARN_ON(!ec))
171 return;
172
173 ec->ec_wakeup_mask |= value;
174}
175EXPORT_SYMBOL_GPL(olpc_ec_wakeup_set);
176
177void olpc_ec_wakeup_clear(u16 value)
178{
179 struct olpc_ec_priv *ec = ec_priv;
180
181 if (WARN_ON(!ec))
182 return;
183
184 ec->ec_wakeup_mask &= ~value;
185}
186EXPORT_SYMBOL_GPL(olpc_ec_wakeup_clear);
187
188int olpc_ec_mask_write(u16 bits)
189{
190 struct olpc_ec_priv *ec = ec_priv;
191
192 if (WARN_ON(!ec))
193 return -ENODEV;
194
195 /* EC version 0x5f adds support for wide SCI mask */
196 if (ec->version >= 0x5f) {
197 __be16 ec_word = cpu_to_be16(bits);
198
199 return olpc_ec_cmd(EC_WRITE_EXT_SCI_MASK, (void *)&ec_word, 2, NULL, 0);
200 } else {
201 u8 ec_byte = bits & 0xff;
202
203 return olpc_ec_cmd(EC_WRITE_SCI_MASK, &ec_byte, 1, NULL, 0);
204 }
205}
206EXPORT_SYMBOL_GPL(olpc_ec_mask_write);
207
208/*
209 * Returns true if the compile and runtime configurations allow for EC events
210 * to wake the system.
211 */
212bool olpc_ec_wakeup_available(void)
213{
214 if (WARN_ON(!ec_driver))
215 return false;
216
217 return ec_driver->wakeup_available;
218}
219EXPORT_SYMBOL_GPL(olpc_ec_wakeup_available);
220
221int olpc_ec_sci_query(u16 *sci_value)
222{
223 struct olpc_ec_priv *ec = ec_priv;
224 int ret;
225
226 if (WARN_ON(!ec))
227 return -ENODEV;
228
229 /* EC version 0x5f adds support for wide SCI mask */
230 if (ec->version >= 0x5f) {
231 __be16 ec_word;
232
233 ret = olpc_ec_cmd(EC_EXT_SCI_QUERY, NULL, 0, (void *)&ec_word, 2);
234 if (ret == 0)
235 *sci_value = be16_to_cpu(ec_word);
236 } else {
237 u8 ec_byte;
238
239 ret = olpc_ec_cmd(EC_SCI_QUERY, NULL, 0, &ec_byte, 1);
240 if (ret == 0)
241 *sci_value = ec_byte;
242 }
243
244 return ret;
245}
246EXPORT_SYMBOL_GPL(olpc_ec_sci_query);
247
6cca83d4
AS
248#ifdef CONFIG_DEBUG_FS
249
250/*
251 * debugfs support for "generic commands", to allow sending
252 * arbitrary EC commands from userspace.
253 */
254
255#define EC_MAX_CMD_ARGS (5 + 1) /* cmd byte + 5 args */
256#define EC_MAX_CMD_REPLY (8)
257
258static DEFINE_MUTEX(ec_dbgfs_lock);
259static unsigned char ec_dbgfs_resp[EC_MAX_CMD_REPLY];
260static unsigned int ec_dbgfs_resp_bytes;
261
262static ssize_t ec_dbgfs_cmd_write(struct file *file, const char __user *buf,
263 size_t size, loff_t *ppos)
264{
265 int i, m;
266 unsigned char ec_cmd[EC_MAX_CMD_ARGS];
267 unsigned int ec_cmd_int[EC_MAX_CMD_ARGS];
268 char cmdbuf[64];
269 int ec_cmd_bytes;
270
271 mutex_lock(&ec_dbgfs_lock);
272
273 size = simple_write_to_buffer(cmdbuf, sizeof(cmdbuf), ppos, buf, size);
274
275 m = sscanf(cmdbuf, "%x:%u %x %x %x %x %x", &ec_cmd_int[0],
276 &ec_dbgfs_resp_bytes, &ec_cmd_int[1], &ec_cmd_int[2],
277 &ec_cmd_int[3], &ec_cmd_int[4], &ec_cmd_int[5]);
278 if (m < 2 || ec_dbgfs_resp_bytes > EC_MAX_CMD_REPLY) {
279 /* reset to prevent overflow on read */
280 ec_dbgfs_resp_bytes = 0;
281
282 pr_debug("olpc-ec: bad ec cmd: cmd:response-count [arg1 [arg2 ...]]\n");
283 size = -EINVAL;
284 goto out;
285 }
286
287 /* convert scanf'd ints to char */
288 ec_cmd_bytes = m - 2;
289 for (i = 0; i <= ec_cmd_bytes; i++)
290 ec_cmd[i] = ec_cmd_int[i];
291
7d3777d1
AS
292 pr_debug("olpc-ec: debugfs cmd 0x%02x with %d args %5ph, want %d returns\n",
293 ec_cmd[0], ec_cmd_bytes, ec_cmd + 1,
294 ec_dbgfs_resp_bytes);
6cca83d4
AS
295
296 olpc_ec_cmd(ec_cmd[0], (ec_cmd_bytes == 0) ? NULL : &ec_cmd[1],
297 ec_cmd_bytes, ec_dbgfs_resp, ec_dbgfs_resp_bytes);
298
7d3777d1
AS
299 pr_debug("olpc-ec: response %8ph (%d bytes expected)\n",
300 ec_dbgfs_resp, ec_dbgfs_resp_bytes);
6cca83d4
AS
301
302out:
303 mutex_unlock(&ec_dbgfs_lock);
304 return size;
305}
306
307static ssize_t ec_dbgfs_cmd_read(struct file *file, char __user *buf,
308 size_t size, loff_t *ppos)
309{
310 unsigned int i, r;
311 char *rp;
312 char respbuf[64];
313
314 mutex_lock(&ec_dbgfs_lock);
315 rp = respbuf;
316 rp += sprintf(rp, "%02x", ec_dbgfs_resp[0]);
317 for (i = 1; i < ec_dbgfs_resp_bytes; i++)
318 rp += sprintf(rp, ", %02x", ec_dbgfs_resp[i]);
319 mutex_unlock(&ec_dbgfs_lock);
320 rp += sprintf(rp, "\n");
321
322 r = rp - respbuf;
323 return simple_read_from_buffer(buf, size, ppos, respbuf, r);
324}
325
326static const struct file_operations ec_dbgfs_ops = {
327 .write = ec_dbgfs_cmd_write,
328 .read = ec_dbgfs_cmd_read,
329};
330
331static struct dentry *olpc_ec_setup_debugfs(void)
332{
333 struct dentry *dbgfs_dir;
334
335 dbgfs_dir = debugfs_create_dir("olpc-ec", NULL);
336 if (IS_ERR_OR_NULL(dbgfs_dir))
337 return NULL;
338
339 debugfs_create_file("cmd", 0600, dbgfs_dir, NULL, &ec_dbgfs_ops);
340
341 return dbgfs_dir;
342}
343
344#else
345
346static struct dentry *olpc_ec_setup_debugfs(void)
347{
348 return NULL;
349}
350
351#endif /* CONFIG_DEBUG_FS */
352
231c0c21
LR
353static int olpc_ec_set_dcon_power(struct olpc_ec_priv *ec, bool state)
354{
355 unsigned char ec_byte = state;
356 int ret;
357
358 if (ec->dcon_enabled == state)
359 return 0;
360
361 ret = olpc_ec_cmd(EC_DCON_POWER_MODE, &ec_byte, 1, NULL, 0);
362 if (ret)
363 return ret;
364
365 ec->dcon_enabled = state;
366 return 0;
367}
368
369static int dcon_regulator_enable(struct regulator_dev *rdev)
370{
371 struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
372
373 return olpc_ec_set_dcon_power(ec, true);
374}
375
376static int dcon_regulator_disable(struct regulator_dev *rdev)
377{
378 struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
379
380 return olpc_ec_set_dcon_power(ec, false);
381}
382
383static int dcon_regulator_is_enabled(struct regulator_dev *rdev)
384{
385 struct olpc_ec_priv *ec = rdev_get_drvdata(rdev);
386
387 return ec->dcon_enabled ? 1 : 0;
388}
389
390static struct regulator_ops dcon_regulator_ops = {
391 .enable = dcon_regulator_enable,
392 .disable = dcon_regulator_disable,
393 .is_enabled = dcon_regulator_is_enabled,
394};
395
396static const struct regulator_desc dcon_desc = {
397 .name = "dcon",
398 .id = 0,
399 .ops = &dcon_regulator_ops,
400 .type = REGULATOR_VOLTAGE,
401 .owner = THIS_MODULE,
402};
403
ac250415
AS
404static int olpc_ec_probe(struct platform_device *pdev)
405{
d278b7a2 406 struct olpc_ec_priv *ec;
231c0c21 407 struct regulator_config config = { };
ac250415
AS
408 int err;
409
410 if (!ec_driver)
411 return -ENODEV;
412
d278b7a2
AS
413 ec = kzalloc(sizeof(*ec), GFP_KERNEL);
414 if (!ec)
415 return -ENOMEM;
99ecb01c 416
d278b7a2 417 ec->drv = ec_driver;
99ecb01c
AS
418 INIT_WORK(&ec->worker, olpc_ec_worker);
419 mutex_init(&ec->cmd_lock);
420
421 INIT_LIST_HEAD(&ec->cmd_q);
422 spin_lock_init(&ec->cmd_q_lock);
423
d278b7a2
AS
424 ec_priv = ec;
425 platform_set_drvdata(pdev, ec);
426
ec9964b4
LR
427 /* get the EC revision */
428 err = olpc_ec_cmd(EC_FIRMWARE_REV, NULL, 0, &ec->version, 1);
cf5e6a71
LR
429 if (err)
430 goto error;
ac250415 431
231c0c21
LR
432 config.dev = pdev->dev.parent;
433 config.driver_data = ec;
434 ec->dcon_enabled = true;
435 ec->dcon_rdev = devm_regulator_register(&pdev->dev, &dcon_desc,
436 &config);
437 if (IS_ERR(ec->dcon_rdev)) {
438 dev_err(&pdev->dev, "failed to register DCON regulator\n");
3c555a78 439 err = PTR_ERR(ec->dcon_rdev);
cf5e6a71 440 goto error;
231c0c21
LR
441 }
442
ec9964b4
LR
443 ec->dbgfs_dir = olpc_ec_setup_debugfs();
444
cf5e6a71
LR
445 return 0;
446
447error:
448 ec_priv = NULL;
449 kfree(ec);
ac250415
AS
450 return err;
451}
452
453static int olpc_ec_suspend(struct device *dev)
454{
455 struct platform_device *pdev = to_platform_device(dev);
d278b7a2
AS
456 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
457 int err = 0;
458
ec9964b4
LR
459 olpc_ec_mask_write(ec->ec_wakeup_mask);
460
d278b7a2
AS
461 if (ec_driver->suspend)
462 err = ec_driver->suspend(pdev);
463 if (!err)
464 ec->suspended = true;
465
466 return err;
ac250415
AS
467}
468
469static int olpc_ec_resume(struct device *dev)
470{
471 struct platform_device *pdev = to_platform_device(dev);
d278b7a2
AS
472 struct olpc_ec_priv *ec = platform_get_drvdata(pdev);
473
474 ec->suspended = false;
ac250415
AS
475 return ec_driver->resume ? ec_driver->resume(pdev) : 0;
476}
477
478static const struct dev_pm_ops olpc_ec_pm_ops = {
479 .suspend_late = olpc_ec_suspend,
480 .resume_early = olpc_ec_resume,
481};
482
483static struct platform_driver olpc_ec_plat_driver = {
484 .probe = olpc_ec_probe,
485 .driver = {
486 .name = "olpc-ec",
487 .pm = &olpc_ec_pm_ops,
488 },
489};
490
491static int __init olpc_ec_init_module(void)
492{
493 return platform_driver_register(&olpc_ec_plat_driver);
494}
93dbc1b3 495arch_initcall(olpc_ec_init_module);