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
17static const struct regmap_range hmc5843_readable_ranges[] = {
18 regmap_reg_range(0, HMC5843_ID_END),
19};
20
21static 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
26static const struct regmap_range hmc5843_writable_ranges[] = {
27 regmap_reg_range(0, HMC5843_MODE_REG),
28};
29
30static 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
35static const struct regmap_range hmc5843_volatile_ranges[] = {
36 regmap_reg_range(HMC5843_DATA_OUT_MSB_REGS, HMC5843_STATUS_REG),
37};
38
39static 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
44static 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
55static int hmc5843_i2c_probe(struct i2c_client *cli)
56{
57 const struct i2c_device_id *id = i2c_client_get_device_id(client: cli);
58 struct regmap *regmap = devm_regmap_init_i2c(cli,
59 &hmc5843_i2c_regmap_config);
60 if (IS_ERR(ptr: regmap))
61 return PTR_ERR(ptr: regmap);
62
63 return hmc5843_common_probe(dev: &cli->dev,
64 regmap,
65 id: id->driver_data, name: id->name);
66}
67
68static void hmc5843_i2c_remove(struct i2c_client *client)
69{
70 hmc5843_common_remove(dev: &client->dev);
71}
72
73static 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};
80MODULE_DEVICE_TABLE(i2c, hmc5843_id);
81
82static 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};
89MODULE_DEVICE_TABLE(of, hmc5843_of_match);
90
91static 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};
101module_i2c_driver(hmc5843_driver);
102
103MODULE_AUTHOR("Josef Gajdusek <atx@atx.name>");
104MODULE_DESCRIPTION("HMC5843/5883/5883L/5983 i2c driver");
105MODULE_LICENSE("GPL");
106MODULE_IMPORT_NS(IIO_HMC5843);
107

source code of linux/drivers/iio/magnetometer/hmc5843_i2c.c