mfd: tps65217: Add support for IRQs
[cascardo/linux.git] / drivers / mfd / tps65217.c
1 /*
2  * tps65217.c
3  *
4  * TPS65217 chip family multi-function driver
5  *
6  * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation version 2.
11  *
12  * This program is distributed "as is" WITHOUT ANY WARRANTY of any
13  * kind, whether express or implied; without even the implied warranty
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <linux/device.h>
19 #include <linux/err.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/i2c.h>
23 #include <linux/irq.h>
24 #include <linux/irqdomain.h>
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/of.h>
28 #include <linux/of_device.h>
29 #include <linux/platform_device.h>
30 #include <linux/regmap.h>
31 #include <linux/slab.h>
32
33 #include <linux/mfd/core.h>
34 #include <linux/mfd/tps65217.h>
35
36 static struct resource charger_resources[] = {
37         DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_AC, "AC"),
38         DEFINE_RES_IRQ_NAMED(TPS65217_IRQ_USB, "USB"),
39 };
40
41 struct tps65217_irq {
42         int mask;
43         int interrupt;
44 };
45
46 static const struct tps65217_irq tps65217_irqs[] = {
47         [TPS65217_IRQ_PB] = {
48                 .mask = TPS65217_INT_PBM,
49                 .interrupt = TPS65217_INT_PBI,
50         },
51         [TPS65217_IRQ_AC] = {
52                 .mask = TPS65217_INT_ACM,
53                 .interrupt = TPS65217_INT_ACI,
54         },
55         [TPS65217_IRQ_USB] = {
56                 .mask = TPS65217_INT_USBM,
57                 .interrupt = TPS65217_INT_USBI,
58         },
59 };
60
61 static void tps65217_irq_lock(struct irq_data *data)
62 {
63         struct tps65217 *tps = irq_data_get_irq_chip_data(data);
64
65         mutex_lock(&tps->irq_lock);
66 }
67
68 static void tps65217_irq_sync_unlock(struct irq_data *data)
69 {
70         struct tps65217 *tps = irq_data_get_irq_chip_data(data);
71         int ret;
72
73         ret = tps65217_reg_write(tps, TPS65217_REG_INT, tps->irq_mask,
74                                 TPS65217_PROTECT_NONE);
75         if (ret != 0)
76                 dev_err(tps->dev, "Failed to sync IRQ masks\n");
77
78         mutex_unlock(&tps->irq_lock);
79 }
80
81 static const inline struct tps65217_irq *
82 irq_to_tps65217_irq(struct tps65217 *tps, struct irq_data *data)
83 {
84         return &tps65217_irqs[data->hwirq];
85 }
86
87 static void tps65217_irq_enable(struct irq_data *data)
88 {
89         struct tps65217 *tps = irq_data_get_irq_chip_data(data);
90         const struct tps65217_irq *irq_data = irq_to_tps65217_irq(tps, data);
91
92         tps->irq_mask &= ~irq_data->mask;
93 }
94
95 static void tps65217_irq_disable(struct irq_data *data)
96 {
97         struct tps65217 *tps = irq_data_get_irq_chip_data(data);
98         const struct tps65217_irq *irq_data = irq_to_tps65217_irq(tps, data);
99
100         tps->irq_mask |= irq_data->mask;
101 }
102
103 static struct irq_chip tps65217_irq_chip = {
104         .irq_bus_lock           = tps65217_irq_lock,
105         .irq_bus_sync_unlock    = tps65217_irq_sync_unlock,
106         .irq_enable             = tps65217_irq_enable,
107         .irq_disable            = tps65217_irq_disable,
108 };
109
110 static struct mfd_cell tps65217s[] = {
111         {
112                 .name = "tps65217-pmic",
113                 .of_compatible = "ti,tps65217-pmic",
114         },
115         {
116                 .name = "tps65217-bl",
117                 .of_compatible = "ti,tps65217-bl",
118         },
119         {
120                 .name = "tps65217-charger",
121                 .num_resources = ARRAY_SIZE(charger_resources),
122                 .resources = charger_resources,
123                 .of_compatible = "ti,tps65217-charger",
124         },
125 };
126
127 static irqreturn_t tps65217_irq_thread(int irq, void *data)
128 {
129         struct tps65217 *tps = data;
130         unsigned int status;
131         bool handled = false;
132         int i;
133         int ret;
134
135         ret = tps65217_reg_read(tps, TPS65217_REG_INT, &status);
136         if (ret < 0) {
137                 dev_err(tps->dev, "Failed to read IRQ status: %d\n",
138                         ret);
139                 return IRQ_NONE;
140         }
141
142         for (i = 0; i < ARRAY_SIZE(tps65217_irqs); i++) {
143                 if (status & tps65217_irqs[i].interrupt) {
144                         handle_nested_irq(irq_find_mapping(tps->irq_domain, i));
145                         handled = true;
146                 }
147         }
148
149         if (handled)
150                 return IRQ_HANDLED;
151
152         return IRQ_NONE;
153 }
154
155 static int tps65217_irq_map(struct irq_domain *h, unsigned int virq,
156                         irq_hw_number_t hw)
157 {
158         struct tps65217 *tps = h->host_data;
159
160         irq_set_chip_data(virq, tps);
161         irq_set_chip_and_handler(virq, &tps65217_irq_chip, handle_edge_irq);
162         irq_set_nested_thread(virq, 1);
163         irq_set_parent(virq, tps->irq);
164         irq_set_noprobe(virq);
165
166         return 0;
167 }
168
169 static const struct irq_domain_ops tps65217_irq_domain_ops = {
170         .map = tps65217_irq_map,
171 };
172
173 static int tps65217_irq_init(struct tps65217 *tps, int irq)
174 {
175         int ret;
176
177         mutex_init(&tps->irq_lock);
178         tps->irq = irq;
179
180         /* Mask all interrupt sources */
181         tps->irq_mask = (TPS65217_INT_RESERVEDM | TPS65217_INT_PBM
182                         | TPS65217_INT_ACM | TPS65217_INT_USBM);
183         tps65217_reg_write(tps, TPS65217_REG_INT, tps->irq_mask,
184                         TPS65217_PROTECT_NONE);
185
186         tps->irq_domain = irq_domain_add_linear(tps->dev->of_node,
187                 TPS65217_NUM_IRQ, &tps65217_irq_domain_ops, tps);
188         if (!tps->irq_domain) {
189                 dev_err(tps->dev, "Could not create IRQ domain\n");
190                 return -ENOMEM;
191         }
192
193         ret = devm_request_threaded_irq(tps->dev, irq, NULL,
194                                         tps65217_irq_thread, IRQF_ONESHOT,
195                                         "tps65217-irq", tps);
196         if (ret) {
197                 dev_err(tps->dev, "Failed to request IRQ %d: %d\n",
198                         irq, ret);
199                 return ret;
200         }
201
202         return 0;
203 }
204
205 /**
206  * tps65217_reg_read: Read a single tps65217 register.
207  *
208  * @tps: Device to read from.
209  * @reg: Register to read.
210  * @val: Contians the value
211  */
212 int tps65217_reg_read(struct tps65217 *tps, unsigned int reg,
213                         unsigned int *val)
214 {
215         return regmap_read(tps->regmap, reg, val);
216 }
217 EXPORT_SYMBOL_GPL(tps65217_reg_read);
218
219 /**
220  * tps65217_reg_write: Write a single tps65217 register.
221  *
222  * @tps65217: Device to write to.
223  * @reg: Register to write to.
224  * @val: Value to write.
225  * @level: Password protected level
226  */
227 int tps65217_reg_write(struct tps65217 *tps, unsigned int reg,
228                         unsigned int val, unsigned int level)
229 {
230         int ret;
231         unsigned int xor_reg_val;
232
233         switch (level) {
234         case TPS65217_PROTECT_NONE:
235                 return regmap_write(tps->regmap, reg, val);
236         case TPS65217_PROTECT_L1:
237                 xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
238                 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
239                                                         xor_reg_val);
240                 if (ret < 0)
241                         return ret;
242
243                 return regmap_write(tps->regmap, reg, val);
244         case TPS65217_PROTECT_L2:
245                 xor_reg_val = reg ^ TPS65217_PASSWORD_REGS_UNLOCK;
246                 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
247                                                         xor_reg_val);
248                 if (ret < 0)
249                         return ret;
250                 ret = regmap_write(tps->regmap, reg, val);
251                 if (ret < 0)
252                         return ret;
253                 ret = regmap_write(tps->regmap, TPS65217_REG_PASSWORD,
254                                                         xor_reg_val);
255                 if (ret < 0)
256                         return ret;
257                 return regmap_write(tps->regmap, reg, val);
258         default:
259                 return -EINVAL;
260         }
261 }
262 EXPORT_SYMBOL_GPL(tps65217_reg_write);
263
264 /**
265  * tps65217_update_bits: Modify bits w.r.t mask, val and level.
266  *
267  * @tps65217: Device to write to.
268  * @reg: Register to read-write to.
269  * @mask: Mask.
270  * @val: Value to write.
271  * @level: Password protected level
272  */
273 static int tps65217_update_bits(struct tps65217 *tps, unsigned int reg,
274                 unsigned int mask, unsigned int val, unsigned int level)
275 {
276         int ret;
277         unsigned int data;
278
279         ret = tps65217_reg_read(tps, reg, &data);
280         if (ret) {
281                 dev_err(tps->dev, "Read from reg 0x%x failed\n", reg);
282                 return ret;
283         }
284
285         data &= ~mask;
286         data |= val & mask;
287
288         ret = tps65217_reg_write(tps, reg, data, level);
289         if (ret)
290                 dev_err(tps->dev, "Write for reg 0x%x failed\n", reg);
291
292         return ret;
293 }
294
295 int tps65217_set_bits(struct tps65217 *tps, unsigned int reg,
296                 unsigned int mask, unsigned int val, unsigned int level)
297 {
298         return tps65217_update_bits(tps, reg, mask, val, level);
299 }
300 EXPORT_SYMBOL_GPL(tps65217_set_bits);
301
302 int tps65217_clear_bits(struct tps65217 *tps, unsigned int reg,
303                 unsigned int mask, unsigned int level)
304 {
305         return tps65217_update_bits(tps, reg, mask, 0, level);
306 }
307 EXPORT_SYMBOL_GPL(tps65217_clear_bits);
308
309 static bool tps65217_volatile_reg(struct device *dev, unsigned int reg)
310 {
311         switch (reg) {
312         case TPS65217_REG_INT:
313                 return true;
314         default:
315                 return false;
316         }
317 }
318
319 static const struct regmap_config tps65217_regmap_config = {
320         .reg_bits = 8,
321         .val_bits = 8,
322
323         .max_register = TPS65217_REG_MAX,
324         .volatile_reg = tps65217_volatile_reg,
325 };
326
327 static const struct of_device_id tps65217_of_match[] = {
328         { .compatible = "ti,tps65217", .data = (void *)TPS65217 },
329         { /* sentinel */ },
330 };
331 MODULE_DEVICE_TABLE(of, tps65217_of_match);
332
333 static int tps65217_probe(struct i2c_client *client,
334                                 const struct i2c_device_id *ids)
335 {
336         struct tps65217 *tps;
337         unsigned int version;
338         unsigned long chip_id = ids->driver_data;
339         const struct of_device_id *match;
340         bool status_off = false;
341         int ret;
342
343         if (client->dev.of_node) {
344                 match = of_match_device(tps65217_of_match, &client->dev);
345                 if (!match) {
346                         dev_err(&client->dev,
347                                 "Failed to find matching dt id\n");
348                         return -EINVAL;
349                 }
350                 chip_id = (unsigned long)match->data;
351                 status_off = of_property_read_bool(client->dev.of_node,
352                                         "ti,pmic-shutdown-controller");
353         }
354
355         if (!chip_id) {
356                 dev_err(&client->dev, "id is null.\n");
357                 return -ENODEV;
358         }
359
360         tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
361         if (!tps)
362                 return -ENOMEM;
363
364         i2c_set_clientdata(client, tps);
365         tps->dev = &client->dev;
366         tps->id = chip_id;
367
368         tps->regmap = devm_regmap_init_i2c(client, &tps65217_regmap_config);
369         if (IS_ERR(tps->regmap)) {
370                 ret = PTR_ERR(tps->regmap);
371                 dev_err(tps->dev, "Failed to allocate register map: %d\n",
372                         ret);
373                 return ret;
374         }
375
376         if (client->irq) {
377                 tps65217_irq_init(tps, client->irq);
378         } else {
379                 int i;
380
381                 /* Don't tell children about IRQ resources which won't fire */
382                 for (i = 0; i < ARRAY_SIZE(tps65217s); i++)
383                         tps65217s[i].num_resources = 0;
384         }
385
386         ret = devm_mfd_add_devices(tps->dev, -1, tps65217s,
387                                    ARRAY_SIZE(tps65217s), NULL, 0,
388                                    tps->irq_domain);
389         if (ret < 0) {
390                 dev_err(tps->dev, "mfd_add_devices failed: %d\n", ret);
391                 return ret;
392         }
393
394         ret = tps65217_reg_read(tps, TPS65217_REG_CHIPID, &version);
395         if (ret < 0) {
396                 dev_err(tps->dev, "Failed to read revision register: %d\n",
397                         ret);
398                 return ret;
399         }
400
401         /* Set the PMIC to shutdown on PWR_EN toggle */
402         if (status_off) {
403                 ret = tps65217_set_bits(tps, TPS65217_REG_STATUS,
404                                 TPS65217_STATUS_OFF, TPS65217_STATUS_OFF,
405                                 TPS65217_PROTECT_NONE);
406                 if (ret)
407                         dev_warn(tps->dev, "unable to set the status OFF\n");
408         }
409
410         dev_info(tps->dev, "TPS65217 ID %#x version 1.%d\n",
411                         (version & TPS65217_CHIPID_CHIP_MASK) >> 4,
412                         version & TPS65217_CHIPID_REV_MASK);
413
414         return 0;
415 }
416
417 static const struct i2c_device_id tps65217_id_table[] = {
418         {"tps65217", TPS65217},
419         { /* sentinel */ }
420 };
421 MODULE_DEVICE_TABLE(i2c, tps65217_id_table);
422
423 static struct i2c_driver tps65217_driver = {
424         .driver         = {
425                 .name   = "tps65217",
426                 .of_match_table = tps65217_of_match,
427         },
428         .id_table       = tps65217_id_table,
429         .probe          = tps65217_probe,
430 };
431
432 static int __init tps65217_init(void)
433 {
434         return i2c_add_driver(&tps65217_driver);
435 }
436 subsys_initcall(tps65217_init);
437
438 static void __exit tps65217_exit(void)
439 {
440         i2c_del_driver(&tps65217_driver);
441 }
442 module_exit(tps65217_exit);
443
444 MODULE_AUTHOR("AnilKumar Ch <anilkumar@ti.com>");
445 MODULE_DESCRIPTION("TPS65217 chip family multi-function driver");
446 MODULE_LICENSE("GPL v2");