Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 1 | /* |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 2 | * Memory-mapped interface driver for DW SPI Core |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2010, Octasic semiconductor. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms and conditions of the GNU General Public License, |
| 8 | * version 2, as published by the Free Software Foundation. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/clk.h> |
Jamie Iles | 50c01fc | 2011-01-11 12:43:52 +0000 | [diff] [blame] | 12 | #include <linux/err.h> |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 13 | #include <linux/interrupt.h> |
| 14 | #include <linux/platform_device.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 15 | #include <linux/slab.h> |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 16 | #include <linux/spi/spi.h> |
Grant Likely | 568a60e | 2011-02-28 12:47:12 -0700 | [diff] [blame] | 17 | #include <linux/scatterlist.h> |
Paul Gortmaker | d7614de | 2011-07-03 15:44:29 -0400 | [diff] [blame] | 18 | #include <linux/module.h> |
Steffen Trumtrar | 22dae17 | 2014-06-13 15:36:18 +0200 | [diff] [blame] | 19 | #include <linux/of.h> |
Baruch Siach | d9c73bb | 2014-01-31 12:07:47 +0200 | [diff] [blame] | 20 | #include <linux/of_gpio.h> |
Steffen Trumtrar | 22dae17 | 2014-06-13 15:36:18 +0200 | [diff] [blame] | 21 | #include <linux/of_platform.h> |
Andy Shevchenko | 9899995 | 2015-10-14 23:12:25 +0300 | [diff] [blame] | 22 | #include <linux/property.h> |
Grant Likely | 568a60e | 2011-02-28 12:47:12 -0700 | [diff] [blame] | 23 | |
Grant Likely | ca632f5 | 2011-06-06 01:16:30 -0600 | [diff] [blame] | 24 | #include "spi-dw.h" |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 25 | |
| 26 | #define DRIVER_NAME "dw_spi_mmio" |
| 27 | |
| 28 | struct dw_spi_mmio { |
Jean-Hugues Deschenes | 0a4c1d7 | 2010-01-21 09:55:42 -0700 | [diff] [blame] | 29 | struct dw_spi dws; |
| 30 | struct clk *clk; |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 31 | }; |
| 32 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 33 | static int dw_spi_mmio_probe(struct platform_device *pdev) |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 34 | { |
| 35 | struct dw_spi_mmio *dwsmmio; |
| 36 | struct dw_spi *dws; |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 37 | struct resource *mem; |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 38 | int ret; |
Steffen Trumtrar | 22dae17 | 2014-06-13 15:36:18 +0200 | [diff] [blame] | 39 | int num_cs; |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 40 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 41 | dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio), |
| 42 | GFP_KERNEL); |
| 43 | if (!dwsmmio) |
| 44 | return -ENOMEM; |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 45 | |
| 46 | dws = &dwsmmio->dws; |
| 47 | |
| 48 | /* Get basic io resource and map it */ |
| 49 | mem = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 50 | if (!mem) { |
| 51 | dev_err(&pdev->dev, "no mem resource?\n"); |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 52 | return -EINVAL; |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 55 | dws->regs = devm_ioremap_resource(&pdev->dev, mem); |
| 56 | if (IS_ERR(dws->regs)) { |
| 57 | dev_err(&pdev->dev, "SPI region map failed\n"); |
| 58 | return PTR_ERR(dws->regs); |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | dws->irq = platform_get_irq(pdev, 0); |
| 62 | if (dws->irq < 0) { |
| 63 | dev_err(&pdev->dev, "no irq resource?\n"); |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 64 | return dws->irq; /* -ENXIO */ |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 65 | } |
| 66 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 67 | dwsmmio->clk = devm_clk_get(&pdev->dev, NULL); |
| 68 | if (IS_ERR(dwsmmio->clk)) |
| 69 | return PTR_ERR(dwsmmio->clk); |
Baruch Siach | 020fe3f | 2013-12-30 20:30:45 +0200 | [diff] [blame] | 70 | ret = clk_prepare_enable(dwsmmio->clk); |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 71 | if (ret) |
| 72 | return ret; |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 73 | |
Baruch Siach | 2418991e | 2014-01-26 10:14:32 +0200 | [diff] [blame] | 74 | dws->bus_num = pdev->id; |
Steffen Trumtrar | 22dae17 | 2014-06-13 15:36:18 +0200 | [diff] [blame] | 75 | |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 76 | dws->max_freq = clk_get_rate(dwsmmio->clk); |
| 77 | |
Andy Shevchenko | 9899995 | 2015-10-14 23:12:25 +0300 | [diff] [blame] | 78 | device_property_read_u32(&pdev->dev, "reg-io-width", &dws->reg_io_width); |
Michael van der Westhuizen | c4fe57f | 2015-08-18 22:21:53 +0200 | [diff] [blame] | 79 | |
Steffen Trumtrar | 22dae17 | 2014-06-13 15:36:18 +0200 | [diff] [blame] | 80 | num_cs = 4; |
| 81 | |
Andy Shevchenko | 9899995 | 2015-10-14 23:12:25 +0300 | [diff] [blame] | 82 | device_property_read_u32(&pdev->dev, "num-cs", &num_cs); |
Steffen Trumtrar | 22dae17 | 2014-06-13 15:36:18 +0200 | [diff] [blame] | 83 | |
| 84 | dws->num_cs = num_cs; |
| 85 | |
Baruch Siach | d9c73bb | 2014-01-31 12:07:47 +0200 | [diff] [blame] | 86 | if (pdev->dev.of_node) { |
| 87 | int i; |
| 88 | |
| 89 | for (i = 0; i < dws->num_cs; i++) { |
| 90 | int cs_gpio = of_get_named_gpio(pdev->dev.of_node, |
| 91 | "cs-gpios", i); |
| 92 | |
| 93 | if (cs_gpio == -EPROBE_DEFER) { |
| 94 | ret = cs_gpio; |
| 95 | goto out; |
| 96 | } |
| 97 | |
| 98 | if (gpio_is_valid(cs_gpio)) { |
| 99 | ret = devm_gpio_request(&pdev->dev, cs_gpio, |
| 100 | dev_name(&pdev->dev)); |
| 101 | if (ret) |
| 102 | goto out; |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 107 | ret = dw_spi_add_host(&pdev->dev, dws); |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 108 | if (ret) |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 109 | goto out; |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 110 | |
| 111 | platform_set_drvdata(pdev, dwsmmio); |
| 112 | return 0; |
| 113 | |
Baruch Siach | 04f421e | 2013-12-30 20:30:44 +0200 | [diff] [blame] | 114 | out: |
Baruch Siach | 020fe3f | 2013-12-30 20:30:45 +0200 | [diff] [blame] | 115 | clk_disable_unprepare(dwsmmio->clk); |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 116 | return ret; |
| 117 | } |
| 118 | |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 119 | static int dw_spi_mmio_remove(struct platform_device *pdev) |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 120 | { |
| 121 | struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev); |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 122 | |
Baruch Siach | 020fe3f | 2013-12-30 20:30:45 +0200 | [diff] [blame] | 123 | clk_disable_unprepare(dwsmmio->clk); |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 124 | dw_spi_remove_host(&dwsmmio->dws); |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 125 | |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Steffen Trumtrar | 22dae17 | 2014-06-13 15:36:18 +0200 | [diff] [blame] | 129 | static const struct of_device_id dw_spi_mmio_of_match[] = { |
| 130 | { .compatible = "snps,dw-apb-ssi", }, |
| 131 | { /* end of table */} |
| 132 | }; |
| 133 | MODULE_DEVICE_TABLE(of, dw_spi_mmio_of_match); |
| 134 | |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 135 | static struct platform_driver dw_spi_mmio_driver = { |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 136 | .probe = dw_spi_mmio_probe, |
Grant Likely | fd4a319 | 2012-12-07 16:57:14 +0000 | [diff] [blame] | 137 | .remove = dw_spi_mmio_remove, |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 138 | .driver = { |
| 139 | .name = DRIVER_NAME, |
Steffen Trumtrar | 22dae17 | 2014-06-13 15:36:18 +0200 | [diff] [blame] | 140 | .of_match_table = dw_spi_mmio_of_match, |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 141 | }, |
| 142 | }; |
Grant Likely | 940ab88 | 2011-10-05 11:29:49 -0600 | [diff] [blame] | 143 | module_platform_driver(dw_spi_mmio_driver); |
Jean-Hugues Deschenes | f7b6fd6 | 2010-01-21 07:46:42 -0700 | [diff] [blame] | 144 | |
| 145 | MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>"); |
| 146 | MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core"); |
| 147 | MODULE_LICENSE("GPL v2"); |