1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * STMicroelectronics LSM9DS0 IMU driver
4  *
5  * Copyright (C) 2021, Intel Corporation
6  *
7  * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
8  */
9 
10 #include <linux/i2c.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/regmap.h>
15 
16 #include <linux/iio/common/st_sensors_i2c.h>
17 
18 #include "st_lsm9ds0.h"
19 
20 static const struct of_device_id st_lsm9ds0_of_match[] = {
21 	{
22 		.compatible = "st,lsm303d-imu",
23 		.data = LSM303D_IMU_DEV_NAME,
24 	},
25 	{
26 		.compatible = "st,lsm9ds0-imu",
27 		.data = LSM9DS0_IMU_DEV_NAME,
28 	},
29 	{}
30 };
31 MODULE_DEVICE_TABLE(of, st_lsm9ds0_of_match);
32 
33 static const struct i2c_device_id st_lsm9ds0_id_table[] = {
34 	{ LSM303D_IMU_DEV_NAME },
35 	{ LSM9DS0_IMU_DEV_NAME },
36 	{}
37 };
38 MODULE_DEVICE_TABLE(i2c, st_lsm9ds0_id_table);
39 
40 static const struct acpi_device_id st_lsm9ds0_acpi_match[] = {
41 	{"ACCL0001", (kernel_ulong_t)LSM303D_IMU_DEV_NAME},
42 	{ },
43 };
44 MODULE_DEVICE_TABLE(acpi, st_lsm9ds0_acpi_match);
45 
46 static const struct regmap_config st_lsm9ds0_regmap_config = {
47 	.reg_bits	= 8,
48 	.val_bits	= 8,
49 	.read_flag_mask	= 0x80,
50 };
51 
st_lsm9ds0_i2c_probe(struct i2c_client * client)52 static int st_lsm9ds0_i2c_probe(struct i2c_client *client)
53 {
54 	const struct regmap_config *config = &st_lsm9ds0_regmap_config;
55 	struct device *dev = &client->dev;
56 	struct st_lsm9ds0 *lsm9ds0;
57 	struct regmap *regmap;
58 
59 	st_sensors_dev_name_probe(dev, client->name, sizeof(client->name));
60 
61 	lsm9ds0 = devm_kzalloc(dev, sizeof(*lsm9ds0), GFP_KERNEL);
62 	if (!lsm9ds0)
63 		return -ENOMEM;
64 
65 	lsm9ds0->dev = dev;
66 	lsm9ds0->name = client->name;
67 	lsm9ds0->irq = client->irq;
68 
69 	regmap = devm_regmap_init_i2c(client, config);
70 	if (IS_ERR(regmap))
71 		return PTR_ERR(regmap);
72 
73 	i2c_set_clientdata(client, lsm9ds0);
74 
75 	return st_lsm9ds0_probe(lsm9ds0, regmap);
76 }
77 
78 static struct i2c_driver st_lsm9ds0_driver = {
79 	.driver = {
80 		.name = "st-lsm9ds0-i2c",
81 		.of_match_table = st_lsm9ds0_of_match,
82 		.acpi_match_table = st_lsm9ds0_acpi_match,
83 	},
84 	.probe = st_lsm9ds0_i2c_probe,
85 	.id_table = st_lsm9ds0_id_table,
86 };
87 module_i2c_driver(st_lsm9ds0_driver);
88 
89 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
90 MODULE_DESCRIPTION("STMicroelectronics LSM9DS0 IMU I2C driver");
91 MODULE_LICENSE("GPL v2");
92 MODULE_IMPORT_NS(IIO_ST_SENSORS);
93