xref: /linux/drivers/iio/magnetometer/st_magn_spi.c (revision d0b7fea58ab91d6363430cbc00b1d1e1b1996552)
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
2665a687a2SLorenzo Bianconi  */
2765a687a2SLorenzo Bianconi static const struct of_device_id st_magn_of_match[] = {
2865a687a2SLorenzo Bianconi 	{
2965a687a2SLorenzo Bianconi 		.compatible = "st,lis3mdl-magn",
3065a687a2SLorenzo Bianconi 		.data = LIS3MDL_MAGN_DEV_NAME,
3165a687a2SLorenzo Bianconi 	},
3265a687a2SLorenzo Bianconi 	{
3365a687a2SLorenzo Bianconi 		.compatible = "st,lsm303agr-magn",
3465a687a2SLorenzo Bianconi 		.data = LSM303AGR_MAGN_DEV_NAME,
3565a687a2SLorenzo Bianconi 	},
36*d0b7fea5SLorenzo Bianconi 	{
37*d0b7fea5SLorenzo Bianconi 		.compatible = "st,lis2mdl",
38*d0b7fea5SLorenzo Bianconi 		.data = LIS2MDL_MAGN_DEV_NAME,
39*d0b7fea5SLorenzo Bianconi 	},
4065a687a2SLorenzo Bianconi 	{}
4165a687a2SLorenzo Bianconi };
4265a687a2SLorenzo Bianconi MODULE_DEVICE_TABLE(of, st_magn_of_match);
4365a687a2SLorenzo Bianconi #else
4465a687a2SLorenzo Bianconi #define st_magn_of_match	NULL
4565a687a2SLorenzo Bianconi #endif
4665a687a2SLorenzo Bianconi 
47872e79adSDenis Ciocca static int st_magn_spi_probe(struct spi_device *spi)
48872e79adSDenis Ciocca {
49872e79adSDenis Ciocca 	struct iio_dev *indio_dev;
50872e79adSDenis Ciocca 	struct st_sensor_data *mdata;
51872e79adSDenis Ciocca 	int err;
52872e79adSDenis Ciocca 
537fc5ad7eSSachin Kamat 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
547fc5ad7eSSachin Kamat 	if (!indio_dev)
557fc5ad7eSSachin Kamat 		return -ENOMEM;
56872e79adSDenis Ciocca 
57872e79adSDenis Ciocca 	mdata = iio_priv(indio_dev);
58872e79adSDenis Ciocca 
5965a687a2SLorenzo Bianconi 	st_sensors_of_name_probe(&spi->dev, st_magn_of_match,
6065a687a2SLorenzo Bianconi 				 spi->modalias, sizeof(spi->modalias));
61872e79adSDenis Ciocca 	st_sensors_spi_configure(indio_dev, spi, mdata);
62872e79adSDenis Ciocca 
632109eb4cSDenis CIOCCA 	err = st_magn_common_probe(indio_dev);
64872e79adSDenis Ciocca 	if (err < 0)
657fc5ad7eSSachin Kamat 		return err;
66872e79adSDenis Ciocca 
67872e79adSDenis Ciocca 	return 0;
68872e79adSDenis Ciocca }
69872e79adSDenis Ciocca 
70872e79adSDenis Ciocca static int st_magn_spi_remove(struct spi_device *spi)
71872e79adSDenis Ciocca {
72872e79adSDenis Ciocca 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
73872e79adSDenis Ciocca 	st_magn_common_remove(indio_dev);
74872e79adSDenis Ciocca 
75872e79adSDenis Ciocca 	return 0;
76872e79adSDenis Ciocca }
77872e79adSDenis Ciocca 
78872e79adSDenis Ciocca static const struct spi_device_id st_magn_id_table[] = {
79872e79adSDenis Ciocca 	{ LIS3MDL_MAGN_DEV_NAME },
801e9676a8SGiuseppe Barba 	{ LSM303AGR_MAGN_DEV_NAME },
81*d0b7fea5SLorenzo Bianconi 	{ LIS2MDL_MAGN_DEV_NAME },
82872e79adSDenis Ciocca 	{},
83872e79adSDenis Ciocca };
84872e79adSDenis Ciocca MODULE_DEVICE_TABLE(spi, st_magn_id_table);
85872e79adSDenis Ciocca 
86872e79adSDenis Ciocca static struct spi_driver st_magn_driver = {
87872e79adSDenis Ciocca 	.driver = {
88872e79adSDenis Ciocca 		.name = "st-magn-spi",
8965a687a2SLorenzo Bianconi 		.of_match_table = of_match_ptr(st_magn_of_match),
90872e79adSDenis Ciocca 	},
91872e79adSDenis Ciocca 	.probe = st_magn_spi_probe,
92872e79adSDenis Ciocca 	.remove = st_magn_spi_remove,
93872e79adSDenis Ciocca 	.id_table = st_magn_id_table,
94872e79adSDenis Ciocca };
95872e79adSDenis Ciocca module_spi_driver(st_magn_driver);
96872e79adSDenis Ciocca 
97872e79adSDenis Ciocca MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
98872e79adSDenis Ciocca MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
99872e79adSDenis Ciocca MODULE_LICENSE("GPL v2");
100