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> |
| 21 | #include <linux/gpio.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 23 | |
| 24 | #include <linux/mfd/wm8994/core.h> |
| 25 | #include <linux/mfd/wm8994/registers.h> |
| 26 | #include <linux/mfd/wm8994/pdata.h> |
| 27 | |
| 28 | struct wm8994_ldo { |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 29 | struct regulator_dev *regulator; |
| 30 | struct wm8994 *wm8994; |
| 31 | }; |
| 32 | |
| 33 | #define WM8994_LDO1_MAX_SELECTOR 0x7 |
| 34 | #define WM8994_LDO2_MAX_SELECTOR 0x3 |
| 35 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 36 | static struct regulator_ops wm8994_ldo1_ops = { |
Axel Lin | f2d103a | 2012-05-23 22:40:58 +0800 | [diff] [blame] | 37 | .list_voltage = regulator_list_voltage_linear, |
| 38 | .map_voltage = regulator_map_voltage_linear, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 39 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 40 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | static int wm8994_ldo2_list_voltage(struct regulator_dev *rdev, |
| 44 | unsigned int selector) |
| 45 | { |
Mark Brown | 5a7743e | 2010-12-13 15:07:40 +0000 | [diff] [blame] | 46 | struct wm8994_ldo *ldo = rdev_get_drvdata(rdev); |
| 47 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 48 | if (selector > WM8994_LDO2_MAX_SELECTOR) |
| 49 | return -EINVAL; |
| 50 | |
Mark Brown | 5a7743e | 2010-12-13 15:07:40 +0000 | [diff] [blame] | 51 | switch (ldo->wm8994->type) { |
| 52 | case WM8994: |
| 53 | return (selector * 100000) + 900000; |
| 54 | case WM8958: |
| 55 | return (selector * 100000) + 1000000; |
Mark Brown | a1ff89e | 2011-07-24 12:40:48 +0100 | [diff] [blame] | 56 | case WM1811: |
| 57 | switch (selector) { |
| 58 | case 0: |
| 59 | return -EINVAL; |
| 60 | default: |
| 61 | return (selector * 100000) + 950000; |
| 62 | } |
| 63 | break; |
Mark Brown | 5a7743e | 2010-12-13 15:07:40 +0000 | [diff] [blame] | 64 | default: |
| 65 | return -EINVAL; |
| 66 | } |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 69 | static struct regulator_ops wm8994_ldo2_ops = { |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 70 | .list_voltage = wm8994_ldo2_list_voltage, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 71 | .get_voltage_sel = regulator_get_voltage_sel_regmap, |
| 72 | .set_voltage_sel = regulator_set_voltage_sel_regmap, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
Mark Brown | 25e4d60 | 2012-04-03 20:47:27 +0100 | [diff] [blame] | 75 | static const struct regulator_desc wm8994_ldo_desc[] = { |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 76 | { |
| 77 | .name = "LDO1", |
| 78 | .id = 1, |
| 79 | .type = REGULATOR_VOLTAGE, |
| 80 | .n_voltages = WM8994_LDO1_MAX_SELECTOR + 1, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 81 | .vsel_reg = WM8994_LDO_1, |
| 82 | .vsel_mask = WM8994_LDO1_VSEL_MASK, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 83 | .ops = &wm8994_ldo1_ops, |
Axel Lin | f2d103a | 2012-05-23 22:40:58 +0800 | [diff] [blame] | 84 | .min_uV = 2400000, |
| 85 | .uV_step = 100000, |
Mark Brown | 00581b3 | 2012-06-27 14:29:27 +0100 | [diff] [blame] | 86 | .enable_time = 3000, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 87 | .owner = THIS_MODULE, |
| 88 | }, |
| 89 | { |
| 90 | .name = "LDO2", |
| 91 | .id = 2, |
| 92 | .type = REGULATOR_VOLTAGE, |
| 93 | .n_voltages = WM8994_LDO2_MAX_SELECTOR + 1, |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 94 | .vsel_reg = WM8994_LDO_2, |
| 95 | .vsel_mask = WM8994_LDO2_VSEL_MASK, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 96 | .ops = &wm8994_ldo2_ops, |
Mark Brown | 00581b3 | 2012-06-27 14:29:27 +0100 | [diff] [blame] | 97 | .enable_time = 3000, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 98 | .owner = THIS_MODULE, |
| 99 | }, |
| 100 | }; |
| 101 | |
| 102 | static __devinit int wm8994_ldo_probe(struct platform_device *pdev) |
| 103 | { |
| 104 | struct wm8994 *wm8994 = dev_get_drvdata(pdev->dev.parent); |
| 105 | struct wm8994_pdata *pdata = wm8994->dev->platform_data; |
| 106 | int id = pdev->id % ARRAY_SIZE(pdata->ldo); |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 107 | struct regulator_config config = { }; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 108 | struct wm8994_ldo *ldo; |
| 109 | int ret; |
| 110 | |
| 111 | dev_dbg(&pdev->dev, "Probing LDO%d\n", id + 1); |
| 112 | |
Mark Brown | b9e0348 | 2012-02-15 23:12:17 -0800 | [diff] [blame] | 113 | ldo = devm_kzalloc(&pdev->dev, sizeof(struct wm8994_ldo), GFP_KERNEL); |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 114 | if (ldo == NULL) { |
| 115 | dev_err(&pdev->dev, "Unable to allocate private data\n"); |
| 116 | return -ENOMEM; |
| 117 | } |
| 118 | |
| 119 | ldo->wm8994 = wm8994; |
| 120 | |
Mark Brown | 5d0526e | 2012-05-08 17:45:31 +0100 | [diff] [blame] | 121 | config.dev = wm8994->dev; |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 122 | config.driver_data = ldo; |
Mark Brown | 633b6fc | 2012-04-15 11:25:36 +0100 | [diff] [blame] | 123 | config.regmap = wm8994->regmap; |
Mark Brown | 495fd8c | 2012-06-27 14:31:17 +0100 | [diff] [blame] | 124 | if (pdata) { |
Mark Brown | b68b45d | 2012-05-14 13:24:13 +0100 | [diff] [blame] | 125 | config.init_data = pdata->ldo[id].init_data; |
Mark Brown | 495fd8c | 2012-06-27 14:31:17 +0100 | [diff] [blame] | 126 | config.ena_gpio = pdata->ldo[id].enable; |
| 127 | } |
Mark Brown | c172708 | 2012-04-04 00:50:22 +0100 | [diff] [blame] | 128 | |
| 129 | ldo->regulator = regulator_register(&wm8994_ldo_desc[id], &config); |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 130 | if (IS_ERR(ldo->regulator)) { |
| 131 | ret = PTR_ERR(ldo->regulator); |
| 132 | dev_err(wm8994->dev, "Failed to register LDO%d: %d\n", |
| 133 | id + 1, ret); |
Mark Brown | 495fd8c | 2012-06-27 14:31:17 +0100 | [diff] [blame] | 134 | goto err; |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | platform_set_drvdata(pdev, ldo); |
| 138 | |
| 139 | return 0; |
| 140 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 141 | err: |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static __devexit int wm8994_ldo_remove(struct platform_device *pdev) |
| 146 | { |
| 147 | struct wm8994_ldo *ldo = platform_get_drvdata(pdev); |
| 148 | |
Dmitry Torokhov | 598b357 | 2010-02-23 23:38:44 -0800 | [diff] [blame] | 149 | platform_set_drvdata(pdev, NULL); |
| 150 | |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 151 | regulator_unregister(ldo->regulator); |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | static struct platform_driver wm8994_ldo_driver = { |
| 157 | .probe = wm8994_ldo_probe, |
Bill Pemberton | 5eb9f2b | 2012-11-19 13:20:42 -0500 | [diff] [blame^] | 158 | .remove = wm8994_ldo_remove, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 159 | .driver = { |
| 160 | .name = "wm8994-ldo", |
Dmitry Torokhov | 598b357 | 2010-02-23 23:38:44 -0800 | [diff] [blame] | 161 | .owner = THIS_MODULE, |
Mark Brown | 69dc16c | 2010-01-25 19:41:57 +0000 | [diff] [blame] | 162 | }, |
| 163 | }; |
| 164 | |
| 165 | static int __init wm8994_ldo_init(void) |
| 166 | { |
| 167 | int ret; |
| 168 | |
| 169 | ret = platform_driver_register(&wm8994_ldo_driver); |
| 170 | if (ret != 0) |
| 171 | pr_err("Failed to register Wm8994 GP LDO driver: %d\n", ret); |
| 172 | |
| 173 | return ret; |
| 174 | } |
| 175 | subsys_initcall(wm8994_ldo_init); |
| 176 | |
| 177 | static void __exit wm8994_ldo_exit(void) |
| 178 | { |
| 179 | platform_driver_unregister(&wm8994_ldo_driver); |
| 180 | } |
| 181 | module_exit(wm8994_ldo_exit); |
| 182 | |
| 183 | /* Module information */ |
| 184 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 185 | MODULE_DESCRIPTION("WM8994 LDO driver"); |
| 186 | MODULE_LICENSE("GPL"); |
| 187 | MODULE_ALIAS("platform:wm8994-ldo"); |