Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009-2010, Lars-Peter Clausen <lars@metafoo.de> |
| 3 | * PCF50633 backlight device driver |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License as published by the |
| 7 | * Free Software Foundation; either version 2 of the License, or (at your |
| 8 | * option) any later version. |
| 9 | * |
| 10 | * You should have received a copy of the GNU General Public License along |
| 11 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 12 | * 675 Mass Ave, Cambridge, MA 02139, USA. |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | |
| 21 | #include <linux/backlight.h> |
| 22 | #include <linux/fb.h> |
| 23 | |
| 24 | #include <linux/mfd/pcf50633/core.h> |
| 25 | #include <linux/mfd/pcf50633/backlight.h> |
| 26 | |
| 27 | struct pcf50633_bl { |
| 28 | struct pcf50633 *pcf; |
| 29 | struct backlight_device *bl; |
| 30 | |
| 31 | unsigned int brightness; |
| 32 | unsigned int brightness_limit; |
| 33 | }; |
| 34 | |
| 35 | /* |
| 36 | * pcf50633_bl_set_brightness_limit |
| 37 | * |
| 38 | * Update the brightness limit for the pc50633 backlight. The actual brightness |
| 39 | * will not go above the limit. This is useful to limit power drain for example |
| 40 | * on low battery. |
| 41 | * |
| 42 | * @dev: Pointer to a pcf50633 device |
| 43 | * @limit: The brightness limit. Valid values are 0-63 |
| 44 | */ |
| 45 | int pcf50633_bl_set_brightness_limit(struct pcf50633 *pcf, unsigned int limit) |
| 46 | { |
| 47 | struct pcf50633_bl *pcf_bl = platform_get_drvdata(pcf->bl_pdev); |
| 48 | |
| 49 | if (!pcf_bl) |
| 50 | return -ENODEV; |
| 51 | |
| 52 | pcf_bl->brightness_limit = limit & 0x3f; |
| 53 | backlight_update_status(pcf_bl->bl); |
| 54 | |
Jingoo Han | ae872db | 2012-12-17 16:00:30 -0800 | [diff] [blame] | 55 | return 0; |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static int pcf50633_bl_update_status(struct backlight_device *bl) |
| 59 | { |
| 60 | struct pcf50633_bl *pcf_bl = bl_get_data(bl); |
| 61 | unsigned int new_brightness; |
| 62 | |
| 63 | |
| 64 | if (bl->props.state & (BL_CORE_SUSPENDED | BL_CORE_FBBLANK) || |
| 65 | bl->props.power != FB_BLANK_UNBLANK) |
| 66 | new_brightness = 0; |
| 67 | else if (bl->props.brightness < pcf_bl->brightness_limit) |
| 68 | new_brightness = bl->props.brightness; |
| 69 | else |
| 70 | new_brightness = pcf_bl->brightness_limit; |
| 71 | |
| 72 | |
| 73 | if (pcf_bl->brightness == new_brightness) |
| 74 | return 0; |
| 75 | |
| 76 | if (new_brightness) { |
| 77 | pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDOUT, |
| 78 | new_brightness); |
| 79 | if (!pcf_bl->brightness) |
| 80 | pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDENA, 1); |
| 81 | } else { |
| 82 | pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDENA, 0); |
| 83 | } |
| 84 | |
| 85 | pcf_bl->brightness = new_brightness; |
| 86 | |
| 87 | return 0; |
| 88 | } |
| 89 | |
| 90 | static int pcf50633_bl_get_brightness(struct backlight_device *bl) |
| 91 | { |
| 92 | struct pcf50633_bl *pcf_bl = bl_get_data(bl); |
Jingoo Han | c915ae5 | 2014-08-27 10:13:10 +0900 | [diff] [blame] | 93 | |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 94 | return pcf_bl->brightness; |
| 95 | } |
| 96 | |
| 97 | static const struct backlight_ops pcf50633_bl_ops = { |
| 98 | .get_brightness = pcf50633_bl_get_brightness, |
| 99 | .update_status = pcf50633_bl_update_status, |
| 100 | .options = BL_CORE_SUSPENDRESUME, |
| 101 | }; |
| 102 | |
Bill Pemberton | 1b9e450 | 2012-11-19 13:21:46 -0500 | [diff] [blame] | 103 | static int pcf50633_bl_probe(struct platform_device *pdev) |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 104 | { |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 105 | struct pcf50633_bl *pcf_bl; |
| 106 | struct device *parent = pdev->dev.parent; |
Jingoo Han | c512794c | 2013-11-12 15:09:04 -0800 | [diff] [blame] | 107 | struct pcf50633_platform_data *pcf50633_data = dev_get_platdata(parent); |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 108 | struct pcf50633_bl_platform_data *pdata = pcf50633_data->backlight_data; |
| 109 | struct backlight_properties bl_props; |
| 110 | |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 111 | pcf_bl = devm_kzalloc(&pdev->dev, sizeof(*pcf_bl), GFP_KERNEL); |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 112 | if (!pcf_bl) |
| 113 | return -ENOMEM; |
| 114 | |
Corentin Chary | f5f4fd4 | 2012-05-29 15:07:20 -0700 | [diff] [blame] | 115 | memset(&bl_props, 0, sizeof(bl_props)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 116 | bl_props.type = BACKLIGHT_RAW; |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 117 | bl_props.max_brightness = 0x3f; |
| 118 | bl_props.power = FB_BLANK_UNBLANK; |
| 119 | |
| 120 | if (pdata) { |
| 121 | bl_props.brightness = pdata->default_brightness; |
| 122 | pcf_bl->brightness_limit = pdata->default_brightness_limit; |
| 123 | } else { |
| 124 | bl_props.brightness = 0x3f; |
| 125 | pcf_bl->brightness_limit = 0x3f; |
| 126 | } |
| 127 | |
| 128 | pcf_bl->pcf = dev_to_pcf50633(pdev->dev.parent); |
| 129 | |
Jingoo Han | c423a77 | 2013-11-12 15:09:23 -0800 | [diff] [blame] | 130 | pcf_bl->bl = devm_backlight_device_register(&pdev->dev, pdev->name, |
| 131 | &pdev->dev, pcf_bl, |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 132 | &pcf50633_bl_ops, &bl_props); |
| 133 | |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 134 | if (IS_ERR(pcf_bl->bl)) |
| 135 | return PTR_ERR(pcf_bl->bl); |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 136 | |
| 137 | platform_set_drvdata(pdev, pcf_bl); |
| 138 | |
| 139 | pcf50633_reg_write(pcf_bl->pcf, PCF50633_REG_LEDDIM, pdata->ramp_time); |
| 140 | |
Jingoo Han | ae872db | 2012-12-17 16:00:30 -0800 | [diff] [blame] | 141 | /* |
| 142 | * Should be different from bl_props.brightness, so we do not exit |
| 143 | * update_status early the first time it's called |
| 144 | */ |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 145 | pcf_bl->brightness = pcf_bl->bl->props.brightness + 1; |
| 146 | |
| 147 | backlight_update_status(pcf_bl->bl); |
| 148 | |
| 149 | return 0; |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 150 | } |
| 151 | |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 152 | static struct platform_driver pcf50633_bl_driver = { |
| 153 | .probe = pcf50633_bl_probe, |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 154 | .driver = { |
| 155 | .name = "pcf50633-backlight", |
| 156 | }, |
| 157 | }; |
| 158 | |
Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 159 | module_platform_driver(pcf50633_bl_driver); |
Lars-Peter Clausen | 2ddfd12 | 2010-05-12 02:44:32 +0200 | [diff] [blame] | 160 | |
| 161 | MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>"); |
| 162 | MODULE_DESCRIPTION("PCF50633 backlight driver"); |
| 163 | MODULE_LICENSE("GPL"); |
| 164 | MODULE_ALIAS("platform:pcf50633-backlight"); |