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