Merge commit 'main-jb-2012.08.03-B4' into t114-0806
[linux-2.6.git] / drivers / mfd / tps65090.c
1 /*
2  * Core driver for TI TPS65090 PMIC family
3  *
4  * Copyright (c) 2012, NVIDIA CORPORATION.  All rights reserved.
5
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 #include <linux/interrupt.h>
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 #include <linux/i2c.h>
25 #include <linux/mfd/core.h>
26 #include <linux/mfd/tps65090.h>
27 #include <linux/regmap.h>
28 #include <linux/err.h>
29
30 #define NUM_INT_REG 2
31 #define TOTAL_NUM_REG 0x18
32
33 /* interrupt status registers */
34 #define TPS65090_INT_STS        0x0
35 #define TPS65090_INT_STS2       0x1
36
37 /* interrupt mask registers */
38 #define TPS65090_INT_MSK        0x2
39 #define TPS65090_INT_MSK2       0x3
40
41 struct tps65090_irq_data {
42         u8              mask_reg;
43         u8              mask_pos;
44 };
45
46 #define TPS65090_IRQ(_reg, _mask_pos)           \
47         {                                       \
48                 .mask_reg       = (_reg),       \
49                 .mask_pos       = (_mask_pos),  \
50         }
51
52 static const struct tps65090_irq_data tps65090_irqs[] = {
53         [0]             = TPS65090_IRQ(0, 0),
54         [1]             = TPS65090_IRQ(0, 1),
55         [2]             = TPS65090_IRQ(0, 2),
56         [3]             = TPS65090_IRQ(0, 3),
57         [4]             = TPS65090_IRQ(0, 4),
58         [5]             = TPS65090_IRQ(0, 5),
59         [6]             = TPS65090_IRQ(0, 6),
60         [7]             = TPS65090_IRQ(0, 7),
61         [8]             = TPS65090_IRQ(1, 0),
62         [9]             = TPS65090_IRQ(1, 1),
63         [10]            = TPS65090_IRQ(1, 2),
64         [11]            = TPS65090_IRQ(1, 3),
65         [12]            = TPS65090_IRQ(1, 4),
66         [13]            = TPS65090_IRQ(1, 5),
67         [14]            = TPS65090_IRQ(1, 6),
68         [15]            = TPS65090_IRQ(1, 7),
69 };
70
71 static struct mfd_cell tps65090s[] = {
72         {
73                 .name = "tps65910-pmic",
74         },
75         {
76                 .name = "tps65910-regulator",
77         },
78 };
79
80 struct tps65090 {
81         struct mutex            lock;
82         struct device           *dev;
83         struct i2c_client       *client;
84         struct regmap           *rmap;
85         struct irq_chip         irq_chip;
86         struct mutex            irq_lock;
87         int                     irq_base;
88         unsigned int            id;
89 };
90
91 int tps65090_write(struct device *dev, int reg, uint8_t val)
92 {
93         struct tps65090 *tps = dev_get_drvdata(dev);
94         return regmap_write(tps->rmap, reg, val);
95 }
96 EXPORT_SYMBOL_GPL(tps65090_write);
97
98 int tps65090_read(struct device *dev, int reg, uint8_t *val)
99 {
100         struct tps65090 *tps = dev_get_drvdata(dev);
101         unsigned int temp_val;
102         int ret;
103         ret = regmap_read(tps->rmap, reg, &temp_val);
104         if (!ret)
105                 *val = temp_val;
106         return ret;
107 }
108 EXPORT_SYMBOL_GPL(tps65090_read);
109
110 int tps65090_set_bits(struct device *dev, int reg, uint8_t bit_num)
111 {
112         struct tps65090 *tps = dev_get_drvdata(dev);
113         return regmap_update_bits(tps->rmap, reg, BIT(bit_num), ~0u);
114 }
115 EXPORT_SYMBOL_GPL(tps65090_set_bits);
116
117 int tps65090_clr_bits(struct device *dev, int reg, uint8_t bit_num)
118 {
119         struct tps65090 *tps = dev_get_drvdata(dev);
120         return regmap_update_bits(tps->rmap, reg, BIT(bit_num), 0u);
121 }
122 EXPORT_SYMBOL_GPL(tps65090_clr_bits);
123
124 static void tps65090_irq_lock(struct irq_data *data)
125 {
126         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
127
128         mutex_lock(&tps65090->irq_lock);
129 }
130
131 static void tps65090_irq_mask(struct irq_data *irq_data)
132 {
133         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
134         unsigned int __irq = irq_data->hwirq;
135         const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
136
137         tps65090_set_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
138                 data->mask_pos);
139 }
140
141 static void tps65090_irq_unmask(struct irq_data *irq_data)
142 {
143         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
144         unsigned int __irq = irq_data->irq - tps65090->irq_base;
145         const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
146
147         tps65090_clr_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
148                 data->mask_pos);
149 }
150
151 static void tps65090_irq_sync_unlock(struct irq_data *data)
152 {
153         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
154
155         mutex_unlock(&tps65090->irq_lock);
156 }
157
158 static irqreturn_t tps65090_irq(int irq, void *data)
159 {
160         struct tps65090 *tps65090 = data;
161         int ret = 0;
162         u8 status = 0, mask = 0;
163         unsigned long int acks = 0;
164         int i;
165
166         for (i = 0; i < NUM_INT_REG; i++) {
167                 ret = tps65090_read(tps65090->dev, TPS65090_INT_MSK + i, &mask);
168                 if (ret < 0) {
169                         dev_err(tps65090->dev,
170                                 "failed to read mask reg [addr:%d]\n",
171                                 TPS65090_INT_MSK + i);
172                         return IRQ_NONE;
173                 }
174                 ret = tps65090_read(tps65090->dev, TPS65090_INT_STS + i,
175                         &status);
176                 if (ret < 0) {
177                         dev_err(tps65090->dev,
178                                 "failed to read status reg [addr:%d]\n",
179                                  TPS65090_INT_STS + i);
180                         return IRQ_NONE;
181                 }
182                 if (status) {
183                         /* Ack only those interrupts which are not masked */
184                         status &= (~mask);
185                         ret = tps65090_write(tps65090->dev,
186                                         TPS65090_INT_STS + i, status);
187                         if (ret < 0) {
188                                 dev_err(tps65090->dev,
189                                         "failed to write interrupt status\n");
190                                 return IRQ_NONE;
191                         }
192                         acks |= (status << (i * 8));
193                 }
194         }
195
196         for_each_set_bit(i, &acks, ARRAY_SIZE(tps65090_irqs))
197                 handle_nested_irq(tps65090->irq_base + i);
198         return acks ? IRQ_HANDLED : IRQ_NONE;
199 }
200
201 static int __devinit tps65090_irq_init(struct tps65090 *tps65090, int irq,
202         int irq_base)
203 {
204         int i, ret;
205
206         if (!irq_base) {
207                 dev_err(tps65090->dev, "IRQ base not set\n");
208                 return -EINVAL;
209         }
210
211         mutex_init(&tps65090->irq_lock);
212
213         for (i = 0; i < NUM_INT_REG; i++)
214                 tps65090_write(tps65090->dev, TPS65090_INT_MSK + i, 0xFF);
215
216         for (i = 0; i < NUM_INT_REG; i++)
217                 tps65090_write(tps65090->dev, TPS65090_INT_STS + i, 0xff);
218
219         tps65090->irq_base = irq_base;
220         tps65090->irq_chip.name = "tps65090";
221         tps65090->irq_chip.irq_mask = tps65090_irq_mask;
222         tps65090->irq_chip.irq_unmask = tps65090_irq_unmask;
223         tps65090->irq_chip.irq_bus_lock = tps65090_irq_lock;
224         tps65090->irq_chip.irq_bus_sync_unlock = tps65090_irq_sync_unlock;
225
226         for (i = 0; i < ARRAY_SIZE(tps65090_irqs); i++) {
227                 int __irq = i + tps65090->irq_base;
228                 irq_set_chip_data(__irq, tps65090);
229                 irq_set_chip_and_handler(__irq, &tps65090->irq_chip,
230                                          handle_simple_irq);
231                 irq_set_nested_thread(__irq, 1);
232 #ifdef CONFIG_ARM
233                 set_irq_flags(__irq, IRQF_VALID);
234 #endif
235         }
236
237         ret = request_threaded_irq(irq, NULL, tps65090_irq, IRQF_ONESHOT,
238                                 "tps65090", tps65090);
239         if (!ret) {
240                 device_init_wakeup(tps65090->dev, 1);
241                 enable_irq_wake(irq);
242         }
243
244         return ret;
245 }
246
247 static bool is_volatile_reg(struct device *dev, unsigned int reg)
248 {
249         if ((reg == TPS65090_INT_STS) || (reg == TPS65090_INT_STS))
250                 return true;
251         else
252                 return false;
253 }
254
255 static const struct regmap_config tps65090_regmap_config = {
256         .reg_bits = 8,
257         .val_bits = 8,
258         .max_register = TOTAL_NUM_REG,
259         .num_reg_defaults_raw = TOTAL_NUM_REG,
260         .cache_type = REGCACHE_RBTREE,
261         .volatile_reg = is_volatile_reg,
262 };
263
264 static int __devinit tps65090_i2c_probe(struct i2c_client *client,
265                                         const struct i2c_device_id *id)
266 {
267         struct tps65090_platform_data *pdata = client->dev.platform_data;
268         struct tps65090 *tps65090;
269         int ret;
270
271         if (!pdata) {
272                 dev_err(&client->dev, "tps65090 requires platform data\n");
273                 return -EINVAL;
274         }
275
276         tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
277         if (!tps65090) {
278                 dev_err(&client->dev, "mem alloc for tps65090 failed\n");
279                 return -ENOMEM;
280         }
281
282         tps65090->dev = &client->dev;
283         i2c_set_clientdata(client, tps65090);
284
285         if (client->irq) {
286                 ret = tps65090_irq_init(tps65090, client->irq, pdata->irq_base);
287                 if (ret) {
288                         dev_err(&client->dev, "IRQ init failed with err: %d\n",
289                                 ret);
290                         goto err_exit;
291                 }
292         }
293
294         tps65090->rmap = regmap_init_i2c(tps65090->client,
295                 &tps65090_regmap_config);
296         if (IS_ERR(tps65090->rmap)) {
297                 dev_err(&client->dev, "regmap_init failed with err: %ld\n",
298                         PTR_ERR(tps65090->rmap));
299                 goto err_irq_exit;
300         };
301
302         ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
303                 ARRAY_SIZE(tps65090s), NULL, 0);
304         if (ret) {
305                 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
306                         ret);
307                 goto err_regmap_exit;
308         }
309
310         return 0;
311
312 err_regmap_exit:
313         regmap_exit(tps65090->rmap);
314
315 err_irq_exit:
316         if (client->irq)
317                 free_irq(client->irq, tps65090);
318 err_exit:
319         return ret;
320 }
321
322 static int __devexit tps65090_i2c_remove(struct i2c_client *client)
323 {
324         struct tps65090 *tps65090 = i2c_get_clientdata(client);
325
326         mfd_remove_devices(tps65090->dev);
327         regmap_exit(tps65090->rmap);
328         if (client->irq)
329                 free_irq(client->irq, tps65090);
330
331         return 0;
332 }
333
334 #ifdef CONFIG_PM
335 static int tps65090_i2c_suspend(struct i2c_client *client, pm_message_t state)
336 {
337         struct i2c_client *client = to_i2c_client(dev);
338         if (client->irq)
339                 disable_irq(client->irq);
340         return 0;
341 }
342
343 static int tps65090_resume(struct device *dev)
344 {
345         struct i2c_client *client = to_i2c_client(dev);
346         if (client->irq)
347                 enable_irq(client->irq);
348         return 0;
349 }
350 #endif
351
352 static const struct i2c_device_id tps65090_id_table[] = {
353         { "tps65090", 0 },
354         { },
355 };
356 MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
357
358 static struct i2c_driver tps65090_driver = {
359         .driver = {
360                 .name   = "tps65090",
361                 .owner  = THIS_MODULE,
362                 .pm     = &tps65090_pm_ops,
363         },
364         .probe          = tps65090_i2c_probe,
365         .remove         = __devexit_p(tps65090_i2c_remove),
366         .id_table       = tps65090_id_table,
367 };
368
369 static int __init tps65090_init(void)
370 {
371         return i2c_add_driver(&tps65090_driver);
372 }
373 subsys_initcall(tps65090_init);
374
375 static void __exit tps65090_exit(void)
376 {
377         i2c_del_driver(&tps65090_driver);
378 }
379 module_exit(tps65090_exit);
380
381 MODULE_DESCRIPTION("TPS65090 core driver");
382 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
383 MODULE_LICENSE("GPL v2");