summaryrefslogtreecommitdiffstats
path: root/package/kernel/leds-gca230718/src/leds-gca230718.c
blob: d759453cd9ff67746220a72dca9e738841cf84bf (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// SPDX-License-Identifier: GPL-2.0-only
/*
 * GCA230718 LED support (e.g. for D-Link M30) using I2C
 *
 * Copyright 2022 Roland Reinl <reinlroland+github@gmail.com>
 *
 * This driver can control RGBW LEDs which are connected to a GCA230718.
 */

#include <linux/delay.h>
#include <linux/led-class-multicolor.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/of_device.h>
#include <linux/property.h>
#include <linux/i2c.h>
#include <linux/mutex.h>
#include <linux/version.h>

#define GCA230718_MAX_LEDS                                      (4u)

#define GCA230718_OPMODE_DISABLED                               (0x00u)
#define GCA230718_OPMODE_NO_TOGGLE                              (0x01u)
#define GCA230718_OPMODE_TOGGLE_RAMP_CONTROL_DISABLED           (0x02u)
#define GCA230718_OPMODE_TOGGLE_RAMP_CONTROL_ENSABLED           (0x03u)

#define GCA230718_1ST_SEQUENCE_BYTE_1                           (0x02u)
#define GCA230718_2ND_SEQUENCE_BYTE_1                           (0x01u)
#define GCA230718_3RD_SEQUENCE_BYTE_1                           (0x03u)

struct gca230718_led
{
	enum led_brightness brightness;
	struct i2c_client *client;
	struct led_classdev ledClassDev;
};

struct gca230718_private
{
	struct mutex lock;
	struct gca230718_led leds[GCA230718_MAX_LEDS];
};

static void gca230718_init_private_led_data(struct gca230718_private* data)
{
	u8 ledIndex;
	for (ledIndex = 0; ledIndex < GCA230718_MAX_LEDS; ledIndex++)
	{
		data->leds[ledIndex].client = NULL;
	}
}

static void gca230718_send_sequence(struct i2c_client *client, u8 byte0, struct gca230718_private* gca230718_privateData)
{
	int status = 0;
	u8 ledIndex;
	const u8 resetCommand[2] = { 0x81, 0xE4 };
	const u8 resetCommandRegister = 0x00;

	u8 controlCommand[13];
	const u8 controlCommandRegister = 0x03;

	controlCommand[0] = 0x0C; /* Unknown */
	controlCommand[1] = byte0;
	controlCommand[2] = GCA230718_OPMODE_NO_TOGGLE;
	/* Byte 3-6 are set below to the brighness value of the individual LEDs */
	controlCommand[7] = 0x01; /* Frequency, doesn't care as long as GCA230718_OPMODE_NO_TOGGLE is used above */
	/* Byte 8-11 are set below to the brighness value of the individual LEDs */
	controlCommand[12] = 0x87;

	for (ledIndex = 0; ledIndex < GCA230718_MAX_LEDS; ledIndex++)
	{
		controlCommand[3 + ledIndex] = gca230718_privateData->leds[ledIndex].brightness;
		controlCommand[8 + ledIndex] = gca230718_privateData->leds[ledIndex].brightness;
	}

	mutex_lock(&(gca230718_privateData->lock));

	if ((status = i2c_smbus_write_i2c_block_data(client, resetCommandRegister, sizeof(resetCommand), resetCommand)) != 0)
	{
		pr_info("Error %i during call of i2c_smbus_write_i2c_block_data for reset command\n", status);
	}
	else if ((status = i2c_smbus_write_i2c_block_data(client, controlCommandRegister, sizeof(controlCommand), controlCommand)) != 0)
	{
		pr_info("Error %i during call of i2c_smbus_write_i2c_block_data for control command\n", status);
	}

	mutex_unlock(&(gca230718_privateData->lock));
}

static int gca230718_set_brightness(struct led_classdev *led_cdev, enum led_brightness value)
{
	struct gca230718_led* led;
	struct i2c_client* client;

	led = container_of(led_cdev, struct gca230718_led, ledClassDev);
	client = led->client;

	if (client != NULL)
	{
		struct gca230718_private* gca230718_privateData;

		led->brightness = value;
		gca230718_privateData = i2c_get_clientdata(client);

		gca230718_send_sequence(client, GCA230718_2ND_SEQUENCE_BYTE_1, gca230718_privateData);
	}

	return 0;
}

static int gca230718_probe(struct i2c_client *client, const struct i2c_device_id *id)
{
	int status = 0;
	struct gca230718_private* gca230718_privateData;

	pr_info("Enter gca230718_probe for device address %u\n", client->addr);
	gca230718_privateData = devm_kzalloc(&(client->dev), sizeof(struct gca230718_private), GFP_KERNEL);

	if (gca230718_privateData == NULL)
	{
		pr_info("Error during allocating memory for private data\n");
		status = -ENOMEM;
	}
	else
	{
		struct device_node* ledNode;
		mutex_init(&gca230718_privateData->lock);
		gca230718_init_private_led_data(gca230718_privateData);
		i2c_set_clientdata(client, gca230718_privateData);

		for_each_child_of_node(client->dev.of_node, ledNode)
		{
			u32 regValue = 0;
			if (of_property_read_u32(ledNode, "reg", &regValue) != 0)
			{
				pr_info("Missing entry \"reg\" in node %s\n", ledNode->name); 
			}
			else if (regValue >= GCA230718_MAX_LEDS)
			{
				pr_info("Invalid entry \"reg\" in node %s (%u)\n", ledNode->name, regValue);
			}
			else
			{
				struct led_classdev* ledClassDev = &(gca230718_privateData->leds[regValue].ledClassDev);
				struct led_init_data init_data = {};

				gca230718_privateData->leds[regValue].client = client;
				init_data.fwnode = of_fwnode_handle(ledNode);

				pr_info("Creating LED for node %s: reg=%u\n", ledNode->name, regValue); 

				ledClassDev->name = of_get_property(ledNode, "label", NULL);
				if (ledClassDev->name == NULL)
				{
					ledClassDev->name = ledNode->name;
				}

				ledClassDev->brightness = LED_OFF;
				ledClassDev->max_brightness = LED_FULL;
				ledClassDev->brightness_set_blocking = gca230718_set_brightness;
	
				if (devm_led_classdev_register_ext(&(client->dev), ledClassDev, &init_data) != 0)
				{
					pr_info("Error during call of devm_led_classdev_register_ext");
				}
			}
		}
	}

	if (status == 0)
	{
		/* 
		Send full initialization sequence.
		Afterwards only GCA230718_2ND_SEQUENCE_BYTE_1 must be send to upddate the brightness values.
		*/
		gca230718_send_sequence(client, GCA230718_1ST_SEQUENCE_BYTE_1, gca230718_privateData);
		gca230718_send_sequence(client, GCA230718_2ND_SEQUENCE_BYTE_1, gca230718_privateData);
		gca230718_send_sequence(client, GCA230718_3RD_SEQUENCE_BYTE_1, gca230718_privateData);
	}

	return status;
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(5,18,0)
static void gca230718_remove(struct i2c_client *client)
#else
static int gca230718_remove(struct i2c_client *client)
#endif
{
	struct gca230718_private* gca230718_privateData;
	gca230718_privateData = i2c_get_clientdata(client);
	mutex_destroy(&gca230718_privateData->lock);
	gca230718_init_private_led_data(gca230718_privateData);

#if LINUX_VERSION_CODE < KERNEL_VERSION(5,18,0)
	return 0;
#endif
}

static const struct i2c_device_id gca230718_i2c_ids[] = {
	{ "gca230718", 0 },
	{},
};
MODULE_DEVICE_TABLE(i2c, gca230718_i2c_ids);

static const struct of_device_id gca230718_dt_ids[] = {
	{ .compatible = "unknown,gca230718" },
	{},
};
MODULE_DEVICE_TABLE(of, gca230718_dt_ids);

static struct i2c_driver gca230718_driver = {
	.probe		= gca230718_probe,
	.remove		= gca230718_remove,
	.id_table	= gca230718_i2c_ids,
	.driver = {
		.name		= KBUILD_MODNAME,
		.of_match_table	= gca230718_dt_ids,
	},
};

module_i2c_driver(gca230718_driver);

MODULE_AUTHOR("Roland Reinl <reinlroland+github@gmail.com>");
MODULE_DESCRIPTION("GCA230718 LED support (e.g. for D-Link M30) using I2C");
MODULE_LICENSE("GPL");