xref: /linux/drivers/iio/accel/bma220_i2c.c (revision 83bd89291f5cc866f60d32c34e268896c7ba8a3d)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Bosch triaxial acceleration sensor
4  *
5  * Copyright (c) 2025 Petre Rodan <petre.rodan@subdimension.ro>
6  *
7  * Datasheet: https://media.digikey.com/pdf/Data%20Sheets/Bosch/BMA220.pdf
8  * I2C address is either 0x0b or 0x0a depending on CSB (pin 10)
9  */
10 
11 #include <linux/bitfield.h>
12 #include <linux/i2c.h>
13 #include <linux/mod_devicetable.h>
14 #include <linux/module.h>
15 #include <linux/regmap.h>
16 #include <linux/types.h>
17 
18 #include "bma220.h"
19 
bma220_set_wdt(struct regmap * regmap,const u8 val)20 static int bma220_set_wdt(struct regmap *regmap, const u8 val)
21 {
22 	return regmap_update_bits(regmap, BMA220_REG_WDT, BMA220_WDT_MASK,
23 				  FIELD_PREP(BMA220_WDT_MASK, val));
24 }
25 
bma220_i2c_probe(struct i2c_client * client)26 static int bma220_i2c_probe(struct i2c_client *client)
27 {
28 	struct regmap *regmap;
29 	int ret;
30 
31 	regmap = devm_regmap_init_i2c(client, &bma220_i2c_regmap_config);
32 	if (IS_ERR(regmap))
33 		return dev_err_probe(&client->dev, PTR_ERR(regmap),
34 				     "failed to create regmap\n");
35 
36 	ret = bma220_common_probe(&client->dev, regmap, client->irq);
37 	if (ret)
38 		return ret;
39 
40 	return bma220_set_wdt(regmap, BMA220_WDT_1MS);
41 }
42 
43 static const struct of_device_id bma220_i2c_match[] = {
44 	{ .compatible = "bosch,bma220" },
45 	{ }
46 };
47 MODULE_DEVICE_TABLE(of, bma220_i2c_match);
48 
49 static const struct i2c_device_id bma220_i2c_id[] = {
50 	{ "bma220" },
51 	{ }
52 };
53 MODULE_DEVICE_TABLE(i2c, bma220_i2c_id);
54 
55 static struct i2c_driver bma220_i2c_driver = {
56 	.driver = {
57 		.name = "bma220_i2c",
58 		.pm = pm_sleep_ptr(&bma220_pm_ops),
59 		.of_match_table = bma220_i2c_match,
60 	},
61 	.probe = bma220_i2c_probe,
62 	.id_table = bma220_i2c_id,
63 };
64 module_i2c_driver(bma220_i2c_driver);
65 
66 MODULE_AUTHOR("Petre Rodan <petre.rodan@subdimension.ro>");
67 MODULE_DESCRIPTION("Bosch triaxial acceleration sensor i2c driver");
68 MODULE_LICENSE("GPL");
69 MODULE_IMPORT_NS("IIO_BOSCH_BMA220");
70