aboutsummaryrefslogtreecommitdiff
path: root/cds9k-led.c
blob: 3f976cc609d67cb69002ce143cad2f2f123d06b4 (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
#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_RATE  0x1

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

void cds9k_led_brightness_set(struct led_classdev *led_cdev, enum led_brightness brightness)
{
	struct cds9k_led *led = container_of(led_cdev, struct cds9k_led, cdev);

	regmap_write(led->regmap, led->base + LED_REG_VALUE, brightness);
}

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);
	/* FIXME log something */
	if (!ret)
		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->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;
	/* FIXME default state keep/on etc */

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

	ret = devm_led_classdev_register_ext(&pdev->dev, &led->cdev, &init_data);
	if (ret)
		return ret;

	return 0;
}

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,
	//.shutdown = cds9k_led_shutdown,
};
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");