pinmux-aspeed.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Copyright (C) 2019 IBM Corp. */
  3. /* Pieces to enable drivers to implement the .set callback */
  4. #include "pinmux-aspeed.h"
  5. static const char *const aspeed_pinmux_ips[] = {
  6. [ASPEED_IP_SCU] = "SCU",
  7. [ASPEED_IP_GFX] = "GFX",
  8. [ASPEED_IP_LPC] = "LPC",
  9. };
  10. static inline void aspeed_sig_desc_print_val(
  11. const struct aspeed_sig_desc *desc, bool enable, u32 rv)
  12. {
  13. pr_debug("Want %s%X[0x%08X]=0x%X, got 0x%X from 0x%08X\n",
  14. aspeed_pinmux_ips[desc->ip], desc->reg,
  15. desc->mask, enable ? desc->enable : desc->disable,
  16. (rv & desc->mask) >> __ffs(desc->mask), rv);
  17. }
  18. /**
  19. * aspeed_sig_desc_eval() - Query the enabled or disabled state of a signal
  20. * descriptor.
  21. *
  22. * @desc: The signal descriptor of interest
  23. * @enabled: True to query the enabled state, false to query disabled state
  24. * @map: The IP block's regmap instance
  25. *
  26. * Return: 1 if the descriptor's bitfield is configured to the state
  27. * selected by @enabled, 0 if not, and less than zero if an unrecoverable
  28. * failure occurred
  29. *
  30. * Evaluation of descriptor state is non-trivial in that it is not a binary
  31. * outcome: The bitfields can be greater than one bit in size and thus can take
  32. * a value that is neither the enabled nor disabled state recorded in the
  33. * descriptor (typically this means a different function to the one of interest
  34. * is enabled). Thus we must explicitly test for either condition as required.
  35. */
  36. int aspeed_sig_desc_eval(const struct aspeed_sig_desc *desc,
  37. bool enabled, struct regmap *map)
  38. {
  39. int ret;
  40. unsigned int raw;
  41. u32 want;
  42. if (!map)
  43. return -ENODEV;
  44. ret = regmap_read(map, desc->reg, &raw);
  45. if (ret)
  46. return ret;
  47. aspeed_sig_desc_print_val(desc, enabled, raw);
  48. want = enabled ? desc->enable : desc->disable;
  49. return ((raw & desc->mask) >> __ffs(desc->mask)) == want;
  50. }
  51. /**
  52. * aspeed_sig_expr_eval - Query the enabled or disabled state for a
  53. * mux function's signal on a pin
  54. *
  55. * @ctx: The driver context for the pinctrl IP
  56. * @expr: An expression controlling the signal for a mux function on a pin
  57. * @enabled: True to query the enabled state, false to query disabled state
  58. *
  59. * Return: 1 if the expression composed by @enabled evaluates true, 0 if not,
  60. * and less than zero if an unrecoverable failure occurred.
  61. *
  62. * A mux function is enabled or disabled if the function's signal expression
  63. * for each pin in the function's pin group evaluates true for the desired
  64. * state. An signal expression evaluates true if all of its associated signal
  65. * descriptors evaluate true for the desired state.
  66. *
  67. * If an expression's state is described by more than one bit, either through
  68. * multi-bit bitfields in a single signal descriptor or through multiple signal
  69. * descriptors of a single bit then it is possible for the expression to be in
  70. * neither the enabled nor disabled state. Thus we must explicitly test for
  71. * either condition as required.
  72. */
  73. int aspeed_sig_expr_eval(struct aspeed_pinmux_data *ctx,
  74. const struct aspeed_sig_expr *expr, bool enabled)
  75. {
  76. int ret;
  77. int i;
  78. if (ctx->ops->eval)
  79. return ctx->ops->eval(ctx, expr, enabled);
  80. for (i = 0; i < expr->ndescs; i++) {
  81. const struct aspeed_sig_desc *desc = &expr->descs[i];
  82. ret = aspeed_sig_desc_eval(desc, enabled, ctx->maps[desc->ip]);
  83. if (ret <= 0)
  84. return ret;
  85. }
  86. return 1;
  87. }