msm-cdc-pinctrl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 chip_wakeup_default_val[MAX_GPIOS] = {0};
  173. u32 i = 0, temp = 0;
  174. int count = 0;
  175. gpio_data = devm_kzalloc(&pdev->dev,
  176. sizeof(struct msm_cdc_pinctrl_info),
  177. GFP_KERNEL);
  178. if (!gpio_data)
  179. return -ENOMEM;
  180. gpio_data->pinctrl = devm_pinctrl_get(&pdev->dev);
  181. if (IS_ERR_OR_NULL(gpio_data->pinctrl)) {
  182. dev_err(&pdev->dev, "%s: Cannot get cdc gpio pinctrl:%ld\n",
  183. __func__, PTR_ERR(gpio_data->pinctrl));
  184. ret = PTR_ERR(gpio_data->pinctrl);
  185. goto err_pctrl_get;
  186. }
  187. gpio_data->pinctrl_active = pinctrl_lookup_state(
  188. gpio_data->pinctrl, "aud_active");
  189. if (IS_ERR_OR_NULL(gpio_data->pinctrl_active)) {
  190. dev_err(&pdev->dev, "%s: Cannot get aud_active pinctrl state:%ld\n",
  191. __func__, PTR_ERR(gpio_data->pinctrl_active));
  192. ret = PTR_ERR(gpio_data->pinctrl_active);
  193. goto err_lookup_state;
  194. }
  195. gpio_data->pinctrl_sleep = pinctrl_lookup_state(
  196. gpio_data->pinctrl, "aud_sleep");
  197. if (IS_ERR_OR_NULL(gpio_data->pinctrl_sleep)) {
  198. dev_err(&pdev->dev, "%s: Cannot get aud_sleep pinctrl state:%ld\n",
  199. __func__, PTR_ERR(gpio_data->pinctrl_sleep));
  200. ret = PTR_ERR(gpio_data->pinctrl_sleep);
  201. goto err_lookup_state;
  202. }
  203. /* skip setting to sleep state for LPI_TLMM GPIOs */
  204. if (!of_property_read_bool(pdev->dev.of_node, "qcom,lpi-gpios")) {
  205. /* Set pinctrl state to aud_sleep by default */
  206. ret = pinctrl_select_state(gpio_data->pinctrl,
  207. gpio_data->pinctrl_sleep);
  208. if (ret)
  209. dev_err(&pdev->dev, "%s: set cdc gpio sleep state fail: %d\n",
  210. __func__, ret);
  211. }
  212. count = of_property_count_u32_elems(pdev->dev.of_node, "qcom,chip-wakeup-reg");
  213. if (count <= 0)
  214. goto cdc_tlmm_gpio;
  215. if (!of_property_read_u32_array(pdev->dev.of_node, "qcom,chip-wakeup-reg",
  216. chip_wakeup_reg, count)) {
  217. if (of_property_read_u32_array(pdev->dev.of_node,
  218. "qcom,chip-wakeup-maskbit",
  219. gpio_data->chip_wakeup_maskbit, count)) {
  220. dev_err(&pdev->dev,
  221. "chip-wakeup-maskbit needed if chip-wakeup-reg is defined!\n");
  222. goto cdc_tlmm_gpio;
  223. }
  224. gpio_data->chip_wakeup_reg = true;
  225. for (i = 0; i < count; i++) {
  226. gpio_data->chip_wakeup_register[i] =
  227. devm_ioremap(&pdev->dev, chip_wakeup_reg[i], 0x4);
  228. }
  229. if (!of_property_read_u32_array(pdev->dev.of_node,
  230. "qcom,chip-wakeup-default-val",
  231. chip_wakeup_default_val, count)) {
  232. for (i = 0; i < count; i++) {
  233. temp = ioread32(gpio_data->chip_wakeup_register[i]);
  234. if (chip_wakeup_default_val[i])
  235. temp |= (1 <<
  236. gpio_data->chip_wakeup_maskbit[i]);
  237. else
  238. temp &= ~(1 <<
  239. gpio_data->chip_wakeup_maskbit[i]);
  240. iowrite32(temp, gpio_data->chip_wakeup_register[i]);
  241. }
  242. }
  243. gpio_data->wakeup_reg_count = count;
  244. }
  245. cdc_tlmm_gpio:
  246. count = of_property_count_u32_elems(pdev->dev.of_node, "qcom,tlmm-pins");
  247. if (count <= 0)
  248. goto cdc_rst;
  249. if (!of_property_read_u32_array(pdev->dev.of_node, "qcom,tlmm-pins",
  250. tlmm_gpio, count)) {
  251. gpio_data->wakeup_capable = true;
  252. for (i = 0; i < count; i++)
  253. gpio_data->tlmm_gpio[i] = tlmm_gpio[i];
  254. gpio_data->count = count;
  255. }
  256. cdc_rst:
  257. gpio_data->gpio = of_get_named_gpio(pdev->dev.of_node,
  258. "qcom,cdc-rst-n-gpio", 0);
  259. if (gpio_is_valid(gpio_data->gpio)) {
  260. ret = gpio_request(gpio_data->gpio, "MSM_CDC_RESET");
  261. if (ret) {
  262. dev_err(&pdev->dev, "%s: Failed to request gpio %d\n",
  263. __func__, gpio_data->gpio);
  264. goto err_lookup_state;
  265. }
  266. }
  267. dev_set_drvdata(&pdev->dev, gpio_data);
  268. return 0;
  269. err_lookup_state:
  270. devm_pinctrl_put(gpio_data->pinctrl);
  271. err_pctrl_get:
  272. devm_kfree(&pdev->dev, gpio_data);
  273. return ret;
  274. }
  275. static int msm_cdc_pinctrl_remove(struct platform_device *pdev)
  276. {
  277. struct msm_cdc_pinctrl_info *gpio_data;
  278. gpio_data = dev_get_drvdata(&pdev->dev);
  279. /* to free the requested gpio before exiting */
  280. if (gpio_data) {
  281. if (gpio_is_valid(gpio_data->gpio))
  282. gpio_free(gpio_data->gpio);
  283. if (gpio_data->pinctrl)
  284. devm_pinctrl_put(gpio_data->pinctrl);
  285. }
  286. devm_kfree(&pdev->dev, gpio_data);
  287. return 0;
  288. }
  289. static const struct of_device_id msm_cdc_pinctrl_match[] = {
  290. {.compatible = "qcom,msm-cdc-pinctrl"},
  291. {}
  292. };
  293. static struct platform_driver msm_cdc_pinctrl_driver = {
  294. .driver = {
  295. .name = "msm-cdc-pinctrl",
  296. .owner = THIS_MODULE,
  297. .of_match_table = msm_cdc_pinctrl_match,
  298. .suppress_bind_attrs = true,
  299. },
  300. .probe = msm_cdc_pinctrl_probe,
  301. .remove = msm_cdc_pinctrl_remove,
  302. };
  303. int msm_cdc_pinctrl_drv_init(void)
  304. {
  305. return platform_driver_register(&msm_cdc_pinctrl_driver);
  306. }
  307. void msm_cdc_pinctrl_drv_exit(void)
  308. {
  309. platform_driver_unregister(&msm_cdc_pinctrl_driver);
  310. }
  311. MODULE_DESCRIPTION("MSM CODEC pin control platform driver");
  312. MODULE_LICENSE("GPL v2");