^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) * TI LP8788 MFD - backlight driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2012 Texas Instruments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/backlight.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/mfd/lp8788.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/pwm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) /* Register address */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define LP8788_BL_CONFIG 0x96
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define LP8788_BL_EN BIT(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #define LP8788_BL_PWM_INPUT_EN BIT(5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define LP8788_BL_FULLSCALE_SHIFT 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define LP8788_BL_DIM_MODE_SHIFT 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #define LP8788_BL_PWM_POLARITY_SHIFT 6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define LP8788_BL_BRIGHTNESS 0x97
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define LP8788_BL_RAMP 0x98
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define LP8788_BL_RAMP_RISE_SHIFT 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define MAX_BRIGHTNESS 127
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #define DEFAULT_BL_NAME "lcd-backlight"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct lp8788_bl_config {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) enum lp8788_bl_ctrl_mode bl_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) enum lp8788_bl_dim_mode dim_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) enum lp8788_bl_full_scale_current full_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) enum lp8788_bl_ramp_step rise_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) enum lp8788_bl_ramp_step fall_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) enum pwm_polarity pwm_pol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct lp8788_bl {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) struct lp8788 *lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct backlight_device *bl_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) struct lp8788_backlight_platform_data *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) enum lp8788_bl_ctrl_mode mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) struct pwm_device *pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static struct lp8788_bl_config default_bl_config = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) .bl_mode = LP8788_BL_REGISTER_ONLY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) .dim_mode = LP8788_DIM_EXPONENTIAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) .full_scale = LP8788_FULLSCALE_1900uA,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) .rise_time = LP8788_RAMP_8192us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) .fall_time = LP8788_RAMP_8192us,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) .pwm_pol = PWM_POLARITY_NORMAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static inline bool is_brightness_ctrl_by_pwm(enum lp8788_bl_ctrl_mode mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) return mode == LP8788_BL_COMB_PWM_BASED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) static inline bool is_brightness_ctrl_by_register(enum lp8788_bl_ctrl_mode mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return mode == LP8788_BL_REGISTER_ONLY ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) mode == LP8788_BL_COMB_REGISTER_BASED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int lp8788_backlight_configure(struct lp8788_bl *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct lp8788_backlight_platform_data *pdata = bl->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct lp8788_bl_config *cfg = &default_bl_config;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) u8 val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * Update chip configuration if platform data exists,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * otherwise use the default settings.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (pdata) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) cfg->bl_mode = pdata->bl_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) cfg->dim_mode = pdata->dim_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) cfg->full_scale = pdata->full_scale;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) cfg->rise_time = pdata->rise_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) cfg->fall_time = pdata->fall_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) cfg->pwm_pol = pdata->pwm_pol;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* Brightness ramp up/down */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) val = (cfg->rise_time << LP8788_BL_RAMP_RISE_SHIFT) | cfg->fall_time;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ret = lp8788_write_byte(bl->lp, LP8788_BL_RAMP, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) /* Fullscale current setting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) val = (cfg->full_scale << LP8788_BL_FULLSCALE_SHIFT) |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) (cfg->dim_mode << LP8788_BL_DIM_MODE_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* Brightness control mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) switch (cfg->bl_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) case LP8788_BL_REGISTER_ONLY:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) val |= LP8788_BL_EN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) case LP8788_BL_COMB_PWM_BASED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) case LP8788_BL_COMB_REGISTER_BASED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) val |= LP8788_BL_EN | LP8788_BL_PWM_INPUT_EN |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) (cfg->pwm_pol << LP8788_BL_PWM_POLARITY_SHIFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) dev_err(bl->lp->dev, "invalid mode: %d\n", cfg->bl_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) bl->mode = cfg->bl_mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return lp8788_write_byte(bl->lp, LP8788_BL_CONFIG, val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) static void lp8788_pwm_ctrl(struct lp8788_bl *bl, int br, int max_br)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) unsigned int period;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) unsigned int duty;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct device *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct pwm_device *pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (!bl->pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) period = bl->pdata->period_ns;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) duty = br * period / max_br;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) dev = bl->lp->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* request PWM device with the consumer name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) if (!bl->pwm) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) pwm = devm_pwm_get(dev, LP8788_DEV_BACKLIGHT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) if (IS_ERR(pwm)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) dev_err(dev, "can not get PWM device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) return;
^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) bl->pwm = pwm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * FIXME: pwm_apply_args() should be removed when switching to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * the atomic PWM API.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) pwm_apply_args(pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) pwm_config(bl->pwm, duty, period);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) if (duty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) pwm_enable(bl->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) pwm_disable(bl->pwm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) static int lp8788_bl_update_status(struct backlight_device *bl_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) struct lp8788_bl *bl = bl_get_data(bl_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) enum lp8788_bl_ctrl_mode mode = bl->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (bl_dev->props.state & BL_CORE_SUSPENDED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) bl_dev->props.brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) if (is_brightness_ctrl_by_pwm(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) int brt = bl_dev->props.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) int max = bl_dev->props.max_brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) lp8788_pwm_ctrl(bl, brt, max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) } else if (is_brightness_ctrl_by_register(mode)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) u8 brt = bl_dev->props.brightness;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) lp8788_write_byte(bl->lp, LP8788_BL_BRIGHTNESS, brt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) static const struct backlight_ops lp8788_bl_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) .options = BL_CORE_SUSPENDRESUME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) .update_status = lp8788_bl_update_status,
^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 lp8788_backlight_register(struct lp8788_bl *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) struct backlight_device *bl_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) struct backlight_properties props;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct lp8788_backlight_platform_data *pdata = bl->pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int init_brt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) props.type = BACKLIGHT_PLATFORM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) props.max_brightness = MAX_BRIGHTNESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) /* Initial brightness */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) if (pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) init_brt = min_t(int, pdata->initial_brightness,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) props.max_brightness);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) init_brt = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) props.brightness = init_brt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) /* Backlight device name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (!pdata || !pdata->name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) name = DEFAULT_BL_NAME;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) name = pdata->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) bl_dev = backlight_device_register(name, bl->lp->dev, bl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) &lp8788_bl_ops, &props);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) if (IS_ERR(bl_dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return PTR_ERR(bl_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) bl->bl_dev = bl_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static void lp8788_backlight_unregister(struct lp8788_bl *bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) struct backlight_device *bl_dev = bl->bl_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) backlight_device_unregister(bl_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) static ssize_t lp8788_get_bl_ctl_mode(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) struct lp8788_bl *bl = dev_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) enum lp8788_bl_ctrl_mode mode = bl->mode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) char *strmode;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (is_brightness_ctrl_by_pwm(mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) strmode = "PWM based";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) else if (is_brightness_ctrl_by_register(mode))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) strmode = "Register based";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) strmode = "Invalid mode";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) return scnprintf(buf, PAGE_SIZE, "%s\n", strmode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp8788_get_bl_ctl_mode, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) static struct attribute *lp8788_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) &dev_attr_bl_ctl_mode.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) static const struct attribute_group lp8788_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) .attrs = lp8788_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) static int lp8788_backlight_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) struct lp8788_bl *bl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) bl = devm_kzalloc(lp->dev, sizeof(struct lp8788_bl), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (!bl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) bl->lp = lp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) if (lp->pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) bl->pdata = lp->pdata->bl_pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) platform_set_drvdata(pdev, bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) ret = lp8788_backlight_configure(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) dev_err(lp->dev, "backlight config err: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) goto err_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) ret = lp8788_backlight_register(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) dev_err(lp->dev, "register backlight err: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) goto err_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) ret = sysfs_create_group(&pdev->dev.kobj, &lp8788_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) dev_err(lp->dev, "register sysfs err: %d\n", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) goto err_sysfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) backlight_update_status(bl->bl_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) err_sysfs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) lp8788_backlight_unregister(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) err_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) static int lp8788_backlight_remove(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) struct lp8788_bl *bl = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) struct backlight_device *bl_dev = bl->bl_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) bl_dev->props.brightness = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) backlight_update_status(bl_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) sysfs_remove_group(&pdev->dev.kobj, &lp8788_attr_group);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) lp8788_backlight_unregister(bl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) static struct platform_driver lp8788_bl_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) .probe = lp8788_backlight_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) .remove = lp8788_backlight_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) .name = LP8788_DEV_BACKLIGHT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) module_platform_driver(lp8788_bl_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) MODULE_DESCRIPTION("Texas Instruments LP8788 Backlight Driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) MODULE_AUTHOR("Milo Kim");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) MODULE_ALIAS("platform:lp8788-backlight");