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/s2mps11.h>
30 #include <linux/mfd/samsung/s5m8763.h>
31 #include <linux/mfd/samsung/s5m8767.h>
32 #include <linux/regmap.h>
34 static const struct mfd_cell s5m8751_devs
[] = {
36 .name
= "s5m8751-pmic",
38 .name
= "s5m-charger",
40 .name
= "s5m8751-codec",
44 static const struct mfd_cell s5m8763_devs
[] = {
46 .name
= "s5m8763-pmic",
50 .name
= "s5m-charger",
54 static const struct mfd_cell s5m8767_devs
[] = {
56 .name
= "s5m8767-pmic",
60 .name
= "s5m8767-clk",
64 static const struct mfd_cell s2mps11_devs
[] = {
66 .name
= "s2mps11-pmic",
68 .name
= "s2mps11-clk",
73 static struct of_device_id sec_dt_match
[] = {
74 { .compatible
= "samsung,s5m8767-pmic",
75 .data
= (void *)S5M8767X
,
77 { .compatible
= "samsung,s2mps11-pmic",
78 .data
= (void *)S2MPS11X
,
84 static bool s2mps11_volatile(struct device
*dev
, unsigned int reg
)
87 case S2MPS11_REG_INT1M
:
88 case S2MPS11_REG_INT2M
:
89 case S2MPS11_REG_INT3M
:
96 static bool s5m8763_volatile(struct device
*dev
, unsigned int reg
)
99 case S5M8763_REG_IRQM1
:
100 case S5M8763_REG_IRQM2
:
101 case S5M8763_REG_IRQM3
:
102 case S5M8763_REG_IRQM4
:
109 static const struct regmap_config sec_regmap_config
= {
114 static const struct regmap_config s2mps11_regmap_config
= {
118 .max_register
= S2MPS11_REG_L38CTRL
,
119 .volatile_reg
= s2mps11_volatile
,
120 .cache_type
= REGCACHE_FLAT
,
123 static const struct regmap_config s5m8763_regmap_config
= {
127 .max_register
= S5M8763_REG_LBCNFG2
,
128 .volatile_reg
= s5m8763_volatile
,
129 .cache_type
= REGCACHE_FLAT
,
132 static const struct regmap_config s5m8767_regmap_config
= {
136 .max_register
= S5M8767_REG_LDO28CTRL
,
137 .volatile_reg
= s2mps11_volatile
,
138 .cache_type
= REGCACHE_FLAT
,
141 static const struct regmap_config sec_rtc_regmap_config
= {
148 * Only the common platform data elements for s5m8767 are parsed here from the
149 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
150 * others have to parse their own platform data elements from device tree.
152 * The s5m8767 platform data structure is instantiated here and the drivers for
153 * the sub-modules need not instantiate another instance while parsing their
156 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
159 struct sec_platform_data
*pd
;
161 pd
= devm_kzalloc(dev
, sizeof(*pd
), GFP_KERNEL
);
163 dev_err(dev
, "could not allocate memory for pdata\n");
164 return ERR_PTR(-ENOMEM
);
168 * ToDo: the 'wakeup' member in the platform data is more of a linux
169 * specfic information. Hence, there is no binding for that yet and
176 static struct sec_platform_data
*sec_pmic_i2c_parse_dt_pdata(
183 static inline int sec_i2c_get_driver_data(struct i2c_client
*i2c
,
184 const struct i2c_device_id
*id
)
187 if (i2c
->dev
.of_node
) {
188 const struct of_device_id
*match
;
189 match
= of_match_node(sec_dt_match
, i2c
->dev
.of_node
);
190 return (int)match
->data
;
193 return (int)id
->driver_data
;
196 static int sec_pmic_probe(struct i2c_client
*i2c
,
197 const struct i2c_device_id
*id
)
199 struct sec_platform_data
*pdata
= dev_get_platdata(&i2c
->dev
);
200 const struct regmap_config
*regmap
;
201 struct sec_pmic_dev
*sec_pmic
;
204 sec_pmic
= devm_kzalloc(&i2c
->dev
, sizeof(struct sec_pmic_dev
),
206 if (sec_pmic
== NULL
)
209 i2c_set_clientdata(i2c
, sec_pmic
);
210 sec_pmic
->dev
= &i2c
->dev
;
212 sec_pmic
->irq
= i2c
->irq
;
213 sec_pmic
->type
= sec_i2c_get_driver_data(i2c
, id
);
215 if (sec_pmic
->dev
->of_node
) {
216 pdata
= sec_pmic_i2c_parse_dt_pdata(sec_pmic
->dev
);
218 ret
= PTR_ERR(pdata
);
221 pdata
->device_type
= sec_pmic
->type
;
224 sec_pmic
->device_type
= pdata
->device_type
;
225 sec_pmic
->ono
= pdata
->ono
;
226 sec_pmic
->irq_base
= pdata
->irq_base
;
227 sec_pmic
->wakeup
= pdata
->wakeup
;
228 sec_pmic
->pdata
= pdata
;
231 switch (sec_pmic
->device_type
) {
233 regmap
= &s2mps11_regmap_config
;
236 regmap
= &s5m8763_regmap_config
;
239 regmap
= &s5m8767_regmap_config
;
242 regmap
= &sec_regmap_config
;
246 sec_pmic
->regmap_pmic
= devm_regmap_init_i2c(i2c
, regmap
);
247 if (IS_ERR(sec_pmic
->regmap_pmic
)) {
248 ret
= PTR_ERR(sec_pmic
->regmap_pmic
);
249 dev_err(&i2c
->dev
, "Failed to allocate register map: %d\n",
254 sec_pmic
->rtc
= i2c_new_dummy(i2c
->adapter
, RTC_I2C_ADDR
);
255 i2c_set_clientdata(sec_pmic
->rtc
, sec_pmic
);
257 sec_pmic
->regmap_rtc
= devm_regmap_init_i2c(sec_pmic
->rtc
,
258 &sec_rtc_regmap_config
);
259 if (IS_ERR(sec_pmic
->regmap_rtc
)) {
260 ret
= PTR_ERR(sec_pmic
->regmap_rtc
);
261 dev_err(&i2c
->dev
, "Failed to allocate RTC register map: %d\n",
266 if (pdata
&& pdata
->cfg_pmic_irq
)
267 pdata
->cfg_pmic_irq();
269 sec_irq_init(sec_pmic
);
271 pm_runtime_set_active(sec_pmic
->dev
);
273 switch (sec_pmic
->device_type
) {
275 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8751_devs
,
276 ARRAY_SIZE(s5m8751_devs
), NULL
, 0, NULL
);
279 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8763_devs
,
280 ARRAY_SIZE(s5m8763_devs
), NULL
, 0, NULL
);
283 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s5m8767_devs
,
284 ARRAY_SIZE(s5m8767_devs
), NULL
, 0, NULL
);
287 ret
= mfd_add_devices(sec_pmic
->dev
, -1, s2mps11_devs
,
288 ARRAY_SIZE(s2mps11_devs
), NULL
, 0, NULL
);
291 /* If this happens the probe function is problem */
298 device_init_wakeup(sec_pmic
->dev
, sec_pmic
->wakeup
);
303 sec_irq_exit(sec_pmic
);
304 i2c_unregister_device(sec_pmic
->rtc
);
308 static int sec_pmic_remove(struct i2c_client
*i2c
)
310 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
312 mfd_remove_devices(sec_pmic
->dev
);
313 sec_irq_exit(sec_pmic
);
314 i2c_unregister_device(sec_pmic
->rtc
);
318 static int sec_pmic_suspend(struct device
*dev
)
320 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
321 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
323 if (device_may_wakeup(dev
)) {
324 enable_irq_wake(sec_pmic
->irq
);
326 * PMIC IRQ must be disabled during suspend for RTC alarm
328 * When device is woken up from suspend by RTC Alarm, an
329 * interrupt occurs before resuming I2C bus controller.
330 * The interrupt is handled by regmap_irq_thread which tries
331 * to read RTC registers. This read fails (I2C is still
332 * suspended) and RTC Alarm interrupt is disabled.
334 disable_irq(sec_pmic
->irq
);
340 static int sec_pmic_resume(struct device
*dev
)
342 struct i2c_client
*i2c
= container_of(dev
, struct i2c_client
, dev
);
343 struct sec_pmic_dev
*sec_pmic
= i2c_get_clientdata(i2c
);
345 if (device_may_wakeup(dev
)) {
346 disable_irq_wake(sec_pmic
->irq
);
347 enable_irq(sec_pmic
->irq
);
353 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops
, sec_pmic_suspend
, sec_pmic_resume
);
355 static const struct i2c_device_id sec_pmic_id
[] = {
359 MODULE_DEVICE_TABLE(i2c
, sec_pmic_id
);
361 static struct i2c_driver sec_pmic_driver
= {
364 .owner
= THIS_MODULE
,
365 .pm
= &sec_pmic_pm_ops
,
366 .of_match_table
= of_match_ptr(sec_dt_match
),
368 .probe
= sec_pmic_probe
,
369 .remove
= sec_pmic_remove
,
370 .id_table
= sec_pmic_id
,
373 static int __init
sec_pmic_init(void)
375 return i2c_add_driver(&sec_pmic_driver
);
378 subsys_initcall(sec_pmic_init
);
380 static void __exit
sec_pmic_exit(void)
382 i2c_del_driver(&sec_pmic_driver
);
384 module_exit(sec_pmic_exit
);
386 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
387 MODULE_DESCRIPTION("Core support for the S5M MFD");
388 MODULE_LICENSE("GPL");