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