4 * Copyright (c) 2012 Samsung Electronics Co., Ltd
5 * http://www.samsung.com
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
21 #include <linux/of_irq.h>
22 #include <linux/interrupt.h>
23 #include <linux/pm_runtime.h>
24 #include <linux/mutex.h>
25 #include <linux/mfd/core.h>
26 #include <linux/mfd/samsung/core.h>
27 #include <linux/mfd/samsung/irq.h>
28 #include <linux/mfd/samsung/s2mpa01.h>
29 #include <linux/mfd/samsung/s2mps11.h>
30 #include <linux/mfd/samsung/s2mps13.h>
31 #include <linux/mfd/samsung/s2mps14.h>
32 #include <linux/mfd/samsung/s2mpu02.h>
33 #include <linux/mfd/samsung/s5m8763.h>
34 #include <linux/mfd/samsung/s5m8767.h>
35 #include <linux/regmap.h>
37 static const struct mfd_cell s5m8751_devs[] = {
39 .name = "s5m8751-pmic",
41 .name = "s5m-charger",
43 .name = "s5m8751-codec",
47 static const struct mfd_cell s5m8763_devs[] = {
49 .name = "s5m8763-pmic",
53 .name = "s5m-charger",
57 static const struct mfd_cell s5m8767_devs[] = {
59 .name = "s5m8767-pmic",
63 .name = "s5m8767-clk",
64 .of_compatible = "samsung,s5m8767-clk",
68 static const struct mfd_cell s2mps11_devs[] = {
70 .name = "s2mps11-pmic",
72 .name = "s2mps14-rtc",
74 .name = "s2mps11-clk",
75 .of_compatible = "samsung,s2mps11-clk",
79 static const struct mfd_cell s2mps13_devs[] = {
80 { .name = "s2mps13-pmic", },
81 { .name = "s2mps13-rtc", },
83 .name = "s2mps13-clk",
84 .of_compatible = "samsung,s2mps13-clk",
88 static const struct mfd_cell s2mps14_devs[] = {
90 .name = "s2mps14-pmic",
92 .name = "s2mps14-rtc",
94 .name = "s2mps14-clk",
95 .of_compatible = "samsung,s2mps14-clk",
99 static const struct mfd_cell s2mpa01_devs[] = {
101 .name = "s2mpa01-pmic",
105 static const struct mfd_cell s2mpu02_devs[] = {
106 { .name = "s2mpu02-pmic", },
107 { .name = "s2mpu02-rtc", },
109 .name = "s2mpu02-clk",
110 .of_compatible = "samsung,s2mpu02-clk",
115 static const struct of_device_id sec_dt_match[] = {
116 { .compatible = "samsung,s5m8767-pmic",
117 .data = (void *)S5M8767X,
119 .compatible = "samsung,s2mps11-pmic",
120 .data = (void *)S2MPS11X,
122 .compatible = "samsung,s2mps13-pmic",
123 .data = (void *)S2MPS13X,
125 .compatible = "samsung,s2mps14-pmic",
126 .data = (void *)S2MPS14X,
128 .compatible = "samsung,s2mpa01-pmic",
129 .data = (void *)S2MPA01,
131 .compatible = "samsung,s2mpu02-pmic",
132 .data = (void *)S2MPU02,
139 static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
142 case S2MPA01_REG_INT1M:
143 case S2MPA01_REG_INT2M:
144 case S2MPA01_REG_INT3M:
151 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
154 case S2MPS11_REG_INT1M:
155 case S2MPS11_REG_INT2M:
156 case S2MPS11_REG_INT3M:
163 static bool s2mpu02_volatile(struct device *dev, unsigned int reg)
166 case S2MPU02_REG_INT1M:
167 case S2MPU02_REG_INT2M:
168 case S2MPU02_REG_INT3M:
175 static bool s5m8763_volatile(struct device *dev, unsigned int reg)
178 case S5M8763_REG_IRQM1:
179 case S5M8763_REG_IRQM2:
180 case S5M8763_REG_IRQM3:
181 case S5M8763_REG_IRQM4:
188 static const struct regmap_config sec_regmap_config = {
193 static const struct regmap_config s2mpa01_regmap_config = {
197 .max_register = S2MPA01_REG_LDO_OVCB4,
198 .volatile_reg = s2mpa01_volatile,
199 .cache_type = REGCACHE_FLAT,
202 static const struct regmap_config s2mps11_regmap_config = {
206 .max_register = S2MPS11_REG_L38CTRL,
207 .volatile_reg = s2mps11_volatile,
208 .cache_type = REGCACHE_FLAT,
211 static const struct regmap_config s2mps13_regmap_config = {
215 .max_register = S2MPS13_REG_LDODSCH5,
216 .volatile_reg = s2mps11_volatile,
217 .cache_type = REGCACHE_FLAT,
220 static const struct regmap_config s2mps14_regmap_config = {
224 .max_register = S2MPS14_REG_LDODSCH3,
225 .volatile_reg = s2mps11_volatile,
226 .cache_type = REGCACHE_FLAT,
229 static const struct regmap_config s2mpu02_regmap_config = {
233 .max_register = S2MPU02_REG_DVSDATA,
234 .volatile_reg = s2mpu02_volatile,
235 .cache_type = REGCACHE_FLAT,
238 static const struct regmap_config s5m8763_regmap_config = {
242 .max_register = S5M8763_REG_LBCNFG2,
243 .volatile_reg = s5m8763_volatile,
244 .cache_type = REGCACHE_FLAT,
247 static const struct regmap_config s5m8767_regmap_config = {
251 .max_register = S5M8767_REG_LDO28CTRL,
252 .volatile_reg = s2mps11_volatile,
253 .cache_type = REGCACHE_FLAT,
256 static void sec_pmic_dump_rev(struct sec_pmic_dev *sec_pmic)
260 /* For each device type, the REG_ID is always the first register */
261 if (!regmap_read(sec_pmic->regmap_pmic, S2MPS11_REG_ID, &val))
262 dev_dbg(sec_pmic->dev, "Revision: 0x%x\n", val);
267 * Only the common platform data elements for s5m8767 are parsed here from the
268 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
269 * others have to parse their own platform data elements from device tree.
271 * The s5m8767 platform data structure is instantiated here and the drivers for
272 * the sub-modules need not instantiate another instance while parsing their
275 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
278 struct sec_platform_data *pd;
280 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
282 return ERR_PTR(-ENOMEM);
285 * ToDo: the 'wakeup' member in the platform data is more of a linux
286 * specfic information. Hence, there is no binding for that yet and
290 pd->manual_poweroff = of_property_read_bool(dev->of_node,
291 "samsung,s2mps11-acokb-ground");
295 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
302 static inline unsigned long sec_i2c_get_driver_data(struct i2c_client *i2c,
303 const struct i2c_device_id *id)
306 if (i2c->dev.of_node) {
307 const struct of_device_id *match;
309 match = of_match_node(sec_dt_match, i2c->dev.of_node);
310 return (unsigned long)match->data;
313 return id->driver_data;
316 static int sec_pmic_probe(struct i2c_client *i2c,
317 const struct i2c_device_id *id)
319 struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
320 const struct regmap_config *regmap;
321 const struct mfd_cell *sec_devs;
322 struct sec_pmic_dev *sec_pmic;
323 unsigned long device_type;
324 int ret, num_sec_devs;
326 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
328 if (sec_pmic == NULL)
331 i2c_set_clientdata(i2c, sec_pmic);
332 sec_pmic->dev = &i2c->dev;
334 sec_pmic->irq = i2c->irq;
335 device_type = sec_i2c_get_driver_data(i2c, id);
337 if (sec_pmic->dev->of_node) {
338 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
340 ret = PTR_ERR(pdata);
343 pdata->device_type = device_type;
346 sec_pmic->device_type = pdata->device_type;
347 sec_pmic->irq_base = pdata->irq_base;
348 sec_pmic->wakeup = pdata->wakeup;
349 sec_pmic->pdata = pdata;
352 switch (sec_pmic->device_type) {
354 regmap = &s2mpa01_regmap_config;
357 regmap = &s2mps11_regmap_config;
360 regmap = &s2mps13_regmap_config;
363 regmap = &s2mps14_regmap_config;
366 regmap = &s5m8763_regmap_config;
369 regmap = &s5m8767_regmap_config;
372 regmap = &s2mpu02_regmap_config;
375 regmap = &sec_regmap_config;
379 sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
380 if (IS_ERR(sec_pmic->regmap_pmic)) {
381 ret = PTR_ERR(sec_pmic->regmap_pmic);
382 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
387 if (pdata && pdata->cfg_pmic_irq)
388 pdata->cfg_pmic_irq();
390 sec_irq_init(sec_pmic);
392 pm_runtime_set_active(sec_pmic->dev);
394 switch (sec_pmic->device_type) {
396 sec_devs = s5m8751_devs;
397 num_sec_devs = ARRAY_SIZE(s5m8751_devs);
400 sec_devs = s5m8763_devs;
401 num_sec_devs = ARRAY_SIZE(s5m8763_devs);
404 sec_devs = s5m8767_devs;
405 num_sec_devs = ARRAY_SIZE(s5m8767_devs);
408 sec_devs = s2mpa01_devs;
409 num_sec_devs = ARRAY_SIZE(s2mpa01_devs);
412 sec_devs = s2mps11_devs;
413 num_sec_devs = ARRAY_SIZE(s2mps11_devs);
416 sec_devs = s2mps13_devs;
417 num_sec_devs = ARRAY_SIZE(s2mps13_devs);
420 sec_devs = s2mps14_devs;
421 num_sec_devs = ARRAY_SIZE(s2mps14_devs);
424 sec_devs = s2mpu02_devs;
425 num_sec_devs = ARRAY_SIZE(s2mpu02_devs);
428 /* If this happens the probe function is problem */
431 ret = mfd_add_devices(sec_pmic->dev, -1, sec_devs, num_sec_devs, NULL,
436 device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
437 sec_pmic_dump_rev(sec_pmic);
442 sec_irq_exit(sec_pmic);
446 static int sec_pmic_remove(struct i2c_client *i2c)
448 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
450 mfd_remove_devices(sec_pmic->dev);
451 sec_irq_exit(sec_pmic);
455 static void sec_pmic_shutdown(struct i2c_client *i2c)
457 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
458 unsigned int reg, mask;
460 if (!sec_pmic->pdata->manual_poweroff)
463 switch (sec_pmic->device_type) {
465 reg = S2MPS11_REG_CTRL1;
466 mask = S2MPS11_CTRL1_PWRHOLD_MASK;
470 * Currently only one board with S2MPS11 needs this, so just
473 dev_warn(sec_pmic->dev,
474 "Unsupported device %lu for manual power off\n",
475 sec_pmic->device_type);
479 regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, 0);
482 #ifdef CONFIG_PM_SLEEP
483 static int sec_pmic_suspend(struct device *dev)
485 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
486 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
488 if (device_may_wakeup(dev))
489 enable_irq_wake(sec_pmic->irq);
491 * PMIC IRQ must be disabled during suspend for RTC alarm
493 * When device is woken up from suspend, an
494 * interrupt occurs before resuming I2C bus controller.
495 * The interrupt is handled by regmap_irq_thread which tries
496 * to read RTC registers. This read fails (I2C is still
497 * suspended) and RTC Alarm interrupt is disabled.
499 disable_irq(sec_pmic->irq);
504 static int sec_pmic_resume(struct device *dev)
506 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
507 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
509 if (device_may_wakeup(dev))
510 disable_irq_wake(sec_pmic->irq);
511 enable_irq(sec_pmic->irq);
515 #endif /* CONFIG_PM_SLEEP */
517 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
519 static const struct i2c_device_id sec_pmic_id[] = {
523 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
525 static struct i2c_driver sec_pmic_driver = {
528 .pm = &sec_pmic_pm_ops,
529 .of_match_table = of_match_ptr(sec_dt_match),
531 .probe = sec_pmic_probe,
532 .remove = sec_pmic_remove,
533 .shutdown = sec_pmic_shutdown,
534 .id_table = sec_pmic_id,
537 static int __init sec_pmic_init(void)
539 return i2c_add_driver(&sec_pmic_driver);
542 subsys_initcall(sec_pmic_init);
544 static void __exit sec_pmic_exit(void)
546 i2c_del_driver(&sec_pmic_driver);
548 module_exit(sec_pmic_exit);
550 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
551 MODULE_DESCRIPTION("Core support for the S5M MFD");
552 MODULE_LICENSE("GPL");