hi655x-pmic.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Device driver for MFD hi655x PMIC
  4. *
  5. * Copyright (c) 2016 HiSilicon Ltd.
  6. *
  7. * Authors:
  8. * Chen Feng <[email protected]>
  9. * Fei Wang <[email protected]>
  10. */
  11. #include <linux/io.h>
  12. #include <linux/interrupt.h>
  13. #include <linux/init.h>
  14. #include <linux/mfd/core.h>
  15. #include <linux/mfd/hi655x-pmic.h>
  16. #include <linux/module.h>
  17. #include <linux/gpio/consumer.h>
  18. #include <linux/of_platform.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. static const struct regmap_irq hi655x_irqs[] = {
  22. { .reg_offset = 0, .mask = OTMP_D1R_INT_MASK },
  23. { .reg_offset = 0, .mask = VSYS_2P5_R_INT_MASK },
  24. { .reg_offset = 0, .mask = VSYS_UV_D3R_INT_MASK },
  25. { .reg_offset = 0, .mask = VSYS_6P0_D200UR_INT_MASK },
  26. { .reg_offset = 0, .mask = PWRON_D4SR_INT_MASK },
  27. { .reg_offset = 0, .mask = PWRON_D20F_INT_MASK },
  28. { .reg_offset = 0, .mask = PWRON_D20R_INT_MASK },
  29. { .reg_offset = 0, .mask = RESERVE_INT_MASK },
  30. };
  31. static const struct regmap_irq_chip hi655x_irq_chip = {
  32. .name = "hi655x-pmic",
  33. .irqs = hi655x_irqs,
  34. .num_regs = 1,
  35. .num_irqs = ARRAY_SIZE(hi655x_irqs),
  36. .status_base = HI655X_IRQ_STAT_BASE,
  37. .ack_base = HI655X_IRQ_STAT_BASE,
  38. .mask_base = HI655X_IRQ_MASK_BASE,
  39. };
  40. static struct regmap_config hi655x_regmap_config = {
  41. .reg_bits = 32,
  42. .reg_stride = HI655X_STRIDE,
  43. .val_bits = 8,
  44. .max_register = HI655X_BUS_ADDR(0x400) - HI655X_STRIDE,
  45. };
  46. static const struct resource pwrkey_resources[] = {
  47. {
  48. .name = "down",
  49. .start = PWRON_D20R_INT,
  50. .end = PWRON_D20R_INT,
  51. .flags = IORESOURCE_IRQ,
  52. }, {
  53. .name = "up",
  54. .start = PWRON_D20F_INT,
  55. .end = PWRON_D20F_INT,
  56. .flags = IORESOURCE_IRQ,
  57. }, {
  58. .name = "hold 4s",
  59. .start = PWRON_D4SR_INT,
  60. .end = PWRON_D4SR_INT,
  61. .flags = IORESOURCE_IRQ,
  62. },
  63. };
  64. static const struct mfd_cell hi655x_pmic_devs[] = {
  65. {
  66. .name = "hi65xx-powerkey",
  67. .num_resources = ARRAY_SIZE(pwrkey_resources),
  68. .resources = &pwrkey_resources[0],
  69. },
  70. { .name = "hi655x-regulator", },
  71. { .name = "hi655x-clk", },
  72. };
  73. static void hi655x_local_irq_clear(struct regmap *map)
  74. {
  75. int i;
  76. regmap_write(map, HI655X_ANA_IRQM_BASE, HI655X_IRQ_CLR);
  77. for (i = 0; i < HI655X_IRQ_ARRAY; i++) {
  78. regmap_write(map, HI655X_IRQ_STAT_BASE + i * HI655X_STRIDE,
  79. HI655X_IRQ_CLR);
  80. }
  81. }
  82. static int hi655x_pmic_probe(struct platform_device *pdev)
  83. {
  84. int ret;
  85. struct hi655x_pmic *pmic;
  86. struct device *dev = &pdev->dev;
  87. void __iomem *base;
  88. pmic = devm_kzalloc(dev, sizeof(*pmic), GFP_KERNEL);
  89. if (!pmic)
  90. return -ENOMEM;
  91. pmic->dev = dev;
  92. pmic->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  93. base = devm_ioremap_resource(dev, pmic->res);
  94. if (IS_ERR(base))
  95. return PTR_ERR(base);
  96. pmic->regmap = devm_regmap_init_mmio_clk(dev, NULL, base,
  97. &hi655x_regmap_config);
  98. if (IS_ERR(pmic->regmap))
  99. return PTR_ERR(pmic->regmap);
  100. regmap_read(pmic->regmap, HI655X_BUS_ADDR(HI655X_VER_REG), &pmic->ver);
  101. if ((pmic->ver < PMU_VER_START) || (pmic->ver > PMU_VER_END)) {
  102. dev_warn(dev, "PMU version %d unsupported\n", pmic->ver);
  103. return -EINVAL;
  104. }
  105. hi655x_local_irq_clear(pmic->regmap);
  106. pmic->gpio = devm_gpiod_get_optional(dev, "pmic", GPIOD_IN);
  107. if (IS_ERR(pmic->gpio))
  108. return dev_err_probe(dev, PTR_ERR(pmic->gpio),
  109. "Failed to request hi655x pmic-gpio");
  110. ret = regmap_add_irq_chip(pmic->regmap, gpiod_to_irq(pmic->gpio),
  111. IRQF_TRIGGER_LOW | IRQF_NO_SUSPEND, 0,
  112. &hi655x_irq_chip, &pmic->irq_data);
  113. if (ret) {
  114. dev_err(dev, "Failed to obtain 'hi655x_pmic_irq' %d\n", ret);
  115. return ret;
  116. }
  117. platform_set_drvdata(pdev, pmic);
  118. ret = mfd_add_devices(dev, PLATFORM_DEVID_AUTO, hi655x_pmic_devs,
  119. ARRAY_SIZE(hi655x_pmic_devs), NULL, 0,
  120. regmap_irq_get_domain(pmic->irq_data));
  121. if (ret) {
  122. dev_err(dev, "Failed to register device %d\n", ret);
  123. regmap_del_irq_chip(gpiod_to_irq(pmic->gpio), pmic->irq_data);
  124. return ret;
  125. }
  126. return 0;
  127. }
  128. static int hi655x_pmic_remove(struct platform_device *pdev)
  129. {
  130. struct hi655x_pmic *pmic = platform_get_drvdata(pdev);
  131. regmap_del_irq_chip(gpiod_to_irq(pmic->gpio), pmic->irq_data);
  132. mfd_remove_devices(&pdev->dev);
  133. return 0;
  134. }
  135. static const struct of_device_id hi655x_pmic_match[] = {
  136. { .compatible = "hisilicon,hi655x-pmic", },
  137. {},
  138. };
  139. MODULE_DEVICE_TABLE(of, hi655x_pmic_match);
  140. static struct platform_driver hi655x_pmic_driver = {
  141. .driver = {
  142. .name = "hi655x-pmic",
  143. .of_match_table = of_match_ptr(hi655x_pmic_match),
  144. },
  145. .probe = hi655x_pmic_probe,
  146. .remove = hi655x_pmic_remove,
  147. };
  148. module_platform_driver(hi655x_pmic_driver);
  149. MODULE_AUTHOR("Chen Feng <[email protected]>");
  150. MODULE_DESCRIPTION("Hisilicon hi655x PMIC driver");
  151. MODULE_LICENSE("GPL v2");