Merge tag 'for-linus-20190706' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / iio / magnetometer / st_magn_spi.c
blob0d47070611b187ab5ed6e20322c41491502976d0
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * STMicroelectronics magnetometers driver
5 * Copyright 2012-2013 STMicroelectronics Inc.
7 * Denis Ciocca <denis.ciocca@st.com>
8 */
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/spi/spi.h>
14 #include <linux/iio/iio.h>
16 #include <linux/iio/common/st_sensors.h>
17 #include <linux/iio/common/st_sensors_spi.h>
18 #include "st_magn.h"
20 #ifdef CONFIG_OF
22 * For new single-chip sensors use <device_name> as compatible string.
23 * For old single-chip devices keep <device_name>-magn to maintain
24 * compatibility
25 * For multi-chip devices, use <device_name>-magn to distinguish which
26 * capability is being used
28 static const struct of_device_id st_magn_of_match[] = {
30 .compatible = "st,lis3mdl-magn",
31 .data = LIS3MDL_MAGN_DEV_NAME,
34 .compatible = "st,lsm303agr-magn",
35 .data = LSM303AGR_MAGN_DEV_NAME,
38 .compatible = "st,lis2mdl",
39 .data = LIS2MDL_MAGN_DEV_NAME,
42 .compatible = "st,lsm9ds1-magn",
43 .data = LSM9DS1_MAGN_DEV_NAME,
47 MODULE_DEVICE_TABLE(of, st_magn_of_match);
48 #else
49 #define st_magn_of_match NULL
50 #endif
52 static int st_magn_spi_probe(struct spi_device *spi)
54 struct iio_dev *indio_dev;
55 struct st_sensor_data *mdata;
56 int err;
58 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
59 if (!indio_dev)
60 return -ENOMEM;
62 mdata = iio_priv(indio_dev);
64 st_sensors_of_name_probe(&spi->dev, st_magn_of_match,
65 spi->modalias, sizeof(spi->modalias));
66 st_sensors_spi_configure(indio_dev, spi, mdata);
68 err = st_magn_common_probe(indio_dev);
69 if (err < 0)
70 return err;
72 return 0;
75 static int st_magn_spi_remove(struct spi_device *spi)
77 struct iio_dev *indio_dev = spi_get_drvdata(spi);
78 st_magn_common_remove(indio_dev);
80 return 0;
83 static const struct spi_device_id st_magn_id_table[] = {
84 { LIS3MDL_MAGN_DEV_NAME },
85 { LSM303AGR_MAGN_DEV_NAME },
86 { LIS2MDL_MAGN_DEV_NAME },
87 { LSM9DS1_MAGN_DEV_NAME },
88 {},
90 MODULE_DEVICE_TABLE(spi, st_magn_id_table);
92 static struct spi_driver st_magn_driver = {
93 .driver = {
94 .name = "st-magn-spi",
95 .of_match_table = of_match_ptr(st_magn_of_match),
97 .probe = st_magn_spi_probe,
98 .remove = st_magn_spi_remove,
99 .id_table = st_magn_id_table,
101 module_spi_driver(st_magn_driver);
103 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
104 MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
105 MODULE_LICENSE("GPL v2");