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	0x42
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,const struct i2c_device_id * id)44 static int adxl367_i2c_probe(struct i2c_client *client,
45 			     const struct i2c_device_id *id)
46 {
47 	struct adxl367_i2c_state *st;
48 	struct regmap *regmap;
49 
50 	st = devm_kzalloc(&client->dev, sizeof(*st), GFP_KERNEL);
51 	if (!st)
52 		return -ENOMEM;
53 
54 	regmap = devm_regmap_init_i2c(client, &adxl367_i2c_regmap_config);
55 	if (IS_ERR(regmap))
56 		return PTR_ERR(regmap);
57 
58 	st->regmap = regmap;
59 
60 	return adxl367_probe(&client->dev, &adxl367_i2c_ops, st, regmap,
61 			     client->irq);
62 }
63 
64 static const struct i2c_device_id adxl367_i2c_id[] = {
65 	{ "adxl367", 0 },
66 	{ },
67 };
68 MODULE_DEVICE_TABLE(i2c, adxl367_i2c_id);
69 
70 static const struct of_device_id adxl367_of_match[] = {
71 	{ .compatible = "adi,adxl367" },
72 	{ },
73 };
74 MODULE_DEVICE_TABLE(of, adxl367_of_match);
75 
76 static struct i2c_driver adxl367_i2c_driver = {
77 	.driver = {
78 		.name = "adxl367_i2c",
79 		.of_match_table = adxl367_of_match,
80 	},
81 	.probe = adxl367_i2c_probe,
82 	.id_table = adxl367_i2c_id,
83 };
84 
85 module_i2c_driver(adxl367_i2c_driver);
86 
87 MODULE_IMPORT_NS(IIO_ADXL367);
88 MODULE_AUTHOR("Cosmin Tanislav <cosmin.tanislav@analog.com>");
89 MODULE_DESCRIPTION("Analog Devices ADXL367 3-axis accelerometer I2C driver");
90 MODULE_LICENSE("GPL");
91