pinctrl-meson8-pmx.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * First generation of pinmux driver for Amlogic Meson SoCs
  3. *
  4. * Copyright (C) 2014 Beniamino Galvani <b.galvani@gmail.com>
  5. * Copyright (C) 2017 Jerome Brunet <jbrunet@baylibre.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * version 2 as published by the Free Software Foundation.
  10. *
  11. * You should have received a copy of the GNU General Public License
  12. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. /* For this first generation of pinctrl driver every pinmux group can be
  15. * enabled by a specific bit in the first register range. When all groups for
  16. * a given pin are disabled the pin acts as a GPIO.
  17. */
  18. #include <linux/device.h>
  19. #include <linux/regmap.h>
  20. #include <linux/pinctrl/pinctrl.h>
  21. #include <linux/pinctrl/pinmux.h>
  22. #include "pinctrl-meson.h"
  23. #include "pinctrl-meson8-pmx.h"
  24. /**
  25. * meson8_pmx_disable_other_groups() - disable other groups using a given pin
  26. *
  27. * @pc: meson pin controller device
  28. * @pin: number of the pin
  29. * @sel_group: index of the selected group, or -1 if none
  30. *
  31. * The function disables all pinmux groups using a pin except the
  32. * selected one. If @sel_group is -1 all groups are disabled, leaving
  33. * the pin in GPIO mode.
  34. */
  35. static void meson8_pmx_disable_other_groups(struct meson_pinctrl *pc,
  36. unsigned int pin, int sel_group)
  37. {
  38. struct meson_pmx_group *group;
  39. struct meson8_pmx_data *pmx_data;
  40. int i, j;
  41. for (i = 0; i < pc->data->num_groups; i++) {
  42. group = &pc->data->groups[i];
  43. pmx_data = (struct meson8_pmx_data *)group->data;
  44. if (pmx_data->is_gpio || i == sel_group)
  45. continue;
  46. for (j = 0; j < group->num_pins; j++) {
  47. if (group->pins[j] == pin) {
  48. /* We have found a group using the pin */
  49. regmap_update_bits(pc->reg_mux,
  50. pmx_data->reg * 4,
  51. BIT(pmx_data->bit), 0);
  52. }
  53. }
  54. }
  55. }
  56. static int meson8_pmx_set_mux(struct pinctrl_dev *pcdev, unsigned func_num,
  57. unsigned group_num)
  58. {
  59. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  60. struct meson_pmx_func *func = &pc->data->funcs[func_num];
  61. struct meson_pmx_group *group = &pc->data->groups[group_num];
  62. struct meson8_pmx_data *pmx_data =
  63. (struct meson8_pmx_data *)group->data;
  64. int i, ret = 0;
  65. dev_dbg(pc->dev, "enable function %s, group %s\n", func->name,
  66. group->name);
  67. /*
  68. * Disable groups using the same pin.
  69. * The selected group is not disabled to avoid glitches.
  70. */
  71. for (i = 0; i < group->num_pins; i++)
  72. meson8_pmx_disable_other_groups(pc, group->pins[i], group_num);
  73. /* Function 0 (GPIO) doesn't need any additional setting */
  74. if (func_num)
  75. ret = regmap_update_bits(pc->reg_mux, pmx_data->reg * 4,
  76. BIT(pmx_data->bit),
  77. BIT(pmx_data->bit));
  78. return ret;
  79. }
  80. static int meson8_pmx_request_gpio(struct pinctrl_dev *pcdev,
  81. struct pinctrl_gpio_range *range,
  82. unsigned offset)
  83. {
  84. struct meson_pinctrl *pc = pinctrl_dev_get_drvdata(pcdev);
  85. meson8_pmx_disable_other_groups(pc, offset, -1);
  86. return 0;
  87. }
  88. const struct pinmux_ops meson8_pmx_ops = {
  89. .set_mux = meson8_pmx_set_mux,
  90. .get_functions_count = meson_pmx_get_funcs_count,
  91. .get_function_name = meson_pmx_get_func_name,
  92. .get_function_groups = meson_pmx_get_groups,
  93. .gpio_request_enable = meson8_pmx_request_gpio,
  94. };