hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
kernel/drivers/iio/common/cros_ec_sensors/cros_ec_sensors.c
....@@ -1,22 +1,13 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * cros_ec_sensors - Driver for Chrome OS Embedded Controller sensors.
34 *
45 * Copyright (C) 2016 Google, Inc
56 *
6
- * This software is licensed under the terms of the GNU General Public
7
- * License version 2, as published by the Free Software Foundation, and
8
- * may be copied, distributed, and modified under those terms.
9
- *
10
- * This program is distributed in the hope that it will be useful,
11
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
- * GNU General Public License for more details.
14
- *
157 * This driver uses the cros-ec interface to communicate with the Chrome OS
168 * EC about sensors data. Data access is presented through iio sysfs.
179 */
1810
19
-#include <linux/delay.h>
2011 #include <linux/device.h>
2112 #include <linux/iio/buffer.h>
2213 #include <linux/iio/common/cros_ec_sensors_core.h>
....@@ -25,12 +16,11 @@
2516 #include <linux/iio/trigger_consumer.h>
2617 #include <linux/iio/triggered_buffer.h>
2718 #include <linux/kernel.h>
28
-#include <linux/mfd/cros_ec.h>
29
-#include <linux/mfd/cros_ec_commands.h>
3019 #include <linux/module.h>
20
+#include <linux/platform_data/cros_ec_commands.h>
21
+#include <linux/platform_data/cros_ec_proto.h>
3122 #include <linux/platform_device.h>
3223 #include <linux/slab.h>
33
-#include <linux/sysfs.h>
3424
3525 #define CROS_EC_SENSORS_MAX_CHANNELS 4
3626
....@@ -73,10 +63,35 @@
7363
7464 /* Save values */
7565 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
76
- st->core.calib[i] =
66
+ st->core.calib[i].offset =
7767 st->core.resp->sensor_offset.offset[i];
7868 ret = IIO_VAL_INT;
79
- *val = st->core.calib[idx];
69
+ *val = st->core.calib[idx].offset;
70
+ break;
71
+ case IIO_CHAN_INFO_CALIBSCALE:
72
+ st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_SCALE;
73
+ st->core.param.sensor_offset.flags = 0;
74
+
75
+ ret = cros_ec_motion_send_host_cmd(&st->core, 0);
76
+ if (ret == -EPROTO || ret == -EOPNOTSUPP) {
77
+ /* Reading calibscale is not supported on older EC. */
78
+ *val = 1;
79
+ *val2 = 0;
80
+ ret = IIO_VAL_INT_PLUS_MICRO;
81
+ break;
82
+ } else if (ret) {
83
+ break;
84
+ }
85
+
86
+ /* Save values */
87
+ for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
88
+ st->core.calib[i].scale =
89
+ st->core.resp->sensor_scale.scale[i];
90
+
91
+ *val = st->core.calib[idx].scale >> 15;
92
+ *val2 = ((st->core.calib[idx].scale & 0x7FFF) * 1000000LL) /
93
+ MOTION_SENSE_DEFAULT_SCALE;
94
+ ret = IIO_VAL_INT_PLUS_MICRO;
8095 break;
8196 case IIO_CHAN_INFO_SCALE:
8297 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
....@@ -144,7 +159,7 @@
144159
145160 switch (mask) {
146161 case IIO_CHAN_INFO_CALIBBIAS:
147
- st->core.calib[idx] = val;
162
+ st->core.calib[idx].offset = val;
148163
149164 /* Send to EC for each axis, even if not complete */
150165 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET;
....@@ -152,8 +167,23 @@
152167 MOTION_SENSE_SET_OFFSET;
153168 for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
154169 st->core.param.sensor_offset.offset[i] =
155
- st->core.calib[i];
170
+ st->core.calib[i].offset;
156171 st->core.param.sensor_offset.temp =
172
+ EC_MOTION_SENSE_INVALID_CALIB_TEMP;
173
+
174
+ ret = cros_ec_motion_send_host_cmd(&st->core, 0);
175
+ break;
176
+ case IIO_CHAN_INFO_CALIBSCALE:
177
+ st->core.calib[idx].scale = val;
178
+ /* Send to EC for each axis, even if not complete */
179
+
180
+ st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_SCALE;
181
+ st->core.param.sensor_offset.flags =
182
+ MOTION_SENSE_SET_OFFSET;
183
+ for (i = CROS_EC_SENSOR_X; i < CROS_EC_SENSOR_MAX_AXIS; i++)
184
+ st->core.param.sensor_scale.scale[i] =
185
+ st->core.calib[i].scale;
186
+ st->core.param.sensor_scale.temp =
157187 EC_MOTION_SENSE_INVALID_CALIB_TEMP;
158188
159189 ret = cros_ec_motion_send_host_cmd(&st->core, 0);
....@@ -170,6 +200,10 @@
170200 st->core.param.sensor_range.roundup = 1;
171201
172202 ret = cros_ec_motion_send_host_cmd(&st->core, 0);
203
+ if (ret == 0) {
204
+ st->core.range_updated = true;
205
+ st->core.curr_range = val;
206
+ }
173207 break;
174208 default:
175209 ret = cros_ec_sensors_core_write(
....@@ -185,27 +219,25 @@
185219 static const struct iio_info ec_sensors_info = {
186220 .read_raw = &cros_ec_sensors_read,
187221 .write_raw = &cros_ec_sensors_write,
222
+ .read_avail = &cros_ec_sensors_core_read_avail,
188223 };
189224
190225 static int cros_ec_sensors_probe(struct platform_device *pdev)
191226 {
192227 struct device *dev = &pdev->dev;
193
- struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
194228 struct iio_dev *indio_dev;
195229 struct cros_ec_sensors_state *state;
196230 struct iio_chan_spec *channel;
197231 int ret, i;
198232
199
- if (!ec_dev || !ec_dev->ec_dev) {
200
- dev_warn(&pdev->dev, "No CROS EC device found.\n");
201
- return -EINVAL;
202
- }
203
-
204233 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*state));
205234 if (!indio_dev)
206235 return -ENOMEM;
207236
208
- ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
237
+ ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
238
+ cros_ec_sensors_capture,
239
+ cros_ec_sensors_push_data,
240
+ true);
209241 if (ret)
210242 return ret;
211243
....@@ -216,10 +248,12 @@
216248 /* Common part */
217249 channel->info_mask_separate =
218250 BIT(IIO_CHAN_INFO_RAW) |
219
- BIT(IIO_CHAN_INFO_CALIBBIAS);
251
+ BIT(IIO_CHAN_INFO_CALIBBIAS) |
252
+ BIT(IIO_CHAN_INFO_CALIBSCALE);
220253 channel->info_mask_shared_by_all =
221254 BIT(IIO_CHAN_INFO_SCALE) |
222
- BIT(IIO_CHAN_INFO_FREQUENCY) |
255
+ BIT(IIO_CHAN_INFO_SAMP_FREQ);
256
+ channel->info_mask_shared_by_all_available =
223257 BIT(IIO_CHAN_INFO_SAMP_FREQ);
224258 channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
225259 channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
....@@ -263,11 +297,6 @@
263297 state->core.read_ec_sensors_data = cros_ec_sensors_read_lpc;
264298 else
265299 state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
266
-
267
- ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
268
- cros_ec_sensors_capture, NULL);
269
- if (ret)
270
- return ret;
271300
272301 return devm_iio_device_register(dev, indio_dev);
273302 }