]> git.ipfire.org Git - thirdparty/linux.git/blame - drivers/spi/spi-clps711x.c
spi: clps711x: Convert to use CS GPIO descriptors
[thirdparty/linux.git] / drivers / spi / spi-clps711x.c
CommitLineData
161b96c3
AS
1/*
2 * CLPS711X SPI bus driver
3 *
6acaadc8 4 * Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru>
161b96c3
AS
5 *
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>
054320b2 14#include <linux/gpio/consumer.h>
161b96c3
AS
15#include <linux/module.h>
16#include <linux/interrupt.h>
17#include <linux/platform_device.h>
3dc92594
AS
18#include <linux/regmap.h>
19#include <linux/mfd/syscon.h>
20#include <linux/mfd/syscon/clps711x.h>
161b96c3 21#include <linux/spi/spi.h>
161b96c3 22
6acaadc8 23#define DRIVER_NAME "clps711x-spi"
161b96c3 24
3dc92594
AS
25#define SYNCIO_FRMLEN(x) ((x) << 8)
26#define SYNCIO_TXFRMEN (1 << 14)
27
161b96c3 28struct spi_clps711x_data {
3dc92594
AS
29 void __iomem *syncio;
30 struct regmap *syscon;
161b96c3 31 struct clk *spi_clk;
161b96c3
AS
32
33 u8 *tx_buf;
34 u8 *rx_buf;
8dda9d9a 35 unsigned int bpw;
161b96c3 36 int len;
161b96c3
AS
37};
38
bf5c2e27
AL
39static int spi_clps711x_prepare_message(struct spi_master *master,
40 struct spi_message *msg)
161b96c3 41{
3dc92594 42 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
8dda9d9a 43 struct spi_device *spi = msg->spi;
161b96c3 44
3dc92594
AS
45 /* Setup mode for transfer */
46 return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN,
47 (spi->mode & SPI_CPHA) ?
48 SYSCON3_ADCCKNSEN : 0);
bf5c2e27 49}
161b96c3 50
bf5c2e27
AL
51static int spi_clps711x_transfer_one(struct spi_master *master,
52 struct spi_device *spi,
53 struct spi_transfer *xfer)
54{
55 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
56 u8 data;
161b96c3 57
a5b4b234 58 clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz);
161b96c3 59
bf5c2e27 60 hw->len = xfer->len;
bed890b4 61 hw->bpw = xfer->bits_per_word;
bf5c2e27
AL
62 hw->tx_buf = (u8 *)xfer->tx_buf;
63 hw->rx_buf = (u8 *)xfer->rx_buf;
161b96c3 64
bf5c2e27
AL
65 /* Initiate transfer */
66 data = hw->tx_buf ? *hw->tx_buf++ : 0;
3dc92594
AS
67 writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio);
68
bf5c2e27 69 return 1;
161b96c3
AS
70}
71
72static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
73{
bf5c2e27
AL
74 struct spi_master *master = dev_id;
75 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
c7a26f12 76 u8 data;
161b96c3
AS
77
78 /* Handle RX */
3dc92594 79 data = readb(hw->syncio);
161b96c3 80 if (hw->rx_buf)
c7a26f12 81 *hw->rx_buf++ = data;
161b96c3
AS
82
83 /* Handle TX */
c7a26f12
AS
84 if (--hw->len > 0) {
85 data = hw->tx_buf ? *hw->tx_buf++ : 0;
3dc92594
AS
86 writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
87 hw->syncio);
161b96c3 88 } else
bf5c2e27 89 spi_finalize_current_transfer(master);
161b96c3
AS
90
91 return IRQ_HANDLED;
92}
93
fd4a319b 94static int spi_clps711x_probe(struct platform_device *pdev)
161b96c3 95{
161b96c3 96 struct spi_clps711x_data *hw;
3dc92594
AS
97 struct spi_master *master;
98 struct resource *res;
6acaadc8 99 int irq, ret;
161b96c3 100
3dc92594
AS
101 irq = platform_get_irq(pdev, 0);
102 if (irq < 0)
103 return irq;
104
3e9ea4b4
AS
105 master = spi_alloc_master(&pdev->dev, sizeof(*hw));
106 if (!master)
161b96c3 107 return -ENOMEM;
3e9ea4b4 108
054320b2 109 master->use_gpio_descriptors = true;
6acaadc8 110 master->bus_num = -1;
161b96c3 111 master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
8dda9d9a 112 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8);
6acaadc8 113 master->dev.of_node = pdev->dev.of_node;
bf5c2e27
AL
114 master->prepare_message = spi_clps711x_prepare_message;
115 master->transfer_one = spi_clps711x_transfer_one;
161b96c3
AS
116
117 hw = spi_master_get_devdata(master);
118
a5b4b234 119 hw->spi_clk = devm_clk_get(&pdev->dev, NULL);
161b96c3 120 if (IS_ERR(hw->spi_clk)) {
161b96c3
AS
121 ret = PTR_ERR(hw->spi_clk);
122 goto err_out;
123 }
161b96c3 124
6acaadc8
AS
125 hw->syscon =
126 syscon_regmap_lookup_by_compatible("cirrus,ep7209-syscon3");
3dc92594
AS
127 if (IS_ERR(hw->syscon)) {
128 ret = PTR_ERR(hw->syscon);
129 goto err_out;
130 }
131
3dc92594
AS
132 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
133 hw->syncio = devm_ioremap_resource(&pdev->dev, res);
134 if (IS_ERR(hw->syncio)) {
135 ret = PTR_ERR(hw->syncio);
136 goto err_out;
137 }
138
161b96c3 139 /* Disable extended mode due hardware problems */
3dc92594 140 regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0);
161b96c3
AS
141
142 /* Clear possible pending interrupt */
3dc92594 143 readl(hw->syncio);
161b96c3 144
3dc92594 145 ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0,
bf5c2e27 146 dev_name(&pdev->dev), master);
3dc92594 147 if (ret)
c7083790 148 goto err_out;
161b96c3 149
c493fc4b 150 ret = devm_spi_register_master(&pdev->dev, master);
6acaadc8 151 if (!ret)
161b96c3 152 return 0;
161b96c3 153
161b96c3 154err_out:
161b96c3 155 spi_master_put(master);
161b96c3
AS
156
157 return ret;
158}
159
6acaadc8
AS
160static const struct of_device_id clps711x_spi_dt_ids[] = {
161 { .compatible = "cirrus,ep7209-spi", },
162 { }
163};
164MODULE_DEVICE_TABLE(of, clps711x_spi_dt_ids);
165
161b96c3
AS
166static struct platform_driver clps711x_spi_driver = {
167 .driver = {
168 .name = DRIVER_NAME,
6acaadc8 169 .of_match_table = clps711x_spi_dt_ids,
161b96c3
AS
170 },
171 .probe = spi_clps711x_probe,
161b96c3
AS
172};
173module_platform_driver(clps711x_spi_driver);
174
175MODULE_LICENSE("GPL");
176MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
177MODULE_DESCRIPTION("CLPS711X SPI bus driver");
350a9b33 178MODULE_ALIAS("platform:" DRIVER_NAME);