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