forked from ~ljy/RK356X_SDK_RELEASE

hc
2024-05-10 37f49e37ab4cb5d0bc4c60eb5c6d4dd57db767bb
kernel/drivers/iio/magnetometer/bmc150_magn.c
....@@ -1,3 +1,4 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Bosch BMC150 three-axis magnetic field sensor driver
34 *
....@@ -6,15 +7,6 @@
67 * This code is based on bmm050_api.c authored by contact@bosch.sensortec.com:
78 *
89 * (C) Copyright 2011~2014 Bosch Sensortec GmbH All Rights Reserved
9
- *
10
- * This program is free software; you can redistribute it and/or modify it
11
- * under the terms and conditions of the GNU General Public License,
12
- * version 2, as published by the Free Software Foundation.
13
- *
14
- * This program is distributed in the hope it will be useful, but WITHOUT
15
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17
- * more details.
1810 */
1911
2012 #include <linux/module.h>
....@@ -143,8 +135,12 @@
143135 */
144136 struct mutex mutex;
145137 struct regmap *regmap;
146
- /* 4 x 32 bits for x, y z, 4 bytes align, 64 bits timestamp */
147
- s32 buffer[6];
138
+ struct iio_mount_matrix orientation;
139
+ /* Ensure timestamp is naturally aligned */
140
+ struct {
141
+ s32 chans[3];
142
+ s64 timestamp __aligned(8);
143
+ } scan;
148144 struct iio_trigger *dready_trig;
149145 bool dready_trigger_on;
150146 int max_odr;
....@@ -267,7 +263,7 @@
267263 int ret;
268264
269265 if (on) {
270
- ret = pm_runtime_get_sync(data->dev);
266
+ ret = pm_runtime_resume_and_get(data->dev);
271267 } else {
272268 pm_runtime_mark_last_busy(data->dev);
273269 ret = pm_runtime_put_autosuspend(data->dev);
....@@ -276,9 +272,6 @@
276272 if (ret < 0) {
277273 dev_err(data->dev,
278274 "failed to change power state to %d\n", on);
279
- if (on)
280
- pm_runtime_put_noidle(data->dev);
281
-
282275 return ret;
283276 }
284277 #endif
....@@ -612,6 +605,20 @@
612605 return len;
613606 }
614607
608
+static const struct iio_mount_matrix *
609
+bmc150_magn_get_mount_matrix(const struct iio_dev *indio_dev,
610
+ const struct iio_chan_spec *chan)
611
+{
612
+ struct bmc150_magn_data *data = iio_priv(indio_dev);
613
+
614
+ return &data->orientation;
615
+}
616
+
617
+static const struct iio_chan_spec_ext_info bmc150_magn_ext_info[] = {
618
+ IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, bmc150_magn_get_mount_matrix),
619
+ { }
620
+};
621
+
615622 static IIO_DEV_ATTR_SAMP_FREQ_AVAIL(bmc150_magn_show_samp_freq_avail);
616623
617624 static struct attribute *bmc150_magn_attributes[] = {
....@@ -638,6 +645,7 @@
638645 .storagebits = 32, \
639646 .endianness = IIO_LE \
640647 }, \
648
+ .ext_info = bmc150_magn_ext_info, \
641649 }
642650
643651 static const struct iio_chan_spec bmc150_magn_channels[] = {
....@@ -665,11 +673,11 @@
665673 int ret;
666674
667675 mutex_lock(&data->mutex);
668
- ret = bmc150_magn_read_xyz(data, data->buffer);
676
+ ret = bmc150_magn_read_xyz(data, data->scan.chans);
669677 if (ret < 0)
670678 goto err;
671679
672
- iio_push_to_buffers_with_timestamp(indio_dev, data->buffer,
680
+ iio_push_to_buffers_with_timestamp(indio_dev, &data->scan,
673681 pf->timestamp);
674682
675683 err:
....@@ -828,8 +836,6 @@
828836
829837 static const struct iio_buffer_setup_ops bmc150_magn_buffer_setup_ops = {
830838 .preenable = bmc150_magn_buffer_preenable,
831
- .postenable = iio_triggered_buffer_postenable,
832
- .predisable = iio_triggered_buffer_predisable,
833839 .postdisable = bmc150_magn_buffer_postdisable,
834840 };
835841
....@@ -861,6 +867,11 @@
861867 data->irq = irq;
862868 data->dev = dev;
863869
870
+ ret = iio_read_mount_matrix(dev, "mount-matrix",
871
+ &data->orientation);
872
+ if (ret)
873
+ return ret;
874
+
864875 if (!name && ACPI_HANDLE(dev))
865876 name = bmc150_magn_match_acpi_device(dev);
866877
....@@ -870,7 +881,6 @@
870881 if (ret < 0)
871882 return ret;
872883
873
- indio_dev->dev.parent = dev;
874884 indio_dev->channels = bmc150_magn_channels;
875885 indio_dev->num_channels = ARRAY_SIZE(bmc150_magn_channels);
876886 indio_dev->available_scan_masks = bmc150_magn_scan_masks;
....@@ -931,12 +941,15 @@
931941 ret = iio_device_register(indio_dev);
932942 if (ret < 0) {
933943 dev_err(dev, "unable to register iio device\n");
934
- goto err_buffer_cleanup;
944
+ goto err_pm_cleanup;
935945 }
936946
937947 dev_dbg(dev, "Registered device %s\n", name);
938948 return 0;
939949
950
+err_pm_cleanup:
951
+ pm_runtime_dont_use_autosuspend(dev);
952
+ pm_runtime_disable(dev);
940953 err_buffer_cleanup:
941954 iio_triggered_buffer_cleanup(indio_dev);
942955 err_free_irq:
....@@ -960,7 +973,6 @@
960973
961974 pm_runtime_disable(dev);
962975 pm_runtime_set_suspended(dev);
963
- pm_runtime_put_noidle(dev);
964976
965977 iio_triggered_buffer_cleanup(indio_dev);
966978