]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/mfd/cros_ec.c
mfd: cros_ec: cleanup: Remove EC wrapper functions
[karo-tx-linux.git] / drivers / mfd / cros_ec.c
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
20 #include <linux/interrupt.h>
21 #include <linux/slab.h>
22 #include <linux/module.h>
23 #include <linux/mfd/core.h>
24 #include <linux/mfd/cros_ec.h>
25 #include <linux/mfd/cros_ec_commands.h>
26
27 int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
28                        struct cros_ec_command *msg)
29 {
30         uint8_t *out;
31         int csum, i;
32
33         BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
34         out = ec_dev->dout;
35         out[0] = EC_CMD_VERSION0 + msg->version;
36         out[1] = msg->command;
37         out[2] = msg->outsize;
38         csum = out[0] + out[1] + out[2];
39         for (i = 0; i < msg->outsize; i++)
40                 csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->outdata[i];
41         out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = (uint8_t)(csum & 0xff);
42
43         return EC_MSG_TX_PROTO_BYTES + msg->outsize;
44 }
45 EXPORT_SYMBOL(cros_ec_prepare_tx);
46
47 static irqreturn_t ec_irq_thread(int irq, void *data)
48 {
49         struct cros_ec_device *ec_dev = data;
50
51         if (device_may_wakeup(ec_dev->dev))
52                 pm_wakeup_event(ec_dev->dev, 0);
53
54         blocking_notifier_call_chain(&ec_dev->event_notifier, 1, ec_dev);
55
56         return IRQ_HANDLED;
57 }
58
59 static const struct mfd_cell cros_devs[] = {
60         {
61                 .name = "cros-ec-keyb",
62                 .id = 1,
63                 .of_compatible = "google,cros-ec-keyb",
64         },
65         {
66                 .name = "cros-ec-i2c-tunnel",
67                 .id = 2,
68                 .of_compatible = "google,cros-ec-i2c-tunnel",
69         },
70 };
71
72 int cros_ec_register(struct cros_ec_device *ec_dev)
73 {
74         struct device *dev = ec_dev->dev;
75         int err = 0;
76
77         BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
78
79         if (ec_dev->din_size) {
80                 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
81                 if (!ec_dev->din)
82                         return -ENOMEM;
83         }
84         if (ec_dev->dout_size) {
85                 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
86                 if (!ec_dev->dout)
87                         return -ENOMEM;
88         }
89
90         if (!ec_dev->irq) {
91                 dev_dbg(dev, "no valid IRQ: %d\n", ec_dev->irq);
92                 return err;
93         }
94
95         err = request_threaded_irq(ec_dev->irq, NULL, ec_irq_thread,
96                                    IRQF_TRIGGER_LOW | IRQF_ONESHOT,
97                                    "chromeos-ec", ec_dev);
98         if (err) {
99                 dev_err(dev, "request irq %d: error %d\n", ec_dev->irq, err);
100                 return err;
101         }
102
103         err = mfd_add_devices(dev, 0, cros_devs,
104                               ARRAY_SIZE(cros_devs),
105                               NULL, ec_dev->irq, NULL);
106         if (err) {
107                 dev_err(dev, "failed to add mfd devices\n");
108                 goto fail_mfd;
109         }
110
111         dev_info(dev, "Chrome EC device registered\n");
112
113         return 0;
114
115 fail_mfd:
116         free_irq(ec_dev->irq, ec_dev);
117
118         return err;
119 }
120 EXPORT_SYMBOL(cros_ec_register);
121
122 int cros_ec_remove(struct cros_ec_device *ec_dev)
123 {
124         mfd_remove_devices(ec_dev->dev);
125         free_irq(ec_dev->irq, ec_dev);
126
127         return 0;
128 }
129 EXPORT_SYMBOL(cros_ec_remove);
130
131 #ifdef CONFIG_PM_SLEEP
132 int cros_ec_suspend(struct cros_ec_device *ec_dev)
133 {
134         struct device *dev = ec_dev->dev;
135
136         if (device_may_wakeup(dev))
137                 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
138
139         disable_irq(ec_dev->irq);
140         ec_dev->was_wake_device = ec_dev->wake_enabled;
141
142         return 0;
143 }
144 EXPORT_SYMBOL(cros_ec_suspend);
145
146 int cros_ec_resume(struct cros_ec_device *ec_dev)
147 {
148         enable_irq(ec_dev->irq);
149
150         if (ec_dev->wake_enabled) {
151                 disable_irq_wake(ec_dev->irq);
152                 ec_dev->wake_enabled = 0;
153         }
154
155         return 0;
156 }
157 EXPORT_SYMBOL(cros_ec_resume);
158
159 #endif
160
161 MODULE_LICENSE("GPL");
162 MODULE_DESCRIPTION("ChromeOS EC core driver");