Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Backlight driver for Dialog Semiconductor DA9030/DA9034 |
| 3 | * |
| 4 | * Copyright (C) 2008 Compulab, Ltd. |
Jingoo Han | 685746d | 2012-12-17 16:00:15 -0800 | [diff] [blame] | 5 | * Mike Rapoport <mike@compulab.co.il> |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) 2006-2008 Marvell International Ltd. |
Jingoo Han | 685746d | 2012-12-17 16:00:15 -0800 | [diff] [blame] | 8 | * Eric Miao <eric.miao@marvell.com> |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/init.h> |
| 17 | #include <linux/platform_device.h> |
| 18 | #include <linux/fb.h> |
| 19 | #include <linux/backlight.h> |
| 20 | #include <linux/mfd/da903x.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Paul Gortmaker | 355b200 | 2011-07-03 16:17:28 -0400 | [diff] [blame] | 22 | #include <linux/module.h> |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 23 | |
| 24 | #define DA9030_WLED_CONTROL 0x25 |
| 25 | #define DA9030_WLED_CP_EN (1 << 6) |
| 26 | #define DA9030_WLED_TRIM(x) ((x) & 0x7) |
| 27 | |
| 28 | #define DA9034_WLED_CONTROL1 0x3C |
| 29 | #define DA9034_WLED_CONTROL2 0x3D |
Haojian Zhuang | b3a8549 | 2009-11-05 10:27:13 -0500 | [diff] [blame] | 30 | #define DA9034_WLED_ISET(x) ((x) & 0x1f) |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 31 | |
| 32 | #define DA9034_WLED_BOOST_EN (1 << 5) |
| 33 | |
| 34 | #define DA9030_MAX_BRIGHTNESS 7 |
| 35 | #define DA9034_MAX_BRIGHTNESS 0x7f |
| 36 | |
| 37 | struct da903x_backlight_data { |
| 38 | struct device *da903x_dev; |
| 39 | int id; |
| 40 | int current_brightness; |
| 41 | }; |
| 42 | |
| 43 | static int da903x_backlight_set(struct backlight_device *bl, int brightness) |
| 44 | { |
| 45 | struct da903x_backlight_data *data = bl_get_data(bl); |
| 46 | struct device *dev = data->da903x_dev; |
| 47 | uint8_t val; |
| 48 | int ret = 0; |
| 49 | |
| 50 | switch (data->id) { |
| 51 | case DA9034_ID_WLED: |
| 52 | ret = da903x_update(dev, DA9034_WLED_CONTROL1, |
| 53 | brightness, 0x7f); |
| 54 | if (ret) |
| 55 | return ret; |
| 56 | |
| 57 | if (data->current_brightness && brightness == 0) |
| 58 | ret = da903x_clr_bits(dev, |
| 59 | DA9034_WLED_CONTROL2, |
| 60 | DA9034_WLED_BOOST_EN); |
| 61 | |
| 62 | if (data->current_brightness == 0 && brightness) |
| 63 | ret = da903x_set_bits(dev, |
| 64 | DA9034_WLED_CONTROL2, |
| 65 | DA9034_WLED_BOOST_EN); |
| 66 | break; |
| 67 | case DA9030_ID_WLED: |
| 68 | val = DA9030_WLED_TRIM(brightness); |
| 69 | val |= brightness ? DA9030_WLED_CP_EN : 0; |
| 70 | ret = da903x_write(dev, DA9030_WLED_CONTROL, val); |
| 71 | break; |
| 72 | } |
| 73 | |
| 74 | if (ret) |
| 75 | return ret; |
| 76 | |
| 77 | data->current_brightness = brightness; |
| 78 | return 0; |
| 79 | } |
| 80 | |
| 81 | static int da903x_backlight_update_status(struct backlight_device *bl) |
| 82 | { |
| 83 | int brightness = bl->props.brightness; |
| 84 | |
| 85 | if (bl->props.power != FB_BLANK_UNBLANK) |
| 86 | brightness = 0; |
| 87 | |
| 88 | if (bl->props.fb_blank != FB_BLANK_UNBLANK) |
| 89 | brightness = 0; |
| 90 | |
Jingoo Han | d1720b5 | 2013-04-29 16:17:51 -0700 | [diff] [blame] | 91 | if (bl->props.state & BL_CORE_SUSPENDED) |
| 92 | brightness = 0; |
| 93 | |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 94 | return da903x_backlight_set(bl, brightness); |
| 95 | } |
| 96 | |
| 97 | static int da903x_backlight_get_brightness(struct backlight_device *bl) |
| 98 | { |
| 99 | struct da903x_backlight_data *data = bl_get_data(bl); |
Jingoo Han | d1720b5 | 2013-04-29 16:17:51 -0700 | [diff] [blame] | 100 | |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 101 | return data->current_brightness; |
| 102 | } |
| 103 | |
Emese Revfy | 9905a43 | 2009-12-14 00:58:57 +0100 | [diff] [blame] | 104 | static const struct backlight_ops da903x_backlight_ops = { |
Jingoo Han | d1720b5 | 2013-04-29 16:17:51 -0700 | [diff] [blame] | 105 | .options = BL_CORE_SUSPENDRESUME, |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 106 | .update_status = da903x_backlight_update_status, |
| 107 | .get_brightness = da903x_backlight_get_brightness, |
| 108 | }; |
| 109 | |
| 110 | static int da903x_backlight_probe(struct platform_device *pdev) |
| 111 | { |
Jingoo Han | c512794c | 2013-11-12 15:09:04 -0800 | [diff] [blame] | 112 | struct da9034_backlight_pdata *pdata = dev_get_platdata(&pdev->dev); |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 113 | struct da903x_backlight_data *data; |
| 114 | struct backlight_device *bl; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 115 | struct backlight_properties props; |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 116 | int max_brightness; |
| 117 | |
Julia Lawall | ce96922 | 2012-03-23 15:02:00 -0700 | [diff] [blame] | 118 | data = devm_kzalloc(&pdev->dev, sizeof(*data), GFP_KERNEL); |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 119 | if (data == NULL) |
| 120 | return -ENOMEM; |
| 121 | |
| 122 | switch (pdev->id) { |
| 123 | case DA9030_ID_WLED: |
| 124 | max_brightness = DA9030_MAX_BRIGHTNESS; |
| 125 | break; |
| 126 | case DA9034_ID_WLED: |
| 127 | max_brightness = DA9034_MAX_BRIGHTNESS; |
| 128 | break; |
| 129 | default: |
| 130 | dev_err(&pdev->dev, "invalid backlight device ID(%d)\n", |
| 131 | pdev->id); |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 132 | return -EINVAL; |
| 133 | } |
| 134 | |
| 135 | data->id = pdev->id; |
| 136 | data->da903x_dev = pdev->dev.parent; |
| 137 | data->current_brightness = 0; |
| 138 | |
Haojian Zhuang | b3a8549 | 2009-11-05 10:27:13 -0500 | [diff] [blame] | 139 | /* adjust the WLED output current */ |
| 140 | if (pdata) |
| 141 | da903x_write(data->da903x_dev, DA9034_WLED_CONTROL2, |
| 142 | DA9034_WLED_ISET(pdata->output_current)); |
| 143 | |
Corentin Chary | f5f4fd4 | 2012-05-29 15:07:20 -0700 | [diff] [blame] | 144 | memset(&props, 0, sizeof(props)); |
Matthew Garrett | bb7ca74 | 2011-03-22 16:30:21 -0700 | [diff] [blame] | 145 | props.type = BACKLIGHT_RAW; |
Matthew Garrett | a19a6ee | 2010-02-17 16:39:44 -0500 | [diff] [blame] | 146 | props.max_brightness = max_brightness; |
Jingoo Han | 5644529 | 2013-11-12 15:09:12 -0800 | [diff] [blame] | 147 | bl = devm_backlight_device_register(&pdev->dev, pdev->name, |
| 148 | data->da903x_dev, data, |
| 149 | &da903x_backlight_ops, &props); |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 150 | if (IS_ERR(bl)) { |
| 151 | dev_err(&pdev->dev, "failed to register backlight\n"); |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 152 | return PTR_ERR(bl); |
| 153 | } |
| 154 | |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 155 | bl->props.brightness = max_brightness; |
| 156 | |
| 157 | platform_set_drvdata(pdev, bl); |
| 158 | backlight_update_status(bl); |
| 159 | return 0; |
| 160 | } |
| 161 | |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 162 | static struct platform_driver da903x_backlight_driver = { |
| 163 | .driver = { |
| 164 | .name = "da903x-backlight", |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 165 | }, |
| 166 | .probe = da903x_backlight_probe, |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 167 | }; |
| 168 | |
Axel Lin | 81178e0 | 2012-01-10 15:09:11 -0800 | [diff] [blame] | 169 | module_platform_driver(da903x_backlight_driver); |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 170 | |
| 171 | MODULE_DESCRIPTION("Backlight Driver for Dialog Semiconductor DA9030/DA9034"); |
Jingoo Han | 685746d | 2012-12-17 16:00:15 -0800 | [diff] [blame] | 172 | MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>"); |
| 173 | MODULE_AUTHOR("Mike Rapoport <mike@compulab.co.il>"); |
Mike Rapoport | 923ae48 | 2008-10-13 09:03:44 +0100 | [diff] [blame] | 174 | MODULE_LICENSE("GPL"); |
| 175 | MODULE_ALIAS("platform:da903x-backlight"); |