]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/staging/wilc1000/linux_wlan_spi.c
staging: wilc1000: remove unnecessary typecast
[karo-tx-linux.git] / drivers / staging / wilc1000 / linux_wlan_spi.c
1 #include <linux/module.h>
2 #include <linux/init.h>
3 #include <linux/kernel.h>
4 #include <linux/fs.h>
5 #include <linux/slab.h>
6 #include <linux/types.h>
7 #include <linux/cdev.h>
8 #include <asm/uaccess.h>
9 #include <linux/device.h>
10 #include <linux/spi/spi.h>
11
12 #include "linux_wlan_common.h"
13
14 #define USE_SPI_DMA     0       /* johnny add */
15
16 #ifdef WILC_ASIC_A0
17  #if defined(PLAT_PANDA_ES_OMAP4460)
18   #define MIN_SPEED 12000000
19   #define MAX_SPEED 24000000
20  #elif defined(PLAT_WMS8304)
21   #define MIN_SPEED 12000000
22   #define MAX_SPEED 24000000 /* 4000000 */
23  #elif defined(CUSTOMER_PLATFORM)
24 /*
25   TODO : define Clock speed under 48M.
26  *
27  * ex)
28  * #define MIN_SPEED 24000000
29  * #define MAX_SPEED 48000000
30  */
31  #else
32   #define MIN_SPEED 24000000
33   #define MAX_SPEED 48000000
34  #endif
35 #else /* WILC_ASIC_A0 */
36 /* Limit clk to 6MHz on FPGA. */
37  #define MIN_SPEED 6000000
38  #define MAX_SPEED 6000000
39 #endif /* WILC_ASIC_A0 */
40
41 static uint32_t SPEED = MIN_SPEED;
42
43 struct spi_device *wilc_spi_dev;
44 void linux_spi_deinit(void *vp);
45
46 static int __init wilc_bus_probe(struct spi_device *spi)
47 {
48
49         PRINT_D(BUS_DBG, "spiModalias: %s\n", spi->modalias);
50         PRINT_D(BUS_DBG, "spiMax-Speed: %d\n", spi->max_speed_hz);
51         wilc_spi_dev = spi;
52
53         printk("Driver Initializing success\n");
54         return 0;
55 }
56
57 static int __exit wilc_bus_remove(struct spi_device *spi)
58 {
59
60         /* linux_spi_deinit(NULL); */
61
62         return 0;
63 }
64
65 #ifdef CONFIG_OF
66 static const struct of_device_id wilc1000_of_match[] = {
67         { .compatible = "atmel,wilc_spi", },
68         {}
69 };
70 MODULE_DEVICE_TABLE(of, wilc1000_of_match);
71 #endif
72
73 struct spi_driver wilc_bus __refdata = {
74         .driver = {
75                 .name = MODALIAS,
76 #ifdef CONFIG_OF
77                 .of_match_table = wilc1000_of_match,
78 #endif
79         },
80         .probe =  wilc_bus_probe,
81         .remove = __exit_p(wilc_bus_remove),
82 };
83
84
85 void linux_spi_deinit(void *vp)
86 {
87
88         spi_unregister_driver(&wilc_bus);
89
90         SPEED = MIN_SPEED;
91         PRINT_ER("@@@@@@@@@@@@ restore SPI speed to %d @@@@@@@@@\n", SPEED);
92
93 }
94
95
96
97 int linux_spi_init(void *vp)
98 {
99         int ret = 1;
100         static int called;
101
102
103         if (called == 0) {
104                 called++;
105                 ret = spi_register_driver(&wilc_bus);
106         }
107
108         /* change return value to match WILC interface */
109         (ret < 0) ? (ret = 0) : (ret = 1);
110
111         return ret;
112 }
113
114 #if defined(PLAT_WMS8304)
115 #define TXRX_PHASE_SIZE (4096)
116 #endif
117
118 #if defined (NM73131_0_BOARD)
119
120 int linux_spi_write(uint8_t *b, uint32_t len)
121 {
122
123         int ret;
124
125         if (len > 0 && b != NULL) {
126                 struct spi_message msg;
127                 PRINT_D(BUS_DBG, "Request writing %d bytes\n", len);
128                 struct spi_transfer tr = {
129                         .tx_buf = b,
130                         .len = len,
131                         .speed_hz = SPEED,
132                         .delay_usecs = 0,
133                 };
134
135                 spi_message_init(&msg);
136                 spi_message_add_tail(&tr, &msg);
137                 ret = spi_sync(wilc_spi_dev, &msg);
138                 if (ret < 0) {
139                         PRINT_ER("SPI transaction failed\n");
140                 }
141
142         } else {
143                 PRINT_ER("can't write data with the following length: %d\n", len);
144                 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
145                 ret = -1;
146         }
147
148         /* change return value to match WILC interface */
149         (ret < 0) ? (ret = 0) : (ret = 1);
150
151
152         return ret;
153 }
154
155 #elif defined(TXRX_PHASE_SIZE)
156
157 int linux_spi_write(uint8_t *b, uint32_t len)
158 {
159         int ret;
160         if (len > 0 && b != NULL) {
161                 int i = 0;
162                 int blk = len / TXRX_PHASE_SIZE;
163                 int remainder = len % TXRX_PHASE_SIZE;
164
165                 char *r_buffer = kzalloc(TXRX_PHASE_SIZE, GFP_KERNEL);
166                 if (!r_buffer) {
167                         PRINT_ER("Failed to allocate memory for r_buffer\n");
168                 }
169
170                 if (blk) {
171                         while (i < blk) {
172                                 struct spi_message msg;
173                                 struct spi_transfer tr = {
174                                         .tx_buf = b + (i * TXRX_PHASE_SIZE),
175                                         /* .rx_buf = NULL, */
176                                         .len = TXRX_PHASE_SIZE,
177                                         .speed_hz = SPEED,
178                                         .bits_per_word = 8,
179                                         .delay_usecs = 0,
180                                 };
181                                 /*
182                                  * char *r_buffer = (char*) kzalloc(TXRX_PHASE_SIZE, GFP_KERNEL);
183                                  * if(! r_buffer){
184                                  *      PRINT_ER("Failed to allocate memory for r_buffer\n");
185                                  * }
186                                  */
187                                 tr.rx_buf = r_buffer;
188
189                                 memset(&msg, 0, sizeof(msg));
190                                 spi_message_init(&msg);
191                                 msg.spi = wilc_spi_dev;
192                                 msg.is_dma_mapped = USE_SPI_DMA;
193
194                                 spi_message_add_tail(&tr, &msg);
195                                 ret = spi_sync(wilc_spi_dev, &msg);
196                                 if (ret < 0) {
197                                         PRINT_ER("SPI transaction failed\n");
198                                 }
199                                 /* i += MJ_WRITE_SIZE; */
200                                 i++;
201
202                         }
203                 }
204                 if (remainder) {
205                         struct spi_message msg;
206                         struct spi_transfer tr = {
207                                 .tx_buf = b + (blk * TXRX_PHASE_SIZE),
208                                 /* .rx_buf = NULL, */
209                                 .len = remainder,
210                                 .speed_hz = SPEED,
211                                 .bits_per_word = 8,
212                                 .delay_usecs = 0,
213                         };
214                         /*
215                          * char *r_buffer = (char*) kzalloc(remainder, GFP_KERNEL);
216                          * if(! r_buffer){
217                          *      PRINT_ER("Failed to allocate memory for r_buffer\n");
218                          * }
219                          */
220                         tr.rx_buf = r_buffer;
221
222                         memset(&msg, 0, sizeof(msg));
223                         spi_message_init(&msg);
224                         msg.spi = wilc_spi_dev;
225                         msg.is_dma_mapped = USE_SPI_DMA;                                /* rachel */
226
227                         spi_message_add_tail(&tr, &msg);
228                         ret = spi_sync(wilc_spi_dev, &msg);
229                         if (ret < 0) {
230                                 PRINT_ER("SPI transaction failed\n");
231                         }
232                 }
233                 if (r_buffer)
234                         kfree(r_buffer);
235         } else {
236                 PRINT_ER("can't write data with the following length: %d\n", len);
237                 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
238                 ret = -1;
239         }
240
241         /* change return value to match WILC interface */
242         (ret < 0) ? (ret = 0) : (ret = 1);
243
244         return ret;
245
246 }
247
248 #else
249 int linux_spi_write(uint8_t *b, uint32_t len)
250 {
251
252         int ret;
253         struct spi_message msg;
254
255         if (len > 0 && b != NULL) {
256                 struct spi_transfer tr = {
257                         .tx_buf = b,
258                         /* .rx_buf = r_buffer, */
259                         .len = len,
260                         .speed_hz = SPEED,
261                         .delay_usecs = 0,
262                 };
263                 char *r_buffer = kzalloc(len, GFP_KERNEL);
264                 if (!r_buffer) {
265                         PRINT_ER("Failed to allocate memory for r_buffer\n");
266                 }
267                 tr.rx_buf = r_buffer;
268                 PRINT_D(BUS_DBG, "Request writing %d bytes\n", len);
269
270                 memset(&msg, 0, sizeof(msg));
271                 spi_message_init(&msg);
272 /* [[johnny add */
273                 msg.spi = wilc_spi_dev;
274                 msg.is_dma_mapped = USE_SPI_DMA;
275 /* ]] */
276                 spi_message_add_tail(&tr, &msg);
277
278                 ret = spi_sync(wilc_spi_dev, &msg);
279                 if (ret < 0) {
280                         PRINT_ER("SPI transaction failed\n");
281                 }
282
283                 kfree(r_buffer);
284         } else {
285                 PRINT_ER("can't write data with the following length: %d\n", len);
286                 PRINT_ER("FAILED due to NULL buffer or ZERO length check the following length: %d\n", len);
287                 ret = -1;
288         }
289
290         /* change return value to match WILC interface */
291         (ret < 0) ? (ret = 0) : (ret = 1);
292
293
294         return ret;
295 }
296
297 #endif
298
299 #if defined (NM73131_0_BOARD)
300
301 int linux_spi_read(unsigned char *rb, unsigned long rlen)
302 {
303
304         int ret;
305
306         if (rlen > 0) {
307                 struct spi_message msg;
308                 struct spi_transfer tr = {
309                         .rx_buf = rb,
310                         .len = rlen,
311                         .speed_hz = SPEED,
312                         .delay_usecs = 0,
313
314                 };
315
316                 spi_message_init(&msg);
317                 spi_message_add_tail(&tr, &msg);
318                 ret = spi_sync(wilc_spi_dev, &msg);
319                 if (ret < 0) {
320                         PRINT_ER("SPI transaction failed\n");
321                 }
322         } else {
323                 PRINT_ER("can't read data with the following length: %ld\n", rlen);
324                 ret = -1;
325         }
326         /* change return value to match WILC interface */
327         (ret < 0) ? (ret = 0) : (ret = 1);
328
329         return ret;
330 }
331
332 #elif defined(TXRX_PHASE_SIZE)
333
334 int linux_spi_read(unsigned char *rb, unsigned long rlen)
335 {
336         int ret;
337
338         if (rlen > 0) {
339                 int i = 0;
340
341                 int blk = rlen / TXRX_PHASE_SIZE;
342                 int remainder = rlen % TXRX_PHASE_SIZE;
343
344                 char *t_buffer = kzalloc(TXRX_PHASE_SIZE, GFP_KERNEL);
345                 if (!t_buffer) {
346                         PRINT_ER("Failed to allocate memory for t_buffer\n");
347                 }
348
349                 if (blk) {
350                         while (i < blk) {
351                                 struct spi_message msg;
352                                 struct spi_transfer tr = {
353                                         /* .tx_buf = NULL, */
354                                         .rx_buf = rb + (i * TXRX_PHASE_SIZE),
355                                         .len = TXRX_PHASE_SIZE,
356                                         .speed_hz = SPEED,
357                                         .bits_per_word = 8,
358                                         .delay_usecs = 0,
359                                 };
360                                 tr.tx_buf = t_buffer;
361
362                                 memset(&msg, 0, sizeof(msg));
363                                 spi_message_init(&msg);
364                                 msg.spi = wilc_spi_dev;
365                                 msg.is_dma_mapped = USE_SPI_DMA;
366
367                                 spi_message_add_tail(&tr, &msg);
368                                 ret = spi_sync(wilc_spi_dev, &msg);
369                                 if (ret < 0) {
370                                         PRINT_ER("SPI transaction failed\n");
371                                 }
372                                 i++;
373                         }
374                 }
375                 if (remainder) {
376                         struct spi_message msg;
377                         struct spi_transfer tr = {
378                                 /* .tx_buf = NULL, */
379                                 .rx_buf = rb + (blk * TXRX_PHASE_SIZE),
380                                 .len = remainder,
381                                 .speed_hz = SPEED,
382                                 .bits_per_word = 8,
383                                 .delay_usecs = 0,
384                         };
385                         /*
386                          * char *t_buffer = (char*) kzalloc(remainder, GFP_KERNEL);
387                          * if(! t_buffer){
388                          *      PRINT_ER("Failed to allocate memory for t_buffer\n");
389                          * }
390                          */
391                         tr.tx_buf = t_buffer;
392
393                         memset(&msg, 0, sizeof(msg));
394                         spi_message_init(&msg);
395                         msg.spi = wilc_spi_dev;
396                         msg.is_dma_mapped = USE_SPI_DMA;                                /* rachel */
397
398                         spi_message_add_tail(&tr, &msg);
399                         ret = spi_sync(wilc_spi_dev, &msg);
400                         if (ret < 0) {
401                                 PRINT_ER("SPI transaction failed\n");
402                         }
403                 }
404
405                 if (t_buffer)
406                         kfree(t_buffer);
407         } else {
408                 PRINT_ER("can't read data with the following length: %ld\n", rlen);
409                 ret = -1;
410         }
411         /* change return value to match WILC interface */
412         (ret < 0) ? (ret = 0) : (ret = 1);
413
414         return ret;
415 }
416
417 #else
418 int linux_spi_read(unsigned char *rb, unsigned long rlen)
419 {
420
421         int ret;
422
423         if (rlen > 0) {
424                 struct spi_message msg;
425                 struct spi_transfer tr = {
426                         /*              .tx_buf = t_buffer, */
427                         .rx_buf = rb,
428                         .len = rlen,
429                         .speed_hz = SPEED,
430                         .delay_usecs = 0,
431
432                 };
433                 char *t_buffer = kzalloc(rlen, GFP_KERNEL);
434                 if (!t_buffer) {
435                         PRINT_ER("Failed to allocate memory for t_buffer\n");
436                 }
437                 tr.tx_buf = t_buffer;
438
439                 memset(&msg, 0, sizeof(msg));
440                 spi_message_init(&msg);
441 /* [[ johnny add */
442                 msg.spi = wilc_spi_dev;
443                 msg.is_dma_mapped = USE_SPI_DMA;
444 /* ]] */
445                 spi_message_add_tail(&tr, &msg);
446
447                 ret = spi_sync(wilc_spi_dev, &msg);
448                 if (ret < 0) {
449                         PRINT_ER("SPI transaction failed\n");
450                 }
451                 kfree(t_buffer);
452         } else {
453                 PRINT_ER("can't read data with the following length: %ld\n", rlen);
454                 ret = -1;
455         }
456         /* change return value to match WILC interface */
457         (ret < 0) ? (ret = 0) : (ret = 1);
458
459         return ret;
460 }
461
462 #endif
463
464 int linux_spi_write_read(unsigned char *wb, unsigned char *rb, unsigned int rlen)
465 {
466
467         int ret;
468
469         if (rlen > 0) {
470                 struct spi_message msg;
471                 struct spi_transfer tr = {
472                         .rx_buf = rb,
473                         .tx_buf = wb,
474                         .len = rlen,
475                         .speed_hz = SPEED,
476                         .bits_per_word = 8,
477                         .delay_usecs = 0,
478
479                 };
480
481                 memset(&msg, 0, sizeof(msg));
482                 spi_message_init(&msg);
483                 msg.spi = wilc_spi_dev;
484                 msg.is_dma_mapped = USE_SPI_DMA;
485
486                 spi_message_add_tail(&tr, &msg);
487                 ret = spi_sync(wilc_spi_dev, &msg);
488                 if (ret < 0) {
489                         PRINT_ER("SPI transaction failed\n");
490                 }
491         } else {
492                 PRINT_ER("can't read data with the following length: %d\n", rlen);
493                 ret = -1;
494         }
495         /* change return value to match WILC interface */
496         (ret < 0) ? (ret = 0) : (ret = 1);
497
498         return ret;
499 }
500
501 int linux_spi_set_max_speed(void)
502 {
503         SPEED = MAX_SPEED;
504
505         PRINT_INFO(BUS_DBG, "@@@@@@@@@@@@ change SPI speed to %d @@@@@@@@@\n", SPEED);
506         return 1;
507 }