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/interrupt.h>
14 #include <linux/pm_runtime.h>
15 #include <linux/mutex.h>
16 #include <linux/mfd/core.h>
17 #include <linux/mfd/samsung/core.h>
18 #include <linux/mfd/samsung/irq.h>
19 #include <linux/mfd/samsung/s2mpa01.h>
20 #include <linux/mfd/samsung/s2mps11.h>
21 #include <linux/mfd/samsung/s2mps13.h>
22 #include <linux/mfd/samsung/s2mps14.h>
23 #include <linux/mfd/samsung/s2mps15.h>
24 #include <linux/mfd/samsung/s2mpu02.h>
25 #include <linux/mfd/samsung/s5m8767.h>
26 #include <linux/regmap.h>
28 static const struct mfd_cell s5m8767_devs[] = {
29 { .name = "s5m8767-pmic", },
30 { .name = "s5m-rtc", },
32 .name = "s5m8767-clk",
33 .of_compatible = "samsung,s5m8767-clk",
37 static const struct mfd_cell s2mps11_devs[] = {
38 { .name = "s2mps11-regulator", },
39 { .name = "s2mps14-rtc", },
41 .name = "s2mps11-clk",
42 .of_compatible = "samsung,s2mps11-clk",
46 static const struct mfd_cell s2mps13_devs[] = {
47 { .name = "s2mps13-regulator", },
48 { .name = "s2mps13-rtc", },
50 .name = "s2mps13-clk",
51 .of_compatible = "samsung,s2mps13-clk",
55 static const struct mfd_cell s2mps14_devs[] = {
56 { .name = "s2mps14-regulator", },
57 { .name = "s2mps14-rtc", },
59 .name = "s2mps14-clk",
60 .of_compatible = "samsung,s2mps14-clk",
64 static const struct mfd_cell s2mps15_devs[] = {
65 { .name = "s2mps15-regulator", },
66 { .name = "s2mps15-rtc", },
68 .name = "s2mps13-clk",
69 .of_compatible = "samsung,s2mps13-clk",
73 static const struct mfd_cell s2mpa01_devs[] = {
74 { .name = "s2mpa01-pmic", },
75 { .name = "s2mps14-rtc", },
78 static const struct mfd_cell s2mpu02_devs[] = {
79 { .name = "s2mpu02-regulator", },
82 static const struct of_device_id sec_dt_match[] = {
84 .compatible = "samsung,s5m8767-pmic",
85 .data = (void *)S5M8767X,
87 .compatible = "samsung,s2mps11-pmic",
88 .data = (void *)S2MPS11X,
90 .compatible = "samsung,s2mps13-pmic",
91 .data = (void *)S2MPS13X,
93 .compatible = "samsung,s2mps14-pmic",
94 .data = (void *)S2MPS14X,
96 .compatible = "samsung,s2mps15-pmic",
97 .data = (void *)S2MPS15X,
99 .compatible = "samsung,s2mpa01-pmic",
100 .data = (void *)S2MPA01,
102 .compatible = "samsung,s2mpu02-pmic",
103 .data = (void *)S2MPU02,
108 MODULE_DEVICE_TABLE(of, sec_dt_match);
110 static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
113 case S2MPA01_REG_INT1M:
114 case S2MPA01_REG_INT2M:
115 case S2MPA01_REG_INT3M:
122 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
125 case S2MPS11_REG_INT1M:
126 case S2MPS11_REG_INT2M:
127 case S2MPS11_REG_INT3M:
134 static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
137 case S2MPU02_REG_INT1M:
138 case S2MPU02_REG_INT2M:
139 case S2MPU02_REG_INT3M:
146 static const struct regmap_config sec_regmap_config = {
151 static const struct regmap_config s2mpa01_regmap_config = {
155 .max_register = S2MPA01_REG_LDO_OVCB4,
156 .volatile_reg = s2mpa01_volatile,
157 .cache_type = REGCACHE_FLAT,
160 static const struct regmap_config s2mps11_regmap_config = {
164 .max_register = S2MPS11_REG_L38CTRL,
165 .volatile_reg = s2mps11_volatile,
166 .cache_type = REGCACHE_FLAT,
169 static const struct regmap_config s2mps13_regmap_config = {
173 .max_register = S2MPS13_REG_LDODSCH5,
174 .volatile_reg = s2mps11_volatile,
175 .cache_type = REGCACHE_FLAT,
178 static const struct regmap_config s2mps14_regmap_config = {
182 .max_register = S2MPS14_REG_LDODSCH3,
183 .volatile_reg = s2mps11_volatile,
184 .cache_type = REGCACHE_FLAT,
187 static const struct regmap_config s2mps15_regmap_config = {
191 .max_register = S2MPS15_REG_LDODSCH4,
192 .volatile_reg = s2mps11_volatile,
193 .cache_type = REGCACHE_FLAT,
196 static const struct regmap_config s2mpu02_regmap_config = {
200 .max_register = S2MPU02_REG_DVSDATA,
201 .volatile_reg = s2mpu02_volatile,
202 .cache_type = REGCACHE_FLAT,
205 static const struct regmap_config s5m8767_regmap_config = {
209 .max_register = S5M8767_REG_LDO28CTRL,
210 .volatile_reg = s2mps11_volatile,
211 .cache_type = REGCACHE_FLAT,
214 static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic)
218 /* For each device type, the REG_ID is always the first register */
219 if (!regmap_read(sec_pmic->regmap_pmic, S2MPS11_REG_ID, &val))
220 dev_dbg(sec_pmic->dev, "Revision: 0x%x\n", val);
223 static void sec_pmic_configure(struct sec_pmic_dev *sec_pmic)
227 if (sec_pmic->device_type != S2MPS13X)
230 if (sec_pmic->pdata->disable_wrstbi) {
232 * If WRSTBI pin is pulled down this feature must be disabled
233 * because each Suspend to RAM will trigger buck voltage reset
236 err = regmap_update_bits(sec_pmic->regmap_pmic,
238 S2MPS13_REG_WRSTBI_MASK, 0x0);
240 dev_warn(sec_pmic->dev,
241 "Cannot initialize WRSTBI config: %d\n",
247 * Only the common platform data elements for s5m8767 are parsed here from the
248 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
249 * others have to parse their own platform data elements from device tree.
251 * The s5m8767 platform data structure is instantiated here and the drivers for
252 * the sub-modules need not instantiate another instance while parsing their
255 static struct sec_platform_data *
256 sec_pmic_i2c_parse_dt_pdata(struct device *dev)
258 struct sec_platform_data *pd;
260 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
262 return ERR_PTR(-ENOMEM);
264 pd->manual_poweroff = of_property_read_bool(dev->of_node,
265 "samsung,s2mps11-acokb-ground");
266 pd->disable_wrstbi = of_property_read_bool(dev->of_node,
267 "samsung,s2mps11-wrstbi-ground");
271 static int sec_pmic_probe(struct i2c_client *i2c)
273 const struct regmap_config *regmap;
274 struct sec_platform_data *pdata;
275 const struct mfd_cell *sec_devs;
276 struct sec_pmic_dev *sec_pmic;
277 int ret, num_sec_devs;
279 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
281 if (sec_pmic == NULL)
284 i2c_set_clientdata(i2c, sec_pmic);
285 sec_pmic->dev = &i2c->dev;
287 sec_pmic->irq = i2c->irq;
289 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
291 ret = PTR_ERR(pdata);
295 sec_pmic->device_type = (unsigned long)of_device_get_match_data(sec_pmic->dev);
296 sec_pmic->pdata = pdata;
298 switch (sec_pmic->device_type) {
300 regmap = &s2mpa01_regmap_config;
303 regmap = &s2mps11_regmap_config;
306 regmap = &s2mps13_regmap_config;
309 regmap = &s2mps14_regmap_config;
312 regmap = &s2mps15_regmap_config;
315 regmap = &s5m8767_regmap_config;
318 regmap = &s2mpu02_regmap_config;
321 regmap = &sec_regmap_config;
325 sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
326 if (IS_ERR(sec_pmic->regmap_pmic)) {
327 ret = PTR_ERR(sec_pmic->regmap_pmic);
328 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
333 sec_irq_init(sec_pmic);
335 pm_runtime_set_active(sec_pmic->dev);
337 switch (sec_pmic->device_type) {
339 sec_devs = s5m8767_devs;
340 num_sec_devs = ARRAY_SIZE(s5m8767_devs);
343 sec_devs = s2mpa01_devs;
344 num_sec_devs = ARRAY_SIZE(s2mpa01_devs);
347 sec_devs = s2mps11_devs;
348 num_sec_devs = ARRAY_SIZE(s2mps11_devs);
351 sec_devs = s2mps13_devs;
352 num_sec_devs = ARRAY_SIZE(s2mps13_devs);
355 sec_devs = s2mps14_devs;
356 num_sec_devs = ARRAY_SIZE(s2mps14_devs);
359 sec_devs = s2mps15_devs;
360 num_sec_devs = ARRAY_SIZE(s2mps15_devs);
363 sec_devs = s2mpu02_devs;
364 num_sec_devs = ARRAY_SIZE(s2mpu02_devs);
367 dev_err(&i2c->dev, "Unsupported device type (%lu)\n",
368 sec_pmic->device_type);
371 ret = devm_mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs,
376 sec_pmic_configure(sec_pmic);
377 sec_pmic_dump_rev(sec_pmic);
382 static void sec_pmic_shutdown(struct i2c_client *i2c)
384 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
385 unsigned int reg, mask;
387 if (!sec_pmic->pdata->manual_poweroff)
390 switch (sec_pmic->device_type) {
392 reg = S2MPS11_REG_CTRL1;
393 mask = S2MPS11_CTRL1_PWRHOLD_MASK;
397 * Currently only one board with S2MPS11 needs this, so just
400 dev_warn(sec_pmic->dev,
401 "Unsupported device %lu for manual power off\n",
402 sec_pmic->device_type);
406 regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0);
409 static int sec_pmic_suspend(struct device *dev)
411 struct i2c_client *i2c = to_i2c_client(dev);
412 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
414 if (device_may_wakeup(dev))
415 enable_irq_wake(sec_pmic->irq);
417 * PMIC IRQ must be disabled during suspend for RTC alarm
419 * When device is woken up from suspend, an
420 * interrupt occurs before resuming I2C bus controller.
421 * The interrupt is handled by regmap_irq_thread which tries
422 * to read RTC registers. This read fails (I2C is still
423 * suspended) and RTC Alarm interrupt is disabled.
425 disable_irq(sec_pmic->irq);
430 static int sec_pmic_resume(struct device *dev)
432 struct i2c_client *i2c = to_i2c_client(dev);
433 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
435 if (device_may_wakeup(dev))
436 disable_irq_wake(sec_pmic->irq);
437 enable_irq(sec_pmic->irq);
442 static DEFINE_SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops,
443 sec_pmic_suspend, sec_pmic_resume);
445 static struct i2c_driver sec_pmic_driver = {
448 .pm = pm_sleep_ptr(&sec_pmic_pm_ops),
449 .of_match_table = sec_dt_match,
451 .probe = sec_pmic_probe,
452 .shutdown = sec_pmic_shutdown,
454 module_i2c_driver(sec_pmic_driver);
457 MODULE_DESCRIPTION("Core support for the S5M MFD");
458 MODULE_LICENSE("GPL");