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