blob: 2eaae52cb5b8f91ff17eb2d2bc79fe335cf66139 [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>
Venu Byravarasu3c33be062012-03-16 11:10:19 +053028#include <linux/err.h>
29
30#define NUM_INT_REG 2
31#define TOTAL_NUM_REG 0x18
32
33/* interrupt status registers */
34#define TPS65090_INT_STS 0x0
35#define TPS65090_INT_STS2 0x1
36
37/* interrupt mask registers */
38#define TPS65090_INT_MSK 0x2
39#define TPS65090_INT_MSK2 0x3
40
Laxman Dewangan759f2592012-11-20 08:44:49 +053041#define TPS65090_INT1_MASK_VAC_STATUS_CHANGE 1
42#define TPS65090_INT1_MASK_VSYS_STATUS_CHANGE 2
43#define TPS65090_INT1_MASK_BAT_STATUS_CHANGE 3
44#define TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE 4
45#define TPS65090_INT1_MASK_CHARGING_COMPLETE 5
46#define TPS65090_INT1_MASK_OVERLOAD_DCDC1 6
47#define TPS65090_INT1_MASK_OVERLOAD_DCDC2 7
48#define TPS65090_INT2_MASK_OVERLOAD_DCDC3 0
49#define TPS65090_INT2_MASK_OVERLOAD_FET1 1
50#define TPS65090_INT2_MASK_OVERLOAD_FET2 2
51#define TPS65090_INT2_MASK_OVERLOAD_FET3 3
52#define TPS65090_INT2_MASK_OVERLOAD_FET4 4
53#define TPS65090_INT2_MASK_OVERLOAD_FET5 5
54#define TPS65090_INT2_MASK_OVERLOAD_FET6 6
55#define TPS65090_INT2_MASK_OVERLOAD_FET7 7
Venu Byravarasu3c33be062012-03-16 11:10:19 +053056
57static struct mfd_cell tps65090s[] = {
58 {
Venu Byravarasub7e53782012-07-18 18:33:42 +053059 .name = "tps65090-pmic",
Venu Byravarasu3c33be062012-03-16 11:10:19 +053060 },
61 {
Laxman Dewangane2e8ffc2012-11-20 08:44:45 +053062 .name = "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] = {
69 .mask = TPS65090_INT1_MASK_VAC_STATUS_CHANGE,
70 },
71 [TPS65090_IRQ_VSYS_STATUS_CHANGE] = {
72 .mask = TPS65090_INT1_MASK_VSYS_STATUS_CHANGE,
73 },
74 [TPS65090_IRQ_BAT_STATUS_CHANGE] = {
75 .mask = TPS65090_INT1_MASK_BAT_STATUS_CHANGE,
76 },
77 [TPS65090_IRQ_CHARGING_STATUS_CHANGE] = {
78 .mask = TPS65090_INT1_MASK_CHARGING_STATUS_CHANGE,
79 },
80 [TPS65090_IRQ_CHARGING_COMPLETE] = {
81 .mask = TPS65090_INT1_MASK_CHARGING_COMPLETE,
82 },
83 [TPS65090_IRQ_OVERLOAD_DCDC1] = {
84 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC1,
85 },
86 [TPS65090_IRQ_OVERLOAD_DCDC2] = {
87 .mask = TPS65090_INT1_MASK_OVERLOAD_DCDC2,
88 },
89 /* INT2 IRQs*/
90 [TPS65090_IRQ_OVERLOAD_DCDC3] = {
91 .reg_offset = 1,
92 .mask = TPS65090_INT2_MASK_OVERLOAD_DCDC3,
93 },
94 [TPS65090_IRQ_OVERLOAD_FET1] = {
95 .reg_offset = 1,
96 .mask = TPS65090_INT2_MASK_OVERLOAD_FET1,
97 },
98 [TPS65090_IRQ_OVERLOAD_FET2] = {
99 .reg_offset = 1,
100 .mask = TPS65090_INT2_MASK_OVERLOAD_FET2,
101 },
102 [TPS65090_IRQ_OVERLOAD_FET3] = {
103 .reg_offset = 1,
104 .mask = TPS65090_INT2_MASK_OVERLOAD_FET3,
105 },
106 [TPS65090_IRQ_OVERLOAD_FET4] = {
107 .reg_offset = 1,
108 .mask = TPS65090_INT2_MASK_OVERLOAD_FET4,
109 },
110 [TPS65090_IRQ_OVERLOAD_FET5] = {
111 .reg_offset = 1,
112 .mask = TPS65090_INT2_MASK_OVERLOAD_FET5,
113 },
114 [TPS65090_IRQ_OVERLOAD_FET6] = {
115 .reg_offset = 1,
116 .mask = TPS65090_INT2_MASK_OVERLOAD_FET6,
117 },
118 [TPS65090_IRQ_OVERLOAD_FET7] = {
119 .reg_offset = 1,
120 .mask = TPS65090_INT2_MASK_OVERLOAD_FET7,
121 },
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,
129 .status_base = TPS65090_INT_STS,
130 .mask_base = TPS65090_INT_MSK,
131 .mask_invert = true,
132};
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530133
134static bool is_volatile_reg(struct device *dev, unsigned int reg)
135{
Laxman Dewangan759f2592012-11-20 08:44:49 +0530136 if ((reg == TPS65090_INT_STS) || (reg == TPS65090_INT_STS2))
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530137 return true;
138 else
139 return false;
140}
141
142static const struct regmap_config tps65090_regmap_config = {
143 .reg_bits = 8,
144 .val_bits = 8,
145 .max_register = TOTAL_NUM_REG,
146 .num_reg_defaults_raw = TOTAL_NUM_REG,
147 .cache_type = REGCACHE_RBTREE,
148 .volatile_reg = is_volatile_reg,
149};
150
151static int __devinit tps65090_i2c_probe(struct i2c_client *client,
152 const struct i2c_device_id *id)
153{
154 struct tps65090_platform_data *pdata = client->dev.platform_data;
155 struct tps65090 *tps65090;
156 int ret;
157
158 if (!pdata) {
159 dev_err(&client->dev, "tps65090 requires platform data\n");
160 return -EINVAL;
161 }
162
Laxman Dewangane8e6f042012-11-20 08:44:46 +0530163 tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
164 if (!tps65090) {
165 dev_err(&client->dev, "mem alloc for tps65090 failed\n");
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530166 return -ENOMEM;
Laxman Dewangane8e6f042012-11-20 08:44:46 +0530167 }
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530168
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530169 tps65090->dev = &client->dev;
170 i2c_set_clientdata(client, tps65090);
171
Laxman Dewangan3863db32012-11-20 08:44:47 +0530172 tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530173 if (IS_ERR(tps65090->rmap)) {
Axel Linb683a0a2012-04-25 09:30:36 +0800174 ret = PTR_ERR(tps65090->rmap);
175 dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
Laxman Dewangan759f2592012-11-20 08:44:49 +0530176 return ret;
177 }
178
179 if (client->irq) {
180 ret = regmap_add_irq_chip(tps65090->rmap, client->irq,
181 IRQF_ONESHOT | IRQF_TRIGGER_LOW, pdata->irq_base,
182 &tps65090_irq_chip, &tps65090->irq_data);
183 if (ret) {
184 dev_err(&client->dev,
185 "IRQ init failed with err: %d\n", ret);
186 return ret;
187 }
Axel Linb683a0a2012-04-25 09:30:36 +0800188 }
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530189
190 ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
Laxman Dewangan759f2592012-11-20 08:44:49 +0530191 ARRAY_SIZE(tps65090s), NULL,
192 regmap_irq_chip_get_base(tps65090->irq_data), NULL);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530193 if (ret) {
194 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
195 ret);
Axel Lin1d88f7a2012-04-25 10:04:58 +0800196 goto err_irq_exit;
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530197 }
198
199 return 0;
200
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530201err_irq_exit:
202 if (client->irq)
Laxman Dewangan759f2592012-11-20 08:44:49 +0530203 regmap_del_irq_chip(client->irq, tps65090->irq_data);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530204 return ret;
205}
206
207static int __devexit tps65090_i2c_remove(struct i2c_client *client)
208{
209 struct tps65090 *tps65090 = i2c_get_clientdata(client);
210
211 mfd_remove_devices(tps65090->dev);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530212 if (client->irq)
Laxman Dewangan759f2592012-11-20 08:44:49 +0530213 regmap_del_irq_chip(client->irq, tps65090->irq_data);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530214
215 return 0;
216}
217
Mark Brown1ca5513af2012-05-07 10:03:17 +0100218#ifdef CONFIG_PM_SLEEP
Mark Brownb6c9eee2012-05-07 10:03:18 +0100219static int tps65090_suspend(struct device *dev)
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530220{
Mark Brownb6c9eee2012-05-07 10:03:18 +0100221 struct i2c_client *client = to_i2c_client(dev);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530222 if (client->irq)
223 disable_irq(client->irq);
224 return 0;
225}
226
Mark Brownb6c9eee2012-05-07 10:03:18 +0100227static int tps65090_resume(struct device *dev)
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530228{
Mark Brownb6c9eee2012-05-07 10:03:18 +0100229 struct i2c_client *client = to_i2c_client(dev);
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530230 if (client->irq)
231 enable_irq(client->irq);
232 return 0;
233}
234#endif
235
Mark Brownb6c9eee2012-05-07 10:03:18 +0100236static const struct dev_pm_ops tps65090_pm_ops = {
237 SET_SYSTEM_SLEEP_PM_OPS(tps65090_suspend, tps65090_resume)
238};
239
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530240static const struct i2c_device_id tps65090_id_table[] = {
241 { "tps65090", 0 },
242 { },
243};
244MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
245
246static struct i2c_driver tps65090_driver = {
247 .driver = {
248 .name = "tps65090",
249 .owner = THIS_MODULE,
Mark Brownb6c9eee2012-05-07 10:03:18 +0100250 .pm = &tps65090_pm_ops,
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530251 },
252 .probe = tps65090_i2c_probe,
253 .remove = __devexit_p(tps65090_i2c_remove),
Venu Byravarasu3c33be062012-03-16 11:10:19 +0530254 .id_table = tps65090_id_table,
255};
256
257static int __init tps65090_init(void)
258{
259 return i2c_add_driver(&tps65090_driver);
260}
261subsys_initcall(tps65090_init);
262
263static void __exit tps65090_exit(void)
264{
265 i2c_del_driver(&tps65090_driver);
266}
267module_exit(tps65090_exit);
268
269MODULE_DESCRIPTION("TPS65090 core driver");
270MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
271MODULE_LICENSE("GPL v2");