blob: d7ec318c40c30f519ed4c6a2608d061ec79d4e9d [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.
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>
Laxman Dewangan40719312013-01-29 14:35:15 +053028#include <linux/of.h>
29#include <linux/of_device.h>
Venu Byravarasu3c33be062012-03-16 11:10:19 +053030#include <linux/err.h>
31
32#define NUM_INT_REG 2
Venu Byravarasu3c33be062012-03-16 11:10:19 +053033
Laxman Dewangan759f2592012-11-20 08:44:49 +053034#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
Venu Byravarasu3c33be062012-03-16 11:10:19 +053049
Rhyland Klein36c772e2013-03-12 18:08:07 -040050static 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
Doug Anderson7d811772014-04-16 16:12:25 -070058enum tps65090_cells {
59 PMIC = 0,
60 CHARGER = 1,
61};
62
63static struct mfd_cell tps65090s[] = {
64 [PMIC] = {
Venu Byravarasub7e53782012-07-18 18:33:42 +053065 .name = "tps65090-pmic",
Venu Byravarasu3c33be062012-03-16 11:10:19 +053066 },
Doug Anderson7d811772014-04-16 16:12:25 -070067 [CHARGER] = {
Laxman Dewangane2e8ffc2012-11-20 08:44:45 +053068 .name = "tps65090-charger",
Rhyland Klein36c772e2013-03-12 18:08:07 -040069 .num_resources = ARRAY_SIZE(charger_resources),
70 .resources = &charger_resources[0],
Rhyland Kleinb50cf352013-04-10 13:14:55 -040071 .of_compatible = "ti,tps65090-charger",
Venu Byravarasu3c33be062012-03-16 11:10:19 +053072 },
73};
74
Laxman Dewangan759f2592012-11-20 08:44:49 +053075static const struct regmap_irq tps65090_irqs[] = {
76 /* INT1 IRQs*/
77 [TPS65090_IRQ_VAC_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000078 .mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053079 },
80 [TPS65090_IRQ_VSYS_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000081 .mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053082 },
83 [TPS65090_IRQ_BAT_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000084 .mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053085 },
86 [TPS65090_IRQ_CHARGING_STATUS_CHANGE] = {
Lee Jones12849b62014-11-10 12:28:36 +000087 .mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053088 },
89 [TPS65090_IRQ_CHARGING_COMPLETE] = {
Lee Jones12849b62014-11-10 12:28:36 +000090 .mask = TPS65090_INT1_MASK_CHARGING_COMPLETE,
Laxman Dewangan759f2592012-11-20 08:44:49 +053091 },
92 [TPS65090_IRQ_OVERLOAD_DCDC1] = {
Lee Jones12849b62014-11-10 12:28:36 +000093 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1,
Laxman Dewangan759f2592012-11-20 08:44:49 +053094 },
95 [TPS65090_IRQ_OVERLOAD_DCDC2] = {
Lee Jones12849b62014-11-10 12:28:36 +000096 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2,
Laxman Dewangan759f2592012-11-20 08:44:49 +053097 },
98 /* INT2 IRQs*/
99 [TPS65090_IRQ_OVERLOAD_DCDC3] = {
Lee Jones12849b62014-11-10 12:28:36 +0000100 .reg_offset = 1,
101 .mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530102 },
103 [TPS65090_IRQ_OVERLOAD_FET1] = {
Lee Jones12849b62014-11-10 12:28:36 +0000104 .reg_offset = 1,
105 .mask = TPS65090_INT2_MASK_OVERLOAD_FET1,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530106 },
107 [TPS65090_IRQ_OVERLOAD_FET2] = {
Lee Jones12849b62014-11-10 12:28:36 +0000108 .reg_offset = 1,
109 .mask = TPS65090_INT2_MASK_OVERLOAD_FET2,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530110 },
111 [TPS65090_IRQ_OVERLOAD_FET3] = {
Lee Jones12849b62014-11-10 12:28:36 +0000112 .reg_offset = 1,
113 .mask = TPS65090_INT2_MASK_OVERLOAD_FET3,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530114 },
115 [TPS65090_IRQ_OVERLOAD_FET4] = {
Lee Jones12849b62014-11-10 12:28:36 +0000116 .reg_offset = 1,
117 .mask = TPS65090_INT2_MASK_OVERLOAD_FET4,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530118 },
119 [TPS65090_IRQ_OVERLOAD_FET5] = {
Lee Jones12849b62014-11-10 12:28:36 +0000120 .reg_offset = 1,
121 .mask = TPS65090_INT2_MASK_OVERLOAD_FET5,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530122 },
123 [TPS65090_IRQ_OVERLOAD_FET6] = {
Lee Jones12849b62014-11-10 12:28:36 +0000124 .reg_offset = 1,
125 .mask = TPS65090_INT2_MASK_OVERLOAD_FET6,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530126 },
127 [TPS65090_IRQ_OVERLOAD_FET7] = {
Lee Jones12849b62014-11-10 12:28:36 +0000128 .reg_offset = 1,
129 .mask = TPS65090_INT2_MASK_OVERLOAD_FET7,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530130 },
131};
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530132
Laxman Dewangan759f2592012-11-20 08:44:49 +0530133static 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,
Doug Andersonc42ba722014-04-16 16:12:27 -0700138 .status_base = TPS65090_REG_INTR_STS,
139 .mask_base = TPS65090_REG_INTR_MASK,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530140 .mask_invert = true,
141};
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530142
143static bool is_volatile_reg(struct device *dev, unsigned int reg)
144{
Doug Andersonc42ba722014-04-16 16:12:27 -0700145 /* 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:
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530155 return false;
Doug Andersonc42ba722014-04-16 16:12:27 -0700156 }
157 return true;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530158}
159
160static const struct regmap_config tps65090_regmap_config = {
161 .reg_bits = 8,
162 .val_bits = 8,
Maciej S. Szmigiero5c148892016-01-31 23:00:06 +0100163 .max_register = TPS65090_MAX_REG,
164 .num_reg_defaults_raw = TPS65090_NUM_REGS,
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530165 .cache_type = REGCACHE_RBTREE,
166 .volatile_reg = is_volatile_reg,
167};
168
Laxman Dewangan40719312013-01-29 14:35:15 +0530169#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
Bill Pembertonf791be42012-11-19 13:23:04 -0500177static int tps65090_i2c_probe(struct i2c_client *client,
Lee Jones12849b62014-11-10 12:28:36 +0000178 const struct i2c_device_id *id)
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530179{
Jingoo Han334a41c2013-07-30 17:10:05 +0900180 struct tps65090_platform_data *pdata = dev_get_platdata(&client->dev);
Laxman Dewangan40719312013-01-29 14:35:15 +0530181 int irq_base = 0;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530182 struct tps65090 *tps65090;
183 int ret;
184
Laxman Dewangan40719312013-01-29 14:35:15 +0530185 if (!pdata && !client->dev.of_node) {
186 dev_err(&client->dev,
187 "tps65090 requires platform data or of_node\n");
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530188 return -EINVAL;
189 }
190
Laxman Dewangan40719312013-01-29 14:35:15 +0530191 if (pdata)
192 irq_base = pdata->irq_base;
193
Laxman Dewangane8e6f042012-11-20 08:44:46 +0530194 tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
195 if (!tps65090) {
196 dev_err(&client->dev, "mem alloc for tps65090 failed\n");
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530197 return -ENOMEM;
Laxman Dewangane8e6f042012-11-20 08:44:46 +0530198 }
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530199
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530200 tps65090->dev = &client->dev;
201 i2c_set_clientdata(client, tps65090);
202
Laxman Dewangan3863db32012-11-20 08:44:47 +0530203 tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530204 if (IS_ERR(tps65090->rmap)) {
Axel Linb683a0a2012-04-25 09:30:36 +0800205 ret = PTR_ERR(tps65090->rmap);
206 dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
Laxman Dewangan759f2592012-11-20 08:44:49 +0530207 return ret;
208 }
209
210 if (client->irq) {
211 ret = regmap_add_irq_chip(tps65090->rmap, client->irq,
Lee Jones12849b62014-11-10 12:28:36 +0000212 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);
Laxman Dewangan759f2592012-11-20 08:44:49 +0530217 return ret;
218 }
Doug Anderson7d811772014-04-16 16:12:25 -0700219 } else {
220 /* Don't tell children they have an IRQ that'll never fire */
221 tps65090s[CHARGER].num_resources = 0;
Axel Linb683a0a2012-04-25 09:30:36 +0800222 }
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530223
224 ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
Lee Jones12849b62014-11-10 12:28:36 +0000225 ARRAY_SIZE(tps65090s), NULL,
226 0, regmap_irq_get_domain(tps65090->irq_data));
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530227 if (ret) {
228 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
229 ret);
Axel Lin1d88f7a2012-04-25 10:04:58 +0800230 goto err_irq_exit;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530231 }
232
233 return 0;
234
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530235err_irq_exit:
236 if (client->irq)
Laxman Dewangan759f2592012-11-20 08:44:49 +0530237 regmap_del_irq_chip(client->irq, tps65090->irq_data);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530238 return ret;
239}
240
Bill Pemberton4740f732012-11-19 13:26:01 -0500241static int tps65090_i2c_remove(struct i2c_client *client)
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530242{
243 struct tps65090 *tps65090 = i2c_get_clientdata(client);
244
245 mfd_remove_devices(tps65090->dev);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530246 if (client->irq)
Laxman Dewangan759f2592012-11-20 08:44:49 +0530247 regmap_del_irq_chip(client->irq, tps65090->irq_data);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530248
249 return 0;
250}
251
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530252static 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",
Laxman Dewangan40719312013-01-29 14:35:15 +0530261 .of_match_table = of_match_ptr(tps65090_of_match),
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530262 },
263 .probe = tps65090_i2c_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500264 .remove = tps65090_i2c_remove,
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530265 .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");