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