xref: /openbmc/linux/drivers/spi/spi-clps711x.c (revision 350a9b33046ef0d9909f68b87f411f4cf4e286f7)
1161b96c3SAlexander Shiyan /*
2161b96c3SAlexander Shiyan  *  CLPS711X SPI bus driver
3161b96c3SAlexander Shiyan  *
498984796SAlexander Shiyan  *  Copyright (C) 2012-2014 Alexander Shiyan <shc_work@mail.ru>
5161b96c3SAlexander Shiyan  *
6161b96c3SAlexander Shiyan  * This program is free software; you can redistribute it and/or modify
7161b96c3SAlexander Shiyan  * it under the terms of the GNU General Public License as published by
8161b96c3SAlexander Shiyan  * the Free Software Foundation; either version 2 of the License, or
9161b96c3SAlexander Shiyan  * (at your option) any later version.
10161b96c3SAlexander Shiyan  */
11161b96c3SAlexander Shiyan 
12161b96c3SAlexander Shiyan #include <linux/io.h>
13161b96c3SAlexander Shiyan #include <linux/clk.h>
14161b96c3SAlexander Shiyan #include <linux/init.h>
15161b96c3SAlexander Shiyan #include <linux/gpio.h>
16161b96c3SAlexander Shiyan #include <linux/delay.h>
17161b96c3SAlexander Shiyan #include <linux/module.h>
18161b96c3SAlexander Shiyan #include <linux/interrupt.h>
19161b96c3SAlexander Shiyan #include <linux/platform_device.h>
20161b96c3SAlexander Shiyan #include <linux/spi/spi.h>
21161b96c3SAlexander Shiyan #include <linux/platform_data/spi-clps711x.h>
22161b96c3SAlexander Shiyan 
23161b96c3SAlexander Shiyan #include <mach/hardware.h>
24161b96c3SAlexander Shiyan 
25161b96c3SAlexander Shiyan #define DRIVER_NAME	"spi-clps711x"
26161b96c3SAlexander Shiyan 
27161b96c3SAlexander Shiyan struct spi_clps711x_data {
28161b96c3SAlexander Shiyan 	struct completion	done;
29161b96c3SAlexander Shiyan 
30161b96c3SAlexander Shiyan 	struct clk		*spi_clk;
31161b96c3SAlexander Shiyan 	u32			max_speed_hz;
32161b96c3SAlexander Shiyan 
33161b96c3SAlexander Shiyan 	u8			*tx_buf;
34161b96c3SAlexander Shiyan 	u8			*rx_buf;
35161b96c3SAlexander Shiyan 	int			count;
36161b96c3SAlexander Shiyan 	int			len;
37161b96c3SAlexander Shiyan 
38161b96c3SAlexander Shiyan 	int			chipselect[0];
39161b96c3SAlexander Shiyan };
40161b96c3SAlexander Shiyan 
41161b96c3SAlexander Shiyan static int spi_clps711x_setup(struct spi_device *spi)
42161b96c3SAlexander Shiyan {
43161b96c3SAlexander Shiyan 	struct spi_clps711x_data *hw = spi_master_get_devdata(spi->master);
44161b96c3SAlexander Shiyan 
45161b96c3SAlexander Shiyan 	/* We are expect that SPI-device is not selected */
46161b96c3SAlexander Shiyan 	gpio_direction_output(hw->chipselect[spi->chip_select],
47161b96c3SAlexander Shiyan 			      !(spi->mode & SPI_CS_HIGH));
48161b96c3SAlexander Shiyan 
49161b96c3SAlexander Shiyan 	return 0;
50161b96c3SAlexander Shiyan }
51161b96c3SAlexander Shiyan 
52161b96c3SAlexander Shiyan static void spi_clps711x_setup_mode(struct spi_device *spi)
53161b96c3SAlexander Shiyan {
54161b96c3SAlexander Shiyan 	/* Setup edge for transfer */
55161b96c3SAlexander Shiyan 	if (spi->mode & SPI_CPHA)
56161b96c3SAlexander Shiyan 		clps_writew(clps_readw(SYSCON3) | SYSCON3_ADCCKNSEN, SYSCON3);
57161b96c3SAlexander Shiyan 	else
58161b96c3SAlexander Shiyan 		clps_writew(clps_readw(SYSCON3) & ~SYSCON3_ADCCKNSEN, SYSCON3);
59161b96c3SAlexander Shiyan }
60161b96c3SAlexander Shiyan 
61161b96c3SAlexander Shiyan static int spi_clps711x_setup_xfer(struct spi_device *spi,
62161b96c3SAlexander Shiyan 				   struct spi_transfer *xfer)
63161b96c3SAlexander Shiyan {
64161b96c3SAlexander Shiyan 	u32 speed = xfer->speed_hz ? : spi->max_speed_hz;
65766ed704SLaxman Dewangan 	u8 bpw = xfer->bits_per_word;
66161b96c3SAlexander Shiyan 	struct spi_clps711x_data *hw = spi_master_get_devdata(spi->master);
67161b96c3SAlexander Shiyan 
68161b96c3SAlexander Shiyan 	if (bpw != 8) {
69161b96c3SAlexander Shiyan 		dev_err(&spi->dev, "Unsupported master bus width %i\n", bpw);
70161b96c3SAlexander Shiyan 		return -EINVAL;
71161b96c3SAlexander Shiyan 	}
72161b96c3SAlexander Shiyan 
73161b96c3SAlexander Shiyan 	/* Setup SPI frequency divider */
74161b96c3SAlexander Shiyan 	if (!speed || (speed >= hw->max_speed_hz))
75161b96c3SAlexander Shiyan 		clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
76161b96c3SAlexander Shiyan 			    SYSCON1_ADCKSEL(3), SYSCON1);
77161b96c3SAlexander Shiyan 	else if (speed >= (hw->max_speed_hz / 2))
78161b96c3SAlexander Shiyan 		clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
79161b96c3SAlexander Shiyan 			    SYSCON1_ADCKSEL(2), SYSCON1);
80161b96c3SAlexander Shiyan 	else if (speed >= (hw->max_speed_hz / 8))
81161b96c3SAlexander Shiyan 		clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
82161b96c3SAlexander Shiyan 			    SYSCON1_ADCKSEL(1), SYSCON1);
83161b96c3SAlexander Shiyan 	else
84161b96c3SAlexander Shiyan 		clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) |
85161b96c3SAlexander Shiyan 			    SYSCON1_ADCKSEL(0), SYSCON1);
86161b96c3SAlexander Shiyan 
87161b96c3SAlexander Shiyan 	return 0;
88161b96c3SAlexander Shiyan }
89161b96c3SAlexander Shiyan 
90161b96c3SAlexander Shiyan static int spi_clps711x_transfer_one_message(struct spi_master *master,
91161b96c3SAlexander Shiyan 					     struct spi_message *msg)
92161b96c3SAlexander Shiyan {
93161b96c3SAlexander Shiyan 	struct spi_clps711x_data *hw = spi_master_get_devdata(master);
94161b96c3SAlexander Shiyan 	struct spi_transfer *xfer;
95161b96c3SAlexander Shiyan 	int status = 0, cs = hw->chipselect[msg->spi->chip_select];
96161b96c3SAlexander Shiyan 	u32 data;
97161b96c3SAlexander Shiyan 
98161b96c3SAlexander Shiyan 	spi_clps711x_setup_mode(msg->spi);
99161b96c3SAlexander Shiyan 
100161b96c3SAlexander Shiyan 	list_for_each_entry(xfer, &msg->transfers, transfer_list) {
101161b96c3SAlexander Shiyan 		if (spi_clps711x_setup_xfer(msg->spi, xfer)) {
102161b96c3SAlexander Shiyan 			status = -EINVAL;
103161b96c3SAlexander Shiyan 			goto out_xfr;
104161b96c3SAlexander Shiyan 		}
105161b96c3SAlexander Shiyan 
106161b96c3SAlexander Shiyan 		gpio_set_value(cs, !!(msg->spi->mode & SPI_CS_HIGH));
107161b96c3SAlexander Shiyan 
10816735d02SWolfram Sang 		reinit_completion(&hw->done);
109161b96c3SAlexander Shiyan 
110161b96c3SAlexander Shiyan 		hw->count = 0;
111161b96c3SAlexander Shiyan 		hw->len = xfer->len;
112161b96c3SAlexander Shiyan 		hw->tx_buf = (u8 *)xfer->tx_buf;
113161b96c3SAlexander Shiyan 		hw->rx_buf = (u8 *)xfer->rx_buf;
114161b96c3SAlexander Shiyan 
115161b96c3SAlexander Shiyan 		/* Initiate transfer */
116161b96c3SAlexander Shiyan 		data = hw->tx_buf ? hw->tx_buf[hw->count] : 0;
117161b96c3SAlexander Shiyan 		clps_writel(data | SYNCIO_FRMLEN(8) | SYNCIO_TXFRMEN, SYNCIO);
118161b96c3SAlexander Shiyan 
119161b96c3SAlexander Shiyan 		wait_for_completion(&hw->done);
120161b96c3SAlexander Shiyan 
121161b96c3SAlexander Shiyan 		if (xfer->delay_usecs)
122161b96c3SAlexander Shiyan 			udelay(xfer->delay_usecs);
123161b96c3SAlexander Shiyan 
124161b96c3SAlexander Shiyan 		if (xfer->cs_change ||
125161b96c3SAlexander Shiyan 		    list_is_last(&xfer->transfer_list, &msg->transfers))
126161b96c3SAlexander Shiyan 			gpio_set_value(cs, !(msg->spi->mode & SPI_CS_HIGH));
127161b96c3SAlexander Shiyan 
128161b96c3SAlexander Shiyan 		msg->actual_length += xfer->len;
129161b96c3SAlexander Shiyan 	}
130161b96c3SAlexander Shiyan 
131161b96c3SAlexander Shiyan out_xfr:
132161b96c3SAlexander Shiyan 	msg->status = status;
133161b96c3SAlexander Shiyan 	spi_finalize_current_message(master);
134161b96c3SAlexander Shiyan 
135161b96c3SAlexander Shiyan 	return 0;
136161b96c3SAlexander Shiyan }
137161b96c3SAlexander Shiyan 
138161b96c3SAlexander Shiyan static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
139161b96c3SAlexander Shiyan {
140161b96c3SAlexander Shiyan 	struct spi_clps711x_data *hw = (struct spi_clps711x_data *)dev_id;
141161b96c3SAlexander Shiyan 	u32 data;
142161b96c3SAlexander Shiyan 
143161b96c3SAlexander Shiyan 	/* Handle RX */
144161b96c3SAlexander Shiyan 	data = clps_readb(SYNCIO);
145161b96c3SAlexander Shiyan 	if (hw->rx_buf)
146161b96c3SAlexander Shiyan 		hw->rx_buf[hw->count] = (u8)data;
147161b96c3SAlexander Shiyan 
148161b96c3SAlexander Shiyan 	hw->count++;
149161b96c3SAlexander Shiyan 
150161b96c3SAlexander Shiyan 	/* Handle TX */
151161b96c3SAlexander Shiyan 	if (hw->count < hw->len) {
152161b96c3SAlexander Shiyan 		data = hw->tx_buf ? hw->tx_buf[hw->count] : 0;
153161b96c3SAlexander Shiyan 		clps_writel(data | SYNCIO_FRMLEN(8) | SYNCIO_TXFRMEN, SYNCIO);
154161b96c3SAlexander Shiyan 	} else
155161b96c3SAlexander Shiyan 		complete(&hw->done);
156161b96c3SAlexander Shiyan 
157161b96c3SAlexander Shiyan 	return IRQ_HANDLED;
158161b96c3SAlexander Shiyan }
159161b96c3SAlexander Shiyan 
160fd4a319bSGrant Likely static int spi_clps711x_probe(struct platform_device *pdev)
161161b96c3SAlexander Shiyan {
162161b96c3SAlexander Shiyan 	int i, ret;
163161b96c3SAlexander Shiyan 	struct spi_master *master;
164161b96c3SAlexander Shiyan 	struct spi_clps711x_data *hw;
165161b96c3SAlexander Shiyan 	struct spi_clps711x_pdata *pdata = dev_get_platdata(&pdev->dev);
166161b96c3SAlexander Shiyan 
167161b96c3SAlexander Shiyan 	if (!pdata) {
168161b96c3SAlexander Shiyan 		dev_err(&pdev->dev, "No platform data supplied\n");
169161b96c3SAlexander Shiyan 		return -EINVAL;
170161b96c3SAlexander Shiyan 	}
171161b96c3SAlexander Shiyan 
172161b96c3SAlexander Shiyan 	if (pdata->num_chipselect < 1) {
173161b96c3SAlexander Shiyan 		dev_err(&pdev->dev, "At least one CS must be defined\n");
174161b96c3SAlexander Shiyan 		return -EINVAL;
175161b96c3SAlexander Shiyan 	}
176161b96c3SAlexander Shiyan 
177161b96c3SAlexander Shiyan 	master = spi_alloc_master(&pdev->dev,
178161b96c3SAlexander Shiyan 				  sizeof(struct spi_clps711x_data) +
179161b96c3SAlexander Shiyan 				  sizeof(int) * pdata->num_chipselect);
180161b96c3SAlexander Shiyan 	if (!master) {
181161b96c3SAlexander Shiyan 		dev_err(&pdev->dev, "SPI allocating memory error\n");
182161b96c3SAlexander Shiyan 		return -ENOMEM;
183161b96c3SAlexander Shiyan 	}
184161b96c3SAlexander Shiyan 
185161b96c3SAlexander Shiyan 	master->bus_num = pdev->id;
186161b96c3SAlexander Shiyan 	master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
18724778be2SStephen Warren 	master->bits_per_word_mask = SPI_BPW_MASK(8);
188161b96c3SAlexander Shiyan 	master->num_chipselect = pdata->num_chipselect;
189161b96c3SAlexander Shiyan 	master->setup = spi_clps711x_setup;
190161b96c3SAlexander Shiyan 	master->transfer_one_message = spi_clps711x_transfer_one_message;
191161b96c3SAlexander Shiyan 
192161b96c3SAlexander Shiyan 	hw = spi_master_get_devdata(master);
193161b96c3SAlexander Shiyan 
194161b96c3SAlexander Shiyan 	for (i = 0; i < master->num_chipselect; i++) {
195161b96c3SAlexander Shiyan 		hw->chipselect[i] = pdata->chipselect[i];
196161b96c3SAlexander Shiyan 		if (!gpio_is_valid(hw->chipselect[i])) {
197161b96c3SAlexander Shiyan 			dev_err(&pdev->dev, "Invalid CS GPIO %i\n", i);
198161b96c3SAlexander Shiyan 			ret = -EINVAL;
199161b96c3SAlexander Shiyan 			goto err_out;
200161b96c3SAlexander Shiyan 		}
20198984796SAlexander Shiyan 		if (devm_gpio_request(&pdev->dev, hw->chipselect[i], NULL)) {
202161b96c3SAlexander Shiyan 			dev_err(&pdev->dev, "Can't get CS GPIO %i\n", i);
203161b96c3SAlexander Shiyan 			ret = -EINVAL;
204161b96c3SAlexander Shiyan 			goto err_out;
205161b96c3SAlexander Shiyan 		}
206161b96c3SAlexander Shiyan 	}
207161b96c3SAlexander Shiyan 
208161b96c3SAlexander Shiyan 	hw->spi_clk = devm_clk_get(&pdev->dev, "spi");
209161b96c3SAlexander Shiyan 	if (IS_ERR(hw->spi_clk)) {
210161b96c3SAlexander Shiyan 		dev_err(&pdev->dev, "Can't get clocks\n");
211161b96c3SAlexander Shiyan 		ret = PTR_ERR(hw->spi_clk);
212161b96c3SAlexander Shiyan 		goto err_out;
213161b96c3SAlexander Shiyan 	}
214161b96c3SAlexander Shiyan 	hw->max_speed_hz = clk_get_rate(hw->spi_clk);
215161b96c3SAlexander Shiyan 
216161b96c3SAlexander Shiyan 	init_completion(&hw->done);
217161b96c3SAlexander Shiyan 	platform_set_drvdata(pdev, master);
218161b96c3SAlexander Shiyan 
219161b96c3SAlexander Shiyan 	/* Disable extended mode due hardware problems */
220161b96c3SAlexander Shiyan 	clps_writew(clps_readw(SYSCON3) & ~SYSCON3_ADCCON, SYSCON3);
221161b96c3SAlexander Shiyan 
222161b96c3SAlexander Shiyan 	/* Clear possible pending interrupt */
223161b96c3SAlexander Shiyan 	clps_readl(SYNCIO);
224161b96c3SAlexander Shiyan 
225161b96c3SAlexander Shiyan 	ret = devm_request_irq(&pdev->dev, IRQ_SSEOTI, spi_clps711x_isr, 0,
226161b96c3SAlexander Shiyan 			       dev_name(&pdev->dev), hw);
227161b96c3SAlexander Shiyan 	if (ret) {
228161b96c3SAlexander Shiyan 		dev_err(&pdev->dev, "Can't request IRQ\n");
229c7083790SSachin Kamat 		goto err_out;
230161b96c3SAlexander Shiyan 	}
231161b96c3SAlexander Shiyan 
232c493fc4bSJingoo Han 	ret = devm_spi_register_master(&pdev->dev, master);
233161b96c3SAlexander Shiyan 	if (!ret) {
234161b96c3SAlexander Shiyan 		dev_info(&pdev->dev,
235161b96c3SAlexander Shiyan 			 "SPI bus driver initialized. Master clock %u Hz\n",
236161b96c3SAlexander Shiyan 			 hw->max_speed_hz);
237161b96c3SAlexander Shiyan 		return 0;
238161b96c3SAlexander Shiyan 	}
239161b96c3SAlexander Shiyan 
240161b96c3SAlexander Shiyan 	dev_err(&pdev->dev, "Failed to register master\n");
241161b96c3SAlexander Shiyan 
242161b96c3SAlexander Shiyan err_out:
243161b96c3SAlexander Shiyan 	spi_master_put(master);
244161b96c3SAlexander Shiyan 
245161b96c3SAlexander Shiyan 	return ret;
246161b96c3SAlexander Shiyan }
247161b96c3SAlexander Shiyan 
248161b96c3SAlexander Shiyan static struct platform_driver clps711x_spi_driver = {
249161b96c3SAlexander Shiyan 	.driver	= {
250161b96c3SAlexander Shiyan 		.name	= DRIVER_NAME,
251161b96c3SAlexander Shiyan 		.owner	= THIS_MODULE,
252161b96c3SAlexander Shiyan 	},
253161b96c3SAlexander Shiyan 	.probe	= spi_clps711x_probe,
254161b96c3SAlexander Shiyan };
255161b96c3SAlexander Shiyan module_platform_driver(clps711x_spi_driver);
256161b96c3SAlexander Shiyan 
257161b96c3SAlexander Shiyan MODULE_LICENSE("GPL");
258161b96c3SAlexander Shiyan MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
259161b96c3SAlexander Shiyan MODULE_DESCRIPTION("CLPS711X SPI bus driver");
260*350a9b33SAxel Lin MODULE_ALIAS("platform:" DRIVER_NAME);
261