mfd: cros_ec: Move protocol helpers out of the MFD driver
[linux-2.6-block.git] / drivers / mfd / cros_ec.c
CommitLineData
4ab6174e
SG
1/*
2 * ChromeOS EC multi-function device
3 *
4 * Copyright (C) 2012 Google, Inc
5 *
6 * This software is licensed under the terms of the GNU General Public
7 * License version 2, as published by the Free Software Foundation, and
8 * may be copied, distributed, and modified under those terms.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * The ChromeOS EC multi function device is used to mux all the requests
16 * to the EC device for its multiple features: keyboard controller,
17 * battery charging and regulator control, firmware update.
18 */
19
bb03ffb9 20#include <linux/of_platform.h>
4ab6174e
SG
21#include <linux/interrupt.h>
22#include <linux/slab.h>
5ebeaff5 23#include <linux/module.h>
4ab6174e
SG
24#include <linux/mfd/core.h>
25#include <linux/mfd/cros_ec.h>
a6551a76 26
5ac98553 27static const struct mfd_cell cros_devs[] = {
8a4be850
JMC
28 {
29 .name = "cros-ec-ctl",
bb03ffb9 30 .id = PLATFORM_DEVID_AUTO,
8a4be850 31 },
4ab6174e
SG
32};
33
34int cros_ec_register(struct cros_ec_device *ec_dev)
35{
36 struct device *dev = ec_dev->dev;
37 int err = 0;
38
4ab6174e 39 if (ec_dev->din_size) {
22f9ee75
LJ
40 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
41 if (!ec_dev->din)
42 return -ENOMEM;
4ab6174e
SG
43 }
44 if (ec_dev->dout_size) {
22f9ee75
LJ
45 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
46 if (!ec_dev->dout)
47 return -ENOMEM;
4ab6174e
SG
48 }
49
63427530
AB
50 mutex_init(&ec_dev->lock);
51
4ab6174e
SG
52 err = mfd_add_devices(dev, 0, cros_devs,
53 ARRAY_SIZE(cros_devs),
54 NULL, ec_dev->irq, NULL);
55 if (err) {
56 dev_err(dev, "failed to add mfd devices\n");
d1fd345e 57 return err;
4ab6174e
SG
58 }
59
bb03ffb9
TB
60 if (IS_ENABLED(CONFIG_OF) && dev->of_node) {
61 err = of_platform_populate(dev->of_node, NULL, NULL, dev);
62 if (err) {
63 mfd_remove_devices(dev);
64 dev_err(dev, "Failed to register sub-devices\n");
65 return err;
66 }
67 }
68
533cec8f 69 dev_info(dev, "Chrome EC device registered\n");
4ab6174e
SG
70
71 return 0;
4ab6174e 72}
5ebeaff5 73EXPORT_SYMBOL(cros_ec_register);
4ab6174e
SG
74
75int cros_ec_remove(struct cros_ec_device *ec_dev)
76{
77 mfd_remove_devices(ec_dev->dev);
4ab6174e
SG
78
79 return 0;
80}
5ebeaff5 81EXPORT_SYMBOL(cros_ec_remove);
4ab6174e
SG
82
83#ifdef CONFIG_PM_SLEEP
84int cros_ec_suspend(struct cros_ec_device *ec_dev)
85{
86 struct device *dev = ec_dev->dev;
87
88 if (device_may_wakeup(dev))
89 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
90
91 disable_irq(ec_dev->irq);
92 ec_dev->was_wake_device = ec_dev->wake_enabled;
93
94 return 0;
95}
5ebeaff5 96EXPORT_SYMBOL(cros_ec_suspend);
4ab6174e
SG
97
98int cros_ec_resume(struct cros_ec_device *ec_dev)
99{
100 enable_irq(ec_dev->irq);
101
102 if (ec_dev->wake_enabled) {
103 disable_irq_wake(ec_dev->irq);
104 ec_dev->wake_enabled = 0;
105 }
106
107 return 0;
108}
5ebeaff5
SO
109EXPORT_SYMBOL(cros_ec_resume);
110
4ab6174e 111#endif
a865a589
BR
112
113MODULE_LICENSE("GPL");
114MODULE_DESCRIPTION("ChromeOS EC core driver");