Re: [U-Boot] [PATCH v2 3/5] power: regulator: palmas: Add regulator support

2016-09-18 Thread Keerthy



On Monday 19 September 2016 09:21 AM, Keerthy wrote:

The driver provides regulator set/get voltage
enable/disable functions for palmas family of PMICs.

Signed-off-by: Keerthy 
Reviewed-by: Simon Glass 
---

Changes in v2:

  * Converted all dm_i2c_reg to dm_i2c_read_reg
  * Removed an instance of double blank lines.
  * Added Simon's Reviewed-by.

 drivers/power/regulator/Kconfig|   8 +
 drivers/power/regulator/Makefile   |   1 +
 drivers/power/regulator/palmas_regulator.c | 459 +
 3 files changed, 468 insertions(+)
 create mode 100644 drivers/power/regulator/palmas_regulator.c

diff --git a/drivers/power/regulator/Kconfig b/drivers/power/regulator/Kconfig
index 17f22dd..adb710a 100644
--- a/drivers/power/regulator/Kconfig
+++ b/drivers/power/regulator/Kconfig
@@ -115,3 +115,11 @@ config REGULATOR_TPS65090
regulators, one for each FET. The standard regulator interface is
supported, but it is only possible to turn the regulators on or off.
There is no voltage/current control.
+
+config DM_REGULATOR_PALMAS
+   bool "Enable driver for PALMAS PMIC regulators"
+   depends on PMIC_PALMAS
+   ---help---
+   This enables implementation of driver-model regulator uclass
+   features for REGULATOR PALMAS and the family of PALMAS PMICs.
+   The driver implements get/set api for: value and enable.
diff --git a/drivers/power/regulator/Makefile b/drivers/power/regulator/Makefile
index 1590d85..75080d4 100644
--- a/drivers/power/regulator/Makefile
+++ b/drivers/power/regulator/Makefile
@@ -14,3 +14,4 @@ obj-$(CONFIG_REGULATOR_RK808) += rk808.o
 obj-$(CONFIG_REGULATOR_S5M8767) += s5m8767.o
 obj-$(CONFIG_DM_REGULATOR_SANDBOX) += sandbox.o
 obj-$(CONFIG_REGULATOR_TPS65090) += tps65090_regulator.o
