xref: /linux/drivers/iio/magnetometer/st_magn_spi.c (revision 49e41801b335f64610bbfd23e8f2bbaf34d46276)
1 /*
2  * STMicroelectronics magnetometers driver
3  *
4  * Copyright 2012-2013 STMicroelectronics Inc.
5  *
6  * Denis Ciocca <denis.ciocca@st.com>
7  *
8  * Licensed under the GPL-2.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
15 #include <linux/iio/iio.h>
16 
17 #include <linux/iio/common/st_sensors.h>
18 #include <linux/iio/common/st_sensors_spi.h>
19 #include "st_magn.h"
20 
21 #ifdef CONFIG_OF
22 /*
23  * For new single-chip sensors use <device_name> as compatible string.
24  * For old single-chip devices keep <device_name>-magn to maintain
25  * compatibility
26  * For multi-chip devices, use <device_name>-magn to distinguish which
27  * capability is being used
28  */
29 static const struct of_device_id st_magn_of_match[] = {
30 	{
31 		.compatible = "st,lis3mdl-magn",
32 		.data = LIS3MDL_MAGN_DEV_NAME,
33 	},
34 	{
35 		.compatible = "st,lsm303agr-magn",
36 		.data = LSM303AGR_MAGN_DEV_NAME,
37 	},
38 	{
39 		.compatible = "st,lis2mdl",
40 		.data = LIS2MDL_MAGN_DEV_NAME,
41 	},
42 	{
43 		.compatible = "st,lsm9ds1-magn",
44 		.data = LSM9DS1_MAGN_DEV_NAME,
45 	},
46 	{}
47 };
48 MODULE_DEVICE_TABLE(of, st_magn_of_match);
49 #else
50 #define st_magn_of_match	NULL
51 #endif
52 
53 static int st_magn_spi_probe(struct spi_device *spi)
54 {
55 	struct iio_dev *indio_dev;
56 	struct st_sensor_data *mdata;
57 	int err;
58 
59 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
60 	if (!indio_dev)
61 		return -ENOMEM;
62 
63 	mdata = iio_priv(indio_dev);
64 
65 	st_sensors_of_name_probe(&spi->dev, st_magn_of_match,
66 				 spi->modalias, sizeof(spi->modalias));
67 	st_sensors_spi_configure(indio_dev, spi, mdata);
68 
69 	err = st_magn_common_probe(indio_dev);
70 	if (err < 0)
71 		return err;
72 
73 	return 0;
74 }
75 
76 static int st_magn_spi_remove(struct spi_device *spi)
77 {
78 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
79 	st_magn_common_remove(indio_dev);
80 
81 	return 0;
82 }
83 
84 static const struct spi_device_id st_magn_id_table[] = {
85 	{ LIS3MDL_MAGN_DEV_NAME },
86 	{ LSM303AGR_MAGN_DEV_NAME },
87 	{ LIS2MDL_MAGN_DEV_NAME },
88 	{ LSM9DS1_MAGN_DEV_NAME },
89 	{},
90 };
91 MODULE_DEVICE_TABLE(spi, st_magn_id_table);
92 
93 static struct spi_driver st_magn_driver = {
94 	.driver = {
95 		.name = "st-magn-spi",
96 		.of_match_table = of_match_ptr(st_magn_of_match),
97 	},
98 	.probe = st_magn_spi_probe,
99 	.remove = st_magn_spi_remove,
100 	.id_table = st_magn_id_table,
101 };
102 module_spi_driver(st_magn_driver);
103 
104 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
105 MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
106 MODULE_LICENSE("GPL v2");
107