blob: 77a104d2b1243a503d8daa0660d73ae19baa819e [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/mfd/88pm860x.h>
Paul Gortmaker54f4ded2011-07-03 13:56:03 -040020#include <linux/module.h>
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050021
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050022#define LED_PWM_MASK (0x1F)
23#define LED_CURRENT_MASK (0x07 << 5)
24
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050025#define LED_BLINK_MASK (0x7F)
26
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040027#define LED_ON_CONTINUOUS (0x0F << 3)
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050028
29#define LED1_BLINK_EN (1 << 1)
30#define LED2_BLINK_EN (1 << 2)
31
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050032struct pm860x_led {
33 struct led_classdev cdev;
34 struct i2c_client *i2c;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050035 struct pm860x_chip *chip;
36 struct mutex lock;
37 char name[MFD_NAME_SIZE];
38
39 int port;
40 int iset;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050041 unsigned char brightness;
42 unsigned char current_brightness;
43
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080044 int reg_control;
45 int reg_blink;
46 int blink_mask;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050047};
48
Jett.Zhoub3b97472012-02-28 10:59:29 +080049static int led_power_set(struct pm860x_chip *chip, int port, int on)
50{
51 int ret = -EINVAL;
52
53 switch (port) {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080054 case 0:
55 case 1:
56 case 2:
Jett.Zhoub3b97472012-02-28 10:59:29 +080057 ret = on ? pm8606_osc_enable(chip, RGB1_ENABLE) :
58 pm8606_osc_disable(chip, RGB1_ENABLE);
59 break;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080060 case 3:
61 case 4:
62 case 5:
Jett.Zhoub3b97472012-02-28 10:59:29 +080063 ret = on ? pm8606_osc_enable(chip, RGB2_ENABLE) :
64 pm8606_osc_disable(chip, RGB2_ENABLE);
65 break;
66 }
67 return ret;
68}
69
Andrew Lunn8824fef2015-08-20 11:50:34 +020070static int pm860x_led_set(struct led_classdev *cdev,
71 enum led_brightness value)
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050072{
Andrew Lunn8824fef2015-08-20 11:50:34 +020073 struct pm860x_led *led = container_of(cdev, struct pm860x_led, cdev);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040074 struct pm860x_chip *chip;
Haojian Zhuang3154c342011-03-07 23:43:10 +080075 unsigned char buf[3];
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080076 int ret;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -050077
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040078 chip = led->chip;
79 mutex_lock(&led->lock);
Andrew Lunn8824fef2015-08-20 11:50:34 +020080 led->brightness = value >> 3;
81
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040082 if ((led->current_brightness == 0) && led->brightness) {
Jett.Zhoub3b97472012-02-28 10:59:29 +080083 led_power_set(chip, led->port, 1);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040084 if (led->iset) {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080085 pm860x_set_bits(led->i2c, led->reg_control,
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040086 LED_CURRENT_MASK, led->iset);
87 }
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080088 pm860x_set_bits(led->i2c, led->reg_blink,
Haojian Zhuang3154c342011-03-07 23:43:10 +080089 LED_BLINK_MASK, LED_ON_CONTINUOUS);
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080090 pm860x_set_bits(led->i2c, PM8606_WLED3B, led->blink_mask,
91 led->blink_mask);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040092 }
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080093 pm860x_set_bits(led->i2c, led->reg_control, LED_PWM_MASK,
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -040094 led->brightness);
Haojian Zhuang3154c342011-03-07 23:43:10 +080095
96 if (led->brightness == 0) {
Haojian Zhuang894fc8f2012-08-08 23:17:27 +080097 pm860x_bulk_read(led->i2c, led->reg_control, 3, buf);
Haojian Zhuang3154c342011-03-07 23:43:10 +080098 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 Zhuang894fc8f2012-08-08 23:17:27 +0800103 pm860x_set_bits(led->i2c, led->reg_control,
Haojian Zhuang3154c342011-03-07 23:43:10 +0800104 LED_CURRENT_MASK, 0);
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800105 pm860x_set_bits(led->i2c, PM8606_WLED3B,
106 led->blink_mask, 0);
Jett.Zhoub3b97472012-02-28 10:59:29 +0800107 led_power_set(chip, led->port, 0);
Haojian Zhuang3154c342011-03-07 23:43:10 +0800108 }
109 }
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -0400110 led->current_brightness = led->brightness;
111 dev_dbg(chip->dev, "Update LED. (reg:%d, brightness:%d)\n",
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800112 led->reg_control, led->brightness);
Haojian Zhuangf5d59fc2010-09-08 09:44:33 -0400113 mutex_unlock(&led->lock);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500114
Andrew Lunn8824fef2015-08-20 11:50:34 +0200115 return 0;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500116}
117
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800118#ifdef CONFIG_OF
119static int pm860x_led_dt_init(struct platform_device *pdev,
120 struct pm860x_led *data)
121{
Axel Lin61d4eb22013-01-27 04:56:57 -0800122 struct device_node *nproot, *np;
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800123 int iset = 0;
Axel Lin61d4eb22013-01-27 04:56:57 -0800124
Krzysztof Kozlowski1175d5b2014-04-04 00:51:20 -0700125 if (!pdev->dev.parent->of_node)
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800126 return -ENODEV;
Krzysztof Kozlowski1175d5b2014-04-04 00:51:20 -0700127 nproot = of_get_child_by_name(pdev->dev.parent->of_node, "leds");
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800128 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 Lawallffdc3072015-10-24 16:42:30 +0200137 of_node_put(np);
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800138 break;
139 }
140 }
Axel Lin61d4eb22013-01-27 04:56:57 -0800141 of_node_put(nproot);
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800142 return 0;
143}
144#else
145#define pm860x_led_dt_init(x, y) (-1)
146#endif
147
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500148static int pm860x_led_probe(struct platform_device *pdev)
149{
150 struct pm860x_chip *chip = dev_get_drvdata(pdev->dev.parent);
Jingoo Han87aae1e2013-07-30 01:07:35 -0700151 struct pm860x_led_pdata *pdata = dev_get_platdata(&pdev->dev);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500152 struct pm860x_led *data;
153 struct resource *res;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800154 int ret = 0;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500155
Devendra Nagab523cfe2012-07-02 18:15:43 +0800156 data = devm_kzalloc(&pdev->dev, sizeof(struct pm860x_led), GFP_KERNEL);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500157 if (data == NULL)
158 return -ENOMEM;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800159 res = platform_get_resource_byname(pdev, IORESOURCE_REG, "control");
160 if (!res) {
161 dev_err(&pdev->dev, "No REG resource for control\n");
Jingoo Han3651c4b2012-10-28 21:31:07 -0700162 return -ENXIO;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800163 }
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 Han3651c4b2012-10-28 21:31:07 -0700168 return -ENXIO;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800169 }
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 Zhuang0a2f9152010-01-06 17:10:09 -0500198 data->chip = chip;
199 data->i2c = (chip->id == CHIP_PM8606) ? chip->client : chip->companion;
Haojian Zhuang894fc8f2012-08-08 23:17:27 +0800200 data->port = pdev->id;
Haojian Zhuang2e57d562012-09-21 18:06:52 +0800201 if (pm860x_led_dt_init(pdev, data))
202 if (pdata)
203 data->iset = pdata->iset;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500204
205 data->current_brightness = 0;
206 data->cdev.name = data->name;
Andrew Lunn8824fef2015-08-20 11:50:34 +0200207 data->cdev.brightness_set_blocking = pm860x_led_set;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500208 mutex_init(&data->lock);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500209
Amitoj Kaur Chawla375446d2016-03-09 08:53:29 +0530210 ret = devm_led_classdev_register(chip->dev, &data->cdev);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500211 if (ret < 0) {
212 dev_err(&pdev->dev, "Failed to register LED: %d\n", ret);
Devendra Nagab523cfe2012-07-02 18:15:43 +0800213 return ret;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500214 }
Haojian Zhuang3154c342011-03-07 23:43:10 +0800215 pm860x_led_set(&data->cdev, 0);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500216 return 0;
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500217}
218
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500219
220static struct platform_driver pm860x_led_driver = {
221 .driver = {
222 .name = "88pm860x-led",
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500223 },
224 .probe = pm860x_led_probe,
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500225};
226
Axel Lin892a8842012-01-10 15:09:24 -0800227module_platform_driver(pm860x_led_driver);
Haojian Zhuang0a2f9152010-01-06 17:10:09 -0500228
229MODULE_DESCRIPTION("LED driver for Marvell PM860x");
230MODULE_AUTHOR("Haojian Zhuang <haojian.zhuang@marvell.com>");
231MODULE_LICENSE("GPL");
232MODULE_ALIAS("platform:88pm860x-led");