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