0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <linux/io.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/init.h>
0015 #include <linux/mfd/core.h>
0016 #include <linux/mfd/hi655x-pmic.h>
0017 #include <linux/module.h>
0018 #include <linux/gpio/consumer.h>
0019 #include <linux/of_platform.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/regmap.h>
0022
0023 static const struct regmap_irq hi655x_irqs[] = {
0024 { .reg_offset = 0, .mask = OTMP_D1R_INT_MASK },
0025 { .reg_offset = 0, .mask = VSYS_2P5_R_INT_MASK },
0026 { .reg_offset = 0, .mask = VSYS_UV_D3R_INT_MASK },
0027 { .reg_offset = 0, .mask = VSYS_6P0_D200UR_INT_MASK },
0028 { .reg_offset = 0, .mask = PWRON_D4SR_INT_MASK },
0029 { .reg_offset = 0, .mask = PWRON_D20F_INT_MASK },
0030 { .reg_offset = 0, .mask = PWRON_D20R_INT_MASK },
0031 { .reg_offset = 0, .mask = RESERVE_INT_MASK },
0032 };
0033
0034 static const struct regmap_irq_chip hi655x_irq_chip = {
0035 .name = "hi655x-pmic",
0036 .irqs = hi655x_irqs,
0037 .num_regs = 1,
0038 .num_irqs = ARRAY_SIZE(hi655x_irqs),
0039 .status_base = HI655X_IRQ_STAT_BASE,
0040 .ack_base = HI655X_IRQ_STAT_BASE,
0041 .mask_base = HI655X_IRQ_MASK_BASE,
0042 };
0043
0044 static struct regmap_config hi655x_regmap_config = {
0045 .reg_bits = 32,
0046 .reg_stride = HI655X_STRIDE,
0047 .val_bits = 8,
0048 .max_register = HI655X_BUS_ADDR(0x400) - HI655X_STRIDE,
0049 };
0050
0051 static const struct resource pwrkey_resources[] = {
0052 {
0053 .name = "down",
0054 .start = PWRON_D20R_INT,
0055 .end = PWRON_D20R_INT,
0056 .flags = IORESOURCE_IRQ,
0057 }, {
0058 .name = "up",
0059 .start = PWRON_D20F_INT,
0060 .end = PWRON_D20F_INT,
0061 .flags = IORESOURCE_IRQ,
0062 }, {
0063 .name = "hold 4s",
0064 .start = PWRON_D4SR_INT,
0065 .end = PWRON_D4SR_INT,
0066 .flags = IORESOURCE_IRQ,
0067 },
0068 };
0069
0070 static const struct mfd_cell hi655x_pmic_devs[] = {
0071 {
0072 .name = "hi65xx-powerkey",
0073 .num_resources = ARRAY_SIZE(pwrkey_resources),
0074 .resources = &pwrkey_resources[0],
0075 },
0076 { .name = "hi655x-regulator", },
0077 { .name = "hi655x-clk", },
0078 };
0079
0080 static void hi655x_local_irq_clear(struct regmap *map)
0081 {
0082 int i;
0083
0084 regmap_write(map, HI655X_ANA_IRQM_BASE, HI655X_IRQ_CLR);
0085 for (i = 0; i < HI655X_IRQ_ARRAY; i++) {
0086 regmap_write(map, HI655X_IRQ_STAT_BASE + i * HI655X_STRIDE,
0087 HI655X_IRQ_CLR);
0088 }
0089 }
0090
0091 static int hi655x_pmic_probe(struct platform_device *pdev)
0092 {
0093 int ret;
0094 struct hi655x_pmic *pmic;
0095 struct device *dev = &pdev->dev;
0096 void __iomem *base;
0097
0098 pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL);
0099 if (!pmic)
0100 return -ENOMEM;
0101 pmic->dev = dev;
0102
0103 pmic->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
0104 base = devm_ioremap_resource(dev, pmic->res);
0105 if (IS_ERR(base))
0106 return PTR_ERR(base);
0107
0108 pmic->regmap = devm_regmap_init_mmio_clk(dev, NULL, base,
0109 &hi655x_regmap_config);
0110 if (IS_ERR(pmic->regmap))
0111 return PTR_ERR(pmic->regmap);
0112
0113 regmap_read(pmic->regmap, HI655X_BUS_ADDR(HI655X_VER_REG), &pmic->ver);
0114 if ((pmic->ver < PMU_VER_START) || (pmic->ver > PMU_VER_END)) {
0115 dev_warn(dev, "PMU version %d unsupported\n", pmic->ver);
0116 return -EINVAL;
0117 }
0118
0119 hi655x_local_irq_clear(pmic->regmap);
0120
0121 pmic->gpio = devm_gpiod_get_optional(dev, "pmic", GPIOD_IN);
0122 if (IS_ERR(pmic->gpio))
0123 return dev_err_probe(dev, PTR_ERR(pmic->gpio),
0124 "Failed to request hi655x pmic-gpio");
0125
0126 ret = regmap_add_irq_chip(pmic->regmap, gpiod_to_irq(pmic->gpio),
0127 IRQF_TRIGGER_LOW | IRQF_NO_SUSPEND, 0,
0128 &hi655x_irq_chip, &pmic->irq_data);
0129 if (ret) {
0130 dev_err(dev, "Failed to obtain 'hi655x_pmic_irq' %d\n", ret);
0131 return ret;
0132 }
0133
0134 platform_set_drvdata(pdev, pmic);
0135
0136 ret = mfd_add_devices(dev, PLATFORM_DEVID_AUTO, hi655x_pmic_devs,
0137 ARRAY_SIZE(hi655x_pmic_devs), NULL, 0,
0138 regmap_irq_get_domain(pmic->irq_data));
0139 if (ret) {
0140 dev_err(dev, "Failed to register device %d\n", ret);
0141 regmap_del_irq_chip(gpiod_to_irq(pmic->gpio), pmic->irq_data);
0142 return ret;
0143 }
0144
0145 return 0;
0146 }
0147
0148 static int hi655x_pmic_remove(struct platform_device *pdev)
0149 {
0150 struct hi655x_pmic *pmic = platform_get_drvdata(pdev);
0151
0152 regmap_del_irq_chip(gpiod_to_irq(pmic->gpio), pmic->irq_data);
0153 mfd_remove_devices(&pdev->dev);
0154 return 0;
0155 }
0156
0157 static const struct of_device_id hi655x_pmic_match[] = {
0158 { .compatible = "hisilicon,hi655x-pmic", },
0159 {},
0160 };
0161 MODULE_DEVICE_TABLE(of, hi655x_pmic_match);
0162
0163 static struct platform_driver hi655x_pmic_driver = {
0164 .driver = {
0165 .name = "hi655x-pmic",
0166 .of_match_table = of_match_ptr(hi655x_pmic_match),
0167 },
0168 .probe = hi655x_pmic_probe,
0169 .remove = hi655x_pmic_remove,
0170 };
0171 module_platform_driver(hi655x_pmic_driver);
0172
0173 MODULE_AUTHOR("Chen Feng <puck.chen@hisilicon.com>");
0174 MODULE_DESCRIPTION("Hisilicon hi655x PMIC driver");
0175 MODULE_LICENSE("GPL v2");