Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 2 | /* |
| 3 | * LED driver for Marvell 88PM860x |
| 4 | * |
| 5 | * Copyright (C) 2009 Marvell International Ltd. |
| 6 | * Haojian Zhuang <haojian.zhuang@marvell.com> |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/kernel.h> |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 10 | #include <linux/of.h> |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 11 | #include <linux/platform_device.h> |
| 12 | #include <linux/i2c.h> |
| 13 | #include <linux/leds.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 15 | #include <linux/mfd/88pm860x.h> |
Paul Gortmaker | 54f4ded | 2011-07-03 13:56:03 -0400 | [diff] [blame] | 16 | #include <linux/module.h> |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 17 | |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 18 | #define LED_PWM_MASK (0x1F) |
| 19 | #define LED_CURRENT_MASK (0x07 << 5) |
| 20 | |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 21 | #define LED_BLINK_MASK (0x7F) |
| 22 | |
Haojian Zhuang | f5d59fc | 2010-09-08 09:44:33 -0400 | [diff] [blame] | 23 | #define LED_ON_CONTINUOUS (0x0F << 3) |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 24 | |
| 25 | #define LED1_BLINK_EN (1 << 1) |
| 26 | #define LED2_BLINK_EN (1 << 2) |
| 27 | |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 28 | struct pm860x_led { |
| 29 | struct led_classdev cdev; |
| 30 | struct i2c_client *i2c; |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 31 | struct pm860x_chip *chip; |
| 32 | struct mutex lock; |
| 33 | char name[MFD_NAME_SIZE]; |
| 34 | |
| 35 | int port; |
| 36 | int iset; |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 37 | unsigned char brightness; |
| 38 | unsigned char current_brightness; |
| 39 | |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 40 | int reg_control; |
| 41 | int reg_blink; |
| 42 | int blink_mask; |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 43 | }; |
| 44 | |
Jett.Zhou | b3b9747 | 2012-02-28 10:59:29 +0800 | [diff] [blame] | 45 | static int led_power_set(struct pm860x_chip *chip, int port, int on) |
| 46 | { |
| 47 | int ret = -EINVAL; |
| 48 | |
| 49 | switch (port) { |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 50 | case 0: |
| 51 | case 1: |
| 52 | case 2: |
Jett.Zhou | b3b9747 | 2012-02-28 10:59:29 +0800 | [diff] [blame] | 53 | ret = on ? pm8606_osc_enable(chip, RGB1_ENABLE) : |
| 54 | pm8606_osc_disable(chip, RGB1_ENABLE); |
| 55 | break; |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 56 | case 3: |
| 57 | case 4: |
| 58 | case 5: |
Jett.Zhou | b3b9747 | 2012-02-28 10:59:29 +0800 | [diff] [blame] | 59 | ret = on ? pm8606_osc_enable(chip, RGB2_ENABLE) : |
| 60 | pm8606_osc_disable(chip, RGB2_ENABLE); |
| 61 | break; |
| 62 | } |
| 63 | return ret; |
| 64 | } |
| 65 | |
Andrew Lunn | 8824fef | 2015-08-20 11:50:34 +0200 | [diff] [blame] | 66 | static int pm860x_led_set(struct led_classdev *cdev, |
| 67 | enum led_brightness value) |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 68 | { |
Andrew Lunn | 8824fef | 2015-08-20 11:50:34 +0200 | [diff] [blame] | 69 | struct pm860x_led *led = container_of(cdev, struct pm860x_led, cdev); |
Haojian Zhuang | f5d59fc | 2010-09-08 09:44:33 -0400 | [diff] [blame] | 70 | struct pm860x_chip *chip; |
Haojian Zhuang | 3154c34 | 2011-03-07 23:43:10 +0800 | [diff] [blame] | 71 | unsigned char buf[3]; |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 72 | int ret; |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 73 | |
Haojian Zhuang | f5d59fc | 2010-09-08 09:44:33 -0400 | [diff] [blame] | 74 | chip = led->chip; |
| 75 | mutex_lock(&led->lock); |
Andrew Lunn | 8824fef | 2015-08-20 11:50:34 +0200 | [diff] [blame] | 76 | led->brightness = value >> 3; |
| 77 | |
Haojian Zhuang | f5d59fc | 2010-09-08 09:44:33 -0400 | [diff] [blame] | 78 | if ((led->current_brightness == 0) && led->brightness) { |
Jett.Zhou | b3b9747 | 2012-02-28 10:59:29 +0800 | [diff] [blame] | 79 | led_power_set(chip, led->port, 1); |
Haojian Zhuang | f5d59fc | 2010-09-08 09:44:33 -0400 | [diff] [blame] | 80 | if (led->iset) { |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 81 | pm860x_set_bits(led->i2c, led->reg_control, |
Haojian Zhuang | f5d59fc | 2010-09-08 09:44:33 -0400 | [diff] [blame] | 82 | LED_CURRENT_MASK, led->iset); |
| 83 | } |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 84 | pm860x_set_bits(led->i2c, led->reg_blink, |
Haojian Zhuang | 3154c34 | 2011-03-07 23:43:10 +0800 | [diff] [blame] | 85 | LED_BLINK_MASK, LED_ON_CONTINUOUS); |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 86 | pm860x_set_bits(led->i2c, PM8606_WLED3B, led->blink_mask, |
| 87 | led->blink_mask); |
Haojian Zhuang | f5d59fc | 2010-09-08 09:44:33 -0400 | [diff] [blame] | 88 | } |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 89 | pm860x_set_bits(led->i2c, led->reg_control, LED_PWM_MASK, |
Haojian Zhuang | f5d59fc | 2010-09-08 09:44:33 -0400 | [diff] [blame] | 90 | led->brightness); |
Haojian Zhuang | 3154c34 | 2011-03-07 23:43:10 +0800 | [diff] [blame] | 91 | |
| 92 | if (led->brightness == 0) { |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 93 | pm860x_bulk_read(led->i2c, led->reg_control, 3, buf); |
Haojian Zhuang | 3154c34 | 2011-03-07 23:43:10 +0800 | [diff] [blame] | 94 | ret = buf[0] & LED_PWM_MASK; |
| 95 | ret |= buf[1] & LED_PWM_MASK; |
| 96 | ret |= buf[2] & LED_PWM_MASK; |
| 97 | if (ret == 0) { |
| 98 | /* unset current since no led is lighting */ |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 99 | pm860x_set_bits(led->i2c, led->reg_control, |
Haojian Zhuang | 3154c34 | 2011-03-07 23:43:10 +0800 | [diff] [blame] | 100 | LED_CURRENT_MASK, 0); |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 101 | pm860x_set_bits(led->i2c, PM8606_WLED3B, |
| 102 | led->blink_mask, 0); |
Jett.Zhou | b3b9747 | 2012-02-28 10:59:29 +0800 | [diff] [blame] | 103 | led_power_set(chip, led->port, 0); |
Haojian Zhuang | 3154c34 | 2011-03-07 23:43:10 +0800 | [diff] [blame] | 104 | } |
| 105 | } |
Haojian Zhuang | f5d59fc | 2010-09-08 09:44:33 -0400 | [diff] [blame] | 106 | led->current_brightness = led->brightness; |
| 107 | dev_dbg(chip->dev, "Update LED. (reg:%d, brightness:%d)\n", |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 108 | led->reg_control, led->brightness); |
Haojian Zhuang | f5d59fc | 2010-09-08 09:44:33 -0400 | [diff] [blame] | 109 | mutex_unlock(&led->lock); |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 110 | |
Andrew Lunn | 8824fef | 2015-08-20 11:50:34 +0200 | [diff] [blame] | 111 | return 0; |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 114 | #ifdef CONFIG_OF |
| 115 | static int pm860x_led_dt_init(struct platform_device *pdev, |
| 116 | struct pm860x_led *data) |
| 117 | { |
Axel Lin | 61d4eb2 | 2013-01-27 04:56:57 -0800 | [diff] [blame] | 118 | struct device_node *nproot, *np; |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 119 | int iset = 0; |
Axel Lin | 61d4eb2 | 2013-01-27 04:56:57 -0800 | [diff] [blame] | 120 | |
Marek Behún | 8853c95e9 | 2020-09-18 00:32:54 +0200 | [diff] [blame] | 121 | if (!dev_of_node(pdev->dev.parent)) |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 122 | return -ENODEV; |
Marek Behún | 8853c95e9 | 2020-09-18 00:32:54 +0200 | [diff] [blame] | 123 | nproot = of_get_child_by_name(dev_of_node(pdev->dev.parent), "leds"); |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 124 | if (!nproot) { |
| 125 | dev_err(&pdev->dev, "failed to find leds node\n"); |
| 126 | return -ENODEV; |
| 127 | } |
Marek Behún | 99a013c | 2020-09-18 00:32:56 +0200 | [diff] [blame] | 128 | for_each_available_child_of_node(nproot, np) { |
Rob Herring | 555fc5b | 2018-12-05 13:50:27 -0600 | [diff] [blame] | 129 | if (of_node_name_eq(np, data->name)) { |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 130 | of_property_read_u32(np, "marvell,88pm860x-iset", |
| 131 | &iset); |
| 132 | data->iset = PM8606_LED_CURRENT(iset); |
Julia Lawall | ffdc307 | 2015-10-24 16:42:30 +0200 | [diff] [blame] | 133 | of_node_put(np); |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 134 | break; |
| 135 | } |
| 136 | } |
Axel Lin | 61d4eb2 | 2013-01-27 04:56:57 -0800 | [diff] [blame] | 137 | of_node_put(nproot); |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | #else |
| 141 | #define pm860x_led_dt_init(x, y) (-1) |
| 142 | #endif |
| 143 | |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 144 | static int pm860x_led_probe(struct platform_device *pdev) |
| 145 | { |
| 146 | struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent); |
Jingoo Han | 87aae1e | 2013-07-30 01:07:35 -0700 | [diff] [blame] | 147 | struct pm860x_led_pdata *pdata = dev_get_platdata(&pdev->dev); |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 148 | struct pm860x_led *data; |
| 149 | struct resource *res; |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 150 | int ret = 0; |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 151 | |
Devendra Naga | b523cfe | 2012-07-02 18:15:43 +0800 | [diff] [blame] | 152 | data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_led), GFP_KERNEL); |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 153 | if (data == NULL) |
| 154 | return -ENOMEM; |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 155 | res = platform_get_resource_byname(pdev, IORESOURCE_REG, "control"); |
| 156 | if (!res) { |
| 157 | dev_err(&pdev->dev, "No REG resource for control\n"); |
Jingoo Han | 3651c4b | 2012-10-28 21:31:07 -0700 | [diff] [blame] | 158 | return -ENXIO; |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 159 | } |
| 160 | data->reg_control = res->start; |
| 161 | res = platform_get_resource_byname(pdev, IORESOURCE_REG, "blink"); |
| 162 | if (!res) { |
| 163 | dev_err(&pdev->dev, "No REG resource for blink\n"); |
Jingoo Han | 3651c4b | 2012-10-28 21:31:07 -0700 | [diff] [blame] | 164 | return -ENXIO; |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 165 | } |
| 166 | data->reg_blink = res->start; |
| 167 | memset(data->name, 0, MFD_NAME_SIZE); |
| 168 | switch (pdev->id) { |
| 169 | case 0: |
| 170 | data->blink_mask = LED1_BLINK_EN; |
| 171 | sprintf(data->name, "led0-red"); |
| 172 | break; |
| 173 | case 1: |
| 174 | data->blink_mask = LED1_BLINK_EN; |
| 175 | sprintf(data->name, "led0-green"); |
| 176 | break; |
| 177 | case 2: |
| 178 | data->blink_mask = LED1_BLINK_EN; |
| 179 | sprintf(data->name, "led0-blue"); |
| 180 | break; |
| 181 | case 3: |
| 182 | data->blink_mask = LED2_BLINK_EN; |
| 183 | sprintf(data->name, "led1-red"); |
| 184 | break; |
| 185 | case 4: |
| 186 | data->blink_mask = LED2_BLINK_EN; |
| 187 | sprintf(data->name, "led1-green"); |
| 188 | break; |
| 189 | case 5: |
| 190 | data->blink_mask = LED2_BLINK_EN; |
| 191 | sprintf(data->name, "led1-blue"); |
| 192 | break; |
| 193 | } |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 194 | data->chip = chip; |
| 195 | data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion; |
Haojian Zhuang | 894fc8f | 2012-08-08 23:17:27 +0800 | [diff] [blame] | 196 | data->port = pdev->id; |
Haojian Zhuang | 2e57d56 | 2012-09-21 18:06:52 +0800 | [diff] [blame] | 197 | if (pm860x_led_dt_init(pdev, data)) |
| 198 | if (pdata) |
| 199 | data->iset = pdata->iset; |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 200 | |
| 201 | data->current_brightness = 0; |
| 202 | data->cdev.name = data->name; |
Andrew Lunn | 8824fef | 2015-08-20 11:50:34 +0200 | [diff] [blame] | 203 | data->cdev.brightness_set_blocking = pm860x_led_set; |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 204 | mutex_init(&data->lock); |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 205 | |
Johan Hovold | eca21c2 | 2020-06-01 15:39:45 +0200 | [diff] [blame] | 206 | ret = led_classdev_register(chip->dev, &data->cdev); |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 207 | if (ret < 0) { |
| 208 | dev_err(&pdev->dev, "Failed to register LED: %d\n", ret); |
Devendra Naga | b523cfe | 2012-07-02 18:15:43 +0800 | [diff] [blame] | 209 | return ret; |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 210 | } |
Haojian Zhuang | 3154c34 | 2011-03-07 23:43:10 +0800 | [diff] [blame] | 211 | pm860x_led_set(&data->cdev, 0); |
Johan Hovold | eca21c2 | 2020-06-01 15:39:45 +0200 | [diff] [blame] | 212 | |
| 213 | platform_set_drvdata(pdev, data); |
| 214 | |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 215 | return 0; |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 216 | } |
| 217 | |
Johan Hovold | eca21c2 | 2020-06-01 15:39:45 +0200 | [diff] [blame] | 218 | static int pm860x_led_remove(struct platform_device *pdev) |
| 219 | { |
| 220 | struct pm860x_led *data = platform_get_drvdata(pdev); |
| 221 | |
| 222 | led_classdev_unregister(&data->cdev); |
| 223 | |
| 224 | return 0; |
| 225 | } |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 226 | |
| 227 | static struct platform_driver pm860x_led_driver = { |
| 228 | .driver = { |
| 229 | .name = "88pm860x-led", |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 230 | }, |
| 231 | .probe = pm860x_led_probe, |
Johan Hovold | eca21c2 | 2020-06-01 15:39:45 +0200 | [diff] [blame] | 232 | .remove = pm860x_led_remove, |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 233 | }; |
| 234 | |
Axel Lin | 892a884 | 2012-01-10 15:09:24 -0800 | [diff] [blame] | 235 | module_platform_driver(pm860x_led_driver); |
Haojian Zhuang | 0a2f915 | 2010-01-06 17:10:09 -0500 | [diff] [blame] | 236 | |
| 237 | MODULE_DESCRIPTION("LED driver for Marvell PM860x"); |
| 238 | MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>"); |
| 239 | MODULE_LICENSE("GPL"); |
| 240 | MODULE_ALIAS("platform:88pm860x-led"); |