xref: /openbmc/linux/drivers/mfd/tps65090.c (revision b9c79323)
1 /*
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>
28 #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 
41 struct tps65090_irq_data {
42 	u8		mask_reg;
43 	u8		mask_pos;
44 };
45 
46 #define TPS65090_IRQ(_reg, _mask_pos)		\
47 	{					\
48 		.mask_reg	= (_reg),	\
49 		.mask_pos	= (_mask_pos),	\
50 	}
51 
52 static const struct tps65090_irq_data tps65090_irqs[] = {
53 	[0]		= TPS65090_IRQ(0, 0),
54 	[1]		= TPS65090_IRQ(0, 1),
55 	[2]		= TPS65090_IRQ(0, 2),
56 	[3]		= TPS65090_IRQ(0, 3),
57 	[4]		= TPS65090_IRQ(0, 4),
58 	[5]		= TPS65090_IRQ(0, 5),
59 	[6]		= TPS65090_IRQ(0, 6),
60 	[7]		= TPS65090_IRQ(0, 7),
61 	[8]		= TPS65090_IRQ(1, 0),
62 	[9]		= TPS65090_IRQ(1, 1),
63 	[10]		= TPS65090_IRQ(1, 2),
64 	[11]		= TPS65090_IRQ(1, 3),
65 	[12]		= TPS65090_IRQ(1, 4),
66 	[13]		= TPS65090_IRQ(1, 5),
67 	[14]		= TPS65090_IRQ(1, 6),
68 	[15]		= TPS65090_IRQ(1, 7),
69 };
70 
71 static struct mfd_cell tps65090s[] = {
72 	{
73 		.name = "tps65090-pmic",
74 	},
75 	{
76 		.name = "tps65090-charger",
77 	},
78 };
79 
80 static void tps65090_irq_lock(struct irq_data *data)
81 {
82 	struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
83 
84 	mutex_lock(&tps65090->irq_lock);
85 }
86 
87 static void tps65090_irq_mask(struct irq_data *irq_data)
88 {
89 	struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
90 	unsigned int __irq = irq_data->hwirq;
91 	const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
92 
93 	tps65090_set_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
94 		data->mask_pos);
95 }
96 
97 static void tps65090_irq_unmask(struct irq_data *irq_data)
98 {
99 	struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
100 	unsigned int __irq = irq_data->irq - tps65090->irq_base;
101 	const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
102 
103 	tps65090_clr_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
104 		data->mask_pos);
105 }
106 
107 static void tps65090_irq_sync_unlock(struct irq_data *data)
108 {
109 	struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
110 
111 	mutex_unlock(&tps65090->irq_lock);
112 }
113 
114 static irqreturn_t tps65090_irq(int irq, void *data)
115 {
116 	struct tps65090 *tps65090 = data;
117 	int ret = 0;
118 	u8 status, mask;
119 	unsigned long int acks = 0;
120 	int i;
121 
122 	for (i = 0; i < NUM_INT_REG; i++) {
123 		ret = tps65090_read(tps65090->dev, TPS65090_INT_MSK + i, &mask);
124 		if (ret < 0) {
125 			dev_err(tps65090->dev,
126 				"failed to read mask reg [addr:%d]\n",
127 				TPS65090_INT_MSK + i);
128 			return IRQ_NONE;
129 		}
130 		ret = tps65090_read(tps65090->dev, TPS65090_INT_STS + i,
131 			&status);
132 		if (ret < 0) {
133 			dev_err(tps65090->dev,
134 				"failed to read status reg [addr:%d]\n",
135 				 TPS65090_INT_STS + i);
136 			return IRQ_NONE;
137 		}
138 		if (status) {
139 			/* Ack only those interrupts which are not masked */
140 			status &= (~mask);
141 			ret = tps65090_write(tps65090->dev,
142 					TPS65090_INT_STS + i, status);
143 			if (ret < 0) {
144 				dev_err(tps65090->dev,
145 					"failed to write interrupt status\n");
146 				return IRQ_NONE;
147 			}
148 			acks |= (status << (i * 8));
149 		}
150 	}
151 
152 	for_each_set_bit(i, &acks, ARRAY_SIZE(tps65090_irqs))
153 		handle_nested_irq(tps65090->irq_base + i);
154 	return acks ? IRQ_HANDLED : IRQ_NONE;
155 }
156 
157 static int __devinit tps65090_irq_init(struct tps65090 *tps65090, int irq,
158 	int irq_base)
159 {
160 	int i, ret;
161 
162 	if (!irq_base) {
163 		dev_err(tps65090->dev, "IRQ base not set\n");
164 		return -EINVAL;
165 	}
166 
167 	mutex_init(&tps65090->irq_lock);
168 
169 	for (i = 0; i < NUM_INT_REG; i++)
170 		tps65090_write(tps65090->dev, TPS65090_INT_MSK + i, 0xFF);
171 
172 	for (i = 0; i < NUM_INT_REG; i++)
173 		tps65090_write(tps65090->dev, TPS65090_INT_STS + i, 0xff);
174 
175 	tps65090->irq_base = irq_base;
176 	tps65090->irq_chip.name = "tps65090";
177 	tps65090->irq_chip.irq_mask = tps65090_irq_mask;
178 	tps65090->irq_chip.irq_unmask = tps65090_irq_unmask;
179 	tps65090->irq_chip.irq_bus_lock = tps65090_irq_lock;
180 	tps65090->irq_chip.irq_bus_sync_unlock = tps65090_irq_sync_unlock;
181 
182 	for (i = 0; i < ARRAY_SIZE(tps65090_irqs); i++) {
183 		int __irq = i + tps65090->irq_base;
184 		irq_set_chip_data(__irq, tps65090);
185 		irq_set_chip_and_handler(__irq, &tps65090->irq_chip,
186 					 handle_simple_irq);
187 		irq_set_nested_thread(__irq, 1);
188 #ifdef CONFIG_ARM
189 		set_irq_flags(__irq, IRQF_VALID);
190 #endif
191 	}
192 
193 	ret = request_threaded_irq(irq, NULL, tps65090_irq, IRQF_ONESHOT,
194 				"tps65090", tps65090);
195 	if (!ret) {
196 		device_init_wakeup(tps65090->dev, 1);
197 		enable_irq_wake(irq);
198 	}
199 
200 	return ret;
201 }
202 
203 static bool is_volatile_reg(struct device *dev, unsigned int reg)
204 {
205 	if (reg == TPS65090_INT_STS)
206 		return true;
207 	else
208 		return false;
209 }
210 
211 static const struct regmap_config tps65090_regmap_config = {
212 	.reg_bits = 8,
213 	.val_bits = 8,
214 	.max_register = TOTAL_NUM_REG,
215 	.num_reg_defaults_raw = TOTAL_NUM_REG,
216 	.cache_type = REGCACHE_RBTREE,
217 	.volatile_reg = is_volatile_reg,
218 };
219 
220 static int __devinit tps65090_i2c_probe(struct i2c_client *client,
221 					const struct i2c_device_id *id)
222 {
223 	struct tps65090_platform_data *pdata = client->dev.platform_data;
224 	struct tps65090 *tps65090;
225 	int ret;
226 
227 	if (!pdata) {
228 		dev_err(&client->dev, "tps65090 requires platform data\n");
229 		return -EINVAL;
230 	}
231 
232 	tps65090 = devm_kzalloc(&client->dev, sizeof(*tps65090), GFP_KERNEL);
233 	if (!tps65090) {
234 		dev_err(&client->dev, "mem alloc for tps65090 failed\n");
235 		return -ENOMEM;
236 	}
237 
238 	tps65090->dev = &client->dev;
239 	i2c_set_clientdata(client, tps65090);
240 
241 	if (client->irq) {
242 		ret = tps65090_irq_init(tps65090, client->irq, pdata->irq_base);
243 		if (ret) {
244 			dev_err(&client->dev, "IRQ init failed with err: %d\n",
245 				ret);
246 			goto err_exit;
247 		}
248 	}
249 
250 	tps65090->rmap = devm_regmap_init_i2c(client, &tps65090_regmap_config);
251 	if (IS_ERR(tps65090->rmap)) {
252 		ret = PTR_ERR(tps65090->rmap);
253 		dev_err(&client->dev, "regmap_init failed with err: %d\n", ret);
254 		goto err_irq_exit;
255 	}
256 
257 	ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
258 			      ARRAY_SIZE(tps65090s), NULL, 0, NULL);
259 	if (ret) {
260 		dev_err(&client->dev, "add mfd devices failed with err: %d\n",
261 			ret);
262 		goto err_irq_exit;
263 	}
264 
265 	return 0;
266 
267 err_irq_exit:
268 	if (client->irq)
269 		free_irq(client->irq, tps65090);
270 err_exit:
271 	return ret;
272 }
273 
274 static int __devexit tps65090_i2c_remove(struct i2c_client *client)
275 {
276 	struct tps65090 *tps65090 = i2c_get_clientdata(client);
277 
278 	mfd_remove_devices(tps65090->dev);
279 	if (client->irq)
280 		free_irq(client->irq, tps65090);
281 
282 	return 0;
283 }
284 
285 #ifdef CONFIG_PM_SLEEP
286 static int tps65090_suspend(struct device *dev)
287 {
288 	struct i2c_client *client = to_i2c_client(dev);
289 	if (client->irq)
290 		disable_irq(client->irq);
291 	return 0;
292 }
293 
294 static int tps65090_resume(struct device *dev)
295 {
296 	struct i2c_client *client = to_i2c_client(dev);
297 	if (client->irq)
298 		enable_irq(client->irq);
299 	return 0;
300 }
301 #endif
302 
303 static const struct dev_pm_ops tps65090_pm_ops = {
304 	SET_SYSTEM_SLEEP_PM_OPS(tps65090_suspend, tps65090_resume)
305 };
306 
307 static const struct i2c_device_id tps65090_id_table[] = {
308 	{ "tps65090", 0 },
309 	{ },
310 };
311 MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
312 
313 static struct i2c_driver tps65090_driver = {
314 	.driver	= {
315 		.name	= "tps65090",
316 		.owner	= THIS_MODULE,
317 		.pm	= &tps65090_pm_ops,
318 	},
319 	.probe		= tps65090_i2c_probe,
320 	.remove		= __devexit_p(tps65090_i2c_remove),
321 	.id_table	= tps65090_id_table,
322 };
323 
324 static int __init tps65090_init(void)
325 {
326 	return i2c_add_driver(&tps65090_driver);
327 }
328 subsys_initcall(tps65090_init);
329 
330 static void __exit tps65090_exit(void)
331 {
332 	i2c_del_driver(&tps65090_driver);
333 }
334 module_exit(tps65090_exit);
335 
336 MODULE_DESCRIPTION("TPS65090 core driver");
337 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
338 MODULE_LICENSE("GPL v2");
339