1 // SPDX-License-Identifier: GPL-2.0+
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd
4 // http://www.samsung.com
6 #include <linux/module.h>
7 #include <linux/moduleparam.h>
8 #include <linux/init.h>
10 #include <linux/slab.h>
11 #include <linux/i2c.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>
30 static const struct mfd_cell s5m8751_devs[] = {
31 { .name = "s5m8751-pmic", },
32 { .name = "s5m-charger", },
33 { .name = "s5m8751-codec", },
36 static const struct mfd_cell s5m8763_devs[] = {
37 { .name = "s5m8763-pmic", },
38 { .name = "s5m-rtc", },
39 { .name = "s5m-charger", },
42 static const struct mfd_cell s5m8767_devs[] = {
43 { .name = "s5m8767-pmic", },
44 { .name = "s5m-rtc", },
46 .name = "s5m8767-clk",
47 .of_compatible = "samsung,s5m8767-clk",
51 static const struct mfd_cell s2mps11_devs[] = {
52 { .name = "s2mps11-regulator", },
53 { .name = "s2mps14-rtc", },
55 .name = "s2mps11-clk",
56 .of_compatible = "samsung,s2mps11-clk",
60 static const struct mfd_cell s2mps13_devs[] = {
61 { .name = "s2mps13-regulator", },
62 { .name = "s2mps13-rtc", },
64 .name = "s2mps13-clk",
65 .of_compatible = "samsung,s2mps13-clk",
69 static const struct mfd_cell s2mps14_devs[] = {
70 { .name = "s2mps14-regulator", },
71 { .name = "s2mps14-rtc", },
73 .name = "s2mps14-clk",
74 .of_compatible = "samsung,s2mps14-clk",
78 static const struct mfd_cell s2mps15_devs[] = {
79 { .name = "s2mps15-regulator", },
80 { .name = "s2mps15-rtc", },
82 .name = "s2mps13-clk",
83 .of_compatible = "samsung,s2mps13-clk",
87 static const struct mfd_cell s2mpa01_devs[] = {
88 { .name = "s2mpa01-pmic", },
91 static const struct mfd_cell s2mpu02_devs[] = {
92 { .name = "s2mpu02-regulator", },
96 static const struct of_device_id sec_dt_match[] = {
98 .compatible = "samsung,s5m8767-pmic",
99 .data = (void *)S5M8767X,
101 .compatible = "samsung,s2mps11-pmic",
102 .data = (void *)S2MPS11X,
104 .compatible = "samsung,s2mps13-pmic",
105 .data = (void *)S2MPS13X,
107 .compatible = "samsung,s2mps14-pmic",
108 .data = (void *)S2MPS14X,
110 .compatible = "samsung,s2mps15-pmic",
111 .data = (void *)S2MPS15X,
113 .compatible = "samsung,s2mpa01-pmic",
114 .data = (void *)S2MPA01,
116 .compatible = "samsung,s2mpu02-pmic",
117 .data = (void *)S2MPU02,
122 MODULE_DEVICE_TABLE(of, sec_dt_match);
125 static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
128 case S2MPA01_REG_INT1M:
129 case S2MPA01_REG_INT2M:
130 case S2MPA01_REG_INT3M:
137 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
140 case S2MPS11_REG_INT1M:
141 case S2MPS11_REG_INT2M:
142 case S2MPS11_REG_INT3M:
149 static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
152 case S2MPU02_REG_INT1M:
153 case S2MPU02_REG_INT2M:
154 case S2MPU02_REG_INT3M:
161 static bool s5m8763_volatile(struct device *dev, unsigned int reg)
164 case S5M8763_REG_IRQM1:
165 case S5M8763_REG_IRQM2:
166 case S5M8763_REG_IRQM3:
167 case S5M8763_REG_IRQM4:
174 static const struct regmap_config sec_regmap_config = {
179 static const struct regmap_config s2mpa01_regmap_config = {
183 .max_register = S2MPA01_REG_LDO_OVCB4,
184 .volatile_reg = s2mpa01_volatile,
185 .cache_type = REGCACHE_FLAT,
188 static const struct regmap_config s2mps11_regmap_config = {
192 .max_register = S2MPS11_REG_L38CTRL,
193 .volatile_reg = s2mps11_volatile,
194 .cache_type = REGCACHE_FLAT,
197 static const struct regmap_config s2mps13_regmap_config = {
201 .max_register = S2MPS13_REG_LDODSCH5,
202 .volatile_reg = s2mps11_volatile,
203 .cache_type = REGCACHE_FLAT,
206 static const struct regmap_config s2mps14_regmap_config = {
210 .max_register = S2MPS14_REG_LDODSCH3,
211 .volatile_reg = s2mps11_volatile,
212 .cache_type = REGCACHE_FLAT,
215 static const struct regmap_config s2mps15_regmap_config = {
219 .max_register = S2MPS15_REG_LDODSCH4,
220 .volatile_reg = s2mps11_volatile,
221 .cache_type = REGCACHE_FLAT,
224 static const struct regmap_config s2mpu02_regmap_config = {
228 .max_register = S2MPU02_REG_DVSDATA,
229 .volatile_reg = s2mpu02_volatile,
230 .cache_type = REGCACHE_FLAT,
233 static const struct regmap_config s5m8763_regmap_config = {
237 .max_register = S5M8763_REG_LBCNFG2,
238 .volatile_reg = s5m8763_volatile,
239 .cache_type = REGCACHE_FLAT,
242 static const struct regmap_config s5m8767_regmap_config = {
246 .max_register = S5M8767_REG_LDO28CTRL,
247 .volatile_reg = s2mps11_volatile,
248 .cache_type = REGCACHE_FLAT,
251 static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic)
255 /* For each device type, the REG_ID is always the first register */
256 if (!regmap_read(sec_pmic->regmap_pmic, S2MPS11_REG_ID, &val))
257 dev_dbg(sec_pmic->dev, "Revision: 0x%x\n", val);
260 static void sec_pmic_configure(struct sec_pmic_dev *sec_pmic)
264 if (sec_pmic->device_type != S2MPS13X)
267 if (sec_pmic->pdata->disable_wrstbi) {
269 * If WRSTBI pin is pulled down this feature must be disabled
270 * because each Suspend to RAM will trigger buck voltage reset
273 err = regmap_update_bits(sec_pmic->regmap_pmic,
275 S2MPS13_REG_WRSTBI_MASK, 0x0);
277 dev_warn(sec_pmic->dev,
278 "Cannot initialize WRSTBI config: %d\n",
285 * Only the common platform data elements for s5m8767 are parsed here from the
286 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
287 * others have to parse their own platform data elements from device tree.
289 * The s5m8767 platform data structure is instantiated here and the drivers for
290 * the sub-modules need not instantiate another instance while parsing their
293 static struct sec_platform_data *
294 sec_pmic_i2c_parse_dt_pdata(struct device *dev)
296 struct sec_platform_data *pd;
298 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
300 return ERR_PTR(-ENOMEM);
303 * ToDo: the 'wakeup' member in the platform data is more of a linux
304 * specfic information. Hence, there is no binding for that yet and
308 pd->manual_poweroff = of_property_read_bool(dev->of_node,
309 "samsung,s2mps11-acokb-ground");
310 pd->disable_wrstbi = of_property_read_bool(dev->of_node,
311 "samsung,s2mps11-wrstbi-ground");
315 static struct sec_platform_data *
316 sec_pmic_i2c_parse_dt_pdata(struct device *dev)
322 static inline unsigned long sec_i2c_get_driver_data(struct i2c_client *i2c,
323 const struct i2c_device_id *id)
326 if (i2c->dev.of_node) {
327 const struct of_device_id *match;
329 match = of_match_node(sec_dt_match, i2c->dev.of_node);
330 return (unsigned long)match->data;
333 return id->driver_data;
336 static int sec_pmic_probe(struct i2c_client *i2c,
337 const struct i2c_device_id *id)
339 struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
340 const struct regmap_config *regmap;
341 const struct mfd_cell *sec_devs;
342 struct sec_pmic_dev *sec_pmic;
343 unsigned long device_type;
344 int ret, num_sec_devs;
346 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
348 if (sec_pmic == NULL)
351 i2c_set_clientdata(i2c, sec_pmic);
352 sec_pmic->dev = &i2c->dev;
354 sec_pmic->irq = i2c->irq;
355 device_type = sec_i2c_get_driver_data(i2c, id);
357 if (sec_pmic->dev->of_node) {
358 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
360 ret = PTR_ERR(pdata);
363 pdata->device_type = device_type;
366 sec_pmic->device_type = pdata->device_type;
367 sec_pmic->irq_base = pdata->irq_base;
368 sec_pmic->wakeup = pdata->wakeup;
369 sec_pmic->pdata = pdata;
372 switch (sec_pmic->device_type) {
374 regmap = &s2mpa01_regmap_config;
377 regmap = &s2mps11_regmap_config;
380 regmap = &s2mps13_regmap_config;
383 regmap = &s2mps14_regmap_config;
386 regmap = &s2mps15_regmap_config;
389 regmap = &s5m8763_regmap_config;
392 regmap = &s5m8767_regmap_config;
395 regmap = &s2mpu02_regmap_config;
398 regmap = &sec_regmap_config;
402 sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
403 if (IS_ERR(sec_pmic->regmap_pmic)) {
404 ret = PTR_ERR(sec_pmic->regmap_pmic);
405 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
410 if (pdata && pdata->cfg_pmic_irq)
411 pdata->cfg_pmic_irq();
413 sec_irq_init(sec_pmic);
415 pm_runtime_set_active(sec_pmic->dev);
417 switch (sec_pmic->device_type) {
419 sec_devs = s5m8751_devs;
420 num_sec_devs = ARRAY_SIZE(s5m8751_devs);
423 sec_devs = s5m8763_devs;
424 num_sec_devs = ARRAY_SIZE(s5m8763_devs);
427 sec_devs = s5m8767_devs;
428 num_sec_devs = ARRAY_SIZE(s5m8767_devs);
431 sec_devs = s2mpa01_devs;
432 num_sec_devs = ARRAY_SIZE(s2mpa01_devs);
435 sec_devs = s2mps11_devs;
436 num_sec_devs = ARRAY_SIZE(s2mps11_devs);
439 sec_devs = s2mps13_devs;
440 num_sec_devs = ARRAY_SIZE(s2mps13_devs);
443 sec_devs = s2mps14_devs;
444 num_sec_devs = ARRAY_SIZE(s2mps14_devs);
447 sec_devs = s2mps15_devs;
448 num_sec_devs = ARRAY_SIZE(s2mps15_devs);
451 sec_devs = s2mpu02_devs;
452 num_sec_devs = ARRAY_SIZE(s2mpu02_devs);
455 dev_err(&i2c->dev, "Unsupported device type (%lu)\n",
456 sec_pmic->device_type);
459 ret = devm_mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs,
464 device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
465 sec_pmic_configure(sec_pmic);
466 sec_pmic_dump_rev(sec_pmic);
471 static void sec_pmic_shutdown(struct i2c_client *i2c)
473 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
474 unsigned int reg, mask;
476 if (!sec_pmic->pdata->manual_poweroff)
479 switch (sec_pmic->device_type) {
481 reg = S2MPS11_REG_CTRL1;
482 mask = S2MPS11_CTRL1_PWRHOLD_MASK;
486 * Currently only one board with S2MPS11 needs this, so just
489 dev_warn(sec_pmic->dev,
490 "Unsupported device %lu for manual power off\n",
491 sec_pmic->device_type);
495 regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0);
498 #ifdef CONFIG_PM_SLEEP
499 static int sec_pmic_suspend(struct device *dev)
501 struct i2c_client *i2c = to_i2c_client(dev);
502 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
504 if (device_may_wakeup(dev))
505 enable_irq_wake(sec_pmic->irq);
507 * PMIC IRQ must be disabled during suspend for RTC alarm
509 * When device is woken up from suspend, an
510 * interrupt occurs before resuming I2C bus controller.
511 * The interrupt is handled by regmap_irq_thread which tries
512 * to read RTC registers. This read fails (I2C is still
513 * suspended) and RTC Alarm interrupt is disabled.
515 disable_irq(sec_pmic->irq);
520 static int sec_pmic_resume(struct device *dev)
522 struct i2c_client *i2c = to_i2c_client(dev);
523 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
525 if (device_may_wakeup(dev))
526 disable_irq_wake(sec_pmic->irq);
527 enable_irq(sec_pmic->irq);
531 #endif /* CONFIG_PM_SLEEP */
533 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
535 static const struct i2c_device_id sec_pmic_id[] = {
539 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
541 static struct i2c_driver sec_pmic_driver = {
544 .pm = &sec_pmic_pm_ops,
545 .of_match_table = of_match_ptr(sec_dt_match),
547 .probe = sec_pmic_probe,
548 .shutdown = sec_pmic_shutdown,
549 .id_table = sec_pmic_id,
552 static int __init sec_pmic_init(void)
554 return i2c_add_driver(&sec_pmic_driver);
557 subsys_initcall(sec_pmic_init);
559 static void __exit sec_pmic_exit(void)
561 i2c_del_driver(&sec_pmic_driver);
563 module_exit(sec_pmic_exit);
565 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
566 MODULE_DESCRIPTION("Core support for the S5M MFD");
567 MODULE_LICENSE("GPL");