[PATCH v6 2/3] regulator: max14577: Add regulator driver for Maxim 14577

2013-12-04 Thread Krzysztof Kozlowski
MAX14577 chip is a multi-function device which includes MUIC,
charger and voltage regulator. The driver is located in drivers/mfd.

This patch adds regulator driver for MAX14577 chip. There are two
regulators in this chip:
1. Safeout LDO with constant voltage output of 4.9V. It can be only
   enabled or disabled.
2. Current regulator for the charger. It provides current from 90mA up
   to 950mA.
Driver supports Device Tree.

Signed-off-by: Krzysztof Kozlowski 
Signed-off-by: Kyungmin Park 
---
 drivers/regulator/Kconfig|7 ++
 drivers/regulator/Makefile   |1 +
 drivers/regulator/max14577.c |  272 ++
 3 files changed, 280 insertions(+)
 create mode 100644 drivers/regulator/max14577.c

diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
index ce785f481281..11ee05341926 100644
--- a/drivers/regulator/Kconfig
+++ b/drivers/regulator/Kconfig
@@ -249,6 +249,13 @@ config REGULATOR_LP8788
help
  This driver supports LP8788 voltage regulator chip.
 
+config REGULATOR_MAX14577
+   tristate "Maxim 14577 regulator"
+   depends on MFD_MAX14577
+   help
+ This driver controls a Maxim 14577 regulator via I2C bus.
+ The regulators include safeout LDO and current regulator 'CHARGER'.
+
 config REGULATOR_MAX1586
tristate "Maxim 1586/1587 voltage regulator"
depends on I2C
diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile
index 01c597ea1744..654bd43a7426 100644
--- a/drivers/regulator/Makefile
+++ b/drivers/regulator/Makefile
@@ -35,6 +35,7 @@ obj-$(CONFIG_REGULATOR_LP872X) += lp872x.o
 obj-$(CONFIG_REGULATOR_LP8788) += lp8788-buck.o
 obj-$(CONFIG_REGULATOR_LP8788) += lp8788-ldo.o
 obj-$(CONFIG_REGULATOR_LP8755) += lp8755.o
+obj-$(CONFIG_REGULATOR_MAX14577) += max14577.o
 obj-$(CONFIG_REGULATOR_MAX1586) += max1586.o
 obj-$(CONFIG_REGULATOR_MAX8649)+= max8649.o
 obj-$(CONFIG_REGULATOR_MAX8660) += max8660.o
