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/slab.h>
14 
15 #include <linux/iio/common/st_sensors_i2c.h>
16 
17 #include "st_lsm9ds0.h"
18 
19 static const struct of_device_id st_lsm9ds0_of_match[] = {
20 	{
21 		.compatible = "st,lsm9ds0-imu",
22 		.data = LSM9DS0_IMU_DEV_NAME,
23 	},
24 	{}
25 };
26 MODULE_DEVICE_TABLE(of, st_lsm9ds0_of_match);
27 
28 static const struct i2c_device_id st_lsm9ds0_id_table[] = {
29 	{ LSM9DS0_IMU_DEV_NAME },
30 	{}
31 };
32 MODULE_DEVICE_TABLE(i2c, st_lsm9ds0_id_table);
33 
34 static const struct regmap_config st_lsm9ds0_regmap_config = {
35 	.reg_bits	= 8,
36 	.val_bits	= 8,
37 	.read_flag_mask	= 0x80,
38 };
39 
40 static int st_lsm9ds0_i2c_probe(struct i2c_client *client)
41 {
42 	const struct regmap_config *config = &st_lsm9ds0_regmap_config;
43 	struct device *dev = &client->dev;
44 	struct st_lsm9ds0 *lsm9ds0;
45 	struct regmap *regmap;
46 
47 	st_sensors_dev_name_probe(dev, client->name, sizeof(client->name));
48 
49 	lsm9ds0 = devm_kzalloc(dev, sizeof(*lsm9ds0), GFP_KERNEL);
50 	if (!lsm9ds0)
51 		return -ENOMEM;
52 
53 	lsm9ds0->dev = dev;
54 	lsm9ds0->name = client->name;
55 	lsm9ds0->irq = client->irq;
56 
57 	regmap = devm_regmap_init_i2c(client, config);
58 	if (IS_ERR(regmap))
59 		return PTR_ERR(regmap);
60 
61 	i2c_set_clientdata(client, lsm9ds0);
62 
63 	return st_lsm9ds0_probe(lsm9ds0, regmap);
64 }
65 
66 static int st_lsm9ds0_i2c_remove(struct i2c_client *client)
67 {
68 	return st_lsm9ds0_remove(i2c_get_clientdata(client));
69 }
70 
71 static struct i2c_driver st_lsm9ds0_driver = {
72 	.driver = {
73 		.name = "st-lsm9ds0-i2c",
74 		.of_match_table = st_lsm9ds0_of_match,
75 	},
76 	.probe_new = st_lsm9ds0_i2c_probe,
77 	.remove = st_lsm9ds0_i2c_remove,
78 	.id_table = st_lsm9ds0_id_table,
79 };
80 module_i2c_driver(st_lsm9ds0_driver);
81 
82 MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
83 MODULE_DESCRIPTION("STMicroelectronics LSM9DS0 IMU I2C driver");
84 MODULE_LICENSE("GPL v2");
85