summaryrefslogtreecommitdiff
path: root/drivers/char/mxc_mu.c
blob: 5d16f62a6246250f2a5c42c15c265a854c2d3ce2 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
/*
 * Copyright 2004-2007 Freescale Semiconductor, Inc. All Rights Reserved.
 */

/*
 * The code contained herein is licensed under the GNU General Public
 * License. You may obtain a copy of the GNU General Public License
 * Version 2 or later at the following locations:
 *
 * http://www.opensource.org/licenses/gpl-license.html
 * http://www.gnu.org/copyleft/gpl.html
 */

/*!
 * @file mxc_mu.c
 *
 * @brief This file provides all the kernel level and user level API
 * definitions for the message transfer between MCU and DSP core.
 *
 * The Interfaces are with respect to the MCU core. Any driver on the MCU side
 * can transfer messages to the DSP side using the interfaces implemented here.
 *
 * @ingroup MU
 */

/*
 * Include Files
 */
#include <linux/module.h>
#include <linux/interrupt.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/circ_buf.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/clk.h>
#include <asm/arch/mxc_mu.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include "mxc_mu_reg.h"

#define DVR_VER "2.0"
static struct class *mxc_mu_class;
static int mu_major;

static struct clk *mu_clkp;

/*
 * Used to calculate number of bytes in the read buffer
 */
#define GET_READBYTES(a) CIRC_CNT(mu_rbuf[a].cbuf.head, \
                                  mu_rbuf[a].cbuf.tail, MAX_BUF_SIZE)

/*
 * Used to calculate number of bytes in the write buffer
 */
#define GET_WRITEBYTES(a) CIRC_CNT(mu_wbuf[a].cbuf.head, \
                                   mu_wbuf[a].cbuf.tail, MAX_BUF_SIZE)

/*
 * Used to calculate empty space in the read buffer
 */
#define GET_READSPACE(a) CIRC_SPACE(mu_rbuf[a].cbuf.head, \
                                    mu_rbuf[a].cbuf.tail, MAX_BUF_SIZE)

/*
 * Used to calculate empty space in the write buffer
 */
#define GET_WRITESPACE(a) CIRC_SPACE(mu_wbuf[a].cbuf.head, \
                                     mu_wbuf[a].cbuf.tail, MAX_BUF_SIZE)

/*!
 * There are 4 callback functions, one for each receive
 * interrupt. When 'bind' function is called the callback
 * functions are stored in an array and retrieved in the
 * tasklets when a receive interrupt arrives
 */
static callbackfn rxcallback[NUM_MU_CHANNELS];

/*!
 * There are 4 callback functions, one for each transmit
 * interrupt. When 'bind' function is called the callback
 * functions are stored in an array and retrieved in the
 * tasklets when a transmit interrupt arrives
 */
static callbackfn txcallback[NUM_MU_CHANNELS];

/*!
 * There are 4 callback functions, one for each general purpose
 * interrupt. When 'bind' function is called the callback
 * functions are stored in an array and retrieved in the
 * tasklets when a general purpose interrupt arrives
 */
static callbackfn gpcallback[NUM_MU_CHANNELS];

/*!
 * Declaring lock for mutual exclusion
 */
static DEFINE_SPINLOCK(mu_lock);

/*!
 * Declaring lock to prevent access to
 * allocated registers
 */
static DEFINE_SPINLOCK(alloc_lock);

/*!
 * Number of users waiting in suspendq
 */
static int swait = 0;

/*!
 * To indicate whether any of the MU devices are suspending
 */
static int suspend_flag = 0;

/*!
 * To restore the state of control register while resuming the device
 * to the state it was before suspending it.
 */
static unsigned int mcr_suspend_state = 0;

/*!
 * Indicate block mode - 0 and non block mode - 1. Default is non-block mode
 */
static int blockmode[NUM_MU_CHANNELS] = { 1, 1, 1, 1 };

/*!
 * Device Kernel Buffers. Each channel has one read and write buffer.
 */
typedef struct {
	/*!
	 * Buffer used to store data that was read or the data that is
	 * to be written
	 */
	struct circ_buf cbuf;
	/*!
	 * to indicate number of users waiting on blocking mode of the device
	 */
	int pwait;
} ring_buffer;

ring_buffer mu_rbuf[NUM_MU_CHANNELS], mu_wbuf[NUM_MU_CHANNELS];

/*!
 * reg_allocated[] stores channel handlers for the allocated registers.
 */
static int reg_allocated[NUM_MU_CHANNELS] = { -1, -1, -1, -1 };

static unsigned int wbits[NUM_MU_CHANNELS] = { AS_MUMSR_MTE0, AS_MUMSR_MTE1,
	AS_MUMSR_MTE2, AS_MUMSR_MTE3
};

static unsigned int rbits[NUM_MU_CHANNELS] = { AS_MUMSR_MRF0, AS_MUMSR_MRF1,
	AS_MUMSR_MRF2, AS_MUMSR_MRF3
};

static unsigned int read_en[NUM_MU_CHANNELS] = { AS_MUMCR_MRIE0, AS_MUMCR_MRIE1,
	AS_MUMCR_MRIE2, AS_MUMCR_MRIE3
};

static unsigned int write_en[NUM_MU_CHANNELS] =
    { AS_MUMCR_MTIE0, AS_MUMCR_MTIE1,
	AS_MUMCR_MTIE2, AS_MUMCR_MTIE3
};

static unsigned int genp_en[NUM_MU_CHANNELS] = { AS_MUMCR_MGIE0, AS_MUMCR_MGIE1,
	AS_MUMCR_MGIE2, AS_MUMCR_MGIE3
};

static unsigned int genp_pend[NUM_MU_CHANNELS] =
    { AS_MUMSR_MGIP0, AS_MUMSR_MGIP1,
	AS_MUMSR_MGIP2, AS_MUMSR_MGIP3
};

static unsigned int genp_req[NUM_MU_CHANNELS] =
    { AS_MUMCR_MGIR0, AS_MUMCR_MGIR1,
	AS_MUMCR_MGIR2, AS_MUMCR_MGIR3
};

static unsigned int read_reg[NUM_MU_CHANNELS] = { AS_MUMRR0, AS_MUMRR1,
	AS_MUMRR2, AS_MUMRR3
};

static unsigned int write_reg[NUM_MU_CHANNELS] = { AS_MUMTR0, AS_MUMTR1,
	AS_MUMTR2, AS_MUMTR3
};

/*!
 * The readq and waitq are used by blocking read/write
 */
static wait_queue_head_t readq, writeq, suspendq, wait_before_closeq;

/*!
 * To enable or disable byte swapping
 */
static bool byte_swapping_rd[NUM_MU_CHANNELS] = { false, false, false, false };
static bool byte_swapping_wr[NUM_MU_CHANNELS] = { false, false, false, false };

