0001
0002
0003
0004
0005
0006
0007
0008 #include <linux/io.h>
0009 #include <linux/clk.h>
0010 #include <linux/gpio/consumer.h>
0011 #include <linux/module.h>
0012 #include <linux/of.h>
0013 #include <linux/interrupt.h>
0014 #include <linux/platform_device.h>
0015 #include <linux/regmap.h>
0016 #include <linux/mfd/syscon.h>
0017 #include <linux/mfd/syscon/clps711x.h>
0018 #include <linux/spi/spi.h>
0019
0020 #define DRIVER_NAME "clps711x-spi"
0021
0022 #define SYNCIO_FRMLEN(x) ((x) << 8)
0023 #define SYNCIO_TXFRMEN (1 << 14)
0024
0025 struct spi_clps711x_data {
0026 void __iomem *syncio;
0027 struct regmap *syscon;
0028 struct clk *spi_clk;
0029
0030 u8 *tx_buf;
0031 u8 *rx_buf;
0032 unsigned int bpw;
0033 int len;
0034 };
0035
0036 static int spi_clps711x_prepare_message(struct spi_master *master,
0037 struct spi_message *msg)
0038 {
0039 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
0040 struct spi_device *spi = msg->spi;
0041
0042
0043 return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN,
0044 (spi->mode & SPI_CPHA) ?
0045 SYSCON3_ADCCKNSEN : 0);
0046 }
0047
0048 static int spi_clps711x_transfer_one(struct spi_master *master,
0049 struct spi_device *spi,
0050 struct spi_transfer *xfer)
0051 {
0052 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
0053 u8 data;
0054
0055 clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz);
0056
0057 hw->len = xfer->len;
0058 hw->bpw = xfer->bits_per_word;
0059 hw->tx_buf = (u8 *)xfer->tx_buf;
0060 hw->rx_buf = (u8 *)xfer->rx_buf;
0061
0062
0063 data = hw->tx_buf ? *hw->tx_buf++ : 0;
0064 writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio);
0065
0066 return 1;
0067 }
0068
0069 static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
0070 {
0071 struct spi_master *master = dev_id;
0072 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
0073 u8 data;
0074
0075
0076 data = readb(hw->syncio);
0077 if (hw->rx_buf)
0078 *hw->rx_buf++ = data;
0079
0080
0081 if (--hw->len > 0) {
0082 data = hw->tx_buf ? *hw->tx_buf++ : 0;
0083 writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
0084 hw->syncio);
0085 } else
0086 spi_finalize_current_transfer(master);
0087
0088 return IRQ_HANDLED;
0089 }
0090
0091 static int spi_clps711x_probe(struct platform_device *pdev)
0092 {
0093 struct device_node *np = pdev->dev.of_node;
0094 struct spi_clps711x_data *hw;
0095 struct spi_master *master;
0096 int irq, ret;
0097
0098 irq = platform_get_irq(pdev, 0);
0099 if (irq < 0)
0100 return irq;
0101
0102 master = spi_alloc_master(&pdev->dev, sizeof(*hw));
0103 if (!master)
0104 return -ENOMEM;
0105
0106 master->use_gpio_descriptors = true;
0107 master->bus_num = -1;
0108 master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
0109 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8);
0110 master->dev.of_node = pdev->dev.of_node;
0111 master->prepare_message = spi_clps711x_prepare_message;
0112 master->transfer_one = spi_clps711x_transfer_one;
0113
0114 hw = spi_master_get_devdata(master);
0115
0116 hw->spi_clk = devm_clk_get(&pdev->dev, NULL);
0117 if (IS_ERR(hw->spi_clk)) {
0118 ret = PTR_ERR(hw->spi_clk);
0119 goto err_out;
0120 }
0121
0122 hw->syscon = syscon_regmap_lookup_by_phandle(np, "syscon");
0123 if (IS_ERR(hw->syscon)) {
0124 ret = PTR_ERR(hw->syscon);
0125 goto err_out;
0126 }
0127
0128 hw->syncio = devm_platform_ioremap_resource(pdev, 0);
0129 if (IS_ERR(hw->syncio)) {
0130 ret = PTR_ERR(hw->syncio);
0131 goto err_out;
0132 }
0133
0134
0135 regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0);
0136
0137
0138 readl(hw->syncio);
0139
0140 ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0,
0141 dev_name(&pdev->dev), master);
0142 if (ret)
0143 goto err_out;
0144
0145 ret = devm_spi_register_master(&pdev->dev, master);
0146 if (!ret)
0147 return 0;
0148
0149 err_out:
0150 spi_master_put(master);
0151
0152 return ret;
0153 }
0154
0155 static const struct of_device_id clps711x_spi_dt_ids[] = {
0156 { .compatible = "cirrus,ep7209-spi", },
0157 { }
0158 };
0159 MODULE_DEVICE_TABLE(of, clps711x_spi_dt_ids);
0160
0161 static struct platform_driver clps711x_spi_driver = {
0162 .driver = {
0163 .name = DRIVER_NAME,
0164 .of_match_table = clps711x_spi_dt_ids,
0165 },
0166 .probe = spi_clps711x_probe,
0167 };
0168 module_platform_driver(clps711x_spi_driver);
0169
0170 MODULE_LICENSE("GPL");
0171 MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
0172 MODULE_DESCRIPTION("CLPS711X SPI bus driver");
0173 MODULE_ALIAS("platform:" DRIVER_NAME);