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_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>
30 static const struct mfd_cell s5m8767_devs[] = {
31 { .name = "s5m8767-pmic", },
32 { .name = "s5m-rtc", },
34 .name = "s5m8767-clk",
35 .of_compatible = "samsung,s5m8767-clk",
39 static const struct mfd_cell s2mps11_devs[] = {
40 { .name = "s2mps11-regulator", },
41 { .name = "s2mps14-rtc", },
43 .name = "s2mps11-clk",
44 .of_compatible = "samsung,s2mps11-clk",
48 static const struct mfd_cell s2mps13_devs[] = {
49 { .name = "s2mps13-regulator", },
50 { .name = "s2mps13-rtc", },
52 .name = "s2mps13-clk",
53 .of_compatible = "samsung,s2mps13-clk",
57 static const struct mfd_cell s2mps14_devs[] = {
58 { .name = "s2mps14-regulator", },
59 { .name = "s2mps14-rtc", },
61 .name = "s2mps14-clk",
62 .of_compatible = "samsung,s2mps14-clk",
66 static const struct mfd_cell s2mps15_devs[] = {
67 { .name = "s2mps15-regulator", },
68 { .name = "s2mps15-rtc", },
70 .name = "s2mps13-clk",
71 .of_compatible = "samsung,s2mps13-clk",
75 static const struct mfd_cell s2mpa01_devs[] = {
76 { .name = "s2mpa01-pmic", },
77 { .name = "s2mps14-rtc", },
80 static const struct mfd_cell s2mpu02_devs[] = {
81 { .name = "s2mpu02-regulator", },
84 static const struct of_device_id sec_dt_match[] = {
86 .compatible = "samsung,s5m8767-pmic",
87 .data = (void *)S5M8767X,
89 .compatible = "samsung,s2mps11-pmic",
90 .data = (void *)S2MPS11X,
92 .compatible = "samsung,s2mps13-pmic",
93 .data = (void *)S2MPS13X,
95 .compatible = "samsung,s2mps14-pmic",
96 .data = (void *)S2MPS14X,
98 .compatible = "samsung,s2mps15-pmic",
99 .data = (void *)S2MPS15X,
101 .compatible = "samsung,s2mpa01-pmic",
102 .data = (void *)S2MPA01,
104 .compatible = "samsung,s2mpu02-pmic",
105 .data = (void *)S2MPU02,
110 MODULE_DEVICE_TABLE(of, sec_dt_match);
112 static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
115 case S2MPA01_REG_INT1M:
116 case S2MPA01_REG_INT2M:
117 case S2MPA01_REG_INT3M:
124 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
127 case S2MPS11_REG_INT1M:
128 case S2MPS11_REG_INT2M:
129 case S2MPS11_REG_INT3M:
136 static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
139 case S2MPU02_REG_INT1M:
140 case S2MPU02_REG_INT2M:
141 case S2MPU02_REG_INT3M:
148 static const struct regmap_config sec_regmap_config = {
153 static const struct regmap_config s2mpa01_regmap_config = {
157 .max_register = S2MPA01_REG_LDO_OVCB4,
158 .volatile_reg = s2mpa01_volatile,
159 .cache_type = REGCACHE_FLAT,
162 static const struct regmap_config s2mps11_regmap_config = {
166 .max_register = S2MPS11_REG_L38CTRL,
167 .volatile_reg = s2mps11_volatile,
168 .cache_type = REGCACHE_FLAT,
171 static const struct regmap_config s2mps13_regmap_config = {
175 .max_register = S2MPS13_REG_LDODSCH5,
176 .volatile_reg = s2mps11_volatile,
177 .cache_type = REGCACHE_FLAT,
180 static const struct regmap_config s2mps14_regmap_config = {
184 .max_register = S2MPS14_REG_LDODSCH3,
185 .volatile_reg = s2mps11_volatile,
186 .cache_type = REGCACHE_FLAT,
189 static const struct regmap_config s2mps15_regmap_config = {
193 .max_register = S2MPS15_REG_LDODSCH4,
194 .volatile_reg = s2mps11_volatile,
195 .cache_type = REGCACHE_FLAT,
198 static const struct regmap_config s2mpu02_regmap_config = {
202 .max_register = S2MPU02_REG_DVSDATA,
203 .volatile_reg = s2mpu02_volatile,
204 .cache_type = REGCACHE_FLAT,
207 static const struct regmap_config s5m8767_regmap_config = {
211 .max_register = S5M8767_REG_LDO28CTRL,
212 .volatile_reg = s2mps11_volatile,
213 .cache_type = REGCACHE_FLAT,
216 static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic)
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);
225 static void sec_pmic_configure(struct sec_pmic_dev *sec_pmic)
229 if (sec_pmic->device_type != S2MPS13X)
232 if (sec_pmic->pdata->disable_wrstbi) {
234 * If WRSTBI pin is pulled down this feature must be disabled
235 * because each Suspend to RAM will trigger buck voltage reset
238 err = regmap_update_bits(sec_pmic->regmap_pmic,
240 S2MPS13_REG_WRSTBI_MASK, 0x0);
242 dev_warn(sec_pmic->dev,
243 "Cannot initialize WRSTBI config: %d\n",
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.
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
257 static struct sec_platform_data *
258 sec_pmic_i2c_parse_dt_pdata(struct device *dev)
260 struct sec_platform_data *pd;
262 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
264 return ERR_PTR(-ENOMEM);
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");
273 static int sec_pmic_probe(struct i2c_client *i2c)
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;
281 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
283 if (sec_pmic == NULL)
286 i2c_set_clientdata(i2c, sec_pmic);
287 sec_pmic->dev = &i2c->dev;
289 sec_pmic->irq = i2c->irq;
291 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
293 ret = PTR_ERR(pdata);
297 sec_pmic->device_type = (unsigned long)of_device_get_match_data(sec_pmic->dev);
298 sec_pmic->pdata = pdata;
300 switch (sec_pmic->device_type) {
302 regmap = &s2mpa01_regmap_config;
305 regmap = &s2mps11_regmap_config;
308 regmap = &s2mps13_regmap_config;
311 regmap = &s2mps14_regmap_config;
314 regmap = &s2mps15_regmap_config;
317 regmap = &s5m8767_regmap_config;
320 regmap = &s2mpu02_regmap_config;
323 regmap = &sec_regmap_config;
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",
335 sec_irq_init(sec_pmic);
337 pm_runtime_set_active(sec_pmic->dev);
339 switch (sec_pmic->device_type) {
341 sec_devs = s5m8767_devs;
342 num_sec_devs = ARRAY_SIZE(s5m8767_devs);
345 sec_devs = s2mpa01_devs;
346 num_sec_devs = ARRAY_SIZE(s2mpa01_devs);
349 sec_devs = s2mps11_devs;
350 num_sec_devs = ARRAY_SIZE(s2mps11_devs);
353 sec_devs = s2mps13_devs;
354 num_sec_devs = ARRAY_SIZE(s2mps13_devs);
357 sec_devs = s2mps14_devs;
358 num_sec_devs = ARRAY_SIZE(s2mps14_devs);
361 sec_devs = s2mps15_devs;
362 num_sec_devs = ARRAY_SIZE(s2mps15_devs);
365 sec_devs = s2mpu02_devs;
366 num_sec_devs = ARRAY_SIZE(s2mpu02_devs);
369 dev_err(&i2c->dev, "Unsupported device type (%lu)\n",
370 sec_pmic->device_type);
373 ret = devm_mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs,
378 sec_pmic_configure(sec_pmic);
379 sec_pmic_dump_rev(sec_pmic);
384 static void sec_pmic_shutdown(struct i2c_client *i2c)
386 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
387 unsigned int reg, mask;
389 if (!sec_pmic->pdata->manual_poweroff)
392 switch (sec_pmic->device_type) {
394 reg = S2MPS11_REG_CTRL1;
395 mask = S2MPS11_CTRL1_PWRHOLD_MASK;
399 * Currently only one board with S2MPS11 needs this, so just
402 dev_warn(sec_pmic->dev,
403 "Unsupported device %lu for manual power off\n",
404 sec_pmic->device_type);
408 regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0);
411 static int sec_pmic_suspend(struct device *dev)
413 struct i2c_client *i2c = to_i2c_client(dev);
414 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
416 if (device_may_wakeup(dev))
417 enable_irq_wake(sec_pmic->irq);
419 * PMIC IRQ must be disabled during suspend for RTC alarm
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.
427 disable_irq(sec_pmic->irq);
432 static int sec_pmic_resume(struct device *dev)
434 struct i2c_client *i2c = to_i2c_client(dev);
435 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
437 if (device_may_wakeup(dev))
438 disable_irq_wake(sec_pmic->irq);
439 enable_irq(sec_pmic->irq);
444 static DEFINE_SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops,
445 sec_pmic_suspend, sec_pmic_resume);
447 static struct i2c_driver sec_pmic_driver = {
450 .pm = pm_sleep_ptr(&sec_pmic_pm_ops),
451 .of_match_table = sec_dt_match,
453 .probe_new = sec_pmic_probe,
454 .shutdown = sec_pmic_shutdown,
456 module_i2c_driver(sec_pmic_driver);
459 MODULE_DESCRIPTION("Core support for the S5M MFD");
460 MODULE_LICENSE("GPL");