xref: /openbmc/linux/drivers/iio/accel/st_accel_spi.c (revision ae213c44)
1 /*
2  * STMicroelectronics accelerometers driver
3  *
4  * Copyright 2012-2013 STMicroelectronics Inc.
5  *
6  * Denis Ciocca <denis.ciocca@st.com>
7  *
8  * Licensed under the GPL-2.
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/spi/spi.h>
15 #include <linux/iio/iio.h>
16 
17 #include <linux/iio/common/st_sensors.h>
18 #include <linux/iio/common/st_sensors_spi.h>
19 #include "st_accel.h"
20 
21 #ifdef CONFIG_OF
22 /*
23  * For new single-chip sensors use <device_name> as compatible string.
24  * For old single-chip devices keep <device_name>-accel to maintain
25  * compatibility
26  */
27 static const struct of_device_id st_accel_of_match[] = {
28 	{
29 		/* An older compatible */
30 		.compatible = "st,lis302dl-spi",
31 		.data = LIS3LV02DL_ACCEL_DEV_NAME,
32 	},
33 	{
34 		.compatible = "st,lis3lv02dl-accel",
35 		.data = LIS3LV02DL_ACCEL_DEV_NAME,
36 	},
37 	{
38 		.compatible = "st,lis3dh-accel",
39 		.data = LIS3DH_ACCEL_DEV_NAME,
40 	},
41 	{
42 		.compatible = "st,lsm330d-accel",
43 		.data = LSM330D_ACCEL_DEV_NAME,
44 	},
45 	{
46 		.compatible = "st,lsm330dl-accel",
47 		.data = LSM330DL_ACCEL_DEV_NAME,
48 	},
49 	{
50 		.compatible = "st,lsm330dlc-accel",
51 		.data = LSM330DLC_ACCEL_DEV_NAME,
52 	},
53 	{
54 		.compatible = "st,lis331dlh-accel",
55 		.data = LIS331DLH_ACCEL_DEV_NAME,
56 	},
57 	{
58 		.compatible = "st,lsm330-accel",
59 		.data = LSM330_ACCEL_DEV_NAME,
60 	},
61 	{
62 		.compatible = "st,lsm303agr-accel",
63 		.data = LSM303AGR_ACCEL_DEV_NAME,
64 	},
65 	{
66 		.compatible = "st,lis2dh12-accel",
67 		.data = LIS2DH12_ACCEL_DEV_NAME,
68 	},
69 	{
70 		.compatible = "st,lis3l02dq",
71 		.data = LIS3L02DQ_ACCEL_DEV_NAME,
72 	},
73 	{
74 		.compatible = "st,lng2dm-accel",
75 		.data = LNG2DM_ACCEL_DEV_NAME,
76 	},
77 	{
78 		.compatible = "st,h3lis331dl-accel",
79 		.data = H3LIS331DL_ACCEL_DEV_NAME,
80 	},
81 	{
82 		.compatible = "st,lis331dl-accel",
83 		.data = LIS331DL_ACCEL_DEV_NAME,
84 	},
85 	{
86 		.compatible = "st,lis2dw12",
87 		.data = LIS2DW12_ACCEL_DEV_NAME,
88 	},
89 	{
90 		.compatible = "st,lis3dhh",
91 		.data = LIS3DHH_ACCEL_DEV_NAME,
92 	},
93 	{
94 		.compatible = "st,lis3de",
95 		.data = LIS3DE_ACCEL_DEV_NAME,
96 	},
97 	{}
98 };
99 MODULE_DEVICE_TABLE(of, st_accel_of_match);
100 #else
101 #define st_accel_of_match	NULL
102 #endif
103 
104 static int st_accel_spi_probe(struct spi_device *spi)
105 {
106 	struct iio_dev *indio_dev;
107 	struct st_sensor_data *adata;
108 	int err;
109 
110 	indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adata));
111 	if (!indio_dev)
112 		return -ENOMEM;
113 
114 	adata = iio_priv(indio_dev);
115 
116 	st_sensors_of_name_probe(&spi->dev, st_accel_of_match,
117 				 spi->modalias, sizeof(spi->modalias));
118 	st_sensors_spi_configure(indio_dev, spi, adata);
119 
120 	err = st_accel_common_probe(indio_dev);
121 	if (err < 0)
122 		return err;
123 
124 	return 0;
125 }
126 
127 static int st_accel_spi_remove(struct spi_device *spi)
128 {
129 	st_accel_common_remove(spi_get_drvdata(spi));
130 
131 	return 0;
132 }
133 
134 static const struct spi_device_id st_accel_id_table[] = {
135 	{ LIS3DH_ACCEL_DEV_NAME },
136 	{ LSM330D_ACCEL_DEV_NAME },
137 	{ LSM330DL_ACCEL_DEV_NAME },
138 	{ LSM330DLC_ACCEL_DEV_NAME },
139 	{ LIS331DLH_ACCEL_DEV_NAME },
140 	{ LSM330_ACCEL_DEV_NAME },
141 	{ LSM303AGR_ACCEL_DEV_NAME },
142 	{ LIS2DH12_ACCEL_DEV_NAME },
143 	{ LIS3L02DQ_ACCEL_DEV_NAME },
144 	{ LNG2DM_ACCEL_DEV_NAME },
145 	{ H3LIS331DL_ACCEL_DEV_NAME },
146 	{ LIS331DL_ACCEL_DEV_NAME },
147 	{ LIS3LV02DL_ACCEL_DEV_NAME },
148 	{ LIS2DW12_ACCEL_DEV_NAME },
149 	{ LIS3DHH_ACCEL_DEV_NAME },
150 	{ LIS3DE_ACCEL_DEV_NAME },
151 	{},
152 };
153 MODULE_DEVICE_TABLE(spi, st_accel_id_table);
154 
155 static struct spi_driver st_accel_driver = {
156 	.driver = {
157 		.name = "st-accel-spi",
158 		.of_match_table = of_match_ptr(st_accel_of_match),
159 	},
160 	.probe = st_accel_spi_probe,
161 	.remove = st_accel_spi_remove,
162 	.id_table = st_accel_id_table,
163 };
164 module_spi_driver(st_accel_driver);
165 
166 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
167 MODULE_DESCRIPTION("STMicroelectronics accelerometers spi driver");
168 MODULE_LICENSE("GPL v2");
169