xref: /linux/drivers/iio/magnetometer/st_magn_i2c.c (revision 2109eb4c0c7d21669ae2ad0cda90c438b5470bf3)
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 	{
242d7768a8SLinus Walleij 		.compatible = "st,lsm303dlhc-magn",
252d7768a8SLinus Walleij 		.data = LSM303DLHC_MAGN_DEV_NAME,
262d7768a8SLinus Walleij 	},
272d7768a8SLinus Walleij 	{
282d7768a8SLinus Walleij 		.compatible = "st,lsm303dlm-magn",
292d7768a8SLinus Walleij 		.data = LSM303DLM_MAGN_DEV_NAME,
302d7768a8SLinus Walleij 	},
312d7768a8SLinus Walleij 	{
322d7768a8SLinus Walleij 		.compatible = "st,lis3mdl-magn",
332d7768a8SLinus Walleij 		.data = LIS3MDL_MAGN_DEV_NAME,
342d7768a8SLinus Walleij 	},
352d7768a8SLinus Walleij 	{},
362d7768a8SLinus Walleij };
372d7768a8SLinus Walleij MODULE_DEVICE_TABLE(of, st_magn_of_match);
382d7768a8SLinus Walleij #else
392d7768a8SLinus Walleij #define st_magn_of_match NULL
402d7768a8SLinus Walleij #endif
412d7768a8SLinus Walleij 
42872e79adSDenis Ciocca static int st_magn_i2c_probe(struct i2c_client *client,
43872e79adSDenis Ciocca 						const struct i2c_device_id *id)
44872e79adSDenis Ciocca {
45872e79adSDenis Ciocca 	struct iio_dev *indio_dev;
46872e79adSDenis Ciocca 	struct st_sensor_data *mdata;
47872e79adSDenis Ciocca 	int err;
48872e79adSDenis Ciocca 
497fc5ad7eSSachin Kamat 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mdata));
507fc5ad7eSSachin Kamat 	if (!indio_dev)
517fc5ad7eSSachin Kamat 		return -ENOMEM;
52872e79adSDenis Ciocca 
53872e79adSDenis Ciocca 	mdata = iio_priv(indio_dev);
54872e79adSDenis Ciocca 	mdata->dev = &client->dev;
552d7768a8SLinus Walleij 	st_sensors_of_i2c_probe(client, st_magn_of_match);
56872e79adSDenis Ciocca 
57872e79adSDenis Ciocca 	st_sensors_i2c_configure(indio_dev, client, mdata);
58872e79adSDenis Ciocca 
59*2109eb4cSDenis CIOCCA 	err = st_magn_common_probe(indio_dev);
60872e79adSDenis Ciocca 	if (err < 0)
617fc5ad7eSSachin Kamat 		return err;
62872e79adSDenis Ciocca 
63872e79adSDenis Ciocca 	return 0;
64872e79adSDenis Ciocca }
65872e79adSDenis Ciocca 
66872e79adSDenis Ciocca static int st_magn_i2c_remove(struct i2c_client *client)
67872e79adSDenis Ciocca {
68872e79adSDenis Ciocca 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
69872e79adSDenis Ciocca 	st_magn_common_remove(indio_dev);
70872e79adSDenis Ciocca 
71872e79adSDenis Ciocca 	return 0;
72872e79adSDenis Ciocca }
73872e79adSDenis Ciocca 
74872e79adSDenis Ciocca static const struct i2c_device_id st_magn_id_table[] = {
75872e79adSDenis Ciocca 	{ LSM303DLHC_MAGN_DEV_NAME },
76872e79adSDenis Ciocca 	{ LSM303DLM_MAGN_DEV_NAME },
77872e79adSDenis Ciocca 	{ LIS3MDL_MAGN_DEV_NAME },
78872e79adSDenis Ciocca 	{},
79872e79adSDenis Ciocca };
80872e79adSDenis Ciocca MODULE_DEVICE_TABLE(i2c, st_magn_id_table);
81872e79adSDenis Ciocca 
82872e79adSDenis Ciocca static struct i2c_driver st_magn_driver = {
83872e79adSDenis Ciocca 	.driver = {
84872e79adSDenis Ciocca 		.owner = THIS_MODULE,
85872e79adSDenis Ciocca 		.name = "st-magn-i2c",
862d7768a8SLinus Walleij 		.of_match_table = of_match_ptr(st_magn_of_match),
87872e79adSDenis Ciocca 	},
88872e79adSDenis Ciocca 	.probe = st_magn_i2c_probe,
89872e79adSDenis Ciocca 	.remove = st_magn_i2c_remove,
90872e79adSDenis Ciocca 	.id_table = st_magn_id_table,
91872e79adSDenis Ciocca };
92872e79adSDenis Ciocca module_i2c_driver(st_magn_driver);
93872e79adSDenis Ciocca 
94872e79adSDenis Ciocca MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
95872e79adSDenis Ciocca MODULE_DESCRIPTION("STMicroelectronics magnetometers i2c driver");
96872e79adSDenis Ciocca MODULE_LICENSE("GPL v2");
97