Linux 3.12.39
[linux/fpc-iii.git] / drivers / mfd / sec-core.c
blobd02546b0a8cc805aefa4cfb4c19919ee9cdbdc7f
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",
64 }, {
65 .name = "s2mps11-clk",
69 #ifdef CONFIG_OF
70 static struct of_device_id sec_dt_match[] = {
71 { .compatible = "samsung,s5m8767-pmic",
72 .data = (void *)S5M8767X,
74 { .compatible = "samsung,s2mps11-pmic",
75 .data = (void *)S2MPS11X,
77 {},
79 #endif
81 int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
83 return regmap_read(sec_pmic->regmap, reg, dest);
85 EXPORT_SYMBOL_GPL(sec_reg_read);
87 int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
89 return regmap_bulk_read(sec_pmic->regmap, reg, buf, count);
91 EXPORT_SYMBOL_GPL(sec_bulk_read);
93 int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
95 return regmap_write(sec_pmic->regmap, reg, value);
97 EXPORT_SYMBOL_GPL(sec_reg_write);
99 int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
101 return regmap_raw_write(sec_pmic->regmap, reg, buf, count);
103 EXPORT_SYMBOL_GPL(sec_bulk_write);
105 int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
107 return regmap_update_bits(sec_pmic->regmap, reg, mask, val);
109 EXPORT_SYMBOL_GPL(sec_reg_update);
111 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
113 switch (reg) {
114 case S2MPS11_REG_INT1M:
115 case S2MPS11_REG_INT2M:
116 case S2MPS11_REG_INT3M:
117 return false;
118 default:
119 return true;
123 static bool s5m8763_volatile(struct device *dev, unsigned int reg)
125 switch (reg) {
126 case S5M8763_REG_IRQM1:
127 case S5M8763_REG_IRQM2:
128 case S5M8763_REG_IRQM3:
129 case S5M8763_REG_IRQM4:
130 return false;
131 default:
132 return true;
136 static struct regmap_config sec_regmap_config = {
137 .reg_bits = 8,
138 .val_bits = 8,
141 static struct regmap_config s2mps11_regmap_config = {
142 .reg_bits = 8,
143 .val_bits = 8,
145 .max_register = S2MPS11_REG_L38CTRL,
146 .volatile_reg = s2mps11_volatile,
147 .cache_type = REGCACHE_FLAT,
150 static struct regmap_config s5m8763_regmap_config = {
151 .reg_bits = 8,
152 .val_bits = 8,
154 .max_register = S5M8763_REG_LBCNFG2,
155 .volatile_reg = s5m8763_volatile,
156 .cache_type = REGCACHE_FLAT,
159 static struct regmap_config s5m8767_regmap_config = {
160 .reg_bits = 8,
161 .val_bits = 8,
163 .max_register = S5M8767_REG_LDO28CTRL,
164 .volatile_reg = s2mps11_volatile,
165 .cache_type = REGCACHE_FLAT,
168 #ifdef CONFIG_OF
170 * Only the common platform data elements for s5m8767 are parsed here from the
171 * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
172 * others have to parse their own platform data elements from device tree.
174 * The s5m8767 platform data structure is instantiated here and the drivers for
175 * the sub-modules need not instantiate another instance while parsing their
176 * platform data.
178 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
179 struct device *dev)
181 struct sec_platform_data *pd;
183 pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
184 if (!pd) {
185 dev_err(dev, "could not allocate memory for pdata\n");
186 return ERR_PTR(-ENOMEM);
190 * ToDo: the 'wakeup' member in the platform data is more of a linux
191 * specfic information. Hence, there is no binding for that yet and
192 * not parsed here.
195 return pd;
197 #else
198 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
199 struct device *dev)
201 return 0;
203 #endif
205 static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
206 const struct i2c_device_id *id)
208 #ifdef CONFIG_OF
209 if (i2c->dev.of_node) {
210 const struct of_device_id *match;
211 match = of_match_node(sec_dt_match, i2c->dev.of_node);
212 return (int)match->data;
214 #endif
215 return (int)id->driver_data;
218 static int sec_pmic_probe(struct i2c_client *i2c,
219 const struct i2c_device_id *id)
221 struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
222 const struct regmap_config *regmap;
223 struct sec_pmic_dev *sec_pmic;
224 int ret;
226 sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
227 GFP_KERNEL);
228 if (sec_pmic == NULL)
229 return -ENOMEM;
231 i2c_set_clientdata(i2c, sec_pmic);
232 sec_pmic->dev = &i2c->dev;
233 sec_pmic->i2c = i2c;
234 sec_pmic->irq = i2c->irq;
235 sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
237 if (sec_pmic->dev->of_node) {
238 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
239 if (IS_ERR(pdata)) {
240 ret = PTR_ERR(pdata);
241 return ret;
243 pdata->device_type = sec_pmic->type;
245 if (pdata) {
246 sec_pmic->device_type = pdata->device_type;
247 sec_pmic->ono = pdata->ono;
248 sec_pmic->irq_base = pdata->irq_base;
249 sec_pmic->wakeup = pdata->wakeup;
250 sec_pmic->pdata = pdata;
253 switch (sec_pmic->device_type) {
254 case S2MPS11X:
255 regmap = &s2mps11_regmap_config;
256 break;
257 case S5M8763X:
258 regmap = &s5m8763_regmap_config;
259 break;
260 case S5M8767X:
261 regmap = &s5m8767_regmap_config;
262 break;
263 default:
264 regmap = &sec_regmap_config;
265 break;
268 sec_pmic->regmap = devm_regmap_init_i2c(i2c, regmap);
269 if (IS_ERR(sec_pmic->regmap)) {
270 ret = PTR_ERR(sec_pmic->regmap);
271 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
272 ret);
273 return ret;
276 sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
277 if (!sec_pmic->rtc) {
278 dev_err(&i2c->dev, "Failed to allocate I2C for RTC\n");
279 return -ENODEV;
281 i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
283 if (pdata && pdata->cfg_pmic_irq)
284 pdata->cfg_pmic_irq();
286 sec_irq_init(sec_pmic);
288 pm_runtime_set_active(sec_pmic->dev);
290 switch (sec_pmic->device_type) {
291 case S5M8751X:
292 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
293 ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
294 break;
295 case S5M8763X:
296 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
297 ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
298 break;
299 case S5M8767X:
300 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
301 ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
302 break;
303 case S2MPS11X:
304 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
305 ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
306 break;
307 default:
308 /* If this happens the probe function is problem */
309 BUG();
312 if (ret)
313 goto err;
315 return ret;
317 err:
318 sec_irq_exit(sec_pmic);
319 i2c_unregister_device(sec_pmic->rtc);
320 return ret;
323 static int sec_pmic_remove(struct i2c_client *i2c)
325 struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
327 mfd_remove_devices(sec_pmic->dev);
328 sec_irq_exit(sec_pmic);
329 i2c_unregister_device(sec_pmic->rtc);
330 return 0;
333 static const struct i2c_device_id sec_pmic_id[] = {
334 { "sec_pmic", 0 },
337 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
339 static struct i2c_driver sec_pmic_driver = {
340 .driver = {
341 .name = "sec_pmic",
342 .owner = THIS_MODULE,
343 .of_match_table = of_match_ptr(sec_dt_match),
345 .probe = sec_pmic_probe,
346 .remove = sec_pmic_remove,
347 .id_table = sec_pmic_id,
350 static int __init sec_pmic_init(void)
352 return i2c_add_driver(&sec_pmic_driver);
355 subsys_initcall(sec_pmic_init);
357 static void __exit sec_pmic_exit(void)
359 i2c_del_driver(&sec_pmic_driver);
361 module_exit(sec_pmic_exit);
363 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
364 MODULE_DESCRIPTION("Core support for the S5M MFD");
365 MODULE_LICENSE("GPL");