Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | // |
| 3 | // Copyright (C) 2018 ROHM Semiconductors |
| 4 | // |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 5 | // ROHM BD71837MWV and BD71847MWV PMIC driver |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 6 | // |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 7 | // Datasheet for BD71837MWV available from |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 8 | // https://www.rohm.com/datasheet/BD71837MWV/bd71837mwv-e |
| 9 | |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 10 | #include <linux/gpio_keys.h> |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 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> |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 17 | #include <linux/of_device.h> |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 18 | #include <linux/regmap.h> |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 19 | #include <linux/types.h> |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 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 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 33 | static struct mfd_cell bd718xx_mfd_cells[] = { |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 34 | { |
| 35 | .name = "gpio-keys", |
| 36 | .platform_data = &bd718xx_powerkey_data, |
| 37 | .pdata_size = sizeof(bd718xx_powerkey_data), |
| 38 | }, |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 39 | { .name = "bd718xx-clk", }, |
| 40 | { .name = "bd718xx-pmic", }, |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 41 | }; |
| 42 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 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), |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 51 | }; |
| 52 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 53 | static struct regmap_irq_chip bd718xx_irq_chip = { |
| 54 | .name = "bd718xx-irq", |
| 55 | .irqs = bd718xx_irqs, |
| 56 | .num_irqs = ARRAY_SIZE(bd718xx_irqs), |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 57 | .num_regs = 1, |
| 58 | .irq_reg_stride = 1, |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 59 | .status_base = BD718XX_REG_IRQ, |
| 60 | .mask_base = BD718XX_REG_MIRQ, |
| 61 | .ack_base = BD718XX_REG_IRQ, |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 62 | .init_ack_masked = true, |
| 63 | .mask_invert = false, |
| 64 | }; |
| 65 | |
| 66 | static const struct regmap_range pmic_status_range = { |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 67 | .range_min = BD718XX_REG_IRQ, |
| 68 | .range_max = BD718XX_REG_POW_STATE, |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static const struct regmap_access_table volatile_regs = { |
| 72 | .yes_ranges = &pmic_status_range, |
| 73 | .n_yes_ranges = 1, |
| 74 | }; |
| 75 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 76 | static const struct regmap_config bd718xx_regmap_config = { |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 77 | .reg_bits = 8, |
| 78 | .val_bits = 8, |
| 79 | .volatile_table = &volatile_regs, |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 80 | .max_register = BD718XX_MAX_REGISTER - 1, |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 81 | .cache_type = REGCACHE_RBTREE, |
| 82 | }; |
| 83 | |
Leonard Crestez | e25547f89 | 2019-05-21 20:41:14 +0000 | [diff] [blame] | 84 | static int bd718xx_init_press_duration(struct bd718xx *bd718xx) |
| 85 | { |
| 86 | struct device* dev = bd718xx->chip.dev; |
| 87 | u32 short_press_ms, long_press_ms; |
| 88 | u32 short_press_value, long_press_value; |
| 89 | int ret; |
| 90 | |
| 91 | ret = of_property_read_u32(dev->of_node, "rohm,short-press-ms", |
| 92 | &short_press_ms); |
| 93 | if (!ret) { |
| 94 | short_press_value = min(15u, (short_press_ms + 250) / 500); |
| 95 | ret = regmap_update_bits(bd718xx->chip.regmap, |
| 96 | BD718XX_REG_PWRONCONFIG0, |
| 97 | BD718XX_PWRBTN_PRESS_DURATION_MASK, |
| 98 | short_press_value); |
| 99 | if (ret) { |
| 100 | dev_err(dev, "Failed to init pwron short press\n"); |
| 101 | return ret; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | ret = of_property_read_u32(dev->of_node, "rohm,long-press-ms", |
| 106 | &long_press_ms); |
| 107 | if (!ret) { |
| 108 | long_press_value = min(15u, (long_press_ms + 500) / 1000); |
| 109 | ret = regmap_update_bits(bd718xx->chip.regmap, |
| 110 | BD718XX_REG_PWRONCONFIG1, |
| 111 | BD718XX_PWRBTN_PRESS_DURATION_MASK, |
| 112 | long_press_value); |
| 113 | if (ret) { |
| 114 | dev_err(dev, "Failed to init pwron long press\n"); |
| 115 | return ret; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 122 | static int bd718xx_i2c_probe(struct i2c_client *i2c, |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 123 | const struct i2c_device_id *id) |
| 124 | { |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 125 | struct bd718xx *bd718xx; |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 126 | int ret; |
| 127 | |
| 128 | if (!i2c->irq) { |
| 129 | dev_err(&i2c->dev, "No IRQ configured\n"); |
| 130 | return -EINVAL; |
| 131 | } |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 132 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 133 | bd718xx = devm_kzalloc(&i2c->dev, sizeof(struct bd718xx), GFP_KERNEL); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 134 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 135 | if (!bd718xx) |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 136 | return -ENOMEM; |
| 137 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 138 | bd718xx->chip_irq = i2c->irq; |
Matti Vaittinen | 2a6a7aa | 2019-06-03 10:24:32 +0300 | [diff] [blame] | 139 | bd718xx->chip.chip_type = (unsigned int)(uintptr_t) |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 140 | of_device_get_match_data(&i2c->dev); |
Matti Vaittinen | 2a6a7aa | 2019-06-03 10:24:32 +0300 | [diff] [blame] | 141 | bd718xx->chip.dev = &i2c->dev; |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 142 | dev_set_drvdata(&i2c->dev, bd718xx); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 143 | |
Matti Vaittinen | 2a6a7aa | 2019-06-03 10:24:32 +0300 | [diff] [blame] | 144 | bd718xx->chip.regmap = devm_regmap_init_i2c(i2c, |
| 145 | &bd718xx_regmap_config); |
| 146 | if (IS_ERR(bd718xx->chip.regmap)) { |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 147 | dev_err(&i2c->dev, "regmap initialization failed\n"); |
Matti Vaittinen | 2a6a7aa | 2019-06-03 10:24:32 +0300 | [diff] [blame] | 148 | return PTR_ERR(bd718xx->chip.regmap); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 149 | } |
| 150 | |
Matti Vaittinen | 2a6a7aa | 2019-06-03 10:24:32 +0300 | [diff] [blame] | 151 | ret = devm_regmap_add_irq_chip(&i2c->dev, bd718xx->chip.regmap, |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 152 | bd718xx->chip_irq, IRQF_ONESHOT, 0, |
| 153 | &bd718xx_irq_chip, &bd718xx->irq_data); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 154 | if (ret) { |
| 155 | dev_err(&i2c->dev, "Failed to add irq_chip\n"); |
| 156 | return ret; |
| 157 | } |
| 158 | |
Leonard Crestez | e25547f89 | 2019-05-21 20:41:14 +0000 | [diff] [blame] | 159 | ret = bd718xx_init_press_duration(bd718xx); |
| 160 | if (ret) |
| 161 | return ret; |
| 162 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 163 | ret = regmap_irq_get_virq(bd718xx->irq_data, BD718XX_INT_PWRBTN_S); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 164 | |
| 165 | if (ret < 0) { |
| 166 | dev_err(&i2c->dev, "Failed to get the IRQ\n"); |
| 167 | return ret; |
| 168 | } |
| 169 | |
| 170 | button.irq = ret; |
| 171 | |
Matti Vaittinen | 2a6a7aa | 2019-06-03 10:24:32 +0300 | [diff] [blame] | 172 | ret = devm_mfd_add_devices(bd718xx->chip.dev, PLATFORM_DEVID_AUTO, |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 173 | bd718xx_mfd_cells, |
| 174 | ARRAY_SIZE(bd718xx_mfd_cells), NULL, 0, |
| 175 | regmap_irq_get_domain(bd718xx->irq_data)); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 176 | if (ret) |
| 177 | dev_err(&i2c->dev, "Failed to create subdevices\n"); |
| 178 | |
| 179 | return ret; |
| 180 | } |
| 181 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 182 | static const struct of_device_id bd718xx_of_match[] = { |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 183 | { |
| 184 | .compatible = "rohm,bd71837", |
Matti Vaittinen | 2a6a7aa | 2019-06-03 10:24:32 +0300 | [diff] [blame] | 185 | .data = (void *)ROHM_CHIP_TYPE_BD71837, |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 186 | }, |
| 187 | { |
| 188 | .compatible = "rohm,bd71847", |
Matti Vaittinen | 2a6a7aa | 2019-06-03 10:24:32 +0300 | [diff] [blame] | 189 | .data = (void *)ROHM_CHIP_TYPE_BD71847, |
Matti Vaittinen | 494edd2 | 2018-09-14 11:27:46 +0300 | [diff] [blame] | 190 | }, |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 191 | { } |
| 192 | }; |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 193 | MODULE_DEVICE_TABLE(of, bd718xx_of_match); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 194 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 195 | static struct i2c_driver bd718xx_i2c_driver = { |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 196 | .driver = { |
| 197 | .name = "rohm-bd718x7", |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 198 | .of_match_table = bd718xx_of_match, |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 199 | }, |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 200 | .probe = bd718xx_i2c_probe, |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 201 | }; |
| 202 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 203 | static int __init bd718xx_i2c_init(void) |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 204 | { |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 205 | return i2c_add_driver(&bd718xx_i2c_driver); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /* Initialise early so consumer devices can complete system boot */ |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 209 | subsys_initcall(bd718xx_i2c_init); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 210 | |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 211 | static void __exit bd718xx_i2c_exit(void) |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 212 | { |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 213 | i2c_del_driver(&bd718xx_i2c_driver); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 214 | } |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 215 | module_exit(bd718xx_i2c_exit); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 216 | |
| 217 | MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>"); |
Matti Vaittinen | dd2be63 | 2018-09-14 11:32:26 +0300 | [diff] [blame] | 218 | MODULE_DESCRIPTION("ROHM BD71837/BD71847 Power Management IC driver"); |
Matti Vaittinen | 30107fa | 2018-08-03 14:08:14 +0300 | [diff] [blame] | 219 | MODULE_LICENSE("GPL"); |