hc
2023-12-11 d2ccde1c8e90d38cee87a1b0309ad2827f3fd30d
kernel/drivers/iio/magnetometer/ak8975.c
....@@ -1,26 +1,14 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * A sensor driver for the magnetometer AK8975.
34 *
45 * Magnetic compass sensor driver for monitoring magnetic flux information.
56 *
67 * Copyright (c) 2010, NVIDIA Corporation.
7
- *
8
- * This program is free software; you can redistribute it and/or modify
9
- * it under the terms of the GNU General Public License as published by
10
- * the Free Software Foundation; either version 2 of the License, or
11
- * (at your option) any later version.
12
- *
13
- * This program is distributed in the hope that it will be useful, but WITHOUT
14
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16
- * more details.
17
- *
18
- * You should have received a copy of the GNU General Public License along
19
- * with this program; if not, write to the Free Software Foundation, Inc.,
20
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
218 */
229
2310 #include <linux/module.h>
11
+#include <linux/mod_devicetable.h>
2412 #include <linux/kernel.h>
2513 #include <linux/slab.h>
2614 #include <linux/i2c.h>
....@@ -29,9 +17,7 @@
2917 #include <linux/mutex.h>
3018 #include <linux/delay.h>
3119 #include <linux/bitops.h>
32
-#include <linux/gpio.h>
33
-#include <linux/of_gpio.h>
34
-#include <linux/acpi.h>
20
+#include <linux/gpio/consumer.h>
3521 #include <linux/regulator/consumer.h>
3622 #include <linux/pm_runtime.h>
3723
....@@ -41,8 +27,6 @@
4127 #include <linux/iio/trigger.h>
4228 #include <linux/iio/trigger_consumer.h>
4329 #include <linux/iio/triggered_buffer.h>
44
-
45
-#include <linux/iio/magnetometer/ak8975.h>
4630
4731 /*
4832 * Register definitions, as well as various shifts and masks to get at the
....@@ -219,11 +203,11 @@
219203
220204 /* Compatible Asahi Kasei Compass parts */
221205 enum asahi_compass_chipset {
206
+ AKXXXX = 0,
222207 AK8975,
223208 AK8963,
224209 AK09911,
225210 AK09912,
226
- AK_MAX_TYPE
227211 };
228212
229213 enum ak_ctrl_reg_addr {
....@@ -261,7 +245,7 @@
261245 u8 data_regs[3];
262246 };
263247
264
-static const struct ak_def ak_def_array[AK_MAX_TYPE] = {
248
+static const struct ak_def ak_def_array[] = {
265249 {
266250 .type = AK8975,
267251 .raw_to_gauss = ak8975_raw_to_gauss,
....@@ -373,7 +357,8 @@
373357 struct mutex lock;
374358 u8 asa[3];
375359 long raw_to_gauss[3];
376
- int eoc_gpio;
360
+ struct gpio_desc *eoc_gpiod;
361
+ struct gpio_desc *reset_gpiod;
377362 int eoc_irq;
378363 wait_queue_head_t data_ready_queue;
379364 unsigned long flags;
....@@ -404,12 +389,16 @@
404389 if (ret) {
405390 dev_warn(&data->client->dev,
406391 "Failed to enable specified Vid supply\n");
392
+ regulator_disable(data->vdd);
407393 return ret;
408394 }
395
+
396
+ gpiod_set_value_cansleep(data->reset_gpiod, 0);
397
+
409398 /*
410
- * According to the datasheet the power supply rise time i 200us
399
+ * According to the datasheet the power supply rise time is 200us
411400 * and the minimum wait time before mode setting is 100us, in
412
- * total 300 us. Add some margin and say minimum 500us here.
401
+ * total 300us. Add some margin and say minimum 500us here.
413402 */
414403 usleep_range(500, 1000);
415404 return 0;
....@@ -418,6 +407,8 @@
418407 /* Disable attached power regulator if any. */
419408 static void ak8975_power_off(const struct ak8975_data *data)
420409 {
410
+ gpiod_set_value_cansleep(data->reset_gpiod, 1);
411
+
421412 regulator_disable(data->vid);
422413 regulator_disable(data->vdd);
423414 }
....@@ -517,15 +508,13 @@
517508 if (client->irq)
518509 irq = client->irq;
519510 else
520
- irq = gpio_to_irq(data->eoc_gpio);
511
+ irq = gpiod_to_irq(data->eoc_gpiod);
521512
522513 rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler,
523514 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
524515 dev_name(&client->dev), data);
525516 if (rc < 0) {
526
- dev_err(&client->dev,
527
- "irq %d request failed, (gpio %d): %d\n",
528
- irq, data->eoc_gpio, rc);
517
+ dev_err(&client->dev, "irq %d request failed: %d\n", irq, rc);
529518 return rc;
530519 }
531520
....@@ -568,7 +557,7 @@
568557 return ret;
569558 }
570559
571
- if (data->eoc_gpio > 0 || client->irq > 0) {
560
+ if (data->eoc_gpiod || client->irq > 0) {
572561 ret = ak8975_setup_irq(data);
573562 if (ret < 0) {
574563 dev_err(&client->dev,
....@@ -593,7 +582,7 @@
593582 /* Wait for the conversion to complete. */
594583 while (timeout_ms) {
595584 msleep(AK8975_CONVERSION_DONE_POLL_TIME);
596
- if (gpio_get_value(data->eoc_gpio))
585
+ if (gpiod_get_value(data->eoc_gpiod))
597586 break;
598587 timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME;
599588 }
....@@ -665,7 +654,7 @@
665654 /* Wait for the conversion to complete. */
666655 if (data->eoc_irq)
667656 ret = wait_conversion_complete_interrupt(data);
668
- else if (gpio_is_valid(data->eoc_gpio))
657
+ else if (data->eoc_gpiod)
669658 ret = wait_conversion_complete_gpio(data);
670659 else
671660 ret = wait_conversion_complete_polled(data);
....@@ -752,12 +741,14 @@
752741 ak8975_get_mount_matrix(const struct iio_dev *indio_dev,
753742 const struct iio_chan_spec *chan)
754743 {
755
- return &((struct ak8975_data *)iio_priv(indio_dev))->orientation;
744
+ struct ak8975_data *data = iio_priv(indio_dev);
745
+
746
+ return &data->orientation;
756747 }
757748
758749 static const struct iio_chan_spec_ext_info ak8975_ext_info[] = {
759750 IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix),
760
- { },
751
+ { }
761752 };
762753
763754 #define AK8975_CHANNEL(axis, index) \
....@@ -789,31 +780,17 @@
789780 .read_raw = &ak8975_read_raw,
790781 };
791782
792
-#ifdef CONFIG_ACPI
793783 static const struct acpi_device_id ak_acpi_match[] = {
794784 {"AK8975", AK8975},
795785 {"AK8963", AK8963},
796786 {"INVN6500", AK8963},
797787 {"AK009911", AK09911},
798788 {"AK09911", AK09911},
789
+ {"AKM9911", AK09911},
799790 {"AK09912", AK09912},
800
- { },
791
+ { }
801792 };
802793 MODULE_DEVICE_TABLE(acpi, ak_acpi_match);
803
-#endif
804
-
805
-static const char *ak8975_match_acpi_device(struct device *dev,
806
- enum asahi_compass_chipset *chipset)
807
-{
808
- const struct acpi_device_id *id;
809
-
810
- id = acpi_match_device(dev->driver->acpi_match_table, dev);
811
- if (!id)
812
- return NULL;
813
- *chipset = (int)id->driver_data;
814
-
815
- return dev_name(dev);
816
-}
817794
818795 static void ak8975_fill_buffer(struct iio_dev *indio_dev)
819796 {
....@@ -872,36 +849,34 @@
872849 {
873850 struct ak8975_data *data;
874851 struct iio_dev *indio_dev;
875
- int eoc_gpio;
852
+ struct gpio_desc *eoc_gpiod;
853
+ struct gpio_desc *reset_gpiod;
854
+ const void *match;
855
+ unsigned int i;
876856 int err;
857
+ enum asahi_compass_chipset chipset;
877858 const char *name = NULL;
878
- enum asahi_compass_chipset chipset = AK_MAX_TYPE;
879
- const struct ak8975_platform_data *pdata =
880
- dev_get_platdata(&client->dev);
881859
882
- /* Grab and set up the supplied GPIO. */
883
- if (pdata)
884
- eoc_gpio = pdata->eoc_gpio;
885
- else if (client->dev.of_node)
886
- eoc_gpio = of_get_gpio(client->dev.of_node, 0);
887
- else
888
- eoc_gpio = -1;
860
+ /*
861
+ * Grab and set up the supplied GPIO.
862
+ * We may not have a GPIO based IRQ to scan, that is fine, we will
863
+ * poll if so.
864
+ */
865
+ eoc_gpiod = devm_gpiod_get_optional(&client->dev, NULL, GPIOD_IN);
866
+ if (IS_ERR(eoc_gpiod))
867
+ return PTR_ERR(eoc_gpiod);
868
+ if (eoc_gpiod)
869
+ gpiod_set_consumer_name(eoc_gpiod, "ak_8975");
889870
890
- if (eoc_gpio == -EPROBE_DEFER)
891
- return -EPROBE_DEFER;
892
-
893
- /* We may not have a GPIO based IRQ to scan, that is fine, we will
894
- poll if so */
895
- if (gpio_is_valid(eoc_gpio)) {
896
- err = devm_gpio_request_one(&client->dev, eoc_gpio,
897
- GPIOF_IN, "ak_8975");
898
- if (err < 0) {
899
- dev_err(&client->dev,
900
- "failed to request GPIO %d, error %d\n",
901
- eoc_gpio, err);
902
- return err;
903
- }
904
- }
871
+ /*
872
+ * According to AK09911 datasheet, if reset GPIO is provided then
873
+ * deassert reset on ak8975_power_on() and assert reset on
874
+ * ak8975_power_off().
875
+ */
876
+ reset_gpiod = devm_gpiod_get_optional(&client->dev,
877
+ "reset", GPIOD_OUT_HIGH);
878
+ if (IS_ERR(reset_gpiod))
879
+ return PTR_ERR(reset_gpiod);
905880
906881 /* Register with IIO */
907882 indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data));
....@@ -912,36 +887,36 @@
912887 i2c_set_clientdata(client, indio_dev);
913888
914889 data->client = client;
915
- data->eoc_gpio = eoc_gpio;
890
+ data->eoc_gpiod = eoc_gpiod;
891
+ data->reset_gpiod = reset_gpiod;
916892 data->eoc_irq = 0;
917893
918
- if (!pdata) {
919
- err = of_iio_read_mount_matrix(&client->dev,
920
- "mount-matrix",
921
- &data->orientation);
922
- if (err)
923
- return err;
924
- } else
925
- data->orientation = pdata->orientation;
894
+ err = iio_read_mount_matrix(&client->dev, "mount-matrix", &data->orientation);
895
+ if (err)
896
+ return err;
926897
927898 /* id will be NULL when enumerated via ACPI */
928
- if (id) {
899
+ match = device_get_match_data(&client->dev);
900
+ if (match) {
901
+ chipset = (enum asahi_compass_chipset)(match);
902
+ name = dev_name(&client->dev);
903
+ } else if (id) {
929904 chipset = (enum asahi_compass_chipset)(id->driver_data);
930905 name = id->name;
931
- } else if (ACPI_HANDLE(&client->dev)) {
932
- name = ak8975_match_acpi_device(&client->dev, &chipset);
933
- if (!name)
934
- return -ENODEV;
935906 } else
936907 return -ENOSYS;
937908
938
- if (chipset >= AK_MAX_TYPE) {
909
+ for (i = 0; i < ARRAY_SIZE(ak_def_array); i++)
910
+ if (ak_def_array[i].type == chipset)
911
+ break;
912
+
913
+ if (i == ARRAY_SIZE(ak_def_array)) {
939914 dev_err(&client->dev, "AKM device type unsupported: %d\n",
940915 chipset);
941916 return -ENODEV;
942917 }
943918
944
- data->def = &ak_def_array[chipset];
919
+ data->def = &ak_def_array[i];
945920
946921 /* Fetch the regulators */
947922 data->vdd = devm_regulator_get(&client->dev, "vdd");
....@@ -970,7 +945,6 @@
970945 }
971946
972947 mutex_init(&data->lock);
973
- indio_dev->dev.parent = &client->dev;
974948 indio_dev->channels = ak8975_channels;
975949 indio_dev->num_channels = ARRAY_SIZE(ak8975_channels);
976950 indio_dev->info = &ak8975_info;
....@@ -1106,8 +1080,8 @@
11061080 .driver = {
11071081 .name = "ak8975",
11081082 .pm = &ak8975_dev_pm_ops,
1109
- .of_match_table = of_match_ptr(ak8975_of_match),
1110
- .acpi_match_table = ACPI_PTR(ak_acpi_match),
1083
+ .of_match_table = ak8975_of_match,
1084
+ .acpi_match_table = ak_acpi_match,
11111085 },
11121086 .probe = ak8975_probe,
11131087 .remove = ak8975_remove,