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> |
Haojian Zhuang | adb7048 | 2011-03-07 23:43:09 +0800 | [diff] [blame] | 15 | #include <linux/slab.h> |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 16 | #include <linux/fb.h> |
| 17 | #include <linux/i2c.h> |
| 18 | #include <linux/backlight.h> |
| 19 | #include <linux/mfd/88pm860x.h> |
Paul Gortmaker | 355b200 | 2011-07-03 16:17:28 -0400 | [diff] [blame] | 20 | #include <linux/module.h> |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 21 | |
| 22 | #define MAX_BRIGHTNESS (0xFF) |
| 23 | #define MIN_BRIGHTNESS (0) |
| 24 | |
Randy Dunlap | 2550326 | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 25 | #define CURRENT_BITMASK (0x1F << 1) |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 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 | |
Jett.Zhou | 1efc158 | 2012-02-28 10:59:08 +0800 | [diff] [blame] | 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 | |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 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 | |
Jett.Zhou | 1efc158 | 2012-02-28 10:59:08 +0800 | [diff] [blame] | 104 | if (brightness) |
| 105 | backlight_power_set(chip, data->port, 1); |
| 106 | |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 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), |
Randy Dunlap | 2550326 | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 114 | CURRENT_BITMASK, data->iset); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 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 | |
Jett.Zhou | 1efc158 | 2012-02-28 10:59:08 +0800 | [diff] [blame] | 143 | if (brightness == 0) |
| 144 | backlight_power_set(chip, data->port, 0); |
| 145 | |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 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 | |
Lionel Debroux | acc2472 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 187 | static const struct backlight_ops pm860x_backlight_ops = { |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 188 | .options = BL_CORE_SUSPENDRESUME, |
| 189 | .update_status = pm860x_backlight_update_status, |
| 190 | .get_brightness = pm860x_backlight_get_brightness, |
| 191 | }; |
| 192 | |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 193 | static int pm860x_backlight_probe(struct platform_device *pdev) |
| 194 | { |
| 195 | struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 196 | struct pm860x_backlight_pdata *pdata = NULL; |
| 197 | struct pm860x_backlight_data *data; |
| 198 | struct backlight_device *bl; |
| 199 | struct resource *res; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 200 | struct backlight_properties props; |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 201 | char name[MFD_NAME_SIZE]; |
| 202 | int ret; |
| 203 | |
| 204 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 205 | if (res == NULL) { |
| 206 | dev_err(&pdev->dev, "No I/O resource!\n"); |
| 207 | return -EINVAL; |
| 208 | } |
| 209 | |
Samuel Ortiz | 07259a7 | 2011-04-06 16:26:56 +0200 | [diff] [blame] | 210 | pdata = pdev->dev.platform_data; |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 211 | if (pdata == NULL) { |
| 212 | dev_err(&pdev->dev, "platform data isn't assigned to " |
| 213 | "backlight\n"); |
| 214 | return -EINVAL; |
| 215 | } |
| 216 | |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 217 | data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_backlight_data), |
| 218 | GFP_KERNEL); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 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; |
Haojian Zhuang | adb7048 | 2011-03-07 23:43:09 +0800 | [diff] [blame] | 228 | data->port = pdata->flags; |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 229 | if (data->port < 0) { |
| 230 | dev_err(&pdev->dev, "wrong platform data is assigned"); |
| 231 | return -EINVAL; |
| 232 | } |
| 233 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 234 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 235 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 236 | props.max_brightness = MAX_BRIGHTNESS; |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 237 | bl = backlight_device_register(name, &pdev->dev, data, |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 238 | &pm860x_backlight_ops, &props); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 239 | if (IS_ERR(bl)) { |
| 240 | dev_err(&pdev->dev, "failed to register backlight\n"); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 241 | return PTR_ERR(bl); |
| 242 | } |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 243 | bl->props.brightness = MAX_BRIGHTNESS; |
| 244 | |
| 245 | platform_set_drvdata(pdev, bl); |
| 246 | |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 247 | /* read current backlight */ |
| 248 | ret = pm860x_backlight_get_brightness(bl); |
| 249 | if (ret < 0) |
| 250 | goto out; |
| 251 | |
| 252 | backlight_update_status(bl); |
| 253 | return 0; |
| 254 | out: |
Axel Lin | 1f97699 | 2010-05-10 09:29:58 +0800 | [diff] [blame] | 255 | backlight_device_unregister(bl); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | static int pm860x_backlight_remove(struct platform_device *pdev) |
| 260 | { |
| 261 | struct backlight_device *bl = platform_get_drvdata(pdev); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 262 | |
| 263 | backlight_device_unregister(bl); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static struct platform_driver pm860x_backlight_driver = { |
| 268 | .driver = { |
| 269 | .name = "88pm860x-backlight", |
| 270 | .owner = THIS_MODULE, |
| 271 | }, |
| 272 | .probe = pm860x_backlight_probe, |
| 273 | .remove = pm860x_backlight_remove, |
| 274 | }; |
| 275 | |
Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 276 | module_platform_driver(pm860x_backlight_driver); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 277 | |
| 278 | MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606"); |
| 279 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); |
| 280 | MODULE_LICENSE("GPL"); |
| 281 | MODULE_ALIAS("platform:88pm860x-backlight"); |