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, 100*0211f68eSJorge 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, 153*0211f68eSJorge 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 299*0211f68eSJorge Ramirez #define SPMI_HFS430_CLOCK_RATE 1600 300*0211f68eSJorge 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 514*0211f68eSJorge Ramirez static struct spmi_voltage_range hfs430_ranges[] = { 515*0211f68eSJorge Ramirez SPMI_VOLTAGE_RANGE(0, 320000, 320000, 2040000, 2040000, 8000), 516*0211f68eSJorge Ramirez }; 517*0211f68eSJorge 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); 533*0211f68eSJorge 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 const struct spmi_voltage_range *range; 817e92a4047SStephen Boyd int diff_uV; 818e92a4047SStephen Boyd 819e92a4047SStephen Boyd range = spmi_regulator_find_range(vreg); 820e92a4047SStephen Boyd if (!range) 821e92a4047SStephen Boyd return -EINVAL; 822e92a4047SStephen Boyd 823e92a4047SStephen Boyd diff_uV = abs(new_selector - old_selector) * range->step_uV; 824e92a4047SStephen Boyd 825e92a4047SStephen Boyd return DIV_ROUND_UP(diff_uV, vreg->slew_rate); 826e92a4047SStephen Boyd } 827e92a4047SStephen Boyd 828e92a4047SStephen Boyd static int spmi_regulator_common_get_voltage(struct regulator_dev *rdev) 829e92a4047SStephen Boyd { 830e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 831e92a4047SStephen Boyd const struct spmi_voltage_range *range; 832e92a4047SStephen Boyd u8 voltage_sel; 833e92a4047SStephen Boyd 834e92a4047SStephen Boyd spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1); 835e92a4047SStephen Boyd 836e92a4047SStephen Boyd range = spmi_regulator_find_range(vreg); 837e92a4047SStephen Boyd if (!range) 8381b5b1968SStephen Boyd return -EINVAL; 839e92a4047SStephen Boyd 8401b5b1968SStephen Boyd return spmi_hw_selector_to_sw(vreg, voltage_sel, range); 8411b5b1968SStephen Boyd } 8421b5b1968SStephen Boyd 84342ba89c8SJeffrey Hugo static int spmi_regulator_ftsmps426_get_voltage(struct regulator_dev *rdev) 84442ba89c8SJeffrey Hugo { 84542ba89c8SJeffrey Hugo struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 84642ba89c8SJeffrey Hugo const struct spmi_voltage_range *range; 84742ba89c8SJeffrey Hugo u8 buf[2]; 84842ba89c8SJeffrey Hugo int uV; 84942ba89c8SJeffrey Hugo 85042ba89c8SJeffrey Hugo spmi_vreg_read(vreg, SPMI_FTSMPS426_REG_VOLTAGE_LSB, buf, 2); 85142ba89c8SJeffrey Hugo 85242ba89c8SJeffrey Hugo uV = (((unsigned int)buf[1] << 8) | (unsigned int)buf[0]) * 1000; 85342ba89c8SJeffrey Hugo range = vreg->set_points->range; 85442ba89c8SJeffrey Hugo 85542ba89c8SJeffrey Hugo return (uV - range->set_point_min_uV) / range->step_uV; 85642ba89c8SJeffrey Hugo } 85742ba89c8SJeffrey Hugo 8581b5b1968SStephen Boyd static int spmi_regulator_single_map_voltage(struct regulator_dev *rdev, 8591b5b1968SStephen Boyd int min_uV, int max_uV) 8601b5b1968SStephen Boyd { 8611b5b1968SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 8621b5b1968SStephen Boyd 8631b5b1968SStephen Boyd return spmi_regulator_select_voltage(vreg, min_uV, max_uV); 864e92a4047SStephen Boyd } 865e92a4047SStephen Boyd 866e92a4047SStephen Boyd static int spmi_regulator_single_range_set_voltage(struct regulator_dev *rdev, 8671b5b1968SStephen Boyd unsigned selector) 868e92a4047SStephen Boyd { 869e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 8701b5b1968SStephen Boyd u8 sel = selector; 871e92a4047SStephen Boyd 872e92a4047SStephen Boyd /* 873e92a4047SStephen Boyd * Certain types of regulators do not have a range select register so 874e92a4047SStephen Boyd * only voltage set register needs to be written. 875e92a4047SStephen Boyd */ 876e92a4047SStephen Boyd return spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &sel, 1); 877e92a4047SStephen Boyd } 878e92a4047SStephen Boyd 879e92a4047SStephen Boyd static int spmi_regulator_single_range_get_voltage(struct regulator_dev *rdev) 880e92a4047SStephen Boyd { 881e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 8821b5b1968SStephen Boyd u8 selector; 8831b5b1968SStephen Boyd int ret; 884e92a4047SStephen Boyd 8851b5b1968SStephen Boyd ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &selector, 1); 8861b5b1968SStephen Boyd if (ret) 8871b5b1968SStephen Boyd return ret; 888e92a4047SStephen Boyd 8891b5b1968SStephen Boyd return selector; 890e92a4047SStephen Boyd } 891e92a4047SStephen Boyd 892e92a4047SStephen Boyd static int spmi_regulator_ult_lo_smps_set_voltage(struct regulator_dev *rdev, 8931b5b1968SStephen Boyd unsigned selector) 894e92a4047SStephen Boyd { 895e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 896e92a4047SStephen Boyd int ret; 897e92a4047SStephen Boyd u8 range_sel, voltage_sel; 898e92a4047SStephen Boyd 8991b5b1968SStephen Boyd ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel); 900e92a4047SStephen Boyd if (ret) 901e92a4047SStephen Boyd return ret; 902e92a4047SStephen Boyd 903e92a4047SStephen Boyd /* 904e92a4047SStephen Boyd * Calculate VSET based on range 905e92a4047SStephen Boyd * In case of range 0: voltage_sel is a 7 bit value, can be written 906e92a4047SStephen Boyd * witout any modification. 907e92a4047SStephen Boyd * In case of range 1: voltage_sel is a 5 bit value, bits[7-5] set to 908e92a4047SStephen Boyd * [011]. 909e92a4047SStephen Boyd */ 910e92a4047SStephen Boyd if (range_sel == 1) 911e92a4047SStephen Boyd voltage_sel |= ULT_SMPS_RANGE_SPLIT; 912e92a4047SStephen Boyd 9130f94bffaSJulia Lawall return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_VOLTAGE_SET, 914e92a4047SStephen Boyd voltage_sel, 0xff); 915e92a4047SStephen Boyd } 916e92a4047SStephen Boyd 917e92a4047SStephen Boyd static int spmi_regulator_ult_lo_smps_get_voltage(struct regulator_dev *rdev) 918e92a4047SStephen Boyd { 919e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 920e92a4047SStephen Boyd const struct spmi_voltage_range *range; 921e92a4047SStephen Boyd u8 voltage_sel; 922e92a4047SStephen Boyd 923e92a4047SStephen Boyd spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_SET, &voltage_sel, 1); 924e92a4047SStephen Boyd 925e92a4047SStephen Boyd range = spmi_regulator_find_range(vreg); 926e92a4047SStephen Boyd if (!range) 9271b5b1968SStephen Boyd return -EINVAL; 928e92a4047SStephen Boyd 929e92a4047SStephen Boyd if (range->range_sel == 1) 930e92a4047SStephen Boyd voltage_sel &= ~ULT_SMPS_RANGE_SPLIT; 931e92a4047SStephen Boyd 9321b5b1968SStephen Boyd return spmi_hw_selector_to_sw(vreg, voltage_sel, range); 933e92a4047SStephen Boyd } 934e92a4047SStephen Boyd 935e92a4047SStephen Boyd static int spmi_regulator_common_list_voltage(struct regulator_dev *rdev, 936e92a4047SStephen Boyd unsigned selector) 937e92a4047SStephen Boyd { 938e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 939e92a4047SStephen Boyd int uV = 0; 940e92a4047SStephen Boyd int i; 941e92a4047SStephen Boyd 942e92a4047SStephen Boyd if (selector >= vreg->set_points->n_voltages) 943e92a4047SStephen Boyd return 0; 944e92a4047SStephen Boyd 945e92a4047SStephen Boyd for (i = 0; i < vreg->set_points->count; i++) { 9469b2dfee3SStephen Boyd if (selector < vreg->set_points->range[i].n_voltages) { 947e92a4047SStephen Boyd uV = selector * vreg->set_points->range[i].step_uV 948e92a4047SStephen Boyd + vreg->set_points->range[i].set_point_min_uV; 949e92a4047SStephen Boyd break; 9509b2dfee3SStephen Boyd } 951e92a4047SStephen Boyd 952e92a4047SStephen Boyd selector -= vreg->set_points->range[i].n_voltages; 953e92a4047SStephen Boyd } 954e92a4047SStephen Boyd 955e92a4047SStephen Boyd return uV; 956e92a4047SStephen Boyd } 957e92a4047SStephen Boyd 958e92a4047SStephen Boyd static int 959e92a4047SStephen Boyd spmi_regulator_common_set_bypass(struct regulator_dev *rdev, bool enable) 960e92a4047SStephen Boyd { 961e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 962e92a4047SStephen Boyd u8 mask = SPMI_COMMON_MODE_BYPASS_MASK; 963e92a4047SStephen Boyd u8 val = 0; 964e92a4047SStephen Boyd 965e92a4047SStephen Boyd if (enable) 966e92a4047SStephen Boyd val = mask; 967e92a4047SStephen Boyd 968e92a4047SStephen Boyd return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask); 969e92a4047SStephen Boyd } 970e92a4047SStephen Boyd 971e92a4047SStephen Boyd static int 972e92a4047SStephen Boyd spmi_regulator_common_get_bypass(struct regulator_dev *rdev, bool *enable) 973e92a4047SStephen Boyd { 974e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 975e92a4047SStephen Boyd u8 val; 976e92a4047SStephen Boyd int ret; 977e92a4047SStephen Boyd 978e92a4047SStephen Boyd ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, &val, 1); 979e92a4047SStephen Boyd *enable = val & SPMI_COMMON_MODE_BYPASS_MASK; 980e92a4047SStephen Boyd 981e92a4047SStephen Boyd return ret; 982e92a4047SStephen Boyd } 983e92a4047SStephen Boyd 984e92a4047SStephen Boyd static unsigned int spmi_regulator_common_get_mode(struct regulator_dev *rdev) 985e92a4047SStephen Boyd { 986e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 987e92a4047SStephen Boyd u8 reg; 988e92a4047SStephen Boyd 989e92a4047SStephen Boyd spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, ®, 1); 990e92a4047SStephen Boyd 991ba576a62SJeffrey Hugo reg &= SPMI_COMMON_MODE_HPM_MASK | SPMI_COMMON_MODE_AUTO_MASK; 992ba576a62SJeffrey Hugo 993ba576a62SJeffrey Hugo switch (reg) { 994ba576a62SJeffrey Hugo case SPMI_COMMON_MODE_HPM_MASK: 995e92a4047SStephen Boyd return REGULATOR_MODE_NORMAL; 996ba576a62SJeffrey Hugo case SPMI_COMMON_MODE_AUTO_MASK: 997e2adfacdSStephen Boyd return REGULATOR_MODE_FAST; 998ba576a62SJeffrey Hugo default: 999e92a4047SStephen Boyd return REGULATOR_MODE_IDLE; 1000e92a4047SStephen Boyd } 1001ba576a62SJeffrey Hugo } 1002e92a4047SStephen Boyd 100342ba89c8SJeffrey Hugo static unsigned int spmi_regulator_ftsmps426_get_mode(struct regulator_dev *rdev) 100442ba89c8SJeffrey Hugo { 100542ba89c8SJeffrey Hugo struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 100642ba89c8SJeffrey Hugo u8 reg; 100742ba89c8SJeffrey Hugo 100842ba89c8SJeffrey Hugo spmi_vreg_read(vreg, SPMI_COMMON_REG_MODE, ®, 1); 100942ba89c8SJeffrey Hugo 101042ba89c8SJeffrey Hugo switch (reg) { 101142ba89c8SJeffrey Hugo case SPMI_FTSMPS426_MODE_HPM_MASK: 101242ba89c8SJeffrey Hugo return REGULATOR_MODE_NORMAL; 101342ba89c8SJeffrey Hugo case SPMI_FTSMPS426_MODE_AUTO_MASK: 101442ba89c8SJeffrey Hugo return REGULATOR_MODE_FAST; 101542ba89c8SJeffrey Hugo default: 101642ba89c8SJeffrey Hugo return REGULATOR_MODE_IDLE; 101742ba89c8SJeffrey Hugo } 101842ba89c8SJeffrey Hugo } 101942ba89c8SJeffrey Hugo 1020e92a4047SStephen Boyd static int 1021e92a4047SStephen Boyd spmi_regulator_common_set_mode(struct regulator_dev *rdev, unsigned int mode) 1022e92a4047SStephen Boyd { 1023e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 1024e2adfacdSStephen Boyd u8 mask = SPMI_COMMON_MODE_HPM_MASK | SPMI_COMMON_MODE_AUTO_MASK; 1025ba576a62SJeffrey Hugo u8 val; 1026e92a4047SStephen Boyd 1027ba576a62SJeffrey Hugo switch (mode) { 1028ba576a62SJeffrey Hugo case REGULATOR_MODE_NORMAL: 1029e2adfacdSStephen Boyd val = SPMI_COMMON_MODE_HPM_MASK; 1030ba576a62SJeffrey Hugo break; 1031ba576a62SJeffrey Hugo case REGULATOR_MODE_FAST: 1032e2adfacdSStephen Boyd val = SPMI_COMMON_MODE_AUTO_MASK; 1033ba576a62SJeffrey Hugo break; 1034ba576a62SJeffrey Hugo default: 1035ba576a62SJeffrey Hugo val = 0; 1036ba576a62SJeffrey Hugo break; 1037ba576a62SJeffrey Hugo } 1038e92a4047SStephen Boyd 1039e92a4047SStephen Boyd return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask); 1040e92a4047SStephen Boyd } 1041e92a4047SStephen Boyd 1042e92a4047SStephen Boyd static int 104342ba89c8SJeffrey Hugo spmi_regulator_ftsmps426_set_mode(struct regulator_dev *rdev, unsigned int mode) 104442ba89c8SJeffrey Hugo { 104542ba89c8SJeffrey Hugo struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 104642ba89c8SJeffrey Hugo u8 mask = SPMI_FTSMPS426_MODE_MASK; 104742ba89c8SJeffrey Hugo u8 val; 104842ba89c8SJeffrey Hugo 104942ba89c8SJeffrey Hugo switch (mode) { 105042ba89c8SJeffrey Hugo case REGULATOR_MODE_NORMAL: 105142ba89c8SJeffrey Hugo val = SPMI_FTSMPS426_MODE_HPM_MASK; 105242ba89c8SJeffrey Hugo break; 105342ba89c8SJeffrey Hugo case REGULATOR_MODE_FAST: 105442ba89c8SJeffrey Hugo val = SPMI_FTSMPS426_MODE_AUTO_MASK; 105542ba89c8SJeffrey Hugo break; 105642ba89c8SJeffrey Hugo case REGULATOR_MODE_IDLE: 105742ba89c8SJeffrey Hugo val = SPMI_FTSMPS426_MODE_LPM_MASK; 105842ba89c8SJeffrey Hugo break; 105942ba89c8SJeffrey Hugo default: 106042ba89c8SJeffrey Hugo return -EINVAL; 106142ba89c8SJeffrey Hugo } 106242ba89c8SJeffrey Hugo 106342ba89c8SJeffrey Hugo return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_MODE, val, mask); 106442ba89c8SJeffrey Hugo } 106542ba89c8SJeffrey Hugo 106642ba89c8SJeffrey Hugo static int 1067e92a4047SStephen Boyd spmi_regulator_common_set_load(struct regulator_dev *rdev, int load_uA) 1068e92a4047SStephen Boyd { 1069e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 1070e92a4047SStephen Boyd unsigned int mode; 1071e92a4047SStephen Boyd 1072e92a4047SStephen Boyd if (load_uA >= vreg->hpm_min_load) 1073e92a4047SStephen Boyd mode = REGULATOR_MODE_NORMAL; 1074e92a4047SStephen Boyd else 1075e92a4047SStephen Boyd mode = REGULATOR_MODE_IDLE; 1076e92a4047SStephen Boyd 1077e92a4047SStephen Boyd return spmi_regulator_common_set_mode(rdev, mode); 1078e92a4047SStephen Boyd } 1079e92a4047SStephen Boyd 1080e92a4047SStephen Boyd static int spmi_regulator_common_set_pull_down(struct regulator_dev *rdev) 1081e92a4047SStephen Boyd { 1082e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 1083e92a4047SStephen Boyd unsigned int mask = SPMI_COMMON_PULL_DOWN_ENABLE_MASK; 1084e92a4047SStephen Boyd 1085e92a4047SStephen Boyd return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_PULL_DOWN, 1086e92a4047SStephen Boyd mask, mask); 1087e92a4047SStephen Boyd } 1088e92a4047SStephen Boyd 1089e92a4047SStephen Boyd static int spmi_regulator_common_set_soft_start(struct regulator_dev *rdev) 1090e92a4047SStephen Boyd { 1091e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 1092e92a4047SStephen Boyd unsigned int mask = SPMI_LDO_SOFT_START_ENABLE_MASK; 1093e92a4047SStephen Boyd 1094e92a4047SStephen Boyd return spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_SOFT_START, 1095e92a4047SStephen Boyd mask, mask); 1096e92a4047SStephen Boyd } 1097e92a4047SStephen Boyd 1098e92a4047SStephen Boyd static int spmi_regulator_set_ilim(struct regulator_dev *rdev, int ilim_uA) 1099e92a4047SStephen Boyd { 1100e92a4047SStephen Boyd struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 1101e92a4047SStephen Boyd enum spmi_regulator_logical_type type = vreg->logical_type; 1102e92a4047SStephen Boyd unsigned int current_reg; 1103e92a4047SStephen Boyd u8 reg; 1104e92a4047SStephen Boyd u8 mask = SPMI_BOOST_CURRENT_LIMIT_MASK | 1105e92a4047SStephen Boyd SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK; 1106e92a4047SStephen Boyd int max = (SPMI_BOOST_CURRENT_LIMIT_MASK + 1) * 500; 1107e92a4047SStephen Boyd 1108e92a4047SStephen Boyd if (type == SPMI_REGULATOR_LOGICAL_TYPE_BOOST) 1109e92a4047SStephen Boyd current_reg = SPMI_BOOST_REG_CURRENT_LIMIT; 1110e92a4047SStephen Boyd else 1111e92a4047SStephen Boyd current_reg = SPMI_BOOST_BYP_REG_CURRENT_LIMIT; 1112e92a4047SStephen Boyd 1113e92a4047SStephen Boyd if (ilim_uA > max || ilim_uA <= 0) 1114e92a4047SStephen Boyd return -EINVAL; 1115e92a4047SStephen Boyd 1116e92a4047SStephen Boyd reg = (ilim_uA - 1) / 500; 1117e92a4047SStephen Boyd reg |= SPMI_BOOST_CURRENT_LIMIT_ENABLE_MASK; 1118e92a4047SStephen Boyd 1119e92a4047SStephen Boyd return spmi_vreg_update_bits(vreg, current_reg, reg, mask); 1120e92a4047SStephen Boyd } 1121e92a4047SStephen Boyd 1122e92a4047SStephen Boyd static int spmi_regulator_vs_clear_ocp(struct spmi_regulator *vreg) 1123e92a4047SStephen Boyd { 1124e92a4047SStephen Boyd int ret; 1125e92a4047SStephen Boyd 1126e92a4047SStephen Boyd ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE, 1127e92a4047SStephen Boyd SPMI_COMMON_DISABLE, SPMI_COMMON_ENABLE_MASK); 1128e92a4047SStephen Boyd 1129e92a4047SStephen Boyd vreg->vs_enable_time = ktime_get(); 1130e92a4047SStephen Boyd 1131e92a4047SStephen Boyd ret = spmi_vreg_update_bits(vreg, SPMI_COMMON_REG_ENABLE, 1132e92a4047SStephen Boyd SPMI_COMMON_ENABLE, SPMI_COMMON_ENABLE_MASK); 1133e92a4047SStephen Boyd 1134e92a4047SStephen Boyd return ret; 1135e92a4047SStephen Boyd } 1136e92a4047SStephen Boyd 1137e92a4047SStephen Boyd static void spmi_regulator_vs_ocp_work(struct work_struct *work) 1138e92a4047SStephen Boyd { 1139e92a4047SStephen Boyd struct delayed_work *dwork = to_delayed_work(work); 1140e92a4047SStephen Boyd struct spmi_regulator *vreg 1141e92a4047SStephen Boyd = container_of(dwork, struct spmi_regulator, ocp_work); 1142e92a4047SStephen Boyd 1143e92a4047SStephen Boyd spmi_regulator_vs_clear_ocp(vreg); 1144e92a4047SStephen Boyd } 1145e92a4047SStephen Boyd 1146e92a4047SStephen Boyd static irqreturn_t spmi_regulator_vs_ocp_isr(int irq, void *data) 1147e92a4047SStephen Boyd { 1148e92a4047SStephen Boyd struct spmi_regulator *vreg = data; 1149e92a4047SStephen Boyd ktime_t ocp_irq_time; 1150e92a4047SStephen Boyd s64 ocp_trigger_delay_us; 1151e92a4047SStephen Boyd 1152e92a4047SStephen Boyd ocp_irq_time = ktime_get(); 1153e92a4047SStephen Boyd ocp_trigger_delay_us = ktime_us_delta(ocp_irq_time, 1154e92a4047SStephen Boyd vreg->vs_enable_time); 1155e92a4047SStephen Boyd 1156e92a4047SStephen Boyd /* 1157e92a4047SStephen Boyd * Reset the OCP count if there is a large delay between switch enable 1158e92a4047SStephen Boyd * and when OCP triggers. This is indicative of a hotplug event as 1159e92a4047SStephen Boyd * opposed to a fault. 1160e92a4047SStephen Boyd */ 1161e92a4047SStephen Boyd if (ocp_trigger_delay_us > SPMI_VS_OCP_FAULT_DELAY_US) 1162e92a4047SStephen Boyd vreg->ocp_count = 0; 1163e92a4047SStephen Boyd 1164e92a4047SStephen Boyd /* Wait for switch output to settle back to 0 V after OCP triggered. */ 1165e92a4047SStephen Boyd udelay(SPMI_VS_OCP_FALL_DELAY_US); 1166e92a4047SStephen Boyd 1167e92a4047SStephen Boyd vreg->ocp_count++; 1168e92a4047SStephen Boyd 1169e92a4047SStephen Boyd if (vreg->ocp_count == 1) { 1170e92a4047SStephen Boyd /* Immediately clear the over current condition. */ 1171e92a4047SStephen Boyd spmi_regulator_vs_clear_ocp(vreg); 1172e92a4047SStephen Boyd } else if (vreg->ocp_count <= vreg->ocp_max_retries) { 1173e92a4047SStephen Boyd /* Schedule the over current clear task to run later. */ 1174e92a4047SStephen Boyd schedule_delayed_work(&vreg->ocp_work, 1175e92a4047SStephen Boyd msecs_to_jiffies(vreg->ocp_retry_delay_ms) + 1); 1176e92a4047SStephen Boyd } else { 1177e92a4047SStephen Boyd dev_err(vreg->dev, 1178e92a4047SStephen Boyd "OCP triggered %d times; no further retries\n", 1179e92a4047SStephen Boyd vreg->ocp_count); 1180e92a4047SStephen Boyd } 1181e92a4047SStephen Boyd 1182e92a4047SStephen Boyd return IRQ_HANDLED; 1183e92a4047SStephen Boyd } 1184e92a4047SStephen Boyd 11850caecaa8SIlia Lin #define SAW3_VCTL_DATA_MASK 0xFF 11860caecaa8SIlia Lin #define SAW3_VCTL_CLEAR_MASK 0x700FF 11870caecaa8SIlia Lin #define SAW3_AVS_CTL_EN_MASK 0x1 11880caecaa8SIlia Lin #define SAW3_AVS_CTL_TGGL_MASK 0x8000000 11890caecaa8SIlia Lin #define SAW3_AVS_CTL_CLEAR_MASK 0x7efc00 11900caecaa8SIlia Lin 11919689ca0aSNiklas Cassel static struct regmap *saw_regmap; 11920caecaa8SIlia Lin 11930caecaa8SIlia Lin static void spmi_saw_set_vdd(void *data) 11940caecaa8SIlia Lin { 11950caecaa8SIlia Lin u32 vctl, data3, avs_ctl, pmic_sts; 11960caecaa8SIlia Lin bool avs_enabled = false; 11970caecaa8SIlia Lin unsigned long timeout; 11980caecaa8SIlia Lin u8 voltage_sel = *(u8 *)data; 11990caecaa8SIlia Lin 12000caecaa8SIlia Lin regmap_read(saw_regmap, SAW3_AVS_CTL, &avs_ctl); 12010caecaa8SIlia Lin regmap_read(saw_regmap, SAW3_VCTL, &vctl); 12020caecaa8SIlia Lin regmap_read(saw_regmap, SAW3_SPM_PMIC_DATA_3, &data3); 12030caecaa8SIlia Lin 12040caecaa8SIlia Lin /* select the band */ 12050caecaa8SIlia Lin vctl &= ~SAW3_VCTL_CLEAR_MASK; 12060caecaa8SIlia Lin vctl |= (u32)voltage_sel; 12070caecaa8SIlia Lin 12080caecaa8SIlia Lin data3 &= ~SAW3_VCTL_CLEAR_MASK; 12090caecaa8SIlia Lin data3 |= (u32)voltage_sel; 12100caecaa8SIlia Lin 12110caecaa8SIlia Lin /* If AVS is enabled, switch it off during the voltage change */ 12120caecaa8SIlia Lin avs_enabled = SAW3_AVS_CTL_EN_MASK & avs_ctl; 12130caecaa8SIlia Lin if (avs_enabled) { 12140caecaa8SIlia Lin avs_ctl &= ~SAW3_AVS_CTL_TGGL_MASK; 12150caecaa8SIlia Lin regmap_write(saw_regmap, SAW3_AVS_CTL, avs_ctl); 12160caecaa8SIlia Lin } 12170caecaa8SIlia Lin 12180caecaa8SIlia Lin regmap_write(saw_regmap, SAW3_RST, 1); 12190caecaa8SIlia Lin regmap_write(saw_regmap, SAW3_VCTL, vctl); 12200caecaa8SIlia Lin regmap_write(saw_regmap, SAW3_SPM_PMIC_DATA_3, data3); 12210caecaa8SIlia Lin 12220caecaa8SIlia Lin timeout = jiffies + usecs_to_jiffies(100); 12230caecaa8SIlia Lin do { 12240caecaa8SIlia Lin regmap_read(saw_regmap, SAW3_PMIC_STS, &pmic_sts); 12250caecaa8SIlia Lin pmic_sts &= SAW3_VCTL_DATA_MASK; 12260caecaa8SIlia Lin if (pmic_sts == (u32)voltage_sel) 12270caecaa8SIlia Lin break; 12280caecaa8SIlia Lin 12290caecaa8SIlia Lin cpu_relax(); 12300caecaa8SIlia Lin 12310caecaa8SIlia Lin } while (time_before(jiffies, timeout)); 12320caecaa8SIlia Lin 12330caecaa8SIlia Lin /* After successful voltage change, switch the AVS back on */ 12340caecaa8SIlia Lin if (avs_enabled) { 12350caecaa8SIlia Lin pmic_sts &= 0x3f; 12360caecaa8SIlia Lin avs_ctl &= ~SAW3_AVS_CTL_CLEAR_MASK; 12370caecaa8SIlia Lin avs_ctl |= ((pmic_sts - 4) << 10); 12380caecaa8SIlia Lin avs_ctl |= (pmic_sts << 17); 12390caecaa8SIlia Lin avs_ctl |= SAW3_AVS_CTL_TGGL_MASK; 12400caecaa8SIlia Lin regmap_write(saw_regmap, SAW3_AVS_CTL, avs_ctl); 12410caecaa8SIlia Lin } 12420caecaa8SIlia Lin } 12430caecaa8SIlia Lin 12440caecaa8SIlia Lin static int 12450caecaa8SIlia Lin spmi_regulator_saw_set_voltage(struct regulator_dev *rdev, unsigned selector) 12460caecaa8SIlia Lin { 12470caecaa8SIlia Lin struct spmi_regulator *vreg = rdev_get_drvdata(rdev); 12480caecaa8SIlia Lin int ret; 12490caecaa8SIlia Lin u8 range_sel, voltage_sel; 12500caecaa8SIlia Lin 12510caecaa8SIlia Lin ret = spmi_sw_selector_to_hw(vreg, selector, &range_sel, &voltage_sel); 12520caecaa8SIlia Lin if (ret) 12530caecaa8SIlia Lin return ret; 12540caecaa8SIlia Lin 12550caecaa8SIlia Lin if (0 != range_sel) { 12560caecaa8SIlia Lin dev_dbg(&rdev->dev, "range_sel = %02X voltage_sel = %02X", \ 12570caecaa8SIlia Lin range_sel, voltage_sel); 12580caecaa8SIlia Lin return -EINVAL; 12590caecaa8SIlia Lin } 12600caecaa8SIlia Lin 12610caecaa8SIlia Lin /* Always do the SAW register writes on the first CPU */ 12620caecaa8SIlia Lin return smp_call_function_single(0, spmi_saw_set_vdd, \ 12630caecaa8SIlia Lin &voltage_sel, true); 12640caecaa8SIlia Lin } 12650caecaa8SIlia Lin 12660caecaa8SIlia Lin static struct regulator_ops spmi_saw_ops = {}; 12670caecaa8SIlia Lin 1268e92a4047SStephen Boyd static struct regulator_ops spmi_smps_ops = { 12699d485332SAxel Lin .enable = regulator_enable_regmap, 12709d485332SAxel Lin .disable = regulator_disable_regmap, 12719d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 12721b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_common_set_voltage, 12732cf7b99cSStephen Boyd .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 12741b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_common_get_voltage, 12751b5b1968SStephen Boyd .map_voltage = spmi_regulator_common_map_voltage, 1276e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1277e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1278e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1279e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1280e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1281e92a4047SStephen Boyd }; 1282e92a4047SStephen Boyd 1283e92a4047SStephen Boyd static struct regulator_ops spmi_ldo_ops = { 12849d485332SAxel Lin .enable = regulator_enable_regmap, 12859d485332SAxel Lin .disable = regulator_disable_regmap, 12869d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 12871b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_common_set_voltage, 12881b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_common_get_voltage, 12891b5b1968SStephen Boyd .map_voltage = spmi_regulator_common_map_voltage, 1290e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1291e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1292e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1293e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1294e92a4047SStephen Boyd .set_bypass = spmi_regulator_common_set_bypass, 1295e92a4047SStephen Boyd .get_bypass = spmi_regulator_common_get_bypass, 1296e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1297e92a4047SStephen Boyd .set_soft_start = spmi_regulator_common_set_soft_start, 1298e92a4047SStephen Boyd }; 1299e92a4047SStephen Boyd 1300e92a4047SStephen Boyd static struct regulator_ops spmi_ln_ldo_ops = { 13019d485332SAxel Lin .enable = regulator_enable_regmap, 13029d485332SAxel Lin .disable = regulator_disable_regmap, 13039d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13041b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_common_set_voltage, 13051b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_common_get_voltage, 13061b5b1968SStephen Boyd .map_voltage = spmi_regulator_common_map_voltage, 1307e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1308e92a4047SStephen Boyd .set_bypass = spmi_regulator_common_set_bypass, 1309e92a4047SStephen Boyd .get_bypass = spmi_regulator_common_get_bypass, 1310e92a4047SStephen Boyd }; 1311e92a4047SStephen Boyd 1312e92a4047SStephen Boyd static struct regulator_ops spmi_vs_ops = { 1313e92a4047SStephen Boyd .enable = spmi_regulator_vs_enable, 13149d485332SAxel Lin .disable = regulator_disable_regmap, 13159d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 1316e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1317e92a4047SStephen Boyd .set_soft_start = spmi_regulator_common_set_soft_start, 1318e2adfacdSStephen Boyd .set_over_current_protection = spmi_regulator_vs_ocp, 1319919163f6SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1320919163f6SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1321e92a4047SStephen Boyd }; 1322e92a4047SStephen Boyd 1323e92a4047SStephen Boyd static struct regulator_ops spmi_boost_ops = { 13249d485332SAxel Lin .enable = regulator_enable_regmap, 13259d485332SAxel Lin .disable = regulator_disable_regmap, 13269d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13271b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_single_range_set_voltage, 13281b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_single_range_get_voltage, 13291b5b1968SStephen Boyd .map_voltage = spmi_regulator_single_map_voltage, 1330e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1331e92a4047SStephen Boyd .set_input_current_limit = spmi_regulator_set_ilim, 1332e92a4047SStephen Boyd }; 1333e92a4047SStephen Boyd 1334e92a4047SStephen Boyd static struct regulator_ops spmi_ftsmps_ops = { 13359d485332SAxel Lin .enable = regulator_enable_regmap, 13369d485332SAxel Lin .disable = regulator_disable_regmap, 13379d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13381b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_common_set_voltage, 1339e92a4047SStephen Boyd .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 13401b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_common_get_voltage, 13411b5b1968SStephen Boyd .map_voltage = spmi_regulator_common_map_voltage, 1342e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1343e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1344e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1345e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1346e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1347e92a4047SStephen Boyd }; 1348e92a4047SStephen Boyd 1349e92a4047SStephen Boyd static struct regulator_ops spmi_ult_lo_smps_ops = { 13509d485332SAxel Lin .enable = regulator_enable_regmap, 13519d485332SAxel Lin .disable = regulator_disable_regmap, 13529d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13531b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_ult_lo_smps_set_voltage, 13542cf7b99cSStephen Boyd .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 13551b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_ult_lo_smps_get_voltage, 1356e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1357e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1358e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1359e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1360e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1361e92a4047SStephen Boyd }; 1362e92a4047SStephen Boyd 1363e92a4047SStephen Boyd static struct regulator_ops spmi_ult_ho_smps_ops = { 13649d485332SAxel Lin .enable = regulator_enable_regmap, 13659d485332SAxel Lin .disable = regulator_disable_regmap, 13669d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13671b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_single_range_set_voltage, 13682cf7b99cSStephen Boyd .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 13691b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_single_range_get_voltage, 13701b5b1968SStephen Boyd .map_voltage = spmi_regulator_single_map_voltage, 1371e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1372e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1373e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1374e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1375e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1376e92a4047SStephen Boyd }; 1377e92a4047SStephen Boyd 1378e92a4047SStephen Boyd static struct regulator_ops spmi_ult_ldo_ops = { 13799d485332SAxel Lin .enable = regulator_enable_regmap, 13809d485332SAxel Lin .disable = regulator_disable_regmap, 13819d485332SAxel Lin .is_enabled = regulator_is_enabled_regmap, 13821b5b1968SStephen Boyd .set_voltage_sel = spmi_regulator_single_range_set_voltage, 13831b5b1968SStephen Boyd .get_voltage_sel = spmi_regulator_single_range_get_voltage, 13841b5b1968SStephen Boyd .map_voltage = spmi_regulator_single_map_voltage, 1385e92a4047SStephen Boyd .list_voltage = spmi_regulator_common_list_voltage, 1386e92a4047SStephen Boyd .set_mode = spmi_regulator_common_set_mode, 1387e92a4047SStephen Boyd .get_mode = spmi_regulator_common_get_mode, 1388e92a4047SStephen Boyd .set_load = spmi_regulator_common_set_load, 1389e92a4047SStephen Boyd .set_bypass = spmi_regulator_common_set_bypass, 1390e92a4047SStephen Boyd .get_bypass = spmi_regulator_common_get_bypass, 1391e92a4047SStephen Boyd .set_pull_down = spmi_regulator_common_set_pull_down, 1392e92a4047SStephen Boyd .set_soft_start = spmi_regulator_common_set_soft_start, 1393e92a4047SStephen Boyd }; 1394e92a4047SStephen Boyd 139542ba89c8SJeffrey Hugo static struct regulator_ops spmi_ftsmps426_ops = { 139642ba89c8SJeffrey Hugo .enable = regulator_enable_regmap, 139742ba89c8SJeffrey Hugo .disable = regulator_disable_regmap, 139842ba89c8SJeffrey Hugo .is_enabled = regulator_is_enabled_regmap, 139942ba89c8SJeffrey Hugo .set_voltage_sel = spmi_regulator_ftsmps426_set_voltage, 140042ba89c8SJeffrey Hugo .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 140142ba89c8SJeffrey Hugo .get_voltage_sel = spmi_regulator_ftsmps426_get_voltage, 140242ba89c8SJeffrey Hugo .map_voltage = spmi_regulator_single_map_voltage, 140342ba89c8SJeffrey Hugo .list_voltage = spmi_regulator_common_list_voltage, 140442ba89c8SJeffrey Hugo .set_mode = spmi_regulator_ftsmps426_set_mode, 140542ba89c8SJeffrey Hugo .get_mode = spmi_regulator_ftsmps426_get_mode, 140642ba89c8SJeffrey Hugo .set_load = spmi_regulator_common_set_load, 140742ba89c8SJeffrey Hugo .set_pull_down = spmi_regulator_common_set_pull_down, 140842ba89c8SJeffrey Hugo }; 140942ba89c8SJeffrey Hugo 1410*0211f68eSJorge Ramirez static struct regulator_ops spmi_hfs430_ops = { 1411*0211f68eSJorge Ramirez .enable = regulator_enable_regmap, 1412*0211f68eSJorge Ramirez .disable = regulator_disable_regmap, 1413*0211f68eSJorge Ramirez .is_enabled = regulator_is_enabled_regmap, 1414*0211f68eSJorge Ramirez .set_voltage_sel = spmi_regulator_ftsmps426_set_voltage, 1415*0211f68eSJorge Ramirez .set_voltage_time_sel = spmi_regulator_set_voltage_time_sel, 1416*0211f68eSJorge Ramirez .get_voltage_sel = spmi_regulator_ftsmps426_get_voltage, 1417*0211f68eSJorge Ramirez .map_voltage = spmi_regulator_single_map_voltage, 1418*0211f68eSJorge Ramirez .list_voltage = spmi_regulator_common_list_voltage, 1419*0211f68eSJorge Ramirez .set_mode = spmi_regulator_ftsmps426_set_mode, 1420*0211f68eSJorge Ramirez .get_mode = spmi_regulator_ftsmps426_get_mode, 1421*0211f68eSJorge Ramirez }; 1422*0211f68eSJorge Ramirez 1423e92a4047SStephen Boyd /* Maximum possible digital major revision value */ 1424e92a4047SStephen Boyd #define INF 0xFF 1425e92a4047SStephen Boyd 1426e92a4047SStephen Boyd static const struct spmi_regulator_mapping supported_regulators[] = { 1427e92a4047SStephen Boyd /* type subtype dig_min dig_max ltype ops setpoints hpm_min */ 1428e92a4047SStephen Boyd SPMI_VREG(BUCK, GP_CTL, 0, INF, SMPS, smps, smps, 100000), 1429*0211f68eSJorge Ramirez SPMI_VREG(BUCK, HFS430, 0, INF, HFS430, hfs430, hfs430, 10000), 1430e92a4047SStephen Boyd SPMI_VREG(LDO, N300, 0, INF, LDO, ldo, nldo1, 10000), 1431e92a4047SStephen Boyd SPMI_VREG(LDO, N600, 0, 0, LDO, ldo, nldo2, 10000), 1432e92a4047SStephen Boyd SPMI_VREG(LDO, N1200, 0, 0, LDO, ldo, nldo2, 10000), 1433e92a4047SStephen Boyd SPMI_VREG(LDO, N600, 1, INF, LDO, ldo, nldo3, 10000), 1434e92a4047SStephen Boyd SPMI_VREG(LDO, N1200, 1, INF, LDO, ldo, nldo3, 10000), 1435e92a4047SStephen Boyd SPMI_VREG(LDO, N600_ST, 0, 0, LDO, ldo, nldo2, 10000), 1436e92a4047SStephen Boyd SPMI_VREG(LDO, N1200_ST, 0, 0, LDO, ldo, nldo2, 10000), 1437e92a4047SStephen Boyd SPMI_VREG(LDO, N600_ST, 1, INF, LDO, ldo, nldo3, 10000), 1438e92a4047SStephen Boyd SPMI_VREG(LDO, N1200_ST, 1, INF, LDO, ldo, nldo3, 10000), 1439e92a4047SStephen Boyd SPMI_VREG(LDO, P50, 0, INF, LDO, ldo, pldo, 5000), 1440e92a4047SStephen Boyd SPMI_VREG(LDO, P150, 0, INF, LDO, ldo, pldo, 10000), 1441e92a4047SStephen Boyd SPMI_VREG(LDO, P300, 0, INF, LDO, ldo, pldo, 10000), 1442e92a4047SStephen Boyd SPMI_VREG(LDO, P600, 0, INF, LDO, ldo, pldo, 10000), 1443e92a4047SStephen Boyd SPMI_VREG(LDO, P1200, 0, INF, LDO, ldo, pldo, 10000), 1444e92a4047SStephen Boyd SPMI_VREG(LDO, LN, 0, INF, LN_LDO, ln_ldo, ln_ldo, 0), 1445e92a4047SStephen Boyd SPMI_VREG(LDO, LV_P50, 0, INF, LDO, ldo, pldo, 5000), 1446e92a4047SStephen Boyd SPMI_VREG(LDO, LV_P150, 0, INF, LDO, ldo, pldo, 10000), 1447e92a4047SStephen Boyd SPMI_VREG(LDO, LV_P300, 0, INF, LDO, ldo, pldo, 10000), 1448e92a4047SStephen Boyd SPMI_VREG(LDO, LV_P600, 0, INF, LDO, ldo, pldo, 10000), 1449e92a4047SStephen Boyd SPMI_VREG(LDO, LV_P1200, 0, INF, LDO, ldo, pldo, 10000), 1450e92a4047SStephen Boyd SPMI_VREG_VS(LV100, 0, INF), 1451e92a4047SStephen Boyd SPMI_VREG_VS(LV300, 0, INF), 1452e92a4047SStephen Boyd SPMI_VREG_VS(MV300, 0, INF), 1453e92a4047SStephen Boyd SPMI_VREG_VS(MV500, 0, INF), 1454e92a4047SStephen Boyd SPMI_VREG_VS(HDMI, 0, INF), 1455e92a4047SStephen Boyd SPMI_VREG_VS(OTG, 0, INF), 1456e92a4047SStephen Boyd SPMI_VREG(BOOST, 5V_BOOST, 0, INF, BOOST, boost, boost, 0), 1457e92a4047SStephen Boyd SPMI_VREG(FTS, FTS_CTL, 0, INF, FTSMPS, ftsmps, ftsmps, 100000), 1458e92a4047SStephen Boyd SPMI_VREG(FTS, FTS2p5_CTL, 0, INF, FTSMPS, ftsmps, ftsmps2p5, 100000), 145942ba89c8SJeffrey Hugo SPMI_VREG(FTS, FTS426_CTL, 0, INF, FTSMPS426, ftsmps426, ftsmps426, 100000), 1460e92a4047SStephen Boyd SPMI_VREG(BOOST_BYP, BB_2A, 0, INF, BOOST_BYP, boost, boost_byp, 0), 1461e92a4047SStephen Boyd SPMI_VREG(ULT_BUCK, ULT_HF_CTL1, 0, INF, ULT_LO_SMPS, ult_lo_smps, 1462e92a4047SStephen Boyd ult_lo_smps, 100000), 1463e92a4047SStephen Boyd SPMI_VREG(ULT_BUCK, ULT_HF_CTL2, 0, INF, ULT_LO_SMPS, ult_lo_smps, 1464e92a4047SStephen Boyd ult_lo_smps, 100000), 1465e92a4047SStephen Boyd SPMI_VREG(ULT_BUCK, ULT_HF_CTL3, 0, INF, ULT_LO_SMPS, ult_lo_smps, 1466e92a4047SStephen Boyd ult_lo_smps, 100000), 1467e92a4047SStephen Boyd SPMI_VREG(ULT_BUCK, ULT_HF_CTL4, 0, INF, ULT_HO_SMPS, ult_ho_smps, 1468e92a4047SStephen Boyd ult_ho_smps, 100000), 1469e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, N300_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1470e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, N600_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1471e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, N900_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1472e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, N1200_ST, 0, INF, ULT_LDO, ult_ldo, ult_nldo, 10000), 1473e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, LV_P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1474e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, LV_P300, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1475e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, LV_P450, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1476e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, P600, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1477e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, P150, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 10000), 1478e92a4047SStephen Boyd SPMI_VREG(ULT_LDO, P50, 0, INF, ULT_LDO, ult_ldo, ult_pldo, 5000), 1479e92a4047SStephen Boyd }; 1480e92a4047SStephen Boyd 1481e92a4047SStephen Boyd static void spmi_calculate_num_voltages(struct spmi_voltage_set_points *points) 1482e92a4047SStephen Boyd { 1483e92a4047SStephen Boyd unsigned int n; 1484e92a4047SStephen Boyd struct spmi_voltage_range *range = points->range; 1485e92a4047SStephen Boyd 1486e92a4047SStephen Boyd for (; range < points->range + points->count; range++) { 1487e92a4047SStephen Boyd n = 0; 1488e92a4047SStephen Boyd if (range->set_point_max_uV) { 1489e92a4047SStephen Boyd n = range->set_point_max_uV - range->set_point_min_uV; 1490419d06a1SAxel Lin n = (n / range->step_uV) + 1; 1491e92a4047SStephen Boyd } 1492e92a4047SStephen Boyd range->n_voltages = n; 1493e92a4047SStephen Boyd points->n_voltages += n; 1494e92a4047SStephen Boyd } 1495e92a4047SStephen Boyd } 1496e92a4047SStephen Boyd 1497e92a4047SStephen Boyd static int spmi_regulator_match(struct spmi_regulator *vreg, u16 force_type) 1498e92a4047SStephen Boyd { 1499e92a4047SStephen Boyd const struct spmi_regulator_mapping *mapping; 1500e92a4047SStephen Boyd int ret, i; 1501e92a4047SStephen Boyd u32 dig_major_rev; 1502e92a4047SStephen Boyd u8 version[SPMI_COMMON_REG_SUBTYPE - SPMI_COMMON_REG_DIG_MAJOR_REV + 1]; 1503e92a4047SStephen Boyd u8 type, subtype; 1504e92a4047SStephen Boyd 1505e92a4047SStephen Boyd ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_DIG_MAJOR_REV, version, 1506e92a4047SStephen Boyd ARRAY_SIZE(version)); 1507e92a4047SStephen Boyd if (ret) { 15086ee5c044SStephen Boyd dev_dbg(vreg->dev, "could not read version registers\n"); 1509e92a4047SStephen Boyd return ret; 1510e92a4047SStephen Boyd } 1511e92a4047SStephen Boyd dig_major_rev = version[SPMI_COMMON_REG_DIG_MAJOR_REV 1512e92a4047SStephen Boyd - SPMI_COMMON_REG_DIG_MAJOR_REV]; 15130caecaa8SIlia Lin 1514e92a4047SStephen Boyd if (!force_type) { 1515e92a4047SStephen Boyd type = version[SPMI_COMMON_REG_TYPE - 1516e92a4047SStephen Boyd SPMI_COMMON_REG_DIG_MAJOR_REV]; 1517e92a4047SStephen Boyd subtype = version[SPMI_COMMON_REG_SUBTYPE - 1518e92a4047SStephen Boyd SPMI_COMMON_REG_DIG_MAJOR_REV]; 1519e92a4047SStephen Boyd } else { 1520e92a4047SStephen Boyd type = force_type >> 8; 1521e92a4047SStephen Boyd subtype = force_type; 1522e92a4047SStephen Boyd } 1523e92a4047SStephen Boyd 1524e92a4047SStephen Boyd for (i = 0; i < ARRAY_SIZE(supported_regulators); i++) { 1525e92a4047SStephen Boyd mapping = &supported_regulators[i]; 1526e92a4047SStephen Boyd if (mapping->type == type && mapping->subtype == subtype 1527e92a4047SStephen Boyd && mapping->revision_min <= dig_major_rev 1528e92a4047SStephen Boyd && mapping->revision_max >= dig_major_rev) 1529e92a4047SStephen Boyd goto found; 1530e92a4047SStephen Boyd } 1531e92a4047SStephen Boyd 1532e92a4047SStephen Boyd dev_err(vreg->dev, 1533e92a4047SStephen Boyd "unsupported regulator: name=%s type=0x%02X, subtype=0x%02X, dig major rev=0x%02X\n", 1534e92a4047SStephen Boyd vreg->desc.name, type, subtype, dig_major_rev); 1535e92a4047SStephen Boyd 1536e92a4047SStephen Boyd return -ENODEV; 1537e92a4047SStephen Boyd 1538e92a4047SStephen Boyd found: 1539e92a4047SStephen Boyd vreg->logical_type = mapping->logical_type; 1540e92a4047SStephen Boyd vreg->set_points = mapping->set_points; 1541e92a4047SStephen Boyd vreg->hpm_min_load = mapping->hpm_min_load; 1542e92a4047SStephen Boyd vreg->desc.ops = mapping->ops; 1543e92a4047SStephen Boyd 1544e92a4047SStephen Boyd if (mapping->set_points) { 1545e92a4047SStephen Boyd if (!mapping->set_points->n_voltages) 1546e92a4047SStephen Boyd spmi_calculate_num_voltages(mapping->set_points); 1547e92a4047SStephen Boyd vreg->desc.n_voltages = mapping->set_points->n_voltages; 1548e92a4047SStephen Boyd } 1549e92a4047SStephen Boyd 1550e92a4047SStephen Boyd return 0; 1551e92a4047SStephen Boyd } 1552e92a4047SStephen Boyd 15532cf7b99cSStephen Boyd static int spmi_regulator_init_slew_rate(struct spmi_regulator *vreg) 1554e92a4047SStephen Boyd { 1555e92a4047SStephen Boyd int ret; 1556e92a4047SStephen Boyd u8 reg = 0; 15572cf7b99cSStephen Boyd int step, delay, slew_rate, step_delay; 1558e92a4047SStephen Boyd const struct spmi_voltage_range *range; 1559e92a4047SStephen Boyd 1560e92a4047SStephen Boyd ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_STEP_CTRL, ®, 1); 1561e92a4047SStephen Boyd if (ret) { 1562e92a4047SStephen Boyd dev_err(vreg->dev, "spmi read failed, ret=%d\n", ret); 1563e92a4047SStephen Boyd return ret; 1564e92a4047SStephen Boyd } 1565e92a4047SStephen Boyd 1566e92a4047SStephen Boyd range = spmi_regulator_find_range(vreg); 1567e92a4047SStephen Boyd if (!range) 1568e92a4047SStephen Boyd return -EINVAL; 1569e92a4047SStephen Boyd 15702cf7b99cSStephen Boyd switch (vreg->logical_type) { 15712cf7b99cSStephen Boyd case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS: 15722cf7b99cSStephen Boyd step_delay = SPMI_FTSMPS_STEP_DELAY; 15732cf7b99cSStephen Boyd break; 15742cf7b99cSStephen Boyd default: 15752cf7b99cSStephen Boyd step_delay = SPMI_DEFAULT_STEP_DELAY; 15762cf7b99cSStephen Boyd break; 15772cf7b99cSStephen Boyd } 15782cf7b99cSStephen Boyd 1579e92a4047SStephen Boyd step = reg & SPMI_FTSMPS_STEP_CTRL_STEP_MASK; 1580e92a4047SStephen Boyd step >>= SPMI_FTSMPS_STEP_CTRL_STEP_SHIFT; 1581e92a4047SStephen Boyd 1582e92a4047SStephen Boyd delay = reg & SPMI_FTSMPS_STEP_CTRL_DELAY_MASK; 1583e92a4047SStephen Boyd delay >>= SPMI_FTSMPS_STEP_CTRL_DELAY_SHIFT; 1584e92a4047SStephen Boyd 1585e92a4047SStephen Boyd /* slew_rate has units of uV/us */ 1586e92a4047SStephen Boyd slew_rate = SPMI_FTSMPS_CLOCK_RATE * range->step_uV * (1 << step); 15872cf7b99cSStephen Boyd slew_rate /= 1000 * (step_delay << delay); 1588e92a4047SStephen Boyd slew_rate *= SPMI_FTSMPS_STEP_MARGIN_NUM; 1589e92a4047SStephen Boyd slew_rate /= SPMI_FTSMPS_STEP_MARGIN_DEN; 1590e92a4047SStephen Boyd 1591e92a4047SStephen Boyd /* Ensure that the slew rate is greater than 0 */ 1592e92a4047SStephen Boyd vreg->slew_rate = max(slew_rate, 1); 1593e92a4047SStephen Boyd 1594e92a4047SStephen Boyd return ret; 1595e92a4047SStephen Boyd } 1596e92a4047SStephen Boyd 1597*0211f68eSJorge Ramirez static int spmi_regulator_init_slew_rate_ftsmps426(struct spmi_regulator *vreg, 1598*0211f68eSJorge Ramirez int clock_rate) 159942ba89c8SJeffrey Hugo { 160042ba89c8SJeffrey Hugo int ret; 160142ba89c8SJeffrey Hugo u8 reg = 0; 160242ba89c8SJeffrey Hugo int delay, slew_rate; 160342ba89c8SJeffrey Hugo const struct spmi_voltage_range *range = &vreg->set_points->range[0]; 160442ba89c8SJeffrey Hugo 160542ba89c8SJeffrey Hugo ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_STEP_CTRL, ®, 1); 160642ba89c8SJeffrey Hugo if (ret) { 160742ba89c8SJeffrey Hugo dev_err(vreg->dev, "spmi read failed, ret=%d\n", ret); 160842ba89c8SJeffrey Hugo return ret; 160942ba89c8SJeffrey Hugo } 161042ba89c8SJeffrey Hugo 161142ba89c8SJeffrey Hugo delay = reg & SPMI_FTSMPS426_STEP_CTRL_DELAY_MASK; 161242ba89c8SJeffrey Hugo delay >>= SPMI_FTSMPS426_STEP_CTRL_DELAY_SHIFT; 161342ba89c8SJeffrey Hugo 161442ba89c8SJeffrey Hugo /* slew_rate has units of uV/us */ 1615*0211f68eSJorge Ramirez slew_rate = clock_rate * range->step_uV; 161642ba89c8SJeffrey Hugo slew_rate /= 1000 * (SPMI_FTSMPS426_STEP_DELAY << delay); 161742ba89c8SJeffrey Hugo slew_rate *= SPMI_FTSMPS426_STEP_MARGIN_NUM; 161842ba89c8SJeffrey Hugo slew_rate /= SPMI_FTSMPS426_STEP_MARGIN_DEN; 161942ba89c8SJeffrey Hugo 162042ba89c8SJeffrey Hugo /* Ensure that the slew rate is greater than 0 */ 162142ba89c8SJeffrey Hugo vreg->slew_rate = max(slew_rate, 1); 162242ba89c8SJeffrey Hugo 162342ba89c8SJeffrey Hugo return ret; 162442ba89c8SJeffrey Hugo } 162542ba89c8SJeffrey Hugo 1626e2adfacdSStephen Boyd static int spmi_regulator_init_registers(struct spmi_regulator *vreg, 1627e2adfacdSStephen Boyd const struct spmi_regulator_init_data *data) 1628e2adfacdSStephen Boyd { 1629e2adfacdSStephen Boyd int ret; 1630e2adfacdSStephen Boyd enum spmi_regulator_logical_type type; 1631e2adfacdSStephen Boyd u8 ctrl_reg[8], reg, mask; 1632e2adfacdSStephen Boyd 1633e2adfacdSStephen Boyd type = vreg->logical_type; 1634e2adfacdSStephen Boyd 1635e2adfacdSStephen Boyd ret = spmi_vreg_read(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8); 1636e2adfacdSStephen Boyd if (ret) 1637e2adfacdSStephen Boyd return ret; 1638e2adfacdSStephen Boyd 1639e2adfacdSStephen Boyd /* Set up enable pin control. */ 1640e2adfacdSStephen Boyd if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS 1641e2adfacdSStephen Boyd || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO 1642e2adfacdSStephen Boyd || type == SPMI_REGULATOR_LOGICAL_TYPE_VS) 1643e2adfacdSStephen Boyd && !(data->pin_ctrl_enable 1644e2adfacdSStephen Boyd & SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT)) { 1645e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_ENABLE] &= 1646e2adfacdSStephen Boyd ~SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK; 1647e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_ENABLE] |= 1648e2adfacdSStephen Boyd data->pin_ctrl_enable & SPMI_COMMON_ENABLE_FOLLOW_ALL_MASK; 1649e2adfacdSStephen Boyd } 1650e2adfacdSStephen Boyd 1651e2adfacdSStephen Boyd /* Set up mode pin control. */ 1652e2adfacdSStephen Boyd if ((type == SPMI_REGULATOR_LOGICAL_TYPE_SMPS 1653e2adfacdSStephen Boyd || type == SPMI_REGULATOR_LOGICAL_TYPE_LDO) 1654e2adfacdSStephen Boyd && !(data->pin_ctrl_hpm 1655e2adfacdSStephen Boyd & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) { 1656e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] &= 1657e2adfacdSStephen Boyd ~SPMI_COMMON_MODE_FOLLOW_ALL_MASK; 1658e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] |= 1659e2adfacdSStephen Boyd data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_ALL_MASK; 1660e2adfacdSStephen Boyd } 1661e2adfacdSStephen Boyd 1662e2adfacdSStephen Boyd if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS 1663e2adfacdSStephen Boyd && !(data->pin_ctrl_hpm & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) { 1664e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] &= 1665e2adfacdSStephen Boyd ~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK; 1666e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] |= 1667e2adfacdSStephen Boyd data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK; 1668e2adfacdSStephen Boyd } 1669e2adfacdSStephen Boyd 1670e2adfacdSStephen Boyd if ((type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS 1671e2adfacdSStephen Boyd || type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS 1672e2adfacdSStephen Boyd || type == SPMI_REGULATOR_LOGICAL_TYPE_ULT_LDO) 1673e2adfacdSStephen Boyd && !(data->pin_ctrl_hpm 1674e2adfacdSStephen Boyd & SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT)) { 1675e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] &= 1676e2adfacdSStephen Boyd ~SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK; 1677e2adfacdSStephen Boyd ctrl_reg[SPMI_COMMON_IDX_MODE] |= 1678e2adfacdSStephen Boyd data->pin_ctrl_hpm & SPMI_COMMON_MODE_FOLLOW_AWAKE_MASK; 1679e2adfacdSStephen Boyd } 1680e2adfacdSStephen Boyd 1681e2adfacdSStephen Boyd /* Write back any control register values that were modified. */ 1682e2adfacdSStephen Boyd ret = spmi_vreg_write(vreg, SPMI_COMMON_REG_VOLTAGE_RANGE, ctrl_reg, 8); 1683e2adfacdSStephen Boyd if (ret) 1684e2adfacdSStephen Boyd return ret; 1685e2adfacdSStephen Boyd 1686e2adfacdSStephen Boyd /* Set soft start strength and over current protection for VS. */ 1687e2adfacdSStephen Boyd if (type == SPMI_REGULATOR_LOGICAL_TYPE_VS) { 1688e2adfacdSStephen Boyd if (data->vs_soft_start_strength 1689e2adfacdSStephen Boyd != SPMI_VS_SOFT_START_STR_HW_DEFAULT) { 1690e2adfacdSStephen Boyd reg = data->vs_soft_start_strength 1691e2adfacdSStephen Boyd & SPMI_VS_SOFT_START_SEL_MASK; 1692e2adfacdSStephen Boyd mask = SPMI_VS_SOFT_START_SEL_MASK; 1693e2adfacdSStephen Boyd return spmi_vreg_update_bits(vreg, 1694e2adfacdSStephen Boyd SPMI_VS_REG_SOFT_START, 1695e2adfacdSStephen Boyd reg, mask); 1696e2adfacdSStephen Boyd } 1697e2adfacdSStephen Boyd } 1698e2adfacdSStephen Boyd 1699e2adfacdSStephen Boyd return 0; 1700e2adfacdSStephen Boyd } 1701e2adfacdSStephen Boyd 1702e2adfacdSStephen Boyd static void spmi_regulator_get_dt_config(struct spmi_regulator *vreg, 1703e2adfacdSStephen Boyd struct device_node *node, struct spmi_regulator_init_data *data) 1704e2adfacdSStephen Boyd { 1705e2adfacdSStephen Boyd /* 1706e2adfacdSStephen Boyd * Initialize configuration parameters to use hardware default in case 1707e2adfacdSStephen Boyd * no value is specified via device tree. 1708e2adfacdSStephen Boyd */ 1709e2adfacdSStephen Boyd data->pin_ctrl_enable = SPMI_REGULATOR_PIN_CTRL_ENABLE_HW_DEFAULT; 1710e2adfacdSStephen Boyd data->pin_ctrl_hpm = SPMI_REGULATOR_PIN_CTRL_HPM_HW_DEFAULT; 1711e2adfacdSStephen Boyd data->vs_soft_start_strength = SPMI_VS_SOFT_START_STR_HW_DEFAULT; 1712e2adfacdSStephen Boyd 1713e2adfacdSStephen Boyd /* These bindings are optional, so it is okay if they aren't found. */ 1714e2adfacdSStephen Boyd of_property_read_u32(node, "qcom,ocp-max-retries", 1715e2adfacdSStephen Boyd &vreg->ocp_max_retries); 1716e2adfacdSStephen Boyd of_property_read_u32(node, "qcom,ocp-retry-delay", 1717e2adfacdSStephen Boyd &vreg->ocp_retry_delay_ms); 1718e2adfacdSStephen Boyd of_property_read_u32(node, "qcom,pin-ctrl-enable", 1719e2adfacdSStephen Boyd &data->pin_ctrl_enable); 1720e2adfacdSStephen Boyd of_property_read_u32(node, "qcom,pin-ctrl-hpm", &data->pin_ctrl_hpm); 1721e2adfacdSStephen Boyd of_property_read_u32(node, "qcom,vs-soft-start-strength", 1722e2adfacdSStephen Boyd &data->vs_soft_start_strength); 1723e2adfacdSStephen Boyd } 1724e2adfacdSStephen Boyd 1725e92a4047SStephen Boyd static unsigned int spmi_regulator_of_map_mode(unsigned int mode) 1726e92a4047SStephen Boyd { 1727e2adfacdSStephen Boyd if (mode == 1) 1728e92a4047SStephen Boyd return REGULATOR_MODE_NORMAL; 1729e2adfacdSStephen Boyd if (mode == 2) 1730e2adfacdSStephen Boyd return REGULATOR_MODE_FAST; 1731e92a4047SStephen Boyd 1732e92a4047SStephen Boyd return REGULATOR_MODE_IDLE; 1733e92a4047SStephen Boyd } 1734e92a4047SStephen Boyd 1735e92a4047SStephen Boyd static int spmi_regulator_of_parse(struct device_node *node, 1736e92a4047SStephen Boyd const struct regulator_desc *desc, 1737e92a4047SStephen Boyd struct regulator_config *config) 1738e92a4047SStephen Boyd { 1739e2adfacdSStephen Boyd struct spmi_regulator_init_data data = { }; 1740e92a4047SStephen Boyd struct spmi_regulator *vreg = config->driver_data; 1741e92a4047SStephen Boyd struct device *dev = config->dev; 1742e92a4047SStephen Boyd int ret; 1743e92a4047SStephen Boyd 1744e2adfacdSStephen Boyd spmi_regulator_get_dt_config(vreg, node, &data); 1745e2adfacdSStephen Boyd 1746e2adfacdSStephen Boyd if (!vreg->ocp_max_retries) 1747e92a4047SStephen Boyd vreg->ocp_max_retries = SPMI_VS_OCP_DEFAULT_MAX_RETRIES; 1748e2adfacdSStephen Boyd if (!vreg->ocp_retry_delay_ms) 1749e92a4047SStephen Boyd vreg->ocp_retry_delay_ms = SPMI_VS_OCP_DEFAULT_RETRY_DELAY_MS; 1750e92a4047SStephen Boyd 1751e2adfacdSStephen Boyd ret = spmi_regulator_init_registers(vreg, &data); 1752e2adfacdSStephen Boyd if (ret) { 1753e2adfacdSStephen Boyd dev_err(dev, "common initialization failed, ret=%d\n", ret); 1754e2adfacdSStephen Boyd return ret; 1755e2adfacdSStephen Boyd } 1756e2adfacdSStephen Boyd 17572cf7b99cSStephen Boyd switch (vreg->logical_type) { 17582cf7b99cSStephen Boyd case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS: 17592cf7b99cSStephen Boyd case SPMI_REGULATOR_LOGICAL_TYPE_ULT_LO_SMPS: 17602cf7b99cSStephen Boyd case SPMI_REGULATOR_LOGICAL_TYPE_ULT_HO_SMPS: 17612cf7b99cSStephen Boyd case SPMI_REGULATOR_LOGICAL_TYPE_SMPS: 17622cf7b99cSStephen Boyd ret = spmi_regulator_init_slew_rate(vreg); 1763e92a4047SStephen Boyd if (ret) 1764e92a4047SStephen Boyd return ret; 176542ba89c8SJeffrey Hugo break; 176642ba89c8SJeffrey Hugo case SPMI_REGULATOR_LOGICAL_TYPE_FTSMPS426: 1767*0211f68eSJorge Ramirez ret = spmi_regulator_init_slew_rate_ftsmps426(vreg, 1768*0211f68eSJorge Ramirez SPMI_FTSMPS426_CLOCK_RATE); 1769*0211f68eSJorge Ramirez if (ret) 1770*0211f68eSJorge Ramirez return ret; 1771*0211f68eSJorge Ramirez break; 1772*0211f68eSJorge Ramirez case SPMI_REGULATOR_LOGICAL_TYPE_HFS430: 1773*0211f68eSJorge Ramirez ret = spmi_regulator_init_slew_rate_ftsmps426(vreg, 1774*0211f68eSJorge Ramirez SPMI_HFS430_CLOCK_RATE); 177542ba89c8SJeffrey Hugo if (ret) 177642ba89c8SJeffrey Hugo return ret; 177742ba89c8SJeffrey Hugo break; 17782cf7b99cSStephen Boyd default: 17792cf7b99cSStephen Boyd break; 1780e92a4047SStephen Boyd } 1781e92a4047SStephen Boyd 1782e92a4047SStephen Boyd if (vreg->logical_type != SPMI_REGULATOR_LOGICAL_TYPE_VS) 1783e92a4047SStephen Boyd vreg->ocp_irq = 0; 1784e92a4047SStephen Boyd 1785e92a4047SStephen Boyd if (vreg->ocp_irq) { 1786e92a4047SStephen Boyd ret = devm_request_irq(dev, vreg->ocp_irq, 1787e92a4047SStephen Boyd spmi_regulator_vs_ocp_isr, IRQF_TRIGGER_RISING, "ocp", 1788e92a4047SStephen Boyd vreg); 1789e92a4047SStephen Boyd if (ret < 0) { 1790e92a4047SStephen Boyd dev_err(dev, "failed to request irq %d, ret=%d\n", 1791e92a4047SStephen Boyd vreg->ocp_irq, ret); 1792e92a4047SStephen Boyd return ret; 1793e92a4047SStephen Boyd } 1794e92a4047SStephen Boyd 1795e92a4047SStephen Boyd INIT_DELAYED_WORK(&vreg->ocp_work, spmi_regulator_vs_ocp_work); 1796e92a4047SStephen Boyd } 1797e92a4047SStephen Boyd 1798e92a4047SStephen Boyd return 0; 1799e92a4047SStephen Boyd } 1800e92a4047SStephen Boyd 1801e92a4047SStephen Boyd static const struct spmi_regulator_data pm8941_regulators[] = { 1802e92a4047SStephen Boyd { "s1", 0x1400, "vdd_s1", }, 1803e92a4047SStephen Boyd { "s2", 0x1700, "vdd_s2", }, 1804e92a4047SStephen Boyd { "s3", 0x1a00, "vdd_s3", }, 1805c333dfe8SStephen Boyd { "s4", 0xa000, }, 1806e92a4047SStephen Boyd { "l1", 0x4000, "vdd_l1_l3", }, 1807e92a4047SStephen Boyd { "l2", 0x4100, "vdd_l2_lvs_1_2_3", }, 1808e92a4047SStephen Boyd { "l3", 0x4200, "vdd_l1_l3", }, 1809e92a4047SStephen Boyd { "l4", 0x4300, "vdd_l4_l11", }, 1810e92a4047SStephen Boyd { "l5", 0x4400, "vdd_l5_l7", NULL, 0x0410 }, 1811e92a4047SStephen Boyd { "l6", 0x4500, "vdd_l6_l12_l14_l15", }, 1812e92a4047SStephen Boyd { "l7", 0x4600, "vdd_l5_l7", NULL, 0x0410 }, 1813e92a4047SStephen Boyd { "l8", 0x4700, "vdd_l8_l16_l18_19", }, 1814e92a4047SStephen Boyd { "l9", 0x4800, "vdd_l9_l10_l17_l22", }, 1815e92a4047SStephen Boyd { "l10", 0x4900, "vdd_l9_l10_l17_l22", }, 1816e92a4047SStephen Boyd { "l11", 0x4a00, "vdd_l4_l11", }, 1817e92a4047SStephen Boyd { "l12", 0x4b00, "vdd_l6_l12_l14_l15", }, 1818e92a4047SStephen Boyd { "l13", 0x4c00, "vdd_l13_l20_l23_l24", }, 1819e92a4047SStephen Boyd { "l14", 0x4d00, "vdd_l6_l12_l14_l15", }, 1820e92a4047SStephen Boyd { "l15", 0x4e00, "vdd_l6_l12_l14_l15", }, 1821e92a4047SStephen Boyd { "l16", 0x4f00, "vdd_l8_l16_l18_19", }, 1822e92a4047SStephen Boyd { "l17", 0x5000, "vdd_l9_l10_l17_l22", }, 1823e92a4047SStephen Boyd { "l18", 0x5100, "vdd_l8_l16_l18_19", }, 1824e92a4047SStephen Boyd { "l19", 0x5200, "vdd_l8_l16_l18_19", }, 1825e92a4047SStephen Boyd { "l20", 0x5300, "vdd_l13_l20_l23_l24", }, 1826e92a4047SStephen Boyd { "l21", 0x5400, "vdd_l21", }, 1827e92a4047SStephen Boyd { "l22", 0x5500, "vdd_l9_l10_l17_l22", }, 1828e92a4047SStephen Boyd { "l23", 0x5600, "vdd_l13_l20_l23_l24", }, 1829e92a4047SStephen Boyd { "l24", 0x5700, "vdd_l13_l20_l23_l24", }, 1830e92a4047SStephen Boyd { "lvs1", 0x8000, "vdd_l2_lvs_1_2_3", }, 1831e92a4047SStephen Boyd { "lvs2", 0x8100, "vdd_l2_lvs_1_2_3", }, 1832e92a4047SStephen Boyd { "lvs3", 0x8200, "vdd_l2_lvs_1_2_3", }, 183393bfe79bSStephen Boyd { "5vs1", 0x8300, "vin_5vs", "ocp-5vs1", }, 183493bfe79bSStephen Boyd { "5vs2", 0x8400, "vin_5vs", "ocp-5vs2", }, 1835e92a4047SStephen Boyd { } 1836e92a4047SStephen Boyd }; 1837e92a4047SStephen Boyd 1838e92a4047SStephen Boyd static const struct spmi_regulator_data pm8841_regulators[] = { 1839e92a4047SStephen Boyd { "s1", 0x1400, "vdd_s1", }, 1840e92a4047SStephen Boyd { "s2", 0x1700, "vdd_s2", NULL, 0x1c08 }, 1841e92a4047SStephen Boyd { "s3", 0x1a00, "vdd_s3", }, 1842e92a4047SStephen Boyd { "s4", 0x1d00, "vdd_s4", NULL, 0x1c08 }, 1843e92a4047SStephen Boyd { "s5", 0x2000, "vdd_s5", NULL, 0x1c08 }, 1844e92a4047SStephen Boyd { "s6", 0x2300, "vdd_s6", NULL, 0x1c08 }, 1845e92a4047SStephen Boyd { "s7", 0x2600, "vdd_s7", NULL, 0x1c08 }, 1846e92a4047SStephen Boyd { "s8", 0x2900, "vdd_s8", NULL, 0x1c08 }, 1847e92a4047SStephen Boyd { } 1848e92a4047SStephen Boyd }; 1849e92a4047SStephen Boyd 1850e92a4047SStephen Boyd static const struct spmi_regulator_data pm8916_regulators[] = { 1851e92a4047SStephen Boyd { "s1", 0x1400, "vdd_s1", }, 1852e92a4047SStephen Boyd { "s2", 0x1700, "vdd_s2", }, 1853e92a4047SStephen Boyd { "s3", 0x1a00, "vdd_s3", }, 1854e92a4047SStephen Boyd { "s4", 0x1d00, "vdd_s4", }, 1855e92a4047SStephen Boyd { "l1", 0x4000, "vdd_l1_l3", }, 1856e92a4047SStephen Boyd { "l2", 0x4100, "vdd_l2", }, 1857e92a4047SStephen Boyd { "l3", 0x4200, "vdd_l1_l3", }, 1858e92a4047SStephen Boyd { "l4", 0x4300, "vdd_l4_l5_l6", }, 1859e92a4047SStephen Boyd { "l5", 0x4400, "vdd_l4_l5_l6", }, 1860e92a4047SStephen Boyd { "l6", 0x4500, "vdd_l4_l5_l6", }, 1861e92a4047SStephen Boyd { "l7", 0x4600, "vdd_l7", }, 1862e92a4047SStephen Boyd { "l8", 0x4700, "vdd_l8_l11_l14_l15_l16", }, 1863e92a4047SStephen Boyd { "l9", 0x4800, "vdd_l9_l10_l12_l13_l17_l18", }, 1864e92a4047SStephen Boyd { "l10", 0x4900, "vdd_l9_l10_l12_l13_l17_l18", }, 1865e92a4047SStephen Boyd { "l11", 0x4a00, "vdd_l8_l11_l14_l15_l16", }, 1866e92a4047SStephen Boyd { "l12", 0x4b00, "vdd_l9_l10_l12_l13_l17_l18", }, 1867e92a4047SStephen Boyd { "l13", 0x4c00, "vdd_l9_l10_l12_l13_l17_l18", }, 1868e92a4047SStephen Boyd { "l14", 0x4d00, "vdd_l8_l11_l14_l15_l16", }, 1869e92a4047SStephen Boyd { "l15", 0x4e00, "vdd_l8_l11_l14_l15_l16", }, 1870e92a4047SStephen Boyd { "l16", 0x4f00, "vdd_l8_l11_l14_l15_l16", }, 1871e92a4047SStephen Boyd { "l17", 0x5000, "vdd_l9_l10_l12_l13_l17_l18", }, 1872e92a4047SStephen Boyd { "l18", 0x5100, "vdd_l9_l10_l12_l13_l17_l18", }, 1873e92a4047SStephen Boyd { } 1874e92a4047SStephen Boyd }; 1875e92a4047SStephen Boyd 187650314e55SStephen Boyd static const struct spmi_regulator_data pm8994_regulators[] = { 187750314e55SStephen Boyd { "s1", 0x1400, "vdd_s1", }, 187850314e55SStephen Boyd { "s2", 0x1700, "vdd_s2", }, 187950314e55SStephen Boyd { "s3", 0x1a00, "vdd_s3", }, 188050314e55SStephen Boyd { "s4", 0x1d00, "vdd_s4", }, 188150314e55SStephen Boyd { "s5", 0x2000, "vdd_s5", }, 188250314e55SStephen Boyd { "s6", 0x2300, "vdd_s6", }, 188350314e55SStephen Boyd { "s7", 0x2600, "vdd_s7", }, 188450314e55SStephen Boyd { "s8", 0x2900, "vdd_s8", }, 188550314e55SStephen Boyd { "s9", 0x2c00, "vdd_s9", }, 188650314e55SStephen Boyd { "s10", 0x2f00, "vdd_s10", }, 188750314e55SStephen Boyd { "s11", 0x3200, "vdd_s11", }, 188850314e55SStephen Boyd { "s12", 0x3500, "vdd_s12", }, 188950314e55SStephen Boyd { "l1", 0x4000, "vdd_l1", }, 189050314e55SStephen Boyd { "l2", 0x4100, "vdd_l2_l26_l28", }, 189150314e55SStephen Boyd { "l3", 0x4200, "vdd_l3_l11", }, 189250314e55SStephen Boyd { "l4", 0x4300, "vdd_l4_l27_l31", }, 189350314e55SStephen Boyd { "l5", 0x4400, "vdd_l5_l7", }, 189450314e55SStephen Boyd { "l6", 0x4500, "vdd_l6_l12_l32", }, 189550314e55SStephen Boyd { "l7", 0x4600, "vdd_l5_l7", }, 189650314e55SStephen Boyd { "l8", 0x4700, "vdd_l8_l16_l30", }, 189750314e55SStephen Boyd { "l9", 0x4800, "vdd_l9_l10_l18_l22", }, 189850314e55SStephen Boyd { "l10", 0x4900, "vdd_l9_l10_l18_l22", }, 189950314e55SStephen Boyd { "l11", 0x4a00, "vdd_l3_l11", }, 190050314e55SStephen Boyd { "l12", 0x4b00, "vdd_l6_l12_l32", }, 190150314e55SStephen Boyd { "l13", 0x4c00, "vdd_l13_l19_l23_l24", }, 190250314e55SStephen Boyd { "l14", 0x4d00, "vdd_l14_l15", }, 190350314e55SStephen Boyd { "l15", 0x4e00, "vdd_l14_l15", }, 190450314e55SStephen Boyd { "l16", 0x4f00, "vdd_l8_l16_l30", }, 190550314e55SStephen Boyd { "l17", 0x5000, "vdd_l17_l29", }, 190650314e55SStephen Boyd { "l18", 0x5100, "vdd_l9_l10_l18_l22", }, 190750314e55SStephen Boyd { "l19", 0x5200, "vdd_l13_l19_l23_l24", }, 190850314e55SStephen Boyd { "l20", 0x5300, "vdd_l20_l21", }, 190950314e55SStephen Boyd { "l21", 0x5400, "vdd_l20_l21", }, 191050314e55SStephen Boyd { "l22", 0x5500, "vdd_l9_l10_l18_l22", }, 191150314e55SStephen Boyd { "l23", 0x5600, "vdd_l13_l19_l23_l24", }, 191250314e55SStephen Boyd { "l24", 0x5700, "vdd_l13_l19_l23_l24", }, 191350314e55SStephen Boyd { "l25", 0x5800, "vdd_l25", }, 191450314e55SStephen Boyd { "l26", 0x5900, "vdd_l2_l26_l28", }, 191550314e55SStephen Boyd { "l27", 0x5a00, "vdd_l4_l27_l31", }, 191650314e55SStephen Boyd { "l28", 0x5b00, "vdd_l2_l26_l28", }, 191750314e55SStephen Boyd { "l29", 0x5c00, "vdd_l17_l29", }, 191850314e55SStephen Boyd { "l30", 0x5d00, "vdd_l8_l16_l30", }, 191950314e55SStephen Boyd { "l31", 0x5e00, "vdd_l4_l27_l31", }, 192050314e55SStephen Boyd { "l32", 0x5f00, "vdd_l6_l12_l32", }, 192150314e55SStephen Boyd { "lvs1", 0x8000, "vdd_lvs_1_2", }, 192250314e55SStephen Boyd { "lvs2", 0x8100, "vdd_lvs_1_2", }, 192350314e55SStephen Boyd { } 192450314e55SStephen Boyd }; 192550314e55SStephen Boyd 1926ca5cd8c9SRajendra Nayak static const struct spmi_regulator_data pmi8994_regulators[] = { 1927ca5cd8c9SRajendra Nayak { "s1", 0x1400, "vdd_s1", }, 1928ca5cd8c9SRajendra Nayak { "s2", 0x1700, "vdd_s2", }, 1929ca5cd8c9SRajendra Nayak { "s3", 0x1a00, "vdd_s3", }, 1930ca5cd8c9SRajendra Nayak { "l1", 0x4000, "vdd_l1", }, 1931ca5cd8c9SRajendra Nayak { } 1932ca5cd8c9SRajendra Nayak }; 1933ca5cd8c9SRajendra Nayak 193442ba89c8SJeffrey Hugo static const struct spmi_regulator_data pm8005_regulators[] = { 193542ba89c8SJeffrey Hugo { "s1", 0x1400, "vdd_s1", }, 193642ba89c8SJeffrey Hugo { "s2", 0x1700, "vdd_s2", }, 193742ba89c8SJeffrey Hugo { "s3", 0x1a00, "vdd_s3", }, 193842ba89c8SJeffrey Hugo { "s4", 0x1d00, "vdd_s4", }, 193942ba89c8SJeffrey Hugo { } 194042ba89c8SJeffrey Hugo }; 194142ba89c8SJeffrey Hugo 1942*0211f68eSJorge Ramirez static const struct spmi_regulator_data pms405_regulators[] = { 1943*0211f68eSJorge Ramirez { "s3", 0x1a00, "vdd_s3"}, 1944*0211f68eSJorge Ramirez { } 1945*0211f68eSJorge Ramirez }; 1946*0211f68eSJorge Ramirez 1947e92a4047SStephen Boyd static const struct of_device_id qcom_spmi_regulator_match[] = { 194842ba89c8SJeffrey Hugo { .compatible = "qcom,pm8005-regulators", .data = &pm8005_regulators }, 1949e92a4047SStephen Boyd { .compatible = "qcom,pm8841-regulators", .data = &pm8841_regulators }, 1950e92a4047SStephen Boyd { .compatible = "qcom,pm8916-regulators", .data = &pm8916_regulators }, 1951e92a4047SStephen Boyd { .compatible = "qcom,pm8941-regulators", .data = &pm8941_regulators }, 195250314e55SStephen Boyd { .compatible = "qcom,pm8994-regulators", .data = &pm8994_regulators }, 1953ca5cd8c9SRajendra Nayak { .compatible = "qcom,pmi8994-regulators", .data = &pmi8994_regulators }, 1954*0211f68eSJorge Ramirez { .compatible = "qcom,pms405-regulators", .data = &pms405_regulators }, 1955e92a4047SStephen Boyd { } 1956e92a4047SStephen Boyd }; 1957e92a4047SStephen Boyd MODULE_DEVICE_TABLE(of, qcom_spmi_regulator_match); 1958e92a4047SStephen Boyd 1959e92a4047SStephen Boyd static int qcom_spmi_regulator_probe(struct platform_device *pdev) 1960e92a4047SStephen Boyd { 1961e92a4047SStephen Boyd const struct spmi_regulator_data *reg; 196286f4ff7aSJorge Ramirez-Ortiz const struct spmi_voltage_range *range; 1963e92a4047SStephen Boyd const struct of_device_id *match; 1964e92a4047SStephen Boyd struct regulator_config config = { }; 1965e92a4047SStephen Boyd struct regulator_dev *rdev; 1966e92a4047SStephen Boyd struct spmi_regulator *vreg; 1967e92a4047SStephen Boyd struct regmap *regmap; 1968e92a4047SStephen Boyd const char *name; 1969e92a4047SStephen Boyd struct device *dev = &pdev->dev; 19700caecaa8SIlia Lin struct device_node *node = pdev->dev.of_node; 1971fffe7f52SNiklas Cassel struct device_node *syscon, *reg_node; 1972fffe7f52SNiklas Cassel struct property *reg_prop; 19730caecaa8SIlia Lin int ret, lenp; 1974e92a4047SStephen Boyd struct list_head *vreg_list; 1975e92a4047SStephen Boyd 1976e92a4047SStephen Boyd vreg_list = devm_kzalloc(dev, sizeof(*vreg_list), GFP_KERNEL); 1977e92a4047SStephen Boyd if (!vreg_list) 1978e92a4047SStephen Boyd return -ENOMEM; 1979e92a4047SStephen Boyd INIT_LIST_HEAD(vreg_list); 1980e92a4047SStephen Boyd platform_set_drvdata(pdev, vreg_list); 1981e92a4047SStephen Boyd 1982e92a4047SStephen Boyd regmap = dev_get_regmap(dev->parent, NULL); 1983e92a4047SStephen Boyd if (!regmap) 1984e92a4047SStephen Boyd return -ENODEV; 1985e92a4047SStephen Boyd 1986e92a4047SStephen Boyd match = of_match_device(qcom_spmi_regulator_match, &pdev->dev); 1987e92a4047SStephen Boyd if (!match) 1988e92a4047SStephen Boyd return -ENODEV; 1989e92a4047SStephen Boyd 19900caecaa8SIlia Lin if (of_find_property(node, "qcom,saw-reg", &lenp)) { 19910caecaa8SIlia Lin syscon = of_parse_phandle(node, "qcom,saw-reg", 0); 19920caecaa8SIlia Lin saw_regmap = syscon_node_to_regmap(syscon); 19930caecaa8SIlia Lin of_node_put(syscon); 199485046a15SNiklas Cassel if (IS_ERR(saw_regmap)) 19950caecaa8SIlia Lin dev_err(dev, "ERROR reading SAW regmap\n"); 19960caecaa8SIlia Lin } 19970caecaa8SIlia Lin 1998e92a4047SStephen Boyd for (reg = match->data; reg->name; reg++) { 19990caecaa8SIlia Lin 2000fffe7f52SNiklas Cassel if (saw_regmap) { 2001fffe7f52SNiklas Cassel reg_node = of_get_child_by_name(node, reg->name); 2002fffe7f52SNiklas Cassel reg_prop = of_find_property(reg_node, "qcom,saw-slave", 2003fffe7f52SNiklas Cassel &lenp); 2004fffe7f52SNiklas Cassel of_node_put(reg_node); 2005fffe7f52SNiklas Cassel if (reg_prop) 20060caecaa8SIlia Lin continue; 20070caecaa8SIlia Lin } 20080caecaa8SIlia Lin 2009e92a4047SStephen Boyd vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL); 2010e92a4047SStephen Boyd if (!vreg) 2011e92a4047SStephen Boyd return -ENOMEM; 2012e92a4047SStephen Boyd 2013e92a4047SStephen Boyd vreg->dev = dev; 2014e92a4047SStephen Boyd vreg->base = reg->base; 2015e92a4047SStephen Boyd vreg->regmap = regmap; 2016e92a4047SStephen Boyd if (reg->ocp) { 2017e92a4047SStephen Boyd vreg->ocp_irq = platform_get_irq_byname(pdev, reg->ocp); 2018e92a4047SStephen Boyd if (vreg->ocp_irq < 0) { 2019e92a4047SStephen Boyd ret = vreg->ocp_irq; 2020e92a4047SStephen Boyd goto err; 2021e92a4047SStephen Boyd } 2022e92a4047SStephen Boyd } 2023e92a4047SStephen Boyd vreg->desc.id = -1; 2024e92a4047SStephen Boyd vreg->desc.owner = THIS_MODULE; 2025e92a4047SStephen Boyd vreg->desc.type = REGULATOR_VOLTAGE; 20269d485332SAxel Lin vreg->desc.enable_reg = reg->base + SPMI_COMMON_REG_ENABLE; 20279d485332SAxel Lin vreg->desc.enable_mask = SPMI_COMMON_ENABLE_MASK; 20289d485332SAxel Lin vreg->desc.enable_val = SPMI_COMMON_ENABLE; 2029e92a4047SStephen Boyd vreg->desc.name = name = reg->name; 2030e92a4047SStephen Boyd vreg->desc.supply_name = reg->supply; 2031e92a4047SStephen Boyd vreg->desc.of_match = reg->name; 2032e92a4047SStephen Boyd vreg->desc.of_parse_cb = spmi_regulator_of_parse; 2033e92a4047SStephen Boyd vreg->desc.of_map_mode = spmi_regulator_of_map_mode; 2034e92a4047SStephen Boyd 2035e92a4047SStephen Boyd ret = spmi_regulator_match(vreg, reg->force_type); 2036e92a4047SStephen Boyd if (ret) 20376ee5c044SStephen Boyd continue; 2038e92a4047SStephen Boyd 2039fffe7f52SNiklas Cassel if (saw_regmap) { 2040fffe7f52SNiklas Cassel reg_node = of_get_child_by_name(node, reg->name); 2041fffe7f52SNiklas Cassel reg_prop = of_find_property(reg_node, "qcom,saw-leader", 2042fffe7f52SNiklas Cassel &lenp); 2043fffe7f52SNiklas Cassel of_node_put(reg_node); 2044fffe7f52SNiklas Cassel if (reg_prop) { 20450caecaa8SIlia Lin spmi_saw_ops = *(vreg->desc.ops); 2046fffe7f52SNiklas Cassel spmi_saw_ops.set_voltage_sel = 20470caecaa8SIlia Lin spmi_regulator_saw_set_voltage; 20480caecaa8SIlia Lin vreg->desc.ops = &spmi_saw_ops; 20490caecaa8SIlia Lin } 2050fffe7f52SNiklas Cassel } 20510caecaa8SIlia Lin 205286f4ff7aSJorge Ramirez-Ortiz if (vreg->set_points->count == 1) { 205386f4ff7aSJorge Ramirez-Ortiz /* since there is only one range */ 205486f4ff7aSJorge Ramirez-Ortiz range = vreg->set_points->range; 205586f4ff7aSJorge Ramirez-Ortiz vreg->desc.uV_step = range->step_uV; 205686f4ff7aSJorge Ramirez-Ortiz } 205786f4ff7aSJorge Ramirez-Ortiz 2058e92a4047SStephen Boyd config.dev = dev; 2059e92a4047SStephen Boyd config.driver_data = vreg; 20609d485332SAxel Lin config.regmap = regmap; 2061e92a4047SStephen Boyd rdev = devm_regulator_register(dev, &vreg->desc, &config); 2062e92a4047SStephen Boyd if (IS_ERR(rdev)) { 2063e92a4047SStephen Boyd dev_err(dev, "failed to register %s\n", name); 2064e92a4047SStephen Boyd ret = PTR_ERR(rdev); 2065e92a4047SStephen Boyd goto err; 2066e92a4047SStephen Boyd } 2067e92a4047SStephen Boyd 2068e92a4047SStephen Boyd INIT_LIST_HEAD(&vreg->node); 2069e92a4047SStephen Boyd list_add(&vreg->node, vreg_list); 2070e92a4047SStephen Boyd } 2071e92a4047SStephen Boyd 2072e92a4047SStephen Boyd return 0; 2073e92a4047SStephen Boyd 2074e92a4047SStephen Boyd err: 2075e92a4047SStephen Boyd list_for_each_entry(vreg, vreg_list, node) 2076e92a4047SStephen Boyd if (vreg->ocp_irq) 2077e92a4047SStephen Boyd cancel_delayed_work_sync(&vreg->ocp_work); 2078e92a4047SStephen Boyd return ret; 2079e92a4047SStephen Boyd } 2080e92a4047SStephen Boyd 2081e92a4047SStephen Boyd static int qcom_spmi_regulator_remove(struct platform_device *pdev) 2082e92a4047SStephen Boyd { 2083e92a4047SStephen Boyd struct spmi_regulator *vreg; 2084e92a4047SStephen Boyd struct list_head *vreg_list = platform_get_drvdata(pdev); 2085e92a4047SStephen Boyd 2086e92a4047SStephen Boyd list_for_each_entry(vreg, vreg_list, node) 2087e92a4047SStephen Boyd if (vreg->ocp_irq) 2088e92a4047SStephen Boyd cancel_delayed_work_sync(&vreg->ocp_work); 2089e92a4047SStephen Boyd 2090e92a4047SStephen Boyd return 0; 2091e92a4047SStephen Boyd } 2092e92a4047SStephen Boyd 2093e92a4047SStephen Boyd static struct platform_driver qcom_spmi_regulator_driver = { 2094e92a4047SStephen Boyd .driver = { 2095e92a4047SStephen Boyd .name = "qcom-spmi-regulator", 2096e92a4047SStephen Boyd .of_match_table = qcom_spmi_regulator_match, 2097e92a4047SStephen Boyd }, 2098e92a4047SStephen Boyd .probe = qcom_spmi_regulator_probe, 2099e92a4047SStephen Boyd .remove = qcom_spmi_regulator_remove, 2100e92a4047SStephen Boyd }; 2101e92a4047SStephen Boyd module_platform_driver(qcom_spmi_regulator_driver); 2102e92a4047SStephen Boyd 2103e92a4047SStephen Boyd MODULE_DESCRIPTION("Qualcomm SPMI PMIC regulator driver"); 2104e92a4047SStephen Boyd MODULE_LICENSE("GPL v2"); 2105e92a4047SStephen Boyd MODULE_ALIAS("platform:qcom-spmi-regulator"); 2106