Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/lenb/linux...
[firefly-linux-kernel-4.4.55.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/s2mpa01.h>
30 #include <linux/mfd/samsung/s2mps11.h>
31 #include <linux/mfd/samsung/s2mps14.h>
32 #include <linux/mfd/samsung/s5m8763.h>
33 #include <linux/mfd/samsung/s5m8767.h>
34 #include <linux/regmap.h>
35
36 static const struct mfd_cell s5m8751_devs[] = {
37         {
38                 .name = "s5m8751-pmic",
39         }, {
40                 .name = "s5m-charger",
41         }, {
42                 .name = "s5m8751-codec",
43         },
44 };
45
46 static const struct mfd_cell s5m8763_devs[] = {
47         {
48                 .name = "s5m8763-pmic",
49         }, {
50                 .name = "s5m-rtc",
51         }, {
52                 .name = "s5m-charger",
53         },
54 };
55
56 static const struct mfd_cell s5m8767_devs[] = {
57         {
58                 .name = "s5m8767-pmic",
59         }, {
60                 .name = "s5m-rtc",
61         }, {
62                 .name = "s5m8767-clk",
63         }
64 };
65
66 static const struct mfd_cell s2mps11_devs[] = {
67         {
68                 .name = "s2mps11-pmic",
69         }, {
70                 .name = "s2mps11-clk",
71         }
72 };
73
74 static const struct mfd_cell s2mps14_devs[] = {
75         {
76                 .name = "s2mps14-pmic",
77         }, {
78                 .name = "s2mps14-rtc",
79         }, {
80                 .name = "s2mps14-clk",
81         }
82 };
83
84 static const struct mfd_cell s2mpa01_devs[] = {
85         {
86                 .name = "s2mpa01-pmic",
87         },
88 };
89
90 #ifdef CONFIG_OF
91 static struct of_device_id sec_dt_match[] = {
92         {       .compatible = "samsung,s5m8767-pmic",
93                 .data = (void *)S5M8767X,
94         }, {
95                 .compatible = "samsung,s2mps11-pmic",
96                 .data = (void *)S2MPS11X,
97         }, {
98                 .compatible = "samsung,s2mps14-pmic",
99                 .data = (void *)S2MPS14X,
100         }, {
101                 .compatible = "samsung,s2mpa01-pmic",
102                 .data = (void *)S2MPA01,
103         }, {
104                 /* Sentinel */
105         },
106 };
107 #endif
108
109 static bool s2mpa01_volatile(struct device *dev, unsigned int reg)
110 {
111         switch (reg) {
112         case S2MPA01_REG_INT1M:
113         case S2MPA01_REG_INT2M:
114         case S2MPA01_REG_INT3M:
115                 return false;
116         default:
117                 return true;
118         }
119 }
120
121 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
122 {
123         switch (reg) {
124         case S2MPS11_REG_INT1M:
125         case S2MPS11_REG_INT2M:
126         case S2MPS11_REG_INT3M:
127                 return false;
128         default:
129                 return true;
130         }
131 }
132
133 static bool s5m8763_volatile(struct device *dev, unsigned int reg)
134 {
135         switch (reg) {
136         case S5M8763_REG_IRQM1:
137         case S5M8763_REG_IRQM2:
138         case S5M8763_REG_IRQM3:
139         case S5M8763_REG_IRQM4:
140                 return false;
141         default:
142                 return true;
143         }
144 }
145
146 static const struct regmap_config sec_regmap_config = {
147         .reg_bits = 8,
148         .val_bits = 8,
149 };
150
151 static const struct regmap_config s2mpa01_regmap_config = {
152         .reg_bits = 8,
153         .val_bits = 8,
154
155         .max_register = S2MPA01_REG_LDO_OVCB4,
156         .volatile_reg = s2mpa01_volatile,
157         .cache_type = REGCACHE_FLAT,
158 };
159
160 static const struct regmap_config s2mps11_regmap_config = {
161         .reg_bits = 8,
162         .val_bits = 8,
163
164         .max_register = S2MPS11_REG_L38CTRL,
165         .volatile_reg = s2mps11_volatile,
166         .cache_type = REGCACHE_FLAT,
167 };
168
169 static const struct regmap_config s2mps14_regmap_config = {
170         .reg_bits = 8,
171         .val_bits = 8,
172
173         .max_register = S2MPS14_REG_LDODSCH3,
174         .volatile_reg = s2mps11_volatile,
175         .cache_type = REGCACHE_FLAT,
176 };
177
178 static const struct regmap_config s5m8763_regmap_config = {
179         .reg_bits = 8,
180         .val_bits = 8,
181
182         .max_register = S5M8763_REG_LBCNFG2,
183         .volatile_reg = s5m8763_volatile,
184         .cache_type = REGCACHE_FLAT,
185 };
186
187 static const struct regmap_config s5m8767_regmap_config = {
188         .reg_bits = 8,
189         .val_bits = 8,
190
191         .max_register = S5M8767_REG_LDO28CTRL,
192         .volatile_reg = s2mps11_volatile,
193         .cache_type = REGCACHE_FLAT,
194 };
195
196 static const struct regmap_config s5m_rtc_regmap_config = {
197         .reg_bits = 8,
198         .val_bits = 8,
199
200         .max_register = SEC_RTC_REG_MAX,
201 };
202
203 static const struct regmap_config s2mps14_rtc_regmap_config = {
204         .reg_bits = 8,
205         .val_bits = 8,
206
207         .max_register = S2MPS_RTC_REG_MAX,
208 };
209
210 #ifdef CONFIG_OF
211 /*
212  * Only the common platform data elements for s5m8767 are parsed here from the
213  * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
214  * others have to parse their own platform data elements from device tree.
215  *
216  * The s5m8767 platform data structure is instantiated here and the drivers for
217  * the sub-modules need not instantiate another instance while parsing their
218  * platform data.
219  */
220 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
221                                         struct device *dev)
222 {
223         struct sec_platform_data *pd;
224
225         pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
226         if (!pd) {
227                 dev_err(dev, "could not allocate memory for pdata\n");
228                 return ERR_PTR(-ENOMEM);
229         }
230
231         /*
232          * ToDo: the 'wakeup' member in the platform data is more of a linux
233          * specfic information. Hence, there is no binding for that yet and
234          * not parsed here.
235          */
236
237         return pd;
238 }
239 #else
240 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
241                                         struct device *dev)
242 {
243         return NULL;
244 }
245 #endif
246
247 static inline unsigned long sec_i2c_get_driver_data(struct i2c_client *i2c,
248                                                 const struct i2c_device_id *id)
249 {
250 #ifdef CONFIG_OF
251         if (i2c->dev.of_node) {
252                 const struct of_device_id *match;
253                 match = of_match_node(sec_dt_match, i2c->dev.of_node);
254                 return (unsigned long)match->data;
255         }
256 #endif
257         return id->driver_data;
258 }
259
260 static int sec_pmic_probe(struct i2c_client *i2c,
261                             const struct i2c_device_id *id)
262 {
263         struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
264         const struct regmap_config *regmap, *regmap_rtc;
265         struct sec_pmic_dev *sec_pmic;
266         int ret;
267
268         sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
269                                 GFP_KERNEL);
270         if (sec_pmic == NULL)
271                 return -ENOMEM;
272
273         i2c_set_clientdata(i2c, sec_pmic);
274         sec_pmic->dev = &i2c->dev;
275         sec_pmic->i2c = i2c;
276         sec_pmic->irq = i2c->irq;
277         sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
278
279         if (sec_pmic->dev->of_node) {
280                 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
281                 if (IS_ERR(pdata)) {
282                         ret = PTR_ERR(pdata);
283                         return ret;
284                 }
285                 pdata->device_type = sec_pmic->type;
286         }
287         if (pdata) {
288                 sec_pmic->device_type = pdata->device_type;
289                 sec_pmic->ono = pdata->ono;
290                 sec_pmic->irq_base = pdata->irq_base;
291                 sec_pmic->wakeup = pdata->wakeup;
292                 sec_pmic->pdata = pdata;
293         }
294
295         switch (sec_pmic->device_type) {
296         case S2MPA01:
297                 regmap = &s2mpa01_regmap_config;
298                 break;
299         case S2MPS11X:
300                 regmap = &s2mps11_regmap_config;
301                 /*
302                  * The rtc-s5m driver does not support S2MPS11 and there
303                  * is no mfd_cell for S2MPS11 RTC device.
304                  * However we must pass something to devm_regmap_init_i2c()
305                  * so use S5M-like regmap config even though it wouldn't work.
306                  */
307                 regmap_rtc = &s5m_rtc_regmap_config;
308                 break;
309         case S2MPS14X:
310                 regmap = &s2mps14_regmap_config;
311                 regmap_rtc = &s2mps14_rtc_regmap_config;
312                 break;
313         case S5M8763X:
314                 regmap = &s5m8763_regmap_config;
315                 regmap_rtc = &s5m_rtc_regmap_config;
316                 break;
317         case S5M8767X:
318                 regmap = &s5m8767_regmap_config;
319                 regmap_rtc = &s5m_rtc_regmap_config;
320                 break;
321         default:
322                 regmap = &sec_regmap_config;
323                 regmap_rtc = &s5m_rtc_regmap_config;
324                 break;
325         }
326
327         sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
328         if (IS_ERR(sec_pmic->regmap_pmic)) {
329                 ret = PTR_ERR(sec_pmic->regmap_pmic);
330                 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
331                         ret);
332                 return ret;
333         }
334
335         sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
336         if (!sec_pmic->rtc) {
337                 dev_err(&i2c->dev, "Failed to allocate I2C for RTC\n");
338                 return -ENODEV;
339         }
340         i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
341
342         sec_pmic->regmap_rtc = devm_regmap_init_i2c(sec_pmic->rtc, regmap_rtc);
343         if (IS_ERR(sec_pmic->regmap_rtc)) {
344                 ret = PTR_ERR(sec_pmic->regmap_rtc);
345                 dev_err(&i2c->dev, "Failed to allocate RTC register map: %d\n",
346                         ret);
347                 return ret;
348         }
349
350         if (pdata && pdata->cfg_pmic_irq)
351                 pdata->cfg_pmic_irq();
352
353         sec_irq_init(sec_pmic);
354
355         pm_runtime_set_active(sec_pmic->dev);
356
357         switch (sec_pmic->device_type) {
358         case S5M8751X:
359                 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
360                                       ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
361                 break;
362         case S5M8763X:
363                 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
364                                       ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
365                 break;
366         case S5M8767X:
367                 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
368                                       ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
369                 break;
370         case S2MPA01:
371                 ret = mfd_add_devices(sec_pmic->dev, -1, s2mpa01_devs,
372                                       ARRAY_SIZE(s2mpa01_devs), NULL, 0, NULL);
373                 break;
374         case S2MPS11X:
375                 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
376                                       ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
377                 break;
378         case S2MPS14X:
379                 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps14_devs,
380                                       ARRAY_SIZE(s2mps14_devs), NULL, 0, NULL);
381                 break;
382         default:
383                 /* If this happens the probe function is problem */
384                 BUG();
385         }
386
387         if (ret)
388                 goto err;
389
390         device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
391
392         return ret;
393
394 err:
395         sec_irq_exit(sec_pmic);
396         i2c_unregister_device(sec_pmic->rtc);
397         return ret;
398 }
399
400 static int sec_pmic_remove(struct i2c_client *i2c)
401 {
402         struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
403
404         mfd_remove_devices(sec_pmic->dev);
405         sec_irq_exit(sec_pmic);
406         i2c_unregister_device(sec_pmic->rtc);
407         return 0;
408 }
409
410 #ifdef CONFIG_PM_SLEEP
411 static int sec_pmic_suspend(struct device *dev)
412 {
413         struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
414         struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
415
416         if (device_may_wakeup(dev)) {
417                 enable_irq_wake(sec_pmic->irq);
418                 /*
419                  * PMIC IRQ must be disabled during suspend for RTC alarm
420                  * to work properly.
421                  * When device is woken up from suspend by RTC Alarm, an
422                  * interrupt occurs before resuming I2C bus controller.
423                  * The interrupt is handled by regmap_irq_thread which tries
424                  * to read RTC registers. This read fails (I2C is still
425                  * suspended) and RTC Alarm interrupt is disabled.
426                  */
427                 disable_irq(sec_pmic->irq);
428         }
429
430         return 0;
431 }
432
433 static int sec_pmic_resume(struct device *dev)
434 {
435         struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
436         struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
437
438         if (device_may_wakeup(dev)) {
439                 disable_irq_wake(sec_pmic->irq);
440                 enable_irq(sec_pmic->irq);
441         }
442
443         return 0;
444 }
445 #endif /* CONFIG_PM_SLEEP */
446
447 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
448
449 static const struct i2c_device_id sec_pmic_id[] = {
450         { "sec_pmic", 0 },
451         { }
452 };
453 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
454
455 static struct i2c_driver sec_pmic_driver = {
456         .driver = {
457                    .name = "sec_pmic",
458                    .owner = THIS_MODULE,
459                    .pm = &sec_pmic_pm_ops,
460                    .of_match_table = of_match_ptr(sec_dt_match),
461         },
462         .probe = sec_pmic_probe,
463         .remove = sec_pmic_remove,
464         .id_table = sec_pmic_id,
465 };
466
467 static int __init sec_pmic_init(void)
468 {
469         return i2c_add_driver(&sec_pmic_driver);
470 }
471
472 subsys_initcall(sec_pmic_init);
473
474 static void __exit sec_pmic_exit(void)
475 {
476         i2c_del_driver(&sec_pmic_driver);
477 }
478 module_exit(sec_pmic_exit);
479
480 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
481 MODULE_DESCRIPTION("Core support for the S5M MFD");
482 MODULE_LICENSE("GPL");