CHROMIUM: drm/exynos: Fixup init failure cleanup path
[cascardo/linux.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/irq.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
25 #include <linux/power_supply.h>
26 #include <linux/i2c.h>
27 #include <linux/mfd/core.h>
28 #include <linux/mfd/tps65090.h>
29 #include <linux/regmap.h>
30 #include <linux/err.h>
31 #include <linux/irqdomain.h>
32
33 #define NUM_INT_REG 2
34 #define TOTAL_NUM_REG 0x18
35
36 /* interrupt status registers */
37 #define TPS65090_INT_STS        0x0
38 #define TPS65090_INT_STS2       0x1
39
40 /* interrupt mask registers */
41 #define TPS65090_INT_MSK        0x2
42 #define TPS65090_INT_MSK2       0x3
43
44 /* FET control registers */
45 #define TPS65090_FET1_CTRL      0xf
46 #define TPS65090_FET7_CTRL      0x15
47
48 struct tps65090_irq_data {
49         u8              mask_reg;
50         u8              mask_pos;
51 };
52
53 #define TPS65090_IRQ(_reg, _mask_pos)           \
54         {                                       \
55                 .mask_reg       = (_reg),       \
56                 .mask_pos       = (_mask_pos),  \
57         }
58
59 static const struct tps65090_irq_data tps65090_irqs[] = {
60         [0]             = TPS65090_IRQ(0, 0),
61         [1]             = TPS65090_IRQ(0, 1),
62         [2]             = TPS65090_IRQ(0, 2),
63         [3]             = TPS65090_IRQ(0, 3),
64         [4]             = TPS65090_IRQ(0, 4),
65         [5]             = TPS65090_IRQ(0, 5),
66         [6]             = TPS65090_IRQ(0, 6),
67         [7]             = TPS65090_IRQ(0, 7),
68         [8]             = TPS65090_IRQ(1, 0),
69         [9]             = TPS65090_IRQ(1, 1),
70         [10]            = TPS65090_IRQ(1, 2),
71         [11]            = TPS65090_IRQ(1, 3),
72         [12]            = TPS65090_IRQ(1, 4),
73         [13]            = TPS65090_IRQ(1, 5),
74         [14]            = TPS65090_IRQ(1, 6),
75         [15]            = TPS65090_IRQ(1, 7),
76 };
77
78 static struct mfd_cell tps65090s[] = {
79         {
80                 .name = "tps65090-pmic",
81         },
82         {
83                 .name = "tps65090-regulator",
84         },
85         {
86                 .name = "tps65090-charger",
87         },
88 };
89
90 struct tps65090 {
91         struct mutex            lock;
92         struct device           *dev;
93         struct i2c_client       *client;
94         struct regmap           *rmap;
95         struct irq_chip         irq_chip;
96         struct mutex            irq_lock;
97         int                     irq_base;
98         unsigned int            id;
99         struct power_supply     *charger;
100         bool                    no_ack_irq;
101 };
102
103 int tps65090_write(struct device *dev, int reg, uint8_t val)
104 {
105         struct tps65090 *tps = dev_get_drvdata(dev);
106         return regmap_write(tps->rmap, reg, val);
107 }
108 EXPORT_SYMBOL_GPL(tps65090_write);
109
110 int tps65090_read(struct device *dev, int reg, uint8_t *val)
111 {
112         struct tps65090 *tps = dev_get_drvdata(dev);
113         unsigned int temp_val;
114         int ret;
115         ret = regmap_read(tps->rmap, reg, &temp_val);
116         if (!ret)
117                 *val = temp_val;
118         return ret;
119 }
120 EXPORT_SYMBOL_GPL(tps65090_read);
121
122 int tps65090_update_bits(struct device *dev, int reg, unsigned int mask,
123                          unsigned int val)
124 {
125         struct tps65090 *tps = dev_get_drvdata(dev);
126         return regmap_update_bits(tps->rmap, reg, mask, val);
127 }
128 EXPORT_SYMBOL_GPL(tps65090_update_bits);
129
130 int tps65090_set_bits(struct device *dev, int reg, uint8_t bit_num)
131 {
132         struct tps65090 *tps = dev_get_drvdata(dev);
133         return regmap_update_bits(tps->rmap, reg, BIT(bit_num), ~0u);
134 }
135 EXPORT_SYMBOL_GPL(tps65090_set_bits);
136
137 int tps65090_clr_bits(struct device *dev, int reg, uint8_t bit_num)
138 {
139         struct tps65090 *tps = dev_get_drvdata(dev);
140         return regmap_update_bits(tps->rmap, reg, BIT(bit_num), 0u);
141 }
142 EXPORT_SYMBOL_GPL(tps65090_clr_bits);
143
144 static void tps65090_irq_lock(struct irq_data *data)
145 {
146         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
147
148         mutex_lock(&tps65090->irq_lock);
149 }
150
151 static void tps65090_irq_mask(struct irq_data *irq_data)
152 {
153         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
154         unsigned int __irq = irq_data->hwirq;
155         const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
156
157         tps65090_set_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
158                 data->mask_pos);
159 }
160
161 static void tps65090_irq_unmask(struct irq_data *irq_data)
162 {
163         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
164         unsigned int __irq = irq_data->irq - tps65090->irq_base;
165         const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
166
167         tps65090_clr_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
168                 data->mask_pos);
169 }
170
171 static void tps65090_irq_sync_unlock(struct irq_data *data)
172 {
173         struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
174
175         mutex_unlock(&tps65090->irq_lock);
176 }
177
178 static irqreturn_t tps65090_irq(int irq, void *data)
179 {
180         struct tps65090 *tps65090 = data;
181         int ret = 0;
182         u8 status, mask;
183         unsigned long int acks = 0;
184         int i;
185
186         if (!tps65090->charger)
187                 tps65090->charger =
188                         power_supply_get_by_name("tps65090-charger");
189         if (tps65090->charger)
190                 power_supply_changed((struct power_supply *)tps65090->charger);
191
192         if (tps65090->no_ack_irq)
193                 return IRQ_HANDLED;
194
195         for (i = 0; i < NUM_INT_REG; i++) {
196                 ret = tps65090_read(tps65090->dev, TPS65090_INT_MSK + i, &mask);
197                 if (ret < 0) {
198                         dev_err(tps65090->dev,
199                                 "failed to read mask reg [addr:%d]\n",
200                                 TPS65090_INT_MSK + i);
201                         return IRQ_NONE;
202                 }
203                 ret = tps65090_read(tps65090->dev, TPS65090_INT_STS + i,
204                         &status);
205                 if (ret < 0) {
206                         dev_err(tps65090->dev,
207                                 "failed to read status reg [addr:%d]\n",
208                                  TPS65090_INT_STS + i);
209                         return IRQ_NONE;
210                 }
211                 if (status) {
212                         /* Ack only those interrupts which are not masked */
213                         status &= (~mask);
214                         ret = tps65090_write(tps65090->dev,
215                                         TPS65090_INT_STS + i, status);
216                         if (ret < 0) {
217                                 dev_err(tps65090->dev,
218                                         "failed to write interrupt status\n");
219                                 return IRQ_NONE;
220                         }
221                         acks |= (status << (i * 8));
222                 }
223         }
224
225         for_each_set_bit(i, &acks, ARRAY_SIZE(tps65090_irqs))
226                 handle_nested_irq(tps65090->irq_base + i);
227         return acks ? IRQ_HANDLED : IRQ_NONE;
228 }
229
230 static int __devinit tps65090_irq_init(struct tps65090 *tps65090, int irq)
231 {
232         int i, ret;
233         int irq_base;
234         int nr_irqs = ARRAY_SIZE(tps65090_irqs);
235
236         irq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
237         if (IS_ERR_VALUE(irq_base)) {
238                 dev_err(tps65090->dev, "Fail to allocate IRQ descs\n");
239                 return irq_base;
240         }
241
242         irq_domain_add_legacy(tps65090->dev->of_node, nr_irqs, irq_base, 0,
243                               &irq_domain_simple_ops, NULL);
244
245         mutex_init(&tps65090->irq_lock);
246
247         for (i = 0; i < NUM_INT_REG; i++)
248                 tps65090_write(tps65090->dev, TPS65090_INT_MSK + i, 0xFF);
249
250         for (i = 0; i < NUM_INT_REG; i++)
251                 tps65090_write(tps65090->dev, TPS65090_INT_STS + i, 0xff);
252
253         tps65090->irq_base = irq_base;
254         tps65090->irq_chip.name = "tps65090";
255         tps65090->irq_chip.irq_mask = tps65090_irq_mask;
256         tps65090->irq_chip.irq_unmask = tps65090_irq_unmask;
257         tps65090->irq_chip.irq_bus_lock = tps65090_irq_lock;
258         tps65090->irq_chip.irq_bus_sync_unlock = tps65090_irq_sync_unlock;
259
260         tps65090->charger = power_supply_get_by_name("tps65090-charger");
261
262         for (i = 0; i < ARRAY_SIZE(tps65090_irqs); i++) {
263                 int __irq = i + tps65090->irq_base;
264                 irq_set_chip_data(__irq, tps65090);
265                 irq_set_chip_and_handler(__irq, &tps65090->irq_chip,
266                                          handle_simple_irq);
267                 irq_set_nested_thread(__irq, 1);
268 #ifdef CONFIG_ARM
269                 set_irq_flags(__irq, IRQF_VALID);
270 #endif
271         }
272
273         ret = request_threaded_irq(irq, NULL, tps65090_irq,
274                                    IRQF_ONESHOT | IRQF_TRIGGER_RISING,
275                                    "tps65090", tps65090);
276         if (ret) {
277                 dev_err(tps65090->dev, "failed to request threaded irq\n");
278                 irq_free_descs(tps65090->irq_base, nr_irqs);
279                 return ret;
280         }
281
282         return 0;
283 }
284
285 static bool is_volatile_reg(struct device *dev, unsigned int reg)
286 {
287         if ((reg == TPS65090_INT_STS) || (reg == TPS65090_INT_STS2) ||
288                 (reg >= TPS65090_FET1_CTRL && reg <= TPS65090_FET7_CTRL))
289                 return true;
290         else
291                 return false;
292 }
293
294 static const struct regmap_config tps65090_regmap_config = {
295         .reg_bits = 8,
296         .val_bits = 8,
297         .max_register = TOTAL_NUM_REG,
298         .num_reg_defaults_raw = TOTAL_NUM_REG,
299         .cache_type = REGCACHE_RBTREE,
300         .volatile_reg = is_volatile_reg,
301 };
302
303 static int __devinit tps65090_i2c_probe(struct i2c_client *client,
304                                         const struct i2c_device_id *id)
305 {
306         struct tps65090 *tps65090;
307         int ret;
308
309         tps65090 = devm_kzalloc(&client->dev, sizeof(struct tps65090),
310                 GFP_KERNEL);
311         if (tps65090 == NULL)
312                 return -ENOMEM;
313
314         tps65090->client = client;
315         tps65090->dev = &client->dev;
316         i2c_set_clientdata(client, tps65090);
317
318         mutex_init(&tps65090->lock);
319
320         tps65090->rmap = regmap_init_i2c(tps65090->client,
321                 &tps65090_regmap_config);
322         if (IS_ERR(tps65090->rmap)) {
323                 dev_err(&client->dev, "regmap_init failed with err: %ld\n",
324                         PTR_ERR(tps65090->rmap));
325                 ret = -EINVAL;
326                 goto err_exit;
327         };
328
329 #ifdef CONFIG_OF
330         if (of_find_property(tps65090->dev->of_node, "no-ack-irq", NULL)) {
331                 dev_info(tps65090->dev, "Disabling irq acks to chip.\n");
332                 tps65090->no_ack_irq = true;
333         }
334 #endif
335
336         if (client->irq) {
337                 ret = tps65090_irq_init(tps65090, client->irq);
338                 if (ret) {
339                         dev_err(&client->dev, "IRQ init failed with err: %d\n",
340                                 ret);
341                         goto err_exit;
342                 }
343         }
344         ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
345                 ARRAY_SIZE(tps65090s), NULL, 0);
346         if (ret) {
347                 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
348                         ret);
349                 goto err_regmap_exit;
350         }
351
352         return 0;
353
354 err_regmap_exit:
355         regmap_exit(tps65090->rmap);
356
357         if (client->irq) {
358                 free_irq(client->irq, tps65090);
359                 irq_free_descs(tps65090->irq_base, ARRAY_SIZE(tps65090_irqs));
360         }
361 err_exit:
362         return ret;
363 }
364
365 static int __devexit tps65090_i2c_remove(struct i2c_client *client)
366 {
367         struct tps65090 *tps65090 = i2c_get_clientdata(client);
368
369         if (device_can_wakeup(tps65090->dev))
370                 device_init_wakeup(tps65090->dev, 0);
371         mfd_remove_devices(tps65090->dev);
372         regmap_exit(tps65090->rmap);
373         if (client->irq) {
374                 free_irq(client->irq, tps65090);
375                 irq_free_descs(tps65090->irq_base, ARRAY_SIZE(tps65090_irqs));
376         }
377
378         return 0;
379 }
380
381 #ifdef CONFIG_PM
382 static int tps65090_i2c_suspend(struct i2c_client *client, pm_message_t state)
383 {
384         if (client->irq) {
385                 if (device_may_wakeup(&client->dev))
386                         enable_irq_wake(client->irq);
387                 disable_irq(client->irq);
388         }
389         return 0;
390 }
391
392 static int tps65090_i2c_resume(struct i2c_client *client)
393 {
394         if (client->irq) {
395                 enable_irq(client->irq);
396                 if (device_may_wakeup(&client->dev))
397                         disable_irq_wake(client->irq);
398         }
399         return 0;
400 }
401 #endif
402
403 static const struct i2c_device_id tps65090_id_table[] = {
404         { "tps65090", 0 },
405         { },
406 };
407 MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
408
409 static struct i2c_driver tps65090_driver = {
410         .driver = {
411                 .name   = "tps65090",
412                 .owner  = THIS_MODULE,
413         },
414         .probe          = tps65090_i2c_probe,
415         .remove         = __devexit_p(tps65090_i2c_remove),
416 #ifdef CONFIG_PM
417         .suspend        = tps65090_i2c_suspend,
418         .resume         = tps65090_i2c_resume,
419 #endif
420         .id_table       = tps65090_id_table,
421 };
422
423 static int __init tps65090_init(void)
424 {
425         return i2c_add_driver(&tps65090_driver);
426 }
427 subsys_initcall(tps65090_init);
428
429 static void __exit tps65090_exit(void)
430 {
431         i2c_del_driver(&tps65090_driver);
432 }
433 module_exit(tps65090_exit);
434
435 MODULE_DESCRIPTION("TPS65090 core driver");
436 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
437 MODULE_LICENSE("GPL v2");