xref: /openbmc/linux/drivers/iio/gyro/st_gyro_spi.c (revision 7be56a8f)
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>
16 #include <linux/iio/trigger.h>
17 
18 #include <linux/iio/common/st_sensors.h>
19 #include <linux/iio/common/st_sensors_spi.h>
20 #include "st_gyro.h"
21 
22 static int st_gyro_spi_probe(struct spi_device *spi)
23 {
24 	struct iio_dev *indio_dev;
25 	struct st_sensor_data *gdata;
26 	int err;
27 
28 	indio_dev = iio_device_alloc(sizeof(*gdata));
29 	if (indio_dev == NULL) {
30 		err = -ENOMEM;
31 		goto iio_device_alloc_error;
32 	}
33 
34 	gdata = iio_priv(indio_dev);
35 	gdata->dev = &spi->dev;
36 
37 	st_sensors_spi_configure(indio_dev, spi, gdata);
38 
39 	err = st_gyro_common_probe(indio_dev);
40 	if (err < 0)
41 		goto st_gyro_common_probe_error;
42 
43 	return 0;
44 
45 st_gyro_common_probe_error:
46 	iio_device_free(indio_dev);
47 iio_device_alloc_error:
48 	return err;
49 }
50 
51 static int st_gyro_spi_remove(struct spi_device *spi)
52 {
53 	st_gyro_common_remove(spi_get_drvdata(spi));
54 
55 	return 0;
56 }
57 
58 static const struct spi_device_id st_gyro_id_table[] = {
59 	{ L3G4200D_GYRO_DEV_NAME },
60 	{ LSM330D_GYRO_DEV_NAME },
61 	{ LSM330DL_GYRO_DEV_NAME },
62 	{ LSM330DLC_GYRO_DEV_NAME },
63 	{ L3GD20_GYRO_DEV_NAME },
64 	{ L3GD20H_GYRO_DEV_NAME },
65 	{ L3G4IS_GYRO_DEV_NAME },
66 	{ LSM330_GYRO_DEV_NAME },
67 	{},
68 };
69 MODULE_DEVICE_TABLE(spi, st_gyro_id_table);
70 
71 static struct spi_driver st_gyro_driver = {
72 	.driver = {
73 		.owner = THIS_MODULE,
74 		.name = "st-gyro-spi",
75 	},
76 	.probe = st_gyro_spi_probe,
77 	.remove = st_gyro_spi_remove,
78 	.id_table = st_gyro_id_table,
79 };
80 module_spi_driver(st_gyro_driver);
81 
82 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
83 MODULE_DESCRIPTION("STMicroelectronics gyroscopes spi driver");
84 MODULE_LICENSE("GPL v2");
85