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