Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 1 | /* |
| 2 | * CLPS711X SPI bus driver |
| 3 | * |
Alexander Shiyan | 9898479 | 2014-01-10 17:02:05 +0400 | [diff] [blame] | 4 | * Copyright (C) 2012-2014 Alexander Shiyan <shc_work@mail.ru> |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 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> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/gpio.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/interrupt.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/spi/spi.h> |
| 21 | #include <linux/platform_data/spi-clps711x.h> |
| 22 | |
| 23 | #include <mach/hardware.h> |
| 24 | |
| 25 | #define DRIVER_NAME "spi-clps711x" |
| 26 | |
| 27 | struct spi_clps711x_data { |
| 28 | struct completion done; |
| 29 | |
| 30 | struct clk *spi_clk; |
| 31 | u32 max_speed_hz; |
| 32 | |
| 33 | u8 *tx_buf; |
| 34 | u8 *rx_buf; |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 35 | unsigned int bpw; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 36 | int len; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | static int spi_clps711x_setup(struct spi_device *spi) |
| 40 | { |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 41 | /* We are expect that SPI-device is not selected */ |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame^] | 42 | gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH)); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 43 | |
| 44 | return 0; |
| 45 | } |
| 46 | |
| 47 | static void spi_clps711x_setup_mode(struct spi_device *spi) |
| 48 | { |
| 49 | /* Setup edge for transfer */ |
| 50 | if (spi->mode & SPI_CPHA) |
| 51 | clps_writew(clps_readw(SYSCON3) | SYSCON3_ADCCKNSEN, SYSCON3); |
| 52 | else |
| 53 | clps_writew(clps_readw(SYSCON3) & ~SYSCON3_ADCCKNSEN, SYSCON3); |
| 54 | } |
| 55 | |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 56 | static void spi_clps711x_setup_xfer(struct spi_device *spi, |
| 57 | struct spi_transfer *xfer) |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 58 | { |
| 59 | u32 speed = xfer->speed_hz ? : spi->max_speed_hz; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 60 | struct spi_clps711x_data *hw = spi_master_get_devdata(spi->master); |
| 61 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 62 | /* Setup SPI frequency divider */ |
| 63 | if (!speed || (speed >= hw->max_speed_hz)) |
| 64 | clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) | |
| 65 | SYSCON1_ADCKSEL(3), SYSCON1); |
| 66 | else if (speed >= (hw->max_speed_hz / 2)) |
| 67 | clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) | |
| 68 | SYSCON1_ADCKSEL(2), SYSCON1); |
| 69 | else if (speed >= (hw->max_speed_hz / 8)) |
| 70 | clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) | |
| 71 | SYSCON1_ADCKSEL(1), SYSCON1); |
| 72 | else |
| 73 | clps_writel((clps_readl(SYSCON1) & ~SYSCON1_ADCKSEL_MASK) | |
| 74 | SYSCON1_ADCKSEL(0), SYSCON1); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static int spi_clps711x_transfer_one_message(struct spi_master *master, |
| 78 | struct spi_message *msg) |
| 79 | { |
| 80 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 81 | struct spi_device *spi = msg->spi; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 82 | struct spi_transfer *xfer; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 83 | |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 84 | spi_clps711x_setup_mode(spi); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 85 | |
| 86 | list_for_each_entry(xfer, &msg->transfers, transfer_list) { |
Alexander Shiyan | c7a26f1 | 2014-02-02 10:59:48 +0400 | [diff] [blame] | 87 | u8 data; |
| 88 | |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 89 | spi_clps711x_setup_xfer(spi, xfer); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 90 | |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame^] | 91 | gpio_set_value(spi->cs_gpio, !!(spi->mode & SPI_CS_HIGH)); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 92 | |
Wolfram Sang | 16735d0 | 2013-11-14 14:32:02 -0800 | [diff] [blame] | 93 | reinit_completion(&hw->done); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 94 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 95 | hw->len = xfer->len; |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 96 | hw->bpw = xfer->bits_per_word ? : spi->bits_per_word; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 97 | hw->tx_buf = (u8 *)xfer->tx_buf; |
| 98 | hw->rx_buf = (u8 *)xfer->rx_buf; |
| 99 | |
| 100 | /* Initiate transfer */ |
Alexander Shiyan | c7a26f1 | 2014-02-02 10:59:48 +0400 | [diff] [blame] | 101 | data = hw->tx_buf ? *hw->tx_buf++ : 0; |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 102 | clps_writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, |
| 103 | SYNCIO); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 104 | |
| 105 | wait_for_completion(&hw->done); |
| 106 | |
| 107 | if (xfer->delay_usecs) |
| 108 | udelay(xfer->delay_usecs); |
| 109 | |
| 110 | if (xfer->cs_change || |
| 111 | list_is_last(&xfer->transfer_list, &msg->transfers)) |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame^] | 112 | gpio_set_value(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH)); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 113 | |
| 114 | msg->actual_length += xfer->len; |
| 115 | } |
| 116 | |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 117 | msg->status = 0; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 118 | spi_finalize_current_message(master); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static irqreturn_t spi_clps711x_isr(int irq, void *dev_id) |
| 124 | { |
| 125 | struct spi_clps711x_data *hw = (struct spi_clps711x_data *)dev_id; |
Alexander Shiyan | c7a26f1 | 2014-02-02 10:59:48 +0400 | [diff] [blame] | 126 | u8 data; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 127 | |
| 128 | /* Handle RX */ |
| 129 | data = clps_readb(SYNCIO); |
| 130 | if (hw->rx_buf) |
Alexander Shiyan | c7a26f1 | 2014-02-02 10:59:48 +0400 | [diff] [blame] | 131 | *hw->rx_buf++ = data; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 132 | |
| 133 | /* Handle TX */ |
Alexander Shiyan | c7a26f1 | 2014-02-02 10:59:48 +0400 | [diff] [blame] | 134 | if (--hw->len > 0) { |
| 135 | data = hw->tx_buf ? *hw->tx_buf++ : 0; |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 136 | clps_writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, |
| 137 | SYNCIO); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 138 | } else |
| 139 | complete(&hw->done); |
| 140 | |
| 141 | return IRQ_HANDLED; |
| 142 | } |
| 143 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 144 | static int spi_clps711x_probe(struct platform_device *pdev) |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 145 | { |
| 146 | int i, ret; |
| 147 | struct spi_master *master; |
| 148 | struct spi_clps711x_data *hw; |
| 149 | struct spi_clps711x_pdata *pdata = dev_get_platdata(&pdev->dev); |
| 150 | |
| 151 | if (!pdata) { |
| 152 | dev_err(&pdev->dev, "No platform data supplied\n"); |
| 153 | return -EINVAL; |
| 154 | } |
| 155 | |
| 156 | if (pdata->num_chipselect < 1) { |
| 157 | dev_err(&pdev->dev, "At least one CS must be defined\n"); |
| 158 | return -EINVAL; |
| 159 | } |
| 160 | |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame^] | 161 | master = spi_alloc_master(&pdev->dev, sizeof(*hw)); |
| 162 | if (!master) |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 163 | return -ENOMEM; |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame^] | 164 | |
| 165 | master->cs_gpios = devm_kzalloc(&pdev->dev, sizeof(int) * |
| 166 | pdata->num_chipselect, GFP_KERNEL); |
| 167 | if (!master->cs_gpios) { |
| 168 | ret = -ENOMEM; |
| 169 | goto err_out; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | master->bus_num = pdev->id; |
| 173 | master->mode_bits = SPI_CPHA | SPI_CS_HIGH; |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 174 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 175 | master->num_chipselect = pdata->num_chipselect; |
| 176 | master->setup = spi_clps711x_setup; |
| 177 | master->transfer_one_message = spi_clps711x_transfer_one_message; |
| 178 | |
| 179 | hw = spi_master_get_devdata(master); |
| 180 | |
| 181 | for (i = 0; i < master->num_chipselect; i++) { |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame^] | 182 | master->cs_gpios[i] = pdata->chipselect[i]; |
| 183 | if (!gpio_is_valid(master->cs_gpios[i])) { |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 184 | dev_err(&pdev->dev, "Invalid CS GPIO %i\n", i); |
| 185 | ret = -EINVAL; |
| 186 | goto err_out; |
| 187 | } |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame^] | 188 | if (devm_gpio_request(&pdev->dev, master->cs_gpios[i], NULL)) { |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 189 | dev_err(&pdev->dev, "Can't get CS GPIO %i\n", i); |
| 190 | ret = -EINVAL; |
| 191 | goto err_out; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | hw->spi_clk = devm_clk_get(&pdev->dev, "spi"); |
| 196 | if (IS_ERR(hw->spi_clk)) { |
| 197 | dev_err(&pdev->dev, "Can't get clocks\n"); |
| 198 | ret = PTR_ERR(hw->spi_clk); |
| 199 | goto err_out; |
| 200 | } |
| 201 | hw->max_speed_hz = clk_get_rate(hw->spi_clk); |
| 202 | |
| 203 | init_completion(&hw->done); |
| 204 | platform_set_drvdata(pdev, master); |
| 205 | |
| 206 | /* Disable extended mode due hardware problems */ |
| 207 | clps_writew(clps_readw(SYSCON3) & ~SYSCON3_ADCCON, SYSCON3); |
| 208 | |
| 209 | /* Clear possible pending interrupt */ |
| 210 | clps_readl(SYNCIO); |
| 211 | |
| 212 | ret = devm_request_irq(&pdev->dev, IRQ_SSEOTI, spi_clps711x_isr, 0, |
| 213 | dev_name(&pdev->dev), hw); |
| 214 | if (ret) { |
| 215 | dev_err(&pdev->dev, "Can't request IRQ\n"); |
Sachin Kamat | c708379 | 2013-09-27 15:32:53 +0530 | [diff] [blame] | 216 | goto err_out; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 217 | } |
| 218 | |
Jingoo Han | c493fc4 | 2013-09-24 13:27:48 +0900 | [diff] [blame] | 219 | ret = devm_spi_register_master(&pdev->dev, master); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 220 | if (!ret) { |
| 221 | dev_info(&pdev->dev, |
| 222 | "SPI bus driver initialized. Master clock %u Hz\n", |
| 223 | hw->max_speed_hz); |
| 224 | return 0; |
| 225 | } |
| 226 | |
| 227 | dev_err(&pdev->dev, "Failed to register master\n"); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 228 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 229 | err_out: |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 230 | spi_master_put(master); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 235 | static struct platform_driver clps711x_spi_driver = { |
| 236 | .driver = { |
| 237 | .name = DRIVER_NAME, |
| 238 | .owner = THIS_MODULE, |
| 239 | }, |
| 240 | .probe = spi_clps711x_probe, |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 241 | }; |
| 242 | module_platform_driver(clps711x_spi_driver); |
| 243 | |
| 244 | MODULE_LICENSE("GPL"); |
| 245 | MODULE_AUTHOR("Alexander Shiyan <shc_work@mail.ru>"); |
| 246 | MODULE_DESCRIPTION("CLPS711X SPI bus driver"); |
Axel Lin | 350a9b3 | 2014-01-14 17:01:54 +0800 | [diff] [blame] | 247 | MODULE_ALIAS("platform:" DRIVER_NAME); |