/*
 * Declaring Bottom-half handlers - one for GP, RX and TX interrupt lines
 */
static void mxc_mu_rxhandler(unsigned long someval);
static void mxc_mu_txhandler(unsigned long someval);
static void mxc_mu_gphandler(unsigned long someval);

/*
 * Tasklets Declarations
 */
/*
 * Declaring tasklet to handle receive interrupts
 */
DECLARE_TASKLET(rxch0tasklet, (void *)mxc_mu_rxhandler, (unsigned long)0);
DECLARE_TASKLET(rxch1tasklet, (void *)mxc_mu_rxhandler, (unsigned long)1);
DECLARE_TASKLET(rxch2tasklet, (void *)mxc_mu_rxhandler, (unsigned long)2);
DECLARE_TASKLET(rxch3tasklet, (void *)mxc_mu_rxhandler, (unsigned long)3);

/*
 * Declaring tasklet to handle transmit interrupts
 */
DECLARE_TASKLET(txch0tasklet, (void *)mxc_mu_txhandler, (unsigned long)0);
DECLARE_TASKLET(txch1tasklet, (void *)mxc_mu_txhandler, (unsigned long)1);
DECLARE_TASKLET(txch2tasklet, (void *)mxc_mu_txhandler, (unsigned long)2);
DECLARE_TASKLET(txch3tasklet, (void *)mxc_mu_txhandler, (unsigned long)3);

/*
 * Declaring tasklet to handle general purpose interrupts
 */
DECLARE_TASKLET(gpch0tasklet, (void *)mxc_mu_gphandler, (unsigned long)0);
DECLARE_TASKLET(gpch1tasklet, (void *)mxc_mu_gphandler, (unsigned long)1);
DECLARE_TASKLET(gpch2tasklet, (void *)mxc_mu_gphandler, (unsigned long)2);
DECLARE_TASKLET(gpch3tasklet, (void *)mxc_mu_gphandler, (unsigned long)3);

/*!
 * Allocating Resources:
 * Allocating channels required to the caller if they are free.
 * 'reg_allocated' variable holds all the registers that have been allocated
 *
 * @param   chnum   The channel number required is passed
 *
 * @return          Returns unique channel handler associated with channel. This
 *                  channel handler is used to validate the user accessing the
 *                  channel.
 *                  Returns channel handler - on success
 *                  Returns negative value - on error
 *                     If invalid channel is passed, it returns -ENODEV error.
 *                     If no channel is available, returns -EBUSY.
 */
int mxc_mu_alloc_channel(int chnum)
{
	unsigned long flags;
	int ret_val = -EBUSY;

	if (chnum < 0 || chnum > 3) {
		return -ENODEV;
	}
	if (suspend_flag == 1) {
		return -EBUSY;
	}
	/* Acquiring Lock */
	spin_lock_irqsave(&alloc_lock, flags);
	if (reg_allocated[chnum] == -1) {
		/*
		 * If channel requested is available, create
		 * channel handler and store in the array
		 */
		reg_allocated[chnum] = BASE_NUM + chnum;
		ret_val = BASE_NUM + chnum;
	}
	spin_unlock_irqrestore(&alloc_lock, flags);
	return ret_val;
}

/*!
 * Deallocating Resources:
 *
 * @param   chand   The channel handler associated with the channel
 *                  that is to be freed is passed.
 *
 * @return          Returns 0 on success and -1 if channel was not
 *                  already allocated or ENODEV error if invalid
 *                  channel is obtained from the channel handler
 */
int mxc_mu_dealloc_channel(int chand)
{
	unsigned long flags;
	int retval = -1;
	int chnum = chand - BASE_NUM;

	if (chnum < 0 || chnum > 3) {
		return -ENODEV;
	}
	if (suspend_flag == 1) {
		return -EBUSY;
	}
	spin_lock_irqsave(&alloc_lock, flags);
	if (reg_allocated[chnum] == chand) {
		/* deallocating */
		reg_allocated[chnum] = -1;
		/* clearing callback arrays */
		rxcallback[chnum] = NULL;
		txcallback[chnum] = NULL;
		gpcallback[chnum] = NULL;
		retval = 0;
	}
	spin_unlock_irqrestore(&alloc_lock, flags);
	return retval;
}

/* NON BLOCKING READ/WRITE */

/*!
 * This function is called by other modules to read from one of the
 * receive registers on the MCU side.
 *
 * @param   chand         The channel handler associated with the channel
 *                        to be accessed is passed
 * @param   mu_msg        Buffer where the read data has to be stored
 *                        is passed by pointer as an argument
 *
 * @return                Returns 0 on success or
 *                        Returns NO_DATA error if there is no data to read or
 *                        Returns NO_ACCESS error if the incorrect channel
 *                        is accessed or ENODEV error if invalid
 *                        channel is obtained from the channel handler
 *                        Returns -1 if the buffer passed is not allocated
 */
int mxc_mu_mcuread(int chand, char *mu_msg)
{
	/* mu_msg should be 4 bytes long */
	int chnum = chand - BASE_NUM;

	if (chnum < 0 || chnum > 3) {
		return -ENODEV;
	}
	if (suspend_flag == 1) {
		return -EBUSY;
	}
	if (mu_msg == NULL) {
		return -1;
	}
	if ((readl(AS_MUMSR) & rbits[chnum]) == 0) {
		return NO_DATA;
	}
	if (reg_allocated[chnum] == chand) {
		*(unsigned int *)mu_msg = readl(read_reg[chnum]);
	} else {
		return NO_ACCESS;
	}
	if (byte_swapping_rd[chnum] == true) {
		*(unsigned int *)mu_msg = swab32(*(unsigned int *)mu_msg);
	}
	return 0;
}

/*!
 * This function is called by other modules to write to one of the
 * transmit registers on the MCU side
 *
 * @param   chand         The channel handler associated with the channel
 *                        to be accessed is passed
 * @param   mu_msg        Buffer where the write data has to be stored
 *                        is passed by pointer as an argument
 *
 * @return                Returns 0 on success or
 *                        Returns NO_DATA error if the register not empty or
 *                        Returns NO_ACCESS error if the incorrect channel
 *                        is accessed or ENODEV error if invalid
 *                        channel is obtained from the channel handler
 *                        Returns -1 if the buffer passed is not allocated
 */
int mxc_mu_mcuwrite(int chand, char *mu_msg)
{
	int chnum = chand - BASE_NUM;

	if (chnum < 0 || chnum > 3) {
		return -ENODEV;
	}
	if (suspend_flag == 1) {
		return -EBUSY;
	}
	if (mu_msg == NULL) {
		return -1;
	}
	if (byte_swapping_wr[chnum] == true) {
		/* Swap the bytes */
		*(unsigned int *)mu_msg = swab32(*(unsigned int *)mu_msg);
	}
	if ((readl(AS_MUMSR) & wbits[chnum]) == 0) {
		return NO_DATA;
	}
	/* Check whether the register is allocated */
	if (reg_allocated[chnum] == chand) {
		writel(*(unsigned int *)mu_msg, write_reg[chnum]);
	} else {
		return NO_ACCESS;
	}
	return 0;
}

