blob: c32c1b6c98fa3463db2226547f570f43edca61ec [file] [log] [blame]
Matti Vaittinen30107fa2018-08-03 14:08:14 +03001// SPDX-License-Identifier: GPL-2.0-or-later
2//
3// Copyright (C) 2018 ROHM Semiconductors
4//
Matti Vaittinendd2be632018-09-14 11:32:26 +03005// ROHM BD71837MWV and BD71847MWV PMIC driver
Matti Vaittinen30107fa2018-08-03 14:08:14 +03006//
Matti Vaittinendd2be632018-09-14 11:32:26 +03007// Datasheet for BD71837MWV available from
Matti Vaittinen30107fa2018-08-03 14:08:14 +03008// https://www.rohm.com/datasheet/BD71837MWV/bd71837mwv-e
9
Matti Vaittinen494edd22018-09-14 11:27:46 +030010#include <linux/gpio_keys.h>
Matti Vaittinen30107fa2018-08-03 14:08:14 +030011#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>
Matti Vaittinen494edd22018-09-14 11:27:46 +030017#include <linux/of_device.h>
Matti Vaittinen30107fa2018-08-03 14:08:14 +030018#include <linux/regmap.h>
Matti Vaittinen494edd22018-09-14 11:27:46 +030019#include <linux/types.h>
Matti Vaittinen30107fa2018-08-03 14:08:14 +030020
21static struct gpio_keys_button button = {
22 .code = KEY_POWER,
23 .gpio = -1,
24 .type = EV_KEY,
25};
26
27static struct gpio_keys_platform_data bd718xx_powerkey_data = {
28 .buttons = &button,
29 .nbuttons = 1,
30 .name = "bd718xx-pwrkey",
31};
32
Matti Vaittinen1b1c26b2020-01-20 15:42:38 +020033static struct mfd_cell bd71837_mfd_cells[] = {
Matti Vaittinen30107fa2018-08-03 14:08:14 +030034 {
35 .name = "gpio-keys",
36 .platform_data = &bd718xx_powerkey_data,
37 .pdata_size = sizeof(bd718xx_powerkey_data),
38 },
Matti Vaittinen1b1c26b2020-01-20 15:42:38 +020039 { .name = "bd71837-clk", },
40 { .name = "bd71837-pmic", },
41};
42
43static 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", },
Matti Vaittinen30107fa2018-08-03 14:08:14 +030051};
52
Matti Vaittinendd2be632018-09-14 11:32:26 +030053static 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),
Matti Vaittinen30107fa2018-08-03 14:08:14 +030061};
62
Matti Vaittinendd2be632018-09-14 11:32:26 +030063static struct regmap_irq_chip bd718xx_irq_chip = {
64 .name = "bd718xx-irq",
65 .irqs = bd718xx_irqs,
66 .num_irqs = ARRAY_SIZE(bd718xx_irqs),
Matti Vaittinen30107fa2018-08-03 14:08:14 +030067 .num_regs = 1,
68 .irq_reg_stride = 1,
Matti Vaittinen494edd22018-09-14 11:27:46 +030069 .status_base = BD718XX_REG_IRQ,
70 .mask_base = BD718XX_REG_MIRQ,
71 .ack_base = BD718XX_REG_IRQ,
Matti Vaittinen30107fa2018-08-03 14:08:14 +030072 .init_ack_masked = true,
73 .mask_invert = false,
74};
75
76static const struct regmap_range pmic_status_range = {
Matti Vaittinen494edd22018-09-14 11:27:46 +030077 .range_min = BD718XX_REG_IRQ,
78 .range_max = BD718XX_REG_POW_STATE,
Matti Vaittinen30107fa2018-08-03 14:08:14 +030079};
80
81static const struct regmap_access_table volatile_regs = {
82 .yes_ranges = &pmic_status_range,
83 .n_yes_ranges = 1,
84};
85
Matti Vaittinendd2be632018-09-14 11:32:26 +030086static const struct regmap_config bd718xx_regmap_config = {
Matti Vaittinen30107fa2018-08-03 14:08:14 +030087 .reg_bits = 8,
88 .val_bits = 8,
89 .volatile_table = &volatile_regs,
Matti Vaittinen494edd22018-09-14 11:27:46 +030090 .max_register = BD718XX_MAX_REGISTER - 1,
Matti Vaittinen30107fa2018-08-03 14:08:14 +030091 .cache_type = REGCACHE_RBTREE,
92};
93
Leonard Cresteze25547f892019-05-21 20:41:14 +000094static int bd718xx_init_press_duration(struct bd718xx *bd718xx)
95{
96 struct device* dev = bd718xx->chip.dev;
97 u32 short_press_ms, long_press_ms;
98 u32 short_press_value, long_press_value;
99 int ret;
100
101 ret = of_property_read_u32(dev->of_node, "rohm,short-press-ms",
102 &short_press_ms);
103 if (!ret) {
104 short_press_value = min(15u, (short_press_ms + 250) / 500);
105 ret = regmap_update_bits(bd718xx->chip.regmap,
106 BD718XX_REG_PWRONCONFIG0,
107 BD718XX_PWRBTN_PRESS_DURATION_MASK,
108 short_press_value);
109 if (ret) {
110 dev_err(dev, "Failed to init pwron short press\n");
111 return ret;
112 }
113 }
114
115 ret = of_property_read_u32(dev->of_node, "rohm,long-press-ms",
116 &long_press_ms);
117 if (!ret) {
118 long_press_value = min(15u, (long_press_ms + 500) / 1000);
119 ret = regmap_update_bits(bd718xx->chip.regmap,
120 BD718XX_REG_PWRONCONFIG1,
121 BD718XX_PWRBTN_PRESS_DURATION_MASK,
122 long_press_value);
123 if (ret) {
124 dev_err(dev, "Failed to init pwron long press\n");
125 return ret;
126 }
127 }
128
129 return 0;
130}
131
Matti Vaittinendd2be632018-09-14 11:32:26 +0300132static int bd718xx_i2c_probe(struct i2c_client *i2c,
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300133 const struct i2c_device_id *id)
134{
Matti Vaittinendd2be632018-09-14 11:32:26 +0300135 struct bd718xx *bd718xx;
Matti Vaittinen494edd22018-09-14 11:27:46 +0300136 int ret;
Matti Vaittinen1b1c26b2020-01-20 15:42:38 +0200137 unsigned int chip_type;
138 struct mfd_cell *mfd;
139 int cells;
Matti Vaittinen494edd22018-09-14 11:27:46 +0300140
141 if (!i2c->irq) {
142 dev_err(&i2c->dev, "No IRQ configured\n");
143 return -EINVAL;
144 }
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300145
Matti Vaittinendd2be632018-09-14 11:32:26 +0300146 bd718xx = devm_kzalloc(&i2c->dev, sizeof(struct bd718xx), GFP_KERNEL);
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300147
Matti Vaittinendd2be632018-09-14 11:32:26 +0300148 if (!bd718xx)
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300149 return -ENOMEM;
150
Matti Vaittinendd2be632018-09-14 11:32:26 +0300151 bd718xx->chip_irq = i2c->irq;
Matti Vaittinen1b1c26b2020-01-20 15:42:38 +0200152 chip_type = (unsigned int)(uintptr_t)
153 of_device_get_match_data(&i2c->dev);
154 switch (chip_type) {
155 case ROHM_CHIP_TYPE_BD71837:
156 mfd = bd71837_mfd_cells;
157 cells = ARRAY_SIZE(bd71837_mfd_cells);
158 break;
159 case ROHM_CHIP_TYPE_BD71847:
160 mfd = bd71847_mfd_cells;
161 cells = ARRAY_SIZE(bd71847_mfd_cells);
162 break;
163 default:
164 dev_err(&i2c->dev, "Unknown device type");
165 return -EINVAL;
166 }
Matti Vaittinen2a6a7aa2019-06-03 10:24:32 +0300167 bd718xx->chip.dev = &i2c->dev;
Matti Vaittinendd2be632018-09-14 11:32:26 +0300168 dev_set_drvdata(&i2c->dev, bd718xx);
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300169
Matti Vaittinen2a6a7aa2019-06-03 10:24:32 +0300170 bd718xx->chip.regmap = devm_regmap_init_i2c(i2c,
171 &bd718xx_regmap_config);
172 if (IS_ERR(bd718xx->chip.regmap)) {
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300173 dev_err(&i2c->dev, "regmap initialization failed\n");
Matti Vaittinen2a6a7aa2019-06-03 10:24:32 +0300174 return PTR_ERR(bd718xx->chip.regmap);
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300175 }
176
Matti Vaittinen2a6a7aa2019-06-03 10:24:32 +0300177 ret = devm_regmap_add_irq_chip(&i2c->dev, bd718xx->chip.regmap,
Matti Vaittinendd2be632018-09-14 11:32:26 +0300178 bd718xx->chip_irq, IRQF_ONESHOT, 0,
179 &bd718xx_irq_chip, &bd718xx->irq_data);
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300180 if (ret) {
181 dev_err(&i2c->dev, "Failed to add irq_chip\n");
182 return ret;
183 }
184
Leonard Cresteze25547f892019-05-21 20:41:14 +0000185 ret = bd718xx_init_press_duration(bd718xx);
186 if (ret)
187 return ret;
188
Matti Vaittinendd2be632018-09-14 11:32:26 +0300189 ret = regmap_irq_get_virq(bd718xx->irq_data, BD718XX_INT_PWRBTN_S);
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300190
191 if (ret < 0) {
192 dev_err(&i2c->dev, "Failed to get the IRQ\n");
193 return ret;
194 }
195
196 button.irq = ret;
197
Matti Vaittinen2a6a7aa2019-06-03 10:24:32 +0300198 ret = devm_mfd_add_devices(bd718xx->chip.dev, PLATFORM_DEVID_AUTO,
Matti Vaittinen1b1c26b2020-01-20 15:42:38 +0200199 mfd, cells, NULL, 0,
Matti Vaittinendd2be632018-09-14 11:32:26 +0300200 regmap_irq_get_domain(bd718xx->irq_data));
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300201 if (ret)
202 dev_err(&i2c->dev, "Failed to create subdevices\n");
203
204 return ret;
205}
206
Matti Vaittinendd2be632018-09-14 11:32:26 +0300207static const struct of_device_id bd718xx_of_match[] = {
Matti Vaittinen494edd22018-09-14 11:27:46 +0300208 {
209 .compatible = "rohm,bd71837",
Matti Vaittinen2a6a7aa2019-06-03 10:24:32 +0300210 .data = (void *)ROHM_CHIP_TYPE_BD71837,
Matti Vaittinen494edd22018-09-14 11:27:46 +0300211 },
212 {
213 .compatible = "rohm,bd71847",
Matti Vaittinen2a6a7aa2019-06-03 10:24:32 +0300214 .data = (void *)ROHM_CHIP_TYPE_BD71847,
Matti Vaittinen494edd22018-09-14 11:27:46 +0300215 },
Matti Vaittinen8c352e62020-01-20 15:43:01 +0200216 {
217 .compatible = "rohm,bd71850",
218 .data = (void *)ROHM_CHIP_TYPE_BD71847,
219 },
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300220 { }
221};
Matti Vaittinendd2be632018-09-14 11:32:26 +0300222MODULE_DEVICE_TABLE(of, bd718xx_of_match);
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300223
Matti Vaittinendd2be632018-09-14 11:32:26 +0300224static struct i2c_driver bd718xx_i2c_driver = {
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300225 .driver = {
226 .name = "rohm-bd718x7",
Matti Vaittinendd2be632018-09-14 11:32:26 +0300227 .of_match_table = bd718xx_of_match,
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300228 },
Matti Vaittinendd2be632018-09-14 11:32:26 +0300229 .probe = bd718xx_i2c_probe,
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300230};
231
Matti Vaittinendd2be632018-09-14 11:32:26 +0300232static int __init bd718xx_i2c_init(void)
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300233{
Matti Vaittinendd2be632018-09-14 11:32:26 +0300234 return i2c_add_driver(&bd718xx_i2c_driver);
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300235}
236
237/* Initialise early so consumer devices can complete system boot */
Matti Vaittinendd2be632018-09-14 11:32:26 +0300238subsys_initcall(bd718xx_i2c_init);
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300239
Matti Vaittinendd2be632018-09-14 11:32:26 +0300240static void __exit bd718xx_i2c_exit(void)
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300241{
Matti Vaittinendd2be632018-09-14 11:32:26 +0300242 i2c_del_driver(&bd718xx_i2c_driver);
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300243}
Matti Vaittinendd2be632018-09-14 11:32:26 +0300244module_exit(bd718xx_i2c_exit);
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300245
246MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
Matti Vaittinendd2be632018-09-14 11:32:26 +0300247MODULE_DESCRIPTION("ROHM BD71837/BD71847 Power Management IC driver");
Matti Vaittinen30107fa2018-08-03 14:08:14 +0300248MODULE_LICENSE("GPL");