2 * Copyright (C) 2012 Google, Inc
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 * The ChromeOS EC multi function device is used to mux all the requests
20 * to the EC device for its multiple features : keyboard controller,
21 * battery charging and regulator control, firmware update.
24 #include <linux/interrupt.h>
25 #include <linux/slab.h>
26 #include <linux/mfd/core.h>
27 #include <linux/mfd/chromeos_ec.h>
28 #include <linux/mfd/chromeos_ec_commands.h>
29 #include <linux/of_platform.h>
31 int cros_ec_prepare_tx(struct chromeos_ec_device *ec_dev,
32 struct chromeos_ec_msg *msg)
37 BUG_ON(msg->out_len > EC_HOST_PARAM_SIZE);
39 out[0] = EC_CMD_VERSION0 + msg->version;
41 out[2] = msg->out_len;
42 csum = out[0] + out[1] + out[2];
43 for (i = 0; i < msg->out_len; i++)
44 csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->out_buf[i];
45 out[EC_MSG_TX_HEADER_BYTES + msg->out_len] = (uint8_t)(csum & 0xff);
47 return EC_MSG_TX_PROTO_BYTES + msg->out_len;
50 static int cros_ec_command_sendrecv(struct chromeos_ec_device *ec_dev,
51 uint16_t cmd, void *out_buf, int out_len,
52 void *in_buf, int in_len)
54 struct chromeos_ec_msg msg;
56 msg.version = cmd >> 8;
58 msg.out_buf = out_buf;
59 msg.out_len = out_len;
63 return ec_dev->command_xfer(ec_dev, &msg);
66 static int cros_ec_command_recv(struct chromeos_ec_device *ec_dev,
67 uint16_t cmd, void *buf, int buf_len)
69 return cros_ec_command_sendrecv(ec_dev, cmd, NULL, 0, buf, buf_len);
72 static int cros_ec_command_send(struct chromeos_ec_device *ec_dev,
73 uint16_t cmd, void *buf, int buf_len)
75 return cros_ec_command_sendrecv(ec_dev, cmd, buf, buf_len, NULL, 0);
78 struct chromeos_ec_device *__devinit cros_ec_alloc(const char *name)
80 struct chromeos_ec_device *ec_dev;
82 ec_dev = kzalloc(sizeof(*ec_dev), GFP_KERNEL);
84 dev_err(ec_dev->dev, "cannot allocate\n");
92 void cros_ec_free(struct chromeos_ec_device *ec)
97 static irqreturn_t ec_irq_thread(int irq, void *data)
99 struct chromeos_ec_device *ec_dev = data;
101 if (device_may_wakeup(ec_dev->dev))
102 pm_wakeup_event(ec_dev->dev, 0);
104 blocking_notifier_call_chain(&ec_dev->event_notifier, 1, ec_dev);
109 static int __devinit check_protocol_version(struct chromeos_ec_device *ec)
112 struct ec_response_proto_version data;
114 ret = ec->command_recv(ec, EC_CMD_PROTO_VERSION, &data, sizeof(data));
117 dev_info(ec->dev, "protocol version: %d\n", data.version);
118 if (data.version != EC_PROTO_VERSION)
119 return -EPROTONOSUPPORT;
124 static struct mfd_cell cros_devs[] = {
130 .name = "cros_ec-fw",
135 int __devinit cros_ec_register(struct chromeos_ec_device *ec_dev)
137 struct device *dev = ec_dev->dev;
139 struct device_node *node;
142 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
143 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->wake_notifier);
145 ec_dev->command_send = cros_ec_command_send;
146 ec_dev->command_recv = cros_ec_command_recv;
147 ec_dev->command_sendrecv = cros_ec_command_sendrecv;
149 if (ec_dev->din_size) {
150 ec_dev->din = kmalloc(ec_dev->din_size, GFP_KERNEL);
153 dev_err(dev, "cannot allocate din\n");
157 if (ec_dev->dout_size) {
158 ec_dev->dout = kmalloc(ec_dev->dout_size, GFP_KERNEL);
161 dev_err(dev, "cannot allocate dout\n");
167 dev_dbg(dev, "no valid IRQ: %d\n", ec_dev->irq);
171 err = request_threaded_irq(ec_dev->irq, NULL, ec_irq_thread,
172 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
173 "chromeos-ec", ec_dev);
175 dev_err(dev, "request irq %d: error %d\n", ec_dev->irq, err);
179 err = check_protocol_version(ec_dev);
181 dev_err(dev, "protocol version check failed: %d\n", err);
185 err = mfd_add_devices(dev, 0, cros_devs,
186 ARRAY_SIZE(cros_devs),
191 /* adding sub-devices declared in the device tree */
192 for_each_child_of_node(dev->of_node, node) {
194 struct mfd_cell cell = {
199 dev_dbg(dev, "adding MFD sub-device %s\n", node->name);
200 if (of_modalias_node(node, name, sizeof(name)) < 0) {
201 dev_err(dev, "modalias failure on %s\n",
206 err = mfd_add_devices(dev, 0x10, &cell, 1, NULL, 0);
208 dev_err(dev, "fail to add %s\n", node->full_name);
211 dev_info(dev, "Chrome EC (%s)\n", ec_dev->name);
217 free_irq(ec_dev->irq, ec_dev);
226 int __devinit cros_ec_remove(struct chromeos_ec_device *ec_dev)
228 mfd_remove_devices(ec_dev->dev);
229 free_irq(ec_dev->irq, ec_dev);
236 #ifdef CONFIG_PM_SLEEP
237 int cros_ec_suspend(struct chromeos_ec_device *ec_dev)
239 struct device *dev = ec_dev->dev;
241 if (device_may_wakeup(dev))
242 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
244 disable_irq(ec_dev->irq);
249 int cros_ec_resume(struct chromeos_ec_device *ec_dev)
252 * When the EC is not a wake source, then it could not have caused the
253 * resume, so we should do the resume processing. This may clear the
254 * EC's key scan buffer, for example. If the EC is a wake source (e.g.
255 * the lid is open and the user might press a key to wake) then we
256 * don't want to do resume processing (key scan buffer should be
259 if (!ec_dev->wake_enabled)
260 blocking_notifier_call_chain(&ec_dev->wake_notifier, 1, ec_dev);
261 enable_irq(ec_dev->irq);
263 if (ec_dev->wake_enabled) {
264 disable_irq_wake(ec_dev->irq);
265 ec_dev->wake_enabled = 0;