Commit | Line | Data |
---|---|---|
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> | |
161b96c3 | 14 | #include <linux/gpio.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 | 28 | struct 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 | ||
39 | static int spi_clps711x_setup(struct spi_device *spi) | |
40 | { | |
6acaadc8 AS |
41 | if (!spi->controller_state) { |
42 | int ret; | |
43 | ||
44 | ret = devm_gpio_request(&spi->master->dev, spi->cs_gpio, | |
45 | dev_name(&spi->master->dev)); | |
46 | if (ret) | |
47 | return ret; | |
48 | ||
49 | spi->controller_state = spi; | |
50 | } | |
51 | ||
161b96c3 | 52 | /* We are expect that SPI-device is not selected */ |
3e9ea4b4 | 53 | gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH)); |
161b96c3 AS |
54 | |
55 | return 0; | |
56 | } | |
57 | ||
bf5c2e27 AL |
58 | static int spi_clps711x_prepare_message(struct spi_master *master, |
59 | struct spi_message *msg) | |
161b96c3 | 60 | { |
3dc92594 | 61 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); |
8dda9d9a | 62 | struct spi_device *spi = msg->spi; |
161b96c3 | 63 | |
3dc92594 AS |
64 | /* Setup mode for transfer */ |
65 | return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN, | |
66 | (spi->mode & SPI_CPHA) ? | |
67 | SYSCON3_ADCCKNSEN : 0); | |
bf5c2e27 | 68 | } |
161b96c3 | 69 | |
bf5c2e27 AL |
70 | static int spi_clps711x_transfer_one(struct spi_master *master, |
71 | struct spi_device *spi, | |
72 | struct spi_transfer *xfer) | |
73 | { | |
74 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); | |
75 | u8 data; | |
161b96c3 | 76 | |
a5b4b234 | 77 | clk_set_rate(hw->spi_clk, xfer->speed_hz ? : spi->max_speed_hz); |
161b96c3 | 78 | |
bf5c2e27 | 79 | hw->len = xfer->len; |
bed890b4 | 80 | hw->bpw = xfer->bits_per_word; |
bf5c2e27 AL |
81 | hw->tx_buf = (u8 *)xfer->tx_buf; |
82 | hw->rx_buf = (u8 *)xfer->rx_buf; | |
161b96c3 | 83 | |
bf5c2e27 AL |
84 | /* Initiate transfer */ |
85 | data = hw->tx_buf ? *hw->tx_buf++ : 0; | |
3dc92594 AS |
86 | writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio); |
87 | ||
bf5c2e27 | 88 | return 1; |
161b96c3 AS |
89 | } |
90 | ||
91 | static irqreturn_t spi_clps711x_isr(int irq, void *dev_id) | |
92 | { | |
bf5c2e27 AL |
93 | struct spi_master *master = dev_id; |
94 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); | |
c7a26f12 | 95 | u8 data; |
161b96c3 AS |
96 | |
97 | /* Handle RX */ | |
3dc92594 | 98 | data = readb(hw->syncio); |
161b96c3 | 99 | if (hw->rx_buf) |
c7a26f12 | 100 | *hw->rx_buf++ = data; |
161b96c3 AS |
101 | |
102 | /* Handle TX */ | |
c7a26f12 AS |
103 | if (--hw->len > 0) { |
104 | data = hw->tx_buf ? *hw->tx_buf++ : 0; | |
3dc92594 AS |
105 | writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, |
106 | hw->syncio); | |
161b96c3 | 107 | } else |
bf5c2e27 | 108 | spi_finalize_current_transfer(master); |
161b96c3 AS |
109 | |
110 | return IRQ_HANDLED; | |
111 | } | |
112 | ||
fd4a319b | 113 | static int spi_clps711x_probe(struct platform_device *pdev) |
161b96c3 | 114 | { |
161b96c3 | 115 | struct spi_clps711x_data *hw; |
3dc92594 AS |
116 | struct spi_master *master; |
117 | struct resource *res; | |
6acaadc8 | 118 | int irq, ret; |
161b96c3 | 119 | |
3dc92594 AS |
120 | irq = platform_get_irq(pdev, 0); |
121 | if (irq < 0) | |
122 | return irq; | |
123 | ||
3e9ea4b4 AS |
124 | master = spi_alloc_master(&pdev->dev, sizeof(*hw)); |
125 | if (!master) | |
161b96c3 | 126 | return -ENOMEM; |
3e9ea4b4 | 127 | |
6acaadc8 | 128 | master->bus_num = -1; |
161b96c3 | 129 | master->mode_bits = SPI_CPHA | SPI_CS_HIGH; |
8dda9d9a | 130 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8); |
6acaadc8 | 131 | master->dev.of_node = pdev->dev.of_node; |
161b96c3 | 132 | master->setup = spi_clps711x_setup; |
bf5c2e27 AL |
133 | master->prepare_message = spi_clps711x_prepare_message; |
134 | master->transfer_one = spi_clps711x_transfer_one; | |
161b96c3 AS |
135 | |
136 | hw = spi_master_get_devdata(master); | |
137 | ||
a5b4b234 | 138 | hw->spi_clk = devm_clk_get(&pdev->dev, NULL); |
161b96c3 | 139 | if (IS_ERR(hw->spi_clk)) { |
161b96c3 AS |
140 | ret = PTR_ERR(hw->spi_clk); |
141 | goto err_out; | |
142 | } | |
161b96c3 | 143 | |
6acaadc8 AS |
144 | hw->syscon = |
145 | syscon_regmap_lookup_by_compatible("cirrus,ep7209-syscon3"); | |
3dc92594 AS |
146 | if (IS_ERR(hw->syscon)) { |
147 | ret = PTR_ERR(hw->syscon); | |
148 | goto err_out; | |
149 | } | |
150 | ||
3dc92594 AS |
151 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
152 | hw->syncio = devm_ioremap_resource(&pdev->dev, res); | |
153 | if (IS_ERR(hw->syncio)) { | |
154 | ret = PTR_ERR(hw->syncio); | |
155 | goto err_out; | |
156 | } | |
157 | ||
161b96c3 | 158 | /* Disable extended mode due hardware problems */ |
3dc92594 | 159 | regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0); |
161b96c3 AS |
160 | |
161 | /* Clear possible pending interrupt */ | |
3dc92594 | 162 | readl(hw->syncio); |
161b96c3 | 163 | |
3dc92594 | 164 | ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0, |
bf5c2e27 | 165 | dev_name(&pdev->dev), master); |
3dc92594 | 166 | if (ret) |
c7083790 | 167 | goto err_out; |
161b96c3 | 168 | |
c493fc4b | 169 | ret = devm_spi_register_master(&pdev->dev, master); |
6acaadc8 | 170 | if (!ret) |
161b96c3 | 171 | return 0; |
161b96c3 | 172 | |
161b96c3 | 173 | err_out: |
161b96c3 | 174 | spi_master_put(master); |
161b96c3 AS |
175 | |
176 | return ret; | |
177 | } | |
178 | ||
6acaadc8 AS |
179 | static const struct of_device_id clps711x_spi_dt_ids[] = { |
180 | { .compatible = "cirrus,ep7209-spi", }, | |
181 | { } | |
182 | }; | |
183 | MODULE_DEVICE_TABLE(of, clps711x_spi_dt_ids); | |
184 | ||
161b96c3 AS |
185 | static struct platform_driver clps711x_spi_driver = { |
186 | .driver = { | |
187 | .name = DRIVER_NAME, | |
6acaadc8 | 188 | .of_match_table = clps711x_spi_dt_ids, |
161b96c3 AS |
189 | }, |
190 | .probe = spi_clps711x_probe, | |
161b96c3 AS |
191 | }; |
192 | module_platform_driver(clps711x_spi_driver); | |
193 | ||
194 | MODULE_LICENSE("GPL"); | |
195 | MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>"); | |
196 | MODULE_DESCRIPTION("CLPS711X SPI bus driver"); | |
350a9b33 | 197 | MODULE_ALIAS("platform:" DRIVER_NAME); |