xref: /openbmc/linux/drivers/mfd/sec-core.c (revision 745b7908)
1 // SPDX-License-Identifier: GPL-2.0+
2 //
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd
4 //              http://www.samsung.com
5 
6 #include <linux/module.h>
7 #include <linux/moduleparam.h>
8 #include <linux/init.h>
9 #include <linux/err.h>
10 #include <linux/slab.h>
11 #include <linux/i2c.h>
12 #include <linux/of.h>
13 #include <linux/of_device.h>
14 #include <linux/of_irq.h>
15 #include <linux/interrupt.h>
16 #include <linux/pm_runtime.h>
17 #include <linux/mutex.h>
18 #include <linux/mfd/core.h>
19 #include <linux/mfd/samsung/core.h>
20 #include <linux/mfd/samsung/irq.h>
21 #include <linux/mfd/samsung/s2mpa01.h>
22 #include <linux/mfd/samsung/s2mps11.h>
23 #include <linux/mfd/samsung/s2mps13.h>
24 #include <linux/mfd/samsung/s2mps14.h>
25 #include <linux/mfd/samsung/s2mps15.h>
26 #include <linux/mfd/samsung/s2mpu02.h>
27 #include <linux/mfd/samsung/s5m8767.h>
28 #include <linux/regmap.h>
29 
30 static const struct mfd_cell s5m8767_devs[] = {
31 	{ .name = "s5m8767-pmic", },
32 	{ .name = "s5m-rtc", },
33 	{
34 		.name = "s5m8767-clk",
35 		.of_compatible = "samsung,s5m8767-clk",
36 	},
37 };
38 
39 static const struct mfd_cell s2mps11_devs[] = {
40 	{ .name = "s2mps11-regulator", },
41 	{ .name = "s2mps14-rtc", },
42 	{
43 		.name = "s2mps11-clk",
44 		.of_compatible = "samsung,s2mps11-clk",
45 	},
46 };
47 
48 static const struct mfd_cell s2mps13_devs[] = {
49 	{ .name = "s2mps13-regulator", },
50 	{ .name = "s2mps13-rtc", },
51 	{
52 		.name = "s2mps13-clk",
53 		.of_compatible = "samsung,s2mps13-clk",
54 	},
55 };
56 
57 static const struct mfd_cell s2mps14_devs[] = {
58 	{ .name = "s2mps14-regulator", },
59 	{ .name = "s2mps14-rtc", },
60 	{
61 		.name = "s2mps14-clk",
62 		.of_compatible = "samsung,s2mps14-clk",
63 	},
64 };
65 
66 static const struct mfd_cell s2mps15_devs[] = {
67 	{ .name = "s2mps15-regulator", },
68 	{ .name = "s2mps15-rtc", },
69 	{
70 		.name = "s2mps13-clk",
71 		.of_compatible = "samsung,s2mps13-clk",
72 	},
73 };
74 
75 static const struct mfd_cell s2mpa01_devs[] = {
76 	{ .name = "s2mpa01-pmic", },
77 	{ .name = "s2mps14-rtc", },
78 };
79 
80 static const struct mfd_cell s2mpu02_devs[] = {
81 	{ .name = "s2mpu02-regulator", },
82 };
83 
84 static const struct of_device_id sec_dt_match[] = {
85 	{
86 		.compatible = "samsung,s5m8767-pmic",
87 		.data = (void *)S5M8767X,
88 	}, {
89 		.compatible = "samsung,s2mps11-pmic",
90 		.data = (void *)S2MPS11X,
91 	}, {
92 		.compatible = "samsung,s2mps13-pmic",
93 		.data = (void *)S2MPS13X,
94 	}, {
95 		.compatible = "samsung,s2mps14-pmic",
96 		.data = (void *)S2MPS14X,
97 	}, {
98 		.compatible = "samsung,s2mps15-pmic",
99 		.data = (void *)S2MPS15X,
100 	}, {
101 		.compatible = "samsung,s2mpa01-pmic",
102 		.data = (void *)S2MPA01,
103 	}, {
104 		.compatible = "samsung,s2mpu02-pmic",
105 		.data = (void *)S2MPU02,
106 	}, {
107 		/* Sentinel */
108 	},
109 };
110 MODULE_DEVICE_TABLE(of, sec_dt_match);
111 
112 static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
113 {
114 	switch (reg) {
115 	case S2MPA01_REG_INT1M:
116 	case S2MPA01_REG_INT2M:
117 	case S2MPA01_REG_INT3M:
118 		return false;
119 	default:
120 		return true;
121 	}
122 }
123 
124 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
125 {
126 	switch (reg) {
127 	case S2MPS11_REG_INT1M:
128 	case S2MPS11_REG_INT2M:
129 	case S2MPS11_REG_INT3M:
130 		return false;
131 	default:
132 		return true;
133 	}
134 }
135 
136 static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
137 {
138 	switch (reg) {
139 	case S2MPU02_REG_INT1M:
140 	case S2MPU02_REG_INT2M:
141 	case S2MPU02_REG_INT3M:
142 		return false;
143 	default:
144 		return true;
145 	}
146 }
147 
148 static const struct regmap_config sec_regmap_config = {
149 	.reg_bits = 8,
150 	.val_bits = 8,
151 };
152 
153 static const struct regmap_config s2mpa01_regmap_config = {
154 	.reg_bits = 8,
155 	.val_bits = 8,
156 
157 	.max_register = S2MPA01_REG_LDO_OVCB4,
158 	.volatile_reg = s2mpa01_volatile,
159 	.cache_type = REGCACHE_FLAT,
160 };
161 
162 static const struct regmap_config s2mps11_regmap_config = {
163 	.reg_bits = 8,
164 	.val_bits = 8,
165 
166 	.max_register = S2MPS11_REG_L38CTRL,
167 	.volatile_reg = s2mps11_volatile,
168 	.cache_type = REGCACHE_FLAT,
169 };
170 
171 static const struct regmap_config s2mps13_regmap_config = {
172 	.reg_bits = 8,
173 	.val_bits = 8,
174 
175 	.max_register = S2MPS13_REG_LDODSCH5,
176 	.volatile_reg = s2mps11_volatile,
177 	.cache_type = REGCACHE_FLAT,
178 };
179 
180 static const struct regmap_config s2mps14_regmap_config = {
181 	.reg_bits = 8,
182 	.val_bits = 8,
183 
184 	.max_register = S2MPS14_REG_LDODSCH3,
185 	.volatile_reg = s2mps11_volatile,
186 	.cache_type = REGCACHE_FLAT,
187 };
188 
189 static const struct regmap_config s2mps15_regmap_config = {
190 	.reg_bits = 8,
191 	.val_bits = 8,
192 
193 	.max_register = S2MPS15_REG_LDODSCH4,
194 	.volatile_reg = s2mps11_volatile,
195 	.cache_type = REGCACHE_FLAT,
196 };
197 
198 static const struct regmap_config s2mpu02_regmap_config = {
199 	.reg_bits = 8,
200 	.val_bits = 8,
201 
202 	.max_register = S2MPU02_REG_DVSDATA,
203 	.volatile_reg = s2mpu02_volatile,
204 	.cache_type = REGCACHE_FLAT,
205 };
206 
207 static const struct regmap_config s5m8767_regmap_config = {
208 	.reg_bits = 8,
209 	.val_bits = 8,
210 
211 	.max_register = S5M8767_REG_LDO28CTRL,
212 	.volatile_reg = s2mps11_volatile,
213 	.cache_type = REGCACHE_FLAT,
214 };
215 
216 static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic)
217 {
218 	unsigned int val;
219 
220 	/* For each device type, the REG_ID is always the first register */
221 	if (!regmap_read(sec_pmic->regmap_pmic, S2MPS11_REG_ID, &val))
222 		dev_dbg(sec_pmic->dev, "Revision: 0x%x\n", val);
223 }
224 
225 static void sec_pmic_configure(struct sec_pmic_dev *sec_pmic)
226 {
227 	int err;
228 
229 	if (sec_pmic->device_type != S2MPS13X)
230 		return;
231 
232 	if (sec_pmic->pdata->disable_wrstbi) {
233 		/*
234 		 * If WRSTBI pin is pulled down this feature must be disabled
235 		 * because each Suspend to RAM will trigger buck voltage reset
236 		 * to default values.
237 		 */
238 		err = regmap_update_bits(sec_pmic->regmap_pmic,
239 					 S2MPS13_REG_WRSTBI,
240 					 S2MPS13_REG_WRSTBI_MASK, 0x0);
241 		if (err)
242 			dev_warn(sec_pmic->dev,
243 				 "Cannot initialize WRSTBI config: %d\n",
244 				 err);
245 	}
246 }
247 
248 /*
249  * Only the common platform data elements for s5m8767 are parsed here from the
250  * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
251  * others have to parse their own platform data elements from device tree.
252  *
253  * The s5m8767 platform data structure is instantiated here and the drivers for
254  * the sub-modules need not instantiate another instance while parsing their
255  * platform data.
256  */
257 static struct sec_platform_data *
258 sec_pmic_i2c_parse_dt_pdata(struct device *dev)
259 {
260 	struct sec_platform_data *pd;
261 
262 	pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
263 	if (!pd)
264 		return ERR_PTR(-ENOMEM);
265 
266 	pd->manual_poweroff = of_property_read_bool(dev->of_node,
267 						"samsung,s2mps11-acokb-ground");
268 	pd->disable_wrstbi = of_property_read_bool(dev->of_node,
269 						"samsung,s2mps11-wrstbi-ground");
270 	return pd;
271 }
272 
273 static int sec_pmic_probe(struct i2c_client *i2c)
274 {
275 	const struct regmap_config *regmap;
276 	struct sec_platform_data *pdata;
277 	const struct mfd_cell *sec_devs;
278 	struct sec_pmic_dev *sec_pmic;
279 	int ret, num_sec_devs;
280 
281 	sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
282 				GFP_KERNEL);
283 	if (sec_pmic == NULL)
284 		return -ENOMEM;
285 
286 	i2c_set_clientdata(i2c, sec_pmic);
287 	sec_pmic->dev = &i2c->dev;
288 	sec_pmic->i2c = i2c;
289 	sec_pmic->irq = i2c->irq;
290 
291 	pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
292 	if (IS_ERR(pdata)) {
293 		ret = PTR_ERR(pdata);
294 		return ret;
295 	}
296 
297 	sec_pmic->device_type = (unsigned long)of_device_get_match_data(sec_pmic->dev);
298 	sec_pmic->pdata = pdata;
299 
300 	switch (sec_pmic->device_type) {
301 	case S2MPA01:
302 		regmap = &s2mpa01_regmap_config;
303 		break;
304 	case S2MPS11X:
305 		regmap = &s2mps11_regmap_config;
306 		break;
307 	case S2MPS13X:
308 		regmap = &s2mps13_regmap_config;
309 		break;
310 	case S2MPS14X:
311 		regmap = &s2mps14_regmap_config;
312 		break;
313 	case S2MPS15X:
314 		regmap = &s2mps15_regmap_config;
315 		break;
316 	case S5M8767X:
317 		regmap = &s5m8767_regmap_config;
318 		break;
319 	case S2MPU02:
320 		regmap = &s2mpu02_regmap_config;
321 		break;
322 	default:
323 		regmap = &sec_regmap_config;
324 		break;
325 	}
326 
327 	sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
328 	if (IS_ERR(sec_pmic->regmap_pmic)) {
329 		ret = PTR_ERR(sec_pmic->regmap_pmic);
330 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
331 			ret);
332 		return ret;
333 	}
334 
335 	sec_irq_init(sec_pmic);
336 
337 	pm_runtime_set_active(sec_pmic->dev);
338 
339 	switch (sec_pmic->device_type) {
340 	case S5M8767X:
341 		sec_devs = s5m8767_devs;
342 		num_sec_devs = ARRAY_SIZE(s5m8767_devs);
343 		break;
344 	case S2MPA01:
345 		sec_devs = s2mpa01_devs;
346 		num_sec_devs = ARRAY_SIZE(s2mpa01_devs);
347 		break;
348 	case S2MPS11X:
349 		sec_devs = s2mps11_devs;
350 		num_sec_devs = ARRAY_SIZE(s2mps11_devs);
351 		break;
352 	case S2MPS13X:
353 		sec_devs = s2mps13_devs;
354 		num_sec_devs = ARRAY_SIZE(s2mps13_devs);
355 		break;
356 	case S2MPS14X:
357 		sec_devs = s2mps14_devs;
358 		num_sec_devs = ARRAY_SIZE(s2mps14_devs);
359 		break;
360 	case S2MPS15X:
361 		sec_devs = s2mps15_devs;
362 		num_sec_devs = ARRAY_SIZE(s2mps15_devs);
363 		break;
364 	case S2MPU02:
365 		sec_devs = s2mpu02_devs;
366 		num_sec_devs = ARRAY_SIZE(s2mpu02_devs);
367 		break;
368 	default:
369 		dev_err(&i2c->dev, "Unsupported device type (%lu)\n",
370 			sec_pmic->device_type);
371 		return -ENODEV;
372 	}
373 	ret = devm_mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs,
374 				   NULL, 0, NULL);
375 	if (ret)
376 		return ret;
377 
378 	sec_pmic_configure(sec_pmic);
379 	sec_pmic_dump_rev(sec_pmic);
380 
381 	return ret;
382 }
383 
384 static void sec_pmic_shutdown(struct i2c_client *i2c)
385 {
386 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
387 	unsigned int reg, mask;
388 
389 	if (!sec_pmic->pdata->manual_poweroff)
390 		return;
391 
392 	switch (sec_pmic->device_type) {
393 	case S2MPS11X:
394 		reg = S2MPS11_REG_CTRL1;
395 		mask = S2MPS11_CTRL1_PWRHOLD_MASK;
396 		break;
397 	default:
398 		/*
399 		 * Currently only one board with S2MPS11 needs this, so just
400 		 * ignore the rest.
401 		 */
402 		dev_warn(sec_pmic->dev,
403 			"Unsupported device %lu for manual power off\n",
404 			sec_pmic->device_type);
405 		return;
406 	}
407 
408 	regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0);
409 }
410 
411 static int sec_pmic_suspend(struct device *dev)
412 {
413 	struct i2c_client *i2c = to_i2c_client(dev);
414 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
415 
416 	if (device_may_wakeup(dev))
417 		enable_irq_wake(sec_pmic->irq);
418 	/*
419 	 * PMIC IRQ must be disabled during suspend for RTC alarm
420 	 * to work properly.
421 	 * When device is woken up from suspend, an
422 	 * interrupt occurs before resuming I2C bus controller.
423 	 * The interrupt is handled by regmap_irq_thread which tries
424 	 * to read RTC registers. This read fails (I2C is still
425 	 * suspended) and RTC Alarm interrupt is disabled.
426 	 */
427 	disable_irq(sec_pmic->irq);
428 
429 	return 0;
430 }
431 
432 static int sec_pmic_resume(struct device *dev)
433 {
434 	struct i2c_client *i2c = to_i2c_client(dev);
435 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
436 
437 	if (device_may_wakeup(dev))
438 		disable_irq_wake(sec_pmic->irq);
439 	enable_irq(sec_pmic->irq);
440 
441 	return 0;
442 }
443 
444 static DEFINE_SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops,
445 				sec_pmic_suspend, sec_pmic_resume);
446 
447 static struct i2c_driver sec_pmic_driver = {
448 	.driver = {
449 		   .name = "sec_pmic",
450 		   .pm = pm_sleep_ptr(&sec_pmic_pm_ops),
451 		   .of_match_table = sec_dt_match,
452 	},
453 	.probe_new = sec_pmic_probe,
454 	.shutdown = sec_pmic_shutdown,
455 };
456 module_i2c_driver(sec_pmic_driver);
457 
458 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
459 MODULE_DESCRIPTION("Core support for the S5M MFD");
460 MODULE_LICENSE("GPL");
461