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