blob: fb2ab72c0c40c78b812e3b5658862bfb3e227e55 [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Linus Walleij7f866982016-08-16 11:25:10 +02002/* Copyright (c) 2010, 2011, 2016 The Linux Foundation. All rights reserved.
Linus Walleij7f866982016-08-16 11:25:10 +02003 */
4#include <linux/leds.h>
5#include <linux/module.h>
6#include <linux/of.h>
7#include <linux/of_device.h>
8#include <linux/platform_device.h>
9#include <linux/pm.h>
10#include <linux/regmap.h>
11
12#define PM8058_LED_TYPE_COMMON 0x00
13#define PM8058_LED_TYPE_KEYPAD 0x01
14#define PM8058_LED_TYPE_FLASH 0x02
15
16#define PM8058_LED_TYPE_COMMON_MASK 0xf8
17#define PM8058_LED_TYPE_KEYPAD_MASK 0xf0
18#define PM8058_LED_TYPE_COMMON_SHIFT 3
19#define PM8058_LED_TYPE_KEYPAD_SHIFT 4
20
21struct pm8058_led {
22 struct regmap *map;
23 u32 reg;
24 u32 ledtype;
25 struct led_classdev cdev;
26};
27
28static void pm8058_led_set(struct led_classdev *cled,
29 enum led_brightness value)
30{
31 struct pm8058_led *led;
32 int ret = 0;
33 unsigned int mask = 0;
34 unsigned int val = 0;
35
36 led = container_of(cled, struct pm8058_led, cdev);
37 switch (led->ledtype) {
38 case PM8058_LED_TYPE_COMMON:
39 mask = PM8058_LED_TYPE_COMMON_MASK;
40 val = value << PM8058_LED_TYPE_COMMON_SHIFT;
41 break;
42 case PM8058_LED_TYPE_KEYPAD:
43 case PM8058_LED_TYPE_FLASH:
44 mask = PM8058_LED_TYPE_KEYPAD_MASK;
45 val = value << PM8058_LED_TYPE_KEYPAD_SHIFT;
46 break;
47 default:
48 break;
49 }
50
51 ret = regmap_update_bits(led->map, led->reg, mask, val);
52 if (ret)
53 pr_err("Failed to set LED brightness\n");
54}
55
56static enum led_brightness pm8058_led_get(struct led_classdev *cled)
57{
58 struct pm8058_led *led;
59 int ret;
60 unsigned int val;
61
62 led = container_of(cled, struct pm8058_led, cdev);
63
64 ret = regmap_read(led->map, led->reg, &val);
65 if (ret) {
66 pr_err("Failed to get LED brightness\n");
67 return LED_OFF;
68 }
69
70 switch (led->ledtype) {
71 case PM8058_LED_TYPE_COMMON:
72 val &= PM8058_LED_TYPE_COMMON_MASK;
73 val >>= PM8058_LED_TYPE_COMMON_SHIFT;
74 break;
75 case PM8058_LED_TYPE_KEYPAD:
76 case PM8058_LED_TYPE_FLASH:
77 val &= PM8058_LED_TYPE_KEYPAD_MASK;
78 val >>= PM8058_LED_TYPE_KEYPAD_SHIFT;
79 break;
80 default:
81 val = LED_OFF;
82 break;
83 }
84
85 return val;
86}
87
88static int pm8058_led_probe(struct platform_device *pdev)
89{
Marek Behúneed951b2020-09-18 00:33:08 +020090 struct led_init_data init_data = {};
Marek Behúnfb270ba2020-09-18 00:33:09 +020091 struct device *dev = &pdev->dev;
Linus Walleij7f866982016-08-16 11:25:10 +020092 struct pm8058_led *led;
Marek Behúnfb270ba2020-09-18 00:33:09 +020093 struct device_node *np;
Linus Walleij7f866982016-08-16 11:25:10 +020094 int ret;
95 struct regmap *map;
96 const char *state;
97 enum led_brightness maxbright;
98
Marek Behúnfb270ba2020-09-18 00:33:09 +020099 led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
Linus Walleij7f866982016-08-16 11:25:10 +0200100 if (!led)
101 return -ENOMEM;
102
Marek Behúnfb270ba2020-09-18 00:33:09 +0200103 led->ledtype = (u32)(unsigned long)of_device_get_match_data(dev);
Linus Walleij7f866982016-08-16 11:25:10 +0200104
Marek Behúnfb270ba2020-09-18 00:33:09 +0200105 map = dev_get_regmap(dev->parent, NULL);
Linus Walleij7f866982016-08-16 11:25:10 +0200106 if (!map) {
Marek Behúnfb270ba2020-09-18 00:33:09 +0200107 dev_err(dev, "Parent regmap unavailable.\n");
Linus Walleij7f866982016-08-16 11:25:10 +0200108 return -ENXIO;
109 }
110 led->map = map;
111
Marek Behúnfb270ba2020-09-18 00:33:09 +0200112 np = dev_of_node(dev);
113
Linus Walleij7f866982016-08-16 11:25:10 +0200114 ret = of_property_read_u32(np, "reg", &led->reg);
115 if (ret) {
Marek Behúnfb270ba2020-09-18 00:33:09 +0200116 dev_err(dev, "no register offset specified\n");
Linus Walleij7f866982016-08-16 11:25:10 +0200117 return -EINVAL;
118 }
119
Linus Walleij7f866982016-08-16 11:25:10 +0200120 led->cdev.brightness_set = pm8058_led_set;
121 led->cdev.brightness_get = pm8058_led_get;
122 if (led->ledtype == PM8058_LED_TYPE_COMMON)
123 maxbright = 31; /* 5 bits */
124 else
125 maxbright = 15; /* 4 bits */
126 led->cdev.max_brightness = maxbright;
127
128 state = of_get_property(np, "default-state", NULL);
129 if (state) {
130 if (!strcmp(state, "keep")) {
131 led->cdev.brightness = pm8058_led_get(&led->cdev);
132 } else if (!strcmp(state, "on")) {
133 led->cdev.brightness = maxbright;
134 pm8058_led_set(&led->cdev, maxbright);
135 } else {
136 led->cdev.brightness = LED_OFF;
137 pm8058_led_set(&led->cdev, LED_OFF);
138 }
139 }
140
141 if (led->ledtype == PM8058_LED_TYPE_KEYPAD ||
142 led->ledtype == PM8058_LED_TYPE_FLASH)
143 led->cdev.flags = LED_CORE_SUSPENDRESUME;
144
Marek Behúneed951b2020-09-18 00:33:08 +0200145 init_data.fwnode = of_fwnode_handle(np);
146
Marek Behúnfb270ba2020-09-18 00:33:09 +0200147 ret = devm_led_classdev_register_ext(dev, &led->cdev, &init_data);
Marek Behún1016daf92020-09-18 00:33:10 +0200148 if (ret)
Marek Behúnfb270ba2020-09-18 00:33:09 +0200149 dev_err(dev, "Failed to register LED for %pOF\n", np);
Linus Walleij7f866982016-08-16 11:25:10 +0200150
Marek Behún1016daf92020-09-18 00:33:10 +0200151 return ret;
Linus Walleij7f866982016-08-16 11:25:10 +0200152}
153
154static const struct of_device_id pm8058_leds_id_table[] = {
155 {
156 .compatible = "qcom,pm8058-led",
157 .data = (void *)PM8058_LED_TYPE_COMMON
158 },
159 {
160 .compatible = "qcom,pm8058-keypad-led",
161 .data = (void *)PM8058_LED_TYPE_KEYPAD
162 },
163 {
164 .compatible = "qcom,pm8058-flash-led",
165 .data = (void *)PM8058_LED_TYPE_FLASH
166 },
167 { },
168};
169MODULE_DEVICE_TABLE(of, pm8058_leds_id_table);
170
171static struct platform_driver pm8058_led_driver = {
172 .probe = pm8058_led_probe,
173 .driver = {
174 .name = "pm8058-leds",
175 .of_match_table = pm8058_leds_id_table,
176 },
177};
178module_platform_driver(pm8058_led_driver);
179
180MODULE_DESCRIPTION("PM8058 LEDs driver");
181MODULE_LICENSE("GPL v2");
182MODULE_ALIAS("platform:pm8058-leds");