/*!
 * Tasklet used by the Interrupt service routine to handle the muxed
 * receive interrupts. When an interrupt occurs, control from the top main
 * interrupt service routine is transferred to this tasklet which sends the
 * channel number to the call back function
 *
 * @param   chnum  index value indicating channel number
 *
 */
static void mxc_mu_rxhandler(unsigned long chnum)
{
	if (rxcallback[chnum] != NULL) {
		rxcallback[chnum] (chnum);
	}
}

/*!
 * Tasklet used by the Interrupt service routine to handle the muxed
 * transmit interrupts. When an interrupt occurs, control from the top main
 * interrupt service routine is transferred to this tasklet which sends the
 * channel number to the call back function
 *
 * @param   chnum  index value indicating channel number
 *
 */
static void mxc_mu_txhandler(unsigned long chnum)
{
	if (txcallback[chnum] != NULL) {
		txcallback[chnum] (chnum);
	}
}

/*!
 * Tasklet used by the Interrupt service routine to handle the general
 * purpose interrupts. When an interrupt occurs, control from the top main
 * interrupt service routine is transferred to this tasklet which sends the
 * channel number to the call back function
 *
 * @param   chnum  index value indicating channel number
 *
 */
static void mxc_mu_gphandler(unsigned long chnum)
{
	if (gpcallback[chnum] != NULL) {
		gpcallback[chnum] (chnum);
	}
}

/*!
 * Interrupt service routine registered to handle the individual general purpose
 * interrupts or muxed. Interrupts are cleared in ISR before scheduling tasklet
 *
 * @param   irq     the interrupt number
 * @param   dev_id  driver private data
 *
 * @return          The function returns \b IRQ_RETVAL(1) if interrupt was
 *                  handled, returns \b IRQ_RETVAL(0) if the interrupt was
 *                  not handled.
 *                  \b IRQ_RETVAL is defined in \b include/linux/interrupt.h.
 */
static irqreturn_t mxc_mu_mcugphand(int irq, void *dev_id)
{
	int handled = 0;
	unsigned int sreg1, sreg2, status = 0;

	sreg1 = readl(AS_MUMSR);
	sreg2 = readl(AS_MUMCR);
	/* When General Purpose Interrupt occurs */
	if ((sreg1 & AS_MUMSR_MGIP0) && (sreg2 & AS_MUMCR_MGIE0)) {
		status |= (AS_MUMSR_MGIP0);
		tasklet_schedule(&gpch0tasklet);
		handled = 1;
	}
	if ((sreg1 & AS_MUMSR_MGIP1) && (sreg2 & AS_MUMCR_MGIE1)) {
		status |= (AS_MUMSR_MGIP1);
		tasklet_schedule(&gpch1tasklet);
		handled = 1;
	}
	if ((sreg1 & AS_MUMSR_MGIP2) && (sreg2 & AS_MUMCR_MGIE2)) {
		status |= (AS_MUMSR_MGIP2);
		tasklet_schedule(&gpch2tasklet);
		handled = 1;
	}
	if ((sreg1 & AS_MUMSR_MGIP3) && (sreg2 & AS_MUMCR_MGIE3)) {
		status |= (AS_MUMSR_MGIP3);
		tasklet_schedule(&gpch3tasklet);
		handled = 1;
	}
	writel(status, AS_MUMSR);
	return IRQ_RETVAL(handled);
}

/*!
 * Interrupt service routine registered to handle the muxed receive
 * interrupts. Interrupts disabled inside ISR before scheduling tasklet
 *
 * @param   irq     the interrupt number
 * @param   dev_id  driver private data
 *
 * @return          The function returns \b IRQ_RETVAL(1) if interrupt was
 *                  handled, returns \b IRQ_RETVAL(0) if the interrupt was
 *                  not handled.
 *                  \b IRQ_RETVAL is defined in \b include/linux/interrupt.h.
 */
static irqreturn_t mxc_mu_mcurxhand(int irq, void *dev_id)
{
	int handled = 0;
	unsigned int sreg1, sreg2, control;

	sreg1 = readl(AS_MUMSR);
	sreg2 = readl(AS_MUMCR);
	control = sreg2;
	/* When Receive Interrupt occurs */
	if ((sreg1 & AS_MUMSR_MRF0) && (sreg2 & AS_MUMCR_MRIE0)) {
		control &= ~(AS_MUMCR_MRIE0);
		tasklet_schedule(&rxch0tasklet);
		handled = 1;
	}
	if ((sreg1 & AS_MUMSR_MRF1) && (sreg2 & AS_MUMCR_MRIE1)) {
		control &= ~(AS_MUMCR_MRIE1);
		tasklet_schedule(&rxch1tasklet);
		handled = 1;
	}
	if ((sreg1 & AS_MUMSR_MRF2) && (sreg2 & AS_MUMCR_MRIE2)) {
		control &= ~(AS_MUMCR_MRIE2);
		tasklet_schedule(&rxch2tasklet);
		handled = 1;
	}
	if ((sreg1 & AS_MUMSR_MRF3) && (sreg2 & AS_MUMCR_MRIE3)) {
		control &= ~(AS_MUMCR_MRIE3);
		tasklet_schedule(&rxch3tasklet);
		handled = 1;
	}
	writel(control, AS_MUMCR);
	return IRQ_RETVAL(handled);
}

/*!
 * Interrupt service routine registered to handle the muxed transmit interrupts
 * Interrupts disabled inside ISR before scheduling tasklet
 *
 * @param   irq     the interrupt number
 * @param   dev_id  driver private data
 *
 * @return          The function returns \b IRQ_RETVAL(1) if interrupt was
 *                  handled, returns \b IRQ_RETVAL(0) if the interrupt was
 *                  not handled.
 *                  \b IRQ_RETVAL is defined in \b include/linux/interrupt.h.
 */
static irqreturn_t mxc_mu_mcutxhand(int irq, void *dev_id)
{
	int handled = 0;
	unsigned int sreg1, sreg2, control;

	sreg1 = readl(AS_MUMSR);
	sreg2 = readl(AS_MUMCR);
	control = sreg2;
	/* When Transmit Interrupt occurs */
	if ((sreg1 & AS_MUMSR_MTE0) && (sreg2 & AS_MUMCR_MTIE0)) {
		control &= ~(AS_MUMCR_MTIE0);
		tasklet_schedule(&txch0tasklet);
		handled = 1;
	}
	if ((sreg1 & AS_MUMSR_MTE1) && (sreg2 & AS_MUMCR_MTIE1)) {
		control &= ~(AS_MUMCR_MTIE1);
		tasklet_schedule(&txch1tasklet);
		handled = 1;
	}
	if ((sreg1 & AS_MUMSR_MTE2) && (sreg2 & AS_MUMCR_MTIE2)) {
		control &= ~(AS_MUMCR_MTIE2);
		tasklet_schedule(&txch2tasklet);
		handled = 1;
	}
	if ((sreg1 & AS_MUMSR_MTE3) && (sreg2 & AS_MUMCR_MTIE3)) {
		control &= ~(AS_MUMCR_MTIE3);
		tasklet_schedule(&txch3tasklet);
		handled = 1;
	}
	writel(control, AS_MUMCR);
	return IRQ_RETVAL(handled);
}

