blob: 0de41de5078d7382d0fd505ff2d771b0e3d437a3 [file] [log] [blame]
Mohan Pallaka19052de2012-04-18 15:32:19 +05301/* Copyright (c) 2012, Code Aurora Forum. All rights reserved.
2 *
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>
19
20#include <mach/pmic.h>
21
22#define LED_MPP(x) ((x) & 0xFF)
23#define LED_CURR(x) ((x) >> 16)
24
25struct pmic_mpp_led_data {
26 struct led_classdev cdev;
27 int curr;
28 int mpp;
29};
30
31static void pm_mpp_led_set(struct led_classdev *led_cdev,
32 enum led_brightness value)
33{
34 struct pmic_mpp_led_data *led;
35 int ret;
36
37 led = container_of(led_cdev, struct pmic_mpp_led_data, cdev);
38
39 if (value < LED_OFF || value > led->cdev.max_brightness) {
40 dev_err(led->cdev.dev, "Invalid brightness value");
41 return;
42 }
43
44 ret = pmic_secure_mpp_config_i_sink(led->mpp, led->curr,
45 value ? PM_MPP__I_SINK__SWITCH_ENA :
46 PM_MPP__I_SINK__SWITCH_DIS);
47 if (ret)
48 dev_err(led_cdev->dev, "can't set mpp led\n");
49}
50
51static int pmic_mpp_led_probe(struct platform_device *pdev)
52{
53 const struct led_platform_data *pdata = pdev->dev.platform_data;
54 struct pmic_mpp_led_data *led, *tmp_led;
55 int i, rc;
56
57 if (!pdata) {
58 dev_err(&pdev->dev, "platform data not supplied\n");
59 return -EINVAL;
60 }
61
62 led = kcalloc(pdata->num_leds, sizeof(*led), GFP_KERNEL);
63 if (!led) {
64 dev_err(&pdev->dev, "failed to alloc memory\n");
65 return -ENOMEM;
66 }
67
68 platform_set_drvdata(pdev, led);
69
70 for (i = 0; i < pdata->num_leds; i++) {
71 tmp_led = &led[i];
72 tmp_led->cdev.name = pdata->leds[i].name;
73 tmp_led->cdev.brightness_set = pm_mpp_led_set;
74 tmp_led->cdev.brightness = LED_OFF;
75 tmp_led->cdev.max_brightness = LED_FULL;
76 tmp_led->mpp = LED_MPP(pdata->leds[i].flags);
77 tmp_led->curr = LED_CURR(pdata->leds[i].flags);
78
79 if (tmp_led->curr < PM_MPP__I_SINK__LEVEL_5mA ||
80 tmp_led->curr > PM_MPP__I_SINK__LEVEL_40mA) {
81 dev_err(&pdev->dev, "invalid current\n");
82 goto unreg_led_cdev;
83 }
84
85 rc = led_classdev_register(&pdev->dev, &tmp_led->cdev);
86 if (rc) {
87 dev_err(&pdev->dev, "failed to register led\n");
88 goto unreg_led_cdev;
89 }
90 }
91
92 return 0;
93
94unreg_led_cdev:
95 while (i)
96 led_classdev_unregister(&led[--i].cdev);
97
98 kfree(led);
99 return rc;
100
101}
102
103static int __devexit pmic_mpp_led_remove(struct platform_device *pdev)
104{
105 const struct led_platform_data *pdata = pdev->dev.platform_data;
106 struct pmic_mpp_led_data *led = platform_get_drvdata(pdev);
107 int i;
108
109 for (i = 0; i < pdata->num_leds; i++)
110 led_classdev_unregister(&led[i].cdev);
111
112 kfree(led);
113
114 return 0;
115}
116
117static struct platform_driver pmic_mpp_led_driver = {
118 .probe = pmic_mpp_led_probe,
119 .remove = __devexit_p(pmic_mpp_led_remove),
120 .driver = {
121 .name = "pmic-mpp-leds",
122 .owner = THIS_MODULE,
123 },
124};
125
126static int __init pmic_mpp_led_init(void)
127{
128 return platform_driver_register(&pmic_mpp_led_driver);
129}
130module_init(pmic_mpp_led_init);
131
132static void __exit pmic_mpp_led_exit(void)
133{
134 platform_driver_unregister(&pmic_mpp_led_driver);
135}
136module_exit(pmic_mpp_led_exit);
137
138MODULE_DESCRIPTION("PMIC MPP LEDs driver");
139MODULE_LICENSE("GPL v2");
140MODULE_ALIAS("platform:pmic-mpp-leds");