kryo-regulator.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105
  1. /*
  2. * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 and
  6. * only version 2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/cpu_pm.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/delay.h>
  17. #include <linux/init.h>
  18. #include <linux/io.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/of.h>
  22. #include <linux/slab.h>
  23. #include <linux/string.h>
  24. #include <linux/of_address.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/regulator/driver.h>
  27. #include <linux/regulator/machine.h>
  28. #include <linux/regulator/of_regulator.h>
  29. #include <linux/regulator/msm-ldo-regulator.h>
  30. #include <soc/qcom/spm.h>
  31. #define KRYO_REGULATOR_DRIVER_NAME "kryo-regulator"
  32. #define kvreg_err(kvreg, message, ...) \
  33. pr_err("%s: " message, (kvreg)->name, ##__VA_ARGS__)
  34. #define kvreg_info(kvreg, message, ...) \
  35. pr_info("%s: " message, (kvreg)->name, ##__VA_ARGS__)
  36. #define kvreg_debug(kvreg, message, ...) \
  37. pr_debug("%s: " message, (kvreg)->name, ##__VA_ARGS__)
  38. /* CPUSS power domain register offsets */
  39. #define APCC_PWR_CTL_OVERRIDE 0x38
  40. #define APCC_PGS_RET_STATUS 0xe0
  41. /* APCS CSR register offsets */
  42. #define APCS_VERSION 0xfd0
  43. /* Cluster power domain register offsets */
  44. #define APC_LDO_VREF_SET 0x08
  45. #define APC_RET_VREF_SET 0x10
  46. #define APC_PWR_GATE_MODE 0x18
  47. #define APC_PWR_GATE_DLY 0x28
  48. #define APC_LDO_CFG 0x40
  49. #define APC_APM_CFG 0x50
  50. #define APC_PGSCTL_STS 0x60
  51. /* Register bit mask definitions*/
  52. #define PWR_GATE_SWITCH_MODE_MASK GENMASK(0, 0)
  53. #define VREF_MASK GENMASK(6, 0)
  54. #define APM_CFG_MASK GENMASK(7, 0)
  55. #define FSM_CUR_STATE_MASK GENMASK(5, 4)
  56. #define APC_PWR_GATE_DLY_MASK GENMASK(11, 0)
  57. #define APCC_PGS_MASK(cluster) (0x7 << (0x3 * (cluster)))
  58. /* Register bit definitions */
  59. #define VREF_BIT_POS 0
  60. /* Maximum delay to wait before declaring a Power Gate Switch timed out */
  61. #define PWR_GATE_SWITCH_TIMEOUT_US 5
  62. #define PWR_GATE_SWITCH_MODE_LDO 0
  63. #define PWR_GATE_SWITCH_MODE_BHS 1
  64. #define MSM8996_CPUSS_VER_1P1 0x10010000
  65. #define LDO_N_VOLTAGES 0x80
  66. #define AFFINITY_LEVEL_M3 2
  67. #define SHARED_CPU_REG_NUM 0
  68. #define VDD_SUPPLY_STEP_UV 5000
  69. #define VDD_SUPPLY_MIN_UV 80000
  70. struct kryo_regulator {
  71. struct list_head link;
  72. spinlock_t slock;
  73. struct regulator_desc desc;
  74. struct regulator_dev *rdev;
  75. struct regulator_dev *retention_rdev;
  76. struct regulator_desc retention_desc;
  77. const char *name;
  78. enum msm_ldo_supply_mode mode;
  79. enum msm_ldo_supply_mode retention_mode;
  80. enum msm_ldo_supply_mode pre_lpm_state_mode;
  81. void __iomem *reg_base;
  82. void __iomem *pm_apcc_base;
  83. struct dentry *debugfs;
  84. struct notifier_block cpu_pm_notifier;
  85. unsigned long lpm_enter_count;
  86. unsigned long lpm_exit_count;
  87. int volt;
  88. int retention_volt;
  89. int headroom_volt;
  90. int pre_lpm_state_volt;
  91. int vref_func_step_volt;
  92. int vref_func_min_volt;
  93. int vref_func_max_volt;
  94. int vref_ret_step_volt;
  95. int vref_ret_min_volt;
  96. int vref_ret_max_volt;
  97. int cluster_num;
  98. u32 ldo_config_init;
  99. u32 apm_config_init;
  100. u32 version;
  101. bool vreg_en;
  102. };
  103. static struct dentry *kryo_debugfs_base;
  104. static DEFINE_MUTEX(kryo_regulator_list_mutex);
  105. static LIST_HEAD(kryo_regulator_list);
  106. static bool is_between(int left, int right, int value)
  107. {
  108. if (left >= right && left >= value && value >= right)
  109. return true;
  110. if (left <= right && left <= value && value <= right)
  111. return true;
  112. return false;
  113. }
  114. static void kryo_masked_write(struct kryo_regulator *kvreg,
  115. int reg, u32 mask, u32 val)
  116. {
  117. u32 reg_val;
  118. reg_val = readl_relaxed(kvreg->reg_base + reg);
  119. reg_val &= ~mask;
  120. reg_val |= (val & mask);
  121. writel_relaxed(reg_val, kvreg->reg_base + reg);
  122. /* Ensure write above completes */
  123. mb();
  124. }
  125. static inline void kryo_pm_apcc_masked_write(struct kryo_regulator *kvreg,
  126. int reg, u32 mask, u32 val)
  127. {
  128. u32 reg_val, orig_val;
  129. reg_val = orig_val = readl_relaxed(kvreg->pm_apcc_base + reg);
  130. reg_val &= ~mask;
  131. reg_val |= (val & mask);
  132. if (reg_val != orig_val) {
  133. writel_relaxed(reg_val, kvreg->pm_apcc_base + reg);
  134. /* Ensure write above completes */
  135. mb();
  136. }
  137. }
  138. static inline int kryo_decode_retention_volt(struct kryo_regulator *kvreg,
  139. int reg)
  140. {
  141. return kvreg->vref_ret_min_volt + reg * kvreg->vref_ret_step_volt;
  142. }
  143. static inline int kryo_encode_retention_volt(struct kryo_regulator *kvreg,
  144. int volt)
  145. {
  146. int encoded_volt = DIV_ROUND_UP(volt - kvreg->vref_ret_min_volt,
  147. kvreg->vref_ret_step_volt);
  148. if (encoded_volt >= LDO_N_VOLTAGES || encoded_volt < 0)
  149. return -EINVAL;
  150. else
  151. return encoded_volt;
  152. }
  153. static inline int kryo_decode_functional_volt(struct kryo_regulator *kvreg,
  154. int reg)
  155. {
  156. return kvreg->vref_func_min_volt + reg * kvreg->vref_func_step_volt;
  157. }
  158. static inline int kryo_encode_functional_volt(struct kryo_regulator *kvreg,
  159. int volt)
  160. {
  161. int encoded_volt = DIV_ROUND_UP(volt - kvreg->vref_func_min_volt,
  162. kvreg->vref_func_step_volt);
  163. if (encoded_volt >= LDO_N_VOLTAGES || encoded_volt < 0)
  164. return -EINVAL;
  165. else
  166. return encoded_volt;
  167. }
  168. /* Locks must be held by the caller */
  169. static int kryo_set_retention_volt(struct kryo_regulator *kvreg, int volt)
  170. {
  171. int reg_val;
  172. reg_val = kryo_encode_retention_volt(kvreg, volt);
  173. if (reg_val < 0) {
  174. kvreg_err(kvreg, "unsupported LDO retention voltage, rc=%d\n",
  175. reg_val);
  176. return reg_val;
  177. }
  178. kryo_masked_write(kvreg, APC_RET_VREF_SET, VREF_MASK,
  179. reg_val << VREF_BIT_POS);
  180. kvreg->retention_volt = kryo_decode_retention_volt(kvreg, reg_val);
  181. kvreg_debug(kvreg, "Set LDO retention voltage=%d uV (0x%x)\n",
  182. kvreg->retention_volt, reg_val);
  183. return 0;
  184. }
  185. /* Locks must be held by the caller */
  186. static int kryo_set_ldo_volt(struct kryo_regulator *kvreg, int volt)
  187. {
  188. int reg_val;
  189. /*
  190. * Assume the consumer ensures the requested voltage satisfies the
  191. * headroom and adjustment voltage requirements. The value may be
  192. * rounded up if necessary, to match the LDO resolution. Configure it.
  193. */
  194. reg_val = kryo_encode_functional_volt(kvreg, volt);
  195. if (reg_val < 0) {
  196. kvreg_err(kvreg, "unsupported LDO functional voltage, rc=%d\n",
  197. reg_val);
  198. return reg_val;
  199. }
  200. kryo_masked_write(kvreg, APC_LDO_VREF_SET, VREF_MASK,
  201. reg_val << VREF_BIT_POS);
  202. kvreg->volt = kryo_decode_functional_volt(kvreg, reg_val);
  203. kvreg_debug(kvreg, "Set LDO voltage=%d uV (0x%x)\n",
  204. kvreg->volt, reg_val);
  205. return 0;
  206. }
  207. /* Locks must be held by the caller */
  208. static int kryo_configure_mode(struct kryo_regulator *kvreg,
  209. enum msm_ldo_supply_mode mode)
  210. {
  211. u32 reg;
  212. int timeout = PWR_GATE_SWITCH_TIMEOUT_US;
  213. /* Configure LDO or BHS mode */
  214. kryo_masked_write(kvreg, APC_PWR_GATE_MODE, PWR_GATE_SWITCH_MODE_MASK,
  215. mode == LDO_MODE ? PWR_GATE_SWITCH_MODE_LDO
  216. : PWR_GATE_SWITCH_MODE_BHS);
  217. /* Complete register write before reading HW status register */
  218. mb();
  219. /* Delay to allow Power Gate Switch FSM to reach idle state */
  220. while (timeout > 0) {
  221. reg = readl_relaxed(kvreg->reg_base + APC_PGSCTL_STS);
  222. if (!(reg & FSM_CUR_STATE_MASK))
  223. break;
  224. udelay(1);
  225. timeout--;
  226. }
  227. if (timeout == 0) {
  228. kvreg_err(kvreg, "PGS switch to %s failed. APC_PGSCTL_STS=0x%x\n",
  229. mode == LDO_MODE ? "LDO" : "BHS", reg);
  230. return -ETIMEDOUT;
  231. }
  232. kvreg->mode = mode;
  233. kvreg_debug(kvreg, "using %s mode\n", mode == LDO_MODE ? "LDO" : "BHS");
  234. return 0;
  235. }
  236. static int kryo_regulator_enable(struct regulator_dev *rdev)
  237. {
  238. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  239. int rc;
  240. unsigned long flags;
  241. if (kvreg->vreg_en == true)
  242. return 0;
  243. spin_lock_irqsave(&kvreg->slock, flags);
  244. rc = kryo_set_ldo_volt(kvreg, kvreg->volt);
  245. if (rc) {
  246. kvreg_err(kvreg, "set voltage failed, rc=%d\n", rc);
  247. goto done;
  248. }
  249. kvreg->vreg_en = true;
  250. kvreg_debug(kvreg, "enabled\n");
  251. done:
  252. spin_unlock_irqrestore(&kvreg->slock, flags);
  253. return rc;
  254. }
  255. static int kryo_regulator_disable(struct regulator_dev *rdev)
  256. {
  257. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  258. int rc;
  259. unsigned long flags;
  260. if (kvreg->vreg_en == false)
  261. return 0;
  262. spin_lock_irqsave(&kvreg->slock, flags);
  263. kvreg->vreg_en = false;
  264. kvreg_debug(kvreg, "disabled\n");
  265. spin_unlock_irqrestore(&kvreg->slock, flags);
  266. return rc;
  267. }
  268. static int kryo_regulator_is_enabled(struct regulator_dev *rdev)
  269. {
  270. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  271. return kvreg->vreg_en;
  272. }
  273. static int kryo_regulator_set_voltage(struct regulator_dev *rdev,
  274. int min_volt, int max_volt, unsigned int *selector)
  275. {
  276. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  277. int rc;
  278. unsigned long flags;
  279. spin_lock_irqsave(&kvreg->slock, flags);
  280. if (!kvreg->vreg_en) {
  281. kvreg->volt = min_volt;
  282. spin_unlock_irqrestore(&kvreg->slock, flags);
  283. return 0;
  284. }
  285. rc = kryo_set_ldo_volt(kvreg, min_volt);
  286. if (rc)
  287. kvreg_err(kvreg, "set voltage failed, rc=%d\n", rc);
  288. spin_unlock_irqrestore(&kvreg->slock, flags);
  289. return rc;
  290. }
  291. static int kryo_regulator_get_voltage(struct regulator_dev *rdev)
  292. {
  293. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  294. return kvreg->volt;
  295. }
  296. static int kryo_regulator_set_bypass(struct regulator_dev *rdev,
  297. bool enable)
  298. {
  299. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  300. int rc;
  301. unsigned long flags;
  302. spin_lock_irqsave(&kvreg->slock, flags);
  303. /*
  304. * LDO Vref voltage must be programmed before switching
  305. * modes to ensure stable operation.
  306. */
  307. rc = kryo_set_ldo_volt(kvreg, kvreg->volt);
  308. if (rc)
  309. kvreg_err(kvreg, "set voltage failed, rc=%d\n", rc);
  310. rc = kryo_configure_mode(kvreg, enable);
  311. if (rc)
  312. kvreg_err(kvreg, "could not configure to %s mode\n",
  313. enable == LDO_MODE ? "LDO" : "BHS");
  314. spin_unlock_irqrestore(&kvreg->slock, flags);
  315. return rc;
  316. }
  317. static int kryo_regulator_get_bypass(struct regulator_dev *rdev,
  318. bool *enable)
  319. {
  320. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  321. *enable = kvreg->mode;
  322. return 0;
  323. }
  324. static int kryo_regulator_list_voltage(struct regulator_dev *rdev,
  325. unsigned int selector)
  326. {
  327. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  328. if (selector < kvreg->desc.n_voltages)
  329. return kryo_decode_functional_volt(kvreg, selector);
  330. else
  331. return 0;
  332. }
  333. static int kryo_regulator_retention_set_voltage(struct regulator_dev *rdev,
  334. int min_volt, int max_volt, unsigned int *selector)
  335. {
  336. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  337. int rc;
  338. unsigned long flags;
  339. spin_lock_irqsave(&kvreg->slock, flags);
  340. rc = kryo_set_retention_volt(kvreg, min_volt);
  341. if (rc)
  342. kvreg_err(kvreg, "set voltage failed, rc=%d\n", rc);
  343. spin_unlock_irqrestore(&kvreg->slock, flags);
  344. return rc;
  345. }
  346. static int kryo_regulator_retention_get_voltage(struct regulator_dev *rdev)
  347. {
  348. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  349. return kvreg->retention_volt;
  350. }
  351. static int kryo_regulator_retention_set_bypass(struct regulator_dev *rdev,
  352. bool enable)
  353. {
  354. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  355. int timeout = PWR_GATE_SWITCH_TIMEOUT_US;
  356. int rc = 0;
  357. u32 reg_val;
  358. unsigned long flags;
  359. spin_lock_irqsave(&kvreg->slock, flags);
  360. kryo_pm_apcc_masked_write(kvreg,
  361. APCC_PWR_CTL_OVERRIDE,
  362. APCC_PGS_MASK(kvreg->cluster_num),
  363. enable ?
  364. 0 : APCC_PGS_MASK(kvreg->cluster_num));
  365. /* Ensure write above completes before proceeding */
  366. mb();
  367. if (enable == BHS_MODE && kvreg->version < MSM8996_CPUSS_VER_1P1) {
  368. /* No status register, delay worst case */
  369. udelay(PWR_GATE_SWITCH_TIMEOUT_US);
  370. } else if (enable == BHS_MODE) {
  371. while (timeout > 0) {
  372. reg_val = readl_relaxed(kvreg->pm_apcc_base
  373. + APCC_PGS_RET_STATUS);
  374. if (!(reg_val & APCC_PGS_MASK(kvreg->cluster_num)))
  375. break;
  376. udelay(1);
  377. timeout--;
  378. }
  379. if (timeout == 0) {
  380. kvreg_err(kvreg, "PGS switch timed out. APCC_PGS_RET_STATUS=0x%x\n",
  381. reg_val);
  382. rc = -ETIMEDOUT;
  383. goto done;
  384. }
  385. }
  386. /* Bypassed LDO retention operation == disallow LDO retention */
  387. kvreg_debug(kvreg, "%s LDO retention\n",
  388. enable ? "enabled" : "disabled");
  389. kvreg->retention_mode = enable == LDO_MODE ? LDO_MODE
  390. : BHS_MODE;
  391. done:
  392. spin_unlock_irqrestore(&kvreg->slock, flags);
  393. return rc;
  394. }
  395. static int kryo_regulator_retention_get_bypass(struct regulator_dev *rdev,
  396. bool *enable)
  397. {
  398. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  399. *enable = kvreg->retention_mode;
  400. return 0;
  401. }
  402. static int kryo_regulator_retention_list_voltage(struct regulator_dev *rdev,
  403. unsigned int selector)
  404. {
  405. struct kryo_regulator *kvreg = rdev_get_drvdata(rdev);
  406. if (selector < kvreg->retention_desc.n_voltages)
  407. return kryo_decode_retention_volt(kvreg, selector);
  408. else
  409. return 0;
  410. }
  411. static struct regulator_ops kryo_regulator_ops = {
  412. .enable = kryo_regulator_enable,
  413. .disable = kryo_regulator_disable,
  414. .is_enabled = kryo_regulator_is_enabled,
  415. .set_voltage = kryo_regulator_set_voltage,
  416. .get_voltage = kryo_regulator_get_voltage,
  417. .set_bypass = kryo_regulator_set_bypass,
  418. .get_bypass = kryo_regulator_get_bypass,
  419. .list_voltage = kryo_regulator_list_voltage,
  420. };
  421. static struct regulator_ops kryo_regulator_retention_ops = {
  422. .set_voltage = kryo_regulator_retention_set_voltage,
  423. .get_voltage = kryo_regulator_retention_get_voltage,
  424. .set_bypass = kryo_regulator_retention_set_bypass,
  425. .get_bypass = kryo_regulator_retention_get_bypass,
  426. .list_voltage = kryo_regulator_retention_list_voltage,
  427. };
  428. static void kryo_ldo_voltage_init(struct kryo_regulator *kvreg)
  429. {
  430. kryo_set_retention_volt(kvreg, kvreg->retention_volt);
  431. kryo_set_ldo_volt(kvreg, kvreg->volt);
  432. }
  433. #define APC_PWR_GATE_DLY_INIT 0x00000101
  434. static int kryo_hw_init(struct kryo_regulator *kvreg)
  435. {
  436. /* Set up VREF_LDO and VREF_RET */
  437. kryo_ldo_voltage_init(kvreg);
  438. /* Program LDO and APM configuration registers */
  439. writel_relaxed(kvreg->ldo_config_init, kvreg->reg_base + APC_LDO_CFG);
  440. kryo_masked_write(kvreg, APC_APM_CFG, APM_CFG_MASK,
  441. kvreg->apm_config_init);
  442. /* Configure power gate sequencer delay */
  443. kryo_masked_write(kvreg, APC_PWR_GATE_DLY, APC_PWR_GATE_DLY_MASK,
  444. APC_PWR_GATE_DLY_INIT);
  445. /* Allow LDO retention mode only when it's safe to do so */
  446. kryo_pm_apcc_masked_write(kvreg,
  447. APCC_PWR_CTL_OVERRIDE,
  448. APCC_PGS_MASK(kvreg->cluster_num),
  449. APCC_PGS_MASK(kvreg->cluster_num));
  450. /* Complete the above writes before other accesses */
  451. mb();
  452. return 0;
  453. }
  454. static ssize_t kryo_dbg_mode_read(struct file *file, char __user *buff,
  455. size_t count, loff_t *ppos)
  456. {
  457. struct kryo_regulator *kvreg = file->private_data;
  458. char buf[10];
  459. int len = 0;
  460. u32 reg_val;
  461. unsigned long flags;
  462. if (!kvreg)
  463. return -ENODEV;
  464. /* Confirm HW state matches Kryo regulator device state */
  465. spin_lock_irqsave(&kvreg->slock, flags);
  466. reg_val = readl_relaxed(kvreg->reg_base + APC_PWR_GATE_MODE);
  467. if (((reg_val & PWR_GATE_SWITCH_MODE_MASK) == PWR_GATE_SWITCH_MODE_LDO
  468. && kvreg->mode != LDO_MODE) ||
  469. ((reg_val & PWR_GATE_SWITCH_MODE_MASK) == PWR_GATE_SWITCH_MODE_BHS
  470. && kvreg->mode != BHS_MODE)) {
  471. kvreg_err(kvreg, "HW state disagrees on PWR gate mode! reg=0x%x\n",
  472. reg_val);
  473. len = snprintf(buf, sizeof(buf), "ERR\n");
  474. } else {
  475. len = snprintf(buf, sizeof(buf), "%s\n",
  476. kvreg->mode == LDO_MODE ?
  477. "LDO" : "BHS");
  478. }
  479. spin_unlock_irqrestore(&kvreg->slock, flags);
  480. return simple_read_from_buffer(buff, count, ppos, buf, len);
  481. }
  482. static int kryo_dbg_base_open(struct inode *inode, struct file *file)
  483. {
  484. file->private_data = inode->i_private;
  485. return 0;
  486. }
  487. static const struct file_operations kryo_dbg_mode_fops = {
  488. .open = kryo_dbg_base_open,
  489. .read = kryo_dbg_mode_read,
  490. };
  491. static void kryo_debugfs_init(struct kryo_regulator *kvreg)
  492. {
  493. struct dentry *temp;
  494. if (IS_ERR_OR_NULL(kryo_debugfs_base)) {
  495. if (PTR_ERR(kryo_debugfs_base) != -ENODEV)
  496. kvreg_err(kvreg, "Base directory missing, cannot create debugfs nodes rc=%ld\n",
  497. PTR_ERR(kryo_debugfs_base));
  498. return;
  499. }
  500. kvreg->debugfs = debugfs_create_dir(kvreg->name, kryo_debugfs_base);
  501. if (IS_ERR_OR_NULL(kvreg->debugfs)) {
  502. kvreg_err(kvreg, "debugfs directory creation failed rc=%ld\n",
  503. PTR_ERR(kvreg->debugfs));
  504. return;
  505. }
  506. temp = debugfs_create_file("mode", 0444, kvreg->debugfs,
  507. kvreg, &kryo_dbg_mode_fops);
  508. if (IS_ERR_OR_NULL(temp)) {
  509. kvreg_err(kvreg, "mode node creation failed rc=%ld\n",
  510. PTR_ERR(temp));
  511. return;
  512. }
  513. }
  514. static void kryo_debugfs_deinit(struct kryo_regulator *kvreg)
  515. {
  516. debugfs_remove_recursive(kvreg->debugfs);
  517. }
  518. static void kryo_debugfs_base_init(void)
  519. {
  520. kryo_debugfs_base = debugfs_create_dir(KRYO_REGULATOR_DRIVER_NAME,
  521. NULL);
  522. if (IS_ERR_OR_NULL(kryo_debugfs_base)) {
  523. if (PTR_ERR(kryo_debugfs_base) != -ENODEV)
  524. pr_err("%s debugfs base directory creation failed rc=%ld\n",
  525. KRYO_REGULATOR_DRIVER_NAME,
  526. PTR_ERR(kryo_debugfs_base));
  527. }
  528. }
  529. static void kryo_debugfs_base_remove(void)
  530. {
  531. debugfs_remove_recursive(kryo_debugfs_base);
  532. }
  533. static int kryo_regulator_init_data(struct platform_device *pdev,
  534. struct kryo_regulator *kvreg)
  535. {
  536. int rc = 0;
  537. struct device *dev = &pdev->dev;
  538. struct resource *res;
  539. void __iomem *temp;
  540. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pm-apc");
  541. if (!res) {
  542. dev_err(dev, "PM APC register address missing\n");
  543. return -EINVAL;
  544. }
  545. kvreg->reg_base = devm_ioremap(dev, res->start, resource_size(res));
  546. if (!kvreg->reg_base) {
  547. dev_err(dev, "failed to map PM APC registers\n");
  548. return -ENOMEM;
  549. }
  550. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pm-apcc");
  551. if (!res) {
  552. dev_err(dev, "PM APCC register address missing\n");
  553. return -EINVAL;
  554. }
  555. kvreg->pm_apcc_base = devm_ioremap(dev, res->start, resource_size(res));
  556. if (!kvreg->pm_apcc_base) {
  557. dev_err(dev, "failed to map PM APCC registers\n");
  558. return -ENOMEM;
  559. }
  560. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "apcs-csr");
  561. if (!res) {
  562. dev_err(dev, "missing APCS CSR physical base address");
  563. return -EINVAL;
  564. }
  565. temp = ioremap(res->start, resource_size(res));
  566. if (!temp) {
  567. dev_err(dev, "failed to map APCS CSR registers\n");
  568. return -ENOMEM;
  569. }
  570. kvreg->version = readl_relaxed(temp + APCS_VERSION);
  571. iounmap(temp);
  572. rc = of_property_read_u32(dev->of_node,
  573. "qcom,vref-functional-step-voltage",
  574. &kvreg->vref_func_step_volt);
  575. if (rc < 0) {
  576. dev_err(dev, "qcom,vref-functional-step-voltage missing rc=%d\n",
  577. rc);
  578. return rc;
  579. }
  580. rc = of_property_read_u32(dev->of_node,
  581. "qcom,vref-functional-min-voltage",
  582. &kvreg->vref_func_min_volt);
  583. if (rc < 0) {
  584. dev_err(dev, "qcom,vref-functional-min-voltage missing rc=%d\n",
  585. rc);
  586. return rc;
  587. }
  588. kvreg->vref_func_max_volt = kryo_decode_functional_volt(kvreg,
  589. LDO_N_VOLTAGES - 1);
  590. rc = of_property_read_u32(dev->of_node,
  591. "qcom,vref-retention-step-voltage",
  592. &kvreg->vref_ret_step_volt);
  593. if (rc < 0) {
  594. dev_err(dev, "qcom,vref-retention-step-voltage missing rc=%d\n",
  595. rc);
  596. return rc;
  597. }
  598. rc = of_property_read_u32(dev->of_node,
  599. "qcom,vref-retention-min-voltage",
  600. &kvreg->vref_ret_min_volt);
  601. if (rc < 0) {
  602. dev_err(dev, "qcom,vref-retention-min-voltage missing rc=%d\n",
  603. rc);
  604. return rc;
  605. }
  606. kvreg->vref_ret_max_volt = kryo_decode_retention_volt(kvreg,
  607. LDO_N_VOLTAGES - 1);
  608. rc = of_property_read_u32(dev->of_node, "qcom,ldo-default-voltage",
  609. &kvreg->volt);
  610. if (rc < 0) {
  611. dev_err(dev, "qcom,ldo-default-voltage missing rc=%d\n", rc);
  612. return rc;
  613. }
  614. if (!is_between(kvreg->vref_func_min_volt,
  615. kvreg->vref_func_max_volt,
  616. kvreg->volt)) {
  617. dev_err(dev, "qcom,ldo-default-voltage=%d uV outside allowed range\n",
  618. kvreg->volt);
  619. return -EINVAL;
  620. }
  621. rc = of_property_read_u32(dev->of_node, "qcom,retention-voltage",
  622. &kvreg->retention_volt);
  623. if (rc < 0) {
  624. dev_err(dev, "qcom,retention-voltage missing rc=%d\n", rc);
  625. return rc;
  626. }
  627. if (!is_between(kvreg->vref_ret_min_volt,
  628. kvreg->vref_ret_max_volt,
  629. kvreg->retention_volt)) {
  630. dev_err(dev, "qcom,retention-voltage=%d uV outside allowed range\n",
  631. kvreg->retention_volt);
  632. return -EINVAL;
  633. }
  634. rc = of_property_read_u32(dev->of_node, "qcom,ldo-headroom-voltage",
  635. &kvreg->headroom_volt);
  636. if (rc < 0) {
  637. dev_err(dev, "qcom,ldo-headroom-voltage missing rc=%d\n", rc);
  638. return rc;
  639. }
  640. rc = of_property_read_u32(dev->of_node, "qcom,ldo-config-init",
  641. &kvreg->ldo_config_init);
  642. if (rc < 0) {
  643. dev_err(dev, "qcom,ldo-config-init missing rc=%d\n", rc);
  644. return rc;
  645. }
  646. rc = of_property_read_u32(dev->of_node, "qcom,apm-config-init",
  647. &kvreg->apm_config_init);
  648. if (rc < 0) {
  649. dev_err(dev, "qcom,apm-config-init missing rc=%d\n", rc);
  650. return rc;
  651. }
  652. rc = of_property_read_u32(dev->of_node, "qcom,cluster-num",
  653. &kvreg->cluster_num);
  654. if (rc < 0) {
  655. dev_err(dev, "qcom,cluster-num missing rc=%d\n", rc);
  656. return rc;
  657. }
  658. return rc;
  659. }
  660. static int kryo_regulator_retention_init(struct kryo_regulator *kvreg,
  661. struct platform_device *pdev,
  662. struct device_node *ret_node)
  663. {
  664. struct device *dev = &pdev->dev;
  665. struct regulator_init_data *init_data;
  666. struct regulator_config reg_config = {};
  667. int rc = 0;
  668. init_data = of_get_regulator_init_data(dev, ret_node,
  669. &kvreg->retention_desc);
  670. if (!init_data) {
  671. kvreg_err(kvreg, "regulator init data is missing\n");
  672. return -EINVAL;
  673. }
  674. if (!init_data->constraints.name) {
  675. kvreg_err(kvreg, "regulator name is missing from constraints\n");
  676. return -EINVAL;
  677. }
  678. init_data->constraints.valid_ops_mask |= REGULATOR_CHANGE_BYPASS
  679. | REGULATOR_CHANGE_VOLTAGE;
  680. init_data->constraints.input_uV = init_data->constraints.max_uV;
  681. kvreg->retention_desc.name = init_data->constraints.name;
  682. kvreg->retention_desc.n_voltages = LDO_N_VOLTAGES;
  683. kvreg->retention_desc.ops = &kryo_regulator_retention_ops;
  684. kvreg->retention_desc.type = REGULATOR_VOLTAGE;
  685. kvreg->retention_desc.owner = THIS_MODULE;
  686. reg_config.dev = dev;
  687. reg_config.init_data = init_data;
  688. reg_config.driver_data = kvreg;
  689. reg_config.of_node = ret_node;
  690. kvreg->retention_rdev = regulator_register(&kvreg->retention_desc,
  691. &reg_config);
  692. if (IS_ERR(kvreg->retention_rdev)) {
  693. rc = PTR_ERR(kvreg->retention_rdev);
  694. kvreg_err(kvreg, "regulator_register failed, rc=%d\n", rc);
  695. return rc;
  696. }
  697. return rc;
  698. }
  699. static int kryo_regulator_lpm_prepare(struct kryo_regulator *kvreg)
  700. {
  701. int vdd_volt_uv, bhs_volt, vdd_vlvl = 0;
  702. unsigned long flags;
  703. spin_lock_irqsave(&kvreg->slock, flags);
  704. kvreg->pre_lpm_state_mode = kvreg->mode;
  705. kvreg->pre_lpm_state_volt = kvreg->volt;
  706. if (kvreg->mode == LDO_MODE) {
  707. if (!vdd_vlvl) {
  708. vdd_vlvl = msm_spm_get_vdd(SHARED_CPU_REG_NUM);
  709. if (vdd_vlvl < 0) {
  710. kvreg_err(kvreg, "could not get vdd supply voltage level, rc=%d\n",
  711. vdd_vlvl);
  712. spin_unlock_irqrestore(&kvreg->slock, flags);
  713. return NOTIFY_BAD;
  714. }
  715. vdd_volt_uv = vdd_vlvl * VDD_SUPPLY_STEP_UV
  716. + VDD_SUPPLY_MIN_UV;
  717. }
  718. kvreg_debug(kvreg, "switching to BHS mode, vdd_apcc=%d uV, current LDO Vref=%d, LPM enter count=%lx\n",
  719. vdd_volt_uv, kvreg->volt, kvreg->lpm_enter_count);
  720. /*
  721. * Program vdd supply minus LDO headroom as voltage.
  722. * Cap this value to the maximum physically supported
  723. * LDO voltage, if necessary.
  724. */
  725. bhs_volt = vdd_volt_uv - kvreg->headroom_volt;
  726. if (bhs_volt > kvreg->vref_func_max_volt) {
  727. kvreg_debug(kvreg, "limited to LDO output of %d uV when switching to BHS mode\n",
  728. kvreg->vref_func_max_volt);
  729. bhs_volt = kvreg->vref_func_max_volt;
  730. }
  731. kryo_set_ldo_volt(kvreg, bhs_volt);
  732. /* Switch Power Gate Mode */
  733. kryo_configure_mode(kvreg, BHS_MODE);
  734. }
  735. kvreg->lpm_enter_count++;
  736. spin_unlock_irqrestore(&kvreg->slock, flags);
  737. return NOTIFY_OK;
  738. }
  739. static int kryo_regulator_lpm_resume(struct kryo_regulator *kvreg)
  740. {
  741. unsigned long flags;
  742. spin_lock_irqsave(&kvreg->slock, flags);
  743. if (kvreg->mode == BHS_MODE &&
  744. kvreg->pre_lpm_state_mode == LDO_MODE) {
  745. kvreg_debug(kvreg, "switching to LDO mode, cached LDO Vref=%d, LPM exit count=%lx\n",
  746. kvreg->pre_lpm_state_volt, kvreg->lpm_exit_count);
  747. /*
  748. * Cached voltage value corresponds to vdd supply minus
  749. * LDO headroom, reprogram it.
  750. */
  751. kryo_set_ldo_volt(kvreg, kvreg->volt);
  752. /* Switch Power Gate Mode */
  753. kryo_configure_mode(kvreg, LDO_MODE);
  754. /* Request final LDO output voltage */
  755. kryo_set_ldo_volt(kvreg, kvreg->pre_lpm_state_volt);
  756. }
  757. kvreg->lpm_exit_count++;
  758. spin_unlock_irqrestore(&kvreg->slock, flags);
  759. if (kvreg->lpm_exit_count != kvreg->lpm_enter_count) {
  760. kvreg_err(kvreg, "LPM entry/exit counter mismatch, this is not expected: enter=%lx exit=%lx\n",
  761. kvreg->lpm_enter_count, kvreg->lpm_exit_count);
  762. BUG_ON(1);
  763. }
  764. return NOTIFY_OK;
  765. }
  766. static int kryo_regulator_cpu_pm_callback(struct notifier_block *self,
  767. unsigned long cmd, void *v)
  768. {
  769. struct kryo_regulator *kvreg = container_of(self, struct kryo_regulator,
  770. cpu_pm_notifier);
  771. unsigned long aff_level = (unsigned long) v;
  772. int rc = NOTIFY_OK;
  773. switch (cmd) {
  774. case CPU_CLUSTER_PM_ENTER:
  775. if (aff_level == AFFINITY_LEVEL_M3)
  776. rc = kryo_regulator_lpm_prepare(kvreg);
  777. break;
  778. case CPU_CLUSTER_PM_EXIT:
  779. if (aff_level == AFFINITY_LEVEL_M3)
  780. rc = kryo_regulator_lpm_resume(kvreg);
  781. break;
  782. }
  783. return rc;
  784. }
  785. static int kryo_regulator_probe(struct platform_device *pdev)
  786. {
  787. struct device *dev = &pdev->dev;
  788. struct kryo_regulator *kvreg;
  789. struct regulator_config reg_config = {};
  790. struct regulator_init_data *init_data = pdev->dev.platform_data;
  791. struct device_node *child;
  792. int rc = 0;
  793. if (!dev->of_node) {
  794. dev_err(dev, "Device tree node is missing\n");
  795. return -ENODEV;
  796. }
  797. init_data = of_get_regulator_init_data(dev, dev->of_node, NULL);
  798. if (!init_data) {
  799. dev_err(dev, "regulator init data is missing\n");
  800. return -EINVAL;
  801. }
  802. if (!init_data->constraints.name) {
  803. dev_err(dev, "regulator name is missing from constraints\n");
  804. return -EINVAL;
  805. }
  806. init_data->constraints.valid_ops_mask |= REGULATOR_CHANGE_VOLTAGE
  807. | REGULATOR_CHANGE_BYPASS | REGULATOR_CHANGE_STATUS;
  808. init_data->constraints.input_uV = init_data->constraints.max_uV;
  809. kvreg = devm_kzalloc(dev, sizeof(*kvreg), GFP_KERNEL);
  810. if (!kvreg)
  811. return -ENOMEM;
  812. rc = kryo_regulator_init_data(pdev, kvreg);
  813. if (rc) {
  814. dev_err(dev, "could not parse and ioremap all device tree properties\n");
  815. return rc;
  816. }
  817. spin_lock_init(&kvreg->slock);
  818. kvreg->name = init_data->constraints.name;
  819. kvreg->desc.name = kvreg->name;
  820. kvreg->desc.n_voltages = LDO_N_VOLTAGES;
  821. kvreg->desc.ops = &kryo_regulator_ops;
  822. kvreg->desc.type = REGULATOR_VOLTAGE;
  823. kvreg->desc.owner = THIS_MODULE;
  824. kvreg->mode = BHS_MODE;
  825. for_each_available_child_of_node(dev->of_node, child) {
  826. kryo_regulator_retention_init(kvreg, pdev, child);
  827. if (rc) {
  828. dev_err(dev, "could not initialize retention regulator, rc=%d\n",
  829. rc);
  830. return rc;
  831. }
  832. break;
  833. }
  834. /* CPUSS PM Register Initialization */
  835. rc = kryo_hw_init(kvreg);
  836. if (rc) {
  837. dev_err(dev, "unable to perform CPUSS PM initialization sequence\n");
  838. return rc;
  839. }
  840. reg_config.dev = dev;
  841. reg_config.init_data = init_data;
  842. reg_config.driver_data = kvreg;
  843. reg_config.of_node = dev->of_node;
  844. kvreg->rdev = regulator_register(&kvreg->desc, &reg_config);
  845. if (IS_ERR(kvreg->rdev)) {
  846. rc = PTR_ERR(kvreg->rdev);
  847. kvreg_err(kvreg, "regulator_register failed, rc=%d\n", rc);
  848. return rc;
  849. }
  850. platform_set_drvdata(pdev, kvreg);
  851. kryo_debugfs_init(kvreg);
  852. mutex_lock(&kryo_regulator_list_mutex);
  853. list_add_tail(&kvreg->link, &kryo_regulator_list);
  854. mutex_unlock(&kryo_regulator_list_mutex);
  855. kvreg->cpu_pm_notifier.notifier_call = kryo_regulator_cpu_pm_callback;
  856. cpu_pm_register_notifier(&kvreg->cpu_pm_notifier);
  857. kvreg_debug(kvreg, "registered cpu pm notifier\n");
  858. kvreg_info(kvreg, "default LDO functional volt=%d uV, LDO retention volt=%d uV, Vref func=%d + %d*(val), cluster-num=%d\n",
  859. kvreg->volt, kvreg->retention_volt,
  860. kvreg->vref_func_min_volt,
  861. kvreg->vref_func_step_volt,
  862. kvreg->cluster_num);
  863. return rc;
  864. }
  865. static int kryo_regulator_remove(struct platform_device *pdev)
  866. {
  867. struct kryo_regulator *kvreg = platform_get_drvdata(pdev);
  868. mutex_lock(&kryo_regulator_list_mutex);
  869. list_del(&kvreg->link);
  870. mutex_unlock(&kryo_regulator_list_mutex);
  871. cpu_pm_unregister_notifier(&kvreg->cpu_pm_notifier);
  872. regulator_unregister(kvreg->rdev);
  873. platform_set_drvdata(pdev, NULL);
  874. kryo_debugfs_deinit(kvreg);
  875. return 0;
  876. }
  877. static const struct of_device_id kryo_regulator_match_table[] = {
  878. { .compatible = "qcom,kryo-regulator", },
  879. {}
  880. };
  881. static struct platform_driver kryo_regulator_driver = {
  882. .probe = kryo_regulator_probe,
  883. .remove = kryo_regulator_remove,
  884. .driver = {
  885. .name = KRYO_REGULATOR_DRIVER_NAME,
  886. .of_match_table = kryo_regulator_match_table,
  887. .owner = THIS_MODULE,
  888. },
  889. };
  890. static int __init kryo_regulator_init(void)
  891. {
  892. kryo_debugfs_base_init();
  893. return platform_driver_register(&kryo_regulator_driver);
  894. }
  895. static void __exit kryo_regulator_exit(void)
  896. {
  897. platform_driver_unregister(&kryo_regulator_driver);
  898. kryo_debugfs_base_remove();
  899. }
  900. MODULE_DESCRIPTION("Kryo regulator driver");
  901. MODULE_LICENSE("GPL v2");
  902. arch_initcall(kryo_regulator_init);
  903. module_exit(kryo_regulator_exit);