forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathradio-wl1273.c
2174 lines (1747 loc) · 50.5 KB
/
radio-wl1273.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
/*
* Driver for the Texas Instruments WL1273 FM radio.
*
* Copyright (C) 2011 Nokia Corporation
* Author: Matti J. Aaltonen <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <linux/delay.h>
#include <linux/firmware.h>
#include <linux/interrupt.h>
#include <linux/mfd/wl1273-core.h>
#include <linux/slab.h>
#include <linux/module.h>
#include <media/v4l2-common.h>
#include <media/v4l2-ctrls.h>
#include <media/v4l2-device.h>
#include <media/v4l2-ioctl.h>
#define DRIVER_DESC "Wl1273 FM Radio"
#define WL1273_POWER_SET_OFF 0
#define WL1273_POWER_SET_FM BIT(0)
#define WL1273_POWER_SET_RDS BIT(1)
#define WL1273_POWER_SET_RETENTION BIT(4)
#define WL1273_PUPD_SET_OFF 0x00
#define WL1273_PUPD_SET_ON 0x01
#define WL1273_PUPD_SET_RETENTION 0x10
#define WL1273_FREQ(x) (x * 10000 / 625)
#define WL1273_INV_FREQ(x) (x * 625 / 10000)
/*
* static int radio_nr - The number of the radio device
*
* The default is 0.
*/
static int radio_nr;
module_param(radio_nr, int, 0);
MODULE_PARM_DESC(radio_nr, "The number of the radio device. Default = 0");
struct wl1273_device {
char *bus_type;
u8 forbidden;
unsigned int preemphasis;
unsigned int spacing;
unsigned int tx_power;
unsigned int rx_frequency;
unsigned int tx_frequency;
unsigned int rangelow;
unsigned int rangehigh;
unsigned int band;
bool stereo;
/* RDS */
unsigned int rds_on;
wait_queue_head_t read_queue;
struct mutex lock; /* for serializing fm radio operations */
struct completion busy;
unsigned char *buffer;
unsigned int buf_size;
unsigned int rd_index;
unsigned int wr_index;
/* Selected interrupts */
u16 irq_flags;
u16 irq_received;
struct v4l2_ctrl_handler ctrl_handler;
struct v4l2_device v4l2dev;
struct video_device videodev;
struct device *dev;
struct wl1273_core *core;
struct file *owner;
char *write_buf;
unsigned int rds_users;
};
#define WL1273_IRQ_MASK (WL1273_FR_EVENT | \
WL1273_POW_ENB_EVENT)
/*
* static unsigned int rds_buf - the number of RDS buffer blocks used.
*
* The default number is 100.
*/
static unsigned int rds_buf = 100;
module_param(rds_buf, uint, 0);
MODULE_PARM_DESC(rds_buf, "Number of RDS buffer entries. Default = 100");
static int wl1273_fm_write_fw(struct wl1273_core *core,
__u8 *fw, int len)
{
struct i2c_client *client = core->client;
struct i2c_msg msg;
int i, r = 0;
msg.addr = client->addr;
msg.flags = 0;
for (i = 0; i <= len; i++) {
msg.len = fw[0];
msg.buf = fw + 1;
fw += msg.len + 1;
dev_dbg(&client->dev, "%s:len[%d]: %d\n", __func__, i, msg.len);
r = i2c_transfer(client->adapter, &msg, 1);
if (r < 0 && i < len + 1)
break;
}
dev_dbg(&client->dev, "%s: i: %d\n", __func__, i);
dev_dbg(&client->dev, "%s: len + 1: %d\n", __func__, len + 1);
/* Last transfer always fails. */
if (i == len || r == 1)
r = 0;
return r;
}
#define WL1273_FIFO_HAS_DATA(status) (1 << 5 & status)
#define WL1273_RDS_CORRECTABLE_ERROR (1 << 3)
#define WL1273_RDS_UNCORRECTABLE_ERROR (1 << 4)
static int wl1273_fm_rds(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
struct i2c_client *client = core->client;
u16 val;
u8 b0 = WL1273_RDS_DATA_GET, status;
struct v4l2_rds_data rds = { 0, 0, 0 };
struct i2c_msg msg[] = {
{
.addr = client->addr,
.flags = 0,
.buf = &b0,
.len = 1,
},
{
.addr = client->addr,
.flags = I2C_M_RD,
.buf = (u8 *) &rds,
.len = sizeof(rds),
}
};
int r;
if (core->mode != WL1273_MODE_RX)
return 0;
r = core->read(core, WL1273_RDS_SYNC_GET, &val);
if (r)
return r;
if ((val & 0x01) == 0) {
/* RDS decoder not synchronized */
return -EAGAIN;
}
/* copy all four RDS blocks to internal buffer */
do {
r = i2c_transfer(client->adapter, msg, ARRAY_SIZE(msg));
if (r != ARRAY_SIZE(msg)) {
dev_err(radio->dev, WL1273_FM_DRIVER_NAME
": %s: read_rds error r == %i)\n",
__func__, r);
}
status = rds.block;
if (!WL1273_FIFO_HAS_DATA(status))
break;
/* copy bits 0-2 (the block ID) to bits 3-5 */
rds.block = V4L2_RDS_BLOCK_MSK & status;
rds.block |= rds.block << 3;
/* copy the error bits to standard positions */
if (WL1273_RDS_UNCORRECTABLE_ERROR & status) {
rds.block |= V4L2_RDS_BLOCK_ERROR;
rds.block &= ~V4L2_RDS_BLOCK_CORRECTED;
} else if (WL1273_RDS_CORRECTABLE_ERROR & status) {
rds.block &= ~V4L2_RDS_BLOCK_ERROR;
rds.block |= V4L2_RDS_BLOCK_CORRECTED;
}
/* copy RDS block to internal buffer */
memcpy(&radio->buffer[radio->wr_index], &rds, RDS_BLOCK_SIZE);
radio->wr_index += 3;
/* wrap write pointer */
if (radio->wr_index >= radio->buf_size)
radio->wr_index = 0;
/* check for overflow & start over */
if (radio->wr_index == radio->rd_index) {
dev_dbg(radio->dev, "RDS OVERFLOW");
radio->rd_index = 0;
radio->wr_index = 0;
break;
}
} while (WL1273_FIFO_HAS_DATA(status));
/* wake up read queue */
if (radio->wr_index != radio->rd_index)
wake_up_interruptible(&radio->read_queue);
return 0;
}
static irqreturn_t wl1273_fm_irq_thread_handler(int irq, void *dev_id)
{
struct wl1273_device *radio = dev_id;
struct wl1273_core *core = radio->core;
u16 flags;
int r;
r = core->read(core, WL1273_FLAG_GET, &flags);
if (r)
goto out;
if (flags & WL1273_BL_EVENT) {
radio->irq_received = flags;
dev_dbg(radio->dev, "IRQ: BL\n");
}
if (flags & WL1273_RDS_EVENT) {
msleep(200);
wl1273_fm_rds(radio);
}
if (flags & WL1273_BBLK_EVENT)
dev_dbg(radio->dev, "IRQ: BBLK\n");
if (flags & WL1273_LSYNC_EVENT)
dev_dbg(radio->dev, "IRQ: LSYNC\n");
if (flags & WL1273_LEV_EVENT) {
u16 level;
r = core->read(core, WL1273_RSSI_LVL_GET, &level);
if (r)
goto out;
if (level > 14)
dev_dbg(radio->dev, "IRQ: LEV: 0x%x04\n", level);
}
if (flags & WL1273_IFFR_EVENT)
dev_dbg(radio->dev, "IRQ: IFFR\n");
if (flags & WL1273_PI_EVENT)
dev_dbg(radio->dev, "IRQ: PI\n");
if (flags & WL1273_PD_EVENT)
dev_dbg(radio->dev, "IRQ: PD\n");
if (flags & WL1273_STIC_EVENT)
dev_dbg(radio->dev, "IRQ: STIC\n");
if (flags & WL1273_MAL_EVENT)
dev_dbg(radio->dev, "IRQ: MAL\n");
if (flags & WL1273_POW_ENB_EVENT) {
complete(&radio->busy);
dev_dbg(radio->dev, "NOT BUSY\n");
dev_dbg(radio->dev, "IRQ: POW_ENB\n");
}
if (flags & WL1273_SCAN_OVER_EVENT)
dev_dbg(radio->dev, "IRQ: SCAN_OVER\n");
if (flags & WL1273_ERROR_EVENT)
dev_dbg(radio->dev, "IRQ: ERROR\n");
if (flags & WL1273_FR_EVENT) {
u16 freq;
dev_dbg(radio->dev, "IRQ: FR:\n");
if (core->mode == WL1273_MODE_RX) {
r = core->write(core, WL1273_TUNER_MODE_SET,
TUNER_MODE_STOP_SEARCH);
if (r) {
dev_err(radio->dev,
"%s: TUNER_MODE_SET fails: %d\n",
__func__, r);
goto out;
}
r = core->read(core, WL1273_FREQ_SET, &freq);
if (r)
goto out;
if (radio->band == WL1273_BAND_JAPAN)
radio->rx_frequency = WL1273_BAND_JAPAN_LOW +
freq * 50;
else
radio->rx_frequency = WL1273_BAND_OTHER_LOW +
freq * 50;
/*
* The driver works better with this msleep,
* the documentation doesn't mention it.
*/
usleep_range(10000, 15000);
dev_dbg(radio->dev, "%dkHz\n", radio->rx_frequency);
} else {
r = core->read(core, WL1273_CHANL_SET, &freq);
if (r)
goto out;
dev_dbg(radio->dev, "%dkHz\n", freq);
}
dev_dbg(radio->dev, "%s: NOT BUSY\n", __func__);
}
out:
core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
complete(&radio->busy);
return IRQ_HANDLED;
}
static int wl1273_fm_set_tx_freq(struct wl1273_device *radio, unsigned int freq)
{
struct wl1273_core *core = radio->core;
int r = 0;
unsigned long t;
if (freq < WL1273_BAND_TX_LOW) {
dev_err(radio->dev,
"Frequency out of range: %d < %d\n", freq,
WL1273_BAND_TX_LOW);
return -ERANGE;
}
if (freq > WL1273_BAND_TX_HIGH) {
dev_err(radio->dev,
"Frequency out of range: %d > %d\n", freq,
WL1273_BAND_TX_HIGH);
return -ERANGE;
}
/*
* The driver works better with this sleep,
* the documentation doesn't mention it.
*/
usleep_range(5000, 10000);
dev_dbg(radio->dev, "%s: freq: %d kHz\n", __func__, freq);
/* Set the current tx channel */
r = core->write(core, WL1273_CHANL_SET, freq / 10);
if (r)
return r;
reinit_completion(&radio->busy);
/* wait for the FR IRQ */
t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
if (!t)
return -ETIMEDOUT;
dev_dbg(radio->dev, "WL1273_CHANL_SET: %lu\n", t);
/* Enable the output power */
r = core->write(core, WL1273_POWER_ENB_SET, 1);
if (r)
return r;
reinit_completion(&radio->busy);
/* wait for the POWER_ENB IRQ */
t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
if (!t)
return -ETIMEDOUT;
radio->tx_frequency = freq;
dev_dbg(radio->dev, "WL1273_POWER_ENB_SET: %lu\n", t);
return 0;
}
static int wl1273_fm_set_rx_freq(struct wl1273_device *radio, unsigned int freq)
{
struct wl1273_core *core = radio->core;
int r, f;
unsigned long t;
if (freq < radio->rangelow) {
dev_err(radio->dev,
"Frequency out of range: %d < %d\n", freq,
radio->rangelow);
r = -ERANGE;
goto err;
}
if (freq > radio->rangehigh) {
dev_err(radio->dev,
"Frequency out of range: %d > %d\n", freq,
radio->rangehigh);
r = -ERANGE;
goto err;
}
dev_dbg(radio->dev, "%s: %dkHz\n", __func__, freq);
core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
if (radio->band == WL1273_BAND_JAPAN)
f = (freq - WL1273_BAND_JAPAN_LOW) / 50;
else
f = (freq - WL1273_BAND_OTHER_LOW) / 50;
r = core->write(core, WL1273_FREQ_SET, f);
if (r) {
dev_err(radio->dev, "FREQ_SET fails\n");
goto err;
}
r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_PRESET);
if (r) {
dev_err(radio->dev, "TUNER_MODE_SET fails\n");
goto err;
}
reinit_completion(&radio->busy);
t = wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(2000));
if (!t) {
dev_err(radio->dev, "%s: TIMEOUT\n", __func__);
return -ETIMEDOUT;
}
radio->rd_index = 0;
radio->wr_index = 0;
radio->rx_frequency = freq;
return 0;
err:
return r;
}
static int wl1273_fm_get_freq(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
unsigned int freq;
u16 f;
int r;
if (core->mode == WL1273_MODE_RX) {
r = core->read(core, WL1273_FREQ_SET, &f);
if (r)
return r;
dev_dbg(radio->dev, "Freq get: 0x%04x\n", f);
if (radio->band == WL1273_BAND_JAPAN)
freq = WL1273_BAND_JAPAN_LOW + 50 * f;
else
freq = WL1273_BAND_OTHER_LOW + 50 * f;
} else {
r = core->read(core, WL1273_CHANL_SET, &f);
if (r)
return r;
freq = f * 10;
}
return freq;
}
/**
* wl1273_fm_upload_firmware_patch() - Upload the firmware.
* @radio: A pointer to the device struct.
*
* The firmware file consists of arrays of bytes where the first byte
* gives the array length. The first byte in the file gives the
* number of these arrays.
*/
static int wl1273_fm_upload_firmware_patch(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
unsigned int packet_num;
const struct firmware *fw_p;
const char *fw_name = "radio-wl1273-fw.bin";
struct device *dev = radio->dev;
__u8 *ptr;
int r;
dev_dbg(dev, "%s:\n", __func__);
/*
* Uploading the firmware patch is not always necessary,
* so we only print an info message.
*/
if (request_firmware(&fw_p, fw_name, dev)) {
dev_info(dev, "%s - %s not found\n", __func__, fw_name);
return 0;
}
ptr = (__u8 *) fw_p->data;
packet_num = ptr[0];
dev_dbg(dev, "%s: packets: %d\n", __func__, packet_num);
r = wl1273_fm_write_fw(core, ptr + 1, packet_num);
if (r) {
dev_err(dev, "FW upload error: %d\n", r);
goto out;
}
/* ignore possible error here */
core->write(core, WL1273_RESET, 0);
dev_dbg(dev, "%s - download OK, r: %d\n", __func__, r);
out:
release_firmware(fw_p);
return r;
}
static int wl1273_fm_stop(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
if (core->mode == WL1273_MODE_RX) {
int r = core->write(core, WL1273_POWER_SET,
WL1273_POWER_SET_OFF);
if (r)
dev_err(radio->dev, "%s: POWER_SET fails: %d\n",
__func__, r);
} else if (core->mode == WL1273_MODE_TX) {
int r = core->write(core, WL1273_PUPD_SET,
WL1273_PUPD_SET_OFF);
if (r)
dev_err(radio->dev,
"%s: PUPD_SET fails: %d\n", __func__, r);
}
if (core->pdata->disable) {
core->pdata->disable();
dev_dbg(radio->dev, "Back to reset\n");
}
return 0;
}
static int wl1273_fm_start(struct wl1273_device *radio, int new_mode)
{
struct wl1273_core *core = radio->core;
struct wl1273_fm_platform_data *pdata = core->pdata;
struct device *dev = radio->dev;
int r = -EINVAL;
if (pdata->enable && core->mode == WL1273_MODE_OFF) {
dev_dbg(radio->dev, "Out of reset\n");
pdata->enable();
msleep(250);
}
if (new_mode == WL1273_MODE_RX) {
u16 val = WL1273_POWER_SET_FM;
if (radio->rds_on)
val |= WL1273_POWER_SET_RDS;
/* If this fails try again */
r = core->write(core, WL1273_POWER_SET, val);
if (r) {
msleep(100);
r = core->write(core, WL1273_POWER_SET, val);
if (r) {
dev_err(dev, "%s: POWER_SET fails\n", __func__);
goto fail;
}
}
/* rds buffer configuration */
radio->wr_index = 0;
radio->rd_index = 0;
} else if (new_mode == WL1273_MODE_TX) {
/* If this fails try again once */
r = core->write(core, WL1273_PUPD_SET, WL1273_PUPD_SET_ON);
if (r) {
msleep(100);
r = core->write(core, WL1273_PUPD_SET,
WL1273_PUPD_SET_ON);
if (r) {
dev_err(dev, "%s: PUPD_SET fails\n", __func__);
goto fail;
}
}
if (radio->rds_on) {
r = core->write(core, WL1273_RDS_DATA_ENB, 1);
if (r) {
dev_err(dev, "%s: RDS_DATA_ENB ON fails\n",
__func__);
goto fail;
}
} else {
r = core->write(core, WL1273_RDS_DATA_ENB, 0);
if (r) {
dev_err(dev, "%s: RDS_DATA_ENB OFF fails\n",
__func__);
goto fail;
}
}
} else {
dev_warn(dev, "%s: Illegal mode.\n", __func__);
}
if (core->mode == WL1273_MODE_OFF) {
r = wl1273_fm_upload_firmware_patch(radio);
if (r)
dev_warn(dev, "Firmware upload failed.\n");
/*
* Sometimes the chip is in a wrong power state at this point.
* So we set the power once again.
*/
if (new_mode == WL1273_MODE_RX) {
u16 val = WL1273_POWER_SET_FM;
if (radio->rds_on)
val |= WL1273_POWER_SET_RDS;
r = core->write(core, WL1273_POWER_SET, val);
if (r) {
dev_err(dev, "%s: POWER_SET fails\n", __func__);
goto fail;
}
} else if (new_mode == WL1273_MODE_TX) {
r = core->write(core, WL1273_PUPD_SET,
WL1273_PUPD_SET_ON);
if (r) {
dev_err(dev, "%s: PUPD_SET fails\n", __func__);
goto fail;
}
}
}
return 0;
fail:
if (pdata->disable)
pdata->disable();
dev_dbg(dev, "%s: return: %d\n", __func__, r);
return r;
}
static int wl1273_fm_suspend(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
int r;
/* Cannot go from OFF to SUSPENDED */
if (core->mode == WL1273_MODE_RX)
r = core->write(core, WL1273_POWER_SET,
WL1273_POWER_SET_RETENTION);
else if (core->mode == WL1273_MODE_TX)
r = core->write(core, WL1273_PUPD_SET,
WL1273_PUPD_SET_RETENTION);
else
r = -EINVAL;
if (r) {
dev_err(radio->dev, "%s: POWER_SET fails: %d\n", __func__, r);
goto out;
}
out:
return r;
}
static int wl1273_fm_set_mode(struct wl1273_device *radio, int mode)
{
struct wl1273_core *core = radio->core;
struct device *dev = radio->dev;
int old_mode;
int r;
dev_dbg(dev, "%s\n", __func__);
dev_dbg(dev, "Forbidden modes: 0x%02x\n", radio->forbidden);
old_mode = core->mode;
if (mode & radio->forbidden) {
r = -EPERM;
goto out;
}
switch (mode) {
case WL1273_MODE_RX:
case WL1273_MODE_TX:
r = wl1273_fm_start(radio, mode);
if (r) {
dev_err(dev, "%s: Cannot start.\n", __func__);
wl1273_fm_stop(radio);
goto out;
}
core->mode = mode;
r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
if (r) {
dev_err(dev, "INT_MASK_SET fails.\n");
goto out;
}
/* remember previous settings */
if (mode == WL1273_MODE_RX) {
r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
if (r) {
dev_err(dev, "set freq fails: %d.\n", r);
goto out;
}
r = core->set_volume(core, core->volume);
if (r) {
dev_err(dev, "set volume fails: %d.\n", r);
goto out;
}
dev_dbg(dev, "%s: Set vol: %d.\n", __func__,
core->volume);
} else {
r = wl1273_fm_set_tx_freq(radio, radio->tx_frequency);
if (r) {
dev_err(dev, "set freq fails: %d.\n", r);
goto out;
}
}
dev_dbg(radio->dev, "%s: Set audio mode.\n", __func__);
r = core->set_audio(core, core->audio_mode);
if (r)
dev_err(dev, "Cannot set audio mode.\n");
break;
case WL1273_MODE_OFF:
r = wl1273_fm_stop(radio);
if (r)
dev_err(dev, "%s: Off fails: %d\n", __func__, r);
else
core->mode = WL1273_MODE_OFF;
break;
case WL1273_MODE_SUSPENDED:
r = wl1273_fm_suspend(radio);
if (r)
dev_err(dev, "%s: Suspend fails: %d\n", __func__, r);
else
core->mode = WL1273_MODE_SUSPENDED;
break;
default:
dev_err(dev, "%s: Unknown mode: %d\n", __func__, mode);
r = -EINVAL;
break;
}
out:
if (r)
core->mode = old_mode;
return r;
}
static int wl1273_fm_set_seek(struct wl1273_device *radio,
unsigned int wrap_around,
unsigned int seek_upward,
int level)
{
struct wl1273_core *core = radio->core;
int r = 0;
unsigned int dir = (seek_upward == 0) ? 0 : 1;
unsigned int f;
f = radio->rx_frequency;
dev_dbg(radio->dev, "rx_frequency: %d\n", f);
if (dir && f + radio->spacing <= radio->rangehigh)
r = wl1273_fm_set_rx_freq(radio, f + radio->spacing);
else if (dir && wrap_around)
r = wl1273_fm_set_rx_freq(radio, radio->rangelow);
else if (f - radio->spacing >= radio->rangelow)
r = wl1273_fm_set_rx_freq(radio, f - radio->spacing);
else if (wrap_around)
r = wl1273_fm_set_rx_freq(radio, radio->rangehigh);
if (r)
goto out;
if (level < SCHAR_MIN || level > SCHAR_MAX)
return -EINVAL;
reinit_completion(&radio->busy);
dev_dbg(radio->dev, "%s: BUSY\n", __func__);
r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
if (r)
goto out;
dev_dbg(radio->dev, "%s\n", __func__);
r = core->write(core, WL1273_SEARCH_LVL_SET, level);
if (r)
goto out;
r = core->write(core, WL1273_SEARCH_DIR_SET, dir);
if (r)
goto out;
r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
if (r)
goto out;
/* wait for the FR IRQ */
wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000));
if (!(radio->irq_received & WL1273_BL_EVENT)) {
r = -ETIMEDOUT;
goto out;
}
radio->irq_received &= ~WL1273_BL_EVENT;
if (!wrap_around)
goto out;
/* Wrap around */
dev_dbg(radio->dev, "Wrap around in HW seek.\n");
if (seek_upward)
f = radio->rangelow;
else
f = radio->rangehigh;
r = wl1273_fm_set_rx_freq(radio, f);
if (r)
goto out;
reinit_completion(&radio->busy);
dev_dbg(radio->dev, "%s: BUSY\n", __func__);
r = core->write(core, WL1273_TUNER_MODE_SET, TUNER_MODE_AUTO_SEEK);
if (r)
goto out;
/* wait for the FR IRQ */
if (!wait_for_completion_timeout(&radio->busy, msecs_to_jiffies(1000)))
r = -ETIMEDOUT;
out:
dev_dbg(radio->dev, "%s: Err: %d\n", __func__, r);
return r;
}
/**
* wl1273_fm_get_tx_ctune() - Get the TX tuning capacitor value.
* @radio: A pointer to the device struct.
*/
static unsigned int wl1273_fm_get_tx_ctune(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
struct device *dev = radio->dev;
u16 val;
int r;
if (core->mode == WL1273_MODE_OFF ||
core->mode == WL1273_MODE_SUSPENDED)
return -EPERM;
r = core->read(core, WL1273_READ_FMANT_TUNE_VALUE, &val);
if (r) {
dev_err(dev, "%s: read error: %d\n", __func__, r);
goto out;
}
out:
return val;
}
/**
* wl1273_fm_set_preemphasis() - Set the TX pre-emphasis value.
* @radio: A pointer to the device struct.
* @preemphasis: The new pre-amphasis value.
*
* Possible pre-emphasis values are: V4L2_PREEMPHASIS_DISABLED,
* V4L2_PREEMPHASIS_50_uS and V4L2_PREEMPHASIS_75_uS.
*/
static int wl1273_fm_set_preemphasis(struct wl1273_device *radio,
unsigned int preemphasis)
{
struct wl1273_core *core = radio->core;
int r;
u16 em;
if (core->mode == WL1273_MODE_OFF ||
core->mode == WL1273_MODE_SUSPENDED)
return -EPERM;
mutex_lock(&core->lock);
switch (preemphasis) {
case V4L2_PREEMPHASIS_DISABLED:
em = 1;
break;
case V4L2_PREEMPHASIS_50_uS:
em = 0;
break;
case V4L2_PREEMPHASIS_75_uS:
em = 2;
break;
default:
r = -EINVAL;
goto out;
}
r = core->write(core, WL1273_PREMPH_SET, em);
if (r)
goto out;
radio->preemphasis = preemphasis;
out:
mutex_unlock(&core->lock);
return r;
}
static int wl1273_fm_rds_on(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
int r;
dev_dbg(radio->dev, "%s\n", __func__);
if (radio->rds_on)
return 0;
r = core->write(core, WL1273_POWER_SET,
WL1273_POWER_SET_FM | WL1273_POWER_SET_RDS);
if (r)
goto out;
r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
if (r)
dev_err(radio->dev, "set freq fails: %d.\n", r);
out:
return r;
}
static int wl1273_fm_rds_off(struct wl1273_device *radio)
{
struct wl1273_core *core = radio->core;
int r;
if (!radio->rds_on)
return 0;
radio->irq_flags &= ~WL1273_RDS_EVENT;
r = core->write(core, WL1273_INT_MASK_SET, radio->irq_flags);
if (r)
goto out;
/* Service pending read */
wake_up_interruptible(&radio->read_queue);
dev_dbg(radio->dev, "%s\n", __func__);
r = core->write(core, WL1273_POWER_SET, WL1273_POWER_SET_FM);
if (r)
goto out;
r = wl1273_fm_set_rx_freq(radio, radio->rx_frequency);
if (r)
dev_err(radio->dev, "set freq fails: %d.\n", r);
out:
dev_dbg(radio->dev, "%s: exiting...\n", __func__);
return r;
}