Revert "unicode: Don't special case ignorable code points"
[linux.git] / drivers / platform / chrome / cros_ec_sensorhub.c
blob50cdae67fa3204f61649c3cf733d81ad4f3ad428
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Sensor HUB driver that discovers sensors behind a ChromeOS Embedded
4 * Controller.
6 * Copyright 2019 Google LLC
7 */
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,
30 char *sensor_name,
31 int sensor_num)
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,
39 PLATFORM_DEVID_AUTO,
40 &sensor_platforms,
41 sizeof(sensor_platforms));
42 if (IS_ERR(pdev))
43 return PTR_ERR(pdev);
45 return devm_add_action_or_reset(parent,
46 cros_ec_sensorhub_free_sensor,
47 pdev);
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;
56 int ret, i;
57 char *name;
60 msg->version = 1;
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);
69 if (ret < 0) {
70 dev_warn(dev, "no info for EC sensor %d : %d/%d\n",
71 i, ret, msg->result);
72 continue;
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;
103 ret = cros_ec_sensorhub_allocate_sensor(dev, name, i);
104 if (ret)
105 return ret;
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,
116 "cros-ec-lid-angle",
118 if (ret)
119 return ret;
122 return 0;
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);
136 if (!msg)
137 return -ENOMEM;
139 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
141 data = devm_kzalloc(dev, sizeof(struct cros_ec_sensorhub), GFP_KERNEL);
142 if (!data)
143 return -ENOMEM;
145 mutex_init(&data->cmd_lock);
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;
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) {
159 dev_err(dev,
160 "Unable to retrieve sensor information (err:%d)\n",
161 sensor_num);
162 return sensor_num;
164 if (sensor_num == 0) {
165 dev_err(dev, "Zero sensors reported.\n");
166 return -EINVAL;
168 data->sensor_num = sensor_num;
171 * Prepare the ring handler before enumering the
172 * sensors.
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;
180 /* Enumerate the sensors.*/
181 ret = cros_ec_sensorhub_register(dev, data);
182 if (ret)
183 return ret;
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);
191 if (ret)
192 return ret;
194 * The msg and its data is not under the control of the
195 * ring handler.
197 return devm_add_action_or_reset(dev,
198 cros_ec_sensorhub_ring_remove,
199 data);
202 } else {
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);
211 if (ret)
212 return ret;
217 return 0;
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);
233 return 0;
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);
243 return 0;
245 #endif
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[] = {
252 { DRV_NAME, 0 },
255 MODULE_DEVICE_TABLE(platform, cros_ec_sensorhub_id);
257 static struct platform_driver cros_ec_sensorhub_driver = {
258 .driver = {
259 .name = DRV_NAME,
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);
268 MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>");
269 MODULE_DESCRIPTION("ChromeOS EC MEMS Sensor Hub Driver");
270 MODULE_LICENSE("GPL");