Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Backlight driver for Maxim MAX8925 |
| 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/max8925.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Paul Gortmaker | 355b200 | 2011-07-03 16:17:28 -0400 | [diff] [blame] | 20 | #include <linux/module.h> |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 21 | |
| 22 | #define MAX_BRIGHTNESS (0xff) |
| 23 | #define MIN_BRIGHTNESS (0) |
| 24 | |
| 25 | #define LWX_FREQ(x) (((x - 601) / 100) & 0x7) |
| 26 | |
| 27 | struct max8925_backlight_data { |
| 28 | struct max8925_chip *chip; |
| 29 | |
| 30 | int current_brightness; |
| 31 | }; |
| 32 | |
| 33 | static int max8925_backlight_set(struct backlight_device *bl, int brightness) |
| 34 | { |
| 35 | struct max8925_backlight_data *data = bl_get_data(bl); |
| 36 | struct max8925_chip *chip = data->chip; |
| 37 | unsigned char value; |
| 38 | int ret; |
| 39 | |
| 40 | if (brightness > MAX_BRIGHTNESS) |
| 41 | value = MAX_BRIGHTNESS; |
| 42 | else |
| 43 | value = brightness; |
| 44 | |
| 45 | ret = max8925_reg_write(chip->i2c, MAX8925_WLED_CNTL, value); |
| 46 | if (ret < 0) |
| 47 | goto out; |
| 48 | |
| 49 | if (!data->current_brightness && brightness) |
| 50 | /* enable WLED output */ |
| 51 | ret = max8925_set_bits(chip->i2c, MAX8925_WLED_MODE_CNTL, 1, 1); |
| 52 | else if (!brightness) |
| 53 | /* disable WLED output */ |
| 54 | ret = max8925_set_bits(chip->i2c, MAX8925_WLED_MODE_CNTL, 1, 0); |
| 55 | if (ret < 0) |
| 56 | goto out; |
| 57 | dev_dbg(chip->dev, "set brightness %d\n", value); |
| 58 | data->current_brightness = value; |
| 59 | return 0; |
| 60 | out: |
| 61 | dev_dbg(chip->dev, "set brightness %d failure with return value:%d\n", |
| 62 | value, ret); |
| 63 | return ret; |
| 64 | } |
| 65 | |
| 66 | static int max8925_backlight_update_status(struct backlight_device *bl) |
| 67 | { |
| 68 | int brightness = bl->props.brightness; |
| 69 | |
| 70 | if (bl->props.power != FB_BLANK_UNBLANK) |
| 71 | brightness = 0; |
| 72 | |
| 73 | if (bl->props.fb_blank != FB_BLANK_UNBLANK) |
| 74 | brightness = 0; |
| 75 | |
| 76 | if (bl->props.state & BL_CORE_SUSPENDED) |
| 77 | brightness = 0; |
| 78 | |
| 79 | return max8925_backlight_set(bl, brightness); |
| 80 | } |
| 81 | |
| 82 | static int max8925_backlight_get_brightness(struct backlight_device *bl) |
| 83 | { |
| 84 | struct max8925_backlight_data *data = bl_get_data(bl); |
| 85 | struct max8925_chip *chip = data->chip; |
| 86 | int ret; |
| 87 | |
| 88 | ret = max8925_reg_read(chip->i2c, MAX8925_WLED_CNTL); |
| 89 | if (ret < 0) |
| 90 | return -EINVAL; |
| 91 | data->current_brightness = ret; |
| 92 | dev_dbg(chip->dev, "get brightness %d\n", data->current_brightness); |
| 93 | return ret; |
| 94 | } |
| 95 | |
Lionel Debroux | acc2472 | 2010-11-16 14:14:02 +0100 | [diff] [blame] | 96 | static const struct backlight_ops max8925_backlight_ops = { |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 97 | .options = BL_CORE_SUSPENDRESUME, |
| 98 | .update_status = max8925_backlight_update_status, |
| 99 | .get_brightness = max8925_backlight_get_brightness, |
| 100 | }; |
| 101 | |
| 102 | static int __devinit max8925_backlight_probe(struct platform_device *pdev) |
| 103 | { |
| 104 | struct max8925_chip *chip = dev_get_drvdata(pdev->dev.parent); |
| 105 | struct max8925_platform_data *max8925_pdata; |
| 106 | struct max8925_backlight_pdata *pdata = NULL; |
| 107 | struct max8925_backlight_data *data; |
| 108 | struct backlight_device *bl; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 109 | struct backlight_properties props; |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 110 | struct resource *res; |
| 111 | char name[MAX8925_NAME_SIZE]; |
| 112 | unsigned char value; |
| 113 | int ret; |
| 114 | |
| 115 | res = platform_get_resource(pdev, IORESOURCE_IO, 0); |
| 116 | if (res == NULL) { |
| 117 | dev_err(&pdev->dev, "No I/O resource!\n"); |
| 118 | return -EINVAL; |
| 119 | } |
| 120 | |
| 121 | if (pdev->dev.parent->platform_data) { |
| 122 | max8925_pdata = pdev->dev.parent->platform_data; |
| 123 | pdata = max8925_pdata->backlight; |
| 124 | } |
| 125 | |
| 126 | if (!pdata) { |
| 127 | dev_err(&pdev->dev, "platform data isn't assigned to " |
| 128 | "backlight\n"); |
| 129 | return -EINVAL; |
| 130 | } |
| 131 | |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 132 | data = devm_kzalloc(&pdev->dev, sizeof(struct max8925_backlight_data), |
| 133 | GFP_KERNEL); |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 134 | if (data == NULL) |
| 135 | return -ENOMEM; |
| 136 | strncpy(name, res->name, MAX8925_NAME_SIZE); |
| 137 | data->chip = chip; |
| 138 | data->current_brightness = 0; |
| 139 | |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 140 | memset(&props, 0, sizeof(struct backlight_properties)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 141 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 142 | props.max_brightness = MAX_BRIGHTNESS; |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 143 | bl = backlight_device_register(name, &pdev->dev, data, |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 144 | &max8925_backlight_ops, &props); |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 145 | if (IS_ERR(bl)) { |
| 146 | dev_err(&pdev->dev, "failed to register backlight\n"); |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 147 | return PTR_ERR(bl); |
| 148 | } |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 149 | bl->props.brightness = MAX_BRIGHTNESS; |
| 150 | |
| 151 | platform_set_drvdata(pdev, bl); |
| 152 | |
| 153 | value = 0; |
| 154 | if (pdata->lxw_scl) |
| 155 | value |= (1 << 7); |
| 156 | if (pdata->lxw_freq) |
| 157 | value |= (LWX_FREQ(pdata->lxw_freq) << 4); |
| 158 | if (pdata->dual_string) |
| 159 | value |= (1 << 1); |
| 160 | ret = max8925_set_bits(chip->i2c, MAX8925_WLED_MODE_CNTL, 0xfe, value); |
| 161 | if (ret < 0) |
| 162 | goto out; |
| 163 | |
| 164 | backlight_update_status(bl); |
| 165 | return 0; |
| 166 | out: |
Axel Lin | 146e10a | 2010-05-10 09:27:34 +0800 | [diff] [blame] | 167 | backlight_device_unregister(bl); |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 168 | return ret; |
| 169 | } |
| 170 | |
| 171 | static int __devexit max8925_backlight_remove(struct platform_device *pdev) |
| 172 | { |
| 173 | struct backlight_device *bl = platform_get_drvdata(pdev); |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 174 | |
| 175 | backlight_device_unregister(bl); |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 176 | return 0; |
| 177 | } |
| 178 | |
| 179 | static struct platform_driver max8925_backlight_driver = { |
| 180 | .driver = { |
| 181 | .name = "max8925-backlight", |
| 182 | .owner = THIS_MODULE, |
| 183 | }, |
| 184 | .probe = max8925_backlight_probe, |
| 185 | .remove = __devexit_p(max8925_backlight_remove), |
| 186 | }; |
| 187 | |
Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 188 | module_platform_driver(max8925_backlight_driver); |
Haojian Zhuang | 4f811ef | 2009-12-18 10:01:45 -0500 | [diff] [blame] | 189 | |
| 190 | MODULE_DESCRIPTION("Backlight Driver for Maxim MAX8925"); |
| 191 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); |
| 192 | MODULE_LICENSE("GPL"); |
| 193 | MODULE_ALIAS("platform:max8925-backlight"); |