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