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