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