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