blob: 88846aeab74ff90ec0f4f6b54d43b0e0159ce438 [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
33#define TOTAL_NUM_REG 0x18
34
35/* interrupt status registers */
36#define TPS65090_INT_STS 0x0
37#define TPS65090_INT_STS2 0x1
38
39/* interrupt mask registers */
40#define TPS65090_INT_MSK 0x2
41#define TPS65090_INT_MSK2 0x3
42
Laxman Dewangan759f2592012-11-20 08:44:49 +053043#define TPS65090_INT1_MASK_VAC_STATUS_CHANGE 1
44#define TPS65090_INT1_MASK_VSYS_STATUS_CHANGE 2
45#define TPS65090_INT1_MASK_BAT_STATUS_CHANGE 3
46#define TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE 4
47#define TPS65090_INT1_MASK_CHARGING_COMPLETE 5
48#define TPS65090_INT1_MASK_OVERLOAD_DCDC1 6
49#define TPS65090_INT1_MASK_OVERLOAD_DCDC2 7
50#define TPS65090_INT2_MASK_OVERLOAD_DCDC3 0
51#define TPS65090_INT2_MASK_OVERLOAD_FET1 1
52#define TPS65090_INT2_MASK_OVERLOAD_FET2 2
53#define TPS65090_INT2_MASK_OVERLOAD_FET3 3
54#define TPS65090_INT2_MASK_OVERLOAD_FET4 4
55#define TPS65090_INT2_MASK_OVERLOAD_FET5 5
56#define TPS65090_INT2_MASK_OVERLOAD_FET6 6
57#define TPS65090_INT2_MASK_OVERLOAD_FET7 7
Venu Byravarasu3c33be062012-03-16 11:10:19 +053058
Rhyland Klein36c772e2013-03-12 18:08:07 -040059static struct resource charger_resources[] = {
60 {
61 .start = TPS65090_IRQ_VAC_STATUS_CHANGE,
62 .end = TPS65090_IRQ_VAC_STATUS_CHANGE,
63 .flags = IORESOURCE_IRQ,
64 }
65};
66
Venu Byravarasu3c33be062012-03-16 11:10:19 +053067static struct mfd_cell tps65090s[] = {
68 {
Venu Byravarasub7e53782012-07-18 18:33:42 +053069 .name = "tps65090-pmic",
Venu Byravarasu3c33be062012-03-16 11:10:19 +053070 },
71 {
Laxman Dewangane2e8ffc2012-11-20 08:44:45 +053072 .name = "tps65090-charger",
Rhyland Klein36c772e2013-03-12 18:08:07 -040073 .num_resources = ARRAY_SIZE(charger_resources),
74 .resources = &charger_resources[0],
Venu Byravarasu3c33be062012-03-16 11:10:19 +053075 },
76};
77
Laxman Dewangan759f2592012-11-20 08:44:49 +053078static const struct regmap_irq tps65090_irqs[] = {
79 /* INT1 IRQs*/
80 [TPS65090_IRQ_VAC_STATUS_CHANGE] = {
81 .mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE,
82 },
83 [TPS65090_IRQ_VSYS_STATUS_CHANGE] = {
84 .mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE,
85 },
86 [TPS65090_IRQ_BAT_STATUS_CHANGE] = {
87 .mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE,
88 },
89 [TPS65090_IRQ_CHARGING_STATUS_CHANGE] = {
90 .mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE,
91 },
92 [TPS65090_IRQ_CHARGING_COMPLETE] = {
93 .mask = TPS65090_INT1_MASK_CHARGING_COMPLETE,
94 },
95 [TPS65090_IRQ_OVERLOAD_DCDC1] = {
96 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1,
97 },
98 [TPS65090_IRQ_OVERLOAD_DCDC2] = {
99 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2,
100 },
101 /* INT2 IRQs*/
102 [TPS65090_IRQ_OVERLOAD_DCDC3] = {
103 .reg_offset = 1,
104 .mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3,
105 },
106 [TPS65090_IRQ_OVERLOAD_FET1] = {
107 .reg_offset = 1,
108 .mask = TPS65090_INT2_MASK_OVERLOAD_FET1,
109 },
110 [TPS65090_IRQ_OVERLOAD_FET2] = {
111 .reg_offset = 1,
112 .mask = TPS65090_INT2_MASK_OVERLOAD_FET2,
113 },
114 [TPS65090_IRQ_OVERLOAD_FET3] = {
115 .reg_offset = 1,
116 .mask = TPS65090_INT2_MASK_OVERLOAD_FET3,
117 },
118 [TPS65090_IRQ_OVERLOAD_FET4] = {
119 .reg_offset = 1,
120 .mask = TPS65090_INT2_MASK_OVERLOAD_FET4,
121 },
122 [TPS65090_IRQ_OVERLOAD_FET5] = {
123 .reg_offset = 1,
124 .mask = TPS65090_INT2_MASK_OVERLOAD_FET5,
125 },
126 [TPS65090_IRQ_OVERLOAD_FET6] = {
127 .reg_offset = 1,
128 .mask = TPS65090_INT2_MASK_OVERLOAD_FET6,
129 },
130 [TPS65090_IRQ_OVERLOAD_FET7] = {
131 .reg_offset = 1,
132 .mask = TPS65090_INT2_MASK_OVERLOAD_FET7,
133 },
134};
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530135
Laxman Dewangan759f2592012-11-20 08:44:49 +0530136static struct regmap_irq_chip tps65090_irq_chip = {
137 .name = "tps65090",
138 .irqs = tps65090_irqs,
139 .num_irqs = ARRAY_SIZE(tps65090_irqs),
140 .num_regs = NUM_INT_REG,
141 .status_base = TPS65090_INT_STS,
142 .mask_base = TPS65090_INT_MSK,
143 .mask_invert = true,
144};
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530145
146static bool is_volatile_reg(struct device *dev, unsigned int reg)
147{
Laxman Dewangan759f2592012-11-20 08:44:49 +0530148 if ((reg == TPS65090_INT_STS) || (reg == TPS65090_INT_STS2))
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530149 return true;
150 else
151 return false;
152}
153
154static const struct regmap_config tps65090_regmap_config = {
155 .reg_bits = 8,
156 .val_bits = 8,
157 .max_register = TOTAL_NUM_REG,
158 .num_reg_defaults_raw = TOTAL_NUM_REG,
159 .cache_type = REGCACHE_RBTREE,
160 .volatile_reg = is_volatile_reg,
161};
162
Laxman Dewangan40719312013-01-29 14:35:15 +0530163#ifdef CONFIG_OF
164static const struct of_device_id tps65090_of_match[] = {
165 { .compatible = "ti,tps65090",},
166 {},
167};
168MODULE_DEVICE_TABLE(of, tps65090_of_match);
169#endif
170
Bill Pembertonf791be42012-11-19 13:23:04 -0500171static int tps65090_i2c_probe(struct i2c_client *client,
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530172 const struct i2c_device_id *id)
173{
174 struct tps65090_platform_data *pdata = client->dev.platform_data;
Laxman Dewangan40719312013-01-29 14:35:15 +0530175 int irq_base = 0;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530176 struct tps65090 *tps65090;
177 int ret;
178
Laxman Dewangan40719312013-01-29 14:35:15 +0530179 if (!pdata && !client->dev.of_node) {
180 dev_err(&client->dev,
181 "tps65090 requires platform data or of_node\n");
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530182 return -EINVAL;
183 }
184
Laxman Dewangan40719312013-01-29 14:35:15 +0530185 if (pdata)
186 irq_base = pdata->irq_base;
187
Laxman Dewangane8e6f042012-11-20 08:44:46 +0530188 tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
189 if (!tps65090) {
190 dev_err(&client->dev, "mem alloc for tps65090 failed\n");
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530191 return -ENOMEM;
Laxman Dewangane8e6f042012-11-20 08:44:46 +0530192 }
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530193
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530194 tps65090->dev = &client->dev;
195 i2c_set_clientdata(client, tps65090);
196
Laxman Dewangan3863db32012-11-20 08:44:47 +0530197 tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530198 if (IS_ERR(tps65090->rmap)) {
Axel Linb683a0a2012-04-25 09:30:36 +0800199 ret = PTR_ERR(tps65090->rmap);
200 dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
Laxman Dewangan759f2592012-11-20 08:44:49 +0530201 return ret;
202 }
203
204 if (client->irq) {
205 ret = regmap_add_irq_chip(tps65090->rmap, client->irq,
Laxman Dewangan40719312013-01-29 14:35:15 +0530206 IRQF_ONESHOT | IRQF_TRIGGER_LOW, irq_base,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530207 &tps65090_irq_chip, &tps65090->irq_data);
208 if (ret) {
209 dev_err(&client->dev,
210 "IRQ init failed with err: %d\n", ret);
211 return ret;
212 }
Axel Linb683a0a2012-04-25 09:30:36 +0800213 }
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530214
215 ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530216 ARRAY_SIZE(tps65090s), NULL,
Laxman Dewangan4f979ed2013-01-29 14:35:17 +0530217 0, regmap_irq_get_domain(tps65090->irq_data));
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530218 if (ret) {
219 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
220 ret);
Axel Lin1d88f7a2012-04-25 10:04:58 +0800221 goto err_irq_exit;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530222 }
223
224 return 0;
225
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530226err_irq_exit:
227 if (client->irq)
Laxman Dewangan759f2592012-11-20 08:44:49 +0530228 regmap_del_irq_chip(client->irq, tps65090->irq_data);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530229 return ret;
230}
231
Bill Pemberton4740f732012-11-19 13:26:01 -0500232static int tps65090_i2c_remove(struct i2c_client *client)
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530233{
234 struct tps65090 *tps65090 = i2c_get_clientdata(client);
235
236 mfd_remove_devices(tps65090->dev);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530237 if (client->irq)
Laxman Dewangan759f2592012-11-20 08:44:49 +0530238 regmap_del_irq_chip(client->irq, tps65090->irq_data);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530239
240 return 0;
241}
242
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530243static const struct i2c_device_id tps65090_id_table[] = {
244 { "tps65090", 0 },
245 { },
246};
247MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
248
249static struct i2c_driver tps65090_driver = {
250 .driver = {
251 .name = "tps65090",
252 .owner = THIS_MODULE,
Laxman Dewangan40719312013-01-29 14:35:15 +0530253 .of_match_table = of_match_ptr(tps65090_of_match),
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530254 },
255 .probe = tps65090_i2c_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500256 .remove = tps65090_i2c_remove,
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530257 .id_table = tps65090_id_table,
258};
259
260static int __init tps65090_init(void)
261{
262 return i2c_add_driver(&tps65090_driver);
263}
264subsys_initcall(tps65090_init);
265
266static void __exit tps65090_exit(void)
267{
268 i2c_del_driver(&tps65090_driver);
269}
270module_exit(tps65090_exit);
271
272MODULE_DESCRIPTION("TPS65090 core driver");
273MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
274MODULE_LICENSE("GPL v2");