xref: /openbmc/linux/drivers/leds/leds-lp8788.c (revision 75bf465f0bc33e9b776a46d6a1b9b990f5fb7c37)
1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2eb80411bSKim, Milo /*
3eb80411bSKim, Milo  * TI LP8788 MFD - keyled driver
4eb80411bSKim, Milo  *
5eb80411bSKim, Milo  * Copyright 2012 Texas Instruments
6eb80411bSKim, Milo  *
7eb80411bSKim, Milo  * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
8eb80411bSKim, Milo  */
9eb80411bSKim, Milo 
10eb80411bSKim, Milo #include <linux/module.h>
11eb80411bSKim, Milo #include <linux/slab.h>
12eb80411bSKim, Milo #include <linux/err.h>
13eb80411bSKim, Milo #include <linux/platform_device.h>
14eb80411bSKim, Milo #include <linux/leds.h>
15eb80411bSKim, Milo #include <linux/mutex.h>
16eb80411bSKim, Milo #include <linux/mfd/lp8788.h>
17eb80411bSKim, Milo #include <linux/mfd/lp8788-isink.h>
18eb80411bSKim, Milo 
19eb80411bSKim, Milo #define MAX_BRIGHTNESS			LP8788_ISINK_MAX_PWM
20eb80411bSKim, Milo #define DEFAULT_LED_NAME		"keyboard-backlight"
21eb80411bSKim, Milo 
22eb80411bSKim, Milo struct lp8788_led {
23eb80411bSKim, Milo 	struct lp8788 *lp;
24eb80411bSKim, Milo 	struct mutex lock;
25eb80411bSKim, Milo 	struct led_classdev led_dev;
26eb80411bSKim, Milo 	enum lp8788_isink_number isink_num;
27eb80411bSKim, Milo 	int on;
28eb80411bSKim, Milo };
29eb80411bSKim, Milo 
30eb80411bSKim, Milo struct lp8788_led_config {
31eb80411bSKim, Milo 	enum lp8788_isink_scale scale;
32eb80411bSKim, Milo 	enum lp8788_isink_number num;
33eb80411bSKim, Milo 	int iout;
34eb80411bSKim, Milo };
35eb80411bSKim, Milo 
36eb80411bSKim, Milo static struct lp8788_led_config default_led_config = {
37eb80411bSKim, Milo 	.scale = LP8788_ISINK_SCALE_100mA,
38eb80411bSKim, Milo 	.num   = LP8788_ISINK_3,
39eb80411bSKim, Milo 	.iout  = 0,
40eb80411bSKim, Milo };
41eb80411bSKim, Milo 
lp8788_led_init_device(struct lp8788_led * led,struct lp8788_led_platform_data * pdata)42eb80411bSKim, Milo static int lp8788_led_init_device(struct lp8788_led *led,
43eb80411bSKim, Milo 				struct lp8788_led_platform_data *pdata)
44eb80411bSKim, Milo {
45eb80411bSKim, Milo 	struct lp8788_led_config *cfg = &default_led_config;
46eb80411bSKim, Milo 	u8 addr, mask, val;
47eb80411bSKim, Milo 	int ret;
48eb80411bSKim, Milo 
49eb80411bSKim, Milo 	if (pdata) {
50eb80411bSKim, Milo 		cfg->scale = pdata->scale;
51eb80411bSKim, Milo 		cfg->num = pdata->num;
52eb80411bSKim, Milo 		cfg->iout = pdata->iout_code;
53eb80411bSKim, Milo 	}
54eb80411bSKim, Milo 
55eb80411bSKim, Milo 	led->isink_num = cfg->num;
56eb80411bSKim, Milo 
57eb80411bSKim, Milo 	/* scale configuration */
58eb80411bSKim, Milo 	addr = LP8788_ISINK_CTRL;
59eb80411bSKim, Milo 	mask = 1 << (cfg->num + LP8788_ISINK_SCALE_OFFSET);
604df7309aSAxel Lin 	val = cfg->scale << (cfg->num + LP8788_ISINK_SCALE_OFFSET);
61eb80411bSKim, Milo 	ret = lp8788_update_bits(led->lp, addr, mask, val);
62eb80411bSKim, Milo 	if (ret)
63eb80411bSKim, Milo 		return ret;
64eb80411bSKim, Milo 
65eb80411bSKim, Milo 	/* current configuration */
66eb80411bSKim, Milo 	addr = lp8788_iout_addr[cfg->num];
67eb80411bSKim, Milo 	mask = lp8788_iout_mask[cfg->num];
68eb80411bSKim, Milo 	val = cfg->iout;
69eb80411bSKim, Milo 
70eb80411bSKim, Milo 	return lp8788_update_bits(led->lp, addr, mask, val);
71eb80411bSKim, Milo }
72eb80411bSKim, Milo 
lp8788_led_enable(struct lp8788_led * led,enum lp8788_isink_number num,int on)7364998371SAndrew Lunn static int lp8788_led_enable(struct lp8788_led *led,
74eb80411bSKim, Milo 			enum lp8788_isink_number num, int on)
75eb80411bSKim, Milo {
7664998371SAndrew Lunn 	int ret;
7764998371SAndrew Lunn 
78eb80411bSKim, Milo 	u8 mask = 1 << num;
79eb80411bSKim, Milo 	u8 val = on << num;
80eb80411bSKim, Milo 
8164998371SAndrew Lunn 	ret = lp8788_update_bits(led->lp, LP8788_ISINK_CTRL, mask, val);
8264998371SAndrew Lunn 	if (ret == 0)
83eb80411bSKim, Milo 		led->on = on;
8464998371SAndrew Lunn 
8564998371SAndrew Lunn 	return ret;
86eb80411bSKim, Milo }
87eb80411bSKim, Milo 
lp8788_brightness_set(struct led_classdev * led_cdev,enum led_brightness val)8864998371SAndrew Lunn static int lp8788_brightness_set(struct led_classdev *led_cdev,
8964998371SAndrew Lunn 				enum led_brightness val)
90eb80411bSKim, Milo {
9164998371SAndrew Lunn 	struct lp8788_led *led =
9264998371SAndrew Lunn 			container_of(led_cdev, struct lp8788_led, led_dev);
9364998371SAndrew Lunn 
94eb80411bSKim, Milo 	enum lp8788_isink_number num = led->isink_num;
9564998371SAndrew Lunn 	int enable, ret;
96eb80411bSKim, Milo 
97eb80411bSKim, Milo 	mutex_lock(&led->lock);
98eb80411bSKim, Milo 
99eb80411bSKim, Milo 	switch (num) {
100eb80411bSKim, Milo 	case LP8788_ISINK_1:
101eb80411bSKim, Milo 	case LP8788_ISINK_2:
102eb80411bSKim, Milo 	case LP8788_ISINK_3:
10364998371SAndrew Lunn 		ret = lp8788_write_byte(led->lp, lp8788_pwm_addr[num], val);
10464998371SAndrew Lunn 		if (ret < 0)
10564998371SAndrew Lunn 			goto unlock;
106eb80411bSKim, Milo 		break;
107eb80411bSKim, Milo 	default:
108d45bb116SDevendra Naga 		mutex_unlock(&led->lock);
10964998371SAndrew Lunn 		return -EINVAL;
110eb80411bSKim, Milo 	}
111eb80411bSKim, Milo 
112eb80411bSKim, Milo 	enable = (val > 0) ? 1 : 0;
113eb80411bSKim, Milo 	if (enable != led->on)
11464998371SAndrew Lunn 		ret = lp8788_led_enable(led, num, enable);
11564998371SAndrew Lunn unlock:
116eb80411bSKim, Milo 	mutex_unlock(&led->lock);
11764998371SAndrew Lunn 	return ret;
118eb80411bSKim, Milo }
119eb80411bSKim, Milo 
lp8788_led_probe(struct platform_device * pdev)12098ea1ea2SBill Pemberton static int lp8788_led_probe(struct platform_device *pdev)
121eb80411bSKim, Milo {
122eb80411bSKim, Milo 	struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
123eb80411bSKim, Milo 	struct lp8788_led_platform_data *led_pdata;
124eb80411bSKim, Milo 	struct lp8788_led *led;
125a1932edfSKim, Milo 	struct device *dev = &pdev->dev;
126eb80411bSKim, Milo 	int ret;
127eb80411bSKim, Milo 
128a1932edfSKim, Milo 	led = devm_kzalloc(dev, sizeof(struct lp8788_led), GFP_KERNEL);
129eb80411bSKim, Milo 	if (!led)
130eb80411bSKim, Milo 		return -ENOMEM;
131eb80411bSKim, Milo 
132eb80411bSKim, Milo 	led->lp = lp;
133eb80411bSKim, Milo 	led->led_dev.max_brightness = MAX_BRIGHTNESS;
13464998371SAndrew Lunn 	led->led_dev.brightness_set_blocking = lp8788_brightness_set;
135eb80411bSKim, Milo 
136eb80411bSKim, Milo 	led_pdata = lp->pdata ? lp->pdata->led_pdata : NULL;
137eb80411bSKim, Milo 
138eb80411bSKim, Milo 	if (!led_pdata || !led_pdata->name)
139eb80411bSKim, Milo 		led->led_dev.name = DEFAULT_LED_NAME;
140eb80411bSKim, Milo 	else
141eb80411bSKim, Milo 		led->led_dev.name = led_pdata->name;
142eb80411bSKim, Milo 
143eb80411bSKim, Milo 	mutex_init(&led->lock);
144eb80411bSKim, Milo 
145eb80411bSKim, Milo 	ret = lp8788_led_init_device(led, led_pdata);
146eb80411bSKim, Milo 	if (ret) {
147a1932edfSKim, Milo 		dev_err(dev, "led init device err: %d\n", ret);
148eb80411bSKim, Milo 		return ret;
149eb80411bSKim, Milo 	}
150eb80411bSKim, Milo 
151f165a66eSAmitoj Kaur Chawla 	ret = devm_led_classdev_register(dev, &led->led_dev);
152eb80411bSKim, Milo 	if (ret) {
153a1932edfSKim, Milo 		dev_err(dev, "led register err: %d\n", ret);
154eb80411bSKim, Milo 		return ret;
155eb80411bSKim, Milo 	}
156eb80411bSKim, Milo 
157eb80411bSKim, Milo 	return 0;
158eb80411bSKim, Milo }
159eb80411bSKim, Milo 
160eb80411bSKim, Milo static struct platform_driver lp8788_led_driver = {
161eb80411bSKim, Milo 	.probe = lp8788_led_probe,
162eb80411bSKim, Milo 	.driver = {
163eb80411bSKim, Milo 		.name = LP8788_DEV_KEYLED,
164eb80411bSKim, Milo 	},
165eb80411bSKim, Milo };
166eb80411bSKim, Milo module_platform_driver(lp8788_led_driver);
167eb80411bSKim, Milo 
168eb80411bSKim, Milo MODULE_DESCRIPTION("Texas Instruments LP8788 Keyboard LED Driver");
169eb80411bSKim, Milo MODULE_AUTHOR("Milo Kim");
170eb80411bSKim, Milo MODULE_LICENSE("GPL");
171eb80411bSKim, Milo MODULE_ALIAS("platform:lp8788-keyled");
172