blob: 6968df4d7828d8bd6065121fb58cc744c188819c [file] [log] [blame]
Venu Byravarasu3c33be062012-03-16 11:10:19 +05301/*
2 * Core driver for TI TPS65090 PMIC family
3 *
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
Paul Gortmaker26fce5e2019-01-13 13:36:41 -05005 *
6 * Author: Venu Byravarasu <vbyravarasu@nvidia.com>
7 *
Venu Byravarasu3c33be062012-03-16 11:10:19 +05308 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
11
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21#include <linux/interrupt.h>
22#include <linux/irq.h>
23#include <linux/kernel.h>
Paul Gortmaker26fce5e2019-01-13 13:36:41 -050024#include <linux/init.h>
Venu Byravarasu3c33be062012-03-16 11:10:19 +053025#include <linux/mutex.h>
26#include <linux/slab.h>
27#include <linux/i2c.h>
28#include <linux/mfd/core.h>
29#include <linux/mfd/tps65090.h>
Laxman Dewangan40719312013-01-29 14:35:15 +053030#include <linux/of.h>
31#include <linux/of_device.h>
Venu Byravarasu3c33be062012-03-16 11:10:19 +053032#include <linux/err.h>
33
34#define NUM_INT_REG 2
Venu Byravarasu3c33be062012-03-16 11:10:19 +053035
Laxman Dewangan759f2592012-11-20 08:44:49 +053036#define TPS65090_INT1_MASK_VAC_STATUS_CHANGE 1
37#define TPS65090_INT1_MASK_VSYS_STATUS_CHANGE 2
38#define TPS65090_INT1_MASK_BAT_STATUS_CHANGE 3
39#define TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE 4
40#define TPS65090_INT1_MASK_CHARGING_COMPLETE 5
41#define TPS65090_INT1_MASK_OVERLOAD_DCDC1 6
42#define TPS65090_INT1_MASK_OVERLOAD_DCDC2 7
43#define TPS65090_INT2_MASK_OVERLOAD_DCDC3 0
44#define TPS65090_INT2_MASK_OVERLOAD_FET1 1
45#define TPS65090_INT2_MASK_OVERLOAD_FET2 2
46#define TPS65090_INT2_MASK_OVERLOAD_FET3 3
47#define TPS65090_INT2_MASK_OVERLOAD_FET4 4
48#define TPS65090_INT2_MASK_OVERLOAD_FET5 5
49#define TPS65090_INT2_MASK_OVERLOAD_FET6 6
50#define TPS65090_INT2_MASK_OVERLOAD_FET7 7
Venu Byravarasu3c33be062012-03-16 11:10:19 +053051
Rhyland Klein36c772e2013-03-12 18:08:07 -040052static struct resource charger_resources[] = {
53 {
54 .start = TPS65090_IRQ_VAC_STATUS_CHANGE,
55 .end = TPS65090_IRQ_VAC_STATUS_CHANGE,
56 .flags = IORESOURCE_IRQ,
57 }
58};
59
Doug Anderson7d811772014-04-16 16:12:25 -070060enum tps65090_cells {
61 PMIC = 0,
62 CHARGER = 1,
63};
64
65static struct mfd_cell tps65090s[] = {
66 [PMIC] = {
Venu Byravarasub7e53782012-07-18 18:33:42 +053067 .name = "tps65090-pmic",
Venu Byravarasu3c33be062012-03-16 11:10:19 +053068 },
Doug Anderson7d811772014-04-16 16:12:25 -070069 [CHARGER] = {
Laxman Dewangane2e8ffc2012-11-20 08:44:45 +053070 .name = "tps65090-charger",
Rhyland Klein36c772e2013-03-12 18:08:07 -040071 .num_resources = ARRAY_SIZE(charger_resources),
72 .resources = &charger_resources[0],
Rhyland Kleinb50cf352013-04-10 13:14:55 -040073 .of_compatible = "ti,tps65090-charger",
Venu Byravarasu3c33be062012-03-16 11:10:19 +053074 },
75};
76
Laxman Dewangan759f2592012-11-20 08:44:49 +053077static const struct regmap_irq tps65090_irqs[] = {
78 /* INT1 IRQs*/
79 [TPS65090_IRQ_VAC_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000080 .mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053081 },
82 [TPS65090_IRQ_VSYS_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000083 .mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053084 },
85 [TPS65090_IRQ_BAT_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000086 .mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053087 },
88 [TPS65090_IRQ_CHARGING_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000089 .mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053090 },
91 [TPS65090_IRQ_CHARGING_COMPLETE] = {
Lee Jones12849b62014-11-10 12:28:36 +000092 .mask = TPS65090_INT1_MASK_CHARGING_COMPLETE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053093 },
94 [TPS65090_IRQ_OVERLOAD_DCDC1] = {
Lee Jones12849b62014-11-10 12:28:36 +000095 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1,
Laxman Dewangan759f2592012-11-20 08:44:49 +053096 },
97 [TPS65090_IRQ_OVERLOAD_DCDC2] = {
Lee Jones12849b62014-11-10 12:28:36 +000098 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2,
Laxman Dewangan759f2592012-11-20 08:44:49 +053099 },
100 /* INT2 IRQs*/
101 [TPS65090_IRQ_OVERLOAD_DCDC3] = {
Lee Jones12849b62014-11-10 12:28:36 +0000102 .reg_offset = 1,
103 .mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530104 },
105 [TPS65090_IRQ_OVERLOAD_FET1] = {
Lee Jones12849b62014-11-10 12:28:36 +0000106 .reg_offset = 1,
107 .mask = TPS65090_INT2_MASK_OVERLOAD_FET1,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530108 },
109 [TPS65090_IRQ_OVERLOAD_FET2] = {
Lee Jones12849b62014-11-10 12:28:36 +0000110 .reg_offset = 1,
111 .mask = TPS65090_INT2_MASK_OVERLOAD_FET2,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530112 },
113 [TPS65090_IRQ_OVERLOAD_FET3] = {
Lee Jones12849b62014-11-10 12:28:36 +0000114 .reg_offset = 1,
115 .mask = TPS65090_INT2_MASK_OVERLOAD_FET3,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530116 },
117 [TPS65090_IRQ_OVERLOAD_FET4] = {
Lee Jones12849b62014-11-10 12:28:36 +0000118 .reg_offset = 1,
119 .mask = TPS65090_INT2_MASK_OVERLOAD_FET4,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530120 },
121 [TPS65090_IRQ_OVERLOAD_FET5] = {
Lee Jones12849b62014-11-10 12:28:36 +0000122 .reg_offset = 1,
123 .mask = TPS65090_INT2_MASK_OVERLOAD_FET5,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530124 },
125 [TPS65090_IRQ_OVERLOAD_FET6] = {
Lee Jones12849b62014-11-10 12:28:36 +0000126 .reg_offset = 1,
127 .mask = TPS65090_INT2_MASK_OVERLOAD_FET6,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530128 },
129 [TPS65090_IRQ_OVERLOAD_FET7] = {
Lee Jones12849b62014-11-10 12:28:36 +0000130 .reg_offset = 1,
131 .mask = TPS65090_INT2_MASK_OVERLOAD_FET7,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530132 },
133};
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530134
Laxman Dewangan759f2592012-11-20 08:44:49 +0530135static struct regmap_irq_chip tps65090_irq_chip = {
136 .name = "tps65090",
137 .irqs = tps65090_irqs,
138 .num_irqs = ARRAY_SIZE(tps65090_irqs),
139 .num_regs = NUM_INT_REG,
Doug Andersonc42ba722014-04-16 16:12:27 -0700140 .status_base = TPS65090_REG_INTR_STS,
141 .mask_base = TPS65090_REG_INTR_MASK,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530142 .mask_invert = true,
143};
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530144
145static bool is_volatile_reg(struct device *dev, unsigned int reg)
146{
Doug Andersonc42ba722014-04-16 16:12:27 -0700147 /* Nearly all registers have status bits mixed in, except a few */
148 switch (reg) {
149 case TPS65090_REG_INTR_MASK:
150 case TPS65090_REG_INTR_MASK2:
151 case TPS65090_REG_CG_CTRL0:
152 case TPS65090_REG_CG_CTRL1:
153 case TPS65090_REG_CG_CTRL2:
154 case TPS65090_REG_CG_CTRL3:
155 case TPS65090_REG_CG_CTRL4:
156 case TPS65090_REG_CG_CTRL5:
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530157 return false;
Doug Andersonc42ba722014-04-16 16:12:27 -0700158 }
159 return true;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530160}
161
162static const struct regmap_config tps65090_regmap_config = {
163 .reg_bits = 8,
164 .val_bits = 8,
Maciej S. Szmigiero5c148892016-01-31 23:00:06 +0100165 .max_register = TPS65090_MAX_REG,
166 .num_reg_defaults_raw = TPS65090_NUM_REGS,
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530167 .cache_type = REGCACHE_RBTREE,
168 .volatile_reg = is_volatile_reg,
169};
170
Laxman Dewangan40719312013-01-29 14:35:15 +0530171#ifdef CONFIG_OF
172static const struct of_device_id tps65090_of_match[] = {
173 { .compatible = "ti,tps65090",},
174 {},
175};
Laxman Dewangan40719312013-01-29 14:35:15 +0530176#endif
177
Bill Pembertonf791be42012-11-19 13:23:04 -0500178static int tps65090_i2c_probe(struct i2c_client *client,
Lee Jones12849b62014-11-10 12:28:36 +0000179 const struct i2c_device_id *id)
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530180{
Jingoo Han334a41c2013-07-30 17:10:05 +0900181 struct tps65090_platform_data *pdata = dev_get_platdata(&client->dev);
Laxman Dewangan40719312013-01-29 14:35:15 +0530182 int irq_base = 0;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530183 struct tps65090 *tps65090;
184 int ret;
185
Laxman Dewangan40719312013-01-29 14:35:15 +0530186 if (!pdata && !client->dev.of_node) {
187 dev_err(&client->dev,
188 "tps65090 requires platform data or of_node\n");
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530189 return -EINVAL;
190 }
191
Laxman Dewangan40719312013-01-29 14:35:15 +0530192 if (pdata)
193 irq_base = pdata->irq_base;
194
Laxman Dewangane8e6f042012-11-20 08:44:46 +0530195 tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
Markus Elfring8a232f02018-03-08 15:25:58 +0100196 if (!tps65090)
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530197 return -ENOMEM;
198
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530199 tps65090->dev = &client->dev;
200 i2c_set_clientdata(client, tps65090);
201
Laxman Dewangan3863db32012-11-20 08:44:47 +0530202 tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530203 if (IS_ERR(tps65090->rmap)) {
Axel Linb683a0a2012-04-25 09:30:36 +0800204 ret = PTR_ERR(tps65090->rmap);
205 dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
Laxman Dewangan759f2592012-11-20 08:44:49 +0530206 return ret;
207 }
208
209 if (client->irq) {
210 ret = regmap_add_irq_chip(tps65090->rmap, client->irq,
Lee Jones12849b62014-11-10 12:28:36 +0000211 IRQF_ONESHOT | IRQF_TRIGGER_LOW, irq_base,
212 &tps65090_irq_chip, &tps65090->irq_data);
213 if (ret) {
214 dev_err(&client->dev,
215 "IRQ init failed with err: %d\n", ret);
Laxman Dewangan759f2592012-11-20 08:44:49 +0530216 return ret;
217 }
Doug Anderson7d811772014-04-16 16:12:25 -0700218 } else {
219 /* Don't tell children they have an IRQ that'll never fire */
220 tps65090s[CHARGER].num_resources = 0;
Axel Linb683a0a2012-04-25 09:30:36 +0800221 }
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530222
223 ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
Lee Jones12849b62014-11-10 12:28:36 +0000224 ARRAY_SIZE(tps65090s), NULL,
225 0, regmap_irq_get_domain(tps65090->irq_data));
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530226 if (ret) {
227 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
228 ret);
Axel Lin1d88f7a2012-04-25 10:04:58 +0800229 goto err_irq_exit;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530230 }
231
232 return 0;
233
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530234err_irq_exit:
235 if (client->irq)
Laxman Dewangan759f2592012-11-20 08:44:49 +0530236 regmap_del_irq_chip(client->irq, tps65090->irq_data);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530237 return ret;
238}
239
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530240
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530241static const struct i2c_device_id tps65090_id_table[] = {
242 { "tps65090", 0 },
243 { },
244};
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530245
246static struct i2c_driver tps65090_driver = {
247 .driver = {
248 .name = "tps65090",
Paul Gortmaker26fce5e2019-01-13 13:36:41 -0500249 .suppress_bind_attrs = true,
Laxman Dewangan40719312013-01-29 14:35:15 +0530250 .of_match_table = of_match_ptr(tps65090_of_match),
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530251 },
252 .probe = tps65090_i2c_probe,
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530253 .id_table = tps65090_id_table,
254};
255
256static int __init tps65090_init(void)
257{
258 return i2c_add_driver(&tps65090_driver);
259}
260subsys_initcall(tps65090_init);