pinctrl-max7360.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2025 Bootlin
  4. *
  5. * Author: Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>
  6. */
  7. #include <linux/array_size.h>
  8. #include <linux/dev_printk.h>
  9. #include <linux/device.h>
  10. #include <linux/device/devres.h>
  11. #include <linux/err.h>
  12. #include <linux/init.h>
  13. #include <linux/mfd/max7360.h>
  14. #include <linux/module.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/regmap.h>
  17. #include <linux/stddef.h>
  18. #include <linux/pinctrl/pinctrl.h>
  19. #include <linux/pinctrl/pinconf-generic.h>
  20. #include <linux/pinctrl/pinmux.h>
  21. #include "core.h"
  22. #include "pinmux.h"
  23. struct max7360_pinctrl {
  24. struct pinctrl_dev *pctldev;
  25. struct pinctrl_desc pinctrl_desc;
  26. };
  27. static const struct pinctrl_pin_desc max7360_pins[] = {
  28. PINCTRL_PIN(0, "PORT0"),
  29. PINCTRL_PIN(1, "PORT1"),
  30. PINCTRL_PIN(2, "PORT2"),
  31. PINCTRL_PIN(3, "PORT3"),
  32. PINCTRL_PIN(4, "PORT4"),
  33. PINCTRL_PIN(5, "PORT5"),
  34. PINCTRL_PIN(6, "PORT6"),
  35. PINCTRL_PIN(7, "PORT7"),
  36. };
  37. static const unsigned int port0_pins[] = {0};
  38. static const unsigned int port1_pins[] = {1};
  39. static const unsigned int port2_pins[] = {2};
  40. static const unsigned int port3_pins[] = {3};
  41. static const unsigned int port4_pins[] = {4};
  42. static const unsigned int port5_pins[] = {5};
  43. static const unsigned int port6_pins[] = {6};
  44. static const unsigned int port7_pins[] = {7};
  45. static const unsigned int rotary_pins[] = {6, 7};
  46. static const struct pingroup max7360_groups[] = {
  47. PINCTRL_PINGROUP("PORT0", port0_pins, ARRAY_SIZE(port0_pins)),
  48. PINCTRL_PINGROUP("PORT1", port1_pins, ARRAY_SIZE(port1_pins)),
  49. PINCTRL_PINGROUP("PORT2", port2_pins, ARRAY_SIZE(port2_pins)),
  50. PINCTRL_PINGROUP("PORT3", port3_pins, ARRAY_SIZE(port3_pins)),
  51. PINCTRL_PINGROUP("PORT4", port4_pins, ARRAY_SIZE(port4_pins)),
  52. PINCTRL_PINGROUP("PORT5", port5_pins, ARRAY_SIZE(port5_pins)),
  53. PINCTRL_PINGROUP("PORT6", port6_pins, ARRAY_SIZE(port6_pins)),
  54. PINCTRL_PINGROUP("PORT7", port7_pins, ARRAY_SIZE(port7_pins)),
  55. PINCTRL_PINGROUP("ROTARY", rotary_pins, ARRAY_SIZE(rotary_pins)),
  56. };
  57. static int max7360_pinctrl_get_groups_count(struct pinctrl_dev *pctldev)
  58. {
  59. return ARRAY_SIZE(max7360_groups);
  60. }
  61. static const char *max7360_pinctrl_get_group_name(struct pinctrl_dev *pctldev,
  62. unsigned int group)
  63. {
  64. return max7360_groups[group].name;
  65. }
  66. static int max7360_pinctrl_get_group_pins(struct pinctrl_dev *pctldev,
  67. unsigned int group,
  68. const unsigned int **pins,
  69. unsigned int *num_pins)
  70. {
  71. *pins = max7360_groups[group].pins;
  72. *num_pins = max7360_groups[group].npins;
  73. return 0;
  74. }
  75. static const struct pinctrl_ops max7360_pinctrl_ops = {
  76. .get_groups_count = max7360_pinctrl_get_groups_count,
  77. .get_group_name = max7360_pinctrl_get_group_name,
  78. .get_group_pins = max7360_pinctrl_get_group_pins,
  79. #ifdef CONFIG_OF
  80. .dt_node_to_map = pinconf_generic_dt_node_to_map_pin,
  81. .dt_free_map = pinconf_generic_dt_free_map,
  82. #endif
  83. };
  84. static const char * const simple_groups[] = {
  85. "PORT0", "PORT1", "PORT2", "PORT3",
  86. "PORT4", "PORT5", "PORT6", "PORT7",
  87. };
  88. static const char * const rotary_groups[] = { "ROTARY" };
  89. #define MAX7360_PINCTRL_FN_GPIO 0
  90. #define MAX7360_PINCTRL_FN_PWM 1
  91. #define MAX7360_PINCTRL_FN_ROTARY 2
  92. static const struct pinfunction max7360_functions[] = {
  93. [MAX7360_PINCTRL_FN_GPIO] = PINCTRL_PINFUNCTION("gpio", simple_groups,
  94. ARRAY_SIZE(simple_groups)),
  95. [MAX7360_PINCTRL_FN_PWM] = PINCTRL_PINFUNCTION("pwm", simple_groups,
  96. ARRAY_SIZE(simple_groups)),
  97. [MAX7360_PINCTRL_FN_ROTARY] = PINCTRL_PINFUNCTION("rotary", rotary_groups,
  98. ARRAY_SIZE(rotary_groups)),
  99. };
  100. static int max7360_get_functions_count(struct pinctrl_dev *pctldev)
  101. {
  102. return ARRAY_SIZE(max7360_functions);
  103. }
  104. static const char *max7360_get_function_name(struct pinctrl_dev *pctldev, unsigned int selector)
  105. {
  106. return max7360_functions[selector].name;
  107. }
  108. static int max7360_get_function_groups(struct pinctrl_dev *pctldev, unsigned int selector,
  109. const char * const **groups,
  110. unsigned int * const num_groups)
  111. {
  112. *groups = max7360_functions[selector].groups;
  113. *num_groups = max7360_functions[selector].ngroups;
  114. return 0;
  115. }
  116. static int max7360_set_mux(struct pinctrl_dev *pctldev, unsigned int selector,
  117. unsigned int group)
  118. {
  119. struct regmap *regmap = dev_get_regmap(pctldev->dev->parent, NULL);
  120. int val;
  121. /*
  122. * GPIO and PWM functions are the same: we only need to handle the
  123. * rotary encoder function, on pins 6 and 7.
  124. */
  125. if (max7360_groups[group].pins[0] >= 6) {
  126. if (selector == MAX7360_PINCTRL_FN_ROTARY)
  127. val = MAX7360_GPIO_CFG_RTR_EN;
  128. else
  129. val = 0;
  130. return regmap_write_bits(regmap, MAX7360_REG_GPIOCFG, MAX7360_GPIO_CFG_RTR_EN, val);
  131. }
  132. return 0;
  133. }
  134. static const struct pinmux_ops max7360_pmxops = {
  135. .get_functions_count = max7360_get_functions_count,
  136. .get_function_name = max7360_get_function_name,
  137. .get_function_groups = max7360_get_function_groups,
  138. .set_mux = max7360_set_mux,
  139. .strict = true,
  140. };
  141. static int max7360_pinctrl_probe(struct platform_device *pdev)
  142. {
  143. struct regmap *regmap;
  144. struct pinctrl_desc *pd;
  145. struct max7360_pinctrl *chip;
  146. struct device *dev = &pdev->dev;
  147. regmap = dev_get_regmap(dev->parent, NULL);
  148. if (!regmap)
  149. return dev_err_probe(dev, -ENODEV, "Could not get parent regmap\n");
  150. chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
  151. if (!chip)
  152. return -ENOMEM;
  153. pd = &chip->pinctrl_desc;
  154. pd->pctlops = &max7360_pinctrl_ops;
  155. pd->pmxops = &max7360_pmxops;
  156. pd->name = dev_name(dev);
  157. pd->pins = max7360_pins;
  158. pd->npins = MAX7360_MAX_GPIO;
  159. pd->owner = THIS_MODULE;
  160. /*
  161. * This MFD sub-device does not have any associated device tree node:
  162. * properties are stored in the device node of the parent (MFD) device
  163. * and this same node is used in phandles of client devices.
  164. * Reuse this device tree node here, as otherwise the pinctrl subsystem
  165. * would be confused by this topology.
  166. */
  167. device_set_of_node_from_dev(dev, dev->parent);
  168. chip->pctldev = devm_pinctrl_register(dev, pd, chip);
  169. if (IS_ERR(chip->pctldev))
  170. return dev_err_probe(dev, PTR_ERR(chip->pctldev), "can't register controller\n");
  171. return 0;
  172. }
  173. static struct platform_driver max7360_pinctrl_driver = {
  174. .driver = {
  175. .name = "max7360-pinctrl",
  176. },
  177. .probe = max7360_pinctrl_probe,
  178. };
  179. module_platform_driver(max7360_pinctrl_driver);
  180. MODULE_DESCRIPTION("MAX7360 pinctrl driver");
  181. MODULE_AUTHOR("Mathieu Dubois-Briand <mathieu.dubois-briand@bootlin.com>");
  182. MODULE_LICENSE("GPL");