aboutsummaryrefslogtreecommitdiff
path: root/cds9k-led.c
blob: 55f4ce71fcc439b4651f959b26972fceedfed270 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <linux/device.h>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/regmap.h>
#include <linux/leds.h>

#define LED_REG_VALUE  0x0
#define LED_REG_PERIOD 0x1

#define LED_BLINK_MIN_MS 20
#define LED_BLINK_MAX_MS 5100
#define MS_TO_CDS9K(x) ((x) / 10)
#define CDS9K_TO_MS(x) ((x) * 10)

struct cds9k_led {
	struct led_classdev cdev;
	struct regmap *regmap;
	struct device *dev;
	u32 base;
};

int cds9k_led_blink_set(struct led_classdev *led_cdev,
                         unsigned long *delay_on,
                         unsigned long *delay_off)
{
	struct cds9k_led *led = container_of(led_cdev, struct cds9k_led, cdev);
	unsigned long blink_value;
	/* Only 50% duty is supported by controller, so we ignore delay_off */
	blink_value = MS_TO_CDS9K(*delay_on);
	if (blink_value > 255) {
		blink_value = 255;
	}
	*delay_off = *delay_on = CDS9K_TO_MS(blink_value);

	return regmap_write(led->regmap, led->base + LED_REG_PERIOD, blink_value);
}

void cds9k_led_brightness_set(struct led_classdev *led_cdev,
                              enum led_brightness brightness)
{
	unsigned long blink_off = 0;
	struct cds9k_led *led = container_of(led_cdev, struct cds9k_led, cdev);
	regmap_write(led->regmap, led->base + LED_REG_VALUE, brightness);
	if (brightness == LED_OFF)
		cds9k_led_blink_set(led_cdev, &blink_off, &blink_off);
}

enum led_brightness cds9k_led_brightness_get(struct led_classdev *led_cdev)
{
	int ret;
	unsigned int brightness;
	struct cds9k_led *led = container_of(led_cdev, struct cds9k_led, cdev);

	ret = regmap_read(led->regmap, led->base + LED_REG_VALUE, &brightness);
	if (ret) {
		dev_err(led->dev, "Failed to read LED brightness");
		return LED_OFF;
	}

	if (brightness > LED_FULL)
		brightness = LED_FULL;

	return brightness;
}

static int cds9k_led_probe(struct platform_device *pdev)
{
	int ret;
	struct cds9k_led *led;
	struct device_node *np = pdev->dev.of_node;
	struct led_init_data init_data = {};

	led = devm_kzalloc(&pdev->dev, sizeof(*led), GFP_KERNEL);
	if (!led)
		return -ENOMEM;

	ret = device_property_read_u32(&pdev->dev, "reg", &led->base);
	if (ret)
		return -EINVAL;

	if (!pdev->dev.parent)
		return -ENODEV;

	led->regmap = dev_get_regmap(pdev->dev.parent, NULL);
	if (!led->regmap)
		return -ENODEV;

	led->dev = &pdev->dev;
	led->cdev.name = of_get_property(np, "label", NULL) ? : np->name;
	led->cdev.default_trigger = of_get_property(np, "linux,default-trigger", NULL);
	led->cdev.brightness_set = cds9k_led_brightness_set;
	led->cdev.brightness_get = cds9k_led_brightness_get;
	led->cdev.max_brightness = 0xFF;
	led->cdev.blink_set = cds9k_led_blink_set;
	/* FIXME default state keep/on etc */

	init_data.fwnode = of_fwnode_handle(np);
	init_data.devicename = "cds9k_led";

	return devm_led_classdev_register_ext(&pdev->dev, &led->cdev, &init_data);
}

static struct of_device_id cds9k_led_of_match[] = {
	{ .compatible = "david,cds9k-led" },
	{}
};
MODULE_DEVICE_TABLE(of, cds9k_led_of_match);

static struct platform_driver cds9k_led = {
	.driver = {
		.name = "cds9k-led",
		.of_match_table = cds9k_led_of_match,
	},
	.probe = cds9k_led_probe,
};
module_platform_driver(cds9k_led);

MODULE_AUTHOR("David Phillips <david@yeah.nah.nz>");
MODULE_DESCRIPTION("LED driver for the CDS9K board controller");
MODULE_LICENSE("GPL v2");