forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-11 072de836f53be56a70cecf70b43ae43b7ce17376
kernel/drivers/spi/spi-rockchip.c
....@@ -1,22 +1,13 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd
34 * Author: Addy Ke <addy.ke@rock-chips.com>
4
- *
5
- * This program is free software; you can redistribute it and/or modify it
6
- * under the terms and conditions of the GNU General Public License,
7
- * version 2, as published by the Free Software Foundation.
8
- *
9
- * This program is distributed in the hope it will be useful, but WITHOUT
10
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
- * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12
- * more details.
13
- *
145 */
156
7
+#include <linux/acpi.h>
168 #include <linux/clk.h>
179 #include <linux/delay.h>
1810 #include <linux/dmaengine.h>
19
-#include <linux/gpio.h>
2011 #include <linux/interrupt.h>
2112 #include <linux/miscdevice.h>
2213 #include <linux/module.h>
....@@ -197,6 +188,7 @@
197188
198189 struct clk *spiclk;
199190 struct clk *apb_pclk;
191
+ struct clk *sclk_in;
200192
201193 void __iomem *regs;
202194 dma_addr_t dma_addr_rx;
....@@ -227,8 +219,10 @@
227219
228220 struct pinctrl_state *high_speed_state;
229221 bool slave_aborted;
230
- bool gpio_requested;
231222 bool cs_inactive; /* spi slave tansmition stop when cs inactive */
223
+ bool cs_high_supported; /* native CS supports active-high polarity */
224
+ struct gpio_desc *ready; /* spi slave transmission ready */
225
+
232226 struct spi_transfer *xfer; /* Store xfer temporarily */
233227 phys_addr_t base_addr_phy;
234228 struct miscdevice miscdev;
....@@ -296,12 +290,12 @@
296290 /* Keep things powered as long as CS is asserted */
297291 pm_runtime_get_sync(rs->dev);
298292
299
- if (gpio_is_valid(spi->cs_gpio))
293
+ if (spi->cs_gpiod)
300294 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, 1);
301295 else
302296 ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select));
303297 } else {
304
- if (gpio_is_valid(spi->cs_gpio))
298
+ if (spi->cs_gpiod)
305299 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, 1);
306300 else
307301 ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select));
....@@ -362,7 +356,7 @@
362356 static void rockchip_spi_pio_reader(struct rockchip_spi *rs)
363357 {
364358 u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR);
365
- u32 rx_left = rs->rx_left > words ? rs->rx_left - words : 0;
359
+ u32 rx_left = (rs->rx_left > words) ? rs->rx_left - words : 0;
366360
367361 /* the hardware doesn't allow us to change fifo threshold
368362 * level while spi is enabled, so instead make sure to leave
....@@ -528,7 +522,7 @@
528522 .direction = DMA_MEM_TO_DEV,
529523 .dst_addr = rs->dma_addr_tx,
530524 .dst_addr_width = rs->n_bytes,
531
- .dst_maxburst = 8,
525
+ .dst_maxburst = rs->fifo_len / 4,
532526 };
533527
534528 dmaengine_slave_config(ctlr->dma_tx, &txconf);
....@@ -678,7 +672,9 @@
678672 * ctlr->bits_per_word_mask, so this shouldn't
679673 * happen
680674 */
681
- unreachable();
675
+ dev_err(rs->dev, "unknown bits per word: %d\n",
676
+ xfer->bits_per_word);
677
+ return -EINVAL;
682678 }
683679
684680 if (xfer_mode == ROCKCHIP_SPI_DMA) {
....@@ -864,8 +860,17 @@
864860 ret = rockchip_spi_prepare_irq(rs, ctlr, xfer);
865861 }
866862
863
+ if (rs->ready) {
864
+ gpiod_set_value(rs->ready, 0);
865
+ udelay(1);
866
+ gpiod_set_value(rs->ready, 1);
867
+ }
868
+
867869 if (ret > 0)
868870 ret = rockchip_spi_transfer_wait(ctlr, xfer);
871
+
872
+ if (rs->ready)
873
+ gpiod_set_value(rs->ready, 0);
869874
870875 return ret;
871876 }
....@@ -886,10 +891,13 @@
886891
887892 static int rockchip_spi_setup(struct spi_device *spi)
888893 {
889
-
890
- int ret = -EINVAL;
891894 struct rockchip_spi *rs = spi_controller_get_devdata(spi->controller);
892895 u32 cr0;
896
+
897
+ if (!spi->cs_gpiod && (spi->mode & SPI_CS_HIGH) && !rs->cs_high_supported) {
898
+ dev_warn(&spi->dev, "setup: non GPIO CS can't be active-high\n");
899
+ return -EINVAL;
900
+ }
893901
894902 pm_runtime_get_sync(rs->dev);
895903
....@@ -903,39 +911,7 @@
903911
904912 pm_runtime_put(rs->dev);
905913
906
- if (spi->cs_gpio == -ENOENT)
907
- return 0;
908
-
909
- if (!rs->gpio_requested && gpio_is_valid(spi->cs_gpio)) {
910
- ret = gpio_request_one(spi->cs_gpio,
911
- (spi->mode & SPI_CS_HIGH) ?
912
- GPIOF_OUT_INIT_LOW : GPIOF_OUT_INIT_HIGH,
913
- dev_name(&spi->dev));
914
- if (ret)
915
- dev_err(&spi->dev, "can't request chipselect gpio %d\n",
916
- spi->cs_gpio);
917
- else
918
- rs->gpio_requested = true;
919
- } else {
920
- if (gpio_is_valid(spi->cs_gpio)) {
921
- int mode = ((spi->mode & SPI_CS_HIGH) ? 0 : 1);
922
-
923
- ret = gpio_direction_output(spi->cs_gpio, mode);
924
- if (ret)
925
- dev_err(&spi->dev, "chipselect gpio %d setup failed (%d)\n",
926
- spi->cs_gpio, ret);
927
- }
928
- }
929
-
930
- return ret;
931
-}
932
-
933
-static void rockchip_spi_cleanup(struct spi_device *spi)
934
-{
935
- struct rockchip_spi *rs = spi_controller_get_devdata(spi->controller);
936
-
937
- if (rs->gpio_requested)
938
- gpio_free(spi->cs_gpio);
914
+ return 0;
939915 }
940916
941917 static int rockchip_spi_misc_open(struct inode *inode, struct file *filp)
....@@ -999,10 +975,11 @@
999975 struct spi_controller *ctlr;
1000976 struct resource *mem;
1001977 struct device_node *np = pdev->dev.of_node;
1002
- u32 rsd_nsecs, csm;
978
+ u32 rsd_nsecs, num_cs, csm;
1003979 bool slave_mode;
1004980 struct pinctrl *pinctrl = NULL;
1005981 const struct rockchip_spi_quirks *quirks_cfg;
982
+ u32 val;
1006983
1007984 slave_mode = of_property_read_bool(np, "spi-slave");
1008985
....@@ -1016,6 +993,7 @@
1016993 if (!ctlr)
1017994 return -ENOMEM;
1018995
996
+ ctlr->rt = device_property_read_bool(&pdev->dev, "rockchip,rt");
1019997 platform_set_drvdata(pdev, ctlr);
1020998
1021999 rs = spi_controller_get_devdata(ctlr);
....@@ -1030,17 +1008,26 @@
10301008 }
10311009 rs->base_addr_phy = mem->start;
10321010
1033
- rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
1011
+ if (!has_acpi_companion(&pdev->dev))
1012
+ rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk");
10341013 if (IS_ERR(rs->apb_pclk)) {
10351014 dev_err(&pdev->dev, "Failed to get apb_pclk\n");
10361015 ret = PTR_ERR(rs->apb_pclk);
10371016 goto err_put_ctlr;
10381017 }
10391018
1040
- rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
1019
+ if (!has_acpi_companion(&pdev->dev))
1020
+ rs->spiclk = devm_clk_get(&pdev->dev, "spiclk");
10411021 if (IS_ERR(rs->spiclk)) {
10421022 dev_err(&pdev->dev, "Failed to get spi_pclk\n");
10431023 ret = PTR_ERR(rs->spiclk);
1024
+ goto err_put_ctlr;
1025
+ }
1026
+
1027
+ rs->sclk_in = devm_clk_get_optional(&pdev->dev, "sclk_in");
1028
+ if (IS_ERR(rs->sclk_in)) {
1029
+ dev_err(&pdev->dev, "Failed to get sclk_in\n");
1030
+ ret = PTR_ERR(rs->sclk_in);
10441031 goto err_put_ctlr;
10451032 }
10461033
....@@ -1056,23 +1043,35 @@
10561043 goto err_disable_apbclk;
10571044 }
10581045
1046
+ ret = clk_prepare_enable(rs->sclk_in);
1047
+ if (ret < 0) {
1048
+ dev_err(&pdev->dev, "Failed to enable sclk_in\n");
1049
+ goto err_disable_spiclk;
1050
+ }
1051
+
10591052 spi_enable_chip(rs, false);
10601053
10611054 ret = platform_get_irq(pdev, 0);
10621055 if (ret < 0)
1063
- goto err_disable_spiclk;
1056
+ goto err_disable_sclk_in;
10641057
10651058 ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL,
10661059 IRQF_ONESHOT, dev_name(&pdev->dev), ctlr);
10671060 if (ret)
1068
- goto err_disable_spiclk;
1061
+ goto err_disable_sclk_in;
10691062
10701063 rs->dev = &pdev->dev;
1071
- rs->freq = clk_get_rate(rs->spiclk);
1072
- rs->gpio_requested = false;
10731064
1074
- if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns",
1075
- &rsd_nsecs)) {
1065
+ rs->freq = clk_get_rate(rs->spiclk);
1066
+ if (!rs->freq) {
1067
+ ret = device_property_read_u32(&pdev->dev, "clock-frequency", &rs->freq);
1068
+ if (ret) {
1069
+ dev_warn(rs->dev, "Failed to get clock or clock-frequency property\n");
1070
+ goto err_disable_sclk_in;
1071
+ }
1072
+ }
1073
+
1074
+ if (!device_property_read_u32(&pdev->dev, "rx-sample-delay-ns", &rsd_nsecs)) {
10761075 /* rx sample delay is expressed in parent clock cycles (max 3) */
10771076 u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8),
10781077 1000000000 >> 8);
....@@ -1102,33 +1101,47 @@
11021101 if (!rs->fifo_len) {
11031102 dev_err(&pdev->dev, "Failed to get fifo length\n");
11041103 ret = -EINVAL;
1105
- goto err_disable_spiclk;
1104
+ goto err_disable_sclk_in;
11061105 }
11071106 quirks_cfg = device_get_match_data(&pdev->dev);
11081107 if (quirks_cfg)
11091108 rs->max_baud_div_in_cpha = quirks_cfg->max_baud_div_in_cpha;
1109
+
1110
+ if (!device_property_read_u32(&pdev->dev, "rockchip,autosuspend-delay-ms", &val)) {
1111
+ if (val > 0) {
1112
+ pm_runtime_set_autosuspend_delay(&pdev->dev, val);
1113
+ pm_runtime_use_autosuspend(&pdev->dev);
1114
+ }
1115
+ }
11101116
11111117 pm_runtime_set_active(&pdev->dev);
11121118 pm_runtime_enable(&pdev->dev);
11131119
11141120 ctlr->auto_runtime_pm = true;
11151121 ctlr->bus_num = pdev->id;
1116
- ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST | SPI_CS_HIGH;
1122
+ ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST;
11171123 if (slave_mode) {
11181124 ctlr->mode_bits |= SPI_NO_CS;
11191125 ctlr->slave_abort = rockchip_spi_slave_abort;
11201126 } else {
11211127 ctlr->flags = SPI_MASTER_GPIO_SS;
1128
+ ctlr->max_native_cs = ROCKCHIP_SPI_MAX_CS_NUM;
1129
+ /*
1130
+ * rk spi0 has two native cs, spi1..5 one cs only
1131
+ * if num-cs is missing in the dts, default to 1
1132
+ */
1133
+ if (device_property_read_u32(&pdev->dev, "num-cs", &num_cs))
1134
+ num_cs = 1;
1135
+ ctlr->num_chipselect = num_cs;
1136
+ ctlr->use_gpio_descriptors = true;
11221137 }
1123
- ctlr->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM;
11241138 ctlr->dev.of_node = pdev->dev.of_node;
11251139 ctlr->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4);
11261140 ctlr->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX;
11271141 ctlr->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT);
11281142
1129
- ctlr->set_cs = rockchip_spi_set_cs;
11301143 ctlr->setup = rockchip_spi_setup;
1131
- ctlr->cleanup = rockchip_spi_cleanup;
1144
+ ctlr->set_cs = rockchip_spi_set_cs;
11321145 ctlr->transfer_one = rockchip_spi_transfer_one;
11331146 ctlr->max_transfer_size = rockchip_spi_max_transfer_size;
11341147 ctlr->handle_err = rockchip_spi_handle_err;
....@@ -1169,8 +1182,9 @@
11691182 }
11701183
11711184 switch (rs->version) {
1172
- case ROCKCHIP_SPI_VER2_TYPE1:
11731185 case ROCKCHIP_SPI_VER2_TYPE2:
1186
+ rs->cs_high_supported = true;
1187
+ ctlr->mode_bits |= SPI_CS_HIGH;
11741188 if (slave_mode)
11751189 rs->cs_inactive = true;
11761190 else
....@@ -1178,7 +1192,11 @@
11781192 break;
11791193 default:
11801194 rs->cs_inactive = false;
1195
+ break;
11811196 }
1197
+ if (device_property_read_bool(&pdev->dev, "rockchip,cs-inactive-disable"))
1198
+ rs->cs_inactive = false;
1199
+
11821200 pinctrl = devm_pinctrl_get(&pdev->dev);
11831201 if (!IS_ERR(pinctrl)) {
11841202 rs->high_speed_state = pinctrl_lookup_state(pinctrl, "high_speed");
....@@ -1186,6 +1204,13 @@
11861204 dev_warn(&pdev->dev, "no high_speed pinctrl state\n");
11871205 rs->high_speed_state = NULL;
11881206 }
1207
+ }
1208
+
1209
+ rs->ready = devm_gpiod_get_optional(&pdev->dev, "ready", GPIOD_OUT_HIGH);
1210
+ if (IS_ERR(rs->ready)) {
1211
+ ret = dev_err_probe(&pdev->dev, PTR_ERR(rs->ready),
1212
+ "invalid ready-gpios property in node\n");
1213
+ goto err_free_dma_rx;
11891214 }
11901215
11911216 ret = devm_spi_register_controller(&pdev->dev, ctlr);
....@@ -1210,7 +1235,8 @@
12101235 dev_info(&pdev->dev, "register misc device %s\n", misc_name);
12111236 }
12121237
1213
- dev_info(rs->dev, "probed, poll=%d, rsd=%d\n", rs->poll, rs->rsd);
1238
+ dev_info(rs->dev, "probed, poll=%d, rsd=%d, cs-inactive=%d, ready=%d\n",
1239
+ rs->poll, rs->rsd, rs->cs_inactive, rs->ready ? 1 : 0);
12141240
12151241 return 0;
12161242
....@@ -1222,6 +1248,8 @@
12221248 dma_release_channel(ctlr->dma_tx);
12231249 err_disable_pm_runtime:
12241250 pm_runtime_disable(&pdev->dev);
1251
+err_disable_sclk_in:
1252
+ clk_disable_unprepare(rs->sclk_in);
12251253 err_disable_spiclk:
12261254 clk_disable_unprepare(rs->spiclk);
12271255 err_disable_apbclk:
....@@ -1242,6 +1270,7 @@
12421270
12431271 pm_runtime_get_sync(&pdev->dev);
12441272
1273
+ clk_disable_unprepare(rs->sclk_in);
12451274 clk_disable_unprepare(rs->spiclk);
12461275 clk_disable_unprepare(rs->apb_pclk);
12471276
....@@ -1344,15 +1373,18 @@
13441373 .compatible = "rockchip,px30-spi",
13451374 .data = &rockchip_spi_quirks_cfg,
13461375 },
1347
- { .compatible = "rockchip,rv1108-spi", },
1348
- { .compatible = "rockchip,rv1126-spi", },
13491376 { .compatible = "rockchip,rk3036-spi", },
13501377 { .compatible = "rockchip,rk3066-spi", },
13511378 { .compatible = "rockchip,rk3188-spi", },
13521379 { .compatible = "rockchip,rk3228-spi", },
13531380 { .compatible = "rockchip,rk3288-spi", },
1381
+ { .compatible = "rockchip,rk3308-spi", },
1382
+ { .compatible = "rockchip,rk3328-spi", },
13541383 { .compatible = "rockchip,rk3368-spi", },
13551384 { .compatible = "rockchip,rk3399-spi", },
1385
+ { .compatible = "rockchip,rv1106-spi", },
1386
+ { .compatible = "rockchip,rv1108-spi", },
1387
+ { .compatible = "rockchip,rv1126-spi", },
13561388 { },
13571389 };
13581390 MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match);