xref: /linux/drivers/mfd/simple-mfd-i2c.c (revision 509d3f45847627f4c5cdce004c3ec79262b5239c)
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/array_size.h>
19 #include <linux/dev_printk.h>
20 #include <linux/err.h>
21 #include <linux/i2c.h>
22 #include <linux/mfd/core.h>
23 #include <linux/mod_devicetable.h>
24 #include <linux/module.h>
25 #include <linux/of_platform.h>
26 #include <linux/platform_device.h>
27 #include <linux/property.h>
28 #include <linux/regmap.h>
29 #include <linux/stddef.h>
30 
31 #include "simple-mfd-i2c.h"
32 
33 static const struct regmap_config regmap_config_8r_8v = {
34 	.reg_bits = 8,
35 	.val_bits = 8,
36 };
37 
38 static int simple_mfd_i2c_probe(struct i2c_client *i2c)
39 {
40 	const struct simple_mfd_data *simple_mfd_data;
41 	const struct regmap_config *regmap_config;
42 	struct regmap *regmap;
43 	int ret;
44 
45 	simple_mfd_data = device_get_match_data(&i2c->dev);
46 
47 	/* If no regmap_config is specified, use the default 8reg and 8val bits */
48 	if (!simple_mfd_data || !simple_mfd_data->regmap_config)
49 		regmap_config = &regmap_config_8r_8v;
50 	else
51 		regmap_config = simple_mfd_data->regmap_config;
52 
53 	regmap = devm_regmap_init_i2c(i2c, regmap_config);
54 	if (IS_ERR(regmap))
55 		return PTR_ERR(regmap);
56 
57 	/* If no MFD cells are specified, register using the DT child nodes instead */
58 	if (!simple_mfd_data || !simple_mfd_data->mfd_cell)
59 		return devm_of_platform_populate(&i2c->dev);
60 
61 	ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_AUTO,
62 				   simple_mfd_data->mfd_cell,
63 				   simple_mfd_data->mfd_cell_size,
64 				   NULL, 0, NULL);
65 	if (ret)
66 		dev_err(&i2c->dev, "Failed to add child devices\n");
67 
68 	return ret;
69 }
70 
71 static const struct mfd_cell sy7636a_cells[] = {
72 	{ .name = "sy7636a-regulator", },
73 	{ .name = "sy7636a-temperature", },
74 };
75 
76 static const struct simple_mfd_data silergy_sy7636a = {
77 	.mfd_cell = sy7636a_cells,
78 	.mfd_cell_size = ARRAY_SIZE(sy7636a_cells),
79 };
80 
81 static const struct mfd_cell max5970_cells[] = {
82 	{ .name = "max5970-regulator", },
83 	{ .name = "max5970-iio", },
84 	{ .name = "max5970-led", },
85 };
86 
87 static const struct simple_mfd_data maxim_max5970 = {
88 	.mfd_cell = max5970_cells,
89 	.mfd_cell_size = ARRAY_SIZE(max5970_cells),
90 };
91 
92 static const struct mfd_cell max77705_sensor_cells[] = {
93 	{ .name = "max77705-battery" },
94 	{ .name = "max77705-hwmon", },
95 };
96 
97 static const struct simple_mfd_data maxim_mon_max77705 = {
98 	.mfd_cell = max77705_sensor_cells,
99 	.mfd_cell_size = ARRAY_SIZE(max77705_sensor_cells),
100 };
101 
102 static const struct regmap_config spacemit_p1_regmap_config = {
103 	.reg_bits = 8,
104 	.val_bits = 8,
105 };
106 
107 static const struct mfd_cell spacemit_p1_cells[] = {
108 	{ .name = "spacemit-p1-regulator", },
109 	{ .name = "spacemit-p1-rtc", },
110 };
111 
112 static const struct simple_mfd_data spacemit_p1 = {
113 	.regmap_config = &spacemit_p1_regmap_config,
114 	.mfd_cell = spacemit_p1_cells,
115 	.mfd_cell_size = ARRAY_SIZE(spacemit_p1_cells),
116 };
117 
118 static const struct of_device_id simple_mfd_i2c_of_match[] = {
119 	{ .compatible = "fsl,ls1028aqds-fpga" },
120 	{ .compatible = "fsl,lx2160aqds-fpga" },
121 	{ .compatible = "fsl,lx2160ardb-fpga" },
122 	{ .compatible = "kontron,sl28cpld" },
123 	{ .compatible = "maxim,max5970", .data = &maxim_max5970 },
124 	{ .compatible = "maxim,max5978", .data = &maxim_max5970 },
125 	{ .compatible = "maxim,max77705-battery", .data = &maxim_mon_max77705 },
126 	{ .compatible = "silergy,sy7636a", .data = &silergy_sy7636a },
127 	{ .compatible = "spacemit,p1", .data = &spacemit_p1 },
128 	{}
129 };
130 MODULE_DEVICE_TABLE(of, simple_mfd_i2c_of_match);
131 
132 static struct i2c_driver simple_mfd_i2c_driver = {
133 	.probe = simple_mfd_i2c_probe,
134 	.driver = {
135 		.name = "simple-mfd-i2c",
136 		.of_match_table = simple_mfd_i2c_of_match,
137 	},
138 };
139 module_i2c_driver(simple_mfd_i2c_driver);
140 
141 MODULE_AUTHOR("Michael Walle <michael@walle.cc>");
142 MODULE_DESCRIPTION("Simple MFD - I2C driver");
143 MODULE_LICENSE("GPL v2");
144