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