xref: /linux/drivers/iio/accel/st_accel_spi.c (revision 3a2c4d55e32ad65efebdb6de44eef3bfa08bb49d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics accelerometers driver
4  *
5  * Copyright 2012-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/spi/spi.h>
13 #include <linux/iio/iio.h>
14 
15 #include <linux/iio/common/st_sensors.h>
16 #include <linux/iio/common/st_sensors_spi.h>
17 #include "st_accel.h"
18 
19 /*
20  * For new single-chip sensors use <device_name> as compatible string.
21  * For old single-chip devices keep <device_name>-accel to maintain
22  * compatibility
23  */
24 static const struct of_device_id st_accel_of_match[] = {
25 	{
26 		/* An older compatible */
27 		.compatible = "st,lis302dl-spi",
28 		.data = LIS3LV02DL_ACCEL_DEV_NAME,
29 	},
30 	{
31 		.compatible = "st,lis3lv02dl-accel",
32 		.data = LIS3LV02DL_ACCEL_DEV_NAME,
33 	},
34 	{
35 		.compatible = "st,lis3dh-accel",
36 		.data = LIS3DH_ACCEL_DEV_NAME,
37 	},
38 	{
39 		.compatible = "st,lsm330d-accel",
40 		.data = LSM330D_ACCEL_DEV_NAME,
41 	},
42 	{
43 		.compatible = "st,lsm330dl-accel",
44 		.data = LSM330DL_ACCEL_DEV_NAME,
45 	},
46 	{
47 		.compatible = "st,lsm330dlc-accel",
48 		.data = LSM330DLC_ACCEL_DEV_NAME,
49 	},
50 	{
51 		.compatible = "st,lis331dlh-accel",
52 		.data = LIS331DLH_ACCEL_DEV_NAME,
53 	},
54 	{
55 		.compatible = "st,lsm330-accel",
56 		.data = LSM330_ACCEL_DEV_NAME,
57 	},
58 	{
59 		.compatible = "st,lsm303agr-accel",
60 		.data = LSM303AGR_ACCEL_DEV_NAME,
61 	},
62 	{
63 		.compatible = "st,lis2dh12-accel",
64 		.data = LIS2DH12_ACCEL_DEV_NAME,
65 	},
66 	{
67 		.compatible = "st,lis2ds12",
68 		.data = LIS2DS12_ACCEL_DEV_NAME,
69 	},
70 	{
71 		.compatible = "st,lis3l02dq",
72 		.data = LIS3L02DQ_ACCEL_DEV_NAME,
73 	},
74 	{
75 		.compatible = "st,lng2dm-accel",
76 		.data = LNG2DM_ACCEL_DEV_NAME,
77 	},
78 	{
79 		.compatible = "st,h3lis331dl-accel",
80 		.data = H3LIS331DL_ACCEL_DEV_NAME,
81 	},
82 	{
83 		.compatible = "st,lis331dl-accel",
84 		.data = LIS331DL_ACCEL_DEV_NAME,
85 	},
86 	{
87 		.compatible = "st,lis2dw12",
88 		.data = LIS2DW12_ACCEL_DEV_NAME,
89 	},
90 	{
91 		.compatible = "st,lis3dhh",
92 		.data = LIS3DHH_ACCEL_DEV_NAME,
93 	},
94 	{
95 		.compatible = "st,lis3de",
96 		.data = LIS3DE_ACCEL_DEV_NAME,
97 	},
98 	{
99 		.compatible = "st,lis302dl",
100 		.data = LIS302DL_ACCEL_DEV_NAME,
101 	},
102 	{
103 		.compatible = "st,lsm303c-accel",
104 		.data = LSM303C_ACCEL_DEV_NAME,
105 	},
106 	{
107 		.compatible = "st,iis328dq",
108 		.data = IIS328DQ_ACCEL_DEV_NAME,
109 	},
110 	{ }
111 };
112 MODULE_DEVICE_TABLE(of, st_accel_of_match);
113 
114 static int st_accel_spi_probe(struct spi_device *spi)
115 {
116 	const struct st_sensor_settings *settings;
117 	struct st_sensor_data *adata;
118 	struct iio_dev *indio_dev;
119 	int err;
120 
121 	st_sensors_dev_name_probe(&spi->dev, spi->modalias, sizeof(spi->modalias));
122 
123 	settings = st_accel_get_settings(spi->modalias);
124 	if (!settings) {
125 		dev_err(&spi->dev, "device name %s not recognized.\n",
126 			spi->modalias);
127 		return -ENODEV;
128 	}
129 
130 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adata));
131 	if (!indio_dev)
132 		return -ENOMEM;
133 
134 	adata = iio_priv(indio_dev);
135 	adata->sensor_settings = (struct st_sensor_settings *)settings;
136 
137 	err = st_sensors_spi_configure(indio_dev, spi);
138 	if (err < 0)
139 		return err;
140 
141 	err = st_sensors_power_enable(indio_dev);
142 	if (err)
143 		return err;
144 
145 	return st_accel_common_probe(indio_dev);
146 }
147 
148 static const struct spi_device_id st_accel_id_table[] = {
149 	{ .name = LIS3DH_ACCEL_DEV_NAME },
150 	{ .name = LSM330D_ACCEL_DEV_NAME },
151 	{ .name = LSM330DL_ACCEL_DEV_NAME },
152 	{ .name = LSM330DLC_ACCEL_DEV_NAME },
153 	{ .name = LIS331DLH_ACCEL_DEV_NAME },
154 	{ .name = LSM330_ACCEL_DEV_NAME },
155 	{ .name = LSM303AGR_ACCEL_DEV_NAME },
156 	{ .name = LIS2DH12_ACCEL_DEV_NAME },
157 	{ .name = LIS2DS12_ACCEL_DEV_NAME },
158 	{ .name = LIS3L02DQ_ACCEL_DEV_NAME },
159 	{ .name = LNG2DM_ACCEL_DEV_NAME },
160 	{ .name = H3LIS331DL_ACCEL_DEV_NAME },
161 	{ .name = LIS331DL_ACCEL_DEV_NAME },
162 	{ .name = LIS3LV02DL_ACCEL_DEV_NAME },
163 	{ .name = LIS2DW12_ACCEL_DEV_NAME },
164 	{ .name = LIS3DHH_ACCEL_DEV_NAME },
165 	{ .name = LIS3DE_ACCEL_DEV_NAME },
166 	{ .name = LIS302DL_ACCEL_DEV_NAME },
167 	{ .name = LSM303C_ACCEL_DEV_NAME },
168 	{ .name = IIS328DQ_ACCEL_DEV_NAME },
169 	{ }
170 };
171 MODULE_DEVICE_TABLE(spi, st_accel_id_table);
172 
173 static struct spi_driver st_accel_driver = {
174 	.driver = {
175 		.name = "st-accel-spi",
176 		.of_match_table = st_accel_of_match,
177 	},
178 	.probe = st_accel_spi_probe,
179 	.id_table = st_accel_id_table,
180 };
181 module_spi_driver(st_accel_driver);
182 
183 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
184 MODULE_DESCRIPTION("STMicroelectronics accelerometers spi driver");
185 MODULE_LICENSE("GPL v2");
186 MODULE_IMPORT_NS("IIO_ST_SENSORS");
187