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 */ |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 84 | }; |
| 85 | |
| 86 | static int max8660_write(struct max8660 *max8660, u8 reg, u8 mask, u8 val) |
| 87 | { |
Sachin Kamat | f920ec7 | 2014-03-18 15:53:02 +0530 | [diff] [blame] | 88 | static const u8 max8660_addresses[MAX8660_N_REGS] = { |
Sachin Kamat | dda5e36 | 2014-03-19 18:02:58 +0530 | [diff] [blame] | 89 | 0x10, 0x12, 0x20, 0x23, 0x24, 0x29, 0x2a, 0x32, 0x33, 0x39, 0x80 |
| 90 | }; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 91 | |
| 92 | int ret; |
| 93 | u8 reg_val = (max8660->shadow_regs[reg] & mask) | val; |
Sachin Kamat | f920ec7 | 2014-03-18 15:53:02 +0530 | [diff] [blame] | 94 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 95 | dev_vdbg(&max8660->client->dev, "Writing reg %02x with %02x\n", |
| 96 | max8660_addresses[reg], reg_val); |
| 97 | |
| 98 | ret = i2c_smbus_write_byte_data(max8660->client, |
| 99 | max8660_addresses[reg], reg_val); |
| 100 | if (ret == 0) |
| 101 | max8660->shadow_regs[reg] = reg_val; |
| 102 | |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /* |
| 108 | * DCDC functions |
| 109 | */ |
| 110 | |
| 111 | static int max8660_dcdc_is_enabled(struct regulator_dev *rdev) |
| 112 | { |
| 113 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 114 | u8 val = max8660->shadow_regs[MAX8660_OVER1]; |
| 115 | u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4; |
Sachin Kamat | f920ec7 | 2014-03-18 15:53:02 +0530 | [diff] [blame] | 116 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 117 | return !!(val & mask); |
| 118 | } |
| 119 | |
| 120 | static int max8660_dcdc_enable(struct regulator_dev *rdev) |
| 121 | { |
| 122 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 123 | u8 bit = (rdev_get_id(rdev) == MAX8660_V3) ? 1 : 4; |
Sachin Kamat | f920ec7 | 2014-03-18 15:53:02 +0530 | [diff] [blame] | 124 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 125 | return max8660_write(max8660, MAX8660_OVER1, 0xff, bit); |
| 126 | } |
| 127 | |
| 128 | static int max8660_dcdc_disable(struct regulator_dev *rdev) |
| 129 | { |
| 130 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 131 | u8 mask = (rdev_get_id(rdev) == MAX8660_V3) ? ~1 : ~4; |
Sachin Kamat | f920ec7 | 2014-03-18 15:53:02 +0530 | [diff] [blame] | 132 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 133 | return max8660_write(max8660, MAX8660_OVER1, mask, 0); |
| 134 | } |
| 135 | |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 136 | static int max8660_dcdc_get_voltage_sel(struct regulator_dev *rdev) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 137 | { |
| 138 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 139 | u8 reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2; |
| 140 | u8 selector = max8660->shadow_regs[reg]; |
Sachin Kamat | f920ec7 | 2014-03-18 15:53:02 +0530 | [diff] [blame] | 141 | |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 142 | return selector; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 143 | } |
| 144 | |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 145 | static int max8660_dcdc_set_voltage_sel(struct regulator_dev *rdev, |
| 146 | unsigned int selector) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 147 | { |
| 148 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 149 | u8 reg, bits; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 150 | int ret; |
| 151 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 152 | reg = (rdev_get_id(rdev) == MAX8660_V3) ? MAX8660_ADTV2 : MAX8660_SDTV2; |
| 153 | ret = max8660_write(max8660, reg, 0, selector); |
| 154 | if (ret) |
| 155 | return ret; |
| 156 | |
| 157 | /* Select target voltage register and activate regulation */ |
| 158 | bits = (rdev_get_id(rdev) == MAX8660_V3) ? 0x03 : 0x30; |
| 159 | return max8660_write(max8660, MAX8660_VCC1, 0xff, bits); |
| 160 | } |
| 161 | |
| 162 | static struct regulator_ops max8660_dcdc_ops = { |
| 163 | .is_enabled = max8660_dcdc_is_enabled, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 164 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 165 | .map_voltage = regulator_map_voltage_linear, |
| 166 | .set_voltage_sel = max8660_dcdc_set_voltage_sel, |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 167 | .get_voltage_sel = max8660_dcdc_get_voltage_sel, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | |
| 171 | /* |
| 172 | * LDO5 functions |
| 173 | */ |
| 174 | |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 175 | static int max8660_ldo5_get_voltage_sel(struct regulator_dev *rdev) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 176 | { |
| 177 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 178 | |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 179 | u8 selector = max8660->shadow_regs[MAX8660_MDTV2]; |
| 180 | return selector; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 181 | } |
| 182 | |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 183 | static int max8660_ldo5_set_voltage_sel(struct regulator_dev *rdev, |
| 184 | unsigned int selector) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 185 | { |
| 186 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 187 | int ret; |
| 188 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 189 | ret = max8660_write(max8660, MAX8660_MDTV2, 0, selector); |
| 190 | if (ret) |
| 191 | return ret; |
| 192 | |
| 193 | /* Select target voltage register and activate regulation */ |
| 194 | return max8660_write(max8660, MAX8660_VCC1, 0xff, 0xc0); |
| 195 | } |
| 196 | |
| 197 | static struct regulator_ops max8660_ldo5_ops = { |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 198 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 199 | .map_voltage = regulator_map_voltage_linear, |
| 200 | .set_voltage_sel = max8660_ldo5_set_voltage_sel, |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 201 | .get_voltage_sel = max8660_ldo5_get_voltage_sel, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 202 | }; |
| 203 | |
| 204 | |
| 205 | /* |
| 206 | * LDO67 functions |
| 207 | */ |
| 208 | |
| 209 | static int max8660_ldo67_is_enabled(struct regulator_dev *rdev) |
| 210 | { |
| 211 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 212 | u8 val = max8660->shadow_regs[MAX8660_OVER2]; |
| 213 | u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4; |
Sachin Kamat | f920ec7 | 2014-03-18 15:53:02 +0530 | [diff] [blame] | 214 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 215 | return !!(val & mask); |
| 216 | } |
| 217 | |
| 218 | static int max8660_ldo67_enable(struct regulator_dev *rdev) |
| 219 | { |
| 220 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 221 | u8 bit = (rdev_get_id(rdev) == MAX8660_V6) ? 2 : 4; |
Sachin Kamat | f920ec7 | 2014-03-18 15:53:02 +0530 | [diff] [blame] | 222 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 223 | return max8660_write(max8660, MAX8660_OVER2, 0xff, bit); |
| 224 | } |
| 225 | |
| 226 | static int max8660_ldo67_disable(struct regulator_dev *rdev) |
| 227 | { |
| 228 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 229 | u8 mask = (rdev_get_id(rdev) == MAX8660_V6) ? ~2 : ~4; |
Sachin Kamat | f920ec7 | 2014-03-18 15:53:02 +0530 | [diff] [blame] | 230 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 231 | return max8660_write(max8660, MAX8660_OVER2, mask, 0); |
| 232 | } |
| 233 | |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 234 | static int max8660_ldo67_get_voltage_sel(struct regulator_dev *rdev) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 235 | { |
| 236 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
| 237 | u8 shift = (rdev_get_id(rdev) == MAX8660_V6) ? 0 : 4; |
| 238 | u8 selector = (max8660->shadow_regs[MAX8660_L12VCR] >> shift) & 0xf; |
Sachin Kamat | f920ec7 | 2014-03-18 15:53:02 +0530 | [diff] [blame] | 239 | |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 240 | return selector; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 243 | static int max8660_ldo67_set_voltage_sel(struct regulator_dev *rdev, |
| 244 | unsigned int selector) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 245 | { |
| 246 | struct max8660 *max8660 = rdev_get_drvdata(rdev); |
Mark Brown | 3a93f2a | 2010-11-10 14:38:29 +0000 | [diff] [blame] | 247 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 248 | if (rdev_get_id(rdev) == MAX8660_V6) |
| 249 | return max8660_write(max8660, MAX8660_L12VCR, 0xf0, selector); |
| 250 | else |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 251 | return max8660_write(max8660, MAX8660_L12VCR, 0x0f, |
| 252 | selector << 4); |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static struct regulator_ops max8660_ldo67_ops = { |
| 256 | .is_enabled = max8660_ldo67_is_enabled, |
| 257 | .enable = max8660_ldo67_enable, |
| 258 | .disable = max8660_ldo67_disable, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 259 | .list_voltage = regulator_list_voltage_linear, |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 260 | .map_voltage = regulator_map_voltage_linear, |
Axel Lin | 9392c4f | 2012-05-15 21:42:12 +0800 | [diff] [blame] | 261 | .get_voltage_sel = max8660_ldo67_get_voltage_sel, |
Axel Lin | 5eb3394 | 2012-05-15 21:43:29 +0800 | [diff] [blame] | 262 | .set_voltage_sel = max8660_ldo67_set_voltage_sel, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 263 | }; |
| 264 | |
Axel Lin | 621adb3 | 2012-04-06 08:26:59 +0800 | [diff] [blame] | 265 | static const struct regulator_desc max8660_reg[] = { |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 266 | { |
| 267 | .name = "V3(DCDC)", |
| 268 | .id = MAX8660_V3, |
| 269 | .ops = &max8660_dcdc_ops, |
| 270 | .type = REGULATOR_VOLTAGE, |
| 271 | .n_voltages = MAX8660_DCDC_MAX_SEL + 1, |
| 272 | .owner = THIS_MODULE, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 273 | .min_uV = MAX8660_DCDC_MIN_UV, |
| 274 | .uV_step = MAX8660_DCDC_STEP, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 275 | }, |
| 276 | { |
| 277 | .name = "V4(DCDC)", |
| 278 | .id = MAX8660_V4, |
| 279 | .ops = &max8660_dcdc_ops, |
| 280 | .type = REGULATOR_VOLTAGE, |
| 281 | .n_voltages = MAX8660_DCDC_MAX_SEL + 1, |
| 282 | .owner = THIS_MODULE, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 283 | .min_uV = MAX8660_DCDC_MIN_UV, |
| 284 | .uV_step = MAX8660_DCDC_STEP, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 285 | }, |
| 286 | { |
| 287 | .name = "V5(LDO)", |
| 288 | .id = MAX8660_V5, |
| 289 | .ops = &max8660_ldo5_ops, |
| 290 | .type = REGULATOR_VOLTAGE, |
| 291 | .n_voltages = MAX8660_LDO5_MAX_SEL + 1, |
| 292 | .owner = THIS_MODULE, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 293 | .min_uV = MAX8660_LDO5_MIN_UV, |
| 294 | .uV_step = MAX8660_LDO5_STEP, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 295 | }, |
| 296 | { |
| 297 | .name = "V6(LDO)", |
| 298 | .id = MAX8660_V6, |
| 299 | .ops = &max8660_ldo67_ops, |
| 300 | .type = REGULATOR_VOLTAGE, |
| 301 | .n_voltages = MAX8660_LDO67_MAX_SEL + 1, |
| 302 | .owner = THIS_MODULE, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 303 | .min_uV = MAX8660_LDO67_MIN_UV, |
| 304 | .uV_step = MAX8660_LDO67_STEP, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 305 | }, |
| 306 | { |
| 307 | .name = "V7(LDO)", |
| 308 | .id = MAX8660_V7, |
| 309 | .ops = &max8660_ldo67_ops, |
| 310 | .type = REGULATOR_VOLTAGE, |
| 311 | .n_voltages = MAX8660_LDO67_MAX_SEL + 1, |
| 312 | .owner = THIS_MODULE, |
Axel Lin | 134d34a | 2012-05-15 21:41:18 +0800 | [diff] [blame] | 313 | .min_uV = MAX8660_LDO67_MIN_UV, |
| 314 | .uV_step = MAX8660_LDO67_STEP, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 315 | }, |
| 316 | }; |
| 317 | |
Daniel Mack | 4a678f0 | 2013-08-02 10:19:15 +0200 | [diff] [blame] | 318 | enum { |
| 319 | MAX8660 = 0, |
| 320 | MAX8661 = 1, |
| 321 | }; |
| 322 | |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 323 | #ifdef CONFIG_OF |
| 324 | static const struct of_device_id max8660_dt_ids[] = { |
| 325 | { .compatible = "maxim,max8660", .data = (void *) MAX8660 }, |
| 326 | { .compatible = "maxim,max8661", .data = (void *) MAX8661 }, |
| 327 | { } |
| 328 | }; |
| 329 | MODULE_DEVICE_TABLE(of, max8660_dt_ids); |
| 330 | |
| 331 | static int max8660_pdata_from_dt(struct device *dev, |
| 332 | struct device_node **of_node, |
| 333 | struct max8660_platform_data *pdata) |
| 334 | { |
| 335 | int matched, i; |
| 336 | struct device_node *np; |
| 337 | struct max8660_subdev_data *sub; |
Javier Martinez Canillas | c988980 | 2014-11-03 15:40:43 +0100 | [diff] [blame] | 338 | struct of_regulator_match rmatch[ARRAY_SIZE(max8660_reg)] = { }; |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 339 | |
Sachin Kamat | 826adb4 | 2014-02-14 17:19:49 +0530 | [diff] [blame] | 340 | np = of_get_child_by_name(dev->of_node, "regulators"); |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 341 | if (!np) { |
| 342 | dev_err(dev, "missing 'regulators' subnode in DT\n"); |
| 343 | return -EINVAL; |
| 344 | } |
| 345 | |
| 346 | for (i = 0; i < ARRAY_SIZE(rmatch); i++) |
| 347 | rmatch[i].name = max8660_reg[i].name; |
| 348 | |
| 349 | matched = of_regulator_match(dev, np, rmatch, ARRAY_SIZE(rmatch)); |
Sachin Kamat | cde248f | 2014-02-17 14:33:34 +0530 | [diff] [blame] | 350 | of_node_put(np); |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 351 | if (matched <= 0) |
| 352 | return matched; |
| 353 | |
| 354 | pdata->subdevs = devm_kzalloc(dev, sizeof(struct max8660_subdev_data) * |
| 355 | matched, GFP_KERNEL); |
| 356 | if (!pdata->subdevs) |
| 357 | return -ENOMEM; |
| 358 | |
| 359 | pdata->num_subdevs = matched; |
| 360 | sub = pdata->subdevs; |
| 361 | |
| 362 | for (i = 0; i < matched; i++) { |
| 363 | sub->id = i; |
| 364 | sub->name = rmatch[i].name; |
| 365 | sub->platform_data = rmatch[i].init_data; |
| 366 | of_node[i] = rmatch[i].of_node; |
| 367 | sub++; |
| 368 | } |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | #else |
| 373 | static inline int max8660_pdata_from_dt(struct device *dev, |
| 374 | struct device_node **of_node, |
Daniel Mack | d6c7e11 | 2013-08-08 12:02:18 +0200 | [diff] [blame] | 375 | struct max8660_platform_data *pdata) |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 376 | { |
| 377 | return 0; |
| 378 | } |
| 379 | #endif |
| 380 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 381 | static int max8660_probe(struct i2c_client *client, |
Dmitry Torokhov | 308f100 | 2010-02-23 23:38:28 -0800 | [diff] [blame] | 382 | const struct i2c_device_id *i2c_id) |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 383 | { |
Daniel Mack | de492e8 | 2013-08-02 10:19:16 +0200 | [diff] [blame] | 384 | struct device *dev = &client->dev; |
Colin Ian King | dfb85ba | 2015-04-07 23:06:46 +0100 | [diff] [blame] | 385 | struct max8660_platform_data pdata_of, *pdata = dev_get_platdata(dev); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 386 | struct regulator_config config = { }; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 387 | struct max8660 *max8660; |
| 388 | int boot_on, i, id, ret = -EINVAL; |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 389 | struct device_node *of_node[MAX8660_V_END]; |
Daniel Mack | d6c7e11 | 2013-08-08 12:02:18 +0200 | [diff] [blame] | 390 | unsigned long type; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 391 | |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 392 | if (dev->of_node && !pdata) { |
| 393 | const struct of_device_id *id; |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 394 | |
| 395 | id = of_match_device(of_match_ptr(max8660_dt_ids), dev); |
| 396 | if (!id) |
| 397 | return -ENODEV; |
| 398 | |
| 399 | ret = max8660_pdata_from_dt(dev, of_node, &pdata_of); |
| 400 | if (ret < 0) |
| 401 | return ret; |
| 402 | |
| 403 | pdata = &pdata_of; |
Daniel Mack | d6c7e11 | 2013-08-08 12:02:18 +0200 | [diff] [blame] | 404 | type = (unsigned long) id->data; |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 405 | } else { |
| 406 | type = i2c_id->driver_data; |
| 407 | memset(of_node, 0, sizeof(of_node)); |
| 408 | } |
| 409 | |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 410 | if (pdata->num_subdevs > MAX8660_V_END) { |
Daniel Mack | de492e8 | 2013-08-02 10:19:16 +0200 | [diff] [blame] | 411 | dev_err(dev, "Too many regulators found!\n"); |
Axel Lin | 4d26f7d | 2012-04-11 23:08:27 +0800 | [diff] [blame] | 412 | return -EINVAL; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 413 | } |
| 414 | |
Krzysztof Kozlowski | 67f76a9 | 2014-03-10 10:37:19 +0100 | [diff] [blame] | 415 | max8660 = devm_kzalloc(dev, sizeof(struct max8660), GFP_KERNEL); |
Axel Lin | 4d26f7d | 2012-04-11 23:08:27 +0800 | [diff] [blame] | 416 | if (!max8660) |
| 417 | return -ENOMEM; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 418 | |
| 419 | max8660->client = client; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 420 | |
| 421 | if (pdata->en34_is_high) { |
| 422 | /* Simulate always on */ |
| 423 | max8660->shadow_regs[MAX8660_OVER1] = 5; |
| 424 | } else { |
| 425 | /* Otherwise devices can be toggled via software */ |
| 426 | max8660_dcdc_ops.enable = max8660_dcdc_enable; |
| 427 | max8660_dcdc_ops.disable = max8660_dcdc_disable; |
| 428 | } |
| 429 | |
| 430 | /* |
| 431 | * First, set up shadow registers to prevent glitches. As some |
| 432 | * registers are shared between regulators, everything must be properly |
| 433 | * set up for all regulators in advance. |
| 434 | */ |
| 435 | max8660->shadow_regs[MAX8660_ADTV1] = |
| 436 | max8660->shadow_regs[MAX8660_ADTV2] = |
| 437 | max8660->shadow_regs[MAX8660_SDTV1] = |
| 438 | max8660->shadow_regs[MAX8660_SDTV2] = 0x1b; |
| 439 | max8660->shadow_regs[MAX8660_MDTV1] = |
| 440 | max8660->shadow_regs[MAX8660_MDTV2] = 0x04; |
| 441 | |
| 442 | for (i = 0; i < pdata->num_subdevs; i++) { |
| 443 | |
| 444 | if (!pdata->subdevs[i].platform_data) |
Markus Pargmann | c0cf5a59 | 2015-04-10 15:23:42 +0200 | [diff] [blame] | 445 | boot_on = false; |
| 446 | else |
| 447 | boot_on = pdata->subdevs[i].platform_data->constraints.boot_on; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 448 | |
| 449 | switch (pdata->subdevs[i].id) { |
| 450 | case MAX8660_V3: |
| 451 | if (boot_on) |
| 452 | max8660->shadow_regs[MAX8660_OVER1] |= 1; |
| 453 | break; |
| 454 | |
| 455 | case MAX8660_V4: |
| 456 | if (boot_on) |
| 457 | max8660->shadow_regs[MAX8660_OVER1] |= 4; |
| 458 | break; |
| 459 | |
| 460 | case MAX8660_V5: |
| 461 | break; |
| 462 | |
| 463 | case MAX8660_V6: |
| 464 | if (boot_on) |
| 465 | max8660->shadow_regs[MAX8660_OVER2] |= 2; |
| 466 | break; |
| 467 | |
| 468 | case MAX8660_V7: |
Daniel Mack | 4a678f0 | 2013-08-02 10:19:15 +0200 | [diff] [blame] | 469 | if (type == MAX8661) { |
Daniel Mack | de492e8 | 2013-08-02 10:19:16 +0200 | [diff] [blame] | 470 | dev_err(dev, "Regulator not on this chip!\n"); |
Sachin Kamat | dcfb6b5 | 2013-09-04 11:07:58 +0530 | [diff] [blame] | 471 | return -EINVAL; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | if (boot_on) |
| 475 | max8660->shadow_regs[MAX8660_OVER2] |= 4; |
| 476 | break; |
| 477 | |
| 478 | default: |
Daniel Mack | de492e8 | 2013-08-02 10:19:16 +0200 | [diff] [blame] | 479 | dev_err(dev, "invalid regulator %s\n", |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 480 | pdata->subdevs[i].name); |
Sachin Kamat | dcfb6b5 | 2013-09-04 11:07:58 +0530 | [diff] [blame] | 481 | return ret; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 482 | } |
| 483 | } |
| 484 | |
| 485 | /* Finally register devices */ |
| 486 | for (i = 0; i < pdata->num_subdevs; i++) { |
Krzysztof Kozlowski | 67f76a9 | 2014-03-10 10:37:19 +0100 | [diff] [blame] | 487 | struct regulator_dev *rdev; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 488 | |
| 489 | id = pdata->subdevs[i].id; |
| 490 | |
Daniel Mack | de492e8 | 2013-08-02 10:19:16 +0200 | [diff] [blame] | 491 | config.dev = dev; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 492 | config.init_data = pdata->subdevs[i].platform_data; |
Daniel Mack | abe4c51 | 2013-08-02 12:59:44 +0200 | [diff] [blame] | 493 | config.of_node = of_node[i]; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 494 | config.driver_data = max8660; |
| 495 | |
Krzysztof Kozlowski | 67f76a9 | 2014-03-10 10:37:19 +0100 | [diff] [blame] | 496 | rdev = devm_regulator_register(&client->dev, |
Sachin Kamat | dcfb6b5 | 2013-09-04 11:07:58 +0530 | [diff] [blame] | 497 | &max8660_reg[id], &config); |
Krzysztof Kozlowski | 67f76a9 | 2014-03-10 10:37:19 +0100 | [diff] [blame] | 498 | if (IS_ERR(rdev)) { |
| 499 | ret = PTR_ERR(rdev); |
Sachin Kamat | dcfb6b5 | 2013-09-04 11:07:58 +0530 | [diff] [blame] | 500 | dev_err(&client->dev, "failed to register %s\n", |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 501 | max8660_reg[id].name); |
Krzysztof Kozlowski | 67f76a9 | 2014-03-10 10:37:19 +0100 | [diff] [blame] | 502 | return PTR_ERR(rdev); |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Axel Lin | 53a4bef | 2010-08-06 13:35:27 +0800 | [diff] [blame] | 506 | i2c_set_clientdata(client, max8660); |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 507 | return 0; |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static const struct i2c_device_id max8660_id[] = { |
Daniel Mack | 4a678f0 | 2013-08-02 10:19:15 +0200 | [diff] [blame] | 511 | { .name = "max8660", .driver_data = MAX8660 }, |
| 512 | { .name = "max8661", .driver_data = MAX8661 }, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 513 | { } |
| 514 | }; |
| 515 | MODULE_DEVICE_TABLE(i2c, max8660_id); |
| 516 | |
| 517 | static struct i2c_driver max8660_driver = { |
| 518 | .probe = max8660_probe, |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 519 | .driver = { |
| 520 | .name = "max8660", |
Wolfram Sang | 27f37e4 | 2009-09-25 09:39:26 +0200 | [diff] [blame] | 521 | }, |
| 522 | .id_table = max8660_id, |
| 523 | }; |
| 524 | |
| 525 | static int __init max8660_init(void) |
| 526 | { |
| 527 | return i2c_add_driver(&max8660_driver); |
| 528 | } |
| 529 | subsys_initcall(max8660_init); |
| 530 | |
| 531 | static void __exit max8660_exit(void) |
| 532 | { |
| 533 | i2c_del_driver(&max8660_driver); |
| 534 | } |
| 535 | module_exit(max8660_exit); |
| 536 | |
| 537 | /* Module information */ |
| 538 | MODULE_DESCRIPTION("MAXIM 8660/8661 voltage regulator driver"); |
| 539 | MODULE_AUTHOR("Wolfram Sang"); |
| 540 | MODULE_LICENSE("GPL v2"); |