1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // Copyright (C) 2018 ROHM Semiconductors
5 // ROHM BD71837MWV and BD71847MWV PMIC driver
7 // Datasheet for BD71837MWV available from
8 // https://www.rohm.com/datasheet/BD71837MWV/bd71837mwv-e
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>
21 static struct gpio_keys_button button = {
27 static struct gpio_keys_platform_data bd718xx_powerkey_data = {
30 .name = "bd718xx-pwrkey",
33 static struct mfd_cell bd71837_mfd_cells[] = {
36 .platform_data = &bd718xx_powerkey_data,
37 .pdata_size = sizeof(bd718xx_powerkey_data),
39 { .name = "bd71837-clk", },
40 { .name = "bd71837-pmic", },
43 static struct mfd_cell bd71847_mfd_cells[] = {
46 .platform_data = &bd718xx_powerkey_data,
47 .pdata_size = sizeof(bd718xx_powerkey_data),
49 { .name = "bd71847-clk", },
50 { .name = "bd71847-pmic", },
53 static const struct regmap_irq bd718xx_irqs[] = {
54 REGMAP_IRQ_REG(BD718XX_INT_SWRST, 0, BD718XX_INT_SWRST_MASK),
55 REGMAP_IRQ_REG(BD718XX_INT_PWRBTN_S, 0, BD718XX_INT_PWRBTN_S_MASK),
56 REGMAP_IRQ_REG(BD718XX_INT_PWRBTN_L, 0, BD718XX_INT_PWRBTN_L_MASK),
57 REGMAP_IRQ_REG(BD718XX_INT_PWRBTN, 0, BD718XX_INT_PWRBTN_MASK),
58 REGMAP_IRQ_REG(BD718XX_INT_WDOG, 0, BD718XX_INT_WDOG_MASK),
59 REGMAP_IRQ_REG(BD718XX_INT_ON_REQ, 0, BD718XX_INT_ON_REQ_MASK),
60 REGMAP_IRQ_REG(BD718XX_INT_STBY_REQ, 0, BD718XX_INT_STBY_REQ_MASK),
63 static struct regmap_irq_chip bd718xx_irq_chip = {
64 .name = "bd718xx-irq",
66 .num_irqs = ARRAY_SIZE(bd718xx_irqs),
69 .status_base = BD718XX_REG_IRQ,
70 .mask_base = BD718XX_REG_MIRQ,
71 .ack_base = BD718XX_REG_IRQ,
72 .init_ack_masked = true,
76 static const struct regmap_range pmic_status_range = {
77 .range_min = BD718XX_REG_IRQ,
78 .range_max = BD718XX_REG_POW_STATE,
81 static const struct regmap_access_table volatile_regs = {
82 .yes_ranges = &pmic_status_range,
86 static const struct regmap_config bd718xx_regmap_config = {
89 .volatile_table = &volatile_regs,
90 .max_register = BD718XX_MAX_REGISTER - 1,
91 .cache_type = REGCACHE_RBTREE,
94 static int bd718xx_init_press_duration(struct regmap *regmap,
97 u32 short_press_ms, long_press_ms;
98 u32 short_press_value, long_press_value;
101 ret = of_property_read_u32(dev->of_node, "rohm,short-press-ms",
104 short_press_value = min(15u, (short_press_ms + 250) / 500);
105 ret = regmap_update_bits(regmap, BD718XX_REG_PWRONCONFIG0,
106 BD718XX_PWRBTN_PRESS_DURATION_MASK,
109 dev_err(dev, "Failed to init pwron short press\n");
114 ret = of_property_read_u32(dev->of_node, "rohm,long-press-ms",
117 long_press_value = min(15u, (long_press_ms + 500) / 1000);
118 ret = regmap_update_bits(regmap, BD718XX_REG_PWRONCONFIG1,
119 BD718XX_PWRBTN_PRESS_DURATION_MASK,
122 dev_err(dev, "Failed to init pwron long press\n");
130 static int bd718xx_i2c_probe(struct i2c_client *i2c,
131 const struct i2c_device_id *id)
133 struct regmap *regmap;
134 struct regmap_irq_chip_data *irq_data;
136 unsigned int chip_type;
137 struct mfd_cell *mfd;
141 dev_err(&i2c->dev, "No IRQ configured\n");
144 chip_type = (unsigned int)(uintptr_t)
145 of_device_get_match_data(&i2c->dev);
147 case ROHM_CHIP_TYPE_BD71837:
148 mfd = bd71837_mfd_cells;
149 cells = ARRAY_SIZE(bd71837_mfd_cells);
151 case ROHM_CHIP_TYPE_BD71847:
152 mfd = bd71847_mfd_cells;
153 cells = ARRAY_SIZE(bd71847_mfd_cells);
156 dev_err(&i2c->dev, "Unknown device type");
160 regmap = devm_regmap_init_i2c(i2c, &bd718xx_regmap_config);
161 if (IS_ERR(regmap)) {
162 dev_err(&i2c->dev, "regmap initialization failed\n");
163 return PTR_ERR(regmap);
166 ret = devm_regmap_add_irq_chip(&i2c->dev, regmap, i2c->irq,
167 IRQF_ONESHOT, 0, &bd718xx_irq_chip,
170 dev_err(&i2c->dev, "Failed to add irq_chip\n");
174 ret = bd718xx_init_press_duration(regmap, &i2c->dev);
178 ret = regmap_irq_get_virq(irq_data, BD718XX_INT_PWRBTN_S);
181 dev_err(&i2c->dev, "Failed to get the IRQ\n");
187 ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_AUTO,
189 regmap_irq_get_domain(irq_data));
191 dev_err(&i2c->dev, "Failed to create subdevices\n");
196 static const struct of_device_id bd718xx_of_match[] = {
198 .compatible = "rohm,bd71837",
199 .data = (void *)ROHM_CHIP_TYPE_BD71837,
202 .compatible = "rohm,bd71847",
203 .data = (void *)ROHM_CHIP_TYPE_BD71847,
206 .compatible = "rohm,bd71850",
207 .data = (void *)ROHM_CHIP_TYPE_BD71847,
211 MODULE_DEVICE_TABLE(of, bd718xx_of_match);
213 static struct i2c_driver bd718xx_i2c_driver = {
215 .name = "rohm-bd718x7",
216 .of_match_table = bd718xx_of_match,
218 .probe = bd718xx_i2c_probe,
221 static int __init bd718xx_i2c_init(void)
223 return i2c_add_driver(&bd718xx_i2c_driver);
226 /* Initialise early so consumer devices can complete system boot */
227 subsys_initcall(bd718xx_i2c_init);
229 static void __exit bd718xx_i2c_exit(void)
231 i2c_del_driver(&bd718xx_i2c_driver);
233 module_exit(bd718xx_i2c_exit);
235 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
236 MODULE_DESCRIPTION("ROHM BD71837/BD71847 Power Management IC driver");
237 MODULE_LICENSE("GPL");