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