xref: /linux/drivers/mfd/adp5585.c (revision 1a4eabf662543c62ae1e71a26d1c8e6643c66388)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Analog Devices ADP5585 I/O expander, PWM controller and keypad controller
4  *
5  * Copyright 2022 NXP
6  * Copyright 2024 Ideas on Board Oy
7  * Copyright 2025 Analog Devices Inc.
8  */
9 
10 #include <linux/array_size.h>
11 #include <linux/device.h>
12 #include <linux/err.h>
13 #include <linux/i2c.h>
14 #include <linux/mfd/adp5585.h>
15 #include <linux/mfd/core.h>
16 #include <linux/mod_devicetable.h>
17 #include <linux/module.h>
18 #include <linux/regmap.h>
19 #include <linux/types.h>
20 
21 enum {
22 	ADP5585_DEV_GPIO,
23 	ADP5585_DEV_PWM,
24 	ADP5585_DEV_MAX
25 };
26 
27 static const struct mfd_cell adp5585_devs[ADP5585_DEV_MAX] = {
28 	MFD_CELL_NAME("adp5585-gpio"),
29 	MFD_CELL_NAME("adp5585-pwm"),
30 };
31 
32 static const struct regmap_range adp5585_volatile_ranges[] = {
33 	regmap_reg_range(ADP5585_ID, ADP5585_GPI_STATUS_B),
34 };
35 
36 static const struct regmap_access_table adp5585_volatile_regs = {
37 	.yes_ranges = adp5585_volatile_ranges,
38 	.n_yes_ranges = ARRAY_SIZE(adp5585_volatile_ranges),
39 };
40 
41 /*
42  * Chip variants differ in the default configuration of pull-up and pull-down
43  * resistors, and therefore have different default register values:
44  *
45  * - The -00, -01 and -03 variants (collectively referred to as
46  *   ADP5585_REGMAP_00) have pull-up on all GPIO pins by default.
47  * - The -02 variant has no default pull-up or pull-down resistors.
48  * - The -04 variant has default pull-down resistors on all GPIO pins.
49  */
50 
51 static const u8 adp5585_regmap_defaults_00[ADP5585_MAX_REG + 1] = {
52 	/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
53 	/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
54 	/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 	/* 0x18 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 	/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 	/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 	/* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59 	/* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
60 };
61 
62 static const u8 adp5585_regmap_defaults_02[ADP5585_MAX_REG + 1] = {
63 	/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 	/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 	/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc3,
66 	/* 0x18 */ 0x03, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00,
67 	/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68 	/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 	/* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70 	/* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
71 };
72 
73 static const u8 adp5585_regmap_defaults_04[ADP5585_MAX_REG + 1] = {
74 	/* 0x00 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 	/* 0x08 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
76 	/* 0x10 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x55,
77 	/* 0x18 */ 0x05, 0x55, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
78 	/* 0x20 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
79 	/* 0x28 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
80 	/* 0x30 */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81 	/* 0x38 */ 0x00, 0x00, 0x00, 0x00, 0x00,
82 };
83 
84 static const u8 *adp5585_regmap_defaults[ADP5585_MAX] = {
85 	[ADP5585_00] = adp5585_regmap_defaults_00,
86 	[ADP5585_01] = adp5585_regmap_defaults_00,
87 	[ADP5585_02] = adp5585_regmap_defaults_02,
88 	[ADP5585_03] = adp5585_regmap_defaults_00,
89 	[ADP5585_04] = adp5585_regmap_defaults_04,
90 };
91 
92 static const struct regmap_config adp5585_regmap_config_template = {
93 	.reg_bits = 8,
94 	.val_bits = 8,
95 	.max_register = ADP5585_MAX_REG,
96 	.volatile_table = &adp5585_volatile_regs,
97 	.cache_type = REGCACHE_MAPLE,
98 	.num_reg_defaults_raw = ADP5585_MAX_REG + 1,
99 };
100 
101 static struct regmap_config *adp5585_fill_regmap_config(const struct adp5585_dev *adp5585)
102 {
103 	struct regmap_config *regmap_config;
104 
105 	regmap_config = devm_kmemdup(adp5585->dev, &adp5585_regmap_config_template,
106 				     sizeof(*regmap_config), GFP_KERNEL);
107 	if (!regmap_config)
108 		return ERR_PTR(-ENOMEM);
109 
110 	regmap_config->reg_defaults_raw = adp5585_regmap_defaults[adp5585->variant];
111 	return regmap_config;
112 }
113 
114 static int adp5585_add_devices(struct device *dev)
115 {
116 	int ret;
117 
118 	if (device_property_present(dev, "#pwm-cells")) {
119 		ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO,
120 					   &adp5585_devs[ADP5585_DEV_PWM], 1, NULL, 0, NULL);
121 		if (ret)
122 			return dev_err_probe(dev, ret, "Failed to add PWM device\n");
123 	}
124 
125 	if (device_property_present(dev, "#gpio-cells")) {
126 		ret = devm_mfd_add_devices(dev, PLATFORM_DEVID_AUTO,
127 					   &adp5585_devs[ADP5585_DEV_GPIO], 1, NULL, 0, NULL);
128 		if (ret)
129 			return dev_err_probe(dev, ret, "Failed to add GPIO device\n");
130 	}
131 
132 	return 0;
133 }
134 
135 static void adp5585_osc_disable(void *data)
136 {
137 	const struct adp5585_dev *adp5585 = data;
138 
139 	regmap_write(adp5585->regmap, ADP5585_GENERAL_CFG, 0);
140 }
141 
142 static int adp5585_i2c_probe(struct i2c_client *i2c)
143 {
144 	struct regmap_config *regmap_config;
145 	struct adp5585_dev *adp5585;
146 	unsigned int id;
147 	int ret;
148 
149 	adp5585 = devm_kzalloc(&i2c->dev, sizeof(*adp5585), GFP_KERNEL);
150 	if (!adp5585)
151 		return -ENOMEM;
152 
153 	i2c_set_clientdata(i2c, adp5585);
154 	adp5585->dev = &i2c->dev;
155 
156 	adp5585->variant = (enum adp5585_variant)(uintptr_t)i2c_get_match_data(i2c);
157 	if (!adp5585->variant)
158 		return -ENODEV;
159 
160 	regmap_config = adp5585_fill_regmap_config(adp5585);
161 	if (IS_ERR(regmap_config))
162 		return PTR_ERR(regmap_config);
163 
164 	adp5585->regmap = devm_regmap_init_i2c(i2c, regmap_config);
165 	if (IS_ERR(adp5585->regmap))
166 		return dev_err_probe(&i2c->dev, PTR_ERR(adp5585->regmap),
167 				     "Failed to initialize register map\n");
168 
169 	ret = regmap_read(adp5585->regmap, ADP5585_ID, &id);
170 	if (ret)
171 		return dev_err_probe(&i2c->dev, ret,
172 				     "Failed to read device ID\n");
173 
174 	if ((id & ADP5585_MAN_ID_MASK) != ADP5585_MAN_ID_VALUE)
175 		return dev_err_probe(&i2c->dev, -ENODEV,
176 				     "Invalid device ID 0x%02x\n", id);
177 
178 	/*
179 	 * Enable the internal oscillator, as it's shared between multiple
180 	 * functions.
181 	 */
182 	ret = regmap_set_bits(adp5585->regmap, ADP5585_GENERAL_CFG, ADP5585_OSC_EN);
183 	if (ret)
184 		return ret;
185 
186 	ret = devm_add_action_or_reset(&i2c->dev, adp5585_osc_disable, adp5585);
187 	if (ret)
188 		return ret;
189 
190 	return adp5585_add_devices(&i2c->dev);
191 }
192 
193 static int adp5585_suspend(struct device *dev)
194 {
195 	struct adp5585_dev *adp5585 = dev_get_drvdata(dev);
196 
197 	regcache_cache_only(adp5585->regmap, true);
198 
199 	return 0;
200 }
201 
202 static int adp5585_resume(struct device *dev)
203 {
204 	struct adp5585_dev *adp5585 = dev_get_drvdata(dev);
205 
206 	regcache_cache_only(adp5585->regmap, false);
207 	regcache_mark_dirty(adp5585->regmap);
208 
209 	return regcache_sync(adp5585->regmap);
210 }
211 
212 static DEFINE_SIMPLE_DEV_PM_OPS(adp5585_pm, adp5585_suspend, adp5585_resume);
213 
214 static const struct of_device_id adp5585_of_match[] = {
215 	{
216 		.compatible = "adi,adp5585-00",
217 		.data = (void *)ADP5585_00,
218 	}, {
219 		.compatible = "adi,adp5585-01",
220 		.data = (void *)ADP5585_01,
221 	}, {
222 		.compatible = "adi,adp5585-02",
223 		.data = (void *)ADP5585_02,
224 	}, {
225 		.compatible = "adi,adp5585-03",
226 		.data = (void *)ADP5585_03,
227 	}, {
228 		.compatible = "adi,adp5585-04",
229 		.data = (void *)ADP5585_04,
230 	},
231 	{ /* sentinel */ }
232 };
233 MODULE_DEVICE_TABLE(of, adp5585_of_match);
234 
235 static struct i2c_driver adp5585_i2c_driver = {
236 	.driver = {
237 		.name = "adp5585",
238 		.of_match_table = adp5585_of_match,
239 		.pm = pm_sleep_ptr(&adp5585_pm),
240 	},
241 	.probe = adp5585_i2c_probe,
242 };
243 module_i2c_driver(adp5585_i2c_driver);
244 
245 MODULE_DESCRIPTION("ADP5585 core driver");
246 MODULE_AUTHOR("Haibo Chen <haibo.chen@nxp.com>");
247 MODULE_LICENSE("GPL");
248