forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-13 9d77db3c730780c8ef5ccd4b66403ff5675cfe4e
kernel/drivers/iio/accel/bma220_spi.c
....@@ -1,20 +1,19 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /**
23 * BMA220 Digital triaxial acceleration sensor driver
34 *
4
- * Copyright (c) 2016, Intel Corporation.
5
- *
6
- * This file is subject to the terms and conditions of version 2 of
7
- * the GNU General Public License. See the file COPYING in the main
8
- * directory of this archive for more details.
5
+ * Copyright (c) 2016,2020 Intel Corporation.
96 */
107
11
-#include <linux/acpi.h>
8
+#include <linux/bits.h>
129 #include <linux/kernel.h>
10
+#include <linux/mod_devicetable.h>
1311 #include <linux/module.h>
12
+#include <linux/spi/spi.h>
13
+
1414 #include <linux/iio/buffer.h>
1515 #include <linux/iio/iio.h>
1616 #include <linux/iio/sysfs.h>
17
-#include <linux/spi/spi.h>
1817 #include <linux/iio/trigger_consumer.h>
1918 #include <linux/iio/triggered_buffer.h>
2019
....@@ -26,14 +25,13 @@
2625 #define BMA220_REG_SUSPEND 0x18
2726
2827 #define BMA220_CHIP_ID 0xDD
29
-#define BMA220_READ_MASK 0x80
30
-#define BMA220_RANGE_MASK 0x03
28
+#define BMA220_READ_MASK BIT(7)
29
+#define BMA220_RANGE_MASK GENMASK(1, 0)
3130 #define BMA220_DATA_SHIFT 2
3231 #define BMA220_SUSPEND_SLEEP 0xFF
3332 #define BMA220_SUSPEND_WAKE 0x00
3433
3534 #define BMA220_DEVICE_NAME "bma220"
36
-#define BMA220_SCALE_AVAILABLE "0.623 1.248 2.491 4.983"
3735
3836 #define BMA220_ACCEL_CHANNEL(index, reg, axis) { \
3937 .type = IIO_ACCEL, \
....@@ -58,19 +56,8 @@
5856 AXIS_Z,
5957 };
6058
61
-static IIO_CONST_ATTR(in_accel_scale_available, BMA220_SCALE_AVAILABLE);
62
-
63
-static struct attribute *bma220_attributes[] = {
64
- &iio_const_attr_in_accel_scale_available.dev_attr.attr,
65
- NULL,
66
-};
67
-
68
-static const struct attribute_group bma220_attribute_group = {
69
- .attrs = bma220_attributes,
70
-};
71
-
72
-static const int bma220_scale_table[][4] = {
73
- {0, 623000}, {1, 248000}, {2, 491000}, {4, 983000}
59
+static const int bma220_scale_table[][2] = {
60
+ {0, 623000}, {1, 248000}, {2, 491000}, {4, 983000},
7461 };
7562
7663 struct bma220_data {
....@@ -189,10 +176,26 @@
189176 return -EINVAL;
190177 }
191178
179
+static int bma220_read_avail(struct iio_dev *indio_dev,
180
+ struct iio_chan_spec const *chan,
181
+ const int **vals, int *type, int *length,
182
+ long mask)
183
+{
184
+ switch (mask) {
185
+ case IIO_CHAN_INFO_SCALE:
186
+ *vals = (int *)bma220_scale_table;
187
+ *type = IIO_VAL_INT_PLUS_MICRO;
188
+ *length = ARRAY_SIZE(bma220_scale_table) * 2;
189
+ return IIO_AVAIL_LIST;
190
+ default:
191
+ return -EINVAL;
192
+ }
193
+}
194
+
192195 static const struct iio_info bma220_info = {
193196 .read_raw = bma220_read_raw,
194197 .write_raw = bma220_write_raw,
195
- .attrs = &bma220_attribute_group,
198
+ .read_avail = bma220_read_avail,
196199 };
197200
198201 static int bma220_init(struct spi_device *spi)
....@@ -205,10 +208,12 @@
205208
206209 /* Make sure the chip is powered on */
207210 ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
211
+ if (ret == BMA220_SUSPEND_WAKE)
212
+ ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
208213 if (ret < 0)
209214 return ret;
210
- else if (ret == BMA220_SUSPEND_WAKE)
211
- return bma220_read_reg(spi, BMA220_REG_SUSPEND);
215
+ if (ret == BMA220_SUSPEND_WAKE)
216
+ return -EBUSY;
212217
213218 return 0;
214219 }
....@@ -219,10 +224,12 @@
219224
220225 /* Make sure the chip is powered off */
221226 ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
227
+ if (ret == BMA220_SUSPEND_SLEEP)
228
+ ret = bma220_read_reg(spi, BMA220_REG_SUSPEND);
222229 if (ret < 0)
223230 return ret;
224
- else if (ret == BMA220_SUSPEND_SLEEP)
225
- return bma220_read_reg(spi, BMA220_REG_SUSPEND);
231
+ if (ret == BMA220_SUSPEND_SLEEP)
232
+ return -EBUSY;
226233
227234 return 0;
228235 }
....@@ -244,7 +251,6 @@
244251 spi_set_drvdata(spi, indio_dev);
245252 mutex_init(&data->lock);
246253
247
- indio_dev->dev.parent = &spi->dev;
248254 indio_dev->info = &bma220_info;
249255 indio_dev->name = BMA220_DEVICE_NAME;
250256 indio_dev->modes = INDIO_DIRECT_MODE;
....@@ -253,7 +259,7 @@
253259 indio_dev->available_scan_masks = bma220_accel_scan_masks;
254260
255261 ret = bma220_init(data->spi_device);
256
- if (ret < 0)
262
+ if (ret)
257263 return ret;
258264
259265 ret = iio_triggered_buffer_setup(indio_dev, iio_pollfunc_store_time,
....@@ -286,30 +292,21 @@
286292 return bma220_deinit(spi);
287293 }
288294
289
-#ifdef CONFIG_PM_SLEEP
290
-static int bma220_suspend(struct device *dev)
295
+static __maybe_unused int bma220_suspend(struct device *dev)
291296 {
292
- struct bma220_data *data =
293
- iio_priv(spi_get_drvdata(to_spi_device(dev)));
297
+ struct bma220_data *data = iio_priv(dev_get_drvdata(dev));
294298
295299 /* The chip can be suspended/woken up by a simple register read. */
296300 return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND);
297301 }
298302
299
-static int bma220_resume(struct device *dev)
303
+static __maybe_unused int bma220_resume(struct device *dev)
300304 {
301
- struct bma220_data *data =
302
- iio_priv(spi_get_drvdata(to_spi_device(dev)));
305
+ struct bma220_data *data = iio_priv(dev_get_drvdata(dev));
303306
304307 return bma220_read_reg(data->spi_device, BMA220_REG_SUSPEND);
305308 }
306
-
307309 static SIMPLE_DEV_PM_OPS(bma220_pm_ops, bma220_suspend, bma220_resume);
308
-
309
-#define BMA220_PM_OPS (&bma220_pm_ops)
310
-#else
311
-#define BMA220_PM_OPS NULL
312
-#endif
313310
314311 static const struct spi_device_id bma220_spi_id[] = {
315312 {"bma220", 0},
....@@ -320,20 +317,18 @@
320317 {"BMA0220", 0},
321318 {}
322319 };
323
-
324320 MODULE_DEVICE_TABLE(spi, bma220_spi_id);
325321
326322 static struct spi_driver bma220_driver = {
327323 .driver = {
328324 .name = "bma220_spi",
329
- .pm = BMA220_PM_OPS,
330
- .acpi_match_table = ACPI_PTR(bma220_acpi_id),
325
+ .pm = &bma220_pm_ops,
326
+ .acpi_match_table = bma220_acpi_id,
331327 },
332328 .probe = bma220_probe,
333329 .remove = bma220_remove,
334330 .id_table = bma220_spi_id,
335331 };
336
-
337332 module_spi_driver(bma220_driver);
338333
339334 MODULE_AUTHOR("Tiberiu Breana <tiberiu.a.breana@intel.com>");