1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3 * ADXL371/ADXL372 3-Axis Digital Accelerometer I2C driver
4 *
5 * Copyright 2018 Analog Devices Inc.
6 */
7
8 #include <linux/i2c.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/regmap.h>
12
13 #include "adxl372.h"
14
15 static const struct regmap_config adxl372_regmap_config = {
16 .reg_bits = 8,
17 .val_bits = 8,
18 .readable_noinc_reg = adxl372_readable_noinc_reg,
19 };
20
adxl372_i2c_probe(struct i2c_client * client)21 static int adxl372_i2c_probe(struct i2c_client *client)
22 {
23 const struct adxl372_chip_info *chip_info;
24 struct regmap *regmap;
25 unsigned int regval;
26 int ret;
27
28 chip_info = i2c_get_match_data(client);
29
30 regmap = devm_regmap_init_i2c(client, &adxl372_regmap_config);
31 if (IS_ERR(regmap))
32 return PTR_ERR(regmap);
33
34 ret = regmap_read(regmap, ADXL372_REVID, ®val);
35 if (ret < 0)
36 return ret;
37
38 /* Starting with the 3rd revision an I2C chip bug was fixed */
39 if (regval < 3)
40 dev_warn(&client->dev,
41 "I2C might not work properly with other devices on the bus");
42
43 return adxl372_probe(&client->dev, regmap, client->irq, chip_info);
44 }
45
46 static const struct i2c_device_id adxl372_i2c_id[] = {
47 { "adxl371", (kernel_ulong_t)&adxl371_chip_info },
48 { "adxl372", (kernel_ulong_t)&adxl372_chip_info },
49 { }
50 };
51 MODULE_DEVICE_TABLE(i2c, adxl372_i2c_id);
52
53 static const struct of_device_id adxl372_of_match[] = {
54 { .compatible = "adi,adxl371", .data = &adxl371_chip_info },
55 { .compatible = "adi,adxl372", .data = &adxl372_chip_info },
56 { }
57 };
58 MODULE_DEVICE_TABLE(of, adxl372_of_match);
59
60 static struct i2c_driver adxl372_i2c_driver = {
61 .driver = {
62 .name = "adxl372_i2c",
63 .of_match_table = adxl372_of_match,
64 },
65 .probe = adxl372_i2c_probe,
66 .id_table = adxl372_i2c_id,
67 };
68
69 module_i2c_driver(adxl372_i2c_driver);
70
71 MODULE_AUTHOR("Stefan Popa <stefan.popa@analog.com>");
72 MODULE_AUTHOR("Antoniu Miclaus <antoniu.miclaus@analog.com>");
73 MODULE_DESCRIPTION("Analog Devices ADXL371/ADXL372 3-axis accelerometer I2C driver");
74 MODULE_LICENSE("GPL");
75 MODULE_IMPORT_NS("IIO_ADXL372");
76