xref: /linux/drivers/mfd/rohm-bd718x7.c (revision 8e07e0e3964ca4e23ce7b68e2096fe660a888942)
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.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 bd71837_mfd_cells[] = {
34 	{
35 		.name = "gpio-keys",
36 		.platform_data = &bd718xx_powerkey_data,
37 		.pdata_size = sizeof(bd718xx_powerkey_data),
38 	},
39 	{ .name = "bd71837-clk", },
40 	{ .name = "bd71837-pmic", },
41 };
42 
43 static struct mfd_cell bd71847_mfd_cells[] = {
44 	{
45 		.name = "gpio-keys",
46 		.platform_data = &bd718xx_powerkey_data,
47 		.pdata_size = sizeof(bd718xx_powerkey_data),
48 	},
49 	{ .name = "bd71847-clk", },
50 	{ .name = "bd71847-pmic", },
51 };
52 
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),
61 };
62 
63 static struct regmap_irq_chip bd718xx_irq_chip = {
64 	.name = "bd718xx-irq",
65 	.irqs = bd718xx_irqs,
66 	.num_irqs = ARRAY_SIZE(bd718xx_irqs),
67 	.num_regs = 1,
68 	.irq_reg_stride = 1,
69 	.status_base = BD718XX_REG_IRQ,
70 	.mask_base = BD718XX_REG_MIRQ,
71 	.ack_base = BD718XX_REG_IRQ,
72 	.init_ack_masked = true,
73 };
74 
75 static const struct regmap_range pmic_status_range = {
76 	.range_min = BD718XX_REG_IRQ,
77 	.range_max = BD718XX_REG_POW_STATE,
78 };
79 
80 static const struct regmap_access_table volatile_regs = {
81 	.yes_ranges = &pmic_status_range,
82 	.n_yes_ranges = 1,
83 };
84 
85 static const struct regmap_config bd718xx_regmap_config = {
86 	.reg_bits = 8,
87 	.val_bits = 8,
88 	.volatile_table = &volatile_regs,
89 	.max_register = BD718XX_MAX_REGISTER - 1,
90 	.cache_type = REGCACHE_RBTREE,
91 };
92 
93 static int bd718xx_init_press_duration(struct regmap *regmap,
94 				       struct device *dev)
95 {
96 	u32 short_press_ms, long_press_ms;
97 	u32 short_press_value, long_press_value;
98 	int ret;
99 
100 	ret = of_property_read_u32(dev->of_node, "rohm,short-press-ms",
101 				   &short_press_ms);
102 	if (!ret) {
103 		short_press_value = min(15u, (short_press_ms + 250) / 500);
104 		ret = regmap_update_bits(regmap, BD718XX_REG_PWRONCONFIG0,
105 					 BD718XX_PWRBTN_PRESS_DURATION_MASK,
106 					 short_press_value);
107 		if (ret) {
108 			dev_err(dev, "Failed to init pwron short press\n");
109 			return ret;
110 		}
111 	}
112 
113 	ret = of_property_read_u32(dev->of_node, "rohm,long-press-ms",
114 				   &long_press_ms);
115 	if (!ret) {
116 		long_press_value = min(15u, (long_press_ms + 500) / 1000);
117 		ret = regmap_update_bits(regmap, BD718XX_REG_PWRONCONFIG1,
118 					 BD718XX_PWRBTN_PRESS_DURATION_MASK,
119 					 long_press_value);
120 		if (ret) {
121 			dev_err(dev, "Failed to init pwron long press\n");
122 			return ret;
123 		}
124 	}
125 
126 	return 0;
127 }
128 
129 static int bd718xx_i2c_probe(struct i2c_client *i2c)
130 {
131 	struct regmap *regmap;
132 	struct regmap_irq_chip_data *irq_data;
133 	int ret;
134 	unsigned int chip_type;
135 	struct mfd_cell *mfd;
136 	int cells;
137 
138 	if (!i2c->irq) {
139 		dev_err(&i2c->dev, "No IRQ configured\n");
140 		return -EINVAL;
141 	}
142 	chip_type = (unsigned int)(uintptr_t)
143 		    of_device_get_match_data(&i2c->dev);
144 	switch (chip_type) {
145 	case ROHM_CHIP_TYPE_BD71837:
146 		mfd = bd71837_mfd_cells;
147 		cells = ARRAY_SIZE(bd71837_mfd_cells);
148 		break;
149 	case ROHM_CHIP_TYPE_BD71847:
150 		mfd = bd71847_mfd_cells;
151 		cells = ARRAY_SIZE(bd71847_mfd_cells);
152 		break;
153 	default:
154 		dev_err(&i2c->dev, "Unknown device type");
155 		return -EINVAL;
156 	}
157 
158 	regmap = devm_regmap_init_i2c(i2c, &bd718xx_regmap_config);
159 	if (IS_ERR(regmap))
160 		return dev_err_probe(&i2c->dev, PTR_ERR(regmap),
161 				     "regmap initialization failed\n");
162 
163 	ret = devm_regmap_add_irq_chip(&i2c->dev, regmap, i2c->irq,
164 				       IRQF_ONESHOT, 0, &bd718xx_irq_chip,
165 				       &irq_data);
166 	if (ret)
167 		return dev_err_probe(&i2c->dev, ret, "Failed to add irq_chip\n");
168 
169 	ret = bd718xx_init_press_duration(regmap, &i2c->dev);
170 	if (ret)
171 		return ret;
172 
173 	ret = regmap_irq_get_virq(irq_data, BD718XX_INT_PWRBTN_S);
174 
175 	if (ret < 0)
176 		return dev_err_probe(&i2c->dev, ret, "Failed to get the IRQ\n");
177 
178 	button.irq = ret;
179 
180 	ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_AUTO,
181 				   mfd, cells, NULL, 0,
182 				   regmap_irq_get_domain(irq_data));
183 	if (ret)
184 		dev_err_probe(&i2c->dev, ret, "Failed to create subdevices\n");
185 
186 	return ret;
187 }
188 
189 static const struct of_device_id bd718xx_of_match[] = {
190 	{
191 		.compatible = "rohm,bd71837",
192 		.data = (void *)ROHM_CHIP_TYPE_BD71837,
193 	},
194 	{
195 		.compatible = "rohm,bd71847",
196 		.data = (void *)ROHM_CHIP_TYPE_BD71847,
197 	},
198 	{
199 		.compatible = "rohm,bd71850",
200 		.data = (void *)ROHM_CHIP_TYPE_BD71847,
201 	},
202 	{ }
203 };
204 MODULE_DEVICE_TABLE(of, bd718xx_of_match);
205 
206 static struct i2c_driver bd718xx_i2c_driver = {
207 	.driver = {
208 		.name = "rohm-bd718x7",
209 		.of_match_table = bd718xx_of_match,
210 	},
211 	.probe = bd718xx_i2c_probe,
212 };
213 
214 static int __init bd718xx_i2c_init(void)
215 {
216 	return i2c_add_driver(&bd718xx_i2c_driver);
217 }
218 
219 /* Initialise early so consumer devices can complete system boot */
220 subsys_initcall(bd718xx_i2c_init);
221 
222 static void __exit bd718xx_i2c_exit(void)
223 {
224 	i2c_del_driver(&bd718xx_i2c_driver);
225 }
226 module_exit(bd718xx_i2c_exit);
227 
228 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
229 MODULE_DESCRIPTION("ROHM BD71837/BD71847 Power Management IC driver");
230 MODULE_LICENSE("GPL");
231