Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 1 | /* |
| 2 | * max8660.c -- Voltage regulation for the Maxim 8660/8661 |
| 3 | * |
| 4 | * based on max1586.c and wm8400-regulator.c |
| 5 | * |
| 6 | * Copyright (C) 2009 Wolfram Sang, Pengutronix e.K. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; version 2 of the License. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but WITHOUT |
| 13 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 15 | * more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along with |
| 18 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 19 | * Place, Suite 330, Boston, MA 02111-1307 USA |
| 20 | * |
| 21 | * Some info: |
| 22 | * |
| 23 | * Datasheet: http://datasheets.maxim-ic.com/en/ds/MAX8660-MAX8661.pdf |
| 24 | * |
| 25 | * This chip is a bit nasty because it is a write-only device. Thus, the driver |
| 26 | * uses shadow registers to keep track of its values. The main problem appears |
| 27 | * to be the initialization: When Linux boots up, we cannot know if the chip is |
| 28 | * in the default state or not, so we would have to pass such information in |
| 29 | * platform_data. As this adds a bit of complexity to the driver, this is left |
| 30 | * out for now until it is really needed. |
| 31 | * |
| 32 | * [A|S|M]DTV1 registers are currently not used, but [A|S|M]DTV2. |
| 33 | * |
| 34 | * If the driver is feature complete, it might be worth to check if one set of |
| 35 | * functions for V3-V7 is sufficient. For maximum flexibility during |
| 36 | * development, they are separated for now. |
| 37 | * |
| 38 | */ |
| 39 | |
| 40 | #include <linux/module.h> |
| 41 | #include <linux/err.h> |
| 42 | #include <linux/i2c.h> |
| 43 | #include <linux/platform_device.h> |
| 44 | #include <linux/regulator/driver.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 45 | #include <linux/slab.h> |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 46 | #include <linux/regulator/max8660.h> |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 47 | #include <linux/of.h> |
| 48 | #include <linux/of_device.h> |
| 49 | #include <linux/regulator/of_regulator.h> |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 50 | |
| 51 | #define MAX8660_DCDC_MIN_UV 725000 |
| 52 | #define MAX8660_DCDC_MAX_UV 1800000 |
| 53 | #define MAX8660_DCDC_STEP 25000 |
| 54 | #define MAX8660_DCDC_MAX_SEL 0x2b |
| 55 | |
| 56 | #define MAX8660_LDO5_MIN_UV 1700000 |
| 57 | #define MAX8660_LDO5_MAX_UV 2000000 |
| 58 | #define MAX8660_LDO5_STEP 25000 |
| 59 | #define MAX8660_LDO5_MAX_SEL 0x0c |
| 60 | |
| 61 | #define MAX8660_LDO67_MIN_UV 1800000 |
| 62 | #define MAX8660_LDO67_MAX_UV 3300000 |
| 63 | #define MAX8660_LDO67_STEP 100000 |
| 64 | #define MAX8660_LDO67_MAX_SEL 0x0f |
| 65 | |
| 66 | enum { |
| 67 | MAX8660_OVER1, |
| 68 | MAX8660_OVER2, |
| 69 | MAX8660_VCC1, |
| 70 | MAX8660_ADTV1, |
| 71 | MAX8660_ADTV2, |
| 72 | MAX8660_SDTV1, |
| 73 | MAX8660_SDTV2, |
| 74 | MAX8660_MDTV1, |
| 75 | MAX8660_MDTV2, |
| 76 | MAX8660_L12VCR, |
| 77 | MAX8660_FPWM, |
| 78 | MAX8660_N_REGS, /* not a real register */ |
| 79 | }; |
| 80 | |
| 81 | struct max8660 { |
| 82 | struct i2c_client *client; |
| 83 | u8 shadow_regs[MAX8660_N_REGS]; /* as chip is write only */ |
| 84 | struct regulator_dev *rdev[]; |
| 85 | }; |
| 86 | |
| 87 | static int max8660_write(struct max8660 *max8660, u8 reg, u8 mask, u8 val) |
| 88 | { |
| 89 | static const u8 max8660_addresses[MAX8660_N_REGS] = |
| 90 | { 0x10, 0x12, 0x20, 0x23, 0x24, 0x29, 0x2a, 0x32, 0x33, 0x39, 0x80 }; |
| 91 | |
| 92 | int ret; |
| 93 | u8 reg_val = (max8660->shadow_regs[reg] & mask) | val; |
| 94 | dev_vdbg(&max8660->client->dev, "Writing reg %02x with %02x\n", |
| 95 | max8660_addresses[reg], reg_val); |
| 96 | |
| 97 | ret = i2c_smbus_write_byte_data(max8660->client, |
| 98 | max8660_addresses[reg], reg_val); |
| 99 | if (ret == 0) |
| 100 | max8660->shadow_regs[reg] = reg_val; |
| 101 | |
| 102 | return ret; |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /* |
| 107 | * DCDC functions |
| 108 | */ |
| 109 | |
| 110 | static int max8660_dcdc_is_enabled(struct regulator_dev *rdev) |
| 111 | { |
| 112 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 113 | u8 val = max8660->shadow_regs[MAX8660_OVER1]; |
| 114 | u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4; |
| 115 | return !!(val & mask); |
| 116 | } |
| 117 | |
| 118 | static int max8660_dcdc_enable(struct regulator_dev *rdev) |
| 119 | { |
| 120 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 121 | u8 bit = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4; |
| 122 | return max8660_write(max8660, MAX8660_OVER1, 0xff, bit); |
| 123 | } |
| 124 | |
| 125 | static int max8660_dcdc_disable(struct regulator_dev *rdev) |
| 126 | { |
| 127 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 128 | u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? ~1 : ~4; |
| 129 | return max8660_write(max8660, MAX8660_OVER1, mask, 0); |
| 130 | } |
| 131 | |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 132 | static int max8660_dcdc_get_voltage_sel(struct regulator_dev *rdev) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 133 | { |
| 134 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 135 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 136 | u8 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2; |
| 137 | u8 selector = max8660->shadow_regs[reg]; |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 138 | return selector; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 139 | } |
| 140 | |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 141 | static int max8660_dcdc_set_voltage_sel(struct regulator_dev *rdev, |
| 142 | unsigned int selector) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 143 | { |
| 144 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 145 | u8 reg, bits; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 146 | int ret; |
| 147 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 148 | reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2; |
| 149 | ret = max8660_write(max8660, reg, 0, selector); |
| 150 | if (ret) |
| 151 | return ret; |
| 152 | |
| 153 | /* Select target voltage register and activate regulation */ |
| 154 | bits = (rdev_get_id(rdev) == MAX8660_V3) ? 0x03 : 0x30; |
| 155 | return max8660_write(max8660, MAX8660_VCC1, 0xff, bits); |
| 156 | } |
| 157 | |
| 158 | static struct regulator_ops max8660_dcdc_ops = { |
| 159 | .is_enabled = max8660_dcdc_is_enabled, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 160 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 161 | .map_voltage = regulator_map_voltage_linear, |
| 162 | .set_voltage_sel = max8660_dcdc_set_voltage_sel, |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 163 | .get_voltage_sel = max8660_dcdc_get_voltage_sel, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 164 | }; |
| 165 | |
| 166 | |
| 167 | /* |
| 168 | * LDO5 functions |
| 169 | */ |
| 170 | |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 171 | static int max8660_ldo5_get_voltage_sel(struct regulator_dev *rdev) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 172 | { |
| 173 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 174 | |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 175 | u8 selector = max8660->shadow_regs[MAX8660_MDTV2]; |
| 176 | return selector; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 177 | } |
| 178 | |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 179 | static int max8660_ldo5_set_voltage_sel(struct regulator_dev *rdev, |
| 180 | unsigned int selector) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 181 | { |
| 182 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 183 | int ret; |
| 184 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 185 | ret = max8660_write(max8660, MAX8660_MDTV2, 0, selector); |
| 186 | if (ret) |
| 187 | return ret; |
| 188 | |
| 189 | /* Select target voltage register and activate regulation */ |
| 190 | return max8660_write(max8660, MAX8660_VCC1, 0xff, 0xc0); |
| 191 | } |
| 192 | |
| 193 | static struct regulator_ops max8660_ldo5_ops = { |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 194 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 195 | .map_voltage = regulator_map_voltage_linear, |
| 196 | .set_voltage_sel = max8660_ldo5_set_voltage_sel, |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 197 | .get_voltage_sel = max8660_ldo5_get_voltage_sel, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
| 200 | |
| 201 | /* |
| 202 | * LDO67 functions |
| 203 | */ |
| 204 | |
| 205 | static int max8660_ldo67_is_enabled(struct regulator_dev *rdev) |
| 206 | { |
| 207 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 208 | u8 val = max8660->shadow_regs[MAX8660_OVER2]; |
| 209 | u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4; |
| 210 | return !!(val & mask); |
| 211 | } |
| 212 | |
| 213 | static int max8660_ldo67_enable(struct regulator_dev *rdev) |
| 214 | { |
| 215 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 216 | u8 bit = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4; |
| 217 | return max8660_write(max8660, MAX8660_OVER2, 0xff, bit); |
| 218 | } |
| 219 | |
| 220 | static int max8660_ldo67_disable(struct regulator_dev *rdev) |
| 221 | { |
| 222 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 223 | u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? ~2 : ~4; |
| 224 | return max8660_write(max8660, MAX8660_OVER2, mask, 0); |
| 225 | } |
| 226 | |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 227 | static int max8660_ldo67_get_voltage_sel(struct regulator_dev *rdev) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 228 | { |
| 229 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 230 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 231 | u8 shift = (rdev_get_id(rdev) == MAX8660_V6) ? 0 : 4; |
| 232 | u8 selector = (max8660->shadow_regs[MAX8660_L12VCR] >> shift) & 0xf; |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 233 | return selector; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 236 | static int max8660_ldo67_set_voltage_sel(struct regulator_dev *rdev, |
| 237 | unsigned int selector) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 238 | { |
| 239 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 240 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 241 | if (rdev_get_id(rdev) == MAX8660_V6) |
| 242 | return max8660_write(max8660, MAX8660_L12VCR, 0xf0, selector); |
| 243 | else |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 244 | return max8660_write(max8660, MAX8660_L12VCR, 0x0f, |
| 245 | selector << 4); |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static struct regulator_ops max8660_ldo67_ops = { |
| 249 | .is_enabled = max8660_ldo67_is_enabled, |
| 250 | .enable = max8660_ldo67_enable, |
| 251 | .disable = max8660_ldo67_disable, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 252 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 253 | .map_voltage = regulator_map_voltage_linear, |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 254 | .get_voltage_sel = max8660_ldo67_get_voltage_sel, |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 255 | .set_voltage_sel = max8660_ldo67_set_voltage_sel, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 256 | }; |
| 257 | |
Axel Lin | 621adb3 | 2012-04-06 08:26:59 +0800 | [diff] [blame] | 258 | static const struct regulator_desc max8660_reg[] = { |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 259 | { |
| 260 | .name = "V3(DCDC)", |
| 261 | .id = MAX8660_V3, |
| 262 | .ops = &max8660_dcdc_ops, |
| 263 | .type = REGULATOR_VOLTAGE, |
| 264 | .n_voltages = MAX8660_DCDC_MAX_SEL + 1, |
| 265 | .owner = THIS_MODULE, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 266 | .min_uV = MAX8660_DCDC_MIN_UV, |
| 267 | .uV_step = MAX8660_DCDC_STEP, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 268 | }, |
| 269 | { |
| 270 | .name = "V4(DCDC)", |
| 271 | .id = MAX8660_V4, |
| 272 | .ops = &max8660_dcdc_ops, |
| 273 | .type = REGULATOR_VOLTAGE, |
| 274 | .n_voltages = MAX8660_DCDC_MAX_SEL + 1, |
| 275 | .owner = THIS_MODULE, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 276 | .min_uV = MAX8660_DCDC_MIN_UV, |
| 277 | .uV_step = MAX8660_DCDC_STEP, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 278 | }, |
| 279 | { |
| 280 | .name = "V5(LDO)", |
| 281 | .id = MAX8660_V5, |
| 282 | .ops = &max8660_ldo5_ops, |
| 283 | .type = REGULATOR_VOLTAGE, |
| 284 | .n_voltages = MAX8660_LDO5_MAX_SEL + 1, |
| 285 | .owner = THIS_MODULE, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 286 | .min_uV = MAX8660_LDO5_MIN_UV, |
| 287 | .uV_step = MAX8660_LDO5_STEP, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 288 | }, |
| 289 | { |
| 290 | .name = "V6(LDO)", |
| 291 | .id = MAX8660_V6, |
| 292 | .ops = &max8660_ldo67_ops, |
| 293 | .type = REGULATOR_VOLTAGE, |
| 294 | .n_voltages = MAX8660_LDO67_MAX_SEL + 1, |
| 295 | .owner = THIS_MODULE, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 296 | .min_uV = MAX8660_LDO67_MIN_UV, |
| 297 | .uV_step = MAX8660_LDO67_STEP, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 298 | }, |
| 299 | { |
| 300 | .name = "V7(LDO)", |
| 301 | .id = MAX8660_V7, |
| 302 | .ops = &max8660_ldo67_ops, |
| 303 | .type = REGULATOR_VOLTAGE, |
| 304 | .n_voltages = MAX8660_LDO67_MAX_SEL + 1, |
| 305 | .owner = THIS_MODULE, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 306 | .min_uV = MAX8660_LDO67_MIN_UV, |
| 307 | .uV_step = MAX8660_LDO67_STEP, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 308 | }, |
| 309 | }; |
| 310 | |
Daniel Mack | 4a678f0 | 2013-08-02 10:19:15 +0200 | [diff] [blame] | 311 | enum { |
| 312 | MAX8660 = 0, |
| 313 | MAX8661 = 1, |
| 314 | }; |
| 315 | |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 316 | #ifdef CONFIG_OF |
| 317 | static const struct of_device_id max8660_dt_ids[] = { |
| 318 | { .compatible = "maxim,max8660", .data = (void *) MAX8660 }, |
| 319 | { .compatible = "maxim,max8661", .data = (void *) MAX8661 }, |
| 320 | { } |
| 321 | }; |
| 322 | MODULE_DEVICE_TABLE(of, max8660_dt_ids); |
| 323 | |
| 324 | static int max8660_pdata_from_dt(struct device *dev, |
| 325 | struct device_node **of_node, |
| 326 | struct max8660_platform_data *pdata) |
| 327 | { |
| 328 | int matched, i; |
| 329 | struct device_node *np; |
| 330 | struct max8660_subdev_data *sub; |
| 331 | struct of_regulator_match rmatch[ARRAY_SIZE(max8660_reg)]; |
| 332 | |
| 333 | np = of_find_node_by_name(dev->of_node, "regulators"); |
| 334 | if (!np) { |
| 335 | dev_err(dev, "missing 'regulators' subnode in DT\n"); |
| 336 | return -EINVAL; |
| 337 | } |
| 338 | |
| 339 | for (i = 0; i < ARRAY_SIZE(rmatch); i++) |
| 340 | rmatch[i].name = max8660_reg[i].name; |
| 341 | |
| 342 | matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(rmatch)); |
| 343 | if (matched <= 0) |
| 344 | return matched; |
| 345 | |
| 346 | pdata->subdevs = devm_kzalloc(dev, sizeof(struct max8660_subdev_data) * |
| 347 | matched, GFP_KERNEL); |
| 348 | if (!pdata->subdevs) |
| 349 | return -ENOMEM; |
| 350 | |
| 351 | pdata->num_subdevs = matched; |
| 352 | sub = pdata->subdevs; |
| 353 | |
| 354 | for (i = 0; i < matched; i++) { |
| 355 | sub->id = i; |
| 356 | sub->name = rmatch[i].name; |
| 357 | sub->platform_data = rmatch[i].init_data; |
| 358 | of_node[i] = rmatch[i].of_node; |
| 359 | sub++; |
| 360 | } |
| 361 | |
| 362 | return 0; |
| 363 | } |
| 364 | #else |
| 365 | static inline int max8660_pdata_from_dt(struct device *dev, |
| 366 | struct device_node **of_node, |
Daniel Mack | d6c7e11 | 2013-08-08 12:02:18 +0200 | [diff] [blame] | 367 | struct max8660_platform_data *pdata) |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 368 | { |
| 369 | return 0; |
| 370 | } |
| 371 | #endif |
| 372 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 373 | static int max8660_probe(struct i2c_client *client, |
Dmitry Torokhov | 308f100 | 2010-02-23 23:38:28 -0800 | [diff] [blame] | 374 | const struct i2c_device_id *i2c_id) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 375 | { |
| 376 | struct regulator_dev **rdev; |
Daniel Mack | de492e8 | 2013-08-02 10:19:16 +0200 | [diff] [blame] | 377 | struct device *dev = &client->dev; |
| 378 | struct max8660_platform_data *pdata = dev_get_platdata(dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 379 | struct regulator_config config = { }; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 380 | struct max8660 *max8660; |
| 381 | int boot_on, i, id, ret = -EINVAL; |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 382 | struct device_node *of_node[MAX8660_V_END]; |
Daniel Mack | d6c7e11 | 2013-08-08 12:02:18 +0200 | [diff] [blame] | 383 | unsigned long type; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 384 | |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 385 | if (dev->of_node && !pdata) { |
| 386 | const struct of_device_id *id; |
| 387 | struct max8660_platform_data pdata_of; |
| 388 | |
| 389 | id = of_match_device(of_match_ptr(max8660_dt_ids), dev); |
| 390 | if (!id) |
| 391 | return -ENODEV; |
| 392 | |
| 393 | ret = max8660_pdata_from_dt(dev, of_node, &pdata_of); |
| 394 | if (ret < 0) |
| 395 | return ret; |
| 396 | |
| 397 | pdata = &pdata_of; |
Daniel Mack | d6c7e11 | 2013-08-08 12:02:18 +0200 | [diff] [blame] | 398 | type = (unsigned long) id->data; |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 399 | } else { |
| 400 | type = i2c_id->driver_data; |
| 401 | memset(of_node, 0, sizeof(of_node)); |
| 402 | } |
| 403 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 404 | if (pdata->num_subdevs > MAX8660_V_END) { |
Daniel Mack | de492e8 | 2013-08-02 10:19:16 +0200 | [diff] [blame] | 405 | dev_err(dev, "Too many regulators found!\n"); |
Axel Lin | 4d26f7d | 2012-04-11 23:08:27 +0800 | [diff] [blame] | 406 | return -EINVAL; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 407 | } |
| 408 | |
Daniel Mack | de492e8 | 2013-08-02 10:19:16 +0200 | [diff] [blame] | 409 | max8660 = devm_kzalloc(dev, sizeof(struct max8660) + |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 410 | sizeof(struct regulator_dev *) * MAX8660_V_END, |
| 411 | GFP_KERNEL); |
Axel Lin | 4d26f7d | 2012-04-11 23:08:27 +0800 | [diff] [blame] | 412 | if (!max8660) |
| 413 | return -ENOMEM; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 414 | |
| 415 | max8660->client = client; |
| 416 | rdev = max8660->rdev; |
| 417 | |
| 418 | if (pdata->en34_is_high) { |
| 419 | /* Simulate always on */ |
| 420 | max8660->shadow_regs[MAX8660_OVER1] = 5; |
| 421 | } else { |
| 422 | /* Otherwise devices can be toggled via software */ |
| 423 | max8660_dcdc_ops.enable = max8660_dcdc_enable; |
| 424 | max8660_dcdc_ops.disable = max8660_dcdc_disable; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * First, set up shadow registers to prevent glitches. As some |
| 429 | * registers are shared between regulators, everything must be properly |
| 430 | * set up for all regulators in advance. |
| 431 | */ |
| 432 | max8660->shadow_regs[MAX8660_ADTV1] = |
| 433 | max8660->shadow_regs[MAX8660_ADTV2] = |
| 434 | max8660->shadow_regs[MAX8660_SDTV1] = |
| 435 | max8660->shadow_regs[MAX8660_SDTV2] = 0x1b; |
| 436 | max8660->shadow_regs[MAX8660_MDTV1] = |
| 437 | max8660->shadow_regs[MAX8660_MDTV2] = 0x04; |
| 438 | |
| 439 | for (i = 0; i < pdata->num_subdevs; i++) { |
| 440 | |
| 441 | if (!pdata->subdevs[i].platform_data) |
Sachin Kamat | dcfb6b5 | 2013-09-04 11:07:58 +0530 | [diff] [blame] | 442 | return ret; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 443 | |
| 444 | boot_on = pdata->subdevs[i].platform_data->constraints.boot_on; |
| 445 | |
| 446 | switch (pdata->subdevs[i].id) { |
| 447 | case MAX8660_V3: |
| 448 | if (boot_on) |
| 449 | max8660->shadow_regs[MAX8660_OVER1] |= 1; |
| 450 | break; |
| 451 | |
| 452 | case MAX8660_V4: |
| 453 | if (boot_on) |
| 454 | max8660->shadow_regs[MAX8660_OVER1] |= 4; |
| 455 | break; |
| 456 | |
| 457 | case MAX8660_V5: |
| 458 | break; |
| 459 | |
| 460 | case MAX8660_V6: |
| 461 | if (boot_on) |
| 462 | max8660->shadow_regs[MAX8660_OVER2] |= 2; |
| 463 | break; |
| 464 | |
| 465 | case MAX8660_V7: |
Daniel Mack | 4a678f0 | 2013-08-02 10:19:15 +0200 | [diff] [blame] | 466 | if (type == MAX8661) { |
Daniel Mack | de492e8 | 2013-08-02 10:19:16 +0200 | [diff] [blame] | 467 | dev_err(dev, "Regulator not on this chip!\n"); |
Sachin Kamat | dcfb6b5 | 2013-09-04 11:07:58 +0530 | [diff] [blame] | 468 | return -EINVAL; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | if (boot_on) |
| 472 | max8660->shadow_regs[MAX8660_OVER2] |= 4; |
| 473 | break; |
| 474 | |
| 475 | default: |
Daniel Mack | de492e8 | 2013-08-02 10:19:16 +0200 | [diff] [blame] | 476 | dev_err(dev, "invalid regulator %s\n", |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 477 | pdata->subdevs[i].name); |
Sachin Kamat | dcfb6b5 | 2013-09-04 11:07:58 +0530 | [diff] [blame] | 478 | return ret; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | /* Finally register devices */ |
| 483 | for (i = 0; i < pdata->num_subdevs; i++) { |
| 484 | |
| 485 | id = pdata->subdevs[i].id; |
| 486 | |
Daniel Mack | de492e8 | 2013-08-02 10:19:16 +0200 | [diff] [blame] | 487 | config.dev = dev; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 488 | config.init_data = pdata->subdevs[i].platform_data; |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 489 | config.of_node = of_node[i]; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 490 | config.driver_data = max8660; |
| 491 | |
Sachin Kamat | dcfb6b5 | 2013-09-04 11:07:58 +0530 | [diff] [blame] | 492 | rdev[i] = devm_regulator_register(&client->dev, |
| 493 | &max8660_reg[id], &config); |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 494 | if (IS_ERR(rdev[i])) { |
| 495 | ret = PTR_ERR(rdev[i]); |
Sachin Kamat | dcfb6b5 | 2013-09-04 11:07:58 +0530 | [diff] [blame] | 496 | dev_err(&client->dev, "failed to register %s\n", |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 497 | max8660_reg[id].name); |
Sachin Kamat | dcfb6b5 | 2013-09-04 11:07:58 +0530 | [diff] [blame] | 498 | return PTR_ERR(rdev[i]); |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 499 | } |
| 500 | } |
| 501 | |
Axel Lin | 53a4bef | 2010-08-06 13:35:27 +0800 | [diff] [blame] | 502 | i2c_set_clientdata(client, max8660); |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 503 | return 0; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | static const struct i2c_device_id max8660_id[] = { |
Daniel Mack | 4a678f0 | 2013-08-02 10:19:15 +0200 | [diff] [blame] | 507 | { .name = "max8660", .driver_data = MAX8660 }, |
| 508 | { .name = "max8661", .driver_data = MAX8661 }, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 509 | { } |
| 510 | }; |
| 511 | MODULE_DEVICE_TABLE(i2c, max8660_id); |
| 512 | |
| 513 | static struct i2c_driver max8660_driver = { |
| 514 | .probe = max8660_probe, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 515 | .driver = { |
| 516 | .name = "max8660", |
Dmitry Torokhov | 308f100 | 2010-02-23 23:38:28 -0800 | [diff] [blame] | 517 | .owner = THIS_MODULE, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 518 | }, |
| 519 | .id_table = max8660_id, |
| 520 | }; |
| 521 | |
| 522 | static int __init max8660_init(void) |
| 523 | { |
| 524 | return i2c_add_driver(&max8660_driver); |
| 525 | } |
| 526 | subsys_initcall(max8660_init); |
| 527 | |
| 528 | static void __exit max8660_exit(void) |
| 529 | { |
| 530 | i2c_del_driver(&max8660_driver); |
| 531 | } |
| 532 | module_exit(max8660_exit); |
| 533 | |
| 534 | /* Module information */ |
| 535 | MODULE_DESCRIPTION("MAXIM 8660/8661 voltage regulator driver"); |
| 536 | MODULE_AUTHOR("Wolfram Sang"); |
| 537 | MODULE_LICENSE("GPL v2"); |