^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) * linux/drivers/video/backlight/aat2870_bl.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2011, NVIDIA Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Jin Park <jinyoungp@nvidia.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/fb.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/mfd/aat2870.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) struct aat2870_bl_driver_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) struct platform_device *pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) struct backlight_device *bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) int max_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int brightness; /* current brightness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static inline int aat2870_brightness(struct aat2870_bl_driver_data *aat2870_bl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) int brightness)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct backlight_device *bd = aat2870_bl->bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) int val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) val = brightness * (aat2870_bl->max_current - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) val /= bd->props.max_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) static inline int aat2870_bl_enable(struct aat2870_bl_driver_data *aat2870_bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) struct aat2870_data *aat2870
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) = dev_get_drvdata(aat2870_bl->pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return aat2870->write(aat2870, AAT2870_BL_CH_EN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) (u8)aat2870_bl->channels);
^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 inline int aat2870_bl_disable(struct aat2870_bl_driver_data *aat2870_bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct aat2870_data *aat2870
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) = dev_get_drvdata(aat2870_bl->pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return aat2870->write(aat2870, AAT2870_BL_CH_EN, 0x0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) static int aat2870_bl_update_status(struct backlight_device *bd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) struct aat2870_bl_driver_data *aat2870_bl = bl_get_data(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) struct aat2870_data *aat2870 =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) dev_get_drvdata(aat2870_bl->pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) int brightness = bd->props.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if ((brightness < 0) || (bd->props.max_brightness < brightness)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) dev_err(&bd->dev, "invalid brightness, %d\n", brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) dev_dbg(&bd->dev, "brightness=%d, power=%d, state=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) bd->props.brightness, bd->props.power, bd->props.state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) if ((bd->props.power != FB_BLANK_UNBLANK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) (bd->props.state & BL_CORE_FBBLANK) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) (bd->props.state & BL_CORE_SUSPENDED))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) ret = aat2870->write(aat2870, AAT2870_BLM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) (u8)aat2870_brightness(aat2870_bl, brightness));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) if (brightness == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) ret = aat2870_bl_disable(aat2870_bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) } else if (aat2870_bl->brightness == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) ret = aat2870_bl_enable(aat2870_bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) aat2870_bl->brightness = brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int aat2870_bl_check_fb(struct backlight_device *bd, struct fb_info *fi)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static const struct backlight_ops aat2870_bl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) .options = BL_CORE_SUSPENDRESUME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) .update_status = aat2870_bl_update_status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) .check_fb = aat2870_bl_check_fb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) static int aat2870_bl_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) struct aat2870_bl_platform_data *pdata = dev_get_platdata(&pdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) struct aat2870_bl_driver_data *aat2870_bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) struct backlight_device *bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) struct backlight_properties props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (!pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) dev_err(&pdev->dev, "No platform data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) ret = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (pdev->id != AAT2870_ID_BL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) dev_err(&pdev->dev, "Invalid device ID, %d\n", pdev->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) ret = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) aat2870_bl = devm_kzalloc(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) sizeof(struct aat2870_bl_driver_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (!aat2870_bl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) goto out;
^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) memset(&props, 0, sizeof(struct backlight_properties));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) props.type = BACKLIGHT_RAW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) bd = devm_backlight_device_register(&pdev->dev, "aat2870-backlight",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) &pdev->dev, aat2870_bl, &aat2870_bl_ops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) &props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (IS_ERR(bd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) dev_err(&pdev->dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) "Failed allocate memory for backlight device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) ret = PTR_ERR(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) goto out;
^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) aat2870_bl->pdev = pdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) platform_set_drvdata(pdev, aat2870_bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) aat2870_bl->bd = bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (pdata->channels > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) aat2870_bl->channels = pdata->channels;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) aat2870_bl->channels = AAT2870_BL_CH_ALL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (pdata->max_current > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) aat2870_bl->max_current = pdata->max_current;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) aat2870_bl->max_current = AAT2870_CURRENT_27_9;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) if (pdata->max_brightness > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) bd->props.max_brightness = pdata->max_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) bd->props.max_brightness = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) aat2870_bl->brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) bd->props.power = FB_BLANK_UNBLANK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) bd->props.brightness = bd->props.max_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) ret = aat2870_bl_update_status(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) dev_err(&pdev->dev, "Failed to initialize\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static int aat2870_bl_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct aat2870_bl_driver_data *aat2870_bl = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct backlight_device *bd = aat2870_bl->bd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) bd->props.power = FB_BLANK_POWERDOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) bd->props.brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) backlight_update_status(bd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static struct platform_driver aat2870_bl_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) .name = "aat2870-backlight",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) .probe = aat2870_bl_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) .remove = aat2870_bl_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static int __init aat2870_bl_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) return platform_driver_register(&aat2870_bl_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) subsys_initcall(aat2870_bl_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static void __exit aat2870_bl_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) platform_driver_unregister(&aat2870_bl_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) module_exit(aat2870_bl_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) MODULE_DESCRIPTION("AnalogicTech AAT2870 Backlight");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) MODULE_AUTHOR("Jin Park <jinyoungp@nvidia.com>");