Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris...
[linux/fpc-iii.git] / drivers / leds / leds-lp8788.c
blob7c2cb384e7ae74f9250b99bc5d5e16479fdd6c3a
1 /*
2 * TI LP8788 MFD - keyled driver
4 * Copyright 2012 Texas Instruments
6 * Author: Milo(Woogyom) Kim <milo.kim@ti.com>
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.
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>
23 #define MAX_BRIGHTNESS LP8788_ISINK_MAX_PWM
24 #define DEFAULT_LED_NAME "keyboard-backlight"
26 struct lp8788_led {
27 struct lp8788 *lp;
28 struct mutex lock;
29 struct work_struct work;
30 struct led_classdev led_dev;
31 enum lp8788_isink_number isink_num;
32 enum led_brightness brightness;
33 int on;
36 struct lp8788_led_config {
37 enum lp8788_isink_scale scale;
38 enum lp8788_isink_number num;
39 int iout;
42 static struct lp8788_led_config default_led_config = {
43 .scale = LP8788_ISINK_SCALE_100mA,
44 .num = LP8788_ISINK_3,
45 .iout = 0,
48 static int lp8788_led_init_device(struct lp8788_led *led,
49 struct lp8788_led_platform_data *pdata)
51 struct lp8788_led_config *cfg = &default_led_config;
52 u8 addr, mask, val;
53 int ret;
55 if (pdata) {
56 cfg->scale = pdata->scale;
57 cfg->num = pdata->num;
58 cfg->iout = pdata->iout_code;
61 led->isink_num = cfg->num;
63 /* scale configuration */
64 addr = LP8788_ISINK_CTRL;
65 mask = 1 << (cfg->num + LP8788_ISINK_SCALE_OFFSET);
66 val = cfg->scale << (cfg->num + LP8788_ISINK_SCALE_OFFSET);
67 ret = lp8788_update_bits(led->lp, addr, mask, val);
68 if (ret)
69 return ret;
71 /* current configuration */
72 addr = lp8788_iout_addr[cfg->num];
73 mask = lp8788_iout_mask[cfg->num];
74 val = cfg->iout;
76 return lp8788_update_bits(led->lp, addr, mask, val);
79 static void lp8788_led_enable(struct lp8788_led *led,
80 enum lp8788_isink_number num, int on)
82 u8 mask = 1 << num;
83 u8 val = on << num;
85 if (lp8788_update_bits(led->lp, LP8788_ISINK_CTRL, mask, val))
86 return;
88 led->on = on;
91 static void lp8788_led_work(struct work_struct *work)
93 struct lp8788_led *led = container_of(work, struct lp8788_led, work);
94 enum lp8788_isink_number num = led->isink_num;
95 int enable;
96 u8 val = led->brightness;
98 mutex_lock(&led->lock);
100 switch (num) {
101 case LP8788_ISINK_1:
102 case LP8788_ISINK_2:
103 case LP8788_ISINK_3:
104 lp8788_write_byte(led->lp, lp8788_pwm_addr[num], val);
105 break;
106 default:
107 mutex_unlock(&led->lock);
108 return;
111 enable = (val > 0) ? 1 : 0;
112 if (enable != led->on)
113 lp8788_led_enable(led, num, enable);
115 mutex_unlock(&led->lock);
118 static void lp8788_brightness_set(struct led_classdev *led_cdev,
119 enum led_brightness brt_val)
121 struct lp8788_led *led =
122 container_of(led_cdev, struct lp8788_led, led_dev);
124 led->brightness = brt_val;
125 schedule_work(&led->work);
128 static int lp8788_led_probe(struct platform_device *pdev)
130 struct lp8788 *lp = dev_get_drvdata(pdev->dev.parent);
131 struct lp8788_led_platform_data *led_pdata;
132 struct lp8788_led *led;
133 struct device *dev = &pdev->dev;
134 int ret;
136 led = devm_kzalloc(dev, sizeof(struct lp8788_led), GFP_KERNEL);
137 if (!led)
138 return -ENOMEM;
140 led->lp = lp;
141 led->led_dev.max_brightness = MAX_BRIGHTNESS;
142 led->led_dev.brightness_set = lp8788_brightness_set;
144 led_pdata = lp->pdata ? lp->pdata->led_pdata : NULL;
146 if (!led_pdata || !led_pdata->name)
147 led->led_dev.name = DEFAULT_LED_NAME;
148 else
149 led->led_dev.name = led_pdata->name;
151 mutex_init(&led->lock);
152 INIT_WORK(&led->work, lp8788_led_work);
154 platform_set_drvdata(pdev, led);
156 ret = lp8788_led_init_device(led, led_pdata);
157 if (ret) {
158 dev_err(dev, "led init device err: %d\n", ret);
159 return ret;
162 ret = led_classdev_register(dev, &led->led_dev);
163 if (ret) {
164 dev_err(dev, "led register err: %d\n", ret);
165 return ret;
168 return 0;
171 static int lp8788_led_remove(struct platform_device *pdev)
173 struct lp8788_led *led = platform_get_drvdata(pdev);
175 led_classdev_unregister(&led->led_dev);
176 flush_work(&led->work);
178 return 0;
181 static struct platform_driver lp8788_led_driver = {
182 .probe = lp8788_led_probe,
183 .remove = lp8788_led_remove,
184 .driver = {
185 .name = LP8788_DEV_KEYLED,
186 .owner = THIS_MODULE,
189 module_platform_driver(lp8788_led_driver);
191 MODULE_DESCRIPTION("Texas Instruments LP8788 Keyboard LED Driver");
192 MODULE_AUTHOR("Milo Kim");
193 MODULE_LICENSE("GPL");
194 MODULE_ALIAS("platform:lp8788-keyled");