]> Pileus Git - ~andy/linux/blob - drivers/mfd/sec-core.c
Merge tag 'mfd-3.14-1' of git://git.linaro.org/people/ljones/mfd
[~andy/linux] / 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 const 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 const 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 const struct mfd_cell s5m8767_devs[] = {
55         {
56                 .name = "s5m8767-pmic",
57         }, {
58                 .name = "s5m-rtc",
59         }, {
60                 .name = "s5m8767-clk",
61         }
62 };
63
64 static const struct mfd_cell s2mps11_devs[] = {
65         {
66                 .name = "s2mps11-pmic",
67         }, {
68                 .name = "s2mps11-clk",
69         }
70 };
71
72 #ifdef CONFIG_OF
73 static struct of_device_id sec_dt_match[] = {
74         {       .compatible = "samsung,s5m8767-pmic",
75                 .data = (void *)S5M8767X,
76         },
77         {       .compatible = "samsung,s2mps11-pmic",
78                 .data = (void *)S2MPS11X,
79         },
80         {},
81 };
82 #endif
83
84 int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
85 {
86         return regmap_read(sec_pmic->regmap_pmic, reg, dest);
87 }
88 EXPORT_SYMBOL_GPL(sec_reg_read);
89
90 int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
91 {
92         return regmap_bulk_read(sec_pmic->regmap_pmic, reg, buf, count);
93 }
94 EXPORT_SYMBOL_GPL(sec_bulk_read);
95
96 int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
97 {
98         return regmap_write(sec_pmic->regmap_pmic, reg, value);
99 }
100 EXPORT_SYMBOL_GPL(sec_reg_write);
101
102 int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
103 {
104         return regmap_raw_write(sec_pmic->regmap_pmic, reg, buf, count);
105 }
106 EXPORT_SYMBOL_GPL(sec_bulk_write);
107
108 int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
109 {
110         return regmap_update_bits(sec_pmic->regmap_pmic, reg, mask, val);
111 }
112 EXPORT_SYMBOL_GPL(sec_reg_update);
113
114 static bool s2mps11_volatile(struct device *dev, unsigned int reg)
115 {
116         switch (reg) {
117         case S2MPS11_REG_INT1M:
118         case S2MPS11_REG_INT2M:
119         case S2MPS11_REG_INT3M:
120                 return false;
121         default:
122                 return true;
123         }
124 }
125
126 static bool s5m8763_volatile(struct device *dev, unsigned int reg)
127 {
128         switch (reg) {
129         case S5M8763_REG_IRQM1:
130         case S5M8763_REG_IRQM2:
131         case S5M8763_REG_IRQM3:
132         case S5M8763_REG_IRQM4:
133                 return false;
134         default:
135                 return true;
136         }
137 }
138
139 static const struct regmap_config sec_regmap_config = {
140         .reg_bits = 8,
141         .val_bits = 8,
142 };
143
144 static const struct regmap_config s2mps11_regmap_config = {
145         .reg_bits = 8,
146         .val_bits = 8,
147
148         .max_register = S2MPS11_REG_L38CTRL,
149         .volatile_reg = s2mps11_volatile,
150         .cache_type = REGCACHE_FLAT,
151 };
152
153 static const struct regmap_config s5m8763_regmap_config = {
154         .reg_bits = 8,
155         .val_bits = 8,
156
157         .max_register = S5M8763_REG_LBCNFG2,
158         .volatile_reg = s5m8763_volatile,
159         .cache_type = REGCACHE_FLAT,
160 };
161
162 static const struct regmap_config s5m8767_regmap_config = {
163         .reg_bits = 8,
164         .val_bits = 8,
165
166         .max_register = S5M8767_REG_LDO28CTRL,
167         .volatile_reg = s2mps11_volatile,
168         .cache_type = REGCACHE_FLAT,
169 };
170
171 static const struct regmap_config sec_rtc_regmap_config = {
172         .reg_bits = 8,
173         .val_bits = 8,
174 };
175
176 #ifdef CONFIG_OF
177 /*
178  * Only the common platform data elements for s5m8767 are parsed here from the
179  * device tree. Other sub-modules of s5m8767 such as pmic, rtc , charger and
180  * others have to parse their own platform data elements from device tree.
181  *
182  * The s5m8767 platform data structure is instantiated here and the drivers for
183  * the sub-modules need not instantiate another instance while parsing their
184  * platform data.
185  */
186 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
187                                         struct device *dev)
188 {
189         struct sec_platform_data *pd;
190
191         pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
192         if (!pd) {
193                 dev_err(dev, "could not allocate memory for pdata\n");
194                 return ERR_PTR(-ENOMEM);
195         }
196
197         /*
198          * ToDo: the 'wakeup' member in the platform data is more of a linux
199          * specfic information. Hence, there is no binding for that yet and
200          * not parsed here.
201          */
202
203         return pd;
204 }
205 #else
206 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
207                                         struct device *dev)
208 {
209         return NULL;
210 }
211 #endif
212
213 static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
214                                                 const struct i2c_device_id *id)
215 {
216 #ifdef CONFIG_OF
217         if (i2c->dev.of_node) {
218                 const struct of_device_id *match;
219                 match = of_match_node(sec_dt_match, i2c->dev.of_node);
220                 return (int)match->data;
221         }
222 #endif
223         return (int)id->driver_data;
224 }
225
226 static int sec_pmic_probe(struct i2c_client *i2c,
227                             const struct i2c_device_id *id)
228 {
229         struct sec_platform_data *pdata = dev_get_platdata(&i2c->dev);
230         const struct regmap_config *regmap;
231         struct sec_pmic_dev *sec_pmic;
232         int ret;
233
234         sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
235                                 GFP_KERNEL);
236         if (sec_pmic == NULL)
237                 return -ENOMEM;
238
239         i2c_set_clientdata(i2c, sec_pmic);
240         sec_pmic->dev = &i2c->dev;
241         sec_pmic->i2c = i2c;
242         sec_pmic->irq = i2c->irq;
243         sec_pmic->type = sec_i2c_get_driver_data(i2c, id);
244
245         if (sec_pmic->dev->of_node) {
246                 pdata = sec_pmic_i2c_parse_dt_pdata(sec_pmic->dev);
247                 if (IS_ERR(pdata)) {
248                         ret = PTR_ERR(pdata);
249                         return ret;
250                 }
251                 pdata->device_type = sec_pmic->type;
252         }
253         if (pdata) {
254                 sec_pmic->device_type = pdata->device_type;
255                 sec_pmic->ono = pdata->ono;
256                 sec_pmic->irq_base = pdata->irq_base;
257                 sec_pmic->wakeup = pdata->wakeup;
258                 sec_pmic->pdata = pdata;
259         }
260
261         switch (sec_pmic->device_type) {
262         case S2MPS11X:
263                 regmap = &s2mps11_regmap_config;
264                 break;
265         case S5M8763X:
266                 regmap = &s5m8763_regmap_config;
267                 break;
268         case S5M8767X:
269                 regmap = &s5m8767_regmap_config;
270                 break;
271         default:
272                 regmap = &sec_regmap_config;
273                 break;
274         }
275
276         sec_pmic->regmap_pmic = devm_regmap_init_i2c(i2c, regmap);
277         if (IS_ERR(sec_pmic->regmap_pmic)) {
278                 ret = PTR_ERR(sec_pmic->regmap_pmic);
279                 dev_err(&i2c->dev, "Failed to allocate register map: %d\n",
280                         ret);
281                 return ret;
282         }
283
284         sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
285         i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
286
287         sec_pmic->regmap_rtc = devm_regmap_init_i2c(sec_pmic->rtc,
288                         &sec_rtc_regmap_config);
289         if (IS_ERR(sec_pmic->regmap_rtc)) {
290                 ret = PTR_ERR(sec_pmic->regmap_rtc);
291                 dev_err(&i2c->dev, "Failed to allocate RTC register map: %d\n",
292                         ret);
293                 return ret;
294         }
295
296         if (pdata && pdata->cfg_pmic_irq)
297                 pdata->cfg_pmic_irq();
298
299         sec_irq_init(sec_pmic);
300
301         pm_runtime_set_active(sec_pmic->dev);
302
303         switch (sec_pmic->device_type) {
304         case S5M8751X:
305                 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8751_devs,
306                                       ARRAY_SIZE(s5m8751_devs), NULL, 0, NULL);
307                 break;
308         case S5M8763X:
309                 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8763_devs,
310                                       ARRAY_SIZE(s5m8763_devs), NULL, 0, NULL);
311                 break;
312         case S5M8767X:
313                 ret = mfd_add_devices(sec_pmic->dev, -1, s5m8767_devs,
314                                       ARRAY_SIZE(s5m8767_devs), NULL, 0, NULL);
315                 break;
316         case S2MPS11X:
317                 ret = mfd_add_devices(sec_pmic->dev, -1, s2mps11_devs,
318                                       ARRAY_SIZE(s2mps11_devs), NULL, 0, NULL);
319                 break;
320         default:
321                 /* If this happens the probe function is problem */
322                 BUG();
323         }
324
325         if (ret)
326                 goto err;
327
328         device_init_wakeup(sec_pmic->dev, sec_pmic->wakeup);
329
330         return ret;
331
332 err:
333         sec_irq_exit(sec_pmic);
334         i2c_unregister_device(sec_pmic->rtc);
335         return ret;
336 }
337
338 static int sec_pmic_remove(struct i2c_client *i2c)
339 {
340         struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
341
342         mfd_remove_devices(sec_pmic->dev);
343         sec_irq_exit(sec_pmic);
344         i2c_unregister_device(sec_pmic->rtc);
345         return 0;
346 }
347
348 static int sec_pmic_suspend(struct device *dev)
349 {
350         struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
351         struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
352
353         if (device_may_wakeup(dev)) {
354                 enable_irq_wake(sec_pmic->irq);
355                 /*
356                  * PMIC IRQ must be disabled during suspend for RTC alarm
357                  * to work properly.
358                  * When device is woken up from suspend by RTC Alarm, an
359                  * interrupt occurs before resuming I2C bus controller.
360                  * The interrupt is handled by regmap_irq_thread which tries
361                  * to read RTC registers. This read fails (I2C is still
362                  * suspended) and RTC Alarm interrupt is disabled.
363                  */
364                 disable_irq(sec_pmic->irq);
365         }
366
367         return 0;
368 }
369
370 static int sec_pmic_resume(struct device *dev)
371 {
372         struct i2c_client *i2c = container_of(dev, struct i2c_client, dev);
373         struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
374
375         if (device_may_wakeup(dev)) {
376                 disable_irq_wake(sec_pmic->irq);
377                 enable_irq(sec_pmic->irq);
378         }
379
380         return 0;
381 }
382
383 static SIMPLE_DEV_PM_OPS(sec_pmic_pm_ops, sec_pmic_suspend, sec_pmic_resume);
384
385 static const struct i2c_device_id sec_pmic_id[] = {
386         { "sec_pmic", 0 },
387         { }
388 };
389 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
390
391 static struct i2c_driver sec_pmic_driver = {
392         .driver = {
393                    .name = "sec_pmic",
394                    .owner = THIS_MODULE,
395                    .pm = &sec_pmic_pm_ops,
396                    .of_match_table = of_match_ptr(sec_dt_match),
397         },
398         .probe = sec_pmic_probe,
399         .remove = sec_pmic_remove,
400         .id_table = sec_pmic_id,
401 };
402
403 static int __init sec_pmic_init(void)
404 {
405         return i2c_add_driver(&sec_pmic_driver);
406 }
407
408 subsys_initcall(sec_pmic_init);
409
410 static void __exit sec_pmic_exit(void)
411 {
412         i2c_del_driver(&sec_pmic_driver);
413 }
414 module_exit(sec_pmic_exit);
415
416 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
417 MODULE_DESCRIPTION("Core support for the S5M MFD");
418 MODULE_LICENSE("GPL");