Merge tag 'kgdb-5.2-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/danielt...
[linux-2.6-microblaze.git] / drivers / mfd / rohm-bd718x7.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 //
3 // Copyright (C) 2018 ROHM Semiconductors
4 //
5 // ROHM BD71837MWV and BD71847MWV PMIC driver
6 //
7 // Datasheet for BD71837MWV available from
8 // https://www.rohm.com/datasheet/BD71837MWV/bd71837mwv-e
9
10 #include <linux/gpio_keys.h>
11 #include <linux/i2c.h>
12 #include <linux/input.h>
13 #include <linux/interrupt.h>
14 #include <linux/mfd/rohm-bd718x7.h>
15 #include <linux/mfd/core.h>
16 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/regmap.h>
19 #include <linux/types.h>
20
21 static struct gpio_keys_button button = {
22         .code = KEY_POWER,
23         .gpio = -1,
24         .type = EV_KEY,
25 };
26
27 static struct gpio_keys_platform_data bd718xx_powerkey_data = {
28         .buttons = &button,
29         .nbuttons = 1,
30         .name = "bd718xx-pwrkey",
31 };
32
33 static struct mfd_cell bd718xx_mfd_cells[] = {
34         {
35                 .name = "gpio-keys",
36                 .platform_data = &bd718xx_powerkey_data,
37                 .pdata_size = sizeof(bd718xx_powerkey_data),
38         },
39         { .name = "bd718xx-clk", },
40         { .name = "bd718xx-pmic", },
41 };
42
43 static const struct regmap_irq bd718xx_irqs[] = {
44         REGMAP_IRQ_REG(BD718XX_INT_SWRST, 0, BD718XX_INT_SWRST_MASK),
45         REGMAP_IRQ_REG(BD718XX_INT_PWRBTN_S, 0, BD718XX_INT_PWRBTN_S_MASK),
46         REGMAP_IRQ_REG(BD718XX_INT_PWRBTN_L, 0, BD718XX_INT_PWRBTN_L_MASK),
47         REGMAP_IRQ_REG(BD718XX_INT_PWRBTN, 0, BD718XX_INT_PWRBTN_MASK),
48         REGMAP_IRQ_REG(BD718XX_INT_WDOG, 0, BD718XX_INT_WDOG_MASK),
49         REGMAP_IRQ_REG(BD718XX_INT_ON_REQ, 0, BD718XX_INT_ON_REQ_MASK),
50         REGMAP_IRQ_REG(BD718XX_INT_STBY_REQ, 0, BD718XX_INT_STBY_REQ_MASK),
51 };
52
53 static struct regmap_irq_chip bd718xx_irq_chip = {
54         .name = "bd718xx-irq",
55         .irqs = bd718xx_irqs,
56         .num_irqs = ARRAY_SIZE(bd718xx_irqs),
57         .num_regs = 1,
58         .irq_reg_stride = 1,
59         .status_base = BD718XX_REG_IRQ,
60         .mask_base = BD718XX_REG_MIRQ,
61         .ack_base = BD718XX_REG_IRQ,
62         .init_ack_masked = true,
63         .mask_invert = false,
64 };
65
66 static const struct regmap_range pmic_status_range = {
67         .range_min = BD718XX_REG_IRQ,
68         .range_max = BD718XX_REG_POW_STATE,
69 };
70
71 static const struct regmap_access_table volatile_regs = {
72         .yes_ranges = &pmic_status_range,
73         .n_yes_ranges = 1,
74 };
75
76 static const struct regmap_config bd718xx_regmap_config = {
77         .reg_bits = 8,
78         .val_bits = 8,
79         .volatile_table = &volatile_regs,
80         .max_register = BD718XX_MAX_REGISTER - 1,
81         .cache_type = REGCACHE_RBTREE,
82 };
83
84 static int bd718xx_i2c_probe(struct i2c_client *i2c,
85                             const struct i2c_device_id *id)
86 {
87         struct bd718xx *bd718xx;
88         int ret;
89
90         if (!i2c->irq) {
91                 dev_err(&i2c->dev, "No IRQ configured\n");
92                 return -EINVAL;
93         }
94
95         bd718xx = devm_kzalloc(&i2c->dev, sizeof(struct bd718xx), GFP_KERNEL);
96
97         if (!bd718xx)
98                 return -ENOMEM;
99
100         bd718xx->chip_irq = i2c->irq;
101         bd718xx->chip_type = (unsigned int)(uintptr_t)
102                                 of_device_get_match_data(&i2c->dev);
103         bd718xx->dev = &i2c->dev;
104         dev_set_drvdata(&i2c->dev, bd718xx);
105
106         bd718xx->regmap = devm_regmap_init_i2c(i2c, &bd718xx_regmap_config);
107         if (IS_ERR(bd718xx->regmap)) {
108                 dev_err(&i2c->dev, "regmap initialization failed\n");
109                 return PTR_ERR(bd718xx->regmap);
110         }
111
112         ret = devm_regmap_add_irq_chip(&i2c->dev, bd718xx->regmap,
113                                        bd718xx->chip_irq, IRQF_ONESHOT, 0,
114                                        &bd718xx_irq_chip, &bd718xx->irq_data);
115         if (ret) {
116                 dev_err(&i2c->dev, "Failed to add irq_chip\n");
117                 return ret;
118         }
119
120         /* Configure short press to 10 milliseconds */
121         ret = regmap_update_bits(bd718xx->regmap,
122                                  BD718XX_REG_PWRONCONFIG0,
123                                  BD718XX_PWRBTN_PRESS_DURATION_MASK,
124                                  BD718XX_PWRBTN_SHORT_PRESS_10MS);
125         if (ret) {
126                 dev_err(&i2c->dev,
127                         "Failed to configure button short press timeout\n");
128                 return ret;
129         }
130
131         /* Configure long press to 10 seconds */
132         ret = regmap_update_bits(bd718xx->regmap,
133                                  BD718XX_REG_PWRONCONFIG1,
134                                  BD718XX_PWRBTN_PRESS_DURATION_MASK,
135                                  BD718XX_PWRBTN_LONG_PRESS_10S);
136
137         if (ret) {
138                 dev_err(&i2c->dev,
139                         "Failed to configure button long press timeout\n");
140                 return ret;
141         }
142
143         ret = regmap_irq_get_virq(bd718xx->irq_data, BD718XX_INT_PWRBTN_S);
144
145         if (ret < 0) {
146                 dev_err(&i2c->dev, "Failed to get the IRQ\n");
147                 return ret;
148         }
149
150         button.irq = ret;
151
152         ret = devm_mfd_add_devices(bd718xx->dev, PLATFORM_DEVID_AUTO,
153                                    bd718xx_mfd_cells,
154                                    ARRAY_SIZE(bd718xx_mfd_cells), NULL, 0,
155                                    regmap_irq_get_domain(bd718xx->irq_data));
156         if (ret)
157                 dev_err(&i2c->dev, "Failed to create subdevices\n");
158
159         return ret;
160 }
161
162 static const struct of_device_id bd718xx_of_match[] = {
163         {
164                 .compatible = "rohm,bd71837",
165                 .data = (void *)BD718XX_TYPE_BD71837,
166         },
167         {
168                 .compatible = "rohm,bd71847",
169                 .data = (void *)BD718XX_TYPE_BD71847,
170         },
171         { }
172 };
173 MODULE_DEVICE_TABLE(of, bd718xx_of_match);
174
175 static struct i2c_driver bd718xx_i2c_driver = {
176         .driver = {
177                 .name = "rohm-bd718x7",
178                 .of_match_table = bd718xx_of_match,
179         },
180         .probe = bd718xx_i2c_probe,
181 };
182
183 static int __init bd718xx_i2c_init(void)
184 {
185         return i2c_add_driver(&bd718xx_i2c_driver);
186 }
187
188 /* Initialise early so consumer devices can complete system boot */
189 subsys_initcall(bd718xx_i2c_init);
190
191 static void __exit bd718xx_i2c_exit(void)
192 {
193         i2c_del_driver(&bd718xx_i2c_driver);
194 }
195 module_exit(bd718xx_i2c_exit);
196
197 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
198 MODULE_DESCRIPTION("ROHM BD71837/BD71847 Power Management IC driver");
199 MODULE_LICENSE("GPL");