xref: /openbmc/linux/drivers/iio/accel/adxl345_i2c.c (revision bfad37c5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ADXL345 3-Axis Digital Accelerometer I2C driver
4  *
5  * Copyright (c) 2017 Eva Rachel Retuya <eraretuya@gmail.com>
6  *
7  * 7-bit I2C slave address: 0x1D (ALT ADDRESS pin tied to VDDIO) or
8  * 0x53 (ALT ADDRESS pin grounded)
9  */
10 
11 #include <linux/i2c.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 
15 #include "adxl345.h"
16 
17 static const struct regmap_config adxl345_i2c_regmap_config = {
18 	.reg_bits = 8,
19 	.val_bits = 8,
20 };
21 
22 static int adxl345_i2c_probe(struct i2c_client *client,
23 			     const struct i2c_device_id *id)
24 {
25 	struct regmap *regmap;
26 
27 	if (!id)
28 		return -ENODEV;
29 
30 	regmap = devm_regmap_init_i2c(client, &adxl345_i2c_regmap_config);
31 	if (IS_ERR(regmap)) {
32 		dev_err(&client->dev, "Error initializing i2c regmap: %ld\n",
33 			PTR_ERR(regmap));
34 		return PTR_ERR(regmap);
35 	}
36 
37 	return adxl345_core_probe(&client->dev, regmap, id->driver_data,
38 				  id->name);
39 }
40 
41 static const struct i2c_device_id adxl345_i2c_id[] = {
42 	{ "adxl345", ADXL345 },
43 	{ "adxl375", ADXL375 },
44 	{ }
45 };
46 
47 MODULE_DEVICE_TABLE(i2c, adxl345_i2c_id);
48 
49 static const struct of_device_id adxl345_of_match[] = {
50 	{ .compatible = "adi,adxl345" },
51 	{ .compatible = "adi,adxl375" },
52 	{ },
53 };
54 
55 MODULE_DEVICE_TABLE(of, adxl345_of_match);
56 
57 static struct i2c_driver adxl345_i2c_driver = {
58 	.driver = {
59 		.name	= "adxl345_i2c",
60 		.of_match_table = adxl345_of_match,
61 	},
62 	.probe		= adxl345_i2c_probe,
63 	.id_table	= adxl345_i2c_id,
64 };
65 
66 module_i2c_driver(adxl345_i2c_driver);
67 
68 MODULE_AUTHOR("Eva Rachel Retuya <eraretuya@gmail.com>");
69 MODULE_DESCRIPTION("ADXL345 3-Axis Digital Accelerometer I2C driver");
70 MODULE_LICENSE("GPL v2");
71