xref: /linux/drivers/iio/magnetometer/hmc5843_i2c.c (revision 58f6259b7a08f8d47d4629609703d358b042f0fd)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * i2c driver for hmc5843/5843/5883/5883l/5983
4  *
5  * Split from hmc5843.c
6  * Copyright (C) Josef Gajdusek <atx@atx.name>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/i2c.h>
11 #include <linux/regmap.h>
12 #include <linux/iio/iio.h>
13 #include <linux/iio/triggered_buffer.h>
14 
15 #include "hmc5843.h"
16 
17 static const struct regmap_range hmc5843_readable_ranges[] = {
18 	regmap_reg_range(0, HMC5843_ID_END),
19 };
20 
21 static const struct regmap_access_table hmc5843_readable_table = {
22 	.yes_ranges = hmc5843_readable_ranges,
23 	.n_yes_ranges = ARRAY_SIZE(hmc5843_readable_ranges),
24 };
25 
26 static const struct regmap_range hmc5843_writable_ranges[] = {
27 	regmap_reg_range(0, HMC5843_MODE_REG),
28 };
29 
30 static const struct regmap_access_table hmc5843_writable_table = {
31 	.yes_ranges = hmc5843_writable_ranges,
32 	.n_yes_ranges = ARRAY_SIZE(hmc5843_writable_ranges),
33 };
34 
35 static const struct regmap_range hmc5843_volatile_ranges[] = {
36 	regmap_reg_range(HMC5843_DATA_OUT_MSB_REGS, HMC5843_STATUS_REG),
37 };
38 
39 static const struct regmap_access_table hmc5843_volatile_table = {
40 	.yes_ranges = hmc5843_volatile_ranges,
41 	.n_yes_ranges = ARRAY_SIZE(hmc5843_volatile_ranges),
42 };
43 
44 static const struct regmap_config hmc5843_i2c_regmap_config = {
45 	.reg_bits = 8,
46 	.val_bits = 8,
47 
48 	.rd_table = &hmc5843_readable_table,
49 	.wr_table = &hmc5843_writable_table,
50 	.volatile_table = &hmc5843_volatile_table,
51 
52 	.cache_type = REGCACHE_RBTREE,
53 };
54 
55 static int hmc5843_i2c_probe(struct i2c_client *cli)
56 {
57 	const struct i2c_device_id *id = i2c_client_get_device_id(cli);
58 	struct regmap *regmap = devm_regmap_init_i2c(cli,
59 			&hmc5843_i2c_regmap_config);
60 	if (IS_ERR(regmap))
61 		return PTR_ERR(regmap);
62 
63 	return hmc5843_common_probe(&cli->dev,
64 			regmap,
65 			id->driver_data, id->name);
66 }
67 
68 static void hmc5843_i2c_remove(struct i2c_client *client)
69 {
70 	hmc5843_common_remove(&client->dev);
71 }
72 
73 static const struct i2c_device_id hmc5843_id[] = {
74 	{ "hmc5843", HMC5843_ID },
75 	{ "hmc5883", HMC5883_ID },
76 	{ "hmc5883l", HMC5883L_ID },
77 	{ "hmc5983", HMC5983_ID },
78 	{ }
79 };
80 MODULE_DEVICE_TABLE(i2c, hmc5843_id);
81 
82 static const struct of_device_id hmc5843_of_match[] = {
83 	{ .compatible = "honeywell,hmc5843", .data = (void *)HMC5843_ID },
84 	{ .compatible = "honeywell,hmc5883", .data = (void *)HMC5883_ID },
85 	{ .compatible = "honeywell,hmc5883l", .data = (void *)HMC5883L_ID },
86 	{ .compatible = "honeywell,hmc5983", .data = (void *)HMC5983_ID },
87 	{}
88 };
89 MODULE_DEVICE_TABLE(of, hmc5843_of_match);
90 
91 static struct i2c_driver hmc5843_driver = {
92 	.driver = {
93 		.name	= "hmc5843",
94 		.pm	= pm_sleep_ptr(&hmc5843_pm_ops),
95 		.of_match_table = hmc5843_of_match,
96 	},
97 	.id_table	= hmc5843_id,
98 	.probe		= hmc5843_i2c_probe,
99 	.remove		= hmc5843_i2c_remove,
100 };
101 module_i2c_driver(hmc5843_driver);
102 
103 MODULE_AUTHOR("Josef Gajdusek <atx@atx.name>");
104 MODULE_DESCRIPTION("HMC5843/5883/5883L/5983 i2c driver");
105 MODULE_LICENSE("GPL");
106 MODULE_IMPORT_NS(IIO_HMC5843);
107