xref: /linux/drivers/iio/magnetometer/st_magn_spi.c (revision 36452b8ba02a62445847a29779dc581eb4cfeddd)
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  */
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 };
42 MODULE_DEVICE_TABLE(of, st_magn_of_match);
43 #else
44 #define st_magn_of_match	NULL
45 #endif
46 
47 static int st_magn_spi_probe(struct spi_device *spi)
48 {
49 	struct iio_dev *indio_dev;
50 	struct st_sensor_data *mdata;
51 	int err;
52 
53 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
54 	if (!indio_dev)
55 		return -ENOMEM;
56 
57 	mdata = iio_priv(indio_dev);
58 
59 	st_sensors_of_name_probe(&spi->dev, st_magn_of_match,
60 				 spi->modalias, sizeof(spi->modalias));
61 	st_sensors_spi_configure(indio_dev, spi, mdata);
62 
63 	err = st_magn_common_probe(indio_dev);
64 	if (err < 0)
65 		return err;
66 
67 	return 0;
68 }
69 
70 static int st_magn_spi_remove(struct spi_device *spi)
71 {
72 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
73 	st_magn_common_remove(indio_dev);
74 
75 	return 0;
76 }
77 
78 static const struct spi_device_id st_magn_id_table[] = {
79 	{ LIS3MDL_MAGN_DEV_NAME },
80 	{ LSM303AGR_MAGN_DEV_NAME },
81 	{ LIS2MDL_MAGN_DEV_NAME },
82 	{},
83 };
84 MODULE_DEVICE_TABLE(spi, st_magn_id_table);
85 
86 static struct spi_driver st_magn_driver = {
87 	.driver = {
88 		.name = "st-magn-spi",
89 		.of_match_table = of_match_ptr(st_magn_of_match),
90 	},
91 	.probe = st_magn_spi_probe,
92 	.remove = st_magn_spi_remove,
93 	.id_table = st_magn_id_table,
94 };
95 module_spi_driver(st_magn_driver);
96 
97 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
98 MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
99 MODULE_LICENSE("GPL v2");
100