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