-
Notifications
You must be signed in to change notification settings - Fork 860
/
Copy pathjesd204-core.c
1374 lines (1139 loc) · 34.1 KB
/
jesd204-core.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SPDX-License-Identifier: GPL-2.0+
/**
* The JESD204 framework - core logic
*
* Copyright (c) 2019-2024 Analog Devices Inc.
*/
#define pr_fmt(fmt) "jesd204: " fmt
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/property.h>
#include <linux/slab.h>
#include "jesd204-priv.h"
/* IDA to assign each registered device a unique id */
static DEFINE_IDA(jesd204_ida);
static struct bus_type jesd204_bus_type = {
.name = "jesd204",
};
static LIST_HEAD(jesd204_device_list);
static LIST_HEAD(jesd204_topologies);
static unsigned int jesd204_device_count;
static unsigned int jesd204_topologies_count;
static unsigned int jesd204_con_id_counter;
static bool jesd204_dyn_dt_change;
static void jesd204_dev_unregister(struct jesd204_dev *jdev);
/**
* jesd204_get_active_links_num() - Get the number of active links for a JESD204 device.
* @param jdev The JESD204 device structure.
*
* This function retrieves the number of active links for a given JESD204 device.
*
* @return The number of active links on success, or a negative error code on failure.
* -EINVAL is returned if jdev is NULL.
* -EFAULT is returned if the top-level device cannot be found.
*/
int jesd204_get_active_links_num(struct jesd204_dev *jdev)
{
struct jesd204_dev_top *jdev_top;
if (!jdev)
return -EINVAL;
jdev_top = jesd204_dev_get_topology_top_dev(jdev);
if (!jdev_top) {
jesd204_err(jdev, "Could not find top-level device\n");
return -EFAULT;
}
return jdev_top->num_links;
}
EXPORT_SYMBOL_GPL(jesd204_get_active_links_num);
/**
* jesd204_get_links_data() - Get the links data for a JESD204 device
* @jdev: Pointer to the JESD204 device structure
* @links: Array of pointers to store the links data
* @num_links: Number of links to retrieve
*
* This function retrieves the links data for a JESD204 device. It takes a
* pointer to the JESD204 device structure, an array of pointers to store the
* links data, and the number of links to retrieve.
*
* Returns 0 on success, or a negative error code on failure.
*/
int jesd204_get_links_data(struct jesd204_dev *jdev,
struct jesd204_link ** const links,
const unsigned int num_links)
{
struct jesd204_dev_top *jdev_top;
unsigned int i;
if (!jdev || !links || !num_links)
return -EINVAL;
jdev_top = jesd204_dev_get_topology_top_dev(jdev);
if (!jdev_top) {
jesd204_err(jdev, "Could not find top-level device\n");
return -EFAULT;
}
if (jdev_top->num_links != num_links) {
jesd204_err(jdev,
"Link number mismatch: top-level has %u, call has %u\n",
jdev_top->num_links, num_links);
return -EINVAL;
}
for (i = 0; i < jdev_top->num_links; i++)
links[i] = &jdev_top->active_links[i].link;
return 0;
}
EXPORT_SYMBOL_GPL(jesd204_get_links_data);
/**
* jesd204_link_get_state_str() - Get the state string of a JESD204 link
* @lnk: Pointer to the JESD204 link structure
*
* This function returns a string representation of the state of a JESD204 link.
* It takes a pointer to the JESD204 link structure and retrieves the state string
* by calling the jesd204_state_str() function.
*
* Return: Pointer to the state string
*/
const char *jesd204_link_get_state_str(const struct jesd204_link *lnk)
{
struct jesd204_link_opaque *ol =
container_of(lnk, struct jesd204_link_opaque, link);
return jesd204_state_str(ol->state);
}
EXPORT_SYMBOL_GPL(jesd204_link_get_state_str);
/**
* jesd204_link_get_paused() - Check if the JESD204 link is paused.
* @lnk: Pointer to the JESD204 link structure.
*
* This function checks if the JESD204 link is currently paused.
*
* Return: true if the link is paused, false otherwise.
*/
bool jesd204_link_get_paused(const struct jesd204_link *lnk)
{
struct jesd204_link_opaque *ol =
container_of(lnk, struct jesd204_link_opaque, link);
return ol->fsm_paused;
}
EXPORT_SYMBOL_GPL(jesd204_link_get_paused);
int jesd204_device_count_get(void)
{
return jesd204_device_count;
}
static bool jesd204_dev_has_con_in_topology(struct jesd204_dev *jdev,
struct jesd204_dev_top *jdev_top)
{
struct jesd204_dev_con_out *c;
int i;
list_for_each_entry(c, &jdev->outputs, entry) {
if (c->jdev_top == jdev_top)
return true;
}
for (i = 0; i < jdev->inputs_count; i++) {
c = jdev->inputs[i];
if (c->jdev_top == jdev_top)
return true;
}
return false;
}
static int jesd204_link_validate_params(const struct jesd204_link *lnk)
{
if (!lnk->num_lanes) {
pr_err("link[%u], number of lanes is zero\n", lnk->link_id);
return -EINVAL;
}
if (!lnk->num_converters) {
pr_err("link[%u], number of converters is zero\n", lnk->link_id);
return -EINVAL;
}
if (!lnk->bits_per_sample) {
pr_err("link[%u], bits-per-sample is zero\n", lnk->link_id);
return -EINVAL;
}
if (!lnk->sample_rate) {
pr_err("link[%u], sample rate is zero\n", lnk->link_id);
return -EINVAL;
}
return 0;
}
/**
* jesd204_link_get_rate() - Get the lane rate for a JESD204 link
* @lnk: Pointer to the JESD204 link structure
* @lane_rate_hz: Pointer to store the calculated lane rate in Hz
*
* This function calculates the lane rate for a JESD204 link based on the
* parameters specified in the JESD204 link structure. The calculated lane
* rate is stored in the variable pointed to by @lane_rate_hz.
*
* Return: 0 on success, negative error code on failure
*/
int jesd204_link_get_rate(struct jesd204_link *lnk, u64 *lane_rate_hz)
{
u64 rate, encoding_n, encoding_d;
u32 sample_rate_div;
int ret;
ret = jesd204_link_validate_params(lnk);
if (ret)
return ret;
switch (lnk->jesd_version) {
case JESD204_VERSION_C:
switch (lnk->jesd_encoder) {
case JESD204_ENCODER_64B66B:
encoding_n = 66; /* JESD 204C */
encoding_d = 64;
break;
case JESD204_ENCODER_8B10B:
encoding_n = 10; /* JESD 204C */
encoding_d = 8;
break;
case JESD204_ENCODER_64B80B:
encoding_n = 80; /* JESD 204C */
encoding_d = 64;
break;
default:
return -EINVAL;
}
break;
default:
encoding_n = 10; /* JESD 204AB */
encoding_d = 8;
break;
}
sample_rate_div = lnk->sample_rate_div ? lnk->sample_rate_div : 1;
rate = lnk->num_converters * lnk->bits_per_sample *
encoding_n * lnk->sample_rate;
do_div(rate, lnk->num_lanes * encoding_d * sample_rate_div);
*lane_rate_hz = rate;
return 0;
}
EXPORT_SYMBOL_GPL(jesd204_link_get_rate);
/**
* jesd204_link_get_rate_khz() - Get the lane rate in kilohertz for a JESD204 link
* @lnk: Pointer to the JESD204 link structure
* @lane_rate_khz: Pointer to store the lane rate in kilohertz
*
* This function retrieves the lane rate in kilohertz for a JESD204 link.
* It calls jesd204_link_get_rate() to get the lane rate in hertz and then
* converts it to kilohertz by dividing it by 1000.
*
* Return: 0 on success, negative error code on failure
*/
int jesd204_link_get_rate_khz(struct jesd204_link *lnk,
unsigned long *lane_rate_khz)
{
u64 lane_rate_hz;
int ret;
ret = jesd204_link_get_rate(lnk, &lane_rate_hz);
if (ret)
return ret;
*lane_rate_khz = DIV_ROUND_CLOSEST_ULL(lane_rate_hz, 1000);
return ret;
}
EXPORT_SYMBOL_GPL(jesd204_link_get_rate_khz);
/**
* jesd204_link_get_device_clock() - Get the device clock frequency for a JESD204 link
* @lnk: Pointer to the JESD204 link structure
* @device_clock: Pointer to store the device clock frequency in Hz
*
* This function calculates the device clock frequency for a JESD204 link based on the
* lane rate and encoding scheme. The resulting device clock frequency is stored in the
* variable pointed to by @device_clock.
*
* Return: 0 on success, negative error code on failure
*/
int jesd204_link_get_device_clock(struct jesd204_link *lnk,
unsigned long *device_clock)
{
u64 lane_rate_hz;
u32 encoding_n;
int ret;
ret = jesd204_link_get_rate(lnk, &lane_rate_hz);
if (ret)
return ret;
switch (lnk->jesd_version) {
case JESD204_VERSION_C:
switch (lnk->jesd_encoder) {
case JESD204_ENCODER_64B66B:
encoding_n = 66; /* JESD 204C */
break;
case JESD204_ENCODER_8B10B:
encoding_n = 40; /* JESD 204ABC */
break;
case JESD204_ENCODER_64B80B:
encoding_n = 80; /* JESD 204C */
break;
default:
return -EINVAL;
}
break;
default:
encoding_n = 40; /* JESD 204AB */
break;
}
do_div(lane_rate_hz, encoding_n);
*device_clock = lane_rate_hz;
return ret;
}
EXPORT_SYMBOL_GPL(jesd204_link_get_device_clock);
/**
* jesd204_copy_link_params() - Copy link parameters from one JESD204 link to another
* @dst: Pointer to the destination JESD204 link structure
* @src: Pointer to the source JESD204 link structure
*
* This function copies the link parameters from the source JESD204 link structure
* to the destination JESD204 link structure.
*/
void jesd204_copy_link_params(struct jesd204_link *dst,
const struct jesd204_link *src)
{
dst->is_transmit = src->is_transmit;
dst->num_lanes = src->num_lanes;
dst->num_converters = src->num_converters;
dst->octets_per_frame = src->octets_per_frame;
dst->frames_per_multiframe = src->frames_per_multiframe;
dst->num_of_multiblocks_in_emb = src->num_of_multiblocks_in_emb;
dst->bits_per_sample = src->bits_per_sample;
dst->converter_resolution = src->converter_resolution;
dst->jesd_version = src->jesd_version;
dst->jesd_encoder = src->jesd_encoder;
dst->subclass = src->subclass;
dst->device_id = src->device_id;
dst->bank_id = src->bank_id;
dst->scrambling = src->scrambling;
dst->high_density = src->high_density;
dst->ctrl_words_per_frame_clk = src->ctrl_words_per_frame_clk;
dst->ctrl_bits_per_sample = src->ctrl_bits_per_sample;
dst->samples_per_conv_frame = src->samples_per_conv_frame;
dst->dac_adj_resolution_steps = src->dac_adj_resolution_steps;
dst->dac_adj_direction = src->dac_adj_direction;
dst->dac_phase_adj = src->dac_phase_adj;
dst->sysref.mode = src->sysref.mode;
dst->sysref.capture_falling_edge = src->sysref.capture_falling_edge;
dst->sysref.valid_falling_edge = src->sysref.valid_falling_edge;
dst->sysref.lmfc_offset = src->sysref.lmfc_offset;
}
EXPORT_SYMBOL_GPL(jesd204_copy_link_params);
/**
* jesd204_link_get_lmfc_lemc_rate() - Get the LMFC/LEMC rate for a JESD204 link
* @lnk: Pointer to the JESD204 link structure
* @rate_hz: Pointer to store the LMFC/LEMC rate in Hz
*
* This function calculates the LMFC/LEMC rate for a JESD204 link based on the
* link configuration parameters. The calculated rate is stored in the variable
* pointed to by @rate_hz.
*
* Return: 0 on success, negative error code on failure
*/
int jesd204_link_get_lmfc_lemc_rate(struct jesd204_link *lnk,
unsigned long *rate_hz)
{
u64 lane_rate_hz;
u32 bkw;
int ret;
ret = jesd204_link_get_rate(lnk, &lane_rate_hz);
if (ret)
return ret;
switch (lnk->jesd_version) {
case JESD204_VERSION_C:
switch (lnk->jesd_encoder) {
case JESD204_ENCODER_64B66B:
bkw = 66; /* JESD 204C */
fallthrough;
case JESD204_ENCODER_64B80B:
if (lnk->jesd_encoder == JESD204_ENCODER_64B80B)
bkw = 80; /* JESD 204C */
if (lnk->num_of_multiblocks_in_emb) {
do_div(lane_rate_hz, bkw * 32 *
lnk->num_of_multiblocks_in_emb);
} else {
lane_rate_hz *= 8;
do_div(lane_rate_hz, bkw *
lnk->octets_per_frame *
lnk->frames_per_multiframe);
}
break;
case JESD204_ENCODER_8B10B:
do_div(lane_rate_hz, 10 * lnk->octets_per_frame *
lnk->frames_per_multiframe);
break;
default:
return -EINVAL;
}
break;
default:
do_div(lane_rate_hz, 10 * lnk->octets_per_frame *
lnk->frames_per_multiframe);
break;
}
*rate_hz = lane_rate_hz;
return 0;
}
EXPORT_SYMBOL_GPL(jesd204_link_get_lmfc_lemc_rate);
struct jesd204_dev_top *jesd204_dev_get_topology_top_dev(struct jesd204_dev *jdev)
{
struct jesd204_dev_top *jdev_top = jesd204_dev_top_dev(jdev);
if (jdev_top)
return jdev_top;
/* FIXME: enforce that one jdev object can only be in one topology */
list_for_each_entry(jdev_top, &jesd204_topologies, entry) {
if (!jesd204_dev_has_con_in_topology(jdev, jdev_top))
continue;
return jdev_top;
}
jesd204_warn(jdev, "Device isn't a top-device, nor does it belong to topology with top-device\n");
return NULL;
}
/**
* jesd204_sysref_async() - Trigger an asynchronous SYSREF event
* @jdev: The JESD204 device structure
*
* This function triggers an asynchronous SYSREF event for a given JESD204 device.
*
* Return: 0 on success, or a negative error code on failure.
*/
int jesd204_sysref_async(struct jesd204_dev *jdev)
{
struct jesd204_dev_top *jdev_top = jesd204_dev_get_topology_top_dev(jdev);
const struct jesd204_dev_data *dev_data;
if (!jdev_top)
return -EFAULT;
/* No SYSREF registered for this topology */
if (!jdev_top->jdev_sysref)
return 0;
if (!jdev_top->jdev_sysref->dev_data)
return -EFAULT;
dev_data = jdev_top->jdev_sysref->dev_data;
/* By now, this should have been validated to have sysref_cb() */
return dev_data->sysref_cb(jdev_top->jdev_sysref);
}
EXPORT_SYMBOL(jesd204_sysref_async);
/**
* jesd204_sysref_async_force() - Trigger an asynchronous SYSREF event
* @jdev: The JESD204 device structure
*
* This function triggers an asynchronous SYSREF event for a given JESD204 device.
* If there is no primary SYSREF registered for the topology, it will trigger the
* secondary SYSREF. If there is no secondary SYSREF registered, it will return 0.
*
* Return: 0 on success, or a negative error code on failure.
*/
int jesd204_sysref_async_force(struct jesd204_dev *jdev)
{
struct jesd204_dev_top *jdev_top = jesd204_dev_get_topology_top_dev(jdev);
const struct jesd204_dev_data *dev_data;
if (!jdev_top)
return -EFAULT;
/* Primary SYSREF registered for this topology? */
if (jdev_top->jdev_sysref)
return jesd204_sysref_async(jdev);
/* No SYSREF registered for this topology */
if (!jdev_top->jdev_sysref_sec)
return 0;
if (!jdev_top->jdev_sysref_sec->dev_data)
return -EFAULT;
dev_data = jdev_top->jdev_sysref_sec->dev_data;
/* By now, this should have been validated to have sysref_cb() */
return dev_data->sysref_cb(jdev_top->jdev_sysref_sec);
}
EXPORT_SYMBOL(jesd204_sysref_async_force);
/**
* jesd204_dev_is_top() - Check if the given JESD204 device is the top-level device.
* @jdev: The JESD204 device to check.
*
* This function checks if the given JESD204 device is the top-level device in the topology.
*
* Return: True if the device is the top-level device, false otherwise.
*/
bool jesd204_dev_is_top(struct jesd204_dev *jdev)
{
return jdev && jdev->is_top;
}
EXPORT_SYMBOL(jesd204_dev_is_top);
static inline bool dev_is_jesd204_dev(struct device *dev)
{
return device_property_read_bool(dev, "jesd204-device");
}
/**
* jesd204_dev_priv() - Retrieves the private data associated with a JESD204 device
* @jdev: The JESD204 device for which to retrieve the private data
*
* This function returns a pointer to the private data structure associated with
* the given JESD204 device. The private data structure contains device-specific
* information and state.
*
* Return: A pointer to the private data structure.
*/
void *jesd204_dev_priv(struct jesd204_dev *jdev)
{
return jdev->priv;
}
EXPORT_SYMBOL_GPL(jesd204_dev_priv);
/**
* jesd204_dev_from_device() - Retrieve the jesd204_dev structure associated with a given device.
* @dev: Pointer to the device structure.
*
* This function searches the jesd204_device_list for a jesd204_dev structure
* whose parent device matches the given device. If a match is found, a pointer
* to the jesd204_dev structure is returned. Otherwise, NULL is returned.
*
* Return: Pointer to the jesd204_dev structure if found, NULL otherwise.
*/
struct jesd204_dev *jesd204_dev_from_device(struct device *dev)
{
struct jesd204_dev *jdev;
if (!dev)
return NULL;
list_for_each_entry(jdev, &jesd204_device_list, entry) {
if (jdev->dev.parent && jdev->dev.parent == dev)
return jdev;
}
return NULL;
}
EXPORT_SYMBOL_GPL(jesd204_dev_from_device);
/**
* jesd204_dev_to_device() - Retrieves the parent device associated with a JESD204 device.
* @jdev: Pointer to the JESD204 device structure.
*
* This function takes a pointer to a JESD204 device structure and returns
* a pointer to the parent device structure. If the input JESD204 device
* pointer is NULL, the function returns NULL.
*
* Return: Pointer to the parent device structure, or NULL if @jdev is NULL.
*/
struct device *jesd204_dev_to_device(struct jesd204_dev *jdev)
{
return jdev ? jdev->dev.parent : NULL;
}
EXPORT_SYMBOL_GPL(jesd204_dev_to_device);
static void __jesd204_printk(const char *level, const struct jesd204_dev *jdev,
struct va_format *vaf)
{
const struct device *dev;
if (!jdev) {
printk("%sjesd204: (NULL jesd204 device *): %pV", level, vaf);
return;
}
if (!jdev->dev.parent) {
printk("%sjesd204: %pOF: %pV", level, jdev->np, vaf);
return;
}
dev = &jdev->dev;
dev_printk_emit(level[1] - '0', dev, "jesd204: %pOF,%s,parent=%s: %pV",
jdev->np,
dev_name(dev),
dev_name(dev->parent),
vaf);
}
void jesd204_printk(const char *level, const struct jesd204_dev *jdev,
const char *fmt, ...)
{
struct va_format vaf;
va_list args;
va_start(args, fmt);
vaf.fmt = fmt;
vaf.va = &args;
__jesd204_printk(level, jdev, &vaf);
va_end(args);
}
EXPORT_SYMBOL_GPL(jesd204_printk);
static int jesd204_dev_alloc_links(struct jesd204_dev_top *jdev_top)
{
struct jesd204_link_opaque *links;
size_t mem_size;
unsigned int i;
mem_size = jdev_top->num_links * sizeof(*links);
links = kzalloc(mem_size, GFP_KERNEL);
if (!links)
return -ENOMEM;
jdev_top->active_links = links;
for (i = 0; i < jdev_top->num_links; i++) {
links[i].jdev_top = jdev_top;
links[i].link_idx = i;
links[i].link.link_id = jdev_top->link_ids[i];
}
links = kzalloc(mem_size, GFP_KERNEL);
if (!links) {
kfree(jdev_top->active_links);
return -ENOMEM;
}
jdev_top->staged_links = links;
return 0;
}
static int jesd204_dev_init_stop_states(struct jesd204_dev *jdev,
struct device_node *np)
{
unsigned int stop_states[JESD204_FSM_STATES_NUM];
int fsm_state;
int i, ret;
ret = of_property_read_variable_u32_array(np, "jesd204-stop-states",
stop_states,
1, JESD204_FSM_STATES_NUM);
if (ret <= 0)
return 0;
for (i = 0; i < ret; i++) {
if (stop_states[i] >= JESD204_FSM_STATES_NUM) {
pr_err("%pOF: Invalid state ID %u\n", np,
stop_states[i]);
return -EINVAL;
}
jdev->stop_states[stop_states[i]] = true;
fsm_state = stop_states[i] + JESD204_STATE_FSM_OFFSET;
pr_info("%pOF: stop state: '%s'\n", np,
jesd204_state_str(fsm_state));
}
return 0;
}
static struct jesd204_dev *jesd204_dev_alloc(struct device_node *np)
{
struct jesd204_dev_top *jdev_top;
struct jesd204_dev *jdev;
unsigned int link_ids[JESD204_MAX_LINKS];
u32 topo_id;
int i, ret, id;
id = ida_simple_get(&jesd204_ida, 0, 0, GFP_KERNEL);
if (id < 0) {
pr_err("%pOF: Unable to get unique ID for device\n", np);
return ERR_PTR(id);
}
if (of_property_read_u32(np, "jesd204-top-device", &topo_id) == 0) {
ret = of_property_read_variable_u32_array(np,
"jesd204-link-ids",
link_ids,
1,
JESD204_MAX_LINKS);
if (ret < 0) {
pr_err("%pOF error getting 'jesd204-link-ids': %d\n",
np, ret);
goto err_free_id;
}
jdev_top = kzalloc(sizeof(*jdev_top), GFP_KERNEL);
if (!jdev_top) {
ret = -ENOMEM;
goto err_free_id;
}
jdev = &jdev_top->jdev;
jdev_top->topo_id = topo_id;
jdev_top->num_links = ret;
for (i = 0; i < jdev_top->num_links; i++)
jdev_top->link_ids[i] = link_ids[i];
ret = jesd204_dev_alloc_links(jdev_top);
if (ret) {
kfree(jdev_top);
goto err_free_id;
}
jdev->is_top = true;
if (of_property_read_bool(np, "jesd204-ignore-errors"))
for (i = 0; i < jdev_top->num_links; i++)
jdev_top->active_links[i].fsm_ignore_errors
= true;
list_add(&jdev_top->entry, &jesd204_topologies);
jesd204_topologies_count++;
} else {
jdev = kzalloc(sizeof(*jdev), GFP_KERNEL);
if (!jdev) {
ret = -ENOMEM;
goto err_free_id;
}
}
jdev->is_sysref_provider = of_property_read_bool(np,
"jesd204-sysref-provider");
jdev->is_sec_sysref_provider = of_property_read_bool(np,
"jesd204-secondary-sysref-provider");
ret = jesd204_dev_init_stop_states(jdev, np);
if (ret)
goto err_free_id;
jdev->id = id;
jdev->np = of_node_get(np);
INIT_LIST_HEAD(&jdev->outputs);
list_add(&jdev->entry, &jesd204_device_list);
jesd204_device_count++;
return jdev;
err_free_id:
ida_simple_remove(&jesd204_ida, id);
return ERR_PTR(ret);
}
static struct jesd204_dev *jesd204_dev_find_by_of_node(struct device_node *np)
{
struct jesd204_dev *jdev = NULL, *jdev_it;
if (!np)
return NULL;
list_for_each_entry(jdev_it, &jesd204_device_list, entry) {
if (jdev_it->np == np) {
jdev = jdev_it;
break;
}
}
return jdev;
}
static struct jesd204_dev_con_out *jesd204_dev_find_output_con(
struct jesd204_dev *jdev,
struct of_phandle_args *args)
{
struct jesd204_dev_con_out *con;
unsigned int i;
/* find an existing output connection for the current of args */
list_for_each_entry(con, &jdev->outputs, entry) {
if (args->np != con->of.np)
continue;
if (args->args_count != con->of.args_count)
continue;
for (i = 0; i < args->args_count; i++) {
if (args->args[i] != con->of.args[i])
break;
}
if (i != args->args_count)
continue;
return con;
}
return NULL;
}
static int jesd204_dev_create_con(struct jesd204_dev *jdev,
struct of_phandle_args *args)
{
struct jesd204_dev_con_out *con;
struct jesd204_dev *jdev_in;
struct jesd204_dev_list_entry *e;
if (args->args_count < 2) {
pr_err("connection %pOF->%pOF requires 2 args minimum\n",
args->np, jdev->np);
return -EINVAL;
}
jdev_in = jesd204_dev_find_by_of_node(args->np);
if (!jdev_in) {
pr_err("connection %pOF->%pOF invalid\n", args->np, jdev->np);
return -ENOENT;
}
e = kzalloc(sizeof(*e), GFP_KERNEL);
if (!e)
return -ENOMEM;
con = jesd204_dev_find_output_con(jdev_in, args);
if (!con) {
con = kzalloc(sizeof(*con), GFP_KERNEL);
if (!con) {
kfree(e);
return -ENOMEM;
}
con->id = jesd204_con_id_counter++;
con->topo_id = args->args[0];
con->link_id = args->args[1];
con->link_idx = JESD204_LINKS_ALL;
con->owner = jdev_in;
INIT_LIST_HEAD(&con->dests);
memcpy(&con->of, args, sizeof(con->of));
list_add(&con->entry, &jdev_in->outputs);
jdev_in->outputs_count++;
}
pr_info("created con: id=%u, topo=%u, link=%u, %pOF <-> %pOF\n",
con->id, con->topo_id, con->link_id, con->owner->np, jdev->np);
e->jdev = jdev;
list_add(&e->entry, &con->dests);
con->dests_count++;
jdev->inputs[jdev->inputs_count] = con;
jdev->inputs_count++;
return 0;
}
static int jesd204_of_device_create_cons(struct jesd204_dev *jdev)
{
struct device_node *np = jdev->np;
struct of_phandle_args args;
int i, c, ret;
c = of_count_phandle_with_args(np, "jesd204-inputs", "#jesd204-cells");
if (c == -ENOENT || c == 0)
return 0;
if (c < 0)
return c;
jdev->inputs = kcalloc(c, sizeof(*jdev->inputs), GFP_KERNEL);
if (!jdev->inputs)
return -ENOMEM;
for (i = 0; i < c; i++) {
ret = of_parse_phandle_with_args(np,
"jesd204-inputs",
"#jesd204-cells",
i, &args);
/**
* If one bad/non-existing connection is found, then all
* JESD204 topologies won't be initialized. We may
* improve this later, to allow the good configs
*/
if (ret < 0)
return ret;
ret = jesd204_dev_create_con(jdev, &args);
of_node_put(args.np);
if (ret)
return ret;
}
return 0;
}
static int jesd204_of_create_devices(void)
{
struct jesd204_dev_top *jdev_top;
struct jesd204_dev *jdev;
struct device_node *np;
int ret;
for_each_node_with_property(np, "jesd204-device") {
jdev = jesd204_dev_alloc(np);
if (IS_ERR(jdev))
return PTR_ERR(jdev);
}
list_for_each_entry(jdev, &jesd204_device_list, entry) {
ret = jesd204_of_device_create_cons(jdev);
if (ret)
return ret;
}
list_for_each_entry(jdev_top, &jesd204_topologies, entry) {
ret = jesd204_init_topology(jdev_top);
if (ret)
return ret;
}
return 0;
}
static int jesd204_dev_init_link_lane_ids(struct jesd204_dev_top *jdev_top,
unsigned int link_idx,
struct jesd204_link *jlink)
{
struct jesd204_dev *jdev = &jdev_top->jdev;
u8 id;
if (!jlink->num_lanes) {
jesd204_err(jdev, "JESD204 link [%u] number of lanes is 0\n",
jlink->link_id);
jlink->lane_ids = NULL;
return -EINVAL;
}
/* We have some lane IDs provided statically for this link ID; exit */
if (jdev_top->init_links &&
jdev_top->init_links[link_idx].lane_ids)
return 0;
if (jlink->lane_ids)
kfree(jlink->lane_ids);
jlink->lane_ids = kmalloc_array(jlink->num_lanes,
sizeof(*jlink->lane_ids),
GFP_KERNEL);
if (!jlink->lane_ids)
return -ENOMEM;
/* Assign lane IDs, based on lane index */
for (id = 0; id < jlink->num_lanes; id++)
jlink->lane_ids[id] = id;
return 0;
}
static int __jesd204_dev_init_link_data(struct jesd204_dev_top *jdev_top,
unsigned int link_idx)
{
struct jesd204_link_opaque *ol;
int ret;
ol = &jdev_top->active_links[link_idx];
ol->link.link_id = jdev_top->link_ids[link_idx];
ol->jdev_top = jdev_top;
ol->link_idx = link_idx;
ol->link.sysref.lmfc_offset = JESD204_LMFC_OFFSET_UNINITIALIZED;
ret = jesd204_dev_init_link_lane_ids(jdev_top, link_idx, &ol->link);
if (ret)
return ret;
memcpy(&jdev_top->staged_links[link_idx], ol,
sizeof(jdev_top->staged_links[link_idx]));
return 0;
}
/* FIXME: see about maybe handling lane IDs assigned via the link_op for init links */
int jesd204_dev_init_link_data(struct jesd204_dev_top *jdev_top,
unsigned int link_idx)
{
int ret;