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/s2mps14.h>
31 #include <linux/mfd/samsung/s5m8763.h>
32 #include <linux/mfd/samsung/s5m8767.h>
33 #include <linux/regmap.h>
35 static const struct mfd_cell s5m8751_devs
[] = {
37 .name
= "s5m8751-pmic",
39 .name
= "s5m-charger",
41 .name
= "s5m8751-codec",
45 static const struct mfd_cell s5m8763_devs
[] = {
47 .name
= "s5m8763-pmic",
51 .name
= "s5m-charger",
55 static const struct mfd_cell s5m8767_devs
[] = {
57 .name
= "s5m8767-pmic",
61 .name
= "s5m8767-clk",
62 .of_compatible
= "samsung,s5m8767-clk",
66 static const struct mfd_cell s2mps11_devs
[] = {
68 .name
= "s2mps11-pmic",
70 .name
= "s2mps11-clk",
71 .of_compatible
= "samsung,s2mps11-clk",
75 static const struct mfd_cell s2mps14_devs
[] = {
77 .name
= "s2mps14-pmic",
79 .name
= "s2mps14-rtc",
81 .name
= "s2mps14-clk",
82 .of_compatible
= "samsung,s2mps14-clk",
86 static const struct mfd_cell s2mpa01_devs
[] = {
88 .name
= "s2mpa01-pmic",
93 static const struct of_device_id sec_dt_match
[] = {
94 { .compatible
= "samsung,s5m8767-pmic",
95 .data
= (void *)S5M8767X
,
97 .compatible
= "samsung,s2mps11-pmic",
98 .data
= (void *)S2MPS11X
,
100 .compatible
= "samsung,s2mps14-pmic",
101 .data
= (void *)S2MPS14X
,
103 .compatible
= "samsung,s2mpa01-pmic",
104 .data
= (void *)S2MPA01
,
111 static bool s2mpa01_volatile(struct device
*dev
, unsigned int reg
)
114 case S2MPA01_REG_INT1M
:
115 case S2MPA01_REG_INT2M
:
116 case S2MPA01_REG_INT3M
:
123 static bool s2mps11_volatile(struct device
*dev
, unsigned int reg
)
126 case S2MPS11_REG_INT1M
:
127 case S2MPS11_REG_INT2M
:
128 case S2MPS11_REG_INT3M
:
135 static bool s5m8763_volatile(struct device
*dev
, unsigned int reg
)
138 case S5M8763_REG_IRQM1
:
139 case S5M8763_REG_IRQM2
:
140 case S5M8763_REG_IRQM3
:
141 case S5M8763_REG_IRQM4
:
148 static const struct regmap_config sec_regmap_config
= {
153 static const struct regmap_config s2mpa01_regmap_config
= {
157 .max_register
= S2MPA01_REG_LDO_OVCB4
,
158 .volatile_reg
= s2mpa01_volatile
,
159 .cache_type
= REGCACHE_FLAT
,
162 static const struct regmap_config s2mps11_regmap_config
= {
166 .max_register
= S2MPS11_REG_L38CTRL
,
167 .volatile_reg
= s2mps11_volatile
,
168 .cache_type
= REGCACHE_FLAT
,
171 static const struct regmap_config s2mps14_regmap_config
= {
175 .max_register
= S2MPS14_REG_LDODSCH3
,
176 .volatile_reg
= s2mps11_volatile
,
177 .cache_type
= REGCACHE_FLAT
,
180 static const struct regmap_config s5m8763_regmap_config
= {
184 .max_register
= S5M8763_REG_LBCNFG2
,
185 .volatile_reg
= s5m8763_volatile
,
186 .cache_type
= REGCACHE_FLAT
,
189 static const struct regmap_config s5m8767_regmap_config
= {
193 .max_register
= S5M8767_REG_LDO28CTRL
,
194 .volatile_reg
= s2mps11_volatile
,
195 .cache_type
= REGCACHE_FLAT
,
200 * Only the common platform data elements for s5m8767 are parsed here from the
201 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
202 * others have to parse their own platform data elements from device tree.
204 * The s5m8767 platform data structure is instantiated here and the drivers for
205 * the sub-modules need not instantiate another instance while parsing their
208 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
211 struct sec_platform_data
*pd
;
213 pd
= devm_kzalloc(dev
, sizeof(*pd
), GFP_KERNEL
);
215 dev_err(dev
, "could not allocate memory for pdata\n");
216 return ERR_PTR(-ENOMEM
);
220 * ToDo: the 'wakeup' member in the platform data is more of a linux
221 * specfic information. Hence, there is no binding for that yet and
228 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
235 static inline unsigned long sec_i2c_get_driver_data(struct i2c_client
*i2c
,
236 const struct i2c_device_id
*id
)
239 if (i2c
->dev
.of_node
) {
240 const struct of_device_id
*match
;
241 match
= of_match_node(sec_dt_match
, i2c
->dev
.of_node
);
242 return (unsigned long)match
->data
;
245 return id
->driver_data
;
248 static int sec_pmic_probe(struct i2c_client
*i2c
,
249 const struct i2c_device_id
*id
)
251 struct sec_platform_data
*pdata
= dev_get_platdata(&i2c
->dev
);
252 const struct regmap_config
*regmap
;
253 struct sec_pmic_dev
*sec_pmic
;
254 unsigned long device_type
;
257 sec_pmic
= devm_kzalloc(&i2c
->dev
, sizeof(struct sec_pmic_dev
),
259 if (sec_pmic
== NULL
)
262 i2c_set_clientdata(i2c
, sec_pmic
);
263 sec_pmic
->dev
= &i2c
->dev
;
265 sec_pmic
->irq
= i2c
->irq
;
266 device_type
= sec_i2c_get_driver_data(i2c
, id
);
268 if (sec_pmic
->dev
->of_node
) {
269 pdata
= sec_pmic_i2c_parse_dt_pdata(sec_pmic
->dev
);
271 ret
= PTR_ERR(pdata
);
274 pdata
->device_type
= device_type
;
277 sec_pmic
->device_type
= pdata
->device_type
;
278 sec_pmic
->ono
= pdata
->ono
;
279 sec_pmic
->irq_base
= pdata
->irq_base
;
280 sec_pmic
->wakeup
= pdata
->wakeup
;
281 sec_pmic
->pdata
= pdata
;
284 switch (sec_pmic
->device_type
) {
286 regmap
= &s2mpa01_regmap_config
;
289 regmap
= &s2mps11_regmap_config
;
292 regmap
= &s2mps14_regmap_config
;
295 regmap
= &s5m8763_regmap_config
;
298 regmap
= &s5m8767_regmap_config
;
301 regmap
= &sec_regmap_config
;
305 sec_pmic
->regmap_pmic
= devm_regmap_init_i2c(i2c
, regmap
);
306 if (IS_ERR(sec_pmic
->regmap_pmic
)) {
307 ret
= PTR_ERR(sec_pmic
->regmap_pmic
);
308 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
313 if (pdata
&& pdata
->cfg_pmic_irq
)
314 pdata
->cfg_pmic_irq();
316 sec_irq_init(sec_pmic
);
318 pm_runtime_set_active(sec_pmic
->dev
);
320 switch (sec_pmic
->device_type
) {
322 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8751_devs
,
323 ARRAY_SIZE(s5m8751_devs
), NULL
, 0, NULL
);
326 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8763_devs
,
327 ARRAY_SIZE(s5m8763_devs
), NULL
, 0, NULL
);
330 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8767_devs
,
331 ARRAY_SIZE(s5m8767_devs
), NULL
, 0, NULL
);
334 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s2mpa01_devs
,
335 ARRAY_SIZE(s2mpa01_devs
), NULL
, 0, NULL
);
338 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s2mps11_devs
,
339 ARRAY_SIZE(s2mps11_devs
), NULL
, 0, NULL
);
342 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s2mps14_devs
,
343 ARRAY_SIZE(s2mps14_devs
), NULL
, 0, NULL
);
346 /* If this happens the probe function is problem */
353 device_init_wakeup(sec_pmic
->dev
, sec_pmic
->wakeup
);
358 sec_irq_exit(sec_pmic
);
362 static int sec_pmic_remove(struct i2c_client
*i2c
)
364 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
366 mfd_remove_devices(sec_pmic
->dev
);
367 sec_irq_exit(sec_pmic
);
371 #ifdef CONFIG_PM_SLEEP
372 static int sec_pmic_suspend(struct device
*dev
)
374 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
375 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
377 if (device_may_wakeup(dev
))
378 enable_irq_wake(sec_pmic
->irq
);
380 * PMIC IRQ must be disabled during suspend for RTC alarm
382 * When device is woken up from suspend, an
383 * interrupt occurs before resuming I2C bus controller.
384 * The interrupt is handled by regmap_irq_thread which tries
385 * to read RTC registers. This read fails (I2C is still
386 * suspended) and RTC Alarm interrupt is disabled.
388 disable_irq(sec_pmic
->irq
);
393 static int sec_pmic_resume(struct device
*dev
)
395 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
396 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
398 if (device_may_wakeup(dev
))
399 disable_irq_wake(sec_pmic
->irq
);
400 enable_irq(sec_pmic
->irq
);
404 #endif /* CONFIG_PM_SLEEP */
406 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops
, sec_pmic_suspend
, sec_pmic_resume
);
408 static const struct i2c_device_id sec_pmic_id
[] = {
412 MODULE_DEVICE_TABLE(i2c
, sec_pmic_id
);
414 static struct i2c_driver sec_pmic_driver
= {
417 .owner
= THIS_MODULE
,
418 .pm
= &sec_pmic_pm_ops
,
419 .of_match_table
= of_match_ptr(sec_dt_match
),
421 .probe
= sec_pmic_probe
,
422 .remove
= sec_pmic_remove
,
423 .id_table
= sec_pmic_id
,
426 static int __init
sec_pmic_init(void)
428 return i2c_add_driver(&sec_pmic_driver
);
431 subsys_initcall(sec_pmic_init
);
433 static void __exit
sec_pmic_exit(void)
435 i2c_del_driver(&sec_pmic_driver
);
437 module_exit(sec_pmic_exit
);
439 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
440 MODULE_DESCRIPTION("Core support for the S5M MFD");
441 MODULE_LICENSE("GPL");