xref: /linux/drivers/iio/pressure/st_pressure_i2c.c (revision cd570e6fa43a5ed061b91e64dc507a6ac54fac95)
1217494e5SDenis CIOCCA /*
2217494e5SDenis CIOCCA  * STMicroelectronics pressures driver
3217494e5SDenis CIOCCA  *
4217494e5SDenis CIOCCA  * Copyright 2013 STMicroelectronics Inc.
5217494e5SDenis CIOCCA  *
6217494e5SDenis CIOCCA  * Denis Ciocca <denis.ciocca@st.com>
7217494e5SDenis CIOCCA  *
8217494e5SDenis CIOCCA  * Licensed under the GPL-2.
9217494e5SDenis CIOCCA  */
10217494e5SDenis CIOCCA 
11217494e5SDenis CIOCCA #include <linux/kernel.h>
12217494e5SDenis CIOCCA #include <linux/module.h>
13217494e5SDenis CIOCCA #include <linux/slab.h>
149d317724SShrirang Bagul #include <linux/acpi.h>
15217494e5SDenis CIOCCA #include <linux/i2c.h>
16217494e5SDenis CIOCCA #include <linux/iio/iio.h>
17217494e5SDenis CIOCCA 
18217494e5SDenis CIOCCA #include <linux/iio/common/st_sensors.h>
19217494e5SDenis CIOCCA #include <linux/iio/common/st_sensors_i2c.h>
20217494e5SDenis CIOCCA #include "st_pressure.h"
21217494e5SDenis CIOCCA 
222d7768a8SLinus Walleij #ifdef CONFIG_OF
232d7768a8SLinus Walleij static const struct of_device_id st_press_of_match[] = {
242d7768a8SLinus Walleij 	{
252d7768a8SLinus Walleij 		.compatible = "st,lps001wp-press",
262d7768a8SLinus Walleij 		.data = LPS001WP_PRESS_DEV_NAME,
272d7768a8SLinus Walleij 	},
282d7768a8SLinus Walleij 	{
292d7768a8SLinus Walleij 		.compatible = "st,lps25h-press",
302d7768a8SLinus Walleij 		.data = LPS25H_PRESS_DEV_NAME,
312d7768a8SLinus Walleij 	},
322d7768a8SLinus Walleij 	{
332d7768a8SLinus Walleij 		.compatible = "st,lps331ap-press",
342d7768a8SLinus Walleij 		.data = LPS331AP_PRESS_DEV_NAME,
352d7768a8SLinus Walleij 	},
36e039e2f5SGregor Boirie 	{
37e039e2f5SGregor Boirie 		.compatible = "st,lps22hb-press",
38e039e2f5SGregor Boirie 		.data = LPS22HB_PRESS_DEV_NAME,
39e039e2f5SGregor Boirie 	},
40b954d77aSLorenzo Bianconi 	{
41b954d77aSLorenzo Bianconi 		.compatible = "st,lps33hw",
42b954d77aSLorenzo Bianconi 		.data = LPS33HW_PRESS_DEV_NAME,
43b954d77aSLorenzo Bianconi 	},
44b954d77aSLorenzo Bianconi 	{
45b954d77aSLorenzo Bianconi 		.compatible = "st,lps35hw",
46b954d77aSLorenzo Bianconi 		.data = LPS35HW_PRESS_DEV_NAME,
47b954d77aSLorenzo Bianconi 	},
482d7768a8SLinus Walleij 	{},
492d7768a8SLinus Walleij };
502d7768a8SLinus Walleij MODULE_DEVICE_TABLE(of, st_press_of_match);
512d7768a8SLinus Walleij #else
522d7768a8SLinus Walleij #define st_press_of_match NULL
532d7768a8SLinus Walleij #endif
542d7768a8SLinus Walleij 
559d317724SShrirang Bagul #ifdef CONFIG_ACPI
569d317724SShrirang Bagul static const struct acpi_device_id st_press_acpi_match[] = {
579d317724SShrirang Bagul 	{"SNO9210", LPS22HB},
589d317724SShrirang Bagul 	{ },
599d317724SShrirang Bagul };
609d317724SShrirang Bagul MODULE_DEVICE_TABLE(acpi, st_press_acpi_match);
619d317724SShrirang Bagul #else
629d317724SShrirang Bagul #define st_press_acpi_match NULL
639d317724SShrirang Bagul #endif
649d317724SShrirang Bagul 
659d317724SShrirang Bagul static const struct i2c_device_id st_press_id_table[] = {
669d317724SShrirang Bagul 	{ LPS001WP_PRESS_DEV_NAME, LPS001WP },
679d317724SShrirang Bagul 	{ LPS25H_PRESS_DEV_NAME,  LPS25H },
689d317724SShrirang Bagul 	{ LPS331AP_PRESS_DEV_NAME, LPS331AP },
699d317724SShrirang Bagul 	{ LPS22HB_PRESS_DEV_NAME, LPS22HB },
70b954d77aSLorenzo Bianconi 	{ LPS33HW_PRESS_DEV_NAME, LPS33HW },
71b954d77aSLorenzo Bianconi 	{ LPS35HW_PRESS_DEV_NAME, LPS35HW },
729d317724SShrirang Bagul 	{},
739d317724SShrirang Bagul };
749d317724SShrirang Bagul MODULE_DEVICE_TABLE(i2c, st_press_id_table);
759d317724SShrirang Bagul 
76217494e5SDenis CIOCCA static int st_press_i2c_probe(struct i2c_client *client,
77217494e5SDenis CIOCCA 						const struct i2c_device_id *id)
78217494e5SDenis CIOCCA {
79217494e5SDenis CIOCCA 	struct iio_dev *indio_dev;
80a1dcf429SDenis CIOCCA 	struct st_sensor_data *press_data;
819d317724SShrirang Bagul 	int ret;
82217494e5SDenis CIOCCA 
83a1dcf429SDenis CIOCCA 	indio_dev = devm_iio_device_alloc(&client->dev, sizeof(*press_data));
84d0fe8c8bSSachin Kamat 	if (!indio_dev)
85d0fe8c8bSSachin Kamat 		return -ENOMEM;
86217494e5SDenis CIOCCA 
87a1dcf429SDenis CIOCCA 	press_data = iio_priv(indio_dev);
889d317724SShrirang Bagul 
899d317724SShrirang Bagul 	if (client->dev.of_node) {
90250bbbdbSLorenzo Bianconi 		st_sensors_of_name_probe(&client->dev, st_press_of_match,
91250bbbdbSLorenzo Bianconi 					 client->name, sizeof(client->name));
929d317724SShrirang Bagul 	} else if (ACPI_HANDLE(&client->dev)) {
939d317724SShrirang Bagul 		ret = st_sensors_match_acpi_device(&client->dev);
949d317724SShrirang Bagul 		if ((ret < 0) || (ret >= ST_PRESS_MAX))
959d317724SShrirang Bagul 			return -ENODEV;
969d317724SShrirang Bagul 
97*cd570e6fSDominique Martinet 		strlcpy(client->name, st_press_id_table[ret].name,
989d317724SShrirang Bagul 				sizeof(client->name));
999d317724SShrirang Bagul 	} else if (!id)
1009d317724SShrirang Bagul 		return -ENODEV;
101217494e5SDenis CIOCCA 
102a1dcf429SDenis CIOCCA 	st_sensors_i2c_configure(indio_dev, client, press_data);
103217494e5SDenis CIOCCA 
1049d317724SShrirang Bagul 	ret = st_press_common_probe(indio_dev);
1059d317724SShrirang Bagul 	if (ret < 0)
1069d317724SShrirang Bagul 		return ret;
107217494e5SDenis CIOCCA 
108217494e5SDenis CIOCCA 	return 0;
109217494e5SDenis CIOCCA }
110217494e5SDenis CIOCCA 
111217494e5SDenis CIOCCA static int st_press_i2c_remove(struct i2c_client *client)
112217494e5SDenis CIOCCA {
113217494e5SDenis CIOCCA 	st_press_common_remove(i2c_get_clientdata(client));
114217494e5SDenis CIOCCA 
115217494e5SDenis CIOCCA 	return 0;
116217494e5SDenis CIOCCA }
117217494e5SDenis CIOCCA 
118217494e5SDenis CIOCCA static struct i2c_driver st_press_driver = {
119217494e5SDenis CIOCCA 	.driver = {
120217494e5SDenis CIOCCA 		.name = "st-press-i2c",
1212d7768a8SLinus Walleij 		.of_match_table = of_match_ptr(st_press_of_match),
1229d317724SShrirang Bagul 		.acpi_match_table = ACPI_PTR(st_press_acpi_match),
123217494e5SDenis CIOCCA 	},
124217494e5SDenis CIOCCA 	.probe = st_press_i2c_probe,
125217494e5SDenis CIOCCA 	.remove = st_press_i2c_remove,
126217494e5SDenis CIOCCA 	.id_table = st_press_id_table,
127217494e5SDenis CIOCCA };
128217494e5SDenis CIOCCA module_i2c_driver(st_press_driver);
129217494e5SDenis CIOCCA 
130217494e5SDenis CIOCCA MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
131217494e5SDenis CIOCCA MODULE_DESCRIPTION("STMicroelectronics pressures i2c driver");
132217494e5SDenis CIOCCA MODULE_LICENSE("GPL v2");
133