Axel Lin | e8d59d9 | 2019-04-17 22:16:33 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // wm8994-regulator.c -- Regulator driver for the WM8994 |
| 4 | // |
| 5 | // Copyright 2009 Wolfson Microelectronics PLC. |
| 6 | // |
| 7 | // Author: Mark Brown <broonie@opensource.wolfsonmicro.com> |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/moduleparam.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/bitops.h> |
| 13 | #include <linux/err.h> |
| 14 | #include <linux/platform_device.h> |
| 15 | #include <linux/regulator/driver.h> |
Mark Brown | e3d2777 | 2013-04-05 13:11:31 +0100 | [diff] [blame] | 16 | #include <linux/regulator/machine.h> |
Linus Walleij | 1d2f468 | 2018-11-15 09:01:18 +0100 | [diff] [blame] | 17 | #include <linux/gpio/consumer.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 19 | |
| 20 | #include <linux/mfd/wm8994/core.h> |
| 21 | #include <linux/mfd/wm8994/registers.h> |
| 22 | #include <linux/mfd/wm8994/pdata.h> |
| 23 | |
| 24 | struct wm8994_ldo { |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 25 | struct regulator_dev *regulator; |
| 26 | struct wm8994 *wm8994; |
Mark Brown | e3d2777 | 2013-04-05 13:11:31 +0100 | [diff] [blame] | 27 | struct regulator_consumer_supply supply; |
| 28 | struct regulator_init_data init_data; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
| 31 | #define WM8994_LDO1_MAX_SELECTOR 0x7 |
| 32 | #define WM8994_LDO2_MAX_SELECTOR 0x3 |
| 33 | |
Julia Lawall | b0d6dd3 | 2015-12-19 16:31:24 +0100 | [diff] [blame] | 34 | static const struct regulator_ops wm8994_ldo1_ops = { |
Axel Lin | f2d103a | 2012-05-23 22:40:58 +0800 | [diff] [blame] | 35 | .list_voltage = regulator_list_voltage_linear, |
| 36 | .map_voltage = regulator_map_voltage_linear, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 37 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 38 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev, |
| 42 | unsigned int selector) |
| 43 | { |
Mark Brown | 5a7743e | 2010-12-13 15:07:40 +0000 | [diff] [blame] | 44 | struct wm8994_ldo *ldo = rdev_get_drvdata(rdev); |
| 45 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 46 | if (selector > WM8994_LDO2_MAX_SELECTOR) |
| 47 | return -EINVAL; |
| 48 | |
Mark Brown | 5a7743e | 2010-12-13 15:07:40 +0000 | [diff] [blame] | 49 | switch (ldo->wm8994->type) { |
| 50 | case WM8994: |
| 51 | return (selector * 100000) + 900000; |
| 52 | case WM8958: |
| 53 | return (selector * 100000) + 1000000; |
Mark Brown | a1ff89e | 2011-07-24 12:40:48 +0100 | [diff] [blame] | 54 | case WM1811: |
| 55 | switch (selector) { |
| 56 | case 0: |
| 57 | return -EINVAL; |
| 58 | default: |
| 59 | return (selector * 100000) + 950000; |
| 60 | } |
| 61 | break; |
Mark Brown | 5a7743e | 2010-12-13 15:07:40 +0000 | [diff] [blame] | 62 | default: |
| 63 | return -EINVAL; |
| 64 | } |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Julia Lawall | b0d6dd3 | 2015-12-19 16:31:24 +0100 | [diff] [blame] | 67 | static const struct regulator_ops wm8994_ldo2_ops = { |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 68 | .list_voltage = wm8994_ldo2_list_voltage, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 69 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 70 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
Mark Brown | 25e4d60 | 2012-04-03 20:47:27 +0100 | [diff] [blame] | 73 | static const struct regulator_desc wm8994_ldo_desc[] = { |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 74 | { |
| 75 | .name = "LDO1", |
| 76 | .id = 1, |
| 77 | .type = REGULATOR_VOLTAGE, |
| 78 | .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 79 | .vsel_reg = WM8994_LDO_1, |
| 80 | .vsel_mask = WM8994_LDO1_VSEL_MASK, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 81 | .ops = &wm8994_ldo1_ops, |
Axel Lin | f2d103a | 2012-05-23 22:40:58 +0800 | [diff] [blame] | 82 | .min_uV = 2400000, |
| 83 | .uV_step = 100000, |
Mark Brown | 00581b3 | 2012-06-27 14:29:27 +0100 | [diff] [blame] | 84 | .enable_time = 3000, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 85 | .owner = THIS_MODULE, |
| 86 | }, |
| 87 | { |
| 88 | .name = "LDO2", |
| 89 | .id = 2, |
| 90 | .type = REGULATOR_VOLTAGE, |
| 91 | .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 92 | .vsel_reg = WM8994_LDO_2, |
| 93 | .vsel_mask = WM8994_LDO2_VSEL_MASK, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 94 | .ops = &wm8994_ldo2_ops, |
Mark Brown | 00581b3 | 2012-06-27 14:29:27 +0100 | [diff] [blame] | 95 | .enable_time = 3000, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 96 | .owner = THIS_MODULE, |
| 97 | }, |
| 98 | }; |
| 99 | |
Mark Brown | e3d2777 | 2013-04-05 13:11:31 +0100 | [diff] [blame] | 100 | static const struct regulator_consumer_supply wm8994_ldo_consumer[] = { |
| 101 | { .supply = "AVDD1" }, |
| 102 | { .supply = "DCVDD" }, |
| 103 | }; |
| 104 | |
| 105 | static const struct regulator_init_data wm8994_ldo_default[] = { |
| 106 | { |
| 107 | .constraints = { |
| 108 | .valid_ops_mask = REGULATOR_CHANGE_STATUS, |
| 109 | }, |
| 110 | .num_consumer_supplies = 1, |
| 111 | }, |
| 112 | { |
| 113 | .constraints = { |
| 114 | .valid_ops_mask = REGULATOR_CHANGE_STATUS, |
| 115 | }, |
| 116 | .num_consumer_supplies = 1, |
| 117 | }, |
| 118 | }; |
| 119 | |
Bill Pemberton | a502357 | 2012-11-19 13:22:22 -0500 | [diff] [blame] | 120 | static int wm8994_ldo_probe(struct platform_device *pdev) |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 121 | { |
| 122 | struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | dff91d0 | 2013-07-30 17:20:47 +0900 | [diff] [blame] | 123 | struct wm8994_pdata *pdata = dev_get_platdata(wm8994->dev); |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 124 | int id = pdev->id % ARRAY_SIZE(pdata->ldo); |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 125 | struct regulator_config config = { }; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 126 | struct wm8994_ldo *ldo; |
Linus Walleij | 1d2f468 | 2018-11-15 09:01:18 +0100 | [diff] [blame] | 127 | struct gpio_desc *gpiod; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 128 | int ret; |
| 129 | |
| 130 | dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1); |
| 131 | |
Mark Brown | b9e0348 | 2012-02-15 23:12:17 -0800 | [diff] [blame] | 132 | ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL); |
Sachin Kamat | 4deaf85 | 2014-02-20 14:23:23 +0530 | [diff] [blame] | 133 | if (!ldo) |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 134 | return -ENOMEM; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 135 | |
| 136 | ldo->wm8994 = wm8994; |
Mark Brown | e3d2777 | 2013-04-05 13:11:31 +0100 | [diff] [blame] | 137 | ldo->supply = wm8994_ldo_consumer[id]; |
| 138 | ldo->supply.dev_name = dev_name(wm8994->dev); |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 139 | |
Mark Brown | 5d0526e | 2012-05-08 17:45:31 +0100 | [diff] [blame] | 140 | config.dev = wm8994->dev; |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 141 | config.driver_data = ldo; |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 142 | config.regmap = wm8994->regmap; |
Mark Brown | e3d2777 | 2013-04-05 13:11:31 +0100 | [diff] [blame] | 143 | config.init_data = &ldo->init_data; |
Linus Walleij | 1d2f468 | 2018-11-15 09:01:18 +0100 | [diff] [blame] | 144 | |
Charles Keepax | 466affa | 2018-11-21 10:13:16 +0000 | [diff] [blame] | 145 | /* |
| 146 | * Look up LDO enable GPIO from the parent device node, we don't |
| 147 | * use devm because the regulator core will free the GPIO |
| 148 | */ |
| 149 | gpiod = gpiod_get_optional(pdev->dev.parent, |
| 150 | id ? "wlf,ldo2ena" : "wlf,ldo1ena", |
| 151 | GPIOD_OUT_LOW | |
| 152 | GPIOD_FLAGS_BIT_NONEXCLUSIVE); |
Linus Walleij | 1d2f468 | 2018-11-15 09:01:18 +0100 | [diff] [blame] | 153 | if (IS_ERR(gpiod)) |
| 154 | return PTR_ERR(gpiod); |
| 155 | config.ena_gpiod = gpiod; |
Mark Brown | e3d2777 | 2013-04-05 13:11:31 +0100 | [diff] [blame] | 156 | |
| 157 | /* Use default constraints if none set up */ |
Sylwester Nawrocki | 3ea9389 | 2013-04-11 18:36:57 +0200 | [diff] [blame] | 158 | if (!pdata || !pdata->ldo[id].init_data || wm8994->dev->of_node) { |
Mark Brown | e3d2777 | 2013-04-05 13:11:31 +0100 | [diff] [blame] | 159 | dev_dbg(wm8994->dev, "Using default init data, supply %s %s\n", |
| 160 | ldo->supply.dev_name, ldo->supply.supply); |
| 161 | |
| 162 | ldo->init_data = wm8994_ldo_default[id]; |
| 163 | ldo->init_data.consumer_supplies = &ldo->supply; |
Linus Walleij | 1d2f468 | 2018-11-15 09:01:18 +0100 | [diff] [blame] | 164 | if (!gpiod) |
Mark Brown | e3d2777 | 2013-04-05 13:11:31 +0100 | [diff] [blame] | 165 | ldo->init_data.constraints.valid_ops_mask = 0; |
| 166 | } else { |
| 167 | ldo->init_data = *pdata->ldo[id].init_data; |
Mark Brown | 495fd8c | 2012-06-27 14:31:17 +0100 | [diff] [blame] | 168 | } |
Mark Brown | c172708d | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 169 | |
Linus Walleij | daa531d | 2018-12-11 10:53:18 +0100 | [diff] [blame] | 170 | /* |
| 171 | * At this point the GPIO descriptor is handled over to the |
| 172 | * regulator core and we need not worry about it on the |
| 173 | * error path. |
| 174 | */ |
Mark Brown | 9e2bfbb | 2013-08-31 12:01:12 +0100 | [diff] [blame] | 175 | ldo->regulator = devm_regulator_register(&pdev->dev, |
| 176 | &wm8994_ldo_desc[id], |
| 177 | &config); |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 178 | if (IS_ERR(ldo->regulator)) { |
| 179 | ret = PTR_ERR(ldo->regulator); |
| 180 | dev_err(wm8994->dev, "Failed to register LDO%d: %d\n", |
| 181 | id + 1, ret); |
Linus Walleij | daa531d | 2018-12-11 10:53:18 +0100 | [diff] [blame] | 182 | return ret; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | platform_set_drvdata(pdev, ldo); |
| 186 | |
| 187 | return 0; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 190 | static struct platform_driver wm8994_ldo_driver = { |
| 191 | .probe = wm8994_ldo_probe, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 192 | .driver = { |
| 193 | .name = "wm8994-ldo", |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 194 | }, |
| 195 | }; |
| 196 | |
Mark Brown | 0565021 | 2013-04-04 12:08:08 +0100 | [diff] [blame] | 197 | module_platform_driver(wm8994_ldo_driver); |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 198 | |
| 199 | /* Module information */ |
| 200 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 201 | MODULE_DESCRIPTION("WM8994 LDO driver"); |
| 202 | MODULE_LICENSE("GPL"); |
| 203 | MODULE_ALIAS("platform:wm8994-ldo"); |