sde_rotator_io_util.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2012, 2015-2020, The Linux Foundation. All rights reserved.
  4. */
  5. #define pr_fmt(fmt) "%s: " fmt, __func__
  6. #include <linux/clk.h>
  7. #include <linux/err.h>
  8. #include <linux/io.h>
  9. #include <linux/regulator/consumer.h>
  10. #include <linux/delay.h>
  11. #include "sde_rotator_io_util.h"
  12. void sde_reg_w(struct sde_io_data *io, u32 offset, u32 value, u32 debug)
  13. {
  14. u32 in_val;
  15. if (!io || !io->base) {
  16. DEV_ERR("%pS->%s: invalid input\n",
  17. __builtin_return_address(0), __func__);
  18. return;
  19. }
  20. if (offset > io->len) {
  21. DEV_ERR("%pS->%s: offset out of range\n",
  22. __builtin_return_address(0), __func__);
  23. return;
  24. }
  25. DEV_DBG("sdeio:%6.6x:%8.8x\n", offset, value);
  26. writel_relaxed(value, io->base + offset);
  27. if (debug) {
  28. /* ensure register read is ordered after register write */
  29. mb();
  30. in_val = readl_relaxed(io->base + offset);
  31. DEV_DBG("[%08x] => %08x [%08x]\n",
  32. (u32)(unsigned long)(io->base + offset),
  33. value, in_val);
  34. }
  35. } /* sde_reg_w */
  36. u32 sde_reg_r(struct sde_io_data *io, u32 offset, u32 debug)
  37. {
  38. u32 value;
  39. if (!io || !io->base) {
  40. DEV_ERR("%pS->%s: invalid input\n",
  41. __builtin_return_address(0), __func__);
  42. return -EINVAL;
  43. }
  44. if (offset > io->len) {
  45. DEV_ERR("%pS->%s: offset out of range\n",
  46. __builtin_return_address(0), __func__);
  47. return -EINVAL;
  48. }
  49. value = readl_relaxed(io->base + offset);
  50. if (debug)
  51. DEV_DBG("[%08x] <= %08x\n",
  52. (u32)(unsigned long)(io->base + offset), value);
  53. DEV_DBG("sdeio:%6.6x:%8.8x\n", offset, value);
  54. return value;
  55. } /* sde_reg_r */
  56. void sde_reg_dump(void __iomem *base, u32 length, const char *prefix,
  57. u32 debug)
  58. {
  59. if (debug)
  60. print_hex_dump(KERN_INFO, prefix, DUMP_PREFIX_OFFSET, 32, 4,
  61. (void *)base, length, false);
  62. } /* sde_reg_dump */
  63. static struct resource *sde_rot_get_res_byname(struct platform_device *pdev,
  64. unsigned int type, const char *name)
  65. {
  66. struct resource *res = NULL;
  67. res = platform_get_resource_byname(pdev, type, name);
  68. if (!res)
  69. DEV_ERR("%s: '%s' resource not found\n", __func__, name);
  70. return res;
  71. } /* sde_rot_get_res_byname */
  72. int sde_rot_ioremap_byname(struct platform_device *pdev,
  73. struct sde_io_data *io_data, const char *name)
  74. {
  75. struct resource *res = NULL;
  76. if (!pdev || !io_data) {
  77. DEV_ERR("%pS->%s: invalid input\n",
  78. __builtin_return_address(0), __func__);
  79. return -EINVAL;
  80. }
  81. res = sde_rot_get_res_byname(pdev, IORESOURCE_MEM, name);
  82. if (!res) {
  83. DEV_ERR("%pS->%s: '%s' sde_rot_get_res_byname failed\n",
  84. __builtin_return_address(0), __func__, name);
  85. return -ENODEV;
  86. }
  87. io_data->len = (u32)resource_size(res);
  88. io_data->base = ioremap(res->start, io_data->len);
  89. if (!io_data->base) {
  90. DEV_ERR("%pS->%s: '%s' ioremap failed\n",
  91. __builtin_return_address(0), __func__, name);
  92. return -EIO;
  93. }
  94. return 0;
  95. } /* sde_rot_ioremap_byname */
  96. void sde_rot_iounmap(struct sde_io_data *io_data)
  97. {
  98. if (!io_data) {
  99. DEV_ERR("%pS->%s: invalid input\n",
  100. __builtin_return_address(0), __func__);
  101. return;
  102. }
  103. if (io_data->base) {
  104. iounmap(io_data->base);
  105. io_data->base = NULL;
  106. }
  107. io_data->len = 0;
  108. } /* sde_rot_iounmap */
  109. int sde_rot_config_vreg(struct device *dev, struct sde_vreg *in_vreg,
  110. int num_vreg, int config)
  111. {
  112. int i = 0, rc = 0;
  113. struct sde_vreg *curr_vreg = NULL;
  114. enum sde_vreg_type type;
  115. if (!dev || !in_vreg || !num_vreg) {
  116. DEV_ERR("%pS->%s: invalid input\n",
  117. __builtin_return_address(0), __func__);
  118. return -EINVAL;
  119. }
  120. if (config) {
  121. for (i = 0; i < num_vreg; i++) {
  122. curr_vreg = &in_vreg[i];
  123. curr_vreg->vreg = regulator_get(dev,
  124. curr_vreg->vreg_name);
  125. rc = PTR_RET(curr_vreg->vreg);
  126. if (rc) {
  127. DEV_ERR("%pS->%s: %s get failed. rc=%d\n",
  128. __builtin_return_address(0), __func__,
  129. curr_vreg->vreg_name, rc);
  130. curr_vreg->vreg = NULL;
  131. goto vreg_get_fail;
  132. }
  133. type = (regulator_count_voltages(curr_vreg->vreg) > 0)
  134. ? SDE_REG_LDO : SDE_REG_VS;
  135. if (type == SDE_REG_LDO) {
  136. rc = regulator_set_voltage(
  137. curr_vreg->vreg,
  138. curr_vreg->min_voltage,
  139. curr_vreg->max_voltage);
  140. if (rc < 0) {
  141. DEV_ERR("%pS->%s: %s set vltg fail\n",
  142. __builtin_return_address(0),
  143. __func__,
  144. curr_vreg->vreg_name);
  145. goto vreg_set_voltage_fail;
  146. }
  147. }
  148. }
  149. } else {
  150. for (i = num_vreg-1; i >= 0; i--) {
  151. curr_vreg = &in_vreg[i];
  152. if (curr_vreg->vreg) {
  153. type = (regulator_count_voltages(
  154. curr_vreg->vreg) > 0)
  155. ? SDE_REG_LDO : SDE_REG_VS;
  156. if (type == SDE_REG_LDO) {
  157. regulator_set_voltage(curr_vreg->vreg,
  158. 0, curr_vreg->max_voltage);
  159. }
  160. regulator_put(curr_vreg->vreg);
  161. curr_vreg->vreg = NULL;
  162. }
  163. }
  164. }
  165. return 0;
  166. vreg_unconfig:
  167. if (type == SDE_REG_LDO)
  168. regulator_set_load(curr_vreg->vreg, 0);
  169. vreg_set_voltage_fail:
  170. regulator_put(curr_vreg->vreg);
  171. curr_vreg->vreg = NULL;
  172. vreg_get_fail:
  173. for (i--; i >= 0; i--) {
  174. curr_vreg = &in_vreg[i];
  175. type = (regulator_count_voltages(curr_vreg->vreg) > 0)
  176. ? SDE_REG_LDO : SDE_REG_VS;
  177. goto vreg_unconfig;
  178. }
  179. return rc;
  180. } /* sde_rot_config_vreg */
  181. int sde_rot_enable_vreg(struct sde_vreg *in_vreg, int num_vreg, int enable)
  182. {
  183. int i = 0, rc = 0;
  184. bool need_sleep;
  185. if (!in_vreg) {
  186. DEV_ERR("%pS->%s: invalid input\n",
  187. __builtin_return_address(0), __func__);
  188. return -EINVAL;
  189. }
  190. if (enable) {
  191. for (i = 0; i < num_vreg; i++) {
  192. rc = PTR_RET(in_vreg[i].vreg);
  193. if (rc) {
  194. DEV_ERR("%pS->%s: %s regulator error. rc=%d\n",
  195. __builtin_return_address(0), __func__,
  196. in_vreg[i].vreg_name, rc);
  197. goto vreg_set_opt_mode_fail;
  198. }
  199. need_sleep = !regulator_is_enabled(in_vreg[i].vreg);
  200. if (in_vreg[i].pre_on_sleep && need_sleep)
  201. usleep_range(in_vreg[i].pre_on_sleep * 1000,
  202. in_vreg[i].pre_on_sleep * 1000);
  203. rc = regulator_set_load(in_vreg[i].vreg,
  204. in_vreg[i].enable_load);
  205. if (rc < 0) {
  206. DEV_ERR("%pS->%s: %s set opt m fail\n",
  207. __builtin_return_address(0), __func__,
  208. in_vreg[i].vreg_name);
  209. goto vreg_set_opt_mode_fail;
  210. }
  211. rc = regulator_enable(in_vreg[i].vreg);
  212. if (in_vreg[i].post_on_sleep && need_sleep)
  213. usleep_range(in_vreg[i].post_on_sleep * 1000,
  214. in_vreg[i].post_on_sleep * 1000);
  215. if (rc < 0) {
  216. DEV_ERR("%pS->%s: %s enable failed\n",
  217. __builtin_return_address(0), __func__,
  218. in_vreg[i].vreg_name);
  219. goto disable_vreg;
  220. }
  221. }
  222. } else {
  223. for (i = num_vreg-1; i >= 0; i--) {
  224. if (in_vreg[i].pre_off_sleep)
  225. usleep_range(in_vreg[i].pre_off_sleep * 1000,
  226. in_vreg[i].pre_off_sleep * 1000);
  227. regulator_disable(in_vreg[i].vreg);
  228. if (in_vreg[i].post_off_sleep)
  229. usleep_range(in_vreg[i].post_off_sleep * 1000,
  230. in_vreg[i].post_off_sleep * 1000);
  231. regulator_set_load(in_vreg[i].vreg,
  232. in_vreg[i].disable_load);
  233. }
  234. }
  235. return rc;
  236. disable_vreg:
  237. regulator_set_load(in_vreg[i].vreg, in_vreg[i].disable_load);
  238. vreg_set_opt_mode_fail:
  239. for (i--; i >= 0; i--) {
  240. if (in_vreg[i].pre_off_sleep)
  241. usleep_range(in_vreg[i].pre_off_sleep * 1000,
  242. in_vreg[i].pre_off_sleep * 1000);
  243. regulator_disable(in_vreg[i].vreg);
  244. if (in_vreg[i].post_off_sleep)
  245. usleep_range(in_vreg[i].post_off_sleep * 1000,
  246. in_vreg[i].post_off_sleep * 1000);
  247. regulator_set_load(in_vreg[i].vreg,
  248. in_vreg[i].disable_load);
  249. }
  250. return rc;
  251. } /* sde_rot_enable_vreg */
  252. void sde_rot_put_clk(struct sde_clk *clk_arry, int num_clk)
  253. {
  254. int i;
  255. if (!clk_arry) {
  256. DEV_ERR("%pS->%s: invalid input\n",
  257. __builtin_return_address(0), __func__);
  258. return;
  259. }
  260. for (i = num_clk - 1; i >= 0; i--) {
  261. if (clk_arry[i].clk)
  262. clk_put(clk_arry[i].clk);
  263. clk_arry[i].clk = NULL;
  264. }
  265. } /* sde_rot_put_clk */
  266. int sde_rot_get_clk(struct device *dev, struct sde_clk *clk_arry, int num_clk)
  267. {
  268. int i, rc = 0;
  269. if (!dev || !clk_arry) {
  270. DEV_ERR("%pS->%s: invalid input\n",
  271. __builtin_return_address(0), __func__);
  272. return -EINVAL;
  273. }
  274. for (i = 0; i < num_clk; i++) {
  275. clk_arry[i].clk = clk_get(dev, clk_arry[i].clk_name);
  276. rc = PTR_RET(clk_arry[i].clk);
  277. if (rc) {
  278. DEV_ERR("%pS->%s: '%s' get failed. rc=%d\n",
  279. __builtin_return_address(0), __func__,
  280. clk_arry[i].clk_name, rc);
  281. goto error;
  282. }
  283. }
  284. return rc;
  285. error:
  286. sde_rot_put_clk(clk_arry, num_clk);
  287. return rc;
  288. } /* sde_rot_get_clk */
  289. int sde_rot_clk_set_rate(struct sde_clk *clk_arry, int num_clk)
  290. {
  291. int i, rc = 0;
  292. if (!clk_arry) {
  293. DEV_ERR("%pS->%s: invalid input\n",
  294. __builtin_return_address(0), __func__);
  295. return -EINVAL;
  296. }
  297. for (i = 0; i < num_clk; i++) {
  298. if (clk_arry[i].clk) {
  299. if (clk_arry[i].type != SDE_CLK_AHB) {
  300. DEV_DBG("%pS->%s: '%s' rate %ld\n",
  301. __builtin_return_address(0), __func__,
  302. clk_arry[i].clk_name,
  303. clk_arry[i].rate);
  304. rc = clk_set_rate(clk_arry[i].clk,
  305. clk_arry[i].rate);
  306. if (rc) {
  307. DEV_ERR("%pS->%s: %s failed. rc=%d\n",
  308. __builtin_return_address(0),
  309. __func__,
  310. clk_arry[i].clk_name, rc);
  311. break;
  312. }
  313. }
  314. } else {
  315. DEV_ERR("%pS->%s: '%s' is not available\n",
  316. __builtin_return_address(0), __func__,
  317. clk_arry[i].clk_name);
  318. rc = -EPERM;
  319. break;
  320. }
  321. }
  322. return rc;
  323. } /* sde_rot_clk_set_rate */
  324. int sde_rot_enable_clk(struct sde_clk *clk_arry, int num_clk, int enable)
  325. {
  326. int i, rc = 0;
  327. if (!clk_arry) {
  328. DEV_ERR("%pS->%s: invalid input\n",
  329. __builtin_return_address(0), __func__);
  330. return -EINVAL;
  331. }
  332. if (enable) {
  333. for (i = 0; i < num_clk; i++) {
  334. DEV_DBG("%pS->%s: enable '%s'\n",
  335. __builtin_return_address(0), __func__,
  336. clk_arry[i].clk_name);
  337. if (clk_arry[i].clk) {
  338. rc = clk_prepare_enable(clk_arry[i].clk);
  339. if (rc)
  340. DEV_ERR("%pS->%s: %s en fail. rc=%d\n",
  341. __builtin_return_address(0),
  342. __func__,
  343. clk_arry[i].clk_name, rc);
  344. } else {
  345. DEV_ERR("%pS->%s: '%s' is not available\n",
  346. __builtin_return_address(0), __func__,
  347. clk_arry[i].clk_name);
  348. rc = -EPERM;
  349. }
  350. if (rc) {
  351. sde_rot_enable_clk(&clk_arry[i],
  352. i, false);
  353. break;
  354. }
  355. }
  356. } else {
  357. for (i = num_clk - 1; i >= 0; i--) {
  358. DEV_DBG("%pS->%s: disable '%s'\n",
  359. __builtin_return_address(0), __func__,
  360. clk_arry[i].clk_name);
  361. if (clk_arry[i].clk)
  362. clk_disable_unprepare(clk_arry[i].clk);
  363. else
  364. DEV_ERR("%pS->%s: '%s' is not available\n",
  365. __builtin_return_address(0), __func__,
  366. clk_arry[i].clk_name);
  367. }
  368. }
  369. return rc;
  370. } /* sde_rot_enable_clk */