+obj-$(CONFIG_$(SPL_)DM_REGULATOR_PALMAS) += palmas_regulator.o
diff --git a/drivers/power/regulator/palmas_regulator.c 
b/drivers/power/regulator/palmas_regulator.c
new file mode 100644
index 000..8212bf2
--- /dev/null
+++ b/drivers/power/regulator/palmas_regulator.c
@@ -0,0 +1,459 @@
+/*
+ * (C) Copyright 2016
+ * Texas Instruments Incorporated, 
+ *
+ * Keerthy 
+ *
+ * SPDX-License-Identifier:GPL-2.0+
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#defineREGULATOR_ON0x1
+#defineREGULATOR_OFF   0x0
+
+#defineSMPS_MODE_MASK  0x3
+#defineSMPS_MODE_SHIFT 0x0
+#defineLDO_MODE_MASK   0x1
+#defineLDO_MODE_SHIFT  0x0
+
+static const char palmas_smps_ctrl[][PALMAS_SMPS_NUM] = {
+   {0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38, 0x3c},
+   {0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38},
+   {0x20, 0x24, 0x2c, 0x30, 0x38},
+};
+
+static const char palmas_smps_volt[][PALMAS_SMPS_NUM] = {
+   {0x23, 0x27, 0x2b, 0x2f, 0x33, 0x37, 0x3b, 0x3c},
+   {0x23, 0x27, 0x2b, 0x2f, 0x33, 0x37, 0x3b},
+   {0x23, 0x27, 0x2f, 0x33, 0x3B}
+};
+
+static const char palmas_ldo_ctrl[][PALMAS_LDO_NUM] = {
+   {0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x62, 0x64},
+   {0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x62, 0x64},
+   {0x50, 0x52, 0x54, 0x5e, 0x62}
+};
+
+static const char palmas_ldo_volt[][PALMAS_LDO_NUM] = {
+   {0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65},
+   {0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65},
+   {0x51, 0x53, 0x55, 0x5f, 0x63}
+};
+
+static int palmas_smps_enable(struct udevice *dev, int op, bool *enable)
+{
+   int ret;
+   uint8_t val;
+   unsigned int adr;
+   struct dm_regulator_uclass_platdata *uc_pdata;
+
+   uc_pdata = dev_get_uclass_platdata(dev);
+   adr = uc_pdata->ctrl_reg;
+
+   val = dm_i2c_reg_read(dev->parent, adr);
+   if (val < 0)
+   return val;
+
+   if (op == PMIC_OP_GET) {
+   val &= PALMAS_SMPS_STATUS_MASK;
+
+   if (val)
+   *enable = true;
+   else
+   *enable = false;
+
+   return 0;
+   } else if (op == PMIC_OP_SET) {
+   if (*enable)
+   val |= PALMAS_SMPS_MODE_MASK;
+   else
+   val &= ~(PALMAS_SMPS_MODE_MASK);
+
+   dm_i2c_write(dev->parent, adr, , 1);
+   if (ret)
+   return ret;
+   }
+
+   return 0;
+}
+
+static int palmas_smps_volt2hex(int uV)
+{
+   if (uV > PALMAS_LDO_VOLT_MAX)
+   return -EINVAL;
+
+   if (uV > 165)
+   return (uV - 100) / 2 + 0x6;
+
+   if (uV == 50)
+   return 0x6;
+   else
+   return 0x6 + ((uV - 50) / 1);
+}
+
+static int palmas_smps_hex2volt(int hex, bool range)
+{
+   unsigned 

[U-Boot] [PATCH v2 3/5] power: regulator: palmas: Add regulator support

2016-09-18 Thread Keerthy
The driver provides regulator set/get voltage
enable/disable functions for palmas family of PMICs.

Signed-off-by: Keerthy 
Reviewed-by: Simon Glass 
---

Changes in v2:

  * Converted all dm_i2c_reg to dm_i2c_read_reg
  * Removed an instance of double blank lines.
  * Added Simon's Reviewed-by.

 drivers/power/regulator/Kconfig|   8 +
 drivers/power/regulator/Makefile   |   1 +
 drivers/power/regulator/palmas_regulator.c | 459 +
 3 files changed, 468 insertions(+)
 create mode 100644 drivers/power/regulator/palmas_regulator.c

diff --git a/drivers/power/regulator/Kconfig b/drivers/power/regulator/Kconfig
index 17f22dd..adb710a 100644
--- a/drivers/power/regulator/Kconfig
+++ b/drivers/power/regulator/Kconfig
@@ -115,3 +115,11 @@ config REGULATOR_TPS65090
regulators, one for each FET. The standard regulator interface is
supported, but it is only possible to turn the regulators on or off.
There is no voltage/current control.
+
+config DM_REGULATOR_PALMAS
+   bool "Enable driver for PALMAS PMIC regulators"
+   depends on PMIC_PALMAS
+   ---help---
+   This enables implementation of driver-model regulator uclass
+   features for REGULATOR PALMAS and the family of PALMAS PMICs.
+   The driver implements get/set api for: value and enable.
diff --git a/drivers/power/regulator/Makefile b/drivers/power/regulator/Makefile
index 1590d85..75080d4 100644
--- a/drivers/power/regulator/Makefile
+++ b/drivers/power/regulator/Makefile
@@ -14,3 +14,4 @@ obj-$(CONFIG_REGULATOR_RK808) += rk808.o
 obj-$(CONFIG_REGULATOR_S5M8767) += s5m8767.o
 obj-$(CONFIG_DM_REGULATOR_SANDBOX) += sandbox.o
 obj-$(CONFIG_REGULATOR_TPS65090) += tps65090_regulator.o
+obj-$(CONFIG_$(SPL_)DM_REGULATOR_PALMAS) += palmas_regulator.o
diff --git a/drivers/power/regulator/palmas_regulator.c 
b/drivers/power/regulator/palmas_regulator.c
new file mode 100644
index 000..8212bf2
--- /dev/null
+++ b/drivers/power/regulator/palmas_regulator.c
@@ -0,0 +1,459 @@
+/*
+ * (C) Copyright 2016
+ * Texas Instruments Incorporated, 
+ *
+ * Keerthy 
+ *
+ * SPDX-License-Identifier:GPL-2.0+
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+DECLARE_GLOBAL_DATA_PTR;
+
+#defineREGULATOR_ON0x1
+#defineREGULATOR_OFF   0x0
+
+#defineSMPS_MODE_MASK  0x3
+#defineSMPS_MODE_SHIFT 0x0
+#defineLDO_MODE_MASK   0x1
+#defineLDO_MODE_SHIFT  0x0
+
+static const char palmas_smps_ctrl[][PALMAS_SMPS_NUM] = {
+   {0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38, 0x3c},
+   {0x20, 0x24, 0x28, 0x2c, 0x30, 0x34, 0x38},
+   {0x20, 0x24, 0x2c, 0x30, 0x38},
+};
+
+static const char palmas_smps_volt[][PALMAS_SMPS_NUM] = {
+   {0x23, 0x27, 0x2b, 0x2f, 0x33, 0x37, 0x3b, 0x3c},
+   {0x23, 0x27, 0x2b, 0x2f, 0x33, 0x37, 0x3b},
+   {0x23, 0x27, 0x2f, 0x33, 0x3B}
+};
+
+static const char palmas_ldo_ctrl[][PALMAS_LDO_NUM] = {
+   {0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x62, 0x64},
+   {0x50, 0x52, 0x54, 0x56, 0x58, 0x5a, 0x5c, 0x5e, 0x60, 0x62, 0x64},
+   {0x50, 0x52, 0x54, 0x5e, 0x62}
+};
+
+static const char palmas_ldo_volt[][PALMAS_LDO_NUM] = {
+   {0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65},
+   {0x51, 0x53, 0x55, 0x57, 0x59, 0x5b, 0x5d, 0x5f, 0x61, 0x63, 0x65},
+   {0x51, 0x53, 0x55, 0x5f, 0x63}
+};
+
+static int palmas_smps_enable(struct udevice *dev, int op, bool *enable)
+{
+   int ret;
+   uint8_t val;
+   unsigned int adr;
+   struct dm_regulator_uclass_platdata *uc_pdata;
+
+   uc_pdata = dev_get_uclass_platdata(dev);
+   adr = uc_pdata->ctrl_reg;
+
+   val = dm_i2c_reg_read(dev->parent, adr);
+   if (val < 0)
+   return val;
+
+   if (op == PMIC_OP_GET) {
+   val &= PALMAS_SMPS_STATUS_MASK;
+
+   if (val)
+   *enable = true;
+   else
+   *enable = false;
+
+   return 0;
+   } else if (op == PMIC_OP_SET) {
+   if (*enable)
+   val |= PALMAS_SMPS_MODE_MASK;
+   else
+   val &= ~(PALMAS_SMPS_MODE_MASK);
+
+   dm_i2c_write(dev->parent, adr, , 1);
+   if (ret)
+   return ret;
+   }
+
+   return 0;
+}
+
+static int palmas_smps_volt2hex(int uV)
+{
+   if (uV > PALMAS_LDO_VOLT_MAX)
+   return -EINVAL;
+
+   if (uV > 165)
+   return (uV - 100) / 2 + 0x6;
+
+   if (uV == 50)
+   return 0x6;
+   else
+   return 0x6 + ((uV - 50) / 1);
+}
+
+static int palmas_smps_hex2volt(int hex, bool range)
+{
+   unsigned int uV = 0;
+
+   if (hex > PALMAS_SMPS_VOLT_MAX_HEX)
+