Merge tag 'xtensa-20140830' of git://github.com/czankel/xtensa-linux
[deliverable/linux.git] / drivers / mfd / cros_ec.c
CommitLineData
4ab6174e
SG
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>
5ebeaff5 22#include <linux/module.h>
4ab6174e
SG
23#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,
5d4773e2 28 struct cros_ec_command *msg)
4ab6174e
SG
29{
30 uint8_t *out;
31 int csum, i;
32
5d4773e2 33 BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE);
4ab6174e
SG
34 out = ec_dev->dout;
35 out[0] = EC_CMD_VERSION0 + msg->version;
5d4773e2
BR
36 out[1] = msg->command;
37 out[2] = msg->outsize;
4ab6174e 38 csum = out[0] + out[1] + out[2];
5d4773e2
BR
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);
4ab6174e 42
5d4773e2 43 return EC_MSG_TX_PROTO_BYTES + msg->outsize;
4ab6174e 44}
5ebeaff5 45EXPORT_SYMBOL(cros_ec_prepare_tx);
4ab6174e 46
6db07b63
BR
47int 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
5ac98553 65static const struct mfd_cell cros_devs[] = {
4ab6174e
SG
66 {
67 .name = "cros-ec-keyb",
68 .id = 1,
69 .of_compatible = "google,cros-ec-keyb",
70 },
9d230c9e
DA
71 {
72 .name = "cros-ec-i2c-tunnel",
73 .id = 2,
74 .of_compatible = "google,cros-ec-i2c-tunnel",
75 },
4ab6174e
SG
76};
77
78int cros_ec_register(struct cros_ec_device *ec_dev)
79{
80 struct device *dev = ec_dev->dev;
81 int err = 0;
82
4ab6174e 83 if (ec_dev->din_size) {
22f9ee75
LJ
84 ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL);
85 if (!ec_dev->din)
86 return -ENOMEM;
4ab6174e
SG
87 }
88 if (ec_dev->dout_size) {
22f9ee75
LJ
89 ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL);
90 if (!ec_dev->dout)
91 return -ENOMEM;
4ab6174e
SG
92 }
93
4ab6174e
SG
94 err = mfd_add_devices(dev, 0, cros_devs,
95 ARRAY_SIZE(cros_devs),
96 NULL, ec_dev->irq, NULL);
97 if (err) {
98 dev_err(dev, "failed to add mfd devices\n");
d1fd345e 99 return err;
4ab6174e
SG
100 }
101
533cec8f 102 dev_info(dev, "Chrome EC device registered\n");
4ab6174e
SG
103
104 return 0;
4ab6174e 105}
5ebeaff5 106EXPORT_SYMBOL(cros_ec_register);
4ab6174e
SG
107
108int cros_ec_remove(struct cros_ec_device *ec_dev)
109{
110 mfd_remove_devices(ec_dev->dev);
4ab6174e
SG
111
112 return 0;
113}
5ebeaff5 114EXPORT_SYMBOL(cros_ec_remove);
4ab6174e
SG
115
116#ifdef CONFIG_PM_SLEEP
117int cros_ec_suspend(struct cros_ec_device *ec_dev)
118{
119 struct device *dev = ec_dev->dev;
120
121 if (device_may_wakeup(dev))
122 ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq);
123
124 disable_irq(ec_dev->irq);
125 ec_dev->was_wake_device = ec_dev->wake_enabled;
126
127 return 0;
128}
5ebeaff5 129EXPORT_SYMBOL(cros_ec_suspend);
4ab6174e
SG
130
131int cros_ec_resume(struct cros_ec_device *ec_dev)
132{
133 enable_irq(ec_dev->irq);
134
135 if (ec_dev->wake_enabled) {
136 disable_irq_wake(ec_dev->irq);
137 ec_dev->wake_enabled = 0;
138 }
139
140 return 0;
141}
5ebeaff5
SO
142EXPORT_SYMBOL(cros_ec_resume);
143
4ab6174e 144#endif
a865a589
BR
145
146MODULE_LICENSE("GPL");
147MODULE_DESCRIPTION("ChromeOS EC core driver");
This page took 0.105152 seconds and 5 git commands to generate.