Merge tag 'iio-for-5.6a' of git://git.kernel.org/pub/scm/linux/kernel/git/jic23/iio...
[linux-2.6-microblaze.git] / drivers / iio / pressure / st_pressure_i2c.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics pressures driver
4  *
5  * Copyright 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
17 #include <linux/iio/common/st_sensors.h>
18 #include <linux/iio/common/st_sensors_i2c.h>
19 #include "st_pressure.h"
20
21 #ifdef CONFIG_OF
22 static const struct of_device_id st_press_of_match[] = {
23         {
24                 .compatible = "st,lps001wp-press",
25                 .data = LPS001WP_PRESS_DEV_NAME,
26         },
27         {
28                 .compatible = "st,lps25h-press",
29                 .data = LPS25H_PRESS_DEV_NAME,
30         },
31         {
32                 .compatible = "st,lps331ap-press",
33                 .data = LPS331AP_PRESS_DEV_NAME,
34         },
35         {
36                 .compatible = "st,lps22hb-press",
37                 .data = LPS22HB_PRESS_DEV_NAME,
38         },
39         {
40                 .compatible = "st,lps33hw",
41                 .data = LPS33HW_PRESS_DEV_NAME,
42         },
43         {
44                 .compatible = "st,lps35hw",
45                 .data = LPS35HW_PRESS_DEV_NAME,
46         },
47         {
48                 .compatible = "st,lps22hh",
49                 .data = LPS22HH_PRESS_DEV_NAME,
50         },
51         {},
52 };
53 MODULE_DEVICE_TABLE(of, st_press_of_match);
54 #else
55 #define st_press_of_match NULL
56 #endif
57
58 #ifdef CONFIG_ACPI
59 static const struct acpi_device_id st_press_acpi_match[] = {
60         {"SNO9210", LPS22HB},
61         { },
62 };
63 MODULE_DEVICE_TABLE(acpi, st_press_acpi_match);
64 #endif
65
66 static const struct i2c_device_id st_press_id_table[] = {
67         { LPS001WP_PRESS_DEV_NAME, LPS001WP },
68         { LPS25H_PRESS_DEV_NAME,  LPS25H },
69         { LPS331AP_PRESS_DEV_NAME, LPS331AP },
70         { LPS22HB_PRESS_DEV_NAME, LPS22HB },
71         { LPS33HW_PRESS_DEV_NAME, LPS33HW },
72         { LPS35HW_PRESS_DEV_NAME, LPS35HW },
73         { LPS22HH_PRESS_DEV_NAME, LPS22HH },
74         {},
75 };
76 MODULE_DEVICE_TABLE(i2c, st_press_id_table);
77
78 static int st_press_i2c_probe(struct i2c_client *client,
79                               const struct i2c_device_id *id)
80 {
81         const struct st_sensor_settings *settings;
82         struct st_sensor_data *press_data;
83         struct iio_dev *indio_dev;
84         int ret;
85
86         if (client->dev.of_node) {
87                 st_sensors_of_name_probe(&client->dev, st_press_of_match,
88                                          client->name, sizeof(client->name));
89         } else if (ACPI_HANDLE(&client->dev)) {
90                 ret = st_sensors_match_acpi_device(&client->dev);
91                 if ((ret < 0) || (ret >= ST_PRESS_MAX))
92                         return -ENODEV;
93
94                 strlcpy(client->name, st_press_id_table[ret].name,
95                         sizeof(client->name));
96         } else if (!id)
97                 return -ENODEV;
98
99         settings = st_press_get_settings(client->name);
100         if (!settings) {
101                 dev_err(&client->dev, "device name %s not recognized.\n",
102                         client->name);
103                 return -ENODEV;
104         }
105
106         indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*press_data));
107         if (!indio_dev)
108                 return -ENOMEM;
109
110         press_data = iio_priv(indio_dev);
111         press_data->sensor_settings = (struct st_sensor_settings *)settings;
112
113         ret = st_sensors_i2c_configure(indio_dev, client);
114         if (ret < 0)
115                 return ret;
116
117         ret = st_press_common_probe(indio_dev);
118         if (ret < 0)
119                 return ret;
120
121         return 0;
122 }
123
124 static int st_press_i2c_remove(struct i2c_client *client)
125 {
126         st_press_common_remove(i2c_get_clientdata(client));
127
128         return 0;
129 }
130
131 static struct i2c_driver st_press_driver = {
132         .driver = {
133                 .name = "st-press-i2c",
134                 .of_match_table = of_match_ptr(st_press_of_match),
135                 .acpi_match_table = ACPI_PTR(st_press_acpi_match),
136         },
137         .probe = st_press_i2c_probe,
138         .remove = st_press_i2c_remove,
139         .id_table = st_press_id_table,
140 };
141 module_i2c_driver(st_press_driver);
142
143 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
144 MODULE_DESCRIPTION("STMicroelectronics pressures i2c driver");
145 MODULE_LICENSE("GPL v2");