xref: /openbmc/linux/drivers/iio/accel/adxl367_i2c.c (revision 46eeaa11bdd1bc9e077bdf741d32ca7235d263c6)
1cbab791cSCosmin Tanislav // SPDX-License-Identifier: GPL-2.0+
2cbab791cSCosmin Tanislav /*
3cbab791cSCosmin Tanislav  * Copyright (C) 2021 Analog Devices, Inc.
4cbab791cSCosmin Tanislav  * Author: Cosmin Tanislav <cosmin.tanislav@analog.com>
5cbab791cSCosmin Tanislav  */
6cbab791cSCosmin Tanislav 
7cbab791cSCosmin Tanislav #include <linux/i2c.h>
8cbab791cSCosmin Tanislav #include <linux/mod_devicetable.h>
9cbab791cSCosmin Tanislav #include <linux/module.h>
10cbab791cSCosmin Tanislav #include <linux/regmap.h>
11cbab791cSCosmin Tanislav 
12cbab791cSCosmin Tanislav #include "adxl367.h"
13cbab791cSCosmin Tanislav 
14*21e98568SCosmin Tanislav #define ADXL367_I2C_FIFO_DATA	0x18
15cbab791cSCosmin Tanislav 
16cbab791cSCosmin Tanislav struct adxl367_i2c_state {
17cbab791cSCosmin Tanislav 	struct regmap *regmap;
18cbab791cSCosmin Tanislav };
19cbab791cSCosmin Tanislav 
adxl367_readable_noinc_reg(struct device * dev,unsigned int reg)20cbab791cSCosmin Tanislav static bool adxl367_readable_noinc_reg(struct device *dev, unsigned int reg)
21cbab791cSCosmin Tanislav {
22cbab791cSCosmin Tanislav 	return reg == ADXL367_I2C_FIFO_DATA;
23cbab791cSCosmin Tanislav }
24cbab791cSCosmin Tanislav 
adxl367_i2c_read_fifo(void * context,__be16 * fifo_buf,unsigned int fifo_entries)25cbab791cSCosmin Tanislav static int adxl367_i2c_read_fifo(void *context, __be16 *fifo_buf,
26cbab791cSCosmin Tanislav 				 unsigned int fifo_entries)
27cbab791cSCosmin Tanislav {
28cbab791cSCosmin Tanislav 	struct adxl367_i2c_state *st = context;
29cbab791cSCosmin Tanislav 
30cbab791cSCosmin Tanislav 	return regmap_noinc_read(st->regmap, ADXL367_I2C_FIFO_DATA, fifo_buf,
31cbab791cSCosmin Tanislav 				 fifo_entries * sizeof(*fifo_buf));
32cbab791cSCosmin Tanislav }
33cbab791cSCosmin Tanislav 
34cbab791cSCosmin Tanislav static const struct regmap_config adxl367_i2c_regmap_config = {
35cbab791cSCosmin Tanislav 	.reg_bits = 8,
36cbab791cSCosmin Tanislav 	.val_bits = 8,
37cbab791cSCosmin Tanislav 	.readable_noinc_reg = adxl367_readable_noinc_reg,
38cbab791cSCosmin Tanislav };
39cbab791cSCosmin Tanislav 
40cbab791cSCosmin Tanislav static const struct adxl367_ops adxl367_i2c_ops = {
41cbab791cSCosmin Tanislav 	.read_fifo = adxl367_i2c_read_fifo,
42cbab791cSCosmin Tanislav };
43cbab791cSCosmin Tanislav 
adxl367_i2c_probe(struct i2c_client * client)44ecff5cb4SUwe Kleine-König static int adxl367_i2c_probe(struct i2c_client *client)
45cbab791cSCosmin Tanislav {
46cbab791cSCosmin Tanislav 	struct adxl367_i2c_state *st;
47cbab791cSCosmin Tanislav 	struct regmap *regmap;
48cbab791cSCosmin Tanislav 
49cbab791cSCosmin Tanislav 	st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
50cbab791cSCosmin Tanislav 	if (!st)
51cbab791cSCosmin Tanislav 		return -ENOMEM;
52cbab791cSCosmin Tanislav 
53cbab791cSCosmin Tanislav 	regmap = devm_regmap_init_i2c(client, &adxl367_i2c_regmap_config);
54cbab791cSCosmin Tanislav 	if (IS_ERR(regmap))
55cbab791cSCosmin Tanislav 		return PTR_ERR(regmap);
56cbab791cSCosmin Tanislav 
57cbab791cSCosmin Tanislav 	st->regmap = regmap;
58cbab791cSCosmin Tanislav 
59cbab791cSCosmin Tanislav 	return adxl367_probe(&client->dev, &adxl367_i2c_ops, st, regmap,
60cbab791cSCosmin Tanislav 			     client->irq);
61cbab791cSCosmin Tanislav }
62cbab791cSCosmin Tanislav 
63cbab791cSCosmin Tanislav static const struct i2c_device_id adxl367_i2c_id[] = {
64cbab791cSCosmin Tanislav 	{ "adxl367", 0 },
65cbab791cSCosmin Tanislav 	{ },
66cbab791cSCosmin Tanislav };
67cbab791cSCosmin Tanislav MODULE_DEVICE_TABLE(i2c, adxl367_i2c_id);
68cbab791cSCosmin Tanislav 
69cbab791cSCosmin Tanislav static const struct of_device_id adxl367_of_match[] = {
70cbab791cSCosmin Tanislav 	{ .compatible = "adi,adxl367" },
71cbab791cSCosmin Tanislav 	{ },
72cbab791cSCosmin Tanislav };
73cbab791cSCosmin Tanislav MODULE_DEVICE_TABLE(of, adxl367_of_match);
74cbab791cSCosmin Tanislav 
75cbab791cSCosmin Tanislav static struct i2c_driver adxl367_i2c_driver = {
76cbab791cSCosmin Tanislav 	.driver = {
77cbab791cSCosmin Tanislav 		.name = "adxl367_i2c",
78cbab791cSCosmin Tanislav 		.of_match_table = adxl367_of_match,
79cbab791cSCosmin Tanislav 	},
807cf15f42SUwe Kleine-König 	.probe = adxl367_i2c_probe,
81cbab791cSCosmin Tanislav 	.id_table = adxl367_i2c_id,
82cbab791cSCosmin Tanislav };
83cbab791cSCosmin Tanislav 
84cbab791cSCosmin Tanislav module_i2c_driver(adxl367_i2c_driver);
85cbab791cSCosmin Tanislav 
86cbab791cSCosmin Tanislav MODULE_IMPORT_NS(IIO_ADXL367);
87cbab791cSCosmin Tanislav MODULE_AUTHOR("Cosmin Tanislav <cosmin.tanislav@analog.com>");
88cbab791cSCosmin Tanislav MODULE_DESCRIPTION("Analog Devices ADXL367 3-axis accelerometer I2C driver");
89cbab791cSCosmin Tanislav MODULE_LICENSE("GPL");
90