Matt Wagantall | fc72721 | 2012-01-06 18:18:25 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012, Code Aurora Forum. 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/kernel.h> |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
Matt Wagantall | fc72721 | 2012-01-06 18:18:25 -0800 | [diff] [blame] | 16 | #include <linux/io.h> |
| 17 | #include <linux/iopoll.h> |
| 18 | #include <linux/delay.h> |
| 19 | #include <linux/err.h> |
| 20 | #include <linux/of.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 | |
| 26 | #define PWR_ON_MASK BIT(31) |
| 27 | #define EN_REST_WAIT_MASK (0xF << 20) |
| 28 | #define EN_FEW_WAIT_MASK (0xF << 16) |
| 29 | #define CLK_DIS_WAIT_MASK (0xF << 12) |
| 30 | #define SW_OVERRIDE_MASK BIT(2) |
| 31 | #define HW_CONTROL_MASK BIT(1) |
| 32 | #define SW_COLLAPSE_MASK BIT(0) |
| 33 | |
| 34 | /* Wait 2^n CXO cycles between all states. Here, n=2 (4 cycles). */ |
| 35 | #define EN_REST_WAIT_VAL (0x2 << 20) |
| 36 | #define EN_FEW_WAIT_VAL (0x2 << 16) |
| 37 | #define CLK_DIS_WAIT_VAL (0x2 << 12) |
| 38 | |
| 39 | #define TIMEOUT_US 10 |
| 40 | |
| 41 | struct gdsc { |
| 42 | struct regulator_dev *rdev; |
| 43 | struct regulator_desc rdesc; |
| 44 | void __iomem *gdscr; |
| 45 | }; |
| 46 | |
| 47 | static int gdsc_is_enabled(struct regulator_dev *rdev) |
| 48 | { |
| 49 | struct gdsc *sc = rdev_get_drvdata(rdev); |
| 50 | |
| 51 | return !!(readl_relaxed(sc->gdscr) & PWR_ON_MASK); |
| 52 | } |
| 53 | |
| 54 | static int gdsc_enable(struct regulator_dev *rdev) |
| 55 | { |
| 56 | struct gdsc *sc = rdev_get_drvdata(rdev); |
| 57 | uint32_t regval; |
| 58 | int ret; |
| 59 | |
| 60 | regval = readl_relaxed(sc->gdscr); |
| 61 | regval &= ~SW_COLLAPSE_MASK; |
| 62 | writel_relaxed(regval, sc->gdscr); |
| 63 | |
| 64 | ret = readl_tight_poll_timeout(sc->gdscr, regval, regval & PWR_ON_MASK, |
| 65 | TIMEOUT_US); |
Matt Wagantall | 64df133 | 2012-06-26 12:00:19 -0700 | [diff] [blame] | 66 | if (ret) { |
Matt Wagantall | fc72721 | 2012-01-06 18:18:25 -0800 | [diff] [blame] | 67 | dev_err(&rdev->dev, "%s enable timed out\n", sc->rdesc.name); |
Matt Wagantall | 64df133 | 2012-06-26 12:00:19 -0700 | [diff] [blame] | 68 | return ret; |
| 69 | } |
Matt Wagantall | fc72721 | 2012-01-06 18:18:25 -0800 | [diff] [blame] | 70 | |
Matt Wagantall | 64df133 | 2012-06-26 12:00:19 -0700 | [diff] [blame] | 71 | /* |
| 72 | * If clocks to this power domain were already on, they will take an |
| 73 | * additional 4 clock cycles to re-enable after the rail is enabled. |
| 74 | */ |
| 75 | udelay(1); |
| 76 | |
| 77 | return 0; |
Matt Wagantall | fc72721 | 2012-01-06 18:18:25 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static int gdsc_disable(struct regulator_dev *rdev) |
| 81 | { |
| 82 | struct gdsc *sc = rdev_get_drvdata(rdev); |
| 83 | uint32_t regval; |
| 84 | int ret; |
| 85 | |
| 86 | regval = readl_relaxed(sc->gdscr); |
| 87 | regval |= SW_COLLAPSE_MASK; |
| 88 | writel_relaxed(regval, sc->gdscr); |
| 89 | |
| 90 | ret = readl_tight_poll_timeout(sc->gdscr, regval, |
| 91 | !(regval & PWR_ON_MASK), TIMEOUT_US); |
| 92 | if (ret) |
| 93 | dev_err(&rdev->dev, "%s disable timed out\n", sc->rdesc.name); |
| 94 | |
| 95 | return ret; |
| 96 | } |
| 97 | |
| 98 | static struct regulator_ops gdsc_ops = { |
| 99 | .is_enabled = gdsc_is_enabled, |
| 100 | .enable = gdsc_enable, |
| 101 | .disable = gdsc_disable, |
| 102 | }; |
| 103 | |
| 104 | static int __devinit gdsc_probe(struct platform_device *pdev) |
| 105 | { |
| 106 | static atomic_t gdsc_count = ATOMIC_INIT(-1); |
| 107 | struct regulator_init_data *init_data; |
| 108 | struct resource *res; |
| 109 | struct gdsc *sc; |
| 110 | uint32_t regval; |
| 111 | int ret; |
| 112 | |
| 113 | sc = devm_kzalloc(&pdev->dev, sizeof(struct gdsc), GFP_KERNEL); |
| 114 | if (sc == NULL) |
| 115 | return -ENOMEM; |
| 116 | |
Steve Muckle | f132c6c | 2012-06-06 18:30:57 -0700 | [diff] [blame] | 117 | init_data = of_get_regulator_init_data(&pdev->dev, pdev->dev.of_node); |
Matt Wagantall | fc72721 | 2012-01-06 18:18:25 -0800 | [diff] [blame] | 118 | if (init_data == NULL) |
| 119 | return -ENOMEM; |
| 120 | |
| 121 | if (of_get_property(pdev->dev.of_node, "parent-supply", NULL)) |
| 122 | init_data->supply_regulator = "parent"; |
| 123 | |
| 124 | ret = of_property_read_string(pdev->dev.of_node, "regulator-name", |
| 125 | &sc->rdesc.name); |
| 126 | if (ret) |
| 127 | return ret; |
| 128 | |
| 129 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 130 | if (res == NULL) |
| 131 | return -EINVAL; |
| 132 | sc->gdscr = devm_ioremap(&pdev->dev, res->start, resource_size(res)); |
| 133 | if (sc->gdscr == NULL) |
| 134 | return -ENOMEM; |
| 135 | |
| 136 | sc->rdesc.id = atomic_inc_return(&gdsc_count); |
| 137 | sc->rdesc.ops = &gdsc_ops; |
| 138 | sc->rdesc.type = REGULATOR_VOLTAGE; |
| 139 | sc->rdesc.owner = THIS_MODULE; |
| 140 | platform_set_drvdata(pdev, sc); |
| 141 | |
| 142 | /* |
| 143 | * Disable HW trigger: collapse/restore occur based on registers writes. |
| 144 | * Disable SW override: Use hardware state-machine for sequencing. |
| 145 | */ |
| 146 | regval = readl_relaxed(sc->gdscr); |
| 147 | regval &= ~(HW_CONTROL_MASK | SW_OVERRIDE_MASK); |
| 148 | |
| 149 | /* Configure wait time between states. */ |
| 150 | regval &= ~(EN_REST_WAIT_MASK | EN_FEW_WAIT_MASK | CLK_DIS_WAIT_MASK); |
| 151 | regval |= EN_REST_WAIT_VAL | EN_FEW_WAIT_VAL | CLK_DIS_WAIT_VAL; |
| 152 | writel_relaxed(regval, sc->gdscr); |
| 153 | |
| 154 | sc->rdev = regulator_register(&sc->rdesc, &pdev->dev, init_data, sc, |
| 155 | pdev->dev.of_node); |
| 156 | if (IS_ERR(sc->rdev)) { |
| 157 | dev_err(&pdev->dev, "regulator_register(\"%s\") failed.\n", |
| 158 | sc->rdesc.name); |
| 159 | return PTR_ERR(sc->rdev); |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int __devexit gdsc_remove(struct platform_device *pdev) |
| 166 | { |
| 167 | struct gdsc *sc = platform_get_drvdata(pdev); |
| 168 | regulator_unregister(sc->rdev); |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | static struct of_device_id gdsc_match_table[] = { |
| 173 | { .compatible = "qcom,gdsc" }, |
| 174 | {} |
| 175 | }; |
| 176 | |
| 177 | static struct platform_driver gdsc_driver = { |
| 178 | .probe = gdsc_probe, |
| 179 | .remove = __devexit_p(gdsc_remove), |
| 180 | .driver = { |
| 181 | .name = "gdsc", |
| 182 | .of_match_table = gdsc_match_table, |
| 183 | .owner = THIS_MODULE, |
| 184 | }, |
| 185 | }; |
| 186 | |
| 187 | static int __init gdsc_init(void) |
| 188 | { |
| 189 | return platform_driver_register(&gdsc_driver); |
| 190 | } |
| 191 | subsys_initcall(gdsc_init); |
| 192 | |
| 193 | static void __exit gdsc_exit(void) |
| 194 | { |
| 195 | platform_driver_unregister(&gdsc_driver); |
| 196 | } |
| 197 | module_exit(gdsc_exit); |
| 198 | |
| 199 | MODULE_LICENSE("GPL v2"); |
Abhimanyu Kapur | 90ced6e | 2012-06-26 17:41:25 -0700 | [diff] [blame] | 200 | MODULE_DESCRIPTION("MSM8974 GDSC power rail regulator driver"); |