/*!
 * This function is used by other modules to issue DSP hardware reset.
 *
 */
int mxc_mu_dsp_reset(void)
{
	unsigned long flags;

	if (suspend_flag == 1) {
		return -EBUSY;
	}
	spin_lock_irqsave(&mu_lock, flags);
	writel((readl(AS_MUMCR) | AS_MUMCR_DHR), AS_MUMCR);
	spin_unlock_irqrestore(&mu_lock, flags);
	return 0;
}

/*!
 * This function is used by other modules to deassert DSP hardware reset
 */
int mxc_mu_dsp_deassert(void)
{
	unsigned long flags;

	if (suspend_flag == 1) {
		return -EBUSY;
	}
	spin_lock_irqsave(&mu_lock, flags);
	writel((readl(AS_MUMCR) & (~AS_MUMCR_DHR)), AS_MUMCR);
	spin_unlock_irqrestore(&mu_lock, flags);
	return 0;
}

/*!
 * This function is used by other modules to issue DSP Non-Maskable
 * Interrupt to the DSP
 */
int mxc_mu_dsp_nmi(void)
{
	unsigned long flags;

	if (suspend_flag == 1) {
		return -EBUSY;
	}
	spin_lock_irqsave(&mu_lock, flags);
	writel((readl(AS_MUMCR) | AS_MUMCR_DNMI), AS_MUMCR);
	spin_unlock_irqrestore(&mu_lock, flags);
	return 0;
}

/*!
 * This function is used by other modules to retrieve the DSP reset state.
 *
 * @return   Returns 1 if DSP in reset state and 0 otherwise
 */
int mxc_mu_dsp_reset_status(void)
{
	if (suspend_flag == 1) {
		return -EBUSY;
	}
	if ((readl(AS_MUMSR) & AS_MUMSR_DRS) != 0) {
		/* 1 implies the DSP side of MU is in reset state */
		return 1;
	} else {
		/* 0 implies the DSP side of MU is not in reset state */
		return 0;
	}
}

/*!
 * This function is used by other modules to retrieve the DSP Power Mode.
 *
 * @return   Returns a value from which power mode of the DSP side of
 *           of MU unit can be inferred
 *           0 - Run mode,  1 - Wait mode
 *           2 - Stop mode, 3 - DSM mode
 */
unsigned int mxc_mu_dsp_pmode_status(void)
{
	if (suspend_flag == 1) {
		return -EBUSY;
	}
	return (readl(AS_MUMSR) & (AS_MUMSR_DPM1 | AS_MUMSR_DPM0) >> 5);
}

/*!
 * This function is used by other modules to reset the MU Unit. This would reset
 * both the MCU side and the DSP side
 *
 */
int mxc_mu_reset(void)
{
	unsigned long flags;

	if (suspend_flag == 1) {
		return -EBUSY;
	}
	spin_lock_irqsave(&mu_lock, flags);
	writel((readl(AS_MUMCR) | AS_MUMCR_MMUR), AS_MUMCR);
	spin_unlock_irqrestore(&mu_lock, flags);
	return 0;
}

/*!
 * This function is called by unbind function of this module and
 * can also be called from other modules to enable desired
 * receive Interrupt
 *
 * @param   chand    The channel handler associated with the channel
 *                   whose interrupt to be disabled is passed
 * @param   muoper   The value passed is TX, RX, GP
 *
 * @return           Returns 0 on success or ENODEV error if invalid
 *                   channel is obtained from the channel handler
 *                   Returns -1 if muoper is other than RX, TX or GP
 */
int mxc_mu_intdisable(int chand, enum mu_oper muoper)
{
	unsigned int status;
	unsigned long flags;
	int chnum = chand - BASE_NUM;

	if (chnum < 0 || chnum > 3) {
		return -ENODEV;
	}
	if (suspend_flag == 1) {
		return -EBUSY;
	}
	spin_lock_irqsave(&mu_lock, flags);
	status = readl(AS_MUMCR);
	switch (muoper) {
	case RX:
		status &= ~(read_en[chnum]);
		break;
	case TX:
		status &= ~(write_en[chnum]);
		break;
	case GP:
		status &= ~(genp_en[chnum]);
		break;
	default:
		spin_unlock_irqrestore(&mu_lock, flags);
		return -1;
	}
	writel(status, AS_MUMCR);
	spin_unlock_irqrestore(&mu_lock, flags);
	return 0;
}

/*!
 * This function is called by other modules to unbind their
 * call back functions
 *
 * @param   chand    The channel handler associated with the channel
 *                   to be accessed is passed
 * @param   muoper   The value passed is TX, RX, GP
 *
 * @return           Returns 0 on success or ENODEV error if invalid
 *                   channel is obtained from the channel handler
 *                   Returns -1 if muoper is other than RX, TX or GP, or
 *                   if disabling interrupt failed
 */
int mxc_mu_unbind(int chand, enum mu_oper muoper)
{
	int result = 0;
	int chnum = chand - BASE_NUM;

	if (chnum < 0 || chnum > 3) {
		return -ENODEV;
	}
	if (suspend_flag == 1) {
		return -EBUSY;
	}
	if (reg_allocated[chnum] == chand) {
		if (mxc_mu_intdisable(chand, muoper) != 0) {
			return -1;
		}
		switch (muoper) {
		case RX:
			rxcallback[chnum] = NULL;
			break;
		case TX:
			txcallback[chnum] = NULL;
			break;
		case GP:
			gpcallback[chnum] = NULL;
			break;
		default:
			result = -1;
		}
	}
	return result;
}

/*!
 * This function is called by the user and other modules to enable desired
 * interrupt
 *
 * @param   chand     The channel handler associated with the channel
 *                    to be accessed is passed
 * @param    muoper   The value passed is TX, RX, GP
 *
 * @return           Returns 0 on success or ENODEV error if invalid
 *                   channel is obtained from the channel handler
 *                   Returns -1 if muoper is other than RX, TX or GP
 */
