| .. | .. |
|---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
|---|
| 1 | 2 | /* |
|---|
| 2 | 3 | * Copyright (c) 2014, Fuzhou Rockchip Electronics Co., Ltd |
|---|
| 3 | 4 | * 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 | | - * |
|---|
| 14 | 5 | */ |
|---|
| 15 | 6 | |
|---|
| 7 | +#include <linux/acpi.h> |
|---|
| 16 | 8 | #include <linux/clk.h> |
|---|
| 17 | 9 | #include <linux/delay.h> |
|---|
| 18 | 10 | #include <linux/dmaengine.h> |
|---|
| 19 | | -#include <linux/gpio.h> |
|---|
| 20 | 11 | #include <linux/interrupt.h> |
|---|
| 21 | 12 | #include <linux/miscdevice.h> |
|---|
| 22 | 13 | #include <linux/module.h> |
|---|
| .. | .. |
|---|
| 197 | 188 | |
|---|
| 198 | 189 | struct clk *spiclk; |
|---|
| 199 | 190 | struct clk *apb_pclk; |
|---|
| 191 | + struct clk *sclk_in; |
|---|
| 200 | 192 | |
|---|
| 201 | 193 | void __iomem *regs; |
|---|
| 202 | 194 | dma_addr_t dma_addr_rx; |
|---|
| .. | .. |
|---|
| 227 | 219 | |
|---|
| 228 | 220 | struct pinctrl_state *high_speed_state; |
|---|
| 229 | 221 | bool slave_aborted; |
|---|
| 230 | | - bool gpio_requested; |
|---|
| 231 | 222 | bool cs_inactive; /* spi slave tansmition stop when cs inactive */ |
|---|
| 223 | + bool cs_high_supported; /* native CS supports active-high polarity */ |
|---|
| 224 | + |
|---|
| 232 | 225 | struct spi_transfer *xfer; /* Store xfer temporarily */ |
|---|
| 233 | 226 | phys_addr_t base_addr_phy; |
|---|
| 234 | 227 | struct miscdevice miscdev; |
|---|
| .. | .. |
|---|
| 296 | 289 | /* Keep things powered as long as CS is asserted */ |
|---|
| 297 | 290 | pm_runtime_get_sync(rs->dev); |
|---|
| 298 | 291 | |
|---|
| 299 | | - if (gpio_is_valid(spi->cs_gpio)) |
|---|
| 292 | + if (spi->cs_gpiod) |
|---|
| 300 | 293 | ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, 1); |
|---|
| 301 | 294 | else |
|---|
| 302 | 295 | ROCKCHIP_SPI_SET_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select)); |
|---|
| 303 | 296 | } else { |
|---|
| 304 | | - if (gpio_is_valid(spi->cs_gpio)) |
|---|
| 297 | + if (spi->cs_gpiod) |
|---|
| 305 | 298 | ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, 1); |
|---|
| 306 | 299 | else |
|---|
| 307 | 300 | ROCKCHIP_SPI_CLR_BITS(rs->regs + ROCKCHIP_SPI_SER, BIT(spi->chip_select)); |
|---|
| .. | .. |
|---|
| 362 | 355 | static void rockchip_spi_pio_reader(struct rockchip_spi *rs) |
|---|
| 363 | 356 | { |
|---|
| 364 | 357 | u32 words = readl_relaxed(rs->regs + ROCKCHIP_SPI_RXFLR); |
|---|
| 365 | | - u32 rx_left = rs->rx_left > words ? rs->rx_left - words : 0; |
|---|
| 358 | + u32 rx_left = (rs->rx_left > words) ? rs->rx_left - words : 0; |
|---|
| 366 | 359 | |
|---|
| 367 | 360 | /* the hardware doesn't allow us to change fifo threshold |
|---|
| 368 | 361 | * level while spi is enabled, so instead make sure to leave |
|---|
| .. | .. |
|---|
| 528 | 521 | .direction = DMA_MEM_TO_DEV, |
|---|
| 529 | 522 | .dst_addr = rs->dma_addr_tx, |
|---|
| 530 | 523 | .dst_addr_width = rs->n_bytes, |
|---|
| 531 | | - .dst_maxburst = 8, |
|---|
| 524 | + .dst_maxburst = rs->fifo_len / 4, |
|---|
| 532 | 525 | }; |
|---|
| 533 | 526 | |
|---|
| 534 | 527 | dmaengine_slave_config(ctlr->dma_tx, &txconf); |
|---|
| .. | .. |
|---|
| 678 | 671 | * ctlr->bits_per_word_mask, so this shouldn't |
|---|
| 679 | 672 | * happen |
|---|
| 680 | 673 | */ |
|---|
| 681 | | - unreachable(); |
|---|
| 674 | + dev_err(rs->dev, "unknown bits per word: %d\n", |
|---|
| 675 | + xfer->bits_per_word); |
|---|
| 676 | + return -EINVAL; |
|---|
| 682 | 677 | } |
|---|
| 683 | 678 | |
|---|
| 684 | 679 | if (xfer_mode == ROCKCHIP_SPI_DMA) { |
|---|
| .. | .. |
|---|
| 886 | 881 | |
|---|
| 887 | 882 | static int rockchip_spi_setup(struct spi_device *spi) |
|---|
| 888 | 883 | { |
|---|
| 889 | | - |
|---|
| 890 | | - int ret = -EINVAL; |
|---|
| 891 | 884 | struct rockchip_spi *rs = spi_controller_get_devdata(spi->controller); |
|---|
| 892 | 885 | u32 cr0; |
|---|
| 886 | + |
|---|
| 887 | + if (!spi->cs_gpiod && (spi->mode & SPI_CS_HIGH) && !rs->cs_high_supported) { |
|---|
| 888 | + dev_warn(&spi->dev, "setup: non GPIO CS can't be active-high\n"); |
|---|
| 889 | + return -EINVAL; |
|---|
| 890 | + } |
|---|
| 893 | 891 | |
|---|
| 894 | 892 | pm_runtime_get_sync(rs->dev); |
|---|
| 895 | 893 | |
|---|
| .. | .. |
|---|
| 903 | 901 | |
|---|
| 904 | 902 | pm_runtime_put(rs->dev); |
|---|
| 905 | 903 | |
|---|
| 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); |
|---|
| 904 | + return 0; |
|---|
| 939 | 905 | } |
|---|
| 940 | 906 | |
|---|
| 941 | 907 | static int rockchip_spi_misc_open(struct inode *inode, struct file *filp) |
|---|
| .. | .. |
|---|
| 999 | 965 | struct spi_controller *ctlr; |
|---|
| 1000 | 966 | struct resource *mem; |
|---|
| 1001 | 967 | struct device_node *np = pdev->dev.of_node; |
|---|
| 1002 | | - u32 rsd_nsecs, csm; |
|---|
| 968 | + u32 rsd_nsecs, num_cs, csm; |
|---|
| 1003 | 969 | bool slave_mode; |
|---|
| 1004 | 970 | struct pinctrl *pinctrl = NULL; |
|---|
| 1005 | 971 | const struct rockchip_spi_quirks *quirks_cfg; |
|---|
| .. | .. |
|---|
| 1030 | 996 | } |
|---|
| 1031 | 997 | rs->base_addr_phy = mem->start; |
|---|
| 1032 | 998 | |
|---|
| 1033 | | - rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk"); |
|---|
| 999 | + if (!has_acpi_companion(&pdev->dev)) |
|---|
| 1000 | + rs->apb_pclk = devm_clk_get(&pdev->dev, "apb_pclk"); |
|---|
| 1034 | 1001 | if (IS_ERR(rs->apb_pclk)) { |
|---|
| 1035 | 1002 | dev_err(&pdev->dev, "Failed to get apb_pclk\n"); |
|---|
| 1036 | 1003 | ret = PTR_ERR(rs->apb_pclk); |
|---|
| 1037 | 1004 | goto err_put_ctlr; |
|---|
| 1038 | 1005 | } |
|---|
| 1039 | 1006 | |
|---|
| 1040 | | - rs->spiclk = devm_clk_get(&pdev->dev, "spiclk"); |
|---|
| 1007 | + if (!has_acpi_companion(&pdev->dev)) |
|---|
| 1008 | + rs->spiclk = devm_clk_get(&pdev->dev, "spiclk"); |
|---|
| 1041 | 1009 | if (IS_ERR(rs->spiclk)) { |
|---|
| 1042 | 1010 | dev_err(&pdev->dev, "Failed to get spi_pclk\n"); |
|---|
| 1043 | 1011 | ret = PTR_ERR(rs->spiclk); |
|---|
| 1012 | + goto err_put_ctlr; |
|---|
| 1013 | + } |
|---|
| 1014 | + |
|---|
| 1015 | + rs->sclk_in = devm_clk_get_optional(&pdev->dev, "sclk_in"); |
|---|
| 1016 | + if (IS_ERR(rs->sclk_in)) { |
|---|
| 1017 | + dev_err(&pdev->dev, "Failed to get sclk_in\n"); |
|---|
| 1018 | + ret = PTR_ERR(rs->sclk_in); |
|---|
| 1044 | 1019 | goto err_put_ctlr; |
|---|
| 1045 | 1020 | } |
|---|
| 1046 | 1021 | |
|---|
| .. | .. |
|---|
| 1056 | 1031 | goto err_disable_apbclk; |
|---|
| 1057 | 1032 | } |
|---|
| 1058 | 1033 | |
|---|
| 1034 | + ret = clk_prepare_enable(rs->sclk_in); |
|---|
| 1035 | + if (ret < 0) { |
|---|
| 1036 | + dev_err(&pdev->dev, "Failed to enable sclk_in\n"); |
|---|
| 1037 | + goto err_disable_spiclk; |
|---|
| 1038 | + } |
|---|
| 1039 | + |
|---|
| 1059 | 1040 | spi_enable_chip(rs, false); |
|---|
| 1060 | 1041 | |
|---|
| 1061 | 1042 | ret = platform_get_irq(pdev, 0); |
|---|
| 1062 | 1043 | if (ret < 0) |
|---|
| 1063 | | - goto err_disable_spiclk; |
|---|
| 1044 | + goto err_disable_sclk_in; |
|---|
| 1064 | 1045 | |
|---|
| 1065 | 1046 | ret = devm_request_threaded_irq(&pdev->dev, ret, rockchip_spi_isr, NULL, |
|---|
| 1066 | 1047 | IRQF_ONESHOT, dev_name(&pdev->dev), ctlr); |
|---|
| 1067 | 1048 | if (ret) |
|---|
| 1068 | | - goto err_disable_spiclk; |
|---|
| 1049 | + goto err_disable_sclk_in; |
|---|
| 1069 | 1050 | |
|---|
| 1070 | 1051 | rs->dev = &pdev->dev; |
|---|
| 1071 | | - rs->freq = clk_get_rate(rs->spiclk); |
|---|
| 1072 | | - rs->gpio_requested = false; |
|---|
| 1073 | 1052 | |
|---|
| 1074 | | - if (!of_property_read_u32(pdev->dev.of_node, "rx-sample-delay-ns", |
|---|
| 1075 | | - &rsd_nsecs)) { |
|---|
| 1053 | + rs->freq = clk_get_rate(rs->spiclk); |
|---|
| 1054 | + if (!rs->freq) { |
|---|
| 1055 | + ret = device_property_read_u32(&pdev->dev, "clock-frequency", &rs->freq); |
|---|
| 1056 | + if (ret) { |
|---|
| 1057 | + dev_warn(rs->dev, "Failed to get clock or clock-frequency property\n"); |
|---|
| 1058 | + goto err_disable_sclk_in; |
|---|
| 1059 | + } |
|---|
| 1060 | + } |
|---|
| 1061 | + |
|---|
| 1062 | + if (!device_property_read_u32(&pdev->dev, "rx-sample-delay-ns", &rsd_nsecs)) { |
|---|
| 1076 | 1063 | /* rx sample delay is expressed in parent clock cycles (max 3) */ |
|---|
| 1077 | 1064 | u32 rsd = DIV_ROUND_CLOSEST(rsd_nsecs * (rs->freq >> 8), |
|---|
| 1078 | 1065 | 1000000000 >> 8); |
|---|
| .. | .. |
|---|
| 1102 | 1089 | if (!rs->fifo_len) { |
|---|
| 1103 | 1090 | dev_err(&pdev->dev, "Failed to get fifo length\n"); |
|---|
| 1104 | 1091 | ret = -EINVAL; |
|---|
| 1105 | | - goto err_disable_spiclk; |
|---|
| 1092 | + goto err_disable_sclk_in; |
|---|
| 1106 | 1093 | } |
|---|
| 1107 | 1094 | quirks_cfg = device_get_match_data(&pdev->dev); |
|---|
| 1108 | 1095 | if (quirks_cfg) |
|---|
| .. | .. |
|---|
| 1113 | 1100 | |
|---|
| 1114 | 1101 | ctlr->auto_runtime_pm = true; |
|---|
| 1115 | 1102 | ctlr->bus_num = pdev->id; |
|---|
| 1116 | | - ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST | SPI_CS_HIGH; |
|---|
| 1103 | + ctlr->mode_bits = SPI_CPOL | SPI_CPHA | SPI_LOOP | SPI_LSB_FIRST; |
|---|
| 1117 | 1104 | if (slave_mode) { |
|---|
| 1118 | 1105 | ctlr->mode_bits |= SPI_NO_CS; |
|---|
| 1119 | 1106 | ctlr->slave_abort = rockchip_spi_slave_abort; |
|---|
| 1120 | 1107 | } else { |
|---|
| 1121 | 1108 | ctlr->flags = SPI_MASTER_GPIO_SS; |
|---|
| 1109 | + ctlr->max_native_cs = ROCKCHIP_SPI_MAX_CS_NUM; |
|---|
| 1110 | + /* |
|---|
| 1111 | + * rk spi0 has two native cs, spi1..5 one cs only |
|---|
| 1112 | + * if num-cs is missing in the dts, default to 1 |
|---|
| 1113 | + */ |
|---|
| 1114 | + if (device_property_read_u32(&pdev->dev, "num-cs", &num_cs)) |
|---|
| 1115 | + num_cs = 1; |
|---|
| 1116 | + ctlr->num_chipselect = num_cs; |
|---|
| 1117 | + ctlr->use_gpio_descriptors = true; |
|---|
| 1122 | 1118 | } |
|---|
| 1123 | | - ctlr->num_chipselect = ROCKCHIP_SPI_MAX_CS_NUM; |
|---|
| 1124 | 1119 | ctlr->dev.of_node = pdev->dev.of_node; |
|---|
| 1125 | 1120 | ctlr->bits_per_word_mask = SPI_BPW_MASK(16) | SPI_BPW_MASK(8) | SPI_BPW_MASK(4); |
|---|
| 1126 | 1121 | ctlr->min_speed_hz = rs->freq / BAUDR_SCKDV_MAX; |
|---|
| 1127 | 1122 | ctlr->max_speed_hz = min(rs->freq / BAUDR_SCKDV_MIN, MAX_SCLK_OUT); |
|---|
| 1128 | 1123 | |
|---|
| 1129 | | - ctlr->set_cs = rockchip_spi_set_cs; |
|---|
| 1130 | 1124 | ctlr->setup = rockchip_spi_setup; |
|---|
| 1131 | | - ctlr->cleanup = rockchip_spi_cleanup; |
|---|
| 1125 | + ctlr->set_cs = rockchip_spi_set_cs; |
|---|
| 1132 | 1126 | ctlr->transfer_one = rockchip_spi_transfer_one; |
|---|
| 1133 | 1127 | ctlr->max_transfer_size = rockchip_spi_max_transfer_size; |
|---|
| 1134 | 1128 | ctlr->handle_err = rockchip_spi_handle_err; |
|---|
| .. | .. |
|---|
| 1169 | 1163 | } |
|---|
| 1170 | 1164 | |
|---|
| 1171 | 1165 | switch (rs->version) { |
|---|
| 1172 | | - case ROCKCHIP_SPI_VER2_TYPE1: |
|---|
| 1173 | 1166 | case ROCKCHIP_SPI_VER2_TYPE2: |
|---|
| 1167 | + rs->cs_high_supported = true; |
|---|
| 1168 | + ctlr->mode_bits |= SPI_CS_HIGH; |
|---|
| 1174 | 1169 | if (slave_mode) |
|---|
| 1175 | 1170 | rs->cs_inactive = true; |
|---|
| 1176 | 1171 | else |
|---|
| .. | .. |
|---|
| 1178 | 1173 | break; |
|---|
| 1179 | 1174 | default: |
|---|
| 1180 | 1175 | rs->cs_inactive = false; |
|---|
| 1176 | + break; |
|---|
| 1181 | 1177 | } |
|---|
| 1178 | + |
|---|
| 1182 | 1179 | pinctrl = devm_pinctrl_get(&pdev->dev); |
|---|
| 1183 | 1180 | if (!IS_ERR(pinctrl)) { |
|---|
| 1184 | 1181 | rs->high_speed_state = pinctrl_lookup_state(pinctrl, "high_speed"); |
|---|
| .. | .. |
|---|
| 1222 | 1219 | dma_release_channel(ctlr->dma_tx); |
|---|
| 1223 | 1220 | err_disable_pm_runtime: |
|---|
| 1224 | 1221 | pm_runtime_disable(&pdev->dev); |
|---|
| 1222 | +err_disable_sclk_in: |
|---|
| 1223 | + clk_disable_unprepare(rs->sclk_in); |
|---|
| 1225 | 1224 | err_disable_spiclk: |
|---|
| 1226 | 1225 | clk_disable_unprepare(rs->spiclk); |
|---|
| 1227 | 1226 | err_disable_apbclk: |
|---|
| .. | .. |
|---|
| 1242 | 1241 | |
|---|
| 1243 | 1242 | pm_runtime_get_sync(&pdev->dev); |
|---|
| 1244 | 1243 | |
|---|
| 1244 | + clk_disable_unprepare(rs->sclk_in); |
|---|
| 1245 | 1245 | clk_disable_unprepare(rs->spiclk); |
|---|
| 1246 | 1246 | clk_disable_unprepare(rs->apb_pclk); |
|---|
| 1247 | 1247 | |
|---|
| .. | .. |
|---|
| 1344 | 1344 | .compatible = "rockchip,px30-spi", |
|---|
| 1345 | 1345 | .data = &rockchip_spi_quirks_cfg, |
|---|
| 1346 | 1346 | }, |
|---|
| 1347 | | - { .compatible = "rockchip,rv1108-spi", }, |
|---|
| 1348 | | - { .compatible = "rockchip,rv1126-spi", }, |
|---|
| 1349 | 1347 | { .compatible = "rockchip,rk3036-spi", }, |
|---|
| 1350 | 1348 | { .compatible = "rockchip,rk3066-spi", }, |
|---|
| 1351 | 1349 | { .compatible = "rockchip,rk3188-spi", }, |
|---|
| 1352 | 1350 | { .compatible = "rockchip,rk3228-spi", }, |
|---|
| 1353 | 1351 | { .compatible = "rockchip,rk3288-spi", }, |
|---|
| 1352 | + { .compatible = "rockchip,rk3308-spi", }, |
|---|
| 1353 | + { .compatible = "rockchip,rk3328-spi", }, |
|---|
| 1354 | 1354 | { .compatible = "rockchip,rk3368-spi", }, |
|---|
| 1355 | 1355 | { .compatible = "rockchip,rk3399-spi", }, |
|---|
| 1356 | + { .compatible = "rockchip,rv1106-spi", }, |
|---|
| 1357 | + { .compatible = "rockchip,rv1108-spi", }, |
|---|
| 1358 | + { .compatible = "rockchip,rv1126-spi", }, |
|---|
| 1356 | 1359 | { }, |
|---|
| 1357 | 1360 | }; |
|---|
| 1358 | 1361 | MODULE_DEVICE_TABLE(of, rockchip_spi_dt_match); |
|---|