xref: /linux/drivers/iio/magnetometer/st_magn_i2c.c (revision 721a2a7edcdd10f55ef7d3cf74fc276b0d4177e8)
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>
129c6cd755SJonathan Cameron #include <linux/mod_devicetable.h>
13872e79adSDenis Ciocca #include <linux/i2c.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_i2c.h>
18872e79adSDenis Ciocca #include "st_magn.h"
19872e79adSDenis Ciocca 
202d7768a8SLinus Walleij static const struct of_device_id st_magn_of_match[] = {
212d7768a8SLinus Walleij 	{
221038a687SLinus Walleij 		.compatible = "st,lsm303dlh-magn",
231038a687SLinus Walleij 		.data = LSM303DLH_MAGN_DEV_NAME,
241038a687SLinus Walleij 	},
251038a687SLinus Walleij 	{
262d7768a8SLinus Walleij 		.compatible = "st,lsm303dlhc-magn",
272d7768a8SLinus Walleij 		.data = LSM303DLHC_MAGN_DEV_NAME,
282d7768a8SLinus Walleij 	},
292d7768a8SLinus Walleij 	{
302d7768a8SLinus Walleij 		.compatible = "st,lsm303dlm-magn",
312d7768a8SLinus Walleij 		.data = LSM303DLM_MAGN_DEV_NAME,
322d7768a8SLinus Walleij 	},
332d7768a8SLinus Walleij 	{
342d7768a8SLinus Walleij 		.compatible = "st,lis3mdl-magn",
352d7768a8SLinus Walleij 		.data = LIS3MDL_MAGN_DEV_NAME,
362d7768a8SLinus Walleij 	},
371e9676a8SGiuseppe Barba 	{
381e9676a8SGiuseppe Barba 		.compatible = "st,lsm303agr-magn",
391e9676a8SGiuseppe Barba 		.data = LSM303AGR_MAGN_DEV_NAME,
401e9676a8SGiuseppe Barba 	},
41d0b7fea5SLorenzo Bianconi 	{
42d0b7fea5SLorenzo Bianconi 		.compatible = "st,lis2mdl",
43d0b7fea5SLorenzo Bianconi 		.data = LIS2MDL_MAGN_DEV_NAME,
44d0b7fea5SLorenzo Bianconi 	},
450d92aa2cSMartin Kelly 	{
460d92aa2cSMartin Kelly 		.compatible = "st,lsm9ds1-magn",
470d92aa2cSMartin Kelly 		.data = LSM9DS1_MAGN_DEV_NAME,
480d92aa2cSMartin Kelly 	},
49c71cfe55SLI Qingwu 	{
50c71cfe55SLI Qingwu 		.compatible = "st,iis2mdc",
51c71cfe55SLI Qingwu 		.data = IIS2MDC_MAGN_DEV_NAME,
52c71cfe55SLI Qingwu 	},
53*721a2a7eSStephan Gerhold 	{
54*721a2a7eSStephan Gerhold 		.compatible = "st,lsm303c-magn",
55*721a2a7eSStephan Gerhold 		.data = LSM303C_MAGN_DEV_NAME,
56*721a2a7eSStephan Gerhold 	},
572d7768a8SLinus Walleij 	{},
582d7768a8SLinus Walleij };
592d7768a8SLinus Walleij MODULE_DEVICE_TABLE(of, st_magn_of_match);
602d7768a8SLinus Walleij 
619492c00dSUwe Kleine-König static int st_magn_i2c_probe(struct i2c_client *client)
62872e79adSDenis Ciocca {
63291d83f2SDenis Ciocca 	const struct st_sensor_settings *settings;
64872e79adSDenis Ciocca 	struct st_sensor_data *mdata;
65291d83f2SDenis Ciocca 	struct iio_dev *indio_dev;
66872e79adSDenis Ciocca 	int err;
67872e79adSDenis Ciocca 
68efc78983SAndy Shevchenko 	st_sensors_dev_name_probe(&client->dev, client->name, sizeof(client->name));
69291d83f2SDenis Ciocca 
70291d83f2SDenis Ciocca 	settings = st_magn_get_settings(client->name);
71291d83f2SDenis Ciocca 	if (!settings) {
72291d83f2SDenis Ciocca 		dev_err(&client->dev, "device name %s not recognized.\n",
73291d83f2SDenis Ciocca 			client->name);
74291d83f2SDenis Ciocca 		return -ENODEV;
75291d83f2SDenis Ciocca 	}
76291d83f2SDenis Ciocca 
777fc5ad7eSSachin Kamat 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mdata));
787fc5ad7eSSachin Kamat 	if (!indio_dev)
797fc5ad7eSSachin Kamat 		return -ENOMEM;
80872e79adSDenis Ciocca 
81872e79adSDenis Ciocca 	mdata = iio_priv(indio_dev);
82291d83f2SDenis Ciocca 	mdata->sensor_settings = (struct st_sensor_settings *)settings;
83872e79adSDenis Ciocca 
84062809efSDenis Ciocca 	err = st_sensors_i2c_configure(indio_dev, client);
85062809efSDenis Ciocca 	if (err < 0)
86062809efSDenis Ciocca 		return err;
87872e79adSDenis Ciocca 
887db4f2caSAndy Shevchenko 	err = st_sensors_power_enable(indio_dev);
897db4f2caSAndy Shevchenko 	if (err)
907fc5ad7eSSachin Kamat 		return err;
91872e79adSDenis Ciocca 
925363c6c1SAlexandru Ardelean 	return st_magn_common_probe(indio_dev);
93872e79adSDenis Ciocca }
94872e79adSDenis Ciocca 
95872e79adSDenis Ciocca static const struct i2c_device_id st_magn_id_table[] = {
961038a687SLinus Walleij 	{ LSM303DLH_MAGN_DEV_NAME },
97872e79adSDenis Ciocca 	{ LSM303DLHC_MAGN_DEV_NAME },
98872e79adSDenis Ciocca 	{ LSM303DLM_MAGN_DEV_NAME },
99872e79adSDenis Ciocca 	{ LIS3MDL_MAGN_DEV_NAME },
1001e9676a8SGiuseppe Barba 	{ LSM303AGR_MAGN_DEV_NAME },
101d0b7fea5SLorenzo Bianconi 	{ LIS2MDL_MAGN_DEV_NAME },
1020d92aa2cSMartin Kelly 	{ LSM9DS1_MAGN_DEV_NAME },
103c71cfe55SLI Qingwu 	{ IIS2MDC_MAGN_DEV_NAME },
104*721a2a7eSStephan Gerhold 	{ LSM303C_MAGN_DEV_NAME },
105872e79adSDenis Ciocca 	{},
106872e79adSDenis Ciocca };
107872e79adSDenis Ciocca MODULE_DEVICE_TABLE(i2c, st_magn_id_table);
108872e79adSDenis Ciocca 
109872e79adSDenis Ciocca static struct i2c_driver st_magn_driver = {
110872e79adSDenis Ciocca 	.driver = {
111872e79adSDenis Ciocca 		.name = "st-magn-i2c",
112ecb27c5eSAndy Shevchenko 		.of_match_table = st_magn_of_match,
113872e79adSDenis Ciocca 	},
1149492c00dSUwe Kleine-König 	.probe_new = st_magn_i2c_probe,
115872e79adSDenis Ciocca 	.id_table = st_magn_id_table,
116872e79adSDenis Ciocca };
117872e79adSDenis Ciocca module_i2c_driver(st_magn_driver);
118872e79adSDenis Ciocca 
119872e79adSDenis Ciocca MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
120872e79adSDenis Ciocca MODULE_DESCRIPTION("STMicroelectronics magnetometers i2c driver");
121872e79adSDenis Ciocca MODULE_LICENSE("GPL v2");
1220805b512SJonathan Cameron MODULE_IMPORT_NS(IIO_ST_SENSORS);
123