1 /* 2 * Backlight driver for Marvell Semiconductor 88PM8606 3 * 4 * Copyright (C) 2009 Marvell International Ltd. 5 * Haojian Zhuang <haojian.zhuang@marvell.com> 6 * 7 * This program is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License version 2 as 9 * published by the Free Software Foundation. 10 */ 11 12 #include <linux/init.h> 13 #include <linux/kernel.h> 14 #include <linux/platform_device.h> 15 #include <linux/slab.h> 16 #include <linux/fb.h> 17 #include <linux/i2c.h> 18 #include <linux/backlight.h> 19 #include <linux/mfd/88pm860x.h> 20 #include <linux/module.h> 21 22 #define MAX_BRIGHTNESS (0xFF) 23 #define MIN_BRIGHTNESS (0) 24 25 #define CURRENT_BITMASK (0x1F << 1) 26 27 struct pm860x_backlight_data { 28 struct pm860x_chip *chip; 29 struct i2c_client *i2c; 30 int current_brightness; 31 int port; 32 int pwm; 33 int iset; 34 }; 35 36 static inline int wled_a(int port) 37 { 38 int ret; 39 40 ret = ((port - PM8606_BACKLIGHT1) << 1) + 2; 41 return ret; 42 } 43 44 static inline int wled_b(int port) 45 { 46 int ret; 47 48 ret = ((port - PM8606_BACKLIGHT1) << 1) + 3; 49 return ret; 50 } 51 52 /* WLED2 & WLED3 share the same IDC */ 53 static inline int wled_idc(int port) 54 { 55 int ret; 56 57 switch (port) { 58 case PM8606_BACKLIGHT1: 59 case PM8606_BACKLIGHT2: 60 ret = ((port - PM8606_BACKLIGHT1) << 1) + 3; 61 break; 62 case PM8606_BACKLIGHT3: 63 default: 64 ret = ((port - PM8606_BACKLIGHT2) << 1) + 3; 65 break; 66 } 67 return ret; 68 } 69 70 static int pm860x_backlight_set(struct backlight_device *bl, int brightness) 71 { 72 struct pm860x_backlight_data *data = bl_get_data(bl); 73 struct pm860x_chip *chip = data->chip; 74 unsigned char value; 75 int ret; 76 77 if (brightness > MAX_BRIGHTNESS) 78 value = MAX_BRIGHTNESS; 79 else 80 value = brightness; 81 82 ret = pm860x_reg_write(data->i2c, wled_a(data->port), value); 83 if (ret < 0) 84 goto out; 85 86 if ((data->current_brightness == 0) && brightness) { 87 if (data->iset) { 88 ret = pm860x_set_bits(data->i2c, wled_idc(data->port), 89 CURRENT_BITMASK, data->iset); 90 if (ret < 0) 91 goto out; 92 } 93 if (data->pwm) { 94 ret = pm860x_set_bits(data->i2c, PM8606_PWM, 95 PM8606_PWM_FREQ_MASK, data->pwm); 96 if (ret < 0) 97 goto out; 98 } 99 if (brightness == MAX_BRIGHTNESS) { 100 /* set WLED_ON bit as 100% */ 101 ret = pm860x_set_bits(data->i2c, wled_b(data->port), 102 PM8606_WLED_ON, PM8606_WLED_ON); 103 } 104 } else { 105 if (brightness == MAX_BRIGHTNESS) { 106 /* set WLED_ON bit as 100% */ 107 ret = pm860x_set_bits(data->i2c, wled_b(data->port), 108 PM8606_WLED_ON, PM8606_WLED_ON); 109 } else { 110 /* clear WLED_ON bit since it's not 100% */ 111 ret = pm860x_set_bits(data->i2c, wled_b(data->port), 112 PM8606_WLED_ON, 0); 113 } 114 } 115 if (ret < 0) 116 goto out; 117 118 dev_dbg(chip->dev, "set brightness %d\n", value); 119 data->current_brightness = value; 120 return 0; 121 out: 122 dev_dbg(chip->dev, "set brightness %d failure with return " 123 "value:%d\n", value, ret); 124 return ret; 125 } 126 127 static int pm860x_backlight_update_status(struct backlight_device *bl) 128 { 129 int brightness = bl->props.brightness; 130 131 if (bl->props.power != FB_BLANK_UNBLANK) 132 brightness = 0; 133 134 if (bl->props.fb_blank != FB_BLANK_UNBLANK) 135 brightness = 0; 136 137 if (bl->props.state & BL_CORE_SUSPENDED) 138 brightness = 0; 139 140 return pm860x_backlight_set(bl, brightness); 141 } 142 143 static int pm860x_backlight_get_brightness(struct backlight_device *bl) 144 { 145 struct pm860x_backlight_data *data = bl_get_data(bl); 146 struct pm860x_chip *chip = data->chip; 147 int ret; 148 149 ret = pm860x_reg_read(data->i2c, wled_a(data->port)); 150 if (ret < 0) 151 goto out; 152 data->current_brightness = ret; 153 dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness); 154 return data->current_brightness; 155 out: 156 return -EINVAL; 157 } 158 159 static const struct backlight_ops pm860x_backlight_ops = { 160 .options = BL_CORE_SUSPENDRESUME, 161 .update_status = pm860x_backlight_update_status, 162 .get_brightness = pm860x_backlight_get_brightness, 163 }; 164 165 static int pm860x_backlight_probe(struct platform_device *pdev) 166 { 167 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); 168 struct pm860x_backlight_pdata *pdata = NULL; 169 struct pm860x_backlight_data *data; 170 struct backlight_device *bl; 171 struct resource *res; 172 struct backlight_properties props; 173 unsigned char value; 174 char name[MFD_NAME_SIZE]; 175 int ret; 176 177 res = platform_get_resource(pdev, IORESOURCE_IO, 0); 178 if (res == NULL) { 179 dev_err(&pdev->dev, "No I/O resource!\n"); 180 return -EINVAL; 181 } 182 183 pdata = pdev->dev.platform_data; 184 if (pdata == NULL) { 185 dev_err(&pdev->dev, "platform data isn't assigned to " 186 "backlight\n"); 187 return -EINVAL; 188 } 189 190 data = kzalloc(sizeof(struct pm860x_backlight_data), GFP_KERNEL); 191 if (data == NULL) 192 return -ENOMEM; 193 strncpy(name, res->name, MFD_NAME_SIZE); 194 data->chip = chip; 195 data->i2c = (chip->id == CHIP_PM8606) ? chip->client \ 196 : chip->companion; 197 data->current_brightness = MAX_BRIGHTNESS; 198 data->pwm = pdata->pwm; 199 data->iset = pdata->iset; 200 data->port = pdata->flags; 201 if (data->port < 0) { 202 dev_err(&pdev->dev, "wrong platform data is assigned"); 203 kfree(data); 204 return -EINVAL; 205 } 206 207 memset(&props, 0, sizeof(struct backlight_properties)); 208 props.type = BACKLIGHT_RAW; 209 props.max_brightness = MAX_BRIGHTNESS; 210 bl = backlight_device_register(name, &pdev->dev, data, 211 &pm860x_backlight_ops, &props); 212 if (IS_ERR(bl)) { 213 dev_err(&pdev->dev, "failed to register backlight\n"); 214 kfree(data); 215 return PTR_ERR(bl); 216 } 217 bl->props.brightness = MAX_BRIGHTNESS; 218 219 platform_set_drvdata(pdev, bl); 220 221 /* Enable reference VSYS */ 222 ret = pm860x_reg_read(data->i2c, PM8606_VSYS); 223 if (ret < 0) 224 goto out; 225 if ((ret & PM8606_VSYS_EN) == 0) { 226 value = ret | PM8606_VSYS_EN; 227 ret = pm860x_reg_write(data->i2c, PM8606_VSYS, value); 228 if (ret < 0) 229 goto out; 230 } 231 /* Enable reference OSC */ 232 ret = pm860x_reg_read(data->i2c, PM8606_MISC); 233 if (ret < 0) 234 goto out; 235 if ((ret & PM8606_MISC_OSC_EN) == 0) { 236 value = ret | PM8606_MISC_OSC_EN; 237 ret = pm860x_reg_write(data->i2c, PM8606_MISC, value); 238 if (ret < 0) 239 goto out; 240 } 241 /* read current backlight */ 242 ret = pm860x_backlight_get_brightness(bl); 243 if (ret < 0) 244 goto out; 245 246 backlight_update_status(bl); 247 return 0; 248 out: 249 backlight_device_unregister(bl); 250 kfree(data); 251 return ret; 252 } 253 254 static int pm860x_backlight_remove(struct platform_device *pdev) 255 { 256 struct backlight_device *bl = platform_get_drvdata(pdev); 257 struct pm860x_backlight_data *data = bl_get_data(bl); 258 259 backlight_device_unregister(bl); 260 kfree(data); 261 return 0; 262 } 263 264 static struct platform_driver pm860x_backlight_driver = { 265 .driver = { 266 .name = "88pm860x-backlight", 267 .owner = THIS_MODULE, 268 }, 269 .probe = pm860x_backlight_probe, 270 .remove = pm860x_backlight_remove, 271 }; 272 273 static int __init pm860x_backlight_init(void) 274 { 275 return platform_driver_register(&pm860x_backlight_driver); 276 } 277 module_init(pm860x_backlight_init); 278 279 static void __exit pm860x_backlight_exit(void) 280 { 281 platform_driver_unregister(&pm860x_backlight_driver); 282 } 283 module_exit(pm860x_backlight_exit); 284 285 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606"); 286 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); 287 MODULE_LICENSE("GPL"); 288 MODULE_ALIAS("platform:88pm860x-backlight"); 289