blob: bd6235308c6b9d625e6c6220b941e12378a40dcf [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Venu Byravarasu3c33be062012-03-16 11:10:19 +05302/*
3 * Core driver for TI TPS65090 PMIC family
4 *
5 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Paul Gortmaker26fce5e2019-01-13 13:36:41 -05006 *
7 * Author: Venu Byravarasu <vbyravarasu@nvidia.com>
Venu Byravarasu3c33be062012-03-16 11:10:19 +05308 */
9
10#include <linux/interrupt.h>
11#include <linux/irq.h>
12#include <linux/kernel.h>
Paul Gortmaker26fce5e2019-01-13 13:36:41 -050013#include <linux/init.h>
Venu Byravarasu3c33be062012-03-16 11:10:19 +053014#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>
Laxman Dewangan40719312013-01-29 14:35:15 +053019#include <linux/of.h>
20#include <linux/of_device.h>
Venu Byravarasu3c33be062012-03-16 11:10:19 +053021#include <linux/err.h>
22
23#define NUM_INT_REG 2
Venu Byravarasu3c33be062012-03-16 11:10:19 +053024
Laxman Dewangan759f2592012-11-20 08:44:49 +053025#define TPS65090_INT1_MASK_VAC_STATUS_CHANGE 1
26#define TPS65090_INT1_MASK_VSYS_STATUS_CHANGE 2
27#define TPS65090_INT1_MASK_BAT_STATUS_CHANGE 3
28#define TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE 4
29#define TPS65090_INT1_MASK_CHARGING_COMPLETE 5
30#define TPS65090_INT1_MASK_OVERLOAD_DCDC1 6
31#define TPS65090_INT1_MASK_OVERLOAD_DCDC2 7
32#define TPS65090_INT2_MASK_OVERLOAD_DCDC3 0
33#define TPS65090_INT2_MASK_OVERLOAD_FET1 1
34#define TPS65090_INT2_MASK_OVERLOAD_FET2 2
35#define TPS65090_INT2_MASK_OVERLOAD_FET3 3
36#define TPS65090_INT2_MASK_OVERLOAD_FET4 4
37#define TPS65090_INT2_MASK_OVERLOAD_FET5 5
38#define TPS65090_INT2_MASK_OVERLOAD_FET6 6
39#define TPS65090_INT2_MASK_OVERLOAD_FET7 7
Venu Byravarasu3c33be062012-03-16 11:10:19 +053040
Rikard Falkebornc4a164f2020-09-22 21:26:59 +020041static const struct resource charger_resources[] = {
Rhyland Klein36c772e2013-03-12 18:08:07 -040042 {
43 .start = TPS65090_IRQ_VAC_STATUS_CHANGE,
44 .end = TPS65090_IRQ_VAC_STATUS_CHANGE,
45 .flags = IORESOURCE_IRQ,
46 }
47};
48
Doug Anderson7d811772014-04-16 16:12:25 -070049enum tps65090_cells {
50 PMIC = 0,
51 CHARGER = 1,
52};
53
54static struct mfd_cell tps65090s[] = {
55 [PMIC] = {
Venu Byravarasub7e53782012-07-18 18:33:42 +053056 .name = "tps65090-pmic",
Venu Byravarasu3c33be062012-03-16 11:10:19 +053057 },
Doug Anderson7d811772014-04-16 16:12:25 -070058 [CHARGER] = {
Laxman Dewangane2e8ffc2012-11-20 08:44:45 +053059 .name = "tps65090-charger",
Rhyland Klein36c772e2013-03-12 18:08:07 -040060 .num_resources = ARRAY_SIZE(charger_resources),
61 .resources = &charger_resources[0],
Rhyland Kleinb50cf352013-04-10 13:14:55 -040062 .of_compatible = "ti,tps65090-charger",
Venu Byravarasu3c33be062012-03-16 11:10:19 +053063 },
64};
65
Laxman Dewangan759f2592012-11-20 08:44:49 +053066static const struct regmap_irq tps65090_irqs[] = {
67 /* INT1 IRQs*/
68 [TPS65090_IRQ_VAC_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000069 .mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053070 },
71 [TPS65090_IRQ_VSYS_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000072 .mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053073 },
74 [TPS65090_IRQ_BAT_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000075 .mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053076 },
77 [TPS65090_IRQ_CHARGING_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000078 .mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053079 },
80 [TPS65090_IRQ_CHARGING_COMPLETE] = {
Lee Jones12849b62014-11-10 12:28:36 +000081 .mask = TPS65090_INT1_MASK_CHARGING_COMPLETE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053082 },
83 [TPS65090_IRQ_OVERLOAD_DCDC1] = {
Lee Jones12849b62014-11-10 12:28:36 +000084 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1,
Laxman Dewangan759f2592012-11-20 08:44:49 +053085 },
86 [TPS65090_IRQ_OVERLOAD_DCDC2] = {
Lee Jones12849b62014-11-10 12:28:36 +000087 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2,
Laxman Dewangan759f2592012-11-20 08:44:49 +053088 },
89 /* INT2 IRQs*/
90 [TPS65090_IRQ_OVERLOAD_DCDC3] = {
Lee Jones12849b62014-11-10 12:28:36 +000091 .reg_offset = 1,
92 .mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3,
Laxman Dewangan759f2592012-11-20 08:44:49 +053093 },
94 [TPS65090_IRQ_OVERLOAD_FET1] = {
Lee Jones12849b62014-11-10 12:28:36 +000095 .reg_offset = 1,
96 .mask = TPS65090_INT2_MASK_OVERLOAD_FET1,
Laxman Dewangan759f2592012-11-20 08:44:49 +053097 },
98 [TPS65090_IRQ_OVERLOAD_FET2] = {
Lee Jones12849b62014-11-10 12:28:36 +000099 .reg_offset = 1,
100 .mask = TPS65090_INT2_MASK_OVERLOAD_FET2,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530101 },
102 [TPS65090_IRQ_OVERLOAD_FET3] = {
Lee Jones12849b62014-11-10 12:28:36 +0000103 .reg_offset = 1,
104 .mask = TPS65090_INT2_MASK_OVERLOAD_FET3,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530105 },
106 [TPS65090_IRQ_OVERLOAD_FET4] = {
Lee Jones12849b62014-11-10 12:28:36 +0000107 .reg_offset = 1,
108 .mask = TPS65090_INT2_MASK_OVERLOAD_FET4,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530109 },
110 [TPS65090_IRQ_OVERLOAD_FET5] = {
Lee Jones12849b62014-11-10 12:28:36 +0000111 .reg_offset = 1,
112 .mask = TPS65090_INT2_MASK_OVERLOAD_FET5,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530113 },
114 [TPS65090_IRQ_OVERLOAD_FET6] = {
Lee Jones12849b62014-11-10 12:28:36 +0000115 .reg_offset = 1,
116 .mask = TPS65090_INT2_MASK_OVERLOAD_FET6,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530117 },
118 [TPS65090_IRQ_OVERLOAD_FET7] = {
Lee Jones12849b62014-11-10 12:28:36 +0000119 .reg_offset = 1,
120 .mask = TPS65090_INT2_MASK_OVERLOAD_FET7,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530121 },
122};
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530123
Laxman Dewangan759f2592012-11-20 08:44:49 +0530124static struct regmap_irq_chip tps65090_irq_chip = {
125 .name = "tps65090",
126 .irqs = tps65090_irqs,
127 .num_irqs = ARRAY_SIZE(tps65090_irqs),
128 .num_regs = NUM_INT_REG,
Doug Andersonc42ba722014-04-16 16:12:27 -0700129 .status_base = TPS65090_REG_INTR_STS,
130 .mask_base = TPS65090_REG_INTR_MASK,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530131 .mask_invert = true,
132};
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530133
134static bool is_volatile_reg(struct device *dev, unsigned int reg)
135{
Doug Andersonc42ba722014-04-16 16:12:27 -0700136 /* Nearly all registers have status bits mixed in, except a few */
137 switch (reg) {
138 case TPS65090_REG_INTR_MASK:
139 case TPS65090_REG_INTR_MASK2:
140 case TPS65090_REG_CG_CTRL0:
141 case TPS65090_REG_CG_CTRL1:
142 case TPS65090_REG_CG_CTRL2:
143 case TPS65090_REG_CG_CTRL3:
144 case TPS65090_REG_CG_CTRL4:
145 case TPS65090_REG_CG_CTRL5:
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530146 return false;
Doug Andersonc42ba722014-04-16 16:12:27 -0700147 }
148 return true;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530149}
150
151static const struct regmap_config tps65090_regmap_config = {
152 .reg_bits = 8,
153 .val_bits = 8,
Maciej S. Szmigiero5c148892016-01-31 23:00:06 +0100154 .max_register = TPS65090_MAX_REG,
155 .num_reg_defaults_raw = TPS65090_NUM_REGS,
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530156 .cache_type = REGCACHE_RBTREE,
157 .volatile_reg = is_volatile_reg,
158};
159
Laxman Dewangan40719312013-01-29 14:35:15 +0530160#ifdef CONFIG_OF
161static const struct of_device_id tps65090_of_match[] = {
162 { .compatible = "ti,tps65090",},
163 {},
164};
Laxman Dewangan40719312013-01-29 14:35:15 +0530165#endif
166
Bill Pembertonf791be42012-11-19 13:23:04 -0500167static int tps65090_i2c_probe(struct i2c_client *client,
Lee Jones12849b62014-11-10 12:28:36 +0000168 const struct i2c_device_id *id)
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530169{
Jingoo Han334a41ce2013-07-30 17:10:05 +0900170 struct tps65090_platform_data *pdata = dev_get_platdata(&client->dev);
Laxman Dewangan40719312013-01-29 14:35:15 +0530171 int irq_base = 0;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530172 struct tps65090 *tps65090;
173 int ret;
174
Laxman Dewangan40719312013-01-29 14:35:15 +0530175 if (!pdata && !client->dev.of_node) {
176 dev_err(&client->dev,
177 "tps65090 requires platform data or of_node\n");
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530178 return -EINVAL;
179 }
180
Laxman Dewangan40719312013-01-29 14:35:15 +0530181 if (pdata)
182 irq_base = pdata->irq_base;
183
Laxman Dewangane8e6f042012-11-20 08:44:46 +0530184 tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
Markus Elfring8a232f02018-03-08 15:25:58 +0100185 if (!tps65090)
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530186 return -ENOMEM;
187
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530188 tps65090->dev = &client->dev;
189 i2c_set_clientdata(client, tps65090);
190
Laxman Dewangan3863db32012-11-20 08:44:47 +0530191 tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530192 if (IS_ERR(tps65090->rmap)) {
Axel Linb683a0a2012-04-25 09:30:36 +0800193 ret = PTR_ERR(tps65090->rmap);
194 dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
Laxman Dewangan759f2592012-11-20 08:44:49 +0530195 return ret;
196 }
197
198 if (client->irq) {
199 ret = regmap_add_irq_chip(tps65090->rmap, client->irq,
Lee Jones12849b62014-11-10 12:28:36 +0000200 IRQF_ONESHOT | IRQF_TRIGGER_LOW, irq_base,
201 &tps65090_irq_chip, &tps65090->irq_data);
202 if (ret) {
203 dev_err(&client->dev,
204 "IRQ init failed with err: %d\n", ret);
Laxman Dewangan759f2592012-11-20 08:44:49 +0530205 return ret;
206 }
Doug Anderson7d811772014-04-16 16:12:25 -0700207 } else {
208 /* Don't tell children they have an IRQ that'll never fire */
209 tps65090s[CHARGER].num_resources = 0;
Axel Linb683a0a2012-04-25 09:30:36 +0800210 }
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530211
212 ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
Lee Jones12849b62014-11-10 12:28:36 +0000213 ARRAY_SIZE(tps65090s), NULL,
214 0, regmap_irq_get_domain(tps65090->irq_data));
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530215 if (ret) {
216 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
217 ret);
Axel Lin1d88f7a2012-04-25 10:04:58 +0800218 goto err_irq_exit;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530219 }
220
221 return 0;
222
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530223err_irq_exit:
224 if (client->irq)
Laxman Dewangan759f2592012-11-20 08:44:49 +0530225 regmap_del_irq_chip(client->irq, tps65090->irq_data);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530226 return ret;
227}
228
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530229
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530230static const struct i2c_device_id tps65090_id_table[] = {
231 { "tps65090", 0 },
232 { },
233};
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530234
235static struct i2c_driver tps65090_driver = {
236 .driver = {
237 .name = "tps65090",
Paul Gortmaker26fce5e2019-01-13 13:36:41 -0500238 .suppress_bind_attrs = true,
Laxman Dewangan40719312013-01-29 14:35:15 +0530239 .of_match_table = of_match_ptr(tps65090_of_match),
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530240 },
241 .probe = tps65090_i2c_probe,
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530242 .id_table = tps65090_id_table,
243};
244
245static int __init tps65090_init(void)
246{
247 return i2c_add_driver(&tps65090_driver);
248}
249subsys_initcall(tps65090_init);