forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-13 9d77db3c730780c8ef5ccd4b66403ff5675cfe4e
kernel/drivers/iio/light/cros_ec_light_prox.c
....@@ -1,19 +1,10 @@
1
+// SPDX-License-Identifier: GPL-2.0
12 /*
23 * cros_ec_light_prox - Driver for light and prox sensors behing CrosEC.
34 *
45 * Copyright (C) 2017 Google, Inc
5
- *
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.
146 */
157
16
-#include <linux/delay.h>
178 #include <linux/device.h>
189 #include <linux/iio/buffer.h>
1910 #include <linux/iio/common/cros_ec_sensors_core.h>
....@@ -23,12 +14,11 @@
2314 #include <linux/iio/triggered_buffer.h>
2415 #include <linux/iio/trigger_consumer.h>
2516 #include <linux/kernel.h>
26
-#include <linux/mfd/cros_ec.h>
27
-#include <linux/mfd/cros_ec_commands.h>
2817 #include <linux/module.h>
18
+#include <linux/platform_data/cros_ec_commands.h>
19
+#include <linux/platform_data/cros_ec_proto.h>
2920 #include <linux/platform_device.h>
3021 #include <linux/slab.h>
31
-#include <linux/sysfs.h>
3222
3323 /*
3424 * We only represent one entry for light or proximity. EC is merging different
....@@ -52,7 +42,7 @@
5242 struct cros_ec_light_prox_state *st = iio_priv(indio_dev);
5343 u16 data = 0;
5444 s64 val64;
55
- int ret = IIO_VAL_INT;
45
+ int ret;
5646 int idx = chan->scan_index;
5747
5848 mutex_lock(&st->core.cmd_lock);
....@@ -60,23 +50,22 @@
6050 switch (mask) {
6151 case IIO_CHAN_INFO_RAW:
6252 if (chan->type == IIO_PROXIMITY) {
63
- if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
64
- (s16 *)&data) < 0) {
65
- ret = -EIO;
53
+ ret = cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
54
+ (s16 *)&data);
55
+ if (ret)
6656 break;
67
- }
6857 *val = data;
58
+ ret = IIO_VAL_INT;
6959 } else {
7060 ret = -EINVAL;
7161 }
7262 break;
7363 case IIO_CHAN_INFO_PROCESSED:
7464 if (chan->type == IIO_LIGHT) {
75
- if (cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
76
- (s16 *)&data) < 0) {
77
- ret = -EIO;
65
+ ret = cros_ec_sensors_read_cmd(indio_dev, 1 << idx,
66
+ (s16 *)&data);
67
+ if (ret)
7868 break;
79
- }
8069 /*
8170 * The data coming from the light sensor is
8271 * pre-processed and represents the ambient light
....@@ -92,15 +81,16 @@
9281 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET;
9382 st->core.param.sensor_offset.flags = 0;
9483
95
- if (cros_ec_motion_send_host_cmd(&st->core, 0)) {
96
- ret = -EIO;
84
+ ret = cros_ec_motion_send_host_cmd(&st->core, 0);
85
+ if (ret)
9786 break;
98
- }
9987
10088 /* Save values */
101
- st->core.calib[0] = st->core.resp->sensor_offset.offset[0];
89
+ st->core.calib[0].offset =
90
+ st->core.resp->sensor_offset.offset[0];
10291
103
- *val = st->core.calib[idx];
92
+ *val = st->core.calib[idx].offset;
93
+ ret = IIO_VAL_INT;
10494 break;
10595 case IIO_CHAN_INFO_CALIBSCALE:
10696 /*
....@@ -111,10 +101,9 @@
111101 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
112102 st->core.param.sensor_range.data = EC_MOTION_SENSE_NO_VALUE;
113103
114
- if (cros_ec_motion_send_host_cmd(&st->core, 0)) {
115
- ret = -EIO;
104
+ ret = cros_ec_motion_send_host_cmd(&st->core, 0);
105
+ if (ret)
116106 break;
117
- }
118107
119108 val64 = st->core.resp->sensor_range.ret;
120109 *val = val64 >> 16;
....@@ -137,28 +126,30 @@
137126 int val, int val2, long mask)
138127 {
139128 struct cros_ec_light_prox_state *st = iio_priv(indio_dev);
140
- int ret = 0;
129
+ int ret;
141130 int idx = chan->scan_index;
142131
143132 mutex_lock(&st->core.cmd_lock);
144133
145134 switch (mask) {
146135 case IIO_CHAN_INFO_CALIBBIAS:
147
- st->core.calib[idx] = val;
136
+ st->core.calib[idx].offset = val;
148137 /* Send to EC for each axis, even if not complete */
149138 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_OFFSET;
150139 st->core.param.sensor_offset.flags = MOTION_SENSE_SET_OFFSET;
151
- st->core.param.sensor_offset.offset[0] = st->core.calib[0];
140
+ st->core.param.sensor_offset.offset[0] =
141
+ st->core.calib[0].offset;
152142 st->core.param.sensor_offset.temp =
153143 EC_MOTION_SENSE_INVALID_CALIB_TEMP;
154
- if (cros_ec_motion_send_host_cmd(&st->core, 0))
155
- ret = -EIO;
144
+ ret = cros_ec_motion_send_host_cmd(&st->core, 0);
156145 break;
157146 case IIO_CHAN_INFO_CALIBSCALE:
158147 st->core.param.cmd = MOTIONSENSE_CMD_SENSOR_RANGE;
159
- st->core.param.sensor_range.data = (val << 16) | (val2 / 100);
160
- if (cros_ec_motion_send_host_cmd(&st->core, 0))
161
- ret = -EIO;
148
+ st->core.curr_range = (val << 16) | (val2 / 100);
149
+ st->core.param.sensor_range.data = st->core.curr_range;
150
+ ret = cros_ec_motion_send_host_cmd(&st->core, 0);
151
+ if (ret == 0)
152
+ st->core.range_updated = true;
162153 break;
163154 default:
164155 ret = cros_ec_sensors_core_write(&st->core, chan, val, val2,
....@@ -174,27 +165,25 @@
174165 static const struct iio_info cros_ec_light_prox_info = {
175166 .read_raw = &cros_ec_light_prox_read,
176167 .write_raw = &cros_ec_light_prox_write,
168
+ .read_avail = &cros_ec_sensors_core_read_avail,
177169 };
178170
179171 static int cros_ec_light_prox_probe(struct platform_device *pdev)
180172 {
181173 struct device *dev = &pdev->dev;
182
- struct cros_ec_dev *ec_dev = dev_get_drvdata(dev->parent);
183174 struct iio_dev *indio_dev;
184175 struct cros_ec_light_prox_state *state;
185176 struct iio_chan_spec *channel;
186177 int ret;
187178
188
- if (!ec_dev || !ec_dev->ec_dev) {
189
- dev_warn(dev, "No CROS EC device found.\n");
190
- return -EINVAL;
191
- }
192
-
193179 indio_dev = devm_iio_device_alloc(dev, sizeof(*state));
194180 if (!indio_dev)
195181 return -ENOMEM;
196182
197
- ret = cros_ec_sensors_core_init(pdev, indio_dev, true);
183
+ ret = cros_ec_sensors_core_init(pdev, indio_dev, true,
184
+ cros_ec_sensors_capture,
185
+ cros_ec_sensors_push_data,
186
+ true);
198187 if (ret)
199188 return ret;
200189
....@@ -206,16 +195,15 @@
206195
207196 /* Common part */
208197 channel->info_mask_shared_by_all =
209
- BIT(IIO_CHAN_INFO_SAMP_FREQ) |
210
- BIT(IIO_CHAN_INFO_FREQUENCY);
198
+ BIT(IIO_CHAN_INFO_SAMP_FREQ);
199
+ channel->info_mask_shared_by_all_available =
200
+ BIT(IIO_CHAN_INFO_SAMP_FREQ);
211201 channel->scan_type.realbits = CROS_EC_SENSOR_BITS;
212202 channel->scan_type.storagebits = CROS_EC_SENSOR_BITS;
213203 channel->scan_type.shift = 0;
214204 channel->scan_index = 0;
215205 channel->ext_info = cros_ec_sensors_ext_info;
216206 channel->scan_type.sign = 'u';
217
-
218
- state->core.calib[0] = 0;
219207
220208 /* Sensor specific */
221209 switch (state->core.type) {
....@@ -252,11 +240,6 @@
252240 indio_dev->num_channels = CROS_EC_LIGHT_PROX_MAX_CHANNELS;
253241
254242 state->core.read_ec_sensors_data = cros_ec_sensors_read_cmd;
255
-
256
- ret = devm_iio_triggered_buffer_setup(dev, indio_dev, NULL,
257
- cros_ec_sensors_capture, NULL);
258
- if (ret)
259
- return ret;
260243
261244 return devm_iio_device_register(dev, indio_dev);
262245 }