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