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/s2mps15.h>
33 #include <linux/mfd/samsung/s2mpu02.h>
34 #include <linux/mfd/samsung/s5m8763.h>
35 #include <linux/mfd/samsung/s5m8767.h>
36 #include <linux/regmap.h>
38 static const struct mfd_cell s5m8751_devs
[] = {
40 .name
= "s5m8751-pmic",
42 .name
= "s5m-charger",
44 .name
= "s5m8751-codec",
48 static const struct mfd_cell s5m8763_devs
[] = {
50 .name
= "s5m8763-pmic",
54 .name
= "s5m-charger",
58 static const struct mfd_cell s5m8767_devs
[] = {
60 .name
= "s5m8767-pmic",
64 .name
= "s5m8767-clk",
65 .of_compatible
= "samsung,s5m8767-clk",
69 static const struct mfd_cell s2mps11_devs
[] = {
71 .name
= "s2mps11-regulator",
73 .name
= "s2mps14-rtc",
75 .name
= "s2mps11-clk",
76 .of_compatible
= "samsung,s2mps11-clk",
80 static const struct mfd_cell s2mps13_devs
[] = {
81 { .name
= "s2mps13-regulator", },
82 { .name
= "s2mps13-rtc", },
84 .name
= "s2mps13-clk",
85 .of_compatible
= "samsung,s2mps13-clk",
89 static const struct mfd_cell s2mps14_devs
[] = {
91 .name
= "s2mps14-regulator",
93 .name
= "s2mps14-rtc",
95 .name
= "s2mps14-clk",
96 .of_compatible
= "samsung,s2mps14-clk",
100 static const struct mfd_cell s2mps15_devs
[] = {
102 .name
= "s2mps15-regulator",
104 .name
= "s2mps15-rtc",
106 .name
= "s2mps13-clk",
107 .of_compatible
= "samsung,s2mps13-clk",
111 static const struct mfd_cell s2mpa01_devs
[] = {
113 .name
= "s2mpa01-pmic",
117 static const struct mfd_cell s2mpu02_devs
[] = {
119 .name
= "s2mpu02-regulator",
124 static const struct of_device_id sec_dt_match
[] = {
125 { .compatible
= "samsung,s5m8767-pmic",
126 .data
= (void *)S5M8767X
,
128 .compatible
= "samsung,s2mps11-pmic",
129 .data
= (void *)S2MPS11X
,
131 .compatible
= "samsung,s2mps13-pmic",
132 .data
= (void *)S2MPS13X
,
134 .compatible
= "samsung,s2mps14-pmic",
135 .data
= (void *)S2MPS14X
,
137 .compatible
= "samsung,s2mps15-pmic",
138 .data
= (void *)S2MPS15X
,
140 .compatible
= "samsung,s2mpa01-pmic",
141 .data
= (void *)S2MPA01
,
143 .compatible
= "samsung,s2mpu02-pmic",
144 .data
= (void *)S2MPU02
,
149 MODULE_DEVICE_TABLE(of
, sec_dt_match
);
152 static bool s2mpa01_volatile(struct device
*dev
, unsigned int reg
)
155 case S2MPA01_REG_INT1M
:
156 case S2MPA01_REG_INT2M
:
157 case S2MPA01_REG_INT3M
:
164 static bool s2mps11_volatile(struct device
*dev
, unsigned int reg
)
167 case S2MPS11_REG_INT1M
:
168 case S2MPS11_REG_INT2M
:
169 case S2MPS11_REG_INT3M
:
176 static bool s2mpu02_volatile(struct device
*dev
, unsigned int reg
)
179 case S2MPU02_REG_INT1M
:
180 case S2MPU02_REG_INT2M
:
181 case S2MPU02_REG_INT3M
:
188 static bool s5m8763_volatile(struct device
*dev
, unsigned int reg
)
191 case S5M8763_REG_IRQM1
:
192 case S5M8763_REG_IRQM2
:
193 case S5M8763_REG_IRQM3
:
194 case S5M8763_REG_IRQM4
:
201 static const struct regmap_config sec_regmap_config
= {
206 static const struct regmap_config s2mpa01_regmap_config
= {
210 .max_register
= S2MPA01_REG_LDO_OVCB4
,
211 .volatile_reg
= s2mpa01_volatile
,
212 .cache_type
= REGCACHE_FLAT
,
215 static const struct regmap_config s2mps11_regmap_config
= {
219 .max_register
= S2MPS11_REG_L38CTRL
,
220 .volatile_reg
= s2mps11_volatile
,
221 .cache_type
= REGCACHE_FLAT
,
224 static const struct regmap_config s2mps13_regmap_config
= {
228 .max_register
= S2MPS13_REG_LDODSCH5
,
229 .volatile_reg
= s2mps11_volatile
,
230 .cache_type
= REGCACHE_FLAT
,
233 static const struct regmap_config s2mps14_regmap_config
= {
237 .max_register
= S2MPS14_REG_LDODSCH3
,
238 .volatile_reg
= s2mps11_volatile
,
239 .cache_type
= REGCACHE_FLAT
,
242 static const struct regmap_config s2mps15_regmap_config
= {
246 .max_register
= S2MPS15_REG_LDODSCH4
,
247 .volatile_reg
= s2mps11_volatile
,
248 .cache_type
= REGCACHE_FLAT
,
251 static const struct regmap_config s2mpu02_regmap_config
= {
255 .max_register
= S2MPU02_REG_DVSDATA
,
256 .volatile_reg
= s2mpu02_volatile
,
257 .cache_type
= REGCACHE_FLAT
,
260 static const struct regmap_config s5m8763_regmap_config
= {
264 .max_register
= S5M8763_REG_LBCNFG2
,
265 .volatile_reg
= s5m8763_volatile
,
266 .cache_type
= REGCACHE_FLAT
,
269 static const struct regmap_config s5m8767_regmap_config
= {
273 .max_register
= S5M8767_REG_LDO28CTRL
,
274 .volatile_reg
= s2mps11_volatile
,
275 .cache_type
= REGCACHE_FLAT
,
278 static void sec_pmic_dump_rev(struct sec_pmic_dev
*sec_pmic
)
282 /* For each device type, the REG_ID is always the first register */
283 if (!regmap_read(sec_pmic
->regmap_pmic
, S2MPS11_REG_ID
, &val
))
284 dev_dbg(sec_pmic
->dev
, "Revision: 0x%x\n", val
);
287 static void sec_pmic_configure(struct sec_pmic_dev
*sec_pmic
)
291 if (sec_pmic
->device_type
!= S2MPS13X
)
294 if (sec_pmic
->pdata
->disable_wrstbi
) {
296 * If WRSTBI pin is pulled down this feature must be disabled
297 * because each Suspend to RAM will trigger buck voltage reset
300 err
= regmap_update_bits(sec_pmic
->regmap_pmic
,
302 S2MPS13_REG_WRSTBI_MASK
, 0x0);
304 dev_warn(sec_pmic
->dev
,
305 "Cannot initialize WRSTBI config: %d\n",
312 * Only the common platform data elements for s5m8767 are parsed here from the
313 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
314 * others have to parse their own platform data elements from device tree.
316 * The s5m8767 platform data structure is instantiated here and the drivers for
317 * the sub-modules need not instantiate another instance while parsing their
320 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
323 struct sec_platform_data
*pd
;
325 pd
= devm_kzalloc(dev
, sizeof(*pd
), GFP_KERNEL
);
327 return ERR_PTR(-ENOMEM
);
330 * ToDo: the 'wakeup' member in the platform data is more of a linux
331 * specfic information. Hence, there is no binding for that yet and
335 pd
->manual_poweroff
= of_property_read_bool(dev
->of_node
,
336 "samsung,s2mps11-acokb-ground");
337 pd
->disable_wrstbi
= of_property_read_bool(dev
->of_node
,
338 "samsung,s2mps11-wrstbi-ground");
342 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
349 static inline unsigned long sec_i2c_get_driver_data(struct i2c_client
*i2c
,
350 const struct i2c_device_id
*id
)
353 if (i2c
->dev
.of_node
) {
354 const struct of_device_id
*match
;
356 match
= of_match_node(sec_dt_match
, i2c
->dev
.of_node
);
357 return (unsigned long)match
->data
;
360 return id
->driver_data
;
363 static int sec_pmic_probe(struct i2c_client
*i2c
,
364 const struct i2c_device_id
*id
)
366 struct sec_platform_data
*pdata
= dev_get_platdata(&i2c
->dev
);
367 const struct regmap_config
*regmap
;
368 const struct mfd_cell
*sec_devs
;
369 struct sec_pmic_dev
*sec_pmic
;
370 unsigned long device_type
;
371 int ret
, num_sec_devs
;
373 sec_pmic
= devm_kzalloc(&i2c
->dev
, sizeof(struct sec_pmic_dev
),
375 if (sec_pmic
== NULL
)
378 i2c_set_clientdata(i2c
, sec_pmic
);
379 sec_pmic
->dev
= &i2c
->dev
;
381 sec_pmic
->irq
= i2c
->irq
;
382 device_type
= sec_i2c_get_driver_data(i2c
, id
);
384 if (sec_pmic
->dev
->of_node
) {
385 pdata
= sec_pmic_i2c_parse_dt_pdata(sec_pmic
->dev
);
387 ret
= PTR_ERR(pdata
);
390 pdata
->device_type
= device_type
;
393 sec_pmic
->device_type
= pdata
->device_type
;
394 sec_pmic
->irq_base
= pdata
->irq_base
;
395 sec_pmic
->wakeup
= pdata
->wakeup
;
396 sec_pmic
->pdata
= pdata
;
399 switch (sec_pmic
->device_type
) {
401 regmap
= &s2mpa01_regmap_config
;
404 regmap
= &s2mps11_regmap_config
;
407 regmap
= &s2mps13_regmap_config
;
410 regmap
= &s2mps14_regmap_config
;
413 regmap
= &s2mps15_regmap_config
;
416 regmap
= &s5m8763_regmap_config
;
419 regmap
= &s5m8767_regmap_config
;
422 regmap
= &s2mpu02_regmap_config
;
425 regmap
= &sec_regmap_config
;
429 sec_pmic
->regmap_pmic
= devm_regmap_init_i2c(i2c
, regmap
);
430 if (IS_ERR(sec_pmic
->regmap_pmic
)) {
431 ret
= PTR_ERR(sec_pmic
->regmap_pmic
);
432 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
437 if (pdata
&& pdata
->cfg_pmic_irq
)
438 pdata
->cfg_pmic_irq();
440 sec_irq_init(sec_pmic
);
442 pm_runtime_set_active(sec_pmic
->dev
);
444 switch (sec_pmic
->device_type
) {
446 sec_devs
= s5m8751_devs
;
447 num_sec_devs
= ARRAY_SIZE(s5m8751_devs
);
450 sec_devs
= s5m8763_devs
;
451 num_sec_devs
= ARRAY_SIZE(s5m8763_devs
);
454 sec_devs
= s5m8767_devs
;
455 num_sec_devs
= ARRAY_SIZE(s5m8767_devs
);
458 sec_devs
= s2mpa01_devs
;
459 num_sec_devs
= ARRAY_SIZE(s2mpa01_devs
);
462 sec_devs
= s2mps11_devs
;
463 num_sec_devs
= ARRAY_SIZE(s2mps11_devs
);
466 sec_devs
= s2mps13_devs
;
467 num_sec_devs
= ARRAY_SIZE(s2mps13_devs
);
470 sec_devs
= s2mps14_devs
;
471 num_sec_devs
= ARRAY_SIZE(s2mps14_devs
);
474 sec_devs
= s2mps15_devs
;
475 num_sec_devs
= ARRAY_SIZE(s2mps15_devs
);
478 sec_devs
= s2mpu02_devs
;
479 num_sec_devs
= ARRAY_SIZE(s2mpu02_devs
);
482 /* If this happens the probe function is problem */
485 ret
= devm_mfd_add_devices(sec_pmic
->dev
, -1, sec_devs
, num_sec_devs
,
490 device_init_wakeup(sec_pmic
->dev
, sec_pmic
->wakeup
);
491 sec_pmic_configure(sec_pmic
);
492 sec_pmic_dump_rev(sec_pmic
);
497 static void sec_pmic_shutdown(struct i2c_client
*i2c
)
499 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
500 unsigned int reg
, mask
;
502 if (!sec_pmic
->pdata
->manual_poweroff
)
505 switch (sec_pmic
->device_type
) {
507 reg
= S2MPS11_REG_CTRL1
;
508 mask
= S2MPS11_CTRL1_PWRHOLD_MASK
;
512 * Currently only one board with S2MPS11 needs this, so just
515 dev_warn(sec_pmic
->dev
,
516 "Unsupported device %lu for manual power off\n",
517 sec_pmic
->device_type
);
521 regmap_update_bits(sec_pmic
->regmap_pmic
, reg
, mask
, 0);
524 #ifdef CONFIG_PM_SLEEP
525 static int sec_pmic_suspend(struct device
*dev
)
527 struct i2c_client
*i2c
= to_i2c_client(dev
);
528 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
530 if (device_may_wakeup(dev
))
531 enable_irq_wake(sec_pmic
->irq
);
533 * PMIC IRQ must be disabled during suspend for RTC alarm
535 * When device is woken up from suspend, an
536 * interrupt occurs before resuming I2C bus controller.
537 * The interrupt is handled by regmap_irq_thread which tries
538 * to read RTC registers. This read fails (I2C is still
539 * suspended) and RTC Alarm interrupt is disabled.
541 disable_irq(sec_pmic
->irq
);
546 static int sec_pmic_resume(struct device
*dev
)
548 struct i2c_client
*i2c
= to_i2c_client(dev
);
549 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
551 if (device_may_wakeup(dev
))
552 disable_irq_wake(sec_pmic
->irq
);
553 enable_irq(sec_pmic
->irq
);
557 #endif /* CONFIG_PM_SLEEP */
559 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops
, sec_pmic_suspend
, sec_pmic_resume
);
561 static const struct i2c_device_id sec_pmic_id
[] = {
565 MODULE_DEVICE_TABLE(i2c
, sec_pmic_id
);
567 static struct i2c_driver sec_pmic_driver
= {
570 .pm
= &sec_pmic_pm_ops
,
571 .of_match_table
= of_match_ptr(sec_dt_match
),
573 .probe
= sec_pmic_probe
,
574 .shutdown
= sec_pmic_shutdown
,
575 .id_table
= sec_pmic_id
,
578 static int __init
sec_pmic_init(void)
580 return i2c_add_driver(&sec_pmic_driver
);
583 subsys_initcall(sec_pmic_init
);
585 static void __exit
sec_pmic_exit(void)
587 i2c_del_driver(&sec_pmic_driver
);
589 module_exit(sec_pmic_exit
);
591 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
592 MODULE_DESCRIPTION("Core support for the S5M MFD");
593 MODULE_LICENSE("GPL");