blob: 8c03c409fc07753c3b65887862b026a23182d6a6 [file] [log] [blame]
Alexander Shiyan161b96c2012-11-07 21:30:29 +04001/*
2 * CLPS711X SPI bus driver
3 *
Alexander Shiyan6acaadc2016-07-06 16:53:12 +02004 * Copyright (C) 2012-2016 Alexander Shiyan <shc_work@mail.ru>
Alexander Shiyan161b96c2012-11-07 21:30:29 +04005 *
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>
Linus Walleij054320b2019-01-07 16:51:54 +010014#include <linux/gpio/consumer.h>
Alexander Shiyan161b96c2012-11-07 21:30:29 +040015#include <linux/module.h>
16#include <linux/interrupt.h>
17#include <linux/platform_device.h>
Alexander Shiyan3dc92592014-03-22 10:57:35 +040018#include <linux/regmap.h>
19#include <linux/mfd/syscon.h>
20#include <linux/mfd/syscon/clps711x.h>
Alexander Shiyan161b96c2012-11-07 21:30:29 +040021#include <linux/spi/spi.h>
Alexander Shiyan161b96c2012-11-07 21:30:29 +040022
Alexander Shiyan6acaadc2016-07-06 16:53:12 +020023#define DRIVER_NAME "clps711x-spi"
Alexander Shiyan161b96c2012-11-07 21:30:29 +040024
Alexander Shiyan3dc92592014-03-22 10:57:35 +040025#define SYNCIO_FRMLEN(x) ((x) << 8)
26#define SYNCIO_TXFRMEN (1 << 14)
27
Alexander Shiyan161b96c2012-11-07 21:30:29 +040028struct spi_clps711x_data {
Alexander Shiyan3dc92592014-03-22 10:57:35 +040029 void __iomem *syncio;
30 struct regmap *syscon;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040031 struct clk *spi_clk;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040032
33 u8 *tx_buf;
34 u8 *rx_buf;
Alexander Shiyan8dda9d92014-02-02 10:59:49 +040035 unsigned int bpw;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040036 int len;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040037};
38
Axel Linbf5c2e22014-02-18 17:15:54 +080039static int spi_clps711x_prepare_message(struct spi_master *master,
40 struct spi_message *msg)
Alexander Shiyan161b96c2012-11-07 21:30:29 +040041{
Alexander Shiyan3dc92592014-03-22 10:57:35 +040042 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
Alexander Shiyan8dda9d92014-02-02 10:59:49 +040043 struct spi_device *spi = msg->spi;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040044
Alexander Shiyan3dc92592014-03-22 10:57:35 +040045 /* Setup mode for transfer */
46 return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN,
47 (spi->mode & SPI_CPHA) ?
48 SYSCON3_ADCCKNSEN : 0);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040049}
50
Axel Linbf5c2e22014-02-18 17:15:54 +080051static 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;
57
Alexander Shiyana5b4b232014-09-20 11:05:09 +040058 clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz);
Axel Linbf5c2e22014-02-18 17:15:54 +080059
60 hw->len = xfer->len;
Axel Linbed890b2014-03-02 23:24:18 +080061 hw->bpw = xfer->bits_per_word;
Axel Linbf5c2e22014-02-18 17:15:54 +080062 hw->tx_buf = (u8 *)xfer->tx_buf;
63 hw->rx_buf = (u8 *)xfer->rx_buf;
64
65 /* Initiate transfer */
66 data = hw->tx_buf ? *hw->tx_buf++ : 0;
Alexander Shiyan3dc92592014-03-22 10:57:35 +040067 writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio);
68
Axel Linbf5c2e22014-02-18 17:15:54 +080069 return 1;
70}
71
Alexander Shiyan161b96c2012-11-07 21:30:29 +040072static irqreturn_t spi_clps711x_isr(int irq, void *dev_id)
73{
Axel Linbf5c2e22014-02-18 17:15:54 +080074 struct spi_master *master = dev_id;
75 struct spi_clps711x_data *hw = spi_master_get_devdata(master);
Alexander Shiyanc7a26f12014-02-02 10:59:48 +040076 u8 data;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040077
78 /* Handle RX */
Alexander Shiyan3dc92592014-03-22 10:57:35 +040079 data = readb(hw->syncio);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040080 if (hw->rx_buf)
Alexander Shiyanc7a26f12014-02-02 10:59:48 +040081 *hw->rx_buf++ = data;
Alexander Shiyan161b96c2012-11-07 21:30:29 +040082
83 /* Handle TX */
Alexander Shiyanc7a26f12014-02-02 10:59:48 +040084 if (--hw->len > 0) {
85 data = hw->tx_buf ? *hw->tx_buf++ : 0;
Alexander Shiyan3dc92592014-03-22 10:57:35 +040086 writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN,
87 hw->syncio);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040088 } else
Axel Linbf5c2e22014-02-18 17:15:54 +080089 spi_finalize_current_transfer(master);
Alexander Shiyan161b96c2012-11-07 21:30:29 +040090
91 return IRQ_HANDLED;
92}
93
Grant Likelyfd4a3192012-12-07 16:57:14 +000094static int spi_clps711x_probe(struct platform_device *pdev)
Alexander Shiyan161b96c2012-11-07 21:30:29 +040095{
Alexander Shiyan161b96c2012-11-07 21:30:29 +040096 struct spi_clps711x_data *hw;
Alexander Shiyan3dc92592014-03-22 10:57:35 +040097 struct spi_master *master;
98 struct resource *res;
Alexander Shiyan6acaadc2016-07-06 16:53:12 +020099 int irq, ret;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400100
Alexander Shiyan3dc92592014-03-22 10:57:35 +0400101 irq = platform_get_irq(pdev, 0);
102 if (irq < 0)
103 return irq;
104
Alexander Shiyan3e9ea4b2014-02-02 10:59:50 +0400105 master = spi_alloc_master(&pdev->dev, sizeof(*hw));
106 if (!master)
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400107 return -ENOMEM;
Alexander Shiyan3e9ea4b2014-02-02 10:59:50 +0400108
Linus Walleij054320b2019-01-07 16:51:54 +0100109 master->use_gpio_descriptors = true;
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200110 master->bus_num = -1;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400111 master->mode_bits = SPI_CPHA | SPI_CS_HIGH;
Alexander Shiyan8dda9d92014-02-02 10:59:49 +0400112 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8);
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200113 master->dev.of_node = pdev->dev.of_node;
Axel Linbf5c2e22014-02-18 17:15:54 +0800114 master->prepare_message = spi_clps711x_prepare_message;
115 master->transfer_one = spi_clps711x_transfer_one;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400116
117 hw = spi_master_get_devdata(master);
118
Alexander Shiyana5b4b232014-09-20 11:05:09 +0400119 hw->spi_clk = devm_clk_get(&pdev->dev, NULL);
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400120 if (IS_ERR(hw->spi_clk)) {
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400121 ret = PTR_ERR(hw->spi_clk);
122 goto err_out;
123 }
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400124
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200125 hw->syscon =
126 syscon_regmap_lookup_by_compatible("cirrus,ep7209-syscon3");
Alexander Shiyan3dc92592014-03-22 10:57:35 +0400127 if (IS_ERR(hw->syscon)) {
128 ret = PTR_ERR(hw->syscon);
Sachin Kamatc7083792013-09-27 15:32:53 +0530129 goto err_out;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400130 }
131
Alexander Shiyan3dc92592014-03-22 10:57:35 +0400132 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
139 /* Disable extended mode due hardware problems */
140 regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0);
141
142 /* Clear possible pending interrupt */
143 readl(hw->syncio);
144
145 ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0,
146 dev_name(&pdev->dev), master);
147 if (ret)
148 goto err_out;
149
Jingoo Hanc493fc42013-09-24 13:27:48 +0900150 ret = devm_spi_register_master(&pdev->dev, master);
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200151 if (!ret)
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400152 return 0;
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400153
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400154err_out:
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400155 spi_master_put(master);
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400156
157 return ret;
158}
159
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200160static 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
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400166static struct platform_driver clps711x_spi_driver = {
167 .driver = {
168 .name = DRIVER_NAME,
Alexander Shiyan6acaadc2016-07-06 16:53:12 +0200169 .of_match_table = clps711x_spi_dt_ids,
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400170 },
171 .probe = spi_clps711x_probe,
Alexander Shiyan161b96c2012-11-07 21:30:29 +0400172};
173module_platform_driver(clps711x_spi_driver);
174
175MODULE_LICENSE("GPL");
176MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>");
177MODULE_DESCRIPTION("CLPS711X SPI bus driver");
Axel Lin350a9b32014-01-14 17:01:54 +0800178MODULE_ALIAS("platform:" DRIVER_NAME);