Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 1 | /* |
| 2 | * cros_ec_dev - expose the Chrome OS Embedded Controller to user-space |
| 3 | * |
| 4 | * Copyright (C) 2014 Google, Inc. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <linux/fs.h> |
Enric Balletbo i Serra | 5668bfd | 2016-08-01 11:54:38 +0200 | [diff] [blame] | 21 | #include <linux/mfd/core.h> |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 22 | #include <linux/module.h> |
Randy Dunlap | ac31672 | 2018-06-19 22:47:28 -0700 | [diff] [blame] | 23 | #include <linux/mod_devicetable.h> |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 24 | #include <linux/platform_device.h> |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 25 | #include <linux/pm.h> |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 26 | #include <linux/slab.h> |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
| 28 | |
| 29 | #include "cros_ec_dev.h" |
| 30 | |
Thierry Escande | ea01a31 | 2017-11-20 17:15:25 +0100 | [diff] [blame] | 31 | #define DRV_NAME "cros-ec-dev" |
| 32 | |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 33 | /* Device variables */ |
| 34 | #define CROS_MAX_DEV 128 |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 35 | static int ec_major; |
| 36 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 37 | static const struct attribute_group *cros_ec_groups[] = { |
| 38 | &cros_ec_attr_group, |
| 39 | &cros_ec_lightbar_attr_group, |
Emilio López | 18800fc | 2015-09-21 10:38:22 -0300 | [diff] [blame] | 40 | &cros_ec_vbc_attr_group, |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 41 | NULL, |
| 42 | }; |
| 43 | |
| 44 | static struct class cros_class = { |
| 45 | .owner = THIS_MODULE, |
| 46 | .name = "chromeos", |
| 47 | .dev_groups = cros_ec_groups, |
| 48 | }; |
| 49 | |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 50 | /* Basic communication */ |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 51 | static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen) |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 52 | { |
| 53 | struct ec_response_get_version *resp; |
| 54 | static const char * const current_image_name[] = { |
| 55 | "unknown", "read-only", "read-write", "invalid", |
| 56 | }; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 57 | struct cros_ec_command *msg; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 58 | int ret; |
| 59 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 60 | msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL); |
| 61 | if (!msg) |
| 62 | return -ENOMEM; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 63 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 64 | msg->version = 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 65 | msg->command = EC_CMD_GET_VERSION + ec->cmd_offset; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 66 | msg->insize = sizeof(*resp); |
| 67 | msg->outsize = 0; |
| 68 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 69 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 70 | if (ret < 0) |
| 71 | goto exit; |
| 72 | |
| 73 | if (msg->result != EC_RES_SUCCESS) { |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 74 | snprintf(str, maxlen, |
| 75 | "%s\nUnknown EC version: EC returned %d\n", |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 76 | CROS_EC_DEV_VERSION, msg->result); |
| 77 | ret = -EINVAL; |
| 78 | goto exit; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 79 | } |
| 80 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 81 | resp = (struct ec_response_get_version *)msg->data; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 82 | if (resp->current_image >= ARRAY_SIZE(current_image_name)) |
| 83 | resp->current_image = 3; /* invalid */ |
| 84 | |
Olof Johansson | ef59c25 | 2015-03-03 13:22:32 +0100 | [diff] [blame] | 85 | snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION, |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 86 | resp->version_string_ro, resp->version_string_rw, |
| 87 | current_image_name[resp->current_image]); |
| 88 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 89 | ret = 0; |
| 90 | exit: |
| 91 | kfree(msg); |
| 92 | return ret; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 93 | } |
| 94 | |
Vincent Palatin | e4244eb | 2016-08-01 11:54:37 +0200 | [diff] [blame] | 95 | static int cros_ec_check_features(struct cros_ec_dev *ec, int feature) |
| 96 | { |
| 97 | struct cros_ec_command *msg; |
| 98 | int ret; |
| 99 | |
| 100 | if (ec->features[0] == -1U && ec->features[1] == -1U) { |
| 101 | /* features bitmap not read yet */ |
| 102 | |
| 103 | msg = kmalloc(sizeof(*msg) + sizeof(ec->features), GFP_KERNEL); |
| 104 | if (!msg) |
| 105 | return -ENOMEM; |
| 106 | |
| 107 | msg->version = 0; |
| 108 | msg->command = EC_CMD_GET_FEATURES + ec->cmd_offset; |
| 109 | msg->insize = sizeof(ec->features); |
| 110 | msg->outsize = 0; |
| 111 | |
| 112 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
| 113 | if (ret < 0 || msg->result != EC_RES_SUCCESS) { |
| 114 | dev_warn(ec->dev, "cannot get EC features: %d/%d\n", |
| 115 | ret, msg->result); |
| 116 | memset(ec->features, 0, sizeof(ec->features)); |
Stephen Boyd | df7c3bf | 2018-05-30 23:23:43 -0700 | [diff] [blame] | 117 | } else { |
| 118 | memcpy(ec->features, msg->data, sizeof(ec->features)); |
Vincent Palatin | e4244eb | 2016-08-01 11:54:37 +0200 | [diff] [blame] | 119 | } |
| 120 | |
Vincent Palatin | e4244eb | 2016-08-01 11:54:37 +0200 | [diff] [blame] | 121 | dev_dbg(ec->dev, "EC features %08x %08x\n", |
| 122 | ec->features[0], ec->features[1]); |
| 123 | |
| 124 | kfree(msg); |
| 125 | } |
| 126 | |
| 127 | return ec->features[feature / 32] & EC_FEATURE_MASK_0(feature); |
| 128 | } |
| 129 | |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 130 | /* Device file ops */ |
| 131 | static int ec_device_open(struct inode *inode, struct file *filp) |
| 132 | { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 133 | struct cros_ec_dev *ec = container_of(inode->i_cdev, |
| 134 | struct cros_ec_dev, cdev); |
| 135 | filp->private_data = ec; |
| 136 | nonseekable_open(inode, filp); |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | static int ec_device_release(struct inode *inode, struct file *filp) |
| 141 | { |
| 142 | return 0; |
| 143 | } |
| 144 | |
| 145 | static ssize_t ec_device_read(struct file *filp, char __user *buffer, |
| 146 | size_t length, loff_t *offset) |
| 147 | { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 148 | struct cros_ec_dev *ec = filp->private_data; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 149 | char msg[sizeof(struct ec_response_get_version) + |
| 150 | sizeof(CROS_EC_DEV_VERSION)]; |
| 151 | size_t count; |
| 152 | int ret; |
| 153 | |
| 154 | if (*offset != 0) |
| 155 | return 0; |
| 156 | |
| 157 | ret = ec_get_version(ec, msg, sizeof(msg)); |
| 158 | if (ret) |
| 159 | return ret; |
| 160 | |
| 161 | count = min(length, strlen(msg)); |
| 162 | |
| 163 | if (copy_to_user(buffer, msg, count)) |
| 164 | return -EFAULT; |
| 165 | |
| 166 | *offset = count; |
| 167 | return count; |
| 168 | } |
| 169 | |
| 170 | /* Ioctls */ |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 171 | static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg) |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 172 | { |
| 173 | long ret; |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 174 | struct cros_ec_command u_cmd; |
| 175 | struct cros_ec_command *s_cmd; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 176 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 177 | if (copy_from_user(&u_cmd, arg, sizeof(u_cmd))) |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 178 | return -EFAULT; |
| 179 | |
Gwendal Grignou | 5d749d0 | 2016-03-08 09:13:52 -0800 | [diff] [blame] | 180 | if ((u_cmd.outsize > EC_MAX_MSG_BYTES) || |
| 181 | (u_cmd.insize > EC_MAX_MSG_BYTES)) |
| 182 | return -EINVAL; |
| 183 | |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 184 | s_cmd = kmalloc(sizeof(*s_cmd) + max(u_cmd.outsize, u_cmd.insize), |
| 185 | GFP_KERNEL); |
| 186 | if (!s_cmd) |
| 187 | return -ENOMEM; |
| 188 | |
| 189 | if (copy_from_user(s_cmd, arg, sizeof(*s_cmd) + u_cmd.outsize)) { |
| 190 | ret = -EFAULT; |
| 191 | goto exit; |
| 192 | } |
| 193 | |
Dan Carpenter | 096cdc6 | 2016-06-21 16:58:46 +0300 | [diff] [blame] | 194 | if (u_cmd.outsize != s_cmd->outsize || |
| 195 | u_cmd.insize != s_cmd->insize) { |
| 196 | ret = -EINVAL; |
| 197 | goto exit; |
| 198 | } |
| 199 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 200 | s_cmd->command += ec->cmd_offset; |
| 201 | ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd); |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 202 | /* Only copy data to userland if data was received. */ |
| 203 | if (ret < 0) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 204 | goto exit; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 205 | |
Dan Carpenter | 096cdc6 | 2016-06-21 16:58:46 +0300 | [diff] [blame] | 206 | if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize)) |
Javier Martinez Canillas | a841178 | 2015-06-09 13:04:42 +0200 | [diff] [blame] | 207 | ret = -EFAULT; |
| 208 | exit: |
| 209 | kfree(s_cmd); |
| 210 | return ret; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 211 | } |
| 212 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 213 | static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg) |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 214 | { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 215 | struct cros_ec_device *ec_dev = ec->ec_dev; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 216 | struct cros_ec_readmem s_mem = { }; |
| 217 | long num; |
| 218 | |
| 219 | /* Not every platform supports direct reads */ |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 220 | if (!ec_dev->cmd_readmem) |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 221 | return -ENOTTY; |
| 222 | |
| 223 | if (copy_from_user(&s_mem, arg, sizeof(s_mem))) |
| 224 | return -EFAULT; |
| 225 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 226 | num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes, |
| 227 | s_mem.buffer); |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 228 | if (num <= 0) |
| 229 | return num; |
| 230 | |
| 231 | if (copy_to_user((void __user *)arg, &s_mem, sizeof(s_mem))) |
| 232 | return -EFAULT; |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static long ec_device_ioctl(struct file *filp, unsigned int cmd, |
| 238 | unsigned long arg) |
| 239 | { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 240 | struct cros_ec_dev *ec = filp->private_data; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 241 | |
| 242 | if (_IOC_TYPE(cmd) != CROS_EC_DEV_IOC) |
| 243 | return -ENOTTY; |
| 244 | |
| 245 | switch (cmd) { |
| 246 | case CROS_EC_DEV_IOCXCMD: |
| 247 | return ec_device_ioctl_xcmd(ec, (void __user *)arg); |
| 248 | case CROS_EC_DEV_IOCRDMEM: |
| 249 | return ec_device_ioctl_readmem(ec, (void __user *)arg); |
| 250 | } |
| 251 | |
| 252 | return -ENOTTY; |
| 253 | } |
| 254 | |
| 255 | /* Module initialization */ |
| 256 | static const struct file_operations fops = { |
| 257 | .open = ec_device_open, |
| 258 | .release = ec_device_release, |
| 259 | .read = ec_device_read, |
| 260 | .unlocked_ioctl = ec_device_ioctl, |
Guenter Roeck | 2521ea3 | 2016-04-14 19:35:29 -0700 | [diff] [blame] | 261 | #ifdef CONFIG_COMPAT |
| 262 | .compat_ioctl = ec_device_ioctl, |
| 263 | #endif |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 264 | }; |
| 265 | |
Enric Balletbo i Serra | 5668bfd | 2016-08-01 11:54:38 +0200 | [diff] [blame] | 266 | static void cros_ec_sensors_register(struct cros_ec_dev *ec) |
| 267 | { |
| 268 | /* |
| 269 | * Issue a command to get the number of sensor reported. |
| 270 | * Build an array of sensors driver and register them all. |
| 271 | */ |
| 272 | int ret, i, id, sensor_num; |
| 273 | struct mfd_cell *sensor_cells; |
| 274 | struct cros_ec_sensor_platform *sensor_platforms; |
| 275 | int sensor_type[MOTIONSENSE_TYPE_MAX]; |
| 276 | struct ec_params_motion_sense *params; |
| 277 | struct ec_response_motion_sense *resp; |
| 278 | struct cros_ec_command *msg; |
| 279 | |
| 280 | msg = kzalloc(sizeof(struct cros_ec_command) + |
| 281 | max(sizeof(*params), sizeof(*resp)), GFP_KERNEL); |
| 282 | if (msg == NULL) |
| 283 | return; |
| 284 | |
| 285 | msg->version = 2; |
| 286 | msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset; |
| 287 | msg->outsize = sizeof(*params); |
| 288 | msg->insize = sizeof(*resp); |
| 289 | |
| 290 | params = (struct ec_params_motion_sense *)msg->data; |
| 291 | params->cmd = MOTIONSENSE_CMD_DUMP; |
| 292 | |
| 293 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
| 294 | if (ret < 0 || msg->result != EC_RES_SUCCESS) { |
| 295 | dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n", |
| 296 | ret, msg->result); |
| 297 | goto error; |
| 298 | } |
| 299 | |
| 300 | resp = (struct ec_response_motion_sense *)msg->data; |
| 301 | sensor_num = resp->dump.sensor_count; |
Gwendal Grignou | c1d1e91a | 2018-03-23 18:42:47 +0100 | [diff] [blame] | 302 | /* Allocate 1 extra sensors in FIFO are needed */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 303 | sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell), |
Enric Balletbo i Serra | 5668bfd | 2016-08-01 11:54:38 +0200 | [diff] [blame] | 304 | GFP_KERNEL); |
| 305 | if (sensor_cells == NULL) |
| 306 | goto error; |
| 307 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 308 | sensor_platforms = kcalloc(sensor_num + 1, |
| 309 | sizeof(struct cros_ec_sensor_platform), |
| 310 | GFP_KERNEL); |
Enric Balletbo i Serra | 5668bfd | 2016-08-01 11:54:38 +0200 | [diff] [blame] | 311 | if (sensor_platforms == NULL) |
| 312 | goto error_platforms; |
| 313 | |
| 314 | memset(sensor_type, 0, sizeof(sensor_type)); |
| 315 | id = 0; |
| 316 | for (i = 0; i < sensor_num; i++) { |
| 317 | params->cmd = MOTIONSENSE_CMD_INFO; |
| 318 | params->info.sensor_num = i; |
| 319 | ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
| 320 | if (ret < 0 || msg->result != EC_RES_SUCCESS) { |
| 321 | dev_warn(ec->dev, "no info for EC sensor %d : %d/%d\n", |
| 322 | i, ret, msg->result); |
| 323 | continue; |
| 324 | } |
| 325 | switch (resp->info.type) { |
| 326 | case MOTIONSENSE_TYPE_ACCEL: |
| 327 | sensor_cells[id].name = "cros-ec-accel"; |
| 328 | break; |
Gwendal Grignou | d732248 | 2017-01-24 14:41:41 +0100 | [diff] [blame] | 329 | case MOTIONSENSE_TYPE_BARO: |
| 330 | sensor_cells[id].name = "cros-ec-baro"; |
| 331 | break; |
Enric Balletbo i Serra | 5668bfd | 2016-08-01 11:54:38 +0200 | [diff] [blame] | 332 | case MOTIONSENSE_TYPE_GYRO: |
| 333 | sensor_cells[id].name = "cros-ec-gyro"; |
| 334 | break; |
| 335 | case MOTIONSENSE_TYPE_MAG: |
| 336 | sensor_cells[id].name = "cros-ec-mag"; |
| 337 | break; |
| 338 | case MOTIONSENSE_TYPE_PROX: |
| 339 | sensor_cells[id].name = "cros-ec-prox"; |
| 340 | break; |
| 341 | case MOTIONSENSE_TYPE_LIGHT: |
| 342 | sensor_cells[id].name = "cros-ec-light"; |
| 343 | break; |
| 344 | case MOTIONSENSE_TYPE_ACTIVITY: |
| 345 | sensor_cells[id].name = "cros-ec-activity"; |
| 346 | break; |
| 347 | default: |
| 348 | dev_warn(ec->dev, "unknown type %d\n", resp->info.type); |
| 349 | continue; |
| 350 | } |
| 351 | sensor_platforms[id].sensor_num = i; |
| 352 | sensor_cells[id].id = sensor_type[resp->info.type]; |
| 353 | sensor_cells[id].platform_data = &sensor_platforms[id]; |
| 354 | sensor_cells[id].pdata_size = |
| 355 | sizeof(struct cros_ec_sensor_platform); |
| 356 | |
| 357 | sensor_type[resp->info.type]++; |
| 358 | id++; |
| 359 | } |
Enric Balletbo i Serra | 5668bfd | 2016-08-01 11:54:38 +0200 | [diff] [blame] | 360 | |
Gwendal Grignou | c1d1e91a | 2018-03-23 18:42:47 +0100 | [diff] [blame] | 361 | if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2) |
| 362 | ec->has_kb_wake_angle = true; |
| 363 | |
Enric Balletbo i Serra | 5668bfd | 2016-08-01 11:54:38 +0200 | [diff] [blame] | 364 | if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) { |
| 365 | sensor_cells[id].name = "cros-ec-ring"; |
| 366 | id++; |
| 367 | } |
| 368 | |
| 369 | ret = mfd_add_devices(ec->dev, 0, sensor_cells, id, |
| 370 | NULL, 0, NULL); |
| 371 | if (ret) |
| 372 | dev_err(ec->dev, "failed to add EC sensors\n"); |
| 373 | |
| 374 | kfree(sensor_platforms); |
| 375 | error_platforms: |
| 376 | kfree(sensor_cells); |
| 377 | error: |
| 378 | kfree(msg); |
| 379 | } |
| 380 | |
Neil Armstrong | 03a5755 | 2018-07-04 17:08:20 +0200 | [diff] [blame] | 381 | static const struct mfd_cell cros_ec_cec_cells[] = { |
| 382 | { .name = "cros-ec-cec" } |
| 383 | }; |
| 384 | |
Enric Balletbo i Serra | 95a4d07 | 2018-04-18 12:24:01 +0200 | [diff] [blame] | 385 | static const struct mfd_cell cros_ec_rtc_cells[] = { |
| 386 | { .name = "cros-ec-rtc" } |
| 387 | }; |
| 388 | |
Enric Balletbo i Serra | 3144dce | 2018-05-02 17:44:18 +0200 | [diff] [blame] | 389 | static const struct mfd_cell cros_usbpd_charger_cells[] = { |
| 390 | { .name = "cros-usbpd-charger" } |
| 391 | }; |
| 392 | |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 393 | static int ec_device_probe(struct platform_device *pdev) |
| 394 | { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 395 | int retval = -ENOMEM; |
| 396 | struct device *dev = &pdev->dev; |
| 397 | struct cros_ec_platform *ec_platform = dev_get_platdata(dev); |
Gwendal Grignou | 3aa2177 | 2018-05-30 09:54:22 -0700 | [diff] [blame] | 398 | struct cros_ec_dev *ec = devm_kzalloc(dev, sizeof(*ec), GFP_KERNEL); |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 399 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 400 | if (!ec) |
| 401 | return retval; |
| 402 | |
| 403 | dev_set_drvdata(dev, ec); |
| 404 | ec->ec_dev = dev_get_drvdata(dev->parent); |
| 405 | ec->dev = dev; |
| 406 | ec->cmd_offset = ec_platform->cmd_offset; |
Vincent Palatin | e4244eb | 2016-08-01 11:54:37 +0200 | [diff] [blame] | 407 | ec->features[0] = -1U; /* Not cached yet */ |
| 408 | ec->features[1] = -1U; /* Not cached yet */ |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 409 | device_initialize(&ec->class_dev); |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 410 | cdev_init(&ec->cdev, &fops); |
| 411 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 412 | /* |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 413 | * Add the class device |
| 414 | * Link to the character device for creating the /dev entry |
| 415 | * in devtmpfs. |
| 416 | */ |
Logan Gunthorpe | 1c1d152 | 2017-03-17 12:48:14 -0600 | [diff] [blame] | 417 | ec->class_dev.devt = MKDEV(ec_major, pdev->id); |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 418 | ec->class_dev.class = &cros_class; |
| 419 | ec->class_dev.parent = dev; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 420 | |
| 421 | retval = dev_set_name(&ec->class_dev, "%s", ec_platform->ec_name); |
| 422 | if (retval) { |
| 423 | dev_err(dev, "dev_set_name failed => %d\n", retval); |
Logan Gunthorpe | 1c1d152 | 2017-03-17 12:48:14 -0600 | [diff] [blame] | 424 | goto failed; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 425 | } |
| 426 | |
Gwendal Grignou | c1d1e91a | 2018-03-23 18:42:47 +0100 | [diff] [blame] | 427 | /* check whether this EC is a sensor hub. */ |
| 428 | if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE)) |
| 429 | cros_ec_sensors_register(ec); |
| 430 | |
Neil Armstrong | 03a5755 | 2018-07-04 17:08:20 +0200 | [diff] [blame] | 431 | /* Check whether this EC instance has CEC host command support */ |
| 432 | if (cros_ec_check_features(ec, EC_FEATURE_CEC)) { |
| 433 | retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, |
| 434 | cros_ec_cec_cells, |
| 435 | ARRAY_SIZE(cros_ec_cec_cells), |
| 436 | NULL, 0, NULL); |
| 437 | if (retval) |
| 438 | dev_err(ec->dev, |
| 439 | "failed to add cros-ec-cec device: %d\n", |
| 440 | retval); |
| 441 | } |
| 442 | |
Enric Balletbo i Serra | 95a4d07 | 2018-04-18 12:24:01 +0200 | [diff] [blame] | 443 | /* Check whether this EC instance has RTC host command support */ |
| 444 | if (cros_ec_check_features(ec, EC_FEATURE_RTC)) { |
| 445 | retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, |
| 446 | cros_ec_rtc_cells, |
| 447 | ARRAY_SIZE(cros_ec_rtc_cells), |
| 448 | NULL, 0, NULL); |
| 449 | if (retval) |
| 450 | dev_err(ec->dev, |
| 451 | "failed to add cros-ec-rtc device: %d\n", |
| 452 | retval); |
| 453 | } |
| 454 | |
Enric Balletbo i Serra | 3144dce | 2018-05-02 17:44:18 +0200 | [diff] [blame] | 455 | /* Check whether this EC instance has the PD charge manager */ |
| 456 | if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) { |
| 457 | retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, |
| 458 | cros_usbpd_charger_cells, |
| 459 | ARRAY_SIZE(cros_usbpd_charger_cells), |
| 460 | NULL, 0, NULL); |
| 461 | if (retval) |
| 462 | dev_err(ec->dev, |
| 463 | "failed to add cros-usbpd-charger device: %d\n", |
| 464 | retval); |
| 465 | } |
| 466 | |
Gwendal Grignou | c1d1e91a | 2018-03-23 18:42:47 +0100 | [diff] [blame] | 467 | /* Take control of the lightbar from the EC. */ |
| 468 | lb_manual_suspend_ctrl(ec, 1); |
| 469 | |
| 470 | /* We can now add the sysfs class, we know which parameter to show */ |
Logan Gunthorpe | 1c1d152 | 2017-03-17 12:48:14 -0600 | [diff] [blame] | 471 | retval = cdev_device_add(&ec->cdev, &ec->class_dev); |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 472 | if (retval) { |
Logan Gunthorpe | 1c1d152 | 2017-03-17 12:48:14 -0600 | [diff] [blame] | 473 | dev_err(dev, "cdev_device_add failed => %d\n", retval); |
| 474 | goto failed; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 475 | } |
Bill Richardson | 71af4b5 | 2015-02-02 12:26:27 +0100 | [diff] [blame] | 476 | |
Eric Caruso | e862645 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 477 | if (cros_ec_debugfs_init(ec)) |
| 478 | dev_warn(dev, "failed to create debugfs directory\n"); |
| 479 | |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 480 | return 0; |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 481 | |
Logan Gunthorpe | 1c1d152 | 2017-03-17 12:48:14 -0600 | [diff] [blame] | 482 | failed: |
| 483 | put_device(&ec->class_dev); |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 484 | return retval; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | static int ec_device_remove(struct platform_device *pdev) |
| 488 | { |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 489 | struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev); |
Eric Caruso | e862645 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 490 | |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 491 | /* Let the EC take over the lightbar again. */ |
Jeffery Yu | 995c0ec | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 492 | lb_manual_suspend_ctrl(ec, 0); |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 493 | |
Eric Caruso | e862645 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 494 | cros_ec_debugfs_remove(ec); |
| 495 | |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 496 | cdev_del(&ec->cdev); |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 497 | device_unregister(&ec->class_dev); |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 498 | return 0; |
| 499 | } |
| 500 | |
Daniel Hung-yu Wu | e15b7f4 | 2018-04-18 12:24:02 +0200 | [diff] [blame] | 501 | static void ec_device_shutdown(struct platform_device *pdev) |
| 502 | { |
| 503 | struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev); |
| 504 | |
| 505 | /* Be sure to clear up debugfs delayed works */ |
| 506 | cros_ec_debugfs_remove(ec); |
| 507 | } |
| 508 | |
Javier Martinez Canillas | afbf8ec | 2015-06-22 08:27:20 +0200 | [diff] [blame] | 509 | static const struct platform_device_id cros_ec_id[] = { |
Thierry Escande | ea01a31 | 2017-11-20 17:15:25 +0100 | [diff] [blame] | 510 | { DRV_NAME, 0 }, |
Wei-Ning Huang | abeed71 | 2018-04-18 12:24:03 +0200 | [diff] [blame] | 511 | { /* sentinel */ } |
Javier Martinez Canillas | afbf8ec | 2015-06-22 08:27:20 +0200 | [diff] [blame] | 512 | }; |
| 513 | MODULE_DEVICE_TABLE(platform, cros_ec_id); |
| 514 | |
Arnd Bergmann | 5d6a312 | 2017-06-27 17:36:36 +0200 | [diff] [blame] | 515 | static __maybe_unused int ec_device_suspend(struct device *dev) |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 516 | { |
| 517 | struct cros_ec_dev *ec = dev_get_drvdata(dev); |
| 518 | |
Douglas Anderson | 44d99d7 | 2018-04-18 12:24:00 +0200 | [diff] [blame] | 519 | cros_ec_debugfs_suspend(ec); |
| 520 | |
Jeffery Yu | 995c0ec | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 521 | lb_suspend(ec); |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 522 | |
| 523 | return 0; |
| 524 | } |
| 525 | |
Arnd Bergmann | 5d6a312 | 2017-06-27 17:36:36 +0200 | [diff] [blame] | 526 | static __maybe_unused int ec_device_resume(struct device *dev) |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 527 | { |
| 528 | struct cros_ec_dev *ec = dev_get_drvdata(dev); |
| 529 | |
Douglas Anderson | 44d99d7 | 2018-04-18 12:24:00 +0200 | [diff] [blame] | 530 | cros_ec_debugfs_resume(ec); |
| 531 | |
Jeffery Yu | 995c0ec | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 532 | lb_resume(ec); |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 533 | |
| 534 | return 0; |
| 535 | } |
| 536 | |
| 537 | static const struct dev_pm_ops cros_ec_dev_pm_ops = { |
| 538 | #ifdef CONFIG_PM_SLEEP |
| 539 | .suspend = ec_device_suspend, |
| 540 | .resume = ec_device_resume, |
| 541 | #endif |
| 542 | }; |
| 543 | |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 544 | static struct platform_driver cros_ec_dev_driver = { |
| 545 | .driver = { |
Thierry Escande | ea01a31 | 2017-11-20 17:15:25 +0100 | [diff] [blame] | 546 | .name = DRV_NAME, |
Eric Caruso | 405c843 | 2017-05-16 17:46:48 +0200 | [diff] [blame] | 547 | .pm = &cros_ec_dev_pm_ops, |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 548 | }, |
| 549 | .probe = ec_device_probe, |
| 550 | .remove = ec_device_remove, |
Daniel Hung-yu Wu | e15b7f4 | 2018-04-18 12:24:02 +0200 | [diff] [blame] | 551 | .shutdown = ec_device_shutdown, |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 552 | }; |
| 553 | |
| 554 | static int __init cros_ec_dev_init(void) |
| 555 | { |
| 556 | int ret; |
| 557 | dev_t dev = 0; |
| 558 | |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 559 | ret = class_register(&cros_class); |
| 560 | if (ret) { |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 561 | pr_err(CROS_EC_DEV_NAME ": failed to register device class\n"); |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 562 | return ret; |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /* Get a range of minor numbers (starting with 0) to work with */ |
| 566 | ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME); |
| 567 | if (ret < 0) { |
| 568 | pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n"); |
| 569 | goto failed_chrdevreg; |
| 570 | } |
| 571 | ec_major = MAJOR(dev); |
| 572 | |
| 573 | /* Register the driver */ |
| 574 | ret = platform_driver_register(&cros_ec_dev_driver); |
| 575 | if (ret < 0) { |
| 576 | pr_warn(CROS_EC_DEV_NAME ": can't register driver: %d\n", ret); |
| 577 | goto failed_devreg; |
| 578 | } |
| 579 | return 0; |
| 580 | |
| 581 | failed_devreg: |
| 582 | unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV); |
| 583 | failed_chrdevreg: |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 584 | class_unregister(&cros_class); |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 585 | return ret; |
| 586 | } |
| 587 | |
| 588 | static void __exit cros_ec_dev_exit(void) |
| 589 | { |
| 590 | platform_driver_unregister(&cros_ec_dev_driver); |
| 591 | unregister_chrdev(ec_major, CROS_EC_DEV_NAME); |
Gwendal Grignou | 57b33ff | 2015-06-09 13:04:47 +0200 | [diff] [blame] | 592 | class_unregister(&cros_class); |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | module_init(cros_ec_dev_init); |
| 596 | module_exit(cros_ec_dev_exit); |
| 597 | |
Thierry Escande | ea01a31 | 2017-11-20 17:15:25 +0100 | [diff] [blame] | 598 | MODULE_ALIAS("platform:" DRV_NAME); |
Bill Richardson | e7c256f | 2015-02-02 12:26:25 +0100 | [diff] [blame] | 599 | MODULE_AUTHOR("Bill Richardson <wfrichar@chromium.org>"); |
| 600 | MODULE_DESCRIPTION("Userspace interface to the Chrome OS Embedded Controller"); |
| 601 | MODULE_VERSION("1.0"); |
| 602 | MODULE_LICENSE("GPL"); |