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