blob: 0bef5ce0809445516fffff99387fcdc4c2a69f37 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Alexander Shiyan161b96c2012-11-07 21:30:29 +04002/*
3 * CLPS711X SPI bus driver
4 *
Alexander Shiyan6acaadc2016-07-06 16:53:12 +02005 * Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru>
Alexander Shiyan161b96c2012-11-07 21:30:29 +04006 */
7
8#include <linux/io.h>
9#include <linux/clk.h>
Linus Walleij054320b2019-01-07 16:51:54 +010010#include <linux/gpio/consumer.h>
Alexander Shiyan161b96c2012-11-07 21:30:29 +040011#include <linux/module.h>
12#include <linux/interrupt.h>
13#include <linux/platform_device.h>
Alexander Shiyan3dc92592014-03-22 10:57:35 +040014#include <linux/regmap.h>
15#include <linux/mfd/syscon.h>
16#include <linux/mfd/syscon/clps711x.h>
Alexander Shiyan161b96c2012-11-07 21:30:29 +040017#include <linux/spi/spi.h>
Alexander Shiyan161b96c2012-11-07 21:30:29 +040018
Alexander Shiyan6acaadc2016-07-06 16:53:12 +020019#define DRIVER_NAME "clps711x-spi"
Alexander Shiyan161b96c2012-11-07 21:30:29 +040020
Alexander Shiyan3dc92592014-03-22 10:57:35 +040021#define SYNCIO_FRMLEN(x) ((x) << 8)
22#define SYNCIO_TXFRMEN (1 << 14)
23
Alexander Shiyan161b96c2012-11-07 21:30:29 +040024struct spi_clps711x_data {
Alexander Shiyan3dc92592014-03-22 10:57:35 +040025 void __iomem *syncio;
26 struct regmap *syscon;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040027 struct clk *spi_clk;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040028
29 u8 *tx_buf;
30 u8 *rx_buf;
Alexander Shiyan8dda9d92014-02-02 10:59:49 +040031 unsigned int bpw;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040032 int len;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040033};
34
Axel Linbf5c2e22014-02-18 17:15:54 +080035static int spi_clps711x_prepare_message(struct spi_master *master,
36 struct spi_message *msg)
Alexander Shiyan161b96c2012-11-07 21:30:29 +040037{
Alexander Shiyan3dc92592014-03-22 10:57:35 +040038 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
Alexander Shiyan8dda9d92014-02-02 10:59:49 +040039 struct spi_device *spi = msg->spi;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040040
Alexander Shiyan3dc92592014-03-22 10:57:35 +040041 /* Setup mode for transfer */
42 return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN,
43 (spi->mode & SPI_CPHA) ?
44 SYSCON3_ADCCKNSEN : 0);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040045}
46
Axel Linbf5c2e22014-02-18 17:15:54 +080047static int spi_clps711x_transfer_one(struct spi_master *master,
48 struct spi_device *spi,
49 struct spi_transfer *xfer)
50{
51 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
52 u8 data;
53
Alexander Shiyana5b4b232014-09-20 11:05:09 +040054 clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz);
Axel Linbf5c2e22014-02-18 17:15:54 +080055
56 hw->len = xfer->len;
Axel Linbed890b2014-03-02 23:24:18 +080057 hw->bpw = xfer->bits_per_word;
Axel Linbf5c2e22014-02-18 17:15:54 +080058 hw->tx_buf = (u8 *)xfer->tx_buf;
59 hw->rx_buf = (u8 *)xfer->rx_buf;
60
61 /* Initiate transfer */
62 data = hw->tx_buf ? *hw->tx_buf++ : 0;
Alexander Shiyan3dc92592014-03-22 10:57:35 +040063 writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio);
64
Axel Linbf5c2e22014-02-18 17:15:54 +080065 return 1;
66}
67
Alexander Shiyan161b96c2012-11-07 21:30:29 +040068static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
69{
Axel Linbf5c2e22014-02-18 17:15:54 +080070 struct spi_master *master = dev_id;
71 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
Alexander Shiyanc7a26f12014-02-02 10:59:48 +040072 u8 data;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040073
74 /* Handle RX */
Alexander Shiyan3dc92592014-03-22 10:57:35 +040075 data = readb(hw->syncio);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040076 if (hw->rx_buf)
Alexander Shiyanc7a26f12014-02-02 10:59:48 +040077 *hw->rx_buf++ = data;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040078
79 /* Handle TX */
Alexander Shiyanc7a26f12014-02-02 10:59:48 +040080 if (--hw->len > 0) {
81 data = hw->tx_buf ? *hw->tx_buf++ : 0;
Alexander Shiyan3dc92592014-03-22 10:57:35 +040082 writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
83 hw->syncio);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040084 } else
Axel Linbf5c2e22014-02-18 17:15:54 +080085 spi_finalize_current_transfer(master);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040086
87 return IRQ_HANDLED;
88}
89
Grant Likelyfd4a3192012-12-07 16:57:14 +000090static int spi_clps711x_probe(struct platform_device *pdev)
Alexander Shiyan161b96c2012-11-07 21:30:29 +040091{
Alexander Shiyan161b96c2012-11-07 21:30:29 +040092 struct spi_clps711x_data *hw;
Alexander Shiyan3dc92592014-03-22 10:57:35 +040093 struct spi_master *master;
Alexander Shiyan6acaadc2016-07-06 16:53:12 +020094 int irq, ret;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040095
Alexander Shiyan3dc92592014-03-22 10:57:35 +040096 irq = platform_get_irq(pdev, 0);
97 if (irq < 0)
98 return irq;
99
Alexander Shiyan3e9ea4b2014-02-02 10:59:50 +0400100 master = spi_alloc_master(&pdev->dev, sizeof(*hw));
101 if (!master)
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400102 return -ENOMEM;
Alexander Shiyan3e9ea4b2014-02-02 10:59:50 +0400103
Linus Walleij054320b2019-01-07 16:51:54 +0100104 master->use_gpio_descriptors = true;
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200105 master->bus_num = -1;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400106 master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
Junhao He39062412021-02-04 19:08:06 +0800107 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8);
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200108 master->dev.of_node = pdev->dev.of_node;
Axel Linbf5c2e22014-02-18 17:15:54 +0800109 master->prepare_message = spi_clps711x_prepare_message;
110 master->transfer_one = spi_clps711x_transfer_one;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400111
112 hw = spi_master_get_devdata(master);
113
Alexander Shiyana5b4b232014-09-20 11:05:09 +0400114 hw->spi_clk = devm_clk_get(&pdev->dev, NULL);
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400115 if (IS_ERR(hw->spi_clk)) {
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400116 ret = PTR_ERR(hw->spi_clk);
117 goto err_out;
118 }
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400119
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200120 hw->syscon =
121 syscon_regmap_lookup_by_compatible("cirrus,ep7209-syscon3");
Alexander Shiyan3dc92592014-03-22 10:57:35 +0400122 if (IS_ERR(hw->syscon)) {
123 ret = PTR_ERR(hw->syscon);
Sachin Kamatc7083792013-09-27 15:32:53 +0530124 goto err_out;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400125 }
126
YueHaibingae437242019-09-04 21:58:52 +0800127 hw->syncio = devm_platform_ioremap_resource(pdev, 0);
Alexander Shiyan3dc92592014-03-22 10:57:35 +0400128 if (IS_ERR(hw->syncio)) {
129 ret = PTR_ERR(hw->syncio);
130 goto err_out;
131 }
132
133 /* Disable extended mode due hardware problems */
134 regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0);
135
136 /* Clear possible pending interrupt */
137 readl(hw->syncio);
138
139 ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0,
140 dev_name(&pdev->dev), master);
141 if (ret)
142 goto err_out;
143
Jingoo Hanc493fc42013-09-24 13:27:48 +0900144 ret = devm_spi_register_master(&pdev->dev, master);
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200145 if (!ret)
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400146 return 0;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400147
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400148err_out:
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400149 spi_master_put(master);
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400150
151 return ret;
152}
153
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200154static const struct of_device_id clps711x_spi_dt_ids[] = {
155 { .compatible = "cirrus,ep7209-spi", },
156 { }
157};
158MODULE_DEVICE_TABLE(of, clps711x_spi_dt_ids);
159
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400160static struct platform_driver clps711x_spi_driver = {
161 .driver = {
162 .name = DRIVER_NAME,
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200163 .of_match_table = clps711x_spi_dt_ids,
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400164 },
165 .probe = spi_clps711x_probe,
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400166};
167module_platform_driver(clps711x_spi_driver);
168
169MODULE_LICENSE("GPL");
170MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
171MODULE_DESCRIPTION("CLPS711X SPI bus driver");
Axel Lin350a9b32014-01-14 17:01:54 +0800172MODULE_ALIAS("platform:" DRIVER_NAME);