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> |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 14 | #include <linux/of.h> |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 15 | #include <linux/platform_device.h> |
Haojian Zhuang | adb7048 | 2011-03-07 23:43:09 +0800 | [diff] [blame] | 16 | #include <linux/slab.h> |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 17 | #include <linux/fb.h> |
| 18 | #include <linux/i2c.h> |
| 19 | #include <linux/backlight.h> |
| 20 | #include <linux/mfd/88pm860x.h> |
Paul Gortmaker | 355b200 | 2011-07-03 16:17:28 -0400 | [diff] [blame] | 21 | #include <linux/module.h> |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 22 | |
| 23 | #define MAX_BRIGHTNESS (0xFF) |
| 24 | #define MIN_BRIGHTNESS (0) |
| 25 | |
Randy Dunlap | 2550326 | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 26 | #define CURRENT_BITMASK (0x1F << 1) |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 27 | |
| 28 | struct pm860x_backlight_data { |
| 29 | struct pm860x_chip *chip; |
| 30 | struct i2c_client *i2c; |
| 31 | int current_brightness; |
| 32 | int port; |
| 33 | int pwm; |
| 34 | int iset; |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 35 | int reg_duty_cycle; |
| 36 | int reg_always_on; |
| 37 | int reg_current; |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 38 | }; |
| 39 | |
Jett.Zhou | 1efc158 | 2012-02-28 10:59:08 +0800 | [diff] [blame] | 40 | static int backlight_power_set(struct pm860x_chip *chip, int port, |
| 41 | int on) |
| 42 | { |
| 43 | int ret = -EINVAL; |
| 44 | |
| 45 | switch (port) { |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 46 | case 0: |
Jett.Zhou | 1efc158 | 2012-02-28 10:59:08 +0800 | [diff] [blame] | 47 | ret = on ? pm8606_osc_enable(chip, WLED1_DUTY) : |
| 48 | pm8606_osc_disable(chip, WLED1_DUTY); |
| 49 | break; |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 50 | case 1: |
Jett.Zhou | 1efc158 | 2012-02-28 10:59:08 +0800 | [diff] [blame] | 51 | ret = on ? pm8606_osc_enable(chip, WLED2_DUTY) : |
| 52 | pm8606_osc_disable(chip, WLED2_DUTY); |
| 53 | break; |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 54 | case 2: |
Jett.Zhou | 1efc158 | 2012-02-28 10:59:08 +0800 | [diff] [blame] | 55 | ret = on ? pm8606_osc_enable(chip, WLED3_DUTY) : |
| 56 | pm8606_osc_disable(chip, WLED3_DUTY); |
| 57 | break; |
| 58 | } |
| 59 | return ret; |
| 60 | } |
| 61 | |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 62 | static int pm860x_backlight_set(struct backlight_device *bl, int brightness) |
| 63 | { |
| 64 | struct pm860x_backlight_data *data = bl_get_data(bl); |
| 65 | struct pm860x_chip *chip = data->chip; |
| 66 | unsigned char value; |
| 67 | int ret; |
| 68 | |
| 69 | if (brightness > MAX_BRIGHTNESS) |
| 70 | value = MAX_BRIGHTNESS; |
| 71 | else |
| 72 | value = brightness; |
| 73 | |
Jett.Zhou | 1efc158 | 2012-02-28 10:59:08 +0800 | [diff] [blame] | 74 | if (brightness) |
| 75 | backlight_power_set(chip, data->port, 1); |
| 76 | |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 77 | ret = pm860x_reg_write(data->i2c, data->reg_duty_cycle, value); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 78 | if (ret < 0) |
| 79 | goto out; |
| 80 | |
| 81 | if ((data->current_brightness == 0) && brightness) { |
| 82 | if (data->iset) { |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 83 | ret = pm860x_set_bits(data->i2c, data->reg_current, |
Randy Dunlap | 2550326 | 2011-01-20 14:44:31 -0800 | [diff] [blame] | 84 | CURRENT_BITMASK, data->iset); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 85 | if (ret < 0) |
| 86 | goto out; |
| 87 | } |
| 88 | if (data->pwm) { |
| 89 | ret = pm860x_set_bits(data->i2c, PM8606_PWM, |
| 90 | PM8606_PWM_FREQ_MASK, data->pwm); |
| 91 | if (ret < 0) |
| 92 | goto out; |
| 93 | } |
| 94 | if (brightness == MAX_BRIGHTNESS) { |
| 95 | /* set WLED_ON bit as 100% */ |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 96 | ret = pm860x_set_bits(data->i2c, data->reg_always_on, |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 97 | PM8606_WLED_ON, PM8606_WLED_ON); |
| 98 | } |
| 99 | } else { |
| 100 | if (brightness == MAX_BRIGHTNESS) { |
| 101 | /* set WLED_ON bit as 100% */ |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 102 | ret = pm860x_set_bits(data->i2c, data->reg_always_on, |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 103 | PM8606_WLED_ON, PM8606_WLED_ON); |
| 104 | } else { |
| 105 | /* clear WLED_ON bit since it's not 100% */ |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 106 | ret = pm860x_set_bits(data->i2c, data->reg_always_on, |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 107 | PM8606_WLED_ON, 0); |
| 108 | } |
| 109 | } |
| 110 | if (ret < 0) |
| 111 | goto out; |
| 112 | |
Jett.Zhou | 1efc158 | 2012-02-28 10:59:08 +0800 | [diff] [blame] | 113 | if (brightness == 0) |
| 114 | backlight_power_set(chip, data->port, 0); |
| 115 | |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 116 | dev_dbg(chip->dev, "set brightness %d\n", value); |
| 117 | data->current_brightness = value; |
| 118 | return 0; |
| 119 | out: |
Jingoo Han | 20c5a93 | 2012-12-17 16:00:11 -0800 | [diff] [blame] | 120 | dev_dbg(chip->dev, "set brightness %d failure with return value: %d\n", |
| 121 | value, ret); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 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 | |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 147 | ret = pm860x_reg_read(data->i2c, data->reg_duty_cycle); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 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 | |
Lionel Debroux | acc2472 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 157 | static const struct backlight_ops pm860x_backlight_ops = { |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 158 | .options = BL_CORE_SUSPENDRESUME, |
| 159 | .update_status = pm860x_backlight_update_status, |
| 160 | .get_brightness = pm860x_backlight_get_brightness, |
| 161 | }; |
| 162 | |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 163 | #ifdef CONFIG_OF |
| 164 | static int pm860x_backlight_dt_init(struct platform_device *pdev, |
| 165 | struct pm860x_backlight_data *data, |
| 166 | char *name) |
| 167 | { |
Axel Lin | c84c383 | 2013-02-21 16:44:00 -0800 | [diff] [blame] | 168 | struct device_node *nproot, *np; |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 169 | int iset = 0; |
Axel Lin | c84c383 | 2013-02-21 16:44:00 -0800 | [diff] [blame] | 170 | |
| 171 | nproot = of_node_get(pdev->dev.parent->of_node); |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 172 | if (!nproot) |
| 173 | return -ENODEV; |
| 174 | nproot = of_find_node_by_name(nproot, "backlights"); |
| 175 | if (!nproot) { |
| 176 | dev_err(&pdev->dev, "failed to find backlights node\n"); |
| 177 | return -ENODEV; |
| 178 | } |
| 179 | for_each_child_of_node(nproot, np) { |
| 180 | if (!of_node_cmp(np->name, name)) { |
| 181 | of_property_read_u32(np, "marvell,88pm860x-iset", |
| 182 | &iset); |
| 183 | data->iset = PM8606_WLED_CURRENT(iset); |
| 184 | of_property_read_u32(np, "marvell,88pm860x-pwm", |
| 185 | &data->pwm); |
| 186 | break; |
| 187 | } |
| 188 | } |
Axel Lin | c84c383 | 2013-02-21 16:44:00 -0800 | [diff] [blame] | 189 | of_node_put(nproot); |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 190 | return 0; |
| 191 | } |
| 192 | #else |
| 193 | #define pm860x_backlight_dt_init(x, y, z) (-1) |
| 194 | #endif |
| 195 | |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 196 | static int pm860x_backlight_probe(struct platform_device *pdev) |
| 197 | { |
| 198 | struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | c512794c | 2013-11-12 15:09:04 -0800 | [diff] [blame] | 199 | struct pm860x_backlight_pdata *pdata = dev_get_platdata(&pdev->dev); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 200 | struct pm860x_backlight_data *data; |
| 201 | struct backlight_device *bl; |
| 202 | struct resource *res; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 203 | struct backlight_properties props; |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 204 | char name[MFD_NAME_SIZE]; |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 205 | int ret = 0; |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 206 | |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 207 | data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_backlight_data), |
| 208 | GFP_KERNEL); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 209 | if (data == NULL) |
| 210 | return -ENOMEM; |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 211 | res = platform_get_resource_byname(pdev, IORESOURCE_REG, "duty cycle"); |
| 212 | if (!res) { |
| 213 | dev_err(&pdev->dev, "No REG resource for duty cycle\n"); |
Jingoo Han | 3a1f946 | 2012-12-17 16:00:57 -0800 | [diff] [blame] | 214 | return -ENXIO; |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 215 | } |
| 216 | data->reg_duty_cycle = res->start; |
| 217 | res = platform_get_resource_byname(pdev, IORESOURCE_REG, "always on"); |
| 218 | if (!res) { |
Masanari Iida | 1a84db5 | 2014-08-29 23:37:33 +0900 | [diff] [blame] | 219 | dev_err(&pdev->dev, "No REG resource for always on\n"); |
Jingoo Han | 3a1f946 | 2012-12-17 16:00:57 -0800 | [diff] [blame] | 220 | return -ENXIO; |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 221 | } |
| 222 | data->reg_always_on = res->start; |
| 223 | res = platform_get_resource_byname(pdev, IORESOURCE_REG, "current"); |
| 224 | if (!res) { |
| 225 | dev_err(&pdev->dev, "No REG resource for current\n"); |
Jingoo Han | 3a1f946 | 2012-12-17 16:00:57 -0800 | [diff] [blame] | 226 | return -ENXIO; |
Haojian Zhuang | a6ccdcd | 2012-08-08 23:17:26 +0800 | [diff] [blame] | 227 | } |
| 228 | data->reg_current = res->start; |
| 229 | |
| 230 | memset(name, 0, MFD_NAME_SIZE); |
| 231 | sprintf(name, "backlight-%d", pdev->id); |
| 232 | data->port = pdev->id; |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 233 | data->chip = chip; |
Jingoo Han | 2fe2380 | 2012-12-17 16:01:09 -0800 | [diff] [blame] | 234 | data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion; |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 235 | data->current_brightness = MAX_BRIGHTNESS; |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 236 | if (pm860x_backlight_dt_init(pdev, data, name)) { |
| 237 | if (pdata) { |
| 238 | data->pwm = pdata->pwm; |
| 239 | data->iset = pdata->iset; |
| 240 | } |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 241 | } |
| 242 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 243 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 244 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 245 | props.max_brightness = MAX_BRIGHTNESS; |
Jingoo Han | f829c9e | 2013-11-12 15:09:05 -0800 | [diff] [blame] | 246 | bl = devm_backlight_device_register(&pdev->dev, name, &pdev->dev, data, |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 247 | &pm860x_backlight_ops, &props); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 248 | if (IS_ERR(bl)) { |
| 249 | dev_err(&pdev->dev, "failed to register backlight\n"); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 250 | return PTR_ERR(bl); |
| 251 | } |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 252 | bl->props.brightness = MAX_BRIGHTNESS; |
| 253 | |
| 254 | platform_set_drvdata(pdev, bl); |
| 255 | |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 256 | /* read current backlight */ |
| 257 | ret = pm860x_backlight_get_brightness(bl); |
| 258 | if (ret < 0) |
Jingoo Han | f829c9e | 2013-11-12 15:09:05 -0800 | [diff] [blame] | 259 | return ret; |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 260 | |
| 261 | backlight_update_status(bl); |
| 262 | return 0; |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | static struct platform_driver pm860x_backlight_driver = { |
| 266 | .driver = { |
| 267 | .name = "88pm860x-backlight", |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 268 | }, |
| 269 | .probe = pm860x_backlight_probe, |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 270 | }; |
| 271 | |
Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 272 | module_platform_driver(pm860x_backlight_driver); |
Haojian Zhuang | d07e8bf | 2009-11-09 12:41:07 -0500 | [diff] [blame] | 273 | |
| 274 | MODULE_DESCRIPTION("Backlight Driver for Marvell Semiconductor 88PM8606"); |
| 275 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); |
| 276 | MODULE_LICENSE("GPL"); |
| 277 | MODULE_ALIAS("platform:88pm860x-backlight"); |