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> |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 14 | #include <linux/gpio.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/interrupt.h> |
| 18 | #include <linux/platform_device.h> |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 19 | #include <linux/regmap.h> |
| 20 | #include <linux/mfd/syscon.h> |
| 21 | #include <linux/mfd/syscon/clps711x.h> |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 22 | #include <linux/spi/spi.h> |
| 23 | #include <linux/platform_data/spi-clps711x.h> |
| 24 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 25 | #define DRIVER_NAME "spi-clps711x" |
| 26 | |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 27 | #define SYNCIO_FRMLEN(x) ((x) << 8) |
| 28 | #define SYNCIO_TXFRMEN (1 << 14) |
| 29 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 30 | struct spi_clps711x_data { |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 31 | void __iomem *syncio; |
| 32 | struct regmap *syscon; |
| 33 | struct regmap *syscon1; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 34 | struct clk *spi_clk; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 35 | |
| 36 | u8 *tx_buf; |
| 37 | u8 *rx_buf; |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 38 | unsigned int bpw; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 39 | int len; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 40 | }; |
| 41 | |
| 42 | static int spi_clps711x_setup(struct spi_device *spi) |
| 43 | { |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 44 | /* We are expect that SPI-device is not selected */ |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame] | 45 | gpio_direction_output(spi->cs_gpio, !(spi->mode & SPI_CS_HIGH)); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 46 | |
| 47 | return 0; |
| 48 | } |
| 49 | |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 50 | static void spi_clps711x_setup_xfer(struct spi_device *spi, |
| 51 | struct spi_transfer *xfer) |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 52 | { |
Axel Lin | 6f50c6bc | 2014-03-26 23:44:18 +0800 | [diff] [blame] | 53 | struct spi_master *master = spi->master; |
| 54 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 55 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 56 | /* Setup SPI frequency divider */ |
Axel Lin | 6f50c6bc | 2014-03-26 23:44:18 +0800 | [diff] [blame] | 57 | if (xfer->speed_hz >= master->max_speed_hz) |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 58 | regmap_update_bits(hw->syscon1, SYSCON_OFFSET, |
| 59 | SYSCON1_ADCKSEL_MASK, SYSCON1_ADCKSEL(3)); |
Axel Lin | 6f50c6bc | 2014-03-26 23:44:18 +0800 | [diff] [blame] | 60 | else if (xfer->speed_hz >= (master->max_speed_hz / 2)) |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 61 | regmap_update_bits(hw->syscon1, SYSCON_OFFSET, |
| 62 | SYSCON1_ADCKSEL_MASK, SYSCON1_ADCKSEL(2)); |
Axel Lin | 6f50c6bc | 2014-03-26 23:44:18 +0800 | [diff] [blame] | 63 | else if (xfer->speed_hz >= (master->max_speed_hz / 8)) |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 64 | regmap_update_bits(hw->syscon1, SYSCON_OFFSET, |
| 65 | SYSCON1_ADCKSEL_MASK, SYSCON1_ADCKSEL(1)); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 66 | else |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 67 | regmap_update_bits(hw->syscon1, SYSCON_OFFSET, |
| 68 | SYSCON1_ADCKSEL_MASK, SYSCON1_ADCKSEL(0)); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 69 | } |
| 70 | |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 71 | static int spi_clps711x_prepare_message(struct spi_master *master, |
| 72 | struct spi_message *msg) |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 73 | { |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 74 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 75 | struct spi_device *spi = msg->spi; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 76 | |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 77 | /* Setup mode for transfer */ |
| 78 | return regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCKNSEN, |
| 79 | (spi->mode & SPI_CPHA) ? |
| 80 | SYSCON3_ADCCKNSEN : 0); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 81 | } |
| 82 | |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 83 | static int spi_clps711x_transfer_one(struct spi_master *master, |
| 84 | struct spi_device *spi, |
| 85 | struct spi_transfer *xfer) |
| 86 | { |
| 87 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); |
| 88 | u8 data; |
| 89 | |
| 90 | spi_clps711x_setup_xfer(spi, xfer); |
| 91 | |
| 92 | hw->len = xfer->len; |
Axel Lin | bed890b | 2014-03-02 23:24:18 +0800 | [diff] [blame] | 93 | hw->bpw = xfer->bits_per_word; |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 94 | hw->tx_buf = (u8 *)xfer->tx_buf; |
| 95 | hw->rx_buf = (u8 *)xfer->rx_buf; |
| 96 | |
| 97 | /* Initiate transfer */ |
| 98 | data = hw->tx_buf ? *hw->tx_buf++ : 0; |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 99 | writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, hw->syncio); |
| 100 | |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 101 | return 1; |
| 102 | } |
| 103 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 104 | static irqreturn_t spi_clps711x_isr(int irq, void *dev_id) |
| 105 | { |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 106 | struct spi_master *master = dev_id; |
| 107 | struct spi_clps711x_data *hw = spi_master_get_devdata(master); |
Alexander Shiyan | c7a26f1 | 2014-02-02 10:59:48 +0400 | [diff] [blame] | 108 | u8 data; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 109 | |
| 110 | /* Handle RX */ |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 111 | data = readb(hw->syncio); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 112 | if (hw->rx_buf) |
Alexander Shiyan | c7a26f1 | 2014-02-02 10:59:48 +0400 | [diff] [blame] | 113 | *hw->rx_buf++ = data; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 114 | |
| 115 | /* Handle TX */ |
Alexander Shiyan | c7a26f1 | 2014-02-02 10:59:48 +0400 | [diff] [blame] | 116 | if (--hw->len > 0) { |
| 117 | data = hw->tx_buf ? *hw->tx_buf++ : 0; |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 118 | writel(data | SYNCIO_FRMLEN(hw->bpw) | SYNCIO_TXFRMEN, |
| 119 | hw->syncio); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 120 | } else |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 121 | spi_finalize_current_transfer(master); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 122 | |
| 123 | return IRQ_HANDLED; |
| 124 | } |
| 125 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 126 | static int spi_clps711x_probe(struct platform_device *pdev) |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 127 | { |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 128 | struct spi_clps711x_data *hw; |
| 129 | struct spi_clps711x_pdata *pdata = dev_get_platdata(&pdev->dev); |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 130 | struct spi_master *master; |
| 131 | struct resource *res; |
| 132 | int i, irq, ret; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 133 | |
| 134 | if (!pdata) { |
| 135 | dev_err(&pdev->dev, "No platform data supplied\n"); |
| 136 | return -EINVAL; |
| 137 | } |
| 138 | |
| 139 | if (pdata->num_chipselect < 1) { |
| 140 | dev_err(&pdev->dev, "At least one CS must be defined\n"); |
| 141 | return -EINVAL; |
| 142 | } |
| 143 | |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 144 | irq = platform_get_irq(pdev, 0); |
| 145 | if (irq < 0) |
| 146 | return irq; |
| 147 | |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame] | 148 | master = spi_alloc_master(&pdev->dev, sizeof(*hw)); |
| 149 | if (!master) |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 150 | return -ENOMEM; |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame] | 151 | |
| 152 | master->cs_gpios = devm_kzalloc(&pdev->dev, sizeof(int) * |
| 153 | pdata->num_chipselect, GFP_KERNEL); |
| 154 | if (!master->cs_gpios) { |
| 155 | ret = -ENOMEM; |
| 156 | goto err_out; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | master->bus_num = pdev->id; |
| 160 | master->mode_bits = SPI_CPHA | SPI_CS_HIGH; |
Alexander Shiyan | 8dda9d9 | 2014-02-02 10:59:49 +0400 | [diff] [blame] | 161 | master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 8); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 162 | master->num_chipselect = pdata->num_chipselect; |
| 163 | master->setup = spi_clps711x_setup; |
Axel Lin | bf5c2e2 | 2014-02-18 17:15:54 +0800 | [diff] [blame] | 164 | master->prepare_message = spi_clps711x_prepare_message; |
| 165 | master->transfer_one = spi_clps711x_transfer_one; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 166 | |
| 167 | hw = spi_master_get_devdata(master); |
| 168 | |
| 169 | for (i = 0; i < master->num_chipselect; i++) { |
Alexander Shiyan | 3e9ea4b | 2014-02-02 10:59:50 +0400 | [diff] [blame] | 170 | master->cs_gpios[i] = pdata->chipselect[i]; |
Axel Lin | fcba212 | 2014-03-04 12:59:53 +0800 | [diff] [blame] | 171 | ret = devm_gpio_request(&pdev->dev, master->cs_gpios[i], |
| 172 | DRIVER_NAME); |
| 173 | if (ret) { |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 174 | dev_err(&pdev->dev, "Can't get CS GPIO %i\n", i); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 175 | goto err_out; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | hw->spi_clk = devm_clk_get(&pdev->dev, "spi"); |
| 180 | if (IS_ERR(hw->spi_clk)) { |
| 181 | dev_err(&pdev->dev, "Can't get clocks\n"); |
| 182 | ret = PTR_ERR(hw->spi_clk); |
| 183 | goto err_out; |
| 184 | } |
Axel Lin | 6f50c6bc | 2014-03-26 23:44:18 +0800 | [diff] [blame] | 185 | master->max_speed_hz = clk_get_rate(hw->spi_clk); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 186 | |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 187 | platform_set_drvdata(pdev, master); |
| 188 | |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 189 | hw->syscon = syscon_regmap_lookup_by_pdevname("syscon.3"); |
| 190 | if (IS_ERR(hw->syscon)) { |
| 191 | ret = PTR_ERR(hw->syscon); |
Sachin Kamat | c708379 | 2013-09-27 15:32:53 +0530 | [diff] [blame] | 192 | goto err_out; |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 193 | } |
| 194 | |
Alexander Shiyan | 3dc9259 | 2014-03-22 10:57:35 +0400 | [diff] [blame] | 195 | hw->syscon1 = syscon_regmap_lookup_by_pdevname("syscon.1"); |
| 196 | if (IS_ERR(hw->syscon1)) { |
| 197 | ret = PTR_ERR(hw->syscon1); |
| 198 | goto err_out; |
| 199 | } |
| 200 | |
| 201 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 202 | hw->syncio = devm_ioremap_resource(&pdev->dev, res); |
| 203 | if (IS_ERR(hw->syncio)) { |
| 204 | ret = PTR_ERR(hw->syncio); |
| 205 | goto err_out; |
| 206 | } |
| 207 | |
| 208 | /* Disable extended mode due hardware problems */ |
| 209 | regmap_update_bits(hw->syscon, SYSCON_OFFSET, SYSCON3_ADCCON, 0); |
| 210 | |
| 211 | /* Clear possible pending interrupt */ |
| 212 | readl(hw->syncio); |
| 213 | |
| 214 | ret = devm_request_irq(&pdev->dev, irq, spi_clps711x_isr, 0, |
| 215 | dev_name(&pdev->dev), master); |
| 216 | if (ret) |
| 217 | goto err_out; |
| 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", |
Axel Lin | 6f50c6bc | 2014-03-26 23:44:18 +0800 | [diff] [blame] | 223 | master->max_speed_hz); |
Alexander Shiyan | 161b96c | 2012-11-07 21:30:29 +0400 | [diff] [blame] | 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); |