diff options
author | Jin Park <jinyoungp@nvidia.com> | 2010-10-28 18:04:04 +0900 |
---|---|---|
committer | Bharat Nihalani <bnihalani@nvidia.com> | 2010-12-16 21:14:37 -0800 |
commit | a4fa764fe47b52411c56a2859ba4d73cac6c43f7 (patch) | |
tree | ca80ab1c9963e9ec478e2cb09acd6db73d3bb210 /drivers/regulator | |
parent | 4bef0b03add4644b2ff3e53acc74366c60dbff87 (diff) |
Regulators: Add max8907c and max8952
Add max8907c regulator and mfd driver.
Add max8952 regulator driver.
Change-Id: If63b724647d887a8088da0f2deb8af91e4fe3cfa
Signed-off-by: Jin Park <jinyoungp@nvidia.com>
Reviewed-on: http://git-master/r/9902
Reviewed-by: Thomas Cherry <tcherry@nvidia.com>
Reviewed-by: Bharat Nihalani <bnihalani@nvidia.com>
Tested-by: Bharat Nihalani <bnihalani@nvidia.com>
Diffstat (limited to 'drivers/regulator')
-rw-r--r-- | drivers/regulator/Kconfig | 16 | ||||
-rw-r--r-- | drivers/regulator/Makefile | 2 | ||||
-rw-r--r-- | drivers/regulator/max8907c-regulator.c | 416 | ||||
-rw-r--r-- | drivers/regulator/max8952.c | 313 |
4 files changed, 747 insertions, 0 deletions
diff --git a/drivers/regulator/Kconfig b/drivers/regulator/Kconfig index 172951bf23a4..82fba419f2f5 100644 --- a/drivers/regulator/Kconfig +++ b/drivers/regulator/Kconfig @@ -108,6 +108,22 @@ config REGULATOR_MAX8998 via I2C bus. The provided regulator is suitable for S3C6410 and S5PC1XX chips to control VCC_CORE and VCC_USIM voltages. +config REGULATOR_MAX8907C + tristate "Maxim 8907C voltage regulator" + depends on MFD_MAX8907C + help + This driver controls a Maxim 8907C voltage output regulator + via I2C bus. The provided regulator is suitable for Tegra + chip to control Step-Down DC-DC and LDOs. + +config REGULATOR_MAX8952 + tristate "Maxim 8952 voltage regulator" + depends on I2C + help + This driver controls a Maxim 8952 voltage output regulator + via I2C bus. The provided regulator is suitable for Tegra + chip to control Step-Down Regulator. + config REGULATOR_TWL4030 bool "TI TWL4030/TWL5030/TWL6030/TPS695x0 PMIC" depends on TWL4030_CORE diff --git a/drivers/regulator/Makefile b/drivers/regulator/Makefile index 8285fd832e16..13ab9f9b09a0 100644 --- a/drivers/regulator/Makefile +++ b/drivers/regulator/Makefile @@ -18,6 +18,8 @@ obj-$(CONFIG_REGULATOR_MAX8649) += max8649.o obj-$(CONFIG_REGULATOR_MAX8660) += max8660.o obj-$(CONFIG_REGULATOR_MAX8925) += max8925-regulator.o obj-$(CONFIG_REGULATOR_MAX8998) += max8998.o +obj-$(CONFIG_REGULATOR_MAX8907C) += max8907c-regulator.o +obj-$(CONFIG_REGULATOR_MAX8952) += max8952.o obj-$(CONFIG_REGULATOR_WM831X) += wm831x-dcdc.o obj-$(CONFIG_REGULATOR_WM831X) += wm831x-isink.o obj-$(CONFIG_REGULATOR_WM831X) += wm831x-ldo.o diff --git a/drivers/regulator/max8907c-regulator.c b/drivers/regulator/max8907c-regulator.c new file mode 100644 index 000000000000..fc3087670164 --- /dev/null +++ b/drivers/regulator/max8907c-regulator.c @@ -0,0 +1,416 @@ +/* + * max8907c-regulator.c -- support regulators in max8907c + * + * Copyright (C) 2010 Gyungoh Yoo <jack.yoo@maxim-ic.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/slab.h> +#include <linux/err.h> +#include <linux/platform_device.h> +#include <linux/regulator/driver.h> +#include <linux/mfd/max8907c.h> +#include <linux/regulator/max8907c-regulator.h> + +#define MAX8907C_REGULATOR_CNT (ARRAY_SIZE(max8907c_regulators)) + +struct max8907c_regulator_info { + u32 min_uV; + u32 max_uV; + u32 step_uV; + u8 reg_base; + struct regulator_desc desc; +}; + +#define REG_LDO(ids, base, min, max, step) \ + { \ + .min_uV = (min), \ + .max_uV = (max), \ + .step_uV = (step), \ + .reg_base = (base), \ + .desc = { \ + .name = #ids, \ + .id = MAX8907C_##ids, \ + .n_voltages = ((max) - (min)) / (step) + 1, \ + .ops = &max8907c_ldo_ops, \ + .type = REGULATOR_VOLTAGE, \ + .owner = THIS_MODULE, \ + }, \ + } + +#define REG_FIXED(ids, voltage) \ + { \ + .min_uV = (voltage), \ + .max_uV = (voltage), \ + .desc = { \ + .name = #ids, \ + .id = MAX8907C_##ids, \ + .n_voltages = 1, \ + .ops = &max8907c_fixed_ops, \ + .type = REGULATOR_VOLTAGE, \ + .owner = THIS_MODULE, \ + }, \ + } + +#define REG_OUT5V(ids, base, voltage) \ + { \ + .min_uV = (voltage), \ + .max_uV = (voltage), \ + .reg_base = (base), \ + .desc = { \ + .name = #ids, \ + .id = MAX8907C_##ids, \ + .n_voltages = 1, \ + .ops = &max8907c_out5v_ops, \ + .type = REGULATOR_VOLTAGE, \ + .owner = THIS_MODULE, \ + }, \ + } + +#define REG_BBAT(ids, base, min, max, step) \ + { \ + .min_uV = (min), \ + .max_uV = (max), \ + .step_uV = (step), \ + .reg_base = (base), \ + .desc = { \ + .name = #ids, \ + .id = MAX8907C_##ids, \ + .n_voltages = ((max) - (min)) / (step) + 1, \ + .ops = &max8907c_bbat_ops, \ + .type = REGULATOR_VOLTAGE, \ + .owner = THIS_MODULE, \ + }, \ + } + +#define REG_WLED(ids, base, voltage) \ + { \ + .min_uV = (voltage), \ + .max_uV = (voltage), \ + .reg_base = (base), \ + .desc = { \ + .name = #ids, \ + .id = MAX8907C_##ids, \ + .n_voltages = 1, \ + .ops = &max8907c_wled_ops, \ + .type = REGULATOR_CURRENT, \ + .owner = THIS_MODULE, \ + }, \ + } + +#define LDO_750_50(id, base) REG_LDO(id, (base), 750000, 3900000, 50000) +#define LDO_650_25(id, base) REG_LDO(id, (base), 650000, 2225000, 25000) + +static int max8907c_regulator_list_voltage(struct regulator_dev *dev, + unsigned index); +static int max8907c_regulator_ldo_set_voltage(struct regulator_dev *dev, + int min_uV, int max_uV); +static int max8907c_regulator_bbat_set_voltage(struct regulator_dev *dev, + int min_uV, int max_uV); +static int max8907c_regulator_ldo_get_voltage(struct regulator_dev *dev); +static int max8907c_regulator_fixed_get_voltage(struct regulator_dev *dev); +static int max8907c_regulator_bbat_get_voltage(struct regulator_dev *dev); +static int max8907c_regulator_wled_set_current_limit(struct regulator_dev *dev, + int min_uA, int max_uA); +static int max8907c_regulator_wled_get_current_limit(struct regulator_dev *dev); +static int max8907c_regulator_ldo_enable(struct regulator_dev *dev); +static int max8907c_regulator_out5v_enable(struct regulator_dev *dev); +static int max8907c_regulator_ldo_disable(struct regulator_dev *dev); +static int max8907c_regulator_out5v_disable(struct regulator_dev *dev); +static int max8907c_regulator_ldo_is_enabled(struct regulator_dev *dev); +static int max8907c_regulator_out5v_is_enabled(struct regulator_dev *dev); + +static struct regulator_ops max8907c_ldo_ops = { + .list_voltage = max8907c_regulator_list_voltage, + .set_voltage = max8907c_regulator_ldo_set_voltage, + .get_voltage = max8907c_regulator_ldo_get_voltage, + .enable = max8907c_regulator_ldo_enable, + .disable = max8907c_regulator_ldo_disable, + .is_enabled = max8907c_regulator_ldo_is_enabled, +}; + +static struct regulator_ops max8907c_fixed_ops = { + .list_voltage = max8907c_regulator_list_voltage, + .get_voltage = max8907c_regulator_fixed_get_voltage, +}; + +static struct regulator_ops max8907c_out5v_ops = { + .list_voltage = max8907c_regulator_list_voltage, + .get_voltage = max8907c_regulator_fixed_get_voltage, + .enable = max8907c_regulator_out5v_enable, + .disable = max8907c_regulator_out5v_disable, + .is_enabled = max8907c_regulator_out5v_is_enabled, +}; + +static struct regulator_ops max8907c_bbat_ops = { + .list_voltage = max8907c_regulator_list_voltage, + .set_voltage = max8907c_regulator_bbat_set_voltage, + .get_voltage = max8907c_regulator_bbat_get_voltage, +}; + +static struct regulator_ops max8907c_wled_ops = { + .list_voltage = max8907c_regulator_list_voltage, + .set_current_limit = max8907c_regulator_wled_set_current_limit, + .get_current_limit = max8907c_regulator_wled_get_current_limit, + .get_voltage = max8907c_regulator_fixed_get_voltage, +}; + +static struct max8907c_regulator_info max8907c_regulators[] = { + REG_LDO(SD1, MAX8907C_REG_SDCTL1, 650000, 2225000, 25000), + REG_LDO(SD2, MAX8907C_REG_SDCTL2, 637500, 1425000, 12500), + REG_LDO(SD3, MAX8907C_REG_SDCTL3, 750000, 3900000, 50000), + LDO_750_50(LDO1, MAX8907C_REG_LDOCTL1), + LDO_650_25(LDO2, MAX8907C_REG_LDOCTL2), + LDO_650_25(LDO3, MAX8907C_REG_LDOCTL3), + LDO_750_50(LDO4, MAX8907C_REG_LDOCTL4), + LDO_750_50(LDO5, MAX8907C_REG_LDOCTL5), + LDO_750_50(LDO6, MAX8907C_REG_LDOCTL6), + LDO_750_50(LDO7, MAX8907C_REG_LDOCTL7), + LDO_750_50(LDO8, MAX8907C_REG_LDOCTL8), + LDO_750_50(LDO9, MAX8907C_REG_LDOCTL9), + LDO_750_50(LDO10, MAX8907C_REG_LDOCTL10), + LDO_750_50(LDO11, MAX8907C_REG_LDOCTL11), + LDO_750_50(LDO12, MAX8907C_REG_LDOCTL12), + LDO_750_50(LDO13, MAX8907C_REG_LDOCTL13), + LDO_750_50(LDO14, MAX8907C_REG_LDOCTL14), + LDO_750_50(LDO15, MAX8907C_REG_LDOCTL15), + LDO_750_50(LDO16, MAX8907C_REG_LDOCTL16), + LDO_650_25(LDO17, MAX8907C_REG_LDOCTL17), + LDO_650_25(LDO18, MAX8907C_REG_LDOCTL18), + LDO_750_50(LDO19, MAX8907C_REG_LDOCTL19), + LDO_750_50(LDO20, MAX8907C_REG_LDOCTL20), + REG_OUT5V(OUT5V, MAX8907C_REG_OUT5VEN, 5000000), + REG_OUT5V(OUT33V, MAX8907C_REG_OUT33VEN, 3300000), + REG_BBAT(BBAT, MAX8907C_REG_BBAT_CNFG, 2400000, 3000000, 200000), + REG_FIXED(SDBY, 1200000), + REG_FIXED(VRTC, 3300000), + REG_WLED(WLED, MAX8907C_REG_ILED_CNTL, 0), +}; + +static int max8907c_regulator_list_voltage(struct regulator_dev *rdev, + unsigned index) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + + return info->min_uV + info->step_uV * index; +} + +static int max8907c_regulator_ldo_set_voltage(struct regulator_dev *rdev, + int min_uV, int max_uV) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + int val; + + if (min_uV < info->min_uV || max_uV > info->max_uV) + return -EDOM; + + val = (min_uV - info->min_uV) / info->step_uV; + + return max8907c_reg_write(parent, info->reg_base + MAX8907C_VOUT, val); +} + +static int max8907c_regulator_bbat_set_voltage(struct regulator_dev *rdev, + int min_uV, int max_uV) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + int val; + + if (min_uV < info->min_uV || max_uV > info->max_uV) + return -EDOM; + + val = (min_uV - info->min_uV) / info->step_uV; + + return max8907c_set_bits(parent, info->reg_base, MAX8907C_MASK_VBBATTCV, + val); +} + +static int max8907c_regulator_ldo_get_voltage(struct regulator_dev *rdev) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + int val; + + val = max8907c_reg_read(parent, info->reg_base + MAX8907C_VOUT); + return val * info->step_uV + info->min_uV; +} + +static int max8907c_regulator_fixed_get_voltage(struct regulator_dev *rdev) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + + return info->min_uV; +} + +static int max8907c_regulator_bbat_get_voltage(struct regulator_dev *rdev) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + int val; + + val = + max8907c_reg_read(parent, info->reg_base) & MAX8907C_MASK_VBBATTCV; + return val * info->step_uV + info->min_uV; +} + +static int max8907c_regulator_wled_set_current_limit(struct regulator_dev *rdev, + int min_uA, int max_uA) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + + if (min_uA > 25500) + return -EDOM; + + return max8907c_reg_write(parent, info->reg_base, min_uA / 100); +} + +static int max8907c_regulator_wled_get_current_limit(struct regulator_dev *rdev) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + int val; + + val = max8907c_reg_read(parent, info->reg_base); + return val * 100; +} + +static int max8907c_regulator_ldo_enable(struct regulator_dev *rdev) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + + return max8907c_set_bits(parent, info->reg_base + MAX8907C_CTL, + MAX8907C_MASK_LDO_EN | MAX8907C_MASK_LDO_SEQ, + MAX8907C_MASK_LDO_EN | MAX8907C_MASK_LDO_SEQ); +} + +static int max8907c_regulator_out5v_enable(struct regulator_dev *rdev) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + + return max8907c_set_bits(parent, info->reg_base, + MAX8907C_MASK_OUT5V_VINEN | + MAX8907C_MASK_OUT5V_ENSRC | + MAX8907C_MASK_OUT5V_EN, + MAX8907C_MASK_OUT5V_ENSRC | + MAX8907C_MASK_OUT5V_EN); +} + +static int max8907c_regulator_ldo_disable(struct regulator_dev *rdev) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + + return max8907c_set_bits(parent, info->reg_base + MAX8907C_CTL, + MAX8907C_MASK_LDO_EN | MAX8907C_MASK_LDO_SEQ, + MAX8907C_MASK_LDO_SEQ); +} + +static int max8907c_regulator_out5v_disable(struct regulator_dev *rdev) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + + return max8907c_set_bits(parent, info->reg_base, + MAX8907C_MASK_OUT5V_VINEN | + MAX8907C_MASK_OUT5V_ENSRC | + MAX8907C_MASK_OUT5V_EN, + MAX8907C_MASK_OUT5V_ENSRC); +} + +static int max8907c_regulator_ldo_is_enabled(struct regulator_dev *rdev) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + int val; + + val = max8907c_reg_read(parent, info->reg_base + MAX8907C_CTL); + if (val < 0) + return -EDOM; + + return (val & MAX8907C_MASK_LDO_EN) || !(val & MAX8907C_MASK_LDO_SEQ); +} + +static int max8907c_regulator_out5v_is_enabled(struct regulator_dev *rdev) +{ + const struct max8907c_regulator_info *info = rdev_get_drvdata(rdev); + struct device *parent = rdev_get_dev(rdev)->parent->parent; + int val; + + val = max8907c_reg_read(parent, info->reg_base); + if (val < 0) + return -EDOM; + + if ((val & + (MAX8907C_MASK_OUT5V_VINEN | MAX8907C_MASK_OUT5V_ENSRC | + MAX8907C_MASK_OUT5V_EN)) + == MAX8907C_MASK_OUT5V_ENSRC) + return 1; + + return 0; +} + +static int max8907c_regulator_probe(struct platform_device *pdev) +{ + struct max8907c_regulator_info *info; + struct regulator_dev *rdev; + + info = &max8907c_regulators[pdev->id]; + + rdev = regulator_register(&info->desc, + &pdev->dev, pdev->dev.platform_data, info); + if (IS_ERR(rdev)) { + dev_err(&pdev->dev, "Cannot register regulator \"%s\", %ld\n", + info->desc.name, PTR_ERR(rdev)); + goto error; + } + + platform_set_drvdata(pdev, rdev); + return 0; + +error: + return PTR_ERR(rdev); +} + +static int max8907c_regulator_remove(struct platform_device *pdev) +{ + struct regulator_dev *rdev = platform_get_drvdata(pdev); + + regulator_unregister(rdev); + return 0; +} + +static struct platform_driver max8907c_regulator_driver = { + .driver = { + .name = "max8907c-regulator", + .owner = THIS_MODULE, + }, + .probe = max8907c_regulator_probe, + .remove = __devexit_p(max8907c_regulator_remove), +}; + +static int __init max8907c_regulator_init(void) +{ + return platform_driver_register(&max8907c_regulator_driver); +} + +subsys_initcall(max8907c_regulator_init); + +static void __exit max8907c_reg_exit(void) +{ + platform_driver_unregister(&max8907c_regulator_driver); +} + +module_exit(max8907c_reg_exit); + +MODULE_DESCRIPTION("MAX8907C regulator driver"); +MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@maxim-ic.com>"); +MODULE_LICENSE("GPL"); diff --git a/drivers/regulator/max8952.c b/drivers/regulator/max8952.c new file mode 100644 index 000000000000..bc0c8b8835c5 --- /dev/null +++ b/drivers/regulator/max8952.c @@ -0,0 +1,313 @@ +/* + * max8952.c -- support regulators in max8952 + * + * Copyright (C) 2010 Gyungoh Yoo <jack.yoo@maxim-ic.com> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + */ + +#include <linux/module.h> +#include <linux/init.h> +#include <linux/err.h> +#include <linux/i2c.h> +#include <linux/regulator/driver.h> +#include <linux/slab.h> +#include <linux/regulator/max8952.h> +#include <linux/platform_device.h> + +#define VOLTAGE_TO_VALUE(v) (((v) - 750000) / 10000) +#define VALUE_TO_VOLTAGE(val) ((val) * 10000 + 750000) + +struct max8952_info { + int voltages_count; + const int *voltages_list; + u8 reg_base; + struct regulator_desc desc; +}; + +#define REG(ids, base, list) \ + { \ + .voltages_list = (list), \ + .reg_base = (base), \ + .desc = { \ + .name = #ids, \ + .id = MAX8952_##ids, \ + .n_voltages = ARRAY_SIZE((list)), \ + .ops = &max8952_ops, \ + .type = REGULATOR_VOLTAGE, \ + .owner = THIS_MODULE, \ + }, \ + } + +static const int max8952_mode0_voltages[] = { + 750000, 760000, 1260000, 1270000, 1280000, 1370000, 1380000 +}; + +static const int max8952_mode1_voltages[] = { + 750000, 760000, 1040000, 1050000, 1060000, 1370000, 1380000 +}; + +static const int max8952_mode2_voltages[] = { + 750000, 760000, 1210000, 1220000, 1230000, 1370000, 1380000 +}; + +static const int max8952_mode3_voltages[] = { + 750000, 760000, 1040000, 1050000, 1060000, 1370000, 1380000 +}; + +static int max8952_list_voltage(struct regulator_dev *dev, unsigned index); +static int max8952_set_voltage(struct regulator_dev *dev, int min_uV, + int max_uV); +static int max8952_get_voltage(struct regulator_dev *dev); + +static struct regulator_ops max8952_ops = { + .list_voltage = max8952_list_voltage, + .set_voltage = max8952_set_voltage, + .get_voltage = max8952_get_voltage, +}; + +static struct max8952_info max8952_regulators[] = { + REG(MODE0, MAX8952_REG_MODE0, max8952_mode0_voltages), + REG(MODE1, MAX8952_REG_MODE1, max8952_mode1_voltages), + REG(MODE2, MAX8952_REG_MODE2, max8952_mode2_voltages), + REG(MODE3, MAX8952_REG_MODE3, max8952_mode3_voltages), +}; + +#define MAX8952_REGULATOR_CNT (ARRAY_SIZE(max8952_regulators)) + +struct max8952 { + struct i2c_client *i2c; + struct mutex io_lock; + struct regulator_dev *rdev[MAX8952_REGULATOR_CNT]; +}; + +static int max8952_i2c_read(struct i2c_client *i2c, u8 reg, u8 count, u8 * dest) +{ + struct i2c_msg xfer[2]; + int ret; + + xfer[0].addr = i2c->addr; + xfer[0].flags = I2C_M_NOSTART; + xfer[0].len = 1; + xfer[0].buf = ® + + xfer[1].addr = i2c->addr; + xfer[1].flags = I2C_M_RD; + xfer[1].len = count; + xfer[1].buf = dest; + + ret = i2c_transfer(i2c->adapter, xfer, 2); + if (ret == 2) + ret = 0; + else if (ret >= 0) + ret = -EIO; + + return ret; +} + +static int max8952_i2c_write(struct i2c_client *i2c, u8 reg, u8 count, + const u8 *src) +{ + u8 msg[0x100 + 1]; + int ret; + + msg[0] = reg; + memcpy(&msg[1], src, count); + + ret = i2c_master_send(i2c, msg, count + 1); + if (ret < 0) + return ret; + if (ret != count + 1) + return -EIO; + return 0; +} + +static u8 max8952_read(struct max8952 *max8952, u8 reg) +{ + u8 val; + int ret; + + mutex_lock(&max8952->io_lock); + + ret = max8952_i2c_read(max8952->i2c, reg, 1, &val); + + mutex_unlock(&max8952->io_lock); + pr_debug("max8952: reg read reg=%x, val=%x\n", (unsigned int)reg, + (unsigned int)val); + + if (ret < 0) + pr_err("Failed to read max8952 I2C driver: %d\n", ret); + return val; +} + +static int max8952_write(struct max8952 *max8952, u8 reg, u8 val) +{ + int ret; + + pr_debug("max8952: reg write reg=%x, val=%x\n", (unsigned int)reg, + (unsigned int)val); + mutex_lock(&max8952->io_lock); + + ret = max8952_i2c_write(max8952->i2c, reg, 1, &val); + + mutex_unlock(&max8952->io_lock); + + if (ret < 0) + pr_err("Failed to write max8952 I2C driver: %d\n", ret); + return ret; +} + +int max8952_set_bits(struct max8952 *max8952, u8 reg, u8 mask, u8 val) +{ + u8 tmp; + int ret; + + pr_debug("max8952: reg write reg=%02X, val=%02X, mask=%02X\n", + (unsigned int)reg, (unsigned int)val, (unsigned int)mask); + mutex_lock(&max8952->io_lock); + + ret = max8952_i2c_read(max8952->i2c, reg, 1, &tmp); + if (ret == 0) { + val = (tmp & ~mask) | (val & mask); + ret = max8952_i2c_write(max8952->i2c, reg, 1, &val); + } + + mutex_unlock(&max8952->io_lock); + + if (ret != 0) + pr_err("Failed to write max8952 I2C driver: %d\n", ret); + return ret; +} + +static int max8952_list_voltage(struct regulator_dev *rdev, unsigned index) +{ + const struct max8952_info *reg = &max8952_regulators[rdev_get_id(rdev)]; + + return reg->voltages_list[index]; +} + +static int max8952_set_voltage(struct regulator_dev *rdev, int min_uV, + int max_uV) +{ + struct max8952 *max8952 = rdev_get_drvdata(rdev); + const struct max8952_info *reg = &max8952_regulators[rdev_get_id(rdev)]; + int val = -1; + int voltage; + int i; + + for (i = 0; i < reg->desc.n_voltages; i++) { + voltage = reg->voltages_list[i]; + if (min_uV <= voltage && voltage <= max_uV) { + val = VOLTAGE_TO_VALUE(voltage); + break; + } + } + if (val == -1) + return -EDOM; + + return max8952_set_bits(max8952, reg->reg_base, MAX8952_MASK_OUTMODE, + val); +} + +static int max8952_get_voltage(struct regulator_dev *rdev) +{ + struct max8952 *max8952 = rdev_get_drvdata(rdev); + const struct max8952_info *reg = &max8952_regulators[rdev_get_id(rdev)]; + int val; + + val = max8952_read(max8952, reg->reg_base); + val &= MAX8952_MASK_OUTMODE; + + return VALUE_TO_VOLTAGE(val); +} + +static int __devinit max8952_probe(struct i2c_client *i2c, + const struct i2c_device_id *i2c_id) +{ + struct max8952 *max8952; + struct max8952_platform_data *pdata = i2c->dev.platform_data; + struct regulator_dev *rdev; + int id; + int i; + + max8952 = kzalloc(sizeof(struct max8952), GFP_KERNEL); + if (max8952 == NULL) + return -ENOMEM; + + max8952->i2c = i2c; + mutex_init(&max8952->io_lock); + + for (i = 0; i < pdata->num_subdevs; i++) { + id = pdata->subdevs[i]->id; + rdev = + regulator_register(&max8952_regulators[id].desc, &i2c->dev, + (struct regulator_init_data *)pdata-> + subdevs[i]->dev.platform_data, max8952); + if (IS_ERR(rdev)) { + dev_err(&i2c->dev, + "Cannot register regulator \"%s\", %ld\n", + max8952_regulators[id].desc.name, + PTR_ERR(rdev)); + goto error; + } + max8952->rdev[id] = rdev; + } + + i2c_set_clientdata(i2c, max8952); + return 0; + +error: + kfree(max8952); + return PTR_ERR(rdev); +} + +static int __devexit max8952_remove(struct i2c_client *i2c) +{ + struct max8952 *max8952 = i2c_get_clientdata(i2c); + int i; + + for (i = 0; i < MAX8952_REGULATOR_CNT; i++) { + if (max8952->rdev[i]) + regulator_unregister(max8952->rdev[i]); + } + kfree(max8952); + + return 0; +} + +static const struct i2c_device_id max8952_id[] = { + {"max8952", 0}, + {} +}; + +MODULE_DEVICE_TABLE(i2c, max8952_id); + +static struct i2c_driver max8952_driver = { + .probe = max8952_probe, + .remove = __devexit_p(max8952_remove), + .driver = { + .name = "max8952", + .owner = THIS_MODULE, + }, + .id_table = max8952_id, +}; + +static int __init max8952_init(void) +{ + return i2c_add_driver(&max8952_driver); +} + +subsys_initcall(max8952_init); + +static void __exit max8952_exit(void) +{ + i2c_del_driver(&max8952_driver); +} + +module_exit(max8952_exit); + +MODULE_DESCRIPTION("MAX8952 regulator driver"); +MODULE_AUTHOR("Gyungoh Yoo <jack.yoo@maxim-ic.com>"); +MODULE_LICENSE("GPL"); |