1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * STMicroelectronics magnetometers driver
4 *
5 * Copyright 2012-2013 STMicroelectronics Inc.
6 *
7 * Denis Ciocca <denis.ciocca@st.com>
8 */
9
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/mod_devicetable.h>
13 #include <linux/spi/spi.h>
14 #include <linux/iio/iio.h>
15
16 #include <linux/iio/common/st_sensors.h>
17 #include <linux/iio/common/st_sensors_spi.h>
18 #include "st_magn.h"
19
20 /*
21 * For new single-chip sensors use <device_name> as compatible string.
22 * For old single-chip devices keep <device_name>-magn to maintain
23 * compatibility
24 * For multi-chip devices, use <device_name>-magn to distinguish which
25 * capability is being used
26 */
27 static const struct of_device_id st_magn_of_match[] = {
28 {
29 .compatible = "st,lis3mdl-magn",
30 .data = LIS3MDL_MAGN_DEV_NAME,
31 },
32 {
33 .compatible = "st,lsm303agr-magn",
34 .data = LSM303AGR_MAGN_DEV_NAME,
35 },
36 {
37 .compatible = "st,lis2mdl",
38 .data = LIS2MDL_MAGN_DEV_NAME,
39 },
40 {
41 .compatible = "st,lsm9ds1-magn",
42 .data = LSM9DS1_MAGN_DEV_NAME,
43 },
44 {
45 .compatible = "st,iis2mdc",
46 .data = IIS2MDC_MAGN_DEV_NAME,
47 },
48 {}
49 };
50 MODULE_DEVICE_TABLE(of, st_magn_of_match);
51
st_magn_spi_probe(struct spi_device * spi)52 static int st_magn_spi_probe(struct spi_device *spi)
53 {
54 const struct st_sensor_settings *settings;
55 struct st_sensor_data *mdata;
56 struct iio_dev *indio_dev;
57 int err;
58
59 st_sensors_dev_name_probe(&spi->dev, spi->modalias, sizeof(spi->modalias));
60
61 settings = st_magn_get_settings(spi->modalias);
62 if (!settings) {
63 dev_err(&spi->dev, "device name %s not recognized.\n",
64 spi->modalias);
65 return -ENODEV;
66 }
67
68 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
69 if (!indio_dev)
70 return -ENOMEM;
71
72 mdata = iio_priv(indio_dev);
73 mdata->sensor_settings = (struct st_sensor_settings *)settings;
74
75 err = st_sensors_spi_configure(indio_dev, spi);
76 if (err < 0)
77 return err;
78
79 err = st_sensors_power_enable(indio_dev);
80 if (err)
81 return err;
82
83 err = st_magn_common_probe(indio_dev);
84 if (err < 0)
85 goto st_magn_power_off;
86
87 return 0;
88
89 st_magn_power_off:
90 st_sensors_power_disable(indio_dev);
91
92 return err;
93 }
94
st_magn_spi_remove(struct spi_device * spi)95 static int st_magn_spi_remove(struct spi_device *spi)
96 {
97 struct iio_dev *indio_dev = spi_get_drvdata(spi);
98
99 st_magn_common_remove(indio_dev);
100
101 st_sensors_power_disable(indio_dev);
102
103 return 0;
104 }
105
106 static const struct spi_device_id st_magn_id_table[] = {
107 { LIS3MDL_MAGN_DEV_NAME },
108 { LSM303AGR_MAGN_DEV_NAME },
109 { LIS2MDL_MAGN_DEV_NAME },
110 { LSM9DS1_MAGN_DEV_NAME },
111 { IIS2MDC_MAGN_DEV_NAME },
112 {},
113 };
114 MODULE_DEVICE_TABLE(spi, st_magn_id_table);
115
116 static struct spi_driver st_magn_driver = {
117 .driver = {
118 .name = "st-magn-spi",
119 .of_match_table = st_magn_of_match,
120 },
121 .probe = st_magn_spi_probe,
122 .remove = st_magn_spi_remove,
123 .id_table = st_magn_id_table,
124 };
125 module_spi_driver(st_magn_driver);
126
127 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
128 MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
129 MODULE_LICENSE("GPL v2");
130