blob: c53804a1451d4c80777c319924ddc870995337d6 [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
Bill Richardson6db07b62014-06-18 11:14:05 -070047int cros_ec_check_result(struct cros_ec_device *ec_dev,
48 struct cros_ec_command *msg)
49{
50 switch (msg->result) {
51 case EC_RES_SUCCESS:
52 return 0;
53 case EC_RES_IN_PROGRESS:
54 dev_dbg(ec_dev->dev, "command 0x%02x in progress\n",
55 msg->command);
56 return -EAGAIN;
57 default:
58 dev_dbg(ec_dev->dev, "command 0x%02x returned %d\n",
59 msg->command, msg->result);
60 return 0;
61 }
62}
63EXPORT_SYMBOL(cros_ec_check_result);
64
Andrew Brestickera6551a72014-09-18 17:18:56 +020065int cros_ec_cmd_xfer(struct cros_ec_device *ec_dev,
66 struct cros_ec_command *msg)
67{
Andrew Bresticker63427532014-09-18 17:18:57 +020068 int ret;
69
70 mutex_lock(&ec_dev->lock);
71 ret = ec_dev->cmd_xfer(ec_dev, msg);
72 mutex_unlock(&ec_dev->lock);
73
74 return ret;
Andrew Brestickera6551a72014-09-18 17:18:56 +020075}
76EXPORT_SYMBOL(cros_ec_cmd_xfer);
77
Geert Uytterhoeven5ac98552013-11-18 14:33:06 +010078static const struct mfd_cell cros_devs[] = {
Simon Glass4ab61742013-02-25 14:08:37 -080079 {
80 .name = "cros-ec-keyb",
81 .id = 1,
82 .of_compatible = "google,cros-ec-keyb",
83 },
Doug Anderson9d230c92014-04-30 10:44:09 -070084 {
85 .name = "cros-ec-i2c-tunnel",
86 .id = 2,
87 .of_compatible = "google,cros-ec-i2c-tunnel",
88 },
Simon Glass4ab61742013-02-25 14:08:37 -080089};
90
91int cros_ec_register(struct cros_ec_device *ec_dev)
92{
93 struct device *dev = ec_dev->dev;
94 int err = 0;
95
Simon Glass4ab61742013-02-25 14:08:37 -080096 if (ec_dev->din_size) {
Lee Jones22f9ee72013-05-23 16:25:10 +010097 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
98 if (!ec_dev->din)
99 return -ENOMEM;
Simon Glass4ab61742013-02-25 14:08:37 -0800100 }
101 if (ec_dev->dout_size) {
Lee Jones22f9ee72013-05-23 16:25:10 +0100102 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
103 if (!ec_dev->dout)
104 return -ENOMEM;
Simon Glass4ab61742013-02-25 14:08:37 -0800105 }
106
Andrew Bresticker63427532014-09-18 17:18:57 +0200107 mutex_init(&ec_dev->lock);
108
Simon Glass4ab61742013-02-25 14:08:37 -0800109 err = mfd_add_devices(dev, 0, cros_devs,
110 ARRAY_SIZE(cros_devs),
111 NULL, ec_dev->irq, NULL);
112 if (err) {
113 dev_err(dev, "failed to add mfd devices\n");
Andrew Brestickerd1fd3452014-06-18 11:14:07 -0700114 return err;
Simon Glass4ab61742013-02-25 14:08:37 -0800115 }
116
Bill Richardson533cec82014-06-18 11:14:03 -0700117 dev_info(dev, "Chrome EC device registered\n");
Simon Glass4ab61742013-02-25 14:08:37 -0800118
119 return 0;
Simon Glass4ab61742013-02-25 14:08:37 -0800120}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100121EXPORT_SYMBOL(cros_ec_register);
Simon Glass4ab61742013-02-25 14:08:37 -0800122
123int cros_ec_remove(struct cros_ec_device *ec_dev)
124{
125 mfd_remove_devices(ec_dev->dev);
Simon Glass4ab61742013-02-25 14:08:37 -0800126
127 return 0;
128}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100129EXPORT_SYMBOL(cros_ec_remove);
Simon Glass4ab61742013-02-25 14:08:37 -0800130
131#ifdef CONFIG_PM_SLEEP
132int 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}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100144EXPORT_SYMBOL(cros_ec_suspend);
Simon Glass4ab61742013-02-25 14:08:37 -0800145
146int 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}
Samuel Ortiz5ebeaff2013-03-20 09:46:15 +0100157EXPORT_SYMBOL(cros_ec_resume);
158
Simon Glass4ab61742013-02-25 14:08:37 -0800159#endif
Bill Richardsona865a582014-04-17 12:32:17 -0700160
161MODULE_LICENSE("GPL");
162MODULE_DESCRIPTION("ChromeOS EC core driver");