xref: /openbmc/linux/drivers/spi/spi-clps711x.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2161b96c3SAlexander Shiyan /*
3161b96c3SAlexander Shiyan  *  CLPS711X SPI bus driver
4161b96c3SAlexander Shiyan  *
56acaadc8SAlexander Shiyan  *  Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru>
6161b96c3SAlexander Shiyan  */
7161b96c3SAlexander Shiyan 
8161b96c3SAlexander Shiyan #include <linux/io.h>
9161b96c3SAlexander Shiyan #include <linux/clk.h>
10054320b2SLinus Walleij #include <linux/gpio/consumer.h>
11161b96c3SAlexander Shiyan #include <linux/module.h>
12b0ceb621SAlexander Shiyan #include <linux/of.h>
13161b96c3SAlexander Shiyan #include <linux/interrupt.h>
14161b96c3SAlexander Shiyan #include <linux/platform_device.h>
153dc92594SAlexander Shiyan #include <linux/regmap.h>
163dc92594SAlexander Shiyan #include <linux/mfd/syscon.h>
173dc92594SAlexander Shiyan #include <linux/mfd/syscon/clps711x.h>
18161b96c3SAlexander Shiyan #include <linux/spi/spi.h>
19161b96c3SAlexander Shiyan 
206acaadc8SAlexander Shiyan #define DRIVER_NAME		"clps711x-spi"
21161b96c3SAlexander Shiyan 
223dc92594SAlexander Shiyan #define SYNCIO_FRMLEN(x)	((x) << 8)
233dc92594SAlexander Shiyan #define SYNCIO_TXFRMEN		(1 << 14)
243dc92594SAlexander Shiyan 
25161b96c3SAlexander Shiyan struct spi_clps711x_data {
263dc92594SAlexander Shiyan 	void __iomem		*syncio;
273dc92594SAlexander Shiyan 	struct regmap		*syscon;
28161b96c3SAlexander Shiyan 	struct clk		*spi_clk;
29161b96c3SAlexander Shiyan 
30161b96c3SAlexander Shiyan 	u8			*tx_buf;
31161b96c3SAlexander Shiyan 	u8			*rx_buf;
328dda9d9aSAlexander Shiyan 	unsigned int		bpw;
33161b96c3SAlexander Shiyan 	int			len;
34161b96c3SAlexander Shiyan };
35161b96c3SAlexander Shiyan 
spi_clps711x_prepare_message(struct spi_controller * host,struct spi_message * msg)36*1baba77bSYang Yingliang static int spi_clps711x_prepare_message(struct spi_controller *host,
37161b96c3SAlexander Shiyan 					struct spi_message *msg)
38161b96c3SAlexander Shiyan {
39*1baba77bSYang Yingliang 	struct spi_clps711x_data *hw = spi_controller_get_devdata(host);
408dda9d9aSAlexander Shiyan 	struct spi_device *spi = msg->spi;
41161b96c3SAlexander Shiyan 
423dc92594SAlexander Shiyan 	/* Setup mode for transfer */
433dc92594SAlexander Shiyan 	return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN,
443dc92594SAlexander Shiyan 				  (spi->mode & SPI_CPHA) ?
453dc92594SAlexander Shiyan 				  SYSCON3_ADCCKNSEN : 0);
46bf5c2e27SAxel Lin }
47bf5c2e27SAxel Lin 
spi_clps711x_transfer_one(struct spi_controller * host,struct spi_device * spi,struct spi_transfer * xfer)48*1baba77bSYang Yingliang static int spi_clps711x_transfer_one(struct spi_controller *host,
49bf5c2e27SAxel Lin 				     struct spi_device *spi,
50bf5c2e27SAxel Lin 				     struct spi_transfer *xfer)
51bf5c2e27SAxel Lin {
52*1baba77bSYang Yingliang 	struct spi_clps711x_data *hw = spi_controller_get_devdata(host);
53c7a26f12SAlexander Shiyan 	u8 data;
54c7a26f12SAlexander Shiyan 
55a5b4b234SAlexander Shiyan 	clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz);
56161b96c3SAlexander Shiyan 
57161b96c3SAlexander Shiyan 	hw->len = xfer->len;
58bed890b4SAxel Lin 	hw->bpw = xfer->bits_per_word;
59161b96c3SAlexander Shiyan 	hw->tx_buf = (u8 *)xfer->tx_buf;
60161b96c3SAlexander Shiyan 	hw->rx_buf = (u8 *)xfer->rx_buf;
61161b96c3SAlexander Shiyan 
62161b96c3SAlexander Shiyan 	/* Initiate transfer */
63c7a26f12SAlexander Shiyan 	data = hw->tx_buf ? *hw->tx_buf++ : 0;
643dc92594SAlexander Shiyan 	writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio);
653dc92594SAlexander Shiyan 
66bf5c2e27SAxel Lin 	return 1;
67161b96c3SAlexander Shiyan }
68161b96c3SAlexander Shiyan 
spi_clps711x_isr(int irq,void * dev_id)69161b96c3SAlexander Shiyan static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
70161b96c3SAlexander Shiyan {
71*1baba77bSYang Yingliang 	struct spi_controller *host = dev_id;
72*1baba77bSYang Yingliang 	struct spi_clps711x_data *hw = spi_controller_get_devdata(host);
73c7a26f12SAlexander Shiyan 	u8 data;
74161b96c3SAlexander Shiyan 
75161b96c3SAlexander Shiyan 	/* Handle RX */
763dc92594SAlexander Shiyan 	data = readb(hw->syncio);
77161b96c3SAlexander Shiyan 	if (hw->rx_buf)
78c7a26f12SAlexander Shiyan 		*hw->rx_buf++ = data;
79161b96c3SAlexander Shiyan 
80161b96c3SAlexander Shiyan 	/* Handle TX */
81c7a26f12SAlexander Shiyan 	if (--hw->len > 0) {
82c7a26f12SAlexander Shiyan 		data = hw->tx_buf ? *hw->tx_buf++ : 0;
833dc92594SAlexander Shiyan 		writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
843dc92594SAlexander Shiyan 		       hw->syncio);
85161b96c3SAlexander Shiyan 	} else
86*1baba77bSYang Yingliang 		spi_finalize_current_transfer(host);
87161b96c3SAlexander Shiyan 
88161b96c3SAlexander Shiyan 	return IRQ_HANDLED;
89161b96c3SAlexander Shiyan }
90161b96c3SAlexander Shiyan 
spi_clps711x_probe(struct platform_device * pdev)91fd4a319bSGrant Likely static int spi_clps711x_probe(struct platform_device *pdev)
92161b96c3SAlexander Shiyan {
93b0ceb621SAlexander Shiyan 	struct device_node *np = pdev->dev.of_node;
94161b96c3SAlexander Shiyan 	struct spi_clps711x_data *hw;
95*1baba77bSYang Yingliang 	struct spi_controller *host;
966acaadc8SAlexander Shiyan 	int irq, ret;
97161b96c3SAlexander Shiyan 
983dc92594SAlexander Shiyan 	irq = platform_get_irq(pdev, 0);
993dc92594SAlexander Shiyan 	if (irq < 0)
1003dc92594SAlexander Shiyan 		return irq;
1013dc92594SAlexander Shiyan 
102*1baba77bSYang Yingliang 	host = spi_alloc_host(&pdev->dev, sizeof(*hw));
103*1baba77bSYang Yingliang 	if (!host)
104161b96c3SAlexander Shiyan 		return -ENOMEM;
1053e9ea4b4SAlexander Shiyan 
106*1baba77bSYang Yingliang 	host->use_gpio_descriptors = true;
107*1baba77bSYang Yingliang 	host->bus_num = -1;
108*1baba77bSYang Yingliang 	host->mode_bits = SPI_CPHA | SPI_CS_HIGH;
109*1baba77bSYang Yingliang 	host->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8);
110*1baba77bSYang Yingliang 	host->dev.of_node = pdev->dev.of_node;
111*1baba77bSYang Yingliang 	host->prepare_message = spi_clps711x_prepare_message;
112*1baba77bSYang Yingliang 	host->transfer_one = spi_clps711x_transfer_one;
113161b96c3SAlexander Shiyan 
114*1baba77bSYang Yingliang 	hw = spi_controller_get_devdata(host);
115161b96c3SAlexander Shiyan 
116a5b4b234SAlexander Shiyan 	hw->spi_clk = devm_clk_get(&pdev->dev, NULL);
117161b96c3SAlexander Shiyan 	if (IS_ERR(hw->spi_clk)) {
118161b96c3SAlexander Shiyan 		ret = PTR_ERR(hw->spi_clk);
119161b96c3SAlexander Shiyan 		goto err_out;
120161b96c3SAlexander Shiyan 	}
121161b96c3SAlexander Shiyan 
122b0ceb621SAlexander Shiyan 	hw->syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
1233dc92594SAlexander Shiyan 	if (IS_ERR(hw->syscon)) {
1243dc92594SAlexander Shiyan 		ret = PTR_ERR(hw->syscon);
125c7083790SSachin Kamat 		goto err_out;
126161b96c3SAlexander Shiyan 	}
127161b96c3SAlexander Shiyan 
128ae43724dSYueHaibing 	hw->syncio = devm_platform_ioremap_resource(pdev, 0);
1293dc92594SAlexander Shiyan 	if (IS_ERR(hw->syncio)) {
1303dc92594SAlexander Shiyan 		ret = PTR_ERR(hw->syncio);
1313dc92594SAlexander Shiyan 		goto err_out;
1323dc92594SAlexander Shiyan 	}
1333dc92594SAlexander Shiyan 
1343dc92594SAlexander Shiyan 	/* Disable extended mode due hardware problems */
1353dc92594SAlexander Shiyan 	regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0);
1363dc92594SAlexander Shiyan 
1373dc92594SAlexander Shiyan 	/* Clear possible pending interrupt */
1383dc92594SAlexander Shiyan 	readl(hw->syncio);
1393dc92594SAlexander Shiyan 
1403dc92594SAlexander Shiyan 	ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0,
141*1baba77bSYang Yingliang 			       dev_name(&pdev->dev), host);
1423dc92594SAlexander Shiyan 	if (ret)
1433dc92594SAlexander Shiyan 		goto err_out;
1443dc92594SAlexander Shiyan 
145*1baba77bSYang Yingliang 	ret = devm_spi_register_controller(&pdev->dev, host);
1466acaadc8SAlexander Shiyan 	if (!ret)
147161b96c3SAlexander Shiyan 		return 0;
148161b96c3SAlexander Shiyan 
149161b96c3SAlexander Shiyan err_out:
150*1baba77bSYang Yingliang 	spi_controller_put(host);
151161b96c3SAlexander Shiyan 
152161b96c3SAlexander Shiyan 	return ret;
153161b96c3SAlexander Shiyan }
154161b96c3SAlexander Shiyan 
1556acaadc8SAlexander Shiyan static const struct of_device_id clps711x_spi_dt_ids[] = {
1566acaadc8SAlexander Shiyan 	{ .compatible = "cirrus,ep7209-spi", },
1576acaadc8SAlexander Shiyan 	{ }
1586acaadc8SAlexander Shiyan };
1596acaadc8SAlexander Shiyan MODULE_DEVICE_TABLE(of, clps711x_spi_dt_ids);
1606acaadc8SAlexander Shiyan 
161161b96c3SAlexander Shiyan static struct platform_driver clps711x_spi_driver = {
162161b96c3SAlexander Shiyan 	.driver	= {
163161b96c3SAlexander Shiyan 		.name	= DRIVER_NAME,
1646acaadc8SAlexander Shiyan 		.of_match_table = clps711x_spi_dt_ids,
165161b96c3SAlexander Shiyan 	},
166161b96c3SAlexander Shiyan 	.probe	= spi_clps711x_probe,
167161b96c3SAlexander Shiyan };
168161b96c3SAlexander Shiyan module_platform_driver(clps711x_spi_driver);
169161b96c3SAlexander Shiyan 
170161b96c3SAlexander Shiyan MODULE_LICENSE("GPL");
171161b96c3SAlexander Shiyan MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
172161b96c3SAlexander Shiyan MODULE_DESCRIPTION("CLPS711X SPI bus driver");
173350a9b33SAxel Lin MODULE_ALIAS("platform:" DRIVER_NAME);
174