197fb5e8dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 2e92a4047SStephen Boyd /* 3e92a4047SStephen Boyd * Copyright (c) 2012-2015, The Linux Foundation. All rights reserved. 4e92a4047SStephen Boyd */ 5e92a4047SStephen Boyd 6e92a4047SStephen Boyd #include <linux/module.h> 7e92a4047SStephen Boyd #include <linux/delay.h> 8e92a4047SStephen Boyd #include <linux/err.h> 9e92a4047SStephen Boyd #include <linux/kernel.h> 10e92a4047SStephen Boyd #include <linux/interrupt.h> 11e92a4047SStephen Boyd #include <linux/bitops.h> 12e92a4047SStephen Boyd #include <linux/slab.h> 13e92a4047SStephen Boyd #include <linux/of.h> 14e92a4047SStephen Boyd #include <linux/of_device.h> 15e92a4047SStephen Boyd #include <linux/platform_device.h> 16e92a4047SStephen Boyd #include <linux/ktime.h> 17e92a4047SStephen Boyd #include <linux/regulator/driver.h> 18e92a4047SStephen Boyd #include <linux/regmap.h> 19e92a4047SStephen Boyd #include <linux/list.h> 200caecaa8SIlia Lin #include <linux/mfd/syscon.h> 210caecaa8SIlia Lin #include <linux/io.h> 22e92a4047SStephen Boyd 23e2adfacdSStephen Boyd /* Pin control enable input pins. */ 24e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_NONE 0x00 25e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN0 0x01 26e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN1 0x02 27e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN2 0x04 28e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_EN3 0x08 29e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT 0x10 30e2adfacdSStephen Boyd 31e2adfacdSStephen Boyd /* Pin control high power mode input pins. */ 32e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_NONE 0x00 33e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN0 0x01 34e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN1 0x02 35e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN2 0x04 36e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_EN3 0x08 37e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_SLEEP_B 0x10 38e2adfacdSStephen Boyd #define SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT 0x20 39e2adfacdSStephen Boyd 40e2adfacdSStephen Boyd /* 41e2adfacdSStephen Boyd * Used with enable parameters to specify that hardware default register values 42e2adfacdSStephen Boyd * should be left unaltered. 43e2adfacdSStephen Boyd */ 44e2adfacdSStephen Boyd #define SPMI_REGULATOR_USE_HW_DEFAULT 2 45e2adfacdSStephen Boyd 46e2adfacdSStephen Boyd /* Soft start strength of a voltage switch type regulator */ 47e2adfacdSStephen Boyd enum spmi_vs_soft_start_str { 48e2adfacdSStephen Boyd SPMI_VS_SOFT_START_STR_0P05_UA = 0, 49e2adfacdSStephen Boyd SPMI_VS_SOFT_START_STR_0P25_UA, 50e2adfacdSStephen Boyd SPMI_VS_SOFT_START_STR_0P55_UA, 51e2adfacdSStephen Boyd SPMI_VS_SOFT_START_STR_0P75_UA, 52e2adfacdSStephen Boyd SPMI_VS_SOFT_START_STR_HW_DEFAULT, 53e2adfacdSStephen Boyd }; 54e2adfacdSStephen Boyd 55e2adfacdSStephen Boyd /** 56e2adfacdSStephen Boyd * struct spmi_regulator_init_data - spmi-regulator initialization data 57e2adfacdSStephen Boyd * @pin_ctrl_enable: Bit mask specifying which hardware pins should be 58e2adfacdSStephen Boyd * used to enable the regulator, if any 59e2adfacdSStephen Boyd * Value should be an ORing of 60e2adfacdSStephen Boyd * SPMI_REGULATOR_PIN_CTRL_ENABLE_* constants. If 61e2adfacdSStephen Boyd * the bit specified by 62e2adfacdSStephen Boyd * SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT is 63e2adfacdSStephen Boyd * set, then pin control enable hardware registers 64e2adfacdSStephen Boyd * will not be modified. 65e2adfacdSStephen Boyd * @pin_ctrl_hpm: Bit mask specifying which hardware pins should be 66e2adfacdSStephen Boyd * used to force the regulator into high power 67e2adfacdSStephen Boyd * mode, if any 68e2adfacdSStephen Boyd * Value should be an ORing of 69e2adfacdSStephen Boyd * SPMI_REGULATOR_PIN_CTRL_HPM_* constants. If 70e2adfacdSStephen Boyd * the bit specified by 71e2adfacdSStephen Boyd * SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT is 72e2adfacdSStephen Boyd * set, then pin control mode hardware registers 73e2adfacdSStephen Boyd * will not be modified. 74e2adfacdSStephen Boyd * @vs_soft_start_strength: This parameter sets the soft start strength for 75e2adfacdSStephen Boyd * voltage switch type regulators. Its value 76e2adfacdSStephen Boyd * should be one of SPMI_VS_SOFT_START_STR_*. If 77e2adfacdSStephen Boyd * its value is SPMI_VS_SOFT_START_STR_HW_DEFAULT, 78e2adfacdSStephen Boyd * then the soft start strength will be left at its 79e2adfacdSStephen Boyd * default hardware value. 80e2adfacdSStephen Boyd */ 81e2adfacdSStephen Boyd struct spmi_regulator_init_data { 82e2adfacdSStephen Boyd unsigned pin_ctrl_enable; 83e2adfacdSStephen Boyd unsigned pin_ctrl_hpm; 84e2adfacdSStephen Boyd enum spmi_vs_soft_start_str vs_soft_start_strength; 85e2adfacdSStephen Boyd }; 86e2adfacdSStephen Boyd 87e92a4047SStephen Boyd /* These types correspond to unique register layouts. */ 88e92a4047SStephen Boyd enum spmi_regulator_logical_type { 89e92a4047SStephen Boyd SPMI_REGULATOR_LOGICAL_TYPE_SMPS, 90e92a4047SStephen Boyd SPMI_REGULATOR_LOGICAL_TYPE_LDO, 91e92a4047SStephen Boyd SPMI_REGULATOR_LOGICAL_TYPE_VS, 92e92a4047SStephen Boyd SPMI_REGULATOR_LOGICAL_TYPE_BOOST, 93e92a4047SStephen Boyd SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS, 94e92a4047SStephen Boyd SPMI_REGULATOR_LOGICAL_TYPE_BOOST_BYP, 95e92a4047SStephen Boyd SPMI_REGULATOR_LOGICAL_TYPE_LN_LDO, 96e92a4047SStephen Boyd SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS, 97e92a4047SStephen Boyd SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS, 98e92a4047SStephen Boyd SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO, 9942ba89c8SJeffrey Hugo SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS426, 1000211f68eSJorge Ramirez SPMI_REGULATOR_LOGICAL_TYPE_HFS430, 101e92a4047SStephen Boyd }; 102e92a4047SStephen Boyd 103e92a4047SStephen Boyd enum spmi_regulator_type { 104e92a4047SStephen Boyd SPMI_REGULATOR_TYPE_BUCK = 0x03, 105e92a4047SStephen Boyd SPMI_REGULATOR_TYPE_LDO = 0x04, 106e92a4047SStephen Boyd SPMI_REGULATOR_TYPE_VS = 0x05, 107e92a4047SStephen Boyd SPMI_REGULATOR_TYPE_BOOST = 0x1b, 108e92a4047SStephen Boyd SPMI_REGULATOR_TYPE_FTS = 0x1c, 109e92a4047SStephen Boyd SPMI_REGULATOR_TYPE_BOOST_BYP = 0x1f, 110e92a4047SStephen Boyd SPMI_REGULATOR_TYPE_ULT_LDO = 0x21, 111e92a4047SStephen Boyd SPMI_REGULATOR_TYPE_ULT_BUCK = 0x22, 112e92a4047SStephen Boyd }; 113e92a4047SStephen Boyd 114e92a4047SStephen Boyd enum spmi_regulator_subtype { 115e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_GP_CTL = 0x08, 116e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_RF_CTL = 0x09, 117e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_N50 = 0x01, 118e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_N150 = 0x02, 119e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_N300 = 0x03, 120e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_N600 = 0x04, 121e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_N1200 = 0x05, 122e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_N600_ST = 0x06, 123e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_N1200_ST = 0x07, 124e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_N900_ST = 0x14, 125e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_N300_ST = 0x15, 126e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_P50 = 0x08, 127e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_P150 = 0x09, 128e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_P300 = 0x0a, 129e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_P600 = 0x0b, 130e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_P1200 = 0x0c, 131e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_LN = 0x10, 132e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_LV_P50 = 0x28, 133e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_LV_P150 = 0x29, 134e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_LV_P300 = 0x2a, 135e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_LV_P600 = 0x2b, 136e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_LV_P1200 = 0x2c, 137e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_LV_P450 = 0x2d, 138e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_LV100 = 0x01, 139e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_LV300 = 0x02, 140e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_MV300 = 0x08, 141e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_MV500 = 0x09, 142e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_HDMI = 0x10, 143e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_OTG = 0x11, 144e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_5V_BOOST = 0x01, 145e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_FTS_CTL = 0x08, 146e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_FTS2p5_CTL = 0x09, 14742ba89c8SJeffrey Hugo SPMI_REGULATOR_SUBTYPE_FTS426_CTL = 0x0a, 148e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_BB_2A = 0x01, 149e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL1 = 0x0d, 150e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL2 = 0x0e, 151e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL3 = 0x0f, 152e92a4047SStephen Boyd SPMI_REGULATOR_SUBTYPE_ULT_HF_CTL4 = 0x10, 1530211f68eSJorge Ramirez SPMI_REGULATOR_SUBTYPE_HFS430 = 0x0a, 154e92a4047SStephen Boyd }; 155e92a4047SStephen Boyd 156e92a4047SStephen Boyd enum spmi_common_regulator_registers { 157e92a4047SStephen Boyd SPMI_COMMON_REG_DIG_MAJOR_REV = 0x01, 158e92a4047SStephen Boyd SPMI_COMMON_REG_TYPE = 0x04, 159e92a4047SStephen Boyd SPMI_COMMON_REG_SUBTYPE = 0x05, 160e92a4047SStephen Boyd SPMI_COMMON_REG_VOLTAGE_RANGE = 0x40, 161e92a4047SStephen Boyd SPMI_COMMON_REG_VOLTAGE_SET = 0x41, 162e92a4047SStephen Boyd SPMI_COMMON_REG_MODE = 0x45, 163e92a4047SStephen Boyd SPMI_COMMON_REG_ENABLE = 0x46, 164e92a4047SStephen Boyd SPMI_COMMON_REG_PULL_DOWN = 0x48, 165e92a4047SStephen Boyd SPMI_COMMON_REG_SOFT_START = 0x4c, 166e92a4047SStephen Boyd SPMI_COMMON_REG_STEP_CTRL = 0x61, 167e92a4047SStephen Boyd }; 168e92a4047SStephen Boyd 16942ba89c8SJeffrey Hugo /* 17042ba89c8SJeffrey Hugo * Second common register layout used by newer devices starting with ftsmps426 17142ba89c8SJeffrey Hugo * Note that some of the registers from the first common layout remain 17242ba89c8SJeffrey Hugo * unchanged and their definition is not duplicated. 17342ba89c8SJeffrey Hugo */ 17442ba89c8SJeffrey Hugo enum spmi_ftsmps426_regulator_registers { 17542ba89c8SJeffrey Hugo SPMI_FTSMPS426_REG_VOLTAGE_LSB = 0x40, 17642ba89c8SJeffrey Hugo SPMI_FTSMPS426_REG_VOLTAGE_MSB = 0x41, 17742ba89c8SJeffrey Hugo SPMI_FTSMPS426_REG_VOLTAGE_ULS_LSB = 0x68, 17842ba89c8SJeffrey Hugo SPMI_FTSMPS426_REG_VOLTAGE_ULS_MSB = 0x69, 17942ba89c8SJeffrey Hugo }; 18042ba89c8SJeffrey Hugo 181e92a4047SStephen Boyd enum spmi_vs_registers { 182e92a4047SStephen Boyd SPMI_VS_REG_OCP = 0x4a, 183e92a4047SStephen Boyd SPMI_VS_REG_SOFT_START = 0x4c, 184e92a4047SStephen Boyd }; 185e92a4047SStephen Boyd 186e92a4047SStephen Boyd enum spmi_boost_registers { 187e92a4047SStephen Boyd SPMI_BOOST_REG_CURRENT_LIMIT = 0x4a, 188e92a4047SStephen Boyd }; 189e92a4047SStephen Boyd 190e92a4047SStephen Boyd enum spmi_boost_byp_registers { 191e92a4047SStephen Boyd SPMI_BOOST_BYP_REG_CURRENT_LIMIT = 0x4b, 192e92a4047SStephen Boyd }; 193e92a4047SStephen Boyd 1940caecaa8SIlia Lin enum spmi_saw3_registers { 1950caecaa8SIlia Lin SAW3_SECURE = 0x00, 1960caecaa8SIlia Lin SAW3_ID = 0x04, 1970caecaa8SIlia Lin SAW3_SPM_STS = 0x0C, 1980caecaa8SIlia Lin SAW3_AVS_STS = 0x10, 1990caecaa8SIlia Lin SAW3_PMIC_STS = 0x14, 2000caecaa8SIlia Lin SAW3_RST = 0x18, 2010caecaa8SIlia Lin SAW3_VCTL = 0x1C, 2020caecaa8SIlia Lin SAW3_AVS_CTL = 0x20, 2030caecaa8SIlia Lin SAW3_AVS_LIMIT = 0x24, 2040caecaa8SIlia Lin SAW3_AVS_DLY = 0x28, 2050caecaa8SIlia Lin SAW3_AVS_HYSTERESIS = 0x2C, 2060caecaa8SIlia Lin SAW3_SPM_STS2 = 0x38, 2070caecaa8SIlia Lin SAW3_SPM_PMIC_DATA_3 = 0x4C, 2080caecaa8SIlia Lin SAW3_VERSION = 0xFD0, 2090caecaa8SIlia Lin }; 2100caecaa8SIlia Lin 211e92a4047SStephen Boyd /* Used for indexing into ctrl_reg. These are offets from 0x40 */ 212e92a4047SStephen Boyd enum spmi_common_control_register_index { 213e92a4047SStephen Boyd SPMI_COMMON_IDX_VOLTAGE_RANGE = 0, 214e92a4047SStephen Boyd SPMI_COMMON_IDX_VOLTAGE_SET = 1, 215e92a4047SStephen Boyd SPMI_COMMON_IDX_MODE = 5, 216e92a4047SStephen Boyd SPMI_COMMON_IDX_ENABLE = 6, 217e92a4047SStephen Boyd }; 218e92a4047SStephen Boyd 219e92a4047SStephen Boyd /* Common regulator control register layout */ 220e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_MASK 0x80 221e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE 0x80 222e92a4047SStephen Boyd #define SPMI_COMMON_DISABLE 0x00 223e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN3_MASK 0x08 224e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN2_MASK 0x04 225e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN1_MASK 0x02 226e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_HW_EN0_MASK 0x01 227e92a4047SStephen Boyd #define SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK 0x0f 228e92a4047SStephen Boyd 229e92a4047SStephen Boyd /* Common regulator mode register layout */ 230e92a4047SStephen Boyd #define SPMI_COMMON_MODE_HPM_MASK 0x80 231e92a4047SStephen Boyd #define SPMI_COMMON_MODE_AUTO_MASK 0x40 232e92a4047SStephen Boyd #define SPMI_COMMON_MODE_BYPASS_MASK 0x20 233e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK 0x10 234e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN3_MASK 0x08 235e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN2_MASK 0x04 236e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN1_MASK 0x02 237e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_HW_EN0_MASK 0x01 238e92a4047SStephen Boyd #define SPMI_COMMON_MODE_FOLLOW_ALL_MASK 0x1f 239e92a4047SStephen Boyd 24042ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_MODE_BYPASS_MASK 3 24142ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_MODE_RETENTION_MASK 4 24242ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_MODE_LPM_MASK 5 24342ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_MODE_AUTO_MASK 6 24442ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_MODE_HPM_MASK 7 24542ba89c8SJeffrey Hugo 24642ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_MODE_MASK 0x07 24742ba89c8SJeffrey Hugo 248e92a4047SStephen Boyd /* Common regulator pull down control register layout */ 249e92a4047SStephen Boyd #define SPMI_COMMON_PULL_DOWN_ENABLE_MASK 0x80 250e92a4047SStephen Boyd 251e92a4047SStephen Boyd /* LDO regulator current limit control register layout */ 252e92a4047SStephen Boyd #define SPMI_LDO_CURRENT_LIMIT_ENABLE_MASK 0x80 253e92a4047SStephen Boyd 254e92a4047SStephen Boyd /* LDO regulator soft start control register layout */ 255e92a4047SStephen Boyd #define SPMI_LDO_SOFT_START_ENABLE_MASK 0x80 256e92a4047SStephen Boyd 257e92a4047SStephen Boyd /* VS regulator over current protection control register layout */ 258e92a4047SStephen Boyd #define SPMI_VS_OCP_OVERRIDE 0x01 259e92a4047SStephen Boyd #define SPMI_VS_OCP_NO_OVERRIDE 0x00 260e92a4047SStephen Boyd 261e92a4047SStephen Boyd /* VS regulator soft start control register layout */ 262e92a4047SStephen Boyd #define SPMI_VS_SOFT_START_ENABLE_MASK 0x80 263e92a4047SStephen Boyd #define SPMI_VS_SOFT_START_SEL_MASK 0x03 264e92a4047SStephen Boyd 265e92a4047SStephen Boyd /* Boost regulator current limit control register layout */ 266e92a4047SStephen Boyd #define SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK 0x80 267e92a4047SStephen Boyd #define SPMI_BOOST_CURRENT_LIMIT_MASK 0x07 268e92a4047SStephen Boyd 269e92a4047SStephen Boyd #define SPMI_VS_OCP_DEFAULT_MAX_RETRIES 10 270e92a4047SStephen Boyd #define SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS 30 271e92a4047SStephen Boyd #define SPMI_VS_OCP_FALL_DELAY_US 90 272e92a4047SStephen Boyd #define SPMI_VS_OCP_FAULT_DELAY_US 20000 273e92a4047SStephen Boyd 274e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_STEP_MASK 0x18 275e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT 3 276e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_DELAY_MASK 0x07 277e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT 0 278e92a4047SStephen Boyd 279e92a4047SStephen Boyd /* Clock rate in kHz of the FTSMPS regulator reference clock. */ 280e92a4047SStephen Boyd #define SPMI_FTSMPS_CLOCK_RATE 19200 281e92a4047SStephen Boyd 282e92a4047SStephen Boyd /* Minimum voltage stepper delay for each step. */ 283e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_DELAY 8 2842cf7b99cSStephen Boyd #define SPMI_DEFAULT_STEP_DELAY 20 285e92a4047SStephen Boyd 286e92a4047SStephen Boyd /* 287e92a4047SStephen Boyd * The ratio SPMI_FTSMPS_STEP_MARGIN_NUM/SPMI_FTSMPS_STEP_MARGIN_DEN is used to 288e92a4047SStephen Boyd * adjust the step rate in order to account for oscillator variance. 289e92a4047SStephen Boyd */ 290e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_MARGIN_NUM 4 291e92a4047SStephen Boyd #define SPMI_FTSMPS_STEP_MARGIN_DEN 5 292e92a4047SStephen Boyd 29342ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_STEP_CTRL_DELAY_MASK 0x03 29442ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_STEP_CTRL_DELAY_SHIFT 0 29542ba89c8SJeffrey Hugo 29642ba89c8SJeffrey Hugo /* Clock rate in kHz of the FTSMPS426 regulator reference clock. */ 29742ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_CLOCK_RATE 4800 29842ba89c8SJeffrey Hugo 2990211f68eSJorge Ramirez #define SPMI_HFS430_CLOCK_RATE 1600 3000211f68eSJorge Ramirez 30142ba89c8SJeffrey Hugo /* Minimum voltage stepper delay for each step. */ 30242ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_STEP_DELAY 2 30342ba89c8SJeffrey Hugo 30442ba89c8SJeffrey Hugo /* 30542ba89c8SJeffrey Hugo * The ratio SPMI_FTSMPS426_STEP_MARGIN_NUM/SPMI_FTSMPS426_STEP_MARGIN_DEN is 30642ba89c8SJeffrey Hugo * used to adjust the step rate in order to account for oscillator variance. 30742ba89c8SJeffrey Hugo */ 30842ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_STEP_MARGIN_NUM 10 30942ba89c8SJeffrey Hugo #define SPMI_FTSMPS426_STEP_MARGIN_DEN 11 31042ba89c8SJeffrey Hugo 31142ba89c8SJeffrey Hugo 312e92a4047SStephen Boyd /* VSET value to decide the range of ULT SMPS */ 313e92a4047SStephen Boyd #define ULT_SMPS_RANGE_SPLIT 0x60 314e92a4047SStephen Boyd 315e92a4047SStephen Boyd /** 316e92a4047SStephen Boyd * struct spmi_voltage_range - regulator set point voltage mapping description 317e92a4047SStephen Boyd * @min_uV: Minimum programmable output voltage resulting from 318e92a4047SStephen Boyd * set point register value 0x00 319e92a4047SStephen Boyd * @max_uV: Maximum programmable output voltage 320e92a4047SStephen Boyd * @step_uV: Output voltage increase resulting from the set point 321e92a4047SStephen Boyd * register value increasing by 1 322e92a4047SStephen Boyd * @set_point_min_uV: Minimum allowed voltage 323e92a4047SStephen Boyd * @set_point_max_uV: Maximum allowed voltage. This may be tweaked in order 324e92a4047SStephen Boyd * to pick which range should be used in the case of 325e92a4047SStephen Boyd * overlapping set points. 326e92a4047SStephen Boyd * @n_voltages: Number of preferred voltage set points present in this 327e92a4047SStephen Boyd * range 328e92a4047SStephen Boyd * @range_sel: Voltage range register value corresponding to this range 329e92a4047SStephen Boyd * 330e92a4047SStephen Boyd * The following relationships must be true for the values used in this struct: 331e92a4047SStephen Boyd * (max_uV - min_uV) % step_uV == 0 332e92a4047SStephen Boyd * (set_point_min_uV - min_uV) % step_uV == 0* 333e92a4047SStephen Boyd * (set_point_max_uV - min_uV) % step_uV == 0* 334e92a4047SStephen Boyd * n_voltages = (set_point_max_uV - set_point_min_uV) / step_uV + 1 335e92a4047SStephen Boyd * 336e92a4047SStephen Boyd * *Note, set_point_min_uV == set_point_max_uV == 0 is allowed in order to 337e92a4047SStephen Boyd * specify that the voltage range has meaning, but is not preferred. 338e92a4047SStephen Boyd */ 339e92a4047SStephen Boyd struct spmi_voltage_range { 340e92a4047SStephen Boyd int min_uV; 341e92a4047SStephen Boyd int max_uV; 342e92a4047SStephen Boyd int step_uV; 343e92a4047SStephen Boyd int set_point_min_uV; 344e92a4047SStephen Boyd int set_point_max_uV; 345e92a4047SStephen Boyd unsigned n_voltages; 346e92a4047SStephen Boyd u8 range_sel; 347e92a4047SStephen Boyd }; 348e92a4047SStephen Boyd 349e92a4047SStephen Boyd /* 350e92a4047SStephen Boyd * The ranges specified in the spmi_voltage_set_points struct must be listed 351e92a4047SStephen Boyd * so that range[i].set_point_max_uV < range[i+1].set_point_min_uV. 352e92a4047SStephen Boyd */ 353e92a4047SStephen Boyd struct spmi_voltage_set_points { 354e92a4047SStephen Boyd struct spmi_voltage_range *range; 355e92a4047SStephen Boyd int count; 356e92a4047SStephen Boyd unsigned n_voltages; 357e92a4047SStephen Boyd }; 358e92a4047SStephen Boyd 359e92a4047SStephen Boyd struct spmi_regulator { 360e92a4047SStephen Boyd struct regulator_desc desc; 361e92a4047SStephen Boyd struct device *dev; 362e92a4047SStephen Boyd struct delayed_work ocp_work; 363e92a4047SStephen Boyd struct regmap *regmap; 364e92a4047SStephen Boyd struct spmi_voltage_set_points *set_points; 365e92a4047SStephen Boyd enum spmi_regulator_logical_type logical_type; 366e92a4047SStephen Boyd int ocp_irq; 367e92a4047SStephen Boyd int ocp_count; 368e92a4047SStephen Boyd int ocp_max_retries; 369e92a4047SStephen Boyd int ocp_retry_delay_ms; 370e92a4047SStephen Boyd int hpm_min_load; 371e92a4047SStephen Boyd int slew_rate; 372e92a4047SStephen Boyd ktime_t vs_enable_time; 373e92a4047SStephen Boyd u16 base; 374e92a4047SStephen Boyd struct list_head node; 375e92a4047SStephen Boyd }; 376e92a4047SStephen Boyd 377e92a4047SStephen Boyd struct spmi_regulator_mapping { 378e92a4047SStephen Boyd enum spmi_regulator_type type; 379e92a4047SStephen Boyd enum spmi_regulator_subtype subtype; 380e92a4047SStephen Boyd enum spmi_regulator_logical_type logical_type; 381e92a4047SStephen Boyd u32 revision_min; 382e92a4047SStephen Boyd u32 revision_max; 383e92a4047SStephen Boyd struct regulator_ops *ops; 384e92a4047SStephen Boyd struct spmi_voltage_set_points *set_points; 385e92a4047SStephen Boyd int hpm_min_load; 386e92a4047SStephen Boyd }; 387e92a4047SStephen Boyd 388e92a4047SStephen Boyd struct spmi_regulator_data { 389e92a4047SStephen Boyd const char *name; 390e92a4047SStephen Boyd u16 base; 391e92a4047SStephen Boyd const char *supply; 392e92a4047SStephen Boyd const char *ocp; 393e92a4047SStephen Boyd u16 force_type; 394e92a4047SStephen Boyd }; 395e92a4047SStephen Boyd 396e92a4047SStephen Boyd #define SPMI_VREG(_type, _subtype, _dig_major_min, _dig_major_max, \ 397e92a4047SStephen Boyd _logical_type, _ops_val, _set_points_val, _hpm_min_load) \ 398e92a4047SStephen Boyd { \ 399e92a4047SStephen Boyd .type = SPMI_REGULATOR_TYPE_##_type, \ 400e92a4047SStephen Boyd .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \ 401e92a4047SStephen Boyd .revision_min = _dig_major_min, \ 402e92a4047SStephen Boyd .revision_max = _dig_major_max, \ 403e92a4047SStephen Boyd .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_##_logical_type, \ 404e92a4047SStephen Boyd .ops = &spmi_##_ops_val##_ops, \ 405e92a4047SStephen Boyd .set_points = &_set_points_val##_set_points, \ 406e92a4047SStephen Boyd .hpm_min_load = _hpm_min_load, \ 407e92a4047SStephen Boyd } 408e92a4047SStephen Boyd 409e92a4047SStephen Boyd #define SPMI_VREG_VS(_subtype, _dig_major_min, _dig_major_max) \ 410e92a4047SStephen Boyd { \ 411e92a4047SStephen Boyd .type = SPMI_REGULATOR_TYPE_VS, \ 412e92a4047SStephen Boyd .subtype = SPMI_REGULATOR_SUBTYPE_##_subtype, \ 413e92a4047SStephen Boyd .revision_min = _dig_major_min, \ 414e92a4047SStephen Boyd .revision_max = _dig_major_max, \ 415e92a4047SStephen Boyd .logical_type = SPMI_REGULATOR_LOGICAL_TYPE_VS, \ 416e92a4047SStephen Boyd .ops = &spmi_vs_ops, \ 417e92a4047SStephen Boyd } 418e92a4047SStephen Boyd 419e92a4047SStephen Boyd #define SPMI_VOLTAGE_RANGE(_range_sel, _min_uV, _set_point_min_uV, \ 420e92a4047SStephen Boyd _set_point_max_uV, _max_uV, _step_uV) \ 421e92a4047SStephen Boyd { \ 422e92a4047SStephen Boyd .min_uV = _min_uV, \ 423e92a4047SStephen Boyd .max_uV = _max_uV, \ 424e92a4047SStephen Boyd .set_point_min_uV = _set_point_min_uV, \ 425e92a4047SStephen Boyd .set_point_max_uV = _set_point_max_uV, \ 426e92a4047SStephen Boyd .step_uV = _step_uV, \ 427e92a4047SStephen Boyd .range_sel = _range_sel, \ 428e92a4047SStephen Boyd } 429e92a4047SStephen Boyd 430e92a4047SStephen Boyd #define DEFINE_SPMI_SET_POINTS(name) \ 431e92a4047SStephen Boyd struct spmi_voltage_set_points name##_set_points = { \ 432e92a4047SStephen Boyd .range = name##_ranges, \ 433e92a4047SStephen Boyd .count = ARRAY_SIZE(name##_ranges), \ 434e92a4047SStephen Boyd } 435e92a4047SStephen Boyd 436e92a4047SStephen Boyd /* 437e92a4047SStephen Boyd * These tables contain the physically available PMIC regulator voltage setpoint 438e92a4047SStephen Boyd * ranges. Where two ranges overlap in hardware, one of the ranges is trimmed 439e92a4047SStephen Boyd * to ensure that the setpoints available to software are monotonically 440e92a4047SStephen Boyd * increasing and unique. The set_voltage callback functions expect these 441e92a4047SStephen Boyd * properties to hold. 442e92a4047SStephen Boyd */ 443e92a4047SStephen Boyd static struct spmi_voltage_range pldo_ranges[] = { 444e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500), 445e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(3, 1500000, 1550000, 3075000, 3075000, 25000), 446e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(4, 1750000, 3100000, 4900000, 4900000, 50000), 447e92a4047SStephen Boyd }; 448e92a4047SStephen Boyd 449e92a4047SStephen Boyd static struct spmi_voltage_range nldo1_ranges[] = { 450e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(2, 750000, 750000, 1537500, 1537500, 12500), 451e92a4047SStephen Boyd }; 452e92a4047SStephen Boyd 453e92a4047SStephen Boyd static struct spmi_voltage_range nldo2_ranges[] = { 454e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 375000, 0, 0, 1537500, 12500), 455e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(1, 375000, 375000, 768750, 768750, 6250), 456e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(2, 750000, 775000, 1537500, 1537500, 12500), 457e92a4047SStephen Boyd }; 458e92a4047SStephen Boyd 459e92a4047SStephen Boyd static struct spmi_voltage_range nldo3_ranges[] = { 460e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500), 461e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(1, 375000, 0, 0, 1537500, 12500), 462e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(2, 750000, 0, 0, 1537500, 12500), 463e92a4047SStephen Boyd }; 464e92a4047SStephen Boyd 465e92a4047SStephen Boyd static struct spmi_voltage_range ln_ldo_ranges[] = { 466e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(1, 690000, 690000, 1110000, 1110000, 60000), 467e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 1380000, 1380000, 2220000, 2220000, 120000), 468e92a4047SStephen Boyd }; 469e92a4047SStephen Boyd 470e92a4047SStephen Boyd static struct spmi_voltage_range smps_ranges[] = { 471e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500), 472e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(1, 1550000, 1575000, 3125000, 3125000, 25000), 473e92a4047SStephen Boyd }; 474e92a4047SStephen Boyd 475e92a4047SStephen Boyd static struct spmi_voltage_range ftsmps_ranges[] = { 476e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 0, 350000, 1275000, 1275000, 5000), 477e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(1, 0, 1280000, 2040000, 2040000, 10000), 478e92a4047SStephen Boyd }; 479e92a4047SStephen Boyd 480e92a4047SStephen Boyd static struct spmi_voltage_range ftsmps2p5_ranges[] = { 481e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 80000, 350000, 1355000, 1355000, 5000), 482e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(1, 160000, 1360000, 2200000, 2200000, 10000), 483e92a4047SStephen Boyd }; 484e92a4047SStephen Boyd 48542ba89c8SJeffrey Hugo static struct spmi_voltage_range ftsmps426_ranges[] = { 48642ba89c8SJeffrey Hugo SPMI_VOLTAGE_RANGE(0, 0, 320000, 1352000, 1352000, 4000), 48742ba89c8SJeffrey Hugo }; 48842ba89c8SJeffrey Hugo 489e92a4047SStephen Boyd static struct spmi_voltage_range boost_ranges[] = { 490e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 4000000, 4000000, 5550000, 5550000, 50000), 491e92a4047SStephen Boyd }; 492e92a4047SStephen Boyd 493e92a4047SStephen Boyd static struct spmi_voltage_range boost_byp_ranges[] = { 494e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 2500000, 2500000, 5200000, 5650000, 50000), 495e92a4047SStephen Boyd }; 496e92a4047SStephen Boyd 497e92a4047SStephen Boyd static struct spmi_voltage_range ult_lo_smps_ranges[] = { 498e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1562500, 1562500, 12500), 499e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(1, 750000, 0, 0, 1525000, 25000), 500e92a4047SStephen Boyd }; 501e92a4047SStephen Boyd 502e92a4047SStephen Boyd static struct spmi_voltage_range ult_ho_smps_ranges[] = { 503e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 1550000, 1550000, 2325000, 2325000, 25000), 504e92a4047SStephen Boyd }; 505e92a4047SStephen Boyd 506e92a4047SStephen Boyd static struct spmi_voltage_range ult_nldo_ranges[] = { 507e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 375000, 375000, 1537500, 1537500, 12500), 508e92a4047SStephen Boyd }; 509e92a4047SStephen Boyd 510e92a4047SStephen Boyd static struct spmi_voltage_range ult_pldo_ranges[] = { 511e92a4047SStephen Boyd SPMI_VOLTAGE_RANGE(0, 1750000, 1750000, 3337500, 3337500, 12500), 512e92a4047SStephen Boyd }; 513e92a4047SStephen Boyd 5140211f68eSJorge Ramirez static struct spmi_voltage_range hfs430_ranges[] = { 5150211f68eSJorge Ramirez SPMI_VOLTAGE_RANGE(0, 320000, 320000, 2040000, 2040000, 8000), 5160211f68eSJorge Ramirez }; 5170211f68eSJorge Ramirez 518e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(pldo); 519e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo1); 520e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo2); 521e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(nldo3); 522e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ln_ldo); 523e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(smps); 524e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ftsmps); 525e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ftsmps2p5); 52642ba89c8SJeffrey Hugo static DEFINE_SPMI_SET_POINTS(ftsmps426); 527e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(boost); 528e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(boost_byp); 529e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_lo_smps); 530e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_ho_smps); 531e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_nldo); 532e92a4047SStephen Boyd static DEFINE_SPMI_SET_POINTS(ult_pldo); 5330211f68eSJorge Ramirez static DEFINE_SPMI_SET_POINTS(hfs430); 534e92a4047SStephen Boyd 535e92a4047SStephen Boyd static inline int spmi_vreg_read(struct spmi_regulator *vreg, u16 addr, u8 *buf, 536e92a4047SStephen Boyd int len) 537e92a4047SStephen Boyd { 538e92a4047SStephen Boyd return regmap_bulk_read(vreg->regmap, vreg->base + addr, buf, len); 539e92a4047SStephen Boyd } 540e92a4047SStephen Boyd 541e92a4047SStephen Boyd static inline int spmi_vreg_write(struct spmi_regulator *vreg, u16 addr, 542e92a4047SStephen Boyd u8 *buf, int len) 543e92a4047SStephen Boyd { 544e92a4047SStephen Boyd return regmap_bulk_write(vreg->regmap, vreg->base + addr, buf, len); 545e92a4047SStephen Boyd } 546e92a4047SStephen Boyd 547e92a4047SStephen Boyd static int spmi_vreg_update_bits(struct spmi_regulator *vreg, u16 addr, u8 val, 548e92a4047SStephen Boyd u8 mask) 549e92a4047SStephen Boyd { 550e92a4047SStephen Boyd return regmap_update_bits(vreg->regmap, vreg->base + addr, mask, val); 551e92a4047SStephen Boyd } 552e92a4047SStephen Boyd 553e92a4047SStephen Boyd static int spmi_regulator_vs_enable(struct regulator_dev *rdev) 554e92a4047SStephen Boyd { 555e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 556e92a4047SStephen Boyd 557e92a4047SStephen Boyd if (vreg->ocp_irq) { 558e92a4047SStephen Boyd vreg->ocp_count = 0; 559e92a4047SStephen Boyd vreg->vs_enable_time = ktime_get(); 560e92a4047SStephen Boyd } 561e92a4047SStephen Boyd 5629d485332SAxel Lin return regulator_enable_regmap(rdev); 563e92a4047SStephen Boyd } 564e92a4047SStephen Boyd 565e2adfacdSStephen Boyd static int spmi_regulator_vs_ocp(struct regulator_dev *rdev) 566e2adfacdSStephen Boyd { 567e2adfacdSStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 568e2adfacdSStephen Boyd u8 reg = SPMI_VS_OCP_OVERRIDE; 569e2adfacdSStephen Boyd 570e2adfacdSStephen Boyd return spmi_vreg_write(vreg, SPMI_VS_REG_OCP, ®, 1); 571e2adfacdSStephen Boyd } 572e2adfacdSStephen Boyd 573e92a4047SStephen Boyd static int spmi_regulator_select_voltage(struct spmi_regulator *vreg, 5741b5b1968SStephen Boyd int min_uV, int max_uV) 575e92a4047SStephen Boyd { 576e92a4047SStephen Boyd const struct spmi_voltage_range *range; 577e92a4047SStephen Boyd int uV = min_uV; 578e92a4047SStephen Boyd int lim_min_uV, lim_max_uV, i, range_id, range_max_uV; 5791b5b1968SStephen Boyd int selector, voltage_sel; 580e92a4047SStephen Boyd 581e92a4047SStephen Boyd /* Check if request voltage is outside of physically settable range. */ 582e92a4047SStephen Boyd lim_min_uV = vreg->set_points->range[0].set_point_min_uV; 583e92a4047SStephen Boyd lim_max_uV = 584e92a4047SStephen Boyd vreg->set_points->range[vreg->set_points->count - 1].set_point_max_uV; 585e92a4047SStephen Boyd 586e92a4047SStephen Boyd if (uV < lim_min_uV && max_uV >= lim_min_uV) 587e92a4047SStephen Boyd uV = lim_min_uV; 588e92a4047SStephen Boyd 589e92a4047SStephen Boyd if (uV < lim_min_uV || uV > lim_max_uV) { 590e92a4047SStephen Boyd dev_err(vreg->dev, 591e92a4047SStephen Boyd "request v=[%d, %d] is outside possible v=[%d, %d]\n", 592e92a4047SStephen Boyd min_uV, max_uV, lim_min_uV, lim_max_uV); 593e92a4047SStephen Boyd return -EINVAL; 594e92a4047SStephen Boyd } 595e92a4047SStephen Boyd 596e92a4047SStephen Boyd /* Find the range which uV is inside of. */ 597e92a4047SStephen Boyd for (i = vreg->set_points->count - 1; i > 0; i--) { 598e92a4047SStephen Boyd range_max_uV = vreg->set_points->range[i - 1].set_point_max_uV; 599e92a4047SStephen Boyd if (uV > range_max_uV && range_max_uV > 0) 600e92a4047SStephen Boyd break; 601e92a4047SStephen Boyd } 602e92a4047SStephen Boyd 603e92a4047SStephen Boyd range_id = i; 604e92a4047SStephen Boyd range = &vreg->set_points->range[range_id]; 605e92a4047SStephen Boyd 606e92a4047SStephen Boyd /* 607e92a4047SStephen Boyd * Force uV to be an allowed set point by applying a ceiling function to 608e92a4047SStephen Boyd * the uV value. 609e92a4047SStephen Boyd */ 6101b5b1968SStephen Boyd voltage_sel = DIV_ROUND_UP(uV - range->min_uV, range->step_uV); 6111b5b1968SStephen Boyd uV = voltage_sel * range->step_uV + range->min_uV; 612e92a4047SStephen Boyd 613e92a4047SStephen Boyd if (uV > max_uV) { 614e92a4047SStephen Boyd dev_err(vreg->dev, 615e92a4047SStephen Boyd "request v=[%d, %d] cannot be met by any set point; " 616e92a4047SStephen Boyd "next set point: %d\n", 617e92a4047SStephen Boyd min_uV, max_uV, uV); 618e92a4047SStephen Boyd return -EINVAL; 619e92a4047SStephen Boyd } 620e92a4047SStephen Boyd 6211b5b1968SStephen Boyd selector = 0; 622e92a4047SStephen Boyd for (i = 0; i < range_id; i++) 6231b5b1968SStephen Boyd selector += vreg->set_points->range[i].n_voltages; 6241b5b1968SStephen Boyd selector += (uV - range->set_point_min_uV) / range->step_uV; 625e92a4047SStephen Boyd 6261b5b1968SStephen Boyd return selector; 6271b5b1968SStephen Boyd } 6281b5b1968SStephen Boyd 6291b5b1968SStephen Boyd static int spmi_sw_selector_to_hw(struct spmi_regulator *vreg, 6301b5b1968SStephen Boyd unsigned selector, u8 *range_sel, 6311b5b1968SStephen Boyd u8 *voltage_sel) 6321b5b1968SStephen Boyd { 6331b5b1968SStephen Boyd const struct spmi_voltage_range *range, *end; 634ab953b9dSStephen Boyd unsigned offset; 6351b5b1968SStephen Boyd 6361b5b1968SStephen Boyd range = vreg->set_points->range; 6371b5b1968SStephen Boyd end = range + vreg->set_points->count; 6381b5b1968SStephen Boyd 6391b5b1968SStephen Boyd for (; range < end; range++) { 6401b5b1968SStephen Boyd if (selector < range->n_voltages) { 641ab953b9dSStephen Boyd /* 642ab953b9dSStephen Boyd * hardware selectors between set point min and real 643ab953b9dSStephen Boyd * min are invalid so we ignore them 644ab953b9dSStephen Boyd */ 645ab953b9dSStephen Boyd offset = range->set_point_min_uV - range->min_uV; 646ab953b9dSStephen Boyd offset /= range->step_uV; 647ab953b9dSStephen Boyd *voltage_sel = selector + offset; 6481b5b1968SStephen Boyd *range_sel = range->range_sel; 649e92a4047SStephen Boyd return 0; 650e92a4047SStephen Boyd } 651e92a4047SStephen Boyd 6521b5b1968SStephen Boyd selector -= range->n_voltages; 6531b5b1968SStephen Boyd } 6541b5b1968SStephen Boyd 6551b5b1968SStephen Boyd return -EINVAL; 6561b5b1968SStephen Boyd } 6571b5b1968SStephen Boyd 6581b5b1968SStephen Boyd static int spmi_hw_selector_to_sw(struct spmi_regulator *vreg, u8 hw_sel, 6591b5b1968SStephen Boyd const struct spmi_voltage_range *range) 6601b5b1968SStephen Boyd { 661ab953b9dSStephen Boyd unsigned sw_sel = 0; 662ab953b9dSStephen Boyd unsigned offset, max_hw_sel; 6631b5b1968SStephen Boyd const struct spmi_voltage_range *r = vreg->set_points->range; 664ab953b9dSStephen Boyd const struct spmi_voltage_range *end = r + vreg->set_points->count; 6651b5b1968SStephen Boyd 666ab953b9dSStephen Boyd for (; r < end; r++) { 667ab953b9dSStephen Boyd if (r == range && range->n_voltages) { 668ab953b9dSStephen Boyd /* 669ab953b9dSStephen Boyd * hardware selectors between set point min and real 670ab953b9dSStephen Boyd * min and between set point max and real max are 671ab953b9dSStephen Boyd * invalid so we return an error if they're 672ab953b9dSStephen Boyd * programmed into the hardware 673ab953b9dSStephen Boyd */ 674ab953b9dSStephen Boyd offset = range->set_point_min_uV - range->min_uV; 675ab953b9dSStephen Boyd offset /= range->step_uV; 676ab953b9dSStephen Boyd if (hw_sel < offset) 677ab953b9dSStephen Boyd return -EINVAL; 678ab953b9dSStephen Boyd 679ab953b9dSStephen Boyd max_hw_sel = range->set_point_max_uV - range->min_uV; 680ab953b9dSStephen Boyd max_hw_sel /= range->step_uV; 681ab953b9dSStephen Boyd if (hw_sel > max_hw_sel) 682ab953b9dSStephen Boyd return -EINVAL; 683ab953b9dSStephen Boyd 684ab953b9dSStephen Boyd return sw_sel + hw_sel - offset; 685ab953b9dSStephen Boyd } 6861b5b1968SStephen Boyd sw_sel += r->n_voltages; 6871b5b1968SStephen Boyd } 6881b5b1968SStephen Boyd 689ab953b9dSStephen Boyd return -EINVAL; 6901b5b1968SStephen Boyd } 6911b5b1968SStephen Boyd 692e92a4047SStephen Boyd static const struct spmi_voltage_range * 693e92a4047SStephen Boyd spmi_regulator_find_range(struct spmi_regulator *vreg) 694e92a4047SStephen Boyd { 695e92a4047SStephen Boyd u8 range_sel; 696e92a4047SStephen Boyd const struct spmi_voltage_range *range, *end; 697e92a4047SStephen Boyd 698e92a4047SStephen Boyd range = vreg->set_points->range; 699e92a4047SStephen Boyd end = range + vreg->set_points->count; 700e92a4047SStephen Boyd 701e92a4047SStephen Boyd spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, &range_sel, 1); 702e92a4047SStephen Boyd 703e92a4047SStephen Boyd for (; range < end; range++) 704e92a4047SStephen Boyd if (range->range_sel == range_sel) 705e92a4047SStephen Boyd return range; 706e92a4047SStephen Boyd 707e92a4047SStephen Boyd return NULL; 708e92a4047SStephen Boyd } 709e92a4047SStephen Boyd 710e92a4047SStephen Boyd static int spmi_regulator_select_voltage_same_range(struct spmi_regulator *vreg, 7111b5b1968SStephen Boyd int min_uV, int max_uV) 712e92a4047SStephen Boyd { 713e92a4047SStephen Boyd const struct spmi_voltage_range *range; 714e92a4047SStephen Boyd int uV = min_uV; 7151b5b1968SStephen Boyd int i, selector; 716e92a4047SStephen Boyd 717e92a4047SStephen Boyd range = spmi_regulator_find_range(vreg); 718e92a4047SStephen Boyd if (!range) 719e92a4047SStephen Boyd goto different_range; 720e92a4047SStephen Boyd 721e92a4047SStephen Boyd if (uV < range->min_uV && max_uV >= range->min_uV) 722e92a4047SStephen Boyd uV = range->min_uV; 723e92a4047SStephen Boyd 724e92a4047SStephen Boyd if (uV < range->min_uV || uV > range->max_uV) { 725e92a4047SStephen Boyd /* Current range doesn't support the requested voltage. */ 726e92a4047SStephen Boyd goto different_range; 727e92a4047SStephen Boyd } 728e92a4047SStephen Boyd 729e92a4047SStephen Boyd /* 730e92a4047SStephen Boyd * Force uV to be an allowed set point by applying a ceiling function to 731e92a4047SStephen Boyd * the uV value. 732e92a4047SStephen Boyd */ 7331b5b1968SStephen Boyd uV = DIV_ROUND_UP(uV - range->min_uV, range->step_uV); 7341b5b1968SStephen Boyd uV = uV * range->step_uV + range->min_uV; 735e92a4047SStephen Boyd 736e92a4047SStephen Boyd if (uV > max_uV) { 737e92a4047SStephen Boyd /* 738e92a4047SStephen Boyd * No set point in the current voltage range is within the 739e92a4047SStephen Boyd * requested min_uV to max_uV range. 740e92a4047SStephen Boyd */ 741e92a4047SStephen Boyd goto different_range; 742e92a4047SStephen Boyd } 743e92a4047SStephen Boyd 7441b5b1968SStephen Boyd selector = 0; 745e92a4047SStephen Boyd for (i = 0; i < vreg->set_points->count; i++) { 746e92a4047SStephen Boyd if (uV >= vreg->set_points->range[i].set_point_min_uV 7479b2dfee3SStephen Boyd && uV <= vreg->set_points->range[i].set_point_max_uV) { 7481b5b1968SStephen Boyd selector += 749e92a4047SStephen Boyd (uV - vreg->set_points->range[i].set_point_min_uV) 750e92a4047SStephen Boyd / vreg->set_points->range[i].step_uV; 751e92a4047SStephen Boyd break; 7529b2dfee3SStephen Boyd } 753e92a4047SStephen Boyd 7541b5b1968SStephen Boyd selector += vreg->set_points->range[i].n_voltages; 755e92a4047SStephen Boyd } 756e92a4047SStephen Boyd 7571b5b1968SStephen Boyd if (selector >= vreg->set_points->n_voltages) 758e92a4047SStephen Boyd goto different_range; 759e92a4047SStephen Boyd 760b1d21a24SStephen Boyd return selector; 761e92a4047SStephen Boyd 762e92a4047SStephen Boyd different_range: 7631b5b1968SStephen Boyd return spmi_regulator_select_voltage(vreg, min_uV, max_uV); 764e92a4047SStephen Boyd } 765e92a4047SStephen Boyd 7661b5b1968SStephen Boyd static int spmi_regulator_common_map_voltage(struct regulator_dev *rdev, 7671b5b1968SStephen Boyd int min_uV, int max_uV) 7681b5b1968SStephen Boyd { 7691b5b1968SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 7701b5b1968SStephen Boyd 7711b5b1968SStephen Boyd /* 7721b5b1968SStephen Boyd * Favor staying in the current voltage range if possible. This avoids 7731b5b1968SStephen Boyd * voltage spikes that occur when changing the voltage range. 7741b5b1968SStephen Boyd */ 7751b5b1968SStephen Boyd return spmi_regulator_select_voltage_same_range(vreg, min_uV, max_uV); 7761b5b1968SStephen Boyd } 7771b5b1968SStephen Boyd 7781b5b1968SStephen Boyd static int 7791b5b1968SStephen Boyd spmi_regulator_common_set_voltage(struct regulator_dev *rdev, unsigned selector) 780e92a4047SStephen Boyd { 781e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 782e92a4047SStephen Boyd int ret; 783e92a4047SStephen Boyd u8 buf[2]; 784e92a4047SStephen Boyd u8 range_sel, voltage_sel; 785e92a4047SStephen Boyd 7861b5b1968SStephen Boyd ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel); 787e92a4047SStephen Boyd if (ret) 788e92a4047SStephen Boyd return ret; 789e92a4047SStephen Boyd 790e92a4047SStephen Boyd buf[0] = range_sel; 791e92a4047SStephen Boyd buf[1] = voltage_sel; 792e92a4047SStephen Boyd return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, buf, 2); 793e92a4047SStephen Boyd } 794e92a4047SStephen Boyd 79542ba89c8SJeffrey Hugo static int spmi_regulator_common_list_voltage(struct regulator_dev *rdev, 79642ba89c8SJeffrey Hugo unsigned selector); 79742ba89c8SJeffrey Hugo 79842ba89c8SJeffrey Hugo static int spmi_regulator_ftsmps426_set_voltage(struct regulator_dev *rdev, 79942ba89c8SJeffrey Hugo unsigned selector) 80042ba89c8SJeffrey Hugo { 80142ba89c8SJeffrey Hugo struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 80242ba89c8SJeffrey Hugo u8 buf[2]; 80342ba89c8SJeffrey Hugo int mV; 80442ba89c8SJeffrey Hugo 80542ba89c8SJeffrey Hugo mV = spmi_regulator_common_list_voltage(rdev, selector) / 1000; 80642ba89c8SJeffrey Hugo 80742ba89c8SJeffrey Hugo buf[0] = mV & 0xff; 80842ba89c8SJeffrey Hugo buf[1] = mV >> 8; 80942ba89c8SJeffrey Hugo return spmi_vreg_write(vreg, SPMI_FTSMPS426_REG_VOLTAGE_LSB, buf, 2); 81042ba89c8SJeffrey Hugo } 81142ba89c8SJeffrey Hugo 812e92a4047SStephen Boyd static int spmi_regulator_set_voltage_time_sel(struct regulator_dev *rdev, 813e92a4047SStephen Boyd unsigned int old_selector, unsigned int new_selector) 814e92a4047SStephen Boyd { 815e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 816e92a4047SStephen Boyd int diff_uV; 817e92a4047SStephen Boyd 81861d7fdc4SJeffrey Hugo diff_uV = abs(spmi_regulator_common_list_voltage(rdev, new_selector) - 81961d7fdc4SJeffrey Hugo spmi_regulator_common_list_voltage(rdev, old_selector)); 820e92a4047SStephen Boyd 821e92a4047SStephen Boyd return DIV_ROUND_UP(diff_uV, vreg->slew_rate); 822e92a4047SStephen Boyd } 823e92a4047SStephen Boyd 824e92a4047SStephen Boyd static int spmi_regulator_common_get_voltage(struct regulator_dev *rdev) 825e92a4047SStephen Boyd { 826e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 827e92a4047SStephen Boyd const struct spmi_voltage_range *range; 828e92a4047SStephen Boyd u8 voltage_sel; 829e92a4047SStephen Boyd 830e92a4047SStephen Boyd spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1); 831e92a4047SStephen Boyd 832e92a4047SStephen Boyd range = spmi_regulator_find_range(vreg); 833e92a4047SStephen Boyd if (!range) 8341b5b1968SStephen Boyd return -EINVAL; 835e92a4047SStephen Boyd 8361b5b1968SStephen Boyd return spmi_hw_selector_to_sw(vreg, voltage_sel, range); 8371b5b1968SStephen Boyd } 8381b5b1968SStephen Boyd 83942ba89c8SJeffrey Hugo static int spmi_regulator_ftsmps426_get_voltage(struct regulator_dev *rdev) 84042ba89c8SJeffrey Hugo { 84142ba89c8SJeffrey Hugo struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 84242ba89c8SJeffrey Hugo const struct spmi_voltage_range *range; 84342ba89c8SJeffrey Hugo u8 buf[2]; 84442ba89c8SJeffrey Hugo int uV; 84542ba89c8SJeffrey Hugo 84642ba89c8SJeffrey Hugo spmi_vreg_read(vreg, SPMI_FTSMPS426_REG_VOLTAGE_LSB, buf, 2); 84742ba89c8SJeffrey Hugo 84842ba89c8SJeffrey Hugo uV = (((unsigned int)buf[1] << 8) | (unsigned int)buf[0]) * 1000; 84942ba89c8SJeffrey Hugo range = vreg->set_points->range; 85042ba89c8SJeffrey Hugo 85142ba89c8SJeffrey Hugo return (uV - range->set_point_min_uV) / range->step_uV; 85242ba89c8SJeffrey Hugo } 85342ba89c8SJeffrey Hugo 8541b5b1968SStephen Boyd static int spmi_regulator_single_map_voltage(struct regulator_dev *rdev, 8551b5b1968SStephen Boyd int min_uV, int max_uV) 8561b5b1968SStephen Boyd { 8571b5b1968SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 8581b5b1968SStephen Boyd 8591b5b1968SStephen Boyd return spmi_regulator_select_voltage(vreg, min_uV, max_uV); 860e92a4047SStephen Boyd } 861e92a4047SStephen Boyd 862e92a4047SStephen Boyd static int spmi_regulator_single_range_set_voltage(struct regulator_dev *rdev, 8631b5b1968SStephen Boyd unsigned selector) 864e92a4047SStephen Boyd { 865e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 8661b5b1968SStephen Boyd u8 sel = selector; 867e92a4047SStephen Boyd 868e92a4047SStephen Boyd /* 869e92a4047SStephen Boyd * Certain types of regulators do not have a range select register so 870e92a4047SStephen Boyd * only voltage set register needs to be written. 871e92a4047SStephen Boyd */ 872e92a4047SStephen Boyd return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &sel, 1); 873e92a4047SStephen Boyd } 874e92a4047SStephen Boyd 875e92a4047SStephen Boyd static int spmi_regulator_single_range_get_voltage(struct regulator_dev *rdev) 876e92a4047SStephen Boyd { 877e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 8781b5b1968SStephen Boyd u8 selector; 8791b5b1968SStephen Boyd int ret; 880e92a4047SStephen Boyd 8811b5b1968SStephen Boyd ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &selector, 1); 8821b5b1968SStephen Boyd if (ret) 8831b5b1968SStephen Boyd return ret; 884e92a4047SStephen Boyd 8851b5b1968SStephen Boyd return selector; 886e92a4047SStephen Boyd } 887e92a4047SStephen Boyd 888e92a4047SStephen Boyd static int spmi_regulator_ult_lo_smps_set_voltage(struct regulator_dev *rdev, 8891b5b1968SStephen Boyd unsigned selector) 890e92a4047SStephen Boyd { 891e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 892e92a4047SStephen Boyd int ret; 893e92a4047SStephen Boyd u8 range_sel, voltage_sel; 894e92a4047SStephen Boyd 8951b5b1968SStephen Boyd ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel); 896e92a4047SStephen Boyd if (ret) 897e92a4047SStephen Boyd return ret; 898e92a4047SStephen Boyd 899e92a4047SStephen Boyd /* 900e92a4047SStephen Boyd * Calculate VSET based on range 901e92a4047SStephen Boyd * In case of range 0: voltage_sel is a 7 bit value, can be written 902e92a4047SStephen Boyd * witout any modification. 903e92a4047SStephen Boyd * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to 904e92a4047SStephen Boyd * [011]. 905e92a4047SStephen Boyd */ 906e92a4047SStephen Boyd if (range_sel == 1) 907e92a4047SStephen Boyd voltage_sel |= ULT_SMPS_RANGE_SPLIT; 908e92a4047SStephen Boyd 9090f94bffaSJulia Lawall return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_VOLTAGE_SET, 910e92a4047SStephen Boyd voltage_sel, 0xff); 911e92a4047SStephen Boyd } 912e92a4047SStephen Boyd 913e92a4047SStephen Boyd static int spmi_regulator_ult_lo_smps_get_voltage(struct regulator_dev *rdev) 914e92a4047SStephen Boyd { 915e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 916e92a4047SStephen Boyd const struct spmi_voltage_range *range; 917e92a4047SStephen Boyd u8 voltage_sel; 918e92a4047SStephen Boyd 919e92a4047SStephen Boyd spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1); 920e92a4047SStephen Boyd 921e92a4047SStephen Boyd range = spmi_regulator_find_range(vreg); 922e92a4047SStephen Boyd if (!range) 9231b5b1968SStephen Boyd return -EINVAL; 924e92a4047SStephen Boyd 925e92a4047SStephen Boyd if (range->range_sel == 1) 926e92a4047SStephen Boyd voltage_sel &= ~ULT_SMPS_RANGE_SPLIT; 927e92a4047SStephen Boyd 9281b5b1968SStephen Boyd return spmi_hw_selector_to_sw(vreg, voltage_sel, range); 929e92a4047SStephen Boyd } 930e92a4047SStephen Boyd 931e92a4047SStephen Boyd static int spmi_regulator_common_list_voltage(struct regulator_dev *rdev, 932e92a4047SStephen Boyd unsigned selector) 933e92a4047SStephen Boyd { 934e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 935e92a4047SStephen Boyd int uV = 0; 936e92a4047SStephen Boyd int i; 937e92a4047SStephen Boyd 938e92a4047SStephen Boyd if (selector >= vreg->set_points->n_voltages) 939e92a4047SStephen Boyd return 0; 940e92a4047SStephen Boyd 941e92a4047SStephen Boyd for (i = 0; i < vreg->set_points->count; i++) { 9429b2dfee3SStephen Boyd if (selector < vreg->set_points->range[i].n_voltages) { 943e92a4047SStephen Boyd uV = selector * vreg->set_points->range[i].step_uV 944e92a4047SStephen Boyd + vreg->set_points->range[i].set_point_min_uV; 945e92a4047SStephen Boyd break; 9469b2dfee3SStephen Boyd } 947e92a4047SStephen Boyd 948e92a4047SStephen Boyd selector -= vreg->set_points->range[i].n_voltages; 949e92a4047SStephen Boyd } 950e92a4047SStephen Boyd 951e92a4047SStephen Boyd return uV; 952e92a4047SStephen Boyd } 953e92a4047SStephen Boyd 954e92a4047SStephen Boyd static int 955e92a4047SStephen Boyd spmi_regulator_common_set_bypass(struct regulator_dev *rdev, bool enable) 956e92a4047SStephen Boyd { 957e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 958e92a4047SStephen Boyd u8 mask = SPMI_COMMON_MODE_BYPASS_MASK; 959e92a4047SStephen Boyd u8 val = 0; 960e92a4047SStephen Boyd 961e92a4047SStephen Boyd if (enable) 962e92a4047SStephen Boyd val = mask; 963e92a4047SStephen Boyd 964e92a4047SStephen Boyd return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask); 965e92a4047SStephen Boyd } 966e92a4047SStephen Boyd 967e92a4047SStephen Boyd static int 968e92a4047SStephen Boyd spmi_regulator_common_get_bypass(struct regulator_dev *rdev, bool *enable) 969e92a4047SStephen Boyd { 970e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 971e92a4047SStephen Boyd u8 val; 972e92a4047SStephen Boyd int ret; 973e92a4047SStephen Boyd 974e92a4047SStephen Boyd ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &val, 1); 975e92a4047SStephen Boyd *enable = val & SPMI_COMMON_MODE_BYPASS_MASK; 976e92a4047SStephen Boyd 977e92a4047SStephen Boyd return ret; 978e92a4047SStephen Boyd } 979e92a4047SStephen Boyd 980e92a4047SStephen Boyd static unsigned int spmi_regulator_common_get_mode(struct regulator_dev *rdev) 981e92a4047SStephen Boyd { 982e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 983e92a4047SStephen Boyd u8 reg; 984e92a4047SStephen Boyd 985e92a4047SStephen Boyd spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, ®, 1); 986e92a4047SStephen Boyd 987ba576a62SJeffrey Hugo reg &= SPMI_COMMON_MODE_HPM_MASK | SPMI_COMMON_MODE_AUTO_MASK; 988ba576a62SJeffrey Hugo 989ba576a62SJeffrey Hugo switch (reg) { 990ba576a62SJeffrey Hugo case SPMI_COMMON_MODE_HPM_MASK: 991e92a4047SStephen Boyd return REGULATOR_MODE_NORMAL; 992ba576a62SJeffrey Hugo case SPMI_COMMON_MODE_AUTO_MASK: 993e2adfacdSStephen Boyd return REGULATOR_MODE_FAST; 994ba576a62SJeffrey Hugo default: 995e92a4047SStephen Boyd return REGULATOR_MODE_IDLE; 996e92a4047SStephen Boyd } 997ba576a62SJeffrey Hugo } 998e92a4047SStephen Boyd 99942ba89c8SJeffrey Hugo static unsigned int spmi_regulator_ftsmps426_get_mode(struct regulator_dev *rdev) 100042ba89c8SJeffrey Hugo { 100142ba89c8SJeffrey Hugo struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 100242ba89c8SJeffrey Hugo u8 reg; 100342ba89c8SJeffrey Hugo 100442ba89c8SJeffrey Hugo spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, ®, 1); 100542ba89c8SJeffrey Hugo 100642ba89c8SJeffrey Hugo switch (reg) { 100742ba89c8SJeffrey Hugo case SPMI_FTSMPS426_MODE_HPM_MASK: 100842ba89c8SJeffrey Hugo return REGULATOR_MODE_NORMAL; 100942ba89c8SJeffrey Hugo case SPMI_FTSMPS426_MODE_AUTO_MASK: 101042ba89c8SJeffrey Hugo return REGULATOR_MODE_FAST; 101142ba89c8SJeffrey Hugo default: 101242ba89c8SJeffrey Hugo return REGULATOR_MODE_IDLE; 101342ba89c8SJeffrey Hugo } 101442ba89c8SJeffrey Hugo } 101542ba89c8SJeffrey Hugo 1016e92a4047SStephen Boyd static int 1017e92a4047SStephen Boyd spmi_regulator_common_set_mode(struct regulator_dev *rdev, unsigned int mode) 1018e92a4047SStephen Boyd { 1019e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 1020e2adfacdSStephen Boyd u8 mask = SPMI_COMMON_MODE_HPM_MASK | SPMI_COMMON_MODE_AUTO_MASK; 1021ba576a62SJeffrey Hugo u8 val; 1022e92a4047SStephen Boyd 1023ba576a62SJeffrey Hugo switch (mode) { 1024ba576a62SJeffrey Hugo case REGULATOR_MODE_NORMAL: 1025e2adfacdSStephen Boyd val = SPMI_COMMON_MODE_HPM_MASK; 1026ba576a62SJeffrey Hugo break; 1027ba576a62SJeffrey Hugo case REGULATOR_MODE_FAST: 1028e2adfacdSStephen Boyd val = SPMI_COMMON_MODE_AUTO_MASK; 1029ba576a62SJeffrey Hugo break; 1030ba576a62SJeffrey Hugo default: 1031ba576a62SJeffrey Hugo val = 0; 1032ba576a62SJeffrey Hugo break; 1033ba576a62SJeffrey Hugo } 1034e92a4047SStephen Boyd 1035e92a4047SStephen Boyd return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask); 1036e92a4047SStephen Boyd } 1037e92a4047SStephen Boyd 1038e92a4047SStephen Boyd static int 103942ba89c8SJeffrey Hugo spmi_regulator_ftsmps426_set_mode(struct regulator_dev *rdev, unsigned int mode) 104042ba89c8SJeffrey Hugo { 104142ba89c8SJeffrey Hugo struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 104242ba89c8SJeffrey Hugo u8 mask = SPMI_FTSMPS426_MODE_MASK; 104342ba89c8SJeffrey Hugo u8 val; 104442ba89c8SJeffrey Hugo 104542ba89c8SJeffrey Hugo switch (mode) { 104642ba89c8SJeffrey Hugo case REGULATOR_MODE_NORMAL: 104742ba89c8SJeffrey Hugo val = SPMI_FTSMPS426_MODE_HPM_MASK; 104842ba89c8SJeffrey Hugo break; 104942ba89c8SJeffrey Hugo case REGULATOR_MODE_FAST: 105042ba89c8SJeffrey Hugo val = SPMI_FTSMPS426_MODE_AUTO_MASK; 105142ba89c8SJeffrey Hugo break; 105242ba89c8SJeffrey Hugo case REGULATOR_MODE_IDLE: 105342ba89c8SJeffrey Hugo val = SPMI_FTSMPS426_MODE_LPM_MASK; 105442ba89c8SJeffrey Hugo break; 105542ba89c8SJeffrey Hugo default: 105642ba89c8SJeffrey Hugo return -EINVAL; 105742ba89c8SJeffrey Hugo } 105842ba89c8SJeffrey Hugo 105942ba89c8SJeffrey Hugo return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask); 106042ba89c8SJeffrey Hugo } 106142ba89c8SJeffrey Hugo 106242ba89c8SJeffrey Hugo static int 1063e92a4047SStephen Boyd spmi_regulator_common_set_load(struct regulator_dev *rdev, int load_uA) 1064e92a4047SStephen Boyd { 1065e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 1066e92a4047SStephen Boyd unsigned int mode; 1067e92a4047SStephen Boyd 1068e92a4047SStephen Boyd if (load_uA >= vreg->hpm_min_load) 1069e92a4047SStephen Boyd mode = REGULATOR_MODE_NORMAL; 1070e92a4047SStephen Boyd else 1071e92a4047SStephen Boyd mode = REGULATOR_MODE_IDLE; 1072e92a4047SStephen Boyd 1073e92a4047SStephen Boyd return spmi_regulator_common_set_mode(rdev, mode); 1074e92a4047SStephen Boyd } 1075e92a4047SStephen Boyd 1076e92a4047SStephen Boyd static int spmi_regulator_common_set_pull_down(struct regulator_dev *rdev) 1077e92a4047SStephen Boyd { 1078e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 1079e92a4047SStephen Boyd unsigned int mask = SPMI_COMMON_PULL_DOWN_ENABLE_MASK; 1080e92a4047SStephen Boyd 1081e92a4047SStephen Boyd return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_PULL_DOWN, 1082e92a4047SStephen Boyd mask, mask); 1083e92a4047SStephen Boyd } 1084e92a4047SStephen Boyd 1085e92a4047SStephen Boyd static int spmi_regulator_common_set_soft_start(struct regulator_dev *rdev) 1086e92a4047SStephen Boyd { 1087e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 1088e92a4047SStephen Boyd unsigned int mask = SPMI_LDO_SOFT_START_ENABLE_MASK; 1089e92a4047SStephen Boyd 1090e92a4047SStephen Boyd return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_SOFT_START, 1091e92a4047SStephen Boyd mask, mask); 1092e92a4047SStephen Boyd } 1093e92a4047SStephen Boyd 1094e92a4047SStephen Boyd static int spmi_regulator_set_ilim(struct regulator_dev *rdev, int ilim_uA) 1095e92a4047SStephen Boyd { 1096e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 1097e92a4047SStephen Boyd enum spmi_regulator_logical_type type = vreg->logical_type; 1098e92a4047SStephen Boyd unsigned int current_reg; 1099e92a4047SStephen Boyd u8 reg; 1100e92a4047SStephen Boyd u8 mask = SPMI_BOOST_CURRENT_LIMIT_MASK | 1101e92a4047SStephen Boyd SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK; 1102e92a4047SStephen Boyd int max = (SPMI_BOOST_CURRENT_LIMIT_MASK + 1) * 500; 1103e92a4047SStephen Boyd 1104e92a4047SStephen Boyd if (type == SPMI_REGULATOR_LOGICAL_TYPE_BOOST) 1105e92a4047SStephen Boyd current_reg = SPMI_BOOST_REG_CURRENT_LIMIT; 1106e92a4047SStephen Boyd else 1107e92a4047SStephen Boyd current_reg = SPMI_BOOST_BYP_REG_CURRENT_LIMIT; 1108e92a4047SStephen Boyd 1109e92a4047SStephen Boyd if (ilim_uA > max || ilim_uA <= 0) 1110e92a4047SStephen Boyd return -EINVAL; 1111e92a4047SStephen Boyd 1112e92a4047SStephen Boyd reg = (ilim_uA - 1) / 500; 1113e92a4047SStephen Boyd reg |= SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK; 1114e92a4047SStephen Boyd 1115e92a4047SStephen Boyd return spmi_vreg_update_bits(vreg, current_reg, reg, mask); 1116e92a4047SStephen Boyd } 1117e92a4047SStephen Boyd 1118e92a4047SStephen Boyd static int spmi_regulator_vs_clear_ocp(struct spmi_regulator *vreg) 1119e92a4047SStephen Boyd { 1120e92a4047SStephen Boyd int ret; 1121e92a4047SStephen Boyd 1122e92a4047SStephen Boyd ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE, 1123e92a4047SStephen Boyd SPMI_COMMON_DISABLE, SPMI_COMMON_ENABLE_MASK); 1124e92a4047SStephen Boyd 1125e92a4047SStephen Boyd vreg->vs_enable_time = ktime_get(); 1126e92a4047SStephen Boyd 1127e92a4047SStephen Boyd ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE, 1128e92a4047SStephen Boyd SPMI_COMMON_ENABLE, SPMI_COMMON_ENABLE_MASK); 1129e92a4047SStephen Boyd 1130e92a4047SStephen Boyd return ret; 1131e92a4047SStephen Boyd } 1132e92a4047SStephen Boyd 1133e92a4047SStephen Boyd static void spmi_regulator_vs_ocp_work(struct work_struct *work) 1134e92a4047SStephen Boyd { 1135e92a4047SStephen Boyd struct delayed_work *dwork = to_delayed_work(work); 1136e92a4047SStephen Boyd struct spmi_regulator *vreg 1137e92a4047SStephen Boyd = container_of(dwork, struct spmi_regulator, ocp_work); 1138e92a4047SStephen Boyd 1139e92a4047SStephen Boyd spmi_regulator_vs_clear_ocp(vreg); 1140e92a4047SStephen Boyd } 1141e92a4047SStephen Boyd 1142e92a4047SStephen Boyd static irqreturn_t spmi_regulator_vs_ocp_isr(int irq, void *data) 1143e92a4047SStephen Boyd { 1144e92a4047SStephen Boyd struct spmi_regulator *vreg = data; 1145e92a4047SStephen Boyd ktime_t ocp_irq_time; 1146e92a4047SStephen Boyd s64 ocp_trigger_delay_us; 1147e92a4047SStephen Boyd 1148e92a4047SStephen Boyd ocp_irq_time = ktime_get(); 1149e92a4047SStephen Boyd ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time, 1150e92a4047SStephen Boyd vreg->vs_enable_time); 1151e92a4047SStephen Boyd 1152e92a4047SStephen Boyd /* 1153e92a4047SStephen Boyd * Reset the OCP count if there is a large delay between switch enable 1154e92a4047SStephen Boyd * and when OCP triggers. This is indicative of a hotplug event as 1155e92a4047SStephen Boyd * opposed to a fault. 1156e92a4047SStephen Boyd */ 1157e92a4047SStephen Boyd if (ocp_trigger_delay_us > SPMI_VS_OCP_FAULT_DELAY_US) 1158e92a4047SStephen Boyd vreg->ocp_count = 0; 1159e92a4047SStephen Boyd 1160e92a4047SStephen Boyd /* Wait for switch output to settle back to 0 V after OCP triggered. */ 1161e92a4047SStephen Boyd udelay(SPMI_VS_OCP_FALL_DELAY_US); 1162e92a4047SStephen Boyd 1163e92a4047SStephen Boyd vreg->ocp_count++; 1164e92a4047SStephen Boyd 1165e92a4047SStephen Boyd if (vreg->ocp_count == 1) { 1166e92a4047SStephen Boyd /* Immediately clear the over current condition. */ 1167e92a4047SStephen Boyd spmi_regulator_vs_clear_ocp(vreg); 1168e92a4047SStephen Boyd } else if (vreg->ocp_count <= vreg->ocp_max_retries) { 1169e92a4047SStephen Boyd /* Schedule the over current clear task to run later. */ 1170e92a4047SStephen Boyd schedule_delayed_work(&vreg->ocp_work, 1171e92a4047SStephen Boyd msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1); 1172e92a4047SStephen Boyd } else { 1173e92a4047SStephen Boyd dev_err(vreg->dev, 1174e92a4047SStephen Boyd "OCP triggered %d times; no further retries\n", 1175e92a4047SStephen Boyd vreg->ocp_count); 1176e92a4047SStephen Boyd } 1177e92a4047SStephen Boyd 1178e92a4047SStephen Boyd return IRQ_HANDLED; 1179e92a4047SStephen Boyd } 1180e92a4047SStephen Boyd 11810caecaa8SIlia Lin #define SAW3_VCTL_DATA_MASK 0xFF 11820caecaa8SIlia Lin #define SAW3_VCTL_CLEAR_MASK 0x700FF 11830caecaa8SIlia Lin #define SAW3_AVS_CTL_EN_MASK 0x1 11840caecaa8SIlia Lin #define SAW3_AVS_CTL_TGGL_MASK 0x8000000 11850caecaa8SIlia Lin #define SAW3_AVS_CTL_CLEAR_MASK 0x7efc00 11860caecaa8SIlia Lin 11879689ca0aSNiklas Cassel static struct regmap *saw_regmap; 11880caecaa8SIlia Lin 11890caecaa8SIlia Lin static void spmi_saw_set_vdd(void *data) 11900caecaa8SIlia Lin { 11910caecaa8SIlia Lin u32 vctl, data3, avs_ctl, pmic_sts; 11920caecaa8SIlia Lin bool avs_enabled = false; 11930caecaa8SIlia Lin unsigned long timeout; 11940caecaa8SIlia Lin u8 voltage_sel = *(u8 *)data; 11950caecaa8SIlia Lin 11960caecaa8SIlia Lin regmap_read(saw_regmap, SAW3_AVS_CTL, &avs_ctl); 11970caecaa8SIlia Lin regmap_read(saw_regmap, SAW3_VCTL, &vctl); 11980caecaa8SIlia Lin regmap_read(saw_regmap, SAW3_SPM_PMIC_DATA_3, &data3); 11990caecaa8SIlia Lin 12000caecaa8SIlia Lin /* select the band */ 12010caecaa8SIlia Lin vctl &= ~SAW3_VCTL_CLEAR_MASK; 12020caecaa8SIlia Lin vctl |= (u32)voltage_sel; 12030caecaa8SIlia Lin 12040caecaa8SIlia Lin data3 &= ~SAW3_VCTL_CLEAR_MASK; 12050caecaa8SIlia Lin data3 |= (u32)voltage_sel; 12060caecaa8SIlia Lin 12070caecaa8SIlia Lin /* If AVS is enabled, switch it off during the voltage change */ 12080caecaa8SIlia Lin avs_enabled = SAW3_AVS_CTL_EN_MASK & avs_ctl; 12090caecaa8SIlia Lin if (avs_enabled) { 12100caecaa8SIlia Lin avs_ctl &= ~SAW3_AVS_CTL_TGGL_MASK; 12110caecaa8SIlia Lin regmap_write(saw_regmap, SAW3_AVS_CTL, avs_ctl); 12120caecaa8SIlia Lin } 12130caecaa8SIlia Lin 12140caecaa8SIlia Lin regmap_write(saw_regmap, SAW3_RST, 1); 12150caecaa8SIlia Lin regmap_write(saw_regmap, SAW3_VCTL, vctl); 12160caecaa8SIlia Lin regmap_write(saw_regmap, SAW3_SPM_PMIC_DATA_3, data3); 12170caecaa8SIlia Lin 12180caecaa8SIlia Lin timeout = jiffies + usecs_to_jiffies(100); 12190caecaa8SIlia Lin do { 12200caecaa8SIlia Lin regmap_read(saw_regmap, SAW3_PMIC_STS, &pmic_sts); 12210caecaa8SIlia Lin pmic_sts &= SAW3_VCTL_DATA_MASK; 12220caecaa8SIlia Lin if (pmic_sts == (u32)voltage_sel) 12230caecaa8SIlia Lin break; 12240caecaa8SIlia Lin 12250caecaa8SIlia Lin cpu_relax(); 12260caecaa8SIlia Lin 12270caecaa8SIlia Lin } while (time_before(jiffies, timeout)); 12280caecaa8SIlia Lin 12290caecaa8SIlia Lin /* After successful voltage change, switch the AVS back on */ 12300caecaa8SIlia Lin if (avs_enabled) { 12310caecaa8SIlia Lin pmic_sts &= 0x3f; 12320caecaa8SIlia Lin avs_ctl &= ~SAW3_AVS_CTL_CLEAR_MASK; 12330caecaa8SIlia Lin avs_ctl |= ((pmic_sts - 4) << 10); 12340caecaa8SIlia Lin avs_ctl |= (pmic_sts << 17); 12350caecaa8SIlia Lin avs_ctl |= SAW3_AVS_CTL_TGGL_MASK; 12360caecaa8SIlia Lin regmap_write(saw_regmap, SAW3_AVS_CTL, avs_ctl); 12370caecaa8SIlia Lin } 12380caecaa8SIlia Lin } 12390caecaa8SIlia Lin 12400caecaa8SIlia Lin static int 12410caecaa8SIlia Lin spmi_regulator_saw_set_voltage(struct regulator_dev *rdev, unsigned selector) 12420caecaa8SIlia Lin { 12430caecaa8SIlia Lin struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 12440caecaa8SIlia Lin int ret; 12450caecaa8SIlia Lin u8 range_sel, voltage_sel; 12460caecaa8SIlia Lin 12470caecaa8SIlia Lin ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel); 12480caecaa8SIlia Lin if (ret) 12490caecaa8SIlia Lin return ret; 12500caecaa8SIlia Lin 12510caecaa8SIlia Lin if (0 != range_sel) { 12520caecaa8SIlia Lin dev_dbg(&rdev->dev, "range_sel = %02X voltage_sel = %02X", \ 12530caecaa8SIlia Lin range_sel, voltage_sel); 12540caecaa8SIlia Lin return -EINVAL; 12550caecaa8SIlia Lin } 12560caecaa8SIlia Lin 12570caecaa8SIlia Lin /* Always do the SAW register writes on the first CPU */ 12580caecaa8SIlia Lin return smp_call_function_single(0, spmi_saw_set_vdd, \ 12590caecaa8SIlia Lin &voltage_sel, true); 12600caecaa8SIlia Lin } 12610caecaa8SIlia Lin 12620caecaa8SIlia Lin static struct regulator_ops spmi_saw_ops = {}; 12630caecaa8SIlia Lin 1264e92a4047SStephen Boyd static struct regulator_ops spmi_smps_ops = { 12659d485332SAxel Lin .enable = regulator_enable_regmap, 12669d485332SAxel Lin .disable = regulator_disable_regmap, 12679d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 12681b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_common_set_voltage, 12692cf7b99cSStephen Boyd .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 12701b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_common_get_voltage, 12711b5b1968SStephen Boyd .map_voltage = spmi_regulator_common_map_voltage, 1272e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1273e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1274e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1275e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1276e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1277e92a4047SStephen Boyd }; 1278e92a4047SStephen Boyd 1279e92a4047SStephen Boyd static struct regulator_ops spmi_ldo_ops = { 12809d485332SAxel Lin .enable = regulator_enable_regmap, 12819d485332SAxel Lin .disable = regulator_disable_regmap, 12829d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 12831b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_common_set_voltage, 12841b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_common_get_voltage, 12851b5b1968SStephen Boyd .map_voltage = spmi_regulator_common_map_voltage, 1286e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1287e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1288e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1289e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1290e92a4047SStephen Boyd .set_bypass = spmi_regulator_common_set_bypass, 1291e92a4047SStephen Boyd .get_bypass = spmi_regulator_common_get_bypass, 1292e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1293e92a4047SStephen Boyd .set_soft_start = spmi_regulator_common_set_soft_start, 1294e92a4047SStephen Boyd }; 1295e92a4047SStephen Boyd 1296e92a4047SStephen Boyd static struct regulator_ops spmi_ln_ldo_ops = { 12979d485332SAxel Lin .enable = regulator_enable_regmap, 12989d485332SAxel Lin .disable = regulator_disable_regmap, 12999d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13001b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_common_set_voltage, 13011b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_common_get_voltage, 13021b5b1968SStephen Boyd .map_voltage = spmi_regulator_common_map_voltage, 1303e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1304e92a4047SStephen Boyd .set_bypass = spmi_regulator_common_set_bypass, 1305e92a4047SStephen Boyd .get_bypass = spmi_regulator_common_get_bypass, 1306e92a4047SStephen Boyd }; 1307e92a4047SStephen Boyd 1308e92a4047SStephen Boyd static struct regulator_ops spmi_vs_ops = { 1309e92a4047SStephen Boyd .enable = spmi_regulator_vs_enable, 13109d485332SAxel Lin .disable = regulator_disable_regmap, 13119d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 1312e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1313e92a4047SStephen Boyd .set_soft_start = spmi_regulator_common_set_soft_start, 1314e2adfacdSStephen Boyd .set_over_current_protection = spmi_regulator_vs_ocp, 1315919163f6SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1316919163f6SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1317e92a4047SStephen Boyd }; 1318e92a4047SStephen Boyd 1319e92a4047SStephen Boyd static struct regulator_ops spmi_boost_ops = { 13209d485332SAxel Lin .enable = regulator_enable_regmap, 13219d485332SAxel Lin .disable = regulator_disable_regmap, 13229d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13231b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_single_range_set_voltage, 13241b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_single_range_get_voltage, 13251b5b1968SStephen Boyd .map_voltage = spmi_regulator_single_map_voltage, 1326e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1327e92a4047SStephen Boyd .set_input_current_limit = spmi_regulator_set_ilim, 1328e92a4047SStephen Boyd }; 1329e92a4047SStephen Boyd 1330e92a4047SStephen Boyd static struct regulator_ops spmi_ftsmps_ops = { 13319d485332SAxel Lin .enable = regulator_enable_regmap, 13329d485332SAxel Lin .disable = regulator_disable_regmap, 13339d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13341b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_common_set_voltage, 1335e92a4047SStephen Boyd .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 13361b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_common_get_voltage, 13371b5b1968SStephen Boyd .map_voltage = spmi_regulator_common_map_voltage, 1338e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1339e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1340e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1341e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1342e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1343e92a4047SStephen Boyd }; 1344e92a4047SStephen Boyd 1345e92a4047SStephen Boyd static struct regulator_ops spmi_ult_lo_smps_ops = { 13469d485332SAxel Lin .enable = regulator_enable_regmap, 13479d485332SAxel Lin .disable = regulator_disable_regmap, 13489d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13491b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_ult_lo_smps_set_voltage, 13502cf7b99cSStephen Boyd .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 13511b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_ult_lo_smps_get_voltage, 1352e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1353e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1354e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1355e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1356e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1357e92a4047SStephen Boyd }; 1358e92a4047SStephen Boyd 1359e92a4047SStephen Boyd static struct regulator_ops spmi_ult_ho_smps_ops = { 13609d485332SAxel Lin .enable = regulator_enable_regmap, 13619d485332SAxel Lin .disable = regulator_disable_regmap, 13629d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13631b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_single_range_set_voltage, 13642cf7b99cSStephen Boyd .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 13651b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_single_range_get_voltage, 13661b5b1968SStephen Boyd .map_voltage = spmi_regulator_single_map_voltage, 1367e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1368e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1369e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1370e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1371e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1372e92a4047SStephen Boyd }; 1373e92a4047SStephen Boyd 1374e92a4047SStephen Boyd static struct regulator_ops spmi_ult_ldo_ops = { 13759d485332SAxel Lin .enable = regulator_enable_regmap, 13769d485332SAxel Lin .disable = regulator_disable_regmap, 13779d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13781b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_single_range_set_voltage, 13791b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_single_range_get_voltage, 13801b5b1968SStephen Boyd .map_voltage = spmi_regulator_single_map_voltage, 1381e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1382e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1383e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1384e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1385e92a4047SStephen Boyd .set_bypass = spmi_regulator_common_set_bypass, 1386e92a4047SStephen Boyd .get_bypass = spmi_regulator_common_get_bypass, 1387e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1388e92a4047SStephen Boyd .set_soft_start = spmi_regulator_common_set_soft_start, 1389e92a4047SStephen Boyd }; 1390e92a4047SStephen Boyd 139142ba89c8SJeffrey Hugo static struct regulator_ops spmi_ftsmps426_ops = { 139242ba89c8SJeffrey Hugo .enable = regulator_enable_regmap, 139342ba89c8SJeffrey Hugo .disable = regulator_disable_regmap, 139442ba89c8SJeffrey Hugo .is_enabled = regulator_is_enabled_regmap, 139542ba89c8SJeffrey Hugo .set_voltage_sel = spmi_regulator_ftsmps426_set_voltage, 139642ba89c8SJeffrey Hugo .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 139742ba89c8SJeffrey Hugo .get_voltage_sel = spmi_regulator_ftsmps426_get_voltage, 139842ba89c8SJeffrey Hugo .map_voltage = spmi_regulator_single_map_voltage, 139942ba89c8SJeffrey Hugo .list_voltage = spmi_regulator_common_list_voltage, 140042ba89c8SJeffrey Hugo .set_mode = spmi_regulator_ftsmps426_set_mode, 140142ba89c8SJeffrey Hugo .get_mode = spmi_regulator_ftsmps426_get_mode, 140242ba89c8SJeffrey Hugo .set_load = spmi_regulator_common_set_load, 140342ba89c8SJeffrey Hugo .set_pull_down = spmi_regulator_common_set_pull_down, 140442ba89c8SJeffrey Hugo }; 140542ba89c8SJeffrey Hugo 14060211f68eSJorge Ramirez static struct regulator_ops spmi_hfs430_ops = { 14070211f68eSJorge Ramirez .enable = regulator_enable_regmap, 14080211f68eSJorge Ramirez .disable = regulator_disable_regmap, 14090211f68eSJorge Ramirez .is_enabled = regulator_is_enabled_regmap, 14100211f68eSJorge Ramirez .set_voltage_sel = spmi_regulator_ftsmps426_set_voltage, 14110211f68eSJorge Ramirez .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 14120211f68eSJorge Ramirez .get_voltage_sel = spmi_regulator_ftsmps426_get_voltage, 14130211f68eSJorge Ramirez .map_voltage = spmi_regulator_single_map_voltage, 14140211f68eSJorge Ramirez .list_voltage = spmi_regulator_common_list_voltage, 14150211f68eSJorge Ramirez .set_mode = spmi_regulator_ftsmps426_set_mode, 14160211f68eSJorge Ramirez .get_mode = spmi_regulator_ftsmps426_get_mode, 14170211f68eSJorge Ramirez }; 14180211f68eSJorge Ramirez 1419e92a4047SStephen Boyd /* Maximum possible digital major revision value */ 1420e92a4047SStephen Boyd #define INF 0xFF 1421e92a4047SStephen Boyd 1422e92a4047SStephen Boyd static const struct spmi_regulator_mapping supported_regulators[] = { 1423e92a4047SStephen Boyd /* type subtype dig_min dig_max ltype ops setpoints hpm_min */ 1424e92a4047SStephen Boyd SPMI_VREG(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000), 14250211f68eSJorge Ramirez SPMI_VREG(BUCK, HFS430, 0, INF, HFS430, hfs430, hfs430, 10000), 1426e92a4047SStephen Boyd SPMI_VREG(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000), 1427e92a4047SStephen Boyd SPMI_VREG(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000), 1428e92a4047SStephen Boyd SPMI_VREG(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000), 1429e92a4047SStephen Boyd SPMI_VREG(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000), 1430e92a4047SStephen Boyd SPMI_VREG(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000), 1431e92a4047SStephen Boyd SPMI_VREG(LDO, N600_ST, 0, 0, LDO, ldo, nldo2, 10000), 1432e92a4047SStephen Boyd SPMI_VREG(LDO, N1200_ST, 0, 0, LDO, ldo, nldo2, 10000), 1433e92a4047SStephen Boyd SPMI_VREG(LDO, N600_ST, 1, INF, LDO, ldo, nldo3, 10000), 1434e92a4047SStephen Boyd SPMI_VREG(LDO, N1200_ST, 1, INF, LDO, ldo, nldo3, 10000), 1435e92a4047SStephen Boyd SPMI_VREG(LDO, P50, 0, INF, LDO, ldo, pldo, 5000), 1436e92a4047SStephen Boyd SPMI_VREG(LDO, P150, 0, INF, LDO, ldo, pldo, 10000), 1437e92a4047SStephen Boyd SPMI_VREG(LDO, P300, 0, INF, LDO, ldo, pldo, 10000), 1438e92a4047SStephen Boyd SPMI_VREG(LDO, P600, 0, INF, LDO, ldo, pldo, 10000), 1439e92a4047SStephen Boyd SPMI_VREG(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000), 1440e92a4047SStephen Boyd SPMI_VREG(LDO, LN, 0, INF, LN_LDO, ln_ldo, ln_ldo, 0), 1441e92a4047SStephen Boyd SPMI_VREG(LDO, LV_P50, 0, INF, LDO, ldo, pldo, 5000), 1442e92a4047SStephen Boyd SPMI_VREG(LDO, LV_P150, 0, INF, LDO, ldo, pldo, 10000), 1443e92a4047SStephen Boyd SPMI_VREG(LDO, LV_P300, 0, INF, LDO, ldo, pldo, 10000), 1444e92a4047SStephen Boyd SPMI_VREG(LDO, LV_P600, 0, INF, LDO, ldo, pldo, 10000), 1445e92a4047SStephen Boyd SPMI_VREG(LDO, LV_P1200, 0, INF, LDO, ldo, pldo, 10000), 1446e92a4047SStephen Boyd SPMI_VREG_VS(LV100, 0, INF), 1447e92a4047SStephen Boyd SPMI_VREG_VS(LV300, 0, INF), 1448e92a4047SStephen Boyd SPMI_VREG_VS(MV300, 0, INF), 1449e92a4047SStephen Boyd SPMI_VREG_VS(MV500, 0, INF), 1450e92a4047SStephen Boyd SPMI_VREG_VS(HDMI, 0, INF), 1451e92a4047SStephen Boyd SPMI_VREG_VS(OTG, 0, INF), 1452e92a4047SStephen Boyd SPMI_VREG(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0), 1453e92a4047SStephen Boyd SPMI_VREG(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000), 1454e92a4047SStephen Boyd SPMI_VREG(FTS, FTS2p5_CTL, 0, INF, FTSMPS, ftsmps, ftsmps2p5, 100000), 145542ba89c8SJeffrey Hugo SPMI_VREG(FTS, FTS426_CTL, 0, INF, FTSMPS426, ftsmps426, ftsmps426, 100000), 1456e92a4047SStephen Boyd SPMI_VREG(BOOST_BYP, BB_2A, 0, INF, BOOST_BYP, boost, boost_byp, 0), 1457e92a4047SStephen Boyd SPMI_VREG(ULT_BUCK, ULT_HF_CTL1, 0, INF, ULT_LO_SMPS, ult_lo_smps, 1458e92a4047SStephen Boyd ult_lo_smps, 100000), 1459e92a4047SStephen Boyd SPMI_VREG(ULT_BUCK, ULT_HF_CTL2, 0, INF, ULT_LO_SMPS, ult_lo_smps, 1460e92a4047SStephen Boyd ult_lo_smps, 100000), 1461e92a4047SStephen Boyd SPMI_VREG(ULT_BUCK, ULT_HF_CTL3, 0, INF, ULT_LO_SMPS, ult_lo_smps, 1462e92a4047SStephen Boyd ult_lo_smps, 100000), 1463e92a4047SStephen Boyd SPMI_VREG(ULT_BUCK, ULT_HF_CTL4, 0, INF, ULT_HO_SMPS, ult_ho_smps, 1464e92a4047SStephen Boyd ult_ho_smps, 100000), 1465e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, N300_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1466e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, N600_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1467e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, N900_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1468e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, N1200_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1469e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, LV_P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1470e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, LV_P300, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1471e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, LV_P450, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1472e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, P600, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1473e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1474e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, P50, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 5000), 1475e92a4047SStephen Boyd }; 1476e92a4047SStephen Boyd 1477e92a4047SStephen Boyd static void spmi_calculate_num_voltages(struct spmi_voltage_set_points *points) 1478e92a4047SStephen Boyd { 1479e92a4047SStephen Boyd unsigned int n; 1480e92a4047SStephen Boyd struct spmi_voltage_range *range = points->range; 1481e92a4047SStephen Boyd 1482e92a4047SStephen Boyd for (; range < points->range + points->count; range++) { 1483e92a4047SStephen Boyd n = 0; 1484e92a4047SStephen Boyd if (range->set_point_max_uV) { 1485e92a4047SStephen Boyd n = range->set_point_max_uV - range->set_point_min_uV; 1486419d06a1SAxel Lin n = (n / range->step_uV) + 1; 1487e92a4047SStephen Boyd } 1488e92a4047SStephen Boyd range->n_voltages = n; 1489e92a4047SStephen Boyd points->n_voltages += n; 1490e92a4047SStephen Boyd } 1491e92a4047SStephen Boyd } 1492e92a4047SStephen Boyd 1493e92a4047SStephen Boyd static int spmi_regulator_match(struct spmi_regulator *vreg, u16 force_type) 1494e92a4047SStephen Boyd { 1495e92a4047SStephen Boyd const struct spmi_regulator_mapping *mapping; 1496e92a4047SStephen Boyd int ret, i; 1497e92a4047SStephen Boyd u32 dig_major_rev; 1498e92a4047SStephen Boyd u8 version[SPMI_COMMON_REG_SUBTYPE - SPMI_COMMON_REG_DIG_MAJOR_REV + 1]; 1499e92a4047SStephen Boyd u8 type, subtype; 1500e92a4047SStephen Boyd 1501e92a4047SStephen Boyd ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_DIG_MAJOR_REV, version, 1502e92a4047SStephen Boyd ARRAY_SIZE(version)); 1503e92a4047SStephen Boyd if (ret) { 15046ee5c044SStephen Boyd dev_dbg(vreg->dev, "could not read version registers\n"); 1505e92a4047SStephen Boyd return ret; 1506e92a4047SStephen Boyd } 1507e92a4047SStephen Boyd dig_major_rev = version[SPMI_COMMON_REG_DIG_MAJOR_REV 1508e92a4047SStephen Boyd - SPMI_COMMON_REG_DIG_MAJOR_REV]; 15090caecaa8SIlia Lin 1510e92a4047SStephen Boyd if (!force_type) { 1511e92a4047SStephen Boyd type = version[SPMI_COMMON_REG_TYPE - 1512e92a4047SStephen Boyd SPMI_COMMON_REG_DIG_MAJOR_REV]; 1513e92a4047SStephen Boyd subtype = version[SPMI_COMMON_REG_SUBTYPE - 1514e92a4047SStephen Boyd SPMI_COMMON_REG_DIG_MAJOR_REV]; 1515e92a4047SStephen Boyd } else { 1516e92a4047SStephen Boyd type = force_type >> 8; 1517e92a4047SStephen Boyd subtype = force_type; 1518e92a4047SStephen Boyd } 1519e92a4047SStephen Boyd 1520e92a4047SStephen Boyd for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) { 1521e92a4047SStephen Boyd mapping = &supported_regulators[i]; 1522e92a4047SStephen Boyd if (mapping->type == type && mapping->subtype == subtype 1523e92a4047SStephen Boyd && mapping->revision_min <= dig_major_rev 1524e92a4047SStephen Boyd && mapping->revision_max >= dig_major_rev) 1525e92a4047SStephen Boyd goto found; 1526e92a4047SStephen Boyd } 1527e92a4047SStephen Boyd 1528e92a4047SStephen Boyd dev_err(vreg->dev, 1529e92a4047SStephen Boyd "unsupported regulator: name=%s type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n", 1530e92a4047SStephen Boyd vreg->desc.name, type, subtype, dig_major_rev); 1531e92a4047SStephen Boyd 1532e92a4047SStephen Boyd return -ENODEV; 1533e92a4047SStephen Boyd 1534e92a4047SStephen Boyd found: 1535e92a4047SStephen Boyd vreg->logical_type = mapping->logical_type; 1536e92a4047SStephen Boyd vreg->set_points = mapping->set_points; 1537e92a4047SStephen Boyd vreg->hpm_min_load = mapping->hpm_min_load; 1538e92a4047SStephen Boyd vreg->desc.ops = mapping->ops; 1539e92a4047SStephen Boyd 1540e92a4047SStephen Boyd if (mapping->set_points) { 1541e92a4047SStephen Boyd if (!mapping->set_points->n_voltages) 1542e92a4047SStephen Boyd spmi_calculate_num_voltages(mapping->set_points); 1543e92a4047SStephen Boyd vreg->desc.n_voltages = mapping->set_points->n_voltages; 1544e92a4047SStephen Boyd } 1545e92a4047SStephen Boyd 1546e92a4047SStephen Boyd return 0; 1547e92a4047SStephen Boyd } 1548e92a4047SStephen Boyd 15492cf7b99cSStephen Boyd static int spmi_regulator_init_slew_rate(struct spmi_regulator *vreg) 1550e92a4047SStephen Boyd { 1551e92a4047SStephen Boyd int ret; 1552e92a4047SStephen Boyd u8 reg = 0; 15532cf7b99cSStephen Boyd int step, delay, slew_rate, step_delay; 1554e92a4047SStephen Boyd const struct spmi_voltage_range *range; 1555e92a4047SStephen Boyd 1556e92a4047SStephen Boyd ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_STEP_CTRL, ®, 1); 1557e92a4047SStephen Boyd if (ret) { 1558e92a4047SStephen Boyd dev_err(vreg->dev, "spmi read failed, ret=%d\n", ret); 1559e92a4047SStephen Boyd return ret; 1560e92a4047SStephen Boyd } 1561e92a4047SStephen Boyd 1562e92a4047SStephen Boyd range = spmi_regulator_find_range(vreg); 1563e92a4047SStephen Boyd if (!range) 1564e92a4047SStephen Boyd return -EINVAL; 1565e92a4047SStephen Boyd 15662cf7b99cSStephen Boyd switch (vreg->logical_type) { 15672cf7b99cSStephen Boyd case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS: 15682cf7b99cSStephen Boyd step_delay = SPMI_FTSMPS_STEP_DELAY; 15692cf7b99cSStephen Boyd break; 15702cf7b99cSStephen Boyd default: 15712cf7b99cSStephen Boyd step_delay = SPMI_DEFAULT_STEP_DELAY; 15722cf7b99cSStephen Boyd break; 15732cf7b99cSStephen Boyd } 15742cf7b99cSStephen Boyd 1575e92a4047SStephen Boyd step = reg & SPMI_FTSMPS_STEP_CTRL_STEP_MASK; 1576e92a4047SStephen Boyd step >>= SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT; 1577e92a4047SStephen Boyd 1578e92a4047SStephen Boyd delay = reg & SPMI_FTSMPS_STEP_CTRL_DELAY_MASK; 1579e92a4047SStephen Boyd delay >>= SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT; 1580e92a4047SStephen Boyd 1581e92a4047SStephen Boyd /* slew_rate has units of uV/us */ 1582e92a4047SStephen Boyd slew_rate = SPMI_FTSMPS_CLOCK_RATE * range->step_uV * (1 << step); 15832cf7b99cSStephen Boyd slew_rate /= 1000 * (step_delay << delay); 1584e92a4047SStephen Boyd slew_rate *= SPMI_FTSMPS_STEP_MARGIN_NUM; 1585e92a4047SStephen Boyd slew_rate /= SPMI_FTSMPS_STEP_MARGIN_DEN; 1586e92a4047SStephen Boyd 1587e92a4047SStephen Boyd /* Ensure that the slew rate is greater than 0 */ 1588e92a4047SStephen Boyd vreg->slew_rate = max(slew_rate, 1); 1589e92a4047SStephen Boyd 1590e92a4047SStephen Boyd return ret; 1591e92a4047SStephen Boyd } 1592e92a4047SStephen Boyd 15930211f68eSJorge Ramirez static int spmi_regulator_init_slew_rate_ftsmps426(struct spmi_regulator *vreg, 15940211f68eSJorge Ramirez int clock_rate) 159542ba89c8SJeffrey Hugo { 159642ba89c8SJeffrey Hugo int ret; 159742ba89c8SJeffrey Hugo u8 reg = 0; 159842ba89c8SJeffrey Hugo int delay, slew_rate; 159942ba89c8SJeffrey Hugo const struct spmi_voltage_range *range = &vreg->set_points->range[0]; 160042ba89c8SJeffrey Hugo 160142ba89c8SJeffrey Hugo ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_STEP_CTRL, ®, 1); 160242ba89c8SJeffrey Hugo if (ret) { 160342ba89c8SJeffrey Hugo dev_err(vreg->dev, "spmi read failed, ret=%d\n", ret); 160442ba89c8SJeffrey Hugo return ret; 160542ba89c8SJeffrey Hugo } 160642ba89c8SJeffrey Hugo 160742ba89c8SJeffrey Hugo delay = reg & SPMI_FTSMPS426_STEP_CTRL_DELAY_MASK; 160842ba89c8SJeffrey Hugo delay >>= SPMI_FTSMPS426_STEP_CTRL_DELAY_SHIFT; 160942ba89c8SJeffrey Hugo 161042ba89c8SJeffrey Hugo /* slew_rate has units of uV/us */ 16110211f68eSJorge Ramirez slew_rate = clock_rate * range->step_uV; 161242ba89c8SJeffrey Hugo slew_rate /= 1000 * (SPMI_FTSMPS426_STEP_DELAY << delay); 161342ba89c8SJeffrey Hugo slew_rate *= SPMI_FTSMPS426_STEP_MARGIN_NUM; 161442ba89c8SJeffrey Hugo slew_rate /= SPMI_FTSMPS426_STEP_MARGIN_DEN; 161542ba89c8SJeffrey Hugo 161642ba89c8SJeffrey Hugo /* Ensure that the slew rate is greater than 0 */ 161742ba89c8SJeffrey Hugo vreg->slew_rate = max(slew_rate, 1); 161842ba89c8SJeffrey Hugo 161942ba89c8SJeffrey Hugo return ret; 162042ba89c8SJeffrey Hugo } 162142ba89c8SJeffrey Hugo 1622e2adfacdSStephen Boyd static int spmi_regulator_init_registers(struct spmi_regulator *vreg, 1623e2adfacdSStephen Boyd const struct spmi_regulator_init_data *data) 1624e2adfacdSStephen Boyd { 1625e2adfacdSStephen Boyd int ret; 1626e2adfacdSStephen Boyd enum spmi_regulator_logical_type type; 1627e2adfacdSStephen Boyd u8 ctrl_reg[8], reg, mask; 1628e2adfacdSStephen Boyd 1629e2adfacdSStephen Boyd type = vreg->logical_type; 1630e2adfacdSStephen Boyd 1631e2adfacdSStephen Boyd ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8); 1632e2adfacdSStephen Boyd if (ret) 1633e2adfacdSStephen Boyd return ret; 1634e2adfacdSStephen Boyd 1635e2adfacdSStephen Boyd /* Set up enable pin control. */ 1636e2adfacdSStephen Boyd if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS 1637e2adfacdSStephen Boyd || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO 1638e2adfacdSStephen Boyd || type == SPMI_REGULATOR_LOGICAL_TYPE_VS) 1639e2adfacdSStephen Boyd && !(data->pin_ctrl_enable 1640e2adfacdSStephen Boyd & SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) { 1641e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_ENABLE] &= 1642e2adfacdSStephen Boyd ~SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK; 1643e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_ENABLE] |= 1644e2adfacdSStephen Boyd data->pin_ctrl_enable & SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK; 1645e2adfacdSStephen Boyd } 1646e2adfacdSStephen Boyd 1647e2adfacdSStephen Boyd /* Set up mode pin control. */ 1648e2adfacdSStephen Boyd if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS 1649e2adfacdSStephen Boyd || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO) 1650e2adfacdSStephen Boyd && !(data->pin_ctrl_hpm 1651e2adfacdSStephen Boyd & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) { 1652e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] &= 1653e2adfacdSStephen Boyd ~SPMI_COMMON_MODE_FOLLOW_ALL_MASK; 1654e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] |= 1655e2adfacdSStephen Boyd data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_ALL_MASK; 1656e2adfacdSStephen Boyd } 1657e2adfacdSStephen Boyd 1658e2adfacdSStephen Boyd if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS 1659e2adfacdSStephen Boyd && !(data->pin_ctrl_hpm & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) { 1660e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] &= 1661e2adfacdSStephen Boyd ~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK; 1662e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] |= 1663e2adfacdSStephen Boyd data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK; 1664e2adfacdSStephen Boyd } 1665e2adfacdSStephen Boyd 1666e2adfacdSStephen Boyd if ((type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS 1667e2adfacdSStephen Boyd || type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS 1668e2adfacdSStephen Boyd || type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO) 1669e2adfacdSStephen Boyd && !(data->pin_ctrl_hpm 1670e2adfacdSStephen Boyd & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) { 1671e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] &= 1672e2adfacdSStephen Boyd ~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK; 1673e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] |= 1674e2adfacdSStephen Boyd data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK; 1675e2adfacdSStephen Boyd } 1676e2adfacdSStephen Boyd 1677e2adfacdSStephen Boyd /* Write back any control register values that were modified. */ 1678e2adfacdSStephen Boyd ret = spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8); 1679e2adfacdSStephen Boyd if (ret) 1680e2adfacdSStephen Boyd return ret; 1681e2adfacdSStephen Boyd 1682e2adfacdSStephen Boyd /* Set soft start strength and over current protection for VS. */ 1683e2adfacdSStephen Boyd if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS) { 1684e2adfacdSStephen Boyd if (data->vs_soft_start_strength 1685e2adfacdSStephen Boyd != SPMI_VS_SOFT_START_STR_HW_DEFAULT) { 1686e2adfacdSStephen Boyd reg = data->vs_soft_start_strength 1687e2adfacdSStephen Boyd & SPMI_VS_SOFT_START_SEL_MASK; 1688e2adfacdSStephen Boyd mask = SPMI_VS_SOFT_START_SEL_MASK; 1689e2adfacdSStephen Boyd return spmi_vreg_update_bits(vreg, 1690e2adfacdSStephen Boyd SPMI_VS_REG_SOFT_START, 1691e2adfacdSStephen Boyd reg, mask); 1692e2adfacdSStephen Boyd } 1693e2adfacdSStephen Boyd } 1694e2adfacdSStephen Boyd 1695e2adfacdSStephen Boyd return 0; 1696e2adfacdSStephen Boyd } 1697e2adfacdSStephen Boyd 1698e2adfacdSStephen Boyd static void spmi_regulator_get_dt_config(struct spmi_regulator *vreg, 1699e2adfacdSStephen Boyd struct device_node *node, struct spmi_regulator_init_data *data) 1700e2adfacdSStephen Boyd { 1701e2adfacdSStephen Boyd /* 1702e2adfacdSStephen Boyd * Initialize configuration parameters to use hardware default in case 1703e2adfacdSStephen Boyd * no value is specified via device tree. 1704e2adfacdSStephen Boyd */ 1705e2adfacdSStephen Boyd data->pin_ctrl_enable = SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT; 1706e2adfacdSStephen Boyd data->pin_ctrl_hpm = SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT; 1707e2adfacdSStephen Boyd data->vs_soft_start_strength = SPMI_VS_SOFT_START_STR_HW_DEFAULT; 1708e2adfacdSStephen Boyd 1709e2adfacdSStephen Boyd /* These bindings are optional, so it is okay if they aren't found. */ 1710e2adfacdSStephen Boyd of_property_read_u32(node, "qcom,ocp-max-retries", 1711e2adfacdSStephen Boyd &vreg->ocp_max_retries); 1712e2adfacdSStephen Boyd of_property_read_u32(node, "qcom,ocp-retry-delay", 1713e2adfacdSStephen Boyd &vreg->ocp_retry_delay_ms); 1714e2adfacdSStephen Boyd of_property_read_u32(node, "qcom,pin-ctrl-enable", 1715e2adfacdSStephen Boyd &data->pin_ctrl_enable); 1716e2adfacdSStephen Boyd of_property_read_u32(node, "qcom,pin-ctrl-hpm", &data->pin_ctrl_hpm); 1717e2adfacdSStephen Boyd of_property_read_u32(node, "qcom,vs-soft-start-strength", 1718e2adfacdSStephen Boyd &data->vs_soft_start_strength); 1719e2adfacdSStephen Boyd } 1720e2adfacdSStephen Boyd 1721e92a4047SStephen Boyd static unsigned int spmi_regulator_of_map_mode(unsigned int mode) 1722e92a4047SStephen Boyd { 1723e2adfacdSStephen Boyd if (mode == 1) 1724e92a4047SStephen Boyd return REGULATOR_MODE_NORMAL; 1725e2adfacdSStephen Boyd if (mode == 2) 1726e2adfacdSStephen Boyd return REGULATOR_MODE_FAST; 1727e92a4047SStephen Boyd 1728e92a4047SStephen Boyd return REGULATOR_MODE_IDLE; 1729e92a4047SStephen Boyd } 1730e92a4047SStephen Boyd 1731e92a4047SStephen Boyd static int spmi_regulator_of_parse(struct device_node *node, 1732e92a4047SStephen Boyd const struct regulator_desc *desc, 1733e92a4047SStephen Boyd struct regulator_config *config) 1734e92a4047SStephen Boyd { 1735e2adfacdSStephen Boyd struct spmi_regulator_init_data data = { }; 1736e92a4047SStephen Boyd struct spmi_regulator *vreg = config->driver_data; 1737e92a4047SStephen Boyd struct device *dev = config->dev; 1738e92a4047SStephen Boyd int ret; 1739e92a4047SStephen Boyd 1740e2adfacdSStephen Boyd spmi_regulator_get_dt_config(vreg, node, &data); 1741e2adfacdSStephen Boyd 1742e2adfacdSStephen Boyd if (!vreg->ocp_max_retries) 1743e92a4047SStephen Boyd vreg->ocp_max_retries = SPMI_VS_OCP_DEFAULT_MAX_RETRIES; 1744e2adfacdSStephen Boyd if (!vreg->ocp_retry_delay_ms) 1745e92a4047SStephen Boyd vreg->ocp_retry_delay_ms = SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS; 1746e92a4047SStephen Boyd 1747e2adfacdSStephen Boyd ret = spmi_regulator_init_registers(vreg, &data); 1748e2adfacdSStephen Boyd if (ret) { 1749e2adfacdSStephen Boyd dev_err(dev, "common initialization failed, ret=%d\n", ret); 1750e2adfacdSStephen Boyd return ret; 1751e2adfacdSStephen Boyd } 1752e2adfacdSStephen Boyd 17532cf7b99cSStephen Boyd switch (vreg->logical_type) { 17542cf7b99cSStephen Boyd case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS: 17552cf7b99cSStephen Boyd case SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS: 17562cf7b99cSStephen Boyd case SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS: 17572cf7b99cSStephen Boyd case SPMI_REGULATOR_LOGICAL_TYPE_SMPS: 17582cf7b99cSStephen Boyd ret = spmi_regulator_init_slew_rate(vreg); 1759e92a4047SStephen Boyd if (ret) 1760e92a4047SStephen Boyd return ret; 176142ba89c8SJeffrey Hugo break; 176242ba89c8SJeffrey Hugo case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS426: 17630211f68eSJorge Ramirez ret = spmi_regulator_init_slew_rate_ftsmps426(vreg, 17640211f68eSJorge Ramirez SPMI_FTSMPS426_CLOCK_RATE); 17650211f68eSJorge Ramirez if (ret) 17660211f68eSJorge Ramirez return ret; 17670211f68eSJorge Ramirez break; 17680211f68eSJorge Ramirez case SPMI_REGULATOR_LOGICAL_TYPE_HFS430: 17690211f68eSJorge Ramirez ret = spmi_regulator_init_slew_rate_ftsmps426(vreg, 17700211f68eSJorge Ramirez SPMI_HFS430_CLOCK_RATE); 177142ba89c8SJeffrey Hugo if (ret) 177242ba89c8SJeffrey Hugo return ret; 177342ba89c8SJeffrey Hugo break; 17742cf7b99cSStephen Boyd default: 17752cf7b99cSStephen Boyd break; 1776e92a4047SStephen Boyd } 1777e92a4047SStephen Boyd 1778e92a4047SStephen Boyd if (vreg->logical_type != SPMI_REGULATOR_LOGICAL_TYPE_VS) 1779e92a4047SStephen Boyd vreg->ocp_irq = 0; 1780e92a4047SStephen Boyd 1781e92a4047SStephen Boyd if (vreg->ocp_irq) { 1782e92a4047SStephen Boyd ret = devm_request_irq(dev, vreg->ocp_irq, 1783e92a4047SStephen Boyd spmi_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp", 1784e92a4047SStephen Boyd vreg); 1785e92a4047SStephen Boyd if (ret < 0) { 1786e92a4047SStephen Boyd dev_err(dev, "failed to request irq %d, ret=%d\n", 1787e92a4047SStephen Boyd vreg->ocp_irq, ret); 1788e92a4047SStephen Boyd return ret; 1789e92a4047SStephen Boyd } 1790e92a4047SStephen Boyd 1791e92a4047SStephen Boyd INIT_DELAYED_WORK(&vreg->ocp_work, spmi_regulator_vs_ocp_work); 1792e92a4047SStephen Boyd } 1793e92a4047SStephen Boyd 1794e92a4047SStephen Boyd return 0; 1795e92a4047SStephen Boyd } 1796e92a4047SStephen Boyd 1797e92a4047SStephen Boyd static const struct spmi_regulator_data pm8941_regulators[] = { 1798e92a4047SStephen Boyd { "s1", 0x1400, "vdd_s1", }, 1799e92a4047SStephen Boyd { "s2", 0x1700, "vdd_s2", }, 1800e92a4047SStephen Boyd { "s3", 0x1a00, "vdd_s3", }, 1801c333dfe8SStephen Boyd { "s4", 0xa000, }, 1802e92a4047SStephen Boyd { "l1", 0x4000, "vdd_l1_l3", }, 1803e92a4047SStephen Boyd { "l2", 0x4100, "vdd_l2_lvs_1_2_3", }, 1804e92a4047SStephen Boyd { "l3", 0x4200, "vdd_l1_l3", }, 1805e92a4047SStephen Boyd { "l4", 0x4300, "vdd_l4_l11", }, 1806e92a4047SStephen Boyd { "l5", 0x4400, "vdd_l5_l7", NULL, 0x0410 }, 1807e92a4047SStephen Boyd { "l6", 0x4500, "vdd_l6_l12_l14_l15", }, 1808e92a4047SStephen Boyd { "l7", 0x4600, "vdd_l5_l7", NULL, 0x0410 }, 1809e92a4047SStephen Boyd { "l8", 0x4700, "vdd_l8_l16_l18_19", }, 1810e92a4047SStephen Boyd { "l9", 0x4800, "vdd_l9_l10_l17_l22", }, 1811e92a4047SStephen Boyd { "l10", 0x4900, "vdd_l9_l10_l17_l22", }, 1812e92a4047SStephen Boyd { "l11", 0x4a00, "vdd_l4_l11", }, 1813e92a4047SStephen Boyd { "l12", 0x4b00, "vdd_l6_l12_l14_l15", }, 1814e92a4047SStephen Boyd { "l13", 0x4c00, "vdd_l13_l20_l23_l24", }, 1815e92a4047SStephen Boyd { "l14", 0x4d00, "vdd_l6_l12_l14_l15", }, 1816e92a4047SStephen Boyd { "l15", 0x4e00, "vdd_l6_l12_l14_l15", }, 1817e92a4047SStephen Boyd { "l16", 0x4f00, "vdd_l8_l16_l18_19", }, 1818e92a4047SStephen Boyd { "l17", 0x5000, "vdd_l9_l10_l17_l22", }, 1819e92a4047SStephen Boyd { "l18", 0x5100, "vdd_l8_l16_l18_19", }, 1820e92a4047SStephen Boyd { "l19", 0x5200, "vdd_l8_l16_l18_19", }, 1821e92a4047SStephen Boyd { "l20", 0x5300, "vdd_l13_l20_l23_l24", }, 1822e92a4047SStephen Boyd { "l21", 0x5400, "vdd_l21", }, 1823e92a4047SStephen Boyd { "l22", 0x5500, "vdd_l9_l10_l17_l22", }, 1824e92a4047SStephen Boyd { "l23", 0x5600, "vdd_l13_l20_l23_l24", }, 1825e92a4047SStephen Boyd { "l24", 0x5700, "vdd_l13_l20_l23_l24", }, 1826e92a4047SStephen Boyd { "lvs1", 0x8000, "vdd_l2_lvs_1_2_3", }, 1827e92a4047SStephen Boyd { "lvs2", 0x8100, "vdd_l2_lvs_1_2_3", }, 1828e92a4047SStephen Boyd { "lvs3", 0x8200, "vdd_l2_lvs_1_2_3", }, 182993bfe79bSStephen Boyd { "5vs1", 0x8300, "vin_5vs", "ocp-5vs1", }, 183093bfe79bSStephen Boyd { "5vs2", 0x8400, "vin_5vs", "ocp-5vs2", }, 1831e92a4047SStephen Boyd { } 1832e92a4047SStephen Boyd }; 1833e92a4047SStephen Boyd 1834e92a4047SStephen Boyd static const struct spmi_regulator_data pm8841_regulators[] = { 1835e92a4047SStephen Boyd { "s1", 0x1400, "vdd_s1", }, 1836e92a4047SStephen Boyd { "s2", 0x1700, "vdd_s2", NULL, 0x1c08 }, 1837e92a4047SStephen Boyd { "s3", 0x1a00, "vdd_s3", }, 1838e92a4047SStephen Boyd { "s4", 0x1d00, "vdd_s4", NULL, 0x1c08 }, 1839e92a4047SStephen Boyd { "s5", 0x2000, "vdd_s5", NULL, 0x1c08 }, 1840e92a4047SStephen Boyd { "s6", 0x2300, "vdd_s6", NULL, 0x1c08 }, 1841e92a4047SStephen Boyd { "s7", 0x2600, "vdd_s7", NULL, 0x1c08 }, 1842e92a4047SStephen Boyd { "s8", 0x2900, "vdd_s8", NULL, 0x1c08 }, 1843e92a4047SStephen Boyd { } 1844e92a4047SStephen Boyd }; 1845e92a4047SStephen Boyd 1846e92a4047SStephen Boyd static const struct spmi_regulator_data pm8916_regulators[] = { 1847e92a4047SStephen Boyd { "s1", 0x1400, "vdd_s1", }, 1848e92a4047SStephen Boyd { "s2", 0x1700, "vdd_s2", }, 1849e92a4047SStephen Boyd { "s3", 0x1a00, "vdd_s3", }, 1850e92a4047SStephen Boyd { "s4", 0x1d00, "vdd_s4", }, 1851e92a4047SStephen Boyd { "l1", 0x4000, "vdd_l1_l3", }, 1852e92a4047SStephen Boyd { "l2", 0x4100, "vdd_l2", }, 1853e92a4047SStephen Boyd { "l3", 0x4200, "vdd_l1_l3", }, 1854e92a4047SStephen Boyd { "l4", 0x4300, "vdd_l4_l5_l6", }, 1855e92a4047SStephen Boyd { "l5", 0x4400, "vdd_l4_l5_l6", }, 1856e92a4047SStephen Boyd { "l6", 0x4500, "vdd_l4_l5_l6", }, 1857e92a4047SStephen Boyd { "l7", 0x4600, "vdd_l7", }, 1858e92a4047SStephen Boyd { "l8", 0x4700, "vdd_l8_l11_l14_l15_l16", }, 1859e92a4047SStephen Boyd { "l9", 0x4800, "vdd_l9_l10_l12_l13_l17_l18", }, 1860e92a4047SStephen Boyd { "l10", 0x4900, "vdd_l9_l10_l12_l13_l17_l18", }, 1861e92a4047SStephen Boyd { "l11", 0x4a00, "vdd_l8_l11_l14_l15_l16", }, 1862e92a4047SStephen Boyd { "l12", 0x4b00, "vdd_l9_l10_l12_l13_l17_l18", }, 1863e92a4047SStephen Boyd { "l13", 0x4c00, "vdd_l9_l10_l12_l13_l17_l18", }, 1864e92a4047SStephen Boyd { "l14", 0x4d00, "vdd_l8_l11_l14_l15_l16", }, 1865e92a4047SStephen Boyd { "l15", 0x4e00, "vdd_l8_l11_l14_l15_l16", }, 1866e92a4047SStephen Boyd { "l16", 0x4f00, "vdd_l8_l11_l14_l15_l16", }, 1867e92a4047SStephen Boyd { "l17", 0x5000, "vdd_l9_l10_l12_l13_l17_l18", }, 1868e92a4047SStephen Boyd { "l18", 0x5100, "vdd_l9_l10_l12_l13_l17_l18", }, 1869e92a4047SStephen Boyd { } 1870e92a4047SStephen Boyd }; 1871e92a4047SStephen Boyd 187250314e55SStephen Boyd static const struct spmi_regulator_data pm8994_regulators[] = { 187350314e55SStephen Boyd { "s1", 0x1400, "vdd_s1", }, 187450314e55SStephen Boyd { "s2", 0x1700, "vdd_s2", }, 187550314e55SStephen Boyd { "s3", 0x1a00, "vdd_s3", }, 187650314e55SStephen Boyd { "s4", 0x1d00, "vdd_s4", }, 187750314e55SStephen Boyd { "s5", 0x2000, "vdd_s5", }, 187850314e55SStephen Boyd { "s6", 0x2300, "vdd_s6", }, 187950314e55SStephen Boyd { "s7", 0x2600, "vdd_s7", }, 188050314e55SStephen Boyd { "s8", 0x2900, "vdd_s8", }, 188150314e55SStephen Boyd { "s9", 0x2c00, "vdd_s9", }, 188250314e55SStephen Boyd { "s10", 0x2f00, "vdd_s10", }, 188350314e55SStephen Boyd { "s11", 0x3200, "vdd_s11", }, 188450314e55SStephen Boyd { "s12", 0x3500, "vdd_s12", }, 188550314e55SStephen Boyd { "l1", 0x4000, "vdd_l1", }, 188650314e55SStephen Boyd { "l2", 0x4100, "vdd_l2_l26_l28", }, 188750314e55SStephen Boyd { "l3", 0x4200, "vdd_l3_l11", }, 188850314e55SStephen Boyd { "l4", 0x4300, "vdd_l4_l27_l31", }, 188950314e55SStephen Boyd { "l5", 0x4400, "vdd_l5_l7", }, 189050314e55SStephen Boyd { "l6", 0x4500, "vdd_l6_l12_l32", }, 189150314e55SStephen Boyd { "l7", 0x4600, "vdd_l5_l7", }, 189250314e55SStephen Boyd { "l8", 0x4700, "vdd_l8_l16_l30", }, 189350314e55SStephen Boyd { "l9", 0x4800, "vdd_l9_l10_l18_l22", }, 189450314e55SStephen Boyd { "l10", 0x4900, "vdd_l9_l10_l18_l22", }, 189550314e55SStephen Boyd { "l11", 0x4a00, "vdd_l3_l11", }, 189650314e55SStephen Boyd { "l12", 0x4b00, "vdd_l6_l12_l32", }, 189750314e55SStephen Boyd { "l13", 0x4c00, "vdd_l13_l19_l23_l24", }, 189850314e55SStephen Boyd { "l14", 0x4d00, "vdd_l14_l15", }, 189950314e55SStephen Boyd { "l15", 0x4e00, "vdd_l14_l15", }, 190050314e55SStephen Boyd { "l16", 0x4f00, "vdd_l8_l16_l30", }, 190150314e55SStephen Boyd { "l17", 0x5000, "vdd_l17_l29", }, 190250314e55SStephen Boyd { "l18", 0x5100, "vdd_l9_l10_l18_l22", }, 190350314e55SStephen Boyd { "l19", 0x5200, "vdd_l13_l19_l23_l24", }, 190450314e55SStephen Boyd { "l20", 0x5300, "vdd_l20_l21", }, 190550314e55SStephen Boyd { "l21", 0x5400, "vdd_l20_l21", }, 190650314e55SStephen Boyd { "l22", 0x5500, "vdd_l9_l10_l18_l22", }, 190750314e55SStephen Boyd { "l23", 0x5600, "vdd_l13_l19_l23_l24", }, 190850314e55SStephen Boyd { "l24", 0x5700, "vdd_l13_l19_l23_l24", }, 190950314e55SStephen Boyd { "l25", 0x5800, "vdd_l25", }, 191050314e55SStephen Boyd { "l26", 0x5900, "vdd_l2_l26_l28", }, 191150314e55SStephen Boyd { "l27", 0x5a00, "vdd_l4_l27_l31", }, 191250314e55SStephen Boyd { "l28", 0x5b00, "vdd_l2_l26_l28", }, 191350314e55SStephen Boyd { "l29", 0x5c00, "vdd_l17_l29", }, 191450314e55SStephen Boyd { "l30", 0x5d00, "vdd_l8_l16_l30", }, 191550314e55SStephen Boyd { "l31", 0x5e00, "vdd_l4_l27_l31", }, 191650314e55SStephen Boyd { "l32", 0x5f00, "vdd_l6_l12_l32", }, 191750314e55SStephen Boyd { "lvs1", 0x8000, "vdd_lvs_1_2", }, 191850314e55SStephen Boyd { "lvs2", 0x8100, "vdd_lvs_1_2", }, 191950314e55SStephen Boyd { } 192050314e55SStephen Boyd }; 192150314e55SStephen Boyd 1922ca5cd8c9SRajendra Nayak static const struct spmi_regulator_data pmi8994_regulators[] = { 1923ca5cd8c9SRajendra Nayak { "s1", 0x1400, "vdd_s1", }, 1924ca5cd8c9SRajendra Nayak { "s2", 0x1700, "vdd_s2", }, 1925ca5cd8c9SRajendra Nayak { "s3", 0x1a00, "vdd_s3", }, 1926ca5cd8c9SRajendra Nayak { "l1", 0x4000, "vdd_l1", }, 1927ca5cd8c9SRajendra Nayak { } 1928ca5cd8c9SRajendra Nayak }; 1929ca5cd8c9SRajendra Nayak 193042ba89c8SJeffrey Hugo static const struct spmi_regulator_data pm8005_regulators[] = { 193142ba89c8SJeffrey Hugo { "s1", 0x1400, "vdd_s1", }, 193242ba89c8SJeffrey Hugo { "s2", 0x1700, "vdd_s2", }, 193342ba89c8SJeffrey Hugo { "s3", 0x1a00, "vdd_s3", }, 193442ba89c8SJeffrey Hugo { "s4", 0x1d00, "vdd_s4", }, 193542ba89c8SJeffrey Hugo { } 193642ba89c8SJeffrey Hugo }; 193742ba89c8SJeffrey Hugo 19380211f68eSJorge Ramirez static const struct spmi_regulator_data pms405_regulators[] = { 19390211f68eSJorge Ramirez { "s3", 0x1a00, "vdd_s3"}, 19400211f68eSJorge Ramirez { } 19410211f68eSJorge Ramirez }; 19420211f68eSJorge Ramirez 1943e92a4047SStephen Boyd static const struct of_device_id qcom_spmi_regulator_match[] = { 194442ba89c8SJeffrey Hugo { .compatible = "qcom,pm8005-regulators", .data = &pm8005_regulators }, 1945e92a4047SStephen Boyd { .compatible = "qcom,pm8841-regulators", .data = &pm8841_regulators }, 1946e92a4047SStephen Boyd { .compatible = "qcom,pm8916-regulators", .data = &pm8916_regulators }, 1947e92a4047SStephen Boyd { .compatible = "qcom,pm8941-regulators", .data = &pm8941_regulators }, 194850314e55SStephen Boyd { .compatible = "qcom,pm8994-regulators", .data = &pm8994_regulators }, 1949ca5cd8c9SRajendra Nayak { .compatible = "qcom,pmi8994-regulators", .data = &pmi8994_regulators }, 19500211f68eSJorge Ramirez { .compatible = "qcom,pms405-regulators", .data = &pms405_regulators }, 1951e92a4047SStephen Boyd { } 1952e92a4047SStephen Boyd }; 1953e92a4047SStephen Boyd MODULE_DEVICE_TABLE(of, qcom_spmi_regulator_match); 1954e92a4047SStephen Boyd 1955e92a4047SStephen Boyd static int qcom_spmi_regulator_probe(struct platform_device *pdev) 1956e92a4047SStephen Boyd { 1957e92a4047SStephen Boyd const struct spmi_regulator_data *reg; 195886f4ff7aSJorge Ramirez-Ortiz const struct spmi_voltage_range *range; 1959e92a4047SStephen Boyd const struct of_device_id *match; 1960e92a4047SStephen Boyd struct regulator_config config = { }; 1961e92a4047SStephen Boyd struct regulator_dev *rdev; 1962e92a4047SStephen Boyd struct spmi_regulator *vreg; 1963e92a4047SStephen Boyd struct regmap *regmap; 1964e92a4047SStephen Boyd const char *name; 1965e92a4047SStephen Boyd struct device *dev = &pdev->dev; 19660caecaa8SIlia Lin struct device_node *node = pdev->dev.of_node; 1967fffe7f52SNiklas Cassel struct device_node *syscon, *reg_node; 1968fffe7f52SNiklas Cassel struct property *reg_prop; 19690caecaa8SIlia Lin int ret, lenp; 1970e92a4047SStephen Boyd struct list_head *vreg_list; 1971e92a4047SStephen Boyd 1972e92a4047SStephen Boyd vreg_list = devm_kzalloc(dev, sizeof(*vreg_list), GFP_KERNEL); 1973e92a4047SStephen Boyd if (!vreg_list) 1974e92a4047SStephen Boyd return -ENOMEM; 1975e92a4047SStephen Boyd INIT_LIST_HEAD(vreg_list); 1976e92a4047SStephen Boyd platform_set_drvdata(pdev, vreg_list); 1977e92a4047SStephen Boyd 1978e92a4047SStephen Boyd regmap = dev_get_regmap(dev->parent, NULL); 1979e92a4047SStephen Boyd if (!regmap) 1980e92a4047SStephen Boyd return -ENODEV; 1981e92a4047SStephen Boyd 1982e92a4047SStephen Boyd match = of_match_device(qcom_spmi_regulator_match, &pdev->dev); 1983e92a4047SStephen Boyd if (!match) 1984e92a4047SStephen Boyd return -ENODEV; 1985e92a4047SStephen Boyd 19860caecaa8SIlia Lin if (of_find_property(node, "qcom,saw-reg", &lenp)) { 19870caecaa8SIlia Lin syscon = of_parse_phandle(node, "qcom,saw-reg", 0); 19880caecaa8SIlia Lin saw_regmap = syscon_node_to_regmap(syscon); 19890caecaa8SIlia Lin of_node_put(syscon); 199085046a15SNiklas Cassel if (IS_ERR(saw_regmap)) 19910caecaa8SIlia Lin dev_err(dev, "ERROR reading SAW regmap\n"); 19920caecaa8SIlia Lin } 19930caecaa8SIlia Lin 1994e92a4047SStephen Boyd for (reg = match->data; reg->name; reg++) { 19950caecaa8SIlia Lin 1996fffe7f52SNiklas Cassel if (saw_regmap) { 1997fffe7f52SNiklas Cassel reg_node = of_get_child_by_name(node, reg->name); 1998fffe7f52SNiklas Cassel reg_prop = of_find_property(reg_node, "qcom,saw-slave", 1999fffe7f52SNiklas Cassel &lenp); 2000fffe7f52SNiklas Cassel of_node_put(reg_node); 2001fffe7f52SNiklas Cassel if (reg_prop) 20020caecaa8SIlia Lin continue; 20030caecaa8SIlia Lin } 20040caecaa8SIlia Lin 2005e92a4047SStephen Boyd vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL); 2006e92a4047SStephen Boyd if (!vreg) 2007e92a4047SStephen Boyd return -ENOMEM; 2008e92a4047SStephen Boyd 2009e92a4047SStephen Boyd vreg->dev = dev; 2010e92a4047SStephen Boyd vreg->base = reg->base; 2011e92a4047SStephen Boyd vreg->regmap = regmap; 2012e92a4047SStephen Boyd if (reg->ocp) { 2013e92a4047SStephen Boyd vreg->ocp_irq = platform_get_irq_byname(pdev, reg->ocp); 2014e92a4047SStephen Boyd if (vreg->ocp_irq < 0) { 2015e92a4047SStephen Boyd ret = vreg->ocp_irq; 2016e92a4047SStephen Boyd goto err; 2017e92a4047SStephen Boyd } 2018e92a4047SStephen Boyd } 2019e92a4047SStephen Boyd vreg->desc.id = -1; 2020e92a4047SStephen Boyd vreg->desc.owner = THIS_MODULE; 2021e92a4047SStephen Boyd vreg->desc.type = REGULATOR_VOLTAGE; 20229d485332SAxel Lin vreg->desc.enable_reg = reg->base + SPMI_COMMON_REG_ENABLE; 20239d485332SAxel Lin vreg->desc.enable_mask = SPMI_COMMON_ENABLE_MASK; 20249d485332SAxel Lin vreg->desc.enable_val = SPMI_COMMON_ENABLE; 2025e92a4047SStephen Boyd vreg->desc.name = name = reg->name; 2026e92a4047SStephen Boyd vreg->desc.supply_name = reg->supply; 2027e92a4047SStephen Boyd vreg->desc.of_match = reg->name; 2028e92a4047SStephen Boyd vreg->desc.of_parse_cb = spmi_regulator_of_parse; 2029e92a4047SStephen Boyd vreg->desc.of_map_mode = spmi_regulator_of_map_mode; 2030e92a4047SStephen Boyd 2031e92a4047SStephen Boyd ret = spmi_regulator_match(vreg, reg->force_type); 2032e92a4047SStephen Boyd if (ret) 20336ee5c044SStephen Boyd continue; 2034e92a4047SStephen Boyd 2035fffe7f52SNiklas Cassel if (saw_regmap) { 2036fffe7f52SNiklas Cassel reg_node = of_get_child_by_name(node, reg->name); 2037fffe7f52SNiklas Cassel reg_prop = of_find_property(reg_node, "qcom,saw-leader", 2038fffe7f52SNiklas Cassel &lenp); 2039fffe7f52SNiklas Cassel of_node_put(reg_node); 2040fffe7f52SNiklas Cassel if (reg_prop) { 20410caecaa8SIlia Lin spmi_saw_ops = *(vreg->desc.ops); 2042fffe7f52SNiklas Cassel spmi_saw_ops.set_voltage_sel = 20430caecaa8SIlia Lin spmi_regulator_saw_set_voltage; 20440caecaa8SIlia Lin vreg->desc.ops = &spmi_saw_ops; 20450caecaa8SIlia Lin } 2046fffe7f52SNiklas Cassel } 20470caecaa8SIlia Lin 2048*b01d1823SJeffrey Hugo if (vreg->set_points && vreg->set_points->count == 1) { 204986f4ff7aSJorge Ramirez-Ortiz /* since there is only one range */ 205086f4ff7aSJorge Ramirez-Ortiz range = vreg->set_points->range; 205186f4ff7aSJorge Ramirez-Ortiz vreg->desc.uV_step = range->step_uV; 205286f4ff7aSJorge Ramirez-Ortiz } 205386f4ff7aSJorge Ramirez-Ortiz 2054e92a4047SStephen Boyd config.dev = dev; 2055e92a4047SStephen Boyd config.driver_data = vreg; 20569d485332SAxel Lin config.regmap = regmap; 2057e92a4047SStephen Boyd rdev = devm_regulator_register(dev, &vreg->desc, &config); 2058e92a4047SStephen Boyd if (IS_ERR(rdev)) { 2059e92a4047SStephen Boyd dev_err(dev, "failed to register %s\n", name); 2060e92a4047SStephen Boyd ret = PTR_ERR(rdev); 2061e92a4047SStephen Boyd goto err; 2062e92a4047SStephen Boyd } 2063e92a4047SStephen Boyd 2064e92a4047SStephen Boyd INIT_LIST_HEAD(&vreg->node); 2065e92a4047SStephen Boyd list_add(&vreg->node, vreg_list); 2066e92a4047SStephen Boyd } 2067e92a4047SStephen Boyd 2068e92a4047SStephen Boyd return 0; 2069e92a4047SStephen Boyd 2070e92a4047SStephen Boyd err: 2071e92a4047SStephen Boyd list_for_each_entry(vreg, vreg_list, node) 2072e92a4047SStephen Boyd if (vreg->ocp_irq) 2073e92a4047SStephen Boyd cancel_delayed_work_sync(&vreg->ocp_work); 2074e92a4047SStephen Boyd return ret; 2075e92a4047SStephen Boyd } 2076e92a4047SStephen Boyd 2077e92a4047SStephen Boyd static int qcom_spmi_regulator_remove(struct platform_device *pdev) 2078e92a4047SStephen Boyd { 2079e92a4047SStephen Boyd struct spmi_regulator *vreg; 2080e92a4047SStephen Boyd struct list_head *vreg_list = platform_get_drvdata(pdev); 2081e92a4047SStephen Boyd 2082e92a4047SStephen Boyd list_for_each_entry(vreg, vreg_list, node) 2083e92a4047SStephen Boyd if (vreg->ocp_irq) 2084e92a4047SStephen Boyd cancel_delayed_work_sync(&vreg->ocp_work); 2085e92a4047SStephen Boyd 2086e92a4047SStephen Boyd return 0; 2087e92a4047SStephen Boyd } 2088e92a4047SStephen Boyd 2089e92a4047SStephen Boyd static struct platform_driver qcom_spmi_regulator_driver = { 2090e92a4047SStephen Boyd .driver = { 2091e92a4047SStephen Boyd .name = "qcom-spmi-regulator", 2092e92a4047SStephen Boyd .of_match_table = qcom_spmi_regulator_match, 2093e92a4047SStephen Boyd }, 2094e92a4047SStephen Boyd .probe = qcom_spmi_regulator_probe, 2095e92a4047SStephen Boyd .remove = qcom_spmi_regulator_remove, 2096e92a4047SStephen Boyd }; 2097e92a4047SStephen Boyd module_platform_driver(qcom_spmi_regulator_driver); 2098e92a4047SStephen Boyd 2099e92a4047SStephen Boyd MODULE_DESCRIPTION("Qualcomm SPMI PMIC regulator driver"); 2100e92a4047SStephen Boyd MODULE_LICENSE("GPL v2"); 2101e92a4047SStephen Boyd MODULE_ALIAS("platform:qcom-spmi-regulator"); 2102