blob: 9b9525ccca1573999288ced7ef88e841e300d4b3 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Kim, Miloeb804112012-07-20 16:43:59 +08002/*
3 * TI LP8788 MFD - keyled driver
4 *
5 * Copyright 2012 Texas Instruments
6 *
7 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
Kim, Miloeb804112012-07-20 16:43:59 +08008 */
9
10#include <linux/module.h>
11#include <linux/slab.h>
12#include <linux/err.h>
13#include <linux/platform_device.h>
14#include <linux/leds.h>
15#include <linux/mutex.h>
16#include <linux/mfd/lp8788.h>
17#include <linux/mfd/lp8788-isink.h>
18
19#define MAX_BRIGHTNESS LP8788_ISINK_MAX_PWM
20#define DEFAULT_LED_NAME "keyboard-backlight"
21
22struct lp8788_led {
23 struct lp8788 *lp;
24 struct mutex lock;
Kim, Miloeb804112012-07-20 16:43:59 +080025 struct led_classdev led_dev;
26 enum lp8788_isink_number isink_num;
Kim, Miloeb804112012-07-20 16:43:59 +080027 int on;
28};
29
30struct lp8788_led_config {
31 enum lp8788_isink_scale scale;
32 enum lp8788_isink_number num;
33 int iout;
34};
35
36static struct lp8788_led_config default_led_config = {
37 .scale = LP8788_ISINK_SCALE_100mA,
38 .num = LP8788_ISINK_3,
39 .iout = 0,
40};
41
42static int lp8788_led_init_device(struct lp8788_led *led,
43 struct lp8788_led_platform_data *pdata)
44{
45 struct lp8788_led_config *cfg = &default_led_config;
46 u8 addr, mask, val;
47 int ret;
48
49 if (pdata) {
50 cfg->scale = pdata->scale;
51 cfg->num = pdata->num;
52 cfg->iout = pdata->iout_code;
53 }
54
55 led->isink_num = cfg->num;
56
57 /* scale configuration */
58 addr = LP8788_ISINK_CTRL;
59 mask = 1 << (cfg->num + LP8788_ISINK_SCALE_OFFSET);
Axel Lin4df73092012-08-01 20:40:34 +080060 val = cfg->scale << (cfg->num + LP8788_ISINK_SCALE_OFFSET);
Kim, Miloeb804112012-07-20 16:43:59 +080061 ret = lp8788_update_bits(led->lp, addr, mask, val);
62 if (ret)
63 return ret;
64
65 /* current configuration */
66 addr = lp8788_iout_addr[cfg->num];
67 mask = lp8788_iout_mask[cfg->num];
68 val = cfg->iout;
69
70 return lp8788_update_bits(led->lp, addr, mask, val);
71}
72
Andrew Lunn64998372015-08-20 12:27:05 +020073static int lp8788_led_enable(struct lp8788_led *led,
Kim, Miloeb804112012-07-20 16:43:59 +080074 enum lp8788_isink_number num, int on)
75{
Andrew Lunn64998372015-08-20 12:27:05 +020076 int ret;
77
Kim, Miloeb804112012-07-20 16:43:59 +080078 u8 mask = 1 << num;
79 u8 val = on << num;
80
Andrew Lunn64998372015-08-20 12:27:05 +020081 ret = lp8788_update_bits(led->lp, LP8788_ISINK_CTRL, mask, val);
82 if (ret == 0)
83 led->on = on;
Kim, Miloeb804112012-07-20 16:43:59 +080084
Andrew Lunn64998372015-08-20 12:27:05 +020085 return ret;
Kim, Miloeb804112012-07-20 16:43:59 +080086}
87
Andrew Lunn64998372015-08-20 12:27:05 +020088static int lp8788_brightness_set(struct led_classdev *led_cdev,
89 enum led_brightness val)
Kim, Miloeb804112012-07-20 16:43:59 +080090{
Andrew Lunn64998372015-08-20 12:27:05 +020091 struct lp8788_led *led =
92 container_of(led_cdev, struct lp8788_led, led_dev);
93
Kim, Miloeb804112012-07-20 16:43:59 +080094 enum lp8788_isink_number num = led->isink_num;
Andrew Lunn64998372015-08-20 12:27:05 +020095 int enable, ret;
Kim, Miloeb804112012-07-20 16:43:59 +080096
97 mutex_lock(&led->lock);
98
99 switch (num) {
100 case LP8788_ISINK_1:
101 case LP8788_ISINK_2:
102 case LP8788_ISINK_3:
Andrew Lunn64998372015-08-20 12:27:05 +0200103 ret = lp8788_write_byte(led->lp, lp8788_pwm_addr[num], val);
104 if (ret < 0)
105 goto unlock;
Kim, Miloeb804112012-07-20 16:43:59 +0800106 break;
107 default:
Devendra Nagad45bb112012-07-27 02:00:31 +0800108 mutex_unlock(&led->lock);
Andrew Lunn64998372015-08-20 12:27:05 +0200109 return -EINVAL;
Kim, Miloeb804112012-07-20 16:43:59 +0800110 }
111
112 enable = (val > 0) ? 1 : 0;
113 if (enable != led->on)
Andrew Lunn64998372015-08-20 12:27:05 +0200114 ret = lp8788_led_enable(led, num, enable);
115unlock:
Kim, Miloeb804112012-07-20 16:43:59 +0800116 mutex_unlock(&led->lock);
Andrew Lunn64998372015-08-20 12:27:05 +0200117 return ret;
Kim, Miloeb804112012-07-20 16:43:59 +0800118}
119
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500120static int lp8788_led_probe(struct platform_device *pdev)
Kim, Miloeb804112012-07-20 16:43:59 +0800121{
122 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
123 struct lp8788_led_platform_data *led_pdata;
124 struct lp8788_led *led;
Kim, Miloa1932ed2013-01-02 22:28:12 -0800125 struct device *dev = &pdev->dev;
Kim, Miloeb804112012-07-20 16:43:59 +0800126 int ret;
127
Kim, Miloa1932ed2013-01-02 22:28:12 -0800128 led = devm_kzalloc(dev, sizeof(struct lp8788_led), GFP_KERNEL);
Kim, Miloeb804112012-07-20 16:43:59 +0800129 if (!led)
130 return -ENOMEM;
131
132 led->lp = lp;
133 led->led_dev.max_brightness = MAX_BRIGHTNESS;
Andrew Lunn64998372015-08-20 12:27:05 +0200134 led->led_dev.brightness_set_blocking = lp8788_brightness_set;
Kim, Miloeb804112012-07-20 16:43:59 +0800135
136 led_pdata = lp->pdata ? lp->pdata->led_pdata : NULL;
137
138 if (!led_pdata || !led_pdata->name)
139 led->led_dev.name = DEFAULT_LED_NAME;
140 else
141 led->led_dev.name = led_pdata->name;
142
143 mutex_init(&led->lock);
Kim, Miloeb804112012-07-20 16:43:59 +0800144
Kim, Miloeb804112012-07-20 16:43:59 +0800145 ret = lp8788_led_init_device(led, led_pdata);
146 if (ret) {
Kim, Miloa1932ed2013-01-02 22:28:12 -0800147 dev_err(dev, "led init device err: %d\n", ret);
Kim, Miloeb804112012-07-20 16:43:59 +0800148 return ret;
149 }
150
Amitoj Kaur Chawlaf165a662016-03-09 08:53:38 +0530151 ret = devm_led_classdev_register(dev, &led->led_dev);
Kim, Miloeb804112012-07-20 16:43:59 +0800152 if (ret) {
Kim, Miloa1932ed2013-01-02 22:28:12 -0800153 dev_err(dev, "led register err: %d\n", ret);
Kim, Miloeb804112012-07-20 16:43:59 +0800154 return ret;
155 }
156
157 return 0;
158}
159
Kim, Miloeb804112012-07-20 16:43:59 +0800160static struct platform_driver lp8788_led_driver = {
161 .probe = lp8788_led_probe,
Kim, Miloeb804112012-07-20 16:43:59 +0800162 .driver = {
163 .name = LP8788_DEV_KEYLED,
Kim, Miloeb804112012-07-20 16:43:59 +0800164 },
165};
166module_platform_driver(lp8788_led_driver);
167
168MODULE_DESCRIPTION("Texas Instruments LP8788 Keyboard LED Driver");
169MODULE_AUTHOR("Milo Kim");
170MODULE_LICENSE("GPL");
171MODULE_ALIAS("platform:lp8788-keyled");