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