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 s2dos05_devs
[] = {
38 { .name
= "s2dos05-regulator", },
41 static const struct mfd_cell s2mps11_devs
[] = {
42 { .name
= "s2mps11-regulator", },
43 { .name
= "s2mps14-rtc", },
45 .name
= "s2mps11-clk",
46 .of_compatible
= "samsung,s2mps11-clk",
50 static const struct mfd_cell s2mps13_devs
[] = {
51 { .name
= "s2mps13-regulator", },
52 { .name
= "s2mps13-rtc", },
54 .name
= "s2mps13-clk",
55 .of_compatible
= "samsung,s2mps13-clk",
59 static const struct mfd_cell s2mps14_devs
[] = {
60 { .name
= "s2mps14-regulator", },
61 { .name
= "s2mps14-rtc", },
63 .name
= "s2mps14-clk",
64 .of_compatible
= "samsung,s2mps14-clk",
68 static const struct mfd_cell s2mps15_devs
[] = {
69 { .name
= "s2mps15-regulator", },
70 { .name
= "s2mps15-rtc", },
72 .name
= "s2mps13-clk",
73 .of_compatible
= "samsung,s2mps13-clk",
77 static const struct mfd_cell s2mpa01_devs
[] = {
78 { .name
= "s2mpa01-pmic", },
79 { .name
= "s2mps14-rtc", },
82 static const struct mfd_cell s2mpu02_devs
[] = {
83 { .name
= "s2mpu02-regulator", },
86 static const struct of_device_id sec_dt_match
[] = {
88 .compatible
= "samsung,s5m8767-pmic",
89 .data
= (void *)S5M8767X
,
91 .compatible
= "samsung,s2dos05",
92 .data
= (void *)S2DOS05
,
94 .compatible
= "samsung,s2mps11-pmic",
95 .data
= (void *)S2MPS11X
,
97 .compatible
= "samsung,s2mps13-pmic",
98 .data
= (void *)S2MPS13X
,
100 .compatible
= "samsung,s2mps14-pmic",
101 .data
= (void *)S2MPS14X
,
103 .compatible
= "samsung,s2mps15-pmic",
104 .data
= (void *)S2MPS15X
,
106 .compatible
= "samsung,s2mpa01-pmic",
107 .data
= (void *)S2MPA01
,
109 .compatible
= "samsung,s2mpu02-pmic",
110 .data
= (void *)S2MPU02
,
115 MODULE_DEVICE_TABLE(of
, sec_dt_match
);
117 static bool s2mpa01_volatile(struct device
*dev
, unsigned int reg
)
120 case S2MPA01_REG_INT1M
:
121 case S2MPA01_REG_INT2M
:
122 case S2MPA01_REG_INT3M
:
129 static bool s2mps11_volatile(struct device
*dev
, unsigned int reg
)
132 case S2MPS11_REG_INT1M
:
133 case S2MPS11_REG_INT2M
:
134 case S2MPS11_REG_INT3M
:
141 static bool s2mpu02_volatile(struct device
*dev
, unsigned int reg
)
144 case S2MPU02_REG_INT1M
:
145 case S2MPU02_REG_INT2M
:
146 case S2MPU02_REG_INT3M
:
153 static const struct regmap_config sec_regmap_config
= {
158 static const struct regmap_config s2mpa01_regmap_config
= {
162 .max_register
= S2MPA01_REG_LDO_OVCB4
,
163 .volatile_reg
= s2mpa01_volatile
,
164 .cache_type
= REGCACHE_FLAT
,
167 static const struct regmap_config s2mps11_regmap_config
= {
171 .max_register
= S2MPS11_REG_L38CTRL
,
172 .volatile_reg
= s2mps11_volatile
,
173 .cache_type
= REGCACHE_FLAT
,
176 static const struct regmap_config s2mps13_regmap_config
= {
180 .max_register
= S2MPS13_REG_LDODSCH5
,
181 .volatile_reg
= s2mps11_volatile
,
182 .cache_type
= REGCACHE_FLAT
,
185 static const struct regmap_config s2mps14_regmap_config
= {
189 .max_register
= S2MPS14_REG_LDODSCH3
,
190 .volatile_reg
= s2mps11_volatile
,
191 .cache_type
= REGCACHE_FLAT
,
194 static const struct regmap_config s2mps15_regmap_config
= {
198 .max_register
= S2MPS15_REG_LDODSCH4
,
199 .volatile_reg
= s2mps11_volatile
,
200 .cache_type
= REGCACHE_FLAT
,
203 static const struct regmap_config s2mpu02_regmap_config
= {
207 .max_register
= S2MPU02_REG_DVSDATA
,
208 .volatile_reg
= s2mpu02_volatile
,
209 .cache_type
= REGCACHE_FLAT
,
212 static const struct regmap_config s5m8767_regmap_config
= {
216 .max_register
= S5M8767_REG_LDO28CTRL
,
217 .volatile_reg
= s2mps11_volatile
,
218 .cache_type
= REGCACHE_FLAT
,
221 static void sec_pmic_dump_rev(struct sec_pmic_dev
*sec_pmic
)
225 /* For each device type, the REG_ID is always the first register */
226 if (!regmap_read(sec_pmic
->regmap_pmic
, S2MPS11_REG_ID
, &val
))
227 dev_dbg(sec_pmic
->dev
, "Revision: 0x%x\n", val
);
230 static void sec_pmic_configure(struct sec_pmic_dev
*sec_pmic
)
234 if (sec_pmic
->device_type
!= S2MPS13X
)
237 if (sec_pmic
->pdata
->disable_wrstbi
) {
239 * If WRSTBI pin is pulled down this feature must be disabled
240 * because each Suspend to RAM will trigger buck voltage reset
243 err
= regmap_update_bits(sec_pmic
->regmap_pmic
,
245 S2MPS13_REG_WRSTBI_MASK
, 0x0);
247 dev_warn(sec_pmic
->dev
,
248 "Cannot initialize WRSTBI config: %d\n",
254 * Only the common platform data elements for s5m8767 are parsed here from the
255 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
256 * others have to parse their own platform data elements from device tree.
258 * The s5m8767 platform data structure is instantiated here and the drivers for
259 * the sub-modules need not instantiate another instance while parsing their
262 static struct sec_platform_data
*
263 sec_pmic_i2c_parse_dt_pdata(struct device
*dev
)
265 struct sec_platform_data
*pd
;
267 pd
= devm_kzalloc(dev
, sizeof(*pd
), GFP_KERNEL
);
269 return ERR_PTR(-ENOMEM
);
271 pd
->manual_poweroff
= of_property_read_bool(dev
->of_node
,
272 "samsung,s2mps11-acokb-ground");
273 pd
->disable_wrstbi
= of_property_read_bool(dev
->of_node
,
274 "samsung,s2mps11-wrstbi-ground");
278 static int sec_pmic_probe(struct i2c_client
*i2c
)
280 const struct regmap_config
*regmap
;
281 struct sec_platform_data
*pdata
;
282 const struct mfd_cell
*sec_devs
;
283 struct sec_pmic_dev
*sec_pmic
;
284 int ret
, num_sec_devs
;
286 sec_pmic
= devm_kzalloc(&i2c
->dev
, sizeof(struct sec_pmic_dev
),
288 if (sec_pmic
== NULL
)
291 i2c_set_clientdata(i2c
, sec_pmic
);
292 sec_pmic
->dev
= &i2c
->dev
;
294 sec_pmic
->irq
= i2c
->irq
;
296 pdata
= sec_pmic_i2c_parse_dt_pdata(sec_pmic
->dev
);
298 ret
= PTR_ERR(pdata
);
302 sec_pmic
->device_type
= (unsigned long)of_device_get_match_data(sec_pmic
->dev
);
303 sec_pmic
->pdata
= pdata
;
305 switch (sec_pmic
->device_type
) {
307 regmap
= &s2mpa01_regmap_config
;
310 regmap
= &s2mps11_regmap_config
;
313 regmap
= &s2mps13_regmap_config
;
316 regmap
= &s2mps14_regmap_config
;
319 regmap
= &s2mps15_regmap_config
;
322 regmap
= &s5m8767_regmap_config
;
325 regmap
= &s2mpu02_regmap_config
;
328 regmap
= &sec_regmap_config
;
332 sec_pmic
->regmap_pmic
= devm_regmap_init_i2c(i2c
, regmap
);
333 if (IS_ERR(sec_pmic
->regmap_pmic
)) {
334 ret
= PTR_ERR(sec_pmic
->regmap_pmic
);
335 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
340 sec_irq_init(sec_pmic
);
342 pm_runtime_set_active(sec_pmic
->dev
);
344 switch (sec_pmic
->device_type
) {
346 sec_devs
= s5m8767_devs
;
347 num_sec_devs
= ARRAY_SIZE(s5m8767_devs
);
350 sec_devs
= s2dos05_devs
;
351 num_sec_devs
= ARRAY_SIZE(s2dos05_devs
);
354 sec_devs
= s2mpa01_devs
;
355 num_sec_devs
= ARRAY_SIZE(s2mpa01_devs
);
358 sec_devs
= s2mps11_devs
;
359 num_sec_devs
= ARRAY_SIZE(s2mps11_devs
);
362 sec_devs
= s2mps13_devs
;
363 num_sec_devs
= ARRAY_SIZE(s2mps13_devs
);
366 sec_devs
= s2mps14_devs
;
367 num_sec_devs
= ARRAY_SIZE(s2mps14_devs
);
370 sec_devs
= s2mps15_devs
;
371 num_sec_devs
= ARRAY_SIZE(s2mps15_devs
);
374 sec_devs
= s2mpu02_devs
;
375 num_sec_devs
= ARRAY_SIZE(s2mpu02_devs
);
378 dev_err(&i2c
->dev
, "Unsupported device type (%lu)\n",
379 sec_pmic
->device_type
);
382 ret
= devm_mfd_add_devices(sec_pmic
->dev
, -1, sec_devs
, num_sec_devs
,
387 sec_pmic_configure(sec_pmic
);
388 sec_pmic_dump_rev(sec_pmic
);
393 static void sec_pmic_shutdown(struct i2c_client
*i2c
)
395 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
396 unsigned int reg
, mask
;
398 if (!sec_pmic
->pdata
->manual_poweroff
)
401 switch (sec_pmic
->device_type
) {
403 reg
= S2MPS11_REG_CTRL1
;
404 mask
= S2MPS11_CTRL1_PWRHOLD_MASK
;
408 * Currently only one board with S2MPS11 needs this, so just
411 dev_warn(sec_pmic
->dev
,
412 "Unsupported device %lu for manual power off\n",
413 sec_pmic
->device_type
);
417 regmap_update_bits(sec_pmic
->regmap_pmic
, reg
, mask
, 0);
420 static int sec_pmic_suspend(struct device
*dev
)
422 struct i2c_client
*i2c
= to_i2c_client(dev
);
423 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
425 if (device_may_wakeup(dev
))
426 enable_irq_wake(sec_pmic
->irq
);
428 * PMIC IRQ must be disabled during suspend for RTC alarm
430 * When device is woken up from suspend, an
431 * interrupt occurs before resuming I2C bus controller.
432 * The interrupt is handled by regmap_irq_thread which tries
433 * to read RTC registers. This read fails (I2C is still
434 * suspended) and RTC Alarm interrupt is disabled.
436 disable_irq(sec_pmic
->irq
);
441 static int sec_pmic_resume(struct device
*dev
)
443 struct i2c_client
*i2c
= to_i2c_client(dev
);
444 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
446 if (device_may_wakeup(dev
))
447 disable_irq_wake(sec_pmic
->irq
);
448 enable_irq(sec_pmic
->irq
);
453 static DEFINE_SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops
,
454 sec_pmic_suspend
, sec_pmic_resume
);
456 static struct i2c_driver sec_pmic_driver
= {
459 .pm
= pm_sleep_ptr(&sec_pmic_pm_ops
),
460 .of_match_table
= sec_dt_match
,
462 .probe
= sec_pmic_probe
,
463 .shutdown
= sec_pmic_shutdown
,
465 module_i2c_driver(sec_pmic_driver
);
467 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
468 MODULE_DESCRIPTION("Core support for the S5M MFD");
469 MODULE_LICENSE("GPL");