xref: /openbmc/linux/drivers/spi/spi-clps711x.c (revision 3dc925945b00c231419e12c00c998cdcc3a6b8cf)
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>
20*3dc92594SAlexander Shiyan #include <linux/regmap.h>
21*3dc92594SAlexander Shiyan #include <linux/mfd/syscon.h>
22*3dc92594SAlexander Shiyan #include <linux/mfd/syscon/clps711x.h>
23161b96c3SAlexander Shiyan #include <linux/spi/spi.h>
24161b96c3SAlexander Shiyan #include <linux/platform_data/spi-clps711x.h>
25161b96c3SAlexander Shiyan 
26161b96c3SAlexander Shiyan #define DRIVER_NAME	"spi-clps711x"
27161b96c3SAlexander Shiyan 
28*3dc92594SAlexander Shiyan #define SYNCIO_FRMLEN(x)	((x) << 8)
29*3dc92594SAlexander Shiyan #define SYNCIO_TXFRMEN		(1 << 14)
30*3dc92594SAlexander Shiyan 
31161b96c3SAlexander Shiyan struct spi_clps711x_data {
32*3dc92594SAlexander Shiyan 	void __iomem		*syncio;
33*3dc92594SAlexander Shiyan 	struct regmap		*syscon;
34*3dc92594SAlexander Shiyan 	struct regmap		*syscon1;
35161b96c3SAlexander Shiyan 	struct clk		*spi_clk;
36161b96c3SAlexander Shiyan 	u32			max_speed_hz;
37161b96c3SAlexander Shiyan 
38161b96c3SAlexander Shiyan 	u8			*tx_buf;
39161b96c3SAlexander Shiyan 	u8			*rx_buf;
408dda9d9aSAlexander Shiyan 	unsigned int		bpw;
41161b96c3SAlexander Shiyan 	int			len;
42161b96c3SAlexander Shiyan };
43161b96c3SAlexander Shiyan 
44161b96c3SAlexander Shiyan static int spi_clps711x_setup(struct spi_device *spi)
45161b96c3SAlexander Shiyan {
46161b96c3SAlexander Shiyan 	/* We are expect that SPI-device is not selected */
473e9ea4b4SAlexander Shiyan 	gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH));
48161b96c3SAlexander Shiyan 
49161b96c3SAlexander Shiyan 	return 0;
50161b96c3SAlexander Shiyan }
51161b96c3SAlexander Shiyan 
528dda9d9aSAlexander Shiyan static void spi_clps711x_setup_xfer(struct spi_device *spi,
53161b96c3SAlexander Shiyan 				    struct spi_transfer *xfer)
54161b96c3SAlexander Shiyan {
55161b96c3SAlexander Shiyan 	struct spi_clps711x_data *hw = spi_master_get_devdata(spi->master);
56161b96c3SAlexander Shiyan 
57161b96c3SAlexander Shiyan 	/* Setup SPI frequency divider */
58bed890b4SAxel Lin 	if (!xfer->speed_hz || (xfer->speed_hz >= hw->max_speed_hz))
59*3dc92594SAlexander Shiyan 		regmap_update_bits(hw->syscon1, SYSCON_OFFSET,
60*3dc92594SAlexander Shiyan 				   SYSCON1_ADCKSEL_MASK, SYSCON1_ADCKSEL(3));
61bed890b4SAxel Lin 	else if (xfer->speed_hz >= (hw->max_speed_hz / 2))
62*3dc92594SAlexander Shiyan 		regmap_update_bits(hw->syscon1, SYSCON_OFFSET,
63*3dc92594SAlexander Shiyan 				   SYSCON1_ADCKSEL_MASK, SYSCON1_ADCKSEL(2));
64bed890b4SAxel Lin 	else if (xfer->speed_hz >= (hw->max_speed_hz / 8))
65*3dc92594SAlexander Shiyan 		regmap_update_bits(hw->syscon1, SYSCON_OFFSET,
66*3dc92594SAlexander Shiyan 				   SYSCON1_ADCKSEL_MASK, SYSCON1_ADCKSEL(1));
67161b96c3SAlexander Shiyan 	else
68*3dc92594SAlexander Shiyan 		regmap_update_bits(hw->syscon1, SYSCON_OFFSET,
69*3dc92594SAlexander Shiyan 				   SYSCON1_ADCKSEL_MASK, SYSCON1_ADCKSEL(0));
70161b96c3SAlexander Shiyan }
71161b96c3SAlexander Shiyan 
72bf5c2e27SAxel Lin static int spi_clps711x_prepare_message(struct spi_master *master,
73161b96c3SAlexander Shiyan 					struct spi_message *msg)
74161b96c3SAlexander Shiyan {
75*3dc92594SAlexander Shiyan 	struct spi_clps711x_data *hw = spi_master_get_devdata(master);
768dda9d9aSAlexander Shiyan 	struct spi_device *spi = msg->spi;
77161b96c3SAlexander Shiyan 
78*3dc92594SAlexander Shiyan 	/* Setup mode for transfer */
79*3dc92594SAlexander Shiyan 	return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN,
80*3dc92594SAlexander Shiyan 				  (spi->mode & SPI_CPHA) ?
81*3dc92594SAlexander Shiyan 				  SYSCON3_ADCCKNSEN : 0);
82bf5c2e27SAxel Lin }
83bf5c2e27SAxel Lin 
84bf5c2e27SAxel Lin static int spi_clps711x_transfer_one(struct spi_master *master,
85bf5c2e27SAxel Lin 				     struct spi_device *spi,
86bf5c2e27SAxel Lin 				     struct spi_transfer *xfer)
87bf5c2e27SAxel Lin {
88bf5c2e27SAxel Lin 	struct spi_clps711x_data *hw = spi_master_get_devdata(master);
89c7a26f12SAlexander Shiyan 	u8 data;
90c7a26f12SAlexander Shiyan 
918dda9d9aSAlexander Shiyan 	spi_clps711x_setup_xfer(spi, xfer);
92161b96c3SAlexander Shiyan 
93161b96c3SAlexander Shiyan 	hw->len = xfer->len;
94bed890b4SAxel Lin 	hw->bpw = xfer->bits_per_word;
95161b96c3SAlexander Shiyan 	hw->tx_buf = (u8 *)xfer->tx_buf;
96161b96c3SAlexander Shiyan 	hw->rx_buf = (u8 *)xfer->rx_buf;
97161b96c3SAlexander Shiyan 
98161b96c3SAlexander Shiyan 	/* Initiate transfer */
99c7a26f12SAlexander Shiyan 	data = hw->tx_buf ? *hw->tx_buf++ : 0;
100*3dc92594SAlexander Shiyan 	writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio);
101*3dc92594SAlexander Shiyan 
102bf5c2e27SAxel Lin 	return 1;
103161b96c3SAlexander Shiyan }
104161b96c3SAlexander Shiyan 
105161b96c3SAlexander Shiyan static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
106161b96c3SAlexander Shiyan {
107bf5c2e27SAxel Lin 	struct spi_master *master = dev_id;
108bf5c2e27SAxel Lin 	struct spi_clps711x_data *hw = spi_master_get_devdata(master);
109c7a26f12SAlexander Shiyan 	u8 data;
110161b96c3SAlexander Shiyan 
111161b96c3SAlexander Shiyan 	/* Handle RX */
112*3dc92594SAlexander Shiyan 	data = readb(hw->syncio);
113161b96c3SAlexander Shiyan 	if (hw->rx_buf)
114c7a26f12SAlexander Shiyan 		*hw->rx_buf++ = data;
115161b96c3SAlexander Shiyan 
116161b96c3SAlexander Shiyan 	/* Handle TX */
117c7a26f12SAlexander Shiyan 	if (--hw->len > 0) {
118c7a26f12SAlexander Shiyan 		data = hw->tx_buf ? *hw->tx_buf++ : 0;
119*3dc92594SAlexander Shiyan 		writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
120*3dc92594SAlexander Shiyan 		       hw->syncio);
121161b96c3SAlexander Shiyan 	} else
122bf5c2e27SAxel Lin 		spi_finalize_current_transfer(master);
123161b96c3SAlexander Shiyan 
124161b96c3SAlexander Shiyan 	return IRQ_HANDLED;
125161b96c3SAlexander Shiyan }
126161b96c3SAlexander Shiyan 
127fd4a319bSGrant Likely static int spi_clps711x_probe(struct platform_device *pdev)
128161b96c3SAlexander Shiyan {
129161b96c3SAlexander Shiyan 	struct spi_clps711x_data *hw;
130161b96c3SAlexander Shiyan 	struct spi_clps711x_pdata *pdata = dev_get_platdata(&pdev->dev);
131*3dc92594SAlexander Shiyan 	struct spi_master *master;
132*3dc92594SAlexander Shiyan 	struct resource *res;
133*3dc92594SAlexander Shiyan 	int i, irq, ret;
134161b96c3SAlexander Shiyan 
135161b96c3SAlexander Shiyan 	if (!pdata) {
136161b96c3SAlexander Shiyan 		dev_err(&pdev->dev, "No platform data supplied\n");
137161b96c3SAlexander Shiyan 		return -EINVAL;
138161b96c3SAlexander Shiyan 	}
139161b96c3SAlexander Shiyan 
140161b96c3SAlexander Shiyan 	if (pdata->num_chipselect < 1) {
141161b96c3SAlexander Shiyan 		dev_err(&pdev->dev, "At least one CS must be defined\n");
142161b96c3SAlexander Shiyan 		return -EINVAL;
143161b96c3SAlexander Shiyan 	}
144161b96c3SAlexander Shiyan 
145*3dc92594SAlexander Shiyan 	irq = platform_get_irq(pdev, 0);
146*3dc92594SAlexander Shiyan 	if (irq < 0)
147*3dc92594SAlexander Shiyan 		return irq;
148*3dc92594SAlexander Shiyan 
1493e9ea4b4SAlexander Shiyan 	master = spi_alloc_master(&pdev->dev, sizeof(*hw));
1503e9ea4b4SAlexander Shiyan 	if (!master)
151161b96c3SAlexander Shiyan 		return -ENOMEM;
1523e9ea4b4SAlexander Shiyan 
1533e9ea4b4SAlexander Shiyan 	master->cs_gpios = devm_kzalloc(&pdev->dev, sizeof(int) *
1543e9ea4b4SAlexander Shiyan 					pdata->num_chipselect, GFP_KERNEL);
1553e9ea4b4SAlexander Shiyan 	if (!master->cs_gpios) {
1563e9ea4b4SAlexander Shiyan 		ret = -ENOMEM;
1573e9ea4b4SAlexander Shiyan 		goto err_out;
158161b96c3SAlexander Shiyan 	}
159161b96c3SAlexander Shiyan 
160161b96c3SAlexander Shiyan 	master->bus_num = pdev->id;
161161b96c3SAlexander Shiyan 	master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
1628dda9d9aSAlexander Shiyan 	master->bits_per_word_mask =  SPI_BPW_RANGE_MASK(1, 8);
163161b96c3SAlexander Shiyan 	master->num_chipselect = pdata->num_chipselect;
164161b96c3SAlexander Shiyan 	master->setup = spi_clps711x_setup;
165bf5c2e27SAxel Lin 	master->prepare_message = spi_clps711x_prepare_message;
166bf5c2e27SAxel Lin 	master->transfer_one = spi_clps711x_transfer_one;
167161b96c3SAlexander Shiyan 
168161b96c3SAlexander Shiyan 	hw = spi_master_get_devdata(master);
169161b96c3SAlexander Shiyan 
170161b96c3SAlexander Shiyan 	for (i = 0; i < master->num_chipselect; i++) {
1713e9ea4b4SAlexander Shiyan 		master->cs_gpios[i] = pdata->chipselect[i];
172fcba212dSAxel Lin 		ret = devm_gpio_request(&pdev->dev, master->cs_gpios[i],
173fcba212dSAxel Lin 					DRIVER_NAME);
174fcba212dSAxel Lin 		if (ret) {
175161b96c3SAlexander Shiyan 			dev_err(&pdev->dev, "Can't get CS GPIO %i\n", i);
176161b96c3SAlexander Shiyan 			goto err_out;
177161b96c3SAlexander Shiyan 		}
178161b96c3SAlexander Shiyan 	}
179161b96c3SAlexander Shiyan 
180161b96c3SAlexander Shiyan 	hw->spi_clk = devm_clk_get(&pdev->dev, "spi");
181161b96c3SAlexander Shiyan 	if (IS_ERR(hw->spi_clk)) {
182161b96c3SAlexander Shiyan 		dev_err(&pdev->dev, "Can't get clocks\n");
183161b96c3SAlexander Shiyan 		ret = PTR_ERR(hw->spi_clk);
184161b96c3SAlexander Shiyan 		goto err_out;
185161b96c3SAlexander Shiyan 	}
186161b96c3SAlexander Shiyan 	hw->max_speed_hz = clk_get_rate(hw->spi_clk);
187161b96c3SAlexander Shiyan 
188161b96c3SAlexander Shiyan 	platform_set_drvdata(pdev, master);
189161b96c3SAlexander Shiyan 
190*3dc92594SAlexander Shiyan 	hw->syscon = syscon_regmap_lookup_by_pdevname("syscon.3");
191*3dc92594SAlexander Shiyan 	if (IS_ERR(hw->syscon)) {
192*3dc92594SAlexander Shiyan 		ret = PTR_ERR(hw->syscon);
193c7083790SSachin Kamat 		goto err_out;
194161b96c3SAlexander Shiyan 	}
195161b96c3SAlexander Shiyan 
196*3dc92594SAlexander Shiyan 	hw->syscon1 = syscon_regmap_lookup_by_pdevname("syscon.1");
197*3dc92594SAlexander Shiyan 	if (IS_ERR(hw->syscon1)) {
198*3dc92594SAlexander Shiyan 		ret = PTR_ERR(hw->syscon1);
199*3dc92594SAlexander Shiyan 		goto err_out;
200*3dc92594SAlexander Shiyan 	}
201*3dc92594SAlexander Shiyan 
202*3dc92594SAlexander Shiyan 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
203*3dc92594SAlexander Shiyan 	hw->syncio = devm_ioremap_resource(&pdev->dev, res);
204*3dc92594SAlexander Shiyan 	if (IS_ERR(hw->syncio)) {
205*3dc92594SAlexander Shiyan 		ret = PTR_ERR(hw->syncio);
206*3dc92594SAlexander Shiyan 		goto err_out;
207*3dc92594SAlexander Shiyan 	}
208*3dc92594SAlexander Shiyan 
209*3dc92594SAlexander Shiyan 	/* Disable extended mode due hardware problems */
210*3dc92594SAlexander Shiyan 	regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0);
211*3dc92594SAlexander Shiyan 
212*3dc92594SAlexander Shiyan 	/* Clear possible pending interrupt */
213*3dc92594SAlexander Shiyan 	readl(hw->syncio);
214*3dc92594SAlexander Shiyan 
215*3dc92594SAlexander Shiyan 	ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0,
216*3dc92594SAlexander Shiyan 			       dev_name(&pdev->dev), master);
217*3dc92594SAlexander Shiyan 	if (ret)
218*3dc92594SAlexander Shiyan 		goto err_out;
219*3dc92594SAlexander Shiyan 
220c493fc4bSJingoo Han 	ret = devm_spi_register_master(&pdev->dev, master);
221161b96c3SAlexander Shiyan 	if (!ret) {
222161b96c3SAlexander Shiyan 		dev_info(&pdev->dev,
223161b96c3SAlexander Shiyan 			 "SPI bus driver initialized. Master clock %u Hz\n",
224161b96c3SAlexander Shiyan 			 hw->max_speed_hz);
225161b96c3SAlexander Shiyan 		return 0;
226161b96c3SAlexander Shiyan 	}
227161b96c3SAlexander Shiyan 
228161b96c3SAlexander Shiyan 	dev_err(&pdev->dev, "Failed to register master\n");
229161b96c3SAlexander Shiyan 
230161b96c3SAlexander Shiyan err_out:
231161b96c3SAlexander Shiyan 	spi_master_put(master);
232161b96c3SAlexander Shiyan 
233161b96c3SAlexander Shiyan 	return ret;
234161b96c3SAlexander Shiyan }
235161b96c3SAlexander Shiyan 
236161b96c3SAlexander Shiyan static struct platform_driver clps711x_spi_driver = {
237161b96c3SAlexander Shiyan 	.driver	= {
238161b96c3SAlexander Shiyan 		.name	= DRIVER_NAME,
239161b96c3SAlexander Shiyan 		.owner	= THIS_MODULE,
240161b96c3SAlexander Shiyan 	},
241161b96c3SAlexander Shiyan 	.probe	= spi_clps711x_probe,
242161b96c3SAlexander Shiyan };
243161b96c3SAlexander Shiyan module_platform_driver(clps711x_spi_driver);
244161b96c3SAlexander Shiyan 
245161b96c3SAlexander Shiyan MODULE_LICENSE("GPL");
246161b96c3SAlexander Shiyan MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
247161b96c3SAlexander Shiyan MODULE_DESCRIPTION("CLPS711X SPI bus driver");
248350a9b33SAxel Lin MODULE_ALIAS("platform:" DRIVER_NAME);
249