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