Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 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 | |
| 30 | struct pbias_reg_info { |
| 31 | u32 enable; |
| 32 | u32 enable_mask; |
| 33 | u32 vmode; |
| 34 | unsigned int enable_time; |
| 35 | char *name; |
| 36 | }; |
| 37 | |
| 38 | struct pbias_regulator_data { |
| 39 | struct regulator_desc desc; |
| 40 | void __iomem *pbias_addr; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 41 | struct regulator_dev *dev; |
| 42 | struct regmap *syscon; |
| 43 | const struct pbias_reg_info *info; |
| 44 | int voltage; |
| 45 | }; |
| 46 | |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 47 | static const unsigned int pbias_volt_table[] = { |
| 48 | 1800000, |
| 49 | 3000000 |
| 50 | }; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 51 | |
| 52 | static int pbias_regulator_enable(struct regulator_dev *rdev) |
| 53 | { |
| 54 | struct pbias_regulator_data *data = rdev_get_drvdata(rdev); |
| 55 | const struct pbias_reg_info *info = data->info; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 56 | |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 57 | return regmap_update_bits(data->syscon, rdev->desc->enable_reg, |
| 58 | info->enable_mask, info->enable); |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static int pbias_regulator_is_enable(struct regulator_dev *rdev) |
| 62 | { |
| 63 | struct pbias_regulator_data *data = rdev_get_drvdata(rdev); |
| 64 | const struct pbias_reg_info *info = data->info; |
| 65 | int value; |
| 66 | |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 67 | regmap_read(data->syscon, rdev->desc->enable_reg, &value); |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 68 | |
Axel Lin | 1cb7b43 | 2014-03-08 11:55:29 +0800 | [diff] [blame] | 69 | return (value & info->enable_mask) == info->enable; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | static struct regulator_ops pbias_regulator_voltage_ops = { |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 73 | .list_voltage = regulator_list_voltage_table, |
| 74 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 75 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
| 76 | .enable = pbias_regulator_enable, |
| 77 | .disable = regulator_disable_regmap, |
| 78 | .is_enabled = pbias_regulator_is_enable, |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 79 | }; |
| 80 | |
| 81 | static const struct pbias_reg_info pbias_mmc_omap2430 = { |
| 82 | .enable = BIT(1), |
| 83 | .enable_mask = BIT(1), |
| 84 | .vmode = BIT(0), |
| 85 | .enable_time = 100, |
| 86 | .name = "pbias_mmc_omap2430" |
| 87 | }; |
| 88 | |
| 89 | static const struct pbias_reg_info pbias_sim_omap3 = { |
| 90 | .enable = BIT(9), |
| 91 | .enable_mask = BIT(9), |
| 92 | .vmode = BIT(8), |
| 93 | .enable_time = 100, |
| 94 | .name = "pbias_sim_omap3" |
| 95 | }; |
| 96 | |
| 97 | static const struct pbias_reg_info pbias_mmc_omap4 = { |
| 98 | .enable = BIT(26) | BIT(22), |
| 99 | .enable_mask = BIT(26) | BIT(25) | BIT(22), |
| 100 | .vmode = BIT(21), |
| 101 | .enable_time = 100, |
| 102 | .name = "pbias_mmc_omap4" |
| 103 | }; |
| 104 | |
| 105 | static const struct pbias_reg_info pbias_mmc_omap5 = { |
| 106 | .enable = BIT(27) | BIT(26), |
| 107 | .enable_mask = BIT(27) | BIT(25) | BIT(26), |
| 108 | .vmode = BIT(21), |
| 109 | .enable_time = 100, |
| 110 | .name = "pbias_mmc_omap5" |
| 111 | }; |
| 112 | |
| 113 | static struct of_regulator_match pbias_matches[] = { |
| 114 | { .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430}, |
| 115 | { .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3}, |
| 116 | { .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4}, |
| 117 | { .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5}, |
| 118 | }; |
| 119 | #define PBIAS_NUM_REGS ARRAY_SIZE(pbias_matches) |
| 120 | |
| 121 | static const struct of_device_id pbias_of_match[] = { |
| 122 | { .compatible = "ti,pbias-omap", }, |
| 123 | {}, |
| 124 | }; |
| 125 | MODULE_DEVICE_TABLE(of, pbias_of_match); |
| 126 | |
| 127 | static int pbias_regulator_probe(struct platform_device *pdev) |
| 128 | { |
| 129 | struct device_node *np = pdev->dev.of_node; |
| 130 | struct pbias_regulator_data *drvdata; |
| 131 | struct resource *res; |
| 132 | struct regulator_config cfg = { }; |
| 133 | struct regmap *syscon; |
| 134 | const struct pbias_reg_info *info; |
| 135 | int ret = 0; |
| 136 | int count, idx, data_idx = 0; |
| 137 | |
| 138 | count = of_regulator_match(&pdev->dev, np, pbias_matches, |
| 139 | PBIAS_NUM_REGS); |
| 140 | if (count < 0) |
| 141 | return count; |
| 142 | |
| 143 | drvdata = devm_kzalloc(&pdev->dev, sizeof(struct pbias_regulator_data) |
| 144 | * count, GFP_KERNEL); |
| 145 | if (drvdata == NULL) { |
| 146 | dev_err(&pdev->dev, "Failed to allocate device data\n"); |
| 147 | return -ENOMEM; |
| 148 | } |
| 149 | |
| 150 | syscon = syscon_regmap_lookup_by_phandle(np, "syscon"); |
| 151 | if (IS_ERR(syscon)) |
| 152 | return PTR_ERR(syscon); |
| 153 | |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 154 | cfg.regmap = syscon; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 155 | cfg.dev = &pdev->dev; |
| 156 | |
| 157 | for (idx = 0; idx < PBIAS_NUM_REGS && data_idx < count; idx++) { |
| 158 | if (!pbias_matches[idx].init_data || |
| 159 | !pbias_matches[idx].of_node) |
| 160 | continue; |
| 161 | |
| 162 | info = pbias_matches[idx].driver_data; |
| 163 | if (!info) |
| 164 | return -ENODEV; |
| 165 | |
| 166 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 167 | if (!res) |
| 168 | return -EINVAL; |
| 169 | |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 170 | drvdata[data_idx].syscon = syscon; |
| 171 | drvdata[data_idx].info = info; |
| 172 | drvdata[data_idx].desc.name = info->name; |
| 173 | drvdata[data_idx].desc.owner = THIS_MODULE; |
| 174 | drvdata[data_idx].desc.type = REGULATOR_VOLTAGE; |
| 175 | drvdata[data_idx].desc.ops = &pbias_regulator_voltage_ops; |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 176 | drvdata[data_idx].desc.volt_table = pbias_volt_table; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 177 | drvdata[data_idx].desc.n_voltages = 2; |
| 178 | drvdata[data_idx].desc.enable_time = info->enable_time; |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 179 | drvdata[data_idx].desc.vsel_reg = res->start; |
| 180 | drvdata[data_idx].desc.vsel_mask = info->vmode; |
| 181 | drvdata[data_idx].desc.enable_reg = res->start; |
| 182 | drvdata[data_idx].desc.enable_mask = info->enable_mask; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 183 | |
| 184 | cfg.init_data = pbias_matches[idx].init_data; |
| 185 | cfg.driver_data = &drvdata[data_idx]; |
| 186 | cfg.of_node = pbias_matches[idx].of_node; |
| 187 | |
| 188 | drvdata[data_idx].dev = devm_regulator_register(&pdev->dev, |
| 189 | &drvdata[data_idx].desc, &cfg); |
| 190 | if (IS_ERR(drvdata[data_idx].dev)) { |
| 191 | ret = PTR_ERR(drvdata[data_idx].dev); |
| 192 | dev_err(&pdev->dev, |
| 193 | "Failed to register regulator: %d\n", ret); |
| 194 | goto err_regulator; |
| 195 | } |
| 196 | data_idx++; |
| 197 | } |
| 198 | |
| 199 | platform_set_drvdata(pdev, drvdata); |
| 200 | |
| 201 | err_regulator: |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | static struct platform_driver pbias_regulator_driver = { |
| 206 | .probe = pbias_regulator_probe, |
| 207 | .driver = { |
| 208 | .name = "pbias-regulator", |
| 209 | .owner = THIS_MODULE, |
| 210 | .of_match_table = of_match_ptr(pbias_of_match), |
| 211 | }, |
| 212 | }; |
| 213 | |
| 214 | module_platform_driver(pbias_regulator_driver); |
| 215 | |
| 216 | MODULE_AUTHOR("Balaji T K <balajitk@ti.com>"); |
| 217 | MODULE_DESCRIPTION("pbias voltage regulator"); |
| 218 | MODULE_LICENSE("GPL"); |
| 219 | MODULE_ALIAS("platform:pbias-regulator"); |