xref: /linux/drivers/iio/magnetometer/st_magn_spi.c (revision 7db4f2cacbede1c6d95552c0d10e77398665a733)
1fda8d26eSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2872e79adSDenis Ciocca /*
3872e79adSDenis Ciocca  * STMicroelectronics magnetometers driver
4872e79adSDenis Ciocca  *
5872e79adSDenis Ciocca  * Copyright 2012-2013 STMicroelectronics Inc.
6872e79adSDenis Ciocca  *
7872e79adSDenis Ciocca  * Denis Ciocca <denis.ciocca@st.com>
8872e79adSDenis Ciocca  */
9872e79adSDenis Ciocca 
10872e79adSDenis Ciocca #include <linux/kernel.h>
11872e79adSDenis Ciocca #include <linux/module.h>
12872e79adSDenis Ciocca #include <linux/slab.h>
13872e79adSDenis Ciocca #include <linux/spi/spi.h>
14872e79adSDenis Ciocca #include <linux/iio/iio.h>
15872e79adSDenis Ciocca 
16872e79adSDenis Ciocca #include <linux/iio/common/st_sensors.h>
17872e79adSDenis Ciocca #include <linux/iio/common/st_sensors_spi.h>
18872e79adSDenis Ciocca #include "st_magn.h"
19872e79adSDenis Ciocca 
2065a687a2SLorenzo Bianconi /*
2165a687a2SLorenzo Bianconi  * For new single-chip sensors use <device_name> as compatible string.
2265a687a2SLorenzo Bianconi  * For old single-chip devices keep <device_name>-magn to maintain
2365a687a2SLorenzo Bianconi  * compatibility
2453759e25SMartin Kelly  * For multi-chip devices, use <device_name>-magn to distinguish which
2553759e25SMartin Kelly  * capability is being used
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 	},
36d0b7fea5SLorenzo Bianconi 	{
37d0b7fea5SLorenzo Bianconi 		.compatible = "st,lis2mdl",
38d0b7fea5SLorenzo Bianconi 		.data = LIS2MDL_MAGN_DEV_NAME,
39d0b7fea5SLorenzo Bianconi 	},
400d92aa2cSMartin Kelly 	{
410d92aa2cSMartin Kelly 		.compatible = "st,lsm9ds1-magn",
420d92aa2cSMartin Kelly 		.data = LSM9DS1_MAGN_DEV_NAME,
430d92aa2cSMartin Kelly 	},
44c71cfe55SLI Qingwu 	{
45c71cfe55SLI Qingwu 		.compatible = "st,iis2mdc",
46c71cfe55SLI Qingwu 		.data = IIS2MDC_MAGN_DEV_NAME,
47c71cfe55SLI Qingwu 	},
4865a687a2SLorenzo Bianconi 	{}
4965a687a2SLorenzo Bianconi };
5065a687a2SLorenzo Bianconi MODULE_DEVICE_TABLE(of, st_magn_of_match);
5165a687a2SLorenzo Bianconi 
52872e79adSDenis Ciocca static int st_magn_spi_probe(struct spi_device *spi)
53872e79adSDenis Ciocca {
54291d83f2SDenis Ciocca 	const struct st_sensor_settings *settings;
55872e79adSDenis Ciocca 	struct st_sensor_data *mdata;
56291d83f2SDenis Ciocca 	struct iio_dev *indio_dev;
57872e79adSDenis Ciocca 	int err;
58872e79adSDenis Ciocca 
59efc78983SAndy Shevchenko 	st_sensors_dev_name_probe(&spi->dev, spi->modalias, sizeof(spi->modalias));
60291d83f2SDenis Ciocca 
61291d83f2SDenis Ciocca 	settings = st_magn_get_settings(spi->modalias);
62291d83f2SDenis Ciocca 	if (!settings) {
63291d83f2SDenis Ciocca 		dev_err(&spi->dev, "device name %s not recognized.\n",
64291d83f2SDenis Ciocca 			spi->modalias);
65291d83f2SDenis Ciocca 		return -ENODEV;
66291d83f2SDenis Ciocca 	}
67291d83f2SDenis Ciocca 
687fc5ad7eSSachin Kamat 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*mdata));
697fc5ad7eSSachin Kamat 	if (!indio_dev)
707fc5ad7eSSachin Kamat 		return -ENOMEM;
71872e79adSDenis Ciocca 
72872e79adSDenis Ciocca 	mdata = iio_priv(indio_dev);
73291d83f2SDenis Ciocca 	mdata->sensor_settings = (struct st_sensor_settings *)settings;
74872e79adSDenis Ciocca 
751ecd245eSDenis Ciocca 	err = st_sensors_spi_configure(indio_dev, spi);
761ecd245eSDenis Ciocca 	if (err < 0)
771ecd245eSDenis Ciocca 		return err;
78872e79adSDenis Ciocca 
79*7db4f2caSAndy Shevchenko 	err = st_sensors_power_enable(indio_dev);
80*7db4f2caSAndy Shevchenko 	if (err)
817fc5ad7eSSachin Kamat 		return err;
82872e79adSDenis Ciocca 
83*7db4f2caSAndy Shevchenko 	err = st_magn_common_probe(indio_dev);
84*7db4f2caSAndy Shevchenko 	if (err < 0)
85*7db4f2caSAndy Shevchenko 		goto st_magn_power_off;
86*7db4f2caSAndy Shevchenko 
87872e79adSDenis Ciocca 	return 0;
88*7db4f2caSAndy Shevchenko 
89*7db4f2caSAndy Shevchenko st_magn_power_off:
90*7db4f2caSAndy Shevchenko 	st_sensors_power_disable(indio_dev);
91*7db4f2caSAndy Shevchenko 
92*7db4f2caSAndy Shevchenko 	return err;
93872e79adSDenis Ciocca }
94872e79adSDenis Ciocca 
95872e79adSDenis Ciocca static int st_magn_spi_remove(struct spi_device *spi)
96872e79adSDenis Ciocca {
97872e79adSDenis Ciocca 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
98*7db4f2caSAndy Shevchenko 
99*7db4f2caSAndy Shevchenko 	st_sensors_power_disable(indio_dev);
100*7db4f2caSAndy Shevchenko 
101872e79adSDenis Ciocca 	st_magn_common_remove(indio_dev);
102872e79adSDenis Ciocca 
103872e79adSDenis Ciocca 	return 0;
104872e79adSDenis Ciocca }
105872e79adSDenis Ciocca 
106872e79adSDenis Ciocca static const struct spi_device_id st_magn_id_table[] = {
107872e79adSDenis Ciocca 	{ LIS3MDL_MAGN_DEV_NAME },
1081e9676a8SGiuseppe Barba 	{ LSM303AGR_MAGN_DEV_NAME },
109d0b7fea5SLorenzo Bianconi 	{ LIS2MDL_MAGN_DEV_NAME },
1100d92aa2cSMartin Kelly 	{ LSM9DS1_MAGN_DEV_NAME },
111c71cfe55SLI Qingwu 	{ IIS2MDC_MAGN_DEV_NAME },
112872e79adSDenis Ciocca 	{},
113872e79adSDenis Ciocca };
114872e79adSDenis Ciocca MODULE_DEVICE_TABLE(spi, st_magn_id_table);
115872e79adSDenis Ciocca 
116872e79adSDenis Ciocca static struct spi_driver st_magn_driver = {
117872e79adSDenis Ciocca 	.driver = {
118872e79adSDenis Ciocca 		.name = "st-magn-spi",
119ecb27c5eSAndy Shevchenko 		.of_match_table = st_magn_of_match,
120872e79adSDenis Ciocca 	},
121872e79adSDenis Ciocca 	.probe = st_magn_spi_probe,
122872e79adSDenis Ciocca 	.remove = st_magn_spi_remove,
123872e79adSDenis Ciocca 	.id_table = st_magn_id_table,
124872e79adSDenis Ciocca };
125872e79adSDenis Ciocca module_spi_driver(st_magn_driver);
126872e79adSDenis Ciocca 
127872e79adSDenis Ciocca MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
128872e79adSDenis Ciocca MODULE_DESCRIPTION("STMicroelectronics magnetometers spi driver");
129872e79adSDenis Ciocca MODULE_LICENSE("GPL v2");
130