Linux 4.19.133
[linux/fpc-iii.git] / drivers / mfd / tps6586x.c
blob9c7925ca13cf008f6580bad59554eeb86a1b70dd
1 /*
2 * Core driver for TI TPS6586x PMIC family
4 * Copyright (c) 2010 CompuLab Ltd.
5 * Mike Rapoport <mike@compulab.co.il>
7 * Based on da903x.c.
8 * Copyright (C) 2008 Compulab, Ltd.
9 * Mike Rapoport <mike@compulab.co.il>
10 * Copyright (C) 2006-2008 Marvell International Ltd.
11 * Eric Miao <eric.miao@marvell.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
18 #include <linux/interrupt.h>
19 #include <linux/irq.h>
20 #include <linux/irqdomain.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/mutex.h>
24 #include <linux/slab.h>
25 #include <linux/err.h>
26 #include <linux/i2c.h>
27 #include <linux/platform_device.h>
28 #include <linux/regmap.h>
29 #include <linux/of.h>
31 #include <linux/mfd/core.h>
32 #include <linux/mfd/tps6586x.h>
34 #define TPS6586X_SUPPLYENE 0x14
35 #define EXITSLREQ_BIT BIT(1)
36 #define SLEEP_MODE_BIT BIT(3)
38 /* interrupt control registers */
39 #define TPS6586X_INT_ACK1 0xb5
40 #define TPS6586X_INT_ACK2 0xb6
41 #define TPS6586X_INT_ACK3 0xb7
42 #define TPS6586X_INT_ACK4 0xb8
44 /* interrupt mask registers */
45 #define TPS6586X_INT_MASK1 0xb0
46 #define TPS6586X_INT_MASK2 0xb1
47 #define TPS6586X_INT_MASK3 0xb2
48 #define TPS6586X_INT_MASK4 0xb3
49 #define TPS6586X_INT_MASK5 0xb4
51 /* device id */
52 #define TPS6586X_VERSIONCRC 0xcd
54 /* Maximum register */
55 #define TPS6586X_MAX_REGISTER TPS6586X_VERSIONCRC
57 struct tps6586x_irq_data {
58 u8 mask_reg;
59 u8 mask_mask;
62 #define TPS6586X_IRQ(_reg, _mask) \
63 { \
64 .mask_reg = (_reg) - TPS6586X_INT_MASK1, \
65 .mask_mask = (_mask), \
68 static const struct tps6586x_irq_data tps6586x_irqs[] = {
69 [TPS6586X_INT_PLDO_0] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 0),
70 [TPS6586X_INT_PLDO_1] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 1),
71 [TPS6586X_INT_PLDO_2] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 2),
72 [TPS6586X_INT_PLDO_3] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 3),
73 [TPS6586X_INT_PLDO_4] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 4),
74 [TPS6586X_INT_PLDO_5] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 5),
75 [TPS6586X_INT_PLDO_6] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 6),
76 [TPS6586X_INT_PLDO_7] = TPS6586X_IRQ(TPS6586X_INT_MASK1, 1 << 7),
77 [TPS6586X_INT_COMP_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 0),
78 [TPS6586X_INT_ADC] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 1),
79 [TPS6586X_INT_PLDO_8] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 2),
80 [TPS6586X_INT_PLDO_9] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 3),
81 [TPS6586X_INT_PSM_0] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 4),
82 [TPS6586X_INT_PSM_1] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 5),
83 [TPS6586X_INT_PSM_2] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 6),
84 [TPS6586X_INT_PSM_3] = TPS6586X_IRQ(TPS6586X_INT_MASK2, 1 << 7),
85 [TPS6586X_INT_RTC_ALM1] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 4),
86 [TPS6586X_INT_ACUSB_OVP] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 0x03),
87 [TPS6586X_INT_USB_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 2),
88 [TPS6586X_INT_AC_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 3),
89 [TPS6586X_INT_BAT_DET] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 1 << 0),
90 [TPS6586X_INT_CHG_STAT] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 0xfc),
91 [TPS6586X_INT_CHG_TEMP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0x06),
92 [TPS6586X_INT_PP] = TPS6586X_IRQ(TPS6586X_INT_MASK3, 0xf0),
93 [TPS6586X_INT_RESUME] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 5),
94 [TPS6586X_INT_LOW_SYS] = TPS6586X_IRQ(TPS6586X_INT_MASK5, 1 << 6),
95 [TPS6586X_INT_RTC_ALM2] = TPS6586X_IRQ(TPS6586X_INT_MASK4, 1 << 1),
98 static struct resource tps6586x_rtc_resources[] = {
100 .start = TPS6586X_INT_RTC_ALM1,
101 .end = TPS6586X_INT_RTC_ALM1,
102 .flags = IORESOURCE_IRQ,
106 static const struct mfd_cell tps6586x_cell[] = {
108 .name = "tps6586x-gpio",
111 .name = "tps6586x-regulator",
114 .name = "tps6586x-rtc",
115 .num_resources = ARRAY_SIZE(tps6586x_rtc_resources),
116 .resources = &tps6586x_rtc_resources[0],
119 .name = "tps6586x-onkey",
123 struct tps6586x {
124 struct device *dev;
125 struct i2c_client *client;
126 struct regmap *regmap;
127 int version;
129 int irq;
130 struct irq_chip irq_chip;
131 struct mutex irq_lock;
132 int irq_base;
133 u32 irq_en;
134 u8 mask_reg[5];
135 struct irq_domain *irq_domain;
138 static inline struct tps6586x *dev_to_tps6586x(struct device *dev)
140 return i2c_get_clientdata(to_i2c_client(dev));
143 int tps6586x_write(struct device *dev, int reg, uint8_t val)
145 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
147 return regmap_write(tps6586x->regmap, reg, val);
149 EXPORT_SYMBOL_GPL(tps6586x_write);
151 int tps6586x_writes(struct device *dev, int reg, int len, uint8_t *val)
153 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
155 return regmap_bulk_write(tps6586x->regmap, reg, val, len);
157 EXPORT_SYMBOL_GPL(tps6586x_writes);
159 int tps6586x_read(struct device *dev, int reg, uint8_t *val)
161 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
162 unsigned int rval;
163 int ret;
165 ret = regmap_read(tps6586x->regmap, reg, &rval);
166 if (!ret)
167 *val = rval;
168 return ret;
170 EXPORT_SYMBOL_GPL(tps6586x_read);
172 int tps6586x_reads(struct device *dev, int reg, int len, uint8_t *val)
174 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
176 return regmap_bulk_read(tps6586x->regmap, reg, val, len);
178 EXPORT_SYMBOL_GPL(tps6586x_reads);
180 int tps6586x_set_bits(struct device *dev, int reg, uint8_t bit_mask)
182 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
184 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, bit_mask);
186 EXPORT_SYMBOL_GPL(tps6586x_set_bits);
188 int tps6586x_clr_bits(struct device *dev, int reg, uint8_t bit_mask)
190 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
192 return regmap_update_bits(tps6586x->regmap, reg, bit_mask, 0);
194 EXPORT_SYMBOL_GPL(tps6586x_clr_bits);
196 int tps6586x_update(struct device *dev, int reg, uint8_t val, uint8_t mask)
198 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
200 return regmap_update_bits(tps6586x->regmap, reg, mask, val);
202 EXPORT_SYMBOL_GPL(tps6586x_update);
204 int tps6586x_irq_get_virq(struct device *dev, int irq)
206 struct tps6586x *tps6586x = dev_to_tps6586x(dev);
208 return irq_create_mapping(tps6586x->irq_domain, irq);
210 EXPORT_SYMBOL_GPL(tps6586x_irq_get_virq);
212 int tps6586x_get_version(struct device *dev)
214 struct tps6586x *tps6586x = dev_get_drvdata(dev);
216 return tps6586x->version;
218 EXPORT_SYMBOL_GPL(tps6586x_get_version);
220 static int __remove_subdev(struct device *dev, void *unused)
222 platform_device_unregister(to_platform_device(dev));
223 return 0;
226 static int tps6586x_remove_subdevs(struct tps6586x *tps6586x)
228 return device_for_each_child(tps6586x->dev, NULL, __remove_subdev);
231 static void tps6586x_irq_lock(struct irq_data *data)
233 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
235 mutex_lock(&tps6586x->irq_lock);
238 static void tps6586x_irq_enable(struct irq_data *irq_data)
240 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
241 unsigned int __irq = irq_data->hwirq;
242 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
244 tps6586x->mask_reg[data->mask_reg] &= ~data->mask_mask;
245 tps6586x->irq_en |= (1 << __irq);
248 static void tps6586x_irq_disable(struct irq_data *irq_data)
250 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
252 unsigned int __irq = irq_data->hwirq;
253 const struct tps6586x_irq_data *data = &tps6586x_irqs[__irq];
255 tps6586x->mask_reg[data->mask_reg] |= data->mask_mask;
256 tps6586x->irq_en &= ~(1 << __irq);
259 static void tps6586x_irq_sync_unlock(struct irq_data *data)
261 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(data);
262 int i;
264 for (i = 0; i < ARRAY_SIZE(tps6586x->mask_reg); i++) {
265 int ret;
266 ret = tps6586x_write(tps6586x->dev,
267 TPS6586X_INT_MASK1 + i,
268 tps6586x->mask_reg[i]);
269 WARN_ON(ret);
272 mutex_unlock(&tps6586x->irq_lock);
275 #ifdef CONFIG_PM_SLEEP
276 static int tps6586x_irq_set_wake(struct irq_data *irq_data, unsigned int on)
278 struct tps6586x *tps6586x = irq_data_get_irq_chip_data(irq_data);
279 return irq_set_irq_wake(tps6586x->irq, on);
281 #else
282 #define tps6586x_irq_set_wake NULL
283 #endif
285 static struct irq_chip tps6586x_irq_chip = {
286 .name = "tps6586x",
287 .irq_bus_lock = tps6586x_irq_lock,
288 .irq_bus_sync_unlock = tps6586x_irq_sync_unlock,
289 .irq_disable = tps6586x_irq_disable,
290 .irq_enable = tps6586x_irq_enable,
291 .irq_set_wake = tps6586x_irq_set_wake,
294 static int tps6586x_irq_map(struct irq_domain *h, unsigned int virq,
295 irq_hw_number_t hw)
297 struct tps6586x *tps6586x = h->host_data;
299 irq_set_chip_data(virq, tps6586x);
300 irq_set_chip_and_handler(virq, &tps6586x_irq_chip, handle_simple_irq);
301 irq_set_nested_thread(virq, 1);
302 irq_set_noprobe(virq);
304 return 0;
307 static const struct irq_domain_ops tps6586x_domain_ops = {
308 .map = tps6586x_irq_map,
309 .xlate = irq_domain_xlate_twocell,
312 static irqreturn_t tps6586x_irq(int irq, void *data)
314 struct tps6586x *tps6586x = data;
315 u32 acks;
316 int ret = 0;
318 ret = tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1,
319 sizeof(acks), (uint8_t *)&acks);
321 if (ret < 0) {
322 dev_err(tps6586x->dev, "failed to read interrupt status\n");
323 return IRQ_NONE;
326 acks = le32_to_cpu(acks);
328 while (acks) {
329 int i = __ffs(acks);
331 if (tps6586x->irq_en & (1 << i))
332 handle_nested_irq(
333 irq_find_mapping(tps6586x->irq_domain, i));
335 acks &= ~(1 << i);
338 return IRQ_HANDLED;
341 static int tps6586x_irq_init(struct tps6586x *tps6586x, int irq,
342 int irq_base)
344 int i, ret;
345 u8 tmp[4];
346 int new_irq_base;
347 int irq_num = ARRAY_SIZE(tps6586x_irqs);
349 tps6586x->irq = irq;
351 mutex_init(&tps6586x->irq_lock);
352 for (i = 0; i < 5; i++) {
353 tps6586x->mask_reg[i] = 0xff;
354 tps6586x_write(tps6586x->dev, TPS6586X_INT_MASK1 + i, 0xff);
357 tps6586x_reads(tps6586x->dev, TPS6586X_INT_ACK1, sizeof(tmp), tmp);
359 if (irq_base > 0) {
360 new_irq_base = irq_alloc_descs(irq_base, 0, irq_num, -1);
361 if (new_irq_base < 0) {
362 dev_err(tps6586x->dev,
363 "Failed to alloc IRQs: %d\n", new_irq_base);
364 return new_irq_base;
366 } else {
367 new_irq_base = 0;
370 tps6586x->irq_domain = irq_domain_add_simple(tps6586x->dev->of_node,
371 irq_num, new_irq_base, &tps6586x_domain_ops,
372 tps6586x);
373 if (!tps6586x->irq_domain) {
374 dev_err(tps6586x->dev, "Failed to create IRQ domain\n");
375 return -ENOMEM;
377 ret = request_threaded_irq(irq, NULL, tps6586x_irq, IRQF_ONESHOT,
378 "tps6586x", tps6586x);
380 if (!ret)
381 device_init_wakeup(tps6586x->dev, 1);
383 return ret;
386 static int tps6586x_add_subdevs(struct tps6586x *tps6586x,
387 struct tps6586x_platform_data *pdata)
389 struct tps6586x_subdev_info *subdev;
390 struct platform_device *pdev;
391 int i, ret = 0;
393 for (i = 0; i < pdata->num_subdevs; i++) {
394 subdev = &pdata->subdevs[i];
396 pdev = platform_device_alloc(subdev->name, subdev->id);
397 if (!pdev) {
398 ret = -ENOMEM;
399 goto failed;
402 pdev->dev.parent = tps6586x->dev;
403 pdev->dev.platform_data = subdev->platform_data;
404 pdev->dev.of_node = subdev->of_node;
406 ret = platform_device_add(pdev);
407 if (ret) {
408 platform_device_put(pdev);
409 goto failed;
412 return 0;
414 failed:
415 tps6586x_remove_subdevs(tps6586x);
416 return ret;
419 #ifdef CONFIG_OF
420 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
422 struct device_node *np = client->dev.of_node;
423 struct tps6586x_platform_data *pdata;
425 pdata = devm_kzalloc(&client->dev, sizeof(*pdata), GFP_KERNEL);
426 if (!pdata)
427 return NULL;
429 pdata->num_subdevs = 0;
430 pdata->subdevs = NULL;
431 pdata->gpio_base = -1;
432 pdata->irq_base = -1;
433 pdata->pm_off = of_property_read_bool(np, "ti,system-power-controller");
435 return pdata;
438 static const struct of_device_id tps6586x_of_match[] = {
439 { .compatible = "ti,tps6586x", },
440 { },
442 #else
443 static struct tps6586x_platform_data *tps6586x_parse_dt(struct i2c_client *client)
445 return NULL;
447 #endif
449 static bool is_volatile_reg(struct device *dev, unsigned int reg)
451 /* Cache all interrupt mask register */
452 if ((reg >= TPS6586X_INT_MASK1) && (reg <= TPS6586X_INT_MASK5))
453 return false;
455 return true;
458 static const struct regmap_config tps6586x_regmap_config = {
459 .reg_bits = 8,
460 .val_bits = 8,
461 .max_register = TPS6586X_MAX_REGISTER,
462 .volatile_reg = is_volatile_reg,
463 .cache_type = REGCACHE_RBTREE,
466 static struct device *tps6586x_dev;
467 static void tps6586x_power_off(void)
469 if (tps6586x_clr_bits(tps6586x_dev, TPS6586X_SUPPLYENE, EXITSLREQ_BIT))
470 return;
472 tps6586x_set_bits(tps6586x_dev, TPS6586X_SUPPLYENE, SLEEP_MODE_BIT);
475 static void tps6586x_print_version(struct i2c_client *client, int version)
477 const char *name;
479 switch (version) {
480 case TPS658621A:
481 name = "TPS658621A";
482 break;
483 case TPS658621CD:
484 name = "TPS658621C/D";
485 break;
486 case TPS658623:
487 name = "TPS658623";
488 break;
489 case TPS658640:
490 case TPS658640v2:
491 name = "TPS658640";
492 break;
493 case TPS658643:
494 name = "TPS658643";
495 break;
496 default:
497 name = "TPS6586X";
498 break;
501 dev_info(&client->dev, "Found %s, VERSIONCRC is %02x\n", name, version);
504 static int tps6586x_i2c_probe(struct i2c_client *client,
505 const struct i2c_device_id *id)
507 struct tps6586x_platform_data *pdata = dev_get_platdata(&client->dev);
508 struct tps6586x *tps6586x;
509 int ret;
510 int version;
512 if (!pdata && client->dev.of_node)
513 pdata = tps6586x_parse_dt(client);
515 if (!pdata) {
516 dev_err(&client->dev, "tps6586x requires platform data\n");
517 return -ENOTSUPP;
520 version = i2c_smbus_read_byte_data(client, TPS6586X_VERSIONCRC);
521 if (version < 0) {
522 dev_err(&client->dev, "Chip ID read failed: %d\n", version);
523 return -EIO;
526 tps6586x = devm_kzalloc(&client->dev, sizeof(*tps6586x), GFP_KERNEL);
527 if (!tps6586x)
528 return -ENOMEM;
530 tps6586x->version = version;
531 tps6586x_print_version(client, tps6586x->version);
533 tps6586x->client = client;
534 tps6586x->dev = &client->dev;
535 i2c_set_clientdata(client, tps6586x);
537 tps6586x->regmap = devm_regmap_init_i2c(client,
538 &tps6586x_regmap_config);
539 if (IS_ERR(tps6586x->regmap)) {
540 ret = PTR_ERR(tps6586x->regmap);
541 dev_err(&client->dev, "regmap init failed: %d\n", ret);
542 return ret;
546 if (client->irq) {
547 ret = tps6586x_irq_init(tps6586x, client->irq,
548 pdata->irq_base);
549 if (ret) {
550 dev_err(&client->dev, "IRQ init failed: %d\n", ret);
551 return ret;
555 ret = mfd_add_devices(tps6586x->dev, -1,
556 tps6586x_cell, ARRAY_SIZE(tps6586x_cell),
557 NULL, 0, tps6586x->irq_domain);
558 if (ret < 0) {
559 dev_err(&client->dev, "mfd_add_devices failed: %d\n", ret);
560 goto err_mfd_add;
563 ret = tps6586x_add_subdevs(tps6586x, pdata);
564 if (ret) {
565 dev_err(&client->dev, "add devices failed: %d\n", ret);
566 goto err_add_devs;
569 if (pdata->pm_off && !pm_power_off) {
570 tps6586x_dev = &client->dev;
571 pm_power_off = tps6586x_power_off;
574 return 0;
576 err_add_devs:
577 mfd_remove_devices(tps6586x->dev);
578 err_mfd_add:
579 if (client->irq)
580 free_irq(client->irq, tps6586x);
581 return ret;
584 static int tps6586x_i2c_remove(struct i2c_client *client)
586 struct tps6586x *tps6586x = i2c_get_clientdata(client);
588 tps6586x_remove_subdevs(tps6586x);
589 mfd_remove_devices(tps6586x->dev);
590 if (client->irq)
591 free_irq(client->irq, tps6586x);
592 return 0;
595 static int __maybe_unused tps6586x_i2c_suspend(struct device *dev)
597 struct tps6586x *tps6586x = dev_get_drvdata(dev);
599 if (tps6586x->client->irq)
600 disable_irq(tps6586x->client->irq);
602 return 0;
605 static int __maybe_unused tps6586x_i2c_resume(struct device *dev)
607 struct tps6586x *tps6586x = dev_get_drvdata(dev);
609 if (tps6586x->client->irq)
610 enable_irq(tps6586x->client->irq);
612 return 0;
615 static SIMPLE_DEV_PM_OPS(tps6586x_pm_ops, tps6586x_i2c_suspend,
616 tps6586x_i2c_resume);
618 static const struct i2c_device_id tps6586x_id_table[] = {
619 { "tps6586x", 0 },
620 { },
622 MODULE_DEVICE_TABLE(i2c, tps6586x_id_table);
624 static struct i2c_driver tps6586x_driver = {
625 .driver = {
626 .name = "tps6586x",
627 .of_match_table = of_match_ptr(tps6586x_of_match),
628 .pm = &tps6586x_pm_ops,
630 .probe = tps6586x_i2c_probe,
631 .remove = tps6586x_i2c_remove,
632 .id_table = tps6586x_id_table,
635 static int __init tps6586x_init(void)
637 return i2c_add_driver(&tps6586x_driver);
639 subsys_initcall(tps6586x_init);
641 static void __exit tps6586x_exit(void)
643 i2c_del_driver(&tps6586x_driver);
645 module_exit(tps6586x_exit);
647 MODULE_DESCRIPTION("TPS6586X core driver");
648 MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>");
649 MODULE_LICENSE("GPL");