blob: c04bcf2518c1188ab89020022b90ad2c97004346 [file] [log] [blame]
Thomas Gleixnerfda8d262019-05-28 09:57:06 -07001// SPDX-License-Identifier: GPL-2.0-only
Denis Ciocca7be56a82013-01-25 23:44:00 +00002/*
3 * STMicroelectronics gyroscopes driver
4 *
5 * Copyright 2012-2013 STMicroelectronics Inc.
6 *
7 * Denis Ciocca <denis.ciocca@st.com>
Denis Ciocca7be56a82013-01-25 23:44:00 +00008 */
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>
Denis Ciocca7be56a82013-01-25 23:44:00 +000015
16#include <linux/iio/common/st_sensors.h>
17#include <linux/iio/common/st_sensors_spi.h>
18#include "st_gyro.h"
19
Lorenzo Bianconi794694c2017-06-27 00:53:45 +020020/*
21 * For new single-chip sensors use <device_name> as compatible string.
22 * For old single-chip devices keep <device_name>-gyro to maintain
23 * compatibility
24 */
25static const struct of_device_id st_gyro_of_match[] = {
26 {
27 .compatible = "st,l3g4200d-gyro",
28 .data = L3G4200D_GYRO_DEV_NAME,
29 },
30 {
31 .compatible = "st,lsm330d-gyro",
32 .data = LSM330D_GYRO_DEV_NAME,
33 },
34 {
35 .compatible = "st,lsm330dl-gyro",
36 .data = LSM330DL_GYRO_DEV_NAME,
37 },
38 {
39 .compatible = "st,lsm330dlc-gyro",
40 .data = LSM330DLC_GYRO_DEV_NAME,
41 },
42 {
43 .compatible = "st,l3gd20-gyro",
44 .data = L3GD20_GYRO_DEV_NAME,
45 },
46 {
47 .compatible = "st,l3gd20h-gyro",
48 .data = L3GD20H_GYRO_DEV_NAME,
49 },
50 {
51 .compatible = "st,l3g4is-gyro",
52 .data = L3G4IS_GYRO_DEV_NAME,
53 },
54 {
55 .compatible = "st,lsm330-gyro",
56 .data = LSM330_GYRO_DEV_NAME,
57 },
58 {
59 .compatible = "st,lsm9ds0-gyro",
60 .data = LSM9DS0_GYRO_DEV_NAME,
61 },
62 {},
63};
64MODULE_DEVICE_TABLE(of, st_gyro_of_match);
Lorenzo Bianconi794694c2017-06-27 00:53:45 +020065
Denis Ciocca7be56a82013-01-25 23:44:00 +000066static int st_gyro_spi_probe(struct spi_device *spi)
67{
Denis Ciocca3c054842019-07-18 15:53:49 -070068 const struct st_sensor_settings *settings;
Denis Ciocca7be56a82013-01-25 23:44:00 +000069 struct st_sensor_data *gdata;
Denis Ciocca3c054842019-07-18 15:53:49 -070070 struct iio_dev *indio_dev;
Denis Ciocca7be56a82013-01-25 23:44:00 +000071 int err;
72
Andy Shevchenkoefc78982019-12-16 19:38:52 +020073 st_sensors_dev_name_probe(&spi->dev, spi->modalias, sizeof(spi->modalias));
Denis Ciocca3c054842019-07-18 15:53:49 -070074
75 settings = st_gyro_get_settings(spi->modalias);
76 if (!settings) {
77 dev_err(&spi->dev, "device name %s not recognized.\n",
78 spi->modalias);
79 return -ENODEV;
80 }
81
Sachin Kamat7a842ed2013-08-13 07:34:00 +010082 indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*gdata));
83 if (!indio_dev)
84 return -ENOMEM;
Denis Ciocca7be56a82013-01-25 23:44:00 +000085
86 gdata = iio_priv(indio_dev);
Denis Ciocca3c054842019-07-18 15:53:49 -070087 gdata->sensor_settings = (struct st_sensor_settings *)settings;
Denis Ciocca7be56a82013-01-25 23:44:00 +000088
Denis Ciocca1ecd2452019-07-18 15:53:52 -070089 err = st_sensors_spi_configure(indio_dev, spi);
90 if (err < 0)
91 return err;
Denis Ciocca7be56a82013-01-25 23:44:00 +000092
Andy Shevchenkobfedc812021-04-14 22:54:51 +030093 err = st_sensors_power_enable(indio_dev);
94 if (err)
Sachin Kamat7a842ed2013-08-13 07:34:00 +010095 return err;
Denis Ciocca7be56a82013-01-25 23:44:00 +000096
Andy Shevchenkobfedc812021-04-14 22:54:51 +030097 err = st_gyro_common_probe(indio_dev);
98 if (err < 0)
99 goto st_gyro_power_off;
100
Denis Ciocca7be56a82013-01-25 23:44:00 +0000101 return 0;
Andy Shevchenkobfedc812021-04-14 22:54:51 +0300102
103st_gyro_power_off:
104 st_sensors_power_disable(indio_dev);
105
106 return err;
Denis Ciocca7be56a82013-01-25 23:44:00 +0000107}
108
109static int st_gyro_spi_remove(struct spi_device *spi)
110{
Andy Shevchenkobfedc812021-04-14 22:54:51 +0300111 struct iio_dev *indio_dev = spi_get_drvdata(spi);
112
Andy Shevchenkobfedc812021-04-14 22:54:51 +0300113 st_gyro_common_remove(indio_dev);
Denis Ciocca7be56a82013-01-25 23:44:00 +0000114
Alexandru Ardeleana2c17c92021-08-23 14:22:00 +0300115 st_sensors_power_disable(indio_dev);
116
Denis Ciocca7be56a82013-01-25 23:44:00 +0000117 return 0;
118}
119
120static const struct spi_device_id st_gyro_id_table[] = {
121 { L3G4200D_GYRO_DEV_NAME },
122 { LSM330D_GYRO_DEV_NAME },
123 { LSM330DL_GYRO_DEV_NAME },
124 { LSM330DLC_GYRO_DEV_NAME },
125 { L3GD20_GYRO_DEV_NAME },
Lorenzo Bianconi45a4e422017-06-19 22:04:30 +0200126 { L3GD20H_GYRO_DEV_NAME },
Denis Ciocca7be56a82013-01-25 23:44:00 +0000127 { L3G4IS_GYRO_DEV_NAME },
128 { LSM330_GYRO_DEV_NAME },
Crestez Dan Leonard41c128c2016-04-19 15:02:11 +0300129 { LSM9DS0_GYRO_DEV_NAME },
Denis Ciocca7be56a82013-01-25 23:44:00 +0000130 {},
131};
132MODULE_DEVICE_TABLE(spi, st_gyro_id_table);
133
134static struct spi_driver st_gyro_driver = {
135 .driver = {
Denis Ciocca7be56a82013-01-25 23:44:00 +0000136 .name = "st-gyro-spi",
Andy Shevchenkoecb27c52019-12-16 19:38:53 +0200137 .of_match_table = st_gyro_of_match,
Denis Ciocca7be56a82013-01-25 23:44:00 +0000138 },
139 .probe = st_gyro_spi_probe,
140 .remove = st_gyro_spi_remove,
141 .id_table = st_gyro_id_table,
142};
143module_spi_driver(st_gyro_driver);
144
145MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
146MODULE_DESCRIPTION("STMicroelectronics gyroscopes spi driver");
147MODULE_LICENSE("GPL v2");