xref: /linux/drivers/iio/magnetometer/st_magn_spi.c (revision 0d92aa2c272faea47cd5e9acb482acdb9eed2c1d)
1872e79adSDenis Ciocca /*
2872e79adSDenis Ciocca  * STMicroelectronics magnetometers driver
3872e79adSDenis Ciocca  *
4872e79adSDenis Ciocca  * Copyright 2012-2013 STMicroelectronics Inc.
5872e79adSDenis Ciocca  *
6872e79adSDenis Ciocca  * Denis Ciocca <denis.ciocca@st.com>
7872e79adSDenis Ciocca  *
8872e79adSDenis Ciocca  * Licensed under the GPL-2.
9872e79adSDenis Ciocca  */
10872e79adSDenis Ciocca 
11872e79adSDenis Ciocca #include <linux/kernel.h>
12872e79adSDenis Ciocca #include <linux/module.h>
13872e79adSDenis Ciocca #include <linux/slab.h>
14872e79adSDenis Ciocca #include <linux/spi/spi.h>
15872e79adSDenis Ciocca #include <linux/iio/iio.h>
16872e79adSDenis Ciocca 
17872e79adSDenis Ciocca #include <linux/iio/common/st_sensors.h>
18872e79adSDenis Ciocca #include <linux/iio/common/st_sensors_spi.h>
19872e79adSDenis Ciocca #include "st_magn.h"
20872e79adSDenis Ciocca 
2165a687a2SLorenzo Bianconi #ifdef CONFIG_OF
2265a687a2SLorenzo Bianconi /*
2365a687a2SLorenzo Bianconi  * For new single-chip sensors use <device_name> as compatible string.
2465a687a2SLorenzo Bianconi  * For old single-chip devices keep <device_name>-magn to maintain
2565a687a2SLorenzo Bianconi  * compatibility
2653759e25SMartin Kelly  * For multi-chip devices, use <device_name>-magn to distinguish which
2753759e25SMartin Kelly  * capability is being used
2865a687a2SLorenzo Bianconi  */
2965a687a2SLorenzo Bianconi static const struct of_device_id st_magn_of_match[] = {
3065a687a2SLorenzo Bianconi 	{
3165a687a2SLorenzo Bianconi 		.compatible = "st,lis3mdl-magn",
3265a687a2SLorenzo Bianconi 		.data = LIS3MDL_MAGN_DEV_NAME,
3365a687a2SLorenzo Bianconi 	},
3465a687a2SLorenzo Bianconi 	{
3565a687a2SLorenzo Bianconi 		.compatible = "st,lsm303agr-magn",
3665a687a2SLorenzo Bianconi 		.data = LSM303AGR_MAGN_DEV_NAME,
3765a687a2SLorenzo Bianconi 	},
38d0b7fea5SLorenzo Bianconi 	{
39d0b7fea5SLorenzo Bianconi 		.compatible = "st,lis2mdl",
40d0b7fea5SLorenzo Bianconi 		.data = LIS2MDL_MAGN_DEV_NAME,
41d0b7fea5SLorenzo Bianconi 	},
42*0d92aa2cSMartin Kelly 	{
43*0d92aa2cSMartin Kelly 		.compatible = "st,lsm9ds1-magn",
44*0d92aa2cSMartin Kelly 		.data = LSM9DS1_MAGN_DEV_NAME,
45*0d92aa2cSMartin Kelly 	},
4665a687a2SLorenzo Bianconi 	{}
4765a687a2SLorenzo Bianconi };
4865a687a2SLorenzo Bianconi MODULE_DEVICE_TABLE(of, st_magn_of_match);
4965a687a2SLorenzo Bianconi #else
5065a687a2SLorenzo Bianconi #define st_magn_of_match	NULL
5165a687a2SLorenzo Bianconi #endif
5265a687a2SLorenzo Bianconi 
53872e79adSDenis Ciocca static int st_magn_spi_probe(struct spi_device *spi)
54872e79adSDenis Ciocca {
55872e79adSDenis Ciocca 	struct iio_dev *indio_dev;
56872e79adSDenis Ciocca 	struct st_sensor_data *mdata;
57872e79adSDenis Ciocca 	int err;
58872e79adSDenis Ciocca 
597fc5ad7eSSachin Kamat 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
607fc5ad7eSSachin Kamat 	if (!indio_dev)
617fc5ad7eSSachin Kamat 		return -ENOMEM;
62872e79adSDenis Ciocca 
63872e79adSDenis Ciocca 	mdata = iio_priv(indio_dev);
64872e79adSDenis Ciocca 
6565a687a2SLorenzo Bianconi 	st_sensors_of_name_probe(&spi->dev, st_magn_of_match,
6665a687a2SLorenzo Bianconi 				 spi->modalias, sizeof(spi->modalias));
67872e79adSDenis Ciocca 	st_sensors_spi_configure(indio_dev, spi, mdata);
68872e79adSDenis Ciocca 
692109eb4cSDenis CIOCCA 	err = st_magn_common_probe(indio_dev);
70872e79adSDenis Ciocca 	if (err < 0)
717fc5ad7eSSachin Kamat 		return err;
72872e79adSDenis Ciocca 
73872e79adSDenis Ciocca 	return 0;
74872e79adSDenis Ciocca }
75872e79adSDenis Ciocca 
76872e79adSDenis Ciocca static int st_magn_spi_remove(struct spi_device *spi)
77872e79adSDenis Ciocca {
78872e79adSDenis Ciocca 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
79872e79adSDenis Ciocca 	st_magn_common_remove(indio_dev);
80872e79adSDenis Ciocca 
81872e79adSDenis Ciocca 	return 0;
82872e79adSDenis Ciocca }
83872e79adSDenis Ciocca 
84872e79adSDenis Ciocca static const struct spi_device_id st_magn_id_table[] = {
85872e79adSDenis Ciocca 	{ LIS3MDL_MAGN_DEV_NAME },
861e9676a8SGiuseppe Barba 	{ LSM303AGR_MAGN_DEV_NAME },
87d0b7fea5SLorenzo Bianconi 	{ LIS2MDL_MAGN_DEV_NAME },
88*0d92aa2cSMartin Kelly 	{ LSM9DS1_MAGN_DEV_NAME },
89872e79adSDenis Ciocca 	{},
90872e79adSDenis Ciocca };
91872e79adSDenis Ciocca MODULE_DEVICE_TABLE(spi, st_magn_id_table);
92872e79adSDenis Ciocca 
93872e79adSDenis Ciocca static struct spi_driver st_magn_driver = {
94872e79adSDenis Ciocca 	.driver = {
95872e79adSDenis Ciocca 		.name = "st-magn-spi",
9665a687a2SLorenzo Bianconi 		.of_match_table = of_match_ptr(st_magn_of_match),
97872e79adSDenis Ciocca 	},
98872e79adSDenis Ciocca 	.probe = st_magn_spi_probe,
99872e79adSDenis Ciocca 	.remove = st_magn_spi_remove,
100872e79adSDenis Ciocca 	.id_table = st_magn_id_table,
101872e79adSDenis Ciocca };
102872e79adSDenis Ciocca module_spi_driver(st_magn_driver);
103872e79adSDenis Ciocca 
104872e79adSDenis Ciocca MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
105872e79adSDenis Ciocca MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
106872e79adSDenis Ciocca MODULE_LICENSE("GPL v2");
107