blob: d1e1bca90d11e74599a54b89a358ebd0162c3934 [file] [log] [blame]
Haojian Zhuang0a2f9152010-01-06 17:10:09 -05001/*
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 Zhuang2e57d562012-09-21 18:06:52 +080014#include <linux/of.h>
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050015#include <linux/platform_device.h>
16#include <linux/i2c.h>
17#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050019#include <linux/workqueue.h>
20#include <linux/mfd/88pm860x.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040021#include <linux/module.h>
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050022
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050023#define LED_PWM_MASK (0x1F)
24#define LED_CURRENT_MASK (0x07 << 5)
25
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050026#define LED_BLINK_MASK (0x7F)
27
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040028#define LED_ON_CONTINUOUS (0x0F << 3)
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050029
30#define LED1_BLINK_EN (1 << 1)
31#define LED2_BLINK_EN (1 << 2)
32
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050033struct 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 Zhuang0a2f9152010-01-06 17:10:09 -050043 unsigned char brightness;
44 unsigned char current_brightness;
45
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080046 int reg_control;
47 int reg_blink;
48 int blink_mask;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050049};
50
Jett.Zhoub3b97472012-02-28 10:59:29 +080051static int led_power_set(struct pm860x_chip *chip, int port, int on)
52{
53 int ret = -EINVAL;
54
55 switch (port) {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080056 case 0:
57 case 1:
58 case 2:
Jett.Zhoub3b97472012-02-28 10:59:29 +080059 ret = on ? pm8606_osc_enable(chip, RGB1_ENABLE) :
60 pm8606_osc_disable(chip, RGB1_ENABLE);
61 break;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080062 case 3:
63 case 4:
64 case 5:
Jett.Zhoub3b97472012-02-28 10:59:29 +080065 ret = on ? pm8606_osc_enable(chip, RGB2_ENABLE) :
66 pm8606_osc_disable(chip, RGB2_ENABLE);
67 break;
68 }
69 return ret;
70}
71
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050072static void pm860x_led_work(struct work_struct *work)
73{
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040074
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050075 struct pm860x_led *led;
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040076 struct pm860x_chip *chip;
Haojian Zhuang3154c342011-03-07 23:43:10 +080077 unsigned char buf[3];
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080078 int ret;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050079
80 led = container_of(work, struct pm860x_led, work);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040081 chip = led->chip;
82 mutex_lock(&led->lock);
83 if ((led->current_brightness == 0) && led->brightness) {
Jett.Zhoub3b97472012-02-28 10:59:29 +080084 led_power_set(chip, led->port, 1);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040085 if (led->iset) {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080086 pm860x_set_bits(led->i2c, led->reg_control,
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040087 LED_CURRENT_MASK, led->iset);
88 }
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080089 pm860x_set_bits(led->i2c, led->reg_blink,
Haojian Zhuang3154c342011-03-07 23:43:10 +080090 LED_BLINK_MASK, LED_ON_CONTINUOUS);
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080091 pm860x_set_bits(led->i2c, PM8606_WLED3B, led->blink_mask,
92 led->blink_mask);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040093 }
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080094 pm860x_set_bits(led->i2c, led->reg_control, LED_PWM_MASK,
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040095 led->brightness);
Haojian Zhuang3154c342011-03-07 23:43:10 +080096
97 if (led->brightness == 0) {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080098 pm860x_bulk_read(led->i2c, led->reg_control, 3, buf);
Haojian Zhuang3154c342011-03-07 23:43:10 +080099 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 Zhuang894fc8f2012-08-08 23:17:27 +0800104 pm860x_set_bits(led->i2c, led->reg_control,
Haojian Zhuang3154c342011-03-07 23:43:10 +0800105 LED_CURRENT_MASK, 0);
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800106 pm860x_set_bits(led->i2c, PM8606_WLED3B,
107 led->blink_mask, 0);
Jett.Zhoub3b97472012-02-28 10:59:29 +0800108 led_power_set(chip, led->port, 0);
Haojian Zhuang3154c342011-03-07 23:43:10 +0800109 }
110 }
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -0400111 led->current_brightness = led->brightness;
112 dev_dbg(chip->dev, "Update LED. (reg:%d, brightness:%d)\n",
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800113 led->reg_control, led->brightness);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -0400114 mutex_unlock(&led->lock);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500115}
116
117static 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 Zhuang0a2f9152010-01-06 17:10:09 -0500122 data->brightness = value >> 3;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500123 schedule_work(&data->work);
124}
125
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800126#ifdef CONFIG_OF
127static int pm860x_led_dt_init(struct platform_device *pdev,
128 struct pm860x_led *data)
129{
Axel Lin61d4eb22013-01-27 04:56:57 -0800130 struct device_node *nproot, *np;
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800131 int iset = 0;
Axel Lin61d4eb22013-01-27 04:56:57 -0800132
133 nproot = of_node_get(pdev->dev.parent->of_node);
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800134 if (!nproot)
135 return -ENODEV;
136 nproot = of_find_node_by_name(nproot, "leds");
137 if (!nproot) {
138 dev_err(&pdev->dev, "failed to find leds node\n");
139 return -ENODEV;
140 }
141 for_each_child_of_node(nproot, np) {
142 if (!of_node_cmp(np->name, data->name)) {
143 of_property_read_u32(np, "marvell,88pm860x-iset",
144 &iset);
145 data->iset = PM8606_LED_CURRENT(iset);
146 break;
147 }
148 }
Axel Lin61d4eb22013-01-27 04:56:57 -0800149 of_node_put(nproot);
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800150 return 0;
151}
152#else
153#define pm860x_led_dt_init(x, y) (-1)
154#endif
155
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500156static int pm860x_led_probe(struct platform_device *pdev)
157{
158 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
Jingoo Han87aae1e2013-07-30 01:07:35 -0700159 struct pm860x_led_pdata *pdata = dev_get_platdata(&pdev->dev);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500160 struct pm860x_led *data;
161 struct resource *res;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800162 int ret = 0;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500163
Devendra Nagab523cfe2012-07-02 18:15:43 +0800164 data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_led), GFP_KERNEL);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500165 if (data == NULL)
166 return -ENOMEM;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800167 res = platform_get_resource_byname(pdev, IORESOURCE_REG, "control");
168 if (!res) {
169 dev_err(&pdev->dev, "No REG resource for control\n");
Jingoo Han3651c4b2012-10-28 21:31:07 -0700170 return -ENXIO;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800171 }
172 data->reg_control = res->start;
173 res = platform_get_resource_byname(pdev, IORESOURCE_REG, "blink");
174 if (!res) {
175 dev_err(&pdev->dev, "No REG resource for blink\n");
Jingoo Han3651c4b2012-10-28 21:31:07 -0700176 return -ENXIO;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800177 }
178 data->reg_blink = res->start;
179 memset(data->name, 0, MFD_NAME_SIZE);
180 switch (pdev->id) {
181 case 0:
182 data->blink_mask = LED1_BLINK_EN;
183 sprintf(data->name, "led0-red");
184 break;
185 case 1:
186 data->blink_mask = LED1_BLINK_EN;
187 sprintf(data->name, "led0-green");
188 break;
189 case 2:
190 data->blink_mask = LED1_BLINK_EN;
191 sprintf(data->name, "led0-blue");
192 break;
193 case 3:
194 data->blink_mask = LED2_BLINK_EN;
195 sprintf(data->name, "led1-red");
196 break;
197 case 4:
198 data->blink_mask = LED2_BLINK_EN;
199 sprintf(data->name, "led1-green");
200 break;
201 case 5:
202 data->blink_mask = LED2_BLINK_EN;
203 sprintf(data->name, "led1-blue");
204 break;
205 }
Jingoo Han84196a22013-05-23 03:28:33 -0700206 platform_set_drvdata(pdev, data);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500207 data->chip = chip;
208 data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800209 data->port = pdev->id;
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800210 if (pm860x_led_dt_init(pdev, data))
211 if (pdata)
212 data->iset = pdata->iset;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500213
214 data->current_brightness = 0;
215 data->cdev.name = data->name;
216 data->cdev.brightness_set = pm860x_led_set;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500217 mutex_init(&data->lock);
218 INIT_WORK(&data->work, pm860x_led_work);
219
220 ret = led_classdev_register(chip->dev, &data->cdev);
221 if (ret < 0) {
222 dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
Devendra Nagab523cfe2012-07-02 18:15:43 +0800223 return ret;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500224 }
Haojian Zhuang3154c342011-03-07 23:43:10 +0800225 pm860x_led_set(&data->cdev, 0);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500226 return 0;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500227}
228
229static int pm860x_led_remove(struct platform_device *pdev)
230{
231 struct pm860x_led *data = platform_get_drvdata(pdev);
232
233 led_classdev_unregister(&data->cdev);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500234
235 return 0;
236}
237
238static struct platform_driver pm860x_led_driver = {
239 .driver = {
240 .name = "88pm860x-led",
241 .owner = THIS_MODULE,
242 },
243 .probe = pm860x_led_probe,
244 .remove = pm860x_led_remove,
245};
246
Axel Lin892a8842012-01-10 15:09:24 -0800247module_platform_driver(pm860x_led_driver);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500248
249MODULE_DESCRIPTION("LED driver for Marvell PM860x");
250MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
251MODULE_LICENSE("GPL");
252MODULE_ALIAS("platform:88pm860x-led");