1/* 2 * pbias-regulator.c 3 * 4 * Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ 5 * Author: Balaji T K <balajitk@ti.com> 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License as 9 * published by the Free Software Foundation version 2. 10 * 11 * This program is distributed "as is" WITHOUT ANY WARRANTY of any 12 * kind, whether express or implied; without even the implied warranty 13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 */ 16 17#include <linux/err.h> 18#include <linux/io.h> 19#include <linux/module.h> 20#include <linux/mfd/syscon.h> 21#include <linux/platform_device.h> 22#include <linux/regulator/driver.h> 23#include <linux/regulator/machine.h> 24#include <linux/regulator/of_regulator.h> 25#include <linux/regmap.h> 26#include <linux/slab.h> 27#include <linux/of.h> 28#include <linux/of_device.h> 29 30struct pbias_reg_info { 31 u32 enable; 32 u32 enable_mask; 33 u32 disable_val; 34 u32 vmode; 35 unsigned int enable_time; 36 char *name; 37}; 38 39struct pbias_regulator_data { 40 struct regulator_desc desc; 41 void __iomem *pbias_addr; 42 struct regulator_dev *dev; 43 struct regmap *syscon; 44 const struct pbias_reg_info *info; 45 int voltage; 46}; 47 48static const unsigned int pbias_volt_table[] = { 49 1800000, 50 3000000 51}; 52 53static struct regulator_ops pbias_regulator_voltage_ops = { 54 .list_voltage = regulator_list_voltage_table, 55 .get_voltage_sel = regulator_get_voltage_sel_regmap, 56 .set_voltage_sel = regulator_set_voltage_sel_regmap, 57 .enable = regulator_enable_regmap, 58 .disable = regulator_disable_regmap, 59 .is_enabled = regulator_is_enabled_regmap, 60}; 61 62static const struct pbias_reg_info pbias_mmc_omap2430 = { 63 .enable = BIT(1), 64 .enable_mask = BIT(1), 65 .vmode = BIT(0), 66 .disable_val = 0, 67 .enable_time = 100, 68 .name = "pbias_mmc_omap2430" 69}; 70 71static const struct pbias_reg_info pbias_sim_omap3 = { 72 .enable = BIT(9), 73 .enable_mask = BIT(9), 74 .vmode = BIT(8), 75 .enable_time = 100, 76 .name = "pbias_sim_omap3" 77}; 78 79static const struct pbias_reg_info pbias_mmc_omap4 = { 80 .enable = BIT(26) | BIT(22), 81 .enable_mask = BIT(26) | BIT(25) | BIT(22), 82 .disable_val = BIT(25), 83 .vmode = BIT(21), 84 .enable_time = 100, 85 .name = "pbias_mmc_omap4" 86}; 87 88static const struct pbias_reg_info pbias_mmc_omap5 = { 89 .enable = BIT(27) | BIT(26), 90 .enable_mask = BIT(27) | BIT(25) | BIT(26), 91 .disable_val = BIT(25), 92 .vmode = BIT(21), 93 .enable_time = 100, 94 .name = "pbias_mmc_omap5" 95}; 96 97static struct of_regulator_match pbias_matches[] = { 98 { .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430}, 99 { .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3}, 100 { .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4}, 101 { .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5}, 102}; 103#define PBIAS_NUM_REGS ARRAY_SIZE(pbias_matches) 104 105static const struct of_device_id pbias_of_match[] = { 106 { .compatible = "ti,pbias-omap", }, 107 {}, 108}; 109MODULE_DEVICE_TABLE(of, pbias_of_match); 110 111static int pbias_regulator_probe(struct platform_device *pdev) 112{ 113 struct device_node *np = pdev->dev.of_node; 114 struct pbias_regulator_data *drvdata; 115 struct resource *res; 116 struct regulator_config cfg = { }; 117 struct regmap *syscon; 118 const struct pbias_reg_info *info; 119 int ret = 0; 120 int count, idx, data_idx = 0; 121 122 count = of_regulator_match(&pdev->dev, np, pbias_matches, 123 PBIAS_NUM_REGS); 124 if (count < 0) 125 return count; 126 127 drvdata = devm_kzalloc(&pdev->dev, sizeof(struct pbias_regulator_data) 128 * count, GFP_KERNEL); 129 if (!drvdata) 130 return -ENOMEM; 131 132 syscon = syscon_regmap_lookup_by_phandle(np, "syscon"); 133 if (IS_ERR(syscon)) 134 return PTR_ERR(syscon); 135 136 cfg.regmap = syscon; 137 cfg.dev = &pdev->dev; 138 139 for (idx = 0; idx < PBIAS_NUM_REGS && data_idx < count; idx++) { 140 if (!pbias_matches[idx].init_data || 141 !pbias_matches[idx].of_node) 142 continue; 143 144 info = pbias_matches[idx].driver_data; 145 if (!info) 146 return -ENODEV; 147 148 res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 149 if (!res) 150 return -EINVAL; 151 152 drvdata[data_idx].syscon = syscon; 153 drvdata[data_idx].info = info; 154 drvdata[data_idx].desc.name = info->name; 155 drvdata[data_idx].desc.owner = THIS_MODULE; 156 drvdata[data_idx].desc.type = REGULATOR_VOLTAGE; 157 drvdata[data_idx].desc.ops = &pbias_regulator_voltage_ops; 158 drvdata[data_idx].desc.volt_table = pbias_volt_table; 159 drvdata[data_idx].desc.n_voltages = 2; 160 drvdata[data_idx].desc.enable_time = info->enable_time; 161 drvdata[data_idx].desc.vsel_reg = res->start; 162 drvdata[data_idx].desc.vsel_mask = info->vmode; 163 drvdata[data_idx].desc.enable_reg = res->start; 164 drvdata[data_idx].desc.enable_mask = info->enable_mask; 165 drvdata[data_idx].desc.enable_val = info->enable; 166 drvdata[data_idx].desc.disable_val = info->disable_val; 167 168 cfg.init_data = pbias_matches[idx].init_data; 169 cfg.driver_data = &drvdata[data_idx]; 170 cfg.of_node = pbias_matches[idx].of_node; 171 172 drvdata[data_idx].dev = devm_regulator_register(&pdev->dev, 173 &drvdata[data_idx].desc, &cfg); 174 if (IS_ERR(drvdata[data_idx].dev)) { 175 ret = PTR_ERR(drvdata[data_idx].dev); 176 dev_err(&pdev->dev, 177 "Failed to register regulator: %d\n", ret); 178 goto err_regulator; 179 } 180 data_idx++; 181 } 182 183 platform_set_drvdata(pdev, drvdata); 184 185err_regulator: 186 return ret; 187} 188 189static struct platform_driver pbias_regulator_driver = { 190 .probe = pbias_regulator_probe, 191 .driver = { 192 .name = "pbias-regulator", 193 .of_match_table = of_match_ptr(pbias_of_match), 194 }, 195}; 196 197module_platform_driver(pbias_regulator_driver); 198 199MODULE_AUTHOR("Balaji T K <balajitk@ti.com>"); 200MODULE_DESCRIPTION("pbias voltage regulator"); 201MODULE_LICENSE("GPL"); 202MODULE_ALIAS("platform:pbias-regulator"); 203