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