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