| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 1 | /* | 
|  | 2 | * wm831x-isink.c  --  Current sink driver for the WM831x series | 
|  | 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/i2c.h> | 
|  | 20 | #include <linux/platform_device.h> | 
|  | 21 | #include <linux/regulator/driver.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/slab.h> | 
| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 23 |  | 
|  | 24 | #include <linux/mfd/wm831x/core.h> | 
|  | 25 | #include <linux/mfd/wm831x/regulator.h> | 
|  | 26 | #include <linux/mfd/wm831x/pdata.h> | 
|  | 27 |  | 
|  | 28 | #define WM831X_ISINK_MAX_NAME 7 | 
|  | 29 |  | 
|  | 30 | struct wm831x_isink { | 
|  | 31 | char name[WM831X_ISINK_MAX_NAME]; | 
|  | 32 | struct regulator_desc desc; | 
|  | 33 | int reg; | 
|  | 34 | struct wm831x *wm831x; | 
|  | 35 | struct regulator_dev *regulator; | 
|  | 36 | }; | 
|  | 37 |  | 
|  | 38 | static int wm831x_isink_enable(struct regulator_dev *rdev) | 
|  | 39 | { | 
|  | 40 | struct wm831x_isink *isink = rdev_get_drvdata(rdev); | 
|  | 41 | struct wm831x *wm831x = isink->wm831x; | 
|  | 42 | int ret; | 
|  | 43 |  | 
|  | 44 | /* We have a two stage enable: first start the ISINK... */ | 
|  | 45 | ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA, | 
|  | 46 | WM831X_CS1_ENA); | 
|  | 47 | if (ret != 0) | 
|  | 48 | return ret; | 
|  | 49 |  | 
|  | 50 | /* ...then enable drive */ | 
|  | 51 | ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_DRIVE, | 
|  | 52 | WM831X_CS1_DRIVE); | 
|  | 53 | if (ret != 0) | 
|  | 54 | wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA, 0); | 
|  | 55 |  | 
|  | 56 | return ret; | 
|  | 57 |  | 
|  | 58 | } | 
|  | 59 |  | 
|  | 60 | static int wm831x_isink_disable(struct regulator_dev *rdev) | 
|  | 61 | { | 
|  | 62 | struct wm831x_isink *isink = rdev_get_drvdata(rdev); | 
|  | 63 | struct wm831x *wm831x = isink->wm831x; | 
|  | 64 | int ret; | 
|  | 65 |  | 
|  | 66 | ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_DRIVE, 0); | 
|  | 67 | if (ret < 0) | 
|  | 68 | return ret; | 
|  | 69 |  | 
|  | 70 | ret = wm831x_set_bits(wm831x, isink->reg, WM831X_CS1_ENA, 0); | 
|  | 71 | if (ret < 0) | 
|  | 72 | return ret; | 
|  | 73 |  | 
|  | 74 | return ret; | 
|  | 75 |  | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | static int wm831x_isink_is_enabled(struct regulator_dev *rdev) | 
|  | 79 | { | 
|  | 80 | struct wm831x_isink *isink = rdev_get_drvdata(rdev); | 
|  | 81 | struct wm831x *wm831x = isink->wm831x; | 
|  | 82 | int ret; | 
|  | 83 |  | 
|  | 84 | ret = wm831x_reg_read(wm831x, isink->reg); | 
|  | 85 | if (ret < 0) | 
|  | 86 | return ret; | 
|  | 87 |  | 
|  | 88 | if ((ret & (WM831X_CS1_ENA | WM831X_CS1_DRIVE)) == | 
|  | 89 | (WM831X_CS1_ENA | WM831X_CS1_DRIVE)) | 
|  | 90 | return 1; | 
|  | 91 | else | 
|  | 92 | return 0; | 
|  | 93 | } | 
|  | 94 |  | 
|  | 95 | static int wm831x_isink_set_current(struct regulator_dev *rdev, | 
|  | 96 | int min_uA, int max_uA) | 
|  | 97 | { | 
|  | 98 | struct wm831x_isink *isink = rdev_get_drvdata(rdev); | 
|  | 99 | struct wm831x *wm831x = isink->wm831x; | 
|  | 100 | int ret, i; | 
|  | 101 |  | 
|  | 102 | for (i = 0; i < ARRAY_SIZE(wm831x_isinkv_values); i++) { | 
|  | 103 | int val = wm831x_isinkv_values[i]; | 
| Axel Lin | ed3be9a | 2012-03-27 15:18:53 +0800 | [diff] [blame] | 104 | if (min_uA <= val && val <= max_uA) { | 
| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 105 | ret = wm831x_set_bits(wm831x, isink->reg, | 
|  | 106 | WM831X_CS1_ISEL_MASK, i); | 
|  | 107 | return ret; | 
|  | 108 | } | 
|  | 109 | } | 
|  | 110 |  | 
|  | 111 | return -EINVAL; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | static int wm831x_isink_get_current(struct regulator_dev *rdev) | 
|  | 115 | { | 
|  | 116 | struct wm831x_isink *isink = rdev_get_drvdata(rdev); | 
|  | 117 | struct wm831x *wm831x = isink->wm831x; | 
|  | 118 | int ret; | 
|  | 119 |  | 
|  | 120 | ret = wm831x_reg_read(wm831x, isink->reg); | 
|  | 121 | if (ret < 0) | 
|  | 122 | return ret; | 
|  | 123 |  | 
|  | 124 | ret &= WM831X_CS1_ISEL_MASK; | 
|  | 125 | if (ret > WM831X_ISINK_MAX_ISEL) | 
|  | 126 | ret = WM831X_ISINK_MAX_ISEL; | 
|  | 127 |  | 
|  | 128 | return wm831x_isinkv_values[ret]; | 
|  | 129 | } | 
|  | 130 |  | 
|  | 131 | static struct regulator_ops wm831x_isink_ops = { | 
|  | 132 | .is_enabled = wm831x_isink_is_enabled, | 
|  | 133 | .enable = wm831x_isink_enable, | 
|  | 134 | .disable = wm831x_isink_disable, | 
|  | 135 | .set_current_limit = wm831x_isink_set_current, | 
|  | 136 | .get_current_limit = wm831x_isink_get_current, | 
|  | 137 | }; | 
|  | 138 |  | 
|  | 139 | static irqreturn_t wm831x_isink_irq(int irq, void *data) | 
|  | 140 | { | 
|  | 141 | struct wm831x_isink *isink = data; | 
|  | 142 |  | 
|  | 143 | regulator_notifier_call_chain(isink->regulator, | 
|  | 144 | REGULATOR_EVENT_OVER_CURRENT, | 
|  | 145 | NULL); | 
|  | 146 |  | 
|  | 147 | return IRQ_HANDLED; | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 |  | 
|  | 151 | static __devinit int wm831x_isink_probe(struct platform_device *pdev) | 
|  | 152 | { | 
|  | 153 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); | 
|  | 154 | struct wm831x_pdata *pdata = wm831x->dev->platform_data; | 
|  | 155 | struct wm831x_isink *isink; | 
|  | 156 | int id = pdev->id % ARRAY_SIZE(pdata->isink); | 
|  | 157 | struct resource *res; | 
|  | 158 | int ret, irq; | 
|  | 159 |  | 
|  | 160 | dev_dbg(&pdev->dev, "Probing ISINK%d\n", id + 1); | 
|  | 161 |  | 
|  | 162 | if (pdata == NULL || pdata->isink[id] == NULL) | 
|  | 163 | return -ENODEV; | 
|  | 164 |  | 
| Mark Brown | fded2f4 | 2011-12-15 02:11:14 +0800 | [diff] [blame] | 165 | isink = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_isink), | 
|  | 166 | GFP_KERNEL); | 
| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 167 | if (isink == NULL) { | 
|  | 168 | dev_err(&pdev->dev, "Unable to allocate private data\n"); | 
|  | 169 | return -ENOMEM; | 
|  | 170 | } | 
|  | 171 |  | 
| Mark Brown | e8092da | 2009-11-30 14:01:56 +0000 | [diff] [blame] | 172 | isink->wm831x = wm831x; | 
|  | 173 |  | 
| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 174 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); | 
|  | 175 | if (res == NULL) { | 
|  | 176 | dev_err(&pdev->dev, "No I/O resource\n"); | 
|  | 177 | ret = -EINVAL; | 
|  | 178 | goto err; | 
|  | 179 | } | 
|  | 180 | isink->reg = res->start; | 
|  | 181 |  | 
|  | 182 | /* For current parts this is correct; probably need to revisit | 
|  | 183 | * in future. | 
|  | 184 | */ | 
|  | 185 | snprintf(isink->name, sizeof(isink->name), "ISINK%d", id + 1); | 
|  | 186 | isink->desc.name = isink->name; | 
|  | 187 | isink->desc.id = id; | 
|  | 188 | isink->desc.ops = &wm831x_isink_ops; | 
|  | 189 | isink->desc.type = REGULATOR_CURRENT; | 
|  | 190 | isink->desc.owner = THIS_MODULE; | 
|  | 191 |  | 
|  | 192 | isink->regulator = regulator_register(&isink->desc, &pdev->dev, | 
| Rajendra Nayak | 2c043bc | 2011-11-18 16:47:19 +0530 | [diff] [blame] | 193 | pdata->isink[id], isink, NULL); | 
| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 194 | if (IS_ERR(isink->regulator)) { | 
|  | 195 | ret = PTR_ERR(isink->regulator); | 
|  | 196 | dev_err(wm831x->dev, "Failed to register ISINK%d: %d\n", | 
|  | 197 | id + 1, ret); | 
|  | 198 | goto err; | 
|  | 199 | } | 
|  | 200 |  | 
|  | 201 | irq = platform_get_irq(pdev, 0); | 
| Mark Brown | dfda9c2 | 2011-03-01 16:50:43 +0000 | [diff] [blame] | 202 | ret = request_threaded_irq(irq, NULL, wm831x_isink_irq, | 
|  | 203 | IRQF_TRIGGER_RISING, isink->name, isink); | 
| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 204 | if (ret != 0) { | 
|  | 205 | dev_err(&pdev->dev, "Failed to request ISINK IRQ %d: %d\n", | 
|  | 206 | irq, ret); | 
|  | 207 | goto err_regulator; | 
|  | 208 | } | 
|  | 209 |  | 
|  | 210 | platform_set_drvdata(pdev, isink); | 
|  | 211 |  | 
|  | 212 | return 0; | 
|  | 213 |  | 
|  | 214 | err_regulator: | 
|  | 215 | regulator_unregister(isink->regulator); | 
|  | 216 | err: | 
| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 217 | return ret; | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | static __devexit int wm831x_isink_remove(struct platform_device *pdev) | 
|  | 221 | { | 
|  | 222 | struct wm831x_isink *isink = platform_get_drvdata(pdev); | 
| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 223 |  | 
| Dmitry Torokhov | eb66d56 | 2010-02-23 23:38:39 -0800 | [diff] [blame] | 224 | platform_set_drvdata(pdev, NULL); | 
|  | 225 |  | 
| Mark Brown | dfda9c2 | 2011-03-01 16:50:43 +0000 | [diff] [blame] | 226 | free_irq(platform_get_irq(pdev, 0), isink); | 
| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 227 |  | 
|  | 228 | regulator_unregister(isink->regulator); | 
| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 229 |  | 
|  | 230 | return 0; | 
|  | 231 | } | 
|  | 232 |  | 
|  | 233 | static struct platform_driver wm831x_isink_driver = { | 
|  | 234 | .probe = wm831x_isink_probe, | 
|  | 235 | .remove = __devexit_p(wm831x_isink_remove), | 
|  | 236 | .driver		= { | 
|  | 237 | .name	= "wm831x-isink", | 
| Dmitry Torokhov | eb66d56 | 2010-02-23 23:38:39 -0800 | [diff] [blame] | 238 | .owner	= THIS_MODULE, | 
| Mark Brown | d4d6b72 | 2009-07-28 15:23:46 +0100 | [diff] [blame] | 239 | }, | 
|  | 240 | }; | 
|  | 241 |  | 
|  | 242 | static int __init wm831x_isink_init(void) | 
|  | 243 | { | 
|  | 244 | int ret; | 
|  | 245 | ret = platform_driver_register(&wm831x_isink_driver); | 
|  | 246 | if (ret != 0) | 
|  | 247 | pr_err("Failed to register WM831x ISINK driver: %d\n", ret); | 
|  | 248 |  | 
|  | 249 | return ret; | 
|  | 250 | } | 
|  | 251 | subsys_initcall(wm831x_isink_init); | 
|  | 252 |  | 
|  | 253 | static void __exit wm831x_isink_exit(void) | 
|  | 254 | { | 
|  | 255 | platform_driver_unregister(&wm831x_isink_driver); | 
|  | 256 | } | 
|  | 257 | module_exit(wm831x_isink_exit); | 
|  | 258 |  | 
|  | 259 | /* Module information */ | 
|  | 260 | MODULE_AUTHOR("Mark Brown"); | 
|  | 261 | MODULE_DESCRIPTION("WM831x current sink driver"); | 
|  | 262 | MODULE_LICENSE("GPL"); | 
|  | 263 | MODULE_ALIAS("platform:wm831x-isink"); |