| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * cros_ec_dev - expose the Chrome OS Embedded Controller to user-space |
|---|
| 3 | 4 | * |
|---|
| 4 | 5 | * 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 | 6 | */ |
|---|
| 19 | 7 | |
|---|
| 20 | | -#include <linux/fs.h> |
|---|
| 8 | +#include <linux/kconfig.h> |
|---|
| 21 | 9 | #include <linux/mfd/core.h> |
|---|
| 22 | 10 | #include <linux/module.h> |
|---|
| 23 | 11 | #include <linux/mod_devicetable.h> |
|---|
| 12 | +#include <linux/of_platform.h> |
|---|
| 24 | 13 | #include <linux/platform_device.h> |
|---|
| 25 | | -#include <linux/pm.h> |
|---|
| 14 | +#include <linux/platform_data/cros_ec_chardev.h> |
|---|
| 15 | +#include <linux/platform_data/cros_ec_commands.h> |
|---|
| 16 | +#include <linux/platform_data/cros_ec_proto.h> |
|---|
| 26 | 17 | #include <linux/slab.h> |
|---|
| 27 | | -#include <linux/uaccess.h> |
|---|
| 28 | | - |
|---|
| 29 | | -#include "cros_ec_dev.h" |
|---|
| 30 | 18 | |
|---|
| 31 | 19 | #define DRV_NAME "cros-ec-dev" |
|---|
| 32 | | - |
|---|
| 33 | | -/* Device variables */ |
|---|
| 34 | | -#define CROS_MAX_DEV 128 |
|---|
| 35 | | -static int ec_major; |
|---|
| 36 | | - |
|---|
| 37 | | -static const struct attribute_group *cros_ec_groups[] = { |
|---|
| 38 | | - &cros_ec_attr_group, |
|---|
| 39 | | - &cros_ec_lightbar_attr_group, |
|---|
| 40 | | - &cros_ec_vbc_attr_group, |
|---|
| 41 | | - NULL, |
|---|
| 42 | | -}; |
|---|
| 43 | 20 | |
|---|
| 44 | 21 | static struct class cros_class = { |
|---|
| 45 | 22 | .owner = THIS_MODULE, |
|---|
| 46 | 23 | .name = "chromeos", |
|---|
| 47 | | - .dev_groups = cros_ec_groups, |
|---|
| 48 | 24 | }; |
|---|
| 49 | 25 | |
|---|
| 50 | | -/* Basic communication */ |
|---|
| 51 | | -static int ec_get_version(struct cros_ec_dev *ec, char *str, int maxlen) |
|---|
| 52 | | -{ |
|---|
| 53 | | - struct ec_response_get_version *resp; |
|---|
| 54 | | - static const char * const current_image_name[] = { |
|---|
| 55 | | - "unknown", "read-only", "read-write", "invalid", |
|---|
| 56 | | - }; |
|---|
| 57 | | - struct cros_ec_command *msg; |
|---|
| 58 | | - int ret; |
|---|
| 26 | +/** |
|---|
| 27 | + * struct cros_feature_to_name - CrOS feature id to name/short description. |
|---|
| 28 | + * @id: The feature identifier. |
|---|
| 29 | + * @name: Device name associated with the feature id. |
|---|
| 30 | + * @desc: Short name that will be displayed. |
|---|
| 31 | + */ |
|---|
| 32 | +struct cros_feature_to_name { |
|---|
| 33 | + unsigned int id; |
|---|
| 34 | + const char *name; |
|---|
| 35 | + const char *desc; |
|---|
| 36 | +}; |
|---|
| 59 | 37 | |
|---|
| 60 | | - msg = kmalloc(sizeof(*msg) + sizeof(*resp), GFP_KERNEL); |
|---|
| 61 | | - if (!msg) |
|---|
| 62 | | - return -ENOMEM; |
|---|
| 38 | +/** |
|---|
| 39 | + * struct cros_feature_to_cells - CrOS feature id to mfd cells association. |
|---|
| 40 | + * @id: The feature identifier. |
|---|
| 41 | + * @mfd_cells: Pointer to the array of mfd cells that needs to be added. |
|---|
| 42 | + * @num_cells: Number of mfd cells into the array. |
|---|
| 43 | + */ |
|---|
| 44 | +struct cros_feature_to_cells { |
|---|
| 45 | + unsigned int id; |
|---|
| 46 | + const struct mfd_cell *mfd_cells; |
|---|
| 47 | + unsigned int num_cells; |
|---|
| 48 | +}; |
|---|
| 63 | 49 | |
|---|
| 64 | | - msg->version = 0; |
|---|
| 65 | | - msg->command = EC_CMD_GET_VERSION + ec->cmd_offset; |
|---|
| 66 | | - msg->insize = sizeof(*resp); |
|---|
| 67 | | - msg->outsize = 0; |
|---|
| 50 | +static const struct cros_feature_to_name cros_mcu_devices[] = { |
|---|
| 51 | + { |
|---|
| 52 | + .id = EC_FEATURE_FINGERPRINT, |
|---|
| 53 | + .name = CROS_EC_DEV_FP_NAME, |
|---|
| 54 | + .desc = "Fingerprint", |
|---|
| 55 | + }, |
|---|
| 56 | + { |
|---|
| 57 | + .id = EC_FEATURE_ISH, |
|---|
| 58 | + .name = CROS_EC_DEV_ISH_NAME, |
|---|
| 59 | + .desc = "Integrated Sensor Hub", |
|---|
| 60 | + }, |
|---|
| 61 | + { |
|---|
| 62 | + .id = EC_FEATURE_SCP, |
|---|
| 63 | + .name = CROS_EC_DEV_SCP_NAME, |
|---|
| 64 | + .desc = "System Control Processor", |
|---|
| 65 | + }, |
|---|
| 66 | + { |
|---|
| 67 | + .id = EC_FEATURE_TOUCHPAD, |
|---|
| 68 | + .name = CROS_EC_DEV_TP_NAME, |
|---|
| 69 | + .desc = "Touchpad", |
|---|
| 70 | + }, |
|---|
| 71 | +}; |
|---|
| 68 | 72 | |
|---|
| 69 | | - ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
|---|
| 70 | | - if (ret < 0) |
|---|
| 71 | | - goto exit; |
|---|
| 73 | +static const struct mfd_cell cros_ec_cec_cells[] = { |
|---|
| 74 | + { .name = "cros-ec-cec", }, |
|---|
| 75 | +}; |
|---|
| 72 | 76 | |
|---|
| 73 | | - if (msg->result != EC_RES_SUCCESS) { |
|---|
| 74 | | - snprintf(str, maxlen, |
|---|
| 75 | | - "%s\nUnknown EC version: EC returned %d\n", |
|---|
| 76 | | - CROS_EC_DEV_VERSION, msg->result); |
|---|
| 77 | | - ret = -EINVAL; |
|---|
| 78 | | - goto exit; |
|---|
| 79 | | - } |
|---|
| 77 | +static const struct mfd_cell cros_ec_rtc_cells[] = { |
|---|
| 78 | + { .name = "cros-ec-rtc", }, |
|---|
| 79 | +}; |
|---|
| 80 | 80 | |
|---|
| 81 | | - resp = (struct ec_response_get_version *)msg->data; |
|---|
| 82 | | - if (resp->current_image >= ARRAY_SIZE(current_image_name)) |
|---|
| 83 | | - resp->current_image = 3; /* invalid */ |
|---|
| 81 | +static const struct mfd_cell cros_ec_sensorhub_cells[] = { |
|---|
| 82 | + { .name = "cros-ec-sensorhub", }, |
|---|
| 83 | +}; |
|---|
| 84 | 84 | |
|---|
| 85 | | - snprintf(str, maxlen, "%s\n%s\n%s\n%s\n", CROS_EC_DEV_VERSION, |
|---|
| 86 | | - resp->version_string_ro, resp->version_string_rw, |
|---|
| 87 | | - current_image_name[resp->current_image]); |
|---|
| 85 | +static const struct mfd_cell cros_usbpd_charger_cells[] = { |
|---|
| 86 | + { .name = "cros-usbpd-charger", }, |
|---|
| 87 | + { .name = "cros-usbpd-logger", }, |
|---|
| 88 | +}; |
|---|
| 88 | 89 | |
|---|
| 89 | | - ret = 0; |
|---|
| 90 | | -exit: |
|---|
| 91 | | - kfree(msg); |
|---|
| 92 | | - return ret; |
|---|
| 93 | | -} |
|---|
| 90 | +static const struct mfd_cell cros_usbpd_notify_cells[] = { |
|---|
| 91 | + { .name = "cros-usbpd-notify", }, |
|---|
| 92 | +}; |
|---|
| 94 | 93 | |
|---|
| 95 | | -static int cros_ec_check_features(struct cros_ec_dev *ec, int feature) |
|---|
| 96 | | -{ |
|---|
| 97 | | - struct cros_ec_command *msg; |
|---|
| 98 | | - int ret; |
|---|
| 94 | +static const struct cros_feature_to_cells cros_subdevices[] = { |
|---|
| 95 | + { |
|---|
| 96 | + .id = EC_FEATURE_CEC, |
|---|
| 97 | + .mfd_cells = cros_ec_cec_cells, |
|---|
| 98 | + .num_cells = ARRAY_SIZE(cros_ec_cec_cells), |
|---|
| 99 | + }, |
|---|
| 100 | + { |
|---|
| 101 | + .id = EC_FEATURE_RTC, |
|---|
| 102 | + .mfd_cells = cros_ec_rtc_cells, |
|---|
| 103 | + .num_cells = ARRAY_SIZE(cros_ec_rtc_cells), |
|---|
| 104 | + }, |
|---|
| 105 | + { |
|---|
| 106 | + .id = EC_FEATURE_USB_PD, |
|---|
| 107 | + .mfd_cells = cros_usbpd_charger_cells, |
|---|
| 108 | + .num_cells = ARRAY_SIZE(cros_usbpd_charger_cells), |
|---|
| 109 | + }, |
|---|
| 110 | +}; |
|---|
| 99 | 111 | |
|---|
| 100 | | - if (ec->features[0] == -1U && ec->features[1] == -1U) { |
|---|
| 101 | | - /* features bitmap not read yet */ |
|---|
| 112 | +static const struct mfd_cell cros_ec_platform_cells[] = { |
|---|
| 113 | + { .name = "cros-ec-chardev", }, |
|---|
| 114 | + { .name = "cros-ec-debugfs", }, |
|---|
| 115 | + { .name = "cros-ec-lightbar", }, |
|---|
| 116 | + { .name = "cros-ec-sysfs", }, |
|---|
| 117 | +}; |
|---|
| 102 | 118 | |
|---|
| 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)); |
|---|
| 117 | | - } else { |
|---|
| 118 | | - memcpy(ec->features, msg->data, sizeof(ec->features)); |
|---|
| 119 | | - } |
|---|
| 120 | | - |
|---|
| 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 | | - |
|---|
| 130 | | -/* Device file ops */ |
|---|
| 131 | | -static int ec_device_open(struct inode *inode, struct file *filp) |
|---|
| 132 | | -{ |
|---|
| 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); |
|---|
| 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 | | -{ |
|---|
| 148 | | - struct cros_ec_dev *ec = filp->private_data; |
|---|
| 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 */ |
|---|
| 171 | | -static long ec_device_ioctl_xcmd(struct cros_ec_dev *ec, void __user *arg) |
|---|
| 172 | | -{ |
|---|
| 173 | | - long ret; |
|---|
| 174 | | - struct cros_ec_command u_cmd; |
|---|
| 175 | | - struct cros_ec_command *s_cmd; |
|---|
| 176 | | - |
|---|
| 177 | | - if (copy_from_user(&u_cmd, arg, sizeof(u_cmd))) |
|---|
| 178 | | - return -EFAULT; |
|---|
| 179 | | - |
|---|
| 180 | | - if ((u_cmd.outsize > EC_MAX_MSG_BYTES) || |
|---|
| 181 | | - (u_cmd.insize > EC_MAX_MSG_BYTES)) |
|---|
| 182 | | - return -EINVAL; |
|---|
| 183 | | - |
|---|
| 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 | | - |
|---|
| 194 | | - if (u_cmd.outsize != s_cmd->outsize || |
|---|
| 195 | | - u_cmd.insize != s_cmd->insize) { |
|---|
| 196 | | - ret = -EINVAL; |
|---|
| 197 | | - goto exit; |
|---|
| 198 | | - } |
|---|
| 199 | | - |
|---|
| 200 | | - s_cmd->command += ec->cmd_offset; |
|---|
| 201 | | - ret = cros_ec_cmd_xfer(ec->ec_dev, s_cmd); |
|---|
| 202 | | - /* Only copy data to userland if data was received. */ |
|---|
| 203 | | - if (ret < 0) |
|---|
| 204 | | - goto exit; |
|---|
| 205 | | - |
|---|
| 206 | | - if (copy_to_user(arg, s_cmd, sizeof(*s_cmd) + s_cmd->insize)) |
|---|
| 207 | | - ret = -EFAULT; |
|---|
| 208 | | -exit: |
|---|
| 209 | | - kfree(s_cmd); |
|---|
| 210 | | - return ret; |
|---|
| 211 | | -} |
|---|
| 212 | | - |
|---|
| 213 | | -static long ec_device_ioctl_readmem(struct cros_ec_dev *ec, void __user *arg) |
|---|
| 214 | | -{ |
|---|
| 215 | | - struct cros_ec_device *ec_dev = ec->ec_dev; |
|---|
| 216 | | - struct cros_ec_readmem s_mem = { }; |
|---|
| 217 | | - long num; |
|---|
| 218 | | - |
|---|
| 219 | | - /* Not every platform supports direct reads */ |
|---|
| 220 | | - if (!ec_dev->cmd_readmem) |
|---|
| 221 | | - return -ENOTTY; |
|---|
| 222 | | - |
|---|
| 223 | | - if (copy_from_user(&s_mem, arg, sizeof(s_mem))) |
|---|
| 224 | | - return -EFAULT; |
|---|
| 225 | | - |
|---|
| 226 | | - num = ec_dev->cmd_readmem(ec_dev, s_mem.offset, s_mem.bytes, |
|---|
| 227 | | - s_mem.buffer); |
|---|
| 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 | | -{ |
|---|
| 240 | | - struct cros_ec_dev *ec = filp->private_data; |
|---|
| 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, |
|---|
| 261 | | -#ifdef CONFIG_COMPAT |
|---|
| 262 | | - .compat_ioctl = ec_device_ioctl, |
|---|
| 263 | | -#endif |
|---|
| 119 | +static const struct mfd_cell cros_ec_vbc_cells[] = { |
|---|
| 120 | + { .name = "cros-ec-vbc", } |
|---|
| 264 | 121 | }; |
|---|
| 265 | 122 | |
|---|
| 266 | 123 | static void cros_ec_class_release(struct device *dev) |
|---|
| .. | .. |
|---|
| 268 | 125 | kfree(to_cros_ec_dev(dev)); |
|---|
| 269 | 126 | } |
|---|
| 270 | 127 | |
|---|
| 271 | | -static void cros_ec_sensors_register(struct cros_ec_dev *ec) |
|---|
| 272 | | -{ |
|---|
| 273 | | - /* |
|---|
| 274 | | - * Issue a command to get the number of sensor reported. |
|---|
| 275 | | - * Build an array of sensors driver and register them all. |
|---|
| 276 | | - */ |
|---|
| 277 | | - int ret, i, id, sensor_num; |
|---|
| 278 | | - struct mfd_cell *sensor_cells; |
|---|
| 279 | | - struct cros_ec_sensor_platform *sensor_platforms; |
|---|
| 280 | | - int sensor_type[MOTIONSENSE_TYPE_MAX]; |
|---|
| 281 | | - struct ec_params_motion_sense *params; |
|---|
| 282 | | - struct ec_response_motion_sense *resp; |
|---|
| 283 | | - struct cros_ec_command *msg; |
|---|
| 284 | | - |
|---|
| 285 | | - msg = kzalloc(sizeof(struct cros_ec_command) + |
|---|
| 286 | | - max(sizeof(*params), sizeof(*resp)), GFP_KERNEL); |
|---|
| 287 | | - if (msg == NULL) |
|---|
| 288 | | - return; |
|---|
| 289 | | - |
|---|
| 290 | | - msg->version = 2; |
|---|
| 291 | | - msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset; |
|---|
| 292 | | - msg->outsize = sizeof(*params); |
|---|
| 293 | | - msg->insize = sizeof(*resp); |
|---|
| 294 | | - |
|---|
| 295 | | - params = (struct ec_params_motion_sense *)msg->data; |
|---|
| 296 | | - params->cmd = MOTIONSENSE_CMD_DUMP; |
|---|
| 297 | | - |
|---|
| 298 | | - ret = cros_ec_cmd_xfer(ec->ec_dev, msg); |
|---|
| 299 | | - if (ret < 0 || msg->result != EC_RES_SUCCESS) { |
|---|
| 300 | | - dev_warn(ec->dev, "cannot get EC sensor information: %d/%d\n", |
|---|
| 301 | | - ret, msg->result); |
|---|
| 302 | | - goto error; |
|---|
| 303 | | - } |
|---|
| 304 | | - |
|---|
| 305 | | - resp = (struct ec_response_motion_sense *)msg->data; |
|---|
| 306 | | - sensor_num = resp->dump.sensor_count; |
|---|
| 307 | | - /* Allocate 1 extra sensors in FIFO are needed */ |
|---|
| 308 | | - sensor_cells = kcalloc(sensor_num + 1, sizeof(struct mfd_cell), |
|---|
| 309 | | - GFP_KERNEL); |
|---|
| 310 | | - if (sensor_cells == NULL) |
|---|
| 311 | | - goto error; |
|---|
| 312 | | - |
|---|
| 313 | | - sensor_platforms = kcalloc(sensor_num + 1, |
|---|
| 314 | | - sizeof(struct cros_ec_sensor_platform), |
|---|
| 315 | | - 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; |
|---|
| 334 | | - case MOTIONSENSE_TYPE_BARO: |
|---|
| 335 | | - sensor_cells[id].name = "cros-ec-baro"; |
|---|
| 336 | | - break; |
|---|
| 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 | | - |
|---|
| 366 | | - if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2) |
|---|
| 367 | | - ec->has_kb_wake_angle = true; |
|---|
| 368 | | - |
|---|
| 369 | | - if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) { |
|---|
| 370 | | - sensor_cells[id].name = "cros-ec-ring"; |
|---|
| 371 | | - id++; |
|---|
| 372 | | - } |
|---|
| 373 | | - |
|---|
| 374 | | - ret = mfd_add_devices(ec->dev, 0, sensor_cells, id, |
|---|
| 375 | | - NULL, 0, NULL); |
|---|
| 376 | | - if (ret) |
|---|
| 377 | | - dev_err(ec->dev, "failed to add EC sensors\n"); |
|---|
| 378 | | - |
|---|
| 379 | | - kfree(sensor_platforms); |
|---|
| 380 | | -error_platforms: |
|---|
| 381 | | - kfree(sensor_cells); |
|---|
| 382 | | -error: |
|---|
| 383 | | - kfree(msg); |
|---|
| 384 | | -} |
|---|
| 385 | | - |
|---|
| 386 | | -static const struct mfd_cell cros_ec_cec_cells[] = { |
|---|
| 387 | | - { .name = "cros-ec-cec" } |
|---|
| 388 | | -}; |
|---|
| 389 | | - |
|---|
| 390 | | -static const struct mfd_cell cros_ec_rtc_cells[] = { |
|---|
| 391 | | - { .name = "cros-ec-rtc" } |
|---|
| 392 | | -}; |
|---|
| 393 | | - |
|---|
| 394 | | -static const struct mfd_cell cros_usbpd_charger_cells[] = { |
|---|
| 395 | | - { .name = "cros-usbpd-charger" } |
|---|
| 396 | | -}; |
|---|
| 397 | | - |
|---|
| 398 | 128 | static int ec_device_probe(struct platform_device *pdev) |
|---|
| 399 | 129 | { |
|---|
| 400 | 130 | int retval = -ENOMEM; |
|---|
| 131 | + struct device_node *node; |
|---|
| 401 | 132 | struct device *dev = &pdev->dev; |
|---|
| 402 | 133 | struct cros_ec_platform *ec_platform = dev_get_platdata(dev); |
|---|
| 403 | 134 | struct cros_ec_dev *ec = kzalloc(sizeof(*ec), GFP_KERNEL); |
|---|
| 135 | + int i; |
|---|
| 404 | 136 | |
|---|
| 405 | 137 | if (!ec) |
|---|
| 406 | 138 | return retval; |
|---|
| .. | .. |
|---|
| 412 | 144 | ec->features[0] = -1U; /* Not cached yet */ |
|---|
| 413 | 145 | ec->features[1] = -1U; /* Not cached yet */ |
|---|
| 414 | 146 | device_initialize(&ec->class_dev); |
|---|
| 415 | | - cdev_init(&ec->cdev, &fops); |
|---|
| 147 | + |
|---|
| 148 | + for (i = 0; i < ARRAY_SIZE(cros_mcu_devices); i++) { |
|---|
| 149 | + /* |
|---|
| 150 | + * Check whether this is actually a dedicated MCU rather |
|---|
| 151 | + * than an standard EC. |
|---|
| 152 | + */ |
|---|
| 153 | + if (cros_ec_check_features(ec, cros_mcu_devices[i].id)) { |
|---|
| 154 | + dev_info(dev, "CrOS %s MCU detected\n", |
|---|
| 155 | + cros_mcu_devices[i].desc); |
|---|
| 156 | + /* |
|---|
| 157 | + * Help userspace differentiating ECs from other MCU, |
|---|
| 158 | + * regardless of the probing order. |
|---|
| 159 | + */ |
|---|
| 160 | + ec_platform->ec_name = cros_mcu_devices[i].name; |
|---|
| 161 | + break; |
|---|
| 162 | + } |
|---|
| 163 | + } |
|---|
| 416 | 164 | |
|---|
| 417 | 165 | /* |
|---|
| 418 | 166 | * Add the class device |
|---|
| 419 | | - * Link to the character device for creating the /dev entry |
|---|
| 420 | | - * in devtmpfs. |
|---|
| 421 | 167 | */ |
|---|
| 422 | | - ec->class_dev.devt = MKDEV(ec_major, pdev->id); |
|---|
| 423 | 168 | ec->class_dev.class = &cros_class; |
|---|
| 424 | 169 | ec->class_dev.parent = dev; |
|---|
| 425 | 170 | ec->class_dev.release = cros_ec_class_release; |
|---|
| .. | .. |
|---|
| 430 | 175 | goto failed; |
|---|
| 431 | 176 | } |
|---|
| 432 | 177 | |
|---|
| 433 | | - /* check whether this EC is a sensor hub. */ |
|---|
| 434 | | - if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE)) |
|---|
| 435 | | - cros_ec_sensors_register(ec); |
|---|
| 436 | | - |
|---|
| 437 | | - /* Check whether this EC instance has CEC host command support */ |
|---|
| 438 | | - if (cros_ec_check_features(ec, EC_FEATURE_CEC)) { |
|---|
| 439 | | - retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, |
|---|
| 440 | | - cros_ec_cec_cells, |
|---|
| 441 | | - ARRAY_SIZE(cros_ec_cec_cells), |
|---|
| 442 | | - NULL, 0, NULL); |
|---|
| 443 | | - if (retval) |
|---|
| 444 | | - dev_err(ec->dev, |
|---|
| 445 | | - "failed to add cros-ec-cec device: %d\n", |
|---|
| 446 | | - retval); |
|---|
| 447 | | - } |
|---|
| 448 | | - |
|---|
| 449 | | - /* Check whether this EC instance has RTC host command support */ |
|---|
| 450 | | - if (cros_ec_check_features(ec, EC_FEATURE_RTC)) { |
|---|
| 451 | | - retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, |
|---|
| 452 | | - cros_ec_rtc_cells, |
|---|
| 453 | | - ARRAY_SIZE(cros_ec_rtc_cells), |
|---|
| 454 | | - NULL, 0, NULL); |
|---|
| 455 | | - if (retval) |
|---|
| 456 | | - dev_err(ec->dev, |
|---|
| 457 | | - "failed to add cros-ec-rtc device: %d\n", |
|---|
| 458 | | - retval); |
|---|
| 459 | | - } |
|---|
| 460 | | - |
|---|
| 461 | | - /* Check whether this EC instance has the PD charge manager */ |
|---|
| 462 | | - if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) { |
|---|
| 463 | | - retval = mfd_add_devices(ec->dev, PLATFORM_DEVID_AUTO, |
|---|
| 464 | | - cros_usbpd_charger_cells, |
|---|
| 465 | | - ARRAY_SIZE(cros_usbpd_charger_cells), |
|---|
| 466 | | - NULL, 0, NULL); |
|---|
| 467 | | - if (retval) |
|---|
| 468 | | - dev_err(ec->dev, |
|---|
| 469 | | - "failed to add cros-usbpd-charger device: %d\n", |
|---|
| 470 | | - retval); |
|---|
| 471 | | - } |
|---|
| 472 | | - |
|---|
| 473 | | - /* Take control of the lightbar from the EC. */ |
|---|
| 474 | | - lb_manual_suspend_ctrl(ec, 1); |
|---|
| 475 | | - |
|---|
| 476 | | - /* We can now add the sysfs class, we know which parameter to show */ |
|---|
| 477 | | - retval = cdev_device_add(&ec->cdev, &ec->class_dev); |
|---|
| 478 | | - if (retval) { |
|---|
| 479 | | - dev_err(dev, "cdev_device_add failed => %d\n", retval); |
|---|
| 178 | + retval = device_add(&ec->class_dev); |
|---|
| 179 | + if (retval) |
|---|
| 480 | 180 | goto failed; |
|---|
| 181 | + |
|---|
| 182 | + /* check whether this EC is a sensor hub. */ |
|---|
| 183 | + if (cros_ec_get_sensor_count(ec) > 0) { |
|---|
| 184 | + retval = mfd_add_hotplug_devices(ec->dev, |
|---|
| 185 | + cros_ec_sensorhub_cells, |
|---|
| 186 | + ARRAY_SIZE(cros_ec_sensorhub_cells)); |
|---|
| 187 | + if (retval) |
|---|
| 188 | + dev_err(ec->dev, "failed to add %s subdevice: %d\n", |
|---|
| 189 | + cros_ec_sensorhub_cells->name, retval); |
|---|
| 481 | 190 | } |
|---|
| 482 | 191 | |
|---|
| 483 | | - if (cros_ec_debugfs_init(ec)) |
|---|
| 484 | | - dev_warn(dev, "failed to create debugfs directory\n"); |
|---|
| 192 | + /* |
|---|
| 193 | + * The following subdevices can be detected by sending the |
|---|
| 194 | + * EC_FEATURE_GET_CMD Embedded Controller device. |
|---|
| 195 | + */ |
|---|
| 196 | + for (i = 0; i < ARRAY_SIZE(cros_subdevices); i++) { |
|---|
| 197 | + if (cros_ec_check_features(ec, cros_subdevices[i].id)) { |
|---|
| 198 | + retval = mfd_add_hotplug_devices(ec->dev, |
|---|
| 199 | + cros_subdevices[i].mfd_cells, |
|---|
| 200 | + cros_subdevices[i].num_cells); |
|---|
| 201 | + if (retval) |
|---|
| 202 | + dev_err(ec->dev, |
|---|
| 203 | + "failed to add %s subdevice: %d\n", |
|---|
| 204 | + cros_subdevices[i].mfd_cells->name, |
|---|
| 205 | + retval); |
|---|
| 206 | + } |
|---|
| 207 | + } |
|---|
| 208 | + |
|---|
| 209 | + /* |
|---|
| 210 | + * The PD notifier driver cell is separate since it only needs to be |
|---|
| 211 | + * explicitly added on platforms that don't have the PD notifier ACPI |
|---|
| 212 | + * device entry defined. |
|---|
| 213 | + */ |
|---|
| 214 | + if (IS_ENABLED(CONFIG_OF) && ec->ec_dev->dev->of_node) { |
|---|
| 215 | + if (cros_ec_check_features(ec, EC_FEATURE_USB_PD)) { |
|---|
| 216 | + retval = mfd_add_hotplug_devices(ec->dev, |
|---|
| 217 | + cros_usbpd_notify_cells, |
|---|
| 218 | + ARRAY_SIZE(cros_usbpd_notify_cells)); |
|---|
| 219 | + if (retval) |
|---|
| 220 | + dev_err(ec->dev, |
|---|
| 221 | + "failed to add PD notify devices: %d\n", |
|---|
| 222 | + retval); |
|---|
| 223 | + } |
|---|
| 224 | + } |
|---|
| 225 | + |
|---|
| 226 | + /* |
|---|
| 227 | + * The following subdevices cannot be detected by sending the |
|---|
| 228 | + * EC_FEATURE_GET_CMD to the Embedded Controller device. |
|---|
| 229 | + */ |
|---|
| 230 | + retval = mfd_add_hotplug_devices(ec->dev, cros_ec_platform_cells, |
|---|
| 231 | + ARRAY_SIZE(cros_ec_platform_cells)); |
|---|
| 232 | + if (retval) |
|---|
| 233 | + dev_warn(ec->dev, |
|---|
| 234 | + "failed to add cros-ec platform devices: %d\n", |
|---|
| 235 | + retval); |
|---|
| 236 | + |
|---|
| 237 | + /* Check whether this EC instance has a VBC NVRAM */ |
|---|
| 238 | + node = ec->ec_dev->dev->of_node; |
|---|
| 239 | + if (of_property_read_bool(node, "google,has-vbc-nvram")) { |
|---|
| 240 | + retval = mfd_add_hotplug_devices(ec->dev, cros_ec_vbc_cells, |
|---|
| 241 | + ARRAY_SIZE(cros_ec_vbc_cells)); |
|---|
| 242 | + if (retval) |
|---|
| 243 | + dev_warn(ec->dev, "failed to add VBC devices: %d\n", |
|---|
| 244 | + retval); |
|---|
| 245 | + } |
|---|
| 485 | 246 | |
|---|
| 486 | 247 | return 0; |
|---|
| 487 | 248 | |
|---|
| .. | .. |
|---|
| 494 | 255 | { |
|---|
| 495 | 256 | struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev); |
|---|
| 496 | 257 | |
|---|
| 497 | | - /* Let the EC take over the lightbar again. */ |
|---|
| 498 | | - lb_manual_suspend_ctrl(ec, 0); |
|---|
| 499 | | - |
|---|
| 500 | | - cros_ec_debugfs_remove(ec); |
|---|
| 501 | | - |
|---|
| 502 | 258 | mfd_remove_devices(ec->dev); |
|---|
| 503 | | - cdev_del(&ec->cdev); |
|---|
| 504 | 259 | device_unregister(&ec->class_dev); |
|---|
| 505 | 260 | return 0; |
|---|
| 506 | | -} |
|---|
| 507 | | - |
|---|
| 508 | | -static void ec_device_shutdown(struct platform_device *pdev) |
|---|
| 509 | | -{ |
|---|
| 510 | | - struct cros_ec_dev *ec = dev_get_drvdata(&pdev->dev); |
|---|
| 511 | | - |
|---|
| 512 | | - /* Be sure to clear up debugfs delayed works */ |
|---|
| 513 | | - cros_ec_debugfs_remove(ec); |
|---|
| 514 | 261 | } |
|---|
| 515 | 262 | |
|---|
| 516 | 263 | static const struct platform_device_id cros_ec_id[] = { |
|---|
| .. | .. |
|---|
| 519 | 266 | }; |
|---|
| 520 | 267 | MODULE_DEVICE_TABLE(platform, cros_ec_id); |
|---|
| 521 | 268 | |
|---|
| 522 | | -static __maybe_unused int ec_device_suspend(struct device *dev) |
|---|
| 523 | | -{ |
|---|
| 524 | | - struct cros_ec_dev *ec = dev_get_drvdata(dev); |
|---|
| 525 | | - |
|---|
| 526 | | - cros_ec_debugfs_suspend(ec); |
|---|
| 527 | | - |
|---|
| 528 | | - lb_suspend(ec); |
|---|
| 529 | | - |
|---|
| 530 | | - return 0; |
|---|
| 531 | | -} |
|---|
| 532 | | - |
|---|
| 533 | | -static __maybe_unused int ec_device_resume(struct device *dev) |
|---|
| 534 | | -{ |
|---|
| 535 | | - struct cros_ec_dev *ec = dev_get_drvdata(dev); |
|---|
| 536 | | - |
|---|
| 537 | | - cros_ec_debugfs_resume(ec); |
|---|
| 538 | | - |
|---|
| 539 | | - lb_resume(ec); |
|---|
| 540 | | - |
|---|
| 541 | | - return 0; |
|---|
| 542 | | -} |
|---|
| 543 | | - |
|---|
| 544 | | -static const struct dev_pm_ops cros_ec_dev_pm_ops = { |
|---|
| 545 | | -#ifdef CONFIG_PM_SLEEP |
|---|
| 546 | | - .suspend = ec_device_suspend, |
|---|
| 547 | | - .resume = ec_device_resume, |
|---|
| 548 | | -#endif |
|---|
| 549 | | -}; |
|---|
| 550 | | - |
|---|
| 551 | 269 | static struct platform_driver cros_ec_dev_driver = { |
|---|
| 552 | 270 | .driver = { |
|---|
| 553 | 271 | .name = DRV_NAME, |
|---|
| 554 | | - .pm = &cros_ec_dev_pm_ops, |
|---|
| 555 | 272 | }, |
|---|
| 273 | + .id_table = cros_ec_id, |
|---|
| 556 | 274 | .probe = ec_device_probe, |
|---|
| 557 | 275 | .remove = ec_device_remove, |
|---|
| 558 | | - .shutdown = ec_device_shutdown, |
|---|
| 559 | 276 | }; |
|---|
| 560 | 277 | |
|---|
| 561 | 278 | static int __init cros_ec_dev_init(void) |
|---|
| 562 | 279 | { |
|---|
| 563 | 280 | int ret; |
|---|
| 564 | | - dev_t dev = 0; |
|---|
| 565 | 281 | |
|---|
| 566 | 282 | ret = class_register(&cros_class); |
|---|
| 567 | 283 | if (ret) { |
|---|
| 568 | 284 | pr_err(CROS_EC_DEV_NAME ": failed to register device class\n"); |
|---|
| 569 | 285 | return ret; |
|---|
| 570 | 286 | } |
|---|
| 571 | | - |
|---|
| 572 | | - /* Get a range of minor numbers (starting with 0) to work with */ |
|---|
| 573 | | - ret = alloc_chrdev_region(&dev, 0, CROS_MAX_DEV, CROS_EC_DEV_NAME); |
|---|
| 574 | | - if (ret < 0) { |
|---|
| 575 | | - pr_err(CROS_EC_DEV_NAME ": alloc_chrdev_region() failed\n"); |
|---|
| 576 | | - goto failed_chrdevreg; |
|---|
| 577 | | - } |
|---|
| 578 | | - ec_major = MAJOR(dev); |
|---|
| 579 | 287 | |
|---|
| 580 | 288 | /* Register the driver */ |
|---|
| 581 | 289 | ret = platform_driver_register(&cros_ec_dev_driver); |
|---|
| .. | .. |
|---|
| 586 | 294 | return 0; |
|---|
| 587 | 295 | |
|---|
| 588 | 296 | failed_devreg: |
|---|
| 589 | | - unregister_chrdev_region(MKDEV(ec_major, 0), CROS_MAX_DEV); |
|---|
| 590 | | -failed_chrdevreg: |
|---|
| 591 | 297 | class_unregister(&cros_class); |
|---|
| 592 | 298 | return ret; |
|---|
| 593 | 299 | } |
|---|
| .. | .. |
|---|
| 595 | 301 | static void __exit cros_ec_dev_exit(void) |
|---|
| 596 | 302 | { |
|---|
| 597 | 303 | platform_driver_unregister(&cros_ec_dev_driver); |
|---|
| 598 | | - unregister_chrdev(ec_major, CROS_EC_DEV_NAME); |
|---|
| 599 | 304 | class_unregister(&cros_class); |
|---|
| 600 | 305 | } |
|---|
| 601 | 306 | |
|---|