blob: eb03e1215195bd5f9e9ea882cb1895db6ea79cdd [file] [log] [blame]
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -07001/*
Grant Likelyca632f52011-06-06 01:16:30 -06002 * Memory-mapped interface driver for DW SPI Core
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -07003 *
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 Iles50c01fc2011-01-11 12:43:52 +000012#include <linux/err.h>
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070013#include <linux/interrupt.h>
14#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090015#include <linux/slab.h>
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070016#include <linux/spi/spi.h>
Grant Likely568a60e2011-02-28 12:47:12 -070017#include <linux/scatterlist.h>
Paul Gortmakerd7614de2011-07-03 15:44:29 -040018#include <linux/module.h>
Steffen Trumtrar22dae172014-06-13 15:36:18 +020019#include <linux/of.h>
Baruch Siachd9c73bb2014-01-31 12:07:47 +020020#include <linux/of_gpio.h>
Steffen Trumtrar22dae172014-06-13 15:36:18 +020021#include <linux/of_platform.h>
Grant Likely568a60e2011-02-28 12:47:12 -070022
Grant Likelyca632f52011-06-06 01:16:30 -060023#include "spi-dw.h"
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070024
25#define DRIVER_NAME "dw_spi_mmio"
26
27struct dw_spi_mmio {
Jean-Hugues Deschenes0a4c1d72010-01-21 09:55:42 -070028 struct dw_spi dws;
29 struct clk *clk;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070030};
31
Grant Likelyfd4a3192012-12-07 16:57:14 +000032static int dw_spi_mmio_probe(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070033{
34 struct dw_spi_mmio *dwsmmio;
35 struct dw_spi *dws;
Baruch Siach04f421e2013-12-30 20:30:44 +020036 struct resource *mem;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070037 int ret;
Steffen Trumtrar22dae172014-06-13 15:36:18 +020038 int num_cs;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070039
Baruch Siach04f421e2013-12-30 20:30:44 +020040 dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio),
41 GFP_KERNEL);
42 if (!dwsmmio)
43 return -ENOMEM;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070044
45 dws = &dwsmmio->dws;
46
47 /* Get basic io resource and map it */
48 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
49 if (!mem) {
50 dev_err(&pdev->dev, "no mem resource?\n");
Baruch Siach04f421e2013-12-30 20:30:44 +020051 return -EINVAL;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070052 }
53
Baruch Siach04f421e2013-12-30 20:30:44 +020054 dws->regs = devm_ioremap_resource(&pdev->dev, mem);
55 if (IS_ERR(dws->regs)) {
56 dev_err(&pdev->dev, "SPI region map failed\n");
57 return PTR_ERR(dws->regs);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070058 }
59
60 dws->irq = platform_get_irq(pdev, 0);
61 if (dws->irq < 0) {
62 dev_err(&pdev->dev, "no irq resource?\n");
Baruch Siach04f421e2013-12-30 20:30:44 +020063 return dws->irq; /* -ENXIO */
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070064 }
65
Baruch Siach04f421e2013-12-30 20:30:44 +020066 dwsmmio->clk = devm_clk_get(&pdev->dev, NULL);
67 if (IS_ERR(dwsmmio->clk))
68 return PTR_ERR(dwsmmio->clk);
Baruch Siach020fe3f2013-12-30 20:30:45 +020069 ret = clk_prepare_enable(dwsmmio->clk);
Baruch Siach04f421e2013-12-30 20:30:44 +020070 if (ret)
71 return ret;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070072
Baruch Siach2418991e2014-01-26 10:14:32 +020073 dws->bus_num = pdev->id;
Steffen Trumtrar22dae172014-06-13 15:36:18 +020074
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070075 dws->max_freq = clk_get_rate(dwsmmio->clk);
76
Steffen Trumtrar22dae172014-06-13 15:36:18 +020077 num_cs = 4;
78
79 if (pdev->dev.of_node)
80 of_property_read_u32(pdev->dev.of_node, "num-cs", &num_cs);
81
82 dws->num_cs = num_cs;
83
Baruch Siachd9c73bb2014-01-31 12:07:47 +020084 if (pdev->dev.of_node) {
85 int i;
86
87 for (i = 0; i < dws->num_cs; i++) {
88 int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
89 "cs-gpios", i);
90
91 if (cs_gpio == -EPROBE_DEFER) {
92 ret = cs_gpio;
93 goto out;
94 }
95
96 if (gpio_is_valid(cs_gpio)) {
97 ret = devm_gpio_request(&pdev->dev, cs_gpio,
98 dev_name(&pdev->dev));
99 if (ret)
100 goto out;
101 }
102 }
103 }
104
Baruch Siach04f421e2013-12-30 20:30:44 +0200105 ret = dw_spi_add_host(&pdev->dev, dws);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700106 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +0200107 goto out;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700108
109 platform_set_drvdata(pdev, dwsmmio);
110 return 0;
111
Baruch Siach04f421e2013-12-30 20:30:44 +0200112out:
Baruch Siach020fe3f2013-12-30 20:30:45 +0200113 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700114 return ret;
115}
116
Grant Likelyfd4a3192012-12-07 16:57:14 +0000117static int dw_spi_mmio_remove(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700118{
119 struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700120
Baruch Siach020fe3f2013-12-30 20:30:45 +0200121 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700122 dw_spi_remove_host(&dwsmmio->dws);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700123
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700124 return 0;
125}
126
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200127static const struct of_device_id dw_spi_mmio_of_match[] = {
128 { .compatible = "snps,dw-apb-ssi", },
129 { /* end of table */}
130};
131MODULE_DEVICE_TABLE(of, dw_spi_mmio_of_match);
132
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700133static struct platform_driver dw_spi_mmio_driver = {
Grant Likely940ab882011-10-05 11:29:49 -0600134 .probe = dw_spi_mmio_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000135 .remove = dw_spi_mmio_remove,
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700136 .driver = {
137 .name = DRIVER_NAME,
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200138 .of_match_table = dw_spi_mmio_of_match,
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700139 },
140};
Grant Likely940ab882011-10-05 11:29:49 -0600141module_platform_driver(dw_spi_mmio_driver);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700142
143MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
144MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
145MODULE_LICENSE("GPL v2");