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/mfd/cros_ec.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_dev *ec = sensorhub->ec;
55 struct ec_params_motion_sense *params;
56 struct ec_response_motion_sense *resp;
57 struct cros_ec_command *msg;
58 int ret, i, sensor_num;
61 sensor_num = cros_ec_get_sensor_count(ec);
64 "Unable to retrieve sensor information (err:%d)\n",
69 if (sensor_num == 0) {
70 dev_err(dev, "Zero sensors reported.\n");
74 /* Prepare a message to send INFO command to each sensor. */
75 msg = kzalloc(sizeof(*msg) + max(sizeof(*params), sizeof(*resp)),
81 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
82 msg->outsize = sizeof(*params);
83 msg->insize = sizeof(*resp);
84 params = (struct ec_params_motion_sense *)msg->data;
85 resp = (struct ec_response_motion_sense *)msg->data;
87 for (i = 0; i < sensor_num; i++) {
88 params->cmd = MOTIONSENSE_CMD_INFO;
89 params->info.sensor_num = i;
91 ret = cros_ec_cmd_xfer_status(ec->ec_dev, msg);
93 dev_warn(dev, "no info for EC sensor %d : %d/%d\n",
98 switch (resp->info.type) {
99 case MOTIONSENSE_TYPE_ACCEL:
100 name = "cros-ec-accel";
102 case MOTIONSENSE_TYPE_BARO:
103 name = "cros-ec-baro";
105 case MOTIONSENSE_TYPE_GYRO:
106 name = "cros-ec-gyro";
108 case MOTIONSENSE_TYPE_MAG:
109 name = "cros-ec-mag";
111 case MOTIONSENSE_TYPE_PROX:
112 name = "cros-ec-prox";
114 case MOTIONSENSE_TYPE_LIGHT:
115 name = "cros-ec-light";
117 case MOTIONSENSE_TYPE_ACTIVITY:
118 name = "cros-ec-activity";
121 dev_warn(dev, "unknown type %d\n", resp->info.type);
125 ret = cros_ec_sensorhub_allocate_sensor(dev, name, i);
129 sensor_type[resp->info.type]++;
132 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
133 ec->has_kb_wake_angle = true;
135 if (cros_ec_check_features(ec,
136 EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) {
137 ret = cros_ec_sensorhub_allocate_sensor(dev,
152 static int cros_ec_sensorhub_probe(struct platform_device *pdev)
154 struct device *dev = &pdev->dev;
155 struct cros_ec_sensorhub *data;
159 data = devm_kzalloc(dev, sizeof(struct cros_ec_sensorhub), GFP_KERNEL);
163 data->ec = dev_get_drvdata(dev->parent);
164 dev_set_drvdata(dev, data);
166 /* Check whether this EC is a sensor hub. */
167 if (cros_ec_check_features(data->ec, EC_FEATURE_MOTION_SENSE)) {
168 ret = cros_ec_sensorhub_register(dev, data);
173 * If the device has sensors but does not claim to
174 * be a sensor hub, we are in legacy mode.
176 for (i = 0; i < 2; i++) {
177 ret = cros_ec_sensorhub_allocate_sensor(dev,
178 "cros-ec-accel-legacy", i);
187 static struct platform_driver cros_ec_sensorhub_driver = {
191 .probe = cros_ec_sensorhub_probe,
194 module_platform_driver(cros_ec_sensorhub_driver);
196 MODULE_ALIAS("platform:" DRV_NAME);
198 MODULE_DESCRIPTION("ChromeOS EC MEMS Sensor Hub Driver");
199 MODULE_LICENSE("GPL");