]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/mfd/sec-core.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[karo-tx-linux.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_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>
32
33 static struct mfd_cell s5m8751_devs[] = {
34         {
35                 .name = "s5m8751-pmic",
36         }, {
37                 .name = "s5m-charger",
38         }, {
39                 .name = "s5m8751-codec",
40         },
41 };
42
43 static struct mfd_cell s5m8763_devs[] = {
44         {
45                 .name = "s5m8763-pmic",
46         }, {
47                 .name = "s5m-rtc",
48         }, {
49                 .name = "s5m-charger",
50         },
51 };
52
53 static struct mfd_cell s5m8767_devs[] = {
54         {
55                 .name = "s5m8767-pmic",
56         }, {
57                 .name = "s5m-rtc",
58         },
59 };
60
61 static struct mfd_cell s2mps11_devs[] = {
62         {
63                 .name = "s2mps11-pmic",
64         },
65 };
66
67 #ifdef CONFIG_OF
68 static struct of_device_id sec_dt_match[] = {
69         {       .compatible = "samsung,s5m8767-pmic",
70                 .data = (void *)S5M8767X,
71         },
72         {},
73 };
74 #endif
75
76 int sec_reg_read(struct sec_pmic_dev *sec_pmic, u8 reg, void *dest)
77 {
78         return regmap_read(sec_pmic->regmap, reg, dest);
79 }
80 EXPORT_SYMBOL_GPL(sec_reg_read);
81
82 int sec_bulk_read(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
83 {
84         return regmap_bulk_read(sec_pmic->regmap, reg, buf, count);
85 }
86 EXPORT_SYMBOL_GPL(sec_bulk_read);
87
88 int sec_reg_write(struct sec_pmic_dev *sec_pmic, u8 reg, u8 value)
89 {
90         return regmap_write(sec_pmic->regmap, reg, value);
91 }
92 EXPORT_SYMBOL_GPL(sec_reg_write);
93
94 int sec_bulk_write(struct sec_pmic_dev *sec_pmic, u8 reg, int count, u8 *buf)
95 {
96         return regmap_raw_write(sec_pmic->regmap, reg, buf, count);
97 }
98 EXPORT_SYMBOL_GPL(sec_bulk_write);
99
100 int sec_reg_update(struct sec_pmic_dev *sec_pmic, u8 reg, u8 val, u8 mask)
101 {
102         return regmap_update_bits(sec_pmic->regmap, reg, mask, val);
103 }
104 EXPORT_SYMBOL_GPL(sec_reg_update);
105
106 static struct regmap_config sec_regmap_config = {
107         .reg_bits = 8,
108         .val_bits = 8,
109 };
110
111 static struct regmap_config s2mps11_regmap_config = {
112         .reg_bits = 8,
113         .val_bits = 8,
114
115         .max_register = S2MPS11_REG_L38CTRL,
116 };
117
118 static struct regmap_config s5m8763_regmap_config = {
119         .reg_bits = 8,
120         .val_bits = 8,
121
122         .max_register = S5M8763_REG_LBCNFG2,
123 };
124
125 static struct regmap_config s5m8767_regmap_config = {
126         .reg_bits = 8,
127         .val_bits = 8,
128
129         .max_register = S5M8767_REG_LDO28CTRL,
130 };
131
132 #ifdef CONFIG_OF
133 /*
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.
137  *
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.
141  */
142 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
143                                         struct device *dev)
144 {
145         struct sec_platform_data *pd;
146
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);
151         }
152
153         /*
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.
157          */
158
159         return pd;
160 }
161 #else
162 static struct sec_platform_data *sec_pmic_i2c_parse_dt_pdata(
163                                         struct device *dev)
164 {
165         return 0;
166 }
167 #endif
168
169 static inline int sec_i2c_get_driver_data(struct i2c_client *i2c,
170                                                 const struct i2c_device_id *id)
171 {
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;
177         }
178 #endif
179         return (int)id->driver_data;
180 }
181
182 static int sec_pmic_probe(struct i2c_client *i2c,
183                             const struct i2c_device_id *id)
184 {
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;
189
190         sec_pmic = devm_kzalloc(&i2c->dev, sizeof(struct sec_pmic_dev),
191                                 GFP_KERNEL);
192         if (sec_pmic == NULL)
193                 return -ENOMEM;
194
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);
200
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;
206                 }
207                 pdata->device_type = sec_pmic->type;
208         }
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;
215         }
216
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;
230         }
231
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;
238         }
239
240         sec_pmic->rtc = i2c_new_dummy(i2c->adapter, RTC_I2C_ADDR);
241         i2c_set_clientdata(sec_pmic->rtc, sec_pmic);
242
243         if (pdata && pdata->cfg_pmic_irq)
244                 pdata->cfg_pmic_irq();
245
246         sec_irq_init(sec_pmic);
247
248         pm_runtime_set_active(sec_pmic->dev);
249
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();
270         }
271
272         if (ret)
273                 goto err;
274
275         return ret;
276
277 err:
278         sec_irq_exit(sec_pmic);
279         i2c_unregister_device(sec_pmic->rtc);
280         return ret;
281 }
282
283 static int sec_pmic_remove(struct i2c_client *i2c)
284 {
285         struct sec_pmic_dev *sec_pmic = i2c_get_clientdata(i2c);
286
287         mfd_remove_devices(sec_pmic->dev);
288         sec_irq_exit(sec_pmic);
289         i2c_unregister_device(sec_pmic->rtc);
290         return 0;
291 }
292
293 static const struct i2c_device_id sec_pmic_id[] = {
294         { "sec_pmic", 0 },
295         { }
296 };
297 MODULE_DEVICE_TABLE(i2c, sec_pmic_id);
298
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),
304         },
305         .probe = sec_pmic_probe,
306         .remove = sec_pmic_remove,
307         .id_table = sec_pmic_id,
308 };
309
310 static int __init sec_pmic_init(void)
311 {
312         return i2c_add_driver(&sec_pmic_driver);
313 }
314
315 subsys_initcall(sec_pmic_init);
316
317 static void __exit sec_pmic_exit(void)
318 {
319         i2c_del_driver(&sec_pmic_driver);
320 }
321 module_exit(sec_pmic_exit);
322
323 MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>");
324 MODULE_DESCRIPTION("Core support for the S5M MFD");
325 MODULE_LICENSE("GPL");