Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 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> |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 22 | #include <linux/module.h> |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 23 | #include <linux/mfd/core.h> |
| 24 | #include <linux/mfd/cros_ec.h> |
| 25 | #include <linux/mfd/cros_ec_commands.h> |
| 26 | |
| 27 | int cros_ec_prepare_tx(struct cros_ec_device *ec_dev, |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 28 | struct cros_ec_command *msg) |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 29 | { |
| 30 | uint8_t *out; |
| 31 | int csum, i; |
| 32 | |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 33 | BUG_ON(msg->outsize > EC_PROTO2_MAX_PARAM_SIZE); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 34 | out = ec_dev->dout; |
| 35 | out[0] = EC_CMD_VERSION0 + msg->version; |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 36 | out[1] = msg->command; |
| 37 | out[2] = msg->outsize; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 38 | csum = out[0] + out[1] + out[2]; |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 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); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 42 | |
Bill Richardson | 5d4773e | 2014-06-18 11:14:02 -0700 | [diff] [blame] | 43 | return EC_MSG_TX_PROTO_BYTES + msg->outsize; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 44 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 45 | EXPORT_SYMBOL(cros_ec_prepare_tx); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 46 | |
Bill Richardson | 6db07b6 | 2014-06-18 11:14:05 -0700 | [diff] [blame] | 47 | int 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 | } |
| 63 | EXPORT_SYMBOL(cros_ec_check_result); |
| 64 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 65 | static irqreturn_t ec_irq_thread(int irq, void *data) |
| 66 | { |
| 67 | struct cros_ec_device *ec_dev = data; |
| 68 | |
| 69 | if (device_may_wakeup(ec_dev->dev)) |
| 70 | pm_wakeup_event(ec_dev->dev, 0); |
| 71 | |
| 72 | blocking_notifier_call_chain(&ec_dev->event_notifier, 1, ec_dev); |
| 73 | |
| 74 | return IRQ_HANDLED; |
| 75 | } |
| 76 | |
Geert Uytterhoeven | 5ac9855 | 2013-11-18 14:33:06 +0100 | [diff] [blame] | 77 | static const struct mfd_cell cros_devs[] = { |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 78 | { |
| 79 | .name = "cros-ec-keyb", |
| 80 | .id = 1, |
| 81 | .of_compatible = "google,cros-ec-keyb", |
| 82 | }, |
Doug Anderson | 9d230c9 | 2014-04-30 10:44:09 -0700 | [diff] [blame] | 83 | { |
| 84 | .name = "cros-ec-i2c-tunnel", |
| 85 | .id = 2, |
| 86 | .of_compatible = "google,cros-ec-i2c-tunnel", |
| 87 | }, |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | int cros_ec_register(struct cros_ec_device *ec_dev) |
| 91 | { |
| 92 | struct device *dev = ec_dev->dev; |
| 93 | int err = 0; |
| 94 | |
| 95 | BLOCKING_INIT_NOTIFIER_HEAD(&ec_dev->event_notifier); |
| 96 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 97 | if (ec_dev->din_size) { |
Lee Jones | 22f9ee7 | 2013-05-23 16:25:10 +0100 | [diff] [blame] | 98 | ec_dev->din = devm_kzalloc(dev, ec_dev->din_size, GFP_KERNEL); |
| 99 | if (!ec_dev->din) |
| 100 | return -ENOMEM; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 101 | } |
| 102 | if (ec_dev->dout_size) { |
Lee Jones | 22f9ee7 | 2013-05-23 16:25:10 +0100 | [diff] [blame] | 103 | ec_dev->dout = devm_kzalloc(dev, ec_dev->dout_size, GFP_KERNEL); |
| 104 | if (!ec_dev->dout) |
| 105 | return -ENOMEM; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | if (!ec_dev->irq) { |
| 109 | dev_dbg(dev, "no valid IRQ: %d\n", ec_dev->irq); |
Lee Jones | 22f9ee7 | 2013-05-23 16:25:10 +0100 | [diff] [blame] | 110 | return err; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | err = request_threaded_irq(ec_dev->irq, NULL, ec_irq_thread, |
| 114 | IRQF_TRIGGER_LOW | IRQF_ONESHOT, |
| 115 | "chromeos-ec", ec_dev); |
| 116 | if (err) { |
| 117 | dev_err(dev, "request irq %d: error %d\n", ec_dev->irq, err); |
Lee Jones | 22f9ee7 | 2013-05-23 16:25:10 +0100 | [diff] [blame] | 118 | return err; |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | err = mfd_add_devices(dev, 0, cros_devs, |
| 122 | ARRAY_SIZE(cros_devs), |
| 123 | NULL, ec_dev->irq, NULL); |
| 124 | if (err) { |
| 125 | dev_err(dev, "failed to add mfd devices\n"); |
| 126 | goto fail_mfd; |
| 127 | } |
| 128 | |
Bill Richardson | 533cec8 | 2014-06-18 11:14:03 -0700 | [diff] [blame] | 129 | dev_info(dev, "Chrome EC device registered\n"); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 130 | |
| 131 | return 0; |
| 132 | |
| 133 | fail_mfd: |
| 134 | free_irq(ec_dev->irq, ec_dev); |
Lee Jones | 22f9ee7 | 2013-05-23 16:25:10 +0100 | [diff] [blame] | 135 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 136 | return err; |
| 137 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 138 | EXPORT_SYMBOL(cros_ec_register); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 139 | |
| 140 | int cros_ec_remove(struct cros_ec_device *ec_dev) |
| 141 | { |
| 142 | mfd_remove_devices(ec_dev->dev); |
| 143 | free_irq(ec_dev->irq, ec_dev); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 144 | |
| 145 | return 0; |
| 146 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 147 | EXPORT_SYMBOL(cros_ec_remove); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 148 | |
| 149 | #ifdef CONFIG_PM_SLEEP |
| 150 | int cros_ec_suspend(struct cros_ec_device *ec_dev) |
| 151 | { |
| 152 | struct device *dev = ec_dev->dev; |
| 153 | |
| 154 | if (device_may_wakeup(dev)) |
| 155 | ec_dev->wake_enabled = !enable_irq_wake(ec_dev->irq); |
| 156 | |
| 157 | disable_irq(ec_dev->irq); |
| 158 | ec_dev->was_wake_device = ec_dev->wake_enabled; |
| 159 | |
| 160 | return 0; |
| 161 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 162 | EXPORT_SYMBOL(cros_ec_suspend); |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 163 | |
| 164 | int cros_ec_resume(struct cros_ec_device *ec_dev) |
| 165 | { |
| 166 | enable_irq(ec_dev->irq); |
| 167 | |
| 168 | if (ec_dev->wake_enabled) { |
| 169 | disable_irq_wake(ec_dev->irq); |
| 170 | ec_dev->wake_enabled = 0; |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
Samuel Ortiz | 5ebeaff | 2013-03-20 09:46:15 +0100 | [diff] [blame] | 175 | EXPORT_SYMBOL(cros_ec_resume); |
| 176 | |
Simon Glass | 4ab6174 | 2013-02-25 14:08:37 -0800 | [diff] [blame] | 177 | #endif |
Bill Richardson | a865a58 | 2014-04-17 12:32:17 -0700 | [diff] [blame] | 178 | |
| 179 | MODULE_LICENSE("GPL"); |
| 180 | MODULE_DESCRIPTION("ChromeOS EC core driver"); |