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