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; |
Kishon Vijay Abraham I | c329061 | 2015-07-27 16:54:10 +0530 | [diff] [blame] | 33 | u32 disable_val; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 34 | u32 vmode; |
| 35 | unsigned int enable_time; |
| 36 | char *name; |
| 37 | }; |
| 38 | |
| 39 | struct pbias_regulator_data { |
| 40 | struct regulator_desc desc; |
| 41 | void __iomem *pbias_addr; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 42 | struct regulator_dev *dev; |
| 43 | struct regmap *syscon; |
| 44 | const struct pbias_reg_info *info; |
| 45 | int voltage; |
| 46 | }; |
| 47 | |
Kishon Vijay Abraham I | b9c9364 | 2015-09-03 12:20:37 +0530 | [diff] [blame] | 48 | struct pbias_of_data { |
| 49 | unsigned int offset; |
| 50 | }; |
| 51 | |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 52 | static const unsigned int pbias_volt_table[] = { |
| 53 | 1800000, |
| 54 | 3000000 |
| 55 | }; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 56 | |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 57 | static struct regulator_ops pbias_regulator_voltage_ops = { |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 58 | .list_voltage = regulator_list_voltage_table, |
| 59 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 60 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Axel Lin | 75dbf0a | 2014-04-15 12:02:08 +0800 | [diff] [blame] | 61 | .enable = regulator_enable_regmap, |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 62 | .disable = regulator_disable_regmap, |
Axel Lin | 75dbf0a | 2014-04-15 12:02:08 +0800 | [diff] [blame] | 63 | .is_enabled = regulator_is_enabled_regmap, |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | static const struct pbias_reg_info pbias_mmc_omap2430 = { |
| 67 | .enable = BIT(1), |
| 68 | .enable_mask = BIT(1), |
| 69 | .vmode = BIT(0), |
Kishon Vijay Abraham I | c329061 | 2015-07-27 16:54:10 +0530 | [diff] [blame] | 70 | .disable_val = 0, |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 71 | .enable_time = 100, |
| 72 | .name = "pbias_mmc_omap2430" |
| 73 | }; |
| 74 | |
| 75 | static const struct pbias_reg_info pbias_sim_omap3 = { |
| 76 | .enable = BIT(9), |
| 77 | .enable_mask = BIT(9), |
| 78 | .vmode = BIT(8), |
| 79 | .enable_time = 100, |
| 80 | .name = "pbias_sim_omap3" |
| 81 | }; |
| 82 | |
| 83 | static const struct pbias_reg_info pbias_mmc_omap4 = { |
| 84 | .enable = BIT(26) | BIT(22), |
| 85 | .enable_mask = BIT(26) | BIT(25) | BIT(22), |
Kishon Vijay Abraham I | c329061 | 2015-07-27 16:54:10 +0530 | [diff] [blame] | 86 | .disable_val = BIT(25), |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 87 | .vmode = BIT(21), |
| 88 | .enable_time = 100, |
| 89 | .name = "pbias_mmc_omap4" |
| 90 | }; |
| 91 | |
| 92 | static const struct pbias_reg_info pbias_mmc_omap5 = { |
| 93 | .enable = BIT(27) | BIT(26), |
| 94 | .enable_mask = BIT(27) | BIT(25) | BIT(26), |
Kishon Vijay Abraham I | c329061 | 2015-07-27 16:54:10 +0530 | [diff] [blame] | 95 | .disable_val = BIT(25), |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 96 | .vmode = BIT(21), |
| 97 | .enable_time = 100, |
| 98 | .name = "pbias_mmc_omap5" |
| 99 | }; |
| 100 | |
| 101 | static struct of_regulator_match pbias_matches[] = { |
| 102 | { .name = "pbias_mmc_omap2430", .driver_data = (void *)&pbias_mmc_omap2430}, |
| 103 | { .name = "pbias_sim_omap3", .driver_data = (void *)&pbias_sim_omap3}, |
| 104 | { .name = "pbias_mmc_omap4", .driver_data = (void *)&pbias_mmc_omap4}, |
| 105 | { .name = "pbias_mmc_omap5", .driver_data = (void *)&pbias_mmc_omap5}, |
| 106 | }; |
| 107 | #define PBIAS_NUM_REGS ARRAY_SIZE(pbias_matches) |
| 108 | |
Kishon Vijay Abraham I | b9c9364 | 2015-09-03 12:20:37 +0530 | [diff] [blame] | 109 | /* Offset from SCM general area (and syscon) base */ |
| 110 | |
| 111 | static const struct pbias_of_data pbias_of_data_omap2 = { |
| 112 | .offset = 0x230, |
| 113 | }; |
| 114 | |
| 115 | static const struct pbias_of_data pbias_of_data_omap3 = { |
| 116 | .offset = 0x2b0, |
| 117 | }; |
| 118 | |
| 119 | static const struct pbias_of_data pbias_of_data_omap4 = { |
| 120 | .offset = 0x60, |
| 121 | }; |
| 122 | |
| 123 | static const struct pbias_of_data pbias_of_data_omap5 = { |
| 124 | .offset = 0x60, |
| 125 | }; |
| 126 | |
| 127 | static const struct pbias_of_data pbias_of_data_dra7 = { |
| 128 | .offset = 0xe00, |
| 129 | }; |
| 130 | |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 131 | static const struct of_device_id pbias_of_match[] = { |
| 132 | { .compatible = "ti,pbias-omap", }, |
Kishon Vijay Abraham I | b9c9364 | 2015-09-03 12:20:37 +0530 | [diff] [blame] | 133 | { .compatible = "ti,pbias-omap2", .data = &pbias_of_data_omap2, }, |
| 134 | { .compatible = "ti,pbias-omap3", .data = &pbias_of_data_omap3, }, |
| 135 | { .compatible = "ti,pbias-omap4", .data = &pbias_of_data_omap4, }, |
| 136 | { .compatible = "ti,pbias-omap5", .data = &pbias_of_data_omap5, }, |
| 137 | { .compatible = "ti,pbias-dra7", .data = &pbias_of_data_dra7, }, |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 138 | {}, |
| 139 | }; |
| 140 | MODULE_DEVICE_TABLE(of, pbias_of_match); |
| 141 | |
| 142 | static int pbias_regulator_probe(struct platform_device *pdev) |
| 143 | { |
| 144 | struct device_node *np = pdev->dev.of_node; |
| 145 | struct pbias_regulator_data *drvdata; |
| 146 | struct resource *res; |
| 147 | struct regulator_config cfg = { }; |
| 148 | struct regmap *syscon; |
| 149 | const struct pbias_reg_info *info; |
| 150 | int ret = 0; |
| 151 | int count, idx, data_idx = 0; |
Kishon Vijay Abraham I | b9c9364 | 2015-09-03 12:20:37 +0530 | [diff] [blame] | 152 | const struct of_device_id *match; |
| 153 | const struct pbias_of_data *data; |
| 154 | unsigned int offset; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 155 | |
| 156 | count = of_regulator_match(&pdev->dev, np, pbias_matches, |
| 157 | PBIAS_NUM_REGS); |
| 158 | if (count < 0) |
| 159 | return count; |
| 160 | |
| 161 | drvdata = devm_kzalloc(&pdev->dev, sizeof(struct pbias_regulator_data) |
| 162 | * count, GFP_KERNEL); |
Jingoo Han | 0ee42bb | 2014-06-02 15:30:44 +0900 | [diff] [blame] | 163 | if (!drvdata) |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 164 | return -ENOMEM; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 165 | |
| 166 | syscon = syscon_regmap_lookup_by_phandle(np, "syscon"); |
| 167 | if (IS_ERR(syscon)) |
| 168 | return PTR_ERR(syscon); |
| 169 | |
Kishon Vijay Abraham I | b9c9364 | 2015-09-03 12:20:37 +0530 | [diff] [blame] | 170 | match = of_match_device(of_match_ptr(pbias_of_match), &pdev->dev); |
| 171 | if (match && match->data) { |
| 172 | data = match->data; |
| 173 | offset = data->offset; |
| 174 | } else { |
| 175 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 176 | if (!res) |
| 177 | return -EINVAL; |
| 178 | |
| 179 | offset = res->start; |
| 180 | dev_WARN(&pdev->dev, |
| 181 | "using legacy dt data for pbias offset\n"); |
| 182 | } |
| 183 | |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 184 | cfg.regmap = syscon; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 185 | cfg.dev = &pdev->dev; |
| 186 | |
| 187 | for (idx = 0; idx < PBIAS_NUM_REGS && data_idx < count; idx++) { |
| 188 | if (!pbias_matches[idx].init_data || |
| 189 | !pbias_matches[idx].of_node) |
| 190 | continue; |
| 191 | |
| 192 | info = pbias_matches[idx].driver_data; |
| 193 | if (!info) |
| 194 | return -ENODEV; |
| 195 | |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 196 | drvdata[data_idx].syscon = syscon; |
| 197 | drvdata[data_idx].info = info; |
| 198 | drvdata[data_idx].desc.name = info->name; |
| 199 | drvdata[data_idx].desc.owner = THIS_MODULE; |
| 200 | drvdata[data_idx].desc.type = REGULATOR_VOLTAGE; |
| 201 | drvdata[data_idx].desc.ops = &pbias_regulator_voltage_ops; |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 202 | drvdata[data_idx].desc.volt_table = pbias_volt_table; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 203 | drvdata[data_idx].desc.n_voltages = 2; |
| 204 | drvdata[data_idx].desc.enable_time = info->enable_time; |
Kishon Vijay Abraham I | b9c9364 | 2015-09-03 12:20:37 +0530 | [diff] [blame] | 205 | drvdata[data_idx].desc.vsel_reg = offset; |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 206 | drvdata[data_idx].desc.vsel_mask = info->vmode; |
Kishon Vijay Abraham I | b9c9364 | 2015-09-03 12:20:37 +0530 | [diff] [blame] | 207 | drvdata[data_idx].desc.enable_reg = offset; |
Axel Lin | 60e8c1e | 2014-03-08 11:56:47 +0800 | [diff] [blame] | 208 | drvdata[data_idx].desc.enable_mask = info->enable_mask; |
Axel Lin | 75dbf0a | 2014-04-15 12:02:08 +0800 | [diff] [blame] | 209 | drvdata[data_idx].desc.enable_val = info->enable; |
Kishon Vijay Abraham I | c329061 | 2015-07-27 16:54:10 +0530 | [diff] [blame] | 210 | drvdata[data_idx].desc.disable_val = info->disable_val; |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 211 | |
| 212 | cfg.init_data = pbias_matches[idx].init_data; |
| 213 | cfg.driver_data = &drvdata[data_idx]; |
| 214 | cfg.of_node = pbias_matches[idx].of_node; |
| 215 | |
| 216 | drvdata[data_idx].dev = devm_regulator_register(&pdev->dev, |
| 217 | &drvdata[data_idx].desc, &cfg); |
| 218 | if (IS_ERR(drvdata[data_idx].dev)) { |
| 219 | ret = PTR_ERR(drvdata[data_idx].dev); |
| 220 | dev_err(&pdev->dev, |
| 221 | "Failed to register regulator: %d\n", ret); |
| 222 | goto err_regulator; |
| 223 | } |
| 224 | data_idx++; |
| 225 | } |
| 226 | |
| 227 | platform_set_drvdata(pdev, drvdata); |
| 228 | |
| 229 | err_regulator: |
| 230 | return ret; |
| 231 | } |
| 232 | |
| 233 | static struct platform_driver pbias_regulator_driver = { |
| 234 | .probe = pbias_regulator_probe, |
| 235 | .driver = { |
| 236 | .name = "pbias-regulator", |
Balaji T K | 11469e0 | 2014-02-19 20:26:40 +0530 | [diff] [blame] | 237 | .of_match_table = of_match_ptr(pbias_of_match), |
| 238 | }, |
| 239 | }; |
| 240 | |
| 241 | module_platform_driver(pbias_regulator_driver); |
| 242 | |
| 243 | MODULE_AUTHOR("Balaji T K <balajitk@ti.com>"); |
| 244 | MODULE_DESCRIPTION("pbias voltage regulator"); |
| 245 | MODULE_LICENSE("GPL"); |
| 246 | MODULE_ALIAS("platform:pbias-regulator"); |