1 // SPDX-License-Identifier: GPL-2.0
3 * Sensor HUB driver that discovers sensors behind a ChromeOS Embedded
6 * Copyright 2019 Google LLC
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>
20 #define DRV_NAME "cros-ec-sensorhub"
22 static void cros_ec_sensorhub_free_sensor(void *arg)
24 struct platform_device *pdev = arg;
26 platform_device_unregister(pdev);
29 static int cros_ec_sensorhub_allocate_sensor(struct device *parent,
33 struct cros_ec_sensor_platform sensor_platforms = {
34 .sensor_num = sensor_num,
36 struct platform_device *pdev;
38 pdev = platform_device_register_data(parent, sensor_name,
41 sizeof(sensor_platforms));
45 return devm_add_action_or_reset(parent,
46 cros_ec_sensorhub_free_sensor,
50 static int cros_ec_sensorhub_register(struct device *dev,
51 struct cros_ec_sensorhub *sensorhub)
53 int sensor_type[MOTIONSENSE_TYPE_MAX] = { 0 };
54 struct cros_ec_command *msg = sensorhub->msg;
55 struct cros_ec_dev *ec = sensorhub->ec;
61 msg->insize = sizeof(struct ec_response_motion_sense);
62 msg->outsize = sizeof(struct ec_params_motion_sense);
64 for (i = 0; i < sensorhub->sensor_num; i++) {
65 sensorhub->params->cmd = MOTIONSENSE_CMD_INFO;
66 sensorhub->params->info.sensor_num = i;
68 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
70 dev_warn(dev, "no info for EC sensor %d : %d/%d\n",
75 switch (sensorhub->resp->info.type) {
76 case MOTIONSENSE_TYPE_ACCEL:
77 name = "cros-ec-accel";
79 case MOTIONSENSE_TYPE_BARO:
80 name = "cros-ec-baro";
82 case MOTIONSENSE_TYPE_GYRO:
83 name = "cros-ec-gyro";
85 case MOTIONSENSE_TYPE_MAG:
88 case MOTIONSENSE_TYPE_PROX:
89 name = "cros-ec-prox";
91 case MOTIONSENSE_TYPE_LIGHT:
92 name = "cros-ec-light";
94 case MOTIONSENSE_TYPE_ACTIVITY:
95 name = "cros-ec-activity";
98 dev_warn(dev, "unknown type %d\n",
99 sensorhub->resp->info.type);
103 ret = cros_ec_sensorhub_allocate_sensor(dev, name, i);
107 sensor_type[sensorhub->resp->info.type]++;
110 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
111 ec->has_kb_wake_angle = true;
113 if (cros_ec_check_features(ec,
114 EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
115 ret = cros_ec_sensorhub_allocate_sensor(dev,
125 static int cros_ec_sensorhub_probe(struct platform_device *pdev)
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;
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);
139 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
141 data = devm_kzalloc(dev, sizeof(struct cros_ec_sensorhub), GFP_KERNEL);
145 mutex_init(&data->cmd_lock);
150 data->params = (struct ec_params_motion_sense *)msg->data;
151 data->resp = (struct ec_response_motion_sense *)msg->data;
153 dev_set_drvdata(dev, data);
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) {
160 "Unable to retrieve sensor information (err:%d)\n",
164 if (sensor_num == 0) {
165 dev_err(dev, "Zero sensors reported.\n");
168 data->sensor_num = sensor_num;
171 * Prepare the ring handler before enumering the
174 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
175 ret = cros_ec_sensorhub_ring_allocate(data);
180 /* Enumerate the sensors.*/
181 ret = cros_ec_sensorhub_register(dev, data);
186 * When the EC does not have a FIFO, the sensors will query
187 * their data themselves via sysfs or a software trigger.
189 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
190 ret = cros_ec_sensorhub_ring_add(data);
194 * The msg and its data is not under the control of the
197 return devm_add_action_or_reset(dev,
198 cros_ec_sensorhub_ring_remove,
204 * If the device has sensors but does not claim to
205 * be a sensor hub, we are in legacy mode.
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);
220 #ifdef CONFIG_PM_SLEEP
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.
226 static int cros_ec_sensorhub_suspend(struct device *dev)
228 struct cros_ec_sensorhub *sensorhub = dev_get_drvdata(dev);
229 struct cros_ec_dev *ec = sensorhub->ec;
231 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO))
232 return cros_ec_sensorhub_ring_fifo_enable(sensorhub, false);
236 static int cros_ec_sensorhub_resume(struct device *dev)
238 struct cros_ec_sensorhub *sensorhub = dev_get_drvdata(dev);
239 struct cros_ec_dev *ec = sensorhub->ec;
241 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO))
242 return cros_ec_sensorhub_ring_fifo_enable(sensorhub, true);
247 static SIMPLE_DEV_PM_OPS(cros_ec_sensorhub_pm_ops,
248 cros_ec_sensorhub_suspend,
249 cros_ec_sensorhub_resume);
251 static const struct platform_device_id cros_ec_sensorhub_id[] = {
255 MODULE_DEVICE_TABLE(platform, cros_ec_sensorhub_id);
257 static struct platform_driver cros_ec_sensorhub_driver = {
260 .pm = &cros_ec_sensorhub_pm_ops,
262 .probe = cros_ec_sensorhub_probe,
263 .id_table = cros_ec_sensorhub_id,
266 module_platform_driver(cros_ec_sensorhub_driver);
269 MODULE_DESCRIPTION("ChromeOS EC MEMS Sensor Hub Driver");
270 MODULE_LICENSE("GPL");