Merge tag 'v5.15.57' into rpi-5.15.y
[platform/kernel/linux-rpi.git] / drivers / mfd / simple-mfd-i2c.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Simple MFD - I2C
4  *
5  * Author(s):
6  *      Michael Walle <michael@walle.cc>
7  *      Lee Jones <lee.jones@linaro.org>
8  *
9  * This driver creates a single register map with the intention for it to be
10  * shared by all sub-devices.  Children can use their parent's device structure
11  * (dev.parent) in order to reference it.
12  *
13  * Once the register map has been successfully initialised, any sub-devices
14  * represented by child nodes in Device Tree or via the MFD cells in this file
15  * will be subsequently registered.
16  */
17
18 #include <linux/i2c.h>
19 #include <linux/kernel.h>
20 #include <linux/mfd/core.h>
21 #include <linux/module.h>
22 #include <linux/of_platform.h>
23 #include <linux/regmap.h>
24
25 #include "simple-mfd-i2c.h"
26
27 static const struct regmap_config regmap_config_8r_8v = {
28         .reg_bits = 8,
29         .val_bits = 8,
30 };
31
32 static const struct regmap_config regmap_config_16r_8v = {
33         .reg_bits = 16,
34         .val_bits = 8,
35 };
36
37 static const struct simple_mfd_data rpi_poe_core = {
38         .regmap_config = &regmap_config_16r_8v,
39 };
40
41 static int simple_mfd_i2c_probe(struct i2c_client *i2c)
42 {
43         const struct simple_mfd_data *simple_mfd_data;
44         const struct regmap_config *regmap_config;
45         struct regmap *regmap;
46         int ret;
47
48         simple_mfd_data = device_get_match_data(&i2c->dev);
49
50         /* If no regmap_config is specified, use the default 8reg and 8val bits */
51         if (!simple_mfd_data || !simple_mfd_data->regmap_config)
52                 regmap_config = &regmap_config_8r_8v;
53         else
54                 regmap_config = simple_mfd_data->regmap_config;
55
56         regmap = devm_regmap_init_i2c(i2c, regmap_config);
57         if (IS_ERR(regmap))
58                 return PTR_ERR(regmap);
59
60         /* If no MFD cells are spedified, use register the DT child nodes instead */
61         if (!simple_mfd_data || !simple_mfd_data->mfd_cell)
62                 return devm_of_platform_populate(&i2c->dev);
63
64         ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_AUTO,
65                                    simple_mfd_data->mfd_cell,
66                                    simple_mfd_data->mfd_cell_size,
67                                    NULL, 0, NULL);
68         if (ret)
69                 dev_err(&i2c->dev, "Failed to add child devices\n");
70
71         return ret;
72 }
73
74 static const struct of_device_id simple_mfd_i2c_of_match[] = {
75         { .compatible = "kontron,sl28cpld" },
76         { .compatible = "raspberrypi,poe-core", &rpi_poe_core },
77         {}
78 };
79 MODULE_DEVICE_TABLE(of, simple_mfd_i2c_of_match);
80
81 static struct i2c_driver simple_mfd_i2c_driver = {
82         .probe_new = simple_mfd_i2c_probe,
83         .driver = {
84                 .name = "simple-mfd-i2c",
85                 .of_match_table = simple_mfd_i2c_of_match,
86         },
87 };
88 module_i2c_driver(simple_mfd_i2c_driver);
89
90 MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
91 MODULE_DESCRIPTION("Simple MFD - I2C driver");
92 MODULE_LICENSE("GPL v2");