1 // SPDX-License-Identifier: GPL-2.0-or-later
3 // Copyright (C) 2018 ROHM Semiconductors
5 // ROHM BD71837MWV PMIC driver
7 // Datasheet available from
8 // https://www.rohm.com/datasheet/BD71837MWV/bd71837mwv-e
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>
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.
22 #include <linux/gpio_keys.h>
24 static const u8 supported_revisions
[] = { 0xA2 /* BD71837 */ };
26 static struct gpio_keys_button button
= {
32 static struct gpio_keys_platform_data bd718xx_powerkey_data
= {
35 .name
= "bd718xx-pwrkey",
38 static struct mfd_cell bd71837_mfd_cells
[] = {
41 .platform_data
= &bd718xx_powerkey_data
,
42 .pdata_size
= sizeof(bd718xx_powerkey_data
),
44 { .name
= "bd71837-clk", },
45 { .name
= "bd71837-pmic", },
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
),
58 static struct regmap_irq_chip bd71837_irq_chip
= {
59 .name
= "bd71837-irq",
61 .num_irqs
= ARRAY_SIZE(bd71837_irqs
),
64 .status_base
= BD71837_REG_IRQ
,
65 .mask_base
= BD71837_REG_MIRQ
,
66 .ack_base
= BD71837_REG_IRQ
,
67 .init_ack_masked
= true,
71 static const struct regmap_range pmic_status_range
= {
72 .range_min
= BD71837_REG_IRQ
,
73 .range_max
= BD71837_REG_POW_STATE
,
76 static const struct regmap_access_table volatile_regs
= {
77 .yes_ranges
= &pmic_status_range
,
81 static const struct regmap_config bd71837_regmap_config
= {
84 .volatile_table
= &volatile_regs
,
85 .max_register
= BD71837_MAX_REGISTER
- 1,
86 .cache_type
= REGCACHE_RBTREE
,
89 static int bd71837_i2c_probe(struct i2c_client
*i2c
,
90 const struct i2c_device_id
*id
)
92 struct bd71837
*bd71837
;
96 bd71837
= devm_kzalloc(&i2c
->dev
, sizeof(struct bd71837
), GFP_KERNEL
);
101 bd71837
->chip_irq
= i2c
->irq
;
103 if (!bd71837
->chip_irq
) {
104 dev_err(&i2c
->dev
, "No IRQ configured\n");
108 bd71837
->dev
= &i2c
->dev
;
109 dev_set_drvdata(&i2c
->dev
, bd71837
);
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
);
117 ret
= regmap_read(bd71837
->regmap
, BD71837_REG_REV
, &val
);
119 dev_err(&i2c
->dev
, "Read BD71837_REG_DEVICE failed\n");
122 for (i
= 0; i
< ARRAY_SIZE(supported_revisions
); i
++)
123 if (supported_revisions
[i
] == val
)
126 if (i
== ARRAY_SIZE(supported_revisions
)) {
127 dev_err(&i2c
->dev
, "Unsupported chip revision\n");
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
);
135 dev_err(&i2c
->dev
, "Failed to add irq_chip\n");
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
);
146 "Failed to configure button short press timeout\n");
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
);
158 "Failed to configure button long press timeout\n");
162 ret
= regmap_irq_get_virq(bd71837
->irq_data
, BD71837_INT_PWRBTN_S
);
165 dev_err(&i2c
->dev
, "Failed to get the IRQ\n");
171 ret
= devm_mfd_add_devices(bd71837
->dev
, PLATFORM_DEVID_AUTO
,
173 ARRAY_SIZE(bd71837_mfd_cells
), NULL
, 0,
174 regmap_irq_get_domain(bd71837
->irq_data
));
176 dev_err(&i2c
->dev
, "Failed to create subdevices\n");
181 static const struct of_device_id bd71837_of_match
[] = {
182 { .compatible
= "rohm,bd71837", },
185 MODULE_DEVICE_TABLE(of
, bd71837_of_match
);
187 static struct i2c_driver bd71837_i2c_driver
= {
189 .name
= "rohm-bd718x7",
190 .of_match_table
= bd71837_of_match
,
192 .probe
= bd71837_i2c_probe
,
195 static int __init
bd71837_i2c_init(void)
197 return i2c_add_driver(&bd71837_i2c_driver
);
200 /* Initialise early so consumer devices can complete system boot */
201 subsys_initcall(bd71837_i2c_init
);
203 static void __exit
bd71837_i2c_exit(void)
205 i2c_del_driver(&bd71837_i2c_driver
);
207 module_exit(bd71837_i2c_exit
);
209 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
210 MODULE_DESCRIPTION("ROHM BD71837 Power Management IC driver");
211 MODULE_LICENSE("GPL");