2 * SPI bus via the Blackfin SPORT peripheral
4 * Enter bugs at http://blackfin.uclinux.org/
6 * Copyright 2009-2011 Analog Devices Inc.
8 * Licensed under the GPL-2 or later.
11 #include <linux/module.h>
12 #include <linux/delay.h>
13 #include <linux/device.h>
14 #include <linux/gpio.h>
16 #include <linux/ioport.h>
17 #include <linux/irq.h>
18 #include <linux/errno.h>
19 #include <linux/interrupt.h>
20 #include <linux/platform_device.h>
21 #include <linux/spi/spi.h>
22 #include <linux/workqueue.h>
24 #include <asm/portmux.h>
25 #include <asm/bfin5xx_spi.h>
26 #include <asm/blackfin.h>
27 #include <asm/bfin_sport.h>
28 #include <asm/cacheflush.h>
30 #define DRV_NAME "bfin-sport-spi"
31 #define DRV_DESC "SPI bus via the Blackfin SPORT"
33 MODULE_AUTHOR("Cliff Cai");
34 MODULE_DESCRIPTION(DRV_DESC
);
35 MODULE_LICENSE("GPL");
36 MODULE_ALIAS("platform:bfin-sport-spi");
38 enum bfin_sport_spi_state
{
45 struct bfin_sport_spi_master_data
;
47 struct bfin_sport_transfer_ops
{
48 void (*write
) (struct bfin_sport_spi_master_data
*);
49 void (*read
) (struct bfin_sport_spi_master_data
*);
50 void (*duplex
) (struct bfin_sport_spi_master_data
*);
53 struct bfin_sport_spi_master_data
{
54 /* Driver model hookup */
57 /* SPI framework hookup */
58 struct spi_master
*master
;
60 /* Regs base of SPI controller */
61 struct sport_register __iomem
*regs
;
64 /* Pin request list */
67 struct work_struct pump_messages
;
69 struct list_head queue
;
73 /* Message Transfer pump */
74 struct tasklet_struct pump_transfers
;
76 /* Current message transfer state info */
77 enum bfin_sport_spi_state state
;
78 struct spi_message
*cur_msg
;
79 struct spi_transfer
*cur_transfer
;
80 struct bfin_sport_spi_slave_data
*cur_chip
;
95 struct bfin_sport_transfer_ops
*ops
;
98 struct bfin_sport_spi_slave_data
{
101 u16 cs_chg_udelay
; /* Some devices require > 255usec delay */
104 struct bfin_sport_transfer_ops
*ops
;
108 bfin_sport_spi_enable(struct bfin_sport_spi_master_data
*drv_data
)
110 bfin_write_or(&drv_data
->regs
->tcr1
, TSPEN
);
111 bfin_write_or(&drv_data
->regs
->rcr1
, TSPEN
);
116 bfin_sport_spi_disable(struct bfin_sport_spi_master_data
*drv_data
)
118 bfin_write_and(&drv_data
->regs
->tcr1
, ~TSPEN
);
119 bfin_write_and(&drv_data
->regs
->rcr1
, ~TSPEN
);
123 /* Caculate the SPI_BAUD register value based on input HZ */
125 bfin_sport_hz_to_spi_baud(u32 speed_hz
)
127 u_long clk
, sclk
= get_sclk();
128 int div
= (sclk
/ (2 * speed_hz
)) - 1;
133 clk
= sclk
/ (2 * (div
+ 1));
141 /* Chip select operation functions for cs_change flag */
143 bfin_sport_spi_cs_active(struct bfin_sport_spi_slave_data
*chip
)
145 gpio_direction_output(chip
->cs_gpio
, 0);
149 bfin_sport_spi_cs_deactive(struct bfin_sport_spi_slave_data
*chip
)
151 gpio_direction_output(chip
->cs_gpio
, 1);
152 /* Move delay here for consistency */
153 if (chip
->cs_chg_udelay
)
154 udelay(chip
->cs_chg_udelay
);
158 bfin_sport_spi_stat_poll_complete(struct bfin_sport_spi_master_data
*drv_data
)
160 unsigned long timeout
= jiffies
+ HZ
;
161 while (!(bfin_read(&drv_data
->regs
->stat
) & RXNE
)) {
162 if (!time_before(jiffies
, timeout
))
168 bfin_sport_spi_u8_writer(struct bfin_sport_spi_master_data
*drv_data
)
172 while (drv_data
->tx
< drv_data
->tx_end
) {
173 bfin_write(&drv_data
->regs
->tx16
, *drv_data
->tx8
++);
174 bfin_sport_spi_stat_poll_complete(drv_data
);
175 dummy
= bfin_read(&drv_data
->regs
->rx16
);
180 bfin_sport_spi_u8_reader(struct bfin_sport_spi_master_data
*drv_data
)
182 u16 tx_val
= drv_data
->cur_chip
->idle_tx_val
;
184 while (drv_data
->rx
< drv_data
->rx_end
) {
185 bfin_write(&drv_data
->regs
->tx16
, tx_val
);
186 bfin_sport_spi_stat_poll_complete(drv_data
);
187 *drv_data
->rx8
++ = bfin_read(&drv_data
->regs
->rx16
);
192 bfin_sport_spi_u8_duplex(struct bfin_sport_spi_master_data
*drv_data
)
194 while (drv_data
->rx
< drv_data
->rx_end
) {
195 bfin_write(&drv_data
->regs
->tx16
, *drv_data
->tx8
++);
196 bfin_sport_spi_stat_poll_complete(drv_data
);
197 *drv_data
->rx8
++ = bfin_read(&drv_data
->regs
->rx16
);
201 static struct bfin_sport_transfer_ops bfin_sport_transfer_ops_u8
= {
202 .write
= bfin_sport_spi_u8_writer
,
203 .read
= bfin_sport_spi_u8_reader
,
204 .duplex
= bfin_sport_spi_u8_duplex
,
208 bfin_sport_spi_u16_writer(struct bfin_sport_spi_master_data
*drv_data
)
212 while (drv_data
->tx
< drv_data
->tx_end
) {
213 bfin_write(&drv_data
->regs
->tx16
, *drv_data
->tx16
++);
214 bfin_sport_spi_stat_poll_complete(drv_data
);
215 dummy
= bfin_read(&drv_data
->regs
->rx16
);
220 bfin_sport_spi_u16_reader(struct bfin_sport_spi_master_data
*drv_data
)
222 u16 tx_val
= drv_data
->cur_chip
->idle_tx_val
;
224 while (drv_data
->rx
< drv_data
->rx_end
) {
225 bfin_write(&drv_data
->regs
->tx16
, tx_val
);
226 bfin_sport_spi_stat_poll_complete(drv_data
);
227 *drv_data
->rx16
++ = bfin_read(&drv_data
->regs
->rx16
);
232 bfin_sport_spi_u16_duplex(struct bfin_sport_spi_master_data
*drv_data
)
234 while (drv_data
->rx
< drv_data
->rx_end
) {
235 bfin_write(&drv_data
->regs
->tx16
, *drv_data
->tx16
++);
236 bfin_sport_spi_stat_poll_complete(drv_data
);
237 *drv_data
->rx16
++ = bfin_read(&drv_data
->regs
->rx16
);
241 static struct bfin_sport_transfer_ops bfin_sport_transfer_ops_u16
= {
242 .write
= bfin_sport_spi_u16_writer
,
243 .read
= bfin_sport_spi_u16_reader
,
244 .duplex
= bfin_sport_spi_u16_duplex
,
247 /* stop controller and re-config current chip */
249 bfin_sport_spi_restore_state(struct bfin_sport_spi_master_data
*drv_data
)
251 struct bfin_sport_spi_slave_data
*chip
= drv_data
->cur_chip
;
253 bfin_sport_spi_disable(drv_data
);
254 dev_dbg(drv_data
->dev
, "restoring spi ctl state\n");
256 bfin_write(&drv_data
->regs
->tcr1
, chip
->ctl_reg
);
257 bfin_write(&drv_data
->regs
->tclkdiv
, chip
->baud
);
260 bfin_write(&drv_data
->regs
->rcr1
, chip
->ctl_reg
& ~(ITCLK
| ITFS
));
263 bfin_sport_spi_cs_active(chip
);
266 /* test if there is more transfer to be done */
267 static enum bfin_sport_spi_state
268 bfin_sport_spi_next_transfer(struct bfin_sport_spi_master_data
*drv_data
)
270 struct spi_message
*msg
= drv_data
->cur_msg
;
271 struct spi_transfer
*trans
= drv_data
->cur_transfer
;
273 /* Move to next transfer */
274 if (trans
->transfer_list
.next
!= &msg
->transfers
) {
275 drv_data
->cur_transfer
=
276 list_entry(trans
->transfer_list
.next
,
277 struct spi_transfer
, transfer_list
);
278 return RUNNING_STATE
;
285 * caller already set message->status;
286 * dma and pio irqs are blocked give finished message back
289 bfin_sport_spi_giveback(struct bfin_sport_spi_master_data
*drv_data
)
291 struct bfin_sport_spi_slave_data
*chip
= drv_data
->cur_chip
;
293 struct spi_message
*msg
;
295 spin_lock_irqsave(&drv_data
->lock
, flags
);
296 msg
= drv_data
->cur_msg
;
297 drv_data
->state
= START_STATE
;
298 drv_data
->cur_msg
= NULL
;
299 drv_data
->cur_transfer
= NULL
;
300 drv_data
->cur_chip
= NULL
;
301 schedule_work(&drv_data
->pump_messages
);
302 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
304 if (!drv_data
->cs_change
)
305 bfin_sport_spi_cs_deactive(chip
);
308 msg
->complete(msg
->context
);
312 sport_err_handler(int irq
, void *dev_id
)
314 struct bfin_sport_spi_master_data
*drv_data
= dev_id
;
317 dev_dbg(drv_data
->dev
, "%s enter\n", __func__
);
318 status
= bfin_read(&drv_data
->regs
->stat
) & (TOVF
| TUVF
| ROVF
| RUVF
);
321 bfin_write(&drv_data
->regs
->stat
, status
);
324 bfin_sport_spi_disable(drv_data
);
325 dev_err(drv_data
->dev
, "status error:%s%s%s%s\n",
326 status
& TOVF
? " TOVF" : "",
327 status
& TUVF
? " TUVF" : "",
328 status
& ROVF
? " ROVF" : "",
329 status
& RUVF
? " RUVF" : "");
336 bfin_sport_spi_pump_transfers(unsigned long data
)
338 struct bfin_sport_spi_master_data
*drv_data
= (void *)data
;
339 struct spi_message
*message
= NULL
;
340 struct spi_transfer
*transfer
= NULL
;
341 struct spi_transfer
*previous
= NULL
;
342 struct bfin_sport_spi_slave_data
*chip
= NULL
;
343 unsigned int bits_per_word
;
344 u32 tranf_success
= 1;
348 /* Get current state information */
349 message
= drv_data
->cur_msg
;
350 transfer
= drv_data
->cur_transfer
;
351 chip
= drv_data
->cur_chip
;
353 transfer_speed
= bfin_sport_hz_to_spi_baud(transfer
->speed_hz
);
354 bfin_write(&drv_data
->regs
->tclkdiv
, transfer_speed
);
358 * if msg is error or done, report it back using complete() callback
361 /* Handle for abort */
362 if (drv_data
->state
== ERROR_STATE
) {
363 dev_dbg(drv_data
->dev
, "transfer: we've hit an error\n");
364 message
->status
= -EIO
;
365 bfin_sport_spi_giveback(drv_data
);
369 /* Handle end of message */
370 if (drv_data
->state
== DONE_STATE
) {
371 dev_dbg(drv_data
->dev
, "transfer: all done!\n");
373 bfin_sport_spi_giveback(drv_data
);
377 /* Delay if requested at end of transfer */
378 if (drv_data
->state
== RUNNING_STATE
) {
379 dev_dbg(drv_data
->dev
, "transfer: still running ...\n");
380 previous
= list_entry(transfer
->transfer_list
.prev
,
381 struct spi_transfer
, transfer_list
);
382 if (previous
->delay_usecs
)
383 udelay(previous
->delay_usecs
);
386 if (transfer
->len
== 0) {
387 /* Move to next transfer of this msg */
388 drv_data
->state
= bfin_sport_spi_next_transfer(drv_data
);
389 /* Schedule next transfer tasklet */
390 tasklet_schedule(&drv_data
->pump_transfers
);
393 if (transfer
->tx_buf
!= NULL
) {
394 drv_data
->tx
= (void *)transfer
->tx_buf
;
395 drv_data
->tx_end
= drv_data
->tx
+ transfer
->len
;
396 dev_dbg(drv_data
->dev
, "tx_buf is %p, tx_end is %p\n",
397 transfer
->tx_buf
, drv_data
->tx_end
);
401 if (transfer
->rx_buf
!= NULL
) {
402 full_duplex
= transfer
->tx_buf
!= NULL
;
403 drv_data
->rx
= transfer
->rx_buf
;
404 drv_data
->rx_end
= drv_data
->rx
+ transfer
->len
;
405 dev_dbg(drv_data
->dev
, "rx_buf is %p, rx_end is %p\n",
406 transfer
->rx_buf
, drv_data
->rx_end
);
410 drv_data
->cs_change
= transfer
->cs_change
;
412 /* Bits per word setup */
413 bits_per_word
= transfer
->bits_per_word
;
414 if (bits_per_word
== 16)
415 drv_data
->ops
= &bfin_sport_transfer_ops_u16
;
417 drv_data
->ops
= &bfin_sport_transfer_ops_u8
;
418 bfin_write(&drv_data
->regs
->tcr2
, bits_per_word
- 1);
419 bfin_write(&drv_data
->regs
->tfsdiv
, bits_per_word
- 1);
420 bfin_write(&drv_data
->regs
->rcr2
, bits_per_word
- 1);
422 drv_data
->state
= RUNNING_STATE
;
424 if (drv_data
->cs_change
)
425 bfin_sport_spi_cs_active(chip
);
427 dev_dbg(drv_data
->dev
,
428 "now pumping a transfer: width is %d, len is %d\n",
429 bits_per_word
, transfer
->len
);
431 /* PIO mode write then read */
432 dev_dbg(drv_data
->dev
, "doing IO transfer\n");
434 bfin_sport_spi_enable(drv_data
);
436 /* full duplex mode */
437 BUG_ON((drv_data
->tx_end
- drv_data
->tx
) !=
438 (drv_data
->rx_end
- drv_data
->rx
));
439 drv_data
->ops
->duplex(drv_data
);
441 if (drv_data
->tx
!= drv_data
->tx_end
)
443 } else if (drv_data
->tx
!= NULL
) {
444 /* write only half duplex */
446 drv_data
->ops
->write(drv_data
);
448 if (drv_data
->tx
!= drv_data
->tx_end
)
450 } else if (drv_data
->rx
!= NULL
) {
451 /* read only half duplex */
453 drv_data
->ops
->read(drv_data
);
454 if (drv_data
->rx
!= drv_data
->rx_end
)
457 bfin_sport_spi_disable(drv_data
);
459 if (!tranf_success
) {
460 dev_dbg(drv_data
->dev
, "IO write error!\n");
461 drv_data
->state
= ERROR_STATE
;
463 /* Update total byte transferred */
464 message
->actual_length
+= transfer
->len
;
465 /* Move to next transfer of this msg */
466 drv_data
->state
= bfin_sport_spi_next_transfer(drv_data
);
467 if (drv_data
->cs_change
)
468 bfin_sport_spi_cs_deactive(chip
);
471 /* Schedule next transfer tasklet */
472 tasklet_schedule(&drv_data
->pump_transfers
);
475 /* pop a msg from queue and kick off real transfer */
477 bfin_sport_spi_pump_messages(struct work_struct
*work
)
479 struct bfin_sport_spi_master_data
*drv_data
;
481 struct spi_message
*next_msg
;
483 drv_data
= container_of(work
, struct bfin_sport_spi_master_data
, pump_messages
);
485 /* Lock queue and check for queue work */
486 spin_lock_irqsave(&drv_data
->lock
, flags
);
487 if (list_empty(&drv_data
->queue
) || !drv_data
->run
) {
488 /* pumper kicked off but no work to do */
490 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
494 /* Make sure we are not already running a message */
495 if (drv_data
->cur_msg
) {
496 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
500 /* Extract head of queue */
501 next_msg
= list_entry(drv_data
->queue
.next
,
502 struct spi_message
, queue
);
504 drv_data
->cur_msg
= next_msg
;
506 /* Setup the SSP using the per chip configuration */
507 drv_data
->cur_chip
= spi_get_ctldata(drv_data
->cur_msg
->spi
);
509 list_del_init(&drv_data
->cur_msg
->queue
);
511 /* Initialize message state */
512 drv_data
->cur_msg
->state
= START_STATE
;
513 drv_data
->cur_transfer
= list_entry(drv_data
->cur_msg
->transfers
.next
,
514 struct spi_transfer
, transfer_list
);
515 bfin_sport_spi_restore_state(drv_data
);
516 dev_dbg(drv_data
->dev
, "got a message to pump, "
517 "state is set to: baud %d, cs_gpio %i, ctl 0x%x\n",
518 drv_data
->cur_chip
->baud
, drv_data
->cur_chip
->cs_gpio
,
519 drv_data
->cur_chip
->ctl_reg
);
521 dev_dbg(drv_data
->dev
,
522 "the first transfer len is %d\n",
523 drv_data
->cur_transfer
->len
);
525 /* Mark as busy and launch transfers */
526 tasklet_schedule(&drv_data
->pump_transfers
);
529 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
533 * got a msg to transfer, queue it in drv_data->queue.
534 * And kick off message pumper
537 bfin_sport_spi_transfer(struct spi_device
*spi
, struct spi_message
*msg
)
539 struct bfin_sport_spi_master_data
*drv_data
= spi_master_get_devdata(spi
->master
);
542 spin_lock_irqsave(&drv_data
->lock
, flags
);
544 if (!drv_data
->run
) {
545 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
549 msg
->actual_length
= 0;
550 msg
->status
= -EINPROGRESS
;
551 msg
->state
= START_STATE
;
553 dev_dbg(&spi
->dev
, "adding an msg in transfer()\n");
554 list_add_tail(&msg
->queue
, &drv_data
->queue
);
556 if (drv_data
->run
&& !drv_data
->busy
)
557 schedule_work(&drv_data
->pump_messages
);
559 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
564 /* Called every time common spi devices change state */
566 bfin_sport_spi_setup(struct spi_device
*spi
)
568 struct bfin_sport_spi_slave_data
*chip
, *first
= NULL
;
571 /* Only alloc (or use chip_info) on first setup */
572 chip
= spi_get_ctldata(spi
);
574 struct bfin5xx_spi_chip
*chip_info
;
576 chip
= first
= kzalloc(sizeof(*chip
), GFP_KERNEL
);
580 /* platform chip_info isn't required */
581 chip_info
= spi
->controller_data
;
584 * DITFS and TDTYPE are only thing we don't set, but
585 * they probably shouldn't be changed by people.
587 if (chip_info
->ctl_reg
|| chip_info
->enable_dma
) {
589 dev_err(&spi
->dev
, "don't set ctl_reg/enable_dma fields\n");
592 chip
->cs_chg_udelay
= chip_info
->cs_chg_udelay
;
593 chip
->idle_tx_val
= chip_info
->idle_tx_val
;
597 /* translate common spi framework into our register
598 * following configure contents are same for tx and rx.
601 if (spi
->mode
& SPI_CPHA
)
602 chip
->ctl_reg
&= ~TCKFE
;
604 chip
->ctl_reg
|= TCKFE
;
606 if (spi
->mode
& SPI_LSB_FIRST
)
607 chip
->ctl_reg
|= TLSBIT
;
609 chip
->ctl_reg
&= ~TLSBIT
;
611 /* Sport in master mode */
612 chip
->ctl_reg
|= ITCLK
| ITFS
| TFSR
| LATFS
| LTFS
;
614 chip
->baud
= bfin_sport_hz_to_spi_baud(spi
->max_speed_hz
);
616 chip
->cs_gpio
= spi
->chip_select
;
617 ret
= gpio_request(chip
->cs_gpio
, spi
->modalias
);
621 dev_dbg(&spi
->dev
, "setup spi chip %s, width is %d\n",
622 spi
->modalias
, spi
->bits_per_word
);
623 dev_dbg(&spi
->dev
, "ctl_reg is 0x%x, GPIO is %i\n",
624 chip
->ctl_reg
, spi
->chip_select
);
626 spi_set_ctldata(spi
, chip
);
628 bfin_sport_spi_cs_deactive(chip
);
638 * callback for spi framework.
639 * clean driver specific data
642 bfin_sport_spi_cleanup(struct spi_device
*spi
)
644 struct bfin_sport_spi_slave_data
*chip
= spi_get_ctldata(spi
);
649 gpio_free(chip
->cs_gpio
);
655 bfin_sport_spi_init_queue(struct bfin_sport_spi_master_data
*drv_data
)
657 INIT_LIST_HEAD(&drv_data
->queue
);
658 spin_lock_init(&drv_data
->lock
);
660 drv_data
->run
= false;
663 /* init transfer tasklet */
664 tasklet_init(&drv_data
->pump_transfers
,
665 bfin_sport_spi_pump_transfers
, (unsigned long)drv_data
);
667 INIT_WORK(&drv_data
->pump_messages
, bfin_sport_spi_pump_messages
);
673 bfin_sport_spi_start_queue(struct bfin_sport_spi_master_data
*drv_data
)
677 spin_lock_irqsave(&drv_data
->lock
, flags
);
679 if (drv_data
->run
|| drv_data
->busy
) {
680 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
684 drv_data
->run
= true;
685 drv_data
->cur_msg
= NULL
;
686 drv_data
->cur_transfer
= NULL
;
687 drv_data
->cur_chip
= NULL
;
688 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
690 schedule_work(&drv_data
->pump_messages
);
696 bfin_sport_spi_stop_queue(struct bfin_sport_spi_master_data
*drv_data
)
699 unsigned limit
= 500;
702 spin_lock_irqsave(&drv_data
->lock
, flags
);
705 * This is a bit lame, but is optimized for the common execution path.
706 * A wait_queue on the drv_data->busy could be used, but then the common
707 * execution path (pump_messages) would be required to call wake_up or
708 * friends on every SPI message. Do this instead
710 drv_data
->run
= false;
711 while (!list_empty(&drv_data
->queue
) && drv_data
->busy
&& limit
--) {
712 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
714 spin_lock_irqsave(&drv_data
->lock
, flags
);
717 if (!list_empty(&drv_data
->queue
) || drv_data
->busy
)
720 spin_unlock_irqrestore(&drv_data
->lock
, flags
);
726 bfin_sport_spi_destroy_queue(struct bfin_sport_spi_master_data
*drv_data
)
730 status
= bfin_sport_spi_stop_queue(drv_data
);
734 flush_work(&drv_data
->pump_messages
);
739 static int bfin_sport_spi_probe(struct platform_device
*pdev
)
741 struct device
*dev
= &pdev
->dev
;
742 struct bfin5xx_spi_master
*platform_info
;
743 struct spi_master
*master
;
744 struct resource
*res
, *ires
;
745 struct bfin_sport_spi_master_data
*drv_data
;
748 platform_info
= dev_get_platdata(dev
);
750 /* Allocate master with space for drv_data */
751 master
= spi_alloc_master(dev
, sizeof(*master
) + 16);
753 dev_err(dev
, "cannot alloc spi_master\n");
757 drv_data
= spi_master_get_devdata(master
);
758 drv_data
->master
= master
;
760 drv_data
->pin_req
= platform_info
->pin_req
;
762 master
->mode_bits
= SPI_CPOL
| SPI_CPHA
| SPI_LSB_FIRST
;
763 master
->bits_per_word_mask
= SPI_BPW_MASK(8) | SPI_BPW_MASK(16);
764 master
->bus_num
= pdev
->id
;
765 master
->num_chipselect
= platform_info
->num_chipselect
;
766 master
->cleanup
= bfin_sport_spi_cleanup
;
767 master
->setup
= bfin_sport_spi_setup
;
768 master
->transfer
= bfin_sport_spi_transfer
;
770 /* Find and map our resources */
771 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
773 dev_err(dev
, "cannot get IORESOURCE_MEM\n");
775 goto out_error_get_res
;
778 drv_data
->regs
= ioremap(res
->start
, resource_size(res
));
779 if (drv_data
->regs
== NULL
) {
780 dev_err(dev
, "cannot map registers\n");
782 goto out_error_ioremap
;
785 ires
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
787 dev_err(dev
, "cannot get IORESOURCE_IRQ\n");
789 goto out_error_get_ires
;
791 drv_data
->err_irq
= ires
->start
;
793 /* Initial and start queue */
794 status
= bfin_sport_spi_init_queue(drv_data
);
796 dev_err(dev
, "problem initializing queue\n");
797 goto out_error_queue_alloc
;
800 status
= bfin_sport_spi_start_queue(drv_data
);
802 dev_err(dev
, "problem starting queue\n");
803 goto out_error_queue_alloc
;
806 status
= request_irq(drv_data
->err_irq
, sport_err_handler
,
807 0, "sport_spi_err", drv_data
);
809 dev_err(dev
, "unable to request sport err irq\n");
813 status
= peripheral_request_list(drv_data
->pin_req
, DRV_NAME
);
815 dev_err(dev
, "requesting peripherals failed\n");
816 goto out_error_peripheral
;
819 /* Register with the SPI framework */
820 platform_set_drvdata(pdev
, drv_data
);
821 status
= spi_register_master(master
);
823 dev_err(dev
, "problem registering spi master\n");
824 goto out_error_master
;
827 dev_info(dev
, "%s, regs_base@%p\n", DRV_DESC
, drv_data
->regs
);
831 peripheral_free_list(drv_data
->pin_req
);
832 out_error_peripheral
:
833 free_irq(drv_data
->err_irq
, drv_data
);
835 out_error_queue_alloc
:
836 bfin_sport_spi_destroy_queue(drv_data
);
838 iounmap(drv_data
->regs
);
841 spi_master_put(master
);
846 /* stop hardware and remove the driver */
847 static int bfin_sport_spi_remove(struct platform_device
*pdev
)
849 struct bfin_sport_spi_master_data
*drv_data
= platform_get_drvdata(pdev
);
855 /* Remove the queue */
856 status
= bfin_sport_spi_destroy_queue(drv_data
);
860 /* Disable the SSP at the peripheral and SOC level */
861 bfin_sport_spi_disable(drv_data
);
863 /* Disconnect from the SPI framework */
864 spi_unregister_master(drv_data
->master
);
866 peripheral_free_list(drv_data
->pin_req
);
871 #ifdef CONFIG_PM_SLEEP
872 static int bfin_sport_spi_suspend(struct device
*dev
)
874 struct bfin_sport_spi_master_data
*drv_data
= dev_get_drvdata(dev
);
877 status
= bfin_sport_spi_stop_queue(drv_data
);
882 bfin_sport_spi_disable(drv_data
);
887 static int bfin_sport_spi_resume(struct device
*dev
)
889 struct bfin_sport_spi_master_data
*drv_data
= dev_get_drvdata(dev
);
892 /* Enable the SPI interface */
893 bfin_sport_spi_enable(drv_data
);
895 /* Start the queue running */
896 status
= bfin_sport_spi_start_queue(drv_data
);
898 dev_err(drv_data
->dev
, "problem resuming queue\n");
903 static SIMPLE_DEV_PM_OPS(bfin_sport_spi_pm_ops
, bfin_sport_spi_suspend
,
904 bfin_sport_spi_resume
);
906 #define BFIN_SPORT_SPI_PM_OPS (&bfin_sport_spi_pm_ops)
908 #define BFIN_SPORT_SPI_PM_OPS NULL
911 static struct platform_driver bfin_sport_spi_driver
= {
914 .pm
= BFIN_SPORT_SPI_PM_OPS
,
916 .probe
= bfin_sport_spi_probe
,
917 .remove
= bfin_sport_spi_remove
,
919 module_platform_driver(bfin_sport_spi_driver
);