Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/viro/vfs
[linux-2.6-block.git] / drivers / mfd / tps65090.c
CommitLineData
3c33be06
VB
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>
40719314
LD
28#include <linux/of.h>
29#include <linux/of_device.h>
3c33be06
VB
30#include <linux/err.h>
31
32#define NUM_INT_REG 2
3c33be06 33
759f2598
LD
34#define TPS65090_INT1_MASK_VAC_STATUS_CHANGE 1
35#define TPS65090_INT1_MASK_VSYS_STATUS_CHANGE 2
36#define TPS65090_INT1_MASK_BAT_STATUS_CHANGE 3
37#define TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE 4
38#define TPS65090_INT1_MASK_CHARGING_COMPLETE 5
39#define TPS65090_INT1_MASK_OVERLOAD_DCDC1 6
40#define TPS65090_INT1_MASK_OVERLOAD_DCDC2 7
41#define TPS65090_INT2_MASK_OVERLOAD_DCDC3 0
42#define TPS65090_INT2_MASK_OVERLOAD_FET1 1
43#define TPS65090_INT2_MASK_OVERLOAD_FET2 2
44#define TPS65090_INT2_MASK_OVERLOAD_FET3 3
45#define TPS65090_INT2_MASK_OVERLOAD_FET4 4
46#define TPS65090_INT2_MASK_OVERLOAD_FET5 5
47#define TPS65090_INT2_MASK_OVERLOAD_FET6 6
48#define TPS65090_INT2_MASK_OVERLOAD_FET7 7
3c33be06 49
36c772e3
RK
50static struct resource charger_resources[] = {
51 {
52 .start = TPS65090_IRQ_VAC_STATUS_CHANGE,
53 .end = TPS65090_IRQ_VAC_STATUS_CHANGE,
54 .flags = IORESOURCE_IRQ,
55 }
56};
57
7d811771
DA
58enum tps65090_cells {
59 PMIC = 0,
60 CHARGER = 1,
61};
62
63static struct mfd_cell tps65090s[] = {
64 [PMIC] = {
b7e53786 65 .name = "tps65090-pmic",
3c33be06 66 },
7d811771 67 [CHARGER] = {
e2e8ffc9 68 .name = "tps65090-charger",
36c772e3
RK
69 .num_resources = ARRAY_SIZE(charger_resources),
70 .resources = &charger_resources[0],
b50cf35b 71 .of_compatible = "ti,tps65090-charger",
3c33be06
VB
72 },
73};
74
759f2598
LD
75static const struct regmap_irq tps65090_irqs[] = {
76 /* INT1 IRQs*/
77 [TPS65090_IRQ_VAC_STATUS_CHANGE] = {
12849b63 78 .mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE,
759f2598
LD
79 },
80 [TPS65090_IRQ_VSYS_STATUS_CHANGE] = {
12849b63 81 .mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE,
759f2598
LD
82 },
83 [TPS65090_IRQ_BAT_STATUS_CHANGE] = {
12849b63 84 .mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE,
759f2598
LD
85 },
86 [TPS65090_IRQ_CHARGING_STATUS_CHANGE] = {
12849b63 87 .mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE,
759f2598
LD
88 },
89 [TPS65090_IRQ_CHARGING_COMPLETE] = {
12849b63 90 .mask = TPS65090_INT1_MASK_CHARGING_COMPLETE,
759f2598
LD
91 },
92 [TPS65090_IRQ_OVERLOAD_DCDC1] = {
12849b63 93 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1,
759f2598
LD
94 },
95 [TPS65090_IRQ_OVERLOAD_DCDC2] = {
12849b63 96 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2,
759f2598
LD
97 },
98 /* INT2 IRQs*/
99 [TPS65090_IRQ_OVERLOAD_DCDC3] = {
12849b63
LJ
100 .reg_offset = 1,
101 .mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3,
759f2598
LD
102 },
103 [TPS65090_IRQ_OVERLOAD_FET1] = {
12849b63
LJ
104 .reg_offset = 1,
105 .mask = TPS65090_INT2_MASK_OVERLOAD_FET1,
759f2598
LD
106 },
107 [TPS65090_IRQ_OVERLOAD_FET2] = {
12849b63
LJ
108 .reg_offset = 1,
109 .mask = TPS65090_INT2_MASK_OVERLOAD_FET2,
759f2598
LD
110 },
111 [TPS65090_IRQ_OVERLOAD_FET3] = {
12849b63
LJ
112 .reg_offset = 1,
113 .mask = TPS65090_INT2_MASK_OVERLOAD_FET3,
759f2598
LD
114 },
115 [TPS65090_IRQ_OVERLOAD_FET4] = {
12849b63
LJ
116 .reg_offset = 1,
117 .mask = TPS65090_INT2_MASK_OVERLOAD_FET4,
759f2598
LD
118 },
119 [TPS65090_IRQ_OVERLOAD_FET5] = {
12849b63
LJ
120 .reg_offset = 1,
121 .mask = TPS65090_INT2_MASK_OVERLOAD_FET5,
759f2598
LD
122 },
123 [TPS65090_IRQ_OVERLOAD_FET6] = {
12849b63
LJ
124 .reg_offset = 1,
125 .mask = TPS65090_INT2_MASK_OVERLOAD_FET6,
759f2598
LD
126 },
127 [TPS65090_IRQ_OVERLOAD_FET7] = {
12849b63
LJ
128 .reg_offset = 1,
129 .mask = TPS65090_INT2_MASK_OVERLOAD_FET7,
759f2598
LD
130 },
131};
3c33be06 132
759f2598
LD
133static struct regmap_irq_chip tps65090_irq_chip = {
134 .name = "tps65090",
135 .irqs = tps65090_irqs,
136 .num_irqs = ARRAY_SIZE(tps65090_irqs),
137 .num_regs = NUM_INT_REG,
c42ba72e
DA
138 .status_base = TPS65090_REG_INTR_STS,
139 .mask_base = TPS65090_REG_INTR_MASK,
759f2598
LD
140 .mask_invert = true,
141};
3c33be06
VB
142
143static bool is_volatile_reg(struct device *dev, unsigned int reg)
144{
c42ba72e
DA
145 /* Nearly all registers have status bits mixed in, except a few */
146 switch (reg) {
147 case TPS65090_REG_INTR_MASK:
148 case TPS65090_REG_INTR_MASK2:
149 case TPS65090_REG_CG_CTRL0:
150 case TPS65090_REG_CG_CTRL1:
151 case TPS65090_REG_CG_CTRL2:
152 case TPS65090_REG_CG_CTRL3:
153 case TPS65090_REG_CG_CTRL4:
154 case TPS65090_REG_CG_CTRL5:
3c33be06 155 return false;
c42ba72e
DA
156 }
157 return true;
3c33be06
VB
158}
159
160static const struct regmap_config tps65090_regmap_config = {
161 .reg_bits = 8,
162 .val_bits = 8,
5c148890
MS
163 .max_register = TPS65090_MAX_REG,
164 .num_reg_defaults_raw = TPS65090_NUM_REGS,
3c33be06
VB
165 .cache_type = REGCACHE_RBTREE,
166 .volatile_reg = is_volatile_reg,
167};
168
40719314
LD
169#ifdef CONFIG_OF
170static const struct of_device_id tps65090_of_match[] = {
171 { .compatible = "ti,tps65090",},
172 {},
173};
174MODULE_DEVICE_TABLE(of, tps65090_of_match);
175#endif
176
f791be49 177static int tps65090_i2c_probe(struct i2c_client *client,
12849b63 178 const struct i2c_device_id *id)
3c33be06 179{
334a41ce 180 struct tps65090_platform_data *pdata = dev_get_platdata(&client->dev);
40719314 181 int irq_base = 0;
3c33be06
VB
182 struct tps65090 *tps65090;
183 int ret;
184
40719314
LD
185 if (!pdata && !client->dev.of_node) {
186 dev_err(&client->dev,
187 "tps65090 requires platform data or of_node\n");
3c33be06
VB
188 return -EINVAL;
189 }
190
40719314
LD
191 if (pdata)
192 irq_base = pdata->irq_base;
193
e8e6f047 194 tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
8a232f0b 195 if (!tps65090)
3c33be06
VB
196 return -ENOMEM;
197
3c33be06
VB
198 tps65090->dev = &client->dev;
199 i2c_set_clientdata(client, tps65090);
200
3863db3e 201 tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
3c33be06 202 if (IS_ERR(tps65090->rmap)) {
b683a0a6
AL
203 ret = PTR_ERR(tps65090->rmap);
204 dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
759f2598
LD
205 return ret;
206 }
207
208 if (client->irq) {
209 ret = regmap_add_irq_chip(tps65090->rmap, client->irq,
12849b63
LJ
210 IRQF_ONESHOT | IRQF_TRIGGER_LOW, irq_base,
211 &tps65090_irq_chip, &tps65090->irq_data);
212 if (ret) {
213 dev_err(&client->dev,
214 "IRQ init failed with err: %d\n", ret);
759f2598
LD
215 return ret;
216 }
7d811771
DA
217 } else {
218 /* Don't tell children they have an IRQ that'll never fire */
219 tps65090s[CHARGER].num_resources = 0;
b683a0a6 220 }
3c33be06
VB
221
222 ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
12849b63
LJ
223 ARRAY_SIZE(tps65090s), NULL,
224 0, regmap_irq_get_domain(tps65090->irq_data));
3c33be06
VB
225 if (ret) {
226 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
227 ret);
1d88f7a0 228 goto err_irq_exit;
3c33be06
VB
229 }
230
231 return 0;
232
3c33be06
VB
233err_irq_exit:
234 if (client->irq)
759f2598 235 regmap_del_irq_chip(client->irq, tps65090->irq_data);
3c33be06
VB
236 return ret;
237}
238
4740f73f 239static int tps65090_i2c_remove(struct i2c_client *client)
3c33be06
VB
240{
241 struct tps65090 *tps65090 = i2c_get_clientdata(client);
242
243 mfd_remove_devices(tps65090->dev);
3c33be06 244 if (client->irq)
759f2598 245 regmap_del_irq_chip(client->irq, tps65090->irq_data);
3c33be06
VB
246
247 return 0;
248}
249
3c33be06
VB
250static const struct i2c_device_id tps65090_id_table[] = {
251 { "tps65090", 0 },
252 { },
253};
254MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
255
256static struct i2c_driver tps65090_driver = {
257 .driver = {
258 .name = "tps65090",
40719314 259 .of_match_table = of_match_ptr(tps65090_of_match),
3c33be06
VB
260 },
261 .probe = tps65090_i2c_probe,
84449216 262 .remove = tps65090_i2c_remove,
3c33be06
VB
263 .id_table = tps65090_id_table,
264};
265
266static int __init tps65090_init(void)
267{
268 return i2c_add_driver(&tps65090_driver);
269}
270subsys_initcall(tps65090_init);
271
272static void __exit tps65090_exit(void)
273{
274 i2c_del_driver(&tps65090_driver);
275}
276module_exit(tps65090_exit);
277
278MODULE_DESCRIPTION("TPS65090 core driver");
279MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
280MODULE_LICENSE("GPL v2");