gpio: rcar: Fix runtime PM imbalance on error
[linux/fpc-iii.git] / drivers / platform / chrome / cros_ec_sensorhub.c
blobb7f2c00db5e1e423f7ed4d3d7927da0685c29f18
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/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,
29 char *sensor_name,
30 int sensor_num)
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,
38 PLATFORM_DEVID_AUTO,
39 &sensor_platforms,
40 sizeof(sensor_platforms));
41 if (IS_ERR(pdev))
42 return PTR_ERR(pdev);
44 return devm_add_action_or_reset(parent,
45 cros_ec_sensorhub_free_sensor,
46 pdev);
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;
55 int ret, i, sensor_num;
56 char *name;
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;
66 sensorhub->sensor_num = sensor_num;
67 if (sensor_num == 0) {
68 dev_err(dev, "Zero sensors reported.\n");
69 return -EINVAL;
72 msg->version = 1;
73 msg->insize = sizeof(struct ec_response_motion_sense);
74 msg->outsize = sizeof(struct ec_params_motion_sense);
76 for (i = 0; i < sensor_num; i++) {
77 sensorhub->params->cmd = MOTIONSENSE_CMD_INFO;
78 sensorhub->params->info.sensor_num = i;
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;
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;
115 ret = cros_ec_sensorhub_allocate_sensor(dev, name, i);
116 if (ret)
117 return ret;
119 sensor_type[sensorhub->resp->info.type]++;
122 if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2)
123 ec->has_kb_wake_angle = true;
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",
130 if (ret)
131 return ret;
134 return 0;
137 static int cros_ec_sensorhub_probe(struct platform_device *pdev)
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;
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;
152 msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset;
154 data = devm_kzalloc(dev, sizeof(struct cros_ec_sensorhub), GFP_KERNEL);
155 if (!data)
156 return -ENOMEM;
158 mutex_init(&data->cmd_lock);
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;
166 dev_set_drvdata(dev, data);
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 {
175 * If the device has sensors but does not claim to
176 * be a sensor hub, we are in legacy mode.
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;
188 * If the EC does not have a FIFO, the sensors will query their data
189 * themselves via sysfs or a software trigger.
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;
196 * The msg and its data is not under the control of the ring
197 * handler.
199 return devm_add_action_or_reset(dev,
200 cros_ec_sensorhub_ring_remove,
201 data);
204 return 0;
207 #ifdef CONFIG_PM_SLEEP
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.
213 static int cros_ec_sensorhub_suspend(struct device *dev)
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;
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;
224 static int cros_ec_sensorhub_resume(struct device *dev)
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;
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;
234 #endif
236 static SIMPLE_DEV_PM_OPS(cros_ec_sensorhub_pm_ops,
237 cros_ec_sensorhub_suspend,
238 cros_ec_sensorhub_resume);
240 static struct platform_driver cros_ec_sensorhub_driver = {
241 .driver = {
242 .name = DRV_NAME,
243 .pm = &cros_ec_sensorhub_pm_ops,
245 .probe = cros_ec_sensorhub_probe,
248 module_platform_driver(cros_ec_sensorhub_driver);
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");