int mxc_mu_intenable(int chand, enum mu_oper muoper)
{
	unsigned long status = 0;
	unsigned long flags;
	int chnum = chand - BASE_NUM;

	if (chnum < 0 || chnum > 3) {
		return -ENODEV;
	}
	if (suspend_flag == 1) {
		return -EBUSY;
	}
	spin_lock_irqsave(&mu_lock, flags);
	status = readl(AS_MUMCR);
	switch (muoper) {
	case RX:
		status |= (read_en[chnum]);
		break;
	case TX:
		status |= (write_en[chnum]);
		break;
	case GP:
		status |= (genp_en[chnum]);
		break;
	default:
		spin_unlock_irqrestore(&mu_lock, flags);
		return -1;
	}
	writel(status, AS_MUMCR);
	spin_unlock_irqrestore(&mu_lock, flags);
	return 0;
}

/*!
 * This function is called by other modules to bind their
 * call back functions.
 *
 * @param   chand         The channel handler associated with the channel
 *                        to be accessed is passed
 * @param   callback      the caller's callback function
 * @param   muoper        The value passed is TX, RX, GP
 *
 * @return                Returns 0 on success or ENODEV error if invalid
 *                        channel is obtained from the channel handler
 *                        Returns -1 if muoper is other than RX, TX or GP
 */
int mxc_mu_bind(int chand, callbackfn callback, enum mu_oper muoper)
{

	int chnum = chand - BASE_NUM;

	if (chnum < 0 || chnum > 3) {
		return -ENODEV;
	}
	if (suspend_flag == 1) {
		return -EBUSY;
	}
	if (reg_allocated[chnum] == chand) {
		switch (muoper) {
		case RX:
			rxcallback[chnum] = callback;
			break;
		case TX:
			txcallback[chnum] = callback;
			break;
		case GP:
			gpcallback[chnum] = callback;
			break;
		default:
			return -1;
		}
	}
	return 0;
}

/*
 * This function is used to copy data from kernel space buffer
 * to user space buffer
 */
static int mcu_copytouser(int count, int minor, char *userbuff)
{
	int bytes_available = 0, actual_bytes = 0, checkval = 0;

	while (1) {
		bytes_available = CIRC_CNT_TO_END(mu_rbuf[minor].cbuf.head,
						  mu_rbuf[minor].cbuf.tail,
						  MAX_BUF_SIZE);
		/*
		 * If the number of bytes already in the buffer is greater than
		 * count then read count from buffer else read what is in the buffer
		 */
		if (count < bytes_available) {
			bytes_available = count;
		}
		if (bytes_available <= 0) {
			break;
		}
		checkval = copy_to_user(userbuff,
					&mu_rbuf[minor].cbuf.buf[mu_rbuf[minor].
								 cbuf.tail],
					bytes_available);
		if (checkval) {
			break;
		}
		mu_rbuf[minor].cbuf.tail = (mu_rbuf[minor].cbuf.tail +
					    bytes_available) & (MAX_BUF_SIZE -
								1);

		userbuff += bytes_available;
		count -= bytes_available;
		actual_bytes += bytes_available;
	}
	/*
	 * Return the number of bytes copied
	 */
	return actual_bytes;
}

/*
 * This function is used to copy data from user space buffer
 * to kernel space buffer
 */
static int mcu_copyfromuser(int count, int minor, const char *userbuff)
{
	int space_available = 0, bytes_copied = 0, checkval = 0;

	while (1) {
		space_available = CIRC_SPACE_TO_END(mu_wbuf[minor].cbuf.head,
						    mu_wbuf[minor].cbuf.tail,
						    MAX_BUF_SIZE);
		/*
		 * If the space available in the kernel space buffer
		 * is more than required i.e., count then number of bytes
		 * copied from the user space buffer is count else available empty
		 * space for transmission
		 */
		if (space_available >= count) {
			space_available = count;
		}
		if ((space_available % CH_SIZE) != 0) {
			space_available = space_available -
			    (space_available % CH_SIZE);
		}
		if (space_available < CH_SIZE) {
			break;
		}
		checkval =
		    copy_from_user(&mu_wbuf[minor].cbuf.
				   buf[mu_wbuf[minor].cbuf.head], userbuff,
				   space_available);
		if (checkval) {
			break;
		}
		mu_wbuf[minor].cbuf.head =
		    (mu_wbuf[minor].cbuf.head +
		     space_available) & (MAX_BUF_SIZE - 1);

		userbuff += space_available;
		count -= space_available;
		bytes_copied += space_available;
	}

	return bytes_copied;
}

/*!
 * This function allows the caller to determine whether it can
 * read from or write to one or more open files without blocking.
 *
 * @param    filp       Pointer to device file structure
 * @param    wait       used by poll_wait to indicate a change in the
 *                      poll status.
 *
 * @return              the bit mask describing which operations could
 *                      be completed immediately.
 */
static unsigned int mxc_mu_poll(struct file *filep, poll_table * wait)
{
	unsigned int minor;
	unsigned int mask = 0;

	minor = MINOR(filep->f_dentry->d_inode->i_rdev);

	poll_wait(filep, &writeq, wait);
	poll_wait(filep, &readq, wait);

	if (GET_READBYTES(minor)) {
		mask |= POLLIN | POLLRDNORM;
	}
	if (GET_WRITESPACE(minor) >= CH_SIZE) {
		mask |= POLLOUT | POLLWRNORM;
	}
	return mask;
}

/*!
 * The read function is available to the user-space to perform
 * a read operation
 *
 *  @param  filp        Pointer to device file structure
 *  @param  userbuff    User buffer, where read data to be placed.
 *  @param  count       Size of the requested data transfer
 *  @param  offp        File position where the user is accessing.
 *
 *  @return             Returns number of bytes read or
 *                      Returns -EINVAL if the number of bytes
 *                      requested is not a multiple of channel
 *                      receive or transmit size or
 *                      Returns -EAGAIN for a non-block read when no data
 *                      is ready in the buffer or in the register or
 *                      Returns -EFAULT if copy_to_user failed
 */
static ssize_t mxc_mu_read(struct file *filp,
			   char *userbuff, size_t count, loff_t * offp)
{
	int total_bytestocopy = 0;
	struct inode *inode = filp->f_dentry->d_inode;
	unsigned int minor = MINOR(inode->i_rdev);

	while (suspend_flag == 1) {
		swait++;
		/* Block if the device is suspended */
		if (wait_event_interruptible(suspendq, (suspend_flag == 0))) {
			return -ERESTARTSYS;
		}
	}
	/*
	 * If the count value requested is not multiple of channel size then
	 * return error
	 */
	if (((count % CH_SIZE) != 0) || (count == 0)) {
		return -EINVAL;	/* error */
	}
	total_bytestocopy = mcu_copytouser(count, minor, userbuff);
	/*
	 * Enable the interrupt to read from the register
	 */
	mxc_mu_intenable(reg_allocated[minor], RX);
	if (total_bytestocopy != 0) {
		return total_bytestocopy;
	}
	/*
	 * If there is no data in buffer and number of bytes
	 * copied to user space buffer is 0, then block until at least
	 * 4 bytes are read incase of block read else, return immediately
	 * incase of nonblock read
	 */
	if (!(filp->f_flags & O_NONBLOCK)) {
		while (GET_READBYTES(minor) == 0) {
			mu_rbuf[minor].pwait++;
			/* Block */
			if (wait_event_interruptible(readq,
						     (GET_READBYTES(minor) !=
						      0))) {
				return -ERESTARTSYS;
			}
		}
	} else {
		/* Return error for Non-Block read */
		return -EAGAIN;
	}
	/*
	 * Some data is now available in the register,
	 * read the data and copy to the user
	 */
	return (mcu_copytouser(count, minor, userbuff));
}

