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