diff --git a/drivers/regulator/max14577.c b/drivers/regulator/max14577.c
new file mode 100644
index ..516e8af05641
--- /dev/null
+++ b/drivers/regulator/max14577.c
@@ -0,0 +1,272 @@
+/*
+ * max14577.c - Regulator driver for the Maxim 14577
+ *
+ * Copyright (C) 2013 Samsung Electronics
+ * Krzysztof Kozlowski 
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+struct max14577_regulator {
+   struct device *dev;
+   struct max14577 *max14577;
+   struct regulator_dev **regulators;
+};
+
+static int max14577_reg_is_enabled(struct regulator_dev *rdev)
+{
+   int rid = rdev_get_id(rdev);
+   struct regmap *rmap = rdev->regmap;
+   u8 reg_data;
+
+   switch (rid) {
+   case MAX14577_CHARGER:
+   max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, _data);
+   if ((reg_data & CHGCTRL2_MBCHOSTEN_MASK) == 0)
+   return 0;
+   max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, _data);
+   if ((reg_data & STATUS3_CGMBC_MASK) == 0)
+   return 0;
+   /* MBCHOSTEN and CGMBC are on */
+   return 1;
+   default:
+   return -EINVAL;
+   }
+}
+
+static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
+{
+   u8 reg_data;
+   struct regmap *rmap = rdev->regmap;
+
+   if (rdev_get_id(rdev) != MAX14577_CHARGER)
+   return -EINVAL;
+
+   max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, _data);
+
+   if ((reg_data & CHGCTRL4_MBCICHWRCL_MASK) == 0)
+   return MAX14577_REGULATOR_CURRENT_LIMIT_MIN;
+
+   reg_data = ((reg_data & CHGCTRL4_MBCICHWRCH_MASK) >>
+   CHGCTRL4_MBCICHWRCH_SHIFT);
+   return MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START +
+   reg_data * MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP;
+}
+
+static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
+   int min_uA, int max_uA)
+{
+   int i, current_bits = 0xf;
+   u8 reg_data;
+
+   if (rdev_get_id(rdev) != MAX14577_CHARGER)
+   return -EINVAL;
+
+   if (min_uA > MAX14577_REGULATOR_CURRENT_LIMIT_MAX ||
+   max_uA < MAX14577_REGULATOR_CURRENT_LIMIT_MIN)
+   return -EINVAL;
+
+   if (max_uA < MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START) {
+   /* Less than 200 mA, so set 90mA (turn only Low Bit off) */
+   u8 reg_data = 0x0 << 

[PATCH v6 2/3] regulator: max14577: Add regulator driver for Maxim 14577

2013-12-04 Thread Krzysztof Kozlowski
MAX14577 chip is a multi-function device which includes MUIC,
charger and voltage regulator. The driver is located in drivers/mfd.

This patch adds regulator driver for MAX14577 chip. There are two
regulators in this chip:
1. Safeout LDO with constant voltage output of 4.9V. It can be only
   enabled or disabled.
2. Current regulator for the charger. It provides current from 90mA up
   to 950mA.
Driver supports Device Tree.

Signed-off-by: Krzysztof Kozlowski k.kozlow...@samsung.com
Signed-off-by: Kyungmin Park kyungmin.p...@samsung.com
---
 drivers/regulator/Kconfig|7 ++
 drivers/regulator/Makefile   |1 +
 drivers/regulator/max14577.c |  272 ++
 3 files changed, 280 insertions(+)
 create mode 100644 drivers/regulator/max14577.c

diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig
index ce785f481281..11ee05341926 100644
--- a/drivers/regulator/Kconfig
+++ b/drivers/regulator/Kconfig
@@ -249,6 +249,13 @@ config REGULATOR_LP8788
help
  This driver supports LP8788 voltage regulator chip.
 
+config REGULATOR_MAX14577
+   tristate Maxim 14577 regulator
+   depends on MFD_MAX14577
+   help
+ This driver controls a Maxim 14577 regulator via I2C bus.
+ The regulators include safeout LDO and current regulator 'CHARGER'.
+
 config REGULATOR_MAX1586
tristate Maxim 1586/1587 voltage regulator
depends on I2C
diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile
index 01c597ea1744..654bd43a7426 100644
--- a/drivers/regulator/Makefile
+++ b/drivers/regulator/Makefile
@@ -35,6 +35,7 @@ obj-$(CONFIG_REGULATOR_LP872X) += lp872x.o
 obj-$(CONFIG_REGULATOR_LP8788) += lp8788-buck.o
 obj-$(CONFIG_REGULATOR_LP8788) += lp8788-ldo.o
 obj-$(CONFIG_REGULATOR_LP8755) += lp8755.o
+obj-$(CONFIG_REGULATOR_MAX14577) += max14577.o
 obj-$(CONFIG_REGULATOR_MAX1586) += max1586.o
 obj-$(CONFIG_REGULATOR_MAX8649)+= max8649.o
 obj-$(CONFIG_REGULATOR_MAX8660) += max8660.o
diff --git a/drivers/regulator/max14577.c b/drivers/regulator/max14577.c
new file mode 100644
index ..516e8af05641
--- /dev/null
+++ b/drivers/regulator/max14577.c
@@ -0,0 +1,272 @@
+/*
+ * max14577.c - Regulator driver for the Maxim 14577
+ *
+ * Copyright (C) 2013 Samsung Electronics
+ * Krzysztof Kozlowski k.kozlow...@samsung.com
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ */
+
+#include linux/module.h
+#include linux/platform_device.h
+#include linux/regulator/driver.h
+#include linux/mfd/max14577.h
+#include linux/mfd/max14577-private.h
+#include linux/regulator/of_regulator.h
+
+struct max14577_regulator {
+   struct device *dev;
+   struct max14577 *max14577;
+   struct regulator_dev **regulators;
+};
+
+static int max14577_reg_is_enabled(struct regulator_dev *rdev)
+{
+   int rid = rdev_get_id(rdev);
+   struct regmap *rmap = rdev-regmap;
+   u8 reg_data;
+
+   switch (rid) {
+   case MAX14577_CHARGER:
+   max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL2, reg_data);
+   if ((reg_data  CHGCTRL2_MBCHOSTEN_MASK) == 0)
+   return 0;
+   max14577_read_reg(rmap, MAX14577_CHG_REG_STATUS3, reg_data);
+   if ((reg_data  STATUS3_CGMBC_MASK) == 0)
+   return 0;
+   /* MBCHOSTEN and CGMBC are on */
+   return 1;
+   default:
+   return -EINVAL;
+   }
+}
+
+static int max14577_reg_get_current_limit(struct regulator_dev *rdev)
+{
+   u8 reg_data;
+   struct regmap *rmap = rdev-regmap;
+
+   if (rdev_get_id(rdev) != MAX14577_CHARGER)
+   return -EINVAL;
+
+   max14577_read_reg(rmap, MAX14577_CHG_REG_CHG_CTRL4, reg_data);
+
+   if ((reg_data  CHGCTRL4_MBCICHWRCL_MASK) == 0)
+   return MAX14577_REGULATOR_CURRENT_LIMIT_MIN;
+
+   reg_data = ((reg_data  CHGCTRL4_MBCICHWRCH_MASK) 
+   CHGCTRL4_MBCICHWRCH_SHIFT);
+   return MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_START +
+   reg_data * MAX14577_REGULATOR_CURRENT_LIMIT_HIGH_STEP;
+}
+
+static int max14577_reg_set_current_limit(struct regulator_dev *rdev,
+   int min_uA, int max_uA)
+{
+   int i, current_bits = 0xf;
+   u8 reg_data;
+
+   if (rdev_get_id(rdev) != MAX14577_CHARGER)
+   return -EINVAL;
+
+   if (min_uA  MAX14577_REGULATOR_CURRENT_LIMIT_MAX ||
+   max_uA  MAX14577_REGULATOR_CURRENT_LIMIT_MIN)
+