xref: /openbmc/linux/drivers/iio/accel/adxl313_i2c.c (revision ac73d4bf)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * ADXL313 3-Axis Digital Accelerometer
4  *
5  * Copyright (c) 2021 Lucas Stankus <lucas.p.stankus@gmail.com>
6  *
7  * Datasheet: https://www.analog.com/media/en/technical-documentation/data-sheets/ADXL313.pdf
8  */
9 
10 #include <linux/i2c.h>
11 #include <linux/mod_devicetable.h>
12 #include <linux/module.h>
13 #include <linux/regmap.h>
14 
15 #include "adxl313.h"
16 
17 static const struct regmap_config adxl31x_i2c_regmap_config[] = {
18 	[ADXL312] = {
19 		.reg_bits	= 8,
20 		.val_bits	= 8,
21 		.rd_table	= &adxl312_readable_regs_table,
22 		.wr_table	= &adxl312_writable_regs_table,
23 		.max_register	= 0x39,
24 	},
25 	[ADXL313] = {
26 		.reg_bits	= 8,
27 		.val_bits	= 8,
28 		.rd_table	= &adxl313_readable_regs_table,
29 		.wr_table	= &adxl313_writable_regs_table,
30 		.max_register	= 0x39,
31 	},
32 	[ADXL314] = {
33 		.reg_bits	= 8,
34 		.val_bits	= 8,
35 		.rd_table	= &adxl314_readable_regs_table,
36 		.wr_table	= &adxl314_writable_regs_table,
37 		.max_register	= 0x39,
38 	},
39 };
40 
41 static const struct i2c_device_id adxl313_i2c_id[] = {
42 	{ .name = "adxl312", .driver_data = (kernel_ulong_t)&adxl31x_chip_info[ADXL312] },
43 	{ .name = "adxl313", .driver_data = (kernel_ulong_t)&adxl31x_chip_info[ADXL312] },
44 	{ .name = "adxl314", .driver_data = (kernel_ulong_t)&adxl31x_chip_info[ADXL312] },
45 	{ }
46 };
47 
48 MODULE_DEVICE_TABLE(i2c, adxl313_i2c_id);
49 
50 static const struct of_device_id adxl313_of_match[] = {
51 	{ .compatible = "adi,adxl312", .data = &adxl31x_chip_info[ADXL312] },
52 	{ .compatible = "adi,adxl313", .data = &adxl31x_chip_info[ADXL313] },
53 	{ .compatible = "adi,adxl314", .data = &adxl31x_chip_info[ADXL314] },
54 	{ }
55 };
56 
57 MODULE_DEVICE_TABLE(of, adxl313_of_match);
58 
59 static int adxl313_i2c_probe(struct i2c_client *client)
60 {
61 	const struct adxl313_chip_info *chip_data;
62 	struct regmap *regmap;
63 
64 	/*
65 	 * Retrieves device specific data as a pointer to a
66 	 * adxl313_chip_info structure
67 	 */
68 	chip_data = device_get_match_data(&client->dev);
69 	if (!chip_data)
70 		chip_data = (const struct adxl313_chip_info *)i2c_match_id(adxl313_i2c_id, client)->driver_data;
71 
72 	regmap = devm_regmap_init_i2c(client,
73 				      &adxl31x_i2c_regmap_config[chip_data->type]);
74 	if (IS_ERR(regmap)) {
75 		dev_err(&client->dev, "Error initializing i2c regmap: %ld\n",
76 			PTR_ERR(regmap));
77 		return PTR_ERR(regmap);
78 	}
79 
80 	return adxl313_core_probe(&client->dev, regmap, chip_data, NULL);
81 }
82 
83 static struct i2c_driver adxl313_i2c_driver = {
84 	.driver = {
85 		.name	= "adxl313_i2c",
86 		.of_match_table = adxl313_of_match,
87 	},
88 	.probe_new	= adxl313_i2c_probe,
89 	.id_table	= adxl313_i2c_id,
90 };
91 
92 module_i2c_driver(adxl313_i2c_driver);
93 
94 MODULE_AUTHOR("Lucas Stankus <lucas.p.stankus@gmail.com>");
95 MODULE_DESCRIPTION("ADXL313 3-Axis Digital Accelerometer I2C driver");
96 MODULE_LICENSE("GPL v2");
97 MODULE_IMPORT_NS(IIO_ADXL313);
98