xref: /openbmc/linux/drivers/mfd/simple-mfd-i2c.c (revision 9816d859)
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 
simple_mfd_i2c_probe(struct i2c_client * i2c)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 
5181435ed2SLee Jones 	/* If no MFD cells are specified, register using 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 
655913eb45SAlistair Francis static const struct mfd_cell sy7636a_cells[] = {
665913eb45SAlistair Francis 	{ .name = "sy7636a-regulator", },
675913eb45SAlistair Francis 	{ .name = "sy7636a-temperature", },
685913eb45SAlistair Francis };
695913eb45SAlistair Francis 
705913eb45SAlistair Francis static const struct simple_mfd_data silergy_sy7636a = {
715913eb45SAlistair Francis 	.mfd_cell = sy7636a_cells,
725913eb45SAlistair Francis 	.mfd_cell_size = ARRAY_SIZE(sy7636a_cells),
735913eb45SAlistair Francis };
745913eb45SAlistair Francis 
7549f661baSNaresh Solanki static const struct mfd_cell max5970_cells[] = {
7649f661baSNaresh Solanki 	{ .name = "max5970-regulator", },
7749f661baSNaresh Solanki 	{ .name = "max5970-iio", },
7849f661baSNaresh Solanki 	{ .name = "max5970-led", },
790742c2a6SPatrick Rudolph };
800742c2a6SPatrick Rudolph 
8149f661baSNaresh Solanki static const struct simple_mfd_data maxim_max5970 = {
8249f661baSNaresh Solanki 	.mfd_cell = max5970_cells,
8349f661baSNaresh Solanki 	.mfd_cell_size = ARRAY_SIZE(max5970_cells),
840742c2a6SPatrick Rudolph };
850742c2a6SPatrick Rudolph 
863abee457SMichael Walle static const struct of_device_id simple_mfd_i2c_of_match[] = {
87a538ad22SMichael Walle 	{ .compatible = "kontron,sl28cpld" },
885913eb45SAlistair Francis 	{ .compatible = "silergy,sy7636a", .data = &silergy_sy7636a},
8949f661baSNaresh Solanki 	{ .compatible = "maxim,max5970", .data = &maxim_max5970},
9049f661baSNaresh Solanki 	{ .compatible = "maxim,max5978", .data = &maxim_max5970},
913abee457SMichael Walle 	{}
923abee457SMichael Walle };
933abee457SMichael Walle MODULE_DEVICE_TABLE(of, simple_mfd_i2c_of_match);
943abee457SMichael Walle 
953abee457SMichael Walle static struct i2c_driver simple_mfd_i2c_driver = {
96*9816d859SUwe Kleine-König 	.probe = simple_mfd_i2c_probe,
973abee457SMichael Walle 	.driver = {
983abee457SMichael Walle 		.name = "simple-mfd-i2c",
993abee457SMichael Walle 		.of_match_table = simple_mfd_i2c_of_match,
1003abee457SMichael Walle 	},
1013abee457SMichael Walle };
1023abee457SMichael Walle module_i2c_driver(simple_mfd_i2c_driver);
1033abee457SMichael Walle 
1043abee457SMichael Walle MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
1053abee457SMichael Walle MODULE_DESCRIPTION("Simple MFD - I2C driver");
1063abee457SMichael Walle MODULE_LICENSE("GPL v2");
107