msm-cdc-pinctrl.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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/io.h>
  7. #include <linux/err.h>
  8. #include <linux/module.h>
  9. #include <linux/of.h>
  10. #include <linux/of_device.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/gpio.h>
  13. #include <linux/of_gpio.h>
  14. #include <linux/pinctrl/qcom-pinctrl.h>
  15. #include <asoc/msm-cdc-pinctrl.h>
  16. #define MAX_GPIOS 16
  17. struct msm_cdc_pinctrl_info {
  18. struct pinctrl *pinctrl;
  19. struct pinctrl_state *pinctrl_active;
  20. struct pinctrl_state *pinctrl_sleep;
  21. int gpio;
  22. bool state;
  23. u32 tlmm_gpio[MAX_GPIOS];
  24. char __iomem *chip_wakeup_register[MAX_GPIOS];
  25. u32 chip_wakeup_maskbit[MAX_GPIOS];
  26. u32 count;
  27. u32 wakeup_reg_count;
  28. bool wakeup_capable;
  29. bool chip_wakeup_reg;
  30. };
  31. static struct msm_cdc_pinctrl_info *msm_cdc_pinctrl_get_gpiodata(
  32. struct device_node *np)
  33. {
  34. struct platform_device *pdev;
  35. struct msm_cdc_pinctrl_info *gpio_data;
  36. if (!np) {
  37. pr_err("%s: device node is null\n", __func__);
  38. return NULL;
  39. }
  40. pdev = of_find_device_by_node(np);
  41. if (!pdev) {
  42. pr_err("%s: platform device not found!\n", __func__);
  43. return NULL;
  44. }
  45. gpio_data = dev_get_drvdata(&pdev->dev);
  46. if (!gpio_data)
  47. dev_err(&pdev->dev, "%s: cannot find cdc gpio info\n",
  48. __func__);
  49. return gpio_data;
  50. }
  51. /*
  52. * msm_cdc_get_gpio_state: select pinctrl sleep state
  53. * @np: pointer to struct device_node
  54. *
  55. * Returns error code for failure and GPIO value on success
  56. */
  57. int msm_cdc_get_gpio_state(struct device_node *np)
  58. {
  59. struct msm_cdc_pinctrl_info *gpio_data;
  60. int value = -EINVAL;
  61. gpio_data = msm_cdc_pinctrl_get_gpiodata(np);
  62. if (!gpio_data)
  63. return value;
  64. if (gpio_is_valid(gpio_data->gpio))
  65. value = gpio_get_value_cansleep(gpio_data->gpio);
  66. return value;
  67. }
  68. EXPORT_SYMBOL(msm_cdc_get_gpio_state);
  69. /*
  70. * msm_cdc_pinctrl_select_sleep_state: select pinctrl sleep state
  71. * @np: pointer to struct device_node
  72. *
  73. * Returns error code for failure
  74. */
  75. int msm_cdc_pinctrl_select_sleep_state(struct device_node *np)
  76. {
  77. struct msm_cdc_pinctrl_info *gpio_data;
  78. gpio_data = msm_cdc_pinctrl_get_gpiodata(np);
  79. if (!gpio_data)
  80. return -EINVAL;
  81. if (!gpio_data->pinctrl_sleep) {
  82. pr_err("%s: pinctrl sleep state is null\n", __func__);
  83. return -EINVAL;
  84. }
  85. gpio_data->state = false;
  86. return pinctrl_select_state(gpio_data->pinctrl,
  87. gpio_data->pinctrl_sleep);
  88. }
  89. EXPORT_SYMBOL(msm_cdc_pinctrl_select_sleep_state);
  90. /*
  91. * msm_cdc_pinctrl_select_active_state: select pinctrl active state
  92. * @np: pointer to struct device_node
  93. *
  94. * Returns error code for failure
  95. */
  96. int msm_cdc_pinctrl_select_active_state(struct device_node *np)
  97. {
  98. struct msm_cdc_pinctrl_info *gpio_data;
  99. gpio_data = msm_cdc_pinctrl_get_gpiodata(np);
  100. if (!gpio_data)
  101. return -EINVAL;
  102. if (!gpio_data->pinctrl_active) {
  103. pr_err("%s: pinctrl active state is null\n", __func__);
  104. return -EINVAL;
  105. }
  106. gpio_data->state = true;
  107. return pinctrl_select_state(gpio_data->pinctrl,
  108. gpio_data->pinctrl_active);
  109. }
  110. EXPORT_SYMBOL(msm_cdc_pinctrl_select_active_state);
  111. /*
  112. * msm_cdc_pinctrl_get_state: get curren pinctrl state
  113. * @np: pointer to struct device_node
  114. *
  115. * Returns 0 for sleep state, 1 for active state,
  116. * error code for failure
  117. */
  118. int msm_cdc_pinctrl_get_state(struct device_node *np)
  119. {
  120. struct msm_cdc_pinctrl_info *gpio_data;
  121. gpio_data = msm_cdc_pinctrl_get_gpiodata(np);
  122. if (!gpio_data)
  123. return -EINVAL;
  124. return gpio_data->state;
  125. }
  126. EXPORT_SYMBOL(msm_cdc_pinctrl_get_state);
  127. /*
  128. * msm_cdc_pinctrl_set_wakeup_capable: Set a pinctrl to wakeup capable
  129. * @np: pointer to struct device_node
  130. * @enable: wakeup capable when set to true
  131. *
  132. * Returns 0 for success and error code for failure
  133. */
  134. int msm_cdc_pinctrl_set_wakeup_capable(struct device_node *np, bool enable)
  135. {
  136. struct msm_cdc_pinctrl_info *gpio_data;
  137. int ret = 0;
  138. u32 i = 0, temp = 0;
  139. gpio_data = msm_cdc_pinctrl_get_gpiodata(np);
  140. if (!gpio_data)
  141. return -EINVAL;
  142. if (gpio_data->wakeup_capable) {
  143. for (i = 0; i < gpio_data->count; i++) {
  144. ret = msm_gpio_mpm_wake_set(gpio_data->tlmm_gpio[i],
  145. enable);
  146. if (ret < 0)
  147. goto exit;
  148. }
  149. }
  150. if (gpio_data->chip_wakeup_reg) {
  151. for (i = 0; i < gpio_data->wakeup_reg_count; i++) {
  152. temp = ioread32(gpio_data->chip_wakeup_register[i]);
  153. if (enable)
  154. temp |= (1 <<
  155. gpio_data->chip_wakeup_maskbit[i]);
  156. else
  157. temp &= ~(1 <<
  158. gpio_data->chip_wakeup_maskbit[i]);
  159. iowrite32(temp, gpio_data->chip_wakeup_register[i]);
  160. }
  161. }
  162. exit:
  163. return ret;
  164. }
  165. EXPORT_SYMBOL(msm_cdc_pinctrl_set_wakeup_capable);
  166. static int msm_cdc_pinctrl_probe(struct platform_device *pdev)
  167. {
  168. int ret = 0;
  169. struct msm_cdc_pinctrl_info *gpio_data;
  170. u32 tlmm_gpio[MAX_GPIOS] = {0};
  171. u32 chip_wakeup_reg[MAX_GPIOS] = {0};
  172. u32 i = 0;
  173. int count = 0;
  174. gpio_data = devm_kzalloc(&pdev->dev,
  175. sizeof(struct msm_cdc_pinctrl_info),
  176. GFP_KERNEL);
  177. if (!gpio_data)
  178. return -ENOMEM;
  179. gpio_data->pinctrl = devm_pinctrl_get(&pdev->dev);
  180. if (IS_ERR_OR_NULL(gpio_data->pinctrl)) {
  181. dev_err(&pdev->dev, "%s: Cannot get cdc gpio pinctrl:%ld\n",
  182. __func__, PTR_ERR(gpio_data->pinctrl));
  183. ret = PTR_ERR(gpio_data->pinctrl);
  184. goto err_pctrl_get;
  185. }
  186. gpio_data->pinctrl_active = pinctrl_lookup_state(
  187. gpio_data->pinctrl, "aud_active");
  188. if (IS_ERR_OR_NULL(gpio_data->pinctrl_active)) {
  189. dev_err(&pdev->dev, "%s: Cannot get aud_active pinctrl state:%ld\n",
  190. __func__, PTR_ERR(gpio_data->pinctrl_active));
  191. ret = PTR_ERR(gpio_data->pinctrl_active);
  192. goto err_lookup_state;
  193. }
  194. gpio_data->pinctrl_sleep = pinctrl_lookup_state(
  195. gpio_data->pinctrl, "aud_sleep");
  196. if (IS_ERR_OR_NULL(gpio_data->pinctrl_sleep)) {
  197. dev_err(&pdev->dev, "%s: Cannot get aud_sleep pinctrl state:%ld\n",
  198. __func__, PTR_ERR(gpio_data->pinctrl_sleep));
  199. ret = PTR_ERR(gpio_data->pinctrl_sleep);
  200. goto err_lookup_state;
  201. }
  202. /* skip setting to sleep state for LPI_TLMM GPIOs */
  203. if (!of_property_read_bool(pdev->dev.of_node, "qcom,lpi-gpios")) {
  204. /* Set pinctrl state to aud_sleep by default */
  205. ret = pinctrl_select_state(gpio_data->pinctrl,
  206. gpio_data->pinctrl_sleep);
  207. if (ret)
  208. dev_err(&pdev->dev, "%s: set cdc gpio sleep state fail: %d\n",
  209. __func__, ret);
  210. }
  211. count = of_property_count_u32_elems(pdev->dev.of_node, "qcom,chip-wakeup-reg");
  212. if (count <= 0)
  213. goto cdc_tlmm_gpio;
  214. if (!of_property_read_u32_array(pdev->dev.of_node, "qcom,chip-wakeup-reg",
  215. chip_wakeup_reg, count)) {
  216. if (of_property_read_u32_array(pdev->dev.of_node,
  217. "qcom,chip-wakeup-maskbit",
  218. gpio_data->chip_wakeup_maskbit, count)) {
  219. dev_err(&pdev->dev,
  220. "chip-wakeup-maskbit needed if chip-wakeup-reg is defined!\n");
  221. goto cdc_tlmm_gpio;
  222. }
  223. gpio_data->chip_wakeup_reg = true;
  224. for (i = 0; i < count; i++) {
  225. gpio_data->chip_wakeup_register[i] =
  226. devm_ioremap(&pdev->dev, chip_wakeup_reg[i], 0x4);
  227. }
  228. gpio_data->wakeup_reg_count = count;
  229. }
  230. cdc_tlmm_gpio:
  231. count = of_property_count_u32_elems(pdev->dev.of_node, "qcom,tlmm-gpio");
  232. if (count <= 0)
  233. goto cdc_rst;
  234. if (!of_property_read_u32_array(pdev->dev.of_node, "qcom,tlmm-gpio",
  235. tlmm_gpio, count)) {
  236. gpio_data->wakeup_capable = true;
  237. for (i = 0; i < count; i++)
  238. gpio_data->tlmm_gpio[i] = tlmm_gpio[i];
  239. gpio_data->count = count;
  240. }
  241. cdc_rst:
  242. gpio_data->gpio = of_get_named_gpio(pdev->dev.of_node,
  243. "qcom,cdc-rst-n-gpio", 0);
  244. if (gpio_is_valid(gpio_data->gpio)) {
  245. ret = gpio_request(gpio_data->gpio, "MSM_CDC_RESET");
  246. if (ret) {
  247. dev_err(&pdev->dev, "%s: Failed to request gpio %d\n",
  248. __func__, gpio_data->gpio);
  249. goto err_lookup_state;
  250. }
  251. }
  252. dev_set_drvdata(&pdev->dev, gpio_data);
  253. return 0;
  254. err_lookup_state:
  255. devm_pinctrl_put(gpio_data->pinctrl);
  256. err_pctrl_get:
  257. devm_kfree(&pdev->dev, gpio_data);
  258. return ret;
  259. }
  260. static int msm_cdc_pinctrl_remove(struct platform_device *pdev)
  261. {
  262. struct msm_cdc_pinctrl_info *gpio_data;
  263. gpio_data = dev_get_drvdata(&pdev->dev);
  264. /* to free the requested gpio before exiting */
  265. if (gpio_data) {
  266. if (gpio_is_valid(gpio_data->gpio))
  267. gpio_free(gpio_data->gpio);
  268. if (gpio_data->pinctrl)
  269. devm_pinctrl_put(gpio_data->pinctrl);
  270. }
  271. devm_kfree(&pdev->dev, gpio_data);
  272. return 0;
  273. }
  274. static const struct of_device_id msm_cdc_pinctrl_match[] = {
  275. {.compatible = "qcom,msm-cdc-pinctrl"},
  276. {}
  277. };
  278. static struct platform_driver msm_cdc_pinctrl_driver = {
  279. .driver = {
  280. .name = "msm-cdc-pinctrl",
  281. .owner = THIS_MODULE,
  282. .of_match_table = msm_cdc_pinctrl_match,
  283. .suppress_bind_attrs = true,
  284. },
  285. .probe = msm_cdc_pinctrl_probe,
  286. .remove = msm_cdc_pinctrl_remove,
  287. };
  288. int msm_cdc_pinctrl_drv_init(void)
  289. {
  290. return platform_driver_register(&msm_cdc_pinctrl_driver);
  291. }
  292. void msm_cdc_pinctrl_drv_exit(void)
  293. {
  294. platform_driver_unregister(&msm_cdc_pinctrl_driver);
  295. }
  296. MODULE_DESCRIPTION("MSM CODEC pin control platform driver");
  297. MODULE_LICENSE("GPL v2");