pinctrl.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-11-26 GuEe-GUI first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtservice.h>
  12. #include <rtdevice.h>
  13. #define DBG_TAG "rtdm.pinctrl"
  14. #define DBG_LVL DBG_INFO
  15. #include <rtdbg.h>
  16. #ifdef RT_USING_OFW
  17. static rt_err_t ofw_pin_ctrl_confs_apply(struct rt_ofw_node *np, int index)
  18. {
  19. rt_err_t err = -RT_EEMPTY;
  20. rt_phandle phandle;
  21. const fdt32_t *cell;
  22. struct rt_ofw_prop *prop;
  23. char pinctrl_n_name[sizeof("pinctrl-0")];
  24. rt_sprintf(pinctrl_n_name, "pinctrl-%d", index);
  25. index = 0;
  26. rt_ofw_foreach_prop_u32(np, pinctrl_n_name, prop, cell, phandle)
  27. {
  28. struct rt_device_pin *pinctrl = RT_NULL;
  29. struct rt_ofw_node *conf_np, *pinctrl_np;
  30. conf_np = pinctrl_np = rt_ofw_find_node_by_phandle(phandle);
  31. if (!conf_np)
  32. {
  33. err = -RT_EIO;
  34. break;
  35. }
  36. /*
  37. * We always assume the phandle in pinctrl-N is the pinctrl-device
  38. * node's child node. If not, we need a better way to find it:
  39. *
  40. * / {
  41. * serial@4600 {
  42. * device_type = "serial";
  43. * reg = <0x4600 0x100>;
  44. * clock-frequency = <0>;
  45. * pinctrl-names = "default";
  46. * pinctrl-0 = <&uart_pin>;
  47. * };
  48. *
  49. * i2c@4700 {
  50. * reg = <0x4700 0x100>;
  51. * pinctrl-names = "default";
  52. * pinctrl-0 = <&i2c_pin_scl, &i2c_pin_sda>;
  53. * };
  54. *
  55. * pinctrl: pinctrl {
  56. *
  57. * uart_pin {
  58. * multi,pins =
  59. * <0 PD0 1 &uart_rx_pull_up>,
  60. * <0 PD1 1 &uart_tx_pull_up>;
  61. * };
  62. *
  63. * i2c_pin_scl {
  64. * single,pins = <0 PB1>;
  65. * pull = <&i2c_pull_none_smt>;
  66. * function = <1>;
  67. * };
  68. *
  69. * i2c_pin_sda {
  70. * single,pins = <0 PB2>;
  71. * pull = <&i2c_pull_none_smt>;
  72. * function = <1>;
  73. * };
  74. * };
  75. * }
  76. */
  77. rt_ofw_foreach_parent_node(pinctrl_np)
  78. {
  79. if (rt_ofw_prop_read_bool(pinctrl_np, "compatible"))
  80. {
  81. break;
  82. }
  83. }
  84. if (pinctrl_np)
  85. {
  86. if (!rt_ofw_data(pinctrl_np))
  87. {
  88. rt_platform_ofw_request(pinctrl_np);
  89. }
  90. pinctrl = rt_ofw_data(pinctrl_np);
  91. rt_ofw_node_put(pinctrl_np);
  92. }
  93. if (!pinctrl || !pinctrl->ops || !pinctrl->ops->pin_ctrl_confs_apply)
  94. {
  95. if (index)
  96. {
  97. err = -RT_EEMPTY;
  98. }
  99. else
  100. {
  101. err = -RT_ERROR;
  102. }
  103. rt_ofw_node_put(conf_np);
  104. break;
  105. }
  106. err = pinctrl->ops->pin_ctrl_confs_apply(&pinctrl->parent, conf_np);
  107. rt_ofw_node_put(conf_np);
  108. if (err)
  109. {
  110. break;
  111. }
  112. ++index;
  113. }
  114. return err;
  115. }
  116. static int ofw_pin_ctrl_confs_lookup(struct rt_ofw_node *np, const char *name)
  117. {
  118. return rt_ofw_prop_index_of_string(np, "pinctrl-names", name);
  119. }
  120. static rt_err_t ofw_pin_ctrl_confs_apply_by_name(struct rt_ofw_node *np, const char *name)
  121. {
  122. int index;
  123. rt_err_t err;
  124. index = ofw_pin_ctrl_confs_lookup(np, name);
  125. if (index >= 0)
  126. {
  127. err = ofw_pin_ctrl_confs_apply(np, index);
  128. }
  129. else
  130. {
  131. err = -RT_EEMPTY;
  132. }
  133. return err;
  134. }
  135. #endif /* RT_USING_OFW */
  136. rt_ssize_t rt_pin_ctrl_confs_lookup(struct rt_device *device, const char *name)
  137. {
  138. rt_ssize_t res;
  139. if (device && name)
  140. {
  141. res = -RT_ENOSYS;
  142. #ifdef RT_USING_OFW
  143. if (device->ofw_node)
  144. {
  145. res = ofw_pin_ctrl_confs_lookup(device->ofw_node, name);
  146. }
  147. #endif /* RT_USING_OFW */
  148. }
  149. else
  150. {
  151. res = -RT_EINVAL;
  152. }
  153. return res;
  154. }
  155. rt_err_t rt_pin_ctrl_confs_apply(struct rt_device *device, int index)
  156. {
  157. rt_err_t err;
  158. if (device && index >= 0)
  159. {
  160. err = -RT_ENOSYS;
  161. #ifdef RT_USING_OFW
  162. if (device->ofw_node)
  163. {
  164. err = ofw_pin_ctrl_confs_apply(device->ofw_node, index);
  165. }
  166. #endif /* RT_USING_OFW */
  167. }
  168. else
  169. {
  170. err = -RT_EINVAL;
  171. }
  172. return err;
  173. }
  174. rt_err_t rt_pin_ctrl_confs_apply_by_name(struct rt_device *device, const char *name)
  175. {
  176. rt_err_t err;
  177. if (device)
  178. {
  179. if (!name)
  180. {
  181. name = "default";
  182. }
  183. err = -RT_ENOSYS;
  184. #ifdef RT_USING_OFW
  185. if (device->ofw_node)
  186. {
  187. err = ofw_pin_ctrl_confs_apply_by_name(device->ofw_node, name);
  188. }
  189. #endif /* RT_USING_OFW */
  190. RT_UNUSED(name);
  191. }
  192. else
  193. {
  194. err = -RT_EINVAL;
  195. }
  196. return err;
  197. }