Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * Sensor HUB driver that discovers sensors behind a ChromeOS Embedded |
| 4 | * Controller. |
| 5 | * |
| 6 | * Copyright 2019 Google LLC |
| 7 | */ |
| 8 | |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/device.h> |
| 11 | #include <linux/module.h> |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 12 | #include <linux/platform_data/cros_ec_commands.h> |
| 13 | #include <linux/platform_data/cros_ec_proto.h> |
| 14 | #include <linux/platform_data/cros_ec_sensorhub.h> |
| 15 | #include <linux/platform_device.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/types.h> |
| 18 | |
| 19 | #define DRV_NAME "cros-ec-sensorhub" |
| 20 | |
| 21 | static void cros_ec_sensorhub_free_sensor(void *arg) |
| 22 | { |
| 23 | struct platform_device *pdev = arg; |
| 24 | |
| 25 | platform_device_unregister(pdev); |
| 26 | } |
| 27 | |
| 28 | static int cros_ec_sensorhub_allocate_sensor(struct device *parent, |
| 29 | char *sensor_name, |
| 30 | int sensor_num) |
| 31 | { |
| 32 | struct cros_ec_sensor_platform sensor_platforms = { |
| 33 | .sensor_num = sensor_num, |
| 34 | }; |
| 35 | struct platform_device *pdev; |
| 36 | |
| 37 | pdev = platform_device_register_data(parent, sensor_name, |
| 38 | PLATFORM_DEVID_AUTO, |
| 39 | &sensor_platforms, |
| 40 | sizeof(sensor_platforms)); |
| 41 | if (IS_ERR(pdev)) |
| 42 | return PTR_ERR(pdev); |
| 43 | |
| 44 | return devm_add_action_or_reset(parent, |
| 45 | cros_ec_sensorhub_free_sensor, |
| 46 | pdev); |
| 47 | } |
| 48 | |
| 49 | static int cros_ec_sensorhub_register(struct device *dev, |
| 50 | struct cros_ec_sensorhub *sensorhub) |
| 51 | { |
| 52 | int sensor_type[MOTIONSENSE_TYPE_MAX] = { 0 }; |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 53 | struct cros_ec_command *msg = sensorhub->msg; |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 54 | struct cros_ec_dev *ec = sensorhub->ec; |
Gwendal Grignou | b31d1d2 | 2020-04-27 15:59:02 -0700 | [diff] [blame] | 55 | int ret, i; |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 56 | char *name; |
| 57 | |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 58 | |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 59 | msg->version = 1; |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 60 | msg->insize = sizeof(struct ec_response_motion_sense); |
| 61 | msg->outsize = sizeof(struct ec_params_motion_sense); |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 62 | |
Gwendal Grignou | b31d1d2 | 2020-04-27 15:59:02 -0700 | [diff] [blame] | 63 | for (i = 0; i < sensorhub->sensor_num; i++) { |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 64 | sensorhub->params->cmd = MOTIONSENSE_CMD_INFO; |
| 65 | sensorhub->params->info.sensor_num = i; |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 66 | |
| 67 | ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg); |
| 68 | if (ret < 0) { |
| 69 | dev_warn(dev, "no info for EC sensor %d : %d/%d\n", |
| 70 | i, ret, msg->result); |
| 71 | continue; |
| 72 | } |
| 73 | |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 74 | switch (sensorhub->resp->info.type) { |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 75 | case MOTIONSENSE_TYPE_ACCEL: |
| 76 | name = "cros-ec-accel"; |
| 77 | break; |
| 78 | case MOTIONSENSE_TYPE_BARO: |
| 79 | name = "cros-ec-baro"; |
| 80 | break; |
| 81 | case MOTIONSENSE_TYPE_GYRO: |
| 82 | name = "cros-ec-gyro"; |
| 83 | break; |
| 84 | case MOTIONSENSE_TYPE_MAG: |
| 85 | name = "cros-ec-mag"; |
| 86 | break; |
| 87 | case MOTIONSENSE_TYPE_PROX: |
| 88 | name = "cros-ec-prox"; |
| 89 | break; |
| 90 | case MOTIONSENSE_TYPE_LIGHT: |
| 91 | name = "cros-ec-light"; |
| 92 | break; |
| 93 | case MOTIONSENSE_TYPE_ACTIVITY: |
| 94 | name = "cros-ec-activity"; |
| 95 | break; |
| 96 | default: |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 97 | dev_warn(dev, "unknown type %d\n", |
| 98 | sensorhub->resp->info.type); |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 99 | continue; |
| 100 | } |
| 101 | |
| 102 | ret = cros_ec_sensorhub_allocate_sensor(dev, name, i); |
| 103 | if (ret) |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 104 | return ret; |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 105 | |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 106 | sensor_type[sensorhub->resp->info.type]++; |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2) |
| 110 | ec->has_kb_wake_angle = true; |
| 111 | |
| 112 | if (cros_ec_check_features(ec, |
| 113 | EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) { |
| 114 | ret = cros_ec_sensorhub_allocate_sensor(dev, |
| 115 | "cros-ec-lid-angle", |
| 116 | 0); |
| 117 | if (ret) |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 118 | return ret; |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 119 | } |
| 120 | |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 121 | return 0; |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | static int cros_ec_sensorhub_probe(struct platform_device *pdev) |
| 125 | { |
| 126 | struct device *dev = &pdev->dev; |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 127 | struct cros_ec_dev *ec = dev_get_drvdata(dev->parent); |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 128 | struct cros_ec_sensorhub *data; |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 129 | struct cros_ec_command *msg; |
Gwendal Grignou | b31d1d2 | 2020-04-27 15:59:02 -0700 | [diff] [blame] | 130 | int ret, i, sensor_num; |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 131 | |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 132 | msg = devm_kzalloc(dev, sizeof(struct cros_ec_command) + |
| 133 | max((u16)sizeof(struct ec_params_motion_sense), |
| 134 | ec->ec_dev->max_response), GFP_KERNEL); |
| 135 | if (!msg) |
| 136 | return -ENOMEM; |
| 137 | |
| 138 | msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset; |
| 139 | |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 140 | data = devm_kzalloc(dev, sizeof(struct cros_ec_sensorhub), GFP_KERNEL); |
| 141 | if (!data) |
| 142 | return -ENOMEM; |
| 143 | |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 144 | mutex_init(&data->cmd_lock); |
| 145 | |
| 146 | data->dev = dev; |
| 147 | data->ec = ec; |
| 148 | data->msg = msg; |
| 149 | data->params = (struct ec_params_motion_sense *)msg->data; |
| 150 | data->resp = (struct ec_response_motion_sense *)msg->data; |
| 151 | |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 152 | dev_set_drvdata(dev, data); |
| 153 | |
| 154 | /* Check whether this EC is a sensor hub. */ |
Gwendal Grignou | b31d1d2 | 2020-04-27 15:59:02 -0700 | [diff] [blame] | 155 | if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE)) { |
| 156 | sensor_num = cros_ec_get_sensor_count(ec); |
| 157 | if (sensor_num < 0) { |
| 158 | dev_err(dev, |
| 159 | "Unable to retrieve sensor information (err:%d)\n", |
| 160 | sensor_num); |
| 161 | return sensor_num; |
| 162 | } |
| 163 | if (sensor_num == 0) { |
| 164 | dev_err(dev, "Zero sensors reported.\n"); |
| 165 | return -EINVAL; |
| 166 | } |
| 167 | data->sensor_num = sensor_num; |
| 168 | |
| 169 | /* |
| 170 | * Prepare the ring handler before enumering the |
| 171 | * sensors. |
| 172 | */ |
| 173 | if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) { |
| 174 | ret = cros_ec_sensorhub_ring_allocate(data); |
| 175 | if (ret) |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | /* Enumerate the sensors.*/ |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 180 | ret = cros_ec_sensorhub_register(dev, data); |
| 181 | if (ret) |
| 182 | return ret; |
Gwendal Grignou | b31d1d2 | 2020-04-27 15:59:02 -0700 | [diff] [blame] | 183 | |
| 184 | /* |
| 185 | * When the EC does not have a FIFO, the sensors will query |
| 186 | * their data themselves via sysfs or a software trigger. |
| 187 | */ |
| 188 | if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) { |
| 189 | ret = cros_ec_sensorhub_ring_add(data); |
| 190 | if (ret) |
| 191 | return ret; |
| 192 | /* |
| 193 | * The msg and its data is not under the control of the |
| 194 | * ring handler. |
| 195 | */ |
| 196 | return devm_add_action_or_reset(dev, |
| 197 | cros_ec_sensorhub_ring_remove, |
| 198 | data); |
| 199 | } |
| 200 | |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 201 | } else { |
| 202 | /* |
| 203 | * If the device has sensors but does not claim to |
| 204 | * be a sensor hub, we are in legacy mode. |
| 205 | */ |
Gwendal Grignou | cee416a | 2020-03-27 15:34:32 -0700 | [diff] [blame] | 206 | data->sensor_num = 2; |
| 207 | for (i = 0; i < data->sensor_num; i++) { |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 208 | ret = cros_ec_sensorhub_allocate_sensor(dev, |
| 209 | "cros-ec-accel-legacy", i); |
| 210 | if (ret) |
| 211 | return ret; |
| 212 | } |
| 213 | } |
| 214 | |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 215 | |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 216 | return 0; |
| 217 | } |
| 218 | |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 219 | #ifdef CONFIG_PM_SLEEP |
| 220 | /* |
| 221 | * When the EC is suspending, we must stop sending interrupt, |
| 222 | * we may use the same interrupt line for waking up the device. |
| 223 | * Tell the EC to stop sending non-interrupt event on the iio ring. |
| 224 | */ |
| 225 | static int cros_ec_sensorhub_suspend(struct device *dev) |
| 226 | { |
| 227 | struct platform_device *pdev = to_platform_device(dev); |
| 228 | struct cros_ec_sensorhub *sensorhub = platform_get_drvdata(pdev); |
| 229 | struct cros_ec_dev *ec = sensorhub->ec; |
| 230 | |
| 231 | if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) |
| 232 | return cros_ec_sensorhub_ring_fifo_enable(sensorhub, false); |
| 233 | return 0; |
| 234 | } |
| 235 | |
| 236 | static int cros_ec_sensorhub_resume(struct device *dev) |
| 237 | { |
| 238 | struct platform_device *pdev = to_platform_device(dev); |
| 239 | struct cros_ec_sensorhub *sensorhub = platform_get_drvdata(pdev); |
| 240 | struct cros_ec_dev *ec = sensorhub->ec; |
| 241 | |
| 242 | if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) |
| 243 | return cros_ec_sensorhub_ring_fifo_enable(sensorhub, true); |
| 244 | return 0; |
| 245 | } |
| 246 | #endif |
| 247 | |
| 248 | static SIMPLE_DEV_PM_OPS(cros_ec_sensorhub_pm_ops, |
| 249 | cros_ec_sensorhub_suspend, |
| 250 | cros_ec_sensorhub_resume); |
| 251 | |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 252 | static struct platform_driver cros_ec_sensorhub_driver = { |
| 253 | .driver = { |
| 254 | .name = DRV_NAME, |
Gwendal Grignou | 145d59b | 2020-03-27 15:34:33 -0700 | [diff] [blame] | 255 | .pm = &cros_ec_sensorhub_pm_ops, |
Gwendal Grignou | 5306747 | 2019-11-19 13:45:45 +0100 | [diff] [blame] | 256 | }, |
| 257 | .probe = cros_ec_sensorhub_probe, |
| 258 | }; |
| 259 | |
| 260 | module_platform_driver(cros_ec_sensorhub_driver); |
| 261 | |
| 262 | MODULE_ALIAS("platform:" DRV_NAME); |
| 263 | MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>"); |
| 264 | MODULE_DESCRIPTION("ChromeOS EC MEMS Sensor Hub Driver"); |
| 265 | MODULE_LICENSE("GPL"); |