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