Thomas Gleixner | fda8d26 | 2019-05-28 09:57:06 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 2 | /* |
| 3 | * STMicroelectronics gyroscopes driver |
| 4 | * |
| 5 | * Copyright 2012-2013 STMicroelectronics Inc. |
| 6 | * |
| 7 | * Denis Ciocca <denis.ciocca@st.com> |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 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> |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 15 | |
| 16 | #include <linux/iio/common/st_sensors.h> |
| 17 | #include <linux/iio/common/st_sensors_spi.h> |
| 18 | #include "st_gyro.h" |
| 19 | |
Lorenzo Bianconi | 794694c | 2017-06-27 00:53:45 +0200 | [diff] [blame] | 20 | /* |
| 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 | */ |
| 25 | static 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 | }; |
| 64 | MODULE_DEVICE_TABLE(of, st_gyro_of_match); |
Lorenzo Bianconi | 794694c | 2017-06-27 00:53:45 +0200 | [diff] [blame] | 65 | |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 66 | static int st_gyro_spi_probe(struct spi_device *spi) |
| 67 | { |
Denis Ciocca | 3c05484 | 2019-07-18 15:53:49 -0700 | [diff] [blame] | 68 | const struct st_sensor_settings *settings; |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 69 | struct st_sensor_data *gdata; |
Denis Ciocca | 3c05484 | 2019-07-18 15:53:49 -0700 | [diff] [blame] | 70 | struct iio_dev *indio_dev; |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 71 | int err; |
| 72 | |
Andy Shevchenko | efc7898 | 2019-12-16 19:38:52 +0200 | [diff] [blame] | 73 | st_sensors_dev_name_probe(&spi->dev, spi->modalias, sizeof(spi->modalias)); |
Denis Ciocca | 3c05484 | 2019-07-18 15:53:49 -0700 | [diff] [blame] | 74 | |
| 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 Kamat | 7a842ed | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 82 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*gdata)); |
| 83 | if (!indio_dev) |
| 84 | return -ENOMEM; |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 85 | |
| 86 | gdata = iio_priv(indio_dev); |
Denis Ciocca | 3c05484 | 2019-07-18 15:53:49 -0700 | [diff] [blame] | 87 | gdata->sensor_settings = (struct st_sensor_settings *)settings; |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 88 | |
Denis Ciocca | 1ecd245 | 2019-07-18 15:53:52 -0700 | [diff] [blame] | 89 | err = st_sensors_spi_configure(indio_dev, spi); |
| 90 | if (err < 0) |
| 91 | return err; |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 92 | |
Denis CIOCCA | ef67b34 | 2014-10-03 17:35:37 +0200 | [diff] [blame] | 93 | err = st_gyro_common_probe(indio_dev); |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 94 | if (err < 0) |
Sachin Kamat | 7a842ed | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 95 | return err; |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 96 | |
| 97 | return 0; |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | static int st_gyro_spi_remove(struct spi_device *spi) |
| 101 | { |
| 102 | st_gyro_common_remove(spi_get_drvdata(spi)); |
| 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | static const struct spi_device_id st_gyro_id_table[] = { |
| 108 | { L3G4200D_GYRO_DEV_NAME }, |
| 109 | { LSM330D_GYRO_DEV_NAME }, |
| 110 | { LSM330DL_GYRO_DEV_NAME }, |
| 111 | { LSM330DLC_GYRO_DEV_NAME }, |
| 112 | { L3GD20_GYRO_DEV_NAME }, |
Lorenzo Bianconi | 45a4e42 | 2017-06-19 22:04:30 +0200 | [diff] [blame] | 113 | { L3GD20H_GYRO_DEV_NAME }, |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 114 | { L3G4IS_GYRO_DEV_NAME }, |
| 115 | { LSM330_GYRO_DEV_NAME }, |
Crestez Dan Leonard | 41c128c | 2016-04-19 15:02:11 +0300 | [diff] [blame] | 116 | { LSM9DS0_GYRO_DEV_NAME }, |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 117 | {}, |
| 118 | }; |
| 119 | MODULE_DEVICE_TABLE(spi, st_gyro_id_table); |
| 120 | |
| 121 | static struct spi_driver st_gyro_driver = { |
| 122 | .driver = { |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 123 | .name = "st-gyro-spi", |
Andy Shevchenko | ecb27c5 | 2019-12-16 19:38:53 +0200 | [diff] [blame] | 124 | .of_match_table = st_gyro_of_match, |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 125 | }, |
| 126 | .probe = st_gyro_spi_probe, |
| 127 | .remove = st_gyro_spi_remove, |
| 128 | .id_table = st_gyro_id_table, |
| 129 | }; |
| 130 | module_spi_driver(st_gyro_driver); |
| 131 | |
| 132 | MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); |
| 133 | MODULE_DESCRIPTION("STMicroelectronics gyroscopes spi driver"); |
| 134 | MODULE_LICENSE("GPL v2"); |