tps62360_regulator.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
  4. * Tero Kristo <t-kristo@ti.com>
  5. */
  6. #include <common.h>
  7. #include <dm.h>
  8. #include <i2c.h>
  9. #include <dm/device_compat.h>
  10. #include <power/regulator.h>
  11. #define TPS62360_REG_SET0 0
  12. #define TPS62360_I2C_CHIP 0x60
  13. #define TPS62360_VSEL_STEPSIZE 10000 /* In uV */
  14. struct tps62360_regulator_config {
  15. u32 vmin;
  16. u32 vmax;
  17. };
  18. struct tps62360_regulator_pdata {
  19. u8 vsel_offset;
  20. struct udevice *i2c;
  21. struct tps62360_regulator_config *config;
  22. };
  23. /*
  24. * TPS62362/TPS62363 are just re-using these values for now, their preset
  25. * voltage values are just different compared to TPS62360/TPS62361.
  26. */
  27. static struct tps62360_regulator_config tps62360_data = {
  28. .vmin = 770000,
  29. .vmax = 1400000,
  30. };
  31. static struct tps62360_regulator_config tps62361_data = {
  32. .vmin = 500000,
  33. .vmax = 1770000,
  34. };
  35. static int tps62360_regulator_set_value(struct udevice *dev, int uV)
  36. {
  37. struct tps62360_regulator_pdata *pdata = dev_get_plat(dev);
  38. u8 regval;
  39. if (uV < pdata->config->vmin || uV > pdata->config->vmax)
  40. return -EINVAL;
  41. uV -= pdata->config->vmin;
  42. uV = DIV_ROUND_UP(uV, TPS62360_VSEL_STEPSIZE);
  43. if (uV > U8_MAX)
  44. return -EINVAL;
  45. regval = (u8)uV;
  46. return dm_i2c_write(pdata->i2c, TPS62360_REG_SET0 + pdata->vsel_offset,
  47. &regval, 1);
  48. }
  49. static int tps62360_regulator_get_value(struct udevice *dev)
  50. {
  51. u8 regval;
  52. int ret;
  53. struct tps62360_regulator_pdata *pdata = dev_get_plat(dev);
  54. ret = dm_i2c_read(pdata->i2c, TPS62360_REG_SET0 + pdata->vsel_offset,
  55. &regval, 1);
  56. if (ret) {
  57. dev_err(dev, "i2c read failed: %d\n", ret);
  58. return ret;
  59. }
  60. return (u32)regval * TPS62360_VSEL_STEPSIZE + pdata->config->vmin;
  61. }
  62. static int tps62360_regulator_probe(struct udevice *dev)
  63. {
  64. struct tps62360_regulator_pdata *pdata = dev_get_plat(dev);
  65. u8 vsel0;
  66. u8 vsel1;
  67. int ret;
  68. pdata->config = (void *)dev_get_driver_data(dev);
  69. vsel0 = dev_read_bool(dev, "ti,vsel0-state-high");
  70. vsel1 = dev_read_bool(dev, "ti,vsel1-state-high");
  71. pdata->vsel_offset = vsel0 + vsel1 * 2;
  72. ret = i2c_get_chip(dev->parent, TPS62360_I2C_CHIP, 1, &pdata->i2c);
  73. if (ret) {
  74. dev_err(dev, "i2c dev get failed.\n");
  75. return ret;
  76. }
  77. return 0;
  78. }
  79. static const struct dm_regulator_ops tps62360_regulator_ops = {
  80. .get_value = tps62360_regulator_get_value,
  81. .set_value = tps62360_regulator_set_value,
  82. };
  83. static const struct udevice_id tps62360_regulator_ids[] = {
  84. { .compatible = "ti,tps62360", .data = (ulong)&tps62360_data },
  85. { .compatible = "ti,tps62361", .data = (ulong)&tps62361_data },
  86. { .compatible = "ti,tps62362", .data = (ulong)&tps62360_data },
  87. { .compatible = "ti,tps62363", .data = (ulong)&tps62361_data },
  88. { },
  89. };
  90. U_BOOT_DRIVER(tps62360_regulator) = {
  91. .name = "tps62360_regulator",
  92. .id = UCLASS_REGULATOR,
  93. .ops = &tps62360_regulator_ops,
  94. .of_match = tps62360_regulator_ids,
  95. .plat_auto = sizeof(struct tps62360_regulator_pdata),
  96. .probe = tps62360_regulator_probe,
  97. };