GNU Linux-libre 4.19.286-gnu1
[releases.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 PMIC driver
6 //
7 // Datasheet available from
8 // https://www.rohm.com/datasheet/BD71837MWV/bd71837mwv-e
9
10 #include <linux/i2c.h>
11 #include <linux/input.h>
12 #include <linux/interrupt.h>
13 #include <linux/mfd/rohm-bd718x7.h>
14 #include <linux/mfd/core.h>
15 #include <linux/module.h>
16 #include <linux/regmap.h>
17
18 /*
19  * gpio_keys.h requires definiton of bool. It is brought in
20  * by above includes. Keep this as last until gpio_keys.h gets fixed.
21  */
22 #include <linux/gpio_keys.h>
23
24 static const u8 supported_revisions[] = { 0xA2 /* BD71837 */ };
25
26 static struct gpio_keys_button button = {
27         .code = KEY_POWER,
28         .gpio = -1,
29         .type = EV_KEY,
30 };
31
32 static struct gpio_keys_platform_data bd718xx_powerkey_data = {
33         .buttons = &button,
34         .nbuttons = 1,
35         .name = "bd718xx-pwrkey",
36 };
37
38 static struct mfd_cell bd71837_mfd_cells[] = {
39         {
40                 .name = "gpio-keys",
41                 .platform_data = &bd718xx_powerkey_data,
42                 .pdata_size = sizeof(bd718xx_powerkey_data),
43         },
44         { .name = "bd71837-clk", },
45         { .name = "bd71837-pmic", },
46 };
47
48 static const struct regmap_irq bd71837_irqs[] = {
49         REGMAP_IRQ_REG(BD71837_INT_SWRST, 0, BD71837_INT_SWRST_MASK),
50         REGMAP_IRQ_REG(BD71837_INT_PWRBTN_S, 0, BD71837_INT_PWRBTN_S_MASK),
51         REGMAP_IRQ_REG(BD71837_INT_PWRBTN_L, 0, BD71837_INT_PWRBTN_L_MASK),
52         REGMAP_IRQ_REG(BD71837_INT_PWRBTN, 0, BD71837_INT_PWRBTN_MASK),
53         REGMAP_IRQ_REG(BD71837_INT_WDOG, 0, BD71837_INT_WDOG_MASK),
54         REGMAP_IRQ_REG(BD71837_INT_ON_REQ, 0, BD71837_INT_ON_REQ_MASK),
55         REGMAP_IRQ_REG(BD71837_INT_STBY_REQ, 0, BD71837_INT_STBY_REQ_MASK),
56 };
57
58 static struct regmap_irq_chip bd71837_irq_chip = {
59         .name = "bd71837-irq",
60         .irqs = bd71837_irqs,
61         .num_irqs = ARRAY_SIZE(bd71837_irqs),
62         .num_regs = 1,
63         .irq_reg_stride = 1,
64         .status_base = BD71837_REG_IRQ,
65         .mask_base = BD71837_REG_MIRQ,
66         .ack_base = BD71837_REG_IRQ,
67         .init_ack_masked = true,
68         .mask_invert = false,
69 };
70
71 static const struct regmap_range pmic_status_range = {
72         .range_min = BD71837_REG_IRQ,
73         .range_max = BD71837_REG_POW_STATE,
74 };
75
76 static const struct regmap_access_table volatile_regs = {
77         .yes_ranges = &pmic_status_range,
78         .n_yes_ranges = 1,
79 };
80
81 static const struct regmap_config bd71837_regmap_config = {
82         .reg_bits = 8,
83         .val_bits = 8,
84         .volatile_table = &volatile_regs,
85         .max_register = BD71837_MAX_REGISTER - 1,
86         .cache_type = REGCACHE_RBTREE,
87 };
88
89 static int bd71837_i2c_probe(struct i2c_client *i2c,
90                             const struct i2c_device_id *id)
91 {
92         struct bd71837 *bd71837;
93         int ret, i;
94         unsigned int val;
95
96         bd71837 = devm_kzalloc(&i2c->dev, sizeof(struct bd71837), GFP_KERNEL);
97
98         if (!bd71837)
99                 return -ENOMEM;
100
101         bd71837->chip_irq = i2c->irq;
102
103         if (!bd71837->chip_irq) {
104                 dev_err(&i2c->dev, "No IRQ configured\n");
105                 return -EINVAL;
106         }
107
108         bd71837->dev = &i2c->dev;
109         dev_set_drvdata(&i2c->dev, bd71837);
110
111         bd71837->regmap = devm_regmap_init_i2c(i2c, &bd71837_regmap_config);
112         if (IS_ERR(bd71837->regmap)) {
113                 dev_err(&i2c->dev, "regmap initialization failed\n");
114                 return PTR_ERR(bd71837->regmap);
115         }
116
117         ret = regmap_read(bd71837->regmap, BD71837_REG_REV, &val);
118         if (ret) {
119                 dev_err(&i2c->dev, "Read BD71837_REG_DEVICE failed\n");
120                 return ret;
121         }
122         for (i = 0; i < ARRAY_SIZE(supported_revisions); i++)
123                 if (supported_revisions[i] == val)
124                         break;
125
126         if (i == ARRAY_SIZE(supported_revisions)) {
127                 dev_err(&i2c->dev, "Unsupported chip revision\n");
128                 return -ENODEV;
129         }
130
131         ret = devm_regmap_add_irq_chip(&i2c->dev, bd71837->regmap,
132                                        bd71837->chip_irq, IRQF_ONESHOT, 0,
133                                        &bd71837_irq_chip, &bd71837->irq_data);
134         if (ret) {
135                 dev_err(&i2c->dev, "Failed to add irq_chip\n");
136                 return ret;
137         }
138
139         /* Configure short press to 10 milliseconds */
140         ret = regmap_update_bits(bd71837->regmap,
141                                  BD71837_REG_PWRONCONFIG0,
142                                  BD718XX_PWRBTN_PRESS_DURATION_MASK,
143                                  BD718XX_PWRBTN_SHORT_PRESS_10MS);
144         if (ret) {
145                 dev_err(&i2c->dev,
146                         "Failed to configure button short press timeout\n");
147                 return ret;
148         }
149
150         /* Configure long press to 10 seconds */
151         ret = regmap_update_bits(bd71837->regmap,
152                                  BD71837_REG_PWRONCONFIG1,
153                                  BD718XX_PWRBTN_PRESS_DURATION_MASK,
154                                  BD718XX_PWRBTN_LONG_PRESS_10S);
155
156         if (ret) {
157                 dev_err(&i2c->dev,
158                         "Failed to configure button long press timeout\n");
159                 return ret;
160         }
161
162         ret = regmap_irq_get_virq(bd71837->irq_data, BD71837_INT_PWRBTN_S);
163
164         if (ret < 0) {
165                 dev_err(&i2c->dev, "Failed to get the IRQ\n");
166                 return ret;
167         }
168
169         button.irq = ret;
170
171         ret = devm_mfd_add_devices(bd71837->dev, PLATFORM_DEVID_AUTO,
172                                    bd71837_mfd_cells,
173                                    ARRAY_SIZE(bd71837_mfd_cells), NULL, 0,
174                                    regmap_irq_get_domain(bd71837->irq_data));
175         if (ret)
176                 dev_err(&i2c->dev, "Failed to create subdevices\n");
177
178         return ret;
179 }
180
181 static const struct of_device_id bd71837_of_match[] = {
182         { .compatible = "rohm,bd71837", },
183         { }
184 };
185 MODULE_DEVICE_TABLE(of, bd71837_of_match);
186
187 static struct i2c_driver bd71837_i2c_driver = {
188         .driver = {
189                 .name = "rohm-bd718x7",
190                 .of_match_table = bd71837_of_match,
191         },
192         .probe = bd71837_i2c_probe,
193 };
194
195 static int __init bd71837_i2c_init(void)
196 {
197         return i2c_add_driver(&bd71837_i2c_driver);
198 }
199
200 /* Initialise early so consumer devices can complete system boot */
201 subsys_initcall(bd71837_i2c_init);
202
203 static void __exit bd71837_i2c_exit(void)
204 {
205         i2c_del_driver(&bd71837_i2c_driver);
206 }
207 module_exit(bd71837_i2c_exit);
208
209 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
210 MODULE_DESCRIPTION("ROHM BD71837 Power Management IC driver");
211 MODULE_LICENSE("GPL");