-
Notifications
You must be signed in to change notification settings - Fork 0
/
radio-kt0913.c
1181 lines (984 loc) · 33.7 KB
/
radio-kt0913.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-only
/*
* drivers/media/radio/radio-kt0913.c
*
* Driver for the KT0913 radio chip from KTMicro.
* This driver provides a v4l2 interface to the tuner, using the I2C
* protocol to communicate with the chip.
* It exposes two bands, one for AM and another for FM. If the "campus
* band" feature needs to be enabled, set the corresponding module parameter
* to 1.
* Reference Clock and Audio DAC anti-pop configurations should be
* set via a device tree node. Defaults will be used otherwise.
*
* Audio output should be routed to a speaker or an audio capture
* device.
*
* Based on radio-tea5764 by Fabio Belavenuto <belavenuto@gmail.com>
*
* Copyright (c) 2020 Santiago Hormazabal <santiagohssl@gmail.com>
*
* TODO:
* use rd and wr support for the regmap instead of volatile regs.
* add support for the hardware-assisted frequency seek.
* export FM SNR and AM/FM AFC deviation values as RO controls.
*/
#include <linux/module.h>
#include <linux/i2c.h>
#include <linux/err.h>
#include <linux/pm_runtime.h>
#include <linux/of.h>
#include <linux/math64.h>
#include <linux/regmap.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#include <media/v4l2-event.h>
/* ************************************************************************* */
/* registers of the kt0913 */
#define KT0913_REG_CHIP_ID 0x01
#define KT0913_REG_SEEK 0x02
#define KT0913_REG_TUNE 0x03
#define KT0913_REG_VOLUME 0x04
#define KT0913_REG_DSPCFGA 0x05
#define KT0913_REG_LOCFGA 0x0A
#define KT0913_REG_LOCFGC 0x0C
#define KT0913_REG_RXCFG 0x0F
#define KT0913_REG_STATUSA 0x12
#define KT0913_REG_STATUSB 0x13
#define KT0913_REG_STATUSC 0x14
#define KT0913_REG_AMSYSCFG 0x16
#define KT0913_REG_AMCHAN 0x17
#define KT0913_REG_AMCALI 0x18
#define KT0913_REG_GPIOCFG 0x1D
#define KT0913_REG_AMDSP 0x22
#define KT0913_REG_AMSTATUSA 0x24
#define KT0913_REG_AMSTATUSB 0x25
#define KT0913_REG_SOFTMUTE 0x2E
#define KT0913_REG_AMCFG 0x33
#define KT0913_REG_AMCFG2 0x34
#define KT0913_REG_AFC 0x3C
/* register symbols masks, values and shift count */
#define KT0913_TUNE_FMTUNE_MASK 0x8000 /* FM Tune enable */
#define KT0913_TUNE_FMTUNE_ON 0x8000 /* FM Tune enabled */
#define KT0913_TUNE_FMTUNE_OFF 0x0000 /* FM Tune disabled */
#define KT0913_TUNE_FMCHAN_MASK 0x0FFF /* frequency in kHz / 50kHz */
#define KT0913_VOLUME_DMUTE_MASK 0x2000
#define KT0913_VOLUME_DMUTE_ON 0x0000
#define KT0913_VOLUME_DMUTE_OFF 0x2000
#define KT0913_VOLUME_DE_MASK 0x0800 /* de-emphasis time constant */
#define KT0913_VOLUME_DE_75US 0x0000 /* 75us */
#define KT0913_VOLUME_DE_50US 0x0800 /* 50us */
#define KT0913_VOLUME_POP_MASK 0x30 /* audio dac anti-pop config */
#define KT0913_VOLUME_POP_SHIFT 4
#define KT0913_DSPCFGA_MONO_MASK 0x8000 /* mono select (0=stereo, 1=mono) */
#define KT0913_DSPCFGA_MONO_ON 0x8000 /* mono */
#define KT0913_DSPCFGA_MONO_OFF 0x0000 /* stereo */
#define KT0913_LOCFG_CAMPUSBAND_EN_MASK 0x0008 /* campus band fm enable */
#define KT0913_LOCFG_CAMPUSBAND_EN_ON 0x0008 /* FM range 64-110MHz */
#define KT0913_LOCFG_CAMPUSBAND_EN_OFF 0x0000 /* FM range 32-110MHz */
#define KT0913_RXCFGA_STDBY_MASK 0x1000 /* standby mode enable */
#define KT0913_RXCFGA_STDBY_ON 0x1000 /* standby mode enabled */
#define KT0913_RXCFGA_STDBY_OFF 0x0000 /* standby mode disabled */
#define KT0913_RXCFGA_VOLUME_MASK 0x001F /* volume control */
#define KT0913_STATUSA_XTAL_OK 0x8000 /* crystal ready indicator */
#define KT0913_STATUSA_STC 0x4000 /* seek/tune complete */
#define KT0913_STATUSA_PLL_LOCK_MASK 0x800 /* system pll ready indicator */
#define KT0913_STATUSA_PLL_LOCK_LOCKED 0x800 /* system pll ready */
#define KT0913_STATUSA_PLL_LOCK_UNLOCKED 0x000 /* not ready */
#define KT0913_STATUSA_LO_LOCK 0x400 /* LO synthesizer ready indicator */
#define KT0913_STATUSA_ST_MASK 0x300 /* stereo indicator (0x300=stereo, otherwise mono) */
#define KT0913_STATUSA_ST_STEREO 0x300 /* stereo */
#define KT0913_STATUSA_FMRSSI_MASK 0xF8 /* FM RSSI (-100dBm + FMRSSI*3dBm) */
#define KT0913_STATUSA_FMRSSI_SHIFT 3
#define KT0913_STATUSC_PWSTATUS 0x8000 /* power status indicator */
#define KT0913_STATUSC_CHIPRDY 0x2000 /* chip ready indicator */
#define KT0913_STATUSC_FMSNR 0x1FC0 /* FM SNR (unknown units) */
#define KT0913_AMCHAN_AMTUNE_MASK 0x8000 /* AM tune enable */
#define KT0913_AMCHAN_AMTUNE_ON 0x8000 /* AM tune enabled */
#define KT0913_AMCHAN_AMTUNE_OFF 0x0000 /* AM tune disabled */
#define KT0913_AMCHAN_AMCHAN_MASK 0x7FF /* am channel in kHz */
#define KT0913_AMSYSCFG_AM_FM_MASK 0x8000 /* am/fm mode control */
#define KT0913_AMSYSCFG_AM_FM_AM 0x8000 /* am mode */
#define KT0913_AMSYSCFG_AM_FM_FM 0x0000 /* fm mode (default) */
#define KT0913_AMSYSCFG_REFCLK_MASK 0x0F00 /* reference clock selection */
#define KT0913_AMSYSCFG_REFCLK_SHIFT 8
#define KT0913_AMSYSCFG_AU_GAIN_MASK 0x00C0 /* audio gain selection */
#define KT0913_AMSYSCFG_AU_GAIN_6DB 0x0040 /* 6dB audio gain */
#define KT0913_AMSYSCFG_AU_GAIN_3DB 0x0000 /* 3dB audio gain (default) */
#define KT0913_AMSYSCFG_AU_GAIN_0DB 0x00C0 /* 0dB audio gain */
#define KT0913_AMSYSCFG_AU_GAIN_MIN_3DB 0x0080 /* -3dB audio gain */
#define KT0913_AMSTATUSA_AMRSSI_MASK 0x1F00 /* am channel rssi */
#define KT0913_AMSTATUSA_AMRSSI_SHIFT 8
/* constants */
#define KT0913_CHIP_ID 0x544B /* ASCII of 'KT' */
#define V4L2_KHZ_FREQ_MUL 16U /* v4l2 uses 16x the kHz value as their freq */
#define KT0913_FMCHAN_MUL 50U /* kt0913 uses freqs with a 50kHz multiplier */
#define KT0913_FM_RANGE_LOW_NO_CAMPUS 64000U /* 64MHz lower bound for FM */
#define KT0913_FM_RANGE_LOW_CAMPUS 32000U /* 32MHz lower bound for campus FM */
#define KT0913_FM_RANGE_HIGH 110000U /* 110MHz upper bound for FM */
#define KT0913_AM_RANGE_LOW 500U /* 500kHz lower bound for AM */
#define KT0913_AM_RANGE_HIGH 1710U /* 1710kHz upper bound for AM */
#define KT0913_FM_AM_DRIVER_NAME "kt0913-fm-am"
/* ************************************************************************* */
/* v4l2 device number to use. -1 will assign the next free one */
static int kt0913_v4l2_radio_nr = -1;
/* use the extended range of FM down to 32MHz. disabled by default */
static int kt0913_use_campus_band;
/* ************************************************************************* */
/* kt0913 status struct */
struct kt0913_device {
struct v4l2_device v4l2_dev; /* main v4l2 struct */
struct i2c_client *client; /* I2C client */
struct video_device vdev; /* vide_device struct */
struct v4l2_ctrl_handler ctrl_handler; /* ctrl_handler struct */
/* V4L2 Controls */
struct v4l2_ctrl *ctrl_pll_lock; /* PLL lock */
struct v4l2_ctrl *ctrl_volume; /* Overall volume */
struct v4l2_ctrl *ctrl_au_gain; /* Audio Gain */
struct v4l2_ctrl *ctrl_mute; /* Master mute */
struct v4l2_ctrl *ctrl_deemphasis; /* Deemphasis */
/* current operation band (fm, fm_campus, am) */
unsigned int band;
/* audio dac anti-pop setting:
* 0 -> 100uF (default)
* 1 -> 60uF
* 2 -> 20uF
* 3 -> 10uF
*/
unsigned int audio_anti_pop;
/*
* reference clock selection:
* 0 -> 32.768kHz (default)
* 1 -> 6.5MHz
* 2 -> 7.6MHz
* 3 -> 12MHz
* 4 -> 13MHz
* 5 -> 15.2MHz
* 6 -> 19.2MHz
* 7 -> 24MHz
* 8 -> 26MHz
* 9 -> 38kHz
*/
unsigned int refclock_val;
/* Regmap */
struct regmap *regmap;
/* For core assisted locking */
struct mutex mutex;
};
/* ************************************************************************* */
/* Regmap settings */
static const struct regmap_range kt0913_regmap_all_registers_range[] = {
regmap_reg_range(0x01, 0x05),
regmap_reg_range(0x0A, 0x0A),
regmap_reg_range(0x0C, 0x0C),
regmap_reg_range(0x0F, 0x0F),
regmap_reg_range(0x12, 0x14),
regmap_reg_range(0x16, 0x18),
regmap_reg_range(0x1D, 0x1D),
regmap_reg_range(0x22, 0x22),
regmap_reg_range(0x24, 0x25),
regmap_reg_range(0x2E, 0x2F),
regmap_reg_range(0x30, 0x34),
regmap_reg_range(0x3A, 0x3A),
regmap_reg_range(0x3C, 0x3C),
};
static const struct regmap_access_table kt0913_all_registers_access_table = {
.yes_ranges = kt0913_regmap_all_registers_range,
.n_yes_ranges = ARRAY_SIZE(kt0913_regmap_all_registers_range),
};
static const struct reg_sequence kt0913_init_regs_to_defaults[] = {
/* Standby disabled, volume 0dB */
{ KT0913_REG_RXCFG, 0x881F },
/* FM Channel spacing = 50kHz, Right & Left unmuted */
{ KT0913_REG_SEEK, 0x000B },
/* Stereo, High Stereo/Mono blend level, blend disabled */
{ KT0913_REG_DSPCFGA, 0x1000 },
/* FM AFC Enabled */
{ KT0913_REG_LOCFGA, 0x0100 },
/* Campus band disabled by default */
{ KT0913_REG_LOCFGC, 0x0024 },
/*
* FM mode, internal defined bands, clock from XT, 32.768kHz
* 3dB audio gain, AM AFC Enabled
*/
{ KT0913_REG_AMSYSCFG, 0x0002 },
/* Default AM freq = 504kHz */
{ KT0913_REG_AMCHAN, 0x01F8},
/* VOL and CH GPIOs set to HiZ */
{ KT0913_REG_GPIOCFG, 0x0000 },
/* AM Channel bandwidth = 6kHz, non-differential output */
{ KT0913_REG_AMDSP, 0xAFC4 },
/*
* softmute is disabled on AM and FM, but set the defaults:
* strong softmute attn., slow softmute attack/recover,
* lowest AM softumte start level, almost the minimum
* softmute target volume, RSSI mode for softmute, lowest
* FM softmute start level
*/
{ KT0913_REG_SOFTMUTE, 0x0010 },
/* 1kHz for AM channel space, working mode A for the keys */
{ KT0913_REG_AMCFG, 0x1401 },
/* TIME1 = shortest, TIME2 = fastest */
{ KT0913_REG_AMCFG, 0x4050 },
/* set 86MHz as the default frequency, and tune it */
{ KT0913_REG_TUNE, 0x86B8 },
/*
* FM&AM Softmute disabled, Mute disabled, 75us deemp.,
* no bass boost, 100uF anti pop cap
*/
{ KT0913_REG_VOLUME, 0xE080 },
};
static const struct regmap_config kt0913_regmap_config = {
.reg_bits = 8,
.val_bits = 16,
.max_register = KT0913_REG_AFC,
.volatile_table = &kt0913_all_registers_access_table,
.cache_type = REGCACHE_RBTREE,
.val_format_endian = REGMAP_ENDIAN_BIG,
};
/* ************************************************************************* */
/* bands where the kt0913 operates */
enum { BAND_FM, BAND_FM_CAMUS, BAND_AM };
static const struct v4l2_frequency_band kt0913_bands[] = {
{
/* BAND_FM */
.type = V4L2_TUNER_RADIO,
.index = 0, /* index provided to v4l2 */
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = KT0913_FM_RANGE_LOW_NO_CAMPUS * V4L2_KHZ_FREQ_MUL,
.rangehigh = KT0913_FM_RANGE_HIGH * V4L2_KHZ_FREQ_MUL,
.modulation = V4L2_BAND_MODULATION_FM,
},
{
/* BAND_FM_CAMUS */
.type = V4L2_TUNER_RADIO,
.index = 0, /* index provided to v4l2 */
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = KT0913_FM_RANGE_LOW_CAMPUS * V4L2_KHZ_FREQ_MUL,
.rangehigh = KT0913_FM_RANGE_HIGH * V4L2_KHZ_FREQ_MUL,
.modulation = V4L2_BAND_MODULATION_FM,
},
{
/* BAND_AM */
.type = V4L2_TUNER_RADIO,
.index = 1, /* index provided to v4l2 */
.capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_FREQ_BANDS,
.rangelow = KT0913_AM_RANGE_LOW * V4L2_KHZ_FREQ_MUL,
.rangehigh = KT0913_AM_RANGE_HIGH * V4L2_KHZ_FREQ_MUL,
.modulation = V4L2_BAND_MODULATION_AM,
},
};
/* ************************************************************************* */
static inline struct kt0913_device *v4l2_device_to_device(
struct v4l2_device *v4l2_dev)
{
return container_of(v4l2_dev,
struct kt0913_device, v4l2_dev);
}
static inline struct kt0913_device *v4l2_ctrl_to_device(
struct v4l2_ctrl *ctrl_handler)
{
return container_of(ctrl_handler->handler,
struct kt0913_device, ctrl_handler);
}
/* ************************************************************************* */
static inline u32 khz_to_v4l2_freq(unsigned int freq)
{
return freq * V4L2_KHZ_FREQ_MUL;
}
static inline unsigned int v4l2_freq_to_khz(u32 v4l2_freq)
{
return v4l2_freq / V4L2_KHZ_FREQ_MUL;
}
/* ************************************************************************* */
static int __kt0913_get_fm_frequency(struct kt0913_device *radio,
unsigned int *frequency)
{
unsigned int tune_reg;
int ret = regmap_read(radio->regmap, KT0913_REG_TUNE, &tune_reg);
if (ret)
return ret;
*frequency = (tune_reg & KT0913_TUNE_FMCHAN_MASK) * KT0913_FMCHAN_MUL;
return 0;
}
static int __kt0913_set_fm_frequency(struct kt0913_device *radio,
unsigned int frequency)
{
return regmap_write(radio->regmap, KT0913_REG_TUNE,
KT0913_TUNE_FMTUNE_ON | (frequency / KT0913_FMCHAN_MUL));
}
/* ************************************************************************* */
static int __kt0913_set_mute(struct kt0913_device *radio, int on)
{
return regmap_update_bits(radio->regmap,
KT0913_REG_VOLUME, KT0913_VOLUME_DMUTE_MASK,
on ? KT0913_VOLUME_DMUTE_ON : KT0913_VOLUME_DMUTE_OFF);
}
/* ************************************************************************* */
static int __kt0913_set_deemphasis(struct kt0913_device *radio, s32 deemp)
{
switch (deemp) {
case V4L2_DEEMPHASIS_75_uS:
return regmap_update_bits(radio->regmap,
KT0913_REG_VOLUME, KT0913_VOLUME_DE_MASK,
KT0913_VOLUME_DE_50US);
/* 50us is used for the disabled option (which is not supported
* on the chip) and the 50uS value
*/
default:
return regmap_update_bits(radio->regmap,
KT0913_REG_VOLUME, KT0913_VOLUME_DE_MASK,
KT0913_VOLUME_DE_75US);
}
}
/* ************************************************************************* */
static int __kt0913_set_volume(struct kt0913_device *radio, s32 volume)
{
/* map [-60, 0] to [1, 31] which is what the kt0913 expects */
volume = (volume / 2) + 31;
return regmap_update_bits(radio->regmap,
KT0913_REG_RXCFG, KT0913_RXCFGA_VOLUME_MASK,
volume);
}
/* ************************************************************************* */
static int __kt0913_set_standby(struct kt0913_device *radio, int standby)
{
return regmap_update_bits(radio->regmap,
KT0913_REG_RXCFG, KT0913_RXCFGA_STDBY_MASK,
standby ? KT0913_RXCFGA_STDBY_ON : KT0913_RXCFGA_STDBY_OFF);
}
/* ************************************************************************* */
static int __kt0913_get_pll_status(struct kt0913_device *radio, int *locked)
{
unsigned int statusa_reg;
int ret = regmap_read(radio->regmap, KT0913_REG_STATUSA, &statusa_reg);
if (ret)
return ret;
*locked = (statusa_reg & KT0913_STATUSA_PLL_LOCK_MASK) ==
KT0913_STATUSA_PLL_LOCK_LOCKED ? 1 : 0;
return 0;
}
/* ************************************************************************* */
static int __kt0913_get_rx_stereo_or_mono(struct kt0913_device *radio,
int *stereo)
{
unsigned int statusa_reg;
int ret = regmap_read(radio->regmap, KT0913_REG_STATUSA, &statusa_reg);
if (ret)
return ret;
*stereo = (statusa_reg & KT0913_STATUSA_ST_MASK) ==
KT0913_STATUSA_ST_STEREO ? 1 : 0;
return 0;
}
/* ************************************************************************* */
static int __kt0913_get_fm_rssi(struct kt0913_device *radio, s32 *rssi)
{
unsigned int statusa_reg;
int ret = regmap_read(radio->regmap, KT0913_REG_STATUSA, &statusa_reg);
if (ret)
return ret;
/* RSSI(dBm) = -100 + FMRSSI<4:0> * 3dBm
* even tho we can get the value in dBm, we want a %
*/
*rssi = (statusa_reg & KT0913_STATUSA_FMRSSI_MASK) >>
KT0913_STATUSA_FMRSSI_SHIFT;
/* map range 0-31 to 0-65535 */
*rssi *= 65535;
*rssi /= KT0913_STATUSA_FMRSSI_MASK >> KT0913_STATUSA_FMRSSI_SHIFT;
return 0;
}
/* ************************************************************************* */
static int __kt0913_get_cfg_stereo_enabled(struct kt0913_device *radio,
int *stereo)
{
unsigned int dspcfga_reg;
int ret = regmap_read(radio->regmap, KT0913_REG_DSPCFGA, &dspcfga_reg);
if (ret)
return ret;
*stereo = (dspcfga_reg & KT0913_DSPCFGA_MONO_MASK) ==
KT0913_DSPCFGA_MONO_OFF ? 1 : 0;
return ret;
}
static int __kt0913_set_cfg_stereo_enabled(struct kt0913_device *radio,
int stereo)
{
return regmap_update_bits(radio->regmap,
KT0913_REG_DSPCFGA, KT0913_DSPCFGA_MONO_MASK,
stereo ? KT0913_DSPCFGA_MONO_OFF : KT0913_DSPCFGA_MONO_ON);
}
/* ************************************************************************* */
static int __kt0913_set_au_gain(struct kt0913_device *radio, s32 gain)
{
switch (gain) {
case 6:
return regmap_update_bits(radio->regmap,
KT0913_REG_AMSYSCFG, KT0913_AMSYSCFG_AU_GAIN_MASK,
KT0913_AMSYSCFG_AU_GAIN_6DB);
case 3:
return regmap_update_bits(radio->regmap,
KT0913_REG_AMSYSCFG, KT0913_AMSYSCFG_AU_GAIN_MASK,
KT0913_AMSYSCFG_AU_GAIN_3DB);
case 0:
return regmap_update_bits(radio->regmap,
KT0913_REG_AMSYSCFG, KT0913_AMSYSCFG_AU_GAIN_MASK,
KT0913_AMSYSCFG_AU_GAIN_0DB);
case -3:
return regmap_update_bits(radio->regmap,
KT0913_REG_AMSYSCFG, KT0913_AMSYSCFG_AU_GAIN_MASK,
KT0913_AMSYSCFG_AU_GAIN_MIN_3DB);
default:
return -EINVAL;
}
}
/* ************************************************************************* */
static int __kt0913_set_am_fm_band(struct kt0913_device *radio,
unsigned int band)
{
return regmap_update_bits(radio->regmap,
KT0913_REG_AMSYSCFG, KT0913_AMSYSCFG_AM_FM_MASK,
band == BAND_AM ?
KT0913_AMSYSCFG_AM_FM_AM : KT0913_AMSYSCFG_AM_FM_FM);
}
/* ************************************************************************* */
static int __kt0913_get_am_frequency(struct kt0913_device *radio,
unsigned int *frequency)
{
unsigned int amchan_reg;
int ret = regmap_read(radio->regmap, KT0913_REG_AMCHAN, &amchan_reg);
if (ret)
return ret;
*frequency = (amchan_reg & KT0913_AMCHAN_AMCHAN_MASK);
return 0;
}
static int __kt0913_set_am_frequency(struct kt0913_device *radio,
unsigned int frequency)
{
return regmap_write(radio->regmap, KT0913_REG_AMCHAN,
KT0913_AMCHAN_AMTUNE_ON | frequency);
}
/* ************************************************************************* */
static int __kt0913_get_am_rssi(struct kt0913_device *radio, s32 *rssi)
{
unsigned int amstatusa_reg;
int ret = regmap_read(radio->regmap, KT0913_REG_AMSTATUSA, &amstatusa_reg);
if (ret)
return ret;
/* AMRSSI(dBm) = -90 + AMRSSI<4:0> * 3dBm
* even tho we can get the value in dBm, we want a %
*/
*rssi = (amstatusa_reg & KT0913_AMSTATUSA_AMRSSI_MASK) >>
KT0913_AMSTATUSA_AMRSSI_SHIFT;
/* map range 0-31 to 0-65535 */
*rssi *= 65535;
*rssi /= KT0913_AMSTATUSA_AMRSSI_MASK >> KT0913_AMSTATUSA_AMRSSI_SHIFT;
return 0;
}
/* ************************************************************************* */
static int __kt0913_init(struct kt0913_device *radio)
{
int ret = 0;
/* write the defaults */
ret = regmap_multi_reg_write(radio->regmap,
kt0913_init_regs_to_defaults,
ARRAY_SIZE(kt0913_init_regs_to_defaults));
if (ret) {
v4l2_err(radio->client,
"regmap_multi_reg_write() failed! %d", ret);
return ret;
}
/* set the audio dac anti-pop config */
ret = regmap_update_bits(radio->regmap,
KT0913_REG_VOLUME, KT0913_VOLUME_POP_MASK,
radio->audio_anti_pop << KT0913_VOLUME_POP_SHIFT);
if (ret) {
v4l2_err(radio->client,
"regmap_update_bits() failed for anti-pop cfg! %d", ret);
return ret;
}
/* set the reference clock config */
ret = regmap_update_bits(radio->regmap,
KT0913_REG_AMSYSCFG, KT0913_AMSYSCFG_REFCLK_MASK,
radio->refclock_val << KT0913_AMSYSCFG_REFCLK_SHIFT);
if (ret) {
v4l2_err(radio->client,
"regmap_update_bits() failed for refclk cfg! %d", ret);
return ret;
}
if (kt0913_use_campus_band) {
v4l2_info(radio->client,
"campus band is enabled!");
/* set the campus band bit */
ret = regmap_update_bits(radio->regmap,
KT0913_REG_LOCFGC, KT0913_LOCFG_CAMPUSBAND_EN_MASK,
KT0913_LOCFG_CAMPUSBAND_EN_ON);
if (ret) {
v4l2_err(radio->client,
"regmap_update_bits() failed for campus band! %d",
ret);
return ret;
}
}
return __kt0913_set_mute(radio, true);
}
/* ************************************************************************* */
static int kt0913_ioctl_vidioc_g_frequency(struct file *file, void *priv,
struct v4l2_frequency *f)
{
struct kt0913_device *radio = video_drvdata(file);
int ret;
if (f->tuner != 0)
return -EINVAL;
f->type = V4L2_TUNER_RADIO;
if (radio->band == BAND_AM)
ret = __kt0913_get_am_frequency(radio, &f->frequency);
else
ret = __kt0913_get_fm_frequency(radio, &f->frequency);
if (ret)
return ret;
/* convert kHz freq into v4l2 freq */
f->frequency = khz_to_v4l2_freq(f->frequency);
return 0;
}
static int kt0913_ioctl_vidioc_s_frequency(struct file *file, void *priv,
const struct v4l2_frequency *f)
{
struct kt0913_device *radio = video_drvdata(file);
unsigned int freq = f->frequency;
unsigned int new_band = BAND_FM;
int ret;
if (f->tuner != 0 || f->type != V4L2_TUNER_RADIO)
return -EINVAL;
if (freq == 0)
return -EINVAL;
/* check if the requested frequency is contained on the AM band */
if (freq <= kt0913_bands[BAND_AM].rangehigh)
new_band = BAND_AM;
else {
/* check if the requested frequency is contained on the FM band */
if (freq >= kt0913_bands[BAND_FM].rangelow)
new_band = BAND_FM;
/* check if the requested frequency is contained on the campus
* FM band only if that feature was enabled
*/
else if (kt0913_use_campus_band &&
(freq >= kt0913_bands[BAND_FM_CAMUS].rangelow))
new_band = BAND_FM_CAMUS;
else {
v4l2_warn(radio->client,
"frequency out of allowed RF bands (%u kHz)",
v4l2_freq_to_khz(freq));
return -EINVAL;
}
}
/* is the requested band different than the one currently set? */
if (radio->band != new_band) {
ret = __kt0913_set_am_fm_band(radio, new_band);
if (ret)
return ret;
radio->band = new_band;
}
/* clamp the frequency to the band boundaries */
freq = clamp(freq, kt0913_bands[new_band].rangelow,
kt0913_bands[new_band].rangehigh);
/* convert v4l2 freq to kHz */
freq = v4l2_freq_to_khz(freq);
if (radio->band == BAND_AM)
return __kt0913_set_am_frequency(radio, freq);
else
return __kt0913_set_fm_frequency(radio, freq);
}
static int kt0913_ioctl_vidioc_enum_freq_bands(struct file *file, void *priv,
struct v4l2_frequency_band *band)
{
if (band->tuner != 0)
return -EINVAL;
switch (band->index) {
case 0:
if (kt0913_use_campus_band)
*band = kt0913_bands[BAND_FM_CAMUS];
else
*band = kt0913_bands[BAND_FM];
return 0;
case 1:
*band = kt0913_bands[BAND_AM];
return 0;
default:
return -EINVAL;
}
}
/* ************************************************************************* */
/* V4L2 vidioc */
static int kt0913_ioctl_vidioc_querycap(struct file *file, void *priv,
struct v4l2_capability *capability)
{
struct kt0913_device *radio = video_drvdata(file);
struct video_device *dev;
if (!radio)
return -ENODEV;
dev = &radio->vdev;
if (!dev)
return -ENODEV;
strscpy(capability->driver, KT0913_FM_AM_DRIVER_NAME,
sizeof(capability->driver));
strscpy(capability->card, dev->name, sizeof(capability->card));
snprintf(capability->bus_info, sizeof(capability->bus_info),
"I2C:%s", dev_name(&dev->dev));
return 0;
}
static int kt0913_ioctl_vidioc_g_tuner(struct file *file, void *priv,
struct v4l2_tuner *v)
{
struct kt0913_device *radio = video_drvdata(file);
int ret;
int stereo_enabled;
int is_stereo;
if (v->index != 0)
return -EINVAL;
strscpy(v->name, "FM/AM", sizeof(v->name));
v->type = V4L2_TUNER_RADIO;
v->capability = V4L2_TUNER_CAP_LOW | V4L2_TUNER_CAP_STEREO |
V4L2_TUNER_CAP_FREQ_BANDS;
v->rangelow = kt0913_bands[BAND_AM].rangelow;
v->rangehigh = kt0913_bands[BAND_FM].rangehigh;
if (radio->band == BAND_AM) {
v->rxsubchans = V4L2_TUNER_SUB_MONO;
v->audmode = V4L2_TUNER_MODE_MONO;
ret = __kt0913_get_am_rssi(radio, &v->signal);
if (ret)
return ret;
} else {
ret = __kt0913_get_cfg_stereo_enabled(radio, &stereo_enabled);
if (ret)
return ret;
v->rxsubchans = stereo_enabled ?
V4L2_TUNER_SUB_STEREO : V4L2_TUNER_SUB_MONO;
ret = __kt0913_get_rx_stereo_or_mono(radio, &is_stereo);
if (ret)
return ret;
v->audmode = is_stereo ?
V4L2_TUNER_MODE_STEREO : V4L2_TUNER_MODE_MONO;
ret = __kt0913_get_fm_rssi(radio, &v->signal);
if (ret)
return ret;
}
/* AFC is enabled and active by default */
v->afc = 1;
return 0;
}
static int kt0913_ioctl_vidioc_s_tuner(struct file *file, void *priv,
const struct v4l2_tuner *v)
{
struct kt0913_device *radio = video_drvdata(file);
if (v->index != 0)
return -EINVAL;
/* only mono and stereo are supported */
if (v->audmode != V4L2_TUNER_MODE_MONO &&
v->audmode != V4L2_TUNER_MODE_STEREO)
return 0;
/* AM is mono only, so don't try to set it to stereo */
if (radio->band == BAND_AM && v->audmode != V4L2_TUNER_MODE_MONO)
return 0;
/* set to stereo if specified, otherwise set to mono */
return __kt0913_set_cfg_stereo_enabled(radio,
v->audmode == V4L2_TUNER_MODE_STEREO);
}
static int kt0913_s_ctrl(struct v4l2_ctrl *ctrl)
{
struct kt0913_device *radio = v4l2_ctrl_to_device(ctrl);
switch (ctrl->id) {
case V4L2_CID_AUDIO_MUTE:
return __kt0913_set_mute(radio, ctrl->val);
case V4L2_CID_AUDIO_VOLUME:
return __kt0913_set_volume(radio, ctrl->val);
case V4L2_CID_GAIN:
return __kt0913_set_au_gain(radio, ctrl->val);
case V4L2_CID_TUNE_DEEMPHASIS:
return __kt0913_set_deemphasis(radio, ctrl->val);
default:
return -EINVAL;
}
}
static int kt0913_g_volatile_ctrl(struct v4l2_ctrl *ctrl)
{
struct kt0913_device *radio = v4l2_ctrl_to_device(ctrl);
switch (ctrl->id) {
case V4L2_CID_RF_TUNER_PLL_LOCK:
return __kt0913_get_pll_status(radio, &ctrl->val);
default:
return -EINVAL;
}
}
static const struct v4l2_ctrl_ops kt0913_ctrl_ops = {
.s_ctrl = kt0913_s_ctrl,
.g_volatile_ctrl = kt0913_g_volatile_ctrl,
};
/* ************************************************************************* */
/* File system interface (use the ancillary fops for v4l2) */
static const struct v4l2_file_operations kt0913_radio_fops = {
.owner = THIS_MODULE,
.open = v4l2_fh_open,
.release = v4l2_fh_release,
.poll = v4l2_ctrl_poll,
.unlocked_ioctl = video_ioctl2,
};
/* ioctl ops */
static const struct v4l2_ioctl_ops kt0913_ioctl_ops = {
.vidioc_querycap = kt0913_ioctl_vidioc_querycap,
.vidioc_g_tuner = kt0913_ioctl_vidioc_g_tuner,
.vidioc_s_tuner = kt0913_ioctl_vidioc_s_tuner,
.vidioc_g_frequency = kt0913_ioctl_vidioc_g_frequency,
.vidioc_s_frequency = kt0913_ioctl_vidioc_s_frequency,
.vidioc_enum_freq_bands = kt0913_ioctl_vidioc_enum_freq_bands,
/* use ancillary functions for these: */
.vidioc_log_status = v4l2_ctrl_log_status,
.vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
};
/* V4L2 RADIO device structure */
static struct video_device kt0913_radio_template = {
.name = KT0913_FM_AM_DRIVER_NAME,
.fops = &kt0913_radio_fops,
.ioctl_ops = &kt0913_ioctl_ops,
.release = video_device_release_empty,
.vfl_dir = VFL_DIR_RX,
.device_caps = V4L2_CAP_TUNER | V4L2_CAP_RADIO,
};
/* ************************************************************************* */
#if IS_ENABLED(CONFIG_OF)
static const struct of_device_id kt0913_of_match[] = {
{.compatible = "ktm,kt0913" },
{ /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, kt0913_of_match);
#endif /* IS_ENABLED(CONFIG_OF) */
static void __kt0913_parse_dt(struct kt0913_device *radio)
{
const void *ptr_anti_pop = of_get_property(radio->client->dev.of_node,
"ktm,anti-pop", NULL);
const void *ptr_refclk = of_get_property(radio->client->dev.of_node,
"ktm,refclk", NULL);
if (ptr_anti_pop) {
radio->audio_anti_pop =
clamp(be32_to_cpup(ptr_anti_pop), 0U, 3U);
} else {
radio->audio_anti_pop = 0;
v4l2_warn(radio->client,
"No ktm,anti-pop on dt node, using default");
}
if (ptr_refclk) {
radio->refclock_val =
clamp(be32_to_cpup(ptr_refclk), 0U, 9U);
} else {
radio->refclock_val = 0;
v4l2_warn(radio->client,
"No ktm,refclk on dt node, using default");
}
}
/* ************************************************************************* */
static int kt0913_probe(struct i2c_client *client,
const struct i2c_device_id *id)
{
struct kt0913_device *radio;
struct v4l2_device *v4l2_dev;
struct v4l2_ctrl_handler *hdl;
struct regmap *regmap;
int ret;
pr_debug("%s\n", __func__);
/* this driver uses word R/W i2c operations, check if it's supported */
if (!i2c_check_functionality(client->adapter,
I2C_FUNC_SMBUS_READ_WORD_DATA | I2C_FUNC_SMBUS_WRITE_WORD_DATA)) {
v4l2_err(client,
"I2C adapter doesn't support word operations");
return -EIO;
}
/* check if the device exist on the bus before initializing it */
ret = i2c_smbus_read_word_data(client, KT0913_REG_CHIP_ID);
if (ret < 0) {
v4l2_err(client,
"Error reading CHIP ID of the kt0913 (%d)", ret);
return ret;
}
/* check if the CHIP ID register value matches the expected value */
if (ret != KT0913_CHIP_ID) {
v4l2_err(radio->client,
"Invalid CHIP ID: 0x%x, expected 0x%x", ret, KT0913_CHIP_ID);
return -ENODEV;
}
v4l2_info(client,
"kt0913 found @ 0x%x (%s)\n",
client->addr, client->adapter->name);
/* alloc context for the kt0913 radio struct */
radio = devm_kzalloc(&client->dev, sizeof(*radio), GFP_KERNEL);
if (!radio)
return -ENOMEM;
v4l2_dev = &radio->v4l2_dev;
ret = v4l2_device_register(&client->dev, v4l2_dev);
if (ret < 0) {
v4l2_err(client,
"could not register v4l2_dev\n");
goto errfr;
}
mutex_init(&radio->mutex);
/* register the control handler from the context struct */
hdl = &radio->ctrl_handler;
v4l2_ctrl_handler_init(hdl, 5);
/* add the control: Mute */
radio->ctrl_mute = v4l2_ctrl_new_std(hdl, &kt0913_ctrl_ops,
V4L2_CID_AUDIO_MUTE, 0, 1, 1, 0);
if (hdl->error) {
ret = hdl->error;
v4l2_err(v4l2_dev, "Could not register control: mute\n");