blob: 62ea96bde8c535c817cb58c067f32da76fc6b066 [file] [log] [blame]
Duy Truong790f06d2013-02-13 16:38:12 -08001/* Copyright (c) 2012, The Linux Foundation. All rights reserved.
Mohan Pallaka19052de2012-04-18 15:32:19 +05302 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13
14#include <linux/kernel.h>
15#include <linux/init.h>
16#include <linux/platform_device.h>
17#include <linux/leds.h>
18#include <linux/slab.h>
Steve Mucklef132c6c2012-06-06 18:30:57 -070019#include <linux/module.h>
Mohan Pallaka19052de2012-04-18 15:32:19 +053020
21#include <mach/pmic.h>
22
23#define LED_MPP(x) ((x) & 0xFF)
24#define LED_CURR(x) ((x) >> 16)
25
26struct pmic_mpp_led_data {
27 struct led_classdev cdev;
28 int curr;
29 int mpp;
30};
31
32static void pm_mpp_led_set(struct led_classdev *led_cdev,
33 enum led_brightness value)
34{
35 struct pmic_mpp_led_data *led;
36 int ret;
37
38 led = container_of(led_cdev, struct pmic_mpp_led_data, cdev);
39
40 if (value < LED_OFF || value > led->cdev.max_brightness) {
41 dev_err(led->cdev.dev, "Invalid brightness value");
42 return;
43 }
44
45 ret = pmic_secure_mpp_config_i_sink(led->mpp, led->curr,
46 value ? PM_MPP__I_SINK__SWITCH_ENA :
47 PM_MPP__I_SINK__SWITCH_DIS);
48 if (ret)
49 dev_err(led_cdev->dev, "can't set mpp led\n");
50}
51
52static int pmic_mpp_led_probe(struct platform_device *pdev)
53{
54 const struct led_platform_data *pdata = pdev->dev.platform_data;
55 struct pmic_mpp_led_data *led, *tmp_led;
56 int i, rc;
57
58 if (!pdata) {
59 dev_err(&pdev->dev, "platform data not supplied\n");
60 return -EINVAL;
61 }
62
63 led = kcalloc(pdata->num_leds, sizeof(*led), GFP_KERNEL);
64 if (!led) {
65 dev_err(&pdev->dev, "failed to alloc memory\n");
66 return -ENOMEM;
67 }
68
69 platform_set_drvdata(pdev, led);
70
71 for (i = 0; i < pdata->num_leds; i++) {
72 tmp_led = &led[i];
73 tmp_led->cdev.name = pdata->leds[i].name;
74 tmp_led->cdev.brightness_set = pm_mpp_led_set;
75 tmp_led->cdev.brightness = LED_OFF;
76 tmp_led->cdev.max_brightness = LED_FULL;
77 tmp_led->mpp = LED_MPP(pdata->leds[i].flags);
78 tmp_led->curr = LED_CURR(pdata->leds[i].flags);
79
80 if (tmp_led->curr < PM_MPP__I_SINK__LEVEL_5mA ||
81 tmp_led->curr > PM_MPP__I_SINK__LEVEL_40mA) {
82 dev_err(&pdev->dev, "invalid current\n");
83 goto unreg_led_cdev;
84 }
85
86 rc = led_classdev_register(&pdev->dev, &tmp_led->cdev);
87 if (rc) {
88 dev_err(&pdev->dev, "failed to register led\n");
89 goto unreg_led_cdev;
90 }
91 }
92
93 return 0;
94
95unreg_led_cdev:
96 while (i)
97 led_classdev_unregister(&led[--i].cdev);
98
99 kfree(led);
100 return rc;
101
102}
103
104static int __devexit pmic_mpp_led_remove(struct platform_device *pdev)
105{
106 const struct led_platform_data *pdata = pdev->dev.platform_data;
107 struct pmic_mpp_led_data *led = platform_get_drvdata(pdev);
108 int i;
109
110 for (i = 0; i < pdata->num_leds; i++)
111 led_classdev_unregister(&led[i].cdev);
112
113 kfree(led);
114
115 return 0;
116}
117
118static struct platform_driver pmic_mpp_led_driver = {
119 .probe = pmic_mpp_led_probe,
120 .remove = __devexit_p(pmic_mpp_led_remove),
121 .driver = {
122 .name = "pmic-mpp-leds",
123 .owner = THIS_MODULE,
124 },
125};
126
127static int __init pmic_mpp_led_init(void)
128{
129 return platform_driver_register(&pmic_mpp_led_driver);
130}
131module_init(pmic_mpp_led_init);
132
133static void __exit pmic_mpp_led_exit(void)
134{
135 platform_driver_unregister(&pmic_mpp_led_driver);
136}
137module_exit(pmic_mpp_led_exit);
138
139MODULE_DESCRIPTION("PMIC MPP LEDs driver");
140MODULE_LICENSE("GPL v2");
141MODULE_ALIAS("platform:pmic-mpp-leds");