mfd: tps65090: print error message when malloc failed
[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(*tps65090), GFP_KERNEL);
229         if (!tps65090) {
230                 dev_err(&client->dev, "mem alloc for tps65090 failed\n");
231                 return -ENOMEM;
232         }
233
234         tps65090->dev = &client->dev;
235         i2c_set_clientdata(client, tps65090);
236
237         if (client->irq) {
238                 ret = tps65090_irq_init(tps65090, client->irq, pdata->irq_base);
239                 if (ret) {
240                         dev_err(&client->dev, "IRQ init failed with err: %d\n",
241                                 ret);
242                         goto err_exit;
243                 }
244         }
245
246         tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
247         if (IS_ERR(tps65090->rmap)) {
248                 ret = PTR_ERR(tps65090->rmap);
249                 dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
250                 goto err_irq_exit;
251         }
252
253         ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
254                 ARRAY_SIZE(tps65090s), NULL, 0);
255         if (ret) {
256                 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
257                         ret);
258                 goto err_irq_exit;
259         }
260
261         return 0;
262
263 err_irq_exit:
264         if (client->irq)
265                 free_irq(client->irq, tps65090);
266 err_exit:
267         return ret;
268 }
269
270 static int __devexit tps65090_i2c_remove(struct i2c_client *client)
271 {
272         struct tps65090 *tps65090 = i2c_get_clientdata(client);
273
274         mfd_remove_devices(tps65090->dev);
275         if (client->irq)
276                 free_irq(client->irq, tps65090);
277
278         return 0;
279 }
280
281 #ifdef CONFIG_PM_SLEEP
282 static int tps65090_suspend(struct device *dev)
283 {
284         struct i2c_client *client = to_i2c_client(dev);
285         if (client->irq)
286                 disable_irq(client->irq);
287         return 0;
288 }
289
290 static int tps65090_resume(struct device *dev)
291 {
292         struct i2c_client *client = to_i2c_client(dev);
293         if (client->irq)
294                 enable_irq(client->irq);
295         return 0;
296 }
297 #endif
298
299 static const struct dev_pm_ops tps65090_pm_ops = {
300         SET_SYSTEM_SLEEP_PM_OPS(tps65090_suspend, tps65090_resume)
301 };
302
303 static const struct i2c_device_id tps65090_id_table[] = {
304         { "tps65090", 0 },
305         { },
306 };
307 MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
308
309 static struct i2c_driver tps65090_driver = {
310         .driver = {
311                 .name   = "tps65090",
312                 .owner  = THIS_MODULE,
313                 .pm     = &tps65090_pm_ops,
314         },
315         .probe          = tps65090_i2c_probe,
316         .remove         = __devexit_p(tps65090_i2c_remove),
317         .id_table       = tps65090_id_table,
318 };
319
320 static int __init tps65090_init(void)
321 {
322         return i2c_add_driver(&tps65090_driver);
323 }
324 subsys_initcall(tps65090_init);
325
326 static void __exit tps65090_exit(void)
327 {
328         i2c_del_driver(&tps65090_driver);
329 }
330 module_exit(tps65090_exit);
331
332 MODULE_DESCRIPTION("TPS65090 core driver");
333 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
334 MODULE_LICENSE("GPL v2");