1/* 2 * max77693.c - Regulator driver for the Maxim 77693 3 * 4 * Copyright (C) 2013 Samsung Electronics 5 * Jonghwa Lee <jonghwa3.lee@samsung.com> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 2 of the License, or 10 * (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 20 * 21 * This driver is based on max77686.c 22 */ 23 24#include <linux/err.h> 25#include <linux/slab.h> 26#include <linux/platform_device.h> 27#include <linux/module.h> 28#include <linux/export.h> 29#include <linux/regulator/driver.h> 30#include <linux/regulator/machine.h> 31#include <linux/mfd/max77693.h> 32#include <linux/mfd/max77693-private.h> 33#include <linux/regulator/of_regulator.h> 34#include <linux/regmap.h> 35 36#define CHGIN_ILIM_STEP_20mA 20000 37 38/* CHARGER regulator ops */ 39/* CHARGER regulator uses two bits for enabling */ 40static int max77693_chg_is_enabled(struct regulator_dev *rdev) 41{ 42 int ret; 43 unsigned int val; 44 45 ret = regmap_read(rdev->regmap, rdev->desc->enable_reg, &val); 46 if (ret) 47 return ret; 48 49 return (val & rdev->desc->enable_mask) == rdev->desc->enable_mask; 50} 51 52/* 53 * CHARGER regulator - Min : 20mA, Max : 2580mA, step : 20mA 54 * 0x00, 0x01, 0x2, 0x03 = 60 mA 55 * 0x04 ~ 0x7E = (60 + (X - 3) * 20) mA 56 */ 57static int max77693_chg_get_current_limit(struct regulator_dev *rdev) 58{ 59 unsigned int chg_min_uA = rdev->constraints->min_uA; 60 unsigned int chg_max_uA = rdev->constraints->max_uA; 61 unsigned int reg, sel; 62 unsigned int val; 63 int ret; 64 65 ret = regmap_read(rdev->regmap, MAX77693_CHG_REG_CHG_CNFG_09, ®); 66 if (ret < 0) 67 return ret; 68 69 sel = reg & CHG_CNFG_09_CHGIN_ILIM_MASK; 70 71 /* the first four codes for charger current are all 60mA */ 72 if (sel <= 3) 73 sel = 0; 74 else 75 sel -= 3; 76 77 val = chg_min_uA + CHGIN_ILIM_STEP_20mA * sel; 78 if (val > chg_max_uA) 79 return -EINVAL; 80 81 return val; 82} 83 84static int max77693_chg_set_current_limit(struct regulator_dev *rdev, 85 int min_uA, int max_uA) 86{ 87 unsigned int chg_min_uA = rdev->constraints->min_uA; 88 int sel = 0; 89 90 while (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel < min_uA) 91 sel++; 92 93 if (chg_min_uA + CHGIN_ILIM_STEP_20mA * sel > max_uA) 94 return -EINVAL; 95 96 /* the first four codes for charger current are all 60mA */ 97 sel += 3; 98 99 return regmap_write(rdev->regmap, 100 MAX77693_CHG_REG_CHG_CNFG_09, sel); 101} 102/* end of CHARGER regulator ops */ 103 104static const unsigned int max77693_safeout_table[] = { 105 4850000, 106 4900000, 107 4950000, 108 3300000, 109}; 110 111static struct regulator_ops max77693_safeout_ops = { 112 .list_voltage = regulator_list_voltage_table, 113 .is_enabled = regulator_is_enabled_regmap, 114 .enable = regulator_enable_regmap, 115 .disable = regulator_disable_regmap, 116 .get_voltage_sel = regulator_get_voltage_sel_regmap, 117 .set_voltage_sel = regulator_set_voltage_sel_regmap, 118}; 119 120static struct regulator_ops max77693_charger_ops = { 121 .is_enabled = max77693_chg_is_enabled, 122 .enable = regulator_enable_regmap, 123 .disable = regulator_disable_regmap, 124 .get_current_limit = max77693_chg_get_current_limit, 125 .set_current_limit = max77693_chg_set_current_limit, 126}; 127 128#define regulator_desc_esafeout(_num) { \ 129 .name = "ESAFEOUT"#_num, \ 130 .id = MAX77693_ESAFEOUT##_num, \ 131 .of_match = of_match_ptr("ESAFEOUT"#_num), \ 132 .regulators_node = of_match_ptr("regulators"), \ 133 .n_voltages = 4, \ 134 .ops = &max77693_safeout_ops, \ 135 .type = REGULATOR_VOLTAGE, \ 136 .owner = THIS_MODULE, \ 137 .volt_table = max77693_safeout_table, \ 138 .vsel_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \ 139 .vsel_mask = SAFEOUT_CTRL_SAFEOUT##_num##_MASK, \ 140 .enable_reg = MAX77693_CHG_REG_SAFEOUT_CTRL, \ 141 .enable_mask = SAFEOUT_CTRL_ENSAFEOUT##_num##_MASK , \ 142} 143 144static const struct regulator_desc regulators[] = { 145 regulator_desc_esafeout(1), 146 regulator_desc_esafeout(2), 147 { 148 .name = "CHARGER", 149 .id = MAX77693_CHARGER, 150 .of_match = of_match_ptr("CHARGER"), 151 .regulators_node = of_match_ptr("regulators"), 152 .ops = &max77693_charger_ops, 153 .type = REGULATOR_CURRENT, 154 .owner = THIS_MODULE, 155 .enable_reg = MAX77693_CHG_REG_CHG_CNFG_00, 156 .enable_mask = CHG_CNFG_00_CHG_MASK | 157 CHG_CNFG_00_BUCK_MASK, 158 }, 159}; 160 161static int max77693_pmic_probe(struct platform_device *pdev) 162{ 163 struct max77693_dev *iodev = dev_get_drvdata(pdev->dev.parent); 164 int i; 165 struct regulator_config config = { }; 166 167 config.dev = iodev->dev; 168 config.regmap = iodev->regmap; 169 170 for (i = 0; i < ARRAY_SIZE(regulators); i++) { 171 struct regulator_dev *rdev; 172 173 rdev = devm_regulator_register(&pdev->dev, 174 ®ulators[i], &config); 175 if (IS_ERR(rdev)) { 176 dev_err(&pdev->dev, 177 "Failed to initialize regulator-%d\n", i); 178 return PTR_ERR(rdev); 179 } 180 } 181 182 return 0; 183} 184 185static const struct platform_device_id max77693_pmic_id[] = { 186 {"max77693-pmic", 0}, 187 {}, 188}; 189 190MODULE_DEVICE_TABLE(platform, max77693_pmic_id); 191 192static struct platform_driver max77693_pmic_driver = { 193 .driver = { 194 .name = "max77693-pmic", 195 }, 196 .probe = max77693_pmic_probe, 197 .id_table = max77693_pmic_id, 198}; 199 200module_platform_driver(max77693_pmic_driver); 201 202MODULE_DESCRIPTION("MAXIM MAX77693 regulator driver"); 203MODULE_AUTHOR("Jonghwa Lee <jonghwa3.lee@samsung.com>"); 204MODULE_LICENSE("GPL"); 205