xref: /linux/drivers/iio/imu/bmi323/bmi323_i2c.c (revision 4b132aacb0768ac1e652cf517097ea6f237214b9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * I2C driver for Bosch BMI323 6-Axis IMU.
4  *
5  * Copyright (C) 2023, Jagath Jog J <jagathjog1996@gmail.com>
6  */
7 
8 #include <linux/i2c.h>
9 #include <linux/mod_devicetable.h>
10 #include <linux/module.h>
11 #include <linux/regmap.h>
12 
13 #include "bmi323.h"
14 
15 struct bmi323_i2c_priv {
16 	struct i2c_client *i2c;
17 	u8 i2c_rx_buffer[BMI323_FIFO_LENGTH_IN_BYTES + BMI323_I2C_DUMMY];
18 };
19 
20 /*
21  * From BMI323 datasheet section 4: Notes on the Serial Interface Support.
22  * Each I2C register read operation requires to read two dummy bytes before
23  * the actual payload.
24  */
25 static int bmi323_regmap_i2c_read(void *context, const void *reg_buf,
26 				  size_t reg_size, void *val_buf,
27 				  size_t val_size)
28 {
29 	struct bmi323_i2c_priv *priv = context;
30 	struct i2c_msg msgs[2];
31 	int ret;
32 
33 	msgs[0].addr = priv->i2c->addr;
34 	msgs[0].flags = priv->i2c->flags;
35 	msgs[0].len = reg_size;
36 	msgs[0].buf = (u8 *)reg_buf;
37 
38 	msgs[1].addr = priv->i2c->addr;
39 	msgs[1].len = val_size + BMI323_I2C_DUMMY;
40 	msgs[1].buf = priv->i2c_rx_buffer;
41 	msgs[1].flags = priv->i2c->flags | I2C_M_RD;
42 
43 	ret = i2c_transfer(priv->i2c->adapter, msgs, ARRAY_SIZE(msgs));
44 	if (ret < 0)
45 		return -EIO;
46 
47 	memcpy(val_buf, priv->i2c_rx_buffer + BMI323_I2C_DUMMY, val_size);
48 
49 	return 0;
50 }
51 
52 static int bmi323_regmap_i2c_write(void *context, const void *data,
53 				   size_t count)
54 {
55 	struct bmi323_i2c_priv *priv = context;
56 	u8 reg;
57 
58 	reg = *(u8 *)data;
59 	return i2c_smbus_write_i2c_block_data(priv->i2c, reg,
60 					      count - sizeof(u8),
61 					      data + sizeof(u8));
62 }
63 
64 static struct regmap_bus bmi323_regmap_bus = {
65 	.read = bmi323_regmap_i2c_read,
66 	.write = bmi323_regmap_i2c_write,
67 };
68 
69 static const struct regmap_config bmi323_i2c_regmap_config = {
70 	.reg_bits = 8,
71 	.val_bits = 16,
72 	.max_register = BMI323_CFG_RES_REG,
73 	.val_format_endian = REGMAP_ENDIAN_LITTLE,
74 };
75 
76 static int bmi323_i2c_probe(struct i2c_client *i2c)
77 {
78 	struct device *dev = &i2c->dev;
79 	struct bmi323_i2c_priv *priv;
80 	struct regmap *regmap;
81 
82 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
83 	if (!priv)
84 		return -ENOMEM;
85 
86 	priv->i2c = i2c;
87 	regmap = devm_regmap_init(dev, &bmi323_regmap_bus, priv,
88 				  &bmi323_i2c_regmap_config);
89 	if (IS_ERR(regmap))
90 		return dev_err_probe(dev, PTR_ERR(regmap),
91 				     "Failed to initialize I2C Regmap\n");
92 
93 	return bmi323_core_probe(dev);
94 }
95 
96 static const struct acpi_device_id bmi323_acpi_match[] = {
97 	/*
98 	 * The "BOSC0200" identifier used here is not unique to bmi323 devices.
99 	 * The same "BOSC0200" identifier is found in the ACPI tables of devices
100 	 * using the bmc150 chip. This creates a conflict with duplicate ACPI
101 	 * identifiers which multiple drivers want to use. If a non-bmi323
102 	 * device starts to load with this "BOSC0200" ACPI match here, then the
103 	 * chip ID check portion should fail because the chip IDs received (via
104 	 * i2c) are unique between bmc150 and bmi323 and the driver should
105 	 * relinquish the device. If and when a different driver (such as
106 	 * bmc150) starts to load with the "BOSC0200" ACPI match, a short reset
107 	 * should ensure that the device is not in a bad state during that
108 	 * driver initialization. This device reset does occur in both the
109 	 * bmi323 and bmc150 init sequences.
110 	 */
111 	{ "BOSC0200" },
112 	{ }
113 };
114 MODULE_DEVICE_TABLE(acpi, bmi323_acpi_match);
115 
116 static const struct i2c_device_id bmi323_i2c_ids[] = {
117 	{ "bmi323" },
118 	{ }
119 };
120 MODULE_DEVICE_TABLE(i2c, bmi323_i2c_ids);
121 
122 static const struct of_device_id bmi323_of_i2c_match[] = {
123 	{ .compatible = "bosch,bmi323" },
124 	{ }
125 };
126 MODULE_DEVICE_TABLE(of, bmi323_of_i2c_match);
127 
128 static struct i2c_driver bmi323_i2c_driver = {
129 	.driver = {
130 		.name = "bmi323",
131 		.of_match_table = bmi323_of_i2c_match,
132 		.acpi_match_table = bmi323_acpi_match,
133 	},
134 	.probe = bmi323_i2c_probe,
135 	.id_table = bmi323_i2c_ids,
136 };
137 module_i2c_driver(bmi323_i2c_driver);
138 
139 MODULE_DESCRIPTION("Bosch BMI323 IMU driver");
140 MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>");
141 MODULE_LICENSE("GPL");
142 MODULE_IMPORT_NS(IIO_BMI323);
143