Merge remote-tracking branch 'regulator/topic/fan53555' into v3.9-rc8
[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/i2c.h>
26 #include <linux/mfd/core.h>
27 #include <linux/mfd/tps65090.h>
28 #include <linux/of.h>
29 #include <linux/of_device.h>
30 #include <linux/err.h>
31
32 #define NUM_INT_REG 2
33 #define TOTAL_NUM_REG 0x18
34
35 /* interrupt status registers */
36 #define TPS65090_INT_STS        0x0
37 #define TPS65090_INT_STS2       0x1
38
39 /* interrupt mask registers */
40 #define TPS65090_INT_MSK        0x2
41 #define TPS65090_INT_MSK2       0x3
42
43 #define TPS65090_INT1_MASK_VAC_STATUS_CHANGE            1
44 #define TPS65090_INT1_MASK_VSYS_STATUS_CHANGE           2
45 #define TPS65090_INT1_MASK_BAT_STATUS_CHANGE            3
46 #define TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE       4
47 #define TPS65090_INT1_MASK_CHARGING_COMPLETE            5
48 #define TPS65090_INT1_MASK_OVERLOAD_DCDC1               6
49 #define TPS65090_INT1_MASK_OVERLOAD_DCDC2               7
50 #define TPS65090_INT2_MASK_OVERLOAD_DCDC3               0
51 #define TPS65090_INT2_MASK_OVERLOAD_FET1                1
52 #define TPS65090_INT2_MASK_OVERLOAD_FET2                2
53 #define TPS65090_INT2_MASK_OVERLOAD_FET3                3
54 #define TPS65090_INT2_MASK_OVERLOAD_FET4                4
55 #define TPS65090_INT2_MASK_OVERLOAD_FET5                5
56 #define TPS65090_INT2_MASK_OVERLOAD_FET6                6
57 #define TPS65090_INT2_MASK_OVERLOAD_FET7                7
58
59 static struct mfd_cell tps65090s[] = {
60         {
61                 .name = "tps65090-pmic",
62         },
63         {
64                 .name = "tps65090-charger",
65         },
66 };
67
68 static const struct regmap_irq tps65090_irqs[] = {
69         /* INT1 IRQs*/
70         [TPS65090_IRQ_VAC_STATUS_CHANGE] = {
71                         .mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE,
72         },
73         [TPS65090_IRQ_VSYS_STATUS_CHANGE] = {
74                         .mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE,
75         },
76         [TPS65090_IRQ_BAT_STATUS_CHANGE] = {
77                         .mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE,
78         },
79         [TPS65090_IRQ_CHARGING_STATUS_CHANGE] = {
80                         .mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE,
81         },
82         [TPS65090_IRQ_CHARGING_COMPLETE] = {
83                         .mask = TPS65090_INT1_MASK_CHARGING_COMPLETE,
84         },
85         [TPS65090_IRQ_OVERLOAD_DCDC1] = {
86                         .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1,
87         },
88         [TPS65090_IRQ_OVERLOAD_DCDC2] = {
89                         .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2,
90         },
91         /* INT2 IRQs*/
92         [TPS65090_IRQ_OVERLOAD_DCDC3] = {
93                         .reg_offset = 1,
94                         .mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3,
95         },
96         [TPS65090_IRQ_OVERLOAD_FET1] = {
97                         .reg_offset = 1,
98                         .mask = TPS65090_INT2_MASK_OVERLOAD_FET1,
99         },
100         [TPS65090_IRQ_OVERLOAD_FET2] = {
101                         .reg_offset = 1,
102                         .mask = TPS65090_INT2_MASK_OVERLOAD_FET2,
103         },
104         [TPS65090_IRQ_OVERLOAD_FET3] = {
105                         .reg_offset = 1,
106                         .mask = TPS65090_INT2_MASK_OVERLOAD_FET3,
107         },
108         [TPS65090_IRQ_OVERLOAD_FET4] = {
109                         .reg_offset = 1,
110                         .mask = TPS65090_INT2_MASK_OVERLOAD_FET4,
111         },
112         [TPS65090_IRQ_OVERLOAD_FET5] = {
113                         .reg_offset = 1,
114                         .mask = TPS65090_INT2_MASK_OVERLOAD_FET5,
115         },
116         [TPS65090_IRQ_OVERLOAD_FET6] = {
117                         .reg_offset = 1,
118                         .mask = TPS65090_INT2_MASK_OVERLOAD_FET6,
119         },
120         [TPS65090_IRQ_OVERLOAD_FET7] = {
121                         .reg_offset = 1,
122                         .mask = TPS65090_INT2_MASK_OVERLOAD_FET7,
123         },
124 };
125
126 static struct regmap_irq_chip tps65090_irq_chip = {
127         .name = "tps65090",
128         .irqs = tps65090_irqs,
129         .num_irqs = ARRAY_SIZE(tps65090_irqs),
130         .num_regs = NUM_INT_REG,
131         .status_base = TPS65090_INT_STS,
132         .mask_base = TPS65090_INT_MSK,
133         .mask_invert = true,
134 };
135
136 static bool is_volatile_reg(struct device *dev, unsigned int reg)
137 {
138         if ((reg == TPS65090_INT_STS) || (reg == TPS65090_INT_STS2))
139                 return true;
140         else
141                 return false;
142 }
143
144 static const struct regmap_config tps65090_regmap_config = {
145         .reg_bits = 8,
146         .val_bits = 8,
147         .max_register = TOTAL_NUM_REG,
148         .num_reg_defaults_raw = TOTAL_NUM_REG,
149         .cache_type = REGCACHE_RBTREE,
150         .volatile_reg = is_volatile_reg,
151 };
152
153 #ifdef CONFIG_OF
154 static const struct of_device_id tps65090_of_match[] = {
155         { .compatible = "ti,tps65090",},
156         {},
157 };
158 MODULE_DEVICE_TABLE(of, tps65090_of_match);
159 #endif
160
161 static int tps65090_i2c_probe(struct i2c_client *client,
162                                         const struct i2c_device_id *id)
163 {
164         struct tps65090_platform_data *pdata = client->dev.platform_data;
165         int irq_base = 0;
166         struct tps65090 *tps65090;
167         int ret;
168
169         if (!pdata && !client->dev.of_node) {
170                 dev_err(&client->dev,
171                         "tps65090 requires platform data or of_node\n");
172                 return -EINVAL;
173         }
174
175         if (pdata)
176                 irq_base = pdata->irq_base;
177
178         tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
179         if (!tps65090) {
180                 dev_err(&client->dev, "mem alloc for tps65090 failed\n");
181                 return -ENOMEM;
182         }
183
184         tps65090->dev = &client->dev;
185         i2c_set_clientdata(client, tps65090);
186
187         tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
188         if (IS_ERR(tps65090->rmap)) {
189                 ret = PTR_ERR(tps65090->rmap);
190                 dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
191                 return ret;
192         }
193
194         if (client->irq) {
195                 ret = regmap_add_irq_chip(tps65090->rmap, client->irq,
196                         IRQF_ONESHOT | IRQF_TRIGGER_LOW, irq_base,
197                         &tps65090_irq_chip, &tps65090->irq_data);
198                         if (ret) {
199                                 dev_err(&client->dev,
200                                         "IRQ init failed with err: %d\n", ret);
201                         return ret;
202                 }
203         }
204
205         ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
206                 ARRAY_SIZE(tps65090s), NULL,
207                 0, regmap_irq_get_domain(tps65090->irq_data));
208         if (ret) {
209                 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
210                         ret);
211                 goto err_irq_exit;
212         }
213
214         return 0;
215
216 err_irq_exit:
217         if (client->irq)
218                 regmap_del_irq_chip(client->irq, tps65090->irq_data);
219         return ret;
220 }
221
222 static int tps65090_i2c_remove(struct i2c_client *client)
223 {
224         struct tps65090 *tps65090 = i2c_get_clientdata(client);
225
226         mfd_remove_devices(tps65090->dev);
227         if (client->irq)
228                 regmap_del_irq_chip(client->irq, tps65090->irq_data);
229
230         return 0;
231 }
232
233 static const struct i2c_device_id tps65090_id_table[] = {
234         { "tps65090", 0 },
235         { },
236 };
237 MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
238
239 static struct i2c_driver tps65090_driver = {
240         .driver = {
241                 .name   = "tps65090",
242                 .owner  = THIS_MODULE,
243                 .of_match_table = of_match_ptr(tps65090_of_match),
244         },
245         .probe          = tps65090_i2c_probe,
246         .remove         = tps65090_i2c_remove,
247         .id_table       = tps65090_id_table,
248 };
249
250 static int __init tps65090_init(void)
251 {
252         return i2c_add_driver(&tps65090_driver);
253 }
254 subsys_initcall(tps65090_init);
255
256 static void __exit tps65090_exit(void)
257 {
258         i2c_del_driver(&tps65090_driver);
259 }
260 module_exit(tps65090_exit);
261
262 MODULE_DESCRIPTION("TPS65090 core driver");
263 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
264 MODULE_LICENSE("GPL v2");