/*!
 * The write function is available to the user-space to perform
 * a write operation
 *
 *  @param  filp        Pointer to device file structure
 *  @param  userbuff    User buffer, where read data to be placed.
 *  @param  count       Size of the requested data transfer
 *  @param  offp        File position where the user is accessing.
 *
 *  @return             Returns number of bytes read or
 *                      Returns -EINVAL if the number of bytes
 *                      requested is not a multiple of channel
 *                      receive or transmit size or
 *                      Returns -EAGAIN for a non-block read when no data
 *                      is ready in the buffer or in the register or
 *                      Returns -EFAULT if copy_to_user failed
 */
static ssize_t mxc_mu_write(struct file *filp, const char *userbuff, size_t
			    count, loff_t * offp)
{
	int totalbytes_copied = 0;
	struct inode *inode = filp->f_dentry->d_inode;
	unsigned int minor = MINOR(inode->i_rdev);

	while (suspend_flag == 1) {
		swait++;
		/* Block if the device is suspended */
		if (wait_event_interruptible(suspendq, (suspend_flag == 0))) {
			return -ERESTARTSYS;
		}
	}
	/*
	 * If the count value requested is not multiple of channel size then
	 * return error
	 */
	if (((count % CH_SIZE) != 0) || (count == 0)) {
		return -EINVAL;	/* error */
	}
	totalbytes_copied = mcu_copyfromuser(count, minor, userbuff);
	/* Enable the interrupt before return so that data is transmitted */
	mxc_mu_intenable(reg_allocated[minor], TX);
	/*
	 * If number of bytes copied to the kernel space buffer is greater than
	 * 0, return immediately
	 */
	if (totalbytes_copied != 0) {
		return totalbytes_copied;
	}
	/* If there is no space in buffer and number of bytes
	 * copied from user space buffer is 0, then block until at least
	 * 4 bytes are written incase of block write else, return
	 * immediately incase of nonblock write
	 */
	if (!(filp->f_flags & O_NONBLOCK)) {
		while (GET_WRITESPACE(minor) < CH_SIZE) {
			mu_wbuf[minor].pwait++;
			if (wait_event_interruptible(writeq,
						     (GET_WRITESPACE(minor) >=
						      CH_SIZE))) {
				return -ERESTARTSYS;
			}
		}
	} else {
		/* Return error incase of non-block write */
		return -EAGAIN;
	}
	/*
	 * Some space is now available since some
	 * data has been transmitted. So, copy data from
	 * the user space buffer to kernel space buffer,
	 */
	return (mcu_copyfromuser(count, minor, userbuff));
}

/*!
 * The write callback function is used by this module to perform any
 * write requests from user-space
 *
 *  @param  chnum        channel number whose register has to be accessed
 */
static void mxc_mu_writecb(int chnum)
{
	char *message;

	/*
	 * While there are more bytes to transfer and
	 * register is empty, continue transmitting
	 */
	while ((GET_WRITEBYTES(chnum) != 0) && (readl(AS_MUMSR) & wbits[chnum])) {
		/* Maximum of 4 bytes everytime */
		message = &mu_wbuf[chnum].cbuf.buf[mu_wbuf[chnum].cbuf.tail];
		mu_wbuf[chnum].cbuf.tail =
		    (mu_wbuf[chnum].cbuf.tail + CH_SIZE) & (MAX_BUF_SIZE - 1);
		mxc_mu_mcuwrite(reg_allocated[chnum], message);
		/* Emptying write buffer */
	}
	/*
	 * Enable the interrupt if more data has to be transmitted.
	 * Since interrupts are disabled inside ISR.
	 */
	if (GET_WRITEBYTES(chnum) != 0) {
		mxc_mu_intenable(reg_allocated[chnum], TX);
	}
	/* Wake up the sleeping process if the buffer gets emptied */
	if (blockmode[chnum] == 0 && mu_wbuf[chnum].pwait > 0 &&
	    GET_WRITESPACE(chnum) >= CH_SIZE) {
		mu_wbuf[chnum].pwait--;
		/* Wake up */
		wake_up_interruptible(&writeq);
	}
}

/*!
 * The read callback function is used by this module to perform any
 * read requests from user-space
 *
 *  @param  chnum        channel number whose register has to be accessed
 */
static void mxc_mu_readcb(int chnum)
{
	char message[CH_SIZE];
	int index;

	/*
	 * While there more bytes can be read and if
	 * buffer is empty
	 */
	while ((GET_READSPACE(chnum) >= CH_SIZE)
	       && (readl(AS_MUMSR) & rbits[chnum])) {
		mxc_mu_mcuread(reg_allocated[chnum], message);
		index = mu_rbuf[chnum].cbuf.head;
		*(int *)(&(mu_rbuf[chnum].cbuf.buf[index])) = *(int *)message;
		mu_rbuf[chnum].cbuf.head =
		    (mu_rbuf[chnum].cbuf.head + CH_SIZE) & (MAX_BUF_SIZE - 1);
	}
	/*
	 * If no empty space in buffer to store the data that
	 * has been read, then disable the interrupt else
	 * enable the interrupt
	 */
	(GET_READSPACE(chnum) <
	 CH_SIZE) ? mxc_mu_intdisable(reg_allocated[chnum],
				      RX) :
mxc_mu_intenable(reg_allocated[chnum], RX);
	/* Wake up the sleeping process if data is available */
	if (blockmode[chnum] == 0 && mu_rbuf[chnum].pwait > 0 &&
	    GET_READBYTES(chnum) != 0) {
		mu_rbuf[chnum].pwait--;
		/* Wake up */
		wake_up_interruptible(&readq);
	}
}

/*!
 * The general purpose callback function is used by this module to
 * perform any general purpose interrupt requests from user-space
 *
 *  @param  chnum        channel number whose register has to be accessed
 */
static void mxc_mu_genpurposecb(int chnum)
{
	writel((readl(AS_MUMSR) | (genp_pend[chnum])), AS_MUMSR);
}

/*!
 * This function is called when an ioctl call is made from user space.
 *
 * @param    inode    Pointer to device inode
 * @param    file     Pointer to device file structure
 * @param    cmd      Ioctl command
 * @param    arg      Ioctl argument
 *
 * @return    The function returns 0 on success and -EINVAL on
 *            failure when cmd is other than what is specified
 */
