0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/kernel.h>
0017 #include <linux/module.h>
0018 #include <linux/err.h>
0019 #include <linux/errno.h>
0020 #include <linux/slab.h>
0021 #include <linux/spi/spi.h>
0022 #include <linux/regulator/driver.h>
0023 #include <linux/regulator/machine.h>
0024
0025 #define REG_LDO_SET 0x0
0026 #define LDO_ILIM_MASK 1
0027 #define LDO_VSEL_MASK 0x0f
0028 #define LDO2_ILIM_SHIFT 12
0029 #define LDO2_VSEL_SHIFT 4
0030 #define LDO1_ILIM_SHIFT 8
0031 #define LDO1_VSEL_SHIFT 0
0032
0033 #define REG_BLOCK_EN 0x1
0034 #define BLOCK_MASK 1
0035 #define BLOCK_LDO1_SHIFT 0
0036 #define BLOCK_LDO2_SHIFT 1
0037 #define BLOCK_LCD_SHIFT 2
0038 #define BLOCK_USB_SHIFT 3
0039
0040 #define REG_DCDC_SET 0x2
0041 #define DCDC_VDCDC_MASK 0x1f
0042 #define DCDC_VDCDC1_SHIFT 0
0043 #define DCDC_VDCDC2_SHIFT 5
0044 #define DCDC_VDCDC3_SHIFT 10
0045
0046 #define REG_DCDC_EN 0x3
0047 #define DCDCDCDC_EN_MASK 0x1
0048 #define DCDCDCDC1_EN_SHIFT 0
0049 #define DCDCDCDC1_PG_MSK BIT(1)
0050 #define DCDCDCDC2_EN_SHIFT 2
0051 #define DCDCDCDC2_PG_MSK BIT(3)
0052 #define DCDCDCDC3_EN_SHIFT 4
0053 #define DCDCDCDC3_PG_MSK BIT(5)
0054
0055 #define REG_USB 0x4
0056 #define USB_ILIM_SHIFT 0
0057 #define USB_ILIM_MASK 0x3
0058 #define USB_TSD_SHIFT 2
0059 #define USB_TSD_MASK 0x3
0060 #define USB_TWARN_SHIFT 4
0061 #define USB_TWARN_MASK 0x3
0062 #define USB_IWARN_SD BIT(6)
0063 #define USB_FAST_LOOP BIT(7)
0064
0065 #define REG_ALARM 0x5
0066 #define ALARM_LDO1 BIT(0)
0067 #define ALARM_DCDC1 BIT(1)
0068 #define ALARM_DCDC2 BIT(2)
0069 #define ALARM_DCDC3 BIT(3)
0070 #define ALARM_LDO2 BIT(4)
0071 #define ALARM_USB_WARN BIT(5)
0072 #define ALARM_USB_ALARM BIT(6)
0073 #define ALARM_LCD BIT(9)
0074 #define ALARM_TEMP_WARM BIT(10)
0075 #define ALARM_TEMP_HOT BIT(11)
0076 #define ALARM_NRST BIT(14)
0077 #define ALARM_POWERUP BIT(15)
0078
0079 #define REG_INT_ENABLE 0x6
0080 #define INT_LDO1 BIT(0)
0081 #define INT_DCDC1 BIT(1)
0082 #define INT_DCDC2 BIT(2)
0083 #define INT_DCDC3 BIT(3)
0084 #define INT_LDO2 BIT(4)
0085 #define INT_USB_WARN BIT(5)
0086 #define INT_USB_ALARM BIT(6)
0087 #define INT_LCD BIT(9)
0088 #define INT_TEMP_WARM BIT(10)
0089 #define INT_TEMP_HOT BIT(11)
0090 #define INT_GLOBAL_EN BIT(15)
0091
0092 #define REG_INT_STATUS 0x7
0093 #define STATUS_LDO1 BIT(0)
0094 #define STATUS_DCDC1 BIT(1)
0095 #define STATUS_DCDC2 BIT(2)
0096 #define STATUS_DCDC3 BIT(3)
0097 #define STATUS_LDO2 BIT(4)
0098 #define STATUS_USB_WARN BIT(5)
0099 #define STATUS_USB_ALARM BIT(6)
0100 #define STATUS_LCD BIT(9)
0101 #define STATUS_TEMP_WARM BIT(10)
0102 #define STATUS_TEMP_HOT BIT(11)
0103
0104 #define REG_SOFTWARE_RESET 0xb
0105 #define REG_WRITE_ENABLE 0xd
0106 #define REG_REV_ID 0xf
0107
0108 #define N_DCDC 3
0109 #define N_LDO 2
0110 #define N_SWITCH 2
0111 #define N_REGULATORS (N_DCDC + N_LDO + N_SWITCH)
0112
0113 #define CMD_READ(reg) ((reg) << 6)
0114 #define CMD_WRITE(reg) (BIT(5) | (reg) << 6)
0115 #define STAT_CLK BIT(3)
0116 #define STAT_WRITE BIT(2)
0117 #define STAT_INVALID BIT(1)
0118 #define STAT_WP BIT(0)
0119
0120 struct field {
0121 int reg;
0122 int shift;
0123 int mask;
0124 };
0125
0126 struct supply_info {
0127 const char *name;
0128 int n_voltages;
0129 const unsigned int *voltages;
0130 int n_ilimsels;
0131 const unsigned int *ilimsels;
0132 struct field enable, voltage, ilimsel;
0133 };
0134
0135 struct tps6524x {
0136 struct device *dev;
0137 struct spi_device *spi;
0138 struct mutex lock;
0139 struct regulator_desc desc[N_REGULATORS];
0140 };
0141
0142 static int __read_reg(struct tps6524x *hw, int reg)
0143 {
0144 int error = 0;
0145 u16 cmd = CMD_READ(reg), in;
0146 u8 status;
0147 struct spi_message m;
0148 struct spi_transfer t[3];
0149
0150 spi_message_init(&m);
0151 memset(t, 0, sizeof(t));
0152
0153 t[0].tx_buf = &cmd;
0154 t[0].len = 2;
0155 t[0].bits_per_word = 12;
0156 spi_message_add_tail(&t[0], &m);
0157
0158 t[1].rx_buf = ∈
0159 t[1].len = 2;
0160 t[1].bits_per_word = 16;
0161 spi_message_add_tail(&t[1], &m);
0162
0163 t[2].rx_buf = &status;
0164 t[2].len = 1;
0165 t[2].bits_per_word = 4;
0166 spi_message_add_tail(&t[2], &m);
0167
0168 error = spi_sync(hw->spi, &m);
0169 if (error < 0)
0170 return error;
0171
0172 dev_dbg(hw->dev, "read reg %d, data %x, status %x\n",
0173 reg, in, status);
0174
0175 if (!(status & STAT_CLK) || (status & STAT_WRITE))
0176 return -EIO;
0177
0178 if (status & STAT_INVALID)
0179 return -EINVAL;
0180
0181 return in;
0182 }
0183
0184 static int read_reg(struct tps6524x *hw, int reg)
0185 {
0186 int ret;
0187
0188 mutex_lock(&hw->lock);
0189 ret = __read_reg(hw, reg);
0190 mutex_unlock(&hw->lock);
0191
0192 return ret;
0193 }
0194
0195 static int __write_reg(struct tps6524x *hw, int reg, int val)
0196 {
0197 int error = 0;
0198 u16 cmd = CMD_WRITE(reg), out = val;
0199 u8 status;
0200 struct spi_message m;
0201 struct spi_transfer t[3];
0202
0203 spi_message_init(&m);
0204 memset(t, 0, sizeof(t));
0205
0206 t[0].tx_buf = &cmd;
0207 t[0].len = 2;
0208 t[0].bits_per_word = 12;
0209 spi_message_add_tail(&t[0], &m);
0210
0211 t[1].tx_buf = &out;
0212 t[1].len = 2;
0213 t[1].bits_per_word = 16;
0214 spi_message_add_tail(&t[1], &m);
0215
0216 t[2].rx_buf = &status;
0217 t[2].len = 1;
0218 t[2].bits_per_word = 4;
0219 spi_message_add_tail(&t[2], &m);
0220
0221 error = spi_sync(hw->spi, &m);
0222 if (error < 0)
0223 return error;
0224
0225 dev_dbg(hw->dev, "wrote reg %d, data %x, status %x\n",
0226 reg, out, status);
0227
0228 if (!(status & STAT_CLK) || !(status & STAT_WRITE))
0229 return -EIO;
0230
0231 if (status & (STAT_INVALID | STAT_WP))
0232 return -EINVAL;
0233
0234 return error;
0235 }
0236
0237 static int __rmw_reg(struct tps6524x *hw, int reg, int mask, int val)
0238 {
0239 int ret;
0240
0241 ret = __read_reg(hw, reg);
0242 if (ret < 0)
0243 return ret;
0244
0245 ret &= ~mask;
0246 ret |= val;
0247
0248 ret = __write_reg(hw, reg, ret);
0249
0250 return (ret < 0) ? ret : 0;
0251 }
0252
0253 static int rmw_protect(struct tps6524x *hw, int reg, int mask, int val)
0254 {
0255 int ret;
0256
0257 mutex_lock(&hw->lock);
0258
0259 ret = __write_reg(hw, REG_WRITE_ENABLE, 1);
0260 if (ret) {
0261 dev_err(hw->dev, "failed to set write enable\n");
0262 goto error;
0263 }
0264
0265 ret = __rmw_reg(hw, reg, mask, val);
0266 if (ret)
0267 dev_err(hw->dev, "failed to rmw register %d\n", reg);
0268
0269 ret = __write_reg(hw, REG_WRITE_ENABLE, 0);
0270 if (ret) {
0271 dev_err(hw->dev, "failed to clear write enable\n");
0272 goto error;
0273 }
0274
0275 error:
0276 mutex_unlock(&hw->lock);
0277
0278 return ret;
0279 }
0280
0281 static int read_field(struct tps6524x *hw, const struct field *field)
0282 {
0283 int tmp;
0284
0285 tmp = read_reg(hw, field->reg);
0286 if (tmp < 0)
0287 return tmp;
0288
0289 return (tmp >> field->shift) & field->mask;
0290 }
0291
0292 static int write_field(struct tps6524x *hw, const struct field *field,
0293 int val)
0294 {
0295 if (val & ~field->mask)
0296 return -EOVERFLOW;
0297
0298 return rmw_protect(hw, field->reg,
0299 field->mask << field->shift,
0300 val << field->shift);
0301 }
0302
0303 static const unsigned int dcdc1_voltages[] = {
0304 800000, 825000, 850000, 875000,
0305 900000, 925000, 950000, 975000,
0306 1000000, 1025000, 1050000, 1075000,
0307 1100000, 1125000, 1150000, 1175000,
0308 1200000, 1225000, 1250000, 1275000,
0309 1300000, 1325000, 1350000, 1375000,
0310 1400000, 1425000, 1450000, 1475000,
0311 1500000, 1525000, 1550000, 1575000,
0312 };
0313
0314 static const unsigned int dcdc2_voltages[] = {
0315 1400000, 1450000, 1500000, 1550000,
0316 1600000, 1650000, 1700000, 1750000,
0317 1800000, 1850000, 1900000, 1950000,
0318 2000000, 2050000, 2100000, 2150000,
0319 2200000, 2250000, 2300000, 2350000,
0320 2400000, 2450000, 2500000, 2550000,
0321 2600000, 2650000, 2700000, 2750000,
0322 2800000, 2850000, 2900000, 2950000,
0323 };
0324
0325 static const unsigned int dcdc3_voltages[] = {
0326 2400000, 2450000, 2500000, 2550000, 2600000,
0327 2650000, 2700000, 2750000, 2800000, 2850000,
0328 2900000, 2950000, 3000000, 3050000, 3100000,
0329 3150000, 3200000, 3250000, 3300000, 3350000,
0330 3400000, 3450000, 3500000, 3550000, 3600000,
0331 };
0332
0333 static const unsigned int ldo1_voltages[] = {
0334 4300000, 4350000, 4400000, 4450000,
0335 4500000, 4550000, 4600000, 4650000,
0336 4700000, 4750000, 4800000, 4850000,
0337 4900000, 4950000, 5000000, 5050000,
0338 };
0339
0340 static const unsigned int ldo2_voltages[] = {
0341 1100000, 1150000, 1200000, 1250000,
0342 1300000, 1700000, 1750000, 1800000,
0343 1850000, 1900000, 3150000, 3200000,
0344 3250000, 3300000, 3350000, 3400000,
0345 };
0346
0347 static const unsigned int fixed_5000000_voltage[] = {
0348 5000000
0349 };
0350
0351 static const unsigned int ldo_ilimsel[] = {
0352 400000, 1500000
0353 };
0354
0355 static const unsigned int usb_ilimsel[] = {
0356 200000, 400000, 800000, 1000000
0357 };
0358
0359 static const unsigned int fixed_2400000_ilimsel[] = {
0360 2400000
0361 };
0362
0363 static const unsigned int fixed_1200000_ilimsel[] = {
0364 1200000
0365 };
0366
0367 static const unsigned int fixed_400000_ilimsel[] = {
0368 400000
0369 };
0370
0371 #define __MK_FIELD(_reg, _mask, _shift) \
0372 { .reg = (_reg), .mask = (_mask), .shift = (_shift), }
0373
0374 static const struct supply_info supply_info[N_REGULATORS] = {
0375 {
0376 .name = "DCDC1",
0377 .n_voltages = ARRAY_SIZE(dcdc1_voltages),
0378 .voltages = dcdc1_voltages,
0379 .n_ilimsels = ARRAY_SIZE(fixed_2400000_ilimsel),
0380 .ilimsels = fixed_2400000_ilimsel,
0381 .enable = __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
0382 DCDCDCDC1_EN_SHIFT),
0383 .voltage = __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
0384 DCDC_VDCDC1_SHIFT),
0385 },
0386 {
0387 .name = "DCDC2",
0388 .n_voltages = ARRAY_SIZE(dcdc2_voltages),
0389 .voltages = dcdc2_voltages,
0390 .n_ilimsels = ARRAY_SIZE(fixed_1200000_ilimsel),
0391 .ilimsels = fixed_1200000_ilimsel,
0392 .enable = __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
0393 DCDCDCDC2_EN_SHIFT),
0394 .voltage = __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
0395 DCDC_VDCDC2_SHIFT),
0396 },
0397 {
0398 .name = "DCDC3",
0399 .n_voltages = ARRAY_SIZE(dcdc3_voltages),
0400 .voltages = dcdc3_voltages,
0401 .n_ilimsels = ARRAY_SIZE(fixed_1200000_ilimsel),
0402 .ilimsels = fixed_1200000_ilimsel,
0403 .enable = __MK_FIELD(REG_DCDC_EN, DCDCDCDC_EN_MASK,
0404 DCDCDCDC3_EN_SHIFT),
0405 .voltage = __MK_FIELD(REG_DCDC_SET, DCDC_VDCDC_MASK,
0406 DCDC_VDCDC3_SHIFT),
0407 },
0408 {
0409 .name = "LDO1",
0410 .n_voltages = ARRAY_SIZE(ldo1_voltages),
0411 .voltages = ldo1_voltages,
0412 .n_ilimsels = ARRAY_SIZE(ldo_ilimsel),
0413 .ilimsels = ldo_ilimsel,
0414 .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
0415 BLOCK_LDO1_SHIFT),
0416 .voltage = __MK_FIELD(REG_LDO_SET, LDO_VSEL_MASK,
0417 LDO1_VSEL_SHIFT),
0418 .ilimsel = __MK_FIELD(REG_LDO_SET, LDO_ILIM_MASK,
0419 LDO1_ILIM_SHIFT),
0420 },
0421 {
0422 .name = "LDO2",
0423 .n_voltages = ARRAY_SIZE(ldo2_voltages),
0424 .voltages = ldo2_voltages,
0425 .n_ilimsels = ARRAY_SIZE(ldo_ilimsel),
0426 .ilimsels = ldo_ilimsel,
0427 .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
0428 BLOCK_LDO2_SHIFT),
0429 .voltage = __MK_FIELD(REG_LDO_SET, LDO_VSEL_MASK,
0430 LDO2_VSEL_SHIFT),
0431 .ilimsel = __MK_FIELD(REG_LDO_SET, LDO_ILIM_MASK,
0432 LDO2_ILIM_SHIFT),
0433 },
0434 {
0435 .name = "USB",
0436 .n_voltages = ARRAY_SIZE(fixed_5000000_voltage),
0437 .voltages = fixed_5000000_voltage,
0438 .n_ilimsels = ARRAY_SIZE(usb_ilimsel),
0439 .ilimsels = usb_ilimsel,
0440 .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
0441 BLOCK_USB_SHIFT),
0442 .ilimsel = __MK_FIELD(REG_USB, USB_ILIM_MASK,
0443 USB_ILIM_SHIFT),
0444 },
0445 {
0446 .name = "LCD",
0447 .n_voltages = ARRAY_SIZE(fixed_5000000_voltage),
0448 .voltages = fixed_5000000_voltage,
0449 .n_ilimsels = ARRAY_SIZE(fixed_400000_ilimsel),
0450 .ilimsels = fixed_400000_ilimsel,
0451 .enable = __MK_FIELD(REG_BLOCK_EN, BLOCK_MASK,
0452 BLOCK_LCD_SHIFT),
0453 },
0454 };
0455
0456 static int set_voltage_sel(struct regulator_dev *rdev, unsigned selector)
0457 {
0458 const struct supply_info *info;
0459 struct tps6524x *hw;
0460
0461 hw = rdev_get_drvdata(rdev);
0462 info = &supply_info[rdev_get_id(rdev)];
0463
0464 if (rdev->desc->n_voltages == 1)
0465 return -EINVAL;
0466
0467 return write_field(hw, &info->voltage, selector);
0468 }
0469
0470 static int get_voltage_sel(struct regulator_dev *rdev)
0471 {
0472 const struct supply_info *info;
0473 struct tps6524x *hw;
0474 int ret;
0475
0476 hw = rdev_get_drvdata(rdev);
0477 info = &supply_info[rdev_get_id(rdev)];
0478
0479 if (rdev->desc->n_voltages == 1)
0480 return 0;
0481
0482 ret = read_field(hw, &info->voltage);
0483 if (ret < 0)
0484 return ret;
0485 if (WARN_ON(ret >= info->n_voltages))
0486 return -EIO;
0487
0488 return ret;
0489 }
0490
0491 static int set_current_limit(struct regulator_dev *rdev, int min_uA,
0492 int max_uA)
0493 {
0494 const struct supply_info *info;
0495 struct tps6524x *hw;
0496 int i;
0497
0498 hw = rdev_get_drvdata(rdev);
0499 info = &supply_info[rdev_get_id(rdev)];
0500
0501 if (info->n_ilimsels == 1)
0502 return -EINVAL;
0503
0504 for (i = info->n_ilimsels - 1; i >= 0; i--) {
0505 if (min_uA <= info->ilimsels[i] &&
0506 max_uA >= info->ilimsels[i])
0507 return write_field(hw, &info->ilimsel, i);
0508 }
0509
0510 return -EINVAL;
0511 }
0512
0513 static int get_current_limit(struct regulator_dev *rdev)
0514 {
0515 const struct supply_info *info;
0516 struct tps6524x *hw;
0517 int ret;
0518
0519 hw = rdev_get_drvdata(rdev);
0520 info = &supply_info[rdev_get_id(rdev)];
0521
0522 if (info->n_ilimsels == 1)
0523 return info->ilimsels[0];
0524
0525 ret = read_field(hw, &info->ilimsel);
0526 if (ret < 0)
0527 return ret;
0528 if (WARN_ON(ret >= info->n_ilimsels))
0529 return -EIO;
0530
0531 return info->ilimsels[ret];
0532 }
0533
0534 static int enable_supply(struct regulator_dev *rdev)
0535 {
0536 const struct supply_info *info;
0537 struct tps6524x *hw;
0538
0539 hw = rdev_get_drvdata(rdev);
0540 info = &supply_info[rdev_get_id(rdev)];
0541
0542 return write_field(hw, &info->enable, 1);
0543 }
0544
0545 static int disable_supply(struct regulator_dev *rdev)
0546 {
0547 const struct supply_info *info;
0548 struct tps6524x *hw;
0549
0550 hw = rdev_get_drvdata(rdev);
0551 info = &supply_info[rdev_get_id(rdev)];
0552
0553 return write_field(hw, &info->enable, 0);
0554 }
0555
0556 static int is_supply_enabled(struct regulator_dev *rdev)
0557 {
0558 const struct supply_info *info;
0559 struct tps6524x *hw;
0560
0561 hw = rdev_get_drvdata(rdev);
0562 info = &supply_info[rdev_get_id(rdev)];
0563
0564 return read_field(hw, &info->enable);
0565 }
0566
0567 static const struct regulator_ops regulator_ops = {
0568 .is_enabled = is_supply_enabled,
0569 .enable = enable_supply,
0570 .disable = disable_supply,
0571 .get_voltage_sel = get_voltage_sel,
0572 .set_voltage_sel = set_voltage_sel,
0573 .list_voltage = regulator_list_voltage_table,
0574 .map_voltage = regulator_map_voltage_ascend,
0575 .set_current_limit = set_current_limit,
0576 .get_current_limit = get_current_limit,
0577 };
0578
0579 static int pmic_probe(struct spi_device *spi)
0580 {
0581 struct tps6524x *hw;
0582 struct device *dev = &spi->dev;
0583 const struct supply_info *info = supply_info;
0584 struct regulator_init_data *init_data;
0585 struct regulator_config config = { };
0586 struct regulator_dev *rdev;
0587 int i;
0588
0589 init_data = dev_get_platdata(dev);
0590 if (!init_data) {
0591 dev_err(dev, "could not find regulator platform data\n");
0592 return -EINVAL;
0593 }
0594
0595 hw = devm_kzalloc(&spi->dev, sizeof(struct tps6524x), GFP_KERNEL);
0596 if (!hw)
0597 return -ENOMEM;
0598
0599 spi_set_drvdata(spi, hw);
0600
0601 memset(hw, 0, sizeof(struct tps6524x));
0602 hw->dev = dev;
0603 hw->spi = spi;
0604 mutex_init(&hw->lock);
0605
0606 for (i = 0; i < N_REGULATORS; i++, info++, init_data++) {
0607 hw->desc[i].name = info->name;
0608 hw->desc[i].id = i;
0609 hw->desc[i].n_voltages = info->n_voltages;
0610 hw->desc[i].volt_table = info->voltages;
0611 hw->desc[i].ops = ®ulator_ops;
0612 hw->desc[i].type = REGULATOR_VOLTAGE;
0613 hw->desc[i].owner = THIS_MODULE;
0614
0615 config.dev = dev;
0616 config.init_data = init_data;
0617 config.driver_data = hw;
0618
0619 rdev = devm_regulator_register(dev, &hw->desc[i], &config);
0620 if (IS_ERR(rdev))
0621 return PTR_ERR(rdev);
0622 }
0623
0624 return 0;
0625 }
0626
0627 static struct spi_driver pmic_driver = {
0628 .probe = pmic_probe,
0629 .driver = {
0630 .name = "tps6524x",
0631 },
0632 };
0633
0634 module_spi_driver(pmic_driver);
0635
0636 MODULE_DESCRIPTION("TPS6524X PMIC Driver");
0637 MODULE_AUTHOR("Cyril Chemparathy");
0638 MODULE_LICENSE("GPL");
0639 MODULE_ALIAS("spi:tps6524x");