0900-power-Add-Qualcomm-APM.patch 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. From 6c98adf98236b8644b8f5e1aa7af9f1a88ea2766 Mon Sep 17 00:00:00 2001
  2. From: Robert Marko <[email protected]>
  3. Date: Mon, 11 Apr 2022 14:38:08 +0200
  4. Subject: [PATCH] power: Add Qualcomm APM
  5. Add Qualcomm APM driver, which allows scaling cache and memory fabrics.
  6. Signed-off-by: Robert Marko <[email protected]>
  7. ---
  8. drivers/power/Kconfig | 1 +
  9. drivers/power/Makefile | 1 +
  10. drivers/power/qcom/Kconfig | 7 +
  11. drivers/power/qcom/Makefile | 1 +
  12. drivers/power/qcom/apm.c | 944 +++++++++++++++++++++++++++++++++
  13. include/linux/power/qcom/apm.h | 48 ++
  14. 6 files changed, 1002 insertions(+)
  15. create mode 100644 drivers/power/qcom/Kconfig
  16. create mode 100644 drivers/power/qcom/Makefile
  17. create mode 100644 drivers/power/qcom/apm.c
  18. create mode 100644 include/linux/power/qcom/apm.h
  19. --- a/drivers/power/Kconfig
  20. +++ b/drivers/power/Kconfig
  21. @@ -1,3 +1,4 @@
  22. # SPDX-License-Identifier: GPL-2.0-only
  23. source "drivers/power/reset/Kconfig"
  24. source "drivers/power/supply/Kconfig"
  25. +source "drivers/power/qcom/Kconfig"
  26. --- a/drivers/power/Makefile
  27. +++ b/drivers/power/Makefile
  28. @@ -1,3 +1,4 @@
  29. # SPDX-License-Identifier: GPL-2.0-only
  30. obj-$(CONFIG_POWER_RESET) += reset/
  31. obj-$(CONFIG_POWER_SUPPLY) += supply/
  32. +obj-$(CONFIG_QCOM_APM) += qcom/
  33. --- /dev/null
  34. +++ b/drivers/power/qcom/Kconfig
  35. @@ -0,0 +1,7 @@
  36. +config QCOM_APM
  37. + bool "Qualcomm Technologies Inc platform specific APM driver"
  38. + help
  39. + Platform specific driver to manage the power source of
  40. + memory arrays. Interfaces with regulator drivers to ensure
  41. + SRAM Vmin requirements are met across different performance
  42. + levels.
  43. --- /dev/null
  44. +++ b/drivers/power/qcom/Makefile
  45. @@ -0,0 +1 @@
  46. +obj-$(CONFIG_QCOM_APM) += apm.o
  47. --- /dev/null
  48. +++ b/drivers/power/qcom/apm.c
  49. @@ -0,0 +1,944 @@
  50. +/*
  51. + * Copyright (c) 2015-2016, The Linux Foundation. All rights reserved.
  52. + *
  53. + * This program is free software; you can redistribute it and/or modify
  54. + * it under the terms of the GNU General Public License version 2 and
  55. + * only version 2 as published by the Free Software Foundation.
  56. + *
  57. + * This program is distributed in the hope that it will be useful,
  58. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  59. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  60. + * GNU General Public License for more details.
  61. + */
  62. +
  63. +#define pr_fmt(fmt) "%s: " fmt, __func__
  64. +
  65. +#include <linux/debugfs.h>
  66. +#include <linux/delay.h>
  67. +#include <linux/of_device.h>
  68. +#include <linux/init.h>
  69. +#include <linux/io.h>
  70. +#include <linux/kernel.h>
  71. +#include <linux/list.h>
  72. +#include <linux/module.h>
  73. +#include <linux/of.h>
  74. +#include <linux/platform_device.h>
  75. +#include <linux/slab.h>
  76. +#include <linux/string.h>
  77. +#include <linux/power/qcom/apm.h>
  78. +
  79. +/*
  80. + * VDD_APCC
  81. + * =============================================================
  82. + * | VDD_MX | |
  83. + * | ==========================|============= |
  84. + * ___|___ ___|___ ___|___ ___|___ ___|___ ___|___
  85. + * | | | | | | | | | | | |
  86. + * | APCC | | MX HS | | MX HS | | APCC | | MX HS | | APCC |
  87. + * | HS | | | | | | HS | | | | HS |
  88. + * |_______| |_______| |_______| |_______| |_______| |_______|
  89. + * |_________| |_________| |__________|
  90. + * | | |
  91. + * ______|_____ ______|_____ _______|_____
  92. + * | | | | | |
  93. + * | | | | | |
  94. + * | CPU MEM | | L2 MEM | | L3 MEM |
  95. + * | Arrays | | Arrays | | Arrays |
  96. + * | | | | | |
  97. + * |____________| |____________| |_____________|
  98. + *
  99. + */
  100. +
  101. +/* Register value definitions */
  102. +#define APCS_GFMUXA_SEL_VAL 0x13
  103. +#define APCS_GFMUXA_DESEL_VAL 0x03
  104. +#define MSM_APM_MX_MODE_VAL 0x00
  105. +#define MSM_APM_APCC_MODE_VAL 0x10
  106. +#define MSM_APM_MX_DONE_VAL 0x00
  107. +#define MSM_APM_APCC_DONE_VAL 0x03
  108. +#define MSM_APM_OVERRIDE_SEL_VAL 0xb0
  109. +#define MSM_APM_SEC_CLK_SEL_VAL 0x30
  110. +#define SPM_EVENT_SET_VAL 0x01
  111. +#define SPM_EVENT_CLEAR_VAL 0x00
  112. +
  113. +/* Register bit mask definitions */
  114. +#define MSM_APM_CTL_STS_MASK 0x0f
  115. +
  116. +/* Register offset definitions */
  117. +#define APCC_APM_MODE 0x00000098
  118. +#define APCC_APM_CTL_STS 0x000000a8
  119. +#define APCS_SPARE 0x00000068
  120. +#define APCS_VERSION 0x00000fd0
  121. +
  122. +#define HMSS_VERSION_1P2 0x10020000
  123. +
  124. +#define MSM_APM_SWITCH_TIMEOUT_US 10
  125. +#define SPM_WAKEUP_DELAY_US 2
  126. +#define SPM_EVENT_NUM 6
  127. +
  128. +#define MSM_APM_DRIVER_NAME "qcom,msm-apm"
  129. +
  130. +enum {
  131. + MSM8996_ID,
  132. + MSM8953_ID,
  133. + IPQ807x_ID,
  134. +};
  135. +
  136. +struct msm_apm_ctrl_dev {
  137. + struct list_head list;
  138. + struct device *dev;
  139. + enum msm_apm_supply supply;
  140. + spinlock_t lock;
  141. + void __iomem *reg_base;
  142. + void __iomem *apcs_csr_base;
  143. + void __iomem **apcs_spm_events_addr;
  144. + void __iomem *apc0_pll_ctl_addr;
  145. + void __iomem *apc1_pll_ctl_addr;
  146. + u32 version;
  147. + struct dentry *debugfs;
  148. + u32 msm_id;
  149. +};
  150. +
  151. +#if defined(CONFIG_DEBUG_FS)
  152. +static struct dentry *apm_debugfs_base;
  153. +#endif
  154. +
  155. +static DEFINE_MUTEX(apm_ctrl_list_mutex);
  156. +static LIST_HEAD(apm_ctrl_list);
  157. +
  158. +/*
  159. + * Get the resources associated with the APM controller from device tree
  160. + * and remap all I/O addresses that are relevant to this HW revision.
  161. + */
  162. +static int msm_apm_ctrl_devm_ioremap(struct platform_device *pdev,
  163. + struct msm_apm_ctrl_dev *ctrl)
  164. +{
  165. + struct device *dev = &pdev->dev;
  166. + struct resource *res;
  167. + static const char *res_name[SPM_EVENT_NUM] = {
  168. + "apc0-l2-spm",
  169. + "apc1-l2-spm",
  170. + "apc0-cpu0-spm",
  171. + "apc0-cpu1-spm",
  172. + "apc1-cpu0-spm",
  173. + "apc1-cpu1-spm"
  174. + };
  175. + int i, ret = 0;
  176. +
  177. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pm-apcc-glb");
  178. + if (!res) {
  179. + dev_err(dev, "Missing PM APCC Global register physical address");
  180. + return -EINVAL;
  181. + }
  182. + ctrl->reg_base = devm_ioremap(dev, res->start, resource_size(res));
  183. + if (!ctrl->reg_base) {
  184. + dev_err(dev, "Failed to map PM APCC Global registers\n");
  185. + return -ENOMEM;
  186. + }
  187. +
  188. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "apcs-csr");
  189. + if (!res) {
  190. + dev_err(dev, "Missing APCS CSR physical base address");
  191. + return -EINVAL;
  192. + }
  193. + ctrl->apcs_csr_base = devm_ioremap(dev, res->start, resource_size(res));
  194. + if (!ctrl->apcs_csr_base) {
  195. + dev_err(dev, "Failed to map APCS CSR registers\n");
  196. + return -ENOMEM;
  197. + }
  198. +
  199. + ctrl->version = readl_relaxed(ctrl->apcs_csr_base + APCS_VERSION);
  200. +
  201. + if (ctrl->version >= HMSS_VERSION_1P2)
  202. + return ret;
  203. +
  204. + ctrl->apcs_spm_events_addr = devm_kzalloc(&pdev->dev,
  205. + SPM_EVENT_NUM
  206. + * sizeof(void __iomem *),
  207. + GFP_KERNEL);
  208. + if (!ctrl->apcs_spm_events_addr) {
  209. + dev_err(dev, "Failed to allocate memory for APCS SPM event registers\n");
  210. + return -ENOMEM;
  211. + }
  212. +
  213. + for (i = 0; i < SPM_EVENT_NUM; i++) {
  214. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  215. + res_name[i]);
  216. + if (!res) {
  217. + dev_err(dev, "Missing address for %s\n", res_name[i]);
  218. + ret = -EINVAL;
  219. + goto free_events;
  220. + }
  221. +
  222. + ctrl->apcs_spm_events_addr[i] = devm_ioremap(dev, res->start,
  223. + resource_size(res));
  224. + if (!ctrl->apcs_spm_events_addr[i]) {
  225. + dev_err(dev, "Failed to map %s\n", res_name[i]);
  226. + ret = -ENOMEM;
  227. + goto free_events;
  228. + }
  229. +
  230. + dev_dbg(dev, "%s event phys: %pa virt:0x%p\n", res_name[i],
  231. + &res->start, ctrl->apcs_spm_events_addr[i]);
  232. + }
  233. +
  234. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  235. + "apc0-pll-ctl");
  236. + if (!res) {
  237. + dev_err(dev, "Missing APC0 PLL CTL physical address\n");
  238. + ret = -EINVAL;
  239. + goto free_events;
  240. + }
  241. +
  242. + ctrl->apc0_pll_ctl_addr = devm_ioremap(dev,
  243. + res->start,
  244. + resource_size(res));
  245. + if (!ctrl->apc0_pll_ctl_addr) {
  246. + dev_err(dev, "Failed to map APC0 PLL CTL register\n");
  247. + ret = -ENOMEM;
  248. + goto free_events;
  249. + }
  250. +
  251. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  252. + "apc1-pll-ctl");
  253. + if (!res) {
  254. + dev_err(dev, "Missing APC1 PLL CTL physical address\n");
  255. + ret = -EINVAL;
  256. + goto free_events;
  257. + }
  258. +
  259. + ctrl->apc1_pll_ctl_addr = devm_ioremap(dev,
  260. + res->start,
  261. + resource_size(res));
  262. + if (!ctrl->apc1_pll_ctl_addr) {
  263. + dev_err(dev, "Failed to map APC1 PLL CTL register\n");
  264. + ret = -ENOMEM;
  265. + goto free_events;
  266. + }
  267. +
  268. + return ret;
  269. +
  270. +free_events:
  271. + devm_kfree(dev, ctrl->apcs_spm_events_addr);
  272. + return ret;
  273. +}
  274. +
  275. +/* 8953 register offset definition */
  276. +#define MSM8953_APM_DLY_CNTR 0x2ac
  277. +
  278. +/* Register field shift definitions */
  279. +#define APM_CTL_SEL_SWITCH_DLY_SHIFT 0
  280. +#define APM_CTL_RESUME_CLK_DLY_SHIFT 8
  281. +#define APM_CTL_HALT_CLK_DLY_SHIFT 16
  282. +#define APM_CTL_POST_HALT_DLY_SHIFT 24
  283. +
  284. +/* Register field mask definitions */
  285. +#define APM_CTL_SEL_SWITCH_DLY_MASK GENMASK(7, 0)
  286. +#define APM_CTL_RESUME_CLK_DLY_MASK GENMASK(15, 8)
  287. +#define APM_CTL_HALT_CLK_DLY_MASK GENMASK(23, 16)
  288. +#define APM_CTL_POST_HALT_DLY_MASK GENMASK(31, 24)
  289. +
  290. +/*
  291. + * Get the resources associated with the msm8953 APM controller from
  292. + * device tree, remap all I/O addresses, and program the initial
  293. + * register configuration required for the 8953 APM controller device.
  294. + */
  295. +static int msm8953_apm_ctrl_init(struct platform_device *pdev,
  296. + struct msm_apm_ctrl_dev *ctrl)
  297. +{
  298. + struct device *dev = &pdev->dev;
  299. + struct resource *res;
  300. + u32 delay_counter, val = 0, regval = 0;
  301. + int rc = 0;
  302. +
  303. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pm-apcc-glb");
  304. + if (!res) {
  305. + dev_err(dev, "Missing PM APCC Global register physical address\n");
  306. + return -ENODEV;
  307. + }
  308. + ctrl->reg_base = devm_ioremap(dev, res->start, resource_size(res));
  309. + if (!ctrl->reg_base) {
  310. + dev_err(dev, "Failed to map PM APCC Global registers\n");
  311. + return -ENOMEM;
  312. + }
  313. +
  314. + /*
  315. + * Initial APM register configuration required before starting
  316. + * APM HW controller.
  317. + */
  318. + regval = readl_relaxed(ctrl->reg_base + MSM8953_APM_DLY_CNTR);
  319. + val = regval;
  320. +
  321. + if (of_find_property(dev->of_node, "qcom,apm-post-halt-delay", NULL)) {
  322. + rc = of_property_read_u32(dev->of_node,
  323. + "qcom,apm-post-halt-delay", &delay_counter);
  324. + if (rc < 0) {
  325. + dev_err(dev, "apm-post-halt-delay read failed, rc = %d",
  326. + rc);
  327. + return rc;
  328. + }
  329. +
  330. + val &= ~APM_CTL_POST_HALT_DLY_MASK;
  331. + val |= (delay_counter << APM_CTL_POST_HALT_DLY_SHIFT)
  332. + & APM_CTL_POST_HALT_DLY_MASK;
  333. + }
  334. +
  335. + if (of_find_property(dev->of_node, "qcom,apm-halt-clk-delay", NULL)) {
  336. + rc = of_property_read_u32(dev->of_node,
  337. + "qcom,apm-halt-clk-delay", &delay_counter);
  338. + if (rc < 0) {
  339. + dev_err(dev, "apm-halt-clk-delay read failed, rc = %d",
  340. + rc);
  341. + return rc;
  342. + }
  343. +
  344. + val &= ~APM_CTL_HALT_CLK_DLY_MASK;
  345. + val |= (delay_counter << APM_CTL_HALT_CLK_DLY_SHIFT)
  346. + & APM_CTL_HALT_CLK_DLY_MASK;
  347. + }
  348. +
  349. + if (of_find_property(dev->of_node, "qcom,apm-resume-clk-delay", NULL)) {
  350. + rc = of_property_read_u32(dev->of_node,
  351. + "qcom,apm-resume-clk-delay", &delay_counter);
  352. + if (rc < 0) {
  353. + dev_err(dev, "apm-resume-clk-delay read failed, rc = %d",
  354. + rc);
  355. + return rc;
  356. + }
  357. +
  358. + val &= ~APM_CTL_RESUME_CLK_DLY_MASK;
  359. + val |= (delay_counter << APM_CTL_RESUME_CLK_DLY_SHIFT)
  360. + & APM_CTL_RESUME_CLK_DLY_MASK;
  361. + }
  362. +
  363. + if (of_find_property(dev->of_node, "qcom,apm-sel-switch-delay", NULL)) {
  364. + rc = of_property_read_u32(dev->of_node,
  365. + "qcom,apm-sel-switch-delay", &delay_counter);
  366. + if (rc < 0) {
  367. + dev_err(dev, "apm-sel-switch-delay read failed, rc = %d",
  368. + rc);
  369. + return rc;
  370. + }
  371. +
  372. + val &= ~APM_CTL_SEL_SWITCH_DLY_MASK;
  373. + val |= (delay_counter << APM_CTL_SEL_SWITCH_DLY_SHIFT)
  374. + & APM_CTL_SEL_SWITCH_DLY_MASK;
  375. + }
  376. +
  377. + if (val != regval) {
  378. + writel_relaxed(val, ctrl->reg_base + MSM8953_APM_DLY_CNTR);
  379. + /* make sure write completes before return */
  380. + mb();
  381. + }
  382. +
  383. + return rc;
  384. +}
  385. +
  386. +static int msm8996_apm_switch_to_mx(struct msm_apm_ctrl_dev *ctrl_dev)
  387. +{
  388. + int i, timeout = MSM_APM_SWITCH_TIMEOUT_US;
  389. + u32 regval;
  390. + int ret = 0;
  391. + unsigned long flags;
  392. +
  393. + spin_lock_irqsave(&ctrl_dev->lock, flags);
  394. +
  395. + /* Perform revision-specific programming steps */
  396. + if (ctrl_dev->version < HMSS_VERSION_1P2) {
  397. + /* Clear SPM events */
  398. + for (i = 0; i < SPM_EVENT_NUM; i++)
  399. + writel_relaxed(SPM_EVENT_CLEAR_VAL,
  400. + ctrl_dev->apcs_spm_events_addr[i]);
  401. +
  402. + udelay(SPM_WAKEUP_DELAY_US);
  403. +
  404. + /* Switch APC/CBF to GPLL0 clock */
  405. + writel_relaxed(APCS_GFMUXA_SEL_VAL,
  406. + ctrl_dev->apcs_csr_base + APCS_SPARE);
  407. + ndelay(200);
  408. + writel_relaxed(MSM_APM_OVERRIDE_SEL_VAL,
  409. + ctrl_dev->apc0_pll_ctl_addr);
  410. + ndelay(200);
  411. + writel_relaxed(MSM_APM_OVERRIDE_SEL_VAL,
  412. + ctrl_dev->apc1_pll_ctl_addr);
  413. +
  414. + /* Ensure writes complete before proceeding */
  415. + mb();
  416. + }
  417. +
  418. + /* Switch arrays to MX supply and wait for its completion */
  419. + writel_relaxed(MSM_APM_MX_MODE_VAL, ctrl_dev->reg_base +
  420. + APCC_APM_MODE);
  421. +
  422. + /* Ensure write above completes before delaying */
  423. + mb();
  424. +
  425. + while (timeout > 0) {
  426. + regval = readl_relaxed(ctrl_dev->reg_base + APCC_APM_CTL_STS);
  427. + if ((regval & MSM_APM_CTL_STS_MASK) ==
  428. + MSM_APM_MX_DONE_VAL)
  429. + break;
  430. +
  431. + udelay(1);
  432. + timeout--;
  433. + }
  434. +
  435. + if (timeout == 0) {
  436. + ret = -ETIMEDOUT;
  437. + dev_err(ctrl_dev->dev, "APCC to MX APM switch timed out. APCC_APM_CTL_STS=0x%x\n",
  438. + regval);
  439. + }
  440. +
  441. + /* Perform revision-specific programming steps */
  442. + if (ctrl_dev->version < HMSS_VERSION_1P2) {
  443. + /* Switch APC/CBF clocks to original source */
  444. + writel_relaxed(APCS_GFMUXA_DESEL_VAL,
  445. + ctrl_dev->apcs_csr_base + APCS_SPARE);
  446. + ndelay(200);
  447. + writel_relaxed(MSM_APM_SEC_CLK_SEL_VAL,
  448. + ctrl_dev->apc0_pll_ctl_addr);
  449. + ndelay(200);
  450. + writel_relaxed(MSM_APM_SEC_CLK_SEL_VAL,
  451. + ctrl_dev->apc1_pll_ctl_addr);
  452. +
  453. + /* Complete clock source switch before SPM event sequence */
  454. + mb();
  455. +
  456. + /* Set SPM events */
  457. + for (i = 0; i < SPM_EVENT_NUM; i++)
  458. + writel_relaxed(SPM_EVENT_SET_VAL,
  459. + ctrl_dev->apcs_spm_events_addr[i]);
  460. + }
  461. +
  462. + if (!ret) {
  463. + ctrl_dev->supply = MSM_APM_SUPPLY_MX;
  464. + dev_dbg(ctrl_dev->dev, "APM supply switched to MX\n");
  465. + }
  466. +
  467. + spin_unlock_irqrestore(&ctrl_dev->lock, flags);
  468. +
  469. + return ret;
  470. +}
  471. +
  472. +static int msm8996_apm_switch_to_apcc(struct msm_apm_ctrl_dev *ctrl_dev)
  473. +{
  474. + int i, timeout = MSM_APM_SWITCH_TIMEOUT_US;
  475. + u32 regval;
  476. + int ret = 0;
  477. + unsigned long flags;
  478. +
  479. + spin_lock_irqsave(&ctrl_dev->lock, flags);
  480. +
  481. + /* Perform revision-specific programming steps */
  482. + if (ctrl_dev->version < HMSS_VERSION_1P2) {
  483. + /* Clear SPM events */
  484. + for (i = 0; i < SPM_EVENT_NUM; i++)
  485. + writel_relaxed(SPM_EVENT_CLEAR_VAL,
  486. + ctrl_dev->apcs_spm_events_addr[i]);
  487. +
  488. + udelay(SPM_WAKEUP_DELAY_US);
  489. +
  490. + /* Switch APC/CBF to GPLL0 clock */
  491. + writel_relaxed(APCS_GFMUXA_SEL_VAL,
  492. + ctrl_dev->apcs_csr_base + APCS_SPARE);
  493. + ndelay(200);
  494. + writel_relaxed(MSM_APM_OVERRIDE_SEL_VAL,
  495. + ctrl_dev->apc0_pll_ctl_addr);
  496. + ndelay(200);
  497. + writel_relaxed(MSM_APM_OVERRIDE_SEL_VAL,
  498. + ctrl_dev->apc1_pll_ctl_addr);
  499. +
  500. + /* Ensure previous writes complete before proceeding */
  501. + mb();
  502. + }
  503. +
  504. + /* Switch arrays to APCC supply and wait for its completion */
  505. + writel_relaxed(MSM_APM_APCC_MODE_VAL, ctrl_dev->reg_base +
  506. + APCC_APM_MODE);
  507. +
  508. + /* Ensure write above completes before delaying */
  509. + mb();
  510. +
  511. + while (timeout > 0) {
  512. + regval = readl_relaxed(ctrl_dev->reg_base + APCC_APM_CTL_STS);
  513. + if ((regval & MSM_APM_CTL_STS_MASK) ==
  514. + MSM_APM_APCC_DONE_VAL)
  515. + break;
  516. +
  517. + udelay(1);
  518. + timeout--;
  519. + }
  520. +
  521. + if (timeout == 0) {
  522. + ret = -ETIMEDOUT;
  523. + dev_err(ctrl_dev->dev, "MX to APCC APM switch timed out. APCC_APM_CTL_STS=0x%x\n",
  524. + regval);
  525. + }
  526. +
  527. + /* Perform revision-specific programming steps */
  528. + if (ctrl_dev->version < HMSS_VERSION_1P2) {
  529. + /* Set SPM events */
  530. + for (i = 0; i < SPM_EVENT_NUM; i++)
  531. + writel_relaxed(SPM_EVENT_SET_VAL,
  532. + ctrl_dev->apcs_spm_events_addr[i]);
  533. +
  534. + /* Complete SPM event sequence before clock source switch */
  535. + mb();
  536. +
  537. + /* Switch APC/CBF clocks to original source */
  538. + writel_relaxed(APCS_GFMUXA_DESEL_VAL,
  539. + ctrl_dev->apcs_csr_base + APCS_SPARE);
  540. + ndelay(200);
  541. + writel_relaxed(MSM_APM_SEC_CLK_SEL_VAL,
  542. + ctrl_dev->apc0_pll_ctl_addr);
  543. + ndelay(200);
  544. + writel_relaxed(MSM_APM_SEC_CLK_SEL_VAL,
  545. + ctrl_dev->apc1_pll_ctl_addr);
  546. + }
  547. +
  548. + if (!ret) {
  549. + ctrl_dev->supply = MSM_APM_SUPPLY_APCC;
  550. + dev_dbg(ctrl_dev->dev, "APM supply switched to APCC\n");
  551. + }
  552. +
  553. + spin_unlock_irqrestore(&ctrl_dev->lock, flags);
  554. +
  555. + return ret;
  556. +}
  557. +
  558. +/* 8953 register value definitions */
  559. +#define MSM8953_APM_MX_MODE_VAL 0x00
  560. +#define MSM8953_APM_APCC_MODE_VAL 0x02
  561. +#define MSM8953_APM_MX_DONE_VAL 0x00
  562. +#define MSM8953_APM_APCC_DONE_VAL 0x03
  563. +
  564. +/* 8953 register offset definitions */
  565. +#define MSM8953_APCC_APM_MODE 0x000002a8
  566. +#define MSM8953_APCC_APM_CTL_STS 0x000002b0
  567. +
  568. +/* 8953 constants */
  569. +#define MSM8953_APM_SWITCH_TIMEOUT_US 500
  570. +
  571. +/* Register bit mask definitions */
  572. +#define MSM8953_APM_CTL_STS_MASK 0x1f
  573. +
  574. +static int msm8953_apm_switch_to_mx(struct msm_apm_ctrl_dev *ctrl_dev)
  575. +{
  576. + int timeout = MSM8953_APM_SWITCH_TIMEOUT_US;
  577. + u32 regval;
  578. + int ret = 0;
  579. + unsigned long flags;
  580. +
  581. + spin_lock_irqsave(&ctrl_dev->lock, flags);
  582. +
  583. + /* Switch arrays to MX supply and wait for its completion */
  584. + writel_relaxed(MSM8953_APM_MX_MODE_VAL, ctrl_dev->reg_base +
  585. + MSM8953_APCC_APM_MODE);
  586. +
  587. + /* Ensure write above completes before delaying */
  588. + mb();
  589. +
  590. + while (timeout > 0) {
  591. + regval = readl_relaxed(ctrl_dev->reg_base +
  592. + MSM8953_APCC_APM_CTL_STS);
  593. + if ((regval & MSM8953_APM_CTL_STS_MASK) ==
  594. + MSM8953_APM_MX_DONE_VAL)
  595. + break;
  596. +
  597. + udelay(1);
  598. + timeout--;
  599. + }
  600. +
  601. + if (timeout == 0) {
  602. + ret = -ETIMEDOUT;
  603. + dev_err(ctrl_dev->dev, "APCC to MX APM switch timed out. APCC_APM_CTL_STS=0x%x\n",
  604. + regval);
  605. + } else {
  606. + ctrl_dev->supply = MSM_APM_SUPPLY_MX;
  607. + dev_dbg(ctrl_dev->dev, "APM supply switched to MX\n");
  608. + }
  609. +
  610. + spin_unlock_irqrestore(&ctrl_dev->lock, flags);
  611. +
  612. + return ret;
  613. +}
  614. +
  615. +static int msm8953_apm_switch_to_apcc(struct msm_apm_ctrl_dev *ctrl_dev)
  616. +{
  617. + int timeout = MSM8953_APM_SWITCH_TIMEOUT_US;
  618. + u32 regval;
  619. + int ret = 0;
  620. + unsigned long flags;
  621. +
  622. + spin_lock_irqsave(&ctrl_dev->lock, flags);
  623. +
  624. + /* Switch arrays to APCC supply and wait for its completion */
  625. + writel_relaxed(MSM8953_APM_APCC_MODE_VAL, ctrl_dev->reg_base +
  626. + MSM8953_APCC_APM_MODE);
  627. +
  628. + /* Ensure write above completes before delaying */
  629. + mb();
  630. +
  631. + while (timeout > 0) {
  632. + regval = readl_relaxed(ctrl_dev->reg_base +
  633. + MSM8953_APCC_APM_CTL_STS);
  634. + if ((regval & MSM8953_APM_CTL_STS_MASK) ==
  635. + MSM8953_APM_APCC_DONE_VAL)
  636. + break;
  637. +
  638. + udelay(1);
  639. + timeout--;
  640. + }
  641. +
  642. + if (timeout == 0) {
  643. + ret = -ETIMEDOUT;
  644. + dev_err(ctrl_dev->dev, "MX to APCC APM switch timed out. APCC_APM_CTL_STS=0x%x\n",
  645. + regval);
  646. + } else {
  647. + ctrl_dev->supply = MSM_APM_SUPPLY_APCC;
  648. + dev_dbg(ctrl_dev->dev, "APM supply switched to APCC\n");
  649. + }
  650. +
  651. + spin_unlock_irqrestore(&ctrl_dev->lock, flags);
  652. +
  653. + return ret;
  654. +}
  655. +
  656. +static int msm_apm_switch_to_mx(struct msm_apm_ctrl_dev *ctrl_dev)
  657. +{
  658. + int ret = 0;
  659. +
  660. + switch (ctrl_dev->msm_id) {
  661. + case MSM8996_ID:
  662. + ret = msm8996_apm_switch_to_mx(ctrl_dev);
  663. + break;
  664. + case MSM8953_ID:
  665. + case IPQ807x_ID:
  666. + ret = msm8953_apm_switch_to_mx(ctrl_dev);
  667. + break;
  668. + }
  669. +
  670. + return ret;
  671. +}
  672. +
  673. +static int msm_apm_switch_to_apcc(struct msm_apm_ctrl_dev *ctrl_dev)
  674. +{
  675. + int ret = 0;
  676. +
  677. + switch (ctrl_dev->msm_id) {
  678. + case MSM8996_ID:
  679. + ret = msm8996_apm_switch_to_apcc(ctrl_dev);
  680. + break;
  681. + case MSM8953_ID:
  682. + case IPQ807x_ID:
  683. + ret = msm8953_apm_switch_to_apcc(ctrl_dev);
  684. + break;
  685. + }
  686. +
  687. + return ret;
  688. +}
  689. +
  690. +/**
  691. + * msm_apm_get_supply() - Returns the supply that is currently
  692. + * powering the memory arrays
  693. + * @ctrl_dev: Pointer to an MSM APM controller device
  694. + *
  695. + * Returns the supply currently selected by the APM.
  696. + */
  697. +int msm_apm_get_supply(struct msm_apm_ctrl_dev *ctrl_dev)
  698. +{
  699. + return ctrl_dev->supply;
  700. +}
  701. +EXPORT_SYMBOL(msm_apm_get_supply);
  702. +
  703. +/**
  704. + * msm_apm_set_supply() - Perform the necessary steps to switch the voltage
  705. + * source of the memory arrays to a given supply
  706. + * @ctrl_dev: Pointer to an MSM APM controller device
  707. + * @supply: Power rail to use as supply for the memory
  708. + * arrays
  709. + *
  710. + * Returns 0 on success, -ETIMEDOUT on APM switch timeout, or -EPERM if
  711. + * the supply is not supported.
  712. + */
  713. +int msm_apm_set_supply(struct msm_apm_ctrl_dev *ctrl_dev,
  714. + enum msm_apm_supply supply)
  715. +{
  716. + int ret;
  717. +
  718. + switch (supply) {
  719. + case MSM_APM_SUPPLY_APCC:
  720. + ret = msm_apm_switch_to_apcc(ctrl_dev);
  721. + break;
  722. + case MSM_APM_SUPPLY_MX:
  723. + ret = msm_apm_switch_to_mx(ctrl_dev);
  724. + break;
  725. + default:
  726. + ret = -EPERM;
  727. + break;
  728. + }
  729. +
  730. + return ret;
  731. +}
  732. +EXPORT_SYMBOL(msm_apm_set_supply);
  733. +
  734. +/**
  735. + * msm_apm_ctrl_dev_get() - get a handle to the MSM APM controller linked to
  736. + * the device in device tree
  737. + * @dev: Pointer to the device
  738. + *
  739. + * The device must specify "qcom,apm-ctrl" property in its device tree
  740. + * node which points to an MSM APM controller device node.
  741. + *
  742. + * Returns an MSM APM controller handle if successful or ERR_PTR on any error.
  743. + * If the APM controller device hasn't probed yet, ERR_PTR(-EPROBE_DEFER) is
  744. + * returned.
  745. + */
  746. +struct msm_apm_ctrl_dev *msm_apm_ctrl_dev_get(struct device *dev)
  747. +{
  748. + struct msm_apm_ctrl_dev *ctrl_dev = NULL;
  749. + struct msm_apm_ctrl_dev *dev_found = ERR_PTR(-EPROBE_DEFER);
  750. + struct device_node *ctrl_node;
  751. +
  752. + if (!dev || !dev->of_node) {
  753. + pr_err("Invalid device node\n");
  754. + return ERR_PTR(-EINVAL);
  755. + }
  756. +
  757. + ctrl_node = of_parse_phandle(dev->of_node, "qcom,apm-ctrl", 0);
  758. + if (!ctrl_node) {
  759. + pr_err("Could not find qcom,apm-ctrl property in %s\n",
  760. + dev->of_node->full_name);
  761. + return ERR_PTR(-ENXIO);
  762. + }
  763. +
  764. + mutex_lock(&apm_ctrl_list_mutex);
  765. + list_for_each_entry(ctrl_dev, &apm_ctrl_list, list) {
  766. + if (ctrl_dev->dev && ctrl_dev->dev->of_node == ctrl_node) {
  767. + dev_found = ctrl_dev;
  768. + break;
  769. + }
  770. + }
  771. + mutex_unlock(&apm_ctrl_list_mutex);
  772. +
  773. + of_node_put(ctrl_node);
  774. + return dev_found;
  775. +}
  776. +EXPORT_SYMBOL(msm_apm_ctrl_dev_get);
  777. +
  778. +#if defined(CONFIG_DEBUG_FS)
  779. +
  780. +static int apm_supply_dbg_open(struct inode *inode, struct file *filep)
  781. +{
  782. + filep->private_data = inode->i_private;
  783. +
  784. + return 0;
  785. +}
  786. +
  787. +static ssize_t apm_supply_dbg_read(struct file *filep, char __user *ubuf,
  788. + size_t count, loff_t *ppos)
  789. +{
  790. + struct msm_apm_ctrl_dev *ctrl_dev = filep->private_data;
  791. + char buf[10];
  792. + int len;
  793. +
  794. + if (!ctrl_dev) {
  795. + pr_err("invalid apm ctrl handle\n");
  796. + return -ENODEV;
  797. + }
  798. +
  799. + if (ctrl_dev->supply == MSM_APM_SUPPLY_APCC)
  800. + len = snprintf(buf, sizeof(buf), "APCC\n");
  801. + else if (ctrl_dev->supply == MSM_APM_SUPPLY_MX)
  802. + len = snprintf(buf, sizeof(buf), "MX\n");
  803. + else
  804. + len = snprintf(buf, sizeof(buf), "ERR\n");
  805. +
  806. + return simple_read_from_buffer(ubuf, count, ppos, buf, len);
  807. +}
  808. +
  809. +static const struct file_operations apm_supply_fops = {
  810. + .open = apm_supply_dbg_open,
  811. + .read = apm_supply_dbg_read,
  812. +};
  813. +
  814. +static void apm_debugfs_base_init(void)
  815. +{
  816. + apm_debugfs_base = debugfs_create_dir("msm-apm", NULL);
  817. +
  818. + if (IS_ERR_OR_NULL(apm_debugfs_base))
  819. + pr_err("msm-apm debugfs base directory creation failed\n");
  820. +}
  821. +
  822. +static void apm_debugfs_init(struct msm_apm_ctrl_dev *ctrl_dev)
  823. +{
  824. + struct dentry *temp;
  825. +
  826. + if (IS_ERR_OR_NULL(apm_debugfs_base)) {
  827. + pr_err("Base directory missing, cannot create apm debugfs nodes\n");
  828. + return;
  829. + }
  830. +
  831. + ctrl_dev->debugfs = debugfs_create_dir(dev_name(ctrl_dev->dev),
  832. + apm_debugfs_base);
  833. + if (IS_ERR_OR_NULL(ctrl_dev->debugfs)) {
  834. + pr_err("%s debugfs directory creation failed\n",
  835. + dev_name(ctrl_dev->dev));
  836. + return;
  837. + }
  838. +
  839. + temp = debugfs_create_file("supply", S_IRUGO, ctrl_dev->debugfs,
  840. + ctrl_dev, &apm_supply_fops);
  841. + if (IS_ERR_OR_NULL(temp)) {
  842. + pr_err("supply mode creation failed\n");
  843. + return;
  844. + }
  845. +}
  846. +
  847. +static void apm_debugfs_deinit(struct msm_apm_ctrl_dev *ctrl_dev)
  848. +{
  849. + if (!IS_ERR_OR_NULL(ctrl_dev->debugfs))
  850. + debugfs_remove_recursive(ctrl_dev->debugfs);
  851. +}
  852. +
  853. +static void apm_debugfs_base_remove(void)
  854. +{
  855. + debugfs_remove_recursive(apm_debugfs_base);
  856. +}
  857. +#else
  858. +
  859. +static void apm_debugfs_base_init(void)
  860. +{}
  861. +
  862. +static void apm_debugfs_init(struct msm_apm_ctrl_dev *ctrl_dev)
  863. +{}
  864. +
  865. +static void apm_debugfs_deinit(struct msm_apm_ctrl_dev *ctrl_dev)
  866. +{}
  867. +
  868. +static void apm_debugfs_base_remove(void)
  869. +{}
  870. +
  871. +#endif
  872. +
  873. +static struct of_device_id msm_apm_match_table[] = {
  874. + {
  875. + .compatible = "qcom,msm-apm",
  876. + .data = (void *)(uintptr_t)MSM8996_ID,
  877. + },
  878. + {
  879. + .compatible = "qcom,msm8953-apm",
  880. + .data = (void *)(uintptr_t)MSM8953_ID,
  881. + },
  882. + {
  883. + .compatible = "qcom,ipq807x-apm",
  884. + .data = (void *)(uintptr_t)IPQ807x_ID,
  885. + },
  886. + {}
  887. +};
  888. +
  889. +static int msm_apm_probe(struct platform_device *pdev)
  890. +{
  891. + struct device *dev = &pdev->dev;
  892. + struct msm_apm_ctrl_dev *ctrl;
  893. + const struct of_device_id *match;
  894. + int ret = 0;
  895. +
  896. + dev_dbg(dev, "probing MSM Array Power Mux driver\n");
  897. +
  898. + if (!dev->of_node) {
  899. + dev_err(dev, "Device tree node is missing\n");
  900. + return -ENODEV;
  901. + }
  902. +
  903. + match = of_match_device(msm_apm_match_table, dev);
  904. + if (!match)
  905. + return -ENODEV;
  906. +
  907. + ctrl = devm_kzalloc(dev, sizeof(*ctrl), GFP_KERNEL);
  908. + if (!ctrl) {
  909. + dev_err(dev, "MSM APM controller memory allocation failed\n");
  910. + return -ENOMEM;
  911. + }
  912. +
  913. + INIT_LIST_HEAD(&ctrl->list);
  914. + spin_lock_init(&ctrl->lock);
  915. + ctrl->dev = dev;
  916. + ctrl->msm_id = (uintptr_t)match->data;
  917. + platform_set_drvdata(pdev, ctrl);
  918. +
  919. + switch (ctrl->msm_id) {
  920. + case MSM8996_ID:
  921. + ret = msm_apm_ctrl_devm_ioremap(pdev, ctrl);
  922. + if (ret) {
  923. + dev_err(dev, "Failed to add APM controller device\n");
  924. + return ret;
  925. + }
  926. + break;
  927. + case MSM8953_ID:
  928. + case IPQ807x_ID:
  929. + ret = msm8953_apm_ctrl_init(pdev, ctrl);
  930. + if (ret) {
  931. + dev_err(dev, "Failed to initialize APM controller device: ret=%d\n",
  932. + ret);
  933. + return ret;
  934. + }
  935. + break;
  936. + default:
  937. + dev_err(dev, "unable to add APM controller device for msm_id:%d\n",
  938. + ctrl->msm_id);
  939. + return -ENODEV;
  940. + }
  941. +
  942. + apm_debugfs_init(ctrl);
  943. + mutex_lock(&apm_ctrl_list_mutex);
  944. + list_add_tail(&ctrl->list, &apm_ctrl_list);
  945. + mutex_unlock(&apm_ctrl_list_mutex);
  946. +
  947. + dev_dbg(dev, "MSM Array Power Mux driver probe successful");
  948. +
  949. + return ret;
  950. +}
  951. +
  952. +static int msm_apm_remove(struct platform_device *pdev)
  953. +{
  954. + struct msm_apm_ctrl_dev *ctrl_dev;
  955. +
  956. + ctrl_dev = platform_get_drvdata(pdev);
  957. + if (ctrl_dev) {
  958. + mutex_lock(&apm_ctrl_list_mutex);
  959. + list_del(&ctrl_dev->list);
  960. + mutex_unlock(&apm_ctrl_list_mutex);
  961. + apm_debugfs_deinit(ctrl_dev);
  962. + }
  963. +
  964. + return 0;
  965. +}
  966. +
  967. +static struct platform_driver msm_apm_driver = {
  968. + .driver = {
  969. + .name = MSM_APM_DRIVER_NAME,
  970. + .of_match_table = msm_apm_match_table,
  971. + .owner = THIS_MODULE,
  972. + },
  973. + .probe = msm_apm_probe,
  974. + .remove = msm_apm_remove,
  975. +};
  976. +
  977. +static int __init msm_apm_init(void)
  978. +{
  979. + apm_debugfs_base_init();
  980. + return platform_driver_register(&msm_apm_driver);
  981. +}
  982. +
  983. +static void __exit msm_apm_exit(void)
  984. +{
  985. + platform_driver_unregister(&msm_apm_driver);
  986. + apm_debugfs_base_remove();
  987. +}
  988. +
  989. +arch_initcall(msm_apm_init);
  990. +module_exit(msm_apm_exit);
  991. +
  992. +MODULE_DESCRIPTION("MSM Array Power Mux driver");
  993. +MODULE_LICENSE("GPL v2");
  994. --- /dev/null
  995. +++ b/include/linux/power/qcom/apm.h
  996. @@ -0,0 +1,48 @@
  997. +/*
  998. + * Copyright (c) 2015, The Linux Foundation. All rights reserved.
  999. + *
  1000. + * This program is free software; you can redistribute it and/or modify
  1001. + * it under the terms of the GNU General Public License version 2 and
  1002. + * only version 2 as published by the Free Software Foundation.
  1003. + *
  1004. + * This program is distributed in the hope that it will be useful,
  1005. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  1006. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  1007. + * GNU General Public License for more details.
  1008. + */
  1009. +
  1010. +#ifndef __LINUX_POWER_QCOM_APM_H__
  1011. +#define __LINUX_POWER_QCOM_APM_H__
  1012. +
  1013. +#include <linux/device.h>
  1014. +#include <linux/err.h>
  1015. +
  1016. +/**
  1017. + * enum msm_apm_supply - supported power rails to supply memory arrays
  1018. + * %MSM_APM_SUPPLY_APCC: to enable selection of VDD_APCC rail as supply
  1019. + * %MSM_APM_SUPPLY_MX: to enable selection of VDD_MX rail as supply
  1020. + */
  1021. +enum msm_apm_supply {
  1022. + MSM_APM_SUPPLY_APCC,
  1023. + MSM_APM_SUPPLY_MX,
  1024. +};
  1025. +
  1026. +/* Handle used to identify an APM controller device */
  1027. +struct msm_apm_ctrl_dev;
  1028. +
  1029. +#ifdef CONFIG_QCOM_APM
  1030. +struct msm_apm_ctrl_dev *msm_apm_ctrl_dev_get(struct device *dev);
  1031. +int msm_apm_set_supply(struct msm_apm_ctrl_dev *ctrl_dev,
  1032. + enum msm_apm_supply supply);
  1033. +int msm_apm_get_supply(struct msm_apm_ctrl_dev *ctrl_dev);
  1034. +
  1035. +#else
  1036. +static inline struct msm_apm_ctrl_dev *msm_apm_ctrl_dev_get(struct device *dev)
  1037. +{ return ERR_PTR(-EPERM); }
  1038. +static inline int msm_apm_set_supply(struct msm_apm_ctrl_dev *ctrl_dev,
  1039. + enum msm_apm_supply supply)
  1040. +{ return -EPERM; }
  1041. +static inline int msm_apm_get_supply(struct msm_apm_ctrl_dev *ctrl_dev)
  1042. +{ return -EPERM; }
  1043. +#endif
  1044. +#endif