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