Maheshkumar Sivasubramanian | 4ac2376 | 2011-11-02 10:03:06 -0600 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/module.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/init.h> |
| 18 | #include <linux/io.h> |
| 19 | #include <linux/slab.h> |
| 20 | #include <mach/msm_iomap.h> |
| 21 | |
| 22 | #include "spm_driver.h" |
| 23 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 24 | #define MSM_SPM_PMIC_STATE_IDLE 0 |
| 25 | |
| 26 | #define SAW2_V1_VER_REG 0x04 |
| 27 | #define SAW2_V2_VER_REG 0xfd0 |
| 28 | |
| 29 | #define SAW2_MAJOR_2 2 |
| 30 | |
| 31 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 32 | enum { |
| 33 | MSM_SPM_DEBUG_SHADOW = 1U << 0, |
| 34 | MSM_SPM_DEBUG_VCTL = 1U << 1, |
| 35 | }; |
| 36 | |
| 37 | static int msm_spm_debug_mask; |
| 38 | module_param_named( |
| 39 | debug_mask, msm_spm_debug_mask, int, S_IRUGO | S_IWUSR | S_IWGRP |
| 40 | ); |
| 41 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 42 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 43 | static uint32_t msm_spm_reg_offsets_v1[MSM_SPM_REG_NR] = { |
| 44 | [MSM_SPM_REG_SAW2_SECURE] = 0x00, |
| 45 | [MSM_SPM_REG_SAW2_ID] = 0x04, |
| 46 | [MSM_SPM_REG_SAW2_CFG] = 0x08, |
| 47 | [MSM_SPM_REG_SAW2_STS0] = 0x0C, |
| 48 | [MSM_SPM_REG_SAW2_STS1] = 0x10, |
| 49 | [MSM_SPM_REG_SAW2_VCTL] = 0x14, |
| 50 | [MSM_SPM_REG_SAW2_AVS_CTL] = 0x18, |
| 51 | [MSM_SPM_REG_SAW2_AVS_HYSTERESIS] = 0x1C, |
| 52 | [MSM_SPM_REG_SAW2_SPM_CTL] = 0x20, |
| 53 | [MSM_SPM_REG_SAW2_PMIC_DLY] = 0x24, |
| 54 | [MSM_SPM_REG_SAW2_PMIC_DATA_0] = 0x28, |
| 55 | [MSM_SPM_REG_SAW2_PMIC_DATA_1] = 0x2C, |
| 56 | [MSM_SPM_REG_SAW2_RST] = 0x30, |
| 57 | [MSM_SPM_REG_SAW2_SEQ_ENTRY] = 0x80, |
| 58 | }; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 59 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 60 | static uint32_t msm_spm_reg_offsets_v2[MSM_SPM_REG_NR] = { |
| 61 | [MSM_SPM_REG_SAW2_SECURE] = 0x00, |
| 62 | [MSM_SPM_REG_SAW2_ID] = 0x04, |
| 63 | [MSM_SPM_REG_SAW2_CFG] = 0x08, |
| 64 | [MSM_SPM_REG_SAW2_SPM_STS] = 0x0C, |
| 65 | [MSM_SPM_REG_SAW2_AVS_STS] = 0x10, |
| 66 | [MSM_SPM_REG_SAW2_PMIC_STS] = 0x14, |
| 67 | [MSM_SPM_REG_SAW2_RST] = 0x18, |
| 68 | [MSM_SPM_REG_SAW2_VCTL] = 0x1C, |
| 69 | [MSM_SPM_REG_SAW2_AVS_CTL] = 0x20, |
| 70 | [MSM_SPM_REG_SAW2_AVS_LIMIT] = 0x24, |
| 71 | [MSM_SPM_REG_SAW2_AVS_DLY] = 0x28, |
| 72 | [MSM_SPM_REG_SAW2_AVS_HYSTERESIS] = 0x2C, |
| 73 | [MSM_SPM_REG_SAW2_SPM_CTL] = 0x30, |
| 74 | [MSM_SPM_REG_SAW2_SPM_DLY] = 0x34, |
| 75 | [MSM_SPM_REG_SAW2_PMIC_DATA_0] = 0x40, |
| 76 | [MSM_SPM_REG_SAW2_PMIC_DATA_1] = 0x44, |
| 77 | [MSM_SPM_REG_SAW2_PMIC_DATA_2] = 0x48, |
| 78 | [MSM_SPM_REG_SAW2_PMIC_DATA_3] = 0x4C, |
| 79 | [MSM_SPM_REG_SAW2_PMIC_DATA_4] = 0x50, |
| 80 | [MSM_SPM_REG_SAW2_PMIC_DATA_5] = 0x54, |
| 81 | [MSM_SPM_REG_SAW2_PMIC_DATA_6] = 0x58, |
| 82 | [MSM_SPM_REG_SAW2_PMIC_DATA_7] = 0x5C, |
| 83 | [MSM_SPM_REG_SAW2_SEQ_ENTRY] = 0x80, |
| 84 | [MSM_SPM_REG_SAW2_VERSION] = 0xFD0, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /****************************************************************************** |
| 88 | * Internal helper functions |
| 89 | *****************************************************************************/ |
| 90 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 91 | static inline uint32_t msm_spm_drv_get_num_spm_entry( |
| 92 | struct msm_spm_driver_data *dev) |
| 93 | { |
| 94 | return 32; |
| 95 | } |
| 96 | |
| 97 | static void msm_spm_drv_flush_shadow(struct msm_spm_driver_data *dev, |
| 98 | unsigned int reg_index) |
| 99 | { |
| 100 | __raw_writel(dev->reg_shadow[reg_index], |
| 101 | dev->reg_base_addr + dev->reg_offsets[reg_index]); |
| 102 | } |
| 103 | |
| 104 | static void msm_spm_drv_load_shadow(struct msm_spm_driver_data *dev, |
| 105 | unsigned int reg_index) |
| 106 | { |
| 107 | dev->reg_shadow[reg_index] = |
| 108 | __raw_readl(dev->reg_base_addr + |
| 109 | dev->reg_offsets[reg_index]); |
| 110 | } |
| 111 | |
| 112 | static inline void msm_spm_drv_set_start_addr( |
| 113 | struct msm_spm_driver_data *dev, uint32_t addr) |
| 114 | { |
| 115 | addr &= 0x7F; |
| 116 | addr <<= 4; |
| 117 | dev->reg_shadow[MSM_SPM_REG_SAW2_SPM_CTL] &= 0xFFFFF80F; |
| 118 | dev->reg_shadow[MSM_SPM_REG_SAW2_SPM_CTL] |= addr; |
| 119 | } |
| 120 | |
| 121 | static inline bool msm_spm_pmic_arb_present(struct msm_spm_driver_data *dev) |
| 122 | { |
| 123 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_ID); |
| 124 | |
| 125 | if (dev->major == SAW2_MAJOR_2) |
| 126 | return (dev->reg_shadow[MSM_SPM_REG_SAW2_ID] >> 2) & 0x1; |
| 127 | else |
| 128 | return (dev->reg_shadow[MSM_SPM_REG_SAW2_ID] >> 18) & 0x1; |
| 129 | } |
| 130 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 131 | static inline void msm_spm_drv_set_vctl(struct msm_spm_driver_data *dev, |
| 132 | uint32_t vlevel) |
| 133 | { |
| 134 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] &= ~0xFF; |
| 135 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] |= vlevel; |
| 136 | |
| 137 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_0] &= ~0xFF; |
| 138 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_0] |= vlevel; |
Praveen Chidambaram | f3f2b3e | 2012-03-21 20:13:38 -0600 | [diff] [blame] | 139 | |
| 140 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_1] &= ~0x3F; |
| 141 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_1] |= (vlevel & 0x3F); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 144 | static inline void msm_spm_drv_set_vctl2(struct msm_spm_driver_data *dev, |
| 145 | uint32_t vlevel) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 146 | { |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 147 | unsigned int pmic_data = 0; |
| 148 | |
| 149 | pmic_data |= vlevel; |
| 150 | pmic_data |= (dev->vctl_port & 0x7) << 16; |
| 151 | |
| 152 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] &= ~0x700FF; |
| 153 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] |= pmic_data; |
| 154 | |
| 155 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_0] &= ~0x700FF; |
| 156 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_0] |= pmic_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 159 | static inline void msm_spm_drv_apcs_set_vctl(struct msm_spm_driver_data *dev, |
| 160 | unsigned int vlevel) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 161 | { |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 162 | if (dev->major == SAW2_MAJOR_2) |
| 163 | return msm_spm_drv_set_vctl2(dev, vlevel); |
| 164 | else |
| 165 | return msm_spm_drv_set_vctl(dev, vlevel); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | static inline uint32_t msm_spm_drv_get_sts_pmic_state( |
| 169 | struct msm_spm_driver_data *dev) |
| 170 | { |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 171 | if (dev->major == SAW2_MAJOR_2) { |
| 172 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_PMIC_STS); |
| 173 | return (dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_STS] >> 16) & |
| 174 | 0x03; |
| 175 | } else { |
| 176 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_STS0); |
| 177 | return (dev->reg_shadow[MSM_SPM_REG_SAW2_STS0] >> 10) & 0x03; |
| 178 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static inline uint32_t msm_spm_drv_get_sts_curr_pmic_data( |
| 182 | struct msm_spm_driver_data *dev) |
| 183 | { |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 184 | if (dev->major == SAW2_MAJOR_2) { |
| 185 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_PMIC_STS); |
| 186 | return dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_STS] & 0xFF; |
| 187 | } else { |
| 188 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_STS1); |
| 189 | return dev->reg_shadow[MSM_SPM_REG_SAW2_STS1] & 0xFF; |
| 190 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 193 | static inline uint32_t msm_spm_drv_get_saw2_ver(struct msm_spm_driver_data *dev, |
| 194 | uint32_t *major, uint32_t *minor) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 195 | { |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 196 | int ret = -ENODEV; |
| 197 | uint32_t val = 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 198 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 199 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_VERSION); |
| 200 | val = dev->reg_shadow[MSM_SPM_REG_SAW2_VERSION]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 201 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 202 | if (dev->ver_reg == SAW2_V2_VER_REG) { |
| 203 | *major = (val >> 28) & 0xF; |
| 204 | *minor = (val >> 16) & 0xFFF; |
| 205 | ret = 0; |
| 206 | } else if (dev->ver_reg == SAW2_V1_VER_REG) { |
| 207 | *major = (val >> 4) & 0xF; |
| 208 | *minor = val & 0xF; |
| 209 | ret = 0; |
| 210 | } |
| 211 | |
| 212 | return ret; |
| 213 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 214 | |
| 215 | /****************************************************************************** |
| 216 | * Public functions |
| 217 | *****************************************************************************/ |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 218 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 219 | inline int msm_spm_drv_set_spm_enable( |
| 220 | struct msm_spm_driver_data *dev, bool enable) |
| 221 | { |
| 222 | uint32_t value = enable ? 0x01 : 0x00; |
| 223 | |
| 224 | if (!dev) |
| 225 | return -EINVAL; |
| 226 | |
| 227 | if ((dev->reg_shadow[MSM_SPM_REG_SAW2_SPM_CTL] & 0x01) ^ value) { |
| 228 | |
| 229 | dev->reg_shadow[MSM_SPM_REG_SAW2_SPM_CTL] &= ~0x1; |
| 230 | dev->reg_shadow[MSM_SPM_REG_SAW2_SPM_CTL] |= value; |
| 231 | |
| 232 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_SPM_CTL); |
| 233 | wmb(); |
| 234 | } |
| 235 | return 0; |
| 236 | } |
| 237 | void msm_spm_drv_flush_seq_entry(struct msm_spm_driver_data *dev) |
| 238 | { |
| 239 | int i; |
| 240 | int num_spm_entry = msm_spm_drv_get_num_spm_entry(dev); |
| 241 | |
| 242 | if (!dev) { |
| 243 | __WARN(); |
| 244 | return; |
| 245 | } |
| 246 | |
| 247 | for (i = 0; i < num_spm_entry; i++) { |
| 248 | __raw_writel(dev->reg_seq_entry_shadow[i], |
| 249 | dev->reg_base_addr |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 250 | + dev->reg_offsets[MSM_SPM_REG_SAW2_SEQ_ENTRY] |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 251 | + 4 * i); |
| 252 | } |
| 253 | mb(); |
| 254 | } |
| 255 | |
| 256 | int msm_spm_drv_write_seq_data(struct msm_spm_driver_data *dev, |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 257 | uint8_t *cmd, uint32_t *offset) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 258 | { |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 259 | uint32_t cmd_w; |
| 260 | uint32_t offset_w = *offset / 4; |
| 261 | uint8_t last_cmd; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 262 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 263 | if (!cmd) |
| 264 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 265 | |
| 266 | while (1) { |
| 267 | int i; |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 268 | cmd_w = 0; |
| 269 | last_cmd = 0; |
| 270 | cmd_w = dev->reg_seq_entry_shadow[offset_w]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 271 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 272 | for (i = (*offset % 4) ; i < 4; i++) { |
| 273 | last_cmd = *(cmd++); |
| 274 | cmd_w |= last_cmd << (i * 8); |
| 275 | (*offset)++; |
| 276 | if (last_cmd == 0x0f) |
| 277 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 280 | dev->reg_seq_entry_shadow[offset_w++] = cmd_w; |
| 281 | if (last_cmd == 0x0f) |
| 282 | break; |
| 283 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 284 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 285 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | int msm_spm_drv_set_low_power_mode(struct msm_spm_driver_data *dev, |
| 289 | uint32_t addr) |
| 290 | { |
| 291 | |
| 292 | /* SPM is configured to reset start address to zero after end of Program |
| 293 | */ |
| 294 | if (!dev) |
| 295 | return -EINVAL; |
| 296 | |
| 297 | msm_spm_drv_set_start_addr(dev, addr); |
| 298 | |
Maheshkumar Sivasubramanian | daa23bb | 2011-07-01 17:24:51 -0600 | [diff] [blame] | 299 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_SPM_CTL); |
| 300 | wmb(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 301 | |
| 302 | if (msm_spm_debug_mask & MSM_SPM_DEBUG_SHADOW) { |
| 303 | int i; |
| 304 | for (i = 0; i < MSM_SPM_REG_NR; i++) |
| 305 | pr_info("%s: reg %02x = 0x%08x\n", __func__, |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 306 | dev->reg_offsets[i], dev->reg_shadow[i]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | int msm_spm_drv_set_vdd(struct msm_spm_driver_data *dev, unsigned int vlevel) |
| 313 | { |
| 314 | uint32_t timeout_us; |
| 315 | |
| 316 | if (!dev) |
| 317 | return -EINVAL; |
| 318 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 319 | if (!msm_spm_pmic_arb_present(dev)) |
| 320 | return -ENOSYS; |
| 321 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 322 | if (msm_spm_debug_mask & MSM_SPM_DEBUG_VCTL) |
| 323 | pr_info("%s: requesting vlevel 0x%x\n", |
| 324 | __func__, vlevel); |
| 325 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 326 | msm_spm_drv_apcs_set_vctl(dev, vlevel); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 327 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_VCTL); |
| 328 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_PMIC_DATA_0); |
Praveen Chidambaram | f3f2b3e | 2012-03-21 20:13:38 -0600 | [diff] [blame] | 329 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_PMIC_DATA_1); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 330 | mb(); |
| 331 | |
| 332 | /* Wait for PMIC state to return to idle or until timeout */ |
| 333 | timeout_us = dev->vctl_timeout_us; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 334 | while (msm_spm_drv_get_sts_pmic_state(dev) != MSM_SPM_PMIC_STATE_IDLE) { |
| 335 | if (!timeout_us) |
| 336 | goto set_vdd_bail; |
| 337 | |
| 338 | if (timeout_us > 10) { |
| 339 | udelay(10); |
| 340 | timeout_us -= 10; |
| 341 | } else { |
| 342 | udelay(timeout_us); |
| 343 | timeout_us = 0; |
| 344 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 347 | if (msm_spm_drv_get_sts_curr_pmic_data(dev) != vlevel) |
| 348 | goto set_vdd_bail; |
| 349 | |
| 350 | if (msm_spm_debug_mask & MSM_SPM_DEBUG_VCTL) |
| 351 | pr_info("%s: done, remaining timeout %uus\n", |
| 352 | __func__, timeout_us); |
| 353 | |
| 354 | return 0; |
| 355 | |
| 356 | set_vdd_bail: |
| 357 | pr_err("%s: failed, remaining timeout %uus, vlevel 0x%x\n", |
| 358 | __func__, timeout_us, msm_spm_drv_get_sts_curr_pmic_data(dev)); |
| 359 | return -EIO; |
| 360 | } |
| 361 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 362 | int msm_spm_drv_set_phase(struct msm_spm_driver_data *dev, |
| 363 | unsigned int phase_cnt) |
| 364 | { |
| 365 | unsigned int pmic_data = 0; |
| 366 | unsigned int timeout_us = 0; |
| 367 | |
| 368 | if (dev->major != SAW2_MAJOR_2) |
| 369 | return -ENODEV; |
| 370 | |
| 371 | pmic_data |= phase_cnt & 0xFF; |
| 372 | pmic_data |= (dev->phase_port & 0x7) << 16; |
| 373 | |
| 374 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] &= ~0x700FF; |
| 375 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] |= pmic_data; |
| 376 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_VCTL); |
| 377 | mb(); |
| 378 | |
| 379 | /* Wait for PMIC state to return to idle or until timeout */ |
| 380 | timeout_us = dev->vctl_timeout_us; |
| 381 | while (msm_spm_drv_get_sts_pmic_state(dev) != MSM_SPM_PMIC_STATE_IDLE) { |
| 382 | if (!timeout_us) |
| 383 | goto set_phase_bail; |
| 384 | |
| 385 | if (timeout_us > 10) { |
| 386 | udelay(10); |
| 387 | timeout_us -= 10; |
| 388 | } else { |
| 389 | udelay(timeout_us); |
| 390 | timeout_us = 0; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | if (msm_spm_drv_get_sts_curr_pmic_data(dev) != phase_cnt) |
| 395 | goto set_phase_bail; |
| 396 | |
| 397 | if (msm_spm_debug_mask & MSM_SPM_DEBUG_VCTL) |
| 398 | pr_info("%s: done, remaining timeout %uus\n", |
| 399 | __func__, timeout_us); |
| 400 | |
| 401 | return 0; |
| 402 | |
| 403 | set_phase_bail: |
| 404 | pr_err("%s: failed, remaining timeout %uus, phase count %d\n", |
| 405 | __func__, timeout_us, msm_spm_drv_get_sts_curr_pmic_data(dev)); |
| 406 | return -EIO; |
| 407 | |
| 408 | } |
| 409 | |
Maheshkumar Sivasubramanian | 4ac2376 | 2011-11-02 10:03:06 -0600 | [diff] [blame] | 410 | void msm_spm_drv_reinit(struct msm_spm_driver_data *dev) |
| 411 | { |
| 412 | int i; |
| 413 | |
| 414 | for (i = 0; i < MSM_SPM_REG_NR_INITIALIZE; i++) |
| 415 | msm_spm_drv_flush_shadow(dev, i); |
| 416 | |
| 417 | msm_spm_drv_flush_seq_entry(dev); |
| 418 | mb(); |
| 419 | } |
| 420 | |
Stephen Boyd | db35411 | 2012-05-09 14:24:58 -0700 | [diff] [blame^] | 421 | int __devinit msm_spm_drv_init(struct msm_spm_driver_data *dev, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 422 | struct msm_spm_platform_data *data) |
| 423 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 424 | int i; |
| 425 | int num_spm_entry; |
| 426 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 427 | BUG_ON(!dev || !data); |
| 428 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 429 | if (dev->ver_reg == SAW2_V2_VER_REG) |
| 430 | dev->reg_offsets = msm_spm_reg_offsets_v2; |
| 431 | else |
| 432 | dev->reg_offsets = msm_spm_reg_offsets_v1; |
| 433 | |
| 434 | dev->vctl_port = data->vctl_port; |
| 435 | dev->phase_port = data->phase_port; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 436 | dev->reg_base_addr = data->reg_base_addr; |
| 437 | memcpy(dev->reg_shadow, data->reg_init_values, |
| 438 | sizeof(data->reg_init_values)); |
| 439 | |
| 440 | dev->vctl_timeout_us = data->vctl_timeout_us; |
| 441 | |
| 442 | for (i = 0; i < MSM_SPM_REG_NR_INITIALIZE; i++) |
| 443 | msm_spm_drv_flush_shadow(dev, i); |
| 444 | /* barrier to ensure write completes before we update shadow |
| 445 | * registers |
| 446 | */ |
| 447 | mb(); |
| 448 | |
| 449 | for (i = 0; i < MSM_SPM_REG_NR_INITIALIZE; i++) |
| 450 | msm_spm_drv_load_shadow(dev, i); |
| 451 | |
| 452 | /* barrier to ensure read completes before we proceed further*/ |
| 453 | mb(); |
| 454 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 455 | msm_spm_drv_get_saw2_ver(dev, &dev->major, &dev->minor); |
| 456 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 457 | num_spm_entry = msm_spm_drv_get_num_spm_entry(dev); |
| 458 | |
| 459 | dev->reg_seq_entry_shadow = |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 460 | kzalloc(sizeof(*dev->reg_seq_entry_shadow) * num_spm_entry, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 461 | GFP_KERNEL); |
| 462 | |
| 463 | if (!dev->reg_seq_entry_shadow) |
| 464 | return -ENOMEM; |
| 465 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 466 | return 0; |
| 467 | } |