Merge branch 'fixes' of git://git.linaro.org/people/rmk/linux-arm
[linux-2.6.git] / drivers / mfd / sec-core.c
blob79767681483a65b78804e44263bfbba4ef135d2e
1 /*
2 * sec-core.c
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>
20 #include <linux/of_irq.h>
21 #include <linux/interrupt.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/mutex.h>
24 #include <linux/mfd/core.h>
25 #include <linux/mfd/samsung/core.h>
26 #include <linux/mfd/samsung/irq.h>
27 #include <linux/mfd/samsung/rtc.h>
28 #include <linux/mfd/samsung/s2mps11.h>
29 #include <linux/mfd/samsung/s5m8763.h>
30 #include <linux/mfd/samsung/s5m8767.h>
31 #include <linux/regmap.h>
33 static struct mfd_cell s5m8751_devs[] = {
35 .name = "s5m8751-pmic",
36 }, {
37 .name = "s5m-charger",
38 }, {
39 .name = "s5m8751-codec",
43 static struct mfd_cell s5m8763_devs[] = {
45 .name = "s5m8763-pmic",
46 }, {
47 .name = "s5m-rtc",
48 }, {
49 .name = "s5m-charger",
53 static struct mfd_cell s5m8767_devs[] = {
55 .name = "s5m8767-pmic",
56 }, {
57 .name = "s5m-rtc",
61 static struct mfd_cell s2mps11_devs[] = {
63 .name = "s2mps11-pmic",
67 #ifdef CONFIG_OF
68 static struct of_device_id sec_dt_match[] = {
69 { .compatible = "samsung,s5m8767-pmic",
70 .data = (void *)S5M8767X,
72 {},
74 #endif
76 int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
78 return regmap_read(sec_pmic->regmap, reg, dest);
80 EXPORT_SYMBOL_GPL(sec_reg_read);
82 int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
84 return regmap_bulk_read(sec_pmic->regmap, reg, buf, count);
86 EXPORT_SYMBOL_GPL(sec_bulk_read);
88 int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
90 return regmap_write(sec_pmic->regmap, reg, value);
92 EXPORT_SYMBOL_GPL(sec_reg_write);
94 int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
96 return regmap_raw_write(sec_pmic->regmap, reg, buf, count);
98 EXPORT_SYMBOL_GPL(sec_bulk_write);
100 int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
102 return regmap_update_bits(sec_pmic->regmap, reg, mask, val);
104 EXPORT_SYMBOL_GPL(sec_reg_update);
106 static struct regmap_config sec_regmap_config = {
107 .reg_bits = 8,
108 .val_bits = 8,
111 static struct regmap_config s2mps11_regmap_config = {
112 .reg_bits = 8,
113 .val_bits = 8,
115 .max_register = S2MPS11_REG_L38CTRL,
118 static struct regmap_config s5m8763_regmap_config = {
119 .reg_bits = 8,
120 .val_bits = 8,
122 .max_register = S5M8763_REG_LBCNFG2,
125 static struct regmap_config s5m8767_regmap_config = {
126 .reg_bits = 8,
127 .val_bits = 8,
129 .max_register = S5M8767_REG_LDO28CTRL,
132 #ifdef CONFIG_OF
134 * Only the common platform data elements for s5m8767 are parsed here from the
135 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
136 * others have to parse their own platform data elements from device tree.
138 * The s5m8767 platform data structure is instantiated here and the drivers for
139 * the sub-modules need not instantiate another instance while parsing their
140 * platform data.
142 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
143 struct device *dev)
145 struct sec_platform_data *pd;
147 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
148 if (!pd) {
149 dev_err(dev, "could not allocate memory for pdata\n");
150 return ERR_PTR(-ENOMEM);
154 * ToDo: the 'wakeup' member in the platform data is more of a linux
155 * specfic information. Hence, there is no binding for that yet and
156 * not parsed here.
159 return pd;
161 #else
162 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
163 struct device *dev)
165 return 0;
167 #endif
169 static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
170 const struct i2c_device_id *id)
172 #ifdef CONFIG_OF
173 if (i2c->dev.of_node) {
174 const struct of_device_id *match;
175 match = of_match_node(sec_dt_match, i2c->dev.of_node);
176 return (int)match->data;
178 #endif
179 return (int)id->driver_data;
182 static int sec_pmic_probe(struct i2c_client *i2c,
183 const struct i2c_device_id *id)
185 struct sec_platform_data *pdata = i2c->dev.platform_data;
186 const struct regmap_config *regmap;
187 struct sec_pmic_dev *sec_pmic;
188 int ret;
190 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
191 GFP_KERNEL);
192 if (sec_pmic == NULL)
193 return -ENOMEM;
195 i2c_set_clientdata(i2c, sec_pmic);
196 sec_pmic->dev = &i2c->dev;
197 sec_pmic->i2c = i2c;
198 sec_pmic->irq = i2c->irq;
199 sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
201 if (sec_pmic->dev->of_node) {
202 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
203 if (IS_ERR(pdata)) {
204 ret = PTR_ERR(pdata);
205 return ret;
207 pdata->device_type = sec_pmic->type;
209 if (pdata) {
210 sec_pmic->device_type = pdata->device_type;
211 sec_pmic->ono = pdata->ono;
212 sec_pmic->irq_base = pdata->irq_base;
213 sec_pmic->wakeup = pdata->wakeup;
214 sec_pmic->pdata = pdata;
217 switch (sec_pmic->device_type) {
218 case S2MPS11X:
219 regmap = &s2mps11_regmap_config;
220 break;
221 case S5M8763X:
222 regmap = &s5m8763_regmap_config;
223 break;
224 case S5M8767X:
225 regmap = &s5m8767_regmap_config;
226 break;
227 default:
228 regmap = &sec_regmap_config;
229 break;
232 sec_pmic->regmap = devm_regmap_init_i2c(i2c, regmap);
233 if (IS_ERR(sec_pmic->regmap)) {
234 ret = PTR_ERR(sec_pmic->regmap);
235 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
236 ret);
237 return ret;
240 sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
241 i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
243 if (pdata && pdata->cfg_pmic_irq)
244 pdata->cfg_pmic_irq();
246 sec_irq_init(sec_pmic);
248 pm_runtime_set_active(sec_pmic->dev);
250 switch (sec_pmic->device_type) {
251 case S5M8751X:
252 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
253 ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
254 break;
255 case S5M8763X:
256 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
257 ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
258 break;
259 case S5M8767X:
260 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
261 ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
262 break;
263 case S2MPS11X:
264 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
265 ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
266 break;
267 default:
268 /* If this happens the probe function is problem */
269 BUG();
272 if (ret)
273 goto err;
275 return ret;
277 err:
278 sec_irq_exit(sec_pmic);
279 i2c_unregister_device(sec_pmic->rtc);
280 return ret;
283 static int sec_pmic_remove(struct i2c_client *i2c)
285 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
287 mfd_remove_devices(sec_pmic->dev);
288 sec_irq_exit(sec_pmic);
289 i2c_unregister_device(sec_pmic->rtc);
290 return 0;
293 static const struct i2c_device_id sec_pmic_id[] = {
294 { "sec_pmic", 0 },
297 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
299 static struct i2c_driver sec_pmic_driver = {
300 .driver = {
301 .name = "sec_pmic",
302 .owner = THIS_MODULE,
303 .of_match_table = of_match_ptr(sec_dt_match),
305 .probe = sec_pmic_probe,
306 .remove = sec_pmic_remove,
307 .id_table = sec_pmic_id,
310 static int __init sec_pmic_init(void)
312 return i2c_add_driver(&sec_pmic_driver);
315 subsys_initcall(sec_pmic_init);
317 static void __exit sec_pmic_exit(void)
319 i2c_del_driver(&sec_pmic_driver);
321 module_exit(sec_pmic_exit);
323 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
324 MODULE_DESCRIPTION("Core support for the S5M MFD");
325 MODULE_LICENSE("GPL");