Commit | Line | Data |
---|---|---|
89969009 SG |
1 | /* |
2 | * ChromeOS EC multi-function device (I2C) | |
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 | ||
16 | #include <linux/kernel.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/i2c.h> | |
19 | #include <linux/interrupt.h> | |
20 | #include <linux/mfd/cros_ec.h> | |
21 | #include <linux/mfd/cros_ec_commands.h> | |
22 | #include <linux/platform_device.h> | |
23 | #include <linux/slab.h> | |
24 | ||
25 | static inline struct cros_ec_device *to_ec_dev(struct device *dev) | |
26 | { | |
27 | struct i2c_client *client = to_i2c_client(dev); | |
28 | ||
29 | return i2c_get_clientdata(client); | |
30 | } | |
31 | ||
7e6cb5b4 | 32 | static int cros_ec_cmd_xfer_i2c(struct cros_ec_device *ec_dev, |
5d4773e2 | 33 | struct cros_ec_command *msg) |
89969009 SG |
34 | { |
35 | struct i2c_client *client = ec_dev->priv; | |
36 | int ret = -ENOMEM; | |
37 | int i; | |
d6c15ed2 | 38 | int len; |
89969009 SG |
39 | int packet_len; |
40 | u8 *out_buf = NULL; | |
41 | u8 *in_buf = NULL; | |
42 | u8 sum; | |
43 | struct i2c_msg i2c_msg[2]; | |
44 | ||
45 | i2c_msg[0].addr = client->addr; | |
46 | i2c_msg[0].flags = 0; | |
47 | i2c_msg[1].addr = client->addr; | |
48 | i2c_msg[1].flags = I2C_M_RD; | |
49 | ||
50 | /* | |
51 | * allocate larger packet (one byte for checksum, one byte for | |
52 | * length, and one for result code) | |
53 | */ | |
5d4773e2 | 54 | packet_len = msg->insize + 3; |
89969009 SG |
55 | in_buf = kzalloc(packet_len, GFP_KERNEL); |
56 | if (!in_buf) | |
57 | goto done; | |
58 | i2c_msg[1].len = packet_len; | |
59 | i2c_msg[1].buf = (char *)in_buf; | |
60 | ||
61 | /* | |
62 | * allocate larger packet (one byte for checksum, one for | |
63 | * command code, one for length, and one for command version) | |
64 | */ | |
5d4773e2 | 65 | packet_len = msg->outsize + 4; |
89969009 SG |
66 | out_buf = kzalloc(packet_len, GFP_KERNEL); |
67 | if (!out_buf) | |
68 | goto done; | |
69 | i2c_msg[0].len = packet_len; | |
70 | i2c_msg[0].buf = (char *)out_buf; | |
71 | ||
72 | out_buf[0] = EC_CMD_VERSION0 + msg->version; | |
5d4773e2 BR |
73 | out_buf[1] = msg->command; |
74 | out_buf[2] = msg->outsize; | |
89969009 SG |
75 | |
76 | /* copy message payload and compute checksum */ | |
77 | sum = out_buf[0] + out_buf[1] + out_buf[2]; | |
5d4773e2 BR |
78 | for (i = 0; i < msg->outsize; i++) { |
79 | out_buf[3 + i] = msg->outdata[i]; | |
89969009 SG |
80 | sum += out_buf[3 + i]; |
81 | } | |
5d4773e2 | 82 | out_buf[3 + msg->outsize] = sum; |
89969009 SG |
83 | |
84 | /* send command to EC and read answer */ | |
85 | ret = i2c_transfer(client->adapter, i2c_msg, 2); | |
86 | if (ret < 0) { | |
87 | dev_err(ec_dev->dev, "i2c transfer failed: %d\n", ret); | |
88 | goto done; | |
89 | } else if (ret != 2) { | |
90 | dev_err(ec_dev->dev, "failed to get response: %d\n", ret); | |
91 | ret = -EIO; | |
92 | goto done; | |
93 | } | |
94 | ||
95 | /* check response error code */ | |
6db07b63 BR |
96 | msg->result = i2c_msg[1].buf[0]; |
97 | ret = cros_ec_check_result(ec_dev, msg); | |
98 | if (ret) | |
89969009 | 99 | goto done; |
89969009 | 100 | |
d6c15ed2 DA |
101 | len = in_buf[1]; |
102 | if (len > msg->insize) { | |
103 | dev_err(ec_dev->dev, "packet too long (%d bytes, expected %d)", | |
104 | len, msg->insize); | |
105 | ret = -ENOSPC; | |
106 | goto done; | |
107 | } | |
108 | ||
89969009 SG |
109 | /* copy response packet payload and compute checksum */ |
110 | sum = in_buf[0] + in_buf[1]; | |
d6c15ed2 | 111 | for (i = 0; i < len; i++) { |
5d4773e2 | 112 | msg->indata[i] = in_buf[2 + i]; |
89969009 SG |
113 | sum += in_buf[2 + i]; |
114 | } | |
115 | dev_dbg(ec_dev->dev, "packet: %*ph, sum = %02x\n", | |
116 | i2c_msg[1].len, in_buf, sum); | |
d6c15ed2 | 117 | if (sum != in_buf[2 + len]) { |
89969009 SG |
118 | dev_err(ec_dev->dev, "bad packet checksum\n"); |
119 | ret = -EBADMSG; | |
120 | goto done; | |
121 | } | |
122 | ||
d6c15ed2 | 123 | ret = len; |
89969009 SG |
124 | done: |
125 | kfree(in_buf); | |
126 | kfree(out_buf); | |
127 | return ret; | |
128 | } | |
129 | ||
192afe5e | 130 | static int cros_ec_i2c_probe(struct i2c_client *client, |
89969009 SG |
131 | const struct i2c_device_id *dev_id) |
132 | { | |
133 | struct device *dev = &client->dev; | |
134 | struct cros_ec_device *ec_dev = NULL; | |
135 | int err; | |
136 | ||
137 | ec_dev = devm_kzalloc(dev, sizeof(*ec_dev), GFP_KERNEL); | |
138 | if (!ec_dev) | |
139 | return -ENOMEM; | |
140 | ||
141 | i2c_set_clientdata(client, ec_dev); | |
89969009 SG |
142 | ec_dev->dev = dev; |
143 | ec_dev->priv = client; | |
144 | ec_dev->irq = client->irq; | |
7e6cb5b4 | 145 | ec_dev->cmd_xfer = cros_ec_cmd_xfer_i2c; |
89969009 SG |
146 | ec_dev->ec_name = client->name; |
147 | ec_dev->phys_name = client->adapter->name; | |
148 | ec_dev->parent = &client->dev; | |
149 | ||
150 | err = cros_ec_register(ec_dev); | |
151 | if (err) { | |
152 | dev_err(dev, "cannot register EC\n"); | |
153 | return err; | |
154 | } | |
155 | ||
156 | return 0; | |
157 | } | |
158 | ||
192afe5e | 159 | static int cros_ec_i2c_remove(struct i2c_client *client) |
89969009 SG |
160 | { |
161 | struct cros_ec_device *ec_dev = i2c_get_clientdata(client); | |
162 | ||
163 | cros_ec_remove(ec_dev); | |
164 | ||
165 | return 0; | |
166 | } | |
167 | ||
168 | #ifdef CONFIG_PM_SLEEP | |
169 | static int cros_ec_i2c_suspend(struct device *dev) | |
170 | { | |
171 | struct cros_ec_device *ec_dev = to_ec_dev(dev); | |
172 | ||
173 | return cros_ec_suspend(ec_dev); | |
174 | } | |
175 | ||
176 | static int cros_ec_i2c_resume(struct device *dev) | |
177 | { | |
178 | struct cros_ec_device *ec_dev = to_ec_dev(dev); | |
179 | ||
180 | return cros_ec_resume(ec_dev); | |
181 | } | |
182 | #endif | |
183 | ||
184 | static SIMPLE_DEV_PM_OPS(cros_ec_i2c_pm_ops, cros_ec_i2c_suspend, | |
185 | cros_ec_i2c_resume); | |
186 | ||
187 | static const struct i2c_device_id cros_ec_i2c_id[] = { | |
188 | { "cros-ec-i2c", 0 }, | |
189 | { } | |
190 | }; | |
191 | MODULE_DEVICE_TABLE(i2c, cros_ec_i2c_id); | |
192 | ||
193 | static struct i2c_driver cros_ec_driver = { | |
194 | .driver = { | |
195 | .name = "cros-ec-i2c", | |
196 | .owner = THIS_MODULE, | |
197 | .pm = &cros_ec_i2c_pm_ops, | |
198 | }, | |
192afe5e TR |
199 | .probe = cros_ec_i2c_probe, |
200 | .remove = cros_ec_i2c_remove, | |
89969009 SG |
201 | .id_table = cros_ec_i2c_id, |
202 | }; | |
203 | ||
204 | module_i2c_driver(cros_ec_driver); | |
205 | ||
206 | MODULE_LICENSE("GPL"); | |
207 | MODULE_DESCRIPTION("ChromeOS EC multi function device"); |