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 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 87 | static inline uint32_t msm_spm_drv_get_num_spm_entry( |
| 88 | struct msm_spm_driver_data *dev) |
| 89 | { |
| 90 | return 32; |
| 91 | } |
| 92 | |
| 93 | static void msm_spm_drv_flush_shadow(struct msm_spm_driver_data *dev, |
| 94 | unsigned int reg_index) |
| 95 | { |
| 96 | __raw_writel(dev->reg_shadow[reg_index], |
| 97 | dev->reg_base_addr + dev->reg_offsets[reg_index]); |
| 98 | } |
| 99 | |
| 100 | static void msm_spm_drv_load_shadow(struct msm_spm_driver_data *dev, |
| 101 | unsigned int reg_index) |
| 102 | { |
| 103 | dev->reg_shadow[reg_index] = |
| 104 | __raw_readl(dev->reg_base_addr + |
| 105 | dev->reg_offsets[reg_index]); |
| 106 | } |
| 107 | |
| 108 | static inline void msm_spm_drv_set_start_addr( |
| 109 | struct msm_spm_driver_data *dev, uint32_t addr) |
| 110 | { |
| 111 | addr &= 0x7F; |
| 112 | addr <<= 4; |
| 113 | dev->reg_shadow[MSM_SPM_REG_SAW2_SPM_CTL] &= 0xFFFFF80F; |
| 114 | dev->reg_shadow[MSM_SPM_REG_SAW2_SPM_CTL] |= addr; |
| 115 | } |
| 116 | |
| 117 | static inline bool msm_spm_pmic_arb_present(struct msm_spm_driver_data *dev) |
| 118 | { |
| 119 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_ID); |
| 120 | |
| 121 | if (dev->major == SAW2_MAJOR_2) |
| 122 | return (dev->reg_shadow[MSM_SPM_REG_SAW2_ID] >> 2) & 0x1; |
| 123 | else |
| 124 | return (dev->reg_shadow[MSM_SPM_REG_SAW2_ID] >> 18) & 0x1; |
| 125 | } |
| 126 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 127 | static inline void msm_spm_drv_set_vctl(struct msm_spm_driver_data *dev, |
| 128 | uint32_t vlevel) |
| 129 | { |
| 130 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] &= ~0xFF; |
| 131 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] |= vlevel; |
| 132 | |
| 133 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_0] &= ~0xFF; |
| 134 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_0] |= vlevel; |
Praveen Chidambaram | f3f2b3e | 2012-03-21 20:13:38 -0600 | [diff] [blame] | 135 | |
| 136 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_1] &= ~0x3F; |
| 137 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_1] |= (vlevel & 0x3F); |
Girish Mahadevan | d27ca4a | 2012-08-15 09:21:23 -0600 | [diff] [blame] | 138 | |
| 139 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_1] &= ~0x3F0000; |
| 140 | dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_DATA_1] |= |
| 141 | ((vlevel & 0x3F) << 16); |
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 | |
Praveen Chidambaram | 1dbe495 | 2012-10-03 17:06:02 -0600 | [diff] [blame^] | 149 | /** |
| 150 | * VCTL_PORT has to be 0, for PMIC_STS register to be updated. |
| 151 | * Ensure that vctl_port is always set to 0. |
| 152 | */ |
| 153 | WARN_ON(dev->vctl_port); |
| 154 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 155 | pmic_data |= vlevel; |
| 156 | pmic_data |= (dev->vctl_port & 0x7) << 16; |
| 157 | |
| 158 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] &= ~0x700FF; |
| 159 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] |= pmic_data; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 162 | static inline void msm_spm_drv_apcs_set_vctl(struct msm_spm_driver_data *dev, |
| 163 | unsigned int vlevel) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 164 | { |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 165 | if (dev->major == SAW2_MAJOR_2) |
| 166 | return msm_spm_drv_set_vctl2(dev, vlevel); |
| 167 | else |
| 168 | return msm_spm_drv_set_vctl(dev, vlevel); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static inline uint32_t msm_spm_drv_get_sts_pmic_state( |
| 172 | struct msm_spm_driver_data *dev) |
| 173 | { |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 174 | if (dev->major == SAW2_MAJOR_2) { |
| 175 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_PMIC_STS); |
| 176 | return (dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_STS] >> 16) & |
| 177 | 0x03; |
| 178 | } else { |
| 179 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_STS0); |
| 180 | return (dev->reg_shadow[MSM_SPM_REG_SAW2_STS0] >> 10) & 0x03; |
| 181 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 182 | } |
| 183 | |
Praveen Chidambaram | 4133ba1 | 2012-09-29 22:27:03 -0600 | [diff] [blame] | 184 | uint32_t msm_spm_drv_get_sts_curr_pmic_data( |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 185 | struct msm_spm_driver_data *dev) |
| 186 | { |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 187 | if (dev->major == SAW2_MAJOR_2) { |
| 188 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_PMIC_STS); |
| 189 | return dev->reg_shadow[MSM_SPM_REG_SAW2_PMIC_STS] & 0xFF; |
| 190 | } else { |
| 191 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_STS1); |
| 192 | return dev->reg_shadow[MSM_SPM_REG_SAW2_STS1] & 0xFF; |
| 193 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 196 | static inline uint32_t msm_spm_drv_get_saw2_ver(struct msm_spm_driver_data *dev, |
| 197 | uint32_t *major, uint32_t *minor) |
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 | int ret = -ENODEV; |
| 200 | uint32_t val = 0; |
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 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_VERSION); |
| 203 | val = dev->reg_shadow[MSM_SPM_REG_SAW2_VERSION]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 204 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 205 | if (dev->ver_reg == SAW2_V2_VER_REG) { |
| 206 | *major = (val >> 28) & 0xF; |
| 207 | *minor = (val >> 16) & 0xFFF; |
| 208 | ret = 0; |
| 209 | } else if (dev->ver_reg == SAW2_V1_VER_REG) { |
| 210 | *major = (val >> 4) & 0xF; |
| 211 | *minor = val & 0xF; |
| 212 | ret = 0; |
| 213 | } |
| 214 | |
| 215 | return ret; |
| 216 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 217 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 218 | inline int msm_spm_drv_set_spm_enable( |
| 219 | struct msm_spm_driver_data *dev, bool enable) |
| 220 | { |
| 221 | uint32_t value = enable ? 0x01 : 0x00; |
| 222 | |
| 223 | if (!dev) |
| 224 | return -EINVAL; |
| 225 | |
| 226 | if ((dev->reg_shadow[MSM_SPM_REG_SAW2_SPM_CTL] & 0x01) ^ value) { |
| 227 | |
| 228 | dev->reg_shadow[MSM_SPM_REG_SAW2_SPM_CTL] &= ~0x1; |
| 229 | dev->reg_shadow[MSM_SPM_REG_SAW2_SPM_CTL] |= value; |
| 230 | |
| 231 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_SPM_CTL); |
| 232 | wmb(); |
| 233 | } |
| 234 | return 0; |
| 235 | } |
| 236 | void msm_spm_drv_flush_seq_entry(struct msm_spm_driver_data *dev) |
| 237 | { |
| 238 | int i; |
| 239 | int num_spm_entry = msm_spm_drv_get_num_spm_entry(dev); |
| 240 | |
| 241 | if (!dev) { |
| 242 | __WARN(); |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | for (i = 0; i < num_spm_entry; i++) { |
| 247 | __raw_writel(dev->reg_seq_entry_shadow[i], |
| 248 | dev->reg_base_addr |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 249 | + dev->reg_offsets[MSM_SPM_REG_SAW2_SEQ_ENTRY] |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 250 | + 4 * i); |
| 251 | } |
| 252 | mb(); |
| 253 | } |
| 254 | |
| 255 | 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] | 256 | uint8_t *cmd, uint32_t *offset) |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 257 | { |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 258 | uint32_t cmd_w; |
| 259 | uint32_t offset_w = *offset / 4; |
| 260 | uint8_t last_cmd; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 261 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 262 | if (!cmd) |
| 263 | return -EINVAL; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 264 | |
| 265 | while (1) { |
| 266 | int i; |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 267 | cmd_w = 0; |
| 268 | last_cmd = 0; |
| 269 | cmd_w = dev->reg_seq_entry_shadow[offset_w]; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 270 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 271 | for (i = (*offset % 4) ; i < 4; i++) { |
| 272 | last_cmd = *(cmd++); |
| 273 | cmd_w |= last_cmd << (i * 8); |
| 274 | (*offset)++; |
| 275 | if (last_cmd == 0x0f) |
| 276 | break; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 277 | } |
| 278 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 279 | dev->reg_seq_entry_shadow[offset_w++] = cmd_w; |
| 280 | if (last_cmd == 0x0f) |
| 281 | break; |
| 282 | } |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 283 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 284 | return 0; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | int msm_spm_drv_set_low_power_mode(struct msm_spm_driver_data *dev, |
| 288 | uint32_t addr) |
| 289 | { |
| 290 | |
| 291 | /* SPM is configured to reset start address to zero after end of Program |
| 292 | */ |
| 293 | if (!dev) |
| 294 | return -EINVAL; |
| 295 | |
| 296 | msm_spm_drv_set_start_addr(dev, addr); |
| 297 | |
Maheshkumar Sivasubramanian | daa23bb | 2011-07-01 17:24:51 -0600 | [diff] [blame] | 298 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_SPM_CTL); |
| 299 | wmb(); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 300 | |
| 301 | if (msm_spm_debug_mask & MSM_SPM_DEBUG_SHADOW) { |
| 302 | int i; |
| 303 | for (i = 0; i < MSM_SPM_REG_NR; i++) |
| 304 | pr_info("%s: reg %02x = 0x%08x\n", __func__, |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 305 | dev->reg_offsets[i], dev->reg_shadow[i]); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Praveen Chidambaram | 631f282 | 2012-03-16 12:08:42 -0600 | [diff] [blame] | 311 | #ifdef CONFIG_MSM_AVS_HW |
| 312 | static void msm_spm_drv_disable_avs(struct msm_spm_driver_data *dev) |
| 313 | { |
| 314 | msm_spm_drv_load_shadow(dev, MSM_SPM_REG_SAW2_AVS_CTL); |
| 315 | dev->reg_shadow[MSM_SPM_REG_SAW2_AVS_CTL] &= ~BIT(27); |
| 316 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_AVS_CTL); |
| 317 | } |
| 318 | |
| 319 | static void msm_spm_drv_enable_avs(struct msm_spm_driver_data *dev) |
| 320 | { |
| 321 | dev->reg_shadow[MSM_SPM_REG_SAW2_AVS_CTL] |= BIT(27); |
| 322 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_AVS_CTL); |
| 323 | } |
| 324 | |
| 325 | static void msm_spm_drv_set_avs_vlevel(struct msm_spm_driver_data *dev, |
| 326 | unsigned int vlevel) |
| 327 | { |
| 328 | vlevel &= 0x3f; |
| 329 | dev->reg_shadow[MSM_SPM_REG_SAW2_AVS_CTL] &= ~0x7efc00; |
| 330 | dev->reg_shadow[MSM_SPM_REG_SAW2_AVS_CTL] |= ((vlevel - 4) << 10); |
| 331 | dev->reg_shadow[MSM_SPM_REG_SAW2_AVS_CTL] |= (vlevel << 17); |
| 332 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_AVS_CTL); |
| 333 | } |
| 334 | |
| 335 | #else |
| 336 | |
| 337 | static void msm_spm_drv_disable_avs(struct msm_spm_driver_data *dev) { } |
| 338 | |
| 339 | static void msm_spm_drv_enable_avs(struct msm_spm_driver_data *dev) { } |
| 340 | |
| 341 | static void msm_spm_drv_set_avs_vlevel(struct msm_spm_driver_data *dev, |
| 342 | unsigned int vlevel) { } |
| 343 | #endif |
| 344 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 345 | int msm_spm_drv_set_vdd(struct msm_spm_driver_data *dev, unsigned int vlevel) |
| 346 | { |
Praveen Chidambaram | 631f282 | 2012-03-16 12:08:42 -0600 | [diff] [blame] | 347 | uint32_t timeout_us, new_level; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 348 | |
| 349 | if (!dev) |
| 350 | return -EINVAL; |
| 351 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 352 | if (!msm_spm_pmic_arb_present(dev)) |
| 353 | return -ENOSYS; |
| 354 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 355 | if (msm_spm_debug_mask & MSM_SPM_DEBUG_VCTL) |
Praveen Chidambaram | 631f282 | 2012-03-16 12:08:42 -0600 | [diff] [blame] | 356 | pr_info("%s: requesting vlevel %#x\n", __func__, vlevel); |
| 357 | |
| 358 | msm_spm_drv_disable_avs(dev); |
| 359 | |
| 360 | /* Kick the state machine back to idle */ |
| 361 | dev->reg_shadow[MSM_SPM_REG_SAW2_RST] = 1; |
| 362 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_RST); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 363 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 364 | msm_spm_drv_apcs_set_vctl(dev, vlevel); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 365 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_VCTL); |
| 366 | 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] | 367 | 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] | 368 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 369 | timeout_us = dev->vctl_timeout_us; |
Praveen Chidambaram | 631f282 | 2012-03-16 12:08:42 -0600 | [diff] [blame] | 370 | /* Confirm the voltage we set was what hardware sent */ |
| 371 | do { |
| 372 | new_level = msm_spm_drv_get_sts_curr_pmic_data(dev); |
| 373 | if (new_level == vlevel) |
| 374 | break; |
| 375 | udelay(1); |
| 376 | } while (--timeout_us); |
| 377 | if (!timeout_us) { |
| 378 | pr_info("Wrong level %#x\n", new_level); |
| 379 | goto set_vdd_bail; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 382 | if (msm_spm_debug_mask & MSM_SPM_DEBUG_VCTL) |
Praveen Chidambaram | 1dbe495 | 2012-10-03 17:06:02 -0600 | [diff] [blame^] | 383 | pr_info("%s: done, remaining timeout %u us\n", |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 384 | __func__, timeout_us); |
| 385 | |
Praveen Chidambaram | 1dbe495 | 2012-10-03 17:06:02 -0600 | [diff] [blame^] | 386 | /* Set AVS min/max */ |
| 387 | msm_spm_drv_set_avs_vlevel(dev, vlevel); |
Praveen Chidambaram | 631f282 | 2012-03-16 12:08:42 -0600 | [diff] [blame] | 388 | msm_spm_drv_enable_avs(dev); |
Praveen Chidambaram | 1dbe495 | 2012-10-03 17:06:02 -0600 | [diff] [blame^] | 389 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 390 | return 0; |
| 391 | |
| 392 | set_vdd_bail: |
Praveen Chidambaram | 631f282 | 2012-03-16 12:08:42 -0600 | [diff] [blame] | 393 | msm_spm_drv_enable_avs(dev); |
Praveen Chidambaram | 1dbe495 | 2012-10-03 17:06:02 -0600 | [diff] [blame^] | 394 | pr_err("%s: failed %#x, remaining timeout %u us, vlevel %#x\n", |
Praveen Chidambaram | 631f282 | 2012-03-16 12:08:42 -0600 | [diff] [blame] | 395 | __func__, vlevel, timeout_us, new_level); |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 396 | return -EIO; |
| 397 | } |
| 398 | |
Praveen Chidambaram | 1dbe495 | 2012-10-03 17:06:02 -0600 | [diff] [blame^] | 399 | static int msm_spm_drv_get_pmic_port(struct msm_spm_driver_data *dev, |
| 400 | enum msm_spm_pmic_port port) |
| 401 | { |
| 402 | int index = -1; |
| 403 | |
| 404 | switch (port) { |
| 405 | case MSM_SPM_PMIC_VCTL_PORT: |
| 406 | index = dev->vctl_port; |
| 407 | break; |
| 408 | case MSM_SPM_PMIC_PHASE_PORT: |
| 409 | index = dev->phase_port; |
| 410 | break; |
| 411 | case MSM_SPM_PMIC_PFM_PORT: |
| 412 | index = dev->pfm_port; |
| 413 | break; |
| 414 | default: |
| 415 | break; |
| 416 | } |
| 417 | |
| 418 | return index; |
| 419 | } |
| 420 | |
| 421 | int msm_spm_drv_set_pmic_data(struct msm_spm_driver_data *dev, |
| 422 | enum msm_spm_pmic_port port, unsigned int data) |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 423 | { |
| 424 | unsigned int pmic_data = 0; |
| 425 | unsigned int timeout_us = 0; |
Praveen Chidambaram | 1dbe495 | 2012-10-03 17:06:02 -0600 | [diff] [blame^] | 426 | int index = 0; |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 427 | |
| 428 | if (dev->major != SAW2_MAJOR_2) |
| 429 | return -ENODEV; |
| 430 | |
Praveen Chidambaram | 1dbe495 | 2012-10-03 17:06:02 -0600 | [diff] [blame^] | 431 | if (!msm_spm_pmic_arb_present(dev)) |
| 432 | return -ENOSYS; |
| 433 | |
| 434 | index = msm_spm_drv_get_pmic_port(dev, port); |
| 435 | if (index < 0) |
| 436 | return -ENODEV; |
| 437 | |
| 438 | pmic_data |= data & 0xFF; |
| 439 | pmic_data |= (index & 0x7) << 16; |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 440 | |
| 441 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] &= ~0x700FF; |
| 442 | dev->reg_shadow[MSM_SPM_REG_SAW2_VCTL] |= pmic_data; |
| 443 | msm_spm_drv_flush_shadow(dev, MSM_SPM_REG_SAW2_VCTL); |
| 444 | mb(); |
| 445 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 446 | timeout_us = dev->vctl_timeout_us; |
Praveen Chidambaram | 1dbe495 | 2012-10-03 17:06:02 -0600 | [diff] [blame^] | 447 | /** |
| 448 | * Confirm the pmic data set was what hardware sent by |
| 449 | * checking the PMIC FSM state. |
| 450 | * We cannot use the sts_pmic_data and check it against |
| 451 | * the value like we do fot set_vdd, since the PMIC_STS |
| 452 | * is only updated for SAW_VCTL sent with port index 0. |
| 453 | */ |
| 454 | do { |
| 455 | if (msm_spm_drv_get_sts_pmic_state(dev) == |
| 456 | MSM_SPM_PMIC_STATE_IDLE) |
| 457 | break; |
| 458 | udelay(1); |
| 459 | } while (--timeout_us); |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 460 | |
Praveen Chidambaram | 1dbe495 | 2012-10-03 17:06:02 -0600 | [diff] [blame^] | 461 | if (!timeout_us) { |
| 462 | pr_err("%s: failed, remaining timeout %u us, data %d\n", |
| 463 | __func__, timeout_us, data); |
| 464 | return -EIO; |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 465 | } |
| 466 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 467 | return 0; |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 468 | } |
| 469 | |
Maheshkumar Sivasubramanian | 4ac2376 | 2011-11-02 10:03:06 -0600 | [diff] [blame] | 470 | void msm_spm_drv_reinit(struct msm_spm_driver_data *dev) |
| 471 | { |
| 472 | int i; |
| 473 | |
| 474 | for (i = 0; i < MSM_SPM_REG_NR_INITIALIZE; i++) |
| 475 | msm_spm_drv_flush_shadow(dev, i); |
| 476 | |
| 477 | msm_spm_drv_flush_seq_entry(dev); |
| 478 | mb(); |
| 479 | } |
| 480 | |
Stephen Boyd | db35411 | 2012-05-09 14:24:58 -0700 | [diff] [blame] | 481 | int __devinit msm_spm_drv_init(struct msm_spm_driver_data *dev, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 482 | struct msm_spm_platform_data *data) |
| 483 | { |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 484 | int i; |
| 485 | int num_spm_entry; |
| 486 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 487 | BUG_ON(!dev || !data); |
| 488 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 489 | if (dev->ver_reg == SAW2_V2_VER_REG) |
| 490 | dev->reg_offsets = msm_spm_reg_offsets_v2; |
| 491 | else |
| 492 | dev->reg_offsets = msm_spm_reg_offsets_v1; |
| 493 | |
| 494 | dev->vctl_port = data->vctl_port; |
| 495 | dev->phase_port = data->phase_port; |
Praveen Chidambaram | 1dbe495 | 2012-10-03 17:06:02 -0600 | [diff] [blame^] | 496 | dev->pfm_port = data->pfm_port; |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 497 | dev->reg_base_addr = data->reg_base_addr; |
| 498 | memcpy(dev->reg_shadow, data->reg_init_values, |
| 499 | sizeof(data->reg_init_values)); |
| 500 | |
| 501 | dev->vctl_timeout_us = data->vctl_timeout_us; |
| 502 | |
| 503 | for (i = 0; i < MSM_SPM_REG_NR_INITIALIZE; i++) |
| 504 | msm_spm_drv_flush_shadow(dev, i); |
| 505 | /* barrier to ensure write completes before we update shadow |
| 506 | * registers |
| 507 | */ |
| 508 | mb(); |
| 509 | |
| 510 | for (i = 0; i < MSM_SPM_REG_NR_INITIALIZE; i++) |
| 511 | msm_spm_drv_load_shadow(dev, i); |
| 512 | |
| 513 | /* barrier to ensure read completes before we proceed further*/ |
| 514 | mb(); |
| 515 | |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 516 | msm_spm_drv_get_saw2_ver(dev, &dev->major, &dev->minor); |
| 517 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 518 | num_spm_entry = msm_spm_drv_get_num_spm_entry(dev); |
| 519 | |
| 520 | dev->reg_seq_entry_shadow = |
Praveen Chidambaram | aa9d52b | 2012-04-02 11:09:47 -0600 | [diff] [blame] | 521 | kzalloc(sizeof(*dev->reg_seq_entry_shadow) * num_spm_entry, |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 522 | GFP_KERNEL); |
| 523 | |
| 524 | if (!dev->reg_seq_entry_shadow) |
| 525 | return -ENOMEM; |
| 526 | |
Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 527 | return 0; |
| 528 | } |