Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 1 | /* |
| 2 | * s5m8767.c |
| 3 | * |
| 4 | * Copyright (c) 2011 Samsung Electronics Co., Ltd |
| 5 | * http://www.samsung.com |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2 of the License, or (at your |
| 10 | * option) any later version. |
| 11 | * |
| 12 | */ |
| 13 | |
| 14 | #include <linux/bug.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/err.h> |
| 17 | #include <linux/gpio.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/regulator/driver.h> |
| 22 | #include <linux/regulator/machine.h> |
| 23 | #include <linux/mfd/s5m87xx/s5m-core.h> |
| 24 | #include <linux/mfd/s5m87xx/s5m-pmic.h> |
| 25 | |
| 26 | struct s5m8767_info { |
| 27 | struct device *dev; |
| 28 | struct s5m87xx_dev *iodev; |
| 29 | int num_regulators; |
| 30 | struct regulator_dev **rdev; |
| 31 | |
| 32 | int ramp_delay; |
| 33 | bool buck2_ramp; |
| 34 | bool buck3_ramp; |
| 35 | bool buck4_ramp; |
| 36 | |
| 37 | bool buck2_gpiodvs; |
| 38 | bool buck3_gpiodvs; |
| 39 | bool buck4_gpiodvs; |
| 40 | u8 buck2_vol[8]; |
| 41 | u8 buck3_vol[8]; |
| 42 | u8 buck4_vol[8]; |
| 43 | int buck_gpios[3]; |
| 44 | int buck_gpioindex; |
| 45 | }; |
| 46 | |
| 47 | struct s5m_voltage_desc { |
| 48 | int max; |
| 49 | int min; |
| 50 | int step; |
| 51 | }; |
| 52 | |
| 53 | static const struct s5m_voltage_desc buck_voltage_val1 = { |
| 54 | .max = 2225000, |
| 55 | .min = 650000, |
| 56 | .step = 6250, |
| 57 | }; |
| 58 | |
| 59 | static const struct s5m_voltage_desc buck_voltage_val2 = { |
| 60 | .max = 1600000, |
| 61 | .min = 600000, |
| 62 | .step = 6250, |
| 63 | }; |
| 64 | |
| 65 | static const struct s5m_voltage_desc buck_voltage_val3 = { |
| 66 | .max = 3000000, |
| 67 | .min = 750000, |
| 68 | .step = 12500, |
| 69 | }; |
| 70 | |
| 71 | static const struct s5m_voltage_desc ldo_voltage_val1 = { |
| 72 | .max = 3950000, |
| 73 | .min = 800000, |
| 74 | .step = 50000, |
| 75 | }; |
| 76 | |
| 77 | static const struct s5m_voltage_desc ldo_voltage_val2 = { |
| 78 | .max = 2375000, |
| 79 | .min = 800000, |
| 80 | .step = 25000, |
| 81 | }; |
| 82 | |
| 83 | static const struct s5m_voltage_desc *reg_voltage_map[] = { |
| 84 | [S5M8767_LDO1] = &ldo_voltage_val2, |
| 85 | [S5M8767_LDO2] = &ldo_voltage_val2, |
| 86 | [S5M8767_LDO3] = &ldo_voltage_val1, |
| 87 | [S5M8767_LDO4] = &ldo_voltage_val1, |
| 88 | [S5M8767_LDO5] = &ldo_voltage_val1, |
| 89 | [S5M8767_LDO6] = &ldo_voltage_val2, |
| 90 | [S5M8767_LDO7] = &ldo_voltage_val2, |
| 91 | [S5M8767_LDO8] = &ldo_voltage_val2, |
| 92 | [S5M8767_LDO9] = &ldo_voltage_val1, |
| 93 | [S5M8767_LDO10] = &ldo_voltage_val1, |
| 94 | [S5M8767_LDO11] = &ldo_voltage_val1, |
| 95 | [S5M8767_LDO12] = &ldo_voltage_val1, |
| 96 | [S5M8767_LDO13] = &ldo_voltage_val1, |
| 97 | [S5M8767_LDO14] = &ldo_voltage_val1, |
| 98 | [S5M8767_LDO15] = &ldo_voltage_val2, |
| 99 | [S5M8767_LDO16] = &ldo_voltage_val1, |
| 100 | [S5M8767_LDO17] = &ldo_voltage_val1, |
| 101 | [S5M8767_LDO18] = &ldo_voltage_val1, |
| 102 | [S5M8767_LDO19] = &ldo_voltage_val1, |
| 103 | [S5M8767_LDO20] = &ldo_voltage_val1, |
| 104 | [S5M8767_LDO21] = &ldo_voltage_val1, |
| 105 | [S5M8767_LDO22] = &ldo_voltage_val1, |
| 106 | [S5M8767_LDO23] = &ldo_voltage_val1, |
| 107 | [S5M8767_LDO24] = &ldo_voltage_val1, |
| 108 | [S5M8767_LDO25] = &ldo_voltage_val1, |
| 109 | [S5M8767_LDO26] = &ldo_voltage_val1, |
| 110 | [S5M8767_LDO27] = &ldo_voltage_val1, |
| 111 | [S5M8767_LDO28] = &ldo_voltage_val1, |
| 112 | [S5M8767_BUCK1] = &buck_voltage_val1, |
| 113 | [S5M8767_BUCK2] = &buck_voltage_val2, |
| 114 | [S5M8767_BUCK3] = &buck_voltage_val2, |
| 115 | [S5M8767_BUCK4] = &buck_voltage_val2, |
| 116 | [S5M8767_BUCK5] = &buck_voltage_val1, |
| 117 | [S5M8767_BUCK6] = &buck_voltage_val1, |
| 118 | [S5M8767_BUCK7] = NULL, |
| 119 | [S5M8767_BUCK8] = NULL, |
| 120 | [S5M8767_BUCK9] = &buck_voltage_val3, |
| 121 | }; |
| 122 | |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 123 | static int s5m8767_list_voltage(struct regulator_dev *rdev, |
| 124 | unsigned int selector) |
| 125 | { |
| 126 | const struct s5m_voltage_desc *desc; |
Axel Lin | 20a14b8 | 2012-01-31 15:13:31 +0800 | [diff] [blame] | 127 | int reg_id = rdev_get_id(rdev); |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 128 | int val; |
| 129 | |
| 130 | if (reg_id >= ARRAY_SIZE(reg_voltage_map) || reg_id < 0) |
| 131 | return -EINVAL; |
| 132 | |
| 133 | desc = reg_voltage_map[reg_id]; |
| 134 | if (desc == NULL) |
| 135 | return -EINVAL; |
| 136 | |
| 137 | val = desc->min + desc->step * selector; |
| 138 | if (val > desc->max) |
| 139 | return -EINVAL; |
| 140 | |
| 141 | return val; |
| 142 | } |
| 143 | |
| 144 | static int s5m8767_get_register(struct regulator_dev *rdev, int *reg) |
| 145 | { |
Axel Lin | 20a14b8 | 2012-01-31 15:13:31 +0800 | [diff] [blame] | 146 | int reg_id = rdev_get_id(rdev); |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 147 | |
| 148 | switch (reg_id) { |
| 149 | case S5M8767_LDO1 ... S5M8767_LDO2: |
| 150 | *reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1); |
| 151 | break; |
| 152 | case S5M8767_LDO3 ... S5M8767_LDO28: |
| 153 | *reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3); |
| 154 | break; |
| 155 | case S5M8767_BUCK1: |
| 156 | *reg = S5M8767_REG_BUCK1CTRL1; |
| 157 | break; |
| 158 | case S5M8767_BUCK2 ... S5M8767_BUCK4: |
| 159 | *reg = S5M8767_REG_BUCK2CTRL + (reg_id - S5M8767_BUCK2) * 9; |
| 160 | break; |
| 161 | case S5M8767_BUCK5: |
| 162 | *reg = S5M8767_REG_BUCK5CTRL1; |
| 163 | break; |
| 164 | case S5M8767_BUCK6 ... S5M8767_BUCK9: |
| 165 | *reg = S5M8767_REG_BUCK6CTRL1 + (reg_id - S5M8767_BUCK6) * 2; |
| 166 | break; |
| 167 | default: |
| 168 | return -EINVAL; |
| 169 | } |
| 170 | |
| 171 | return 0; |
| 172 | } |
| 173 | |
| 174 | static int s5m8767_reg_is_enabled(struct regulator_dev *rdev) |
| 175 | { |
| 176 | struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev); |
| 177 | int ret, reg; |
| 178 | int mask = 0xc0, pattern = 0xc0; |
| 179 | u8 val; |
| 180 | |
| 181 | ret = s5m8767_get_register(rdev, ®); |
| 182 | if (ret == -EINVAL) |
| 183 | return 1; |
| 184 | else if (ret) |
| 185 | return ret; |
| 186 | |
| 187 | ret = s5m_reg_read(s5m8767->iodev, reg, &val); |
| 188 | if (ret) |
| 189 | return ret; |
| 190 | |
| 191 | return (val & mask) == pattern; |
| 192 | } |
| 193 | |
| 194 | static int s5m8767_reg_enable(struct regulator_dev *rdev) |
| 195 | { |
| 196 | struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev); |
| 197 | int ret, reg; |
| 198 | int mask = 0xc0, pattern = 0xc0; |
| 199 | |
| 200 | ret = s5m8767_get_register(rdev, ®); |
| 201 | if (ret) |
| 202 | return ret; |
| 203 | |
| 204 | return s5m_reg_update(s5m8767->iodev, reg, pattern, mask); |
| 205 | } |
| 206 | |
| 207 | static int s5m8767_reg_disable(struct regulator_dev *rdev) |
| 208 | { |
| 209 | struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev); |
| 210 | int ret, reg; |
| 211 | int mask = 0xc0, pattern = 0xc0; |
| 212 | |
| 213 | ret = s5m8767_get_register(rdev, ®); |
| 214 | if (ret) |
| 215 | return ret; |
| 216 | |
| 217 | return s5m_reg_update(s5m8767->iodev, reg, ~pattern, mask); |
| 218 | } |
| 219 | |
| 220 | static int s5m8767_get_voltage_register(struct regulator_dev *rdev, int *_reg) |
| 221 | { |
Axel Lin | 20a14b8 | 2012-01-31 15:13:31 +0800 | [diff] [blame] | 222 | int reg_id = rdev_get_id(rdev); |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 223 | int reg; |
| 224 | |
| 225 | switch (reg_id) { |
| 226 | case S5M8767_LDO1 ... S5M8767_LDO2: |
| 227 | reg = S5M8767_REG_LDO1CTRL + (reg_id - S5M8767_LDO1); |
| 228 | break; |
| 229 | case S5M8767_LDO3 ... S5M8767_LDO28: |
| 230 | reg = S5M8767_REG_LDO3CTRL + (reg_id - S5M8767_LDO3); |
| 231 | break; |
| 232 | case S5M8767_BUCK1: |
| 233 | reg = S5M8767_REG_BUCK1CTRL2; |
| 234 | break; |
| 235 | case S5M8767_BUCK2: |
| 236 | reg = S5M8767_REG_BUCK2DVS1; |
| 237 | break; |
| 238 | case S5M8767_BUCK3: |
| 239 | reg = S5M8767_REG_BUCK3DVS1; |
| 240 | break; |
| 241 | case S5M8767_BUCK4: |
| 242 | reg = S5M8767_REG_BUCK4DVS1; |
| 243 | break; |
| 244 | case S5M8767_BUCK5: |
| 245 | reg = S5M8767_REG_BUCK5CTRL2; |
| 246 | break; |
| 247 | case S5M8767_BUCK6 ... S5M8767_BUCK9: |
| 248 | reg = S5M8767_REG_BUCK6CTRL2 + (reg_id - S5M8767_BUCK6) * 2; |
| 249 | break; |
| 250 | default: |
| 251 | return -EINVAL; |
| 252 | } |
| 253 | |
| 254 | *_reg = reg; |
| 255 | |
| 256 | return 0; |
| 257 | } |
| 258 | |
| 259 | static int s5m8767_get_voltage_sel(struct regulator_dev *rdev) |
| 260 | { |
| 261 | struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev); |
| 262 | int reg, mask = 0xff, ret; |
Axel Lin | 20a14b8 | 2012-01-31 15:13:31 +0800 | [diff] [blame] | 263 | int reg_id = rdev_get_id(rdev); |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 264 | u8 val; |
| 265 | |
| 266 | ret = s5m8767_get_voltage_register(rdev, ®); |
| 267 | if (ret) |
| 268 | return ret; |
| 269 | |
| 270 | switch (reg_id) { |
| 271 | case S5M8767_LDO1 ... S5M8767_LDO28: |
| 272 | mask = 0x3f; |
| 273 | break; |
| 274 | case S5M8767_BUCK2: |
| 275 | if (s5m8767->buck2_gpiodvs) |
| 276 | reg += s5m8767->buck_gpioindex; |
| 277 | break; |
| 278 | case S5M8767_BUCK3: |
| 279 | if (s5m8767->buck3_gpiodvs) |
| 280 | reg += s5m8767->buck_gpioindex; |
| 281 | break; |
| 282 | case S5M8767_BUCK4: |
| 283 | if (s5m8767->buck4_gpiodvs) |
| 284 | reg += s5m8767->buck_gpioindex; |
| 285 | break; |
| 286 | } |
| 287 | |
| 288 | ret = s5m_reg_read(s5m8767->iodev, reg, &val); |
| 289 | if (ret) |
| 290 | return ret; |
| 291 | |
| 292 | val &= mask; |
| 293 | |
| 294 | return val; |
| 295 | } |
| 296 | |
| 297 | static inline int s5m8767_convert_voltage( |
| 298 | const struct s5m_voltage_desc *desc, |
| 299 | int min_vol, int max_vol) |
| 300 | { |
| 301 | int out_vol = 0; |
| 302 | |
| 303 | if (desc == NULL) |
| 304 | return -EINVAL; |
| 305 | |
| 306 | if (max_vol < desc->min || min_vol > desc->max) |
| 307 | return -EINVAL; |
| 308 | |
| 309 | out_vol = (min_vol - desc->min) / desc->step; |
| 310 | |
| 311 | if (desc->min + desc->step * out_vol > max_vol) |
| 312 | return -EINVAL; |
| 313 | |
| 314 | return out_vol; |
| 315 | } |
| 316 | |
| 317 | static int s5m8767_set_voltage(struct regulator_dev *rdev, |
| 318 | int min_uV, int max_uV, unsigned *selector) |
| 319 | { |
| 320 | struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev); |
| 321 | int min_vol = min_uV, max_vol = max_uV; |
| 322 | const struct s5m_voltage_desc *desc; |
Axel Lin | 20a14b8 | 2012-01-31 15:13:31 +0800 | [diff] [blame] | 323 | int reg_id = rdev_get_id(rdev); |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 324 | int reg, mask, ret; |
| 325 | int i; |
| 326 | u8 val; |
| 327 | |
| 328 | switch (reg_id) { |
| 329 | case S5M8767_LDO1 ... S5M8767_LDO28: |
| 330 | mask = 0x3f; |
| 331 | break; |
| 332 | case S5M8767_BUCK1 ... S5M8767_BUCK6: |
| 333 | mask = 0xff; |
| 334 | break; |
| 335 | case S5M8767_BUCK7 ... S5M8767_BUCK8: |
| 336 | return -EINVAL; |
| 337 | case S5M8767_BUCK9: |
| 338 | mask = 0xff; |
| 339 | break; |
| 340 | default: |
| 341 | return -EINVAL; |
| 342 | } |
| 343 | |
| 344 | desc = reg_voltage_map[reg_id]; |
| 345 | |
| 346 | i = s5m8767_convert_voltage(desc, min_vol, max_vol); |
| 347 | if (i < 0) |
| 348 | return i; |
| 349 | |
| 350 | ret = s5m8767_get_voltage_register(rdev, ®); |
| 351 | if (ret) |
| 352 | return ret; |
| 353 | |
| 354 | s5m_reg_read(s5m8767->iodev, reg, &val); |
| 355 | val = val & mask; |
| 356 | |
| 357 | ret = s5m_reg_write(s5m8767->iodev, reg, val); |
| 358 | *selector = i; |
| 359 | |
| 360 | return ret; |
| 361 | } |
| 362 | |
| 363 | static inline void s5m8767_set_high(struct s5m8767_info *s5m8767) |
| 364 | { |
| 365 | int temp_index = s5m8767->buck_gpioindex; |
| 366 | |
| 367 | gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1); |
| 368 | gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1); |
| 369 | gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1); |
| 370 | } |
| 371 | |
| 372 | static inline void s5m8767_set_low(struct s5m8767_info *s5m8767) |
| 373 | { |
| 374 | int temp_index = s5m8767->buck_gpioindex; |
| 375 | |
| 376 | gpio_set_value(s5m8767->buck_gpios[2], temp_index & 0x1); |
| 377 | gpio_set_value(s5m8767->buck_gpios[1], (temp_index >> 1) & 0x1); |
| 378 | gpio_set_value(s5m8767->buck_gpios[0], (temp_index >> 2) & 0x1); |
| 379 | } |
| 380 | |
| 381 | static int s5m8767_set_voltage_buck(struct regulator_dev *rdev, |
| 382 | int min_uV, int max_uV, unsigned *selector) |
| 383 | { |
| 384 | struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev); |
Axel Lin | 20a14b8 | 2012-01-31 15:13:31 +0800 | [diff] [blame] | 385 | int reg_id = rdev_get_id(rdev); |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 386 | const struct s5m_voltage_desc *desc; |
| 387 | int new_val, old_val, i = 0; |
| 388 | int min_vol = min_uV, max_vol = max_uV; |
| 389 | |
| 390 | if (reg_id < S5M8767_BUCK1 || reg_id > S5M8767_BUCK6) |
| 391 | return -EINVAL; |
| 392 | |
| 393 | switch (reg_id) { |
| 394 | case S5M8767_BUCK1: |
| 395 | return s5m8767_set_voltage(rdev, min_uV, max_uV, selector); |
| 396 | case S5M8767_BUCK2 ... S5M8767_BUCK4: |
| 397 | break; |
| 398 | case S5M8767_BUCK5 ... S5M8767_BUCK6: |
| 399 | return s5m8767_set_voltage(rdev, min_uV, max_uV, selector); |
| 400 | case S5M8767_BUCK9: |
| 401 | return s5m8767_set_voltage(rdev, min_uV, max_uV, selector); |
| 402 | } |
| 403 | |
| 404 | desc = reg_voltage_map[reg_id]; |
| 405 | new_val = s5m8767_convert_voltage(desc, min_vol, max_vol); |
| 406 | if (new_val < 0) |
| 407 | return new_val; |
| 408 | |
| 409 | switch (reg_id) { |
| 410 | case S5M8767_BUCK2: |
| 411 | if (s5m8767->buck2_gpiodvs) { |
| 412 | while (s5m8767->buck2_vol[i] != new_val) |
| 413 | i++; |
| 414 | } else |
| 415 | return s5m8767_set_voltage(rdev, min_uV, |
| 416 | max_uV, selector); |
| 417 | break; |
| 418 | case S5M8767_BUCK3: |
| 419 | if (s5m8767->buck3_gpiodvs) { |
| 420 | while (s5m8767->buck3_vol[i] != new_val) |
| 421 | i++; |
| 422 | } else |
| 423 | return s5m8767_set_voltage(rdev, min_uV, |
| 424 | max_uV, selector); |
| 425 | break; |
| 426 | case S5M8767_BUCK4: |
| 427 | if (s5m8767->buck3_gpiodvs) { |
| 428 | while (s5m8767->buck4_vol[i] != new_val) |
| 429 | i++; |
| 430 | } else |
| 431 | return s5m8767_set_voltage(rdev, min_uV, |
| 432 | max_uV, selector); |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | old_val = s5m8767->buck_gpioindex; |
| 437 | s5m8767->buck_gpioindex = i; |
| 438 | |
| 439 | if (i > old_val) |
| 440 | s5m8767_set_high(s5m8767); |
| 441 | else |
| 442 | s5m8767_set_low(s5m8767); |
| 443 | |
| 444 | *selector = new_val; |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static int s5m8767_set_voltage_time_sel(struct regulator_dev *rdev, |
| 449 | unsigned int old_sel, |
| 450 | unsigned int new_sel) |
| 451 | { |
| 452 | struct s5m8767_info *s5m8767 = rdev_get_drvdata(rdev); |
| 453 | const struct s5m_voltage_desc *desc; |
Axel Lin | 20a14b8 | 2012-01-31 15:13:31 +0800 | [diff] [blame] | 454 | int reg_id = rdev_get_id(rdev); |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 455 | int mask; |
| 456 | int new_val, old_val; |
| 457 | |
| 458 | switch (reg_id) { |
| 459 | case S5M8767_LDO1 ... S5M8767_LDO28: |
| 460 | mask = 0x3f; |
| 461 | break; |
| 462 | case S5M8767_BUCK1 ... S5M8767_BUCK6: |
| 463 | mask = 0xff; |
| 464 | break; |
| 465 | case S5M8767_BUCK7 ... S5M8767_BUCK8: |
| 466 | return -EINVAL; |
| 467 | case S5M8767_BUCK9: |
| 468 | mask = 0xff; |
| 469 | break; |
| 470 | default: |
| 471 | return -EINVAL; |
| 472 | } |
| 473 | desc = reg_voltage_map[reg_id]; |
| 474 | |
| 475 | new_val = s5m8767_convert_voltage(desc, new_sel, new_sel); |
| 476 | if (new_val < 0) |
| 477 | return new_val; |
| 478 | |
| 479 | old_val = s5m8767_convert_voltage(desc, old_sel, old_sel); |
| 480 | if (old_val < 0) |
| 481 | return old_val; |
| 482 | |
| 483 | if (old_sel < new_sel) |
| 484 | return DIV_ROUND_UP(desc->step * (new_val - old_val), |
| 485 | s5m8767->ramp_delay); |
| 486 | else |
| 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static struct regulator_ops s5m8767_ldo_ops = { |
| 491 | .list_voltage = s5m8767_list_voltage, |
| 492 | .is_enabled = s5m8767_reg_is_enabled, |
| 493 | .enable = s5m8767_reg_enable, |
| 494 | .disable = s5m8767_reg_disable, |
| 495 | .get_voltage_sel = s5m8767_get_voltage_sel, |
| 496 | .set_voltage = s5m8767_set_voltage, |
| 497 | .set_voltage_time_sel = s5m8767_set_voltage_time_sel, |
| 498 | }; |
| 499 | |
| 500 | static struct regulator_ops s5m8767_buck_ops = { |
| 501 | .list_voltage = s5m8767_list_voltage, |
| 502 | .is_enabled = s5m8767_reg_is_enabled, |
| 503 | .enable = s5m8767_reg_enable, |
| 504 | .disable = s5m8767_reg_disable, |
| 505 | .get_voltage_sel = s5m8767_get_voltage_sel, |
| 506 | .set_voltage = s5m8767_set_voltage_buck, |
| 507 | .set_voltage_time_sel = s5m8767_set_voltage_time_sel, |
| 508 | }; |
| 509 | |
| 510 | #define regulator_desc_ldo(num) { \ |
| 511 | .name = "LDO"#num, \ |
| 512 | .id = S5M8767_LDO##num, \ |
| 513 | .ops = &s5m8767_ldo_ops, \ |
| 514 | .type = REGULATOR_VOLTAGE, \ |
| 515 | .owner = THIS_MODULE, \ |
| 516 | } |
| 517 | #define regulator_desc_buck(num) { \ |
| 518 | .name = "BUCK"#num, \ |
| 519 | .id = S5M8767_BUCK##num, \ |
| 520 | .ops = &s5m8767_buck_ops, \ |
| 521 | .type = REGULATOR_VOLTAGE, \ |
| 522 | .owner = THIS_MODULE, \ |
| 523 | } |
| 524 | |
| 525 | static struct regulator_desc regulators[] = { |
| 526 | regulator_desc_ldo(1), |
| 527 | regulator_desc_ldo(2), |
| 528 | regulator_desc_ldo(3), |
| 529 | regulator_desc_ldo(4), |
| 530 | regulator_desc_ldo(5), |
| 531 | regulator_desc_ldo(6), |
| 532 | regulator_desc_ldo(7), |
| 533 | regulator_desc_ldo(8), |
| 534 | regulator_desc_ldo(9), |
| 535 | regulator_desc_ldo(10), |
| 536 | regulator_desc_ldo(11), |
| 537 | regulator_desc_ldo(12), |
| 538 | regulator_desc_ldo(13), |
| 539 | regulator_desc_ldo(14), |
| 540 | regulator_desc_ldo(15), |
| 541 | regulator_desc_ldo(16), |
| 542 | regulator_desc_ldo(17), |
| 543 | regulator_desc_ldo(18), |
| 544 | regulator_desc_ldo(19), |
| 545 | regulator_desc_ldo(20), |
| 546 | regulator_desc_ldo(21), |
| 547 | regulator_desc_ldo(22), |
| 548 | regulator_desc_ldo(23), |
| 549 | regulator_desc_ldo(24), |
| 550 | regulator_desc_ldo(25), |
| 551 | regulator_desc_ldo(26), |
| 552 | regulator_desc_ldo(27), |
| 553 | regulator_desc_ldo(28), |
| 554 | regulator_desc_buck(1), |
| 555 | regulator_desc_buck(2), |
| 556 | regulator_desc_buck(3), |
| 557 | regulator_desc_buck(4), |
| 558 | regulator_desc_buck(5), |
| 559 | regulator_desc_buck(6), |
| 560 | regulator_desc_buck(7), |
| 561 | regulator_desc_buck(8), |
| 562 | regulator_desc_buck(9), |
| 563 | }; |
| 564 | |
| 565 | static __devinit int s5m8767_pmic_probe(struct platform_device *pdev) |
| 566 | { |
| 567 | struct s5m87xx_dev *iodev = dev_get_drvdata(pdev->dev.parent); |
| 568 | struct s5m_platform_data *pdata = dev_get_platdata(iodev->dev); |
| 569 | struct regulator_dev **rdev; |
| 570 | struct s5m8767_info *s5m8767; |
| 571 | struct i2c_client *i2c; |
| 572 | int i, ret, size, reg; |
| 573 | |
| 574 | if (!pdata) { |
| 575 | dev_err(pdev->dev.parent, "Platform data not supplied\n"); |
| 576 | return -ENODEV; |
| 577 | } |
| 578 | |
| 579 | s5m8767 = devm_kzalloc(&pdev->dev, sizeof(struct s5m8767_info), |
| 580 | GFP_KERNEL); |
| 581 | if (!s5m8767) |
| 582 | return -ENOMEM; |
| 583 | |
| 584 | size = sizeof(struct regulator_dev *) * (S5M8767_REG_MAX - 2); |
| 585 | s5m8767->rdev = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); |
| 586 | if (!s5m8767->rdev) |
| 587 | return -ENOMEM; |
| 588 | |
| 589 | rdev = s5m8767->rdev; |
| 590 | s5m8767->dev = &pdev->dev; |
| 591 | s5m8767->iodev = iodev; |
| 592 | s5m8767->num_regulators = S5M8767_REG_MAX - 2; |
| 593 | platform_set_drvdata(pdev, s5m8767); |
| 594 | i2c = s5m8767->iodev->i2c; |
| 595 | |
| 596 | s5m8767->buck_gpioindex = pdata->buck_default_idx; |
| 597 | s5m8767->buck2_gpiodvs = pdata->buck2_gpiodvs; |
| 598 | s5m8767->buck3_gpiodvs = pdata->buck3_gpiodvs; |
| 599 | s5m8767->buck4_gpiodvs = pdata->buck4_gpiodvs; |
| 600 | s5m8767->buck_gpios[0] = pdata->buck_gpios[0]; |
| 601 | s5m8767->buck_gpios[1] = pdata->buck_gpios[1]; |
| 602 | s5m8767->buck_gpios[2] = pdata->buck_gpios[2]; |
| 603 | s5m8767->ramp_delay = pdata->buck_ramp_delay; |
| 604 | s5m8767->buck2_ramp = pdata->buck2_ramp_enable; |
| 605 | s5m8767->buck3_ramp = pdata->buck3_ramp_enable; |
| 606 | s5m8767->buck4_ramp = pdata->buck4_ramp_enable; |
| 607 | |
| 608 | for (i = 0; i < 8; i++) { |
| 609 | if (s5m8767->buck2_gpiodvs) { |
| 610 | s5m8767->buck2_vol[i] = |
| 611 | s5m8767_convert_voltage( |
| 612 | &buck_voltage_val2, |
| 613 | pdata->buck2_voltage[i], |
| 614 | pdata->buck2_voltage[i] + |
| 615 | buck_voltage_val2.step); |
| 616 | } |
| 617 | |
| 618 | if (s5m8767->buck3_gpiodvs) { |
| 619 | s5m8767->buck3_vol[i] = |
| 620 | s5m8767_convert_voltage( |
| 621 | &buck_voltage_val2, |
| 622 | pdata->buck3_voltage[i], |
| 623 | pdata->buck3_voltage[i] + |
| 624 | buck_voltage_val2.step); |
| 625 | } |
| 626 | |
| 627 | if (s5m8767->buck4_gpiodvs) { |
| 628 | s5m8767->buck4_vol[i] = |
| 629 | s5m8767_convert_voltage( |
| 630 | &buck_voltage_val2, |
| 631 | pdata->buck4_voltage[i], |
| 632 | pdata->buck4_voltage[i] + |
| 633 | buck_voltage_val2.step); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs || |
| 638 | pdata->buck4_gpiodvs) { |
| 639 | if (gpio_is_valid(pdata->buck_gpios[0]) && |
| 640 | gpio_is_valid(pdata->buck_gpios[1]) && |
| 641 | gpio_is_valid(pdata->buck_gpios[2])) { |
| 642 | ret = gpio_request(pdata->buck_gpios[0], |
| 643 | "S5M8767 SET1"); |
| 644 | if (ret == -EBUSY) |
| 645 | dev_warn(&pdev->dev, "Duplicated gpio request for SET1\n"); |
| 646 | |
| 647 | ret = gpio_request(pdata->buck_gpios[1], |
| 648 | "S5M8767 SET2"); |
| 649 | if (ret == -EBUSY) |
| 650 | dev_warn(&pdev->dev, "Duplicated gpio request for SET2\n"); |
| 651 | |
| 652 | ret = gpio_request(pdata->buck_gpios[2], |
| 653 | "S5M8767 SET3"); |
| 654 | if (ret == -EBUSY) |
| 655 | dev_warn(&pdev->dev, "Duplicated gpio request for SET3\n"); |
| 656 | /* SET1 GPIO */ |
| 657 | gpio_direction_output(pdata->buck_gpios[0], |
| 658 | (s5m8767->buck_gpioindex >> 2) & 0x1); |
| 659 | /* SET2 GPIO */ |
| 660 | gpio_direction_output(pdata->buck_gpios[1], |
| 661 | (s5m8767->buck_gpioindex >> 1) & 0x1); |
| 662 | /* SET3 GPIO */ |
| 663 | gpio_direction_output(pdata->buck_gpios[2], |
| 664 | (s5m8767->buck_gpioindex >> 0) & 0x1); |
| 665 | ret = 0; |
| 666 | } else { |
| 667 | dev_err(&pdev->dev, "GPIO NOT VALID\n"); |
| 668 | ret = -EINVAL; |
| 669 | return ret; |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | if (pdata->buck2_gpiodvs) { |
| 674 | if (pdata->buck3_gpiodvs || pdata->buck4_gpiodvs) { |
| 675 | dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n"); |
| 676 | ret = -EINVAL; |
| 677 | return ret; |
| 678 | } |
| 679 | } |
| 680 | |
| 681 | if (pdata->buck3_gpiodvs) { |
| 682 | if (pdata->buck2_gpiodvs || pdata->buck4_gpiodvs) { |
| 683 | dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n"); |
| 684 | ret = -EINVAL; |
| 685 | return ret; |
| 686 | } |
| 687 | } |
| 688 | |
| 689 | if (pdata->buck4_gpiodvs) { |
| 690 | if (pdata->buck2_gpiodvs || pdata->buck3_gpiodvs) { |
| 691 | dev_err(&pdev->dev, "S5M8767 GPIO DVS NOT VALID\n"); |
| 692 | ret = -EINVAL; |
| 693 | return ret; |
| 694 | } |
| 695 | } |
| 696 | |
| 697 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL, |
| 698 | (pdata->buck2_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1); |
| 699 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL, |
| 700 | (pdata->buck3_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1); |
| 701 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL, |
| 702 | (pdata->buck4_gpiodvs) ? (1 << 1) : (0 << 1), 1 << 1); |
| 703 | |
| 704 | /* Initialize GPIO DVS registers */ |
| 705 | for (i = 0; i < 8; i++) { |
| 706 | if (s5m8767->buck2_gpiodvs) { |
| 707 | s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK2DVS1 + i, |
| 708 | s5m8767->buck2_vol[i]); |
| 709 | } |
| 710 | |
| 711 | if (s5m8767->buck3_gpiodvs) { |
| 712 | s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK3DVS1 + i, |
| 713 | s5m8767->buck3_vol[i]); |
| 714 | } |
| 715 | |
| 716 | if (s5m8767->buck4_gpiodvs) { |
| 717 | s5m_reg_write(s5m8767->iodev, S5M8767_REG_BUCK4DVS1 + i, |
| 718 | s5m8767->buck4_vol[i]); |
| 719 | } |
| 720 | } |
| 721 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK2CTRL, 0x78, 0xff); |
| 722 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK3CTRL, 0x58, 0xff); |
| 723 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_BUCK4CTRL, 0x78, 0xff); |
| 724 | |
| 725 | if (s5m8767->buck2_ramp) |
| 726 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x08, 0x08); |
| 727 | |
| 728 | if (s5m8767->buck3_ramp) |
| 729 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x04, 0x04); |
| 730 | |
| 731 | if (s5m8767->buck4_ramp) |
| 732 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, 0x02, 0x02); |
| 733 | |
| 734 | if (s5m8767->buck2_ramp || s5m8767->buck3_ramp |
| 735 | || s5m8767->buck4_ramp) { |
| 736 | switch (s5m8767->ramp_delay) { |
| 737 | case 15: |
| 738 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, |
| 739 | 0xc0, 0xf0); |
Axel Lin | 047ec22 | 2012-01-12 14:57:09 +0800 | [diff] [blame] | 740 | break; |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 741 | case 25: |
| 742 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, |
| 743 | 0xd0, 0xf0); |
Axel Lin | 047ec22 | 2012-01-12 14:57:09 +0800 | [diff] [blame] | 744 | break; |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 745 | case 50: |
| 746 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, |
| 747 | 0xe0, 0xf0); |
Axel Lin | 047ec22 | 2012-01-12 14:57:09 +0800 | [diff] [blame] | 748 | break; |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 749 | case 100: |
| 750 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, |
| 751 | 0xf0, 0xf0); |
Axel Lin | 047ec22 | 2012-01-12 14:57:09 +0800 | [diff] [blame] | 752 | break; |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 753 | default: |
| 754 | s5m_reg_update(s5m8767->iodev, S5M8767_REG_DVSRAMP, |
| 755 | 0x90, 0xf0); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | for (i = 0; i < pdata->num_regulators; i++) { |
| 760 | const struct s5m_voltage_desc *desc; |
| 761 | int id = pdata->regulators[i].id; |
| 762 | |
| 763 | desc = reg_voltage_map[id]; |
| 764 | if (desc) |
| 765 | regulators[id].n_voltages = |
| 766 | (desc->max - desc->min) / desc->step + 1; |
| 767 | |
| 768 | rdev[i] = regulator_register(®ulators[id], s5m8767->dev, |
Mark Brown | 0ce6987 | 2012-01-17 11:25:45 +0000 | [diff] [blame] | 769 | pdata->regulators[i].initdata, s5m8767, NULL); |
Sangbeom Kim | 9767ec7 | 2012-01-09 19:10:25 +0900 | [diff] [blame] | 770 | if (IS_ERR(rdev[i])) { |
| 771 | ret = PTR_ERR(rdev[i]); |
| 772 | dev_err(s5m8767->dev, "regulator init failed for %d\n", |
| 773 | id); |
| 774 | rdev[i] = NULL; |
| 775 | goto err; |
| 776 | } |
| 777 | } |
| 778 | |
| 779 | return 0; |
| 780 | err: |
| 781 | for (i = 0; i < s5m8767->num_regulators; i++) |
| 782 | if (rdev[i]) |
| 783 | regulator_unregister(rdev[i]); |
| 784 | |
| 785 | return ret; |
| 786 | } |
| 787 | |
| 788 | static int __devexit s5m8767_pmic_remove(struct platform_device *pdev) |
| 789 | { |
| 790 | struct s5m8767_info *s5m8767 = platform_get_drvdata(pdev); |
| 791 | struct regulator_dev **rdev = s5m8767->rdev; |
| 792 | int i; |
| 793 | |
| 794 | for (i = 0; i < s5m8767->num_regulators; i++) |
| 795 | if (rdev[i]) |
| 796 | regulator_unregister(rdev[i]); |
| 797 | |
| 798 | return 0; |
| 799 | } |
| 800 | |
| 801 | static const struct platform_device_id s5m8767_pmic_id[] = { |
| 802 | { "s5m8767-pmic", 0}, |
| 803 | { }, |
| 804 | }; |
| 805 | MODULE_DEVICE_TABLE(platform, s5m8767_pmic_id); |
| 806 | |
| 807 | static struct platform_driver s5m8767_pmic_driver = { |
| 808 | .driver = { |
| 809 | .name = "s5m8767-pmic", |
| 810 | .owner = THIS_MODULE, |
| 811 | }, |
| 812 | .probe = s5m8767_pmic_probe, |
| 813 | .remove = __devexit_p(s5m8767_pmic_remove), |
| 814 | .id_table = s5m8767_pmic_id, |
| 815 | }; |
| 816 | |
| 817 | static int __init s5m8767_pmic_init(void) |
| 818 | { |
| 819 | return platform_driver_register(&s5m8767_pmic_driver); |
| 820 | } |
| 821 | subsys_initcall(s5m8767_pmic_init); |
| 822 | |
| 823 | static void __exit s5m8767_pmic_exit(void) |
| 824 | { |
| 825 | platform_driver_unregister(&s5m8767_pmic_driver); |
| 826 | } |
| 827 | module_exit(s5m8767_pmic_exit); |
| 828 | |
| 829 | /* Module information */ |
| 830 | MODULE_AUTHOR("Sangbeom Kim <sbkim73@samsung.com>"); |
| 831 | MODULE_DESCRIPTION("SAMSUNG S5M8767 Regulator Driver"); |
| 832 | MODULE_LICENSE("GPL"); |