1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics magnetometers driver
4  *
5  * Copyright 2012-2013 STMicroelectronics Inc.
6  *
7  * Denis Ciocca <denis.ciocca@st.com>
8  */
9 
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/slab.h>
13 #include <linux/i2c.h>
14 #include <linux/iio/iio.h>
15 
16 #include <linux/iio/common/st_sensors.h>
17 #include <linux/iio/common/st_sensors_i2c.h>
18 #include "st_magn.h"
19 
20 static const struct of_device_id st_magn_of_match[] = {
21 	{
22 		.compatible = "st,lsm303dlh-magn",
23 		.data = LSM303DLH_MAGN_DEV_NAME,
24 	},
25 	{
26 		.compatible = "st,lsm303dlhc-magn",
27 		.data = LSM303DLHC_MAGN_DEV_NAME,
28 	},
29 	{
30 		.compatible = "st,lsm303dlm-magn",
31 		.data = LSM303DLM_MAGN_DEV_NAME,
32 	},
33 	{
34 		.compatible = "st,lis3mdl-magn",
35 		.data = LIS3MDL_MAGN_DEV_NAME,
36 	},
37 	{
38 		.compatible = "st,lsm303agr-magn",
39 		.data = LSM303AGR_MAGN_DEV_NAME,
40 	},
41 	{
42 		.compatible = "st,lis2mdl",
43 		.data = LIS2MDL_MAGN_DEV_NAME,
44 	},
45 	{
46 		.compatible = "st,lsm9ds1-magn",
47 		.data = LSM9DS1_MAGN_DEV_NAME,
48 	},
49 	{},
50 };
51 MODULE_DEVICE_TABLE(of, st_magn_of_match);
52 
53 static int st_magn_i2c_probe(struct i2c_client *client,
54 			     const struct i2c_device_id *id)
55 {
56 	const struct st_sensor_settings *settings;
57 	struct st_sensor_data *mdata;
58 	struct iio_dev *indio_dev;
59 	int err;
60 
61 	st_sensors_dev_name_probe(&client->dev, client->name, sizeof(client->name));
62 
63 	settings = st_magn_get_settings(client->name);
64 	if (!settings) {
65 		dev_err(&client->dev, "device name %s not recognized.\n",
66 			client->name);
67 		return -ENODEV;
68 	}
69 
70 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*mdata));
71 	if (!indio_dev)
72 		return -ENOMEM;
73 
74 	mdata = iio_priv(indio_dev);
75 	mdata->sensor_settings = (struct st_sensor_settings *)settings;
76 
77 	err = st_sensors_i2c_configure(indio_dev, client);
78 	if (err < 0)
79 		return err;
80 
81 	err = st_magn_common_probe(indio_dev);
82 	if (err < 0)
83 		return err;
84 
85 	return 0;
86 }
87 
88 static int st_magn_i2c_remove(struct i2c_client *client)
89 {
90 	struct iio_dev *indio_dev = i2c_get_clientdata(client);
91 	st_magn_common_remove(indio_dev);
92 
93 	return 0;
94 }
95 
96 static const struct i2c_device_id st_magn_id_table[] = {
97 	{ LSM303DLH_MAGN_DEV_NAME },
98 	{ LSM303DLHC_MAGN_DEV_NAME },
99 	{ LSM303DLM_MAGN_DEV_NAME },
100 	{ LIS3MDL_MAGN_DEV_NAME },
101 	{ LSM303AGR_MAGN_DEV_NAME },
102 	{ LIS2MDL_MAGN_DEV_NAME },
103 	{ LSM9DS1_MAGN_DEV_NAME },
104 	{},
105 };
106 MODULE_DEVICE_TABLE(i2c, st_magn_id_table);
107 
108 static struct i2c_driver st_magn_driver = {
109 	.driver = {
110 		.name = "st-magn-i2c",
111 		.of_match_table = st_magn_of_match,
112 	},
113 	.probe = st_magn_i2c_probe,
114 	.remove = st_magn_i2c_remove,
115 	.id_table = st_magn_id_table,
116 };
117 module_i2c_driver(st_magn_driver);
118 
119 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
120 MODULE_DESCRIPTION("STMicroelectronics magnetometers i2c driver");
121 MODULE_LICENSE("GPL v2");
122