.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * A sensor driver for the magnetometer AK8975. |
---|
3 | 4 | * |
---|
4 | 5 | * Magnetic compass sensor driver for monitoring magnetic flux information. |
---|
5 | 6 | * |
---|
6 | 7 | * 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. |
---|
21 | 8 | */ |
---|
22 | 9 | |
---|
23 | 10 | #include <linux/module.h> |
---|
| 11 | +#include <linux/mod_devicetable.h> |
---|
24 | 12 | #include <linux/kernel.h> |
---|
25 | 13 | #include <linux/slab.h> |
---|
26 | 14 | #include <linux/i2c.h> |
---|
.. | .. |
---|
29 | 17 | #include <linux/mutex.h> |
---|
30 | 18 | #include <linux/delay.h> |
---|
31 | 19 | #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> |
---|
35 | 21 | #include <linux/regulator/consumer.h> |
---|
36 | 22 | #include <linux/pm_runtime.h> |
---|
37 | 23 | |
---|
.. | .. |
---|
41 | 27 | #include <linux/iio/trigger.h> |
---|
42 | 28 | #include <linux/iio/trigger_consumer.h> |
---|
43 | 29 | #include <linux/iio/triggered_buffer.h> |
---|
44 | | - |
---|
45 | | -#include <linux/iio/magnetometer/ak8975.h> |
---|
46 | 30 | |
---|
47 | 31 | /* |
---|
48 | 32 | * Register definitions, as well as various shifts and masks to get at the |
---|
.. | .. |
---|
219 | 203 | |
---|
220 | 204 | /* Compatible Asahi Kasei Compass parts */ |
---|
221 | 205 | enum asahi_compass_chipset { |
---|
| 206 | + AKXXXX = 0, |
---|
222 | 207 | AK8975, |
---|
223 | 208 | AK8963, |
---|
224 | 209 | AK09911, |
---|
225 | 210 | AK09912, |
---|
226 | | - AK_MAX_TYPE |
---|
227 | 211 | }; |
---|
228 | 212 | |
---|
229 | 213 | enum ak_ctrl_reg_addr { |
---|
.. | .. |
---|
261 | 245 | u8 data_regs[3]; |
---|
262 | 246 | }; |
---|
263 | 247 | |
---|
264 | | -static const struct ak_def ak_def_array[AK_MAX_TYPE] = { |
---|
| 248 | +static const struct ak_def ak_def_array[] = { |
---|
265 | 249 | { |
---|
266 | 250 | .type = AK8975, |
---|
267 | 251 | .raw_to_gauss = ak8975_raw_to_gauss, |
---|
.. | .. |
---|
373 | 357 | struct mutex lock; |
---|
374 | 358 | u8 asa[3]; |
---|
375 | 359 | long raw_to_gauss[3]; |
---|
376 | | - int eoc_gpio; |
---|
| 360 | + struct gpio_desc *eoc_gpiod; |
---|
| 361 | + struct gpio_desc *reset_gpiod; |
---|
377 | 362 | int eoc_irq; |
---|
378 | 363 | wait_queue_head_t data_ready_queue; |
---|
379 | 364 | unsigned long flags; |
---|
.. | .. |
---|
404 | 389 | if (ret) { |
---|
405 | 390 | dev_warn(&data->client->dev, |
---|
406 | 391 | "Failed to enable specified Vid supply\n"); |
---|
| 392 | + regulator_disable(data->vdd); |
---|
407 | 393 | return ret; |
---|
408 | 394 | } |
---|
| 395 | + |
---|
| 396 | + gpiod_set_value_cansleep(data->reset_gpiod, 0); |
---|
| 397 | + |
---|
409 | 398 | /* |
---|
410 | | - * According to the datasheet the power supply rise time i 200us |
---|
| 399 | + * According to the datasheet the power supply rise time is 200us |
---|
411 | 400 | * 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. |
---|
413 | 402 | */ |
---|
414 | 403 | usleep_range(500, 1000); |
---|
415 | 404 | return 0; |
---|
.. | .. |
---|
418 | 407 | /* Disable attached power regulator if any. */ |
---|
419 | 408 | static void ak8975_power_off(const struct ak8975_data *data) |
---|
420 | 409 | { |
---|
| 410 | + gpiod_set_value_cansleep(data->reset_gpiod, 1); |
---|
| 411 | + |
---|
421 | 412 | regulator_disable(data->vid); |
---|
422 | 413 | regulator_disable(data->vdd); |
---|
423 | 414 | } |
---|
.. | .. |
---|
517 | 508 | if (client->irq) |
---|
518 | 509 | irq = client->irq; |
---|
519 | 510 | else |
---|
520 | | - irq = gpio_to_irq(data->eoc_gpio); |
---|
| 511 | + irq = gpiod_to_irq(data->eoc_gpiod); |
---|
521 | 512 | |
---|
522 | 513 | rc = devm_request_irq(&client->dev, irq, ak8975_irq_handler, |
---|
523 | 514 | IRQF_TRIGGER_RISING | IRQF_ONESHOT, |
---|
524 | 515 | dev_name(&client->dev), data); |
---|
525 | 516 | 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); |
---|
529 | 518 | return rc; |
---|
530 | 519 | } |
---|
531 | 520 | |
---|
.. | .. |
---|
568 | 557 | return ret; |
---|
569 | 558 | } |
---|
570 | 559 | |
---|
571 | | - if (data->eoc_gpio > 0 || client->irq > 0) { |
---|
| 560 | + if (data->eoc_gpiod || client->irq > 0) { |
---|
572 | 561 | ret = ak8975_setup_irq(data); |
---|
573 | 562 | if (ret < 0) { |
---|
574 | 563 | dev_err(&client->dev, |
---|
.. | .. |
---|
593 | 582 | /* Wait for the conversion to complete. */ |
---|
594 | 583 | while (timeout_ms) { |
---|
595 | 584 | msleep(AK8975_CONVERSION_DONE_POLL_TIME); |
---|
596 | | - if (gpio_get_value(data->eoc_gpio)) |
---|
| 585 | + if (gpiod_get_value(data->eoc_gpiod)) |
---|
597 | 586 | break; |
---|
598 | 587 | timeout_ms -= AK8975_CONVERSION_DONE_POLL_TIME; |
---|
599 | 588 | } |
---|
.. | .. |
---|
665 | 654 | /* Wait for the conversion to complete. */ |
---|
666 | 655 | if (data->eoc_irq) |
---|
667 | 656 | ret = wait_conversion_complete_interrupt(data); |
---|
668 | | - else if (gpio_is_valid(data->eoc_gpio)) |
---|
| 657 | + else if (data->eoc_gpiod) |
---|
669 | 658 | ret = wait_conversion_complete_gpio(data); |
---|
670 | 659 | else |
---|
671 | 660 | ret = wait_conversion_complete_polled(data); |
---|
.. | .. |
---|
752 | 741 | ak8975_get_mount_matrix(const struct iio_dev *indio_dev, |
---|
753 | 742 | const struct iio_chan_spec *chan) |
---|
754 | 743 | { |
---|
755 | | - return &((struct ak8975_data *)iio_priv(indio_dev))->orientation; |
---|
| 744 | + struct ak8975_data *data = iio_priv(indio_dev); |
---|
| 745 | + |
---|
| 746 | + return &data->orientation; |
---|
756 | 747 | } |
---|
757 | 748 | |
---|
758 | 749 | static const struct iio_chan_spec_ext_info ak8975_ext_info[] = { |
---|
759 | 750 | IIO_MOUNT_MATRIX(IIO_SHARED_BY_DIR, ak8975_get_mount_matrix), |
---|
760 | | - { }, |
---|
| 751 | + { } |
---|
761 | 752 | }; |
---|
762 | 753 | |
---|
763 | 754 | #define AK8975_CHANNEL(axis, index) \ |
---|
.. | .. |
---|
789 | 780 | .read_raw = &ak8975_read_raw, |
---|
790 | 781 | }; |
---|
791 | 782 | |
---|
792 | | -#ifdef CONFIG_ACPI |
---|
793 | 783 | static const struct acpi_device_id ak_acpi_match[] = { |
---|
794 | 784 | {"AK8975", AK8975}, |
---|
795 | 785 | {"AK8963", AK8963}, |
---|
796 | 786 | {"INVN6500", AK8963}, |
---|
797 | 787 | {"AK009911", AK09911}, |
---|
798 | 788 | {"AK09911", AK09911}, |
---|
| 789 | + {"AKM9911", AK09911}, |
---|
799 | 790 | {"AK09912", AK09912}, |
---|
800 | | - { }, |
---|
| 791 | + { } |
---|
801 | 792 | }; |
---|
802 | 793 | 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 | | -} |
---|
817 | 794 | |
---|
818 | 795 | static void ak8975_fill_buffer(struct iio_dev *indio_dev) |
---|
819 | 796 | { |
---|
.. | .. |
---|
872 | 849 | { |
---|
873 | 850 | struct ak8975_data *data; |
---|
874 | 851 | 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; |
---|
876 | 856 | int err; |
---|
| 857 | + enum asahi_compass_chipset chipset; |
---|
877 | 858 | 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); |
---|
881 | 859 | |
---|
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"); |
---|
889 | 870 | |
---|
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); |
---|
905 | 880 | |
---|
906 | 881 | /* Register with IIO */ |
---|
907 | 882 | indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*data)); |
---|
.. | .. |
---|
912 | 887 | i2c_set_clientdata(client, indio_dev); |
---|
913 | 888 | |
---|
914 | 889 | data->client = client; |
---|
915 | | - data->eoc_gpio = eoc_gpio; |
---|
| 890 | + data->eoc_gpiod = eoc_gpiod; |
---|
| 891 | + data->reset_gpiod = reset_gpiod; |
---|
916 | 892 | data->eoc_irq = 0; |
---|
917 | 893 | |
---|
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; |
---|
926 | 897 | |
---|
927 | 898 | /* 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) { |
---|
929 | 904 | chipset = (enum asahi_compass_chipset)(id->driver_data); |
---|
930 | 905 | 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; |
---|
935 | 906 | } else |
---|
936 | 907 | return -ENOSYS; |
---|
937 | 908 | |
---|
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)) { |
---|
939 | 914 | dev_err(&client->dev, "AKM device type unsupported: %d\n", |
---|
940 | 915 | chipset); |
---|
941 | 916 | return -ENODEV; |
---|
942 | 917 | } |
---|
943 | 918 | |
---|
944 | | - data->def = &ak_def_array[chipset]; |
---|
| 919 | + data->def = &ak_def_array[i]; |
---|
945 | 920 | |
---|
946 | 921 | /* Fetch the regulators */ |
---|
947 | 922 | data->vdd = devm_regulator_get(&client->dev, "vdd"); |
---|
.. | .. |
---|
970 | 945 | } |
---|
971 | 946 | |
---|
972 | 947 | mutex_init(&data->lock); |
---|
973 | | - indio_dev->dev.parent = &client->dev; |
---|
974 | 948 | indio_dev->channels = ak8975_channels; |
---|
975 | 949 | indio_dev->num_channels = ARRAY_SIZE(ak8975_channels); |
---|
976 | 950 | indio_dev->info = &ak8975_info; |
---|
.. | .. |
---|
1106 | 1080 | .driver = { |
---|
1107 | 1081 | .name = "ak8975", |
---|
1108 | 1082 | .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, |
---|
1111 | 1085 | }, |
---|
1112 | 1086 | .probe = ak8975_probe, |
---|
1113 | 1087 | .remove = ak8975_remove, |
---|