pinctrl.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. pinctrl = rt_ofw_data(pinctrl_np);
  87. rt_ofw_node_put(pinctrl_np);
  88. }
  89. if (!pinctrl || !pinctrl->ops || !pinctrl->ops->pin_ctrl_confs_apply)
  90. {
  91. if (index)
  92. {
  93. err = -RT_EEMPTY;
  94. }
  95. else
  96. {
  97. err = -RT_ERROR;
  98. }
  99. rt_ofw_node_put(conf_np);
  100. break;
  101. }
  102. err = pinctrl->ops->pin_ctrl_confs_apply(&pinctrl->parent, conf_np);
  103. rt_ofw_node_put(conf_np);
  104. if (err)
  105. {
  106. break;
  107. }
  108. ++index;
  109. }
  110. return err;
  111. }
  112. static int ofw_pin_ctrl_confs_lookup(struct rt_ofw_node *np, const char *name)
  113. {
  114. return rt_ofw_prop_index_of_string(np, "pinctrl-names", name);
  115. }
  116. static rt_err_t ofw_pin_ctrl_confs_apply_by_name(struct rt_ofw_node *np, const char *name)
  117. {
  118. int index;
  119. rt_err_t err;
  120. index = ofw_pin_ctrl_confs_lookup(np, name);
  121. if (index >= 0)
  122. {
  123. err = ofw_pin_ctrl_confs_apply(np, index);
  124. }
  125. else
  126. {
  127. err = -RT_EEMPTY;
  128. }
  129. return err;
  130. }
  131. #endif /* RT_USING_OFW */
  132. rt_ssize_t rt_pin_ctrl_confs_lookup(struct rt_device *device, const char *name)
  133. {
  134. rt_ssize_t res;
  135. if (device && name)
  136. {
  137. res = -RT_ENOSYS;
  138. #ifdef RT_USING_OFW
  139. if (device->ofw_node)
  140. {
  141. res = ofw_pin_ctrl_confs_lookup(device->ofw_node, name);
  142. }
  143. #endif /* RT_USING_OFW */
  144. }
  145. else
  146. {
  147. res = -RT_EINVAL;
  148. }
  149. return res;
  150. }
  151. rt_err_t rt_pin_ctrl_confs_apply(struct rt_device *device, int index)
  152. {
  153. rt_err_t err;
  154. if (device && index >= 0)
  155. {
  156. err = -RT_ENOSYS;
  157. #ifdef RT_USING_OFW
  158. if (device->ofw_node)
  159. {
  160. err = ofw_pin_ctrl_confs_apply(device->ofw_node, index);
  161. }
  162. #endif /* RT_USING_OFW */
  163. }
  164. else
  165. {
  166. err = -RT_EINVAL;
  167. }
  168. return err;
  169. }
  170. rt_err_t rt_pin_ctrl_confs_apply_by_name(struct rt_device *device, const char *name)
  171. {
  172. rt_err_t err;
  173. if (device)
  174. {
  175. if (!name)
  176. {
  177. name = "default";
  178. }
  179. err = -RT_ENOSYS;
  180. #ifdef RT_USING_OFW
  181. if (device->ofw_node)
  182. {
  183. err = ofw_pin_ctrl_confs_apply_by_name(device->ofw_node, name);
  184. }
  185. #endif /* RT_USING_OFW */
  186. }
  187. else
  188. {
  189. err = -RT_EINVAL;
  190. }
  191. return err;
  192. }