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 int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
86 return regmap_read(sec_pmic->regmap_pmic, reg, dest);
88 EXPORT_SYMBOL_GPL(sec_reg_read);
90 int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
92 return regmap_bulk_read(sec_pmic->regmap_pmic, reg, buf, count);
94 EXPORT_SYMBOL_GPL(sec_bulk_read);
96 int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
98 return regmap_write(sec_pmic->regmap_pmic, reg, value);
100 EXPORT_SYMBOL_GPL(sec_reg_write);
102 int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
104 return regmap_raw_write(sec_pmic->regmap_pmic, reg, buf, count);
106 EXPORT_SYMBOL_GPL(sec_bulk_write);
108 int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
110 return regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, val);
112 EXPORT_SYMBOL_GPL(sec_reg_update);
114 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
117 case S2MPS11_REG_INT1M:
118 case S2MPS11_REG_INT2M:
119 case S2MPS11_REG_INT3M:
126 static bool s5m8763_volatile(struct device *dev, unsigned int reg)
129 case S5M8763_REG_IRQM1:
130 case S5M8763_REG_IRQM2:
131 case S5M8763_REG_IRQM3:
132 case S5M8763_REG_IRQM4:
139 static const struct regmap_config sec_regmap_config = {
144 static const struct regmap_config s2mps11_regmap_config = {
148 .max_register = S2MPS11_REG_L38CTRL,
149 .volatile_reg = s2mps11_volatile,
150 .cache_type = REGCACHE_FLAT,
153 static const struct regmap_config s5m8763_regmap_config = {
157 .max_register = S5M8763_REG_LBCNFG2,
158 .volatile_reg = s5m8763_volatile,
159 .cache_type = REGCACHE_FLAT,
162 static const struct regmap_config s5m8767_regmap_config = {
166 .max_register = S5M8767_REG_LDO28CTRL,
167 .volatile_reg = s2mps11_volatile,
168 .cache_type = REGCACHE_FLAT,
171 static const struct regmap_config sec_rtc_regmap_config = {
178 * Only the common platform data elements for s5m8767 are parsed here from the
179 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
180 * others have to parse their own platform data elements from device tree.
182 * The s5m8767 platform data structure is instantiated here and the drivers for
183 * the sub-modules need not instantiate another instance while parsing their
186 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
189 struct sec_platform_data *pd;
191 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
193 dev_err(dev, "could not allocate memory for pdata\n");
194 return ERR_PTR(-ENOMEM);
198 * ToDo: the 'wakeup' member in the platform data is more of a linux
199 * specfic information. Hence, there is no binding for that yet and
206 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
213 static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
214 const struct i2c_device_id *id)
217 if (i2c->dev.of_node) {
218 const struct of_device_id *match;
219 match = of_match_node(sec_dt_match, i2c->dev.of_node);
220 return (int)match->data;
223 return (int)id->driver_data;
226 static int sec_pmic_probe(struct i2c_client *i2c,
227 const struct i2c_device_id *id)
229 struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
230 const struct regmap_config *regmap;
231 struct sec_pmic_dev *sec_pmic;
234 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
236 if (sec_pmic == NULL)
239 i2c_set_clientdata(i2c, sec_pmic);
240 sec_pmic->dev = &i2c->dev;
242 sec_pmic->irq = i2c->irq;
243 sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
245 if (sec_pmic->dev->of_node) {
246 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
248 ret = PTR_ERR(pdata);
251 pdata->device_type = sec_pmic->type;
254 sec_pmic->device_type = pdata->device_type;
255 sec_pmic->ono = pdata->ono;
256 sec_pmic->irq_base = pdata->irq_base;
257 sec_pmic->wakeup = pdata->wakeup;
258 sec_pmic->pdata = pdata;
261 switch (sec_pmic->device_type) {
263 regmap = &s2mps11_regmap_config;
266 regmap = &s5m8763_regmap_config;
269 regmap = &s5m8767_regmap_config;
272 regmap = &sec_regmap_config;
276 sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
277 if (IS_ERR(sec_pmic->regmap_pmic)) {
278 ret = PTR_ERR(sec_pmic->regmap_pmic);
279 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
284 sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
285 i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
287 sec_pmic->regmap_rtc = devm_regmap_init_i2c(sec_pmic->rtc,
288 &sec_rtc_regmap_config);
289 if (IS_ERR(sec_pmic->regmap_rtc)) {
290 ret = PTR_ERR(sec_pmic->regmap_rtc);
291 dev_err(&i2c->dev, "Failed to allocate RTC register map: %d\n",
296 if (pdata && pdata->cfg_pmic_irq)
297 pdata->cfg_pmic_irq();
299 sec_irq_init(sec_pmic);
301 pm_runtime_set_active(sec_pmic->dev);
303 switch (sec_pmic->device_type) {
305 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
306 ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
309 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
310 ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
313 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
314 ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
317 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
318 ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
321 /* If this happens the probe function is problem */
328 device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
333 sec_irq_exit(sec_pmic);
334 i2c_unregister_device(sec_pmic->rtc);
338 static int sec_pmic_remove(struct i2c_client *i2c)
340 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
342 mfd_remove_devices(sec_pmic->dev);
343 sec_irq_exit(sec_pmic);
344 i2c_unregister_device(sec_pmic->rtc);
348 static int sec_pmic_suspend(struct device *dev)
350 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
351 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
353 if (device_may_wakeup(dev)) {
354 enable_irq_wake(sec_pmic->irq);
356 * PMIC IRQ must be disabled during suspend for RTC alarm
358 * When device is woken up from suspend by RTC Alarm, an
359 * interrupt occurs before resuming I2C bus controller.
360 * The interrupt is handled by regmap_irq_thread which tries
361 * to read RTC registers. This read fails (I2C is still
362 * suspended) and RTC Alarm interrupt is disabled.
364 disable_irq(sec_pmic->irq);
370 static int sec_pmic_resume(struct device *dev)
372 struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
373 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
375 if (device_may_wakeup(dev)) {
376 disable_irq_wake(sec_pmic->irq);
377 enable_irq(sec_pmic->irq);
383 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
385 static const struct i2c_device_id sec_pmic_id[] = {
389 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
391 static struct i2c_driver sec_pmic_driver = {
394 .owner = THIS_MODULE,
395 .pm = &sec_pmic_pm_ops,
396 .of_match_table = of_match_ptr(sec_dt_match),
398 .probe = sec_pmic_probe,
399 .remove = sec_pmic_remove,
400 .id_table = sec_pmic_id,
403 static int __init sec_pmic_init(void)
405 return i2c_add_driver(&sec_pmic_driver);
408 subsys_initcall(sec_pmic_init);
410 static void __exit sec_pmic_exit(void)
412 i2c_del_driver(&sec_pmic_driver);
414 module_exit(sec_pmic_exit);
417 MODULE_DESCRIPTION("Core support for the S5M MFD");
418 MODULE_LICENSE("GPL");