David Collins | 50059cc | 2017-06-14 12:00:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, The Linux Foundation. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 and |
| 6 | * only version 2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | */ |
| 13 | |
| 14 | #include <linux/bitops.h> |
| 15 | #include <linux/err.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_device.h> |
| 21 | #include <linux/platform_device.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include <linux/types.h> |
| 24 | #include <linux/regulator/driver.h> |
| 25 | #include <linux/regulator/machine.h> |
| 26 | #include <linux/regulator/of_regulator.h> |
| 27 | |
| 28 | #define REFGEN_REG_BIAS_EN 0x08 |
| 29 | #define REFGEN_BIAS_EN_MASK GENMASK(2, 0) |
| 30 | #define REFGEN_BIAS_EN_ENABLE 0x7 |
| 31 | #define REFGEN_BIAS_EN_DISABLE 0x6 |
| 32 | |
| 33 | #define REFGEN_REG_BG_CTRL 0x14 |
David Collins | c56a444 | 2017-11-16 10:29:59 -0800 | [diff] [blame] | 34 | #define REFGEN_BG_CTRL_MASK GENMASK(2, 1) |
David Collins | 50059cc | 2017-06-14 12:00:47 -0700 | [diff] [blame] | 35 | #define REFGEN_BG_CTRL_ENABLE 0x6 |
| 36 | #define REFGEN_BG_CTRL_DISABLE 0x4 |
| 37 | |
| 38 | struct refgen { |
| 39 | struct regulator_desc rdesc; |
| 40 | struct regulator_dev *rdev; |
| 41 | void __iomem *addr; |
| 42 | }; |
| 43 | |
David Collins | c56a444 | 2017-11-16 10:29:59 -0800 | [diff] [blame] | 44 | static void masked_writel(u32 val, u32 mask, void __iomem *addr) |
| 45 | { |
| 46 | u32 reg; |
| 47 | |
| 48 | reg = readl_relaxed(addr); |
| 49 | reg = (reg & ~mask) | (val & mask); |
| 50 | writel_relaxed(reg, addr); |
| 51 | } |
| 52 | |
David Collins | 50059cc | 2017-06-14 12:00:47 -0700 | [diff] [blame] | 53 | static int refgen_enable(struct regulator_dev *rdev) |
| 54 | { |
| 55 | struct refgen *vreg = rdev_get_drvdata(rdev); |
| 56 | |
David Collins | c56a444 | 2017-11-16 10:29:59 -0800 | [diff] [blame] | 57 | masked_writel(REFGEN_BG_CTRL_ENABLE, REFGEN_BG_CTRL_MASK, |
| 58 | vreg->addr + REFGEN_REG_BG_CTRL); |
David Collins | 50059cc | 2017-06-14 12:00:47 -0700 | [diff] [blame] | 59 | writel_relaxed(REFGEN_BIAS_EN_ENABLE, vreg->addr + REFGEN_REG_BIAS_EN); |
| 60 | |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | static int refgen_disable(struct regulator_dev *rdev) |
| 65 | { |
| 66 | struct refgen *vreg = rdev_get_drvdata(rdev); |
| 67 | |
| 68 | writel_relaxed(REFGEN_BIAS_EN_DISABLE, vreg->addr + REFGEN_REG_BIAS_EN); |
David Collins | c56a444 | 2017-11-16 10:29:59 -0800 | [diff] [blame] | 69 | masked_writel(REFGEN_BG_CTRL_DISABLE, REFGEN_BG_CTRL_MASK, |
| 70 | vreg->addr + REFGEN_REG_BG_CTRL); |
David Collins | 50059cc | 2017-06-14 12:00:47 -0700 | [diff] [blame] | 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int refgen_is_enabled(struct regulator_dev *rdev) |
| 76 | { |
| 77 | struct refgen *vreg = rdev_get_drvdata(rdev); |
| 78 | u32 val; |
| 79 | |
| 80 | val = readl_relaxed(vreg->addr + REFGEN_REG_BG_CTRL); |
| 81 | if ((val & REFGEN_BG_CTRL_MASK) != REFGEN_BG_CTRL_ENABLE) |
| 82 | return 0; |
| 83 | |
| 84 | val = readl_relaxed(vreg->addr + REFGEN_REG_BIAS_EN); |
| 85 | if ((val & REFGEN_BIAS_EN_MASK) != REFGEN_BIAS_EN_ENABLE) |
| 86 | return 0; |
| 87 | |
| 88 | return 1; |
| 89 | } |
| 90 | |
| 91 | static struct regulator_ops refgen_ops = { |
| 92 | .enable = refgen_enable, |
| 93 | .disable = refgen_disable, |
| 94 | .is_enabled = refgen_is_enabled, |
| 95 | }; |
| 96 | |
| 97 | static int refgen_probe(struct platform_device *pdev) |
| 98 | { |
| 99 | struct regulator_config config = {}; |
| 100 | struct regulator_init_data *init_data = NULL; |
| 101 | struct device *dev = &pdev->dev; |
| 102 | struct regulator_desc *rdesc; |
| 103 | struct resource *res; |
| 104 | struct refgen *vreg; |
| 105 | int rc; |
| 106 | |
| 107 | vreg = devm_kzalloc(dev, sizeof(*vreg), GFP_KERNEL); |
| 108 | if (!vreg) |
| 109 | return -ENOMEM; |
| 110 | |
| 111 | if (!dev->of_node) { |
| 112 | dev_err(dev, "%s: device tree node missing\n", __func__); |
| 113 | return -ENODEV; |
| 114 | } |
| 115 | |
| 116 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 117 | if (!res || !res->start) { |
| 118 | dev_err(dev, "reg address is missing\n"); |
| 119 | return -EINVAL; |
| 120 | } |
| 121 | |
| 122 | vreg->addr = devm_ioremap_resource(&pdev->dev, res); |
| 123 | if (IS_ERR(vreg->addr)) { |
| 124 | rc = PTR_ERR(vreg->addr); |
| 125 | dev_err(dev, "ioremap failed, rc=%d\n", rc); |
| 126 | return rc; |
| 127 | } |
| 128 | |
| 129 | init_data = of_get_regulator_init_data(dev, dev->of_node, &vreg->rdesc); |
| 130 | if (!init_data) |
| 131 | return -ENOMEM; |
| 132 | |
| 133 | if (init_data->constraints.name == NULL) { |
| 134 | dev_err(dev, "%s: regulator-name not specified\n", __func__); |
| 135 | return -EINVAL; |
| 136 | } |
| 137 | |
| 138 | if (of_get_property(dev->of_node, "parent-supply", NULL)) |
| 139 | init_data->supply_regulator = "parent"; |
| 140 | |
| 141 | rdesc = &vreg->rdesc; |
| 142 | |
| 143 | rdesc->name = "refgen"; |
| 144 | rdesc->ops = &refgen_ops; |
| 145 | rdesc->id = pdev->id; |
| 146 | rdesc->owner = THIS_MODULE; |
| 147 | rdesc->type = REGULATOR_VOLTAGE; |
| 148 | |
| 149 | config.dev = dev; |
| 150 | config.init_data = init_data; |
| 151 | config.driver_data = vreg; |
| 152 | config.of_node = dev->of_node; |
| 153 | |
| 154 | vreg->rdev = devm_regulator_register(dev, rdesc, &config); |
| 155 | if (IS_ERR(vreg->rdev)) { |
| 156 | rc = PTR_ERR(vreg->rdev); |
| 157 | if (rc != -EPROBE_DEFER) |
| 158 | dev_err(dev, "%s: regulator register failed\n", |
| 159 | __func__); |
| 160 | return rc; |
| 161 | } |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
| 166 | static const struct of_device_id refgen_match_table[] = { |
| 167 | { .compatible = "qcom,refgen-regulator", }, |
| 168 | {} |
| 169 | }; |
| 170 | |
| 171 | static struct platform_driver refgen_driver = { |
| 172 | .probe = refgen_probe, |
| 173 | .driver = { |
| 174 | .name = "qcom,refgen-regulator", |
| 175 | .owner = THIS_MODULE, |
| 176 | .of_match_table = refgen_match_table, |
| 177 | }, |
| 178 | }; |
| 179 | |
| 180 | static int __init refgen_init(void) |
| 181 | { |
| 182 | return platform_driver_register(&refgen_driver); |
| 183 | } |
| 184 | arch_initcall(refgen_init); |
| 185 | |
| 186 | static void __exit refgen_exit(void) |
| 187 | { |
| 188 | platform_driver_unregister(&refgen_driver); |
| 189 | } |
| 190 | module_exit(refgen_exit); |
| 191 | |
| 192 | MODULE_LICENSE("GPL v2"); |
| 193 | MODULE_DESCRIPTION("refgen regulator driver"); |