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
17static 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
41static 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[ADXL313] },
44	{ .name = "adxl314", .driver_data = (kernel_ulong_t)&adxl31x_chip_info[ADXL314] },
45	{ }
46};
47
48MODULE_DEVICE_TABLE(i2c, adxl313_i2c_id);
49
50static 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
57MODULE_DEVICE_TABLE(of, adxl313_of_match);
58
59static 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 = i2c_get_match_data(client);
69
70	regmap = devm_regmap_init_i2c(client,
71				      &adxl31x_i2c_regmap_config[chip_data->type]);
72	if (IS_ERR(regmap)) {
73		dev_err(&client->dev, "Error initializing i2c regmap: %ld\n",
74			PTR_ERR(regmap));
75		return PTR_ERR(regmap);
76	}
77
78	return adxl313_core_probe(&client->dev, regmap, chip_data, NULL);
79}
80
81static struct i2c_driver adxl313_i2c_driver = {
82	.driver = {
83		.name	= "adxl313_i2c",
84		.of_match_table = adxl313_of_match,
85	},
86	.probe		= adxl313_i2c_probe,
87	.id_table	= adxl313_i2c_id,
88};
89
90module_i2c_driver(adxl313_i2c_driver);
91
92MODULE_AUTHOR("Lucas Stankus <lucas.p.stankus@gmail.com>");
93MODULE_DESCRIPTION("ADXL313 3-Axis Digital Accelerometer I2C driver");
94MODULE_LICENSE("GPL v2");
95MODULE_IMPORT_NS(IIO_ADXL313);
96