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/spi/spi.h>
14 #include <linux/iio/iio.h>
15 
16 #include <linux/iio/common/st_sensors.h>
17 #include <linux/iio/common/st_sensors_spi.h>
18 #include "st_pressure.h"
19 
20 /*
21  * For new single-chip sensors use <device_name> as compatible string.
22  * For old single-chip devices keep <device_name>-press to maintain
23  * compatibility
24  */
25 static const struct of_device_id st_press_of_match[] = {
26 	{
27 		.compatible = "st,lps001wp-press",
28 		.data = LPS001WP_PRESS_DEV_NAME,
29 	},
30 	{
31 		.compatible = "st,lps25h-press",
32 		.data = LPS25H_PRESS_DEV_NAME,
33 	},
34 	{
35 		.compatible = "st,lps331ap-press",
36 		.data = LPS331AP_PRESS_DEV_NAME,
37 	},
38 	{
39 		.compatible = "st,lps22hb-press",
40 		.data = LPS22HB_PRESS_DEV_NAME,
41 	},
42 	{
43 		.compatible = "st,lps33hw",
44 		.data = LPS33HW_PRESS_DEV_NAME,
45 	},
46 	{
47 		.compatible = "st,lps35hw",
48 		.data = LPS35HW_PRESS_DEV_NAME,
49 	},
50 	{
51 		.compatible = "st,lps22hh",
52 		.data = LPS22HH_PRESS_DEV_NAME,
53 	},
54 	{},
55 };
56 MODULE_DEVICE_TABLE(of, st_press_of_match);
57 
58 static int st_press_spi_probe(struct spi_device *spi)
59 {
60 	const struct st_sensor_settings *settings;
61 	struct st_sensor_data *press_data;
62 	struct iio_dev *indio_dev;
63 	int err;
64 
65 	st_sensors_dev_name_probe(&spi->dev, spi->modalias, sizeof(spi->modalias));
66 
67 	settings = st_press_get_settings(spi->modalias);
68 	if (!settings) {
69 		dev_err(&spi->dev, "device name %s not recognized.\n",
70 			spi->modalias);
71 		return -ENODEV;
72 	}
73 
74 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*press_data));
75 	if (!indio_dev)
76 		return -ENOMEM;
77 
78 	press_data = iio_priv(indio_dev);
79 	press_data->sensor_settings = (struct st_sensor_settings *)settings;
80 
81 	err = st_sensors_spi_configure(indio_dev, spi);
82 	if (err < 0)
83 		return err;
84 
85 	err = st_sensors_power_enable(indio_dev);
86 	if (err)
87 		return err;
88 
89 	err = st_press_common_probe(indio_dev);
90 	if (err < 0)
91 		goto st_press_power_off;
92 
93 	return 0;
94 
95 st_press_power_off:
96 	st_sensors_power_disable(indio_dev);
97 
98 	return err;
99 }
100 
101 static int st_press_spi_remove(struct spi_device *spi)
102 {
103 	struct iio_dev *indio_dev = spi_get_drvdata(spi);
104 
105 	st_sensors_power_disable(indio_dev);
106 
107 	st_press_common_remove(indio_dev);
108 
109 	return 0;
110 }
111 
112 static const struct spi_device_id st_press_id_table[] = {
113 	{ LPS001WP_PRESS_DEV_NAME },
114 	{ LPS25H_PRESS_DEV_NAME },
115 	{ LPS331AP_PRESS_DEV_NAME },
116 	{ LPS22HB_PRESS_DEV_NAME },
117 	{ LPS33HW_PRESS_DEV_NAME },
118 	{ LPS35HW_PRESS_DEV_NAME },
119 	{ LPS22HH_PRESS_DEV_NAME },
120 	{},
121 };
122 MODULE_DEVICE_TABLE(spi, st_press_id_table);
123 
124 static struct spi_driver st_press_driver = {
125 	.driver = {
126 		.name = "st-press-spi",
127 		.of_match_table = st_press_of_match,
128 	},
129 	.probe = st_press_spi_probe,
130 	.remove = st_press_spi_remove,
131 	.id_table = st_press_id_table,
132 };
133 module_spi_driver(st_press_driver);
134 
135 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
136 MODULE_DESCRIPTION("STMicroelectronics pressures spi driver");
137 MODULE_LICENSE("GPL v2");
138