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