^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * PCF50633 backlight device driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/slab.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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/fb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/mfd/pcf50633/core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/mfd/pcf50633/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) struct pcf50633_bl {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct pcf50633 *pcf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct backlight_device *bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) unsigned int brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned int brightness_limit;
^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) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * pcf50633_bl_set_brightness_limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * Update the brightness limit for the pc50633 backlight. The actual brightness
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * will not go above the limit. This is useful to limit power drain for example
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * on low battery.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * @dev: Pointer to a pcf50633 device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * @limit: The brightness limit. Valid values are 0-63
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) int pcf50633_bl_set_brightness_limit(struct pcf50633 *pcf, unsigned int limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) struct pcf50633_bl *pcf_bl = platform_get_drvdata(pcf->bl_pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (!pcf_bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) pcf_bl->brightness_limit = limit & 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) backlight_update_status(pcf_bl->bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static int pcf50633_bl_update_status(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct pcf50633_bl *pcf_bl = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) unsigned int new_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) bl->props.power != FB_BLANK_UNBLANK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) new_brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) else if (bl->props.brightness < pcf_bl->brightness_limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) new_brightness = bl->props.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) new_brightness = pcf_bl->brightness_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) if (pcf_bl->brightness == new_brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (new_brightness) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDOUT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) new_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (!pcf_bl->brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDENA, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDENA, 0);
^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) pcf_bl->brightness = new_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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) static int pcf50633_bl_get_brightness(struct backlight_device *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) struct pcf50633_bl *pcf_bl = bl_get_data(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return pcf_bl->brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static const struct backlight_ops pcf50633_bl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) .get_brightness = pcf50633_bl_get_brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) .update_status = pcf50633_bl_update_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) .options = BL_CORE_SUSPENDRESUME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) static int pcf50633_bl_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) struct pcf50633_bl *pcf_bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) struct device *parent = pdev->dev.parent;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) struct pcf50633_platform_data *pcf50633_data = dev_get_platdata(parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) struct pcf50633_bl_platform_data *pdata = pcf50633_data->backlight_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) struct backlight_properties bl_props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) pcf_bl = devm_kzalloc(&pdev->dev, sizeof(*pcf_bl), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (!pcf_bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) memset(&bl_props, 0, sizeof(bl_props));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) bl_props.type = BACKLIGHT_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) bl_props.max_brightness = 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) bl_props.power = FB_BLANK_UNBLANK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) if (pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) bl_props.brightness = pdata->default_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) pcf_bl->brightness_limit = pdata->default_brightness_limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) bl_props.brightness = 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) pcf_bl->brightness_limit = 0x3f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) pcf_bl->pcf = dev_to_pcf50633(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) pcf_bl->bl = devm_backlight_device_register(&pdev->dev, pdev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) &pdev->dev, pcf_bl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) &pcf50633_bl_ops, &bl_props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) if (IS_ERR(pcf_bl->bl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) return PTR_ERR(pcf_bl->bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) platform_set_drvdata(pdev, pcf_bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDDIM, pdata->ramp_time);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * Should be different from bl_props.brightness, so we do not exit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * update_status early the first time it's called
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) pcf_bl->brightness = pcf_bl->bl->props.brightness + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) backlight_update_status(pcf_bl->bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) static struct platform_driver pcf50633_bl_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) .probe = pcf50633_bl_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) .name = "pcf50633-backlight",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) module_platform_driver(pcf50633_bl_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) MODULE_DESCRIPTION("PCF50633 backlight driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) MODULE_ALIAS("platform:pcf50633-backlight");