Rajeshwari Shinde | e9e8be8 | 2012-08-24 00:39:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 Samsung Electronics |
| 3 | * Rajeshwari Shinde <rajeshwari.s@samsung.com> |
| 4 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
Rajeshwari Shinde | e9e8be8 | 2012-08-24 00:39:23 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Rajeshwari Shinde | 817cc27 | 2013-01-08 21:03:40 +0000 | [diff] [blame] | 9 | #include <fdtdec.h> |
| 10 | #include <i2c.h> |
Rajeshwari Shinde | fe7c362 | 2012-12-10 01:55:47 +0000 | [diff] [blame] | 11 | #include <power/pmic.h> |
| 12 | #include <power/max77686_pmic.h> |
| 13 | #include <errno.h> |
Rajeshwari Shinde | e9e8be8 | 2012-08-24 00:39:23 +0000 | [diff] [blame] | 14 | |
Rajeshwari Shinde | 817cc27 | 2013-01-08 21:03:40 +0000 | [diff] [blame] | 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
Piotr Wilczek | 64a3d4d | 2013-06-25 09:59:47 +0200 | [diff] [blame] | 17 | static const char max77686_buck_addr[] = { |
| 18 | 0xff, 0x10, 0x12, 0x1c, 0x26, 0x30, 0x32, 0x34, 0x36, 0x38 |
| 19 | }; |
| 20 | |
| 21 | static unsigned int max77686_ldo_volt2hex(int ldo, ulong uV) |
| 22 | { |
| 23 | unsigned int hex = 0; |
| 24 | |
| 25 | switch (ldo) { |
| 26 | case 1: |
| 27 | case 2: |
| 28 | case 6: |
| 29 | case 7: |
| 30 | case 8: |
| 31 | case 15: |
| 32 | hex = (uV - 800000) / 25000; |
| 33 | break; |
| 34 | default: |
| 35 | hex = (uV - 800000) / 50000; |
| 36 | } |
| 37 | |
| 38 | if (hex >= 0 && hex <= MAX77686_LDO_VOLT_MAX_HEX) |
| 39 | return hex; |
| 40 | |
| 41 | debug("%s: %ld is wrong voltage value for LDO%d\n", __func__, uV, ldo); |
| 42 | return 0; |
| 43 | } |
| 44 | |
Suriyan Ramasami | cab8ef9 | 2014-10-29 09:22:41 -0700 | [diff] [blame] | 45 | static int max77686_buck_volt2hex(int buck, ulong uV) |
| 46 | { |
| 47 | int hex = 0; |
| 48 | |
| 49 | if (buck < 5 || buck > 9) { |
| 50 | debug("%s: buck %d is not supported\n", __func__, buck); |
| 51 | return -EINVAL; |
| 52 | } |
| 53 | |
| 54 | hex = (uV - 750000) / 50000; |
| 55 | |
| 56 | if (hex >= 0 && hex <= MAX77686_BUCK_VOLT_MAX_HEX) |
| 57 | return hex; |
| 58 | |
| 59 | debug("%s: %ld is wrong voltage value for BUCK%d\n", |
| 60 | __func__, uV, buck); |
| 61 | return -EINVAL; |
| 62 | } |
| 63 | |
Piotr Wilczek | 64a3d4d | 2013-06-25 09:59:47 +0200 | [diff] [blame] | 64 | int max77686_set_ldo_voltage(struct pmic *p, int ldo, ulong uV) |
| 65 | { |
| 66 | unsigned int val, ret, hex, adr; |
| 67 | |
Suriyan Ramasami | cab8ef9 | 2014-10-29 09:22:41 -0700 | [diff] [blame] | 68 | if (ldo < 1 || ldo > 26) { |
Piotr Wilczek | 64a3d4d | 2013-06-25 09:59:47 +0200 | [diff] [blame] | 69 | printf("%s: %d is wrong ldo number\n", __func__, ldo); |
| 70 | return -1; |
| 71 | } |
| 72 | |
| 73 | adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1; |
| 74 | hex = max77686_ldo_volt2hex(ldo, uV); |
| 75 | |
| 76 | if (!hex) |
| 77 | return -1; |
| 78 | |
| 79 | ret = pmic_reg_read(p, adr, &val); |
| 80 | if (ret) |
| 81 | return ret; |
| 82 | |
| 83 | val &= ~MAX77686_LDO_VOLT_MASK; |
| 84 | val |= hex; |
| 85 | ret |= pmic_reg_write(p, adr, val); |
| 86 | |
| 87 | return ret; |
| 88 | } |
| 89 | |
Suriyan Ramasami | cab8ef9 | 2014-10-29 09:22:41 -0700 | [diff] [blame] | 90 | int max77686_set_buck_voltage(struct pmic *p, int buck, ulong uV) |
| 91 | { |
| 92 | unsigned int val, adr; |
| 93 | int hex, ret; |
| 94 | |
| 95 | if (buck < 5 || buck > 9) { |
| 96 | printf("%s: %d is an unsupported bucket number\n", |
| 97 | __func__, buck); |
| 98 | return -EINVAL; |
| 99 | } |
| 100 | |
| 101 | adr = max77686_buck_addr[buck] + 1; |
| 102 | hex = max77686_buck_volt2hex(buck, uV); |
| 103 | |
| 104 | if (hex < 0) |
| 105 | return hex; |
| 106 | |
| 107 | ret = pmic_reg_read(p, adr, &val); |
| 108 | if (ret) |
| 109 | return ret; |
| 110 | |
| 111 | val &= ~MAX77686_BUCK_VOLT_MASK; |
| 112 | ret |= pmic_reg_write(p, adr, val | hex); |
| 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | |
Piotr Wilczek | 64a3d4d | 2013-06-25 09:59:47 +0200 | [diff] [blame] | 117 | int max77686_set_ldo_mode(struct pmic *p, int ldo, char opmode) |
| 118 | { |
| 119 | unsigned int val, ret, adr, mode; |
| 120 | |
Suriyan Ramasami | cab8ef9 | 2014-10-29 09:22:41 -0700 | [diff] [blame] | 121 | if (ldo < 1 || 26 < ldo) { |
Piotr Wilczek | 64a3d4d | 2013-06-25 09:59:47 +0200 | [diff] [blame] | 122 | printf("%s: %d is wrong ldo number\n", __func__, ldo); |
| 123 | return -1; |
| 124 | } |
| 125 | |
| 126 | adr = MAX77686_REG_PMIC_LDO1CTRL1 + ldo - 1; |
| 127 | |
| 128 | /* mode */ |
| 129 | switch (opmode) { |
| 130 | case OPMODE_OFF: |
| 131 | mode = MAX77686_LDO_MODE_OFF; |
| 132 | break; |
| 133 | case OPMODE_STANDBY: |
| 134 | switch (ldo) { |
| 135 | case 2: |
| 136 | case 6: |
| 137 | case 7: |
| 138 | case 8: |
| 139 | case 10: |
| 140 | case 11: |
| 141 | case 12: |
| 142 | case 14: |
| 143 | case 15: |
| 144 | case 16: |
| 145 | mode = MAX77686_LDO_MODE_STANDBY; |
| 146 | break; |
| 147 | default: |
| 148 | mode = 0xff; |
| 149 | } |
| 150 | break; |
| 151 | case OPMODE_LPM: |
| 152 | mode = MAX77686_LDO_MODE_LPM; |
| 153 | break; |
| 154 | case OPMODE_ON: |
| 155 | mode = MAX77686_LDO_MODE_ON; |
| 156 | break; |
| 157 | default: |
| 158 | mode = 0xff; |
| 159 | } |
| 160 | |
| 161 | if (mode == 0xff) { |
| 162 | printf("%s: %d is not supported on LDO%d\n", |
| 163 | __func__, opmode, ldo); |
| 164 | return -1; |
| 165 | } |
| 166 | |
| 167 | ret = pmic_reg_read(p, adr, &val); |
| 168 | if (ret) |
| 169 | return ret; |
| 170 | |
| 171 | val &= ~MAX77686_LDO_MODE_MASK; |
| 172 | val |= mode; |
| 173 | ret |= pmic_reg_write(p, adr, val); |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
| 178 | int max77686_set_buck_mode(struct pmic *p, int buck, char opmode) |
| 179 | { |
| 180 | unsigned int val, ret, mask, adr, size, mode, mode_shift; |
| 181 | |
| 182 | size = ARRAY_SIZE(max77686_buck_addr); |
| 183 | if (buck >= size) { |
| 184 | printf("%s: %d is wrong buck number\n", __func__, buck); |
| 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | adr = max77686_buck_addr[buck]; |
| 189 | |
| 190 | /* mask */ |
| 191 | switch (buck) { |
| 192 | case 2: |
| 193 | case 3: |
| 194 | case 4: |
| 195 | mode_shift = MAX77686_BUCK_MODE_SHIFT_2; |
| 196 | break; |
| 197 | default: |
| 198 | mode_shift = MAX77686_BUCK_MODE_SHIFT_1; |
| 199 | } |
| 200 | |
| 201 | mask = MAX77686_BUCK_MODE_MASK << mode_shift; |
| 202 | |
| 203 | /* mode */ |
| 204 | switch (opmode) { |
| 205 | case OPMODE_OFF: |
Suriyan Ramasami | cab8ef9 | 2014-10-29 09:22:41 -0700 | [diff] [blame] | 206 | mode = MAX77686_BUCK_MODE_OFF << mode_shift; |
Piotr Wilczek | 64a3d4d | 2013-06-25 09:59:47 +0200 | [diff] [blame] | 207 | break; |
| 208 | case OPMODE_STANDBY: |
| 209 | switch (buck) { |
| 210 | case 1: |
| 211 | case 2: |
| 212 | case 3: |
| 213 | case 4: |
| 214 | mode = MAX77686_BUCK_MODE_STANDBY << mode_shift; |
| 215 | break; |
| 216 | default: |
| 217 | mode = 0xff; |
| 218 | } |
| 219 | break; |
| 220 | case OPMODE_LPM: |
| 221 | switch (buck) { |
| 222 | case 2: |
| 223 | case 3: |
| 224 | case 4: |
| 225 | mode = MAX77686_BUCK_MODE_LPM << mode_shift; |
| 226 | break; |
| 227 | default: |
| 228 | mode = 0xff; |
| 229 | } |
| 230 | break; |
| 231 | case OPMODE_ON: |
| 232 | mode = MAX77686_BUCK_MODE_ON << mode_shift; |
| 233 | break; |
| 234 | default: |
| 235 | mode = 0xff; |
| 236 | } |
| 237 | |
| 238 | if (mode == 0xff) { |
| 239 | printf("%s: %d is not supported on BUCK%d\n", |
| 240 | __func__, opmode, buck); |
| 241 | return -1; |
| 242 | } |
| 243 | |
| 244 | ret = pmic_reg_read(p, adr, &val); |
| 245 | if (ret) |
| 246 | return ret; |
| 247 | |
| 248 | val &= ~mask; |
| 249 | val |= mode; |
| 250 | ret |= pmic_reg_write(p, adr, val); |
| 251 | |
| 252 | return ret; |
| 253 | } |
| 254 | |
Rajeshwari Shinde | fe7c362 | 2012-12-10 01:55:47 +0000 | [diff] [blame] | 255 | int pmic_init(unsigned char bus) |
Rajeshwari Shinde | e9e8be8 | 2012-08-24 00:39:23 +0000 | [diff] [blame] | 256 | { |
Rajeshwari Shinde | e9e8be8 | 2012-08-24 00:39:23 +0000 | [diff] [blame] | 257 | static const char name[] = "MAX77686_PMIC"; |
Rajeshwari Shinde | fe7c362 | 2012-12-10 01:55:47 +0000 | [diff] [blame] | 258 | struct pmic *p = pmic_alloc(); |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 259 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Jeroen Hofstee | 746b92b | 2014-06-18 22:13:52 +0200 | [diff] [blame] | 260 | const void *blob = gd->fdt_blob; |
| 261 | int node, parent, tmp; |
| 262 | #endif |
Rajeshwari Shinde | fe7c362 | 2012-12-10 01:55:47 +0000 | [diff] [blame] | 263 | |
| 264 | if (!p) { |
| 265 | printf("%s: POWER allocation error!\n", __func__); |
| 266 | return -ENOMEM; |
| 267 | } |
Rajeshwari Shinde | e9e8be8 | 2012-08-24 00:39:23 +0000 | [diff] [blame] | 268 | |
Masahiro Yamada | 366b24f | 2015-08-12 07:31:55 +0900 | [diff] [blame] | 269 | #if CONFIG_IS_ENABLED(OF_CONTROL) |
Rajeshwari Shinde | 817cc27 | 2013-01-08 21:03:40 +0000 | [diff] [blame] | 270 | node = fdtdec_next_compatible(blob, 0, COMPAT_MAXIM_MAX77686_PMIC); |
| 271 | if (node < 0) { |
| 272 | debug("PMIC: No node for PMIC Chip in device tree\n"); |
| 273 | debug("node = %d\n", node); |
| 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | parent = fdt_parent_offset(blob, node); |
| 278 | if (parent < 0) { |
| 279 | debug("%s: Cannot find node parent\n", __func__); |
| 280 | return -1; |
| 281 | } |
| 282 | |
Jeroen Hofstee | 746b92b | 2014-06-18 22:13:52 +0200 | [diff] [blame] | 283 | /* tmp since p->bus is unsigned */ |
| 284 | tmp = i2c_get_bus_num_fdt(parent); |
| 285 | if (tmp < 0) { |
Rajeshwari Shinde | 817cc27 | 2013-01-08 21:03:40 +0000 | [diff] [blame] | 286 | debug("%s: Cannot find I2C bus\n", __func__); |
| 287 | return -1; |
| 288 | } |
Jeroen Hofstee | 746b92b | 2014-06-18 22:13:52 +0200 | [diff] [blame] | 289 | p->bus = tmp; |
Rajeshwari Shinde | 817cc27 | 2013-01-08 21:03:40 +0000 | [diff] [blame] | 290 | p->hw.i2c.addr = fdtdec_get_int(blob, node, "reg", 9); |
| 291 | #else |
| 292 | p->bus = bus; |
| 293 | p->hw.i2c.addr = MAX77686_I2C_ADDR; |
| 294 | #endif |
| 295 | |
Rajeshwari Shinde | e9e8be8 | 2012-08-24 00:39:23 +0000 | [diff] [blame] | 296 | p->name = name; |
| 297 | p->interface = PMIC_I2C; |
Przemyslaw Marczak | 58ea76f | 2015-04-20 20:07:46 +0200 | [diff] [blame] | 298 | p->number_of_regs = MAX77686_NUM_OF_REGS; |
Rajeshwari Shinde | e9e8be8 | 2012-08-24 00:39:23 +0000 | [diff] [blame] | 299 | p->hw.i2c.tx_num = 1; |
Rajeshwari Shinde | 817cc27 | 2013-01-08 21:03:40 +0000 | [diff] [blame] | 300 | |
| 301 | puts("Board PMIC init\n"); |
Rajeshwari Shinde | e9e8be8 | 2012-08-24 00:39:23 +0000 | [diff] [blame] | 302 | |
| 303 | return 0; |
| 304 | } |