int mxc_mu_ioctl(struct inode *inode, struct file *file,
		 unsigned int cmd, unsigned long arg)
{
	struct inode *i_node = file->f_dentry->d_inode;
	unsigned int minor = MINOR(i_node->i_rdev);
	int i = 0;

	while (suspend_flag == 1) {
		swait++;
		/* Block if the device is suspended */
		if (wait_event_interruptible(suspendq, (suspend_flag == 0))) {
			return -ERESTARTSYS;
		}
	}
	switch (cmd) {
	case SENDINT:
		writel(genp_req[minor], AS_MUMCR);
		break;
	case SENDNMI:
		mxc_mu_dsp_nmi();
		break;
	case SENDDSPRESET:
		mxc_mu_dsp_reset();
		for (i = 0; i < 20; i++) ;
		mxc_mu_dsp_deassert();
		while (mxc_mu_dsp_reset_status() != 0) ;
		break;
	case SENDMURESET:
		mxc_mu_reset();
		break;
	case RXENABLE:
		mxc_mu_intenable(reg_allocated[minor], RX);
		break;
	case TXENABLE:
		mxc_mu_intenable(reg_allocated[minor], TX);
		break;
	case GPENABLE:
		mxc_mu_intenable(reg_allocated[minor], GP);
		break;
	case BYTESWAP_RD:
		byte_swapping_rd[minor] = true;
		break;
	case BYTESWAP_WR:
		byte_swapping_wr[minor] = true;
		break;
	default:
		return -EINVAL;
	}
	return 0;
}

/*!
 * This function is called to close the device. The MCU side of MU interrupts
 * are disabled. Any allocated buffers are freed
 *
 * @param    inode    Pointer to device inode
 * @param    filp     Pointer to device file structure
 *
 * @return    The function returns 0 on success or
 *            returns -ENODEV if incorrect channel is accessed
 *
 */
static int mxc_mu_close(struct inode *inode, struct file *filp)
{
	unsigned int minor = MINOR(inode->i_rdev);

	while (suspend_flag == 1) {
		swait++;
		/* Block if the device is suspended */
		if (wait_event_interruptible(suspendq, (suspend_flag == 0))) {
			return -ERESTARTSYS;
		}
	}
	/* Wait to complete any pending writes */
	/* Block until the write buffer is empty */
	while (GET_WRITEBYTES(minor) != 0) {
		if (wait_event_interruptible_timeout
		    (wait_before_closeq, (GET_WRITEBYTES(minor) == 0),
		     MAX_SCHEDULE_TIMEOUT)) {
			return -ERESTARTSYS;
		}
	}
	if (mxc_mu_dealloc_channel(reg_allocated[minor]) != 0) {
		return -ENODEV;
	}
	kfree(mu_rbuf[minor].cbuf.buf);
	return 0;
}

/*!
 * This function is called when the MU driver is opened. This function does
 * the initialization of the device.
 *
 * @param    inode    Pointer to device inode
 * @param    filp     Pointer to device file structure
 *
 * @return    The function returns 0 on success or a negative value if
 *            channel was not allocated or if kmalloc failed
 */
static int mxc_mu_open(struct inode *inode, struct file *filp)
{
	int chand;
	unsigned int minor = MINOR(inode->i_rdev);

	while (suspend_flag == 1) {
		swait++;
		/* Block if the device is suspended */
		if (wait_event_interruptible(suspendq, (suspend_flag == 0))) {
			return -ERESTARTSYS;
		}
	}
	chand = mxc_mu_alloc_channel(minor);
	if (chand < 0) {
		return chand;
	}
	mxc_mu_bind(reg_allocated[minor], &mxc_mu_readcb, RX);
	mxc_mu_bind(reg_allocated[minor], &mxc_mu_writecb, TX);
	mxc_mu_bind(reg_allocated[minor], &mxc_mu_genpurposecb, GP);
	mxc_mu_intdisable(reg_allocated[minor], TX);
	/*
	 * Read buffer from 0 to MAX_BUF_SIZE
	 * Write buffer from MAX_BUF_SIZE to 2*MAX_BUF_SIZE
	 */
	mu_rbuf[minor].cbuf.buf = kmalloc((2 * MAX_BUF_SIZE), GFP_KERNEL);
	mu_wbuf[minor].cbuf.buf = mu_rbuf[minor].cbuf.buf + MAX_BUF_SIZE;
	if (mu_rbuf[minor].cbuf.buf == NULL) {
		reg_allocated[minor] = -1;
		return -1;
	}
	mu_rbuf[minor].cbuf.tail = mu_rbuf[minor].cbuf.head = 0;
	mu_wbuf[minor].cbuf.tail = mu_wbuf[minor].cbuf.head = 0;

	byte_swapping_rd[minor] = false;
	byte_swapping_wr[minor] = false;

	mu_rbuf[minor].pwait = mu_rbuf[minor].pwait = 0;
	if (filp->f_flags & O_NONBLOCK) {
		blockmode[minor] = 1;
	} else {
		blockmode[minor] = 0;
	}
	mxc_mu_intenable(reg_allocated[minor], RX);
	return 0;
}

/*!
 * This function is called to put the MU in a low power state.
 *
 * @param   pdev  the device structure used to give information on which MU
 *                device (0 through 3 channels) to suspend
 * @param   state the power state the device is entering
 *
 * @return  The function always returns 0.
 */
static int mxc_mu_suspend(struct platform_device *pdev, pm_message_t state)
{
	unsigned long flags;

	suspend_flag = 1;
	spin_lock_irqsave(&mu_lock, flags);
	mcr_suspend_state = readl(AS_MUMCR);
	/* Disabling all receive, transmit, gen. purpose interrupts */
	writel((mcr_suspend_state & 0x0000FFFF), AS_MUMCR);
	spin_unlock_irqrestore(&mu_lock, flags);

	/* Turn off clock */
	clk_disable(mu_clkp);

	return 0;
}

/*!
 * This function is called to resume the MU from a low power state.
 *
 * @param   dev   the device structure used to give information on which MU
 *                device (0 through 3 channels) to suspend
 * @param   level the stage in device suspension process that we want the
 *                device to be put in
 *
 * @return  The function always returns 0.
 */
static int mxc_mu_resume(struct platform_device *pdev)
{
	unsigned long flags;

	/* Turn on clock */
	clk_enable(mu_clkp);

	spin_lock_irqsave(&mu_lock, flags);
	/* Re-enable interrupts or restore state of interrupts */
	writel(mcr_suspend_state, AS_MUMCR);
	while (swait > 0) {
		swait--;
		wake_up_interruptible(&suspendq);
	}
	spin_unlock_irqrestore(&mu_lock, flags);
	suspend_flag = 0;

	return 0;
}

