xref: /linux/drivers/iio/accel/st_accel_i2c.c (revision 249ebf3f65f8530beb2cbfb91bff1d83ba88d23c)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics accelerometers 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/mod_devicetable.h>
13 #include <linux/i2c.h>
14 #include <linux/iio/iio.h>
15 
16 #include <linux/iio/common/st_sensors_i2c.h>
17 #include "st_accel.h"
18 
19 static const struct of_device_id st_accel_of_match[] = {
20 	{
21 		/* An older compatible */
22 		.compatible = "st,lis3lv02d",
23 		.data = LIS3LV02DL_ACCEL_DEV_NAME,
24 	},
25 	{
26 		.compatible = "st,lis3lv02dl-accel",
27 		.data = LIS3LV02DL_ACCEL_DEV_NAME,
28 	},
29 	{
30 		.compatible = "st,lsm303dlh-accel",
31 		.data = LSM303DLH_ACCEL_DEV_NAME,
32 	},
33 	{
34 		.compatible = "st,lsm303dlhc-accel",
35 		.data = LSM303DLHC_ACCEL_DEV_NAME,
36 	},
37 	{
38 		.compatible = "st,lis3dh-accel",
39 		.data = LIS3DH_ACCEL_DEV_NAME,
40 	},
41 	{
42 		.compatible = "st,lsm330d-accel",
43 		.data = LSM330D_ACCEL_DEV_NAME,
44 	},
45 	{
46 		.compatible = "st,lsm330dl-accel",
47 		.data = LSM330DL_ACCEL_DEV_NAME,
48 	},
49 	{
50 		.compatible = "st,lsm330dlc-accel",
51 		.data = LSM330DLC_ACCEL_DEV_NAME,
52 	},
53 	{
54 		.compatible = "st,lis331dl-accel",
55 		.data = LIS331DL_ACCEL_DEV_NAME,
56 	},
57 	{
58 		.compatible = "st,lis331dlh-accel",
59 		.data = LIS331DLH_ACCEL_DEV_NAME,
60 	},
61 	{
62 		.compatible = "st,lsm303dl-accel",
63 		.data = LSM303DL_ACCEL_DEV_NAME,
64 	},
65 	{
66 		.compatible = "st,lsm303dlm-accel",
67 		.data = LSM303DLM_ACCEL_DEV_NAME,
68 	},
69 	{
70 		.compatible = "st,lsm330-accel",
71 		.data = LSM330_ACCEL_DEV_NAME,
72 	},
73 	{
74 		.compatible = "st,lsm303agr-accel",
75 		.data = LSM303AGR_ACCEL_DEV_NAME,
76 	},
77 	{
78 		.compatible = "st,lis2dh12-accel",
79 		.data = LIS2DH12_ACCEL_DEV_NAME,
80 	},
81 	{
82 		.compatible = "st,h3lis331dl-accel",
83 		.data = H3LIS331DL_ACCEL_DEV_NAME,
84 	},
85 	{
86 		.compatible = "st,lis3l02dq",
87 		.data = LIS3L02DQ_ACCEL_DEV_NAME,
88 	},
89 	{
90 		.compatible = "st,lng2dm-accel",
91 		.data = LNG2DM_ACCEL_DEV_NAME,
92 	},
93 	{
94 		.compatible = "st,lis2dw12",
95 		.data = LIS2DW12_ACCEL_DEV_NAME,
96 	},
97 	{
98 		.compatible = "st,lis3de",
99 		.data = LIS3DE_ACCEL_DEV_NAME,
100 	},
101 	{
102 		.compatible = "st,lis2de12",
103 		.data = LIS2DE12_ACCEL_DEV_NAME,
104 	},
105 	{
106 		.compatible = "st,lis2hh12",
107 		.data = LIS2HH12_ACCEL_DEV_NAME,
108 	},
109 	{
110 		.compatible = "st,lis302dl",
111 		.data = LIS302DL_ACCEL_DEV_NAME,
112 	},
113 	{
114 		.compatible = "st,lsm303c-accel",
115 		.data = LSM303C_ACCEL_DEV_NAME,
116 	},
117 	{
118 		.compatible = "silan,sc7a20",
119 		.data = SC7A20_ACCEL_DEV_NAME,
120 	},
121 	{
122 		.compatible = "st,iis328dq",
123 		.data = IIS328DQ_ACCEL_DEV_NAME,
124 	},
125 	{},
126 };
127 MODULE_DEVICE_TABLE(of, st_accel_of_match);
128 
129 static const struct acpi_device_id st_accel_acpi_match[] = {
130 	{"SMO8840", (kernel_ulong_t)LIS2DH12_ACCEL_DEV_NAME},
131 	{"SMO8A90", (kernel_ulong_t)LNG2DM_ACCEL_DEV_NAME},
132 	{ },
133 };
134 MODULE_DEVICE_TABLE(acpi, st_accel_acpi_match);
135 
136 static const struct i2c_device_id st_accel_id_table[] = {
137 	{ LSM303DLH_ACCEL_DEV_NAME },
138 	{ LSM303DLHC_ACCEL_DEV_NAME },
139 	{ LIS3DH_ACCEL_DEV_NAME },
140 	{ LSM330D_ACCEL_DEV_NAME },
141 	{ LSM330DL_ACCEL_DEV_NAME },
142 	{ LSM330DLC_ACCEL_DEV_NAME },
143 	{ LIS331DLH_ACCEL_DEV_NAME },
144 	{ LSM303DL_ACCEL_DEV_NAME },
145 	{ LSM303DLM_ACCEL_DEV_NAME },
146 	{ LSM330_ACCEL_DEV_NAME },
147 	{ LSM303AGR_ACCEL_DEV_NAME },
148 	{ LIS2DH12_ACCEL_DEV_NAME },
149 	{ LIS3L02DQ_ACCEL_DEV_NAME },
150 	{ LNG2DM_ACCEL_DEV_NAME },
151 	{ H3LIS331DL_ACCEL_DEV_NAME },
152 	{ LIS331DL_ACCEL_DEV_NAME },
153 	{ LIS3LV02DL_ACCEL_DEV_NAME },
154 	{ LIS2DW12_ACCEL_DEV_NAME },
155 	{ LIS3DE_ACCEL_DEV_NAME },
156 	{ LIS2DE12_ACCEL_DEV_NAME },
157 	{ LIS2HH12_ACCEL_DEV_NAME },
158 	{ LIS302DL_ACCEL_DEV_NAME },
159 	{ LSM303C_ACCEL_DEV_NAME },
160 	{ SC7A20_ACCEL_DEV_NAME },
161 	{ IIS328DQ_ACCEL_DEV_NAME },
162 	{},
163 };
164 MODULE_DEVICE_TABLE(i2c, st_accel_id_table);
165 
166 static int st_accel_i2c_probe(struct i2c_client *client)
167 {
168 	const struct st_sensor_settings *settings;
169 	struct st_sensor_data *adata;
170 	struct iio_dev *indio_dev;
171 	int ret;
172 
173 	st_sensors_dev_name_probe(&client->dev, client->name, sizeof(client->name));
174 
175 	settings = st_accel_get_settings(client->name);
176 	if (!settings) {
177 		dev_err(&client->dev, "device name %s not recognized.\n",
178 			client->name);
179 		return -ENODEV;
180 	}
181 
182 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*adata));
183 	if (!indio_dev)
184 		return -ENOMEM;
185 
186 	adata = iio_priv(indio_dev);
187 	adata->sensor_settings = (struct st_sensor_settings *)settings;
188 
189 	ret = st_sensors_i2c_configure(indio_dev, client);
190 	if (ret < 0)
191 		return ret;
192 
193 	ret = st_sensors_power_enable(indio_dev);
194 	if (ret)
195 		return ret;
196 
197 	return st_accel_common_probe(indio_dev);
198 }
199 
200 static struct i2c_driver st_accel_driver = {
201 	.driver = {
202 		.name = "st-accel-i2c",
203 		.of_match_table = st_accel_of_match,
204 		.acpi_match_table = st_accel_acpi_match,
205 	},
206 	.probe = st_accel_i2c_probe,
207 	.id_table = st_accel_id_table,
208 };
209 module_i2c_driver(st_accel_driver);
210 
211 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
212 MODULE_DESCRIPTION("STMicroelectronics accelerometers i2c driver");
213 MODULE_LICENSE("GPL v2");
214 MODULE_IMPORT_NS(IIO_ST_SENSORS);
215