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 backlight_power_set(struct pm860x_chip *chip, int port, 71 int on) 72 { 73 int ret = -EINVAL; 74 75 switch (port) { 76 case PM8606_BACKLIGHT1: 77 ret = on ? pm8606_osc_enable(chip, WLED1_DUTY) : 78 pm8606_osc_disable(chip, WLED1_DUTY); 79 break; 80 case PM8606_BACKLIGHT2: 81 ret = on ? pm8606_osc_enable(chip, WLED2_DUTY) : 82 pm8606_osc_disable(chip, WLED2_DUTY); 83 break; 84 case PM8606_BACKLIGHT3: 85 ret = on ? pm8606_osc_enable(chip, WLED3_DUTY) : 86 pm8606_osc_disable(chip, WLED3_DUTY); 87 break; 88 } 89 return ret; 90 } 91 92 static int pm860x_backlight_set(struct backlight_device *bl, int brightness) 93 { 94 struct pm860x_backlight_data *data = bl_get_data(bl); 95 struct pm860x_chip *chip = data->chip; 96 unsigned char value; 97 int ret; 98 99 if (brightness > MAX_BRIGHTNESS) 100 value = MAX_BRIGHTNESS; 101 else 102 value = brightness; 103 104 if (brightness) 105 backlight_power_set(chip, data->port, 1); 106 107 ret = pm860x_reg_write(data->i2c, wled_a(data->port), value); 108 if (ret < 0) 109 goto out; 110 111 if ((data->current_brightness == 0) && brightness) { 112 if (data->iset) { 113 ret = pm860x_set_bits(data->i2c, wled_idc(data->port), 114 CURRENT_BITMASK, data->iset); 115 if (ret < 0) 116 goto out; 117 } 118 if (data->pwm) { 119 ret = pm860x_set_bits(data->i2c, PM8606_PWM, 120 PM8606_PWM_FREQ_MASK, data->pwm); 121 if (ret < 0) 122 goto out; 123 } 124 if (brightness == MAX_BRIGHTNESS) { 125 /* set WLED_ON bit as 100% */ 126 ret = pm860x_set_bits(data->i2c, wled_b(data->port), 127 PM8606_WLED_ON, PM8606_WLED_ON); 128 } 129 } else { 130 if (brightness == MAX_BRIGHTNESS) { 131 /* set WLED_ON bit as 100% */ 132 ret = pm860x_set_bits(data->i2c, wled_b(data->port), 133 PM8606_WLED_ON, PM8606_WLED_ON); 134 } else { 135 /* clear WLED_ON bit since it's not 100% */ 136 ret = pm860x_set_bits(data->i2c, wled_b(data->port), 137 PM8606_WLED_ON, 0); 138 } 139 } 140 if (ret < 0) 141 goto out; 142 143 if (brightness == 0) 144 backlight_power_set(chip, data->port, 0); 145 146 dev_dbg(chip->dev, "set brightness %d\n", value); 147 data->current_brightness = value; 148 return 0; 149 out: 150 dev_dbg(chip->dev, "set brightness %d failure with return " 151 "value:%d\n", value, ret); 152 return ret; 153 } 154 155 static int pm860x_backlight_update_status(struct backlight_device *bl) 156 { 157 int brightness = bl->props.brightness; 158 159 if (bl->props.power != FB_BLANK_UNBLANK) 160 brightness = 0; 161 162 if (bl->props.fb_blank != FB_BLANK_UNBLANK) 163 brightness = 0; 164 165 if (bl->props.state & BL_CORE_SUSPENDED) 166 brightness = 0; 167 168 return pm860x_backlight_set(bl, brightness); 169 } 170 171 static int pm860x_backlight_get_brightness(struct backlight_device *bl) 172 { 173 struct pm860x_backlight_data *data = bl_get_data(bl); 174 struct pm860x_chip *chip = data->chip; 175 int ret; 176 177 ret = pm860x_reg_read(data->i2c, wled_a(data->port)); 178 if (ret < 0) 179 goto out; 180 data->current_brightness = ret; 181 dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness); 182 return data->current_brightness; 183 out: 184 return -EINVAL; 185 } 186 187 static const struct backlight_ops pm860x_backlight_ops = { 188 .options = BL_CORE_SUSPENDRESUME, 189 .update_status = pm860x_backlight_update_status, 190 .get_brightness = pm860x_backlight_get_brightness, 191 }; 192 193 static int pm860x_backlight_probe(struct platform_device *pdev) 194 { 195 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); 196 struct pm860x_backlight_pdata *pdata = NULL; 197 struct pm860x_backlight_data *data; 198 struct backlight_device *bl; 199 struct resource *res; 200 struct backlight_properties props; 201 char name[MFD_NAME_SIZE]; 202 int ret; 203 204 res = platform_get_resource(pdev, IORESOURCE_REG, 0); 205 if (res == NULL) { 206 dev_err(&pdev->dev, "No I/O resource!\n"); 207 return -EINVAL; 208 } 209 210 pdata = pdev->dev.platform_data; 211 if (pdata == NULL) { 212 dev_err(&pdev->dev, "platform data isn't assigned to " 213 "backlight\n"); 214 return -EINVAL; 215 } 216 217 data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_backlight_data), 218 GFP_KERNEL); 219 if (data == NULL) 220 return -ENOMEM; 221 strncpy(name, res->name, MFD_NAME_SIZE); 222 data->chip = chip; 223 data->i2c = (chip->id == CHIP_PM8606) ? chip->client \ 224 : chip->companion; 225 data->current_brightness = MAX_BRIGHTNESS; 226 data->pwm = pdata->pwm; 227 data->iset = pdata->iset; 228 data->port = pdata->flags; 229 if (data->port < 0) { 230 dev_err(&pdev->dev, "wrong platform data is assigned"); 231 kfree(data); 232 return -EINVAL; 233 } 234 235 memset(&props, 0, sizeof(struct backlight_properties)); 236 props.type = BACKLIGHT_RAW; 237 props.max_brightness = MAX_BRIGHTNESS; 238 bl = backlight_device_register(name, &pdev->dev, data, 239 &pm860x_backlight_ops, &props); 240 if (IS_ERR(bl)) { 241 dev_err(&pdev->dev, "failed to register backlight\n"); 242 return PTR_ERR(bl); 243 } 244 bl->props.brightness = MAX_BRIGHTNESS; 245 246 platform_set_drvdata(pdev, bl); 247 248 /* read current backlight */ 249 ret = pm860x_backlight_get_brightness(bl); 250 if (ret < 0) 251 goto out; 252 253 backlight_update_status(bl); 254 return 0; 255 out: 256 backlight_device_unregister(bl); 257 return ret; 258 } 259 260 static int pm860x_backlight_remove(struct platform_device *pdev) 261 { 262 struct backlight_device *bl = platform_get_drvdata(pdev); 263 264 backlight_device_unregister(bl); 265 return 0; 266 } 267 268 static struct platform_driver pm860x_backlight_driver = { 269 .driver = { 270 .name = "88pm860x-backlight", 271 .owner = THIS_MODULE, 272 }, 273 .probe = pm860x_backlight_probe, 274 .remove = pm860x_backlight_remove, 275 }; 276 277 module_platform_driver(pm860x_backlight_driver); 278 279 MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606"); 280 MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); 281 MODULE_LICENSE("GPL"); 282 MODULE_ALIAS("platform:88pm860x-backlight"); 283