| Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Backlight driver for Wolfson Microelectronics WM831x PMICs |
| 4 | * |
| 5 | * Copyright 2009 Wolfson Microelectonics plc |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/kernel.h> |
| 9 | #include <linux/init.h> |
| 10 | #include <linux/platform_device.h> |
| Paul Gortmaker | 355b200 | 2011-07-03 16:17:28 -0400 | [diff] [blame] | 11 | #include <linux/module.h> |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 12 | #include <linux/fb.h> |
| 13 | #include <linux/backlight.h> |
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 15 | |
| 16 | #include <linux/mfd/wm831x/core.h> |
| 17 | #include <linux/mfd/wm831x/pdata.h> |
| 18 | #include <linux/mfd/wm831x/regulator.h> |
| 19 | |
| 20 | struct wm831x_backlight_data { |
| 21 | struct wm831x *wm831x; |
| 22 | int isink_reg; |
| 23 | int current_brightness; |
| 24 | }; |
| 25 | |
| 26 | static int wm831x_backlight_set(struct backlight_device *bl, int brightness) |
| 27 | { |
| 28 | struct wm831x_backlight_data *data = bl_get_data(bl); |
| 29 | struct wm831x *wm831x = data->wm831x; |
| 30 | int power_up = !data->current_brightness && brightness; |
| 31 | int power_down = data->current_brightness && !brightness; |
| 32 | int ret; |
| 33 | |
| 34 | if (power_up) { |
| 35 | /* Enable the ISINK */ |
| 36 | ret = wm831x_set_bits(wm831x, data->isink_reg, |
| 37 | WM831X_CS1_ENA, WM831X_CS1_ENA); |
| 38 | if (ret < 0) |
| 39 | goto err; |
| 40 | |
| 41 | /* Enable the DC-DC */ |
| 42 | ret = wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, |
| 43 | WM831X_DC4_ENA, WM831X_DC4_ENA); |
| 44 | if (ret < 0) |
| 45 | goto err; |
| 46 | } |
| 47 | |
| 48 | if (power_down) { |
| 49 | /* DCDC first */ |
| 50 | ret = wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, |
| 51 | WM831X_DC4_ENA, 0); |
| 52 | if (ret < 0) |
| 53 | goto err; |
| 54 | |
| 55 | /* ISINK */ |
| 56 | ret = wm831x_set_bits(wm831x, data->isink_reg, |
| 57 | WM831X_CS1_DRIVE | WM831X_CS1_ENA, 0); |
| 58 | if (ret < 0) |
| 59 | goto err; |
| 60 | } |
| 61 | |
| 62 | /* Set the new brightness */ |
| 63 | ret = wm831x_set_bits(wm831x, data->isink_reg, |
| 64 | WM831X_CS1_ISEL_MASK, brightness); |
| 65 | if (ret < 0) |
| 66 | goto err; |
| 67 | |
| 68 | if (power_up) { |
| 69 | /* Drive current through the ISINK */ |
| 70 | ret = wm831x_set_bits(wm831x, data->isink_reg, |
| 71 | WM831X_CS1_DRIVE, WM831X_CS1_DRIVE); |
| 72 | if (ret < 0) |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | data->current_brightness = brightness; |
| 77 | |
| 78 | return 0; |
| 79 | |
| 80 | err: |
| 81 | /* If we were in the middle of a power transition always shut down |
| 82 | * for safety. |
| 83 | */ |
| 84 | if (power_up || power_down) { |
| 85 | wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, WM831X_DC4_ENA, 0); |
| 86 | wm831x_set_bits(wm831x, data->isink_reg, WM831X_CS1_ENA, 0); |
| 87 | } |
| 88 | |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | static int wm831x_backlight_update_status(struct backlight_device *bl) |
| 93 | { |
| 94 | int brightness = bl->props.brightness; |
| 95 | |
| 96 | if (bl->props.power != FB_BLANK_UNBLANK) |
| 97 | brightness = 0; |
| 98 | |
| 99 | if (bl->props.fb_blank != FB_BLANK_UNBLANK) |
| 100 | brightness = 0; |
| 101 | |
| 102 | if (bl->props.state & BL_CORE_SUSPENDED) |
| 103 | brightness = 0; |
| 104 | |
| 105 | return wm831x_backlight_set(bl, brightness); |
| 106 | } |
| 107 | |
| 108 | static int wm831x_backlight_get_brightness(struct backlight_device *bl) |
| 109 | { |
| 110 | struct wm831x_backlight_data *data = bl_get_data(bl); |
| Jingoo Han | 4876b66 | 2014-08-27 10:14:06 +0900 | [diff] [blame] | 111 | |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 112 | return data->current_brightness; |
| 113 | } |
| 114 | |
| Emese Revfy | 9905a43 | 2009-12-14 00:58:57 +0100 | [diff] [blame] | 115 | static const struct backlight_ops wm831x_backlight_ops = { |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 116 | .options = BL_CORE_SUSPENDRESUME, |
| 117 | .update_status = wm831x_backlight_update_status, |
| 118 | .get_brightness = wm831x_backlight_get_brightness, |
| 119 | }; |
| 120 | |
| 121 | static int wm831x_backlight_probe(struct platform_device *pdev) |
| 122 | { |
| 123 | struct wm831x *wm831x = dev_get_drvdata(pdev->dev.parent); |
| Jingoo Han | c512794c | 2013-11-12 15:09:04 -0800 | [diff] [blame] | 124 | struct wm831x_pdata *wm831x_pdata = dev_get_platdata(pdev->dev.parent); |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 125 | struct wm831x_backlight_pdata *pdata; |
| 126 | struct wm831x_backlight_data *data; |
| 127 | struct backlight_device *bl; |
| Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 128 | struct backlight_properties props; |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 129 | int ret, i, max_isel, isink_reg, dcdc_cfg; |
| 130 | |
| 131 | /* We need platform data */ |
| Jingoo Han | c512794c | 2013-11-12 15:09:04 -0800 | [diff] [blame] | 132 | if (wm831x_pdata) |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 133 | pdata = wm831x_pdata->backlight; |
| Jingoo Han | c512794c | 2013-11-12 15:09:04 -0800 | [diff] [blame] | 134 | else |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 135 | pdata = NULL; |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 136 | |
| 137 | if (!pdata) { |
| 138 | dev_err(&pdev->dev, "No platform data supplied\n"); |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | |
| 142 | /* Figure out the maximum current we can use */ |
| 143 | for (i = 0; i < WM831X_ISINK_MAX_ISEL; i++) { |
| 144 | if (wm831x_isinkv_values[i] > pdata->max_uA) |
| 145 | break; |
| 146 | } |
| 147 | |
| 148 | if (i == 0) { |
| 149 | dev_err(&pdev->dev, "Invalid max_uA: %duA\n", pdata->max_uA); |
| 150 | return -EINVAL; |
| 151 | } |
| 152 | max_isel = i - 1; |
| 153 | |
| 154 | if (pdata->max_uA != wm831x_isinkv_values[max_isel]) |
| 155 | dev_warn(&pdev->dev, |
| 156 | "Maximum current is %duA not %duA as requested\n", |
| 157 | wm831x_isinkv_values[max_isel], pdata->max_uA); |
| 158 | |
| 159 | switch (pdata->isink) { |
| 160 | case 1: |
| 161 | isink_reg = WM831X_CURRENT_SINK_1; |
| 162 | dcdc_cfg = 0; |
| 163 | break; |
| 164 | case 2: |
| 165 | isink_reg = WM831X_CURRENT_SINK_2; |
| 166 | dcdc_cfg = WM831X_DC4_FBSRC; |
| 167 | break; |
| 168 | default: |
| 169 | dev_err(&pdev->dev, "Invalid ISINK %d\n", pdata->isink); |
| 170 | return -EINVAL; |
| 171 | } |
| 172 | |
| 173 | /* Configure the ISINK to use for feedback */ |
| 174 | ret = wm831x_reg_unlock(wm831x); |
| 175 | if (ret < 0) |
| 176 | return ret; |
| 177 | |
| 178 | ret = wm831x_set_bits(wm831x, WM831X_DC4_CONTROL, WM831X_DC4_FBSRC, |
| 179 | dcdc_cfg); |
| 180 | |
| 181 | wm831x_reg_lock(wm831x); |
| 182 | if (ret < 0) |
| 183 | return ret; |
| 184 | |
| Julia Lawall | 1107d40 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 185 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 186 | if (data == NULL) |
| 187 | return -ENOMEM; |
| 188 | |
| 189 | data->wm831x = wm831x; |
| 190 | data->current_brightness = 0; |
| 191 | data->isink_reg = isink_reg; |
| 192 | |
| Corentin Chary | f5f4fd4 | 2012-05-29 15:07:20 -0700 | [diff] [blame] | 193 | memset(&props, 0, sizeof(props)); |
| Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 194 | props.type = BACKLIGHT_RAW; |
| Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 195 | props.max_brightness = max_isel; |
| Jingoo Han | f369e66 | 2013-11-12 15:09:25 -0800 | [diff] [blame] | 196 | bl = devm_backlight_device_register(&pdev->dev, "wm831x", &pdev->dev, |
| 197 | data, &wm831x_backlight_ops, &props); |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 198 | if (IS_ERR(bl)) { |
| 199 | dev_err(&pdev->dev, "failed to register backlight\n"); |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 200 | return PTR_ERR(bl); |
| 201 | } |
| 202 | |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 203 | bl->props.brightness = max_isel; |
| 204 | |
| 205 | platform_set_drvdata(pdev, bl); |
| 206 | |
| 207 | /* Disable the DCDC if it was started so we can bootstrap */ |
| 208 | wm831x_set_bits(wm831x, WM831X_DCDC_ENABLE, WM831X_DC4_ENA, 0); |
| 209 | |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 210 | backlight_update_status(bl); |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 215 | static struct platform_driver wm831x_backlight_driver = { |
| 216 | .driver = { |
| 217 | .name = "wm831x-backlight", |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 218 | }, |
| 219 | .probe = wm831x_backlight_probe, |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 220 | }; |
| 221 | |
| Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 222 | module_platform_driver(wm831x_backlight_driver); |
| Mark Brown | a4f3d55 | 2009-09-05 14:09:20 +0100 | [diff] [blame] | 223 | |
| 224 | MODULE_DESCRIPTION("Backlight Driver for WM831x PMICs"); |
| 225 | MODULE_AUTHOR("Mark Brown <broonie@opensource.wolfsonmicro.com"); |
| 226 | MODULE_LICENSE("GPL"); |
| 227 | MODULE_ALIAS("platform:wm831x-backlight"); |