blob: 6be2edd41173e7e72766e37f5878f71e1e4ea0b1 [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>
14#include <linux/init.h>
Haojian Zhuang2e57d562012-09-21 18:06:52 +080015#include <linux/of.h>
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050016#include <linux/platform_device.h>
17#include <linux/i2c.h>
18#include <linux/leds.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090019#include <linux/slab.h>
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050020#include <linux/workqueue.h>
21#include <linux/mfd/88pm860x.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040022#include <linux/module.h>
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050023
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050024#define LED_PWM_MASK (0x1F)
25#define LED_CURRENT_MASK (0x07 << 5)
26
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050027#define LED_BLINK_MASK (0x7F)
28
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040029#define LED_ON_CONTINUOUS (0x0F << 3)
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050030
31#define LED1_BLINK_EN (1 << 1)
32#define LED2_BLINK_EN (1 << 2)
33
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050034struct 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 Zhuang0a2f9152010-01-06 17:10:09 -050044 unsigned char brightness;
45 unsigned char current_brightness;
46
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080047 int reg_control;
48 int reg_blink;
49 int blink_mask;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050050};
51
Jett.Zhoub3b97472012-02-28 10:59:29 +080052static int led_power_set(struct pm860x_chip *chip, int port, int on)
53{
54 int ret = -EINVAL;
55
56 switch (port) {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080057 case 0:
58 case 1:
59 case 2:
Jett.Zhoub3b97472012-02-28 10:59:29 +080060 ret = on ? pm8606_osc_enable(chip, RGB1_ENABLE) :
61 pm8606_osc_disable(chip, RGB1_ENABLE);
62 break;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080063 case 3:
64 case 4:
65 case 5:
Jett.Zhoub3b97472012-02-28 10:59:29 +080066 ret = on ? pm8606_osc_enable(chip, RGB2_ENABLE) :
67 pm8606_osc_disable(chip, RGB2_ENABLE);
68 break;
69 }
70 return ret;
71}
72
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050073static void pm860x_led_work(struct work_struct *work)
74{
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040075
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050076 struct pm860x_led *led;
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040077 struct pm860x_chip *chip;
Haojian Zhuang3154c342011-03-07 23:43:10 +080078 unsigned char buf[3];
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080079 int ret;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050080
81 led = container_of(work, struct pm860x_led, work);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040082 chip = led->chip;
83 mutex_lock(&led->lock);
84 if ((led->current_brightness == 0) && led->brightness) {
Jett.Zhoub3b97472012-02-28 10:59:29 +080085 led_power_set(chip, led->port, 1);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040086 if (led->iset) {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080087 pm860x_set_bits(led->i2c, led->reg_control,
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040088 LED_CURRENT_MASK, led->iset);
89 }
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080090 pm860x_set_bits(led->i2c, led->reg_blink,
Haojian Zhuang3154c342011-03-07 23:43:10 +080091 LED_BLINK_MASK, LED_ON_CONTINUOUS);
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080092 pm860x_set_bits(led->i2c, PM8606_WLED3B, led->blink_mask,
93 led->blink_mask);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040094 }
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080095 pm860x_set_bits(led->i2c, led->reg_control, LED_PWM_MASK,
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040096 led->brightness);
Haojian Zhuang3154c342011-03-07 23:43:10 +080097
98 if (led->brightness == 0) {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080099 pm860x_bulk_read(led->i2c, led->reg_control, 3, buf);
Haojian Zhuang3154c342011-03-07 23:43:10 +0800100 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 Zhuang894fc8f2012-08-08 23:17:27 +0800105 pm860x_set_bits(led->i2c, led->reg_control,
Haojian Zhuang3154c342011-03-07 23:43:10 +0800106 LED_CURRENT_MASK, 0);
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800107 pm860x_set_bits(led->i2c, PM8606_WLED3B,
108 led->blink_mask, 0);
Jett.Zhoub3b97472012-02-28 10:59:29 +0800109 led_power_set(chip, led->port, 0);
Haojian Zhuang3154c342011-03-07 23:43:10 +0800110 }
111 }
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -0400112 led->current_brightness = led->brightness;
113 dev_dbg(chip->dev, "Update LED. (reg:%d, brightness:%d)\n",
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800114 led->reg_control, led->brightness);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -0400115 mutex_unlock(&led->lock);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500116}
117
118static 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 Zhuang0a2f9152010-01-06 17:10:09 -0500123 data->brightness = value >> 3;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500124 schedule_work(&data->work);
125}
126
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800127#ifdef CONFIG_OF
128static int pm860x_led_dt_init(struct platform_device *pdev,
129 struct pm860x_led *data)
130{
131 struct device_node *nproot = pdev->dev.parent->of_node, *np;
132 int iset = 0;
133 if (!nproot)
134 return -ENODEV;
135 nproot = of_find_node_by_name(nproot, "leds");
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 }
148 return 0;
149}
150#else
151#define pm860x_led_dt_init(x, y) (-1)
152#endif
153
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500154static int pm860x_led_probe(struct platform_device *pdev)
155{
156 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800157 struct pm860x_led_pdata *pdata = pdev->dev.platform_data;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500158 struct pm860x_led *data;
159 struct resource *res;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800160 int ret = 0;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500161
Devendra Nagab523cfe2012-07-02 18:15:43 +0800162 data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_led), GFP_KERNEL);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500163 if (data == NULL)
164 return -ENOMEM;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800165 res = platform_get_resource_byname(pdev, IORESOURCE_REG, "control");
166 if (!res) {
167 dev_err(&pdev->dev, "No REG resource for control\n");
Jingoo Han3651c4b2012-10-28 21:31:07 -0700168 return -ENXIO;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800169 }
170 data->reg_control = res->start;
171 res = platform_get_resource_byname(pdev, IORESOURCE_REG, "blink");
172 if (!res) {
173 dev_err(&pdev->dev, "No REG resource for blink\n");
Jingoo Han3651c4b2012-10-28 21:31:07 -0700174 return -ENXIO;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800175 }
176 data->reg_blink = res->start;
177 memset(data->name, 0, MFD_NAME_SIZE);
178 switch (pdev->id) {
179 case 0:
180 data->blink_mask = LED1_BLINK_EN;
181 sprintf(data->name, "led0-red");
182 break;
183 case 1:
184 data->blink_mask = LED1_BLINK_EN;
185 sprintf(data->name, "led0-green");
186 break;
187 case 2:
188 data->blink_mask = LED1_BLINK_EN;
189 sprintf(data->name, "led0-blue");
190 break;
191 case 3:
192 data->blink_mask = LED2_BLINK_EN;
193 sprintf(data->name, "led1-red");
194 break;
195 case 4:
196 data->blink_mask = LED2_BLINK_EN;
197 sprintf(data->name, "led1-green");
198 break;
199 case 5:
200 data->blink_mask = LED2_BLINK_EN;
201 sprintf(data->name, "led1-blue");
202 break;
203 }
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500204 dev_set_drvdata(&pdev->dev, data);
205 data->chip = chip;
206 data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800207 data->port = pdev->id;
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800208 if (pm860x_led_dt_init(pdev, data))
209 if (pdata)
210 data->iset = pdata->iset;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500211
212 data->current_brightness = 0;
213 data->cdev.name = data->name;
214 data->cdev.brightness_set = pm860x_led_set;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500215 mutex_init(&data->lock);
216 INIT_WORK(&data->work, pm860x_led_work);
217
218 ret = led_classdev_register(chip->dev, &data->cdev);
219 if (ret < 0) {
220 dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
Devendra Nagab523cfe2012-07-02 18:15:43 +0800221 return ret;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500222 }
Haojian Zhuang3154c342011-03-07 23:43:10 +0800223 pm860x_led_set(&data->cdev, 0);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500224 return 0;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500225}
226
227static int pm860x_led_remove(struct platform_device *pdev)
228{
229 struct pm860x_led *data = platform_get_drvdata(pdev);
230
231 led_classdev_unregister(&data->cdev);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500232
233 return 0;
234}
235
236static struct platform_driver pm860x_led_driver = {
237 .driver = {
238 .name = "88pm860x-led",
239 .owner = THIS_MODULE,
240 },
241 .probe = pm860x_led_probe,
242 .remove = pm860x_led_remove,
243};
244
Axel Lin892a8842012-01-10 15:09:24 -0800245module_platform_driver(pm860x_led_driver);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500246
247MODULE_DESCRIPTION("LED driver for Marvell PM860x");
248MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
249MODULE_LICENSE("GPL");
250MODULE_ALIAS("platform:88pm860x-led");