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/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>
19 #define DRV_NAME "cros-ec-sensorhub"
21 static void cros_ec_sensorhub_free_sensor(void *arg)
23 struct platform_device *pdev = arg;
25 platform_device_unregister(pdev);
28 static int cros_ec_sensorhub_allocate_sensor(struct device *parent,
32 struct cros_ec_sensor_platform sensor_platforms = {
33 .sensor_num = sensor_num,
35 struct platform_device *pdev;
37 pdev = platform_device_register_data(parent, sensor_name,
40 sizeof(sensor_platforms));
44 return devm_add_action_or_reset(parent,
45 cros_ec_sensorhub_free_sensor,
49 static int cros_ec_sensorhub_register(struct device *dev,
50 struct cros_ec_sensorhub *sensorhub)
52 int sensor_type[MOTIONSENSE_TYPE_MAX] = { 0 };
53 struct cros_ec_command *msg = sensorhub->msg;
54 struct cros_ec_dev *ec = sensorhub->ec;
60 msg->insize = sizeof(struct ec_response_motion_sense);
61 msg->outsize = sizeof(struct ec_params_motion_sense);
63 for (i = 0; i < sensorhub->sensor_num; i++) {
64 sensorhub->params->cmd = MOTIONSENSE_CMD_INFO;
65 sensorhub->params->info.sensor_num = i;
67 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
69 dev_warn(dev, "no info for EC sensor %d : %d/%d\n",
74 switch (sensorhub->resp->info.type) {
75 case MOTIONSENSE_TYPE_ACCEL:
76 name = "cros-ec-accel";
78 case MOTIONSENSE_TYPE_BARO:
79 name = "cros-ec-baro";
81 case MOTIONSENSE_TYPE_GYRO:
82 name = "cros-ec-gyro";
84 case MOTIONSENSE_TYPE_MAG:
87 case MOTIONSENSE_TYPE_PROX:
88 name = "cros-ec-prox";
90 case MOTIONSENSE_TYPE_LIGHT:
91 name = "cros-ec-light";
93 case MOTIONSENSE_TYPE_ACTIVITY:
94 name = "cros-ec-activity";
97 dev_warn(dev, "unknown type %d\n",
98 sensorhub->resp->info.type);
102 ret = cros_ec_sensorhub_allocate_sensor(dev, name, i);
106 sensor_type[sensorhub->resp->info.type]++;
109 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
110 ec->has_kb_wake_angle = true;
112 if (cros_ec_check_features(ec,
113 EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
114 ret = cros_ec_sensorhub_allocate_sensor(dev,
124 static int cros_ec_sensorhub_probe(struct platform_device *pdev)
126 struct device *dev = &pdev->dev;
127 struct cros_ec_dev *ec = dev_get_drvdata(dev->parent);
128 struct cros_ec_sensorhub *data;
129 struct cros_ec_command *msg;
130 int ret, i, sensor_num;
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);
138 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
140 data = devm_kzalloc(dev, sizeof(struct cros_ec_sensorhub), GFP_KERNEL);
144 mutex_init(&data->cmd_lock);
149 data->params = (struct ec_params_motion_sense *)msg->data;
150 data->resp = (struct ec_response_motion_sense *)msg->data;
152 dev_set_drvdata(dev, data);
154 /* Check whether this EC is a sensor hub. */
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) {
159 "Unable to retrieve sensor information (err:%d)\n",
163 if (sensor_num == 0) {
164 dev_err(dev, "Zero sensors reported.\n");
167 data->sensor_num = sensor_num;
170 * Prepare the ring handler before enumering the
173 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
174 ret = cros_ec_sensorhub_ring_allocate(data);
179 /* Enumerate the sensors.*/
180 ret = cros_ec_sensorhub_register(dev, data);
185 * When the EC does not have a FIFO, the sensors will query
186 * their data themselves via sysfs or a software trigger.
188 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO)) {
189 ret = cros_ec_sensorhub_ring_add(data);
193 * The msg and its data is not under the control of the
196 return devm_add_action_or_reset(dev,
197 cros_ec_sensorhub_ring_remove,
203 * If the device has sensors but does not claim to
204 * be a sensor hub, we are in legacy mode.
206 data->sensor_num = 2;
207 for (i = 0; i < data->sensor_num; i++) {
208 ret = cros_ec_sensorhub_allocate_sensor(dev,
209 "cros-ec-accel-legacy", i);
219 #ifdef CONFIG_PM_SLEEP
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.
225 static int cros_ec_sensorhub_suspend(struct device *dev)
227 struct cros_ec_sensorhub *sensorhub = dev_get_drvdata(dev);
228 struct cros_ec_dev *ec = sensorhub->ec;
230 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO))
231 return cros_ec_sensorhub_ring_fifo_enable(sensorhub, false);
235 static int cros_ec_sensorhub_resume(struct device *dev)
237 struct cros_ec_sensorhub *sensorhub = dev_get_drvdata(dev);
238 struct cros_ec_dev *ec = sensorhub->ec;
240 if (cros_ec_check_features(ec, EC_FEATURE_MOTION_SENSE_FIFO))
241 return cros_ec_sensorhub_ring_fifo_enable(sensorhub, true);
246 static SIMPLE_DEV_PM_OPS(cros_ec_sensorhub_pm_ops,
247 cros_ec_sensorhub_suspend,
248 cros_ec_sensorhub_resume);
250 static struct platform_driver cros_ec_sensorhub_driver = {
253 .pm = &cros_ec_sensorhub_pm_ops,
255 .probe = cros_ec_sensorhub_probe,
258 module_platform_driver(cros_ec_sensorhub_driver);
260 MODULE_ALIAS("platform:" DRV_NAME);
262 MODULE_DESCRIPTION("ChromeOS EC MEMS Sensor Hub Driver");
263 MODULE_LICENSE("GPL");