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
[] = {
107 .name
= "s2mpu02-pmic",
112 static const struct of_device_id sec_dt_match
[] = {
113 { .compatible
= "samsung,s5m8767-pmic",
114 .data
= (void *)S5M8767X
,
116 .compatible
= "samsung,s2mps11-pmic",
117 .data
= (void *)S2MPS11X
,
119 .compatible
= "samsung,s2mps13-pmic",
120 .data
= (void *)S2MPS13X
,
122 .compatible
= "samsung,s2mps14-pmic",
123 .data
= (void *)S2MPS14X
,
125 .compatible
= "samsung,s2mpa01-pmic",
126 .data
= (void *)S2MPA01
,
128 .compatible
= "samsung,s2mpu02-pmic",
129 .data
= (void *)S2MPU02
,
136 static bool s2mpa01_volatile(struct device
*dev
, unsigned int reg
)
139 case S2MPA01_REG_INT1M
:
140 case S2MPA01_REG_INT2M
:
141 case S2MPA01_REG_INT3M
:
148 static bool s2mps11_volatile(struct device
*dev
, unsigned int reg
)
151 case S2MPS11_REG_INT1M
:
152 case S2MPS11_REG_INT2M
:
153 case S2MPS11_REG_INT3M
:
160 static bool s2mpu02_volatile(struct device
*dev
, unsigned int reg
)
163 case S2MPU02_REG_INT1M
:
164 case S2MPU02_REG_INT2M
:
165 case S2MPU02_REG_INT3M
:
172 static bool s5m8763_volatile(struct device
*dev
, unsigned int reg
)
175 case S5M8763_REG_IRQM1
:
176 case S5M8763_REG_IRQM2
:
177 case S5M8763_REG_IRQM3
:
178 case S5M8763_REG_IRQM4
:
185 static const struct regmap_config sec_regmap_config
= {
190 static const struct regmap_config s2mpa01_regmap_config
= {
194 .max_register
= S2MPA01_REG_LDO_OVCB4
,
195 .volatile_reg
= s2mpa01_volatile
,
196 .cache_type
= REGCACHE_FLAT
,
199 static const struct regmap_config s2mps11_regmap_config
= {
203 .max_register
= S2MPS11_REG_L38CTRL
,
204 .volatile_reg
= s2mps11_volatile
,
205 .cache_type
= REGCACHE_FLAT
,
208 static const struct regmap_config s2mps13_regmap_config
= {
212 .max_register
= S2MPS13_REG_LDODSCH5
,
213 .volatile_reg
= s2mps11_volatile
,
214 .cache_type
= REGCACHE_FLAT
,
217 static const struct regmap_config s2mps14_regmap_config
= {
221 .max_register
= S2MPS14_REG_LDODSCH3
,
222 .volatile_reg
= s2mps11_volatile
,
223 .cache_type
= REGCACHE_FLAT
,
226 static const struct regmap_config s2mpu02_regmap_config
= {
230 .max_register
= S2MPU02_REG_DVSDATA
,
231 .volatile_reg
= s2mpu02_volatile
,
232 .cache_type
= REGCACHE_FLAT
,
235 static const struct regmap_config s5m8763_regmap_config
= {
239 .max_register
= S5M8763_REG_LBCNFG2
,
240 .volatile_reg
= s5m8763_volatile
,
241 .cache_type
= REGCACHE_FLAT
,
244 static const struct regmap_config s5m8767_regmap_config
= {
248 .max_register
= S5M8767_REG_LDO28CTRL
,
249 .volatile_reg
= s2mps11_volatile
,
250 .cache_type
= REGCACHE_FLAT
,
253 static void sec_pmic_dump_rev(struct sec_pmic_dev
*sec_pmic
)
257 /* For each device type, the REG_ID is always the first register */
258 if (!regmap_read(sec_pmic
->regmap_pmic
, S2MPS11_REG_ID
, &val
))
259 dev_dbg(sec_pmic
->dev
, "Revision: 0x%x\n", val
);
262 static void sec_pmic_configure(struct sec_pmic_dev
*sec_pmic
)
266 if (sec_pmic
->device_type
!= S2MPS13X
)
269 if (sec_pmic
->pdata
->disable_wrstbi
) {
271 * If WRSTBI pin is pulled down this feature must be disabled
272 * because each Suspend to RAM will trigger buck voltage reset
275 err
= regmap_update_bits(sec_pmic
->regmap_pmic
,
277 S2MPS13_REG_WRSTBI_MASK
, 0x0);
279 dev_warn(sec_pmic
->dev
,
280 "Cannot initialize WRSTBI config: %d\n",
287 * Only the common platform data elements for s5m8767 are parsed here from the
288 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
289 * others have to parse their own platform data elements from device tree.
291 * The s5m8767 platform data structure is instantiated here and the drivers for
292 * the sub-modules need not instantiate another instance while parsing their
295 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
298 struct sec_platform_data
*pd
;
300 pd
= devm_kzalloc(dev
, sizeof(*pd
), GFP_KERNEL
);
302 return ERR_PTR(-ENOMEM
);
305 * ToDo: the 'wakeup' member in the platform data is more of a linux
306 * specfic information. Hence, there is no binding for that yet and
310 pd
->manual_poweroff
= of_property_read_bool(dev
->of_node
,
311 "samsung,s2mps11-acokb-ground");
312 pd
->disable_wrstbi
= of_property_read_bool(dev
->of_node
,
313 "samsung,s2mps11-wrstbi-ground");
317 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
324 static inline unsigned long sec_i2c_get_driver_data(struct i2c_client
*i2c
,
325 const struct i2c_device_id
*id
)
328 if (i2c
->dev
.of_node
) {
329 const struct of_device_id
*match
;
331 match
= of_match_node(sec_dt_match
, i2c
->dev
.of_node
);
332 return (unsigned long)match
->data
;
335 return id
->driver_data
;
338 static int sec_pmic_probe(struct i2c_client
*i2c
,
339 const struct i2c_device_id
*id
)
341 struct sec_platform_data
*pdata
= dev_get_platdata(&i2c
->dev
);
342 const struct regmap_config
*regmap
;
343 const struct mfd_cell
*sec_devs
;
344 struct sec_pmic_dev
*sec_pmic
;
345 unsigned long device_type
;
346 int ret
, num_sec_devs
;
348 sec_pmic
= devm_kzalloc(&i2c
->dev
, sizeof(struct sec_pmic_dev
),
350 if (sec_pmic
== NULL
)
353 i2c_set_clientdata(i2c
, sec_pmic
);
354 sec_pmic
->dev
= &i2c
->dev
;
356 sec_pmic
->irq
= i2c
->irq
;
357 device_type
= sec_i2c_get_driver_data(i2c
, id
);
359 if (sec_pmic
->dev
->of_node
) {
360 pdata
= sec_pmic_i2c_parse_dt_pdata(sec_pmic
->dev
);
362 ret
= PTR_ERR(pdata
);
365 pdata
->device_type
= device_type
;
368 sec_pmic
->device_type
= pdata
->device_type
;
369 sec_pmic
->irq_base
= pdata
->irq_base
;
370 sec_pmic
->wakeup
= pdata
->wakeup
;
371 sec_pmic
->pdata
= pdata
;
374 switch (sec_pmic
->device_type
) {
376 regmap
= &s2mpa01_regmap_config
;
379 regmap
= &s2mps11_regmap_config
;
382 regmap
= &s2mps13_regmap_config
;
385 regmap
= &s2mps14_regmap_config
;
388 regmap
= &s5m8763_regmap_config
;
391 regmap
= &s5m8767_regmap_config
;
394 regmap
= &s2mpu02_regmap_config
;
397 regmap
= &sec_regmap_config
;
401 sec_pmic
->regmap_pmic
= devm_regmap_init_i2c(i2c
, regmap
);
402 if (IS_ERR(sec_pmic
->regmap_pmic
)) {
403 ret
= PTR_ERR(sec_pmic
->regmap_pmic
);
404 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
409 if (pdata
&& pdata
->cfg_pmic_irq
)
410 pdata
->cfg_pmic_irq();
412 sec_irq_init(sec_pmic
);
414 pm_runtime_set_active(sec_pmic
->dev
);
416 switch (sec_pmic
->device_type
) {
418 sec_devs
= s5m8751_devs
;
419 num_sec_devs
= ARRAY_SIZE(s5m8751_devs
);
422 sec_devs
= s5m8763_devs
;
423 num_sec_devs
= ARRAY_SIZE(s5m8763_devs
);
426 sec_devs
= s5m8767_devs
;
427 num_sec_devs
= ARRAY_SIZE(s5m8767_devs
);
430 sec_devs
= s2mpa01_devs
;
431 num_sec_devs
= ARRAY_SIZE(s2mpa01_devs
);
434 sec_devs
= s2mps11_devs
;
435 num_sec_devs
= ARRAY_SIZE(s2mps11_devs
);
438 sec_devs
= s2mps13_devs
;
439 num_sec_devs
= ARRAY_SIZE(s2mps13_devs
);
442 sec_devs
= s2mps14_devs
;
443 num_sec_devs
= ARRAY_SIZE(s2mps14_devs
);
446 sec_devs
= s2mpu02_devs
;
447 num_sec_devs
= ARRAY_SIZE(s2mpu02_devs
);
450 /* If this happens the probe function is problem */
453 ret
= mfd_add_devices(sec_pmic
->dev
, -1, sec_devs
, num_sec_devs
, NULL
,
458 device_init_wakeup(sec_pmic
->dev
, sec_pmic
->wakeup
);
459 sec_pmic_configure(sec_pmic
);
460 sec_pmic_dump_rev(sec_pmic
);
465 sec_irq_exit(sec_pmic
);
469 static int sec_pmic_remove(struct i2c_client
*i2c
)
471 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
473 mfd_remove_devices(sec_pmic
->dev
);
474 sec_irq_exit(sec_pmic
);
478 static void sec_pmic_shutdown(struct i2c_client
*i2c
)
480 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
481 unsigned int reg
, mask
;
483 if (!sec_pmic
->pdata
->manual_poweroff
)
486 switch (sec_pmic
->device_type
) {
488 reg
= S2MPS11_REG_CTRL1
;
489 mask
= S2MPS11_CTRL1_PWRHOLD_MASK
;
493 * Currently only one board with S2MPS11 needs this, so just
496 dev_warn(sec_pmic
->dev
,
497 "Unsupported device %lu for manual power off\n",
498 sec_pmic
->device_type
);
502 regmap_update_bits(sec_pmic
->regmap_pmic
, reg
, mask
, 0);
505 #ifdef CONFIG_PM_SLEEP
506 static int sec_pmic_suspend(struct device
*dev
)
508 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
509 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
511 if (device_may_wakeup(dev
))
512 enable_irq_wake(sec_pmic
->irq
);
514 * PMIC IRQ must be disabled during suspend for RTC alarm
516 * When device is woken up from suspend, an
517 * interrupt occurs before resuming I2C bus controller.
518 * The interrupt is handled by regmap_irq_thread which tries
519 * to read RTC registers. This read fails (I2C is still
520 * suspended) and RTC Alarm interrupt is disabled.
522 disable_irq(sec_pmic
->irq
);
527 static int sec_pmic_resume(struct device
*dev
)
529 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
530 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
532 if (device_may_wakeup(dev
))
533 disable_irq_wake(sec_pmic
->irq
);
534 enable_irq(sec_pmic
->irq
);
538 #endif /* CONFIG_PM_SLEEP */
540 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops
, sec_pmic_suspend
, sec_pmic_resume
);
542 static const struct i2c_device_id sec_pmic_id
[] = {
546 MODULE_DEVICE_TABLE(i2c
, sec_pmic_id
);
548 static struct i2c_driver sec_pmic_driver
= {
551 .pm
= &sec_pmic_pm_ops
,
552 .of_match_table
= of_match_ptr(sec_dt_match
),
554 .probe
= sec_pmic_probe
,
555 .remove
= sec_pmic_remove
,
556 .shutdown
= sec_pmic_shutdown
,
557 .id_table
= sec_pmic_id
,
560 static int __init
sec_pmic_init(void)
562 return i2c_add_driver(&sec_pmic_driver
);
565 subsys_initcall(sec_pmic_init
);
567 static void __exit
sec_pmic_exit(void)
569 i2c_del_driver(&sec_pmic_driver
);
571 module_exit(sec_pmic_exit
);
573 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
574 MODULE_DESCRIPTION("Core support for the S5M MFD");
575 MODULE_LICENSE("GPL");