2 * CLPS711X SPI bus driver
4 * Copyright (C) 2012 Alexander Shiyan <shc_work@mail.ru>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
13 #include <linux/clk.h>
14 #include <linux/init.h>
15 #include <linux/gpio.h>
16 #include <linux/delay.h>
17 #include <linux/module.h>
18 #include <linux/interrupt.h>
19 #include <linux/platform_device.h>
20 #include <linux/spi/spi.h>
21 #include <linux/platform_data/spi-clps711x.h>
23 #include <mach/hardware.h>
25 #define DRIVER_NAME "spi-clps711x"
27 struct spi_clps711x_data
{
28 struct completion done
;
41 static int spi_clps711x_setup(struct spi_device
*spi
)
43 struct spi_clps711x_data
*hw
= spi_master_get_devdata(spi
->master
);
45 /* We are expect that SPI-device is not selected */
46 gpio_direction_output(hw
->chipselect
[spi
->chip_select
],
47 !(spi
->mode
& SPI_CS_HIGH
));
52 static void spi_clps711x_setup_mode(struct spi_device
*spi
)
54 /* Setup edge for transfer */
55 if (spi
->mode
& SPI_CPHA
)
56 clps_writew(clps_readw(SYSCON3
) | SYSCON3_ADCCKNSEN
, SYSCON3
);
58 clps_writew(clps_readw(SYSCON3
) & ~SYSCON3_ADCCKNSEN
, SYSCON3
);
61 static int spi_clps711x_setup_xfer(struct spi_device
*spi
,
62 struct spi_transfer
*xfer
)
64 u32 speed
= xfer
->speed_hz
? : spi
->max_speed_hz
;
65 u8 bpw
= xfer
->bits_per_word
;
66 struct spi_clps711x_data
*hw
= spi_master_get_devdata(spi
->master
);
69 dev_err(&spi
->dev
, "Unsupported master bus width %i\n", bpw
);
73 /* Setup SPI frequency divider */
74 if (!speed
|| (speed
>= hw
->max_speed_hz
))
75 clps_writel((clps_readl(SYSCON1
) & ~SYSCON1_ADCKSEL_MASK
) |
76 SYSCON1_ADCKSEL(3), SYSCON1
);
77 else if (speed
>= (hw
->max_speed_hz
/ 2))
78 clps_writel((clps_readl(SYSCON1
) & ~SYSCON1_ADCKSEL_MASK
) |
79 SYSCON1_ADCKSEL(2), SYSCON1
);
80 else if (speed
>= (hw
->max_speed_hz
/ 8))
81 clps_writel((clps_readl(SYSCON1
) & ~SYSCON1_ADCKSEL_MASK
) |
82 SYSCON1_ADCKSEL(1), SYSCON1
);
84 clps_writel((clps_readl(SYSCON1
) & ~SYSCON1_ADCKSEL_MASK
) |
85 SYSCON1_ADCKSEL(0), SYSCON1
);
90 static int spi_clps711x_transfer_one_message(struct spi_master
*master
,
91 struct spi_message
*msg
)
93 struct spi_clps711x_data
*hw
= spi_master_get_devdata(master
);
94 struct spi_transfer
*xfer
;
95 int status
= 0, cs
= hw
->chipselect
[msg
->spi
->chip_select
];
98 spi_clps711x_setup_mode(msg
->spi
);
100 list_for_each_entry(xfer
, &msg
->transfers
, transfer_list
) {
101 if (spi_clps711x_setup_xfer(msg
->spi
, xfer
)) {
106 gpio_set_value(cs
, !!(msg
->spi
->mode
& SPI_CS_HIGH
));
108 INIT_COMPLETION(hw
->done
);
112 hw
->tx_buf
= (u8
*)xfer
->tx_buf
;
113 hw
->rx_buf
= (u8
*)xfer
->rx_buf
;
115 /* Initiate transfer */
116 data
= hw
->tx_buf
? hw
->tx_buf
[hw
->count
] : 0;
117 clps_writel(data
| SYNCIO_FRMLEN(8) | SYNCIO_TXFRMEN
, SYNCIO
);
119 wait_for_completion(&hw
->done
);
121 if (xfer
->delay_usecs
)
122 udelay(xfer
->delay_usecs
);
124 if (xfer
->cs_change
||
125 list_is_last(&xfer
->transfer_list
, &msg
->transfers
))
126 gpio_set_value(cs
, !(msg
->spi
->mode
& SPI_CS_HIGH
));
128 msg
->actual_length
+= xfer
->len
;
132 msg
->status
= status
;
133 spi_finalize_current_message(master
);
138 static irqreturn_t
spi_clps711x_isr(int irq
, void *dev_id
)
140 struct spi_clps711x_data
*hw
= (struct spi_clps711x_data
*)dev_id
;
144 data
= clps_readb(SYNCIO
);
146 hw
->rx_buf
[hw
->count
] = (u8
)data
;
151 if (hw
->count
< hw
->len
) {
152 data
= hw
->tx_buf
? hw
->tx_buf
[hw
->count
] : 0;
153 clps_writel(data
| SYNCIO_FRMLEN(8) | SYNCIO_TXFRMEN
, SYNCIO
);
160 static int spi_clps711x_probe(struct platform_device
*pdev
)
163 struct spi_master
*master
;
164 struct spi_clps711x_data
*hw
;
165 struct spi_clps711x_pdata
*pdata
= dev_get_platdata(&pdev
->dev
);
168 dev_err(&pdev
->dev
, "No platform data supplied\n");
172 if (pdata
->num_chipselect
< 1) {
173 dev_err(&pdev
->dev
, "At least one CS must be defined\n");
177 master
= spi_alloc_master(&pdev
->dev
,
178 sizeof(struct spi_clps711x_data
) +
179 sizeof(int) * pdata
->num_chipselect
);
181 dev_err(&pdev
->dev
, "SPI allocating memory error\n");
185 master
->bus_num
= pdev
->id
;
186 master
->mode_bits
= SPI_CPHA
| SPI_CS_HIGH
;
187 master
->bits_per_word_mask
= SPI_BPW_MASK(8);
188 master
->num_chipselect
= pdata
->num_chipselect
;
189 master
->setup
= spi_clps711x_setup
;
190 master
->transfer_one_message
= spi_clps711x_transfer_one_message
;
192 hw
= spi_master_get_devdata(master
);
194 for (i
= 0; i
< master
->num_chipselect
; i
++) {
195 hw
->chipselect
[i
] = pdata
->chipselect
[i
];
196 if (!gpio_is_valid(hw
->chipselect
[i
])) {
197 dev_err(&pdev
->dev
, "Invalid CS GPIO %i\n", i
);
201 if (gpio_request(hw
->chipselect
[i
], DRIVER_NAME
)) {
202 dev_err(&pdev
->dev
, "Can't get CS GPIO %i\n", i
);
208 hw
->spi_clk
= devm_clk_get(&pdev
->dev
, "spi");
209 if (IS_ERR(hw
->spi_clk
)) {
210 dev_err(&pdev
->dev
, "Can't get clocks\n");
211 ret
= PTR_ERR(hw
->spi_clk
);
214 hw
->max_speed_hz
= clk_get_rate(hw
->spi_clk
);
216 init_completion(&hw
->done
);
217 platform_set_drvdata(pdev
, master
);
219 /* Disable extended mode due hardware problems */
220 clps_writew(clps_readw(SYSCON3
) & ~SYSCON3_ADCCON
, SYSCON3
);
222 /* Clear possible pending interrupt */
225 ret
= devm_request_irq(&pdev
->dev
, IRQ_SSEOTI
, spi_clps711x_isr
, 0,
226 dev_name(&pdev
->dev
), hw
);
228 dev_err(&pdev
->dev
, "Can't request IRQ\n");
232 ret
= devm_spi_register_master(&pdev
->dev
, master
);
235 "SPI bus driver initialized. Master clock %u Hz\n",
240 dev_err(&pdev
->dev
, "Failed to register master\n");
244 if (gpio_is_valid(hw
->chipselect
[i
]))
245 gpio_free(hw
->chipselect
[i
]);
247 spi_master_put(master
);
252 static int spi_clps711x_remove(struct platform_device
*pdev
)
255 struct spi_master
*master
= platform_get_drvdata(pdev
);
256 struct spi_clps711x_data
*hw
= spi_master_get_devdata(master
);
258 for (i
= 0; i
< master
->num_chipselect
; i
++)
259 if (gpio_is_valid(hw
->chipselect
[i
]))
260 gpio_free(hw
->chipselect
[i
]);
265 static struct platform_driver clps711x_spi_driver
= {
268 .owner
= THIS_MODULE
,
270 .probe
= spi_clps711x_probe
,
271 .remove
= spi_clps711x_remove
,
273 module_platform_driver(clps711x_spi_driver
);
275 MODULE_LICENSE("GPL");
276 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
277 MODULE_DESCRIPTION("CLPS711X SPI bus driver");