c043a99c8b76188d357a0651769e23522f47a7b2
[openwrt/openwrt.git] / target / linux / mvebu / patches-5.15 / 902-drivers-mfd-Add-a-driver-for-IEI-WT61P803-PUZZLE-MCU.patch
1 From 692cfa85272dd12995b427c0a7a585ced5d54f32 Mon Sep 17 00:00:00 2001
2 From: Luka Kovacic <luka.kovacic () sartura ! hr>
3 Date: Tue, 24 Aug 2021 12:44:33 +0000
4 Subject: [PATCH 2/7] drivers: mfd: Add a driver for IEI WT61P803 PUZZLE MCU
5
6 Add a driver for the IEI WT61P803 PUZZLE microcontroller, used in some
7 IEI Puzzle series devices. The microcontroller controls system power,
8 temperature sensors, fans and LEDs.
9
10 This driver implements the core functionality for device communication
11 over the system serial (serdev bus). It handles MCU messages and the
12 internal MCU properties. Some properties can be managed over sysfs.
13
14 Signed-off-by: Luka Kovacic <luka.kovacic@sartura.hr>
15 Signed-off-by: Pavo Banicevic <pavo.banicevic@sartura.hr>
16 Cc: Luka Perkov <luka.perkov@sartura.hr>
17 Cc: Robert Marko <robert.marko@sartura.hr>
18 ---
19 drivers/mfd/Kconfig | 8 +
20 drivers/mfd/Makefile | 1 +
21 drivers/mfd/iei-wt61p803-puzzle.c | 908 ++++++++++++++++++++++++
22 include/linux/mfd/iei-wt61p803-puzzle.h | 66 ++
23 4 files changed, 983 insertions(+)
24 create mode 100644 drivers/mfd/iei-wt61p803-puzzle.c
25 create mode 100644 include/linux/mfd/iei-wt61p803-puzzle.h
26
27 --- a/drivers/mfd/Kconfig
28 +++ b/drivers/mfd/Kconfig
29 @@ -2188,6 +2188,15 @@ config SGI_MFD_IOC3
30 If you have an SGI Origin, Octane, or a PCI IOC3 card,
31 then say Y. Otherwise say N.
32
33 +config MFD_IEI_WT61P803_PUZZLE
34 + tristate "IEI WT61P803 PUZZLE MCU driver"
35 + depends on SERIAL_DEV_BUS
36 + select MFD_CORE
37 + help
38 + IEI WT61P803 PUZZLE is a system power management microcontroller
39 + used for fan control, temperature sensor reading, LED control
40 + and system identification.
41 +
42 config MFD_INTEL_M10_BMC
43 tristate "Intel MAX 10 Board Management Controller"
44 depends on SPI_MASTER
45 --- a/drivers/mfd/Makefile
46 +++ b/drivers/mfd/Makefile
47 @@ -237,6 +237,7 @@ obj-$(CONFIG_MFD_DLN2) += dln2.o
48 obj-$(CONFIG_MFD_RT4831) += rt4831.o
49 obj-$(CONFIG_MFD_RT5033) += rt5033.o
50 obj-$(CONFIG_MFD_SKY81452) += sky81452.o
51 +obj-$(CONFIG_MFD_IEI_WT61P803_PUZZLE) += iei-wt61p803-puzzle.o
52
53 intel-soc-pmic-objs := intel_soc_pmic_core.o intel_soc_pmic_crc.o
54 obj-$(CONFIG_INTEL_SOC_PMIC) += intel-soc-pmic.o
55 --- /dev/null
56 +++ b/drivers/mfd/iei-wt61p803-puzzle.c
57 @@ -0,0 +1,908 @@
58 +// SPDX-License-Identifier: GPL-2.0-only
59 +/* IEI WT61P803 PUZZLE MCU Driver
60 + * System management microcontroller for fan control, temperature sensor reading,
61 + * LED control and system identification on IEI Puzzle series ARM-based appliances.
62 + *
63 + * Copyright (C) 2020 Sartura Ltd.
64 + * Author: Luka Kovacic <luka.kovacic@sartura.hr>
65 + */
66 +
67 +#include <linux/atomic.h>
68 +#include <linux/delay.h>
69 +#include <linux/export.h>
70 +#include <linux/init.h>
71 +#include <linux/kernel.h>
72 +#include <linux/mfd/core.h>
73 +#include <linux/mfd/iei-wt61p803-puzzle.h>
74 +#include <linux/mod_devicetable.h>
75 +#include <linux/module.h>
76 +#include <linux/of_platform.h>
77 +#include <linux/property.h>
78 +#include <linux/sched.h>
79 +#include <linux/serdev.h>
80 +#include <linux/slab.h>
81 +#include <linux/sysfs.h>
82 +#include <asm/unaligned.h>
83 +
84 +/* start, payload and XOR checksum at end */
85 +#define IEI_WT61P803_PUZZLE_MAX_COMMAND_LENGTH (1 + 20 + 1)
86 +#define IEI_WT61P803_PUZZLE_RESP_BUF_SIZE 512
87 +
88 +#define IEI_WT61P803_PUZZLE_MAC_LENGTH 17
89 +#define IEI_WT61P803_PUZZLE_SN_LENGTH 36
90 +#define IEI_WT61P803_PUZZLE_VERSION_LENGTH 6
91 +#define IEI_WT61P803_PUZZLE_BUILD_INFO_LENGTH 16
92 +#define IEI_WT61P803_PUZZLE_PROTOCOL_VERSION_LENGTH 8
93 +#define IEI_WT61P803_PUZZLE_NB_MAC 8
94 +
95 +/* Use HZ as a timeout value throughout the driver */
96 +#define IEI_WT61P803_PUZZLE_GENERAL_TIMEOUT HZ
97 +
98 +enum iei_wt61p803_puzzle_attribute_type {
99 + IEI_WT61P803_PUZZLE_VERSION,
100 + IEI_WT61P803_PUZZLE_BUILD_INFO,
101 + IEI_WT61P803_PUZZLE_BOOTLOADER_MODE,
102 + IEI_WT61P803_PUZZLE_PROTOCOL_VERSION,
103 + IEI_WT61P803_PUZZLE_SERIAL_NUMBER,
104 + IEI_WT61P803_PUZZLE_MAC_ADDRESS,
105 + IEI_WT61P803_PUZZLE_AC_RECOVERY_STATUS,
106 + IEI_WT61P803_PUZZLE_POWER_LOSS_RECOVERY,
107 + IEI_WT61P803_PUZZLE_POWER_STATUS,
108 +};
109 +
110 +struct iei_wt61p803_puzzle_device_attribute {
111 + struct device_attribute dev_attr;
112 + enum iei_wt61p803_puzzle_attribute_type type;
113 + u8 index;
114 +};
115 +
116 +/**
117 + * struct iei_wt61p803_puzzle_mcu_status - MCU flags state
118 + * @ac_recovery_status_flag: AC Recovery Status Flag
119 + * @power_loss_recovery: System recovery after power loss
120 + * @power_status: System Power-on Method
121 + */
122 +struct iei_wt61p803_puzzle_mcu_status {
123 + u8 ac_recovery_status_flag;
124 + u8 power_loss_recovery;
125 + u8 power_status;
126 +};
127 +
128 +/**
129 + * struct iei_wt61p803_puzzle_reply - MCU reply
130 + * @size: Size of the MCU reply
131 + * @data: Full MCU reply buffer
132 + * @state: Current state of the packet
133 + * @received: Was the response fullfilled
134 + */
135 +struct iei_wt61p803_puzzle_reply {
136 + size_t size;
137 + unsigned char data[IEI_WT61P803_PUZZLE_RESP_BUF_SIZE];
138 + struct completion received;
139 +};
140 +
141 +/**
142 + * struct iei_wt61p803_puzzle_mcu_version - MCU version status
143 + * @version: Primary firmware version
144 + * @build_info: Build date and time
145 + * @bootloader_mode: Status of the MCU operation
146 + * @protocol_version: MCU communication protocol version
147 + * @serial_number: Device factory serial number
148 + * @mac_address: Device factory MAC addresses
149 + *
150 + * Last element of arrays is reserved for '\0'.
151 + */
152 +struct iei_wt61p803_puzzle_mcu_version {
153 + char version[IEI_WT61P803_PUZZLE_VERSION_LENGTH + 1];
154 + char build_info[IEI_WT61P803_PUZZLE_BUILD_INFO_LENGTH + 1];
155 + bool bootloader_mode;
156 + char protocol_version[IEI_WT61P803_PUZZLE_PROTOCOL_VERSION_LENGTH + 1];
157 + char serial_number[IEI_WT61P803_PUZZLE_SN_LENGTH + 1];
158 + char mac_address[IEI_WT61P803_PUZZLE_NB_MAC][IEI_WT61P803_PUZZLE_MAC_LENGTH + 1];
159 +};
160 +
161 +/**
162 + * struct iei_wt61p803_puzzle - IEI WT61P803 PUZZLE MCU Driver
163 + * @serdev: Pointer to underlying serdev device
164 + * @dev: Pointer to underlying dev device
165 + * @reply_lock: Reply mutex lock
166 + * @reply: Pointer to the iei_wt61p803_puzzle_reply struct
167 + * @version: MCU version related data
168 + * @status: MCU status related data
169 + * @response_buffer Command response buffer allocation
170 + * @lock General member mutex lock
171 + */
172 +struct iei_wt61p803_puzzle {
173 + struct serdev_device *serdev;
174 + struct device *dev;
175 + struct mutex reply_lock; /* lock to prevent multiple firmware calls */
176 + struct iei_wt61p803_puzzle_reply *reply;
177 + struct iei_wt61p803_puzzle_mcu_version version;
178 + struct iei_wt61p803_puzzle_mcu_status status;
179 + unsigned char response_buffer[IEI_WT61P803_PUZZLE_BUF_SIZE];
180 + struct mutex lock; /* lock to protect response buffer */
181 +};
182 +
183 +static unsigned char iei_wt61p803_puzzle_checksum(unsigned char *buf, size_t len)
184 +{
185 + unsigned char checksum = 0;
186 + size_t i;
187 +
188 + for (i = 0; i < len; i++)
189 + checksum ^= buf[i];
190 + return checksum;
191 +}
192 +
193 +static int iei_wt61p803_puzzle_process_resp(struct iei_wt61p803_puzzle *mcu,
194 + const unsigned char *raw_resp_data, size_t size)
195 +{
196 + unsigned char checksum;
197 +
198 + /* Check the incoming frame header */
199 + if (!(raw_resp_data[0] == IEI_WT61P803_PUZZLE_CMD_HEADER_START ||
200 + raw_resp_data[0] == IEI_WT61P803_PUZZLE_CMD_HEADER_START_OTHER ||
201 + (raw_resp_data[0] == IEI_WT61P803_PUZZLE_CMD_HEADER_EEPROM &&
202 + raw_resp_data[1] == IEI_WT61P803_PUZZLE_CMD_EEPROM_READ))) {
203 + if (mcu->reply->size + size >= sizeof(mcu->reply->data))
204 + return -EIO;
205 +
206 + /* Append the frame to existing data */
207 + memcpy(mcu->reply->data + mcu->reply->size, raw_resp_data, size);
208 + mcu->reply->size += size;
209 + } else {
210 + if (size >= sizeof(mcu->reply->data))
211 + return -EIO;
212 +
213 + /* Start processing a new frame */
214 + memcpy(mcu->reply->data, raw_resp_data, size);
215 + mcu->reply->size = size;
216 + }
217 +
218 + checksum = iei_wt61p803_puzzle_checksum(mcu->reply->data, mcu->reply->size - 1);
219 + if (checksum != mcu->reply->data[mcu->reply->size - 1]) {
220 + /* The checksum isn't matched yet, wait for new frames */
221 + return size;
222 + }
223 +
224 + /* Received all the data */
225 + complete(&mcu->reply->received);
226 +
227 + return size;
228 +}
229 +
230 +static int iei_wt61p803_puzzle_recv_buf(struct serdev_device *serdev,
231 + const unsigned char *data, size_t size)
232 +{
233 + struct iei_wt61p803_puzzle *mcu = serdev_device_get_drvdata(serdev);
234 + int ret;
235 +
236 + ret = iei_wt61p803_puzzle_process_resp(mcu, data, size);
237 + /* Return the number of processed bytes if function returns error,
238 + * discard the remaining incoming data, since the frame this data
239 + * belongs to is broken anyway
240 + */
241 + if (ret < 0)
242 + return size;
243 +
244 + return ret;
245 +}
246 +
247 +static const struct serdev_device_ops iei_wt61p803_puzzle_serdev_device_ops = {
248 + .receive_buf = iei_wt61p803_puzzle_recv_buf,
249 + .write_wakeup = serdev_device_write_wakeup,
250 +};
251 +
252 +/**
253 + * iei_wt61p803_puzzle_write_command_watchdog() - Watchdog of the normal cmd
254 + * @mcu: Pointer to the iei_wt61p803_puzzle core MFD struct
255 + * @cmd: Pointer to the char array to send (size should be content + 1 (xor))
256 + * @size: Size of the cmd char array
257 + * @reply_data: Pointer to the reply/response data array (should be allocated)
258 + * @reply_size: Pointer to size_t (size of reply_data)
259 + * @retry_count: Number of times to retry sending the command to the MCU
260 + */
261 +int iei_wt61p803_puzzle_write_command_watchdog(struct iei_wt61p803_puzzle *mcu,
262 + unsigned char *cmd, size_t size,
263 + unsigned char *reply_data,
264 + size_t *reply_size, int retry_count)
265 +{
266 + struct device *dev = &mcu->serdev->dev;
267 + int ret, i;
268 +
269 + for (i = 0; i < retry_count; i++) {
270 + ret = iei_wt61p803_puzzle_write_command(mcu, cmd, size,
271 + reply_data, reply_size);
272 + if (ret != -ETIMEDOUT)
273 + return ret;
274 + }
275 +
276 + dev_err(dev, "Command response timed out. Retries: %d\n", retry_count);
277 +
278 + return -ETIMEDOUT;
279 +}
280 +EXPORT_SYMBOL_GPL(iei_wt61p803_puzzle_write_command_watchdog);
281 +
282 +/**
283 + * iei_wt61p803_puzzle_write_command() - Send a structured command to the MCU
284 + * @mcu: Pointer to the iei_wt61p803_puzzle core MFD struct
285 + * @cmd: Pointer to the char array to send (size should be content + 1 (xor))
286 + * @size: Size of the cmd char array
287 + * @reply_data: Pointer to the reply/response data array (should be allocated)
288 + *
289 + * Sends a structured command to the MCU.
290 + */
291 +int iei_wt61p803_puzzle_write_command(struct iei_wt61p803_puzzle *mcu,
292 + unsigned char *cmd, size_t size,
293 + unsigned char *reply_data,
294 + size_t *reply_size)
295 +{
296 + struct device *dev = &mcu->serdev->dev;
297 + int ret;
298 +
299 + if (size <= 1 || size > IEI_WT61P803_PUZZLE_MAX_COMMAND_LENGTH)
300 + return -EINVAL;
301 +
302 + mutex_lock(&mcu->reply_lock);
303 +
304 + cmd[size - 1] = iei_wt61p803_puzzle_checksum(cmd, size - 1);
305 +
306 + /* Initialize reply struct */
307 + reinit_completion(&mcu->reply->received);
308 + mcu->reply->size = 0;
309 + usleep_range(2000, 10000);
310 + serdev_device_write_flush(mcu->serdev);
311 + ret = serdev_device_write_buf(mcu->serdev, cmd, size);
312 + if (ret < 0)
313 + goto exit;
314 +
315 + serdev_device_wait_until_sent(mcu->serdev, IEI_WT61P803_PUZZLE_GENERAL_TIMEOUT);
316 + ret = wait_for_completion_timeout(&mcu->reply->received,
317 + IEI_WT61P803_PUZZLE_GENERAL_TIMEOUT);
318 + if (ret == 0) {
319 + dev_err(dev, "Command reply receive timeout\n");
320 + ret = -ETIMEDOUT;
321 + goto exit;
322 + }
323 +
324 + *reply_size = mcu->reply->size;
325 + /* Copy the received data, as it will not be available after a new frame is received */
326 + memcpy(reply_data, mcu->reply->data, mcu->reply->size);
327 + ret = 0;
328 +exit:
329 + mutex_unlock(&mcu->reply_lock);
330 + return ret;
331 +}
332 +EXPORT_SYMBOL_GPL(iei_wt61p803_puzzle_write_command);
333 +
334 +static int iei_wt61p803_puzzle_buzzer(struct iei_wt61p803_puzzle *mcu, bool long_beep)
335 +{
336 + unsigned char *resp_buf = mcu->response_buffer;
337 + unsigned char buzzer_cmd[4] = {};
338 + size_t reply_size;
339 + int ret;
340 +
341 + buzzer_cmd[0] = IEI_WT61P803_PUZZLE_CMD_HEADER_START;
342 + buzzer_cmd[1] = IEI_WT61P803_PUZZLE_CMD_FUNCTION_SINGLE;
343 + buzzer_cmd[2] = long_beep ? '3' : '2'; /* Buzzer 1.5 / 0.5 second beep */
344 +
345 + mutex_lock(&mcu->lock);
346 + ret = iei_wt61p803_puzzle_write_command(mcu, buzzer_cmd, sizeof(buzzer_cmd),
347 + resp_buf, &reply_size);
348 + if (ret)
349 + goto exit;
350 +
351 + if (reply_size != 3) {
352 + ret = -EIO;
353 + goto exit;
354 + }
355 +
356 + if (!(resp_buf[0] == IEI_WT61P803_PUZZLE_CMD_HEADER_START &&
357 + resp_buf[1] == IEI_WT61P803_PUZZLE_CMD_RESPONSE_OK &&
358 + resp_buf[2] == IEI_WT61P803_PUZZLE_CHECKSUM_RESPONSE_OK)) {
359 + ret = -EPROTO;
360 + goto exit;
361 + }
362 +exit:
363 + mutex_unlock(&mcu->lock);
364 + return ret;
365 +}
366 +
367 +static int iei_wt61p803_puzzle_get_version(struct iei_wt61p803_puzzle *mcu)
368 +{
369 + unsigned char version_cmd[3] = {
370 + IEI_WT61P803_PUZZLE_CMD_HEADER_START_OTHER,
371 + IEI_WT61P803_PUZZLE_CMD_OTHER_VERSION,
372 + };
373 + unsigned char build_info_cmd[3] = {
374 + IEI_WT61P803_PUZZLE_CMD_HEADER_START_OTHER,
375 + IEI_WT61P803_PUZZLE_CMD_OTHER_BUILD,
376 + };
377 + unsigned char bootloader_mode_cmd[3] = {
378 + IEI_WT61P803_PUZZLE_CMD_HEADER_START_OTHER,
379 + IEI_WT61P803_PUZZLE_CMD_OTHER_BOOTLOADER_MODE,
380 + };
381 + unsigned char protocol_version_cmd[3] = {
382 + IEI_WT61P803_PUZZLE_CMD_HEADER_START_OTHER,
383 + IEI_WT61P803_PUZZLE_CMD_OTHER_PROTOCOL_VERSION,
384 + };
385 + unsigned char *rb = mcu->response_buffer;
386 + size_t reply_size;
387 + int ret;
388 +
389 + mutex_lock(&mcu->lock);
390 +
391 + ret = iei_wt61p803_puzzle_write_command(mcu, version_cmd, sizeof(version_cmd),
392 + rb, &reply_size);
393 + if (ret)
394 + goto err;
395 + if (reply_size < 7) {
396 + ret = -EIO;
397 + goto err;
398 + }
399 + sprintf(mcu->version.version, "v%c.%.3s", rb[2], &rb[3]);
400 +
401 + ret = iei_wt61p803_puzzle_write_command(mcu, build_info_cmd,
402 + sizeof(build_info_cmd), rb,
403 + &reply_size);
404 + if (ret)
405 + goto err;
406 + if (reply_size < 15) {
407 + ret = -EIO;
408 + goto err;
409 + }
410 + sprintf(mcu->version.build_info, "%c%c/%c%c/%.4s %c%c:%c%c",
411 + rb[8], rb[9], rb[6], rb[7], &rb[2], rb[10], rb[11],
412 + rb[12], rb[13]);
413 +
414 + ret = iei_wt61p803_puzzle_write_command(mcu, bootloader_mode_cmd,
415 + sizeof(bootloader_mode_cmd), rb,
416 + &reply_size);
417 + if (ret)
418 + goto err;
419 + if (reply_size < 4) {
420 + ret = -EIO;
421 + goto err;
422 + }
423 + if (rb[2] == IEI_WT61P803_PUZZLE_CMD_OTHER_MODE_APPS)
424 + mcu->version.bootloader_mode = false;
425 + else if (rb[2] == IEI_WT61P803_PUZZLE_CMD_OTHER_MODE_BOOTLOADER)
426 + mcu->version.bootloader_mode = true;
427 +
428 + ret = iei_wt61p803_puzzle_write_command(mcu, protocol_version_cmd,
429 + sizeof(protocol_version_cmd), rb,
430 + &reply_size);
431 + if (ret)
432 + goto err;
433 + if (reply_size < 9) {
434 + ret = -EIO;
435 + goto err;
436 + }
437 + sprintf(mcu->version.protocol_version, "v%c.%c%c%c%c%c",
438 + rb[7], rb[6], rb[5], rb[4], rb[3], rb[2]);
439 +err:
440 + mutex_unlock(&mcu->lock);
441 + return ret;
442 +}
443 +
444 +static int iei_wt61p803_puzzle_get_mcu_status(struct iei_wt61p803_puzzle *mcu)
445 +{
446 + unsigned char mcu_status_cmd[5] = {
447 + IEI_WT61P803_PUZZLE_CMD_HEADER_START,
448 + IEI_WT61P803_PUZZLE_CMD_FUNCTION_OTHER,
449 + IEI_WT61P803_PUZZLE_CMD_FUNCTION_OTHER_STATUS,
450 + IEI_WT61P803_PUZZLE_CMD_FUNCTION_OTHER_STATUS,
451 + };
452 + unsigned char *resp_buf = mcu->response_buffer;
453 + size_t reply_size;
454 + int ret;
455 +
456 + mutex_lock(&mcu->lock);
457 + ret = iei_wt61p803_puzzle_write_command(mcu, mcu_status_cmd, sizeof(mcu_status_cmd),
458 + resp_buf, &reply_size);
459 + if (ret)
460 + goto exit;
461 + if (reply_size < 20) {
462 + ret = -EIO;
463 + goto exit;
464 + }
465 +
466 + /* Response format:
467 + * (IDX RESPONSE)
468 + * 0 @
469 + * 1 O
470 + * 2 S
471 + * 3 S
472 + * ...
473 + * 5 AC Recovery Status Flag
474 + * ...
475 + * 10 Power Loss Recovery
476 + * ...
477 + * 19 Power Status (system power on method)
478 + * 20 XOR checksum
479 + */
480 + if (resp_buf[0] == IEI_WT61P803_PUZZLE_CMD_HEADER_START &&
481 + resp_buf[1] == IEI_WT61P803_PUZZLE_CMD_FUNCTION_OTHER &&
482 + resp_buf[2] == IEI_WT61P803_PUZZLE_CMD_FUNCTION_OTHER_STATUS &&
483 + resp_buf[3] == IEI_WT61P803_PUZZLE_CMD_FUNCTION_OTHER_STATUS) {
484 + mcu->status.ac_recovery_status_flag = resp_buf[5];
485 + mcu->status.power_loss_recovery = resp_buf[10];
486 + mcu->status.power_status = resp_buf[19];
487 + }
488 +exit:
489 + mutex_unlock(&mcu->lock);
490 + return ret;
491 +}
492 +
493 +static int iei_wt61p803_puzzle_get_serial_number(struct iei_wt61p803_puzzle *mcu)
494 +{
495 + unsigned char *resp_buf = mcu->response_buffer;
496 + unsigned char serial_number_cmd[5] = {
497 + IEI_WT61P803_PUZZLE_CMD_HEADER_EEPROM,
498 + IEI_WT61P803_PUZZLE_CMD_EEPROM_READ,
499 + 0x00, /* EEPROM read address */
500 + 0x24, /* Data length */
501 + };
502 + size_t reply_size;
503 + int ret;
504 +
505 + mutex_lock(&mcu->lock);
506 + ret = iei_wt61p803_puzzle_write_command(mcu, serial_number_cmd,
507 + sizeof(serial_number_cmd),
508 + resp_buf, &reply_size);
509 + if (ret)
510 + goto err;
511 +
512 + if (reply_size < IEI_WT61P803_PUZZLE_SN_LENGTH + 4) {
513 + ret = -EIO;
514 + goto err;
515 + }
516 +
517 + sprintf(mcu->version.serial_number, "%.*s",
518 + IEI_WT61P803_PUZZLE_SN_LENGTH, resp_buf + 4);
519 +err:
520 + mutex_unlock(&mcu->lock);
521 + return ret;
522 +}
523 +
524 +static int iei_wt61p803_puzzle_write_serial_number(struct iei_wt61p803_puzzle *mcu,
525 + unsigned char serial_number[36])
526 +{
527 + unsigned char *resp_buf = mcu->response_buffer;
528 + unsigned char serial_number_header[4] = {
529 + IEI_WT61P803_PUZZLE_CMD_HEADER_EEPROM,
530 + IEI_WT61P803_PUZZLE_CMD_EEPROM_WRITE,
531 + 0x00, /* EEPROM write address */
532 + 0xC, /* Data length */
533 + };
534 + unsigned char serial_number_cmd[4 + 12 + 1]; /* header, serial number, XOR checksum */
535 + int ret, sn_counter;
536 + size_t reply_size;
537 +
538 + /* The MCU can only handle 22 byte messages, send the S/N in 12 byte chunks */
539 + mutex_lock(&mcu->lock);
540 + for (sn_counter = 0; sn_counter < 3; sn_counter++) {
541 + serial_number_header[2] = 0x0 + 0xC * sn_counter;
542 +
543 + memcpy(serial_number_cmd, serial_number_header, sizeof(serial_number_header));
544 + memcpy(serial_number_cmd + sizeof(serial_number_header),
545 + serial_number + 0xC * sn_counter, 0xC);
546 +
547 + ret = iei_wt61p803_puzzle_write_command(mcu, serial_number_cmd,
548 + sizeof(serial_number_cmd),
549 + resp_buf, &reply_size);
550 + if (ret)
551 + goto err;
552 + if (reply_size != 3) {
553 + ret = -EIO;
554 + goto err;
555 + }
556 + if (!(resp_buf[0] == IEI_WT61P803_PUZZLE_CMD_HEADER_START &&
557 + resp_buf[1] == IEI_WT61P803_PUZZLE_CMD_RESPONSE_OK &&
558 + resp_buf[2] == IEI_WT61P803_PUZZLE_CHECKSUM_RESPONSE_OK)) {
559 + ret = -EPROTO;
560 + goto err;
561 + }
562 + }
563 +
564 + sprintf(mcu->version.serial_number, "%.*s",
565 + IEI_WT61P803_PUZZLE_SN_LENGTH, serial_number);
566 +err:
567 + mutex_unlock(&mcu->lock);
568 + return ret;
569 +}
570 +
571 +static int iei_wt61p803_puzzle_get_mac_address(struct iei_wt61p803_puzzle *mcu, int index)
572 +{
573 + unsigned char *resp_buf = mcu->response_buffer;
574 + unsigned char mac_address_cmd[5] = {
575 + IEI_WT61P803_PUZZLE_CMD_HEADER_EEPROM,
576 + IEI_WT61P803_PUZZLE_CMD_EEPROM_READ,
577 + 0x00, /* EEPROM read address */
578 + 0x11, /* Data length */
579 + };
580 + size_t reply_size;
581 + int ret;
582 +
583 + mutex_lock(&mcu->lock);
584 + mac_address_cmd[2] = 0x24 + 0x11 * index;
585 +
586 + ret = iei_wt61p803_puzzle_write_command(mcu, mac_address_cmd,
587 + sizeof(mac_address_cmd),
588 + resp_buf, &reply_size);
589 + if (ret)
590 + goto err;
591 +
592 + if (reply_size < 22) {
593 + ret = -EIO;
594 + goto err;
595 + }
596 +
597 + sprintf(mcu->version.mac_address[index], "%.*s",
598 + IEI_WT61P803_PUZZLE_MAC_LENGTH, resp_buf + 4);
599 +err:
600 + mutex_unlock(&mcu->lock);
601 + return ret;
602 +}
603 +
604 +static int
605 +iei_wt61p803_puzzle_write_mac_address(struct iei_wt61p803_puzzle *mcu,
606 + unsigned char mac_address[IEI_WT61P803_PUZZLE_MAC_LENGTH],
607 + int mac_address_idx)
608 +{
609 + unsigned char mac_address_cmd[4 + IEI_WT61P803_PUZZLE_MAC_LENGTH + 1];
610 + unsigned char *resp_buf = mcu->response_buffer;
611 + unsigned char mac_address_header[4] = {
612 + IEI_WT61P803_PUZZLE_CMD_HEADER_EEPROM,
613 + IEI_WT61P803_PUZZLE_CMD_EEPROM_WRITE,
614 + 0x00, /* EEPROM write address */
615 + 0x11, /* Data length */
616 + };
617 + size_t reply_size;
618 + int ret;
619 +
620 + if (mac_address_idx < 0 || mac_address_idx >= IEI_WT61P803_PUZZLE_NB_MAC)
621 + return -EINVAL;
622 +
623 + mac_address_header[2] = 0x24 + 0x11 * mac_address_idx;
624 +
625 + /* Concat mac_address_header, mac_address to mac_address_cmd */
626 + memcpy(mac_address_cmd, mac_address_header, sizeof(mac_address_header));
627 + memcpy(mac_address_cmd + sizeof(mac_address_header), mac_address,
628 + IEI_WT61P803_PUZZLE_MAC_LENGTH);
629 +
630 + mutex_lock(&mcu->lock);
631 + ret = iei_wt61p803_puzzle_write_command(mcu, mac_address_cmd,
632 + sizeof(mac_address_cmd),
633 + resp_buf, &reply_size);
634 + if (ret)
635 + goto err;
636 + if (reply_size != 3) {
637 + ret = -EIO;
638 + goto err;
639 + }
640 + if (!(resp_buf[0] == IEI_WT61P803_PUZZLE_CMD_HEADER_START &&
641 + resp_buf[1] == IEI_WT61P803_PUZZLE_CMD_RESPONSE_OK &&
642 + resp_buf[2] == IEI_WT61P803_PUZZLE_CHECKSUM_RESPONSE_OK)) {
643 + ret = -EPROTO;
644 + goto err;
645 + }
646 +
647 + sprintf(mcu->version.mac_address[mac_address_idx], "%.*s",
648 + IEI_WT61P803_PUZZLE_MAC_LENGTH, mac_address);
649 +err:
650 + mutex_unlock(&mcu->lock);
651 + return ret;
652 +}
653 +
654 +static int iei_wt61p803_puzzle_write_power_loss_recovery(struct iei_wt61p803_puzzle *mcu,
655 + int power_loss_recovery_action)
656 +{
657 + unsigned char *resp_buf = mcu->response_buffer;
658 + unsigned char power_loss_recovery_cmd[5] = {};
659 + size_t reply_size;
660 + int ret;
661 +
662 + if (power_loss_recovery_action < 0 || power_loss_recovery_action > 4)
663 + return -EINVAL;
664 +
665 + power_loss_recovery_cmd[0] = IEI_WT61P803_PUZZLE_CMD_HEADER_START;
666 + power_loss_recovery_cmd[1] = IEI_WT61P803_PUZZLE_CMD_FUNCTION_OTHER;
667 + power_loss_recovery_cmd[2] = IEI_WT61P803_PUZZLE_CMD_FUNCTION_OTHER_POWER_LOSS;
668 + power_loss_recovery_cmd[3] = hex_asc[power_loss_recovery_action];
669 +
670 + mutex_lock(&mcu->lock);
671 + ret = iei_wt61p803_puzzle_write_command(mcu, power_loss_recovery_cmd,
672 + sizeof(power_loss_recovery_cmd),
673 + resp_buf, &reply_size);
674 + if (ret)
675 + goto exit;
676 + mcu->status.power_loss_recovery = power_loss_recovery_action;
677 +exit:
678 + mutex_unlock(&mcu->lock);
679 + return ret;
680 +}
681 +
682 +#define to_puzzle_dev_attr(_attr) \
683 + container_of(_attr, struct iei_wt61p803_puzzle_device_attribute, dev_attr)
684 +
685 +static ssize_t show_output(struct device *dev,
686 + struct device_attribute *attr, char *buf)
687 +{
688 + struct iei_wt61p803_puzzle *mcu = dev_get_drvdata(dev);
689 + struct iei_wt61p803_puzzle_device_attribute *pattr = to_puzzle_dev_attr(attr);
690 + int ret;
691 +
692 + switch (pattr->type) {
693 + case IEI_WT61P803_PUZZLE_VERSION:
694 + return scnprintf(buf, PAGE_SIZE, "%s\n", mcu->version.version);
695 + case IEI_WT61P803_PUZZLE_BUILD_INFO:
696 + return scnprintf(buf, PAGE_SIZE, "%s\n", mcu->version.build_info);
697 + case IEI_WT61P803_PUZZLE_BOOTLOADER_MODE:
698 + return scnprintf(buf, PAGE_SIZE, "%d\n", mcu->version.bootloader_mode);
699 + case IEI_WT61P803_PUZZLE_PROTOCOL_VERSION:
700 + return scnprintf(buf, PAGE_SIZE, "%s\n", mcu->version.protocol_version);
701 + case IEI_WT61P803_PUZZLE_SERIAL_NUMBER:
702 + ret = iei_wt61p803_puzzle_get_serial_number(mcu);
703 + if (!ret)
704 + ret = scnprintf(buf, PAGE_SIZE, "%s\n", mcu->version.serial_number);
705 + else
706 + ret = 0;
707 + return ret;
708 + case IEI_WT61P803_PUZZLE_MAC_ADDRESS:
709 + ret = iei_wt61p803_puzzle_get_mac_address(mcu, pattr->index);
710 + if (!ret)
711 + ret = scnprintf(buf, PAGE_SIZE, "%s\n",
712 + mcu->version.mac_address[pattr->index]);
713 + else
714 + ret = 0;
715 + return ret;
716 + case IEI_WT61P803_PUZZLE_AC_RECOVERY_STATUS:
717 + case IEI_WT61P803_PUZZLE_POWER_LOSS_RECOVERY:
718 + case IEI_WT61P803_PUZZLE_POWER_STATUS:
719 + ret = iei_wt61p803_puzzle_get_mcu_status(mcu);
720 + if (ret)
721 + return ret;
722 +
723 + mutex_lock(&mcu->lock);
724 + switch (pattr->type) {
725 + case IEI_WT61P803_PUZZLE_AC_RECOVERY_STATUS:
726 + ret = scnprintf(buf, PAGE_SIZE, "%x\n",
727 + mcu->status.ac_recovery_status_flag);
728 + break;
729 + case IEI_WT61P803_PUZZLE_POWER_LOSS_RECOVERY:
730 + ret = scnprintf(buf, PAGE_SIZE, "%x\n", mcu->status.power_loss_recovery);
731 + break;
732 + case IEI_WT61P803_PUZZLE_POWER_STATUS:
733 + ret = scnprintf(buf, PAGE_SIZE, "%x\n", mcu->status.power_status);
734 + break;
735 + default:
736 + ret = 0;
737 + break;
738 + }
739 + mutex_unlock(&mcu->lock);
740 + return ret;
741 + default:
742 + return 0;
743 + }
744 +
745 + return 0;
746 +}
747 +
748 +static ssize_t store_output(struct device *dev,
749 + struct device_attribute *attr,
750 + const char *buf, size_t len)
751 +{
752 + unsigned char serial_number[IEI_WT61P803_PUZZLE_SN_LENGTH];
753 + unsigned char mac_address[IEI_WT61P803_PUZZLE_MAC_LENGTH];
754 + struct iei_wt61p803_puzzle *mcu = dev_get_drvdata(dev);
755 + struct iei_wt61p803_puzzle_device_attribute *pattr = to_puzzle_dev_attr(attr);
756 + int power_loss_recovery_action = 0;
757 + int ret;
758 +
759 + switch (pattr->type) {
760 + case IEI_WT61P803_PUZZLE_SERIAL_NUMBER:
761 + if (len != (size_t)(IEI_WT61P803_PUZZLE_SN_LENGTH + 1))
762 + return -EINVAL;
763 + memcpy(serial_number, buf, sizeof(serial_number));
764 + ret = iei_wt61p803_puzzle_write_serial_number(mcu, serial_number);
765 + if (ret)
766 + return ret;
767 + return len;
768 + case IEI_WT61P803_PUZZLE_MAC_ADDRESS:
769 + if (len != (size_t)(IEI_WT61P803_PUZZLE_MAC_LENGTH + 1))
770 + return -EINVAL;
771 +
772 + memcpy(mac_address, buf, sizeof(mac_address));
773 +
774 + if (strlen(attr->attr.name) != 13)
775 + return -EIO;
776 +
777 + ret = iei_wt61p803_puzzle_write_mac_address(mcu, mac_address, pattr->index);
778 + if (ret)
779 + return ret;
780 + return len;
781 + case IEI_WT61P803_PUZZLE_POWER_LOSS_RECOVERY:
782 + ret = kstrtoint(buf, 10, &power_loss_recovery_action);
783 + if (ret)
784 + return ret;
785 + ret = iei_wt61p803_puzzle_write_power_loss_recovery(mcu,
786 + power_loss_recovery_action);
787 + if (ret)
788 + return ret;
789 + return len;
790 + default:
791 + return -EINVAL;
792 + }
793 +
794 + return 0;
795 +}
796 +
797 +#define IEI_WT61P803_PUZZLE_ATTR(_name, _mode, _show, _store, _type, _index) \
798 + struct iei_wt61p803_puzzle_device_attribute dev_attr_##_name = \
799 + { .dev_attr = __ATTR(_name, _mode, _show, _store), \
800 + .type = _type, \
801 + .index = _index }
802 +
803 +#define IEI_WT61P803_PUZZLE_ATTR_RO(_name, _type, _id) \
804 + IEI_WT61P803_PUZZLE_ATTR(_name, 0444, show_output, NULL, _type, _id)
805 +
806 +#define IEI_WT61P803_PUZZLE_ATTR_RW(_name, _type, _id) \
807 + IEI_WT61P803_PUZZLE_ATTR(_name, 0644, show_output, store_output, _type, _id)
808 +
809 +static IEI_WT61P803_PUZZLE_ATTR_RO(version, IEI_WT61P803_PUZZLE_VERSION, 0);
810 +static IEI_WT61P803_PUZZLE_ATTR_RO(build_info, IEI_WT61P803_PUZZLE_BUILD_INFO, 0);
811 +static IEI_WT61P803_PUZZLE_ATTR_RO(bootloader_mode, IEI_WT61P803_PUZZLE_BOOTLOADER_MODE, 0);
812 +static IEI_WT61P803_PUZZLE_ATTR_RO(protocol_version, IEI_WT61P803_PUZZLE_PROTOCOL_VERSION, 0);
813 +static IEI_WT61P803_PUZZLE_ATTR_RW(serial_number, IEI_WT61P803_PUZZLE_SERIAL_NUMBER, 0);
814 +static IEI_WT61P803_PUZZLE_ATTR_RW(mac_address_0, IEI_WT61P803_PUZZLE_MAC_ADDRESS, 0);
815 +static IEI_WT61P803_PUZZLE_ATTR_RW(mac_address_1, IEI_WT61P803_PUZZLE_MAC_ADDRESS, 1);
816 +static IEI_WT61P803_PUZZLE_ATTR_RW(mac_address_2, IEI_WT61P803_PUZZLE_MAC_ADDRESS, 2);
817 +static IEI_WT61P803_PUZZLE_ATTR_RW(mac_address_3, IEI_WT61P803_PUZZLE_MAC_ADDRESS, 3);
818 +static IEI_WT61P803_PUZZLE_ATTR_RW(mac_address_4, IEI_WT61P803_PUZZLE_MAC_ADDRESS, 4);
819 +static IEI_WT61P803_PUZZLE_ATTR_RW(mac_address_5, IEI_WT61P803_PUZZLE_MAC_ADDRESS, 5);
820 +static IEI_WT61P803_PUZZLE_ATTR_RW(mac_address_6, IEI_WT61P803_PUZZLE_MAC_ADDRESS, 6);
821 +static IEI_WT61P803_PUZZLE_ATTR_RW(mac_address_7, IEI_WT61P803_PUZZLE_MAC_ADDRESS, 7);
822 +static IEI_WT61P803_PUZZLE_ATTR_RO(ac_recovery_status, IEI_WT61P803_PUZZLE_AC_RECOVERY_STATUS, 0);
823 +static IEI_WT61P803_PUZZLE_ATTR_RW(power_loss_recovery, IEI_WT61P803_PUZZLE_POWER_LOSS_RECOVERY, 0);
824 +static IEI_WT61P803_PUZZLE_ATTR_RO(power_status, IEI_WT61P803_PUZZLE_POWER_STATUS, 0);
825 +
826 +static struct attribute *iei_wt61p803_puzzle_attrs[] = {
827 + &dev_attr_version.dev_attr.attr,
828 + &dev_attr_build_info.dev_attr.attr,
829 + &dev_attr_bootloader_mode.dev_attr.attr,
830 + &dev_attr_protocol_version.dev_attr.attr,
831 + &dev_attr_serial_number.dev_attr.attr,
832 + &dev_attr_mac_address_0.dev_attr.attr,
833 + &dev_attr_mac_address_1.dev_attr.attr,
834 + &dev_attr_mac_address_2.dev_attr.attr,
835 + &dev_attr_mac_address_3.dev_attr.attr,
836 + &dev_attr_mac_address_4.dev_attr.attr,
837 + &dev_attr_mac_address_5.dev_attr.attr,
838 + &dev_attr_mac_address_6.dev_attr.attr,
839 + &dev_attr_mac_address_7.dev_attr.attr,
840 + &dev_attr_ac_recovery_status.dev_attr.attr,
841 + &dev_attr_power_loss_recovery.dev_attr.attr,
842 + &dev_attr_power_status.dev_attr.attr,
843 + NULL
844 +};
845 +ATTRIBUTE_GROUPS(iei_wt61p803_puzzle);
846 +
847 +static int iei_wt61p803_puzzle_sysfs_create(struct device *dev,
848 + struct iei_wt61p803_puzzle *mcu)
849 +{
850 + int ret;
851 +
852 + ret = sysfs_create_groups(&mcu->dev->kobj, iei_wt61p803_puzzle_groups);
853 + if (ret)
854 + mfd_remove_devices(mcu->dev);
855 +
856 + return ret;
857 +}
858 +
859 +static int iei_wt61p803_puzzle_sysfs_remove(struct device *dev,
860 + struct iei_wt61p803_puzzle *mcu)
861 +{
862 + /* Remove sysfs groups */
863 + sysfs_remove_groups(&mcu->dev->kobj, iei_wt61p803_puzzle_groups);
864 + mfd_remove_devices(mcu->dev);
865 +
866 + return 0;
867 +}
868 +
869 +static int iei_wt61p803_puzzle_probe(struct serdev_device *serdev)
870 +{
871 + struct device *dev = &serdev->dev;
872 + struct iei_wt61p803_puzzle *mcu;
873 + u32 baud;
874 + int ret;
875 +
876 + /* Read the baud rate from 'current-speed', because the MCU supports different rates */
877 + if (device_property_read_u32(dev, "current-speed", &baud)) {
878 + dev_err(dev,
879 + "'current-speed' is not specified in device node\n");
880 + return -EINVAL;
881 + }
882 + dev_dbg(dev, "Driver baud rate: %d\n", baud);
883 +
884 + /* Allocate the memory */
885 + mcu = devm_kzalloc(dev, sizeof(*mcu), GFP_KERNEL);
886 + if (!mcu)
887 + return -ENOMEM;
888 +
889 + mcu->reply = devm_kzalloc(dev, sizeof(*mcu->reply), GFP_KERNEL);
890 + if (!mcu->reply)
891 + return -ENOMEM;
892 +
893 + /* Initialize device struct data */
894 + mcu->serdev = serdev;
895 + mcu->dev = dev;
896 + init_completion(&mcu->reply->received);
897 + mutex_init(&mcu->reply_lock);
898 + mutex_init(&mcu->lock);
899 +
900 + /* Setup UART interface */
901 + serdev_device_set_drvdata(serdev, mcu);
902 + serdev_device_set_client_ops(serdev, &iei_wt61p803_puzzle_serdev_device_ops);
903 + ret = devm_serdev_device_open(dev, serdev);
904 + if (ret)
905 + return ret;
906 + serdev_device_set_baudrate(serdev, baud);
907 + serdev_device_set_flow_control(serdev, false);
908 + ret = serdev_device_set_parity(serdev, SERDEV_PARITY_NONE);
909 + if (ret) {
910 + dev_err(dev, "Failed to set parity\n");
911 + return ret;
912 + }
913 +
914 + ret = iei_wt61p803_puzzle_get_version(mcu);
915 + if (ret)
916 + return ret;
917 +
918 + dev_dbg(dev, "MCU version: %s\n", mcu->version.version);
919 + dev_dbg(dev, "MCU firmware build info: %s\n", mcu->version.build_info);
920 + dev_dbg(dev, "MCU in bootloader mode: %s\n",
921 + mcu->version.bootloader_mode ? "true" : "false");
922 + dev_dbg(dev, "MCU protocol version: %s\n", mcu->version.protocol_version);
923 +
924 + if (device_property_read_bool(dev, "enable-beep")) {
925 + ret = iei_wt61p803_puzzle_buzzer(mcu, false);
926 + if (ret)
927 + return ret;
928 + }
929 +
930 + ret = iei_wt61p803_puzzle_sysfs_create(dev, mcu);
931 + if (ret)
932 + return ret;
933 +
934 + return devm_of_platform_populate(dev);
935 +}
936 +
937 +static void iei_wt61p803_puzzle_remove(struct serdev_device *serdev)
938 +{
939 + struct device *dev = &serdev->dev;
940 + struct iei_wt61p803_puzzle *mcu = dev_get_drvdata(dev);
941 +
942 + iei_wt61p803_puzzle_sysfs_remove(dev, mcu);
943 +}
944 +
945 +static const struct of_device_id iei_wt61p803_puzzle_dt_ids[] = {
946 + { .compatible = "iei,wt61p803-puzzle" },
947 + { }
948 +};
949 +
950 +MODULE_DEVICE_TABLE(of, iei_wt61p803_puzzle_dt_ids);
951 +
952 +static struct serdev_device_driver iei_wt61p803_puzzle_drv = {
953 + .probe = iei_wt61p803_puzzle_probe,
954 + .remove = iei_wt61p803_puzzle_remove,
955 + .driver = {
956 + .name = "iei-wt61p803-puzzle",
957 + .of_match_table = iei_wt61p803_puzzle_dt_ids,
958 + },
959 +};
960 +
961 +module_serdev_device_driver(iei_wt61p803_puzzle_drv);
962 +
963 +MODULE_LICENSE("GPL v2");
964 +MODULE_AUTHOR("Luka Kovacic <luka.kovacic@sartura.hr>");
965 +MODULE_DESCRIPTION("IEI WT61P803 PUZZLE MCU Driver");
966 --- /dev/null
967 +++ b/include/linux/mfd/iei-wt61p803-puzzle.h
968 @@ -0,0 +1,66 @@
969 +/* SPDX-License-Identifier: GPL-2.0-only */
970 +/* IEI WT61P803 PUZZLE MCU Driver
971 + * System management microcontroller for fan control, temperature sensor reading,
972 + * LED control and system identification on IEI Puzzle series ARM-based appliances.
973 + *
974 + * Copyright (C) 2020 Sartura Ltd.
975 + * Author: Luka Kovacic <luka.kovacic@sartura.hr>
976 + */
977 +
978 +#ifndef _MFD_IEI_WT61P803_PUZZLE_H_
979 +#define _MFD_IEI_WT61P803_PUZZLE_H_
980 +
981 +#define IEI_WT61P803_PUZZLE_BUF_SIZE 512
982 +
983 +/* Command magic numbers */
984 +#define IEI_WT61P803_PUZZLE_CMD_HEADER_START 0x40 /* @ */
985 +#define IEI_WT61P803_PUZZLE_CMD_HEADER_START_OTHER 0x25 /* % */
986 +#define IEI_WT61P803_PUZZLE_CMD_HEADER_EEPROM 0xF7
987 +
988 +#define IEI_WT61P803_PUZZLE_CMD_RESPONSE_OK 0x30 /* 0 */
989 +#define IEI_WT61P803_PUZZLE_CHECKSUM_RESPONSE_OK 0x70
990 +
991 +#define IEI_WT61P803_PUZZLE_CMD_EEPROM_READ 0xA1
992 +#define IEI_WT61P803_PUZZLE_CMD_EEPROM_WRITE 0xA0
993 +
994 +#define IEI_WT61P803_PUZZLE_CMD_OTHER_VERSION 0x56 /* V */
995 +#define IEI_WT61P803_PUZZLE_CMD_OTHER_BUILD 0x42 /* B */
996 +#define IEI_WT61P803_PUZZLE_CMD_OTHER_BOOTLOADER_MODE 0x4D /* M */
997 +#define IEI_WT61P803_PUZZLE_CMD_OTHER_MODE_BOOTLOADER 0x30
998 +#define IEI_WT61P803_PUZZLE_CMD_OTHER_MODE_APPS 0x31
999 +#define IEI_WT61P803_PUZZLE_CMD_OTHER_PROTOCOL_VERSION 0x50 /* P */
1000 +
1001 +#define IEI_WT61P803_PUZZLE_CMD_FUNCTION_SINGLE 0x43 /* C */
1002 +#define IEI_WT61P803_PUZZLE_CMD_FUNCTION_OTHER 0x4F /* O */
1003 +#define IEI_WT61P803_PUZZLE_CMD_FUNCTION_OTHER_STATUS 0x53 /* S */
1004 +#define IEI_WT61P803_PUZZLE_CMD_FUNCTION_OTHER_POWER_LOSS 0x41 /* A */
1005 +
1006 +#define IEI_WT61P803_PUZZLE_CMD_LED 0x52 /* R */
1007 +#define IEI_WT61P803_PUZZLE_CMD_LED_POWER 0x31 /* 1 */
1008 +
1009 +#define IEI_WT61P803_PUZZLE_CMD_TEMP 0x54 /* T */
1010 +#define IEI_WT61P803_PUZZLE_CMD_TEMP_ALL 0x41 /* A */
1011 +
1012 +#define IEI_WT61P803_PUZZLE_CMD_FAN 0x46 /* F */
1013 +#define IEI_WT61P803_PUZZLE_CMD_FAN_PWM_READ 0x5A /* Z */
1014 +#define IEI_WT61P803_PUZZLE_CMD_FAN_PWM_WRITE 0x57 /* W */
1015 +#define IEI_WT61P803_PUZZLE_CMD_FAN_PWM_BASE 0x30
1016 +#define IEI_WT61P803_PUZZLE_CMD_FAN_RPM_BASE 0x41 /* A */
1017 +
1018 +#define IEI_WT61P803_PUZZLE_CMD_FAN_PWM(x) (IEI_WT61P803_PUZZLE_CMD_FAN_PWM_BASE + (x)) /* 0 - 1 */
1019 +#define IEI_WT61P803_PUZZLE_CMD_FAN_RPM(x) (IEI_WT61P803_PUZZLE_CMD_FAN_RPM_BASE + (x)) /* 0 - 5 */
1020 +
1021 +struct iei_wt61p803_puzzle_mcu_version;
1022 +struct iei_wt61p803_puzzle_reply;
1023 +struct iei_wt61p803_puzzle;
1024 +
1025 +int iei_wt61p803_puzzle_write_command_watchdog(struct iei_wt61p803_puzzle *mcu,
1026 + unsigned char *cmd, size_t size,
1027 + unsigned char *reply_data, size_t *reply_size,
1028 + int retry_count);
1029 +
1030 +int iei_wt61p803_puzzle_write_command(struct iei_wt61p803_puzzle *mcu,
1031 + unsigned char *cmd, size_t size,
1032 + unsigned char *reply_data, size_t *reply_size);
1033 +
1034 +#endif /* _MFD_IEI_WT61P803_PUZZLE_H_ */