blob: c4aecc6f83739779880f85ae6b5ac659a153c095 [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>
Andrew Brestickerd86c21f2014-09-18 17:18:58 +020026#include <linux/delay.h>
27
28#define EC_COMMAND_RETRIES 50
Simon Glass4ab61742013-02-25 14:08:37 -080029
30int cros_ec_prepare_tx(struct cros_ec_device *ec_dev,
Bill Richardson5d4773e2014-06-18 11:14:02 -070031 struct cros_ec_command *msg)
Simon Glass4ab61742013-02-25 14:08:37 -080032{
33 uint8_t *out;
34 int csum, i;
35
Bill Richardson5d4773e2014-06-18 11:14:02 -070036 BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
Simon Glass4ab61742013-02-25 14:08:37 -080037 out = ec_dev->dout;
38 out[0] = EC_CMD_VERSION0 + msg->version;
Bill Richardson5d4773e2014-06-18 11:14:02 -070039 out[1] = msg->command;
40 out[2] = msg->outsize;
Simon Glass4ab61742013-02-25 14:08:37 -080041 csum = out[0] + out[1] + out[2];
Bill Richardson5d4773e2014-06-18 11:14:02 -070042 for (i = 0; i < msg->outsize; i++)
43 csum += out[EC_MSG_TX_HEADER_BYTES + i] = msg->outdata[i];
44 out[EC_MSG_TX_HEADER_BYTES + msg->outsize] = (uint8_t)(csum & 0xff);
Simon Glass4ab61742013-02-25 14:08:37 -080045
Bill Richardson5d4773e2014-06-18 11:14:02 -070046 return EC_MSG_TX_PROTO_BYTES + msg->outsize;
Simon Glass4ab61742013-02-25 14:08:37 -080047}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +010048EXPORT_SYMBOL(cros_ec_prepare_tx);
Simon Glass4ab61742013-02-25 14:08:37 -080049
Bill Richardson6db07b62014-06-18 11:14:05 -070050int cros_ec_check_result(struct cros_ec_device *ec_dev,
51 struct cros_ec_command *msg)
52{
53 switch (msg->result) {
54 case EC_RES_SUCCESS:
55 return 0;
56 case EC_RES_IN_PROGRESS:
57 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
58 msg->command);
59 return -EAGAIN;
60 default:
61 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
62 msg->command, msg->result);
63 return 0;
64 }
65}
66EXPORT_SYMBOL(cros_ec_check_result);
67
Andrew Brestickera6551a72014-09-18 17:18:56 +020068int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
69 struct cros_ec_command *msg)
70{
Andrew Bresticker63427532014-09-18 17:18:57 +020071 int ret;
72
73 mutex_lock(&ec_dev->lock);
74 ret = ec_dev->cmd_xfer(ec_dev, msg);
Andrew Brestickerd86c21f2014-09-18 17:18:58 +020075 if (msg->result == EC_RES_IN_PROGRESS) {
76 int i;
Javier Martinez Canillas1b84f2a2015-02-02 12:26:22 +010077 struct cros_ec_command status_msg = { };
78 struct ec_response_get_comms_status *status;
Andrew Brestickerd86c21f2014-09-18 17:18:58 +020079
Andrew Brestickerd86c21f2014-09-18 17:18:58 +020080 status_msg.command = EC_CMD_GET_COMMS_STATUS;
Javier Martinez Canillas1b84f2a2015-02-02 12:26:22 +010081 status_msg.insize = sizeof(*status);
Andrew Brestickerd86c21f2014-09-18 17:18:58 +020082
83 /*
84 * Query the EC's status until it's no longer busy or
85 * we encounter an error.
86 */
87 for (i = 0; i < EC_COMMAND_RETRIES; i++) {
88 usleep_range(10000, 11000);
89
90 ret = ec_dev->cmd_xfer(ec_dev, &status_msg);
91 if (ret < 0)
92 break;
93
94 msg->result = status_msg.result;
95 if (status_msg.result != EC_RES_SUCCESS)
96 break;
Javier Martinez Canillas1b84f2a2015-02-02 12:26:22 +010097
98 status = (struct ec_response_get_comms_status *)
99 status_msg.indata;
100 if (!(status->flags & EC_COMMS_STATUS_PROCESSING))
Andrew Brestickerd86c21f2014-09-18 17:18:58 +0200101 break;
102 }
103 }
Andrew Bresticker63427532014-09-18 17:18:57 +0200104 mutex_unlock(&ec_dev->lock);
105
106 return ret;
Andrew Brestickera6551a72014-09-18 17:18:56 +0200107}
108EXPORT_SYMBOL(cros_ec_cmd_xfer);
109
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +0100110static const struct mfd_cell cros_devs[] = {
Simon Glass4ab61742013-02-25 14:08:37 -0800111 {
112 .name = "cros-ec-keyb",
113 .id = 1,
114 .of_compatible = "google,cros-ec-keyb",
115 },
Doug Anderson9d230c92014-04-30 10:44:09 -0700116 {
117 .name = "cros-ec-i2c-tunnel",
118 .id = 2,
119 .of_compatible = "google,cros-ec-i2c-tunnel",
120 },
Javier Martinez Canillas8a4be852015-02-02 12:26:26 +0100121 {
122 .name = "cros-ec-ctl",
123 .id = 3,
124 },
Simon Glass4ab61742013-02-25 14:08:37 -0800125};
126
127int cros_ec_register(struct cros_ec_device *ec_dev)
128{
129 struct device *dev = ec_dev->dev;
130 int err = 0;
131
Simon Glass4ab61742013-02-25 14:08:37 -0800132 if (ec_dev->din_size) {
Lee Jones22f9ee72013-05-23 16:25:10 +0100133 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
134 if (!ec_dev->din)
135 return -ENOMEM;
Simon Glass4ab61742013-02-25 14:08:37 -0800136 }
137 if (ec_dev->dout_size) {
Lee Jones22f9ee72013-05-23 16:25:10 +0100138 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
139 if (!ec_dev->dout)
140 return -ENOMEM;
Simon Glass4ab61742013-02-25 14:08:37 -0800141 }
142
Andrew Bresticker63427532014-09-18 17:18:57 +0200143 mutex_init(&ec_dev->lock);
144
Simon Glass4ab61742013-02-25 14:08:37 -0800145 err = mfd_add_devices(dev, 0, cros_devs,
146 ARRAY_SIZE(cros_devs),
147 NULL, ec_dev->irq, NULL);
148 if (err) {
149 dev_err(dev, "failed to add mfd devices\n");
Andrew Brestickerd1fd3452014-06-18 11:14:07 -0700150 return err;
Simon Glass4ab61742013-02-25 14:08:37 -0800151 }
152
Bill Richardson533cec82014-06-18 11:14:03 -0700153 dev_info(dev, "Chrome EC device registered\n");
Simon Glass4ab61742013-02-25 14:08:37 -0800154
155 return 0;
Simon Glass4ab61742013-02-25 14:08:37 -0800156}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100157EXPORT_SYMBOL(cros_ec_register);
Simon Glass4ab61742013-02-25 14:08:37 -0800158
159int cros_ec_remove(struct cros_ec_device *ec_dev)
160{
161 mfd_remove_devices(ec_dev->dev);
Simon Glass4ab61742013-02-25 14:08:37 -0800162
163 return 0;
164}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100165EXPORT_SYMBOL(cros_ec_remove);
Simon Glass4ab61742013-02-25 14:08:37 -0800166
167#ifdef CONFIG_PM_SLEEP
168int cros_ec_suspend(struct cros_ec_device *ec_dev)
169{
170 struct device *dev = ec_dev->dev;
171
172 if (device_may_wakeup(dev))
173 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
174
175 disable_irq(ec_dev->irq);
176 ec_dev->was_wake_device = ec_dev->wake_enabled;
177
178 return 0;
179}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100180EXPORT_SYMBOL(cros_ec_suspend);
Simon Glass4ab61742013-02-25 14:08:37 -0800181
182int cros_ec_resume(struct cros_ec_device *ec_dev)
183{
184 enable_irq(ec_dev->irq);
185
186 if (ec_dev->wake_enabled) {
187 disable_irq_wake(ec_dev->irq);
188 ec_dev->wake_enabled = 0;
189 }
190
191 return 0;
192}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100193EXPORT_SYMBOL(cros_ec_resume);
194
Simon Glass4ab61742013-02-25 14:08:37 -0800195#endif
Bill Richardsona865a582014-04-17 12:32:17 -0700196
197MODULE_LICENSE("GPL");
198MODULE_DESCRIPTION("ChromeOS EC core driver");