1 /* 2 * Copyright (c) 2015 MediaTek Inc. 3 * Author: Henry Chen <henryc.chen@mediatek.com> 4 * 5 * This program is free software; you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License version 2 as 7 * published by the Free Software Foundation. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 */ 14 15 #include <linux/err.h> 16 #include <linux/gpio.h> 17 #include <linux/i2c.h> 18 #include <linux/init.h> 19 #include <linux/interrupt.h> 20 #include <linux/module.h> 21 #include <linux/regmap.h> 22 #include <linux/regulator/driver.h> 23 #include <linux/regulator/machine.h> 24 #include <linux/regulator/of_regulator.h> 25 #include <linux/regulator/mt6311.h> 26 #include <linux/slab.h> 27 #include "mt6311-regulator.h" 28 29 static const struct regmap_config mt6311_regmap_config = { 30 .reg_bits = 8, 31 .val_bits = 8, 32 .max_register = MT6311_FQMTR_CON4, 33 .cache_type = REGCACHE_RBTREE, 34 }; 35 36 /* Default limits measured in millivolts and milliamps */ 37 #define MT6311_MIN_UV 600000 38 #define MT6311_MAX_UV 1393750 39 #define MT6311_STEP_UV 6250 40 41 static const struct regulator_ops mt6311_buck_ops = { 42 .list_voltage = regulator_list_voltage_linear, 43 .map_voltage = regulator_map_voltage_linear, 44 .set_voltage_sel = regulator_set_voltage_sel_regmap, 45 .get_voltage_sel = regulator_get_voltage_sel_regmap, 46 .set_voltage_time_sel = regulator_set_voltage_time_sel, 47 .enable = regulator_enable_regmap, 48 .disable = regulator_disable_regmap, 49 .is_enabled = regulator_is_enabled_regmap, 50 }; 51 52 static const struct regulator_ops mt6311_ldo_ops = { 53 .enable = regulator_enable_regmap, 54 .disable = regulator_disable_regmap, 55 .is_enabled = regulator_is_enabled_regmap, 56 }; 57 58 #define MT6311_BUCK(_id) \ 59 {\ 60 .name = #_id,\ 61 .ops = &mt6311_buck_ops,\ 62 .of_match = of_match_ptr(#_id),\ 63 .regulators_node = of_match_ptr("regulators"),\ 64 .type = REGULATOR_VOLTAGE,\ 65 .id = MT6311_ID_##_id,\ 66 .n_voltages = (MT6311_MAX_UV - MT6311_MIN_UV) / MT6311_STEP_UV + 1,\ 67 .min_uV = MT6311_MIN_UV,\ 68 .uV_step = MT6311_STEP_UV,\ 69 .owner = THIS_MODULE,\ 70 .enable_reg = MT6311_VDVFS11_CON9,\ 71 .enable_mask = MT6311_PMIC_VDVFS11_EN_MASK,\ 72 .vsel_reg = MT6311_VDVFS11_CON12,\ 73 .vsel_mask = MT6311_PMIC_VDVFS11_VOSEL_MASK,\ 74 } 75 76 #define MT6311_LDO(_id) \ 77 {\ 78 .name = #_id,\ 79 .ops = &mt6311_ldo_ops,\ 80 .of_match = of_match_ptr(#_id),\ 81 .regulators_node = of_match_ptr("regulators"),\ 82 .type = REGULATOR_VOLTAGE,\ 83 .id = MT6311_ID_##_id,\ 84 .owner = THIS_MODULE,\ 85 .enable_reg = MT6311_LDO_CON3,\ 86 .enable_mask = MT6311_PMIC_RG_VBIASN_EN_MASK,\ 87 } 88 89 static const struct regulator_desc mt6311_regulators[] = { 90 MT6311_BUCK(VDVFS), 91 MT6311_LDO(VBIASN), 92 }; 93 94 /* 95 * I2C driver interface functions 96 */ 97 static int mt6311_i2c_probe(struct i2c_client *i2c, 98 const struct i2c_device_id *id) 99 { 100 struct regulator_config config = { }; 101 struct regulator_dev *rdev; 102 struct regmap *regmap; 103 int i, ret; 104 unsigned int data; 105 106 regmap = devm_regmap_init_i2c(i2c, &mt6311_regmap_config); 107 if (IS_ERR(regmap)) { 108 ret = PTR_ERR(regmap); 109 dev_err(&i2c->dev, "Failed to allocate register map: %d\n", 110 ret); 111 return ret; 112 } 113 114 ret = regmap_read(regmap, MT6311_SWCID, &data); 115 if (ret < 0) { 116 dev_err(&i2c->dev, "Failed to read DEVICE_ID reg: %d\n", ret); 117 return ret; 118 } 119 120 switch (data) { 121 case MT6311_E1_CID_CODE: 122 case MT6311_E2_CID_CODE: 123 case MT6311_E3_CID_CODE: 124 break; 125 default: 126 dev_err(&i2c->dev, "Unsupported device id = 0x%x.\n", data); 127 return -ENODEV; 128 } 129 130 for (i = 0; i < MT6311_MAX_REGULATORS; i++) { 131 config.dev = &i2c->dev; 132 config.regmap = regmap; 133 134 rdev = devm_regulator_register(&i2c->dev, 135 &mt6311_regulators[i], &config); 136 if (IS_ERR(rdev)) { 137 dev_err(&i2c->dev, 138 "Failed to register MT6311 regulator\n"); 139 return PTR_ERR(rdev); 140 } 141 } 142 143 return 0; 144 } 145 146 static const struct i2c_device_id mt6311_i2c_id[] = { 147 {"mt6311", 0}, 148 {}, 149 }; 150 MODULE_DEVICE_TABLE(i2c, mt6311_i2c_id); 151 152 #ifdef CONFIG_OF 153 static const struct of_device_id mt6311_dt_ids[] = { 154 { .compatible = "mediatek,mt6311-regulator", 155 .data = &mt6311_i2c_id[0] }, 156 {}, 157 }; 158 MODULE_DEVICE_TABLE(of, mt6311_dt_ids); 159 #endif 160 161 static struct i2c_driver mt6311_regulator_driver = { 162 .driver = { 163 .name = "mt6311", 164 .of_match_table = of_match_ptr(mt6311_dt_ids), 165 }, 166 .probe = mt6311_i2c_probe, 167 .id_table = mt6311_i2c_id, 168 }; 169 170 module_i2c_driver(mt6311_regulator_driver); 171 172 MODULE_AUTHOR("Henry Chen <henryc.chen@mediatek.com>"); 173 MODULE_DESCRIPTION("Regulator device driver for Mediatek MT6311"); 174 MODULE_LICENSE("GPL v2"); 175