1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2011 Paul Parsons <lost.distance@yahoo.com>
6 #include <linux/kernel.h>
7 #include <linux/platform_device.h>
8 #include <linux/leds.h>
9 #include <linux/slab.h>
11 #include <linux/mfd/asic3.h>
12 #include <linux/mfd/core.h>
13 #include <linux/module.h>
16 * The HTC ASIC3 LED GPIOs are inputs, not outputs.
17 * Hence we turn the LEDs on/off via the TimeBase register.
21 * When TimeBase is 4 the clock resolution is about 32Hz.
22 * This driver supports hardware blinking with an on+off
23 * period from 62ms (2 clocks) to 125s (4000 clocks).
25 #define MS_TO_CLK(ms) DIV_ROUND_CLOSEST(((ms)*1024), 32000)
26 #define CLK_TO_MS(clk) (((clk)*32000)/1024)
27 #define MAX_CLK 4000 /* Fits into 12-bit Time registers */
28 #define MAX_MS CLK_TO_MS(MAX_CLK)
30 static const unsigned int led_n_base[ASIC3_NUM_LEDS] = {
31 [0] = ASIC3_LED_0_Base,
32 [1] = ASIC3_LED_1_Base,
33 [2] = ASIC3_LED_2_Base,
36 static void brightness_set(struct led_classdev *cdev,
37 enum led_brightness value)
39 struct platform_device *pdev = to_platform_device(cdev->dev->parent);
40 const struct mfd_cell *cell = mfd_get_cell(pdev);
41 struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
45 timebase = (value == LED_OFF) ? 0 : (LED_EN|0x4);
47 base = led_n_base[cell->id];
48 asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), 32);
49 asic3_write_register(asic, (base + ASIC3_LED_DutyTime), 32);
50 asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0);
51 asic3_write_register(asic, (base + ASIC3_LED_TimeBase), timebase);
54 static int blink_set(struct led_classdev *cdev,
55 unsigned long *delay_on,
56 unsigned long *delay_off)
58 struct platform_device *pdev = to_platform_device(cdev->dev->parent);
59 const struct mfd_cell *cell = mfd_get_cell(pdev);
60 struct asic3 *asic = dev_get_drvdata(pdev->dev.parent);
65 if (*delay_on > MAX_MS || *delay_off > MAX_MS)
68 if (*delay_on == 0 && *delay_off == 0) {
69 /* If both are zero then a sensible default should be chosen */
73 on = MS_TO_CLK(*delay_on);
74 off = MS_TO_CLK(*delay_off);
75 if ((on + off) > MAX_CLK)
79 base = led_n_base[cell->id];
80 asic3_write_register(asic, (base + ASIC3_LED_PeriodTime), (on + off));
81 asic3_write_register(asic, (base + ASIC3_LED_DutyTime), on);
82 asic3_write_register(asic, (base + ASIC3_LED_AutoStopCount), 0);
83 asic3_write_register(asic, (base + ASIC3_LED_TimeBase), (LED_EN|0x4));
85 *delay_on = CLK_TO_MS(on);
86 *delay_off = CLK_TO_MS(off);
91 static int asic3_led_probe(struct platform_device *pdev)
93 struct asic3_led *led = dev_get_platdata(&pdev->dev);
96 ret = mfd_cell_enable(pdev);
100 led->cdev = devm_kzalloc(&pdev->dev, sizeof(struct led_classdev),
107 led->cdev->name = led->name;
108 led->cdev->flags = LED_CORE_SUSPENDRESUME;
109 led->cdev->brightness_set = brightness_set;
110 led->cdev->blink_set = blink_set;
111 led->cdev->default_trigger = led->default_trigger;
113 ret = led_classdev_register(&pdev->dev, led->cdev);
120 (void) mfd_cell_disable(pdev);
124 static int asic3_led_remove(struct platform_device *pdev)
126 struct asic3_led *led = dev_get_platdata(&pdev->dev);
128 led_classdev_unregister(led->cdev);
130 return mfd_cell_disable(pdev);
133 #ifdef CONFIG_PM_SLEEP
134 static int asic3_led_suspend(struct device *dev)
136 struct platform_device *pdev = to_platform_device(dev);
137 const struct mfd_cell *cell = mfd_get_cell(pdev);
142 ret = (*cell->suspend)(pdev);
147 static int asic3_led_resume(struct device *dev)
149 struct platform_device *pdev = to_platform_device(dev);
150 const struct mfd_cell *cell = mfd_get_cell(pdev);
155 ret = (*cell->resume)(pdev);
161 static SIMPLE_DEV_PM_OPS(asic3_led_pm_ops, asic3_led_suspend, asic3_led_resume);
163 static struct platform_driver asic3_led_driver = {
164 .probe = asic3_led_probe,
165 .remove = asic3_led_remove,
167 .name = "leds-asic3",
168 .pm = &asic3_led_pm_ops,
172 module_platform_driver(asic3_led_driver);
174 MODULE_AUTHOR("Paul Parsons <lost.distance@yahoo.com>");
175 MODULE_DESCRIPTION("HTC ASIC3 LED driver");
176 MODULE_LICENSE("GPL");
177 MODULE_ALIAS("platform:leds-asic3");