xref: /openbmc/linux/drivers/mfd/simple-mfd-i2c.c (revision 5913eb45d036288babb67e97b210233537ef7b90)
13abee457SMichael Walle // SPDX-License-Identifier: GPL-2.0-only
23abee457SMichael Walle /*
33abee457SMichael Walle  * Simple MFD - I2C
43abee457SMichael Walle  *
5c753ea31SLee Jones  * Author(s):
6c753ea31SLee Jones  * 	Michael Walle <michael@walle.cc>
7c753ea31SLee Jones  * 	Lee Jones <lee.jones@linaro.org>
8c753ea31SLee Jones  *
93abee457SMichael Walle  * This driver creates a single register map with the intention for it to be
103abee457SMichael Walle  * shared by all sub-devices.  Children can use their parent's device structure
113abee457SMichael Walle  * (dev.parent) in order to reference it.
123abee457SMichael Walle  *
133abee457SMichael Walle  * Once the register map has been successfully initialised, any sub-devices
14c753ea31SLee Jones  * represented by child nodes in Device Tree or via the MFD cells in this file
15c753ea31SLee Jones  * will be subsequently registered.
163abee457SMichael Walle  */
173abee457SMichael Walle 
183abee457SMichael Walle #include <linux/i2c.h>
193abee457SMichael Walle #include <linux/kernel.h>
20c753ea31SLee Jones #include <linux/mfd/core.h>
213abee457SMichael Walle #include <linux/module.h>
223abee457SMichael Walle #include <linux/of_platform.h>
233abee457SMichael Walle #include <linux/regmap.h>
243abee457SMichael Walle 
25c753ea31SLee Jones #include "simple-mfd-i2c.h"
26c753ea31SLee Jones 
27c753ea31SLee Jones static const struct regmap_config regmap_config_8r_8v = {
283abee457SMichael Walle 	.reg_bits = 8,
293abee457SMichael Walle 	.val_bits = 8,
303abee457SMichael Walle };
313abee457SMichael Walle 
323abee457SMichael Walle static int simple_mfd_i2c_probe(struct i2c_client *i2c)
333abee457SMichael Walle {
34c753ea31SLee Jones 	const struct simple_mfd_data *simple_mfd_data;
35c753ea31SLee Jones 	const struct regmap_config *regmap_config;
363abee457SMichael Walle 	struct regmap *regmap;
37c753ea31SLee Jones 	int ret;
383abee457SMichael Walle 
39c753ea31SLee Jones 	simple_mfd_data = device_get_match_data(&i2c->dev);
403abee457SMichael Walle 
41c753ea31SLee Jones 	/* If no regmap_config is specified, use the default 8reg and 8val bits */
42c753ea31SLee Jones 	if (!simple_mfd_data || !simple_mfd_data->regmap_config)
43c753ea31SLee Jones 		regmap_config = &regmap_config_8r_8v;
44c753ea31SLee Jones 	else
45c753ea31SLee Jones 		regmap_config = simple_mfd_data->regmap_config;
46c753ea31SLee Jones 
47c753ea31SLee Jones 	regmap = devm_regmap_init_i2c(i2c, regmap_config);
483abee457SMichael Walle 	if (IS_ERR(regmap))
493abee457SMichael Walle 		return PTR_ERR(regmap);
503abee457SMichael Walle 
51c753ea31SLee Jones 	/* If no MFD cells are spedified, use register the DT child nodes instead */
52c753ea31SLee Jones 	if (!simple_mfd_data || !simple_mfd_data->mfd_cell)
533abee457SMichael Walle 		return devm_of_platform_populate(&i2c->dev);
54c753ea31SLee Jones 
55c753ea31SLee Jones 	ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_AUTO,
56c753ea31SLee Jones 				   simple_mfd_data->mfd_cell,
57c753ea31SLee Jones 				   simple_mfd_data->mfd_cell_size,
58c753ea31SLee Jones 				   NULL, 0, NULL);
59c753ea31SLee Jones 	if (ret)
60c753ea31SLee Jones 		dev_err(&i2c->dev, "Failed to add child devices\n");
61c753ea31SLee Jones 
62c753ea31SLee Jones 	return ret;
633abee457SMichael Walle }
643abee457SMichael Walle 
65*5913eb45SAlistair Francis static const struct mfd_cell sy7636a_cells[] = {
66*5913eb45SAlistair Francis 	{ .name = "sy7636a-regulator", },
67*5913eb45SAlistair Francis 	{ .name = "sy7636a-temperature", },
68*5913eb45SAlistair Francis };
69*5913eb45SAlistair Francis 
70*5913eb45SAlistair Francis static const struct simple_mfd_data silergy_sy7636a = {
71*5913eb45SAlistair Francis 	.mfd_cell = sy7636a_cells,
72*5913eb45SAlistair Francis 	.mfd_cell_size = ARRAY_SIZE(sy7636a_cells),
73*5913eb45SAlistair Francis };
74*5913eb45SAlistair Francis 
753abee457SMichael Walle static const struct of_device_id simple_mfd_i2c_of_match[] = {
76a538ad22SMichael Walle 	{ .compatible = "kontron,sl28cpld" },
77*5913eb45SAlistair Francis 	{ .compatible = "silergy,sy7636a", .data = &silergy_sy7636a},
783abee457SMichael Walle 	{}
793abee457SMichael Walle };
803abee457SMichael Walle MODULE_DEVICE_TABLE(of, simple_mfd_i2c_of_match);
813abee457SMichael Walle 
823abee457SMichael Walle static struct i2c_driver simple_mfd_i2c_driver = {
833abee457SMichael Walle 	.probe_new = simple_mfd_i2c_probe,
843abee457SMichael Walle 	.driver = {
853abee457SMichael Walle 		.name = "simple-mfd-i2c",
863abee457SMichael Walle 		.of_match_table = simple_mfd_i2c_of_match,
873abee457SMichael Walle 	},
883abee457SMichael Walle };
893abee457SMichael Walle module_i2c_driver(simple_mfd_i2c_driver);
903abee457SMichael Walle 
913abee457SMichael Walle MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
923abee457SMichael Walle MODULE_DESCRIPTION("Simple MFD - I2C driver");
933abee457SMichael Walle MODULE_LICENSE("GPL v2");
94