1386570d7SVadim Pasternak // SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause)
2386570d7SVadim Pasternak //
3386570d7SVadim Pasternak // Copyright (c) 2018 Mellanox Technologies. All rights reserved.
4386570d7SVadim Pasternak // Copyright (c) 2018 Vadim Pasternak <vadimp@mellanox.com>
5386570d7SVadim Pasternak
6386570d7SVadim Pasternak #include <linux/bitops.h>
7386570d7SVadim Pasternak #include <linux/device.h>
8386570d7SVadim Pasternak #include <linux/io.h>
9386570d7SVadim Pasternak #include <linux/leds.h>
10386570d7SVadim Pasternak #include <linux/module.h>
11386570d7SVadim Pasternak #include <linux/platform_data/mlxreg.h>
12386570d7SVadim Pasternak #include <linux/platform_device.h>
13386570d7SVadim Pasternak #include <linux/regmap.h>
14386570d7SVadim Pasternak
15386570d7SVadim Pasternak /* Codes for LEDs. */
16386570d7SVadim Pasternak #define MLXREG_LED_OFFSET_BLINK_3HZ 0x01 /* Offset from solid: 3Hz blink */
17386570d7SVadim Pasternak #define MLXREG_LED_OFFSET_BLINK_6HZ 0x02 /* Offset from solid: 6Hz blink */
18386570d7SVadim Pasternak #define MLXREG_LED_IS_OFF 0x00 /* Off */
19386570d7SVadim Pasternak #define MLXREG_LED_RED_SOLID 0x05 /* Solid red */
20386570d7SVadim Pasternak #define MLXREG_LED_GREEN_SOLID 0x0D /* Solid green */
21386570d7SVadim Pasternak #define MLXREG_LED_AMBER_SOLID 0x09 /* Solid amber */
22386570d7SVadim Pasternak #define MLXREG_LED_BLINK_3HZ 167 /* ~167 msec off/on - HW support */
23386570d7SVadim Pasternak #define MLXREG_LED_BLINK_6HZ 83 /* ~83 msec off/on - HW support */
24530451d0SVadim Pasternak #define MLXREG_LED_CAPABILITY_CLEAR GENMASK(31, 8) /* Clear mask */
25386570d7SVadim Pasternak
26386570d7SVadim Pasternak /**
27386570d7SVadim Pasternak * struct mlxreg_led_data - led control data:
28386570d7SVadim Pasternak *
29386570d7SVadim Pasternak * @data: led configuration data;
30aedc1357SLee Jones * @led_cdev: led class data;
31386570d7SVadim Pasternak * @base_color: base led color (other colors have constant offset from base);
32386570d7SVadim Pasternak * @led_data: led data;
33386570d7SVadim Pasternak * @data_parent: pointer to private device control data of parent;
34aedc1357SLee Jones * @led_cdev_name: class device name
35386570d7SVadim Pasternak */
36386570d7SVadim Pasternak struct mlxreg_led_data {
37386570d7SVadim Pasternak struct mlxreg_core_data *data;
38386570d7SVadim Pasternak struct led_classdev led_cdev;
39386570d7SVadim Pasternak u8 base_color;
40386570d7SVadim Pasternak void *data_parent;
41386570d7SVadim Pasternak char led_cdev_name[MLXREG_CORE_LABEL_MAX_SIZE];
42386570d7SVadim Pasternak };
43386570d7SVadim Pasternak
44386570d7SVadim Pasternak #define cdev_to_priv(c) container_of(c, struct mlxreg_led_data, led_cdev)
45386570d7SVadim Pasternak
46386570d7SVadim Pasternak /**
47386570d7SVadim Pasternak * struct mlxreg_led_priv_data - platform private data:
48386570d7SVadim Pasternak *
49386570d7SVadim Pasternak * @pdev: platform device;
50386570d7SVadim Pasternak * @pdata: platform data;
51386570d7SVadim Pasternak * @access_lock: mutex for attribute IO access;
52386570d7SVadim Pasternak */
53386570d7SVadim Pasternak struct mlxreg_led_priv_data {
54386570d7SVadim Pasternak struct platform_device *pdev;
55386570d7SVadim Pasternak struct mlxreg_core_platform_data *pdata;
56386570d7SVadim Pasternak struct mutex access_lock; /* protect IO operations */
57386570d7SVadim Pasternak };
58386570d7SVadim Pasternak
59386570d7SVadim Pasternak static int
mlxreg_led_store_hw(struct mlxreg_led_data * led_data,u8 vset)60386570d7SVadim Pasternak mlxreg_led_store_hw(struct mlxreg_led_data *led_data, u8 vset)
61386570d7SVadim Pasternak {
62386570d7SVadim Pasternak struct mlxreg_led_priv_data *priv = led_data->data_parent;
63386570d7SVadim Pasternak struct mlxreg_core_platform_data *led_pdata = priv->pdata;
64386570d7SVadim Pasternak struct mlxreg_core_data *data = led_data->data;
65386570d7SVadim Pasternak u32 regval;
66386570d7SVadim Pasternak u32 nib;
67386570d7SVadim Pasternak int ret;
68386570d7SVadim Pasternak
69386570d7SVadim Pasternak /*
70386570d7SVadim Pasternak * Each LED is controlled through low or high nibble of the relevant
71386570d7SVadim Pasternak * register byte. Register offset is specified by off parameter.
72386570d7SVadim Pasternak * Parameter vset provides color code: 0x0 for off, 0x5 for solid red,
73386570d7SVadim Pasternak * 0x6 for 3Hz blink red, 0xd for solid green, 0xe for 3Hz blink
74386570d7SVadim Pasternak * green.
75386570d7SVadim Pasternak * Parameter mask specifies which nibble is used for specific LED: mask
76386570d7SVadim Pasternak * 0xf0 - lower nibble is to be used (bits from 0 to 3), mask 0x0f -
77386570d7SVadim Pasternak * higher nibble (bits from 4 to 7).
78386570d7SVadim Pasternak */
79386570d7SVadim Pasternak mutex_lock(&priv->access_lock);
80386570d7SVadim Pasternak
81386570d7SVadim Pasternak ret = regmap_read(led_pdata->regmap, data->reg, ®val);
82386570d7SVadim Pasternak if (ret)
83386570d7SVadim Pasternak goto access_error;
84386570d7SVadim Pasternak
85386570d7SVadim Pasternak nib = (ror32(data->mask, data->bit) == 0xf0) ? rol32(vset, data->bit) :
86386570d7SVadim Pasternak rol32(vset, data->bit + 4);
87386570d7SVadim Pasternak regval = (regval & data->mask) | nib;
88386570d7SVadim Pasternak
89386570d7SVadim Pasternak ret = regmap_write(led_pdata->regmap, data->reg, regval);
90386570d7SVadim Pasternak
91386570d7SVadim Pasternak access_error:
92386570d7SVadim Pasternak mutex_unlock(&priv->access_lock);
93386570d7SVadim Pasternak
94386570d7SVadim Pasternak return ret;
95386570d7SVadim Pasternak }
96386570d7SVadim Pasternak
97386570d7SVadim Pasternak static enum led_brightness
mlxreg_led_get_hw(struct mlxreg_led_data * led_data)98386570d7SVadim Pasternak mlxreg_led_get_hw(struct mlxreg_led_data *led_data)
99386570d7SVadim Pasternak {
100386570d7SVadim Pasternak struct mlxreg_led_priv_data *priv = led_data->data_parent;
101386570d7SVadim Pasternak struct mlxreg_core_platform_data *led_pdata = priv->pdata;
102386570d7SVadim Pasternak struct mlxreg_core_data *data = led_data->data;
103386570d7SVadim Pasternak u32 regval;
104386570d7SVadim Pasternak int err;
105386570d7SVadim Pasternak
106386570d7SVadim Pasternak /*
107386570d7SVadim Pasternak * Each LED is controlled through low or high nibble of the relevant
108386570d7SVadim Pasternak * register byte. Register offset is specified by off parameter.
109386570d7SVadim Pasternak * Parameter vset provides color code: 0x0 for off, 0x5 for solid red,
110386570d7SVadim Pasternak * 0x6 for 3Hz blink red, 0xd for solid green, 0xe for 3Hz blink
111386570d7SVadim Pasternak * green.
112386570d7SVadim Pasternak * Parameter mask specifies which nibble is used for specific LED: mask
113386570d7SVadim Pasternak * 0xf0 - lower nibble is to be used (bits from 0 to 3), mask 0x0f -
114386570d7SVadim Pasternak * higher nibble (bits from 4 to 7).
115386570d7SVadim Pasternak */
116386570d7SVadim Pasternak err = regmap_read(led_pdata->regmap, data->reg, ®val);
117386570d7SVadim Pasternak if (err < 0) {
118386570d7SVadim Pasternak dev_warn(led_data->led_cdev.dev, "Failed to get current brightness, error: %d\n",
119386570d7SVadim Pasternak err);
120386570d7SVadim Pasternak /* Assume the LED is OFF */
121386570d7SVadim Pasternak return LED_OFF;
122386570d7SVadim Pasternak }
123386570d7SVadim Pasternak
124386570d7SVadim Pasternak regval = regval & ~data->mask;
125386570d7SVadim Pasternak regval = (ror32(data->mask, data->bit) == 0xf0) ? ror32(regval,
126386570d7SVadim Pasternak data->bit) : ror32(regval, data->bit + 4);
127386570d7SVadim Pasternak if (regval >= led_data->base_color &&
128386570d7SVadim Pasternak regval <= (led_data->base_color + MLXREG_LED_OFFSET_BLINK_6HZ))
129386570d7SVadim Pasternak return LED_FULL;
130386570d7SVadim Pasternak
131386570d7SVadim Pasternak return LED_OFF;
132386570d7SVadim Pasternak }
133386570d7SVadim Pasternak
134386570d7SVadim Pasternak static int
mlxreg_led_brightness_set(struct led_classdev * cled,enum led_brightness value)135386570d7SVadim Pasternak mlxreg_led_brightness_set(struct led_classdev *cled, enum led_brightness value)
136386570d7SVadim Pasternak {
137386570d7SVadim Pasternak struct mlxreg_led_data *led_data = cdev_to_priv(cled);
138386570d7SVadim Pasternak
139386570d7SVadim Pasternak if (value)
140386570d7SVadim Pasternak return mlxreg_led_store_hw(led_data, led_data->base_color);
141386570d7SVadim Pasternak else
142386570d7SVadim Pasternak return mlxreg_led_store_hw(led_data, MLXREG_LED_IS_OFF);
143386570d7SVadim Pasternak }
144386570d7SVadim Pasternak
145386570d7SVadim Pasternak static enum led_brightness
mlxreg_led_brightness_get(struct led_classdev * cled)146386570d7SVadim Pasternak mlxreg_led_brightness_get(struct led_classdev *cled)
147386570d7SVadim Pasternak {
148386570d7SVadim Pasternak struct mlxreg_led_data *led_data = cdev_to_priv(cled);
149386570d7SVadim Pasternak
150386570d7SVadim Pasternak return mlxreg_led_get_hw(led_data);
151386570d7SVadim Pasternak }
152386570d7SVadim Pasternak
153386570d7SVadim Pasternak static int
mlxreg_led_blink_set(struct led_classdev * cled,unsigned long * delay_on,unsigned long * delay_off)154386570d7SVadim Pasternak mlxreg_led_blink_set(struct led_classdev *cled, unsigned long *delay_on,
155386570d7SVadim Pasternak unsigned long *delay_off)
156386570d7SVadim Pasternak {
157386570d7SVadim Pasternak struct mlxreg_led_data *led_data = cdev_to_priv(cled);
158386570d7SVadim Pasternak int err;
159386570d7SVadim Pasternak
160386570d7SVadim Pasternak /*
161386570d7SVadim Pasternak * HW supports two types of blinking: full (6Hz) and half (3Hz).
162386570d7SVadim Pasternak * For delay on/off zero LED is setting to solid color. For others
163386570d7SVadim Pasternak * combination blinking is to be controlled by the software timer.
164386570d7SVadim Pasternak */
165386570d7SVadim Pasternak if (!(*delay_on == 0 && *delay_off == 0) &&
166386570d7SVadim Pasternak !(*delay_on == MLXREG_LED_BLINK_3HZ &&
167386570d7SVadim Pasternak *delay_off == MLXREG_LED_BLINK_3HZ) &&
168386570d7SVadim Pasternak !(*delay_on == MLXREG_LED_BLINK_6HZ &&
169386570d7SVadim Pasternak *delay_off == MLXREG_LED_BLINK_6HZ))
170386570d7SVadim Pasternak return -EINVAL;
171386570d7SVadim Pasternak
172386570d7SVadim Pasternak if (*delay_on == MLXREG_LED_BLINK_6HZ)
173386570d7SVadim Pasternak err = mlxreg_led_store_hw(led_data, led_data->base_color +
174386570d7SVadim Pasternak MLXREG_LED_OFFSET_BLINK_6HZ);
175386570d7SVadim Pasternak else if (*delay_on == MLXREG_LED_BLINK_3HZ)
176386570d7SVadim Pasternak err = mlxreg_led_store_hw(led_data, led_data->base_color +
177386570d7SVadim Pasternak MLXREG_LED_OFFSET_BLINK_3HZ);
178386570d7SVadim Pasternak else
179386570d7SVadim Pasternak err = mlxreg_led_store_hw(led_data, led_data->base_color);
180386570d7SVadim Pasternak
181386570d7SVadim Pasternak return err;
182386570d7SVadim Pasternak }
183386570d7SVadim Pasternak
mlxreg_led_config(struct mlxreg_led_priv_data * priv)184386570d7SVadim Pasternak static int mlxreg_led_config(struct mlxreg_led_priv_data *priv)
185386570d7SVadim Pasternak {
186386570d7SVadim Pasternak struct mlxreg_core_platform_data *led_pdata = priv->pdata;
187386570d7SVadim Pasternak struct mlxreg_core_data *data = led_pdata->data;
188386570d7SVadim Pasternak struct mlxreg_led_data *led_data;
189386570d7SVadim Pasternak struct led_classdev *led_cdev;
190386570d7SVadim Pasternak enum led_brightness brightness;
191530451d0SVadim Pasternak u32 regval;
192386570d7SVadim Pasternak int i;
193386570d7SVadim Pasternak int err;
194386570d7SVadim Pasternak
195386570d7SVadim Pasternak for (i = 0; i < led_pdata->counter; i++, data++) {
196386570d7SVadim Pasternak led_data = devm_kzalloc(&priv->pdev->dev, sizeof(*led_data),
197386570d7SVadim Pasternak GFP_KERNEL);
198386570d7SVadim Pasternak if (!led_data)
199386570d7SVadim Pasternak return -ENOMEM;
200386570d7SVadim Pasternak
201530451d0SVadim Pasternak if (data->capability) {
202530451d0SVadim Pasternak err = regmap_read(led_pdata->regmap, data->capability,
203530451d0SVadim Pasternak ®val);
204530451d0SVadim Pasternak if (err) {
205530451d0SVadim Pasternak dev_err(&priv->pdev->dev, "Failed to query capability register\n");
206530451d0SVadim Pasternak return err;
207530451d0SVadim Pasternak }
208530451d0SVadim Pasternak if (!(regval & data->bit))
209530451d0SVadim Pasternak continue;
210530451d0SVadim Pasternak /*
211530451d0SVadim Pasternak * Field "bit" can contain one capability bit in 0 byte
212530451d0SVadim Pasternak * and offset bit in 1-3 bytes. Clear capability bit and
213530451d0SVadim Pasternak * keep only offset bit.
214530451d0SVadim Pasternak */
215530451d0SVadim Pasternak data->bit &= MLXREG_LED_CAPABILITY_CLEAR;
216530451d0SVadim Pasternak }
217530451d0SVadim Pasternak
218386570d7SVadim Pasternak led_cdev = &led_data->led_cdev;
219386570d7SVadim Pasternak led_data->data_parent = priv;
220386570d7SVadim Pasternak if (strstr(data->label, "red") ||
221386570d7SVadim Pasternak strstr(data->label, "orange")) {
222386570d7SVadim Pasternak brightness = LED_OFF;
223386570d7SVadim Pasternak led_data->base_color = MLXREG_LED_RED_SOLID;
224386570d7SVadim Pasternak } else if (strstr(data->label, "amber")) {
225386570d7SVadim Pasternak brightness = LED_OFF;
226386570d7SVadim Pasternak led_data->base_color = MLXREG_LED_AMBER_SOLID;
227386570d7SVadim Pasternak } else {
228386570d7SVadim Pasternak brightness = LED_OFF;
229386570d7SVadim Pasternak led_data->base_color = MLXREG_LED_GREEN_SOLID;
230386570d7SVadim Pasternak }
2317c6082b9SOleh Kravchenko snprintf(led_data->led_cdev_name, sizeof(led_data->led_cdev_name),
2327c6082b9SOleh Kravchenko "mlxreg:%s", data->label);
233386570d7SVadim Pasternak led_cdev->name = led_data->led_cdev_name;
234386570d7SVadim Pasternak led_cdev->brightness = brightness;
235386570d7SVadim Pasternak led_cdev->max_brightness = LED_ON;
236386570d7SVadim Pasternak led_cdev->brightness_set_blocking =
237386570d7SVadim Pasternak mlxreg_led_brightness_set;
238386570d7SVadim Pasternak led_cdev->brightness_get = mlxreg_led_brightness_get;
239386570d7SVadim Pasternak led_cdev->blink_set = mlxreg_led_blink_set;
240386570d7SVadim Pasternak led_cdev->flags = LED_CORE_SUSPENDRESUME;
241386570d7SVadim Pasternak led_data->data = data;
242386570d7SVadim Pasternak err = devm_led_classdev_register(&priv->pdev->dev, led_cdev);
243386570d7SVadim Pasternak if (err)
244386570d7SVadim Pasternak return err;
245386570d7SVadim Pasternak
246386570d7SVadim Pasternak if (led_cdev->brightness)
247386570d7SVadim Pasternak mlxreg_led_brightness_set(led_cdev,
248386570d7SVadim Pasternak led_cdev->brightness);
249386570d7SVadim Pasternak dev_info(led_cdev->dev, "label: %s, mask: 0x%02x, offset:0x%02x\n",
250386570d7SVadim Pasternak data->label, data->mask, data->reg);
251386570d7SVadim Pasternak }
252386570d7SVadim Pasternak
253386570d7SVadim Pasternak return 0;
254386570d7SVadim Pasternak }
255386570d7SVadim Pasternak
mlxreg_led_probe(struct platform_device * pdev)256386570d7SVadim Pasternak static int mlxreg_led_probe(struct platform_device *pdev)
257386570d7SVadim Pasternak {
258386570d7SVadim Pasternak struct mlxreg_core_platform_data *led_pdata;
259386570d7SVadim Pasternak struct mlxreg_led_priv_data *priv;
260*172ffd26SGeorge Stark int err;
261386570d7SVadim Pasternak
262386570d7SVadim Pasternak led_pdata = dev_get_platdata(&pdev->dev);
263386570d7SVadim Pasternak if (!led_pdata) {
264386570d7SVadim Pasternak dev_err(&pdev->dev, "Failed to get platform data.\n");
265386570d7SVadim Pasternak return -EINVAL;
266386570d7SVadim Pasternak }
267386570d7SVadim Pasternak
268386570d7SVadim Pasternak priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
269386570d7SVadim Pasternak if (!priv)
270386570d7SVadim Pasternak return -ENOMEM;
271386570d7SVadim Pasternak
272*172ffd26SGeorge Stark err = devm_mutex_init(&pdev->dev, &priv->access_lock);
273*172ffd26SGeorge Stark if (err)
274*172ffd26SGeorge Stark return err;
275*172ffd26SGeorge Stark
276386570d7SVadim Pasternak priv->pdev = pdev;
277386570d7SVadim Pasternak priv->pdata = led_pdata;
278386570d7SVadim Pasternak
279386570d7SVadim Pasternak return mlxreg_led_config(priv);
280386570d7SVadim Pasternak }
281386570d7SVadim Pasternak
282386570d7SVadim Pasternak static struct platform_driver mlxreg_led_driver = {
283386570d7SVadim Pasternak .driver = {
284386570d7SVadim Pasternak .name = "leds-mlxreg",
285386570d7SVadim Pasternak },
286386570d7SVadim Pasternak .probe = mlxreg_led_probe,
287386570d7SVadim Pasternak };
288386570d7SVadim Pasternak
289386570d7SVadim Pasternak module_platform_driver(mlxreg_led_driver);
290386570d7SVadim Pasternak
291386570d7SVadim Pasternak MODULE_AUTHOR("Vadim Pasternak <vadimp@mellanox.com>");
292386570d7SVadim Pasternak MODULE_DESCRIPTION("Mellanox LED regmap driver");
293386570d7SVadim Pasternak MODULE_LICENSE("Dual BSD/GPL");
294386570d7SVadim Pasternak MODULE_ALIAS("platform:leds-mlxreg");
295