xref: /openbmc/linux/drivers/iio/accel/adxl355_i2c.c (revision 88a6f899)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ADXL355 3-Axis Digital Accelerometer I2C driver
4  *
5  * Copyright (c) 2021 Puranjay Mohan <puranjay12@gmail.com>
6  */
7 
8 #include <linux/i2c.h>
9 #include <linux/module.h>
10 #include <linux/mod_devicetable.h>
11 #include <linux/regmap.h>
12 
13 #include "adxl355.h"
14 
15 static const struct regmap_config adxl355_i2c_regmap_config = {
16 	.reg_bits = 8,
17 	.val_bits = 8,
18 	.max_register = 0x2F,
19 	.rd_table = &adxl355_readable_regs_tbl,
20 	.wr_table = &adxl355_writeable_regs_tbl,
21 };
22 
23 static int adxl355_i2c_probe(struct i2c_client *client)
24 {
25 	struct regmap *regmap;
26 	const struct adxl355_chip_info *chip_data;
27 	const struct i2c_device_id *adxl355;
28 
29 	chip_data = device_get_match_data(&client->dev);
30 	if (!chip_data) {
31 		adxl355 = to_i2c_driver(client->dev.driver)->id_table;
32 		if (!adxl355)
33 			return -EINVAL;
34 
35 		chip_data = (void *)i2c_match_id(adxl355, client)->driver_data;
36 
37 		if (!chip_data)
38 			return -EINVAL;
39 	}
40 
41 	regmap = devm_regmap_init_i2c(client, &adxl355_i2c_regmap_config);
42 	if (IS_ERR(regmap)) {
43 		dev_err(&client->dev, "Error initializing i2c regmap: %ld\n",
44 			PTR_ERR(regmap));
45 
46 		return PTR_ERR(regmap);
47 	}
48 
49 	return adxl355_core_probe(&client->dev, regmap, chip_data);
50 }
51 
52 static const struct i2c_device_id adxl355_i2c_id[] = {
53 	{ "adxl355", (kernel_ulong_t)&adxl35x_chip_info[ADXL355] },
54 	{ "adxl359", (kernel_ulong_t)&adxl35x_chip_info[ADXL359] },
55 	{ }
56 };
57 MODULE_DEVICE_TABLE(i2c, adxl355_i2c_id);
58 
59 static const struct of_device_id adxl355_of_match[] = {
60 	{ .compatible = "adi,adxl355", .data = &adxl35x_chip_info[ADXL355] },
61 	{ .compatible = "adi,adxl359", .data = &adxl35x_chip_info[ADXL359] },
62 	{ }
63 };
64 MODULE_DEVICE_TABLE(of, adxl355_of_match);
65 
66 static struct i2c_driver adxl355_i2c_driver = {
67 	.driver = {
68 		.name	= "adxl355_i2c",
69 		.of_match_table = adxl355_of_match,
70 	},
71 	.probe		= adxl355_i2c_probe,
72 	.id_table	= adxl355_i2c_id,
73 };
74 module_i2c_driver(adxl355_i2c_driver);
75 
76 MODULE_AUTHOR("Puranjay Mohan <puranjay12@gmail.com>");
77 MODULE_DESCRIPTION("ADXL355 3-Axis Digital Accelerometer I2C driver");
78 MODULE_LICENSE("GPL v2");
79 MODULE_IMPORT_NS(IIO_ADXL355);
80