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 bd718xx_mfd_cells
[] = {
36 .platform_data
= &bd718xx_powerkey_data
,
37 .pdata_size
= sizeof(bd718xx_powerkey_data
),
39 { .name
= "bd718xx-clk", },
40 { .name
= "bd718xx-pmic", },
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
),
53 static struct regmap_irq_chip bd718xx_irq_chip
= {
54 .name
= "bd718xx-irq",
56 .num_irqs
= ARRAY_SIZE(bd718xx_irqs
),
59 .status_base
= BD718XX_REG_IRQ
,
60 .mask_base
= BD718XX_REG_MIRQ
,
61 .ack_base
= BD718XX_REG_IRQ
,
62 .init_ack_masked
= true,
66 static const struct regmap_range pmic_status_range
= {
67 .range_min
= BD718XX_REG_IRQ
,
68 .range_max
= BD718XX_REG_POW_STATE
,
71 static const struct regmap_access_table volatile_regs
= {
72 .yes_ranges
= &pmic_status_range
,
76 static const struct regmap_config bd718xx_regmap_config
= {
79 .volatile_table
= &volatile_regs
,
80 .max_register
= BD718XX_MAX_REGISTER
- 1,
81 .cache_type
= REGCACHE_RBTREE
,
84 static int bd718xx_i2c_probe(struct i2c_client
*i2c
,
85 const struct i2c_device_id
*id
)
87 struct bd718xx
*bd718xx
;
91 dev_err(&i2c
->dev
, "No IRQ configured\n");
95 bd718xx
= devm_kzalloc(&i2c
->dev
, sizeof(struct bd718xx
), GFP_KERNEL
);
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
);
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
);
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
);
116 dev_err(&i2c
->dev
, "Failed to add irq_chip\n");
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
);
127 "Failed to configure button short press timeout\n");
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
);
139 "Failed to configure button long press timeout\n");
143 ret
= regmap_irq_get_virq(bd718xx
->irq_data
, BD718XX_INT_PWRBTN_S
);
146 dev_err(&i2c
->dev
, "Failed to get the IRQ\n");
152 ret
= devm_mfd_add_devices(bd718xx
->dev
, PLATFORM_DEVID_AUTO
,
154 ARRAY_SIZE(bd718xx_mfd_cells
), NULL
, 0,
155 regmap_irq_get_domain(bd718xx
->irq_data
));
157 dev_err(&i2c
->dev
, "Failed to create subdevices\n");
162 static const struct of_device_id bd718xx_of_match
[] = {
164 .compatible
= "rohm,bd71837",
165 .data
= (void *)BD718XX_TYPE_BD71837
,
168 .compatible
= "rohm,bd71847",
169 .data
= (void *)BD718XX_TYPE_BD71847
,
173 MODULE_DEVICE_TABLE(of
, bd718xx_of_match
);
175 static struct i2c_driver bd718xx_i2c_driver
= {
177 .name
= "rohm-bd718x7",
178 .of_match_table
= bd718xx_of_match
,
180 .probe
= bd718xx_i2c_probe
,
183 static int __init
bd718xx_i2c_init(void)
185 return i2c_add_driver(&bd718xx_i2c_driver
);
188 /* Initialise early so consumer devices can complete system boot */
189 subsys_initcall(bd718xx_i2c_init
);
191 static void __exit
bd718xx_i2c_exit(void)
193 i2c_del_driver(&bd718xx_i2c_driver
);
195 module_exit(bd718xx_i2c_exit
);
197 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
198 MODULE_DESCRIPTION("ROHM BD71837/BD71847 Power Management IC driver");
199 MODULE_LICENSE("GPL");