0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/gpio_keys.h>
0011 #include <linux/i2c.h>
0012 #include <linux/input.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/mfd/rohm-bd718x7.h>
0015 #include <linux/mfd/core.h>
0016 #include <linux/module.h>
0017 #include <linux/of_device.h>
0018 #include <linux/regmap.h>
0019 #include <linux/types.h>
0020
0021 static struct gpio_keys_button button = {
0022 .code = KEY_POWER,
0023 .gpio = -1,
0024 .type = EV_KEY,
0025 };
0026
0027 static struct gpio_keys_platform_data bd718xx_powerkey_data = {
0028 .buttons = &button,
0029 .nbuttons = 1,
0030 .name = "bd718xx-pwrkey",
0031 };
0032
0033 static struct mfd_cell bd71837_mfd_cells[] = {
0034 {
0035 .name = "gpio-keys",
0036 .platform_data = &bd718xx_powerkey_data,
0037 .pdata_size = sizeof(bd718xx_powerkey_data),
0038 },
0039 { .name = "bd71837-clk", },
0040 { .name = "bd71837-pmic", },
0041 };
0042
0043 static struct mfd_cell bd71847_mfd_cells[] = {
0044 {
0045 .name = "gpio-keys",
0046 .platform_data = &bd718xx_powerkey_data,
0047 .pdata_size = sizeof(bd718xx_powerkey_data),
0048 },
0049 { .name = "bd71847-clk", },
0050 { .name = "bd71847-pmic", },
0051 };
0052
0053 static const struct regmap_irq bd718xx_irqs[] = {
0054 REGMAP_IRQ_REG(BD718XX_INT_SWRST, 0, BD718XX_INT_SWRST_MASK),
0055 REGMAP_IRQ_REG(BD718XX_INT_PWRBTN_S, 0, BD718XX_INT_PWRBTN_S_MASK),
0056 REGMAP_IRQ_REG(BD718XX_INT_PWRBTN_L, 0, BD718XX_INT_PWRBTN_L_MASK),
0057 REGMAP_IRQ_REG(BD718XX_INT_PWRBTN, 0, BD718XX_INT_PWRBTN_MASK),
0058 REGMAP_IRQ_REG(BD718XX_INT_WDOG, 0, BD718XX_INT_WDOG_MASK),
0059 REGMAP_IRQ_REG(BD718XX_INT_ON_REQ, 0, BD718XX_INT_ON_REQ_MASK),
0060 REGMAP_IRQ_REG(BD718XX_INT_STBY_REQ, 0, BD718XX_INT_STBY_REQ_MASK),
0061 };
0062
0063 static struct regmap_irq_chip bd718xx_irq_chip = {
0064 .name = "bd718xx-irq",
0065 .irqs = bd718xx_irqs,
0066 .num_irqs = ARRAY_SIZE(bd718xx_irqs),
0067 .num_regs = 1,
0068 .irq_reg_stride = 1,
0069 .status_base = BD718XX_REG_IRQ,
0070 .mask_base = BD718XX_REG_MIRQ,
0071 .ack_base = BD718XX_REG_IRQ,
0072 .init_ack_masked = true,
0073 .mask_invert = false,
0074 };
0075
0076 static const struct regmap_range pmic_status_range = {
0077 .range_min = BD718XX_REG_IRQ,
0078 .range_max = BD718XX_REG_POW_STATE,
0079 };
0080
0081 static const struct regmap_access_table volatile_regs = {
0082 .yes_ranges = &pmic_status_range,
0083 .n_yes_ranges = 1,
0084 };
0085
0086 static const struct regmap_config bd718xx_regmap_config = {
0087 .reg_bits = 8,
0088 .val_bits = 8,
0089 .volatile_table = &volatile_regs,
0090 .max_register = BD718XX_MAX_REGISTER - 1,
0091 .cache_type = REGCACHE_RBTREE,
0092 };
0093
0094 static int bd718xx_init_press_duration(struct regmap *regmap,
0095 struct device *dev)
0096 {
0097 u32 short_press_ms, long_press_ms;
0098 u32 short_press_value, long_press_value;
0099 int ret;
0100
0101 ret = of_property_read_u32(dev->of_node, "rohm,short-press-ms",
0102 &short_press_ms);
0103 if (!ret) {
0104 short_press_value = min(15u, (short_press_ms + 250) / 500);
0105 ret = regmap_update_bits(regmap, BD718XX_REG_PWRONCONFIG0,
0106 BD718XX_PWRBTN_PRESS_DURATION_MASK,
0107 short_press_value);
0108 if (ret) {
0109 dev_err(dev, "Failed to init pwron short press\n");
0110 return ret;
0111 }
0112 }
0113
0114 ret = of_property_read_u32(dev->of_node, "rohm,long-press-ms",
0115 &long_press_ms);
0116 if (!ret) {
0117 long_press_value = min(15u, (long_press_ms + 500) / 1000);
0118 ret = regmap_update_bits(regmap, BD718XX_REG_PWRONCONFIG1,
0119 BD718XX_PWRBTN_PRESS_DURATION_MASK,
0120 long_press_value);
0121 if (ret) {
0122 dev_err(dev, "Failed to init pwron long press\n");
0123 return ret;
0124 }
0125 }
0126
0127 return 0;
0128 }
0129
0130 static int bd718xx_i2c_probe(struct i2c_client *i2c,
0131 const struct i2c_device_id *id)
0132 {
0133 struct regmap *regmap;
0134 struct regmap_irq_chip_data *irq_data;
0135 int ret;
0136 unsigned int chip_type;
0137 struct mfd_cell *mfd;
0138 int cells;
0139
0140 if (!i2c->irq) {
0141 dev_err(&i2c->dev, "No IRQ configured\n");
0142 return -EINVAL;
0143 }
0144 chip_type = (unsigned int)(uintptr_t)
0145 of_device_get_match_data(&i2c->dev);
0146 switch (chip_type) {
0147 case ROHM_CHIP_TYPE_BD71837:
0148 mfd = bd71837_mfd_cells;
0149 cells = ARRAY_SIZE(bd71837_mfd_cells);
0150 break;
0151 case ROHM_CHIP_TYPE_BD71847:
0152 mfd = bd71847_mfd_cells;
0153 cells = ARRAY_SIZE(bd71847_mfd_cells);
0154 break;
0155 default:
0156 dev_err(&i2c->dev, "Unknown device type");
0157 return -EINVAL;
0158 }
0159
0160 regmap = devm_regmap_init_i2c(i2c, &bd718xx_regmap_config);
0161 if (IS_ERR(regmap)) {
0162 dev_err(&i2c->dev, "regmap initialization failed\n");
0163 return PTR_ERR(regmap);
0164 }
0165
0166 ret = devm_regmap_add_irq_chip(&i2c->dev, regmap, i2c->irq,
0167 IRQF_ONESHOT, 0, &bd718xx_irq_chip,
0168 &irq_data);
0169 if (ret) {
0170 dev_err(&i2c->dev, "Failed to add irq_chip\n");
0171 return ret;
0172 }
0173
0174 ret = bd718xx_init_press_duration(regmap, &i2c->dev);
0175 if (ret)
0176 return ret;
0177
0178 ret = regmap_irq_get_virq(irq_data, BD718XX_INT_PWRBTN_S);
0179
0180 if (ret < 0) {
0181 dev_err(&i2c->dev, "Failed to get the IRQ\n");
0182 return ret;
0183 }
0184
0185 button.irq = ret;
0186
0187 ret = devm_mfd_add_devices(&i2c->dev, PLATFORM_DEVID_AUTO,
0188 mfd, cells, NULL, 0,
0189 regmap_irq_get_domain(irq_data));
0190 if (ret)
0191 dev_err(&i2c->dev, "Failed to create subdevices\n");
0192
0193 return ret;
0194 }
0195
0196 static const struct of_device_id bd718xx_of_match[] = {
0197 {
0198 .compatible = "rohm,bd71837",
0199 .data = (void *)ROHM_CHIP_TYPE_BD71837,
0200 },
0201 {
0202 .compatible = "rohm,bd71847",
0203 .data = (void *)ROHM_CHIP_TYPE_BD71847,
0204 },
0205 {
0206 .compatible = "rohm,bd71850",
0207 .data = (void *)ROHM_CHIP_TYPE_BD71847,
0208 },
0209 { }
0210 };
0211 MODULE_DEVICE_TABLE(of, bd718xx_of_match);
0212
0213 static struct i2c_driver bd718xx_i2c_driver = {
0214 .driver = {
0215 .name = "rohm-bd718x7",
0216 .of_match_table = bd718xx_of_match,
0217 },
0218 .probe = bd718xx_i2c_probe,
0219 };
0220
0221 static int __init bd718xx_i2c_init(void)
0222 {
0223 return i2c_add_driver(&bd718xx_i2c_driver);
0224 }
0225
0226
0227 subsys_initcall(bd718xx_i2c_init);
0228
0229 static void __exit bd718xx_i2c_exit(void)
0230 {
0231 i2c_del_driver(&bd718xx_i2c_driver);
0232 }
0233 module_exit(bd718xx_i2c_exit);
0234
0235 MODULE_AUTHOR("Matti Vaittinen <matti.vaittinen@fi.rohmeurope.com>");
0236 MODULE_DESCRIPTION("ROHM BD71837/BD71847 Power Management IC driver");
0237 MODULE_LICENSE("GPL");