4 * Copyright (c) 2011 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>
20 #include <linux/interrupt.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/mutex.h>
23 #include <linux/mfd/core.h>
24 #include <linux/mfd/s5m87xx/s5m-core.h>
25 #include <linux/mfd/s5m87xx/s5m-pmic.h>
26 #include <linux/mfd/s5m87xx/s5m-rtc.h>
27 #include <linux/regmap.h>
30 static struct of_device_id __devinitdata s5m87xx_pmic_dt_match[] = {
31 {.compatible = "samsung,s5m8767-pmic"},
36 static struct mfd_cell s5m8751_devs[] = {
38 .name = "s5m8751-pmic",
40 .name = "s5m-charger",
42 .name = "s5m8751-codec",
46 static struct mfd_cell s5m8763_devs[] = {
48 .name = "s5m8763-pmic",
52 .name = "s5m-charger",
56 static struct mfd_cell s5m8767_devs[] = {
58 .name = "s5m8767-pmic",
64 int s5m_reg_read(struct s5m87xx_dev *s5m87xx, u8 reg, void *dest)
66 return regmap_read(s5m87xx->regmap, reg, dest);
68 EXPORT_SYMBOL_GPL(s5m_reg_read);
70 int s5m_bulk_read(struct s5m87xx_dev *s5m87xx, u8 reg, int count, u8 *buf)
72 return regmap_bulk_read(s5m87xx->regmap, reg, buf, count);
74 EXPORT_SYMBOL_GPL(s5m_bulk_read);
76 int s5m_reg_write(struct s5m87xx_dev *s5m87xx, u8 reg, u8 value)
78 return regmap_write(s5m87xx->regmap, reg, value);
80 EXPORT_SYMBOL_GPL(s5m_reg_write);
82 int s5m_bulk_write(struct s5m87xx_dev *s5m87xx, u8 reg, int count, u8 *buf)
84 return regmap_raw_write(s5m87xx->regmap, reg, buf, count);
86 EXPORT_SYMBOL_GPL(s5m_bulk_write);
88 int s5m_reg_update(struct s5m87xx_dev *s5m87xx, u8 reg, u8 val, u8 mask)
90 return regmap_update_bits(s5m87xx->regmap, reg, mask, val);
92 EXPORT_SYMBOL_GPL(s5m_reg_update);
94 static struct regmap_config s5m_regmap_config = {
99 static inline int s5m87xx_i2c_get_driver_data(struct i2c_client *i2c,
100 const struct i2c_device_id *id)
103 if (i2c->dev.of_node) {
104 const struct of_device_id *match;
105 match = of_match_node(s5m87xx_pmic_dt_match, i2c->dev.of_node);
106 return (int)match->data;
109 return (int)id->driver_data;
113 static struct s5m_platform_data *s5m87xx_i2c_parse_dt_pdata(struct device *dev)
115 struct s5m_platform_data *pd;
117 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
119 dev_err(dev, "could not allocate memory for pdata\n");
120 return ERR_PTR(-ENOMEM);
123 if (of_property_read_u32(dev->of_node, "s5m-core,device_type",
125 dev_warn(dev, "no OF device_type property");
127 dev_dbg(dev, "OF device_type property = %u", pd->device_type);
132 static struct s5m_platform_data *s5m8767_i2c_parse_dt_pdata(struct device *dev)
138 static int s5m87xx_i2c_probe(struct i2c_client *i2c,
139 const struct i2c_device_id *id)
141 struct s5m_platform_data *pdata = i2c->dev.platform_data;
142 struct s5m87xx_dev *s5m87xx;
145 s5m87xx = devm_kzalloc(&i2c->dev, sizeof(struct s5m87xx_dev),
150 i2c_set_clientdata(i2c, s5m87xx);
151 s5m87xx->dev = &i2c->dev;
153 s5m87xx->irq = i2c->irq;
154 s5m87xx->type = s5m87xx_i2c_get_driver_data(i2c, id);
156 if (s5m87xx->dev->of_node) {
157 pdata = s5m87xx_i2c_parse_dt_pdata(s5m87xx->dev);
159 ret = PTR_ERR(pdata);
163 s5m87xx->pdata = pdata;
167 dev_warn(s5m87xx->dev, "No platform data found\n");
171 s5m87xx->device_type = pdata->device_type;
172 /* TODO(tbroch): address whether we want this addtional interrupt node
173 and add it to DT parsing if yes.
175 s5m87xx->ono = pdata->ono;
176 /* TODO(tbroch): remove hack below and parse irq_base via DT */
177 s5m87xx->irq_base = pdata->irq_base = MAX77686_IRQ_BASE;
179 s5m87xx->wakeup = i2c->flags & I2C_CLIENT_WAKE;
181 s5m87xx->wakeup = pdata->wakeup;
184 s5m87xx->regmap = regmap_init_i2c(i2c, &s5m_regmap_config);
185 if (IS_ERR(s5m87xx->regmap)) {
186 ret = PTR_ERR(s5m87xx->regmap);
187 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
192 s5m87xx->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
193 i2c_set_clientdata(s5m87xx->rtc, s5m87xx);
195 if (pdata && pdata->cfg_pmic_irq)
196 pdata->cfg_pmic_irq();
198 s5m_irq_init(s5m87xx);
200 pm_runtime_set_active(s5m87xx->dev);
202 switch (s5m87xx->device_type) {
204 ret = mfd_add_devices(s5m87xx->dev, -1, s5m8751_devs,
205 ARRAY_SIZE(s5m8751_devs), NULL, 0);
208 ret = mfd_add_devices(s5m87xx->dev, -1, s5m8763_devs,
209 ARRAY_SIZE(s5m8763_devs), NULL, 0);
212 ret = mfd_add_devices(s5m87xx->dev, -1, s5m8767_devs,
213 ARRAY_SIZE(s5m8767_devs), NULL, 0);
216 /* If this happens the probe function is problem */
226 mfd_remove_devices(s5m87xx->dev);
227 s5m_irq_exit(s5m87xx);
228 i2c_unregister_device(s5m87xx->rtc);
229 regmap_exit(s5m87xx->regmap);
233 static int s5m87xx_i2c_remove(struct i2c_client *i2c)
235 struct s5m87xx_dev *s5m87xx = i2c_get_clientdata(i2c);
237 mfd_remove_devices(s5m87xx->dev);
238 s5m_irq_exit(s5m87xx);
239 i2c_unregister_device(s5m87xx->rtc);
240 regmap_exit(s5m87xx->regmap);
244 static const struct i2c_device_id s5m87xx_i2c_id[] = {
248 MODULE_DEVICE_TABLE(i2c, s5m87xx_i2c_id);
250 static struct i2c_driver s5m87xx_i2c_driver = {
253 .owner = THIS_MODULE,
254 .of_match_table = of_match_ptr(s5m87xx_pmic_dt_match),
256 .probe = s5m87xx_i2c_probe,
257 .remove = s5m87xx_i2c_remove,
258 .id_table = s5m87xx_i2c_id,
261 static int __init s5m87xx_i2c_init(void)
263 return i2c_add_driver(&s5m87xx_i2c_driver);
266 subsys_initcall(s5m87xx_i2c_init);
268 static void __exit s5m87xx_i2c_exit(void)
270 i2c_del_driver(&s5m87xx_i2c_driver);
272 module_exit(s5m87xx_i2c_exit);
274 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
275 MODULE_DESCRIPTION("Core support for the S5M MFD");
276 MODULE_LICENSE("GPL");