Merge remote-tracking branches 'asoc/fix/adsp', 'asoc/fix/arizona', 'asoc/fix/atmel...
[linux-drm-fsl-dcu.git] / drivers / mfd / sec-core.c
1 /*
2  * sec-core.c
3  *
4  * Copyright (c) 2012 Samsung Electronics Co., Ltd
5  *              http://www.samsung.com
6  *
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.
11  *
12  */
13
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.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>
33
34 static struct mfd_cell s5m8751_devs[] = {
35         {
36                 .name = "s5m8751-pmic",
37         }, {
38                 .name = "s5m-charger",
39         }, {
40                 .name = "s5m8751-codec",
41         },
42 };
43
44 static struct mfd_cell s5m8763_devs[] = {
45         {
46                 .name = "s5m8763-pmic",
47         }, {
48                 .name = "s5m-rtc",
49         }, {
50                 .name = "s5m-charger",
51         },
52 };
53
54 static struct mfd_cell s5m8767_devs[] = {
55         {
56                 .name = "s5m8767-pmic",
57         }, {
58                 .name = "s5m-rtc",
59         },
60 };
61
62 static struct mfd_cell s2mps11_devs[] = {
63         {
64                 .name = "s2mps11-pmic",
65         }, {
66                 .name = "s2mps11-clk",
67         }
68 };
69
70 #ifdef CONFIG_OF
71 static struct of_device_id sec_dt_match[] = {
72         {       .compatible = "samsung,s5m8767-pmic",
73                 .data = (void *)S5M8767X,
74         },
75         {       .compatible = "samsung,s2mps11-pmic",
76                 .data = (void *)S2MPS11X,
77         },
78         {},
79 };
80 #endif
81
82 int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
83 {
84         return regmap_read(sec_pmic->regmap_pmic, reg, dest);
85 }
86 EXPORT_SYMBOL_GPL(sec_reg_read);
87
88 int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
89 {
90         return regmap_bulk_read(sec_pmic->regmap_pmic, reg, buf, count);
91 }
92 EXPORT_SYMBOL_GPL(sec_bulk_read);
93
94 int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
95 {
96         return regmap_write(sec_pmic->regmap_pmic, reg, value);
97 }
98 EXPORT_SYMBOL_GPL(sec_reg_write);
99
100 int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
101 {
102         return regmap_raw_write(sec_pmic->regmap_pmic, reg, buf, count);
103 }
104 EXPORT_SYMBOL_GPL(sec_bulk_write);
105
106 int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
107 {
108         return regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, val);
109 }
110 EXPORT_SYMBOL_GPL(sec_reg_update);
111
112 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
113 {
114         switch (reg) {
115         case S2MPS11_REG_INT1M:
116         case S2MPS11_REG_INT2M:
117         case S2MPS11_REG_INT3M:
118                 return false;
119         default:
120                 return true;
121         }
122 }
123
124 static bool s5m8763_volatile(struct device *dev, unsigned int reg)
125 {
126         switch (reg) {
127         case S5M8763_REG_IRQM1:
128         case S5M8763_REG_IRQM2:
129         case S5M8763_REG_IRQM3:
130         case S5M8763_REG_IRQM4:
131                 return false;
132         default:
133                 return true;
134         }
135 }
136
137 static struct regmap_config sec_regmap_config = {
138         .reg_bits = 8,
139         .val_bits = 8,
140 };
141
142 static struct regmap_config s2mps11_regmap_config = {
143         .reg_bits = 8,
144         .val_bits = 8,
145
146         .max_register = S2MPS11_REG_L38CTRL,
147         .volatile_reg = s2mps11_volatile,
148         .cache_type = REGCACHE_FLAT,
149 };
150
151 static struct regmap_config s5m8763_regmap_config = {
152         .reg_bits = 8,
153         .val_bits = 8,
154
155         .max_register = S5M8763_REG_LBCNFG2,
156         .volatile_reg = s5m8763_volatile,
157         .cache_type = REGCACHE_FLAT,
158 };
159
160 static struct regmap_config s5m8767_regmap_config = {
161         .reg_bits = 8,
162         .val_bits = 8,
163
164         .max_register = S5M8767_REG_LDO28CTRL,
165         .volatile_reg = s2mps11_volatile,
166         .cache_type = REGCACHE_FLAT,
167 };
168
169 static const struct regmap_config sec_rtc_regmap_config = {
170         .reg_bits = 8,
171         .val_bits = 8,
172 };
173
174 #ifdef CONFIG_OF
175 /*
176  * Only the common platform data elements for s5m8767 are parsed here from the
177  * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
178  * others have to parse their own platform data elements from device tree.
179  *
180  * The s5m8767 platform data structure is instantiated here and the drivers for
181  * the sub-modules need not instantiate another instance while parsing their
182  * platform data.
183  */
184 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
185                                         struct device *dev)
186 {
187         struct sec_platform_data *pd;
188
189         pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
190         if (!pd) {
191                 dev_err(dev, "could not allocate memory for pdata\n");
192                 return ERR_PTR(-ENOMEM);
193         }
194
195         /*
196          * ToDo: the 'wakeup' member in the platform data is more of a linux
197          * specfic information. Hence, there is no binding for that yet and
198          * not parsed here.
199          */
200
201         return pd;
202 }
203 #else
204 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
205                                         struct device *dev)
206 {
207         return 0;
208 }
209 #endif
210
211 static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
212                                                 const struct i2c_device_id *id)
213 {
214 #ifdef CONFIG_OF
215         if (i2c->dev.of_node) {
216                 const struct of_device_id *match;
217                 match = of_match_node(sec_dt_match, i2c->dev.of_node);
218                 return (int)match->data;
219         }
220 #endif
221         return (int)id->driver_data;
222 }
223
224 static int sec_pmic_probe(struct i2c_client *i2c,
225                             const struct i2c_device_id *id)
226 {
227         struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
228         const struct regmap_config *regmap;
229         struct sec_pmic_dev *sec_pmic;
230         int ret;
231
232         sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
233                                 GFP_KERNEL);
234         if (sec_pmic == NULL)
235                 return -ENOMEM;
236
237         i2c_set_clientdata(i2c, sec_pmic);
238         sec_pmic->dev = &i2c->dev;
239         sec_pmic->i2c = i2c;
240         sec_pmic->irq = i2c->irq;
241         sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
242
243         if (sec_pmic->dev->of_node) {
244                 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
245                 if (IS_ERR(pdata)) {
246                         ret = PTR_ERR(pdata);
247                         return ret;
248                 }
249                 pdata->device_type = sec_pmic->type;
250         }
251         if (pdata) {
252                 sec_pmic->device_type = pdata->device_type;
253                 sec_pmic->ono = pdata->ono;
254                 sec_pmic->irq_base = pdata->irq_base;
255                 sec_pmic->wakeup = pdata->wakeup;
256                 sec_pmic->pdata = pdata;
257         }
258
259         switch (sec_pmic->device_type) {
260         case S2MPS11X:
261                 regmap = &s2mps11_regmap_config;
262                 break;
263         case S5M8763X:
264                 regmap = &s5m8763_regmap_config;
265                 break;
266         case S5M8767X:
267                 regmap = &s5m8767_regmap_config;
268                 break;
269         default:
270                 regmap = &sec_regmap_config;
271                 break;
272         }
273
274         sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
275         if (IS_ERR(sec_pmic->regmap_pmic)) {
276                 ret = PTR_ERR(sec_pmic->regmap_pmic);
277                 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
278                         ret);
279                 return ret;
280         }
281
282         sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
283         i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
284
285         sec_pmic->regmap_rtc = devm_regmap_init_i2c(sec_pmic->rtc,
286                         &sec_rtc_regmap_config);
287         if (IS_ERR(sec_pmic->regmap_rtc)) {
288                 ret = PTR_ERR(sec_pmic->regmap_rtc);
289                 dev_err(&i2c->dev, "Failed to allocate RTC register map: %d\n",
290                         ret);
291                 return ret;
292         }
293
294         if (pdata && pdata->cfg_pmic_irq)
295                 pdata->cfg_pmic_irq();
296
297         sec_irq_init(sec_pmic);
298
299         pm_runtime_set_active(sec_pmic->dev);
300
301         switch (sec_pmic->device_type) {
302         case S5M8751X:
303                 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
304                                       ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
305                 break;
306         case S5M8763X:
307                 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
308                                       ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
309                 break;
310         case S5M8767X:
311                 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
312                                       ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
313                 break;
314         case S2MPS11X:
315                 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
316                                       ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
317                 break;
318         default:
319                 /* If this happens the probe function is problem */
320                 BUG();
321         }
322
323         if (ret)
324                 goto err;
325
326         return ret;
327
328 err:
329         sec_irq_exit(sec_pmic);
330         i2c_unregister_device(sec_pmic->rtc);
331         return ret;
332 }
333
334 static int sec_pmic_remove(struct i2c_client *i2c)
335 {
336         struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
337
338         mfd_remove_devices(sec_pmic->dev);
339         sec_irq_exit(sec_pmic);
340         i2c_unregister_device(sec_pmic->rtc);
341         return 0;
342 }
343
344 static const struct i2c_device_id sec_pmic_id[] = {
345         { "sec_pmic", 0 },
346         { }
347 };
348 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
349
350 static struct i2c_driver sec_pmic_driver = {
351         .driver = {
352                    .name = "sec_pmic",
353                    .owner = THIS_MODULE,
354                    .of_match_table = of_match_ptr(sec_dt_match),
355         },
356         .probe = sec_pmic_probe,
357         .remove = sec_pmic_remove,
358         .id_table = sec_pmic_id,
359 };
360
361 static int __init sec_pmic_init(void)
362 {
363         return i2c_add_driver(&sec_pmic_driver);
364 }
365
366 subsys_initcall(sec_pmic_init);
367
368 static void __exit sec_pmic_exit(void)
369 {
370         i2c_del_driver(&sec_pmic_driver);
371 }
372 module_exit(sec_pmic_exit);
373
374 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
375 MODULE_DESCRIPTION("Core support for the S5M MFD");
376 MODULE_LICENSE("GPL");