xref: /linux/drivers/iio/magnetometer/st_magn_i2c.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/i2c.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_i2c.h>
19872e79adSDenis Ciocca #include "st_magn.h"
20872e79adSDenis Ciocca 
212d7768a8SLinus Walleij #ifdef CONFIG_OF
222d7768a8SLinus Walleij static const struct of_device_id st_magn_of_match[] = {
232d7768a8SLinus Walleij 	{
241038a687SLinus Walleij 		.compatible = "st,lsm303dlh-magn",
251038a687SLinus Walleij 		.data = LSM303DLH_MAGN_DEV_NAME,
261038a687SLinus Walleij 	},
271038a687SLinus Walleij 	{
282d7768a8SLinus Walleij 		.compatible = "st,lsm303dlhc-magn",
292d7768a8SLinus Walleij 		.data = LSM303DLHC_MAGN_DEV_NAME,
302d7768a8SLinus Walleij 	},
312d7768a8SLinus Walleij 	{
322d7768a8SLinus Walleij 		.compatible = "st,lsm303dlm-magn",
332d7768a8SLinus Walleij 		.data = LSM303DLM_MAGN_DEV_NAME,
342d7768a8SLinus Walleij 	},
352d7768a8SLinus Walleij 	{
362d7768a8SLinus Walleij 		.compatible = "st,lis3mdl-magn",
372d7768a8SLinus Walleij 		.data = LIS3MDL_MAGN_DEV_NAME,
382d7768a8SLinus Walleij 	},
391e9676a8SGiuseppe Barba 	{
401e9676a8SGiuseppe Barba 		.compatible = "st,lsm303agr-magn",
411e9676a8SGiuseppe Barba 		.data = LSM303AGR_MAGN_DEV_NAME,
421e9676a8SGiuseppe Barba 	},
43d0b7fea5SLorenzo Bianconi 	{
44d0b7fea5SLorenzo Bianconi 		.compatible = "st,lis2mdl",
45d0b7fea5SLorenzo Bianconi 		.data = LIS2MDL_MAGN_DEV_NAME,
46d0b7fea5SLorenzo Bianconi 	},
47*0d92aa2cSMartin Kelly 	{
48*0d92aa2cSMartin Kelly 		.compatible = "st,lsm9ds1-magn",
49*0d92aa2cSMartin Kelly 		.data = LSM9DS1_MAGN_DEV_NAME,
50*0d92aa2cSMartin Kelly 	},
512d7768a8SLinus Walleij 	{},
522d7768a8SLinus Walleij };
532d7768a8SLinus Walleij MODULE_DEVICE_TABLE(of, st_magn_of_match);
542d7768a8SLinus Walleij #else
552d7768a8SLinus Walleij #define st_magn_of_match NULL
562d7768a8SLinus Walleij #endif
572d7768a8SLinus Walleij 
58872e79adSDenis Ciocca static int st_magn_i2c_probe(struct i2c_client *client,
59872e79adSDenis Ciocca 						const struct i2c_device_id *id)
60872e79adSDenis Ciocca {
61872e79adSDenis Ciocca 	struct iio_dev *indio_dev;
62872e79adSDenis Ciocca 	struct st_sensor_data *mdata;
63872e79adSDenis Ciocca 	int err;
64872e79adSDenis Ciocca 
657fc5ad7eSSachin Kamat 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mdata));
667fc5ad7eSSachin Kamat 	if (!indio_dev)
677fc5ad7eSSachin Kamat 		return -ENOMEM;
68872e79adSDenis Ciocca 
69872e79adSDenis Ciocca 	mdata = iio_priv(indio_dev);
70250bbbdbSLorenzo Bianconi 	st_sensors_of_name_probe(&client->dev, st_magn_of_match,
71250bbbdbSLorenzo Bianconi 				 client->name, sizeof(client->name));
72872e79adSDenis Ciocca 
73872e79adSDenis Ciocca 	st_sensors_i2c_configure(indio_dev, client, mdata);
74872e79adSDenis Ciocca 
752109eb4cSDenis CIOCCA 	err = st_magn_common_probe(indio_dev);
76872e79adSDenis Ciocca 	if (err < 0)
777fc5ad7eSSachin Kamat 		return err;
78872e79adSDenis Ciocca 
79872e79adSDenis Ciocca 	return 0;
80872e79adSDenis Ciocca }
81872e79adSDenis Ciocca 
82872e79adSDenis Ciocca static int st_magn_i2c_remove(struct i2c_client *client)
83872e79adSDenis Ciocca {
84872e79adSDenis Ciocca 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
85872e79adSDenis Ciocca 	st_magn_common_remove(indio_dev);
86872e79adSDenis Ciocca 
87872e79adSDenis Ciocca 	return 0;
88872e79adSDenis Ciocca }
89872e79adSDenis Ciocca 
90872e79adSDenis Ciocca static const struct i2c_device_id st_magn_id_table[] = {
911038a687SLinus Walleij 	{ LSM303DLH_MAGN_DEV_NAME },
92872e79adSDenis Ciocca 	{ LSM303DLHC_MAGN_DEV_NAME },
93872e79adSDenis Ciocca 	{ LSM303DLM_MAGN_DEV_NAME },
94872e79adSDenis Ciocca 	{ LIS3MDL_MAGN_DEV_NAME },
951e9676a8SGiuseppe Barba 	{ LSM303AGR_MAGN_DEV_NAME },
96d0b7fea5SLorenzo Bianconi 	{ LIS2MDL_MAGN_DEV_NAME },
97*0d92aa2cSMartin Kelly 	{ LSM9DS1_MAGN_DEV_NAME },
98872e79adSDenis Ciocca 	{},
99872e79adSDenis Ciocca };
100872e79adSDenis Ciocca MODULE_DEVICE_TABLE(i2c, st_magn_id_table);
101872e79adSDenis Ciocca 
102872e79adSDenis Ciocca static struct i2c_driver st_magn_driver = {
103872e79adSDenis Ciocca 	.driver = {
104872e79adSDenis Ciocca 		.name = "st-magn-i2c",
1052d7768a8SLinus Walleij 		.of_match_table = of_match_ptr(st_magn_of_match),
106872e79adSDenis Ciocca 	},
107872e79adSDenis Ciocca 	.probe = st_magn_i2c_probe,
108872e79adSDenis Ciocca 	.remove = st_magn_i2c_remove,
109872e79adSDenis Ciocca 	.id_table = st_magn_id_table,
110872e79adSDenis Ciocca };
111872e79adSDenis Ciocca module_i2c_driver(st_magn_driver);
112872e79adSDenis Ciocca 
113872e79adSDenis Ciocca MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
114872e79adSDenis Ciocca MODULE_DESCRIPTION("STMicroelectronics magnetometers i2c driver");
115872e79adSDenis Ciocca MODULE_LICENSE("GPL v2");
116