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