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