1 | // SPDX-License-Identifier: GPL-2.0 |
2 | /* |
3 | * Sensor HUB driver that discovers sensors behind a ChromeOS Embedded |
4 | * Controller. |
5 | * |
6 | * Copyright 2019 Google LLC |
7 | */ |
8 | |
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> |
18 | |
19 | #define DRV_NAME "cros-ec-sensorhub" |
20 | |
21 | static void cros_ec_sensorhub_free_sensor(void *arg) |
22 | { |
23 | struct platform_device *pdev = arg; |
24 | |
25 | platform_device_unregister(pdev); |
26 | } |
27 | |
28 | static int cros_ec_sensorhub_allocate_sensor(struct device *parent, |
29 | char *sensor_name, |
30 | int sensor_num) |
31 | { |
32 | struct cros_ec_sensor_platform sensor_platforms = { |
33 | .sensor_num = sensor_num, |
34 | }; |
35 | struct platform_device *pdev; |
36 | |
37 | pdev = platform_device_register_data(parent, name: sensor_name, |
38 | PLATFORM_DEVID_AUTO, |
39 | data: &sensor_platforms, |
40 | size: sizeof(sensor_platforms)); |
41 | if (IS_ERR(ptr: pdev)) |
42 | return PTR_ERR(ptr: pdev); |
43 | |
44 | return devm_add_action_or_reset(parent, |
45 | cros_ec_sensorhub_free_sensor, |
46 | pdev); |
47 | } |
48 | |
49 | static int cros_ec_sensorhub_register(struct device *dev, |
50 | struct cros_ec_sensorhub *sensorhub) |
51 | { |
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; |
56 | char *name; |
57 | |
58 | |
59 | msg->version = 1; |
60 | msg->insize = sizeof(struct ec_response_motion_sense); |
61 | msg->outsize = sizeof(struct ec_params_motion_sense); |
62 | |
63 | for (i = 0; i < sensorhub->sensor_num; i++) { |
64 | sensorhub->params->cmd = MOTIONSENSE_CMD_INFO; |
65 | sensorhub->params->info.sensor_num = i; |
66 | |
67 | ret = cros_ec_cmd_xfer_status(ec_dev: ec->ec_dev, msg); |
68 | if (ret < 0) { |
69 | dev_warn(dev, "no info for EC sensor %d : %d/%d\n" , |
70 | i, ret, msg->result); |
71 | continue; |
72 | } |
73 | |
74 | switch (sensorhub->resp->info.type) { |
75 | case MOTIONSENSE_TYPE_ACCEL: |
76 | name = "cros-ec-accel" ; |
77 | break; |
78 | case MOTIONSENSE_TYPE_BARO: |
79 | name = "cros-ec-baro" ; |
80 | break; |
81 | case MOTIONSENSE_TYPE_GYRO: |
82 | name = "cros-ec-gyro" ; |
83 | break; |
84 | case MOTIONSENSE_TYPE_MAG: |
85 | name = "cros-ec-mag" ; |
86 | break; |
87 | case MOTIONSENSE_TYPE_PROX: |
88 | name = "cros-ec-prox" ; |
89 | break; |
90 | case MOTIONSENSE_TYPE_LIGHT: |
91 | name = "cros-ec-light" ; |
92 | break; |
93 | case MOTIONSENSE_TYPE_ACTIVITY: |
94 | name = "cros-ec-activity" ; |
95 | break; |
96 | default: |
97 | dev_warn(dev, "unknown type %d\n" , |
98 | sensorhub->resp->info.type); |
99 | continue; |
100 | } |
101 | |
102 | ret = cros_ec_sensorhub_allocate_sensor(parent: dev, sensor_name: name, sensor_num: i); |
103 | if (ret) |
104 | return ret; |
105 | |
106 | sensor_type[sensorhub->resp->info.type]++; |
107 | } |
108 | |
109 | if (sensor_type[MOTIONSENSE_TYPE_ACCEL] >= 2) |
110 | ec->has_kb_wake_angle = true; |
111 | |
112 | if (cros_ec_check_features(ec, |
113 | feature: EC_FEATURE_REFINED_TABLET_MODE_HYSTERESIS)) { |
114 | ret = cros_ec_sensorhub_allocate_sensor(parent: dev, |
115 | sensor_name: "cros-ec-lid-angle" , |
116 | sensor_num: 0); |
117 | if (ret) |
118 | return ret; |
119 | } |
120 | |
121 | return 0; |
122 | } |
123 | |
124 | static int cros_ec_sensorhub_probe(struct platform_device *pdev) |
125 | { |
126 | struct device *dev = &pdev->dev; |
127 | struct cros_ec_dev *ec = dev_get_drvdata(dev: dev->parent); |
128 | struct cros_ec_sensorhub *data; |
129 | struct cros_ec_command *msg; |
130 | int ret, i, sensor_num; |
131 | |
132 | msg = devm_kzalloc(dev, size: sizeof(struct cros_ec_command) + |
133 | max((u16)sizeof(struct ec_params_motion_sense), |
134 | ec->ec_dev->max_response), GFP_KERNEL); |
135 | if (!msg) |
136 | return -ENOMEM; |
137 | |
138 | msg->command = EC_CMD_MOTION_SENSE_CMD + ec->cmd_offset; |
139 | |
140 | data = devm_kzalloc(dev, size: sizeof(struct cros_ec_sensorhub), GFP_KERNEL); |
141 | if (!data) |
142 | return -ENOMEM; |
143 | |
144 | mutex_init(&data->cmd_lock); |
145 | |
146 | data->dev = dev; |
147 | data->ec = ec; |
148 | data->msg = msg; |
149 | data->params = (struct ec_params_motion_sense *)msg->data; |
150 | data->resp = (struct ec_response_motion_sense *)msg->data; |
151 | |
152 | dev_set_drvdata(dev, data); |
153 | |
154 | /* Check whether this EC is a sensor hub. */ |
155 | if (cros_ec_check_features(ec, feature: EC_FEATURE_MOTION_SENSE)) { |
156 | sensor_num = cros_ec_get_sensor_count(ec); |
157 | if (sensor_num < 0) { |
158 | dev_err(dev, |
159 | "Unable to retrieve sensor information (err:%d)\n" , |
160 | sensor_num); |
161 | return sensor_num; |
162 | } |
163 | if (sensor_num == 0) { |
164 | dev_err(dev, "Zero sensors reported.\n" ); |
165 | return -EINVAL; |
166 | } |
167 | data->sensor_num = sensor_num; |
168 | |
169 | /* |
170 | * Prepare the ring handler before enumering the |
171 | * sensors. |
172 | */ |
173 | if (cros_ec_check_features(ec, feature: EC_FEATURE_MOTION_SENSE_FIFO)) { |
174 | ret = cros_ec_sensorhub_ring_allocate(sensorhub: data); |
175 | if (ret) |
176 | return ret; |
177 | } |
178 | |
179 | /* Enumerate the sensors.*/ |
180 | ret = cros_ec_sensorhub_register(dev, sensorhub: data); |
181 | if (ret) |
182 | return ret; |
183 | |
184 | /* |
185 | * When the EC does not have a FIFO, the sensors will query |
186 | * their data themselves via sysfs or a software trigger. |
187 | */ |
188 | if (cros_ec_check_features(ec, feature: EC_FEATURE_MOTION_SENSE_FIFO)) { |
189 | ret = cros_ec_sensorhub_ring_add(sensorhub: data); |
190 | if (ret) |
191 | return ret; |
192 | /* |
193 | * The msg and its data is not under the control of the |
194 | * ring handler. |
195 | */ |
196 | return devm_add_action_or_reset(dev, |
197 | cros_ec_sensorhub_ring_remove, |
198 | data); |
199 | } |
200 | |
201 | } else { |
202 | /* |
203 | * If the device has sensors but does not claim to |
204 | * be a sensor hub, we are in legacy mode. |
205 | */ |
206 | data->sensor_num = 2; |
207 | for (i = 0; i < data->sensor_num; i++) { |
208 | ret = cros_ec_sensorhub_allocate_sensor(parent: dev, |
209 | sensor_name: "cros-ec-accel-legacy" , sensor_num: i); |
210 | if (ret) |
211 | return ret; |
212 | } |
213 | } |
214 | |
215 | |
216 | return 0; |
217 | } |
218 | |
219 | #ifdef CONFIG_PM_SLEEP |
220 | /* |
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. |
224 | */ |
225 | static int cros_ec_sensorhub_suspend(struct device *dev) |
226 | { |
227 | struct cros_ec_sensorhub *sensorhub = dev_get_drvdata(dev); |
228 | struct cros_ec_dev *ec = sensorhub->ec; |
229 | |
230 | if (cros_ec_check_features(ec, feature: EC_FEATURE_MOTION_SENSE_FIFO)) |
231 | return cros_ec_sensorhub_ring_fifo_enable(sensorhub, on: false); |
232 | return 0; |
233 | } |
234 | |
235 | static int cros_ec_sensorhub_resume(struct device *dev) |
236 | { |
237 | struct cros_ec_sensorhub *sensorhub = dev_get_drvdata(dev); |
238 | struct cros_ec_dev *ec = sensorhub->ec; |
239 | |
240 | if (cros_ec_check_features(ec, feature: EC_FEATURE_MOTION_SENSE_FIFO)) |
241 | return cros_ec_sensorhub_ring_fifo_enable(sensorhub, on: true); |
242 | return 0; |
243 | } |
244 | #endif |
245 | |
246 | static SIMPLE_DEV_PM_OPS(cros_ec_sensorhub_pm_ops, |
247 | cros_ec_sensorhub_suspend, |
248 | cros_ec_sensorhub_resume); |
249 | |
250 | static struct platform_driver cros_ec_sensorhub_driver = { |
251 | .driver = { |
252 | .name = DRV_NAME, |
253 | .pm = &cros_ec_sensorhub_pm_ops, |
254 | }, |
255 | .probe = cros_ec_sensorhub_probe, |
256 | }; |
257 | |
258 | module_platform_driver(cros_ec_sensorhub_driver); |
259 | |
260 | MODULE_ALIAS("platform:" DRV_NAME); |
261 | MODULE_AUTHOR("Gwendal Grignou <gwendal@chromium.org>" ); |
262 | MODULE_DESCRIPTION("ChromeOS EC MEMS Sensor Hub Driver" ); |
263 | MODULE_LICENSE("GPL" ); |
264 | |