xref: /openbmc/linux/drivers/mfd/sec-core.c (revision d4295e12)
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_irq.h>
14 #include <linux/interrupt.h>
15 #include <linux/pm_runtime.h>
16 #include <linux/mutex.h>
17 #include <linux/mfd/core.h>
18 #include <linux/mfd/samsung/core.h>
19 #include <linux/mfd/samsung/irq.h>
20 #include <linux/mfd/samsung/s2mpa01.h>
21 #include <linux/mfd/samsung/s2mps11.h>
22 #include <linux/mfd/samsung/s2mps13.h>
23 #include <linux/mfd/samsung/s2mps14.h>
24 #include <linux/mfd/samsung/s2mps15.h>
25 #include <linux/mfd/samsung/s2mpu02.h>
26 #include <linux/mfd/samsung/s5m8763.h>
27 #include <linux/mfd/samsung/s5m8767.h>
28 #include <linux/regmap.h>
29 
30 static const struct mfd_cell s5m8751_devs[] = {
31 	{
32 		.name = "s5m8751-pmic",
33 	}, {
34 		.name = "s5m-charger",
35 	}, {
36 		.name = "s5m8751-codec",
37 	},
38 };
39 
40 static const struct mfd_cell s5m8763_devs[] = {
41 	{
42 		.name = "s5m8763-pmic",
43 	}, {
44 		.name = "s5m-rtc",
45 	}, {
46 		.name = "s5m-charger",
47 	},
48 };
49 
50 static const struct mfd_cell s5m8767_devs[] = {
51 	{
52 		.name = "s5m8767-pmic",
53 	}, {
54 		.name = "s5m-rtc",
55 	}, {
56 		.name = "s5m8767-clk",
57 		.of_compatible = "samsung,s5m8767-clk",
58 	}
59 };
60 
61 static const struct mfd_cell s2mps11_devs[] = {
62 	{
63 		.name = "s2mps11-regulator",
64 	}, {
65 		.name = "s2mps14-rtc",
66 	}, {
67 		.name = "s2mps11-clk",
68 		.of_compatible = "samsung,s2mps11-clk",
69 	}
70 };
71 
72 static const struct mfd_cell s2mps13_devs[] = {
73 	{ .name = "s2mps13-regulator", },
74 	{ .name = "s2mps13-rtc", },
75 	{
76 		.name = "s2mps13-clk",
77 		.of_compatible = "samsung,s2mps13-clk",
78 	},
79 };
80 
81 static const struct mfd_cell s2mps14_devs[] = {
82 	{
83 		.name = "s2mps14-regulator",
84 	}, {
85 		.name = "s2mps14-rtc",
86 	}, {
87 		.name = "s2mps14-clk",
88 		.of_compatible = "samsung,s2mps14-clk",
89 	}
90 };
91 
92 static const struct mfd_cell s2mps15_devs[] = {
93 	{
94 		.name = "s2mps15-regulator",
95 	}, {
96 		.name = "s2mps15-rtc",
97 	}, {
98 		.name = "s2mps13-clk",
99 		.of_compatible = "samsung,s2mps13-clk",
100 	},
101 };
102 
103 static const struct mfd_cell s2mpa01_devs[] = {
104 	{
105 		.name = "s2mpa01-pmic",
106 	},
107 };
108 
109 static const struct mfd_cell s2mpu02_devs[] = {
110 	{
111 		.name = "s2mpu02-regulator",
112 	},
113 };
114 
115 #ifdef CONFIG_OF
116 static const struct of_device_id sec_dt_match[] = {
117 	{	.compatible = "samsung,s5m8767-pmic",
118 		.data = (void *)S5M8767X,
119 	}, {
120 		.compatible = "samsung,s2mps11-pmic",
121 		.data = (void *)S2MPS11X,
122 	}, {
123 		.compatible = "samsung,s2mps13-pmic",
124 		.data = (void *)S2MPS13X,
125 	}, {
126 		.compatible = "samsung,s2mps14-pmic",
127 		.data = (void *)S2MPS14X,
128 	}, {
129 		.compatible = "samsung,s2mps15-pmic",
130 		.data = (void *)S2MPS15X,
131 	}, {
132 		.compatible = "samsung,s2mpa01-pmic",
133 		.data = (void *)S2MPA01,
134 	}, {
135 		.compatible = "samsung,s2mpu02-pmic",
136 		.data = (void *)S2MPU02,
137 	}, {
138 		/* Sentinel */
139 	},
140 };
141 MODULE_DEVICE_TABLE(of, sec_dt_match);
142 #endif
143 
144 static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
145 {
146 	switch (reg) {
147 	case S2MPA01_REG_INT1M:
148 	case S2MPA01_REG_INT2M:
149 	case S2MPA01_REG_INT3M:
150 		return false;
151 	default:
152 		return true;
153 	}
154 }
155 
156 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
157 {
158 	switch (reg) {
159 	case S2MPS11_REG_INT1M:
160 	case S2MPS11_REG_INT2M:
161 	case S2MPS11_REG_INT3M:
162 		return false;
163 	default:
164 		return true;
165 	}
166 }
167 
168 static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
169 {
170 	switch (reg) {
171 	case S2MPU02_REG_INT1M:
172 	case S2MPU02_REG_INT2M:
173 	case S2MPU02_REG_INT3M:
174 		return false;
175 	default:
176 		return true;
177 	}
178 }
179 
180 static bool s5m8763_volatile(struct device *dev, unsigned int reg)
181 {
182 	switch (reg) {
183 	case S5M8763_REG_IRQM1:
184 	case S5M8763_REG_IRQM2:
185 	case S5M8763_REG_IRQM3:
186 	case S5M8763_REG_IRQM4:
187 		return false;
188 	default:
189 		return true;
190 	}
191 }
192 
193 static const struct regmap_config sec_regmap_config = {
194 	.reg_bits = 8,
195 	.val_bits = 8,
196 };
197 
198 static const struct regmap_config s2mpa01_regmap_config = {
199 	.reg_bits = 8,
200 	.val_bits = 8,
201 
202 	.max_register = S2MPA01_REG_LDO_OVCB4,
203 	.volatile_reg = s2mpa01_volatile,
204 	.cache_type = REGCACHE_FLAT,
205 };
206 
207 static const struct regmap_config s2mps11_regmap_config = {
208 	.reg_bits = 8,
209 	.val_bits = 8,
210 
211 	.max_register = S2MPS11_REG_L38CTRL,
212 	.volatile_reg = s2mps11_volatile,
213 	.cache_type = REGCACHE_FLAT,
214 };
215 
216 static const struct regmap_config s2mps13_regmap_config = {
217 	.reg_bits = 8,
218 	.val_bits = 8,
219 
220 	.max_register = S2MPS13_REG_LDODSCH5,
221 	.volatile_reg = s2mps11_volatile,
222 	.cache_type = REGCACHE_FLAT,
223 };
224 
225 static const struct regmap_config s2mps14_regmap_config = {
226 	.reg_bits = 8,
227 	.val_bits = 8,
228 
229 	.max_register = S2MPS14_REG_LDODSCH3,
230 	.volatile_reg = s2mps11_volatile,
231 	.cache_type = REGCACHE_FLAT,
232 };
233 
234 static const struct regmap_config s2mps15_regmap_config = {
235 	.reg_bits = 8,
236 	.val_bits = 8,
237 
238 	.max_register = S2MPS15_REG_LDODSCH4,
239 	.volatile_reg = s2mps11_volatile,
240 	.cache_type = REGCACHE_FLAT,
241 };
242 
243 static const struct regmap_config s2mpu02_regmap_config = {
244 	.reg_bits = 8,
245 	.val_bits = 8,
246 
247 	.max_register = S2MPU02_REG_DVSDATA,
248 	.volatile_reg = s2mpu02_volatile,
249 	.cache_type = REGCACHE_FLAT,
250 };
251 
252 static const struct regmap_config s5m8763_regmap_config = {
253 	.reg_bits = 8,
254 	.val_bits = 8,
255 
256 	.max_register = S5M8763_REG_LBCNFG2,
257 	.volatile_reg = s5m8763_volatile,
258 	.cache_type = REGCACHE_FLAT,
259 };
260 
261 static const struct regmap_config s5m8767_regmap_config = {
262 	.reg_bits = 8,
263 	.val_bits = 8,
264 
265 	.max_register = S5M8767_REG_LDO28CTRL,
266 	.volatile_reg = s2mps11_volatile,
267 	.cache_type = REGCACHE_FLAT,
268 };
269 
270 static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic)
271 {
272 	unsigned int val;
273 
274 	/* For each device type, the REG_ID is always the first register */
275 	if (!regmap_read(sec_pmic->regmap_pmic, S2MPS11_REG_ID, &val))
276 		dev_dbg(sec_pmic->dev, "Revision: 0x%x\n", val);
277 }
278 
279 static void sec_pmic_configure(struct sec_pmic_dev *sec_pmic)
280 {
281 	int err;
282 
283 	if (sec_pmic->device_type != S2MPS13X)
284 		return;
285 
286 	if (sec_pmic->pdata->disable_wrstbi) {
287 		/*
288 		 * If WRSTBI pin is pulled down this feature must be disabled
289 		 * because each Suspend to RAM will trigger buck voltage reset
290 		 * to default values.
291 		 */
292 		err = regmap_update_bits(sec_pmic->regmap_pmic,
293 					 S2MPS13_REG_WRSTBI,
294 					 S2MPS13_REG_WRSTBI_MASK, 0x0);
295 		if (err)
296 			dev_warn(sec_pmic->dev,
297 				 "Cannot initialize WRSTBI config: %d\n",
298 				 err);
299 	}
300 }
301 
302 #ifdef CONFIG_OF
303 /*
304  * Only the common platform data elements for s5m8767 are parsed here from the
305  * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
306  * others have to parse their own platform data elements from device tree.
307  *
308  * The s5m8767 platform data structure is instantiated here and the drivers for
309  * the sub-modules need not instantiate another instance while parsing their
310  * platform data.
311  */
312 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
313 					struct device *dev)
314 {
315 	struct sec_platform_data *pd;
316 
317 	pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
318 	if (!pd)
319 		return ERR_PTR(-ENOMEM);
320 
321 	/*
322 	 * ToDo: the 'wakeup' member in the platform data is more of a linux
323 	 * specfic information. Hence, there is no binding for that yet and
324 	 * not parsed here.
325 	 */
326 
327 	pd->manual_poweroff = of_property_read_bool(dev->of_node,
328 						"samsung,s2mps11-acokb-ground");
329 	pd->disable_wrstbi = of_property_read_bool(dev->of_node,
330 						"samsung,s2mps11-wrstbi-ground");
331 	return pd;
332 }
333 #else
334 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
335 					struct device *dev)
336 {
337 	return NULL;
338 }
339 #endif
340 
341 static inline unsigned long sec_i2c_get_driver_data(struct i2c_client *i2c,
342 						const struct i2c_device_id *id)
343 {
344 #ifdef CONFIG_OF
345 	if (i2c->dev.of_node) {
346 		const struct of_device_id *match;
347 
348 		match = of_match_node(sec_dt_match, i2c->dev.of_node);
349 		return (unsigned long)match->data;
350 	}
351 #endif
352 	return id->driver_data;
353 }
354 
355 static int sec_pmic_probe(struct i2c_client *i2c,
356 			    const struct i2c_device_id *id)
357 {
358 	struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
359 	const struct regmap_config *regmap;
360 	const struct mfd_cell *sec_devs;
361 	struct sec_pmic_dev *sec_pmic;
362 	unsigned long device_type;
363 	int ret, num_sec_devs;
364 
365 	sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
366 				GFP_KERNEL);
367 	if (sec_pmic == NULL)
368 		return -ENOMEM;
369 
370 	i2c_set_clientdata(i2c, sec_pmic);
371 	sec_pmic->dev = &i2c->dev;
372 	sec_pmic->i2c = i2c;
373 	sec_pmic->irq = i2c->irq;
374 	device_type = sec_i2c_get_driver_data(i2c, id);
375 
376 	if (sec_pmic->dev->of_node) {
377 		pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
378 		if (IS_ERR(pdata)) {
379 			ret = PTR_ERR(pdata);
380 			return ret;
381 		}
382 		pdata->device_type = device_type;
383 	}
384 	if (pdata) {
385 		sec_pmic->device_type = pdata->device_type;
386 		sec_pmic->irq_base = pdata->irq_base;
387 		sec_pmic->wakeup = pdata->wakeup;
388 		sec_pmic->pdata = pdata;
389 	}
390 
391 	switch (sec_pmic->device_type) {
392 	case S2MPA01:
393 		regmap = &s2mpa01_regmap_config;
394 		break;
395 	case S2MPS11X:
396 		regmap = &s2mps11_regmap_config;
397 		break;
398 	case S2MPS13X:
399 		regmap = &s2mps13_regmap_config;
400 		break;
401 	case S2MPS14X:
402 		regmap = &s2mps14_regmap_config;
403 		break;
404 	case S2MPS15X:
405 		regmap = &s2mps15_regmap_config;
406 		break;
407 	case S5M8763X:
408 		regmap = &s5m8763_regmap_config;
409 		break;
410 	case S5M8767X:
411 		regmap = &s5m8767_regmap_config;
412 		break;
413 	case S2MPU02:
414 		regmap = &s2mpu02_regmap_config;
415 		break;
416 	default:
417 		regmap = &sec_regmap_config;
418 		break;
419 	}
420 
421 	sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
422 	if (IS_ERR(sec_pmic->regmap_pmic)) {
423 		ret = PTR_ERR(sec_pmic->regmap_pmic);
424 		dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
425 			ret);
426 		return ret;
427 	}
428 
429 	if (pdata && pdata->cfg_pmic_irq)
430 		pdata->cfg_pmic_irq();
431 
432 	sec_irq_init(sec_pmic);
433 
434 	pm_runtime_set_active(sec_pmic->dev);
435 
436 	switch (sec_pmic->device_type) {
437 	case S5M8751X:
438 		sec_devs = s5m8751_devs;
439 		num_sec_devs = ARRAY_SIZE(s5m8751_devs);
440 		break;
441 	case S5M8763X:
442 		sec_devs = s5m8763_devs;
443 		num_sec_devs = ARRAY_SIZE(s5m8763_devs);
444 		break;
445 	case S5M8767X:
446 		sec_devs = s5m8767_devs;
447 		num_sec_devs = ARRAY_SIZE(s5m8767_devs);
448 		break;
449 	case S2MPA01:
450 		sec_devs = s2mpa01_devs;
451 		num_sec_devs = ARRAY_SIZE(s2mpa01_devs);
452 		break;
453 	case S2MPS11X:
454 		sec_devs = s2mps11_devs;
455 		num_sec_devs = ARRAY_SIZE(s2mps11_devs);
456 		break;
457 	case S2MPS13X:
458 		sec_devs = s2mps13_devs;
459 		num_sec_devs = ARRAY_SIZE(s2mps13_devs);
460 		break;
461 	case S2MPS14X:
462 		sec_devs = s2mps14_devs;
463 		num_sec_devs = ARRAY_SIZE(s2mps14_devs);
464 		break;
465 	case S2MPS15X:
466 		sec_devs = s2mps15_devs;
467 		num_sec_devs = ARRAY_SIZE(s2mps15_devs);
468 		break;
469 	case S2MPU02:
470 		sec_devs = s2mpu02_devs;
471 		num_sec_devs = ARRAY_SIZE(s2mpu02_devs);
472 		break;
473 	default:
474 		/* If this happens the probe function is problem */
475 		BUG();
476 	}
477 	ret = devm_mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs,
478 				   NULL, 0, NULL);
479 	if (ret)
480 		return ret;
481 
482 	device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
483 	sec_pmic_configure(sec_pmic);
484 	sec_pmic_dump_rev(sec_pmic);
485 
486 	return ret;
487 }
488 
489 static void sec_pmic_shutdown(struct i2c_client *i2c)
490 {
491 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
492 	unsigned int reg, mask;
493 
494 	if (!sec_pmic->pdata->manual_poweroff)
495 		return;
496 
497 	switch (sec_pmic->device_type) {
498 	case S2MPS11X:
499 		reg = S2MPS11_REG_CTRL1;
500 		mask = S2MPS11_CTRL1_PWRHOLD_MASK;
501 		break;
502 	default:
503 		/*
504 		 * Currently only one board with S2MPS11 needs this, so just
505 		 * ignore the rest.
506 		 */
507 		dev_warn(sec_pmic->dev,
508 			"Unsupported device %lu for manual power off\n",
509 			sec_pmic->device_type);
510 		return;
511 	}
512 
513 	regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0);
514 }
515 
516 #ifdef CONFIG_PM_SLEEP
517 static int sec_pmic_suspend(struct device *dev)
518 {
519 	struct i2c_client *i2c = to_i2c_client(dev);
520 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
521 
522 	if (device_may_wakeup(dev))
523 		enable_irq_wake(sec_pmic->irq);
524 	/*
525 	 * PMIC IRQ must be disabled during suspend for RTC alarm
526 	 * to work properly.
527 	 * When device is woken up from suspend, an
528 	 * interrupt occurs before resuming I2C bus controller.
529 	 * The interrupt is handled by regmap_irq_thread which tries
530 	 * to read RTC registers. This read fails (I2C is still
531 	 * suspended) and RTC Alarm interrupt is disabled.
532 	 */
533 	disable_irq(sec_pmic->irq);
534 
535 	return 0;
536 }
537 
538 static int sec_pmic_resume(struct device *dev)
539 {
540 	struct i2c_client *i2c = to_i2c_client(dev);
541 	struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
542 
543 	if (device_may_wakeup(dev))
544 		disable_irq_wake(sec_pmic->irq);
545 	enable_irq(sec_pmic->irq);
546 
547 	return 0;
548 }
549 #endif /* CONFIG_PM_SLEEP */
550 
551 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
552 
553 static const struct i2c_device_id sec_pmic_id[] = {
554 	{ "sec_pmic", 0 },
555 	{ }
556 };
557 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
558 
559 static struct i2c_driver sec_pmic_driver = {
560 	.driver = {
561 		   .name = "sec_pmic",
562 		   .pm = &sec_pmic_pm_ops,
563 		   .of_match_table = of_match_ptr(sec_dt_match),
564 	},
565 	.probe = sec_pmic_probe,
566 	.shutdown = sec_pmic_shutdown,
567 	.id_table = sec_pmic_id,
568 };
569 
570 static int __init sec_pmic_init(void)
571 {
572 	return i2c_add_driver(&sec_pmic_driver);
573 }
574 
575 subsys_initcall(sec_pmic_init);
576 
577 static void __exit sec_pmic_exit(void)
578 {
579 	i2c_del_driver(&sec_pmic_driver);
580 }
581 module_exit(sec_pmic_exit);
582 
583 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
584 MODULE_DESCRIPTION("Core support for the S5M MFD");
585 MODULE_LICENSE("GPL");
586