powergate-bpmp.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /*
  2. * Copyright (c) 2016-2017, NVIDIA CORPORATION. All rights reserved
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. */
  13. #include <linux/of.h>
  14. #include <linux/platform_device.h>
  15. #include <linux/pm_domain.h>
  16. #include <linux/slab.h>
  17. #include <linux/version.h>
  18. #include <soc/tegra/bpmp.h>
  19. #include <soc/tegra/bpmp-abi.h>
  20. struct tegra_powergate_info {
  21. unsigned int id;
  22. char *name;
  23. };
  24. struct tegra_powergate {
  25. struct generic_pm_domain genpd;
  26. struct tegra_bpmp *bpmp;
  27. unsigned int id;
  28. };
  29. static inline struct tegra_powergate *
  30. to_tegra_powergate(struct generic_pm_domain *genpd)
  31. {
  32. return container_of(genpd, struct tegra_powergate, genpd);
  33. }
  34. static int tegra_bpmp_powergate_set_state(struct tegra_bpmp *bpmp,
  35. unsigned int id, u32 state)
  36. {
  37. struct mrq_pg_request request;
  38. struct tegra_bpmp_message msg;
  39. int err;
  40. memset(&request, 0, sizeof(request));
  41. request.cmd = CMD_PG_SET_STATE;
  42. request.id = id;
  43. request.set_state.state = state;
  44. memset(&msg, 0, sizeof(msg));
  45. msg.mrq = MRQ_PG;
  46. msg.tx.data = &request;
  47. msg.tx.size = sizeof(request);
  48. err = tegra_bpmp_transfer(bpmp, &msg);
  49. if (err < 0)
  50. return err;
  51. else if (msg.rx.ret < 0)
  52. return -EINVAL;
  53. return 0;
  54. }
  55. static int tegra_bpmp_powergate_get_state(struct tegra_bpmp *bpmp,
  56. unsigned int id)
  57. {
  58. struct mrq_pg_response response;
  59. struct mrq_pg_request request;
  60. struct tegra_bpmp_message msg;
  61. int err;
  62. memset(&request, 0, sizeof(request));
  63. request.cmd = CMD_PG_GET_STATE;
  64. request.id = id;
  65. memset(&response, 0, sizeof(response));
  66. memset(&msg, 0, sizeof(msg));
  67. msg.mrq = MRQ_PG;
  68. msg.tx.data = &request;
  69. msg.tx.size = sizeof(request);
  70. msg.rx.data = &response;
  71. msg.rx.size = sizeof(response);
  72. err = tegra_bpmp_transfer(bpmp, &msg);
  73. if (err < 0)
  74. return PG_STATE_OFF;
  75. else if (msg.rx.ret < 0)
  76. return -EINVAL;
  77. return response.get_state.state;
  78. }
  79. static int tegra_bpmp_powergate_get_max_id(struct tegra_bpmp *bpmp)
  80. {
  81. struct mrq_pg_response response;
  82. struct mrq_pg_request request;
  83. struct tegra_bpmp_message msg;
  84. int err;
  85. memset(&request, 0, sizeof(request));
  86. request.cmd = CMD_PG_GET_MAX_ID;
  87. memset(&response, 0, sizeof(response));
  88. memset(&msg, 0, sizeof(msg));
  89. msg.mrq = MRQ_PG;
  90. msg.tx.data = &request;
  91. msg.tx.size = sizeof(request);
  92. msg.rx.data = &response;
  93. msg.rx.size = sizeof(response);
  94. err = tegra_bpmp_transfer(bpmp, &msg);
  95. if (err < 0)
  96. return err;
  97. else if (msg.rx.ret < 0)
  98. return -EINVAL;
  99. return response.get_max_id.max_id;
  100. }
  101. static char *tegra_bpmp_powergate_get_name(struct tegra_bpmp *bpmp,
  102. unsigned int id)
  103. {
  104. struct mrq_pg_response response;
  105. struct mrq_pg_request request;
  106. struct tegra_bpmp_message msg;
  107. int err;
  108. memset(&request, 0, sizeof(request));
  109. request.cmd = CMD_PG_GET_NAME;
  110. request.id = id;
  111. memset(&response, 0, sizeof(response));
  112. memset(&msg, 0, sizeof(msg));
  113. msg.mrq = MRQ_PG;
  114. msg.tx.data = &request;
  115. msg.tx.size = sizeof(request);
  116. msg.rx.data = &response;
  117. msg.rx.size = sizeof(response);
  118. err = tegra_bpmp_transfer(bpmp, &msg);
  119. if (err < 0 || msg.rx.ret < 0)
  120. return NULL;
  121. return kstrdup(response.get_name.name, GFP_KERNEL);
  122. }
  123. static inline bool tegra_bpmp_powergate_is_powered(struct tegra_bpmp *bpmp,
  124. unsigned int id)
  125. {
  126. return tegra_bpmp_powergate_get_state(bpmp, id) != PG_STATE_OFF;
  127. }
  128. static int tegra_powergate_power_on(struct generic_pm_domain *domain)
  129. {
  130. struct tegra_powergate *powergate = to_tegra_powergate(domain);
  131. struct tegra_bpmp *bpmp = powergate->bpmp;
  132. return tegra_bpmp_powergate_set_state(bpmp, powergate->id,
  133. PG_STATE_ON);
  134. }
  135. static int tegra_powergate_power_off(struct generic_pm_domain *domain)
  136. {
  137. struct tegra_powergate *powergate = to_tegra_powergate(domain);
  138. struct tegra_bpmp *bpmp = powergate->bpmp;
  139. return tegra_bpmp_powergate_set_state(bpmp, powergate->id,
  140. PG_STATE_OFF);
  141. }
  142. static struct tegra_powergate *
  143. tegra_powergate_add(struct tegra_bpmp *bpmp,
  144. const struct tegra_powergate_info *info)
  145. {
  146. struct tegra_powergate *powergate;
  147. bool off;
  148. int err;
  149. off = !tegra_bpmp_powergate_is_powered(bpmp, info->id);
  150. powergate = devm_kzalloc(bpmp->dev, sizeof(*powergate), GFP_KERNEL);
  151. if (!powergate)
  152. return ERR_PTR(-ENOMEM);
  153. powergate->id = info->id;
  154. powergate->bpmp = bpmp;
  155. powergate->genpd.name = kstrdup(info->name, GFP_KERNEL);
  156. powergate->genpd.power_on = tegra_powergate_power_on;
  157. powergate->genpd.power_off = tegra_powergate_power_off;
  158. err = pm_genpd_init(&powergate->genpd, NULL, off);
  159. if (err < 0) {
  160. kfree(powergate->genpd.name);
  161. return ERR_PTR(err);
  162. }
  163. return powergate;
  164. }
  165. static void tegra_powergate_remove(struct tegra_powergate *powergate)
  166. {
  167. struct generic_pm_domain *genpd = &powergate->genpd;
  168. struct tegra_bpmp *bpmp = powergate->bpmp;
  169. int err;
  170. err = pm_genpd_remove(genpd);
  171. if (err < 0)
  172. dev_err(bpmp->dev, "failed to remove power domain %s: %d\n",
  173. genpd->name, err);
  174. kfree(genpd->name);
  175. }
  176. static int
  177. tegra_bpmp_probe_powergates(struct tegra_bpmp *bpmp,
  178. struct tegra_powergate_info **powergatesp)
  179. {
  180. struct tegra_powergate_info *powergates;
  181. unsigned int max_id, id, count = 0;
  182. unsigned int num_holes = 0;
  183. int err;
  184. err = tegra_bpmp_powergate_get_max_id(bpmp);
  185. if (err < 0)
  186. return err;
  187. max_id = err;
  188. dev_dbg(bpmp->dev, "maximum powergate ID: %u\n", max_id);
  189. powergates = kcalloc(max_id + 1, sizeof(*powergates), GFP_KERNEL);
  190. if (!powergates)
  191. return -ENOMEM;
  192. for (id = 0; id <= max_id; id++) {
  193. struct tegra_powergate_info *info = &powergates[count];
  194. info->name = tegra_bpmp_powergate_get_name(bpmp, id);
  195. if (!info->name || info->name[0] == '\0') {
  196. num_holes++;
  197. continue;
  198. }
  199. info->id = id;
  200. count++;
  201. }
  202. dev_dbg(bpmp->dev, "holes: %u\n", num_holes);
  203. *powergatesp = powergates;
  204. return count;
  205. }
  206. static int tegra_bpmp_add_powergates(struct tegra_bpmp *bpmp,
  207. struct tegra_powergate_info *powergates,
  208. unsigned int count)
  209. {
  210. struct genpd_onecell_data *genpd = &bpmp->genpd;
  211. struct generic_pm_domain **domains;
  212. struct tegra_powergate *powergate;
  213. unsigned int i;
  214. int err;
  215. domains = kcalloc(count, sizeof(*domains), GFP_KERNEL);
  216. if (!domains)
  217. return -ENOMEM;
  218. for (i = 0; i < count; i++) {
  219. powergate = tegra_powergate_add(bpmp, &powergates[i]);
  220. if (IS_ERR(powergate)) {
  221. err = PTR_ERR(powergate);
  222. goto remove;
  223. }
  224. dev_dbg(bpmp->dev, "added power domain %s\n",
  225. powergate->genpd.name);
  226. domains[i] = &powergate->genpd;
  227. }
  228. genpd->num_domains = count;
  229. genpd->domains = domains;
  230. return 0;
  231. remove:
  232. while (i--) {
  233. powergate = to_tegra_powergate(domains[i]);
  234. tegra_powergate_remove(powergate);
  235. }
  236. kfree(genpd->domains);
  237. return err;
  238. }
  239. static void tegra_bpmp_remove_powergates(struct tegra_bpmp *bpmp)
  240. {
  241. struct genpd_onecell_data *genpd = &bpmp->genpd;
  242. unsigned int i = genpd->num_domains;
  243. struct tegra_powergate *powergate;
  244. while (i--) {
  245. dev_dbg(bpmp->dev, "removing power domain %s\n",
  246. genpd->domains[i]->name);
  247. powergate = to_tegra_powergate(genpd->domains[i]);
  248. tegra_powergate_remove(powergate);
  249. }
  250. }
  251. static struct generic_pm_domain *
  252. tegra_powergate_xlate(struct of_phandle_args *spec, void *data)
  253. {
  254. struct generic_pm_domain *domain = ERR_PTR(-ENOENT);
  255. struct genpd_onecell_data *genpd = data;
  256. unsigned int i;
  257. for (i = 0; i < genpd->num_domains; i++) {
  258. struct tegra_powergate *powergate;
  259. powergate = to_tegra_powergate(genpd->domains[i]);
  260. if (powergate->id == spec->args[0]) {
  261. domain = &powergate->genpd;
  262. break;
  263. }
  264. }
  265. return domain;
  266. }
  267. int tegra_bpmp_init_powergates(struct tegra_bpmp *bpmp)
  268. {
  269. struct device_node *np = bpmp->dev->of_node;
  270. struct tegra_powergate_info *powergates;
  271. struct device *dev = bpmp->dev;
  272. unsigned int count, i;
  273. int err;
  274. err = tegra_bpmp_probe_powergates(bpmp, &powergates);
  275. if (err < 0)
  276. return err;
  277. count = err;
  278. dev_dbg(dev, "%u power domains probed\n", count);
  279. err = tegra_bpmp_add_powergates(bpmp, powergates, count);
  280. if (err < 0)
  281. goto free;
  282. bpmp->genpd.xlate = tegra_powergate_xlate;
  283. err = of_genpd_add_provider_onecell(np, &bpmp->genpd);
  284. if (err < 0) {
  285. dev_err(dev, "failed to add power domain provider: %d\n", err);
  286. tegra_bpmp_remove_powergates(bpmp);
  287. }
  288. free:
  289. for (i = 0; i < count; i++)
  290. kfree(powergates[i].name);
  291. kfree(powergates);
  292. return err;
  293. }