summaryrefslogtreecommitdiffstats
path: root/drivers/spi/spi-ep93xx.c
diff options
context:
space:
mode:
authorH Hartley Sweeten <hsweeten@visionengravers.com>2017-02-16 15:07:37 -0500
committerMark Brown <broonie@kernel.org>2017-02-16 15:10:26 -0500
commit55f0cd3fb9c29c20fb94c47e28a9ec8cf704f8c2 (patch)
tree0b1b1c7ed5ec79bdf18719164fc1c0d133cf731b /drivers/spi/spi-ep93xx.c
parentfafd67940774733fa97f4b09412aea6981b82e0a (diff)
spi: spi-ep93xx: simplify GPIO chip selects
This driver requires a GPIO line to be used for the chip select of each SPI device. Remove the ep93xx_spi_chip_ops definition from the platform data and use the spi core GPIO handling for the chip selects. Fix all the ep93xx platforms that use this driver and remove the old Documentation. Signed-off-by: H Hartley Sweeten <hsweeten@visionengravers.com> Signed-off-by: Mark Brown <broonie@kernel.org>
Diffstat (limited to 'drivers/spi/spi-ep93xx.c')
-rw-r--r--drivers/spi/spi-ep93xx.c139
1 files changed, 44 insertions, 95 deletions
diff --git a/drivers/spi/spi-ep93xx.c b/drivers/spi/spi-ep93xx.c
index 17a6387e20b5..b5d766064b7b 100644
--- a/drivers/spi/spi-ep93xx.c
+++ b/drivers/spi/spi-ep93xx.c
@@ -28,6 +28,7 @@
28#include <linux/platform_device.h> 28#include <linux/platform_device.h>
29#include <linux/sched.h> 29#include <linux/sched.h>
30#include <linux/scatterlist.h> 30#include <linux/scatterlist.h>
31#include <linux/gpio.h>
31#include <linux/spi/spi.h> 32#include <linux/spi/spi.h>
32 33
33#include <linux/platform_data/dma-ep93xx.h> 34#include <linux/platform_data/dma-ep93xx.h>
@@ -107,16 +108,6 @@ struct ep93xx_spi {
107 void *zeropage; 108 void *zeropage;
108}; 109};
109 110
110/**
111 * struct ep93xx_spi_chip - SPI device hardware settings
112 * @spi: back pointer to the SPI device
113 * @ops: private chip operations
114 */
115struct ep93xx_spi_chip {
116 const struct spi_device *spi;
117 struct ep93xx_spi_chip_ops *ops;
118};
119
120/* converts bits per word to CR0.DSS value */ 111/* converts bits per word to CR0.DSS value */
121#define bits_per_word_to_dss(bpw) ((bpw) - 1) 112#define bits_per_word_to_dss(bpw) ((bpw) - 1)
122 113
@@ -229,104 +220,36 @@ static int ep93xx_spi_calc_divisors(const struct ep93xx_spi *espi,
229 return -EINVAL; 220 return -EINVAL;
230} 221}
231 222
232static void ep93xx_spi_cs_control(struct spi_device *spi, bool control) 223static void ep93xx_spi_cs_control(struct spi_device *spi, bool enable)
233{
234 struct ep93xx_spi_chip *chip = spi_get_ctldata(spi);
235 int value = (spi->mode & SPI_CS_HIGH) ? control : !control;
236
237 if (chip->ops && chip->ops->cs_control)
238 chip->ops->cs_control(spi, value);
239}
240
241/**
242 * ep93xx_spi_setup() - setup an SPI device
243 * @spi: SPI device to setup
244 *
245 * This function sets up SPI device mode, speed etc. Can be called multiple
246 * times for a single device. Returns %0 in case of success, negative error in
247 * case of failure. When this function returns success, the device is
248 * deselected.
249 */
250static int ep93xx_spi_setup(struct spi_device *spi)
251{ 224{
252 struct ep93xx_spi *espi = spi_master_get_devdata(spi->master); 225 if (spi->mode & SPI_CS_HIGH)
253 struct ep93xx_spi_chip *chip; 226 enable = !enable;
254 227
255 chip = spi_get_ctldata(spi); 228 if (gpio_is_valid(spi->cs_gpio))
256 if (!chip) { 229 gpio_set_value(spi->cs_gpio, !enable);
257 dev_dbg(&espi->pdev->dev, "initial setup for %s\n",
258 spi->modalias);
259
260 chip = kzalloc(sizeof(*chip), GFP_KERNEL);
261 if (!chip)
262 return -ENOMEM;
263
264 chip->spi = spi;
265 chip->ops = spi->controller_data;
266
267 if (chip->ops && chip->ops->setup) {
268 int ret = chip->ops->setup(spi);
269
270 if (ret) {
271 kfree(chip);
272 return ret;
273 }
274 }
275
276 spi_set_ctldata(spi, chip);
277 }
278
279 ep93xx_spi_cs_control(spi, false);
280 return 0;
281} 230}
282 231
283/**
284 * ep93xx_spi_cleanup() - cleans up master controller specific state
285 * @spi: SPI device to cleanup
286 *
287 * This function releases master controller specific state for given @spi
288 * device.
289 */
290static void ep93xx_spi_cleanup(struct spi_device *spi)
291{
292 struct ep93xx_spi_chip *chip;
293
294 chip = spi_get_ctldata(spi);
295 if (chip) {
296 if (chip->ops && chip->ops->cleanup)
297 chip->ops->cleanup(spi);
298 spi_set_ctldata(spi, NULL);
299 kfree(chip);
300 }
301}
302
303/**
304 * ep93xx_spi_chip_setup() - configures hardware according to given @chip
305 * @espi: ep93xx SPI controller struct
306 * @chip: chip specific settings
307 * @speed_hz: transfer speed
308 * @bits_per_word: transfer bits_per_word
309 */
310static int ep93xx_spi_chip_setup(const struct ep93xx_spi *espi, 232static int ep93xx_spi_chip_setup(const struct ep93xx_spi *espi,
311 const struct ep93xx_spi_chip *chip, 233 struct spi_device *spi,
312 u32 speed_hz, u8 bits_per_word) 234 struct spi_transfer *xfer)
313{ 235{
314 u8 dss = bits_per_word_to_dss(bits_per_word); 236 u8 dss = bits_per_word_to_dss(xfer->bits_per_word);
315 u8 div_cpsr = 0; 237 u8 div_cpsr = 0;
316 u8 div_scr = 0; 238 u8 div_scr = 0;
317 u16 cr0; 239 u16 cr0;
318 int err; 240 int err;
319 241
320 err = ep93xx_spi_calc_divisors(espi, speed_hz, &div_cpsr, &div_scr); 242 err = ep93xx_spi_calc_divisors(espi, xfer->speed_hz,
243 &div_cpsr, &div_scr);
321 if (err) 244 if (err)
322 return err; 245 return err;
323 246
324 cr0 = div_scr << SSPCR0_SCR_SHIFT; 247 cr0 = div_scr << SSPCR0_SCR_SHIFT;
325 cr0 |= (chip->spi->mode & (SPI_CPHA|SPI_CPOL)) << SSPCR0_MODE_SHIFT; 248 cr0 |= (spi->mode & (SPI_CPHA | SPI_CPOL)) << SSPCR0_MODE_SHIFT;
326 cr0 |= dss; 249 cr0 |= dss;
327 250
328 dev_dbg(&espi->pdev->dev, "setup: mode %d, cpsr %d, scr %d, dss %d\n", 251 dev_dbg(&espi->pdev->dev, "setup: mode %d, cpsr %d, scr %d, dss %d\n",
329 chip->spi->mode, div_cpsr, div_scr, dss); 252 spi->mode, div_cpsr, div_scr, dss);
330 dev_dbg(&espi->pdev->dev, "setup: cr0 %#x\n", cr0); 253 dev_dbg(&espi->pdev->dev, "setup: cr0 %#x\n", cr0);
331 254
332 ep93xx_spi_write_u8(espi, SSPCPSR, div_cpsr); 255 ep93xx_spi_write_u8(espi, SSPCPSR, div_cpsr);
@@ -603,12 +526,11 @@ static void ep93xx_spi_process_transfer(struct ep93xx_spi *espi,
603 struct spi_message *msg, 526 struct spi_message *msg,
604 struct spi_transfer *t) 527 struct spi_transfer *t)
605{ 528{
606 struct ep93xx_spi_chip *chip = spi_get_ctldata(msg->spi);
607 int err; 529 int err;
608 530
609 msg->state = t; 531 msg->state = t;
610 532
611 err = ep93xx_spi_chip_setup(espi, chip, t->speed_hz, t->bits_per_word); 533 err = ep93xx_spi_chip_setup(espi, msg->spi, t);
612 if (err) { 534 if (err) {
613 dev_err(&espi->pdev->dev, 535 dev_err(&espi->pdev->dev,
614 "failed to setup chip for transfer\n"); 536 "failed to setup chip for transfer\n");
@@ -863,8 +785,13 @@ static int ep93xx_spi_probe(struct platform_device *pdev)
863 struct resource *res; 785 struct resource *res;
864 int irq; 786 int irq;
865 int error; 787 int error;
788 int i;
866 789
867 info = dev_get_platdata(&pdev->dev); 790 info = dev_get_platdata(&pdev->dev);
791 if (!info) {
792 dev_err(&pdev->dev, "missing platform data\n");
793 return -EINVAL;
794 }
868 795
869 irq = platform_get_irq(pdev, 0); 796 irq = platform_get_irq(pdev, 0);
870 if (irq < 0) { 797 if (irq < 0) {
@@ -882,14 +809,36 @@ static int ep93xx_spi_probe(struct platform_device *pdev)
882 if (!master) 809 if (!master)
883 return -ENOMEM; 810 return -ENOMEM;
884 811
885 master->setup = ep93xx_spi_setup;
886 master->transfer_one_message = ep93xx_spi_transfer_one_message; 812 master->transfer_one_message = ep93xx_spi_transfer_one_message;
887 master->cleanup = ep93xx_spi_cleanup;
888 master->bus_num = pdev->id; 813 master->bus_num = pdev->id;
889 master->num_chipselect = info->num_chipselect;
890 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH; 814 master->mode_bits = SPI_CPOL | SPI_CPHA | SPI_CS_HIGH;
891 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16); 815 master->bits_per_word_mask = SPI_BPW_RANGE_MASK(4, 16);
892 816
817 master->num_chipselect = info->num_chipselect;
818 master->cs_gpios = devm_kzalloc(&master->dev,
819 sizeof(int) * master->num_chipselect,
820 GFP_KERNEL);
821 if (!master->cs_gpios) {
822 error = -ENOMEM;
823 goto fail_release_master;
824 }
825
826 for (i = 0; i < master->num_chipselect; i++) {
827 master->cs_gpios[i] = info->chipselect[i];
828
829 if (!gpio_is_valid(master->cs_gpios[i]))
830 continue;
831
832 error = devm_gpio_request_one(&pdev->dev, master->cs_gpios[i],
833 GPIOF_OUT_INIT_HIGH,
834 "ep93xx-spi");
835 if (error) {
836 dev_err(&pdev->dev, "could not request cs gpio %d\n",
837 master->cs_gpios[i]);
838 goto fail_release_master;
839 }
840 }
841
893 platform_set_drvdata(pdev, master); 842 platform_set_drvdata(pdev, master);
894 843
895 espi = spi_master_get_devdata(master); 844 espi = spi_master_get_devdata(master);