-
Notifications
You must be signed in to change notification settings - Fork 7
/
fclkcfg.c
1307 lines (1182 loc) · 39.8 KB
/
fclkcfg.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
/*********************************************************************************
*
* Copyright (C) 2016-2023 Ichiro Kawazome
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
********************************************************************************/
#include <linux/module.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/platform_device.h>
#include <linux/clk.h>
#include <linux/clk-provider.h>
#include <linux/slab.h>
#include <linux/of.h>
#include <linux/of_fdt.h>
#include <linux/stat.h>
#include <linux/limits.h>
#include <linux/types.h>
#include <linux/file.h>
#include <linux/firmware.h>
#include <linux/fs.h>
#include <linux/version.h>
/**
* DOC: fclkcfg constants
*/
MODULE_DESCRIPTION("FPGA Clock Configuration Driver");
MODULE_AUTHOR("ikwzm");
MODULE_LICENSE("Dual BSD/GPL");
#define DRIVER_VERSION "1.7.3"
#define DRIVER_NAME "fclkcfg"
#define DEVICE_MAX_NUM 32
#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3, 11, 0))
#define USE_DEV_GROUPS 1
#else
#define USE_DEV_GROUPS 0
#endif
/**
* DOC: fclkcfg static variables
*
* * info_enable - fclkcfg install/uninstall infomation enable.
* * debug_print - fclkcfg debug print enable.
*/
/**
* info_enable - fclkcfg install/uninstall infomation enable.
*/
static int info_enable = 1;
module_param( info_enable , int, S_IRUGO);
MODULE_PARM_DESC( info_enable , DRIVER_NAME " install/uninstall infomation enable");
/**
* debug_print - fclkcfg debug print enable.
*/
static int debug_print = 0;
module_param( debug_print , int, S_IRUGO);
MODULE_PARM_DESC( debug_print , DRIVER_NAME " debug print enable");
#define DEV_DBG(dev, fmt, ...) {\
if (debug_print){dev_info(dev, fmt, ##__VA_ARGS__);} \
else {dev_dbg (dev, fmt, ##__VA_ARGS__);} \
}
/**
* DOC: fclk state structure
*
* This section defines the structure of fclk state.
*
* * struct fclk_state - fclk state data structure.
* * of_get_fclk_state() - get rate or enable property from device tree.
*
*/
/**
* struct fclk_state - fclk state data structure.
*/
struct fclk_state {
unsigned long rate;
bool enable;
unsigned long resclk;
bool rate_valid;
bool enable_valid;
bool resclk_valid;
};
/**
* of_get_fclk_state() - get rate/enable/resource property from device tree.
*
* @dev: handle to the device structure.
* @of_node: handle to the device tree node.
* @rate_name: rate property name
* @enable_name: enable property name
* @resclk_name: resource clock property name
* @state: address of fclk state data.
* Return: Success(=0) or error status(<0).
*/
static void of_get_fclk_state(
struct device* dev,
struct device_node* of_node,
const char* rate_name,
const char* enable_name,
const char* resclk_name,
struct fclk_state* state)
{
DEV_DBG(dev, "get %s start.\n", rate_name);
{
const char* prop;
prop = of_get_property(of_node, rate_name, NULL);
if (!IS_ERR_OR_NULL(prop)) {
ssize_t prop_status;
unsigned long rate;
if (0 != (prop_status = kstrtoul(prop, 0, &rate))) {
dev_err(dev, "invalid %s.\n", rate_name);
} else {
state->rate_valid = true;
state->rate = rate;
DEV_DBG(dev, "get %s property (=%lu).\n", rate_name, rate);
}
}
}
DEV_DBG(dev, "get %s done.\n" , rate_name);
DEV_DBG(dev, "get %s start.\n", enable_name);
{
int retval;
unsigned int enable;
retval = of_property_read_u32(of_node, enable_name, &enable);
if (retval == 0) {
state->enable_valid = true;
state->enable = (enable != 0);
DEV_DBG(dev, "get %s property (=%d).\n", enable_name, enable);
}
}
DEV_DBG(dev, "get %s done.\n", enable_name);
DEV_DBG(dev, "get %s start.\n", resclk_name);
{
int retval;
unsigned int resclk;
retval = of_property_read_u32(of_node, resclk_name, &resclk);
if (retval == 0) {
state->resclk_valid = true;
state->resclk = resclk;
DEV_DBG(dev, "get %s property (=%d).\n", resclk_name, resclk);
}
}
DEV_DBG(dev, "get %s done.\n", resclk_name);
}
/**
* DOC: fclk device data structure
*
* This section defines the structure of fclk device data.
*
*/
/**
* struct fclk_device_data - fclk device data structure.
*/
struct fclk_device_data {
struct device* device;
struct clk* clk;
struct clk** resource_clks;
int resource_clks_size;
int resource_clk_id;
unsigned long round_rate;
struct fclk_state insert;
struct fclk_state remove;
dev_t device_number;
};
/**
* DOC: fclk device clock operations
*
* This section defines the clock operation.
*
* * __fclk_set_enable() - enable/disable clock.
* * __fclk_set_rate() - set clock rate.
* * __fclk_change_state() - change clock state.
* * __fclk_change_resource() - change resource clock.
*
*/
/**
* __fclk_set_enable() - enable/disable clock.
*
* @this: Pointer to the fclk device data.
* @enable: enable/disable value.
* Return: Success(=0) or error status(<0).
*
*/
static int __fclk_set_enable(struct fclk_device_data* this, bool enable)
{
int status = 0;
if (enable == true) {
if (__clk_is_enabled(this->clk) == false) {
status = clk_prepare_enable(this->clk);
if (status)
dev_err(this->device, "enable failed.");
else
DEV_DBG(this->device, "enable success.");
}
} else {
if (__clk_is_enabled(this->clk) == true) {
clk_disable_unprepare(this->clk);
DEV_DBG(this->device, "disable done.");
}
}
return status;
}
/**
* __fclk_set_rate() - set clock rate.
*
* @this: Pointer to the fclk device data.
* @rate: rate.
* Return: Success(=0) or error status(<0).
*
*/
static int __fclk_set_rate(struct fclk_device_data* this, unsigned long rate)
{
int status;
unsigned long round_rate;
round_rate = clk_round_rate(this->clk, rate);
status = clk_set_rate(this->clk, round_rate);
if (status)
dev_err(this->device, "set_rate(%lu=>%lu) failed." , rate, round_rate);
else
DEV_DBG(this->device, "set_rate(%lu=>%lu) success.", rate, round_rate);
return status;
}
/**
* __fclk_change_resource() - change resource clock.
*
* @this: Pointer to the fclk device data.
* @index: index of resource_clks.
* Return: Success(=0) or error status(<0).
*
*/
static int __fclk_change_resource(struct fclk_device_data* this, int index)
{
struct device* dev = this-> device;
if ((this->resource_clks == NULL) && index == 0) {
this->resource_clk_id = index;
return 0;
}
if ((this->resource_clks != NULL) && (index >= 0) && (index < this->resource_clks_size)) {
struct clk* resource_clk = this->resource_clks[index];
int set_parent_status = 0;
bool found_resource_clk = false;
struct clk* curr_clk = this->clk;
while (!IS_ERR_OR_NULL(curr_clk)) {
if (clk_has_parent(curr_clk, resource_clk) == true) {
found_resource_clk = true;
set_parent_status = clk_set_parent(curr_clk, resource_clk);
break;
}
curr_clk = clk_get_parent(curr_clk);
}
if (set_parent_status != 0) {
dev_err(dev, "clk_set_parent(%s, %s) failed.\n" , __clk_get_name(curr_clk), __clk_get_name(resource_clk));
return set_parent_status;
}
if (found_resource_clk == false) {
dev_err(dev, "%s is not resource clock of %s.\n", __clk_get_name(resource_clk), __clk_get_name(this->clk));
return -EINVAL;
}
this->resource_clk_id = index;
return 0;
}
return -EINVAL;
}
/**
* __fclk_change_state() - change clock state.
*
* @this: Pointer to the fclk device data.
* @next: next state to change.
* Return: Success(=0) or error status(<0).
*
*/
static int __fclk_change_state(struct fclk_device_data* this, struct fclk_state* next)
{
int retval = 0;
bool prev_enable = __clk_is_enabled(this->clk);
bool next_enable = (next->enable_valid == true) ? next->enable : prev_enable;
bool next_resclk = ((next->resclk_valid == true) &&
(next->resclk != this->resource_clk_id));
if (((next->rate_valid == true) || (next_resclk == true)) && (prev_enable == true)) {
if (0 != (retval = __fclk_set_enable(this, false)))
return retval;
prev_enable = false;
}
if (next_resclk == true) {
if (0 != (retval = __fclk_change_resource(this, next->resclk)))
return retval;
}
if (next->rate_valid == true) {
if (0 != (retval = __fclk_set_rate(this, next->rate)))
return retval;
}
if (prev_enable != next_enable) {
if (0 != (retval = __fclk_set_enable(this, next_enable)))
return retval;
}
return retval;
}
/**
* DOC: fclk system class device file show/set operations.
*
* The device file created in system class is as follows.
*
* * /sys/class/<class-name>/<device-name>/driver_version
* * /sys/class/<class-name>/<device-name>/enable
* * /sys/class/<class-name>/<device-name>/rate
* * /sys/class/<class-name>/<device-name>/round_rate
* * /sys/class/<class-name>/<device-name>/resource_clks
* * /sys/class/<class-name>/<device-name>/resource
* * /sys/class/<class-name>/<device-name>/remove_enable
* * /sys/class/<class-name>/<device-name>/remove_rate
* * /sys/class/<class-name>/<device-name>/remove_resource
*/
/**
* fclk_show_driver_version()
*/
static ssize_t fclk_show_driver_version(struct fclk_device_data* this, struct device_attribute *attr, char *buf)
{
if (!this)
return -ENODEV;
return sprintf(buf, "%s\n", DRIVER_VERSION);
}
/**
* fclk_show_enable()
*/
static ssize_t fclk_show_enable(struct fclk_device_data* this, struct device_attribute *attr, char *buf)
{
if (!this)
return -ENODEV;
return sprintf(buf, "%d\n", __clk_is_enabled(this->clk));
}
/**
* fclk_set_enable()
*/
static ssize_t fclk_set_enable(struct fclk_device_data* this, struct device_attribute *attr, const char *buf, size_t size)
{
ssize_t get_result;
int set_result;
unsigned long enable;
if (!this)
return -ENODEV;
if (0 != (get_result = kstrtoul(buf, 0, &enable)))
return get_result;
if (0 != (set_result = __fclk_set_enable(this, (enable != 0))))
return (ssize_t)set_result;
return size;
}
/**
* fclk_show_rate()
*/
static ssize_t fclk_show_rate(struct fclk_device_data* this, struct device_attribute *attr, char *buf)
{
if (!this)
return -ENODEV;
return sprintf(buf, "%lu\n", clk_get_rate(this->clk));
}
/**
* fclk_set_rate()
*/
static ssize_t fclk_set_rate(struct fclk_device_data* this, struct device_attribute *attr, const char *buf, size_t size)
{
ssize_t get_result;
int set_result;
struct fclk_state next_state;
if (!this)
return -ENODEV;
if (0 != (get_result = kstrtoul(buf, 0, &next_state.rate)))
return get_result;
next_state.rate_valid = true;
next_state.enable = false;
next_state.enable_valid = false;
next_state.resclk = 0;
next_state.resclk_valid = false;
if (0 != (set_result = __fclk_change_state(this, &next_state)))
return (ssize_t)set_result;
return size;
}
/**
* fclk_show_round_rate()
*/
static ssize_t fclk_show_round_rate(struct fclk_device_data* this, struct device_attribute *attr, char *buf)
{
if (!this)
return -ENODEV;
return sprintf(buf, "%lu => %lu\n",
this->round_rate,
clk_round_rate(this->clk, this->round_rate)
);
}
/**
* fclk_set_round_rate()
*/
static ssize_t fclk_set_round_rate(struct fclk_device_data* this, struct device_attribute *attr, const char *buf, size_t size)
{
ssize_t get_result;
unsigned long round_rate;
if (!this)
return -ENODEV;
if (0 != (get_result = kstrtoul(buf, 0, &round_rate)))
return get_result;
this->round_rate = round_rate;
return size;
}
/**
* fclk_set_resource()
*/
static ssize_t fclk_set_resource(struct fclk_device_data* this, struct device_attribute *attr, const char *buf, size_t size)
{
ssize_t get_result;
int set_result;
unsigned long resclk;
struct fclk_state next_state;
if (!this)
return -ENODEV;
if (this->resource_clks == NULL)
return size;
if (0 != (get_result = kstrtoul(buf, 0, &resclk)))
return get_result;
if (resclk >= this->resource_clks_size)
return -EINVAL;
next_state.rate = 0;
next_state.rate_valid = false;
next_state.enable = false;
next_state.enable_valid = false;
next_state.resclk = resclk;
next_state.resclk_valid = true;
if (0 != (set_result = __fclk_change_state(this, &next_state)))
return (ssize_t)set_result;
return size;
}
/**
* fclk_show_resource()
*/
static ssize_t fclk_show_resource(struct fclk_device_data* this, struct device_attribute *attr, char *buf)
{
if (!this)
return -ENODEV;
return sprintf(buf, "%d\n", this->resource_clk_id);
}
/**
* fclk_show_resource_clks()
*/
static ssize_t fclk_show_resource_clks(struct fclk_device_data* this, struct device_attribute *attr, char *buf)
{
size_t size = 0;
if (!this)
return -ENODEV;
if ((this->resource_clks != NULL) && (this->resource_clks_size > 0)) {
int i;
char* ptr = buf;
for (i = 0; i < this->resource_clks_size; i++) {
struct clk* resource_clk = this->resource_clks[i];
size_t len;
if (i == this->resource_clks_size-1)
len = sprintf(ptr, "%s\n", __clk_get_name(resource_clk));
else
len = sprintf(ptr, "%s, ", __clk_get_name(resource_clk));
ptr += len;
size += len;
}
}
return size;
}
/**
* DEF_FCLK_STATE_SHOW_ENABLE() - generate fclk_show_ ## state ## _enable() macro
*/
#define DEF_FCLK_STATE_SHOW_ENABLE(state) \
static ssize_t fclk_show_ ## state ## _enable( \
struct fclk_device_data* this, \
struct device_attribute* attr, \
char* buf) \
{ \
if (!this) return -ENODEV; \
if (this->state.enable_valid == false) {return sprintf(buf, "%d\n", -1);} \
if (this->state.enable == true ) {return sprintf(buf, "%d\n", 1);} \
else {return sprintf(buf, "%d\n", 0);} \
}
/**
* DEF_FCLK_STATE_SET_ENABLE() - generate fclk_set_ ## state ## _enable() macro
*/
#define DEF_FCLK_STATE_SET_ENABLE(state) \
static ssize_t fclk_set_ ## state ## _enable( \
struct fclk_device_data* this, \
struct device_attribute* attr, \
const char* buf, \
size_t size) \
{ \
ssize_t get_result; \
long enable; \
if (!this) return -ENODEV; \
if (0 != (get_result = kstrtol(buf, 0, &enable))) \
return get_result; \
if (enable > 0) {this->state.enable_valid = true ;this->state.enable = true ;} \
else if (enable == 0) {this->state.enable_valid = true ;this->state.enable = false;} \
else {this->state.enable_valid = false;} \
return size; \
}
/**
* DEF_FCLK_STATE_SHOW_RATE() - generate fclk_show_ ## state ## _rate() macro
*/
#define DEF_FCLK_STATE_SHOW_RATE(state) \
static ssize_t fclk_show_ ## state ## _rate( \
struct fclk_device_data* this, \
struct device_attribute* attr, \
char* buf) \
{ \
if (!this) return -ENODEV; \
if (this->state.rate_valid == false) {return sprintf(buf, "%d\n" , -1);} \
else {return sprintf(buf, "%lu\n", this->state.rate);} \
}
/**
* DEF_FCLK_STATE_SET_RATE() - generate fclk_set_## state ## _rate() macro
*/
#define DEF_FCLK_STATE_SET_RATE(state) \
static ssize_t fclk_set_ ## state ## _rate( \
struct fclk_device_data* this, \
struct device_attribute* attr, \
const char* buf, \
size_t size) \
{ \
ssize_t get_result; \
long rate; \
if (!this) return -ENODEV; \
if (0 != (get_result = kstrtol(buf, 0, &rate))) \
return get_result; \
if (rate >= 0) {this->state.rate_valid = true ;this->state.rate = (unsigned long)rate;} \
else {this->state.rate_valid = false;} \
return size; \
}
/**
* DEF_FCLK_STATE_SHOW_RESOURCE() - generate fclk_show_ ## state ## _resource() macro
*/
#define DEF_FCLK_STATE_SHOW_RESOURCE(state) \
static ssize_t fclk_show_ ## state ## _resource( \
struct fclk_device_data* this, \
struct device_attribute* attr, \
char* buf) \
{ \
if (!this) return -ENODEV; \
if (this->state.resclk_valid == false) {return sprintf(buf, "%d\n" , -1);} \
else {return sprintf(buf, "%lu\n", this->state.resclk);} \
}
/**
* DEF_FCLK_STATE_SET_RESOURCE() - generate fclk_set_ ## state ## _resource() macro
*/
#define DEF_FCLK_STATE_SET_RESOURCE(state) \
static ssize_t fclk_set_ ## state ## _resource( \
struct fclk_device_data* this, \
struct device_attribute* attr, \
const char* buf, \
size_t size) \
{ \
ssize_t get_result; \
long resource; \
if (!this) return -ENODEV; \
if ((this->resource_clks != NULL) && (this->resource_clks_size > 0)) { \
if (0 != (get_result = kstrtol(buf, 0, &resource))) \
return get_result; \
if ((resource >= 0) && (resource < this->resource_clks_size)) { \
this->state.resclk_valid = true; \
this->state.resclk = (unsigned long)resource; \
return size; \
} \
if (resource < 0) { \
this->state.resclk_valid = false; \
return size; \
} \
return -EINVAL; \
} \
return size;\
}
/**
* fclk_show_remove_enable()
* fclk_show_remove_rate()
* fclk_show_remove_resource()
* fclk_set_remove_enable()
* fclk_set_remove_rate()
* fclk_set_remove_resource()
*/
DEF_FCLK_STATE_SHOW_ENABLE (remove);
DEF_FCLK_STATE_SHOW_RATE (remove);
DEF_FCLK_STATE_SHOW_RESOURCE(remove);
DEF_FCLK_STATE_SET_ENABLE (remove);
DEF_FCLK_STATE_SET_RATE (remove);
DEF_FCLK_STATE_SET_RESOURCE (remove);
/**
* DOC: fclk device data operations
*
* This section defines the operation of fclk device data.
*
* * fclk_device_info() - Print infomation the fclk device data.
* * fclk_device_setup() - Set up the fclk device data.
* * fclk_device_cleanup() - Clean up the fclk device data.
*/
/**
* fclk_device_info() - Print infomation the fclk device data.
*
* @this: Pointer to the fclk device data.
* @pdev: handle to the platform device structure or NULL.
*
*/
static void fclk_device_info(struct fclk_device_data* this, struct platform_device* pdev)
{
struct device* dev = (pdev != NULL)? &pdev->dev : this->device;
#define RES_INFO(dev, tag, resclk) \
if (this->resource_clks != NULL) { \
if (((resclk) >= 0) && ((resclk) < this->resource_clks_size)) \
dev_info(dev, tag "%s\n", __clk_get_name(this->resource_clks[(resclk)]));\
else \
dev_info(dev, tag "%d\n", (int)(resclk)); \
}
dev_info(dev, "driver version : %s\n" , DRIVER_VERSION);
dev_info(dev, "device name : %s\n" , dev_name(this->device));
dev_info(dev, "clock name : %s\n" , __clk_get_name(this->clk));
dev_info(dev, "clock rate : %lu\n", clk_get_rate(this->clk));
dev_info(dev, "clock enabled : %d\n" , __clk_is_enabled(this->clk));
RES_INFO(dev, "resource clock : " , this->resource_clk_id);
if ((this->resource_clks != NULL) && (this->resource_clks_size > 1)) {
int i;
for (i = 0; i < this->resource_clks_size; i++) {
struct clk* resource_clk = this->resource_clks[i];
dev_info(dev, "resource clocks: %d => %s" , i, __clk_get_name(resource_clk));
}
}
{
if (this->remove.rate_valid == true)
dev_info(dev, "remove rate : %lu\n", this->remove.rate );
if (this->remove.enable_valid == true)
dev_info(dev, "remove enable : %d\n" , this->remove.enable);
if (this->remove.resclk_valid == true)
RES_INFO(dev, "remove resource: " , this->remove.resclk);
}
}
/**
* fclk_device_get_state_property() - get rate/enable/resource property from device.
*
* @this: Pointer to the fclk device data.
* @dev: handle to the device structure.
* @of_node: handle to the of node.
* @rate_name: rate property name
* @enable_name: enable property name
* @resclk_name: resource clock property name
* @state: address of fclk state data.
* Return: Success(=0) or error status(<0).
*
*/
static int fclk_device_get_state_property(
struct fclk_device_data* this ,
struct device* dev ,
struct device_node* of_node ,
const char* rate_name ,
const char* enable_name,
const char* resclk_name,
struct fclk_state* state )
{
of_get_fclk_state(dev, of_node, rate_name, enable_name, resclk_name, state);
if ((this->resource_clks != NULL) &&
(state->resclk_valid == true) &&
(state->resclk >= this->resource_clks_size)) {
dev_err(dev, "invalid %s(=%lu).\n", resclk_name, state->resclk);
return -EINVAL;
}
return 0;
}
/**
* fclk_device_setup() - Set up the fclk device data.
*
* @this: Pointer to the fclk device data.
* @dev: handle to the device structure.
* Return: Success(=0) or error status(<0).
*
*/
static int fclk_device_setup(struct fclk_device_data* this, struct device *dev)
{
int retval = 0;
/*
* get clk
*/
DEV_DBG(dev, "of_clk_get(0) start.\n");
{
this->clk = of_clk_get(dev->of_node, 0);
if (IS_ERR_OR_NULL(this->clk)) {
dev_err(dev, "of_clk_get(0) failed.\n");
retval = PTR_ERR(this->clk);
this->clk = NULL;
goto failed;
}
}
DEV_DBG(dev, "of_clk_get(0) done.\n");
/*
* get resource clock list
*/
DEV_DBG(dev, "of_clk_get(1..) start.\n");
{
int clk_index;
struct clk* clk_list[32];
for (clk_index = 0; clk_index < sizeof(clk_list); clk_index++) {
struct clk* resource_clk = of_clk_get(dev->of_node, clk_index+1);
if (IS_ERR_OR_NULL(resource_clk))
break;
clk_list[clk_index] = resource_clk;
}
this->resource_clks_size = clk_index;
if (this->resource_clks_size > 0) {
this->resource_clks = kzalloc(this->resource_clks_size*sizeof(struct clk*), GFP_KERNEL);
if (IS_ERR_OR_NULL(this->resource_clks)) {
dev_err(dev, "create resource_clks falied.\n");
for (clk_index = 0; clk_index < this->resource_clks_size; clk_index++) {
clk_put(clk_list[clk_index]);
}
retval = PTR_ERR(this->resource_clks);
this->resource_clks = NULL;
this->resource_clks_size = 0;
this->resource_clk_id = 0;
goto failed;
} else {
for (clk_index = 0; clk_index < this->resource_clks_size; clk_index++) {
this->resource_clks[clk_index] = clk_list[clk_index];
}
this->resource_clk_id = -1; /* Uninitialized resclk flag */
}
} else {
this->resource_clks = NULL;
this->resource_clks_size = 0;
this->resource_clk_id = 0;
}
}
DEV_DBG(dev, "of_clk_get(1..) done.\n");
/*
* get insert state
*/
if (this->resource_clks != NULL) {
this->insert.resclk_valid = true;
this->insert.resclk = 0;
}
retval = fclk_device_get_state_property(
this, dev, dev->of_node,
"insert-rate", "insert-enable", "insert-resource", &this->insert
);
if (retval)
goto failed;
/*
* change state to insert
*/
retval = __fclk_change_state(this, &this->insert);
if (retval) {
dev_err(dev, "fclk change state failed(%d).\n", retval);
goto failed;
}
this->insert.enable = __clk_is_enabled(this->clk);
this->insert.rate = clk_get_rate(this->clk);
this->insert.resclk = this->resource_clk_id;
/*
* get remove state
*/
retval = fclk_device_get_state_property(
this, dev, dev->of_node,
"remove-rate", "remove-enable", "remove-resource", &this->remove
);
if (retval)
goto failed;
return 0;
failed:
return retval;
}
/**
* fclk_device_cleanup() - Clean up the fclk device data.
*
* @this: Pointer to the fclk device data.
* Return: Success(=0) or error status(<0).
*
*/
static int fclk_device_cleanup(struct fclk_device_data* this)
{
if (!this)
return -ENODEV;
if (this->clk) {
clk_put(this->clk);
this->clk = NULL;
}
if (this->resource_clks != NULL) {
int i;
for (i = 0 ; i < this->resource_clks_size ; i++) {
struct clk* resource_clk = this->resource_clks[i];
clk_put(resource_clk);
}
kfree(this->resource_clks);
this->resource_clks = NULL;
}
this->resource_clks_size = 0;
this->resource_clk_id = 0;
return 0;
}
/**
* DOC: fclkcfg device operations
*
* This section defines the operation of fclkcfg device.
*
* * fclkcfg_sys_class - fclkcfg system class.
* * fclkcfg_device_number - fclkcfg device major number.
* * fclkcfg_device_ida - fclkcfg device minor number allocator variable.
* * fclkcfg_device_attrs - fclkcfg device attribute table.
* * fclkcfg_attr_group - fclkcfg device attribute group.
* * fclkcfg_attr_groups - fclkcfg device attribute group table.
* * fclkcfg_device_create() - Create fclkcfg device.
* * fclkcfg_device_destroy() - Destroy fclkcfg device.
* * fclkcfg_device_attrs -
*/
static struct class* fclkcfg_sys_class = NULL;
static dev_t fclkcfg_device_number = 0;
static DEFINE_IDA( fclkcfg_device_ida );
/**
* DEF_FCLKCFG_SHOW() - generate fclkcfg_show_ ## __attr_name() macro
*/
#define DEF_FCLKCFG_SHOW(__attr_name) \
static ssize_t fclkcfg_show_ ## __attr_name( \
struct device* dev, \
struct device_attribute *attr, \
char *buf) \
{ return fclk_show_ ## __attr_name(dev_get_drvdata(dev), attr, buf);}
/**
* DEF_FCLKCFG_SET() - generate fclkcfg_set_ ## __attr_name() macro
*/
#define DEF_FCLKCFG_SET(__attr_name) \
static ssize_t fclkcfg_set_ ## __attr_name( \
struct device* dev, \
struct device_attribute *attr, \
const char *buf, \
size_t size) \
{ return fclk_set_ ## __attr_name(dev_get_drvdata(dev), attr, buf, size);}
/**
* fclkcfg_show_driver_version()
*/
DEF_FCLKCFG_SHOW(driver_version);
/**
* fclkcfg_show_enable()
* fclkcfg_set_enable()
*/
DEF_FCLKCFG_SHOW(enable);
DEF_FCLKCFG_SET (enable);
/**
* fclkcfg_show_rate()
* fclkcfg_set_rate()
*/
DEF_FCLKCFG_SHOW(rate);
DEF_FCLKCFG_SET (rate);
/**
* fclkcfg_show_round_rate()
* fclkcfg_set_round_rate()
*/
DEF_FCLKCFG_SHOW(round_rate);
DEF_FCLKCFG_SET (round_rate);
/**
* fclkcfg_show_resource()
* fclkcfg_set_resource()
*/
DEF_FCLKCFG_SHOW(resource);
DEF_FCLKCFG_SET (resource);
/**
* fclkcfg_show_resource_clks()
*/
DEF_FCLKCFG_SHOW(resource_clks);
/**
* fclkcfg_show_remove_enable()
* fclkcfg_set_remove_enable()
*/
DEF_FCLKCFG_SHOW(remove_enable);
DEF_FCLKCFG_SET (remove_enable);
/**
* fclkcfg_show_remove_rate()
* fclkcfg_set_remove_rate()
*/
DEF_FCLKCFG_SHOW(remove_rate);
DEF_FCLKCFG_SET (remove_rate);
/**
* fclkcfg_show_remove_resource()
* fclkcfg_set_remove_resource()
*/
DEF_FCLKCFG_SHOW(remove_resource);
DEF_FCLKCFG_SET (remove_resource);
static struct device_attribute fclkcfg_device_attrs[] = {
__ATTR(driver_version , 0444, fclkcfg_show_driver_version , NULL ),
__ATTR(enable , 0664, fclkcfg_show_enable , fclkcfg_set_enable ),
__ATTR(rate , 0664, fclkcfg_show_rate , fclkcfg_set_rate ),
__ATTR(round_rate , 0664, fclkcfg_show_round_rate , fclkcfg_set_round_rate ),
__ATTR(resource , 0664, fclkcfg_show_resource , fclkcfg_set_resource ),
__ATTR(resource_clks , 0444, fclkcfg_show_resource_clks , NULL ),
__ATTR(remove_enable , 0664, fclkcfg_show_remove_enable , fclkcfg_set_remove_enable ),
__ATTR(remove_rate , 0664, fclkcfg_show_remove_rate , fclkcfg_set_remove_rate ),
__ATTR(remove_resource, 0664, fclkcfg_show_remove_resource, fclkcfg_set_remove_resource),
__ATTR_NULL,
};
#if (USE_DEV_GROUPS == 1)