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 i2c_device_id bmi323_i2c_ids[] = { 97 { "bmi323" }, 98 { } 99 }; 100 MODULE_DEVICE_TABLE(i2c, bmi323_i2c_ids); 101 102 static const struct of_device_id bmi323_of_i2c_match[] = { 103 { .compatible = "bosch,bmi323" }, 104 { } 105 }; 106 MODULE_DEVICE_TABLE(of, bmi323_of_i2c_match); 107 108 static struct i2c_driver bmi323_i2c_driver = { 109 .driver = { 110 .name = "bmi323", 111 .of_match_table = bmi323_of_i2c_match, 112 }, 113 .probe = bmi323_i2c_probe, 114 .id_table = bmi323_i2c_ids, 115 }; 116 module_i2c_driver(bmi323_i2c_driver); 117 118 MODULE_DESCRIPTION("Bosch BMI323 IMU driver"); 119 MODULE_AUTHOR("Jagath Jog J <jagathjog1996@gmail.com>"); 120 MODULE_LICENSE("GPL"); 121 MODULE_IMPORT_NS(IIO_BMI323); 122