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 | |
Axel Lin | 055917a | 2012-05-20 10:39:12 +0800 | [diff] [blame] | 46 | /* Supported voltage values for regulators (in microVolts) */ |
| 47 | static const unsigned int VDCDCx_VSEL_table[] = { |
| 48 | 725000, 750000, 775000, 800000, |
| 49 | 825000, 850000, 875000, 900000, |
| 50 | 925000, 950000, 975000, 1000000, |
| 51 | 1025000, 1050000, 1075000, 1100000, |
| 52 | 1125000, 1150000, 1175000, 1200000, |
| 53 | 1225000, 1250000, 1275000, 1300000, |
| 54 | 1325000, 1350000, 1375000, 1400000, |
| 55 | 1425000, 1450000, 1475000, 1500000, |
| 56 | 1550000, 1600000, 1650000, 1700000, |
| 57 | 1750000, 1800000, 1850000, 1900000, |
| 58 | 1950000, 2000000, 2050000, 2100000, |
| 59 | 2150000, 2200000, 2250000, 2300000, |
| 60 | 2350000, 2400000, 2450000, 2500000, |
| 61 | 2550000, 2600000, 2650000, 2700000, |
| 62 | 2750000, 2800000, 2850000, 2900000, |
| 63 | 3000000, 3100000, 3200000, 3300000, |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 64 | }; |
| 65 | |
Axel Lin | 055917a | 2012-05-20 10:39:12 +0800 | [diff] [blame] | 66 | static const unsigned int LDO1_VSEL_table[] = { |
| 67 | 1000000, 1100000, 1200000, 1250000, |
| 68 | 1300000, 1350000, 1400000, 1500000, |
| 69 | 1600000, 1800000, 2500000, 2750000, |
| 70 | 2800000, 3000000, 3100000, 3300000, |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 71 | }; |
| 72 | |
Axel Lin | 93b07e7 | 2012-06-05 08:58:13 +0800 | [diff] [blame] | 73 | /* The voltage mapping table for LDO2 is the same as VDCDCx */ |
| 74 | #define LDO2_VSEL_table VDCDCx_VSEL_table |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 75 | |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 76 | struct tps_info { |
| 77 | const char *name; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 78 | u8 table_len; |
Axel Lin | 055917a | 2012-05-20 10:39:12 +0800 | [diff] [blame] | 79 | const unsigned int *table; |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 80 | |
| 81 | /* Does DCDC high or the low register defines output voltage? */ |
| 82 | bool defdcdc_default; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 83 | }; |
| 84 | |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 85 | static struct tps_info tps6507x_pmic_regs[] = { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 86 | { |
| 87 | .name = "VDCDC1", |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 88 | .table_len = ARRAY_SIZE(VDCDCx_VSEL_table), |
| 89 | .table = VDCDCx_VSEL_table, |
| 90 | }, |
| 91 | { |
| 92 | .name = "VDCDC2", |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 93 | .table_len = ARRAY_SIZE(VDCDCx_VSEL_table), |
| 94 | .table = VDCDCx_VSEL_table, |
| 95 | }, |
| 96 | { |
| 97 | .name = "VDCDC3", |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 98 | .table_len = ARRAY_SIZE(VDCDCx_VSEL_table), |
| 99 | .table = VDCDCx_VSEL_table, |
| 100 | }, |
| 101 | { |
| 102 | .name = "LDO1", |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 103 | .table_len = ARRAY_SIZE(LDO1_VSEL_table), |
| 104 | .table = LDO1_VSEL_table, |
| 105 | }, |
| 106 | { |
| 107 | .name = "LDO2", |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 108 | .table_len = ARRAY_SIZE(LDO2_VSEL_table), |
| 109 | .table = LDO2_VSEL_table, |
| 110 | }, |
| 111 | }; |
| 112 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 113 | struct tps6507x_pmic { |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 114 | struct regulator_desc desc[TPS6507X_NUM_REGULATOR]; |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 115 | struct tps6507x_dev *mfd; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 116 | struct regulator_dev *rdev[TPS6507X_NUM_REGULATOR]; |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 117 | struct tps_info *info[TPS6507X_NUM_REGULATOR]; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 118 | struct mutex io_lock; |
| 119 | }; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 120 | static inline int tps6507x_pmic_read(struct tps6507x_pmic *tps, u8 reg) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 121 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 122 | u8 val; |
| 123 | int err; |
| 124 | |
| 125 | err = tps->mfd->read_dev(tps->mfd, reg, 1, &val); |
| 126 | |
| 127 | if (err) |
| 128 | return err; |
| 129 | |
| 130 | return val; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 131 | } |
| 132 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 133 | 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] | 134 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 135 | return tps->mfd->write_dev(tps->mfd, reg, 1, &val); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 136 | } |
| 137 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 138 | 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] | 139 | { |
| 140 | int err, data; |
| 141 | |
| 142 | mutex_lock(&tps->io_lock); |
| 143 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 144 | data = tps6507x_pmic_read(tps, reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 145 | if (data < 0) { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 146 | 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] | 147 | err = data; |
| 148 | goto out; |
| 149 | } |
| 150 | |
| 151 | data |= mask; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 152 | err = tps6507x_pmic_write(tps, reg, data); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 153 | if (err) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 154 | 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] | 155 | |
| 156 | out: |
| 157 | mutex_unlock(&tps->io_lock); |
| 158 | return err; |
| 159 | } |
| 160 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 161 | 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] | 162 | { |
| 163 | int err, data; |
| 164 | |
| 165 | mutex_lock(&tps->io_lock); |
| 166 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 167 | data = tps6507x_pmic_read(tps, reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 168 | if (data < 0) { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 169 | 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] | 170 | err = data; |
| 171 | goto out; |
| 172 | } |
| 173 | |
| 174 | data &= ~mask; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 175 | err = tps6507x_pmic_write(tps, reg, data); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 176 | if (err) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 177 | 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] | 178 | |
| 179 | out: |
| 180 | mutex_unlock(&tps->io_lock); |
| 181 | return err; |
| 182 | } |
| 183 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 184 | static int tps6507x_pmic_reg_read(struct tps6507x_pmic *tps, u8 reg) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 185 | { |
| 186 | int data; |
| 187 | |
| 188 | mutex_lock(&tps->io_lock); |
| 189 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 190 | data = tps6507x_pmic_read(tps, reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 191 | if (data < 0) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 192 | 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] | 193 | |
| 194 | mutex_unlock(&tps->io_lock); |
| 195 | return data; |
| 196 | } |
| 197 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 198 | 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] | 199 | { |
| 200 | int err; |
| 201 | |
| 202 | mutex_lock(&tps->io_lock); |
| 203 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 204 | err = tps6507x_pmic_write(tps, reg, val); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 205 | if (err < 0) |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 206 | 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] | 207 | |
| 208 | mutex_unlock(&tps->io_lock); |
| 209 | return err; |
| 210 | } |
| 211 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 212 | static int tps6507x_pmic_is_enabled(struct regulator_dev *dev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 213 | { |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 214 | struct tps6507x_pmic *tps = rdev_get_drvdata(dev); |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 215 | int data, rid = rdev_get_id(dev); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 216 | u8 shift; |
| 217 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 218 | if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 219 | return -EINVAL; |
| 220 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 221 | shift = TPS6507X_MAX_REG_ID - rid; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 222 | data = tps6507x_pmic_reg_read(tps, TPS6507X_REG_CON_CTRL1); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 223 | |
| 224 | if (data < 0) |
| 225 | return data; |
| 226 | else |
| 227 | return (data & 1<<shift) ? 1 : 0; |
| 228 | } |
| 229 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 230 | static int tps6507x_pmic_enable(struct regulator_dev *dev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 231 | { |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 232 | struct tps6507x_pmic *tps = rdev_get_drvdata(dev); |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 233 | int rid = rdev_get_id(dev); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 234 | u8 shift; |
| 235 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 236 | if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 237 | return -EINVAL; |
| 238 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 239 | shift = TPS6507X_MAX_REG_ID - rid; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 240 | return tps6507x_pmic_set_bits(tps, TPS6507X_REG_CON_CTRL1, 1 << shift); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 241 | } |
| 242 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 243 | static int tps6507x_pmic_disable(struct regulator_dev *dev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 244 | { |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 245 | struct tps6507x_pmic *tps = rdev_get_drvdata(dev); |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 246 | int rid = rdev_get_id(dev); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 247 | u8 shift; |
| 248 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 249 | if (rid < TPS6507X_DCDC_1 || rid > TPS6507X_LDO_2) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 250 | return -EINVAL; |
| 251 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 252 | shift = TPS6507X_MAX_REG_ID - rid; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 253 | return tps6507x_pmic_clear_bits(tps, TPS6507X_REG_CON_CTRL1, |
| 254 | 1 << shift); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 255 | } |
| 256 | |
Axel Lin | 7c842a1 | 2012-04-20 18:33:52 +0800 | [diff] [blame] | 257 | static int tps6507x_pmic_get_voltage_sel(struct regulator_dev *dev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 258 | { |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 259 | struct tps6507x_pmic *tps = rdev_get_drvdata(dev); |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 260 | int data, rid = rdev_get_id(dev); |
| 261 | u8 reg, mask; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 262 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 263 | switch (rid) { |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 264 | case TPS6507X_DCDC_1: |
| 265 | reg = TPS6507X_REG_DEFDCDC1; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 266 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 267 | break; |
| 268 | case TPS6507X_DCDC_2: |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 269 | if (tps->info[rid]->defdcdc_default) |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 270 | reg = TPS6507X_REG_DEFDCDC2_HIGH; |
| 271 | else |
| 272 | reg = TPS6507X_REG_DEFDCDC2_LOW; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 273 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 274 | break; |
| 275 | case TPS6507X_DCDC_3: |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 276 | if (tps->info[rid]->defdcdc_default) |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 277 | reg = TPS6507X_REG_DEFDCDC3_HIGH; |
| 278 | else |
| 279 | reg = TPS6507X_REG_DEFDCDC3_LOW; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 280 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
| 281 | break; |
| 282 | case TPS6507X_LDO_1: |
| 283 | reg = TPS6507X_REG_LDO_CTRL1; |
| 284 | mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK; |
| 285 | break; |
| 286 | case TPS6507X_LDO_2: |
| 287 | reg = TPS6507X_REG_DEFLDO2; |
| 288 | mask = TPS6507X_REG_DEFLDO2_LDO2_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 289 | break; |
| 290 | default: |
| 291 | return -EINVAL; |
| 292 | } |
| 293 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 294 | data = tps6507x_pmic_reg_read(tps, reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 295 | if (data < 0) |
| 296 | return data; |
| 297 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 298 | data &= mask; |
Axel Lin | 7c842a1 | 2012-04-20 18:33:52 +0800 | [diff] [blame] | 299 | return data; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 300 | } |
| 301 | |
Axel Lin | ca61a7b | 2012-03-12 15:59:54 +0800 | [diff] [blame] | 302 | static int tps6507x_pmic_set_voltage_sel(struct regulator_dev *dev, |
| 303 | unsigned selector) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 304 | { |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 305 | struct tps6507x_pmic *tps = rdev_get_drvdata(dev); |
Axel Lin | ca61a7b | 2012-03-12 15:59:54 +0800 | [diff] [blame] | 306 | int data, rid = rdev_get_id(dev); |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 307 | u8 reg, mask; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 308 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 309 | switch (rid) { |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 310 | case TPS6507X_DCDC_1: |
| 311 | reg = TPS6507X_REG_DEFDCDC1; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 312 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 313 | break; |
| 314 | case TPS6507X_DCDC_2: |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 315 | if (tps->info[rid]->defdcdc_default) |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 316 | reg = TPS6507X_REG_DEFDCDC2_HIGH; |
| 317 | else |
| 318 | reg = TPS6507X_REG_DEFDCDC2_LOW; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 319 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 320 | break; |
| 321 | case TPS6507X_DCDC_3: |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 322 | if (tps->info[rid]->defdcdc_default) |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 323 | reg = TPS6507X_REG_DEFDCDC3_HIGH; |
| 324 | else |
| 325 | reg = TPS6507X_REG_DEFDCDC3_LOW; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 326 | mask = TPS6507X_DEFDCDCX_DCDC_MASK; |
| 327 | break; |
| 328 | case TPS6507X_LDO_1: |
| 329 | reg = TPS6507X_REG_LDO_CTRL1; |
| 330 | mask = TPS6507X_REG_LDO_CTRL1_LDO1_MASK; |
| 331 | break; |
| 332 | case TPS6507X_LDO_2: |
| 333 | reg = TPS6507X_REG_DEFLDO2; |
| 334 | mask = TPS6507X_REG_DEFLDO2_LDO2_MASK; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 335 | break; |
| 336 | default: |
| 337 | return -EINVAL; |
| 338 | } |
| 339 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 340 | data = tps6507x_pmic_reg_read(tps, reg); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 341 | if (data < 0) |
| 342 | return data; |
| 343 | |
| 344 | data &= ~mask; |
Axel Lin | ca61a7b | 2012-03-12 15:59:54 +0800 | [diff] [blame] | 345 | data |= selector; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 346 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 347 | return tps6507x_pmic_reg_write(tps, reg, data); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 348 | } |
| 349 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 350 | static struct regulator_ops tps6507x_pmic_ops = { |
| 351 | .is_enabled = tps6507x_pmic_is_enabled, |
| 352 | .enable = tps6507x_pmic_enable, |
| 353 | .disable = tps6507x_pmic_disable, |
Axel Lin | 7c842a1 | 2012-04-20 18:33:52 +0800 | [diff] [blame] | 354 | .get_voltage_sel = tps6507x_pmic_get_voltage_sel, |
Axel Lin | ca61a7b | 2012-03-12 15:59:54 +0800 | [diff] [blame] | 355 | .set_voltage_sel = tps6507x_pmic_set_voltage_sel, |
Axel Lin | 055917a | 2012-05-20 10:39:12 +0800 | [diff] [blame] | 356 | .list_voltage = regulator_list_voltage_table, |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 357 | }; |
| 358 | |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 359 | static __devinit int tps6507x_pmic_probe(struct platform_device *pdev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 360 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 361 | struct tps6507x_dev *tps6507x_dev = dev_get_drvdata(pdev->dev.parent); |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 362 | struct tps_info *info = &tps6507x_pmic_regs[0]; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 363 | struct regulator_config config = { }; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 364 | struct regulator_init_data *init_data; |
| 365 | struct regulator_dev *rdev; |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 366 | struct tps6507x_pmic *tps; |
Todd Fischer | 0bc20bb | 2010-04-05 20:23:57 -0600 | [diff] [blame] | 367 | struct tps6507x_board *tps_board; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 368 | int i; |
Dmitry Torokhov | 56c2349 | 2010-02-23 23:38:12 -0800 | [diff] [blame] | 369 | int error; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 370 | |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 371 | /** |
Todd Fischer | 0bc20bb | 2010-04-05 20:23:57 -0600 | [diff] [blame] | 372 | * tps_board points to pmic related constants |
| 373 | * coming from the board-evm file. |
| 374 | */ |
| 375 | |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 376 | tps_board = dev_get_platdata(tps6507x_dev->dev); |
Todd Fischer | 0bc20bb | 2010-04-05 20:23:57 -0600 | [diff] [blame] | 377 | if (!tps_board) |
| 378 | return -EINVAL; |
| 379 | |
| 380 | /** |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 381 | * init_data points to array of regulator_init structures |
| 382 | * coming from the board-evm file. |
| 383 | */ |
Todd Fischer | 0bc20bb | 2010-04-05 20:23:57 -0600 | [diff] [blame] | 384 | init_data = tps_board->tps6507x_pmic_init_data; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 385 | if (!init_data) |
Todd Fischer | 0bc20bb | 2010-04-05 20:23:57 -0600 | [diff] [blame] | 386 | return -EINVAL; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 387 | |
Axel Lin | 9eb0c42 | 2012-04-11 14:40:18 +0800 | [diff] [blame] | 388 | tps = devm_kzalloc(&pdev->dev, sizeof(*tps), GFP_KERNEL); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 389 | if (!tps) |
| 390 | return -ENOMEM; |
| 391 | |
| 392 | mutex_init(&tps->io_lock); |
| 393 | |
| 394 | /* common for all regulators */ |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 395 | tps->mfd = tps6507x_dev; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 396 | |
| 397 | for (i = 0; i < TPS6507X_NUM_REGULATOR; i++, info++, init_data++) { |
| 398 | /* Register the regulators */ |
| 399 | tps->info[i] = info; |
Anuj Aggarwal | 7d14831 | 2010-07-12 17:54:06 +0530 | [diff] [blame] | 400 | if (init_data->driver_data) { |
| 401 | struct tps6507x_reg_platform_data *data = |
| 402 | init_data->driver_data; |
| 403 | tps->info[i]->defdcdc_default = data->defdcdc_default; |
| 404 | } |
| 405 | |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 406 | tps->desc[i].name = info->name; |
Axel Lin | 77fa44d | 2011-05-12 13:47:50 +0800 | [diff] [blame] | 407 | tps->desc[i].id = i; |
Axel Lin | 0fcdb10 | 2011-08-02 15:34:12 +0800 | [diff] [blame] | 408 | tps->desc[i].n_voltages = info->table_len; |
Axel Lin | 055917a | 2012-05-20 10:39:12 +0800 | [diff] [blame] | 409 | tps->desc[i].volt_table = info->table; |
Axel Lin | f2933d3 | 2012-03-12 15:57:50 +0800 | [diff] [blame] | 410 | tps->desc[i].ops = &tps6507x_pmic_ops; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 411 | tps->desc[i].type = REGULATOR_VOLTAGE; |
| 412 | tps->desc[i].owner = THIS_MODULE; |
| 413 | |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 414 | config.dev = tps6507x_dev->dev; |
| 415 | config.init_data = init_data; |
| 416 | config.driver_data = tps; |
| 417 | |
| 418 | rdev = regulator_register(&tps->desc[i], &config); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 419 | if (IS_ERR(rdev)) { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 420 | dev_err(tps6507x_dev->dev, |
| 421 | "failed to register %s regulator\n", |
| 422 | pdev->name); |
Dmitry Torokhov | 56c2349 | 2010-02-23 23:38:12 -0800 | [diff] [blame] | 423 | error = PTR_ERR(rdev); |
| 424 | goto fail; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* Save regulator for cleanup */ |
| 428 | tps->rdev[i] = rdev; |
| 429 | } |
| 430 | |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 431 | tps6507x_dev->pmic = tps; |
Axel Lin | d7399fa | 2010-08-09 15:51:23 +0800 | [diff] [blame] | 432 | platform_set_drvdata(pdev, tps6507x_dev); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 433 | |
| 434 | return 0; |
Dmitry Torokhov | 56c2349 | 2010-02-23 23:38:12 -0800 | [diff] [blame] | 435 | |
| 436 | fail: |
| 437 | while (--i >= 0) |
| 438 | regulator_unregister(tps->rdev[i]); |
Dmitry Torokhov | 56c2349 | 2010-02-23 23:38:12 -0800 | [diff] [blame] | 439 | return error; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 440 | } |
| 441 | |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 442 | static int __devexit tps6507x_pmic_remove(struct platform_device *pdev) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 443 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 444 | struct tps6507x_dev *tps6507x_dev = platform_get_drvdata(pdev); |
| 445 | struct tps6507x_pmic *tps = tps6507x_dev->pmic; |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 446 | int i; |
| 447 | |
| 448 | for (i = 0; i < TPS6507X_NUM_REGULATOR; i++) |
| 449 | regulator_unregister(tps->rdev[i]); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 450 | return 0; |
| 451 | } |
| 452 | |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 453 | static struct platform_driver tps6507x_pmic_driver = { |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 454 | .driver = { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 455 | .name = "tps6507x-pmic", |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 456 | .owner = THIS_MODULE, |
| 457 | }, |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 458 | .probe = tps6507x_pmic_probe, |
| 459 | .remove = __devexit_p(tps6507x_pmic_remove), |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 460 | }; |
| 461 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 462 | static int __init tps6507x_pmic_init(void) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 463 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 464 | return platform_driver_register(&tps6507x_pmic_driver); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 465 | } |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 466 | subsys_initcall(tps6507x_pmic_init); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 467 | |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 468 | static void __exit tps6507x_pmic_cleanup(void) |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 469 | { |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 470 | platform_driver_unregister(&tps6507x_pmic_driver); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 471 | } |
Todd Fischer | 4ce5ba5 | 2010-04-05 20:23:58 -0600 | [diff] [blame] | 472 | module_exit(tps6507x_pmic_cleanup); |
Anuj Aggarwal | 3fa5b8e | 2009-08-21 00:39:39 +0530 | [diff] [blame] | 473 | |
| 474 | MODULE_AUTHOR("Texas Instruments"); |
| 475 | MODULE_DESCRIPTION("TPS6507x voltage regulator driver"); |
Liam Girdwood | 9e108d3 | 2009-08-24 10:31:34 +0100 | [diff] [blame] | 476 | MODULE_LICENSE("GPL v2"); |
Todd Fischer | 31dd6a2 | 2010-04-08 09:04:55 +0200 | [diff] [blame] | 477 | MODULE_ALIAS("platform:tps6507x-pmic"); |