Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * STMicroelectronics gyroscopes 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 Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 16 | |
| 17 | #include <linux/iio/common/st_sensors.h> |
| 18 | #include <linux/iio/common/st_sensors_spi.h> |
| 19 | #include "st_gyro.h" |
| 20 | |
Lorenzo Bianconi | 794694c | 2017-06-27 00:53:45 +0200 | [diff] [blame] | 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>-gyro to maintain |
| 25 | * compatibility |
| 26 | */ |
| 27 | static const struct of_device_id st_gyro_of_match[] = { |
| 28 | { |
| 29 | .compatible = "st,l3g4200d-gyro", |
| 30 | .data = L3G4200D_GYRO_DEV_NAME, |
| 31 | }, |
| 32 | { |
| 33 | .compatible = "st,lsm330d-gyro", |
| 34 | .data = LSM330D_GYRO_DEV_NAME, |
| 35 | }, |
| 36 | { |
| 37 | .compatible = "st,lsm330dl-gyro", |
| 38 | .data = LSM330DL_GYRO_DEV_NAME, |
| 39 | }, |
| 40 | { |
| 41 | .compatible = "st,lsm330dlc-gyro", |
| 42 | .data = LSM330DLC_GYRO_DEV_NAME, |
| 43 | }, |
| 44 | { |
| 45 | .compatible = "st,l3gd20-gyro", |
| 46 | .data = L3GD20_GYRO_DEV_NAME, |
| 47 | }, |
| 48 | { |
| 49 | .compatible = "st,l3gd20h-gyro", |
| 50 | .data = L3GD20H_GYRO_DEV_NAME, |
| 51 | }, |
| 52 | { |
| 53 | .compatible = "st,l3g4is-gyro", |
| 54 | .data = L3G4IS_GYRO_DEV_NAME, |
| 55 | }, |
| 56 | { |
| 57 | .compatible = "st,lsm330-gyro", |
| 58 | .data = LSM330_GYRO_DEV_NAME, |
| 59 | }, |
| 60 | { |
| 61 | .compatible = "st,lsm9ds0-gyro", |
| 62 | .data = LSM9DS0_GYRO_DEV_NAME, |
| 63 | }, |
| 64 | {}, |
| 65 | }; |
| 66 | MODULE_DEVICE_TABLE(of, st_gyro_of_match); |
| 67 | #else |
| 68 | #define st_gyro_of_match NULL |
| 69 | #endif |
| 70 | |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 71 | static int st_gyro_spi_probe(struct spi_device *spi) |
| 72 | { |
| 73 | struct iio_dev *indio_dev; |
| 74 | struct st_sensor_data *gdata; |
| 75 | int err; |
| 76 | |
Sachin Kamat | 7a842ed | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 77 | indio_dev = devm_iio_device_alloc(&spi->dev, sizeof(*gdata)); |
| 78 | if (!indio_dev) |
| 79 | return -ENOMEM; |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 80 | |
| 81 | gdata = iio_priv(indio_dev); |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 82 | |
Lorenzo Bianconi | 794694c | 2017-06-27 00:53:45 +0200 | [diff] [blame] | 83 | st_sensors_of_name_probe(&spi->dev, st_gyro_of_match, |
| 84 | spi->modalias, sizeof(spi->modalias)); |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 85 | st_sensors_spi_configure(indio_dev, spi, gdata); |
| 86 | |
Denis CIOCCA | ef67b34 | 2014-10-03 17:35:37 +0200 | [diff] [blame] | 87 | err = st_gyro_common_probe(indio_dev); |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 88 | if (err < 0) |
Sachin Kamat | 7a842ed | 2013-08-13 07:34:00 +0100 | [diff] [blame] | 89 | return err; |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 90 | |
| 91 | return 0; |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static int st_gyro_spi_remove(struct spi_device *spi) |
| 95 | { |
| 96 | st_gyro_common_remove(spi_get_drvdata(spi)); |
| 97 | |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | static const struct spi_device_id st_gyro_id_table[] = { |
| 102 | { L3G4200D_GYRO_DEV_NAME }, |
| 103 | { LSM330D_GYRO_DEV_NAME }, |
| 104 | { LSM330DL_GYRO_DEV_NAME }, |
| 105 | { LSM330DLC_GYRO_DEV_NAME }, |
| 106 | { L3GD20_GYRO_DEV_NAME }, |
Lorenzo Bianconi | 45a4e42 | 2017-06-19 22:04:30 +0200 | [diff] [blame] | 107 | { L3GD20H_GYRO_DEV_NAME }, |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 108 | { L3G4IS_GYRO_DEV_NAME }, |
| 109 | { LSM330_GYRO_DEV_NAME }, |
Crestez Dan Leonard | 41c128c | 2016-04-19 15:02:11 +0300 | [diff] [blame] | 110 | { LSM9DS0_GYRO_DEV_NAME }, |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 111 | {}, |
| 112 | }; |
| 113 | MODULE_DEVICE_TABLE(spi, st_gyro_id_table); |
| 114 | |
| 115 | static struct spi_driver st_gyro_driver = { |
| 116 | .driver = { |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 117 | .name = "st-gyro-spi", |
Lorenzo Bianconi | 794694c | 2017-06-27 00:53:45 +0200 | [diff] [blame] | 118 | .of_match_table = of_match_ptr(st_gyro_of_match), |
Denis Ciocca | 7be56a8 | 2013-01-25 23:44:00 +0000 | [diff] [blame] | 119 | }, |
| 120 | .probe = st_gyro_spi_probe, |
| 121 | .remove = st_gyro_spi_remove, |
| 122 | .id_table = st_gyro_id_table, |
| 123 | }; |
| 124 | module_spi_driver(st_gyro_driver); |
| 125 | |
| 126 | MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>"); |
| 127 | MODULE_DESCRIPTION("STMicroelectronics gyroscopes spi driver"); |
| 128 | MODULE_LICENSE("GPL v2"); |