1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * TI LP8788 MFD - backlight driver 4 * 5 * Copyright 2012 Texas Instruments 6 * 7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com> 8 */ 9 10 #include <linux/backlight.h> 11 #include <linux/err.h> 12 #include <linux/mfd/lp8788.h> 13 #include <linux/module.h> 14 #include <linux/platform_device.h> 15 #include <linux/slab.h> 16 17 /* Register address */ 18 #define LP8788_BL_CONFIG 0x96 19 #define LP8788_BL_EN BIT(0) 20 #define LP8788_BL_PWM_INPUT_EN BIT(5) 21 #define LP8788_BL_FULLSCALE_SHIFT 2 22 #define LP8788_BL_DIM_MODE_SHIFT 1 23 #define LP8788_BL_PWM_POLARITY_SHIFT 6 24 25 #define LP8788_BL_BRIGHTNESS 0x97 26 27 #define LP8788_BL_RAMP 0x98 28 #define LP8788_BL_RAMP_RISE_SHIFT 4 29 30 #define MAX_BRIGHTNESS 127 31 #define DEFAULT_BL_NAME "lcd-backlight" 32 33 struct lp8788_bl { 34 struct lp8788 *lp; 35 struct backlight_device *bl_dev; 36 }; 37 38 static int lp8788_backlight_configure(struct lp8788_bl *bl) 39 { 40 int ret; 41 u8 val; 42 43 /* Brightness ramp up/down */ 44 val = (LP8788_RAMP_8192us << LP8788_BL_RAMP_RISE_SHIFT) | LP8788_RAMP_8192us; 45 ret = lp8788_write_byte(bl->lp, LP8788_BL_RAMP, val); 46 if (ret) 47 return ret; 48 49 /* Fullscale current setting */ 50 val = (LP8788_FULLSCALE_1900uA << LP8788_BL_FULLSCALE_SHIFT) | 51 (LP8788_DIM_EXPONENTIAL << LP8788_BL_DIM_MODE_SHIFT); 52 53 /* Brightness control mode */ 54 val |= LP8788_BL_EN; 55 56 return lp8788_write_byte(bl->lp, LP8788_BL_CONFIG, val); 57 } 58 59 static int lp8788_bl_update_status(struct backlight_device *bl_dev) 60 { 61 struct lp8788_bl *bl = bl_get_data(bl_dev); 62 63 if (bl_dev->props.state & BL_CORE_SUSPENDED) 64 bl_dev->props.brightness = 0; 65 66 lp8788_write_byte(bl->lp, LP8788_BL_BRIGHTNESS, bl_dev->props.brightness); 67 68 return 0; 69 } 70 71 static const struct backlight_ops lp8788_bl_ops = { 72 .options = BL_CORE_SUSPENDRESUME, 73 .update_status = lp8788_bl_update_status, 74 }; 75 76 static int lp8788_backlight_register(struct lp8788_bl *bl) 77 { 78 struct backlight_device *bl_dev; 79 struct backlight_properties props; 80 81 memset(&props, 0, sizeof(struct backlight_properties)); 82 props.type = BACKLIGHT_PLATFORM; 83 props.max_brightness = MAX_BRIGHTNESS; 84 85 /* Initial brightness */ 86 props.brightness = 0; 87 88 /* Backlight device name */ 89 bl_dev = backlight_device_register(DEFAULT_BL_NAME, bl->lp->dev, bl, 90 &lp8788_bl_ops, &props); 91 if (IS_ERR(bl_dev)) 92 return PTR_ERR(bl_dev); 93 94 bl->bl_dev = bl_dev; 95 96 return 0; 97 } 98 99 static void lp8788_backlight_unregister(struct lp8788_bl *bl) 100 { 101 struct backlight_device *bl_dev = bl->bl_dev; 102 103 backlight_device_unregister(bl_dev); 104 } 105 106 static ssize_t lp8788_get_bl_ctl_mode(struct device *dev, 107 struct device_attribute *attr, char *buf) 108 { 109 const char *strmode = "Register based"; 110 111 return scnprintf(buf, PAGE_SIZE, "%s\n", strmode); 112 } 113 114 static DEVICE_ATTR(bl_ctl_mode, S_IRUGO, lp8788_get_bl_ctl_mode, NULL); 115 116 static struct attribute *lp8788_attributes[] = { 117 &dev_attr_bl_ctl_mode.attr, 118 NULL, 119 }; 120 121 static const struct attribute_group lp8788_attr_group = { 122 .attrs = lp8788_attributes, 123 }; 124 125 static int lp8788_backlight_probe(struct platform_device *pdev) 126 { 127 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent); 128 struct lp8788_bl *bl; 129 int ret; 130 131 bl = devm_kzalloc(lp->dev, sizeof(struct lp8788_bl), GFP_KERNEL); 132 if (!bl) 133 return -ENOMEM; 134 135 bl->lp = lp; 136 137 platform_set_drvdata(pdev, bl); 138 139 ret = lp8788_backlight_configure(bl); 140 if (ret) { 141 dev_err(lp->dev, "backlight config err: %d\n", ret); 142 goto err_dev; 143 } 144 145 ret = lp8788_backlight_register(bl); 146 if (ret) { 147 dev_err(lp->dev, "register backlight err: %d\n", ret); 148 goto err_dev; 149 } 150 151 ret = sysfs_create_group(&pdev->dev.kobj, &lp8788_attr_group); 152 if (ret) { 153 dev_err(lp->dev, "register sysfs err: %d\n", ret); 154 goto err_sysfs; 155 } 156 157 backlight_update_status(bl->bl_dev); 158 159 return 0; 160 161 err_sysfs: 162 lp8788_backlight_unregister(bl); 163 err_dev: 164 return ret; 165 } 166 167 static void lp8788_backlight_remove(struct platform_device *pdev) 168 { 169 struct lp8788_bl *bl = platform_get_drvdata(pdev); 170 struct backlight_device *bl_dev = bl->bl_dev; 171 172 bl_dev->props.brightness = 0; 173 backlight_update_status(bl_dev); 174 sysfs_remove_group(&pdev->dev.kobj, &lp8788_attr_group); 175 lp8788_backlight_unregister(bl); 176 } 177 178 static struct platform_driver lp8788_bl_driver = { 179 .probe = lp8788_backlight_probe, 180 .remove_new = lp8788_backlight_remove, 181 .driver = { 182 .name = LP8788_DEV_BACKLIGHT, 183 }, 184 }; 185 module_platform_driver(lp8788_bl_driver); 186 187 MODULE_DESCRIPTION("Texas Instruments LP8788 Backlight Driver"); 188 MODULE_AUTHOR("Milo Kim"); 189 MODULE_LICENSE("GPL"); 190 MODULE_ALIAS("platform:lp8788-backlight"); 191