blob: df086ee4cdaac84a3c810c97be0689b597fc122e [file] [log] [blame]
Alexander Shiyan161b96c2012-11-07 21:30:29 +04001/*
2 * CLPS711X SPI bus driver
3 *
Alexander Shiyan98984792014-01-10 17:02:05 +04004 * Copyright (C) 2012-2014 Alexander Shiyan <shc_work@mail.ru>
Alexander Shiyan161b96c2012-11-07 21:30:29 +04005 *
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.
10 */
11
12#include <linux/io.h>
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>
22
23#include <mach/hardware.h>
24
25#define DRIVER_NAME "spi-clps711x"
26
27struct spi_clps711x_data {
28 struct completion done;
29
30 struct clk *spi_clk;
31 u32 max_speed_hz;
32
33 u8 *tx_buf;
34 u8 *rx_buf;
Alexander Shiyan8dda9d92014-02-02 10:59:49 +040035 unsigned int bpw;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040036 int len;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040037};
38
39static int spi_clps711x_setup(struct spi_device *spi)
40{
Alexander Shiyan161b96c2012-11-07 21:30:29 +040041 /* We are expect that SPI-device is not selected */
Alexander Shiyan3e9ea4b2014-02-02 10:59:50 +040042 gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
Alexander Shiyan161b96c2012-11-07 21:30:29 +040043
44 return 0;
45}
46
47static void spi_clps711x_setup_mode(struct spi_device *spi)
48{
49 /* Setup edge for transfer */
50 if (spi->mode & SPI_CPHA)
51 clps_writew(clps_readw(SYSCON3) | SYSCON3_ADCCKNSEN, SYSCON3);
52 else
53 clps_writew(clps_readw(SYSCON3) & ~SYSCON3_ADCCKNSEN, SYSCON3);
54}
55
Alexander Shiyan8dda9d92014-02-02 10:59:49 +040056static void spi_clps711x_setup_xfer(struct spi_device *spi,
57 struct spi_transfer *xfer)
Alexander Shiyan161b96c2012-11-07 21:30:29 +040058{
59 u32 speed = xfer->speed_hz ? : spi->max_speed_hz;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040060 struct spi_clps711x_data *hw = spi_master_get_devdata(spi->master);
61
Alexander Shiyan161b96c2012-11-07 21:30:29 +040062 /* Setup SPI frequency divider */
63 if (!speed || (speed >= hw->max_speed_hz))
64 clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
65 SYSCON1_ADCKSEL(3), SYSCON1);
66 else if (speed >= (hw->max_speed_hz / 2))
67 clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
68 SYSCON1_ADCKSEL(2), SYSCON1);
69 else if (speed >= (hw->max_speed_hz / 8))
70 clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
71 SYSCON1_ADCKSEL(1), SYSCON1);
72 else
73 clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
74 SYSCON1_ADCKSEL(0), SYSCON1);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040075}
76
77static int spi_clps711x_transfer_one_message(struct spi_master *master,
78 struct spi_message *msg)
79{
80 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
Alexander Shiyan8dda9d92014-02-02 10:59:49 +040081 struct spi_device *spi = msg->spi;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040082 struct spi_transfer *xfer;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040083
Alexander Shiyan8dda9d92014-02-02 10:59:49 +040084 spi_clps711x_setup_mode(spi);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040085
86 list_for_each_entry(xfer, &msg->transfers, transfer_list) {
Alexander Shiyanc7a26f12014-02-02 10:59:48 +040087 u8 data;
88
Alexander Shiyan8dda9d92014-02-02 10:59:49 +040089 spi_clps711x_setup_xfer(spi, xfer);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040090
Alexander Shiyan3e9ea4b2014-02-02 10:59:50 +040091 gpio_set_value(spi->cs_gpio, !!(spi->mode & SPI_CS_HIGH));
Alexander Shiyan161b96c2012-11-07 21:30:29 +040092
Wolfram Sang16735d02013-11-14 14:32:02 -080093 reinit_completion(&hw->done);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040094
Alexander Shiyan161b96c2012-11-07 21:30:29 +040095 hw->len = xfer->len;
Alexander Shiyan8dda9d92014-02-02 10:59:49 +040096 hw->bpw = xfer->bits_per_word ? : spi->bits_per_word;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040097 hw->tx_buf = (u8 *)xfer->tx_buf;
98 hw->rx_buf = (u8 *)xfer->rx_buf;
99
100 /* Initiate transfer */
Alexander Shiyanc7a26f12014-02-02 10:59:48 +0400101 data = hw->tx_buf ? *hw->tx_buf++ : 0;
Alexander Shiyan8dda9d92014-02-02 10:59:49 +0400102 clps_writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
103 SYNCIO);
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400104
105 wait_for_completion(&hw->done);
106
107 if (xfer->delay_usecs)
108 udelay(xfer->delay_usecs);
109
110 if (xfer->cs_change ||
111 list_is_last(&xfer->transfer_list, &msg->transfers))
Alexander Shiyan3e9ea4b2014-02-02 10:59:50 +0400112 gpio_set_value(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400113
114 msg->actual_length += xfer->len;
115 }
116
Alexander Shiyan8dda9d92014-02-02 10:59:49 +0400117 msg->status = 0;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400118 spi_finalize_current_message(master);
119
120 return 0;
121}
122
123static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
124{
125 struct spi_clps711x_data *hw = (struct spi_clps711x_data *)dev_id;
Alexander Shiyanc7a26f12014-02-02 10:59:48 +0400126 u8 data;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400127
128 /* Handle RX */
129 data = clps_readb(SYNCIO);
130 if (hw->rx_buf)
Alexander Shiyanc7a26f12014-02-02 10:59:48 +0400131 *hw->rx_buf++ = data;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400132
133 /* Handle TX */
Alexander Shiyanc7a26f12014-02-02 10:59:48 +0400134 if (--hw->len > 0) {
135 data = hw->tx_buf ? *hw->tx_buf++ : 0;
Alexander Shiyan8dda9d92014-02-02 10:59:49 +0400136 clps_writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
137 SYNCIO);
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400138 } else
139 complete(&hw->done);
140
141 return IRQ_HANDLED;
142}
143
Grant Likelyfd4a3192012-12-07 16:57:14 +0000144static int spi_clps711x_probe(struct platform_device *pdev)
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400145{
146 int i, ret;
147 struct spi_master *master;
148 struct spi_clps711x_data *hw;
149 struct spi_clps711x_pdata *pdata = dev_get_platdata(&pdev->dev);
150
151 if (!pdata) {
152 dev_err(&pdev->dev, "No platform data supplied\n");
153 return -EINVAL;
154 }
155
156 if (pdata->num_chipselect < 1) {
157 dev_err(&pdev->dev, "At least one CS must be defined\n");
158 return -EINVAL;
159 }
160
Alexander Shiyan3e9ea4b2014-02-02 10:59:50 +0400161 master = spi_alloc_master(&pdev->dev, sizeof(*hw));
162 if (!master)
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400163 return -ENOMEM;
Alexander Shiyan3e9ea4b2014-02-02 10:59:50 +0400164
165 master->cs_gpios = devm_kzalloc(&pdev->dev, sizeof(int) *
166 pdata->num_chipselect, GFP_KERNEL);
167 if (!master->cs_gpios) {
168 ret = -ENOMEM;
169 goto err_out;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400170 }
171
172 master->bus_num = pdev->id;
173 master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
Alexander Shiyan8dda9d92014-02-02 10:59:49 +0400174 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8);
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400175 master->num_chipselect = pdata->num_chipselect;
176 master->setup = spi_clps711x_setup;
177 master->transfer_one_message = spi_clps711x_transfer_one_message;
178
179 hw = spi_master_get_devdata(master);
180
181 for (i = 0; i < master->num_chipselect; i++) {
Alexander Shiyan3e9ea4b2014-02-02 10:59:50 +0400182 master->cs_gpios[i] = pdata->chipselect[i];
183 if (!gpio_is_valid(master->cs_gpios[i])) {
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400184 dev_err(&pdev->dev, "Invalid CS GPIO %i\n", i);
185 ret = -EINVAL;
186 goto err_out;
187 }
Alexander Shiyan3e9ea4b2014-02-02 10:59:50 +0400188 if (devm_gpio_request(&pdev->dev, master->cs_gpios[i], NULL)) {
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400189 dev_err(&pdev->dev, "Can't get CS GPIO %i\n", i);
190 ret = -EINVAL;
191 goto err_out;
192 }
193 }
194
195 hw->spi_clk = devm_clk_get(&pdev->dev, "spi");
196 if (IS_ERR(hw->spi_clk)) {
197 dev_err(&pdev->dev, "Can't get clocks\n");
198 ret = PTR_ERR(hw->spi_clk);
199 goto err_out;
200 }
201 hw->max_speed_hz = clk_get_rate(hw->spi_clk);
202
203 init_completion(&hw->done);
204 platform_set_drvdata(pdev, master);
205
206 /* Disable extended mode due hardware problems */
207 clps_writew(clps_readw(SYSCON3) & ~SYSCON3_ADCCON, SYSCON3);
208
209 /* Clear possible pending interrupt */
210 clps_readl(SYNCIO);
211
212 ret = devm_request_irq(&pdev->dev, IRQ_SSEOTI, spi_clps711x_isr, 0,
213 dev_name(&pdev->dev), hw);
214 if (ret) {
215 dev_err(&pdev->dev, "Can't request IRQ\n");
Sachin Kamatc7083792013-09-27 15:32:53 +0530216 goto err_out;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400217 }
218
Jingoo Hanc493fc42013-09-24 13:27:48 +0900219 ret = devm_spi_register_master(&pdev->dev, master);
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400220 if (!ret) {
221 dev_info(&pdev->dev,
222 "SPI bus driver initialized. Master clock %u Hz\n",
223 hw->max_speed_hz);
224 return 0;
225 }
226
227 dev_err(&pdev->dev, "Failed to register master\n");
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400228
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400229err_out:
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400230 spi_master_put(master);
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400231
232 return ret;
233}
234
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400235static struct platform_driver clps711x_spi_driver = {
236 .driver = {
237 .name = DRIVER_NAME,
238 .owner = THIS_MODULE,
239 },
240 .probe = spi_clps711x_probe,
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400241};
242module_platform_driver(clps711x_spi_driver);
243
244MODULE_LICENSE("GPL");
245MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
246MODULE_DESCRIPTION("CLPS711X SPI bus driver");
Axel Lin350a9b32014-01-14 17:01:54 +0800247MODULE_ALIAS("platform:" DRIVER_NAME);