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> 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 }; 53 struct cros_ec_command *msg = sensorhub->msg; 54 struct cros_ec_dev *ec = sensorhub->ec; 55 int ret, i, sensor_num; 56 char *name; 57 58 sensor_num = cros_ec_get_sensor_count(ec); 59 if (sensor_num < 0) { 60 dev_err(dev, 61 "Unable to retrieve sensor information (err:%d)\n", 62 sensor_num); 63 return sensor_num; 64 } 65 66 sensorhub->sensor_num = sensor_num; 67 if (sensor_num == 0) { 68 dev_err(dev, "Zero sensors reported.\n"); 69 return -EINVAL; 70 } 71 72 msg->version = 1; 73 msg->insize = sizeof(struct ec_response_motion_sense); 74 msg->outsize = sizeof(struct ec_params_motion_sense); 75 76 for (i = 0; i < sensor_num; i++) { 77 sensorhub->params->cmd = MOTIONSENSE_CMD_INFO; 78 sensorhub->params->info.sensor_num = i; 79 80 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg); 81 if (ret < 0) { 82 dev_warn(dev, "no info for EC sensor %d : %d/%d\n", 83 i, ret, msg->result); 84 continue; 85 } 86 87 switch (sensorhub->resp->info.type) { 88 case MOTIONSENSE_TYPE_ACCEL: 89 name = "cros-ec-accel"; 90 break; 91 case MOTIONSENSE_TYPE_BARO: 92 name = "cros-ec-baro"; 93 break; 94 case MOTIONSENSE_TYPE_GYRO: 95 name = "cros-ec-gyro"; 96 break; 97 case MOTIONSENSE_TYPE_MAG: 98 name = "cros-ec-mag"; 99 break; 100 case MOTIONSENSE_TYPE_PROX: 101 name = "cros-ec-prox"; 102 break; 103 case MOTIONSENSE_TYPE_LIGHT: 104 name = "cros-ec-light"; 105 break; 106 case MOTIONSENSE_TYPE_ACTIVITY: 107 name = "cros-ec-activity"; 108 break; 109 default: 110 dev_warn(dev, "unknown type %d\n", 111 sensorhub->resp->info.type); 112 continue; 113 } 114 115 ret = cros_ec_sensorhub_allocate_sensor(dev, name, i); 116 if (ret) 117 return ret; 118 119 sensor_type[sensorhub->resp->info.type]++; 120 } 121 122 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2) 123 ec->has_kb_wake_angle = true; 124 125 if (cros_ec_check_features(ec, 126 EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) { 127 ret = cros_ec_sensorhub_allocate_sensor(dev, 128 "cros-ec-lid-angle", 129 0); 130 if (ret) 131 return ret; 132 } 133 134 return 0; 135 } 136 137 static int cros_ec_sensorhub_probe(struct platform_device *pdev) 138 { 139 struct device *dev = &pdev->dev; 140 struct cros_ec_dev *ec = dev_get_drvdata(dev->parent); 141 struct cros_ec_sensorhub *data; 142 struct cros_ec_command *msg; 143 int ret; 144 int i; 145 146 msg = devm_kzalloc(dev, sizeof(struct cros_ec_command) + 147 max((u16)sizeof(struct ec_params_motion_sense), 148 ec->ec_dev->max_response), GFP_KERNEL); 149 if (!msg) 150 return -ENOMEM; 151 152 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset; 153 154 data = devm_kzalloc(dev, sizeof(struct cros_ec_sensorhub), GFP_KERNEL); 155 if (!data) 156 return -ENOMEM; 157 158 mutex_init(&data->cmd_lock); 159 160 data->dev = dev; 161 data->ec = ec; 162 data->msg = msg; 163 data->params = (struct ec_params_motion_sense *)msg->data; 164 data->resp = (struct ec_response_motion_sense *)msg->data; 165 166 dev_set_drvdata(dev, data); 167 168 /* Check whether this EC is a sensor hub. */ 169 if (cros_ec_check_features(data->ec, EC_FEATURE_MOTION_SENSE)) { 170 ret = cros_ec_sensorhub_register(dev, data); 171 if (ret) 172 return ret; 173 } else { 174 /* 175 * If the device has sensors but does not claim to 176 * be a sensor hub, we are in legacy mode. 177 */ 178 data->sensor_num = 2; 179 for (i = 0; i < data->sensor_num; i++) { 180 ret = cros_ec_sensorhub_allocate_sensor(dev, 181 "cros-ec-accel-legacy", i); 182 if (ret) 183 return ret; 184 } 185 } 186 187 /* 188 * If the EC does not have a FIFO, the sensors will query their data 189 * themselves via sysfs or a software trigger. 190 */ 191 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) { 192 ret = cros_ec_sensorhub_ring_add(data); 193 if (ret) 194 return ret; 195 /* 196 * The msg and its data is not under the control of the ring 197 * handler. 198 */ 199 return devm_add_action_or_reset(dev, 200 cros_ec_sensorhub_ring_remove, 201 data); 202 } 203 204 return 0; 205 } 206 207 #ifdef CONFIG_PM_SLEEP 208 /* 209 * When the EC is suspending, we must stop sending interrupt, 210 * we may use the same interrupt line for waking up the device. 211 * Tell the EC to stop sending non-interrupt event on the iio ring. 212 */ 213 static int cros_ec_sensorhub_suspend(struct device *dev) 214 { 215 struct platform_device *pdev = to_platform_device(dev); 216 struct cros_ec_sensorhub *sensorhub = platform_get_drvdata(pdev); 217 struct cros_ec_dev *ec = sensorhub->ec; 218 219 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) 220 return cros_ec_sensorhub_ring_fifo_enable(sensorhub, false); 221 return 0; 222 } 223 224 static int cros_ec_sensorhub_resume(struct device *dev) 225 { 226 struct platform_device *pdev = to_platform_device(dev); 227 struct cros_ec_sensorhub *sensorhub = platform_get_drvdata(pdev); 228 struct cros_ec_dev *ec = sensorhub->ec; 229 230 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) 231 return cros_ec_sensorhub_ring_fifo_enable(sensorhub, true); 232 return 0; 233 } 234 #endif 235 236 static SIMPLE_DEV_PM_OPS(cros_ec_sensorhub_pm_ops, 237 cros_ec_sensorhub_suspend, 238 cros_ec_sensorhub_resume); 239 240 static struct platform_driver cros_ec_sensorhub_driver = { 241 .driver = { 242 .name = DRV_NAME, 243 .pm = &cros_ec_sensorhub_pm_ops, 244 }, 245 .probe = cros_ec_sensorhub_probe, 246 }; 247 248 module_platform_driver(cros_ec_sensorhub_driver); 249 250 MODULE_ALIAS("platform:" DRV_NAME); 251 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>"); 252 MODULE_DESCRIPTION("ChromeOS EC MEMS Sensor Hub Driver"); 253 MODULE_LICENSE("GPL"); 254