blob: 7692da49e59f3f2cf68bceb9bc1b54e665753d64 [file] [log] [blame]
Denis Cioccad6251162013-01-25 23:44:00 +00001/*
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>
Denis Cioccad6251162013-01-25 23:44:00 +000016
17#include <linux/iio/common/st_sensors.h>
18#include <linux/iio/common/st_sensors_spi.h>
19#include "st_accel.h"
20
Lorenzo Bianconiff73e862017-06-27 00:53:43 +020021#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 */
27static 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 {
Lorenzo Bianconi2b96d662017-07-08 13:00:17 +020034 .compatible = "st,lis3lv02dl-accel",
35 .data = LIS3LV02DL_ACCEL_DEV_NAME,
36 },
37 {
Lorenzo Bianconiff73e862017-06-27 00:53:43 +020038 .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 },
Lorenzo Bianconi2b96d662017-07-08 13:00:17 +020077 {
78 .compatible = "st,h3lis331dl-accel",
Lorenzo Bianconib30f1592017-07-08 13:00:18 +020079 .data = H3LIS331DL_ACCEL_DEV_NAME,
Lorenzo Bianconi2b96d662017-07-08 13:00:17 +020080 },
81 {
82 .compatible = "st,lis331dl-accel",
83 .data = LIS331DL_ACCEL_DEV_NAME,
84 },
Lorenzo Bianconif94124f2017-08-30 13:50:42 +020085 {
86 .compatible = "st,lis2dw12",
87 .data = LIS2DW12_ACCEL_DEV_NAME,
88 },
Lorenzo Bianconiff73e862017-06-27 00:53:43 +020089 {}
90};
91MODULE_DEVICE_TABLE(of, st_accel_of_match);
92#else
93#define st_accel_of_match NULL
94#endif
95
Denis Cioccad6251162013-01-25 23:44:00 +000096static int st_accel_spi_probe(struct spi_device *spi)
97{
98 struct iio_dev *indio_dev;
99 struct st_sensor_data *adata;
100 int err;
101
Sachin Kamat6b7e0a92013-07-23 07:47:00 +0100102 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*adata));
103 if (!indio_dev)
104 return -ENOMEM;
Denis Cioccad6251162013-01-25 23:44:00 +0000105
106 adata = iio_priv(indio_dev);
Denis Cioccad6251162013-01-25 23:44:00 +0000107
Lorenzo Bianconiff73e862017-06-27 00:53:43 +0200108 st_sensors_of_name_probe(&spi->dev, st_accel_of_match,
109 spi->modalias, sizeof(spi->modalias));
Denis Cioccad6251162013-01-25 23:44:00 +0000110 st_sensors_spi_configure(indio_dev, spi, adata);
111
Denis CIOCCAb6e6bda62014-10-03 17:35:36 +0200112 err = st_accel_common_probe(indio_dev);
Denis Cioccad6251162013-01-25 23:44:00 +0000113 if (err < 0)
Sachin Kamat6b7e0a92013-07-23 07:47:00 +0100114 return err;
Denis Cioccad6251162013-01-25 23:44:00 +0000115
116 return 0;
Denis Cioccad6251162013-01-25 23:44:00 +0000117}
118
119static int st_accel_spi_remove(struct spi_device *spi)
120{
121 st_accel_common_remove(spi_get_drvdata(spi));
122
123 return 0;
124}
125
126static const struct spi_device_id st_accel_id_table[] = {
Denis Cioccad6251162013-01-25 23:44:00 +0000127 { LIS3DH_ACCEL_DEV_NAME },
128 { LSM330D_ACCEL_DEV_NAME },
129 { LSM330DL_ACCEL_DEV_NAME },
130 { LSM330DLC_ACCEL_DEV_NAME },
131 { LIS331DLH_ACCEL_DEV_NAME },
Denis Cioccad6251162013-01-25 23:44:00 +0000132 { LSM330_ACCEL_DEV_NAME },
Giuseppe Barbaddc05fa2015-07-21 10:35:44 +0200133 { LSM303AGR_ACCEL_DEV_NAME },
Giuseppe Barba34dc5782015-11-12 08:36:49 +0100134 { LIS2DH12_ACCEL_DEV_NAME },
Jonathan Cameron4e68cfb2016-05-22 20:39:29 +0100135 { LIS3L02DQ_ACCEL_DEV_NAME },
Lorenzo Bianconidcdb0a72016-10-25 23:09:03 +0200136 { LNG2DM_ACCEL_DEV_NAME },
Lorenzo Bianconib30f1592017-07-08 13:00:18 +0200137 { H3LIS331DL_ACCEL_DEV_NAME },
Lorenzo Bianconi2b96d662017-07-08 13:00:17 +0200138 { LIS331DL_ACCEL_DEV_NAME },
139 { LIS3LV02DL_ACCEL_DEV_NAME },
Lorenzo Bianconif94124f2017-08-30 13:50:42 +0200140 { LIS2DW12_ACCEL_DEV_NAME },
Denis Cioccad6251162013-01-25 23:44:00 +0000141 {},
142};
143MODULE_DEVICE_TABLE(spi, st_accel_id_table);
144
145static struct spi_driver st_accel_driver = {
146 .driver = {
Denis Cioccad6251162013-01-25 23:44:00 +0000147 .name = "st-accel-spi",
Lorenzo Bianconic24f83b2017-06-20 21:52:09 +0200148 .of_match_table = of_match_ptr(st_accel_of_match),
Denis Cioccad6251162013-01-25 23:44:00 +0000149 },
150 .probe = st_accel_spi_probe,
151 .remove = st_accel_spi_remove,
152 .id_table = st_accel_id_table,
153};
154module_spi_driver(st_accel_driver);
155
156MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
157MODULE_DESCRIPTION("STMicroelectronics accelerometers spi driver");
158MODULE_LICENSE("GPL v2");