]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blob - drivers/mfd/rave-sp.c
Merge branch 'linux-4.18' of git://github.com/skeggsb/linux into drm-fixes
[mirror_ubuntu-hirsute-kernel.git] / drivers / mfd / rave-sp.c
1 // SPDX-License-Identifier: GPL-2.0+
2
3 /*
4 * Multifunction core driver for Zodiac Inflight Innovations RAVE
5 * Supervisory Processor(SP) MCU that is connected via dedicated UART
6 * port
7 *
8 * Copyright (C) 2017 Zodiac Inflight Innovations
9 */
10
11 #include <linux/atomic.h>
12 #include <linux/crc-ccitt.h>
13 #include <linux/delay.h>
14 #include <linux/export.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/kernel.h>
18 #include <linux/mfd/rave-sp.h>
19 #include <linux/module.h>
20 #include <linux/of.h>
21 #include <linux/of_device.h>
22 #include <linux/sched.h>
23 #include <linux/serdev.h>
24 #include <asm/unaligned.h>
25
26 /*
27 * UART protocol using following entities:
28 * - message to MCU => ACK response
29 * - event from MCU => event ACK
30 *
31 * Frame structure:
32 * <STX> <DATA> <CHECKSUM> <ETX>
33 * Where:
34 * - STX - is start of transmission character
35 * - ETX - end of transmission
36 * - DATA - payload
37 * - CHECKSUM - checksum calculated on <DATA>
38 *
39 * If <DATA> or <CHECKSUM> contain one of control characters, then it is
40 * escaped using <DLE> control code. Added <DLE> does not participate in
41 * checksum calculation.
42 */
43 #define RAVE_SP_STX 0x02
44 #define RAVE_SP_ETX 0x03
45 #define RAVE_SP_DLE 0x10
46
47 #define RAVE_SP_MAX_DATA_SIZE 64
48 #define RAVE_SP_CHECKSUM_8B2C 1
49 #define RAVE_SP_CHECKSUM_CCITT 2
50 #define RAVE_SP_CHECKSUM_SIZE RAVE_SP_CHECKSUM_CCITT
51 /*
52 * We don't store STX, ETX and unescaped bytes, so Rx is only
53 * DATA + CSUM
54 */
55 #define RAVE_SP_RX_BUFFER_SIZE \
56 (RAVE_SP_MAX_DATA_SIZE + RAVE_SP_CHECKSUM_SIZE)
57
58 #define RAVE_SP_STX_ETX_SIZE 2
59 /*
60 * For Tx we have to have space for everything, STX, EXT and
61 * potentially stuffed DATA + CSUM data + csum
62 */
63 #define RAVE_SP_TX_BUFFER_SIZE \
64 (RAVE_SP_STX_ETX_SIZE + 2 * RAVE_SP_RX_BUFFER_SIZE)
65
66 #define RAVE_SP_BOOT_SOURCE_GET 0
67 #define RAVE_SP_BOOT_SOURCE_SET 1
68
69 #define RAVE_SP_RDU2_BOARD_TYPE_RMB 0
70 #define RAVE_SP_RDU2_BOARD_TYPE_DEB 1
71
72 #define RAVE_SP_BOOT_SOURCE_SD 0
73 #define RAVE_SP_BOOT_SOURCE_EMMC 1
74 #define RAVE_SP_BOOT_SOURCE_NOR 2
75
76 /**
77 * enum rave_sp_deframer_state - Possible state for de-framer
78 *
79 * @RAVE_SP_EXPECT_SOF: Scanning input for start-of-frame marker
80 * @RAVE_SP_EXPECT_DATA: Got start of frame marker, collecting frame
81 * @RAVE_SP_EXPECT_ESCAPED_DATA: Got escape character, collecting escaped byte
82 */
83 enum rave_sp_deframer_state {
84 RAVE_SP_EXPECT_SOF,
85 RAVE_SP_EXPECT_DATA,
86 RAVE_SP_EXPECT_ESCAPED_DATA,
87 };
88
89 /**
90 * struct rave_sp_deframer - Device protocol deframer
91 *
92 * @state: Current state of the deframer
93 * @data: Buffer used to collect deframed data
94 * @length: Number of bytes de-framed so far
95 */
96 struct rave_sp_deframer {
97 enum rave_sp_deframer_state state;
98 unsigned char data[RAVE_SP_RX_BUFFER_SIZE];
99 size_t length;
100 };
101
102 /**
103 * struct rave_sp_reply - Reply as per RAVE device protocol
104 *
105 * @length: Expected reply length
106 * @data: Buffer to store reply payload in
107 * @code: Expected reply code
108 * @ackid: Expected reply ACK ID
109 * @completion: Successful reply reception completion
110 */
111 struct rave_sp_reply {
112 size_t length;
113 void *data;
114 u8 code;
115 u8 ackid;
116 struct completion received;
117 };
118
119 /**
120 * struct rave_sp_checksum - Variant specific checksum implementation details
121 *
122 * @length: Caculated checksum length
123 * @subroutine: Utilized checksum algorithm implementation
124 */
125 struct rave_sp_checksum {
126 size_t length;
127 void (*subroutine)(const u8 *, size_t, u8 *);
128 };
129
130 /**
131 * struct rave_sp_variant_cmds - Variant specific command routines
132 *
133 * @translate: Generic to variant specific command mapping routine
134 *
135 */
136 struct rave_sp_variant_cmds {
137 int (*translate)(enum rave_sp_command);
138 };
139
140 /**
141 * struct rave_sp_variant - RAVE supervisory processor core variant
142 *
143 * @checksum: Variant specific checksum implementation
144 * @cmd: Variant specific command pointer table
145 *
146 */
147 struct rave_sp_variant {
148 const struct rave_sp_checksum *checksum;
149 struct rave_sp_variant_cmds cmd;
150 };
151
152 /**
153 * struct rave_sp - RAVE supervisory processor core
154 *
155 * @serdev: Pointer to underlying serdev
156 * @deframer: Stored state of the protocol deframer
157 * @ackid: ACK ID used in last reply sent to the device
158 * @bus_lock: Lock to serialize access to the device
159 * @reply_lock: Lock protecting @reply
160 * @reply: Pointer to memory to store reply payload
161 *
162 * @variant: Device variant specific information
163 * @event_notifier_list: Input event notification chain
164 *
165 * @part_number_firmware: Firmware version
166 * @part_number_bootloader: Bootloader version
167 */
168 struct rave_sp {
169 struct serdev_device *serdev;
170 struct rave_sp_deframer deframer;
171 atomic_t ackid;
172 struct mutex bus_lock;
173 struct mutex reply_lock;
174 struct rave_sp_reply *reply;
175
176 const struct rave_sp_variant *variant;
177 struct blocking_notifier_head event_notifier_list;
178
179 const char *part_number_firmware;
180 const char *part_number_bootloader;
181 };
182
183 struct rave_sp_version {
184 u8 hardware;
185 __le16 major;
186 u8 minor;
187 u8 letter[2];
188 } __packed;
189
190 struct rave_sp_status {
191 struct rave_sp_version bootloader_version;
192 struct rave_sp_version firmware_version;
193 u16 rdu_eeprom_flag;
194 u16 dds_eeprom_flag;
195 u8 pic_flag;
196 u8 orientation;
197 u32 etc;
198 s16 temp[2];
199 u8 backlight_current[3];
200 u8 dip_switch;
201 u8 host_interrupt;
202 u16 voltage_28;
203 u8 i2c_device_status;
204 u8 power_status;
205 u8 general_status;
206 u8 deprecated1;
207 u8 power_led_status;
208 u8 deprecated2;
209 u8 periph_power_shutoff;
210 } __packed;
211
212 static bool rave_sp_id_is_event(u8 code)
213 {
214 return (code & 0xF0) == RAVE_SP_EVNT_BASE;
215 }
216
217 static void rave_sp_unregister_event_notifier(struct device *dev, void *res)
218 {
219 struct rave_sp *sp = dev_get_drvdata(dev->parent);
220 struct notifier_block *nb = *(struct notifier_block **)res;
221 struct blocking_notifier_head *bnh = &sp->event_notifier_list;
222
223 WARN_ON(blocking_notifier_chain_unregister(bnh, nb));
224 }
225
226 int devm_rave_sp_register_event_notifier(struct device *dev,
227 struct notifier_block *nb)
228 {
229 struct rave_sp *sp = dev_get_drvdata(dev->parent);
230 struct notifier_block **rcnb;
231 int ret;
232
233 rcnb = devres_alloc(rave_sp_unregister_event_notifier,
234 sizeof(*rcnb), GFP_KERNEL);
235 if (!rcnb)
236 return -ENOMEM;
237
238 ret = blocking_notifier_chain_register(&sp->event_notifier_list, nb);
239 if (!ret) {
240 *rcnb = nb;
241 devres_add(dev, rcnb);
242 } else {
243 devres_free(rcnb);
244 }
245
246 return ret;
247 }
248 EXPORT_SYMBOL_GPL(devm_rave_sp_register_event_notifier);
249
250 static void csum_8b2c(const u8 *buf, size_t size, u8 *crc)
251 {
252 *crc = *buf++;
253 size--;
254
255 while (size--)
256 *crc += *buf++;
257
258 *crc = 1 + ~(*crc);
259 }
260
261 static void csum_ccitt(const u8 *buf, size_t size, u8 *crc)
262 {
263 const u16 calculated = crc_ccitt_false(0xffff, buf, size);
264
265 /*
266 * While the rest of the wire protocol is little-endian,
267 * CCITT-16 CRC in RDU2 device is sent out in big-endian order.
268 */
269 put_unaligned_be16(calculated, crc);
270 }
271
272 static void *stuff(unsigned char *dest, const unsigned char *src, size_t n)
273 {
274 while (n--) {
275 const unsigned char byte = *src++;
276
277 switch (byte) {
278 case RAVE_SP_STX:
279 case RAVE_SP_ETX:
280 case RAVE_SP_DLE:
281 *dest++ = RAVE_SP_DLE;
282 /* FALLTHROUGH */
283 default:
284 *dest++ = byte;
285 }
286 }
287
288 return dest;
289 }
290
291 static int rave_sp_write(struct rave_sp *sp, const u8 *data, u8 data_size)
292 {
293 const size_t checksum_length = sp->variant->checksum->length;
294 unsigned char frame[RAVE_SP_TX_BUFFER_SIZE];
295 unsigned char crc[RAVE_SP_CHECKSUM_SIZE];
296 unsigned char *dest = frame;
297 size_t length;
298
299 if (WARN_ON(checksum_length > sizeof(crc)))
300 return -ENOMEM;
301
302 if (WARN_ON(data_size > sizeof(frame)))
303 return -ENOMEM;
304
305 sp->variant->checksum->subroutine(data, data_size, crc);
306
307 *dest++ = RAVE_SP_STX;
308 dest = stuff(dest, data, data_size);
309 dest = stuff(dest, crc, checksum_length);
310 *dest++ = RAVE_SP_ETX;
311
312 length = dest - frame;
313
314 print_hex_dump_debug("rave-sp tx: ", DUMP_PREFIX_NONE,
315 16, 1, frame, length, false);
316
317 return serdev_device_write(sp->serdev, frame, length, HZ);
318 }
319
320 static u8 rave_sp_reply_code(u8 command)
321 {
322 /*
323 * There isn't a single rule that describes command code ->
324 * ACK code transformation, but, going through various
325 * versions of ICDs, there appear to be three distinct groups
326 * that can be described by simple transformation.
327 */
328 switch (command) {
329 case 0xA0 ... 0xBE:
330 /*
331 * Commands implemented by firmware found in RDU1 and
332 * older devices all seem to obey the following rule
333 */
334 return command + 0x20;
335 case 0xE0 ... 0xEF:
336 /*
337 * Events emitted by all versions of the firmare use
338 * least significant bit to get an ACK code
339 */
340 return command | 0x01;
341 default:
342 /*
343 * Commands implemented by firmware found in RDU2 are
344 * similar to "old" commands, but they use slightly
345 * different offset
346 */
347 return command + 0x40;
348 }
349 }
350
351 int rave_sp_exec(struct rave_sp *sp,
352 void *__data, size_t data_size,
353 void *reply_data, size_t reply_data_size)
354 {
355 struct rave_sp_reply reply = {
356 .data = reply_data,
357 .length = reply_data_size,
358 .received = COMPLETION_INITIALIZER_ONSTACK(reply.received),
359 };
360 unsigned char *data = __data;
361 int command, ret = 0;
362 u8 ackid;
363
364 command = sp->variant->cmd.translate(data[0]);
365 if (command < 0)
366 return command;
367
368 ackid = atomic_inc_return(&sp->ackid);
369 reply.ackid = ackid;
370 reply.code = rave_sp_reply_code((u8)command),
371
372 mutex_lock(&sp->bus_lock);
373
374 mutex_lock(&sp->reply_lock);
375 sp->reply = &reply;
376 mutex_unlock(&sp->reply_lock);
377
378 data[0] = command;
379 data[1] = ackid;
380
381 rave_sp_write(sp, data, data_size);
382
383 if (!wait_for_completion_timeout(&reply.received, HZ)) {
384 dev_err(&sp->serdev->dev, "Command timeout\n");
385 ret = -ETIMEDOUT;
386
387 mutex_lock(&sp->reply_lock);
388 sp->reply = NULL;
389 mutex_unlock(&sp->reply_lock);
390 }
391
392 mutex_unlock(&sp->bus_lock);
393 return ret;
394 }
395 EXPORT_SYMBOL_GPL(rave_sp_exec);
396
397 static void rave_sp_receive_event(struct rave_sp *sp,
398 const unsigned char *data, size_t length)
399 {
400 u8 cmd[] = {
401 [0] = rave_sp_reply_code(data[0]),
402 [1] = data[1],
403 };
404
405 rave_sp_write(sp, cmd, sizeof(cmd));
406
407 blocking_notifier_call_chain(&sp->event_notifier_list,
408 rave_sp_action_pack(data[0], data[2]),
409 NULL);
410 }
411
412 static void rave_sp_receive_reply(struct rave_sp *sp,
413 const unsigned char *data, size_t length)
414 {
415 struct device *dev = &sp->serdev->dev;
416 struct rave_sp_reply *reply;
417 const size_t payload_length = length - 2;
418
419 mutex_lock(&sp->reply_lock);
420 reply = sp->reply;
421
422 if (reply) {
423 if (reply->code == data[0] && reply->ackid == data[1] &&
424 payload_length >= reply->length) {
425 /*
426 * We are relying on memcpy(dst, src, 0) to be a no-op
427 * when handling commands that have a no-payload reply
428 */
429 memcpy(reply->data, &data[2], reply->length);
430 complete(&reply->received);
431 sp->reply = NULL;
432 } else {
433 dev_err(dev, "Ignoring incorrect reply\n");
434 dev_dbg(dev, "Code: expected = 0x%08x received = 0x%08x\n",
435 reply->code, data[0]);
436 dev_dbg(dev, "ACK ID: expected = 0x%08x received = 0x%08x\n",
437 reply->ackid, data[1]);
438 dev_dbg(dev, "Length: expected = %zu received = %zu\n",
439 reply->length, payload_length);
440 }
441 }
442
443 mutex_unlock(&sp->reply_lock);
444 }
445
446 static void rave_sp_receive_frame(struct rave_sp *sp,
447 const unsigned char *data,
448 size_t length)
449 {
450 const size_t checksum_length = sp->variant->checksum->length;
451 const size_t payload_length = length - checksum_length;
452 const u8 *crc_reported = &data[payload_length];
453 struct device *dev = &sp->serdev->dev;
454 u8 crc_calculated[RAVE_SP_CHECKSUM_SIZE];
455
456 if (unlikely(checksum_length > sizeof(crc_calculated))) {
457 dev_warn(dev, "Checksum too long, dropping\n");
458 return;
459 }
460
461 print_hex_dump_debug("rave-sp rx: ", DUMP_PREFIX_NONE,
462 16, 1, data, length, false);
463
464 if (unlikely(length <= checksum_length)) {
465 dev_warn(dev, "Dropping short frame\n");
466 return;
467 }
468
469 sp->variant->checksum->subroutine(data, payload_length,
470 crc_calculated);
471
472 if (memcmp(crc_calculated, crc_reported, checksum_length)) {
473 dev_warn(dev, "Dropping bad frame\n");
474 return;
475 }
476
477 if (rave_sp_id_is_event(data[0]))
478 rave_sp_receive_event(sp, data, length);
479 else
480 rave_sp_receive_reply(sp, data, length);
481 }
482
483 static int rave_sp_receive_buf(struct serdev_device *serdev,
484 const unsigned char *buf, size_t size)
485 {
486 struct device *dev = &serdev->dev;
487 struct rave_sp *sp = dev_get_drvdata(dev);
488 struct rave_sp_deframer *deframer = &sp->deframer;
489 const unsigned char *src = buf;
490 const unsigned char *end = buf + size;
491
492 while (src < end) {
493 const unsigned char byte = *src++;
494
495 switch (deframer->state) {
496 case RAVE_SP_EXPECT_SOF:
497 if (byte == RAVE_SP_STX)
498 deframer->state = RAVE_SP_EXPECT_DATA;
499 break;
500
501 case RAVE_SP_EXPECT_DATA:
502 /*
503 * Treat special byte values first
504 */
505 switch (byte) {
506 case RAVE_SP_ETX:
507 rave_sp_receive_frame(sp,
508 deframer->data,
509 deframer->length);
510 /*
511 * Once we extracted a complete frame
512 * out of a stream, we call it done
513 * and proceed to bailing out while
514 * resetting the framer to initial
515 * state, regardless if we've consumed
516 * all of the stream or not.
517 */
518 goto reset_framer;
519 case RAVE_SP_STX:
520 dev_warn(dev, "Bad frame: STX before ETX\n");
521 /*
522 * If we encounter second "start of
523 * the frame" marker before seeing
524 * corresponding "end of frame", we
525 * reset the framer and ignore both:
526 * frame started by first SOF and
527 * frame started by current SOF.
528 *
529 * NOTE: The above means that only the
530 * frame started by third SOF, sent
531 * after this one will have a chance
532 * to get throught.
533 */
534 goto reset_framer;
535 case RAVE_SP_DLE:
536 deframer->state = RAVE_SP_EXPECT_ESCAPED_DATA;
537 /*
538 * If we encounter escape sequence we
539 * need to skip it and collect the
540 * byte that follows. We do it by
541 * forcing the next iteration of the
542 * encompassing while loop.
543 */
544 continue;
545 }
546 /*
547 * For the rest of the bytes, that are not
548 * speical snoflakes, we do the same thing
549 * that we do to escaped data - collect it in
550 * deframer buffer
551 */
552
553 /* FALLTHROUGH */
554
555 case RAVE_SP_EXPECT_ESCAPED_DATA:
556 if (deframer->length == sizeof(deframer->data)) {
557 dev_warn(dev, "Bad frame: Too long\n");
558 /*
559 * If the amount of data we've
560 * accumulated for current frame so
561 * far starts to exceed the capacity
562 * of deframer's buffer, there's
563 * nothing else we can do but to
564 * discard that data and start
565 * assemblying a new frame again
566 */
567 goto reset_framer;
568 }
569
570 deframer->data[deframer->length++] = byte;
571
572 /*
573 * We've extracted out special byte, now we
574 * can go back to regular data collecting
575 */
576 deframer->state = RAVE_SP_EXPECT_DATA;
577 break;
578 }
579 }
580
581 /*
582 * The only way to get out of the above loop and end up here
583 * is throught consuming all of the supplied data, so here we
584 * report that we processed it all.
585 */
586 return size;
587
588 reset_framer:
589 /*
590 * NOTE: A number of codepaths that will drop us here will do
591 * so before consuming all 'size' bytes of the data passed by
592 * serdev layer. We rely on the fact that serdev layer will
593 * re-execute this handler with the remainder of the Rx bytes
594 * once we report actual number of bytes that we processed.
595 */
596 deframer->state = RAVE_SP_EXPECT_SOF;
597 deframer->length = 0;
598
599 return src - buf;
600 }
601
602 static int rave_sp_rdu1_cmd_translate(enum rave_sp_command command)
603 {
604 if (command >= RAVE_SP_CMD_STATUS &&
605 command <= RAVE_SP_CMD_CONTROL_EVENTS)
606 return command;
607
608 return -EINVAL;
609 }
610
611 static int rave_sp_rdu2_cmd_translate(enum rave_sp_command command)
612 {
613 if (command >= RAVE_SP_CMD_GET_FIRMWARE_VERSION &&
614 command <= RAVE_SP_CMD_GET_GPIO_STATE)
615 return command;
616
617 if (command == RAVE_SP_CMD_REQ_COPPER_REV) {
618 /*
619 * As per RDU2 ICD 3.4.47 CMD_GET_COPPER_REV code is
620 * different from that for RDU1 and it is set to 0x28.
621 */
622 return 0x28;
623 }
624
625 return rave_sp_rdu1_cmd_translate(command);
626 }
627
628 static int rave_sp_default_cmd_translate(enum rave_sp_command command)
629 {
630 /*
631 * All of the following command codes were taken from "Table :
632 * Communications Protocol Message Types" in section 3.3
633 * "MESSAGE TYPES" of Rave PIC24 ICD.
634 */
635 switch (command) {
636 case RAVE_SP_CMD_GET_FIRMWARE_VERSION:
637 return 0x11;
638 case RAVE_SP_CMD_GET_BOOTLOADER_VERSION:
639 return 0x12;
640 case RAVE_SP_CMD_BOOT_SOURCE:
641 return 0x14;
642 case RAVE_SP_CMD_SW_WDT:
643 return 0x1C;
644 case RAVE_SP_CMD_RESET:
645 return 0x1E;
646 case RAVE_SP_CMD_RESET_REASON:
647 return 0x1F;
648 default:
649 return -EINVAL;
650 }
651 }
652
653 static const char *devm_rave_sp_version(struct device *dev,
654 struct rave_sp_version *version)
655 {
656 /*
657 * NOTE: The format string below uses %02d to display u16
658 * intentionally for the sake of backwards compatibility with
659 * legacy software.
660 */
661 return devm_kasprintf(dev, GFP_KERNEL, "%02d%02d%02d.%c%c\n",
662 version->hardware,
663 le16_to_cpu(version->major),
664 version->minor,
665 version->letter[0],
666 version->letter[1]);
667 }
668
669 static int rave_sp_get_status(struct rave_sp *sp)
670 {
671 struct device *dev = &sp->serdev->dev;
672 u8 cmd[] = {
673 [0] = RAVE_SP_CMD_STATUS,
674 [1] = 0
675 };
676 struct rave_sp_status status;
677 const char *version;
678 int ret;
679
680 ret = rave_sp_exec(sp, cmd, sizeof(cmd), &status, sizeof(status));
681 if (ret)
682 return ret;
683
684 version = devm_rave_sp_version(dev, &status.firmware_version);
685 if (!version)
686 return -ENOMEM;
687
688 sp->part_number_firmware = version;
689
690 version = devm_rave_sp_version(dev, &status.bootloader_version);
691 if (!version)
692 return -ENOMEM;
693
694 sp->part_number_bootloader = version;
695
696 return 0;
697 }
698
699 static const struct rave_sp_checksum rave_sp_checksum_8b2c = {
700 .length = 1,
701 .subroutine = csum_8b2c,
702 };
703
704 static const struct rave_sp_checksum rave_sp_checksum_ccitt = {
705 .length = 2,
706 .subroutine = csum_ccitt,
707 };
708
709 static const struct rave_sp_variant rave_sp_legacy = {
710 .checksum = &rave_sp_checksum_8b2c,
711 .cmd = {
712 .translate = rave_sp_default_cmd_translate,
713 },
714 };
715
716 static const struct rave_sp_variant rave_sp_rdu1 = {
717 .checksum = &rave_sp_checksum_8b2c,
718 .cmd = {
719 .translate = rave_sp_rdu1_cmd_translate,
720 },
721 };
722
723 static const struct rave_sp_variant rave_sp_rdu2 = {
724 .checksum = &rave_sp_checksum_ccitt,
725 .cmd = {
726 .translate = rave_sp_rdu2_cmd_translate,
727 },
728 };
729
730 static const struct of_device_id rave_sp_dt_ids[] = {
731 { .compatible = "zii,rave-sp-niu", .data = &rave_sp_legacy },
732 { .compatible = "zii,rave-sp-mezz", .data = &rave_sp_legacy },
733 { .compatible = "zii,rave-sp-esb", .data = &rave_sp_legacy },
734 { .compatible = "zii,rave-sp-rdu1", .data = &rave_sp_rdu1 },
735 { .compatible = "zii,rave-sp-rdu2", .data = &rave_sp_rdu2 },
736 { /* sentinel */ }
737 };
738
739 static const struct serdev_device_ops rave_sp_serdev_device_ops = {
740 .receive_buf = rave_sp_receive_buf,
741 .write_wakeup = serdev_device_write_wakeup,
742 };
743
744 static int rave_sp_probe(struct serdev_device *serdev)
745 {
746 struct device *dev = &serdev->dev;
747 const char *unknown = "unknown\n";
748 struct rave_sp *sp;
749 u32 baud;
750 int ret;
751
752 if (of_property_read_u32(dev->of_node, "current-speed", &baud)) {
753 dev_err(dev,
754 "'current-speed' is not specified in device node\n");
755 return -EINVAL;
756 }
757
758 sp = devm_kzalloc(dev, sizeof(*sp), GFP_KERNEL);
759 if (!sp)
760 return -ENOMEM;
761
762 sp->serdev = serdev;
763 dev_set_drvdata(dev, sp);
764
765 sp->variant = of_device_get_match_data(dev);
766 if (!sp->variant)
767 return -ENODEV;
768
769 mutex_init(&sp->bus_lock);
770 mutex_init(&sp->reply_lock);
771 BLOCKING_INIT_NOTIFIER_HEAD(&sp->event_notifier_list);
772
773 serdev_device_set_client_ops(serdev, &rave_sp_serdev_device_ops);
774 ret = devm_serdev_device_open(dev, serdev);
775 if (ret)
776 return ret;
777
778 serdev_device_set_baudrate(serdev, baud);
779
780 ret = rave_sp_get_status(sp);
781 if (ret) {
782 dev_warn(dev, "Failed to get firmware status: %d\n", ret);
783 sp->part_number_firmware = unknown;
784 sp->part_number_bootloader = unknown;
785 }
786
787 /*
788 * Those strings already have a \n embedded, so there's no
789 * need to have one in format string.
790 */
791 dev_info(dev, "Firmware version: %s", sp->part_number_firmware);
792 dev_info(dev, "Bootloader version: %s", sp->part_number_bootloader);
793
794 return devm_of_platform_populate(dev);
795 }
796
797 MODULE_DEVICE_TABLE(of, rave_sp_dt_ids);
798
799 static struct serdev_device_driver rave_sp_drv = {
800 .probe = rave_sp_probe,
801 .driver = {
802 .name = "rave-sp",
803 .of_match_table = rave_sp_dt_ids,
804 },
805 };
806 module_serdev_device_driver(rave_sp_drv);
807
808 MODULE_LICENSE("GPL");
809 MODULE_AUTHOR("Andrey Vostrikov <andrey.vostrikov@cogentembedded.com>");
810 MODULE_AUTHOR("Nikita Yushchenko <nikita.yoush@cogentembedded.com>");
811 MODULE_AUTHOR("Andrey Smirnov <andrew.smirnov@gmail.com>");
812 MODULE_DESCRIPTION("RAVE SP core driver");