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