Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 1 | /* |
| 2 | * tps6507x-regulator.c |
| 3 | * |
| 4 | * Regulator driver for TPS65073 PMIC |
| 5 | * |
| 6 | * Copyright (C) 2009 Texas Instrument Incorporated - http://www.ti.com/ |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU General Public License as |
| 10 | * published by the Free Software Foundation version 2. |
| 11 | * |
| 12 | * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind, |
| 13 | * whether express or implied; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/kernel.h> |
| 19 | #include <linux/module.h> |
| 20 | #include <linux/init.h> |
| 21 | #include <linux/err.h> |
| 22 | #include <linux/platform_device.h> |
| 23 | #include <linux/regulator/driver.h> |
| 24 | #include <linux/regulator/machine.h> |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 25 | #include <linux/regulator/tps6507x.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 26 | #include <linux/slab.h> |
Todd Fischer | d183fcc | 2010-04-05 20:23:56 -0600 | [diff] [blame] | 27 | #include <linux/mfd/tps6507x.h> |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 28 | |
| 29 | /* DCDC's */ |
| 30 | #define TPS6507X_DCDC_1 0 |
| 31 | #define TPS6507X_DCDC_2 1 |
| 32 | #define TPS6507X_DCDC_3 2 |
| 33 | /* LDOs */ |
| 34 | #define TPS6507X_LDO_1 3 |
| 35 | #define TPS6507X_LDO_2 4 |
| 36 | |
| 37 | #define TPS6507X_MAX_REG_ID TPS6507X_LDO_2 |
| 38 | |
| 39 | /* Number of step-down converters available */ |
| 40 | #define TPS6507X_NUM_DCDC 3 |
| 41 | /* Number of LDO voltage regulators available */ |
| 42 | #define TPS6507X_NUM_LDO 2 |
| 43 | /* Number of total regulators available */ |
| 44 | #define TPS6507X_NUM_REGULATOR (TPS6507X_NUM_DCDC + TPS6507X_NUM_LDO) |
| 45 | |
| 46 | /* Supported voltage values for regulators (in milliVolts) */ |
| 47 | static const u16 VDCDCx_VSEL_table[] = { |
| 48 | 725, 750, 775, 800, |
| 49 | 825, 850, 875, 900, |
| 50 | 925, 950, 975, 1000, |
| 51 | 1025, 1050, 1075, 1100, |
| 52 | 1125, 1150, 1175, 1200, |
| 53 | 1225, 1250, 1275, 1300, |
| 54 | 1325, 1350, 1375, 1400, |
| 55 | 1425, 1450, 1475, 1500, |
| 56 | 1550, 1600, 1650, 1700, |
| 57 | 1750, 1800, 1850, 1900, |
| 58 | 1950, 2000, 2050, 2100, |
| 59 | 2150, 2200, 2250, 2300, |
| 60 | 2350, 2400, 2450, 2500, |
| 61 | 2550, 2600, 2650, 2700, |
| 62 | 2750, 2800, 2850, 2900, |
| 63 | 3000, 3100, 3200, 3300, |
| 64 | }; |
| 65 | |
| 66 | static const u16 LDO1_VSEL_table[] = { |
| 67 | 1000, 1100, 1200, 1250, |
| 68 | 1300, 1350, 1400, 1500, |
| 69 | 1600, 1800, 2500, 2750, |
| 70 | 2800, 3000, 3100, 3300, |
| 71 | }; |
| 72 | |
| 73 | static const u16 LDO2_VSEL_table[] = { |
| 74 | 725, 750, 775, 800, |
| 75 | 825, 850, 875, 900, |
| 76 | 925, 950, 975, 1000, |
| 77 | 1025, 1050, 1075, 1100, |
| 78 | 1125, 1150, 1175, 1200, |
| 79 | 1225, 1250, 1275, 1300, |
| 80 | 1325, 1350, 1375, 1400, |
| 81 | 1425, 1450, 1475, 1500, |
| 82 | 1550, 1600, 1650, 1700, |
| 83 | 1750, 1800, 1850, 1900, |
| 84 | 1950, 2000, 2050, 2100, |
| 85 | 2150, 2200, 2250, 2300, |
| 86 | 2350, 2400, 2450, 2500, |
| 87 | 2550, 2600, 2650, 2700, |
| 88 | 2750, 2800, 2850, 2900, |
| 89 | 3000, 3100, 3200, 3300, |
| 90 | }; |
| 91 | |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 92 | struct tps_info { |
| 93 | const char *name; |
| 94 | unsigned min_uV; |
| 95 | unsigned max_uV; |
| 96 | u8 table_len; |
| 97 | const u16 *table; |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 98 | |
| 99 | /* Does DCDC high or the low register defines output voltage? */ |
| 100 | bool defdcdc_default; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 101 | }; |
| 102 | |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 103 | static struct tps_info tps6507x_pmic_regs[] = { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 104 | { |
| 105 | .name = "VDCDC1", |
| 106 | .min_uV = 725000, |
| 107 | .max_uV = 3300000, |
| 108 | .table_len = ARRAY_SIZE(VDCDCx_VSEL_table), |
| 109 | .table = VDCDCx_VSEL_table, |
| 110 | }, |
| 111 | { |
| 112 | .name = "VDCDC2", |
| 113 | .min_uV = 725000, |
| 114 | .max_uV = 3300000, |
| 115 | .table_len = ARRAY_SIZE(VDCDCx_VSEL_table), |
| 116 | .table = VDCDCx_VSEL_table, |
| 117 | }, |
| 118 | { |
| 119 | .name = "VDCDC3", |
| 120 | .min_uV = 725000, |
| 121 | .max_uV = 3300000, |
| 122 | .table_len = ARRAY_SIZE(VDCDCx_VSEL_table), |
| 123 | .table = VDCDCx_VSEL_table, |
| 124 | }, |
| 125 | { |
| 126 | .name = "LDO1", |
| 127 | .min_uV = 1000000, |
| 128 | .max_uV = 3300000, |
| 129 | .table_len = ARRAY_SIZE(LDO1_VSEL_table), |
| 130 | .table = LDO1_VSEL_table, |
| 131 | }, |
| 132 | { |
| 133 | .name = "LDO2", |
| 134 | .min_uV = 725000, |
| 135 | .max_uV = 3300000, |
| 136 | .table_len = ARRAY_SIZE(LDO2_VSEL_table), |
| 137 | .table = LDO2_VSEL_table, |
| 138 | }, |
| 139 | }; |
| 140 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 141 | struct tps6507x_pmic { |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 142 | struct regulator_desc desc[TPS6507X_NUM_REGULATOR]; |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 143 | struct tps6507x_dev *mfd; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 144 | struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR]; |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 145 | struct tps_info *info[TPS6507X_NUM_REGULATOR]; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 146 | struct mutex io_lock; |
| 147 | }; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 148 | static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 149 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 150 | u8 val; |
| 151 | int err; |
| 152 | |
| 153 | err = tps->mfd->read_dev(tps->mfd, reg, 1, &val); |
| 154 | |
| 155 | if (err) |
| 156 | return err; |
| 157 | |
| 158 | return val; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 159 | } |
| 160 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 161 | static inline int tps6507x_pmic_write(struct tps6507x_pmic *tps, u8 reg, u8 val) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 162 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 163 | return tps->mfd->write_dev(tps->mfd, reg, 1, &val); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 164 | } |
| 165 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 166 | static int tps6507x_pmic_set_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 167 | { |
| 168 | int err, data; |
| 169 | |
| 170 | mutex_lock(&tps->io_lock); |
| 171 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 172 | data = tps6507x_pmic_read(tps, reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 173 | if (data < 0) { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 174 | dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 175 | err = data; |
| 176 | goto out; |
| 177 | } |
| 178 | |
| 179 | data |= mask; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 180 | err = tps6507x_pmic_write(tps, reg, data); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 181 | if (err) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 182 | dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 183 | |
| 184 | out: |
| 185 | mutex_unlock(&tps->io_lock); |
| 186 | return err; |
| 187 | } |
| 188 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 189 | static int tps6507x_pmic_clear_bits(struct tps6507x_pmic *tps, u8 reg, u8 mask) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 190 | { |
| 191 | int err, data; |
| 192 | |
| 193 | mutex_lock(&tps->io_lock); |
| 194 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 195 | data = tps6507x_pmic_read(tps, reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 196 | if (data < 0) { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 197 | dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 198 | err = data; |
| 199 | goto out; |
| 200 | } |
| 201 | |
| 202 | data &= ~mask; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 203 | err = tps6507x_pmic_write(tps, reg, data); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 204 | if (err) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 205 | dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 206 | |
| 207 | out: |
| 208 | mutex_unlock(&tps->io_lock); |
| 209 | return err; |
| 210 | } |
| 211 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 212 | static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 213 | { |
| 214 | int data; |
| 215 | |
| 216 | mutex_lock(&tps->io_lock); |
| 217 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 218 | data = tps6507x_pmic_read(tps, reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 219 | if (data < 0) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 220 | dev_err(tps->mfd->dev, "Read from reg 0x%x failed\n", reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 221 | |
| 222 | mutex_unlock(&tps->io_lock); |
| 223 | return data; |
| 224 | } |
| 225 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 226 | static int tps6507x_pmic_reg_write(struct tps6507x_pmic *tps, u8 reg, u8 val) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 227 | { |
| 228 | int err; |
| 229 | |
| 230 | mutex_lock(&tps->io_lock); |
| 231 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 232 | err = tps6507x_pmic_write(tps, reg, val); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 233 | if (err < 0) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 234 | dev_err(tps->mfd->dev, "Write for reg 0x%x failed\n", reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 235 | |
| 236 | mutex_unlock(&tps->io_lock); |
| 237 | return err; |
| 238 | } |
| 239 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 240 | static int tps6507x_pmic_is_enabled(struct regulator_dev *dev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 241 | { |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 242 | struct tps6507x_pmic *tps = rdev_get_drvdata(dev); |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 243 | int data, rid = rdev_get_id(dev); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 244 | u8 shift; |
| 245 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 246 | if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 247 | return -EINVAL; |
| 248 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 249 | shift = TPS6507X_MAX_REG_ID - rid; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 250 | data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 251 | |
| 252 | if (data < 0) |
| 253 | return data; |
| 254 | else |
| 255 | return (data & 1<<shift) ? 1 : 0; |
| 256 | } |
| 257 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 258 | static int tps6507x_pmic_enable(struct regulator_dev *dev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 259 | { |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 260 | struct tps6507x_pmic *tps = rdev_get_drvdata(dev); |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 261 | int rid = rdev_get_id(dev); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 262 | u8 shift; |
| 263 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 264 | if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 265 | return -EINVAL; |
| 266 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 267 | shift = TPS6507X_MAX_REG_ID - rid; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 268 | return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 269 | } |
| 270 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 271 | static int tps6507x_pmic_disable(struct regulator_dev *dev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 272 | { |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 273 | struct tps6507x_pmic *tps = rdev_get_drvdata(dev); |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 274 | int rid = rdev_get_id(dev); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 275 | u8 shift; |
| 276 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 277 | if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 278 | return -EINVAL; |
| 279 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 280 | shift = TPS6507X_MAX_REG_ID - rid; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 281 | return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1, |
| 282 | 1 << shift); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 283 | } |
| 284 | |
Axel Lin | 7c842a1 | 2012-04-20 18:33:52 +0800 | [diff] [blame] | 285 | static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 286 | { |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 287 | struct tps6507x_pmic *tps = rdev_get_drvdata(dev); |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 288 | int data, rid = rdev_get_id(dev); |
| 289 | u8 reg, mask; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 290 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 291 | switch (rid) { |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 292 | case TPS6507X_DCDC_1: |
| 293 | reg = TPS6507X_REG_DEFDCDC1; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 294 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 295 | break; |
| 296 | case TPS6507X_DCDC_2: |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 297 | if (tps->info[rid]->defdcdc_default) |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 298 | reg = TPS6507X_REG_DEFDCDC2_HIGH; |
| 299 | else |
| 300 | reg = TPS6507X_REG_DEFDCDC2_LOW; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 301 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 302 | break; |
| 303 | case TPS6507X_DCDC_3: |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 304 | if (tps->info[rid]->defdcdc_default) |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 305 | reg = TPS6507X_REG_DEFDCDC3_HIGH; |
| 306 | else |
| 307 | reg = TPS6507X_REG_DEFDCDC3_LOW; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 308 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
| 309 | break; |
| 310 | case TPS6507X_LDO_1: |
| 311 | reg = TPS6507X_REG_LDO_CTRL1; |
| 312 | mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK; |
| 313 | break; |
| 314 | case TPS6507X_LDO_2: |
| 315 | reg = TPS6507X_REG_DEFLDO2; |
| 316 | mask = TPS6507X_REG_DEFLDO2_LDO2_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 317 | break; |
| 318 | default: |
| 319 | return -EINVAL; |
| 320 | } |
| 321 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 322 | data = tps6507x_pmic_reg_read(tps, reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 323 | if (data < 0) |
| 324 | return data; |
| 325 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 326 | data &= mask; |
Axel Lin | 7c842a1 | 2012-04-20 18:33:52 +0800 | [diff] [blame] | 327 | return data; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 328 | } |
| 329 | |
Axel Lin | ca61a7b | 2012-03-12 15:59:54 +0800 | [diff] [blame] | 330 | static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev, |
| 331 | unsigned selector) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 332 | { |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 333 | struct tps6507x_pmic *tps = rdev_get_drvdata(dev); |
Axel Lin | ca61a7b | 2012-03-12 15:59:54 +0800 | [diff] [blame] | 334 | int data, rid = rdev_get_id(dev); |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 335 | u8 reg, mask; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 336 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 337 | switch (rid) { |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 338 | case TPS6507X_DCDC_1: |
| 339 | reg = TPS6507X_REG_DEFDCDC1; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 340 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 341 | break; |
| 342 | case TPS6507X_DCDC_2: |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 343 | if (tps->info[rid]->defdcdc_default) |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 344 | reg = TPS6507X_REG_DEFDCDC2_HIGH; |
| 345 | else |
| 346 | reg = TPS6507X_REG_DEFDCDC2_LOW; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 347 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 348 | break; |
| 349 | case TPS6507X_DCDC_3: |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 350 | if (tps->info[rid]->defdcdc_default) |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 351 | reg = TPS6507X_REG_DEFDCDC3_HIGH; |
| 352 | else |
| 353 | reg = TPS6507X_REG_DEFDCDC3_LOW; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 354 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
| 355 | break; |
| 356 | case TPS6507X_LDO_1: |
| 357 | reg = TPS6507X_REG_LDO_CTRL1; |
| 358 | mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK; |
| 359 | break; |
| 360 | case TPS6507X_LDO_2: |
| 361 | reg = TPS6507X_REG_DEFLDO2; |
| 362 | mask = TPS6507X_REG_DEFLDO2_LDO2_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 363 | break; |
| 364 | default: |
| 365 | return -EINVAL; |
| 366 | } |
| 367 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 368 | data = tps6507x_pmic_reg_read(tps, reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 369 | if (data < 0) |
| 370 | return data; |
| 371 | |
| 372 | data &= ~mask; |
Axel Lin | ca61a7b | 2012-03-12 15:59:54 +0800 | [diff] [blame] | 373 | data |= selector; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 374 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 375 | return tps6507x_pmic_reg_write(tps, reg, data); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 376 | } |
| 377 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 378 | static int tps6507x_pmic_list_voltage(struct regulator_dev *dev, |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 379 | unsigned selector) |
| 380 | { |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 381 | struct tps6507x_pmic *tps = rdev_get_drvdata(dev); |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 382 | int rid = rdev_get_id(dev); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 383 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 384 | if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 385 | return -EINVAL; |
| 386 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 387 | if (selector >= tps->info[rid]->table_len) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 388 | return -EINVAL; |
| 389 | else |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 390 | return tps->info[rid]->table[selector] * 1000; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 391 | } |
| 392 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 393 | static struct regulator_ops tps6507x_pmic_ops = { |
| 394 | .is_enabled = tps6507x_pmic_is_enabled, |
| 395 | .enable = tps6507x_pmic_enable, |
| 396 | .disable = tps6507x_pmic_disable, |
Axel Lin | 7c842a1 | 2012-04-20 18:33:52 +0800 | [diff] [blame] | 397 | .get_voltage_sel = tps6507x_pmic_get_voltage_sel, |
Axel Lin | ca61a7b | 2012-03-12 15:59:54 +0800 | [diff] [blame] | 398 | .set_voltage_sel = tps6507x_pmic_set_voltage_sel, |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 399 | .list_voltage = tps6507x_pmic_list_voltage, |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 400 | }; |
| 401 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 402 | static __devinit int tps6507x_pmic_probe(struct platform_device *pdev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 403 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 404 | struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent); |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 405 | struct tps_info *info = &tps6507x_pmic_regs[0]; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 406 | struct regulator_config config = { }; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 407 | struct regulator_init_data *init_data; |
| 408 | struct regulator_dev *rdev; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 409 | struct tps6507x_pmic *tps; |
Todd Fischer | 0bc20bb | 2010-04-05 20:23:57 -0600 | [diff] [blame] | 410 | struct tps6507x_board *tps_board; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 411 | int i; |
Dmitry Torokhov | 56c2349 | 2010-02-23 23:38:12 -0800 | [diff] [blame] | 412 | int error; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 413 | |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 414 | /** |
Todd Fischer | 0bc20bb | 2010-04-05 20:23:57 -0600 | [diff] [blame] | 415 | * tps_board points to pmic related constants |
| 416 | * coming from the board-evm file. |
| 417 | */ |
| 418 | |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 419 | tps_board = dev_get_platdata(tps6507x_dev->dev); |
Todd Fischer | 0bc20bb | 2010-04-05 20:23:57 -0600 | [diff] [blame] | 420 | if (!tps_board) |
| 421 | return -EINVAL; |
| 422 | |
| 423 | /** |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 424 | * init_data points to array of regulator_init structures |
| 425 | * coming from the board-evm file. |
| 426 | */ |
Todd Fischer | 0bc20bb | 2010-04-05 20:23:57 -0600 | [diff] [blame] | 427 | init_data = tps_board->tps6507x_pmic_init_data; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 428 | if (!init_data) |
Todd Fischer | 0bc20bb | 2010-04-05 20:23:57 -0600 | [diff] [blame] | 429 | return -EINVAL; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 430 | |
Axel Lin | 9eb0c42 | 2012-04-11 14:40:18 +0800 | [diff] [blame] | 431 | tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 432 | if (!tps) |
| 433 | return -ENOMEM; |
| 434 | |
| 435 | mutex_init(&tps->io_lock); |
| 436 | |
| 437 | /* common for all regulators */ |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 438 | tps->mfd = tps6507x_dev; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 439 | |
| 440 | for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) { |
| 441 | /* Register the regulators */ |
| 442 | tps->info[i] = info; |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 443 | if (init_data->driver_data) { |
| 444 | struct tps6507x_reg_platform_data *data = |
| 445 | init_data->driver_data; |
| 446 | tps->info[i]->defdcdc_default = data->defdcdc_default; |
| 447 | } |
| 448 | |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 449 | tps->desc[i].name = info->name; |
Axel Lin | 77fa44d | 2011-05-12 13:47:50 +0800 | [diff] [blame] | 450 | tps->desc[i].id = i; |
Axel Lin | 0fcdb10 | 2011-08-02 15:34:12 +0800 | [diff] [blame] | 451 | tps->desc[i].n_voltages = info->table_len; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 452 | tps->desc[i].ops = &tps6507x_pmic_ops; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 453 | tps->desc[i].type = REGULATOR_VOLTAGE; |
| 454 | tps->desc[i].owner = THIS_MODULE; |
| 455 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 456 | config.dev = tps6507x_dev->dev; |
| 457 | config.init_data = init_data; |
| 458 | config.driver_data = tps; |
| 459 | |
| 460 | rdev = regulator_register(&tps->desc[i], &config); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 461 | if (IS_ERR(rdev)) { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 462 | dev_err(tps6507x_dev->dev, |
| 463 | "failed to register %s regulator\n", |
| 464 | pdev->name); |
Dmitry Torokhov | 56c2349 | 2010-02-23 23:38:12 -0800 | [diff] [blame] | 465 | error = PTR_ERR(rdev); |
| 466 | goto fail; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | /* Save regulator for cleanup */ |
| 470 | tps->rdev[i] = rdev; |
| 471 | } |
| 472 | |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 473 | tps6507x_dev->pmic = tps; |
Axel Lin | d7399fa | 2010-08-09 15:51:23 +0800 | [diff] [blame] | 474 | platform_set_drvdata(pdev, tps6507x_dev); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 475 | |
| 476 | return 0; |
Dmitry Torokhov | 56c2349 | 2010-02-23 23:38:12 -0800 | [diff] [blame] | 477 | |
| 478 | fail: |
| 479 | while (--i >= 0) |
| 480 | regulator_unregister(tps->rdev[i]); |
Dmitry Torokhov | 56c2349 | 2010-02-23 23:38:12 -0800 | [diff] [blame] | 481 | return error; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 482 | } |
| 483 | |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 484 | static int __devexit tps6507x_pmic_remove(struct platform_device *pdev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 485 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 486 | struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev); |
| 487 | struct tps6507x_pmic *tps = tps6507x_dev->pmic; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 488 | int i; |
| 489 | |
| 490 | for (i = 0; i < TPS6507X_NUM_REGULATOR; i++) |
| 491 | regulator_unregister(tps->rdev[i]); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 495 | static struct platform_driver tps6507x_pmic_driver = { |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 496 | .driver = { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 497 | .name = "tps6507x-pmic", |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 498 | .owner = THIS_MODULE, |
| 499 | }, |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 500 | .probe = tps6507x_pmic_probe, |
| 501 | .remove = __devexit_p(tps6507x_pmic_remove), |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 502 | }; |
| 503 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 504 | static int __init tps6507x_pmic_init(void) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 505 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 506 | return platform_driver_register(&tps6507x_pmic_driver); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 507 | } |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 508 | subsys_initcall(tps6507x_pmic_init); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 509 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 510 | static void __exit tps6507x_pmic_cleanup(void) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 511 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 512 | platform_driver_unregister(&tps6507x_pmic_driver); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 513 | } |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 514 | module_exit(tps6507x_pmic_cleanup); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 515 | |
| 516 | MODULE_AUTHOR("Texas Instruments"); |
| 517 | MODULE_DESCRIPTION("TPS6507x voltage regulator driver"); |
Liam Girdwood | 9e108d3 | 2009-08-24 10:31:34 +0100 | [diff] [blame] | 518 | MODULE_LICENSE("GPL v2"); |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 519 | MODULE_ALIAS("platform:tps6507x-pmic"); |