forked from mafredri/asustor-platform-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasustor.c
291 lines (258 loc) · 8.42 KB
/
asustor.c
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* asustor.c - Platform driver for ASUSTOR NAS hardware
*
* Copyright (C) 2021 Mathias Fredriksson <[email protected]>
*/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/dmi.h>
#include <linux/errno.h>
#include <linux/gpio/machine.h>
#include <linux/gpio_keys.h>
#include <linux/input.h>
#include <linux/kernel.h>
#include <linux/leds.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#define GPIO_IT87 "gpio_it87"
#define GPIO_ICH "gpio_ich"
#define GPIO_AS6100 "INT33FF:01"
#define AS6100_GPIO_IT87_BASE 161
#define AS600_GPIO_IT87_BASE 448
// ASUSTOR Leds.
// If ledtrig-blkdev ever lands, use that instead of disk-activity:
// https://lore.kernel.org/linux-leds/[email protected]/
static struct gpio_led asustor_leds[] = {
{ .name = "blue:power", .default_state = LEDS_GPIO_DEFSTATE_ON },
{ .name = "red:power", .default_state = LEDS_GPIO_DEFSTATE_OFF },
{ .name = "green:status", .default_state = LEDS_GPIO_DEFSTATE_ON },
{
.name = "red:status",
.default_state = LEDS_GPIO_DEFSTATE_OFF,
.panic_indicator = 1,
.default_trigger = "panic",
},
{ .name = "blue:usb", .default_state = LEDS_GPIO_DEFSTATE_OFF },
{ .name = "green:usb", .default_state = LEDS_GPIO_DEFSTATE_OFF },
{ .name = "blue:lan", .default_state = LEDS_GPIO_DEFSTATE_ON },
{
.name = "sata1:green:disk",
.default_state = LEDS_GPIO_DEFSTATE_ON,
.default_trigger = "disk-activity",
},
{ .name = "sata1:red:disk", .default_state = LEDS_GPIO_DEFSTATE_OFF },
{
.name = "sata2:green:disk",
.default_state = LEDS_GPIO_DEFSTATE_ON,
.default_trigger = "disk-activity",
},
{ .name = "sata2:red:disk", .default_state = LEDS_GPIO_DEFSTATE_OFF },
{
.name = "sata3:green:disk",
.default_state = LEDS_GPIO_DEFSTATE_ON,
.default_trigger = "disk-activity",
},
{ .name = "sata3:red:disk", .default_state = LEDS_GPIO_DEFSTATE_OFF },
{
.name = "sata4:green:disk",
.default_state = LEDS_GPIO_DEFSTATE_ON,
.default_trigger = "disk-activity",
},
{ .name = "sata4:red:disk", .default_state = LEDS_GPIO_DEFSTATE_OFF },
};
static const struct gpio_led_platform_data asustor_leds_pdata = {
.leds = asustor_leds,
.num_leds = ARRAY_SIZE(asustor_leds),
};
// clang-format off
static struct gpiod_lookup_table asustor_6100_gpio_leds_lookup = {
.dev_id = "leds-gpio",
.table = {
GPIO_LOOKUP_IDX(GPIO_IT87, 56, NULL, 0, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_IT87, 8, NULL, 1, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_IT87, 31, NULL, 2, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_AS6100, 21, NULL, 3, GPIO_ACTIVE_HIGH),
// 4
GPIO_LOOKUP_IDX(GPIO_IT87, 21, NULL, 5, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_IT87, 52, NULL, 6, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX(GPIO_AS6100, 24, NULL, 7, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_AS6100, 15, NULL, 8, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX(GPIO_AS6100, 22, NULL, 9, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_AS6100, 19, NULL, 10, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX(GPIO_AS6100, 25, NULL, 11, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_AS6100, 16, NULL, 12, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX(GPIO_AS6100, 18, NULL, 13, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_AS6100, 17, NULL, 14, GPIO_ACTIVE_HIGH),
{}
},
};
static struct gpiod_lookup_table asustor_600_gpio_leds_lookup = {
.dev_id = "leds-gpio",
.table = {
GPIO_LOOKUP_IDX(GPIO_IT87, 56, NULL, 0, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_IT87, 8, NULL, 1, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_IT87, 31, NULL, 2, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_ICH, 27, NULL, 3, GPIO_ACTIVE_HIGH),
GPIO_LOOKUP_IDX(GPIO_IT87, 21, NULL, 4, GPIO_ACTIVE_LOW),
// 5
GPIO_LOOKUP_IDX(GPIO_IT87, 52, NULL, 6, GPIO_ACTIVE_HIGH),
{}
},
};
// clang-format on
// ASUSTOR Buttons.
// Unfortunately, gpio-keys-polled does not use gpio lookup tables.
static struct gpio_keys_button asustor_gpio_keys_table[] = {
{
.desc = "USB Copy Button",
.code = KEY_COPY,
.type = EV_KEY,
.active_low = 1,
.gpio = -1, // Invalid, set in init.
},
{
.desc = "Power Button",
.code = KEY_POWER,
.type = EV_KEY,
.active_low = 1,
.gpio = -1, // Invalid, set in init.
},
};
static struct gpio_keys_platform_data asustor_keys_pdata = {
.buttons = asustor_gpio_keys_table,
.nbuttons = ARRAY_SIZE(asustor_gpio_keys_table),
.poll_interval = 50,
.name = "asustor-keys",
};
// clang-format off
static struct gpiod_lookup_table asustor_6100_gpio_keys_lookup = {
.dev_id = "gpio-keys-polled",
.table = {
GPIO_LOOKUP_IDX(GPIO_IT87, 20, NULL, 0, GPIO_ACTIVE_LOW),
// 1 (Power Button is already handled properly via ACPI).
{}
},
};
static struct gpiod_lookup_table asustor_600_gpio_keys_lookup = {
.dev_id = "gpio-keys-polled",
.table = {
GPIO_LOOKUP_IDX(GPIO_IT87, 20, NULL, 0, GPIO_ACTIVE_LOW),
GPIO_LOOKUP_IDX(GPIO_IT87, 27, NULL, 1, GPIO_ACTIVE_LOW),
{}
},
};
// clang-format on
// ASUSTOR Platform.
struct asustor_driver_data {
int gpio_base;
struct gpiod_lookup_table *leds;
struct gpiod_lookup_table *keys;
};
static struct asustor_driver_data asustor_6100_driver_data = {
.gpio_base = AS6100_GPIO_IT87_BASE,
.leds = &asustor_6100_gpio_leds_lookup,
.keys = &asustor_6100_gpio_keys_lookup,
};
static struct asustor_driver_data asustor_600_driver_data = {
.gpio_base = AS600_GPIO_IT87_BASE,
.leds = &asustor_600_gpio_leds_lookup,
.keys = &asustor_600_gpio_keys_lookup,
};
static const struct dmi_system_id asustor_systems[] = {
{
.matches = {
DMI_EXACT_MATCH(DMI_SYS_VENDOR, "Insyde"),
DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "AS61xx"),
},
.driver_data = &asustor_6100_driver_data,
},
{
.matches = {
DMI_EXACT_MATCH(DMI_SYS_VENDOR, "ASUSTOR Inc."),
DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "AS-6xxT"),
},
.driver_data = &asustor_600_driver_data,
},
{}
};
MODULE_DEVICE_TABLE(dmi, asustor_systems);
static struct asustor_driver_data *driver_data;
static struct platform_device *asustor_leds_pdev;
static struct platform_device *asustor_keys_pdev;
static struct platform_device *__init asustor_create_pdev(const char *name,
const void *pdata,
size_t sz)
{
struct platform_device *pdev;
pdev = platform_device_register_data(NULL, name, PLATFORM_DEVID_NONE,
pdata, sz);
if (IS_ERR(pdev))
pr_err("failed registering %s: %ld\n", name, PTR_ERR(pdev));
return pdev;
}
// TODO(mafredri): Allow force model for testing.
static int __init asustor_init(void)
{
const struct dmi_system_id *system;
const struct gpiod_lookup *keys_table;
int ret, i;
system = dmi_first_match(asustor_systems);
if (!system) {
pr_info("No supported ASUSTOR mainboard found");
return -ENODEV;
}
pr_info("Found %s/%s\n", system->matches[0].substr,
system->matches[1].substr);
driver_data = system->driver_data;
gpiod_add_lookup_table(driver_data->leds);
gpiod_add_lookup_table(driver_data->keys);
for (i = 0; i < ARRAY_SIZE(asustor_gpio_keys_table); i++) {
// TODO(mafredri): Use gpiod or something less hacky.
// This is here simply because gpio-keys-polled does
// not support gpio lookups.
keys_table = driver_data->keys->table;
for (; keys_table->key != NULL; keys_table++) {
if (i == keys_table->idx) {
asustor_gpio_keys_table[i].gpio =
driver_data->gpio_base +
keys_table->chip_hwnum;
}
}
}
// TODO(mafredri): Handle number of disk slots -> enabled LEDs.
asustor_leds_pdev = asustor_create_pdev(
"leds-gpio", &asustor_leds_pdata, sizeof(asustor_leds_pdata));
if (IS_ERR(asustor_leds_pdev)) {
ret = PTR_ERR(asustor_leds_pdev);
goto err;
}
asustor_keys_pdev =
asustor_create_pdev("gpio-keys-polled", &asustor_keys_pdata,
sizeof(asustor_keys_pdata));
if (IS_ERR(asustor_keys_pdev)) {
ret = PTR_ERR(asustor_keys_pdev);
platform_device_unregister(asustor_leds_pdev);
goto err;
}
return 0;
err:
gpiod_remove_lookup_table(driver_data->leds);
gpiod_remove_lookup_table(driver_data->keys);
return ret;
}
static void __exit asustor_cleanup(void)
{
platform_device_unregister(asustor_leds_pdev);
platform_device_unregister(asustor_keys_pdev);
gpiod_remove_lookup_table(driver_data->leds);
gpiod_remove_lookup_table(driver_data->keys);
}
module_init(asustor_init);
module_exit(asustor_cleanup);
MODULE_AUTHOR("Mathias Fredriksson <[email protected]>");
MODULE_DESCRIPTION("Platform driver for ASUSTOR NAS hardware");
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:asustor");
MODULE_SOFTDEP("pre: it87 gpio-it87 gpio-ich"
" platform:leds-gpio"
" platform:gpio-keys-polled");