msm-cdc-pinctrl.c 6.2 KB

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