msm-cdc-pinctrl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright (c) 2016-2019, The Linux Foundation. All rights reserved.
  3. */
  4. #include <linux/kernel.h>
  5. #include <linux/init.h>
  6. #include <linux/err.h>
  7. #include <linux/module.h>
  8. #include <linux/of.h>
  9. #include <linux/of_device.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/gpio.h>
  12. #include <linux/of_gpio.h>
  13. #include <linux/pinctrl/qcom-pinctrl.h>
  14. #include <asoc/msm-cdc-pinctrl.h>
  15. struct msm_cdc_pinctrl_info {
  16. struct pinctrl *pinctrl;
  17. struct pinctrl_state *pinctrl_active;
  18. struct pinctrl_state *pinctrl_sleep;
  19. int gpio;
  20. bool state;
  21. u32 tlmm_gpio;
  22. bool wakeup_capable;
  23. };
  24. static struct msm_cdc_pinctrl_info *msm_cdc_pinctrl_get_gpiodata(
  25. struct device_node *np)
  26. {
  27. struct platform_device *pdev;
  28. struct msm_cdc_pinctrl_info *gpio_data;
  29. if (!np) {
  30. pr_err("%s: device node is null\n", __func__);
  31. return NULL;
  32. }
  33. pdev = of_find_device_by_node(np);
  34. if (!pdev) {
  35. pr_err("%s: platform device not found!\n", __func__);
  36. return NULL;
  37. }
  38. gpio_data = dev_get_drvdata(&pdev->dev);
  39. if (!gpio_data)
  40. dev_err(&pdev->dev, "%s: cannot find cdc gpio info\n",
  41. __func__);
  42. return gpio_data;
  43. }
  44. /*
  45. * msm_cdc_get_gpio_state: select pinctrl sleep state
  46. * @np: pointer to struct device_node
  47. *
  48. * Returns error code for failure and GPIO value on success
  49. */
  50. int msm_cdc_get_gpio_state(struct device_node *np)
  51. {
  52. struct msm_cdc_pinctrl_info *gpio_data;
  53. int value = -EINVAL;
  54. gpio_data = msm_cdc_pinctrl_get_gpiodata(np);
  55. if (!gpio_data)
  56. return value;
  57. if (gpio_is_valid(gpio_data->gpio))
  58. value = gpio_get_value_cansleep(gpio_data->gpio);
  59. return value;
  60. }
  61. EXPORT_SYMBOL(msm_cdc_get_gpio_state);
  62. /*
  63. * msm_cdc_pinctrl_select_sleep_state: select pinctrl sleep state
  64. * @np: pointer to struct device_node
  65. *
  66. * Returns error code for failure
  67. */
  68. int msm_cdc_pinctrl_select_sleep_state(struct device_node *np)
  69. {
  70. struct msm_cdc_pinctrl_info *gpio_data;
  71. gpio_data = msm_cdc_pinctrl_get_gpiodata(np);
  72. if (!gpio_data)
  73. return -EINVAL;
  74. if (!gpio_data->pinctrl_sleep) {
  75. pr_err("%s: pinctrl sleep state is null\n", __func__);
  76. return -EINVAL;
  77. }
  78. gpio_data->state = false;
  79. return pinctrl_select_state(gpio_data->pinctrl,
  80. gpio_data->pinctrl_sleep);
  81. }
  82. EXPORT_SYMBOL(msm_cdc_pinctrl_select_sleep_state);
  83. /*
  84. * msm_cdc_pinctrl_select_active_state: select pinctrl active state
  85. * @np: pointer to struct device_node
  86. *
  87. * Returns error code for failure
  88. */
  89. int msm_cdc_pinctrl_select_active_state(struct device_node *np)
  90. {
  91. struct msm_cdc_pinctrl_info *gpio_data;
  92. gpio_data = msm_cdc_pinctrl_get_gpiodata(np);
  93. if (!gpio_data)
  94. return -EINVAL;
  95. if (!gpio_data->pinctrl_active) {
  96. pr_err("%s: pinctrl active state is null\n", __func__);
  97. return -EINVAL;
  98. }
  99. gpio_data->state = true;
  100. return pinctrl_select_state(gpio_data->pinctrl,
  101. gpio_data->pinctrl_active);
  102. }
  103. EXPORT_SYMBOL(msm_cdc_pinctrl_select_active_state);
  104. /*
  105. * msm_cdc_pinctrl_get_state: get curren pinctrl state
  106. * @np: pointer to struct device_node
  107. *
  108. * Returns 0 for sleep state, 1 for active state,
  109. * error code for failure
  110. */
  111. int msm_cdc_pinctrl_get_state(struct device_node *np)
  112. {
  113. struct msm_cdc_pinctrl_info *gpio_data;
  114. gpio_data = msm_cdc_pinctrl_get_gpiodata(np);
  115. if (!gpio_data)
  116. return -EINVAL;
  117. return gpio_data->state;
  118. }
  119. EXPORT_SYMBOL(msm_cdc_pinctrl_get_state);
  120. /*
  121. * msm_cdc_pinctrl_set_wakeup_capable: Set a pinctrl to wakeup capable
  122. * @np: pointer to struct device_node
  123. * @enable: wakeup capable when set to true
  124. *
  125. * Returns 0 for success and error code for failure
  126. */
  127. int msm_cdc_pinctrl_set_wakeup_capable(struct device_node *np, bool enable)
  128. {
  129. struct msm_cdc_pinctrl_info *gpio_data;
  130. int ret = 0;
  131. gpio_data = msm_cdc_pinctrl_get_gpiodata(np);
  132. if (!gpio_data)
  133. return -EINVAL;
  134. if (gpio_data->wakeup_capable)
  135. ret = msm_gpio_mpm_wake_set(gpio_data->tlmm_gpio, enable);
  136. return ret;
  137. }
  138. EXPORT_SYMBOL(msm_cdc_pinctrl_set_wakeup_capable);
  139. static int msm_cdc_pinctrl_probe(struct platform_device *pdev)
  140. {
  141. int ret = 0;
  142. struct msm_cdc_pinctrl_info *gpio_data;
  143. u32 tlmm_gpio = 0;
  144. gpio_data = devm_kzalloc(&pdev->dev,
  145. sizeof(struct msm_cdc_pinctrl_info),
  146. GFP_KERNEL);
  147. if (!gpio_data)
  148. return -ENOMEM;
  149. gpio_data->pinctrl = devm_pinctrl_get(&pdev->dev);
  150. if (IS_ERR_OR_NULL(gpio_data->pinctrl)) {
  151. dev_err(&pdev->dev, "%s: Cannot get cdc gpio pinctrl:%ld\n",
  152. __func__, PTR_ERR(gpio_data->pinctrl));
  153. ret = PTR_ERR(gpio_data->pinctrl);
  154. goto err_pctrl_get;
  155. }
  156. gpio_data->pinctrl_active = pinctrl_lookup_state(
  157. gpio_data->pinctrl, "aud_active");
  158. if (IS_ERR_OR_NULL(gpio_data->pinctrl_active)) {
  159. dev_err(&pdev->dev, "%s: Cannot get aud_active pinctrl state:%ld\n",
  160. __func__, PTR_ERR(gpio_data->pinctrl_active));
  161. ret = PTR_ERR(gpio_data->pinctrl_active);
  162. goto err_lookup_state;
  163. }
  164. gpio_data->pinctrl_sleep = pinctrl_lookup_state(
  165. gpio_data->pinctrl, "aud_sleep");
  166. if (IS_ERR_OR_NULL(gpio_data->pinctrl_sleep)) {
  167. dev_err(&pdev->dev, "%s: Cannot get aud_sleep pinctrl state:%ld\n",
  168. __func__, PTR_ERR(gpio_data->pinctrl_sleep));
  169. ret = PTR_ERR(gpio_data->pinctrl_sleep);
  170. goto err_lookup_state;
  171. }
  172. /* skip setting to sleep state for LPI_TLMM GPIOs */
  173. if (!of_property_read_bool(pdev->dev.of_node, "qcom,lpi-gpios")) {
  174. /* Set pinctrl state to aud_sleep by default */
  175. ret = pinctrl_select_state(gpio_data->pinctrl,
  176. gpio_data->pinctrl_sleep);
  177. if (ret)
  178. dev_err(&pdev->dev, "%s: set cdc gpio sleep state fail: %d\n",
  179. __func__, ret);
  180. }
  181. if (!of_property_read_u32(pdev->dev.of_node, "qcom,tlmm-gpio",
  182. &tlmm_gpio)) {
  183. gpio_data->wakeup_capable = true;
  184. gpio_data->tlmm_gpio = tlmm_gpio;
  185. }
  186. gpio_data->gpio = of_get_named_gpio(pdev->dev.of_node,
  187. "qcom,cdc-rst-n-gpio", 0);
  188. if (gpio_is_valid(gpio_data->gpio)) {
  189. ret = gpio_request(gpio_data->gpio, "MSM_CDC_RESET");
  190. if (ret) {
  191. dev_err(&pdev->dev, "%s: Failed to request gpio %d\n",
  192. __func__, gpio_data->gpio);
  193. goto err_lookup_state;
  194. }
  195. }
  196. dev_set_drvdata(&pdev->dev, gpio_data);
  197. return 0;
  198. err_lookup_state:
  199. devm_pinctrl_put(gpio_data->pinctrl);
  200. err_pctrl_get:
  201. devm_kfree(&pdev->dev, gpio_data);
  202. return ret;
  203. }
  204. static int msm_cdc_pinctrl_remove(struct platform_device *pdev)
  205. {
  206. struct msm_cdc_pinctrl_info *gpio_data;
  207. gpio_data = dev_get_drvdata(&pdev->dev);
  208. /* to free the requested gpio before exiting */
  209. if (gpio_data) {
  210. if (gpio_is_valid(gpio_data->gpio))
  211. gpio_free(gpio_data->gpio);
  212. if (gpio_data->pinctrl)
  213. devm_pinctrl_put(gpio_data->pinctrl);
  214. }
  215. devm_kfree(&pdev->dev, gpio_data);
  216. return 0;
  217. }
  218. static const struct of_device_id msm_cdc_pinctrl_match[] = {
  219. {.compatible = "qcom,msm-cdc-pinctrl"},
  220. {}
  221. };
  222. static struct platform_driver msm_cdc_pinctrl_driver = {
  223. .driver = {
  224. .name = "msm-cdc-pinctrl",
  225. .owner = THIS_MODULE,
  226. .of_match_table = msm_cdc_pinctrl_match,
  227. .suppress_bind_attrs = true,
  228. },
  229. .probe = msm_cdc_pinctrl_probe,
  230. .remove = msm_cdc_pinctrl_remove,
  231. };
  232. int msm_cdc_pinctrl_drv_init(void)
  233. {
  234. return platform_driver_register(&msm_cdc_pinctrl_driver);
  235. }
  236. void msm_cdc_pinctrl_drv_exit(void)
  237. {
  238. platform_driver_unregister(&msm_cdc_pinctrl_driver);
  239. }
  240. MODULE_DESCRIPTION("MSM CODEC pin control platform driver");
  241. MODULE_LICENSE("GPL v2");