blob: 38c253a43700627deebce9751382c645d8c11848 [file] [log] [blame]
Kim, Miloeb804112012-07-20 16:43:59 +08001/*
2 * TI LP8788 MFD - keyled driver
3 *
4 * Copyright 2012 Texas Instruments
5 *
6 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/err.h>
17#include <linux/platform_device.h>
18#include <linux/leds.h>
19#include <linux/mutex.h>
20#include <linux/mfd/lp8788.h>
21#include <linux/mfd/lp8788-isink.h>
22
23#define MAX_BRIGHTNESS LP8788_ISINK_MAX_PWM
24#define DEFAULT_LED_NAME "keyboard-backlight"
25
26struct lp8788_led {
27 struct lp8788 *lp;
28 struct mutex lock;
Kim, Miloeb804112012-07-20 16:43:59 +080029 struct led_classdev led_dev;
30 enum lp8788_isink_number isink_num;
Kim, Miloeb804112012-07-20 16:43:59 +080031 int on;
32};
33
34struct lp8788_led_config {
35 enum lp8788_isink_scale scale;
36 enum lp8788_isink_number num;
37 int iout;
38};
39
40static struct lp8788_led_config default_led_config = {
41 .scale = LP8788_ISINK_SCALE_100mA,
42 .num = LP8788_ISINK_3,
43 .iout = 0,
44};
45
46static int lp8788_led_init_device(struct lp8788_led *led,
47 struct lp8788_led_platform_data *pdata)
48{
49 struct lp8788_led_config *cfg = &default_led_config;
50 u8 addr, mask, val;
51 int ret;
52
53 if (pdata) {
54 cfg->scale = pdata->scale;
55 cfg->num = pdata->num;
56 cfg->iout = pdata->iout_code;
57 }
58
59 led->isink_num = cfg->num;
60
61 /* scale configuration */
62 addr = LP8788_ISINK_CTRL;
63 mask = 1 << (cfg->num + LP8788_ISINK_SCALE_OFFSET);
Axel Lin4df73092012-08-01 20:40:34 +080064 val = cfg->scale << (cfg->num + LP8788_ISINK_SCALE_OFFSET);
Kim, Miloeb804112012-07-20 16:43:59 +080065 ret = lp8788_update_bits(led->lp, addr, mask, val);
66 if (ret)
67 return ret;
68
69 /* current configuration */
70 addr = lp8788_iout_addr[cfg->num];
71 mask = lp8788_iout_mask[cfg->num];
72 val = cfg->iout;
73
74 return lp8788_update_bits(led->lp, addr, mask, val);
75}
76
Andrew Lunn64998372015-08-20 12:27:05 +020077static int lp8788_led_enable(struct lp8788_led *led,
Kim, Miloeb804112012-07-20 16:43:59 +080078 enum lp8788_isink_number num, int on)
79{
Andrew Lunn64998372015-08-20 12:27:05 +020080 int ret;
81
Kim, Miloeb804112012-07-20 16:43:59 +080082 u8 mask = 1 << num;
83 u8 val = on << num;
84
Andrew Lunn64998372015-08-20 12:27:05 +020085 ret = lp8788_update_bits(led->lp, LP8788_ISINK_CTRL, mask, val);
86 if (ret == 0)
87 led->on = on;
Kim, Miloeb804112012-07-20 16:43:59 +080088
Andrew Lunn64998372015-08-20 12:27:05 +020089 return ret;
Kim, Miloeb804112012-07-20 16:43:59 +080090}
91
Andrew Lunn64998372015-08-20 12:27:05 +020092static int lp8788_brightness_set(struct led_classdev *led_cdev,
93 enum led_brightness val)
Kim, Miloeb804112012-07-20 16:43:59 +080094{
Andrew Lunn64998372015-08-20 12:27:05 +020095 struct lp8788_led *led =
96 container_of(led_cdev, struct lp8788_led, led_dev);
97
Kim, Miloeb804112012-07-20 16:43:59 +080098 enum lp8788_isink_number num = led->isink_num;
Andrew Lunn64998372015-08-20 12:27:05 +020099 int enable, ret;
Kim, Miloeb804112012-07-20 16:43:59 +0800100
101 mutex_lock(&led->lock);
102
103 switch (num) {
104 case LP8788_ISINK_1:
105 case LP8788_ISINK_2:
106 case LP8788_ISINK_3:
Andrew Lunn64998372015-08-20 12:27:05 +0200107 ret = lp8788_write_byte(led->lp, lp8788_pwm_addr[num], val);
108 if (ret < 0)
109 goto unlock;
Kim, Miloeb804112012-07-20 16:43:59 +0800110 break;
111 default:
Devendra Nagad45bb112012-07-27 02:00:31 +0800112 mutex_unlock(&led->lock);
Andrew Lunn64998372015-08-20 12:27:05 +0200113 return -EINVAL;
Kim, Miloeb804112012-07-20 16:43:59 +0800114 }
115
116 enable = (val > 0) ? 1 : 0;
117 if (enable != led->on)
Andrew Lunn64998372015-08-20 12:27:05 +0200118 ret = lp8788_led_enable(led, num, enable);
119unlock:
Kim, Miloeb804112012-07-20 16:43:59 +0800120 mutex_unlock(&led->lock);
Andrew Lunn64998372015-08-20 12:27:05 +0200121 return ret;
Kim, Miloeb804112012-07-20 16:43:59 +0800122}
123
Bill Pemberton98ea1ea2012-11-19 13:23:02 -0500124static int lp8788_led_probe(struct platform_device *pdev)
Kim, Miloeb804112012-07-20 16:43:59 +0800125{
126 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
127 struct lp8788_led_platform_data *led_pdata;
128 struct lp8788_led *led;
Kim, Miloa1932ed2013-01-02 22:28:12 -0800129 struct device *dev = &pdev->dev;
Kim, Miloeb804112012-07-20 16:43:59 +0800130 int ret;
131
Kim, Miloa1932ed2013-01-02 22:28:12 -0800132 led = devm_kzalloc(dev, sizeof(struct lp8788_led), GFP_KERNEL);
Kim, Miloeb804112012-07-20 16:43:59 +0800133 if (!led)
134 return -ENOMEM;
135
136 led->lp = lp;
137 led->led_dev.max_brightness = MAX_BRIGHTNESS;
Andrew Lunn64998372015-08-20 12:27:05 +0200138 led->led_dev.brightness_set_blocking = lp8788_brightness_set;
Kim, Miloeb804112012-07-20 16:43:59 +0800139
140 led_pdata = lp->pdata ? lp->pdata->led_pdata : NULL;
141
142 if (!led_pdata || !led_pdata->name)
143 led->led_dev.name = DEFAULT_LED_NAME;
144 else
145 led->led_dev.name = led_pdata->name;
146
147 mutex_init(&led->lock);
Kim, Miloeb804112012-07-20 16:43:59 +0800148
Kim, Miloeb804112012-07-20 16:43:59 +0800149 ret = lp8788_led_init_device(led, led_pdata);
150 if (ret) {
Kim, Miloa1932ed2013-01-02 22:28:12 -0800151 dev_err(dev, "led init device err: %d\n", ret);
Kim, Miloeb804112012-07-20 16:43:59 +0800152 return ret;
153 }
154
Amitoj Kaur Chawlaf165a662016-03-09 08:53:38 +0530155 ret = devm_led_classdev_register(dev, &led->led_dev);
Kim, Miloeb804112012-07-20 16:43:59 +0800156 if (ret) {
Kim, Miloa1932ed2013-01-02 22:28:12 -0800157 dev_err(dev, "led register err: %d\n", ret);
Kim, Miloeb804112012-07-20 16:43:59 +0800158 return ret;
159 }
160
161 return 0;
162}
163
Kim, Miloeb804112012-07-20 16:43:59 +0800164static struct platform_driver lp8788_led_driver = {
165 .probe = lp8788_led_probe,
Kim, Miloeb804112012-07-20 16:43:59 +0800166 .driver = {
167 .name = LP8788_DEV_KEYLED,
Kim, Miloeb804112012-07-20 16:43:59 +0800168 },
169};
170module_platform_driver(lp8788_led_driver);
171
172MODULE_DESCRIPTION("Texas Instruments LP8788 Keyboard LED Driver");
173MODULE_AUTHOR("Milo Kim");
174MODULE_LICENSE("GPL");
175MODULE_ALIAS("platform:lp8788-keyled");