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