xref: /openbmc/linux/drivers/iio/gyro/st_gyro_spi.c (revision aeb64ff3)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics gyroscopes 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/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_gyro.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>-gyro to maintain
24  * compatibility
25  */
26 static const struct of_device_id st_gyro_of_match[] = {
27 	{
28 		.compatible = "st,l3g4200d-gyro",
29 		.data = L3G4200D_GYRO_DEV_NAME,
30 	},
31 	{
32 		.compatible = "st,lsm330d-gyro",
33 		.data = LSM330D_GYRO_DEV_NAME,
34 	},
35 	{
36 		.compatible = "st,lsm330dl-gyro",
37 		.data = LSM330DL_GYRO_DEV_NAME,
38 	},
39 	{
40 		.compatible = "st,lsm330dlc-gyro",
41 		.data = LSM330DLC_GYRO_DEV_NAME,
42 	},
43 	{
44 		.compatible = "st,l3gd20-gyro",
45 		.data = L3GD20_GYRO_DEV_NAME,
46 	},
47 	{
48 		.compatible = "st,l3gd20h-gyro",
49 		.data = L3GD20H_GYRO_DEV_NAME,
50 	},
51 	{
52 		.compatible = "st,l3g4is-gyro",
53 		.data = L3G4IS_GYRO_DEV_NAME,
54 	},
55 	{
56 		.compatible = "st,lsm330-gyro",
57 		.data = LSM330_GYRO_DEV_NAME,
58 	},
59 	{
60 		.compatible = "st,lsm9ds0-gyro",
61 		.data = LSM9DS0_GYRO_DEV_NAME,
62 	},
63 	{},
64 };
65 MODULE_DEVICE_TABLE(of, st_gyro_of_match);
66 #else
67 #define st_gyro_of_match	NULL
68 #endif
69 
70 static int st_gyro_spi_probe(struct spi_device *spi)
71 {
72 	const struct st_sensor_settings *settings;
73 	struct st_sensor_data *gdata;
74 	struct iio_dev *indio_dev;
75 	int err;
76 
77 	st_sensors_of_name_probe(&spi->dev, st_gyro_of_match,
78 				 spi->modalias, sizeof(spi->modalias));
79 
80 	settings = st_gyro_get_settings(spi->modalias);
81 	if (!settings) {
82 		dev_err(&spi->dev, "device name %s not recognized.\n",
83 			spi->modalias);
84 		return -ENODEV;
85 	}
86 
87 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*gdata));
88 	if (!indio_dev)
89 		return -ENOMEM;
90 
91 	gdata = iio_priv(indio_dev);
92 	gdata->sensor_settings = (struct st_sensor_settings *)settings;
93 
94 	err = st_sensors_spi_configure(indio_dev, spi);
95 	if (err < 0)
96 		return err;
97 
98 	err = st_gyro_common_probe(indio_dev);
99 	if (err < 0)
100 		return err;
101 
102 	return 0;
103 }
104 
105 static int st_gyro_spi_remove(struct spi_device *spi)
106 {
107 	st_gyro_common_remove(spi_get_drvdata(spi));
108 
109 	return 0;
110 }
111 
112 static const struct spi_device_id st_gyro_id_table[] = {
113 	{ L3G4200D_GYRO_DEV_NAME },
114 	{ LSM330D_GYRO_DEV_NAME },
115 	{ LSM330DL_GYRO_DEV_NAME },
116 	{ LSM330DLC_GYRO_DEV_NAME },
117 	{ L3GD20_GYRO_DEV_NAME },
118 	{ L3GD20H_GYRO_DEV_NAME },
119 	{ L3G4IS_GYRO_DEV_NAME },
120 	{ LSM330_GYRO_DEV_NAME },
121 	{ LSM9DS0_GYRO_DEV_NAME },
122 	{},
123 };
124 MODULE_DEVICE_TABLE(spi, st_gyro_id_table);
125 
126 static struct spi_driver st_gyro_driver = {
127 	.driver = {
128 		.name = "st-gyro-spi",
129 		.of_match_table = of_match_ptr(st_gyro_of_match),
130 	},
131 	.probe = st_gyro_spi_probe,
132 	.remove = st_gyro_spi_remove,
133 	.id_table = st_gyro_id_table,
134 };
135 module_spi_driver(st_gyro_driver);
136 
137 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
138 MODULE_DESCRIPTION("STMicroelectronics gyroscopes spi driver");
139 MODULE_LICENSE("GPL v2");
140