static struct file_operations mu_fops = {
	.owner = THIS_MODULE,
	.read = mxc_mu_read,
	.write = mxc_mu_write,
	.ioctl = mxc_mu_ioctl,
	.open = mxc_mu_open,
	.release = mxc_mu_close,
	.poll = mxc_mu_poll,
};

/*!
 * This function is called while loading the module to initiate message
 * transfers. This function initializes all the registers.
 */
static void mxc_mu_load_mod(void)
{
	/* Initializing wait queues */
	init_waitqueue_head(&readq);
	init_waitqueue_head(&writeq);
	init_waitqueue_head(&suspendq);
	init_waitqueue_head(&wait_before_closeq);
	/* Setting the status and control registers to default values */
	writel(0xffff, AS_MUMSR);
	writel(0, AS_MUMCR);
}

/*!
 * This function is called while unloading driver.
 * This resets all the registers and disables
 * interrupts.
 */
static void mxc_mu_unload_mod(void)
{
	/* setting the status and control registers to default values */
	writel(0xffff, AS_MUMSR);
	writel(0, AS_MUMCR);
}

/*!
 * This function is used to load the module and all the interrupt lines
 * are requested.
 *
 * @return   Returns an Integer on success
 */
static int __init mxc_mu_probe(struct platform_device *pdev)
{
	/* Initializing resources allocated */
	int rx_irq, tx_irq, irq;
	int max_res;
	int i;
	struct class_device *temp_class;

	max_res = pdev->num_resources;
	rx_irq = platform_get_irq(pdev, 0);
	tx_irq = platform_get_irq(pdev, 1);
	if (rx_irq == NO_IRQ || tx_irq == NO_IRQ)
		return -ENXIO;

	mu_clkp = clk_get(&pdev->dev, "mu_clk");
	if (IS_ERR(mu_clkp))
		return -ENXIO;
	clk_enable(mu_clkp);

	if ((mu_major = register_chrdev(0, "mxc_mu", &mu_fops)) < 0) {
		printk(KERN_NOTICE
		       "Can't allocate major number for MU Devices.\n");
		return -EAGAIN;
	}

	mxc_mu_class = class_create(THIS_MODULE, "mxc_mu");

	if (IS_ERR(mxc_mu_class)) {
		printk(KERN_ERR "Error creating mu class.\n");
		unregister_chrdev(mu_major, "mxc_mu");
		return PTR_ERR(mxc_mu_class);
	}

	mxc_mu_load_mod();

	for (i = 0; i <= 3; i++) {
		temp_class =
		    class_device_create(mxc_mu_class, NULL, MKDEV(mu_major, i),
					NULL, "mxc_mu%u", i);

		if (IS_ERR(temp_class))
			goto err_out1;
	}

	if (request_irq(rx_irq, mxc_mu_mcurxhand, 0, "mxc_mu_rx", NULL) != 0)
		goto err_out1;

	if (request_irq(tx_irq, mxc_mu_mcutxhand, 0, "mxc_mu_tx", NULL) != 0)
		goto err_out2;

	for (i = 2; i < max_res; i++) {
		irq = platform_get_irq(pdev, i);
		if (irq == NO_IRQ)
			goto err_out3;

		if (request_irq(irq, mxc_mu_mcugphand, 0, "mxc_mu_mcug", NULL)
		    != 0) {
			printk(KERN_ERR "mxc_mu: request_irq for %d failed\n",
			       irq);
			goto err_out3;
		}
	}

	return 0;

      err_out3:
	for (--i; i >= 2; i--) {
		irq = platform_get_irq(pdev, i);
		free_irq(irq, NULL);
	}
	free_irq(tx_irq, NULL);

      err_out2:
	free_irq(rx_irq, NULL);

      err_out1:
	for (i = 0; i <= 3; i++) {
		class_device_destroy(mxc_mu_class, MKDEV(mu_major, i));
	}
	mxc_mu_unload_mod();
	class_destroy(mxc_mu_class);
	unregister_chrdev(mu_major, "mxc_mu");

	return -1;
}

/*!
 * This function is used to unload the module and all interrupt lines are freed
 */
static void __exit mxc_mu_remove(struct platform_device *pdev)
{
	int irq, max_res, i;
	max_res = pdev->num_resources;

	/* Freeing the resources allocated */
	mxc_mu_unload_mod();
	clk_disable(mu_clkp);
	clk_put(mu_clkp);
	for (i = 0; i < max_res; i++) {
		irq = platform_get_irq(pdev, i);
		free_irq(irq, NULL);
	}
	for (i = 0; i <= 3; i++) {
		class_device_destroy(mxc_mu_class, MKDEV(mu_major, i));
	}
	class_destroy(mxc_mu_class);
	unregister_chrdev(mu_major, "mxc_mu");

}

/*!
 * This structure contains pointers to the power management callback functions.
 */
static struct platform_driver mxc_mu_driver = {
	.driver = {
		   .name = "mxc_mu",
		   },
	.probe = mxc_mu_probe,
	.remove = __exit_p(mxc_mu_remove),
	.suspend = mxc_mu_suspend,
	.resume = mxc_mu_resume,
};

/*
 * Main initialization routine
 */
static int __init mxc_mu_init(void)
{
	/* Register the device driver structure. */
	pr_info("MXC MU Driver %s\n", DVR_VER);
	if (platform_driver_register(&mxc_mu_driver) != 0) {
		printk(KERN_ERR "Driver register failed for mxc_mu_driver\n");
		return -ENODEV;
	}
	return 0;
}

/*
 * Clean up routine
 */
static void __exit mxc_mu_cleanup(void)
{
	/* Unregister the device structure */
	platform_driver_unregister(&mxc_mu_driver);
	printk(KERN_INFO "MU Driver Module Unloaded\n");
}

module_init(mxc_mu_init);
module_exit(mxc_mu_cleanup);

EXPORT_SYMBOL(mxc_mu_bind);
EXPORT_SYMBOL(mxc_mu_unbind);
EXPORT_SYMBOL(mxc_mu_alloc_channel);
EXPORT_SYMBOL(mxc_mu_dealloc_channel);
EXPORT_SYMBOL(mxc_mu_mcuread);
EXPORT_SYMBOL(mxc_mu_mcuwrite);
EXPORT_SYMBOL(mxc_mu_intenable);
EXPORT_SYMBOL(mxc_mu_intdisable);
EXPORT_SYMBOL(mxc_mu_reset);
EXPORT_SYMBOL(mxc_mu_dsp_reset);
EXPORT_SYMBOL(mxc_mu_dsp_deassert);
EXPORT_SYMBOL(mxc_mu_dsp_nmi);
EXPORT_SYMBOL(mxc_mu_dsp_reset_status);
EXPORT_SYMBOL(mxc_mu_dsp_pmode_status);

MODULE_AUTHOR("Freescale Semiconductor, Inc.");
MODULE_DESCRIPTION("Messaging Unit Driver");
MODULE_LICENSE("GPL");