]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/mfd/rohm-bd718x7.c
mfd: regulator: clk: Split rohm-bd718x7.h
[linux.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.chip_type = (unsigned int)(uintptr_t)
102                                 of_device_get_match_data(&i2c->dev);
103         bd718xx->chip.dev = &i2c->dev;
104         dev_set_drvdata(&i2c->dev, bd718xx);
105
106         bd718xx->chip.regmap = devm_regmap_init_i2c(i2c,
107                                                     &bd718xx_regmap_config);
108         if (IS_ERR(bd718xx->chip.regmap)) {
109                 dev_err(&i2c->dev, "regmap initialization failed\n");
110                 return PTR_ERR(bd718xx->chip.regmap);
111         }
112
113         ret = devm_regmap_add_irq_chip(&i2c->dev, bd718xx->chip.regmap,
114                                        bd718xx->chip_irq, IRQF_ONESHOT, 0,
115                                        &bd718xx_irq_chip, &bd718xx->irq_data);
116         if (ret) {
117                 dev_err(&i2c->dev, "Failed to add irq_chip\n");
118                 return ret;
119         }
120
121         /* Configure short press to 10 milliseconds */
122         ret = regmap_update_bits(bd718xx->chip.regmap,
123                                  BD718XX_REG_PWRONCONFIG0,
124                                  BD718XX_PWRBTN_PRESS_DURATION_MASK,
125                                  BD718XX_PWRBTN_SHORT_PRESS_10MS);
126         if (ret) {
127                 dev_err(&i2c->dev,
128                         "Failed to configure button short press timeout\n");
129                 return ret;
130         }
131
132         /* Configure long press to 10 seconds */
133         ret = regmap_update_bits(bd718xx->chip.regmap,
134                                  BD718XX_REG_PWRONCONFIG1,
135                                  BD718XX_PWRBTN_PRESS_DURATION_MASK,
136                                  BD718XX_PWRBTN_LONG_PRESS_10S);
137
138         if (ret) {
139                 dev_err(&i2c->dev,
140                         "Failed to configure button long press timeout\n");
141                 return ret;
142         }
143
144         ret = regmap_irq_get_virq(bd718xx->irq_data, BD718XX_INT_PWRBTN_S);
145
146         if (ret < 0) {
147                 dev_err(&i2c->dev, "Failed to get the IRQ\n");
148                 return ret;
149         }
150
151         button.irq = ret;
152
153         ret = devm_mfd_add_devices(bd718xx->chip.dev, PLATFORM_DEVID_AUTO,
154                                    bd718xx_mfd_cells,
155                                    ARRAY_SIZE(bd718xx_mfd_cells), NULL, 0,
156                                    regmap_irq_get_domain(bd718xx->irq_data));
157         if (ret)
158                 dev_err(&i2c->dev, "Failed to create subdevices\n");
159
160         return ret;
161 }
162
163 static const struct of_device_id bd718xx_of_match[] = {
164         {
165                 .compatible = "rohm,bd71837",
166                 .data = (void *)ROHM_CHIP_TYPE_BD71837,
167         },
168         {
169                 .compatible = "rohm,bd71847",
170                 .data = (void *)ROHM_CHIP_TYPE_BD71847,
171         },
172         { }
173 };
174 MODULE_DEVICE_TABLE(of, bd718xx_of_match);
175
176 static struct i2c_driver bd718xx_i2c_driver = {
177         .driver = {
178                 .name = "rohm-bd718x7",
179                 .of_match_table = bd718xx_of_match,
180         },
181         .probe = bd718xx_i2c_probe,
182 };
183
184 static int __init bd718xx_i2c_init(void)
185 {
186         return i2c_add_driver(&bd718xx_i2c_driver);
187 }
188
189 /* Initialise early so consumer devices can complete system boot */
190 subsys_initcall(bd718xx_i2c_init);
191
192 static void __exit bd718xx_i2c_exit(void)
193 {
194         i2c_del_driver(&bd718xx_i2c_driver);
195 }
196 module_exit(bd718xx_i2c_exit);
197
198 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
199 MODULE_DESCRIPTION("ROHM BD71837/BD71847 Power Management IC driver");
200 MODULE_LICENSE("GPL");