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/rtc.h>
29 #include <linux/mfd/samsung/s2mpa01.h>
30 #include <linux/mfd/samsung/s2mps11.h>
31 #include <linux/mfd/samsung/s2mps14.h>
32 #include <linux/mfd/samsung/s5m8763.h>
33 #include <linux/mfd/samsung/s5m8767.h>
34 #include <linux/regmap.h>
36 static const struct mfd_cell s5m8751_devs
[] = {
38 .name
= "s5m8751-pmic",
40 .name
= "s5m-charger",
42 .name
= "s5m8751-codec",
46 static const struct mfd_cell s5m8763_devs
[] = {
48 .name
= "s5m8763-pmic",
52 .name
= "s5m-charger",
56 static const struct mfd_cell s5m8767_devs
[] = {
58 .name
= "s5m8767-pmic",
62 .name
= "s5m8767-clk",
63 .of_compatible
= "samsung,s5m8767-clk",
67 static const struct mfd_cell s2mps11_devs
[] = {
69 .name
= "s2mps11-pmic",
71 .name
= "s2mps11-clk",
72 .of_compatible
= "samsung,s2mps11-clk",
76 static const struct mfd_cell s2mps14_devs
[] = {
78 .name
= "s2mps14-pmic",
80 .name
= "s2mps14-rtc",
82 .name
= "s2mps14-clk",
83 .of_compatible
= "samsung,s2mps14-clk",
87 static const struct mfd_cell s2mpa01_devs
[] = {
89 .name
= "s2mpa01-pmic",
94 static struct of_device_id sec_dt_match
[] = {
95 { .compatible
= "samsung,s5m8767-pmic",
96 .data
= (void *)S5M8767X
,
98 .compatible
= "samsung,s2mps11-pmic",
99 .data
= (void *)S2MPS11X
,
101 .compatible
= "samsung,s2mps14-pmic",
102 .data
= (void *)S2MPS14X
,
104 .compatible
= "samsung,s2mpa01-pmic",
105 .data
= (void *)S2MPA01
,
112 static bool s2mpa01_volatile(struct device
*dev
, unsigned int reg
)
115 case S2MPA01_REG_INT1M
:
116 case S2MPA01_REG_INT2M
:
117 case S2MPA01_REG_INT3M
:
124 static bool s2mps11_volatile(struct device
*dev
, unsigned int reg
)
127 case S2MPS11_REG_INT1M
:
128 case S2MPS11_REG_INT2M
:
129 case S2MPS11_REG_INT3M
:
136 static bool s5m8763_volatile(struct device
*dev
, unsigned int reg
)
139 case S5M8763_REG_IRQM1
:
140 case S5M8763_REG_IRQM2
:
141 case S5M8763_REG_IRQM3
:
142 case S5M8763_REG_IRQM4
:
149 static const struct regmap_config sec_regmap_config
= {
154 static const struct regmap_config s2mpa01_regmap_config
= {
158 .max_register
= S2MPA01_REG_LDO_OVCB4
,
159 .volatile_reg
= s2mpa01_volatile
,
160 .cache_type
= REGCACHE_FLAT
,
163 static const struct regmap_config s2mps11_regmap_config
= {
167 .max_register
= S2MPS11_REG_L38CTRL
,
168 .volatile_reg
= s2mps11_volatile
,
169 .cache_type
= REGCACHE_FLAT
,
172 static const struct regmap_config s2mps14_regmap_config
= {
176 .max_register
= S2MPS14_REG_LDODSCH3
,
177 .volatile_reg
= s2mps11_volatile
,
178 .cache_type
= REGCACHE_FLAT
,
181 static const struct regmap_config s5m8763_regmap_config
= {
185 .max_register
= S5M8763_REG_LBCNFG2
,
186 .volatile_reg
= s5m8763_volatile
,
187 .cache_type
= REGCACHE_FLAT
,
190 static const struct regmap_config s5m8767_regmap_config
= {
194 .max_register
= S5M8767_REG_LDO28CTRL
,
195 .volatile_reg
= s2mps11_volatile
,
196 .cache_type
= REGCACHE_FLAT
,
199 static const struct regmap_config s5m_rtc_regmap_config
= {
203 .max_register
= SEC_RTC_REG_MAX
,
206 static const struct regmap_config s2mps14_rtc_regmap_config
= {
210 .max_register
= S2MPS_RTC_REG_MAX
,
215 * Only the common platform data elements for s5m8767 are parsed here from the
216 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
217 * others have to parse their own platform data elements from device tree.
219 * The s5m8767 platform data structure is instantiated here and the drivers for
220 * the sub-modules need not instantiate another instance while parsing their
223 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
226 struct sec_platform_data
*pd
;
228 pd
= devm_kzalloc(dev
, sizeof(*pd
), GFP_KERNEL
);
230 dev_err(dev
, "could not allocate memory for pdata\n");
231 return ERR_PTR(-ENOMEM
);
235 * ToDo: the 'wakeup' member in the platform data is more of a linux
236 * specfic information. Hence, there is no binding for that yet and
243 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
250 static inline unsigned long sec_i2c_get_driver_data(struct i2c_client
*i2c
,
251 const struct i2c_device_id
*id
)
254 if (i2c
->dev
.of_node
) {
255 const struct of_device_id
*match
;
256 match
= of_match_node(sec_dt_match
, i2c
->dev
.of_node
);
257 return (unsigned long)match
->data
;
260 return id
->driver_data
;
263 static int sec_pmic_probe(struct i2c_client
*i2c
,
264 const struct i2c_device_id
*id
)
266 struct sec_platform_data
*pdata
= dev_get_platdata(&i2c
->dev
);
267 const struct regmap_config
*regmap
, *regmap_rtc
;
268 struct sec_pmic_dev
*sec_pmic
;
271 sec_pmic
= devm_kzalloc(&i2c
->dev
, sizeof(struct sec_pmic_dev
),
273 if (sec_pmic
== NULL
)
276 i2c_set_clientdata(i2c
, sec_pmic
);
277 sec_pmic
->dev
= &i2c
->dev
;
279 sec_pmic
->irq
= i2c
->irq
;
280 sec_pmic
->type
= sec_i2c_get_driver_data(i2c
, id
);
282 if (sec_pmic
->dev
->of_node
) {
283 pdata
= sec_pmic_i2c_parse_dt_pdata(sec_pmic
->dev
);
285 ret
= PTR_ERR(pdata
);
288 pdata
->device_type
= sec_pmic
->type
;
291 sec_pmic
->device_type
= pdata
->device_type
;
292 sec_pmic
->ono
= pdata
->ono
;
293 sec_pmic
->irq_base
= pdata
->irq_base
;
294 sec_pmic
->wakeup
= pdata
->wakeup
;
295 sec_pmic
->pdata
= pdata
;
298 switch (sec_pmic
->device_type
) {
300 regmap
= &s2mpa01_regmap_config
;
302 * The rtc-s5m driver does not support S2MPA01 and there
303 * is no mfd_cell for S2MPA01 RTC device.
304 * However we must pass something to devm_regmap_init_i2c()
305 * so use S5M-like regmap config even though it wouldn't work.
307 regmap_rtc
= &s5m_rtc_regmap_config
;
310 regmap
= &s2mps11_regmap_config
;
312 * The rtc-s5m driver does not support S2MPS11 and there
313 * is no mfd_cell for S2MPS11 RTC device.
314 * However we must pass something to devm_regmap_init_i2c()
315 * so use S5M-like regmap config even though it wouldn't work.
317 regmap_rtc
= &s5m_rtc_regmap_config
;
320 regmap
= &s2mps14_regmap_config
;
321 regmap_rtc
= &s2mps14_rtc_regmap_config
;
324 regmap
= &s5m8763_regmap_config
;
325 regmap_rtc
= &s5m_rtc_regmap_config
;
328 regmap
= &s5m8767_regmap_config
;
329 regmap_rtc
= &s5m_rtc_regmap_config
;
332 regmap
= &sec_regmap_config
;
333 regmap_rtc
= &s5m_rtc_regmap_config
;
337 sec_pmic
->regmap_pmic
= devm_regmap_init_i2c(i2c
, regmap
);
338 if (IS_ERR(sec_pmic
->regmap_pmic
)) {
339 ret
= PTR_ERR(sec_pmic
->regmap_pmic
);
340 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
345 sec_pmic
->rtc
= i2c_new_dummy(i2c
->adapter
, RTC_I2C_ADDR
);
346 if (!sec_pmic
->rtc
) {
347 dev_err(&i2c
->dev
, "Failed to allocate I2C for RTC\n");
350 i2c_set_clientdata(sec_pmic
->rtc
, sec_pmic
);
352 sec_pmic
->regmap_rtc
= devm_regmap_init_i2c(sec_pmic
->rtc
, regmap_rtc
);
353 if (IS_ERR(sec_pmic
->regmap_rtc
)) {
354 ret
= PTR_ERR(sec_pmic
->regmap_rtc
);
355 dev_err(&i2c
->dev
, "Failed to allocate RTC register map: %d\n",
360 if (pdata
&& pdata
->cfg_pmic_irq
)
361 pdata
->cfg_pmic_irq();
363 sec_irq_init(sec_pmic
);
365 pm_runtime_set_active(sec_pmic
->dev
);
367 switch (sec_pmic
->device_type
) {
369 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8751_devs
,
370 ARRAY_SIZE(s5m8751_devs
), NULL
, 0, NULL
);
373 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8763_devs
,
374 ARRAY_SIZE(s5m8763_devs
), NULL
, 0, NULL
);
377 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8767_devs
,
378 ARRAY_SIZE(s5m8767_devs
), NULL
, 0, NULL
);
381 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s2mpa01_devs
,
382 ARRAY_SIZE(s2mpa01_devs
), NULL
, 0, NULL
);
385 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s2mps11_devs
,
386 ARRAY_SIZE(s2mps11_devs
), NULL
, 0, NULL
);
389 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s2mps14_devs
,
390 ARRAY_SIZE(s2mps14_devs
), NULL
, 0, NULL
);
393 /* If this happens the probe function is problem */
400 device_init_wakeup(sec_pmic
->dev
, sec_pmic
->wakeup
);
405 sec_irq_exit(sec_pmic
);
407 i2c_unregister_device(sec_pmic
->rtc
);
411 static int sec_pmic_remove(struct i2c_client
*i2c
)
413 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
415 mfd_remove_devices(sec_pmic
->dev
);
416 sec_irq_exit(sec_pmic
);
417 i2c_unregister_device(sec_pmic
->rtc
);
421 #ifdef CONFIG_PM_SLEEP
422 static int sec_pmic_suspend(struct device
*dev
)
424 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
425 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
427 if (device_may_wakeup(dev
)) {
428 enable_irq_wake(sec_pmic
->irq
);
430 * PMIC IRQ must be disabled during suspend for RTC alarm
432 * When device is woken up from suspend by RTC Alarm, an
433 * interrupt occurs before resuming I2C bus controller.
434 * The interrupt is handled by regmap_irq_thread which tries
435 * to read RTC registers. This read fails (I2C is still
436 * suspended) and RTC Alarm interrupt is disabled.
438 disable_irq(sec_pmic
->irq
);
444 static int sec_pmic_resume(struct device
*dev
)
446 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
447 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
449 if (device_may_wakeup(dev
)) {
450 disable_irq_wake(sec_pmic
->irq
);
451 enable_irq(sec_pmic
->irq
);
456 #endif /* CONFIG_PM_SLEEP */
458 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops
, sec_pmic_suspend
, sec_pmic_resume
);
460 static const struct i2c_device_id sec_pmic_id
[] = {
464 MODULE_DEVICE_TABLE(i2c
, sec_pmic_id
);
466 static struct i2c_driver sec_pmic_driver
= {
469 .owner
= THIS_MODULE
,
470 .pm
= &sec_pmic_pm_ops
,
471 .of_match_table
= of_match_ptr(sec_dt_match
),
473 .probe
= sec_pmic_probe
,
474 .remove
= sec_pmic_remove
,
475 .id_table
= sec_pmic_id
,
478 static int __init
sec_pmic_init(void)
480 return i2c_add_driver(&sec_pmic_driver
);
483 subsys_initcall(sec_pmic_init
);
485 static void __exit
sec_pmic_exit(void)
487 i2c_del_driver(&sec_pmic_driver
);
489 module_exit(sec_pmic_exit
);
491 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
492 MODULE_DESCRIPTION("Core support for the S5M MFD");
493 MODULE_LICENSE("GPL");