Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 1 | /* |
| 2 | * max8998.c - Voltage regulator driver for the Maxim 8998 |
| 3 | * |
| 4 | * Copyright (C) 2009-2010 Samsung Electronics |
| 5 | * Kyungmin Park <kyungmin.park@samsung.com> |
| 6 | * Marek Szyprowski <m.szyprowski@samsung.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | */ |
| 22 | |
| 23 | #include <linux/module.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/i2c.h> |
| 26 | #include <linux/err.h> |
| 27 | #include <linux/gpio.h> |
| 28 | #include <linux/slab.h> |
| 29 | #include <linux/interrupt.h> |
| 30 | #include <linux/mutex.h> |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 31 | #include <linux/of.h> |
| 32 | #include <linux/of_gpio.h> |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 33 | #include <linux/platform_device.h> |
| 34 | #include <linux/regulator/driver.h> |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 35 | #include <linux/regulator/of_regulator.h> |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 36 | #include <linux/mfd/max8998.h> |
| 37 | #include <linux/mfd/max8998-private.h> |
| 38 | |
| 39 | struct max8998_data { |
| 40 | struct device *dev; |
| 41 | struct max8998_dev *iodev; |
| 42 | int num_regulators; |
Lukasz Majewski | 889cd5a | 2010-09-27 14:32:25 +0200 | [diff] [blame] | 43 | u8 buck1_vol[4]; /* voltages for selection */ |
| 44 | u8 buck2_vol[2]; |
| 45 | unsigned int buck1_idx; /* index to last changed voltage */ |
| 46 | /* value in a set */ |
| 47 | unsigned int buck2_idx; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | struct voltage_map_desc { |
| 51 | int min; |
| 52 | int max; |
| 53 | int step; |
| 54 | }; |
| 55 | |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 56 | /* Voltage maps in uV*/ |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 57 | static const struct voltage_map_desc ldo23_voltage_map_desc = { |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 58 | .min = 800000, .step = 50000, .max = 1300000, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 59 | }; |
| 60 | static const struct voltage_map_desc ldo456711_voltage_map_desc = { |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 61 | .min = 1600000, .step = 100000, .max = 3600000, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 62 | }; |
| 63 | static const struct voltage_map_desc ldo8_voltage_map_desc = { |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 64 | .min = 3000000, .step = 100000, .max = 3600000, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 65 | }; |
| 66 | static const struct voltage_map_desc ldo9_voltage_map_desc = { |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 67 | .min = 2800000, .step = 100000, .max = 3100000, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 68 | }; |
| 69 | static const struct voltage_map_desc ldo10_voltage_map_desc = { |
Marek Szyprowski | 36a6f8c | 2013-01-25 09:25:40 +0100 | [diff] [blame] | 70 | .min = 950000, .step = 50000, .max = 1300000, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 71 | }; |
| 72 | static const struct voltage_map_desc ldo1213_voltage_map_desc = { |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 73 | .min = 800000, .step = 100000, .max = 3300000, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 74 | }; |
| 75 | static const struct voltage_map_desc ldo1415_voltage_map_desc = { |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 76 | .min = 1200000, .step = 100000, .max = 3300000, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 77 | }; |
| 78 | static const struct voltage_map_desc ldo1617_voltage_map_desc = { |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 79 | .min = 1600000, .step = 100000, .max = 3600000, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 80 | }; |
| 81 | static const struct voltage_map_desc buck12_voltage_map_desc = { |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 82 | .min = 750000, .step = 25000, .max = 1525000, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 83 | }; |
| 84 | static const struct voltage_map_desc buck3_voltage_map_desc = { |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 85 | .min = 1600000, .step = 100000, .max = 3600000, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 86 | }; |
| 87 | static const struct voltage_map_desc buck4_voltage_map_desc = { |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 88 | .min = 800000, .step = 100000, .max = 2300000, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 89 | }; |
| 90 | |
| 91 | static const struct voltage_map_desc *ldo_voltage_map[] = { |
| 92 | NULL, |
| 93 | NULL, |
| 94 | &ldo23_voltage_map_desc, /* LDO2 */ |
| 95 | &ldo23_voltage_map_desc, /* LDO3 */ |
| 96 | &ldo456711_voltage_map_desc, /* LDO4 */ |
| 97 | &ldo456711_voltage_map_desc, /* LDO5 */ |
| 98 | &ldo456711_voltage_map_desc, /* LDO6 */ |
| 99 | &ldo456711_voltage_map_desc, /* LDO7 */ |
| 100 | &ldo8_voltage_map_desc, /* LDO8 */ |
| 101 | &ldo9_voltage_map_desc, /* LDO9 */ |
| 102 | &ldo10_voltage_map_desc, /* LDO10 */ |
| 103 | &ldo456711_voltage_map_desc, /* LDO11 */ |
| 104 | &ldo1213_voltage_map_desc, /* LDO12 */ |
| 105 | &ldo1213_voltage_map_desc, /* LDO13 */ |
| 106 | &ldo1415_voltage_map_desc, /* LDO14 */ |
| 107 | &ldo1415_voltage_map_desc, /* LDO15 */ |
| 108 | &ldo1617_voltage_map_desc, /* LDO16 */ |
| 109 | &ldo1617_voltage_map_desc, /* LDO17 */ |
| 110 | &buck12_voltage_map_desc, /* BUCK1 */ |
| 111 | &buck12_voltage_map_desc, /* BUCK2 */ |
| 112 | &buck3_voltage_map_desc, /* BUCK3 */ |
| 113 | &buck4_voltage_map_desc, /* BUCK4 */ |
| 114 | }; |
| 115 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 116 | static int max8998_get_enable_register(struct regulator_dev *rdev, |
| 117 | int *reg, int *shift) |
| 118 | { |
Axel Lin | 7b94791 | 2012-03-01 09:27:29 +0800 | [diff] [blame] | 119 | int ldo = rdev_get_id(rdev); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 120 | |
| 121 | switch (ldo) { |
| 122 | case MAX8998_LDO2 ... MAX8998_LDO5: |
| 123 | *reg = MAX8998_REG_ONOFF1; |
| 124 | *shift = 3 - (ldo - MAX8998_LDO2); |
| 125 | break; |
| 126 | case MAX8998_LDO6 ... MAX8998_LDO13: |
| 127 | *reg = MAX8998_REG_ONOFF2; |
| 128 | *shift = 7 - (ldo - MAX8998_LDO6); |
| 129 | break; |
| 130 | case MAX8998_LDO14 ... MAX8998_LDO17: |
| 131 | *reg = MAX8998_REG_ONOFF3; |
| 132 | *shift = 7 - (ldo - MAX8998_LDO14); |
| 133 | break; |
| 134 | case MAX8998_BUCK1 ... MAX8998_BUCK4: |
| 135 | *reg = MAX8998_REG_ONOFF1; |
| 136 | *shift = 7 - (ldo - MAX8998_BUCK1); |
| 137 | break; |
| 138 | case MAX8998_EN32KHZ_AP ... MAX8998_ENVICHG: |
| 139 | *reg = MAX8998_REG_ONOFF4; |
| 140 | *shift = 7 - (ldo - MAX8998_EN32KHZ_AP); |
| 141 | break; |
| 142 | case MAX8998_ESAFEOUT1 ... MAX8998_ESAFEOUT2: |
| 143 | *reg = MAX8998_REG_CHGR2; |
| 144 | *shift = 7 - (ldo - MAX8998_ESAFEOUT1); |
| 145 | break; |
| 146 | default: |
| 147 | return -EINVAL; |
| 148 | } |
| 149 | |
| 150 | return 0; |
| 151 | } |
| 152 | |
| 153 | static int max8998_ldo_is_enabled(struct regulator_dev *rdev) |
| 154 | { |
| 155 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 156 | struct i2c_client *i2c = max8998->iodev->i2c; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 157 | int ret, reg, shift = 8; |
| 158 | u8 val; |
| 159 | |
| 160 | ret = max8998_get_enable_register(rdev, ®, &shift); |
| 161 | if (ret) |
| 162 | return ret; |
| 163 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 164 | ret = max8998_read_reg(i2c, reg, &val); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 165 | if (ret) |
| 166 | return ret; |
| 167 | |
| 168 | return val & (1 << shift); |
| 169 | } |
| 170 | |
| 171 | static int max8998_ldo_enable(struct regulator_dev *rdev) |
| 172 | { |
| 173 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 174 | struct i2c_client *i2c = max8998->iodev->i2c; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 175 | int reg, shift = 8, ret; |
| 176 | |
| 177 | ret = max8998_get_enable_register(rdev, ®, &shift); |
| 178 | if (ret) |
| 179 | return ret; |
| 180 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 181 | return max8998_update_reg(i2c, reg, 1<<shift, 1<<shift); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | static int max8998_ldo_disable(struct regulator_dev *rdev) |
| 185 | { |
| 186 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 187 | struct i2c_client *i2c = max8998->iodev->i2c; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 188 | int reg, shift = 8, ret; |
| 189 | |
| 190 | ret = max8998_get_enable_register(rdev, ®, &shift); |
| 191 | if (ret) |
| 192 | return ret; |
| 193 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 194 | return max8998_update_reg(i2c, reg, 0, 1<<shift); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | static int max8998_get_voltage_register(struct regulator_dev *rdev, |
| 198 | int *_reg, int *_shift, int *_mask) |
| 199 | { |
Axel Lin | 7b94791 | 2012-03-01 09:27:29 +0800 | [diff] [blame] | 200 | int ldo = rdev_get_id(rdev); |
Lukasz Majewski | 889cd5a | 2010-09-27 14:32:25 +0200 | [diff] [blame] | 201 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 202 | int reg, shift = 0, mask = 0xff; |
| 203 | |
| 204 | switch (ldo) { |
| 205 | case MAX8998_LDO2 ... MAX8998_LDO3: |
| 206 | reg = MAX8998_REG_LDO2_LDO3; |
| 207 | mask = 0xf; |
| 208 | if (ldo == MAX8998_LDO2) |
| 209 | shift = 4; |
| 210 | else |
| 211 | shift = 0; |
| 212 | break; |
| 213 | case MAX8998_LDO4 ... MAX8998_LDO7: |
| 214 | reg = MAX8998_REG_LDO4 + (ldo - MAX8998_LDO4); |
| 215 | break; |
| 216 | case MAX8998_LDO8 ... MAX8998_LDO9: |
| 217 | reg = MAX8998_REG_LDO8_LDO9; |
| 218 | mask = 0xf; |
| 219 | if (ldo == MAX8998_LDO8) |
| 220 | shift = 4; |
| 221 | else |
| 222 | shift = 0; |
| 223 | break; |
| 224 | case MAX8998_LDO10 ... MAX8998_LDO11: |
| 225 | reg = MAX8998_REG_LDO10_LDO11; |
| 226 | if (ldo == MAX8998_LDO10) { |
| 227 | shift = 5; |
| 228 | mask = 0x7; |
| 229 | } else { |
| 230 | shift = 0; |
| 231 | mask = 0x1f; |
| 232 | } |
| 233 | break; |
| 234 | case MAX8998_LDO12 ... MAX8998_LDO17: |
| 235 | reg = MAX8998_REG_LDO12 + (ldo - MAX8998_LDO12); |
| 236 | break; |
| 237 | case MAX8998_BUCK1: |
Lukasz Majewski | 889cd5a | 2010-09-27 14:32:25 +0200 | [diff] [blame] | 238 | reg = MAX8998_REG_BUCK1_VOLTAGE1 + max8998->buck1_idx; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 239 | break; |
| 240 | case MAX8998_BUCK2: |
Lukasz Majewski | 889cd5a | 2010-09-27 14:32:25 +0200 | [diff] [blame] | 241 | reg = MAX8998_REG_BUCK2_VOLTAGE1 + max8998->buck2_idx; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 242 | break; |
| 243 | case MAX8998_BUCK3: |
| 244 | reg = MAX8998_REG_BUCK3; |
| 245 | break; |
| 246 | case MAX8998_BUCK4: |
| 247 | reg = MAX8998_REG_BUCK4; |
| 248 | break; |
| 249 | default: |
| 250 | return -EINVAL; |
| 251 | } |
| 252 | |
| 253 | *_reg = reg; |
| 254 | *_shift = shift; |
| 255 | *_mask = mask; |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
Axel Lin | 7b4354b | 2012-04-11 13:58:09 +0800 | [diff] [blame] | 260 | static int max8998_get_voltage_sel(struct regulator_dev *rdev) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 261 | { |
| 262 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 263 | struct i2c_client *i2c = max8998->iodev->i2c; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 264 | int reg, shift = 0, mask, ret; |
| 265 | u8 val; |
| 266 | |
| 267 | ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); |
| 268 | if (ret) |
| 269 | return ret; |
| 270 | |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 271 | ret = max8998_read_reg(i2c, reg, &val); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 272 | if (ret) |
| 273 | return ret; |
| 274 | |
| 275 | val >>= shift; |
| 276 | val &= mask; |
| 277 | |
Axel Lin | 7b4354b | 2012-04-11 13:58:09 +0800 | [diff] [blame] | 278 | return val; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 279 | } |
| 280 | |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 281 | static int max8998_set_voltage_ldo_sel(struct regulator_dev *rdev, |
| 282 | unsigned selector) |
Lukasz Majewski | 9d92492 | 2010-09-27 14:32:23 +0200 | [diff] [blame] | 283 | { |
| 284 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 285 | struct i2c_client *i2c = max8998->iodev->i2c; |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 286 | int reg, shift = 0, mask, ret; |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 287 | |
Lukasz Majewski | 9d92492 | 2010-09-27 14:32:23 +0200 | [diff] [blame] | 288 | ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); |
| 289 | if (ret) |
| 290 | return ret; |
| 291 | |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 292 | ret = max8998_update_reg(i2c, reg, selector<<shift, mask<<shift); |
Lukasz Majewski | 9d92492 | 2010-09-27 14:32:23 +0200 | [diff] [blame] | 293 | |
| 294 | return ret; |
| 295 | } |
| 296 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 297 | static inline void buck1_gpio_set(int gpio1, int gpio2, int v) |
| 298 | { |
| 299 | gpio_set_value(gpio1, v & 0x1); |
| 300 | gpio_set_value(gpio2, (v >> 1) & 0x1); |
| 301 | } |
| 302 | |
| 303 | static inline void buck2_gpio_set(int gpio, int v) |
| 304 | { |
| 305 | gpio_set_value(gpio, v & 0x1); |
| 306 | } |
| 307 | |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 308 | static int max8998_set_voltage_buck_sel(struct regulator_dev *rdev, |
| 309 | unsigned selector) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 310 | { |
| 311 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 312 | struct max8998_platform_data *pdata = |
| 313 | dev_get_platdata(max8998->iodev->dev); |
Joonyoung Shim | 676e02d | 2010-08-06 11:28:06 +0900 | [diff] [blame] | 314 | struct i2c_client *i2c = max8998->iodev->i2c; |
Axel Lin | 7b94791 | 2012-03-01 09:27:29 +0800 | [diff] [blame] | 315 | int buck = rdev_get_id(rdev); |
Axel Lin | c6163a7 | 2013-02-06 11:10:51 +0800 | [diff] [blame] | 316 | int reg, shift = 0, mask, ret, j; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 317 | static u8 buck1_last_val; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 318 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 319 | ret = max8998_get_voltage_register(rdev, ®, &shift, &mask); |
| 320 | if (ret) |
| 321 | return ret; |
| 322 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 323 | switch (buck) { |
| 324 | case MAX8998_BUCK1: |
| 325 | dev_dbg(max8998->dev, |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 326 | "BUCK1, selector:%d, buck1_vol1:%d, buck1_vol2:%d\n" |
Joe Perches | 85ee7a1 | 2011-04-23 20:38:19 -0700 | [diff] [blame] | 327 | "buck1_vol3:%d, buck1_vol4:%d\n", |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 328 | selector, max8998->buck1_vol[0], max8998->buck1_vol[1], |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 329 | max8998->buck1_vol[2], max8998->buck1_vol[3]); |
| 330 | |
| 331 | if (gpio_is_valid(pdata->buck1_set1) && |
| 332 | gpio_is_valid(pdata->buck1_set2)) { |
| 333 | |
| 334 | /* check if requested voltage */ |
| 335 | /* value is already defined */ |
| 336 | for (j = 0; j < ARRAY_SIZE(max8998->buck1_vol); j++) { |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 337 | if (max8998->buck1_vol[j] == selector) { |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 338 | max8998->buck1_idx = j; |
| 339 | buck1_gpio_set(pdata->buck1_set1, |
| 340 | pdata->buck1_set2, j); |
| 341 | goto buck1_exit; |
| 342 | } |
| 343 | } |
| 344 | |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 345 | if (pdata->buck_voltage_lock) |
| 346 | return -EINVAL; |
| 347 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 348 | /* no predefine regulator found */ |
| 349 | max8998->buck1_idx = (buck1_last_val % 2) + 2; |
| 350 | dev_dbg(max8998->dev, "max8998->buck1_idx:%d\n", |
| 351 | max8998->buck1_idx); |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 352 | max8998->buck1_vol[max8998->buck1_idx] = selector; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 353 | ret = max8998_get_voltage_register(rdev, ®, |
| 354 | &shift, |
| 355 | &mask); |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 356 | ret = max8998_write_reg(i2c, reg, selector); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 357 | buck1_gpio_set(pdata->buck1_set1, |
| 358 | pdata->buck1_set2, max8998->buck1_idx); |
| 359 | buck1_last_val++; |
| 360 | buck1_exit: |
| 361 | dev_dbg(max8998->dev, "%s: SET1:%d, SET2:%d\n", |
| 362 | i2c->name, gpio_get_value(pdata->buck1_set1), |
| 363 | gpio_get_value(pdata->buck1_set2)); |
| 364 | break; |
| 365 | } else { |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 366 | ret = max8998_write_reg(i2c, reg, selector); |
MyungJoo Ham | c5a4655 | 2010-07-20 08:07:07 +0200 | [diff] [blame] | 367 | } |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 368 | break; |
| 369 | |
| 370 | case MAX8998_BUCK2: |
| 371 | dev_dbg(max8998->dev, |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 372 | "BUCK2, selector:%d buck2_vol1:%d, buck2_vol2:%d\n", |
| 373 | selector, max8998->buck2_vol[0], max8998->buck2_vol[1]); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 374 | if (gpio_is_valid(pdata->buck2_set3)) { |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 375 | |
| 376 | /* check if requested voltage */ |
| 377 | /* value is already defined */ |
| 378 | for (j = 0; j < ARRAY_SIZE(max8998->buck2_vol); j++) { |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 379 | if (max8998->buck2_vol[j] == selector) { |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 380 | max8998->buck2_idx = j; |
| 381 | buck2_gpio_set(pdata->buck2_set3, j); |
| 382 | goto buck2_exit; |
| 383 | } |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 384 | } |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 385 | |
| 386 | if (pdata->buck_voltage_lock) |
| 387 | return -EINVAL; |
| 388 | |
| 389 | max8998_get_voltage_register(rdev, |
| 390 | ®, &shift, &mask); |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 391 | ret = max8998_write_reg(i2c, reg, selector); |
| 392 | max8998->buck2_vol[max8998->buck2_idx] = selector; |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 393 | buck2_gpio_set(pdata->buck2_set3, max8998->buck2_idx); |
| 394 | buck2_exit: |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 395 | dev_dbg(max8998->dev, "%s: SET3:%d\n", i2c->name, |
| 396 | gpio_get_value(pdata->buck2_set3)); |
| 397 | } else { |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 398 | ret = max8998_write_reg(i2c, reg, selector); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 399 | } |
| 400 | break; |
| 401 | |
| 402 | case MAX8998_BUCK3: |
| 403 | case MAX8998_BUCK4: |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 404 | ret = max8998_update_reg(i2c, reg, selector<<shift, |
| 405 | mask<<shift); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 406 | break; |
MyungJoo Ham | c5a4655 | 2010-07-20 08:07:07 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame] | 409 | return ret; |
| 410 | } |
| 411 | |
| 412 | static int max8998_set_voltage_buck_time_sel(struct regulator_dev *rdev, |
| 413 | unsigned int old_selector, |
| 414 | unsigned int new_selector) |
| 415 | { |
| 416 | struct max8998_data *max8998 = rdev_get_drvdata(rdev); |
| 417 | struct i2c_client *i2c = max8998->iodev->i2c; |
| 418 | const struct voltage_map_desc *desc; |
| 419 | int buck = rdev_get_id(rdev); |
| 420 | u8 val = 0; |
| 421 | int difference, ret; |
| 422 | |
| 423 | if (buck < MAX8998_BUCK1 || buck > MAX8998_BUCK4) |
| 424 | return -EINVAL; |
| 425 | |
| 426 | desc = ldo_voltage_map[buck]; |
| 427 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 428 | /* Voltage stabilization */ |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame] | 429 | ret = max8998_read_reg(i2c, MAX8998_REG_ONOFF4, &val); |
| 430 | if (ret) |
| 431 | return ret; |
MyungJoo Ham | c5a4655 | 2010-07-20 08:07:07 +0200 | [diff] [blame] | 432 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 433 | /* lp3974 hasn't got ENRAMP bit - ramp is assumed as true */ |
| 434 | /* MAX8998 has ENRAMP bit implemented, so test it*/ |
| 435 | if (max8998->iodev->type == TYPE_MAX8998 && !(val & MAX8998_ENRAMP)) |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame] | 436 | return 0; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 437 | |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 438 | difference = (new_selector - old_selector) * desc->step / 1000; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 439 | if (difference > 0) |
Axel Lin | 81d0a6a | 2013-01-09 19:34:57 +0800 | [diff] [blame] | 440 | return DIV_ROUND_UP(difference, (val & 0x0f) + 1); |
MyungJoo Ham | c5a4655 | 2010-07-20 08:07:07 +0200 | [diff] [blame] | 441 | |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame] | 442 | return 0; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static struct regulator_ops max8998_ldo_ops = { |
Axel Lin | 9a0fbb6 | 2012-06-17 09:33:07 +0800 | [diff] [blame] | 446 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 447 | .map_voltage = regulator_map_voltage_linear, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 448 | .is_enabled = max8998_ldo_is_enabled, |
| 449 | .enable = max8998_ldo_enable, |
| 450 | .disable = max8998_ldo_disable, |
Axel Lin | 7b4354b | 2012-04-11 13:58:09 +0800 | [diff] [blame] | 451 | .get_voltage_sel = max8998_get_voltage_sel, |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 452 | .set_voltage_sel = max8998_set_voltage_ldo_sel, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 453 | }; |
| 454 | |
| 455 | static struct regulator_ops max8998_buck_ops = { |
Axel Lin | 9a0fbb6 | 2012-06-17 09:33:07 +0800 | [diff] [blame] | 456 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 457 | .map_voltage = regulator_map_voltage_linear, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 458 | .is_enabled = max8998_ldo_is_enabled, |
| 459 | .enable = max8998_ldo_enable, |
| 460 | .disable = max8998_ldo_disable, |
Axel Lin | 7b4354b | 2012-04-11 13:58:09 +0800 | [diff] [blame] | 461 | .get_voltage_sel = max8998_get_voltage_sel, |
Axel Lin | baae019 | 2012-06-17 09:34:29 +0800 | [diff] [blame] | 462 | .set_voltage_sel = max8998_set_voltage_buck_sel, |
Axel Lin | 276909d | 2012-04-11 13:59:14 +0800 | [diff] [blame] | 463 | .set_voltage_time_sel = max8998_set_voltage_buck_time_sel, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 464 | }; |
| 465 | |
| 466 | static struct regulator_ops max8998_others_ops = { |
| 467 | .is_enabled = max8998_ldo_is_enabled, |
| 468 | .enable = max8998_ldo_enable, |
| 469 | .disable = max8998_ldo_disable, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 470 | }; |
| 471 | |
| 472 | static struct regulator_desc regulators[] = { |
| 473 | { |
| 474 | .name = "LDO2", |
| 475 | .id = MAX8998_LDO2, |
| 476 | .ops = &max8998_ldo_ops, |
| 477 | .type = REGULATOR_VOLTAGE, |
| 478 | .owner = THIS_MODULE, |
| 479 | }, { |
| 480 | .name = "LDO3", |
| 481 | .id = MAX8998_LDO3, |
| 482 | .ops = &max8998_ldo_ops, |
| 483 | .type = REGULATOR_VOLTAGE, |
| 484 | .owner = THIS_MODULE, |
| 485 | }, { |
| 486 | .name = "LDO4", |
| 487 | .id = MAX8998_LDO4, |
| 488 | .ops = &max8998_ldo_ops, |
| 489 | .type = REGULATOR_VOLTAGE, |
| 490 | .owner = THIS_MODULE, |
| 491 | }, { |
| 492 | .name = "LDO5", |
| 493 | .id = MAX8998_LDO5, |
| 494 | .ops = &max8998_ldo_ops, |
| 495 | .type = REGULATOR_VOLTAGE, |
| 496 | .owner = THIS_MODULE, |
| 497 | }, { |
| 498 | .name = "LDO6", |
| 499 | .id = MAX8998_LDO6, |
| 500 | .ops = &max8998_ldo_ops, |
| 501 | .type = REGULATOR_VOLTAGE, |
| 502 | .owner = THIS_MODULE, |
| 503 | }, { |
| 504 | .name = "LDO7", |
| 505 | .id = MAX8998_LDO7, |
| 506 | .ops = &max8998_ldo_ops, |
| 507 | .type = REGULATOR_VOLTAGE, |
| 508 | .owner = THIS_MODULE, |
| 509 | }, { |
| 510 | .name = "LDO8", |
| 511 | .id = MAX8998_LDO8, |
| 512 | .ops = &max8998_ldo_ops, |
| 513 | .type = REGULATOR_VOLTAGE, |
| 514 | .owner = THIS_MODULE, |
| 515 | }, { |
| 516 | .name = "LDO9", |
| 517 | .id = MAX8998_LDO9, |
| 518 | .ops = &max8998_ldo_ops, |
| 519 | .type = REGULATOR_VOLTAGE, |
| 520 | .owner = THIS_MODULE, |
| 521 | }, { |
| 522 | .name = "LDO10", |
| 523 | .id = MAX8998_LDO10, |
| 524 | .ops = &max8998_ldo_ops, |
| 525 | .type = REGULATOR_VOLTAGE, |
| 526 | .owner = THIS_MODULE, |
| 527 | }, { |
| 528 | .name = "LDO11", |
| 529 | .id = MAX8998_LDO11, |
| 530 | .ops = &max8998_ldo_ops, |
| 531 | .type = REGULATOR_VOLTAGE, |
| 532 | .owner = THIS_MODULE, |
| 533 | }, { |
| 534 | .name = "LDO12", |
| 535 | .id = MAX8998_LDO12, |
| 536 | .ops = &max8998_ldo_ops, |
| 537 | .type = REGULATOR_VOLTAGE, |
| 538 | .owner = THIS_MODULE, |
| 539 | }, { |
| 540 | .name = "LDO13", |
| 541 | .id = MAX8998_LDO13, |
| 542 | .ops = &max8998_ldo_ops, |
| 543 | .type = REGULATOR_VOLTAGE, |
| 544 | .owner = THIS_MODULE, |
| 545 | }, { |
| 546 | .name = "LDO14", |
| 547 | .id = MAX8998_LDO14, |
| 548 | .ops = &max8998_ldo_ops, |
| 549 | .type = REGULATOR_VOLTAGE, |
| 550 | .owner = THIS_MODULE, |
| 551 | }, { |
| 552 | .name = "LDO15", |
| 553 | .id = MAX8998_LDO15, |
| 554 | .ops = &max8998_ldo_ops, |
| 555 | .type = REGULATOR_VOLTAGE, |
| 556 | .owner = THIS_MODULE, |
| 557 | }, { |
| 558 | .name = "LDO16", |
| 559 | .id = MAX8998_LDO16, |
| 560 | .ops = &max8998_ldo_ops, |
| 561 | .type = REGULATOR_VOLTAGE, |
| 562 | .owner = THIS_MODULE, |
| 563 | }, { |
| 564 | .name = "LDO17", |
| 565 | .id = MAX8998_LDO17, |
| 566 | .ops = &max8998_ldo_ops, |
| 567 | .type = REGULATOR_VOLTAGE, |
| 568 | .owner = THIS_MODULE, |
| 569 | }, { |
| 570 | .name = "BUCK1", |
| 571 | .id = MAX8998_BUCK1, |
| 572 | .ops = &max8998_buck_ops, |
| 573 | .type = REGULATOR_VOLTAGE, |
| 574 | .owner = THIS_MODULE, |
| 575 | }, { |
| 576 | .name = "BUCK2", |
| 577 | .id = MAX8998_BUCK2, |
| 578 | .ops = &max8998_buck_ops, |
| 579 | .type = REGULATOR_VOLTAGE, |
| 580 | .owner = THIS_MODULE, |
| 581 | }, { |
| 582 | .name = "BUCK3", |
| 583 | .id = MAX8998_BUCK3, |
| 584 | .ops = &max8998_buck_ops, |
| 585 | .type = REGULATOR_VOLTAGE, |
| 586 | .owner = THIS_MODULE, |
| 587 | }, { |
| 588 | .name = "BUCK4", |
| 589 | .id = MAX8998_BUCK4, |
| 590 | .ops = &max8998_buck_ops, |
| 591 | .type = REGULATOR_VOLTAGE, |
| 592 | .owner = THIS_MODULE, |
| 593 | }, { |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 594 | .name = "EN32KHz-AP", |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 595 | .id = MAX8998_EN32KHZ_AP, |
| 596 | .ops = &max8998_others_ops, |
| 597 | .type = REGULATOR_VOLTAGE, |
| 598 | .owner = THIS_MODULE, |
| 599 | }, { |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 600 | .name = "EN32KHz-CP", |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 601 | .id = MAX8998_EN32KHZ_CP, |
| 602 | .ops = &max8998_others_ops, |
| 603 | .type = REGULATOR_VOLTAGE, |
| 604 | .owner = THIS_MODULE, |
| 605 | }, { |
| 606 | .name = "ENVICHG", |
| 607 | .id = MAX8998_ENVICHG, |
| 608 | .ops = &max8998_others_ops, |
| 609 | .type = REGULATOR_VOLTAGE, |
| 610 | .owner = THIS_MODULE, |
| 611 | }, { |
| 612 | .name = "ESAFEOUT1", |
| 613 | .id = MAX8998_ESAFEOUT1, |
| 614 | .ops = &max8998_others_ops, |
| 615 | .type = REGULATOR_VOLTAGE, |
| 616 | .owner = THIS_MODULE, |
| 617 | }, { |
| 618 | .name = "ESAFEOUT2", |
| 619 | .id = MAX8998_ESAFEOUT2, |
| 620 | .ops = &max8998_others_ops, |
| 621 | .type = REGULATOR_VOLTAGE, |
| 622 | .owner = THIS_MODULE, |
| 623 | } |
| 624 | }; |
| 625 | |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 626 | static int max8998_pmic_dt_parse_dvs_gpio(struct max8998_dev *iodev, |
| 627 | struct max8998_platform_data *pdata, |
| 628 | struct device_node *pmic_np) |
| 629 | { |
| 630 | int gpio; |
| 631 | |
| 632 | gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 0); |
| 633 | if (!gpio_is_valid(gpio)) { |
| 634 | dev_err(iodev->dev, "invalid buck1 gpio[0]: %d\n", gpio); |
| 635 | return -EINVAL; |
| 636 | } |
| 637 | pdata->buck1_set1 = gpio; |
| 638 | |
| 639 | gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck1-dvs-gpios", 1); |
| 640 | if (!gpio_is_valid(gpio)) { |
| 641 | dev_err(iodev->dev, "invalid buck1 gpio[1]: %d\n", gpio); |
| 642 | return -EINVAL; |
| 643 | } |
| 644 | pdata->buck1_set2 = gpio; |
| 645 | |
| 646 | gpio = of_get_named_gpio(pmic_np, "max8998,pmic-buck2-dvs-gpio", 0); |
| 647 | if (!gpio_is_valid(gpio)) { |
| 648 | dev_err(iodev->dev, "invalid buck 2 gpio: %d\n", gpio); |
| 649 | return -EINVAL; |
| 650 | } |
| 651 | pdata->buck2_set3 = gpio; |
| 652 | |
| 653 | return 0; |
| 654 | } |
| 655 | |
| 656 | static int max8998_pmic_dt_parse_pdata(struct max8998_dev *iodev, |
| 657 | struct max8998_platform_data *pdata) |
| 658 | { |
| 659 | struct device_node *pmic_np = iodev->dev->of_node; |
| 660 | struct device_node *regulators_np, *reg_np; |
| 661 | struct max8998_regulator_data *rdata; |
| 662 | unsigned int i; |
| 663 | int ret; |
| 664 | |
| 665 | regulators_np = of_get_child_by_name(pmic_np, "regulators"); |
| 666 | if (!regulators_np) { |
| 667 | dev_err(iodev->dev, "could not find regulators sub-node\n"); |
| 668 | return -EINVAL; |
| 669 | } |
| 670 | |
| 671 | /* count the number of regulators to be supported in pmic */ |
| 672 | pdata->num_regulators = of_get_child_count(regulators_np); |
| 673 | |
| 674 | rdata = devm_kzalloc(iodev->dev, sizeof(*rdata) * |
| 675 | pdata->num_regulators, GFP_KERNEL); |
Sachin Kamat | 58c9537 | 2014-02-17 14:33:35 +0530 | [diff] [blame] | 676 | if (!rdata) { |
| 677 | of_node_put(regulators_np); |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 678 | return -ENOMEM; |
Sachin Kamat | 58c9537 | 2014-02-17 14:33:35 +0530 | [diff] [blame] | 679 | } |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 680 | |
| 681 | pdata->regulators = rdata; |
| 682 | for (i = 0; i < ARRAY_SIZE(regulators); ++i) { |
| 683 | reg_np = of_get_child_by_name(regulators_np, |
| 684 | regulators[i].name); |
| 685 | if (!reg_np) |
| 686 | continue; |
| 687 | |
| 688 | rdata->id = regulators[i].id; |
Javier Martinez Canillas | 072e78b | 2014-11-10 14:43:53 +0100 | [diff] [blame] | 689 | rdata->initdata = of_get_regulator_init_data(iodev->dev, |
| 690 | reg_np, |
| 691 | ®ulators[i]); |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 692 | rdata->reg_node = reg_np; |
| 693 | ++rdata; |
| 694 | } |
| 695 | pdata->num_regulators = rdata - pdata->regulators; |
| 696 | |
Sachin Kamat | 58c9537 | 2014-02-17 14:33:35 +0530 | [diff] [blame] | 697 | of_node_put(reg_np); |
| 698 | of_node_put(regulators_np); |
| 699 | |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 700 | ret = max8998_pmic_dt_parse_dvs_gpio(iodev, pdata, pmic_np); |
| 701 | if (ret) |
| 702 | return -EINVAL; |
| 703 | |
| 704 | if (of_find_property(pmic_np, "max8998,pmic-buck-voltage-lock", NULL)) |
| 705 | pdata->buck_voltage_lock = true; |
| 706 | |
| 707 | ret = of_property_read_u32(pmic_np, |
| 708 | "max8998,pmic-buck1-default-dvs-idx", |
| 709 | &pdata->buck1_default_idx); |
| 710 | if (!ret && pdata->buck1_default_idx >= 4) { |
| 711 | pdata->buck1_default_idx = 0; |
| 712 | dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n"); |
| 713 | } |
| 714 | |
| 715 | ret = of_property_read_u32(pmic_np, |
| 716 | "max8998,pmic-buck2-default-dvs-idx", |
| 717 | &pdata->buck2_default_idx); |
| 718 | if (!ret && pdata->buck2_default_idx >= 2) { |
| 719 | pdata->buck2_default_idx = 0; |
| 720 | dev_warn(iodev->dev, "invalid value for default dvs index, using 0 instead\n"); |
| 721 | } |
| 722 | |
| 723 | ret = of_property_read_u32_array(pmic_np, |
| 724 | "max8998,pmic-buck1-dvs-voltage", |
| 725 | pdata->buck1_voltage, |
| 726 | ARRAY_SIZE(pdata->buck1_voltage)); |
| 727 | if (ret) { |
| 728 | dev_err(iodev->dev, "buck1 voltages not specified\n"); |
| 729 | return -EINVAL; |
| 730 | } |
| 731 | |
| 732 | ret = of_property_read_u32_array(pmic_np, |
| 733 | "max8998,pmic-buck2-dvs-voltage", |
| 734 | pdata->buck2_voltage, |
| 735 | ARRAY_SIZE(pdata->buck2_voltage)); |
| 736 | if (ret) { |
| 737 | dev_err(iodev->dev, "buck2 voltages not specified\n"); |
| 738 | return -EINVAL; |
| 739 | } |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 744 | static int max8998_pmic_probe(struct platform_device *pdev) |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 745 | { |
| 746 | struct max8998_dev *iodev = dev_get_drvdata(pdev->dev.parent); |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 747 | struct max8998_platform_data *pdata = iodev->pdata; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 748 | struct regulator_config config = { }; |
Axel Lin | 8a221df | 2014-03-09 15:32:19 +0800 | [diff] [blame] | 749 | struct regulator_dev *rdev; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 750 | struct max8998_data *max8998; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 751 | struct i2c_client *i2c; |
Axel Lin | 8a221df | 2014-03-09 15:32:19 +0800 | [diff] [blame] | 752 | int i, ret; |
Tomasz Figa | 4280e0b4 | 2013-06-24 14:39:53 +0200 | [diff] [blame] | 753 | unsigned int v; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 754 | |
| 755 | if (!pdata) { |
| 756 | dev_err(pdev->dev.parent, "No platform init data supplied\n"); |
| 757 | return -ENODEV; |
| 758 | } |
| 759 | |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 760 | if (IS_ENABLED(CONFIG_OF) && iodev->dev->of_node) { |
| 761 | ret = max8998_pmic_dt_parse_pdata(iodev, pdata); |
| 762 | if (ret) |
| 763 | return ret; |
| 764 | } |
| 765 | |
Axel Lin | 0f80ea1 | 2012-04-19 09:47:37 +0800 | [diff] [blame] | 766 | max8998 = devm_kzalloc(&pdev->dev, sizeof(struct max8998_data), |
| 767 | GFP_KERNEL); |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 768 | if (!max8998) |
| 769 | return -ENOMEM; |
| 770 | |
Axel Lin | 747cc85 | 2010-08-27 16:37:34 +0800 | [diff] [blame] | 771 | max8998->dev = &pdev->dev; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 772 | max8998->iodev = iodev; |
Axel Lin | c356cbc | 2010-08-22 15:26:49 +0800 | [diff] [blame] | 773 | max8998->num_regulators = pdata->num_regulators; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 774 | platform_set_drvdata(pdev, max8998); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 775 | i2c = max8998->iodev->i2c; |
| 776 | |
MyungJoo Ham | 735a3d9 | 2011-01-11 12:20:05 +0100 | [diff] [blame] | 777 | max8998->buck1_idx = pdata->buck1_default_idx; |
| 778 | max8998->buck2_idx = pdata->buck2_default_idx; |
| 779 | |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 780 | /* NOTE: */ |
| 781 | /* For unused GPIO NOT marked as -1 (thereof equal to 0) WARN_ON */ |
| 782 | /* will be displayed */ |
| 783 | |
| 784 | /* Check if MAX8998 voltage selection GPIOs are defined */ |
| 785 | if (gpio_is_valid(pdata->buck1_set1) && |
| 786 | gpio_is_valid(pdata->buck1_set2)) { |
| 787 | /* Check if SET1 is not equal to 0 */ |
| 788 | if (!pdata->buck1_set1) { |
Thiago Farina | 9df19a5 | 2013-02-23 00:52:35 -0300 | [diff] [blame] | 789 | dev_err(&pdev->dev, |
| 790 | "MAX8998 SET1 GPIO defined as 0 !\n"); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 791 | WARN_ON(!pdata->buck1_set1); |
Sachin Kamat | 8d491bf | 2013-09-04 11:08:02 +0530 | [diff] [blame] | 792 | return -EIO; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 793 | } |
| 794 | /* Check if SET2 is not equal to 0 */ |
| 795 | if (!pdata->buck1_set2) { |
Thiago Farina | 9df19a5 | 2013-02-23 00:52:35 -0300 | [diff] [blame] | 796 | dev_err(&pdev->dev, |
| 797 | "MAX8998 SET2 GPIO defined as 0 !\n"); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 798 | WARN_ON(!pdata->buck1_set2); |
Sachin Kamat | 8d491bf | 2013-09-04 11:08:02 +0530 | [diff] [blame] | 799 | return -EIO; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | gpio_request(pdata->buck1_set1, "MAX8998 BUCK1_SET1"); |
| 803 | gpio_direction_output(pdata->buck1_set1, |
| 804 | max8998->buck1_idx & 0x1); |
| 805 | |
| 806 | |
| 807 | gpio_request(pdata->buck1_set2, "MAX8998 BUCK1_SET2"); |
| 808 | gpio_direction_output(pdata->buck1_set2, |
| 809 | (max8998->buck1_idx >> 1) & 0x1); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 810 | |
Tomasz Figa | 4280e0b4 | 2013-06-24 14:39:53 +0200 | [diff] [blame] | 811 | /* Set predefined values for BUCK1 registers */ |
| 812 | for (v = 0; v < ARRAY_SIZE(pdata->buck1_voltage); ++v) { |
| 813 | i = 0; |
| 814 | while (buck12_voltage_map_desc.min + |
| 815 | buck12_voltage_map_desc.step*i |
| 816 | < pdata->buck1_voltage[v]) |
| 817 | i++; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 818 | |
Tomasz Figa | 4280e0b4 | 2013-06-24 14:39:53 +0200 | [diff] [blame] | 819 | max8998->buck1_vol[v] = i; |
| 820 | ret = max8998_write_reg(i2c, |
| 821 | MAX8998_REG_BUCK1_VOLTAGE1 + v, i); |
| 822 | if (ret) |
Sachin Kamat | 8d491bf | 2013-09-04 11:08:02 +0530 | [diff] [blame] | 823 | return ret; |
Tomasz Figa | 4280e0b4 | 2013-06-24 14:39:53 +0200 | [diff] [blame] | 824 | } |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 825 | } |
| 826 | |
| 827 | if (gpio_is_valid(pdata->buck2_set3)) { |
| 828 | /* Check if SET3 is not equal to 0 */ |
| 829 | if (!pdata->buck2_set3) { |
Thiago Farina | 9df19a5 | 2013-02-23 00:52:35 -0300 | [diff] [blame] | 830 | dev_err(&pdev->dev, |
| 831 | "MAX8998 SET3 GPIO defined as 0 !\n"); |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 832 | WARN_ON(!pdata->buck2_set3); |
Sachin Kamat | 8d491bf | 2013-09-04 11:08:02 +0530 | [diff] [blame] | 833 | return -EIO; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 834 | } |
| 835 | gpio_request(pdata->buck2_set3, "MAX8998 BUCK2_SET3"); |
| 836 | gpio_direction_output(pdata->buck2_set3, |
| 837 | max8998->buck2_idx & 0x1); |
| 838 | |
Tomasz Figa | 4280e0b4 | 2013-06-24 14:39:53 +0200 | [diff] [blame] | 839 | /* Set predefined values for BUCK2 registers */ |
| 840 | for (v = 0; v < ARRAY_SIZE(pdata->buck2_voltage); ++v) { |
| 841 | i = 0; |
| 842 | while (buck12_voltage_map_desc.min + |
| 843 | buck12_voltage_map_desc.step*i |
| 844 | < pdata->buck2_voltage[v]) |
| 845 | i++; |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 846 | |
Tomasz Figa | 4280e0b4 | 2013-06-24 14:39:53 +0200 | [diff] [blame] | 847 | max8998->buck2_vol[v] = i; |
| 848 | ret = max8998_write_reg(i2c, |
| 849 | MAX8998_REG_BUCK2_VOLTAGE1 + v, i); |
| 850 | if (ret) |
Sachin Kamat | 8d491bf | 2013-09-04 11:08:02 +0530 | [diff] [blame] | 851 | return ret; |
Tomasz Figa | 4280e0b4 | 2013-06-24 14:39:53 +0200 | [diff] [blame] | 852 | } |
Lukasz Majewski | 50f19a4 | 2010-09-27 14:32:27 +0200 | [diff] [blame] | 853 | } |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 854 | |
| 855 | for (i = 0; i < pdata->num_regulators; i++) { |
| 856 | const struct voltage_map_desc *desc; |
| 857 | int id = pdata->regulators[i].id; |
| 858 | int index = id - MAX8998_LDO2; |
| 859 | |
| 860 | desc = ldo_voltage_map[id]; |
| 861 | if (desc && regulators[index].ops != &max8998_others_ops) { |
| 862 | int count = (desc->max - desc->min) / desc->step + 1; |
Axel Lin | 9a0fbb6 | 2012-06-17 09:33:07 +0800 | [diff] [blame] | 863 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 864 | regulators[index].n_voltages = count; |
Axel Lin | adf6178 | 2012-12-28 17:10:20 +0800 | [diff] [blame] | 865 | regulators[index].min_uV = desc->min; |
| 866 | regulators[index].uV_step = desc->step; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 867 | } |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 868 | |
| 869 | config.dev = max8998->dev; |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 870 | config.of_node = pdata->regulators[i].reg_node; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 871 | config.init_data = pdata->regulators[i].initdata; |
| 872 | config.driver_data = max8998; |
| 873 | |
Axel Lin | 8a221df | 2014-03-09 15:32:19 +0800 | [diff] [blame] | 874 | rdev = devm_regulator_register(&pdev->dev, ®ulators[index], |
| 875 | &config); |
| 876 | if (IS_ERR(rdev)) { |
| 877 | ret = PTR_ERR(rdev); |
Tomasz Figa | ee999fb | 2013-06-25 16:08:10 +0200 | [diff] [blame] | 878 | dev_err(max8998->dev, "regulator %s init failed (%d)\n", |
| 879 | regulators[index].name, ret); |
Sachin Kamat | 8d491bf | 2013-09-04 11:08:02 +0530 | [diff] [blame] | 880 | return ret; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 881 | } |
| 882 | } |
| 883 | |
| 884 | |
| 885 | return 0; |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 886 | } |
| 887 | |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 888 | static const struct platform_device_id max8998_pmic_id[] = { |
| 889 | { "max8998-pmic", TYPE_MAX8998 }, |
| 890 | { "lp3974-pmic", TYPE_LP3974 }, |
| 891 | { } |
| 892 | }; |
Axel Lin | a51b907 | 2011-03-26 23:28:42 +0800 | [diff] [blame] | 893 | MODULE_DEVICE_TABLE(platform, max8998_pmic_id); |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 894 | |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 895 | static struct platform_driver max8998_pmic_driver = { |
| 896 | .driver = { |
| 897 | .name = "max8998-pmic", |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 898 | }, |
| 899 | .probe = max8998_pmic_probe, |
MyungJoo Ham | 337ce5d | 2011-01-04 14:17:39 +0900 | [diff] [blame] | 900 | .id_table = max8998_pmic_id, |
Kyungmin Park | 156f252 | 2010-06-16 09:04:16 +0200 | [diff] [blame] | 901 | }; |
| 902 | |
| 903 | static int __init max8998_pmic_init(void) |
| 904 | { |
| 905 | return platform_driver_register(&max8998_pmic_driver); |
| 906 | } |
| 907 | subsys_initcall(max8998_pmic_init); |
| 908 | |
| 909 | static void __exit max8998_pmic_cleanup(void) |
| 910 | { |
| 911 | platform_driver_unregister(&max8998_pmic_driver); |
| 912 | } |
| 913 | module_exit(max8998_pmic_cleanup); |
| 914 | |
| 915 | MODULE_DESCRIPTION("MAXIM 8998 voltage regulator driver"); |
| 916 | MODULE_AUTHOR("Kyungmin Park <kyungmin.park@samsung.com>"); |
| 917 | MODULE_LICENSE("GPL"); |