1 From patchwork Mon Mar 20 06:47:26 2017
2 Content-Type: text/plain; charset="utf-8"
4 Content-Transfer-Encoding: 7bit
5 Subject: [v6,3/4] leds: Add LED support for MT6323 PMIC
6 From: sean.wang@mediatek.com
7 X-Patchwork-Id: 9633081
8 Message-Id: <1489992447-13007-4-git-send-email-sean.wang@mediatek.com>
9 To: <rpurdie@rpsys.net>, <jacek.anaszewski@gmail.com>, <lee.jones@linaro.org>,
10 <matthias.bgg@gmail.com>, <pavel@ucw.cz>, <robh+dt@kernel.org>,
11 <mark.rutland@arm.com>
12 Cc: devicetree@vger.kernel.org, keyhaede@gmail.com,
13 Sean Wang <sean.wang@mediatek.com>, linux-kernel@vger.kernel.org,
14 linux-mediatek@lists.infradead.org, linux-leds@vger.kernel.org,
15 linux-arm-kernel@lists.infradead.org
16 Date: Mon, 20 Mar 2017 14:47:26 +0800
18 From: Sean Wang <sean.wang@mediatek.com>
20 MT6323 PMIC is a multi-function device that includes LED function.
21 It allows attaching up to 4 LEDs which can either be on, off or dimmed
22 and/or blinked with the controller.
24 Signed-off-by: Sean Wang <sean.wang@mediatek.com>
25 Reviewed-by: Jacek Anaszewski <jacek.anaszewski@gmail.com>
27 drivers/leds/Kconfig | 8 +
28 drivers/leds/Makefile | 1 +
29 drivers/leds/leds-mt6323.c | 502 +++++++++++++++++++++++++++++++++++++++++++++
30 3 files changed, 511 insertions(+)
31 create mode 100644 drivers/leds/leds-mt6323.c
33 --- a/drivers/leds/Kconfig
34 +++ b/drivers/leds/Kconfig
35 @@ -117,6 +117,14 @@ config LEDS_MIKROTIK_RB532
36 This option enables support for the so called "User LED" of
37 Mikrotik's Routerboard 532.
40 + tristate "LED Support for Mediatek MT6323 PMIC"
41 + depends on LEDS_CLASS
42 + depends on MFD_MT6397
44 + This option enables support for on-chip LED drivers found on
45 + Mediatek MT6323 PMIC.
48 tristate "LED Support for Samsung S3C24XX GPIO LEDs"
51 +++ b/drivers/leds/leds-mt6323.c
54 + * LED driver for Mediatek MT6323 PMIC
56 + * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com>
58 + * This program is free software; you can redistribute it and/or
59 + * modify it under the terms of the GNU General Public License as
60 + * published by the Free Software Foundation; either version 2 of
61 + * the License, or (at your option) any later version.
63 + * This program is distributed in the hope that it will be useful,
64 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
65 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
66 + * GNU General Public License for more details.
68 +#include <linux/kernel.h>
69 +#include <linux/leds.h>
70 +#include <linux/mfd/mt6323/registers.h>
71 +#include <linux/mfd/mt6397/core.h>
72 +#include <linux/module.h>
73 +#include <linux/of.h>
74 +#include <linux/platform_device.h>
75 +#include <linux/regmap.h>
78 + * Register field for MT6323_TOP_CKPDN0 to enable
79 + * 32K clock common for LED device.
81 +#define MT6323_RG_DRV_32K_CK_PDN BIT(11)
82 +#define MT6323_RG_DRV_32K_CK_PDN_MASK BIT(11)
85 + * Register field for MT6323_TOP_CKPDN2 to enable
86 + * individual clock for LED device.
88 +#define MT6323_RG_ISINK_CK_PDN(i) BIT(i)
89 +#define MT6323_RG_ISINK_CK_PDN_MASK(i) BIT(i)
92 + * Register field for MT6323_TOP_CKCON1 to select
95 +#define MT6323_RG_ISINK_CK_SEL_MASK(i) (BIT(10) << (i))
98 + * Register for MT6323_ISINK_CON0 to setup the
99 + * duty cycle of the blink.
101 +#define MT6323_ISINK_CON0(i) (MT6323_ISINK0_CON0 + 0x8 * (i))
102 +#define MT6323_ISINK_DIM_DUTY_MASK (0x1f << 8)
103 +#define MT6323_ISINK_DIM_DUTY(i) (((i) << 8) & \
104 + MT6323_ISINK_DIM_DUTY_MASK)
106 +/* Register to setup the period of the blink. */
107 +#define MT6323_ISINK_CON1(i) (MT6323_ISINK0_CON1 + 0x8 * (i))
108 +#define MT6323_ISINK_DIM_FSEL_MASK (0xffff)
109 +#define MT6323_ISINK_DIM_FSEL(i) ((i) & MT6323_ISINK_DIM_FSEL_MASK)
111 +/* Register to control the brightness. */
112 +#define MT6323_ISINK_CON2(i) (MT6323_ISINK0_CON2 + 0x8 * (i))
113 +#define MT6323_ISINK_CH_STEP_SHIFT 12
114 +#define MT6323_ISINK_CH_STEP_MASK (0x7 << 12)
115 +#define MT6323_ISINK_CH_STEP(i) (((i) << 12) & \
116 + MT6323_ISINK_CH_STEP_MASK)
117 +#define MT6323_ISINK_SFSTR0_TC_MASK (0x3 << 1)
118 +#define MT6323_ISINK_SFSTR0_TC(i) (((i) << 1) & \
119 + MT6323_ISINK_SFSTR0_TC_MASK)
120 +#define MT6323_ISINK_SFSTR0_EN_MASK BIT(0)
121 +#define MT6323_ISINK_SFSTR0_EN BIT(0)
123 +/* Register to LED channel enablement. */
124 +#define MT6323_ISINK_CH_EN_MASK(i) BIT(i)
125 +#define MT6323_ISINK_CH_EN(i) BIT(i)
127 +#define MT6323_MAX_PERIOD 10000
128 +#define MT6323_MAX_LEDS 4
129 +#define MT6323_MAX_BRIGHTNESS 6
130 +#define MT6323_UNIT_DUTY 3125
131 +#define MT6323_CAL_HW_DUTY(o, p) DIV_ROUND_CLOSEST((o) * 100000ul,\
132 + (p) * MT6323_UNIT_DUTY)
137 + * struct mt6323_led - state container for the LED device
138 + * @id: the identifier in MT6323 LED device
139 + * @parent: the pointer to MT6323 LED controller
140 + * @cdev: LED class device for this LED device
141 + * @current_brightness: current state of the LED device
145 + struct mt6323_leds *parent;
146 + struct led_classdev cdev;
147 + enum led_brightness current_brightness;
151 + * struct mt6323_leds - state container for holding LED controller
153 + * @dev: the device pointer
154 + * @hw: the underlying hardware providing shared
155 + * bus for the register operations
156 + * @lock: the lock among process context
157 + * @led: the array that contains the state of individual
160 +struct mt6323_leds {
161 + struct device *dev;
162 + struct mt6397_chip *hw;
163 + /* protect among process context */
165 + struct mt6323_led *led[MT6323_MAX_LEDS];
168 +static int mt6323_led_hw_brightness(struct led_classdev *cdev,
169 + enum led_brightness brightness)
171 + struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
172 + struct mt6323_leds *leds = led->parent;
173 + struct regmap *regmap = leds->hw->regmap;
174 + u32 con2_mask = 0, con2_val = 0;
178 + * Setup current output for the corresponding
179 + * brightness level.
181 + con2_mask |= MT6323_ISINK_CH_STEP_MASK |
182 + MT6323_ISINK_SFSTR0_TC_MASK |
183 + MT6323_ISINK_SFSTR0_EN_MASK;
184 + con2_val |= MT6323_ISINK_CH_STEP(brightness - 1) |
185 + MT6323_ISINK_SFSTR0_TC(2) |
186 + MT6323_ISINK_SFSTR0_EN;
188 + ret = regmap_update_bits(regmap, MT6323_ISINK_CON2(led->id),
189 + con2_mask, con2_val);
193 +static int mt6323_led_hw_off(struct led_classdev *cdev)
195 + struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
196 + struct mt6323_leds *leds = led->parent;
197 + struct regmap *regmap = leds->hw->regmap;
198 + unsigned int status;
201 + status = MT6323_ISINK_CH_EN(led->id);
202 + ret = regmap_update_bits(regmap, MT6323_ISINK_EN_CTRL,
203 + MT6323_ISINK_CH_EN_MASK(led->id), ~status);
207 + usleep_range(100, 300);
208 + ret = regmap_update_bits(regmap, MT6323_TOP_CKPDN2,
209 + MT6323_RG_ISINK_CK_PDN_MASK(led->id),
210 + MT6323_RG_ISINK_CK_PDN(led->id));
217 +static enum led_brightness
218 +mt6323_get_led_hw_brightness(struct led_classdev *cdev)
220 + struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
221 + struct mt6323_leds *leds = led->parent;
222 + struct regmap *regmap = leds->hw->regmap;
223 + unsigned int status;
226 + ret = regmap_read(regmap, MT6323_TOP_CKPDN2, &status);
230 + if (status & MT6323_RG_ISINK_CK_PDN_MASK(led->id))
233 + ret = regmap_read(regmap, MT6323_ISINK_EN_CTRL, &status);
237 + if (!(status & MT6323_ISINK_CH_EN(led->id)))
240 + ret = regmap_read(regmap, MT6323_ISINK_CON2(led->id), &status);
244 + return ((status & MT6323_ISINK_CH_STEP_MASK)
245 + >> MT6323_ISINK_CH_STEP_SHIFT) + 1;
248 +static int mt6323_led_hw_on(struct led_classdev *cdev,
249 + enum led_brightness brightness)
251 + struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
252 + struct mt6323_leds *leds = led->parent;
253 + struct regmap *regmap = leds->hw->regmap;
254 + unsigned int status;
258 + * Setup required clock source, enable the corresponding
259 + * clock and channel and let work with continuous blink as
262 + ret = regmap_update_bits(regmap, MT6323_TOP_CKCON1,
263 + MT6323_RG_ISINK_CK_SEL_MASK(led->id), 0);
267 + status = MT6323_RG_ISINK_CK_PDN(led->id);
268 + ret = regmap_update_bits(regmap, MT6323_TOP_CKPDN2,
269 + MT6323_RG_ISINK_CK_PDN_MASK(led->id),
274 + usleep_range(100, 300);
276 + ret = regmap_update_bits(regmap, MT6323_ISINK_EN_CTRL,
277 + MT6323_ISINK_CH_EN_MASK(led->id),
278 + MT6323_ISINK_CH_EN(led->id));
282 + ret = mt6323_led_hw_brightness(cdev, brightness);
286 + ret = regmap_update_bits(regmap, MT6323_ISINK_CON0(led->id),
287 + MT6323_ISINK_DIM_DUTY_MASK,
288 + MT6323_ISINK_DIM_DUTY(31));
292 + ret = regmap_update_bits(regmap, MT6323_ISINK_CON1(led->id),
293 + MT6323_ISINK_DIM_FSEL_MASK,
294 + MT6323_ISINK_DIM_FSEL(1000));
301 +static int mt6323_led_set_blink(struct led_classdev *cdev,
302 + unsigned long *delay_on,
303 + unsigned long *delay_off)
305 + struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
306 + struct mt6323_leds *leds = led->parent;
307 + struct regmap *regmap = leds->hw->regmap;
308 + unsigned long period;
313 + * Units are in ms, if over the hardware able
314 + * to support, fallback into software blink
316 + period = *delay_on + *delay_off;
318 + if (period > MT6323_MAX_PERIOD)
322 + * LED subsystem requires a default user
323 + * friendly blink pattern for the LED so using
324 + * 1Hz duty cycle 50% here if without specific
325 + * value delay_on and delay off being assigned.
327 + if (!*delay_on && !*delay_off) {
333 + * Calculate duty_hw based on the percentage of period during
334 + * which the led is ON.
336 + duty_hw = MT6323_CAL_HW_DUTY(*delay_on, period);
338 + /* hardware doesn't support zero duty cycle. */
342 + mutex_lock(&leds->lock);
344 + * Set max_brightness as the software blink behavior
345 + * when no blink brightness.
347 + if (!led->current_brightness) {
348 + ret = mt6323_led_hw_on(cdev, cdev->max_brightness);
351 + led->current_brightness = cdev->max_brightness;
354 + ret = regmap_update_bits(regmap, MT6323_ISINK_CON0(led->id),
355 + MT6323_ISINK_DIM_DUTY_MASK,
356 + MT6323_ISINK_DIM_DUTY(duty_hw - 1));
360 + ret = regmap_update_bits(regmap, MT6323_ISINK_CON1(led->id),
361 + MT6323_ISINK_DIM_FSEL_MASK,
362 + MT6323_ISINK_DIM_FSEL(period - 1));
364 + mutex_unlock(&leds->lock);
369 +static int mt6323_led_set_brightness(struct led_classdev *cdev,
370 + enum led_brightness brightness)
372 + struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
373 + struct mt6323_leds *leds = led->parent;
376 + mutex_lock(&leds->lock);
378 + if (!led->current_brightness && brightness) {
379 + ret = mt6323_led_hw_on(cdev, brightness);
382 + } else if (brightness) {
383 + ret = mt6323_led_hw_brightness(cdev, brightness);
387 + ret = mt6323_led_hw_off(cdev);
392 + led->current_brightness = brightness;
394 + mutex_unlock(&leds->lock);
399 +static int mt6323_led_set_dt_default(struct led_classdev *cdev,
400 + struct device_node *np)
402 + struct mt6323_led *led = container_of(cdev, struct mt6323_led, cdev);
406 + led->cdev.name = of_get_property(np, "label", NULL) ? : np->name;
407 + led->cdev.default_trigger = of_get_property(np,
408 + "linux,default-trigger",
411 + state = of_get_property(np, "default-state", NULL);
413 + if (!strcmp(state, "keep")) {
414 + ret = mt6323_get_led_hw_brightness(cdev);
417 + led->current_brightness = ret;
419 + } else if (!strcmp(state, "on")) {
421 + mt6323_led_set_brightness(cdev, cdev->max_brightness);
423 + ret = mt6323_led_set_brightness(cdev, LED_OFF);
430 +static int mt6323_led_probe(struct platform_device *pdev)
432 + struct device *dev = &pdev->dev;
433 + struct device_node *np = pdev->dev.of_node;
434 + struct device_node *child;
435 + struct mt6397_chip *hw = dev_get_drvdata(pdev->dev.parent);
436 + struct mt6323_leds *leds;
437 + struct mt6323_led *led;
439 + unsigned int status;
442 + leds = devm_kzalloc(dev, sizeof(*leds), GFP_KERNEL);
446 + platform_set_drvdata(pdev, leds);
450 + * leds->hw points to the underlying bus for the register
454 + mutex_init(&leds->lock);
456 + status = MT6323_RG_DRV_32K_CK_PDN;
457 + ret = regmap_update_bits(leds->hw->regmap, MT6323_TOP_CKPDN0,
458 + MT6323_RG_DRV_32K_CK_PDN_MASK, ~status);
461 + "Failed to update MT6323_TOP_CKPDN0 Register\n");
465 + for_each_available_child_of_node(np, child) {
466 + ret = of_property_read_u32(child, "reg", ®);
468 + dev_err(dev, "Failed to read led 'reg' property\n");
469 + goto put_child_node;
472 + if (reg < 0 || reg > MT6323_MAX_LEDS || leds->led[reg]) {
473 + dev_err(dev, "Invalid led reg %u\n", reg);
475 + goto put_child_node;
478 + led = devm_kzalloc(dev, sizeof(*led), GFP_KERNEL);
481 + goto put_child_node;
484 + leds->led[reg] = led;
485 + leds->led[reg]->id = reg;
486 + leds->led[reg]->cdev.max_brightness = MT6323_MAX_BRIGHTNESS;
487 + leds->led[reg]->cdev.brightness_set_blocking =
488 + mt6323_led_set_brightness;
489 + leds->led[reg]->cdev.blink_set = mt6323_led_set_blink;
490 + leds->led[reg]->cdev.brightness_get =
491 + mt6323_get_led_hw_brightness;
492 + leds->led[reg]->parent = leds;
494 + ret = mt6323_led_set_dt_default(&leds->led[reg]->cdev, child);
497 + "Failed to LED set default from devicetree\n");
498 + goto put_child_node;
501 + ret = devm_led_classdev_register(dev, &leds->led[reg]->cdev);
503 + dev_err(&pdev->dev, "Failed to register LED: %d\n",
505 + goto put_child_node;
507 + leds->led[reg]->cdev.dev->of_node = child;
513 + of_node_put(child);
517 +static int mt6323_led_remove(struct platform_device *pdev)
519 + struct mt6323_leds *leds = platform_get_drvdata(pdev);
522 + /* Turn the LEDs off on driver removal. */
523 + for (i = 0 ; leds->led[i] ; i++)
524 + mt6323_led_hw_off(&leds->led[i]->cdev);
526 + regmap_update_bits(leds->hw->regmap, MT6323_TOP_CKPDN0,
527 + MT6323_RG_DRV_32K_CK_PDN_MASK,
528 + MT6323_RG_DRV_32K_CK_PDN);
530 + mutex_destroy(&leds->lock);
535 +static const struct of_device_id mt6323_led_dt_match[] = {
536 + { .compatible = "mediatek,mt6323-led" },
539 +MODULE_DEVICE_TABLE(of, mt6323_led_dt_match);
541 +static struct platform_driver mt6323_led_driver = {
542 + .probe = mt6323_led_probe,
543 + .remove = mt6323_led_remove,
545 + .name = "mt6323-led",
546 + .of_match_table = mt6323_led_dt_match,
550 +module_platform_driver(mt6323_led_driver);
552 +MODULE_DESCRIPTION("LED driver for Mediatek MT6323 PMIC");
553 +MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>");
554 +MODULE_LICENSE("GPL");