pmic_pfuze100.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2018-2022 Denx Software Engineering GmbH
  4. * Heiko Schocher <hs@denx.de>
  5. * Philip Oberfichtner <pro@denx.de>
  6. *
  7. * A bootcount driver using the registers MEMA - MEMD on the PFUZE100.
  8. * This works only, if the PMIC is not connected to a battery.
  9. */
  10. #include <common.h>
  11. #include <bootcount.h>
  12. #include <dm.h>
  13. #include <power/pmic.h>
  14. #include <power/pfuze100_pmic.h>
  15. DECLARE_GLOBAL_DATA_PTR;
  16. #define PFUZE_BC_MAGIC 0xdead
  17. struct bootcount_pmic_priv {
  18. struct udevice *pmic;
  19. };
  20. static int pfuze100_get_magic(struct udevice *dev, u32 *magic)
  21. {
  22. int ret;
  23. ret = pmic_reg_read(dev, PFUZE100_MEMA);
  24. if (ret < 0)
  25. return ret;
  26. *magic = ret;
  27. ret = pmic_reg_read(dev, PFUZE100_MEMB);
  28. if (ret < 0)
  29. return ret;
  30. *magic += ret << 8;
  31. return 0;
  32. }
  33. static int pfuze100_set_magic(struct udevice *dev)
  34. {
  35. int ret;
  36. ret = pmic_reg_write(dev, PFUZE100_MEMA, PFUZE_BC_MAGIC & 0xff);
  37. if (ret)
  38. return ret;
  39. ret = pmic_reg_write(dev, PFUZE100_MEMB, (PFUZE_BC_MAGIC >> 8) & 0xff);
  40. return ret;
  41. }
  42. static int pfuze100_get_value(struct udevice *dev, u32 *a)
  43. {
  44. int ret;
  45. ret = pmic_reg_read(dev, PFUZE100_MEMC);
  46. if (ret < 0)
  47. return ret;
  48. *a = ret;
  49. ret = pmic_reg_read(dev, PFUZE100_MEMD);
  50. if (ret < 0)
  51. return ret;
  52. *a += ret << 8;
  53. return 0;
  54. }
  55. static int pfuze100_set_value(struct udevice *dev, u32 val)
  56. {
  57. int ret;
  58. ret = pmic_reg_write(dev, PFUZE100_MEMC, val & 0xff);
  59. if (ret)
  60. return ret;
  61. ret = pmic_reg_write(dev, PFUZE100_MEMD, (val >> 8) & 0xff);
  62. return ret;
  63. }
  64. static int bootcount_pmic_set(struct udevice *dev, const u32 a)
  65. {
  66. struct bootcount_pmic_priv *priv = dev_get_priv(dev);
  67. if (pfuze100_set_magic(priv->pmic)) {
  68. debug("%s: writing magic failed\n", __func__);
  69. return -EIO;
  70. }
  71. if (pfuze100_set_value(priv->pmic, a)) {
  72. debug("%s: writing value failed\n", __func__);
  73. return -EIO;
  74. }
  75. return 0;
  76. }
  77. static int bootcount_pmic_get(struct udevice *dev, u32 *a)
  78. {
  79. struct bootcount_pmic_priv *priv = dev_get_priv(dev);
  80. u32 magic;
  81. if (pfuze100_get_magic(priv->pmic, &magic)) {
  82. debug("%s: reading magic failed\n", __func__);
  83. return -EIO;
  84. }
  85. if (magic != PFUZE_BC_MAGIC) {
  86. *a = 0;
  87. return 0;
  88. }
  89. if (pfuze100_get_value(priv->pmic, a)) {
  90. debug("%s: reading value failed\n", __func__);
  91. return -EIO;
  92. }
  93. return 0;
  94. }
  95. static int bootcount_pmic_probe(struct udevice *dev)
  96. {
  97. struct ofnode_phandle_args phandle_args;
  98. struct bootcount_pmic_priv *priv = dev_get_priv(dev);
  99. struct udevice *pmic;
  100. if (dev_read_phandle_with_args(dev, "pmic", NULL, 0, 0, &phandle_args)) {
  101. debug("%s: pmic backing device not specified\n", dev->name);
  102. return -ENOENT;
  103. }
  104. if (uclass_get_device_by_ofnode(UCLASS_PMIC, phandle_args.node, &pmic)) {
  105. debug("%s: could not get backing device\n", dev->name);
  106. return -ENODEV;
  107. }
  108. priv->pmic = pmic;
  109. return 0;
  110. }
  111. static const struct bootcount_ops bootcount_pmic_ops = {
  112. .get = bootcount_pmic_get,
  113. .set = bootcount_pmic_set,
  114. };
  115. static const struct udevice_id bootcount_pmic_ids[] = {
  116. { .compatible = "u-boot,bootcount-pmic" },
  117. { }
  118. };
  119. U_BOOT_DRIVER(bootcount_pmic) = {
  120. .name = "bootcount-pmic",
  121. .id = UCLASS_BOOTCOUNT,
  122. .priv_auto = sizeof(struct bootcount_pmic_priv),
  123. .probe = bootcount_pmic_probe,
  124. .of_match = bootcount_pmic_ids,
  125. .ops = &bootcount_pmic_ops,
  126. };