blob: 49ed8c340868321a84fcd98fad8d7f0f9c388fd5 [file] [log] [blame]
Simon Glass4ab61742013-02-25 14:08:37 -08001/*
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>
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +010022#include <linux/module.h>
Simon Glass4ab61742013-02-25 14:08:37 -080023#include <linux/mfd/core.h>
24#include <linux/mfd/cros_ec.h>
25#include <linux/mfd/cros_ec_commands.h>
26
27int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -070028 struct cros_ec_command *msg)
Simon Glass4ab61742013-02-25 14:08:37 -080029{
30 uint8_t *out;
31 int csum, i;
32
Bill Richardson5d4773e2014-06-18 11:14:02 -070033 BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
Simon Glass4ab61742013-02-25 14:08:37 -080034 out = ec_dev->dout;
35 out[0] = EC_CMD_VERSION0 + msg->version;
Bill Richardson5d4773e2014-06-18 11:14:02 -070036 out[1] = msg->command;
37 out[2] = msg->outsize;
Simon Glass4ab61742013-02-25 14:08:37 -080038 csum = out[0] + out[1] + out[2];
Bill Richardson5d4773e2014-06-18 11:14:02 -070039 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);
Simon Glass4ab61742013-02-25 14:08:37 -080042
Bill Richardson5d4773e2014-06-18 11:14:02 -070043 return EC_MSG_TX_PROTO_BYTES + msg->outsize;
Simon Glass4ab61742013-02-25 14:08:37 -080044}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +010045EXPORT_SYMBOL(cros_ec_prepare_tx);
Simon Glass4ab61742013-02-25 14:08:37 -080046
47static int cros_ec_command_sendrecv(struct cros_ec_device *ec_dev,
48 uint16_t cmd, void *out_buf, int out_len,
49 void *in_buf, int in_len)
50{
Bill Richardson5d4773e2014-06-18 11:14:02 -070051 struct cros_ec_command msg;
Simon Glass4ab61742013-02-25 14:08:37 -080052
53 msg.version = cmd >> 8;
Bill Richardson5d4773e2014-06-18 11:14:02 -070054 msg.command = cmd & 0xff;
55 msg.outdata = out_buf;
56 msg.outsize = out_len;
57 msg.indata = in_buf;
58 msg.insize = in_len;
Simon Glass4ab61742013-02-25 14:08:37 -080059
Bill Richardson7e6cb5b2014-06-18 11:14:00 -070060 return ec_dev->cmd_xfer(ec_dev, &msg);
Simon Glass4ab61742013-02-25 14:08:37 -080061}
62
63static int cros_ec_command_recv(struct cros_ec_device *ec_dev,
64 uint16_t cmd, void *buf, int buf_len)
65{
66 return cros_ec_command_sendrecv(ec_dev, cmd, NULL, 0, buf, buf_len);
67}
68
69static int cros_ec_command_send(struct cros_ec_device *ec_dev,
70 uint16_t cmd, void *buf, int buf_len)
71{
72 return cros_ec_command_sendrecv(ec_dev, cmd, buf, buf_len, NULL, 0);
73}
74
75static irqreturn_t ec_irq_thread(int irq, void *data)
76{
77 struct cros_ec_device *ec_dev = data;
78
79 if (device_may_wakeup(ec_dev->dev))
80 pm_wakeup_event(ec_dev->dev, 0);
81
82 blocking_notifier_call_chain(&ec_dev->event_notifier, 1, ec_dev);
83
84 return IRQ_HANDLED;
85}
86
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +010087static const struct mfd_cell cros_devs[] = {
Simon Glass4ab61742013-02-25 14:08:37 -080088 {
89 .name = "cros-ec-keyb",
90 .id = 1,
91 .of_compatible = "google,cros-ec-keyb",
92 },
Doug Anderson9d230c92014-04-30 10:44:09 -070093 {
94 .name = "cros-ec-i2c-tunnel",
95 .id = 2,
96 .of_compatible = "google,cros-ec-i2c-tunnel",
97 },
Simon Glass4ab61742013-02-25 14:08:37 -080098};
99
100int cros_ec_register(struct cros_ec_device *ec_dev)
101{
102 struct device *dev = ec_dev->dev;
103 int err = 0;
104
105 BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier);
106
107 ec_dev->command_send = cros_ec_command_send;
108 ec_dev->command_recv = cros_ec_command_recv;
109 ec_dev->command_sendrecv = cros_ec_command_sendrecv;
110
111 if (ec_dev->din_size) {
Lee Jones22f9ee72013-05-23 16:25:10 +0100112 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
113 if (!ec_dev->din)
114 return -ENOMEM;
Simon Glass4ab61742013-02-25 14:08:37 -0800115 }
116 if (ec_dev->dout_size) {
Lee Jones22f9ee72013-05-23 16:25:10 +0100117 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
118 if (!ec_dev->dout)
119 return -ENOMEM;
Simon Glass4ab61742013-02-25 14:08:37 -0800120 }
121
122 if (!ec_dev->irq) {
123 dev_dbg(dev, "no valid IRQ: %d\n", ec_dev->irq);
Lee Jones22f9ee72013-05-23 16:25:10 +0100124 return err;
Simon Glass4ab61742013-02-25 14:08:37 -0800125 }
126
127 err = request_threaded_irq(ec_dev->irq, NULL, ec_irq_thread,
128 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
129 "chromeos-ec", ec_dev);
130 if (err) {
131 dev_err(dev, "request irq %d: error %d\n", ec_dev->irq, err);
Lee Jones22f9ee72013-05-23 16:25:10 +0100132 return err;
Simon Glass4ab61742013-02-25 14:08:37 -0800133 }
134
135 err = mfd_add_devices(dev, 0, cros_devs,
136 ARRAY_SIZE(cros_devs),
137 NULL, ec_dev->irq, NULL);
138 if (err) {
139 dev_err(dev, "failed to add mfd devices\n");
140 goto fail_mfd;
141 }
142
Bill Richardson533cec82014-06-18 11:14:03 -0700143 dev_info(dev, "Chrome EC device registered\n");
Simon Glass4ab61742013-02-25 14:08:37 -0800144
145 return 0;
146
147fail_mfd:
148 free_irq(ec_dev->irq, ec_dev);
Lee Jones22f9ee72013-05-23 16:25:10 +0100149
Simon Glass4ab61742013-02-25 14:08:37 -0800150 return err;
151}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100152EXPORT_SYMBOL(cros_ec_register);
Simon Glass4ab61742013-02-25 14:08:37 -0800153
154int cros_ec_remove(struct cros_ec_device *ec_dev)
155{
156 mfd_remove_devices(ec_dev->dev);
157 free_irq(ec_dev->irq, ec_dev);
Simon Glass4ab61742013-02-25 14:08:37 -0800158
159 return 0;
160}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100161EXPORT_SYMBOL(cros_ec_remove);
Simon Glass4ab61742013-02-25 14:08:37 -0800162
163#ifdef CONFIG_PM_SLEEP
164int cros_ec_suspend(struct cros_ec_device *ec_dev)
165{
166 struct device *dev = ec_dev->dev;
167
168 if (device_may_wakeup(dev))
169 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
170
171 disable_irq(ec_dev->irq);
172 ec_dev->was_wake_device = ec_dev->wake_enabled;
173
174 return 0;
175}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100176EXPORT_SYMBOL(cros_ec_suspend);
Simon Glass4ab61742013-02-25 14:08:37 -0800177
178int cros_ec_resume(struct cros_ec_device *ec_dev)
179{
180 enable_irq(ec_dev->irq);
181
182 if (ec_dev->wake_enabled) {
183 disable_irq_wake(ec_dev->irq);
184 ec_dev->wake_enabled = 0;
185 }
186
187 return 0;
188}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100189EXPORT_SYMBOL(cros_ec_resume);
190
Simon Glass4ab61742013-02-25 14:08:37 -0800191#endif
Bill Richardsona865a582014-04-17 12:32:17 -0700192
193MODULE_LICENSE("GPL");
194MODULE_DESCRIPTION("ChromeOS EC core driver");