mfd: tps65090: fix compilation warning
[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/err.h>
28
29 #define NUM_INT_REG 2
30 #define TOTAL_NUM_REG 0x18
31
32 /* interrupt status registers */
33 #define TPS65090_INT_STS        0x0
34 #define TPS65090_INT_STS2       0x1
35
36 /* interrupt mask registers */
37 #define TPS65090_INT_MSK        0x2
38 #define TPS65090_INT_MSK2       0x3
39
40 struct tps65090_irq_data {
41         u8              mask_reg;
42         u8              mask_pos;
43 };
44
45 #define TPS65090_IRQ(_reg, _mask_pos)           \
46         {                                       \
47                 .mask_reg       = (_reg),       \
48                 .mask_pos       = (_mask_pos),  \
49         }
50
51 static const struct tps65090_irq_data tps65090_irqs[] = {
52         [0]             = TPS65090_IRQ(0, 0),
53         [1]             = TPS65090_IRQ(0, 1),
54         [2]             = TPS65090_IRQ(0, 2),
55         [3]             = TPS65090_IRQ(0, 3),
56         [4]             = TPS65090_IRQ(0, 4),
57         [5]             = TPS65090_IRQ(0, 5),
58         [6]             = TPS65090_IRQ(0, 6),
59         [7]             = TPS65090_IRQ(0, 7),
60         [8]             = TPS65090_IRQ(1, 0),
61         [9]             = TPS65090_IRQ(1, 1),
62         [10]            = TPS65090_IRQ(1, 2),
63         [11]            = TPS65090_IRQ(1, 3),
64         [12]            = TPS65090_IRQ(1, 4),
65         [13]            = TPS65090_IRQ(1, 5),
66         [14]            = TPS65090_IRQ(1, 6),
67         [15]            = TPS65090_IRQ(1, 7),
68 };
69
70 static struct mfd_cell tps65090s[] = {
71         {
72                 .name = "tps65910-pmic",
73         },
74 };
75
76 static void tps65090_irq_lock(struct irq_data *data)
77 {
78         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
79
80         mutex_lock(&tps65090->irq_lock);
81 }
82
83 static void tps65090_irq_mask(struct irq_data *irq_data)
84 {
85         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
86         unsigned int __irq = irq_data->hwirq;
87         const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
88
89         tps65090_set_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
90                 data->mask_pos);
91 }
92
93 static void tps65090_irq_unmask(struct irq_data *irq_data)
94 {
95         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
96         unsigned int __irq = irq_data->irq - tps65090->irq_base;
97         const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
98
99         tps65090_clr_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
100                 data->mask_pos);
101 }
102
103 static void tps65090_irq_sync_unlock(struct irq_data *data)
104 {
105         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
106
107         mutex_unlock(&tps65090->irq_lock);
108 }
109
110 static irqreturn_t tps65090_irq(int irq, void *data)
111 {
112         struct tps65090 *tps65090 = data;
113         int ret = 0;
114         u8 status = 0, mask = 0;
115         unsigned long int acks = 0;
116         int i;
117
118         for (i = 0; i < NUM_INT_REG; i++) {
119                 ret = tps65090_read(tps65090->dev, TPS65090_INT_MSK + i, &mask);
120                 if (ret < 0) {
121                         dev_err(tps65090->dev,
122                                 "failed to read mask reg [addr:%d]\n",
123                                 TPS65090_INT_MSK + i);
124                         return IRQ_NONE;
125                 }
126                 ret = tps65090_read(tps65090->dev, TPS65090_INT_STS + i,
127                         &status);
128                 if (ret < 0) {
129                         dev_err(tps65090->dev,
130                                 "failed to read status reg [addr:%d]\n",
131                                  TPS65090_INT_STS + i);
132                         return IRQ_NONE;
133                 }
134                 if (status) {
135                         /* Ack only those interrupts which are not masked */
136                         status &= (~mask);
137                         ret = tps65090_write(tps65090->dev,
138                                         TPS65090_INT_STS + i, status);
139                         if (ret < 0) {
140                                 dev_err(tps65090->dev,
141                                         "failed to write interrupt status\n");
142                                 return IRQ_NONE;
143                         }
144                         acks |= (status << (i * 8));
145                 }
146         }
147
148         for_each_set_bit(i, &acks, ARRAY_SIZE(tps65090_irqs))
149                 handle_nested_irq(tps65090->irq_base + i);
150         return acks ? IRQ_HANDLED : IRQ_NONE;
151 }
152
153 static int __devinit tps65090_irq_init(struct tps65090 *tps65090, int irq,
154         int irq_base)
155 {
156         int i, ret;
157
158         if (!irq_base) {
159                 dev_err(tps65090->dev, "IRQ base not set\n");
160                 return -EINVAL;
161         }
162
163         mutex_init(&tps65090->irq_lock);
164
165         for (i = 0; i < NUM_INT_REG; i++)
166                 tps65090_write(tps65090->dev, TPS65090_INT_MSK + i, 0xFF);
167
168         for (i = 0; i < NUM_INT_REG; i++)
169                 tps65090_write(tps65090->dev, TPS65090_INT_STS + i, 0xff);
170
171         tps65090->irq_base = irq_base;
172         tps65090->irq_chip.name = "tps65090";
173         tps65090->irq_chip.irq_mask = tps65090_irq_mask;
174         tps65090->irq_chip.irq_unmask = tps65090_irq_unmask;
175         tps65090->irq_chip.irq_bus_lock = tps65090_irq_lock;
176         tps65090->irq_chip.irq_bus_sync_unlock = tps65090_irq_sync_unlock;
177
178         for (i = 0; i < ARRAY_SIZE(tps65090_irqs); i++) {
179                 int __irq = i + tps65090->irq_base;
180                 irq_set_chip_data(__irq, tps65090);
181                 irq_set_chip_and_handler(__irq, &tps65090->irq_chip,
182                                          handle_simple_irq);
183                 irq_set_nested_thread(__irq, 1);
184 #ifdef CONFIG_ARM
185                 set_irq_flags(__irq, IRQF_VALID);
186 #endif
187         }
188
189         ret = request_threaded_irq(irq, NULL, tps65090_irq, IRQF_ONESHOT,
190                                 "tps65090", tps65090);
191         if (!ret) {
192                 device_init_wakeup(tps65090->dev, 1);
193                 enable_irq_wake(irq);
194         }
195
196         return ret;
197 }
198
199 static bool is_volatile_reg(struct device *dev, unsigned int reg)
200 {
201         if ((reg == TPS65090_INT_STS) || (reg == TPS65090_INT_STS))
202                 return true;
203         else
204                 return false;
205 }
206
207 static const struct regmap_config tps65090_regmap_config = {
208         .reg_bits = 8,
209         .val_bits = 8,
210         .max_register = TOTAL_NUM_REG,
211         .num_reg_defaults_raw = TOTAL_NUM_REG,
212         .cache_type = REGCACHE_RBTREE,
213         .volatile_reg = is_volatile_reg,
214 };
215
216 static int __devinit tps65090_i2c_probe(struct i2c_client *client,
217                                         const struct i2c_device_id *id)
218 {
219         struct tps65090_platform_data *pdata = client->dev.platform_data;
220         struct tps65090 *tps65090;
221         int ret;
222
223         if (!pdata) {
224                 dev_err(&client->dev, "tps65090 requires platform data\n");
225                 return -EINVAL;
226         }
227
228         tps65090 = devm_kzalloc(&client->dev, sizeof(struct tps65090),
229                 GFP_KERNEL);
230         if (tps65090 == NULL)
231                 return -ENOMEM;
232
233         tps65090->client = client;
234         tps65090->dev = &client->dev;
235         i2c_set_clientdata(client, tps65090);
236
237         mutex_init(&tps65090->lock);
238
239         if (client->irq) {
240                 ret = tps65090_irq_init(tps65090, client->irq, pdata->irq_base);
241                 if (ret) {
242                         dev_err(&client->dev, "IRQ init failed with err: %d\n",
243                                 ret);
244                         goto err_exit;
245                 }
246         }
247
248         tps65090->rmap = devm_regmap_init_i2c(tps65090->client,
249                                               &tps65090_regmap_config);
250         if (IS_ERR(tps65090->rmap)) {
251                 ret = PTR_ERR(tps65090->rmap);
252                 dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
253                 goto err_irq_exit;
254         }
255
256         ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
257                 ARRAY_SIZE(tps65090s), NULL, 0);
258         if (ret) {
259                 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
260                         ret);
261                 goto err_irq_exit;
262         }
263
264         return 0;
265
266 err_irq_exit:
267         if (client->irq)
268                 free_irq(client->irq, tps65090);
269 err_exit:
270         return ret;
271 }
272
273 static int __devexit tps65090_i2c_remove(struct i2c_client *client)
274 {
275         struct tps65090 *tps65090 = i2c_get_clientdata(client);
276
277         mfd_remove_devices(tps65090->dev);
278         if (client->irq)
279                 free_irq(client->irq, tps65090);
280
281         return 0;
282 }
283
284 #ifdef CONFIG_PM_SLEEP
285 static int tps65090_suspend(struct device *dev)
286 {
287         struct i2c_client *client = to_i2c_client(dev);
288         if (client->irq)
289                 disable_irq(client->irq);
290         return 0;
291 }
292
293 static int tps65090_resume(struct device *dev)
294 {
295         struct i2c_client *client = to_i2c_client(dev);
296         if (client->irq)
297                 enable_irq(client->irq);
298         return 0;
299 }
300 #endif
301
302 static const struct dev_pm_ops tps65090_pm_ops = {
303         SET_SYSTEM_SLEEP_PM_OPS(tps65090_suspend, tps65090_resume)
304 };
305
306 static const struct i2c_device_id tps65090_id_table[] = {
307         { "tps65090", 0 },
308         { },
309 };
310 MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
311
312 static struct i2c_driver tps65090_driver = {
313         .driver = {
314                 .name   = "tps65090",
315                 .owner  = THIS_MODULE,
316                 .pm     = &tps65090_pm_ops,
317         },
318         .probe          = tps65090_i2c_probe,
319         .remove         = __devexit_p(tps65090_i2c_remove),
320         .id_table       = tps65090_id_table,
321 };
322
323 static int __init tps65090_init(void)
324 {
325         return i2c_add_driver(&tps65090_driver);
326 }
327 subsys_initcall(tps65090_init);
328
329 static void __exit tps65090_exit(void)
330 {
331         i2c_del_driver(&tps65090_driver);
332 }
333 module_exit(tps65090_exit);
334
335 MODULE_DESCRIPTION("TPS65090 core driver");
336 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
337 MODULE_LICENSE("GPL v2");