Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 2 | /* |
| 3 | * LED driver for WM831x status LEDs |
| 4 | * |
| 5 | * Copyright(C) 2009 Wolfson Microelectronics PLC. |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 9 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 10 | #include <linux/slab.h> |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 11 | #include <linux/leds.h> |
| 12 | #include <linux/err.h> |
| 13 | #include <linux/mfd/wm831x/core.h> |
| 14 | #include <linux/mfd/wm831x/pdata.h> |
| 15 | #include <linux/mfd/wm831x/status.h> |
Paul Gortmaker | 54f4ded | 2011-07-03 13:56:03 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 17 | |
| 18 | |
| 19 | struct wm831x_status { |
| 20 | struct led_classdev cdev; |
| 21 | struct wm831x *wm831x; |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 22 | struct mutex mutex; |
| 23 | |
| 24 | spinlock_t value_lock; |
| 25 | int reg; /* Control register */ |
| 26 | int reg_val; /* Control register value */ |
| 27 | |
| 28 | int blink; |
| 29 | int blink_time; |
| 30 | int blink_cyc; |
| 31 | int src; |
| 32 | enum led_brightness brightness; |
| 33 | }; |
| 34 | |
| 35 | #define to_wm831x_status(led_cdev) \ |
| 36 | container_of(led_cdev, struct wm831x_status, cdev) |
| 37 | |
Andrew Lunn | f633a51 | 2015-08-20 12:40:45 +0200 | [diff] [blame] | 38 | static void wm831x_status_set(struct wm831x_status *led) |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 39 | { |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 40 | unsigned long flags; |
| 41 | |
| 42 | mutex_lock(&led->mutex); |
| 43 | |
| 44 | led->reg_val &= ~(WM831X_LED_SRC_MASK | WM831X_LED_MODE_MASK | |
| 45 | WM831X_LED_DUTY_CYC_MASK | WM831X_LED_DUR_MASK); |
| 46 | |
| 47 | spin_lock_irqsave(&led->value_lock, flags); |
| 48 | |
| 49 | led->reg_val |= led->src << WM831X_LED_SRC_SHIFT; |
| 50 | if (led->blink) { |
| 51 | led->reg_val |= 2 << WM831X_LED_MODE_SHIFT; |
| 52 | led->reg_val |= led->blink_time << WM831X_LED_DUR_SHIFT; |
| 53 | led->reg_val |= led->blink_cyc; |
| 54 | } else { |
| 55 | if (led->brightness != LED_OFF) |
| 56 | led->reg_val |= 1 << WM831X_LED_MODE_SHIFT; |
| 57 | } |
| 58 | |
| 59 | spin_unlock_irqrestore(&led->value_lock, flags); |
| 60 | |
| 61 | wm831x_reg_write(led->wm831x, led->reg, led->reg_val); |
| 62 | |
| 63 | mutex_unlock(&led->mutex); |
| 64 | } |
| 65 | |
Andrew Lunn | f633a51 | 2015-08-20 12:40:45 +0200 | [diff] [blame] | 66 | static int wm831x_status_brightness_set(struct led_classdev *led_cdev, |
| 67 | enum led_brightness value) |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 68 | { |
| 69 | struct wm831x_status *led = to_wm831x_status(led_cdev); |
| 70 | unsigned long flags; |
| 71 | |
| 72 | spin_lock_irqsave(&led->value_lock, flags); |
| 73 | led->brightness = value; |
| 74 | if (value == LED_OFF) |
| 75 | led->blink = 0; |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 76 | spin_unlock_irqrestore(&led->value_lock, flags); |
Andrew Lunn | f633a51 | 2015-08-20 12:40:45 +0200 | [diff] [blame] | 77 | wm831x_status_set(led); |
| 78 | |
| 79 | return 0; |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static int wm831x_status_blink_set(struct led_classdev *led_cdev, |
| 83 | unsigned long *delay_on, |
| 84 | unsigned long *delay_off) |
| 85 | { |
| 86 | struct wm831x_status *led = to_wm831x_status(led_cdev); |
| 87 | unsigned long flags; |
| 88 | int ret = 0; |
| 89 | |
| 90 | /* Pick some defaults if we've not been given times */ |
| 91 | if (*delay_on == 0 && *delay_off == 0) { |
| 92 | *delay_on = 250; |
| 93 | *delay_off = 250; |
| 94 | } |
| 95 | |
| 96 | spin_lock_irqsave(&led->value_lock, flags); |
| 97 | |
| 98 | /* We only have a limited selection of settings, see if we can |
| 99 | * support the configuration we're being given */ |
| 100 | switch (*delay_on) { |
| 101 | case 1000: |
| 102 | led->blink_time = 0; |
| 103 | break; |
| 104 | case 250: |
| 105 | led->blink_time = 1; |
| 106 | break; |
| 107 | case 125: |
| 108 | led->blink_time = 2; |
| 109 | break; |
| 110 | case 62: |
| 111 | case 63: |
| 112 | /* Actually 62.5ms */ |
| 113 | led->blink_time = 3; |
| 114 | break; |
| 115 | default: |
| 116 | ret = -EINVAL; |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | if (ret == 0) { |
| 121 | switch (*delay_off / *delay_on) { |
| 122 | case 1: |
| 123 | led->blink_cyc = 0; |
| 124 | break; |
| 125 | case 3: |
| 126 | led->blink_cyc = 1; |
| 127 | break; |
| 128 | case 4: |
| 129 | led->blink_cyc = 2; |
| 130 | break; |
| 131 | case 8: |
| 132 | led->blink_cyc = 3; |
| 133 | break; |
| 134 | default: |
| 135 | ret = -EINVAL; |
| 136 | break; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | if (ret == 0) |
| 141 | led->blink = 1; |
| 142 | else |
| 143 | led->blink = 0; |
| 144 | |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 145 | spin_unlock_irqrestore(&led->value_lock, flags); |
Andrew Lunn | f633a51 | 2015-08-20 12:40:45 +0200 | [diff] [blame] | 146 | wm831x_status_set(led); |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 147 | |
| 148 | return ret; |
| 149 | } |
| 150 | |
Jingoo Han | 8a4529a | 2013-01-21 21:59:29 -0800 | [diff] [blame] | 151 | static const char * const led_src_texts[] = { |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 152 | "otp", |
| 153 | "power", |
| 154 | "charger", |
| 155 | "soft", |
| 156 | }; |
| 157 | |
| 158 | static ssize_t wm831x_status_src_show(struct device *dev, |
| 159 | struct device_attribute *attr, char *buf) |
| 160 | { |
| 161 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 162 | struct wm831x_status *led = to_wm831x_status(led_cdev); |
| 163 | int i; |
| 164 | ssize_t ret = 0; |
| 165 | |
| 166 | mutex_lock(&led->mutex); |
| 167 | |
| 168 | for (i = 0; i < ARRAY_SIZE(led_src_texts); i++) |
| 169 | if (i == led->src) |
| 170 | ret += sprintf(&buf[ret], "[%s] ", led_src_texts[i]); |
| 171 | else |
| 172 | ret += sprintf(&buf[ret], "%s ", led_src_texts[i]); |
| 173 | |
| 174 | mutex_unlock(&led->mutex); |
| 175 | |
| 176 | ret += sprintf(&buf[ret], "\n"); |
| 177 | |
| 178 | return ret; |
| 179 | } |
| 180 | |
| 181 | static ssize_t wm831x_status_src_store(struct device *dev, |
| 182 | struct device_attribute *attr, |
| 183 | const char *buf, size_t size) |
| 184 | { |
| 185 | struct led_classdev *led_cdev = dev_get_drvdata(dev); |
| 186 | struct wm831x_status *led = to_wm831x_status(led_cdev); |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 187 | int i; |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 188 | |
Andy Shevchenko | d54e5b5 | 2018-05-03 21:10:03 +0300 | [diff] [blame] | 189 | i = sysfs_match_string(led_src_texts, buf); |
| 190 | if (i >= 0) { |
| 191 | mutex_lock(&led->mutex); |
| 192 | led->src = i; |
| 193 | mutex_unlock(&led->mutex); |
| 194 | wm831x_status_set(led); |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | return size; |
| 198 | } |
| 199 | |
| 200 | static DEVICE_ATTR(src, 0644, wm831x_status_src_show, wm831x_status_src_store); |
| 201 | |
Johan Hovold | b2015ed | 2014-06-25 10:08:53 -0700 | [diff] [blame] | 202 | static struct attribute *wm831x_status_attrs[] = { |
| 203 | &dev_attr_src.attr, |
| 204 | NULL |
| 205 | }; |
| 206 | ATTRIBUTE_GROUPS(wm831x_status); |
| 207 | |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 208 | static int wm831x_status_probe(struct platform_device *pdev) |
| 209 | { |
| 210 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
| 211 | struct wm831x_pdata *chip_pdata; |
| 212 | struct wm831x_status_pdata pdata; |
| 213 | struct wm831x_status *drvdata; |
| 214 | struct resource *res; |
| 215 | int id = pdev->id % ARRAY_SIZE(chip_pdata->status); |
| 216 | int ret; |
| 217 | |
Mark Brown | 61abeba | 2013-08-29 07:18:14 -0700 | [diff] [blame] | 218 | res = platform_get_resource(pdev, IORESOURCE_REG, 0); |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 219 | if (res == NULL) { |
Mark Brown | 61abeba | 2013-08-29 07:18:14 -0700 | [diff] [blame] | 220 | dev_err(&pdev->dev, "No register resource\n"); |
Johan Hovold | b2015ed | 2014-06-25 10:08:53 -0700 | [diff] [blame] | 221 | return -EINVAL; |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 222 | } |
| 223 | |
Mark Brown | 1713cb9 | 2012-01-10 15:09:45 -0800 | [diff] [blame] | 224 | drvdata = devm_kzalloc(&pdev->dev, sizeof(struct wm831x_status), |
| 225 | GFP_KERNEL); |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 226 | if (!drvdata) |
| 227 | return -ENOMEM; |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 228 | |
| 229 | drvdata->wm831x = wm831x; |
| 230 | drvdata->reg = res->start; |
| 231 | |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 232 | if (dev_get_platdata(wm831x->dev)) |
| 233 | chip_pdata = dev_get_platdata(wm831x->dev); |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 234 | else |
| 235 | chip_pdata = NULL; |
| 236 | |
| 237 | memset(&pdata, 0, sizeof(pdata)); |
| 238 | if (chip_pdata && chip_pdata->status[id]) |
| 239 | memcpy(&pdata, chip_pdata->status[id], sizeof(pdata)); |
| 240 | else |
| 241 | pdata.name = dev_name(&pdev->dev); |
| 242 | |
| 243 | mutex_init(&drvdata->mutex); |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 244 | spin_lock_init(&drvdata->value_lock); |
| 245 | |
| 246 | /* We cache the configuration register and read startup values |
| 247 | * from it. */ |
| 248 | drvdata->reg_val = wm831x_reg_read(wm831x, drvdata->reg); |
| 249 | |
| 250 | if (drvdata->reg_val & WM831X_LED_MODE_MASK) |
| 251 | drvdata->brightness = LED_FULL; |
| 252 | else |
| 253 | drvdata->brightness = LED_OFF; |
| 254 | |
| 255 | /* Set a default source if configured, otherwise leave the |
| 256 | * current hardware setting. |
| 257 | */ |
| 258 | if (pdata.default_src == WM831X_STATUS_PRESERVE) { |
| 259 | drvdata->src = drvdata->reg_val; |
| 260 | drvdata->src &= WM831X_LED_SRC_MASK; |
| 261 | drvdata->src >>= WM831X_LED_SRC_SHIFT; |
| 262 | } else { |
| 263 | drvdata->src = pdata.default_src - 1; |
| 264 | } |
| 265 | |
| 266 | drvdata->cdev.name = pdata.name; |
| 267 | drvdata->cdev.default_trigger = pdata.default_trigger; |
Andrew Lunn | f633a51 | 2015-08-20 12:40:45 +0200 | [diff] [blame] | 268 | drvdata->cdev.brightness_set_blocking = wm831x_status_brightness_set; |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 269 | drvdata->cdev.blink_set = wm831x_status_blink_set; |
Johan Hovold | b2015ed | 2014-06-25 10:08:53 -0700 | [diff] [blame] | 270 | drvdata->cdev.groups = wm831x_status_groups; |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 271 | |
Amitoj Kaur Chawla | 8d3b6a4 | 2016-03-09 08:53:47 +0530 | [diff] [blame] | 272 | ret = devm_led_classdev_register(wm831x->dev, &drvdata->cdev); |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 273 | if (ret < 0) { |
| 274 | dev_err(&pdev->dev, "Failed to register LED: %d\n", ret); |
Johan Hovold | b2015ed | 2014-06-25 10:08:53 -0700 | [diff] [blame] | 275 | return ret; |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 276 | } |
| 277 | |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 278 | return 0; |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 279 | } |
| 280 | |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 281 | static struct platform_driver wm831x_status_driver = { |
| 282 | .driver = { |
| 283 | .name = "wm831x-status", |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 284 | }, |
| 285 | .probe = wm831x_status_probe, |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 286 | }; |
| 287 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 288 | module_platform_driver(wm831x_status_driver); |
Mark Brown | c746b55 | 2009-09-05 14:09:21 +0100 | [diff] [blame] | 289 | |
| 290 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com>"); |
| 291 | MODULE_DESCRIPTION("WM831x status LED driver"); |
| 292 | MODULE_LICENSE("GPL"); |
| 293 | MODULE_ALIAS("platform:wm831x-status"); |