2 * Core driver for TI TPS65090 PMIC family
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
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.
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
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/>.
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/power_supply.h>
26 #include <linux/i2c.h>
27 #include <linux/mfd/core.h>
28 #include <linux/mfd/tps65090.h>
29 #include <linux/regmap.h>
30 #include <linux/err.h>
31 #include <linux/irqdomain.h>
34 #define TOTAL_NUM_REG 0x18
36 /* interrupt status registers */
37 #define TPS65090_INT_STS 0x0
38 #define TPS65090_INT_STS2 0x1
40 /* interrupt mask registers */
41 #define TPS65090_INT_MSK 0x2
42 #define TPS65090_INT_MSK2 0x3
44 /* FET control registers */
45 #define TPS65090_FET1_CTRL 0xf
46 #define TPS65090_FET7_CTRL 0x15
48 struct tps65090_irq_data {
53 #define TPS65090_IRQ(_reg, _mask_pos) \
56 .mask_pos = (_mask_pos), \
59 static const struct tps65090_irq_data tps65090_irqs[] = {
60 [0] = TPS65090_IRQ(0, 0),
61 [1] = TPS65090_IRQ(0, 1),
62 [2] = TPS65090_IRQ(0, 2),
63 [3] = TPS65090_IRQ(0, 3),
64 [4] = TPS65090_IRQ(0, 4),
65 [5] = TPS65090_IRQ(0, 5),
66 [6] = TPS65090_IRQ(0, 6),
67 [7] = TPS65090_IRQ(0, 7),
68 [8] = TPS65090_IRQ(1, 0),
69 [9] = TPS65090_IRQ(1, 1),
70 [10] = TPS65090_IRQ(1, 2),
71 [11] = TPS65090_IRQ(1, 3),
72 [12] = TPS65090_IRQ(1, 4),
73 [13] = TPS65090_IRQ(1, 5),
74 [14] = TPS65090_IRQ(1, 6),
75 [15] = TPS65090_IRQ(1, 7),
78 static struct mfd_cell tps65090s[] = {
80 .name = "tps65090-pmic",
83 .name = "tps65090-regulator",
86 .name = "tps65090-charger",
93 struct i2c_client *client;
95 struct irq_chip irq_chip;
96 struct mutex irq_lock;
99 struct power_supply *charger;
103 int tps65090_write(struct device *dev, int reg, uint8_t val)
105 struct tps65090 *tps = dev_get_drvdata(dev);
106 return regmap_write(tps->rmap, reg, val);
108 EXPORT_SYMBOL_GPL(tps65090_write);
110 int tps65090_read(struct device *dev, int reg, uint8_t *val)
112 struct tps65090 *tps = dev_get_drvdata(dev);
113 unsigned int temp_val;
115 ret = regmap_read(tps->rmap, reg, &temp_val);
120 EXPORT_SYMBOL_GPL(tps65090_read);
122 int tps65090_update_bits(struct device *dev, int reg, unsigned int mask,
125 struct tps65090 *tps = dev_get_drvdata(dev);
126 return regmap_update_bits(tps->rmap, reg, mask, val);
128 EXPORT_SYMBOL_GPL(tps65090_update_bits);
130 int tps65090_set_bits(struct device *dev, int reg, uint8_t bit_num)
132 struct tps65090 *tps = dev_get_drvdata(dev);
133 return regmap_update_bits(tps->rmap, reg, BIT(bit_num), ~0u);
135 EXPORT_SYMBOL_GPL(tps65090_set_bits);
137 int tps65090_clr_bits(struct device *dev, int reg, uint8_t bit_num)
139 struct tps65090 *tps = dev_get_drvdata(dev);
140 return regmap_update_bits(tps->rmap, reg, BIT(bit_num), 0u);
142 EXPORT_SYMBOL_GPL(tps65090_clr_bits);
144 static void tps65090_irq_lock(struct irq_data *data)
146 struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
148 mutex_lock(&tps65090->irq_lock);
151 static void tps65090_irq_mask(struct irq_data *irq_data)
153 struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
154 unsigned int __irq = irq_data->hwirq;
155 const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
157 tps65090_set_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
161 static void tps65090_irq_unmask(struct irq_data *irq_data)
163 struct tps65090 *tps65090 = irq_data_get_irq_chip_data(irq_data);
164 unsigned int __irq = irq_data->irq - tps65090->irq_base;
165 const struct tps65090_irq_data *data = &tps65090_irqs[__irq];
167 tps65090_clr_bits(tps65090->dev, (TPS65090_INT_MSK + data->mask_reg),
171 static void tps65090_irq_sync_unlock(struct irq_data *data)
173 struct tps65090 *tps65090 = irq_data_get_irq_chip_data(data);
175 mutex_unlock(&tps65090->irq_lock);
178 static irqreturn_t tps65090_irq(int irq, void *data)
180 struct tps65090 *tps65090 = data;
183 unsigned long int acks = 0;
186 if (!tps65090->charger)
188 power_supply_get_by_name("tps65090-charger");
189 if (tps65090->charger)
190 power_supply_changed((struct power_supply *)tps65090->charger);
192 if (tps65090->no_ack_irq)
195 for (i = 0; i < NUM_INT_REG; i++) {
196 ret = tps65090_read(tps65090->dev, TPS65090_INT_MSK + i, &mask);
198 dev_err(tps65090->dev,
199 "failed to read mask reg [addr:%d]\n",
200 TPS65090_INT_MSK + i);
203 ret = tps65090_read(tps65090->dev, TPS65090_INT_STS + i,
206 dev_err(tps65090->dev,
207 "failed to read status reg [addr:%d]\n",
208 TPS65090_INT_STS + i);
212 /* Ack only those interrupts which are not masked */
214 ret = tps65090_write(tps65090->dev,
215 TPS65090_INT_STS + i, status);
217 dev_err(tps65090->dev,
218 "failed to write interrupt status\n");
221 acks |= (status << (i * 8));
225 for_each_set_bit(i, &acks, ARRAY_SIZE(tps65090_irqs))
226 handle_nested_irq(tps65090->irq_base + i);
227 return acks ? IRQ_HANDLED : IRQ_NONE;
230 static int __devinit tps65090_irq_init(struct tps65090 *tps65090, int irq)
234 int nr_irqs = ARRAY_SIZE(tps65090_irqs);
236 irq_base = irq_alloc_descs(-1, 0, nr_irqs, 0);
237 if (IS_ERR_VALUE(irq_base)) {
238 dev_err(tps65090->dev, "Fail to allocate IRQ descs\n");
242 irq_domain_add_legacy(tps65090->dev->of_node, nr_irqs, irq_base, 0,
243 &irq_domain_simple_ops, NULL);
245 mutex_init(&tps65090->irq_lock);
247 for (i = 0; i < NUM_INT_REG; i++)
248 tps65090_write(tps65090->dev, TPS65090_INT_MSK + i, 0xFF);
250 for (i = 0; i < NUM_INT_REG; i++)
251 tps65090_write(tps65090->dev, TPS65090_INT_STS + i, 0xff);
253 tps65090->irq_base = irq_base;
254 tps65090->irq_chip.name = "tps65090";
255 tps65090->irq_chip.irq_mask = tps65090_irq_mask;
256 tps65090->irq_chip.irq_unmask = tps65090_irq_unmask;
257 tps65090->irq_chip.irq_bus_lock = tps65090_irq_lock;
258 tps65090->irq_chip.irq_bus_sync_unlock = tps65090_irq_sync_unlock;
260 tps65090->charger = power_supply_get_by_name("tps65090-charger");
262 for (i = 0; i < ARRAY_SIZE(tps65090_irqs); i++) {
263 int __irq = i + tps65090->irq_base;
264 irq_set_chip_data(__irq, tps65090);
265 irq_set_chip_and_handler(__irq, &tps65090->irq_chip,
267 irq_set_nested_thread(__irq, 1);
269 set_irq_flags(__irq, IRQF_VALID);
273 ret = request_threaded_irq(irq, NULL, tps65090_irq,
274 IRQF_ONESHOT | IRQF_TRIGGER_RISING,
275 "tps65090", tps65090);
277 dev_err(tps65090->dev, "failed to request threaded irq\n");
278 irq_free_descs(tps65090->irq_base, nr_irqs);
285 static bool is_volatile_reg(struct device *dev, unsigned int reg)
287 if ((reg == TPS65090_INT_STS) || (reg == TPS65090_INT_STS2) ||
288 (reg >= TPS65090_FET1_CTRL && reg <= TPS65090_FET7_CTRL))
294 static const struct regmap_config tps65090_regmap_config = {
297 .max_register = TOTAL_NUM_REG,
298 .num_reg_defaults_raw = TOTAL_NUM_REG,
299 .cache_type = REGCACHE_RBTREE,
300 .volatile_reg = is_volatile_reg,
303 static int __devinit tps65090_i2c_probe(struct i2c_client *client,
304 const struct i2c_device_id *id)
306 struct tps65090 *tps65090;
309 tps65090 = devm_kzalloc(&client->dev, sizeof(struct tps65090),
311 if (tps65090 == NULL)
314 tps65090->client = client;
315 tps65090->dev = &client->dev;
316 i2c_set_clientdata(client, tps65090);
318 mutex_init(&tps65090->lock);
320 tps65090->rmap = regmap_init_i2c(tps65090->client,
321 &tps65090_regmap_config);
322 if (IS_ERR(tps65090->rmap)) {
323 dev_err(&client->dev, "regmap_init failed with err: %ld\n",
324 PTR_ERR(tps65090->rmap));
330 if (of_find_property(tps65090->dev->of_node, "no-ack-irq", NULL)) {
331 dev_info(tps65090->dev, "Disabling irq acks to chip.\n");
332 tps65090->no_ack_irq = true;
337 ret = tps65090_irq_init(tps65090, client->irq);
339 dev_err(&client->dev, "IRQ init failed with err: %d\n",
344 ret = mfd_add_devices(tps65090->dev, -1, tps65090s,
345 ARRAY_SIZE(tps65090s), NULL, 0);
347 dev_err(&client->dev, "add mfd devices failed with err: %d\n",
349 goto err_regmap_exit;
355 regmap_exit(tps65090->rmap);
358 free_irq(client->irq, tps65090);
359 irq_free_descs(tps65090->irq_base, ARRAY_SIZE(tps65090_irqs));
365 static int __devexit tps65090_i2c_remove(struct i2c_client *client)
367 struct tps65090 *tps65090 = i2c_get_clientdata(client);
369 if (device_can_wakeup(tps65090->dev))
370 device_init_wakeup(tps65090->dev, 0);
371 mfd_remove_devices(tps65090->dev);
372 regmap_exit(tps65090->rmap);
374 free_irq(client->irq, tps65090);
375 irq_free_descs(tps65090->irq_base, ARRAY_SIZE(tps65090_irqs));
382 static int tps65090_i2c_suspend(struct i2c_client *client, pm_message_t state)
385 if (device_may_wakeup(&client->dev))
386 enable_irq_wake(client->irq);
387 disable_irq(client->irq);
392 static int tps65090_i2c_resume(struct i2c_client *client)
395 enable_irq(client->irq);
396 if (device_may_wakeup(&client->dev))
397 disable_irq_wake(client->irq);
403 static const struct i2c_device_id tps65090_id_table[] = {
407 MODULE_DEVICE_TABLE(i2c, tps65090_id_table);
409 static struct i2c_driver tps65090_driver = {
412 .owner = THIS_MODULE,
414 .probe = tps65090_i2c_probe,
415 .remove = __devexit_p(tps65090_i2c_remove),
417 .suspend = tps65090_i2c_suspend,
418 .resume = tps65090_i2c_resume,
420 .id_table = tps65090_id_table,
423 static int __init tps65090_init(void)
425 return i2c_add_driver(&tps65090_driver);
427 subsys_initcall(tps65090_init);
429 static void __exit tps65090_exit(void)
431 i2c_del_driver(&tps65090_driver);
433 module_exit(tps65090_exit);
435 MODULE_DESCRIPTION("TPS65090 core driver");
436 MODULE_AUTHOR("Venu Byravarasu <vbyravarasu@nvidia.com>");
437 MODULE_LICENSE("GPL v2");