xref: /linux/drivers/iio/gyro/st_gyro_i2c.c (revision ff5599816711d2e67da2d7561fd36ac48debd433)
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/i2c.h>
15 #include <linux/iio/iio.h>
16 
17 #include <linux/iio/common/st_sensors.h>
18 #include <linux/iio/common/st_sensors_i2c.h>
19 #include "st_gyro.h"
20 
21 static int st_gyro_i2c_probe(struct i2c_client *client,
22 						const struct i2c_device_id *id)
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 = &client->dev;
36 
37 	st_sensors_i2c_configure(indio_dev, client, 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_i2c_remove(struct i2c_client *client)
52 {
53 	st_gyro_common_remove(i2c_get_clientdata(client));
54 
55 	return 0;
56 }
57 
58 static const struct i2c_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(i2c, st_gyro_id_table);
70 
71 static struct i2c_driver st_gyro_driver = {
72 	.driver = {
73 		.owner = THIS_MODULE,
74 		.name = "st-gyro-i2c",
75 	},
76 	.probe = st_gyro_i2c_probe,
77 	.remove = st_gyro_i2c_remove,
78 	.id_table = st_gyro_id_table,
79 };
80 module_i2c_driver(st_gyro_driver);
81 
82 MODULE_AUTHOR("Denis Ciocca <denis.ciocca@st.com>");
83 MODULE_DESCRIPTION("STMicroelectronics gyroscopes i2c driver");
84 MODULE_LICENSE("GPL v2");
85