blob: 447497e9124c922a4e3ecc20af8ab7ee91062e99 [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>
Andy Shevchenko98999952015-10-14 23:12:25 +030022#include <linux/property.h>
Grant Likely568a60e2011-02-28 12:47:12 -070023
Grant Likelyca632f52011-06-06 01:16:30 -060024#include "spi-dw.h"
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070025
26#define DRIVER_NAME "dw_spi_mmio"
27
28struct dw_spi_mmio {
Jean-Hugues Deschenes0a4c1d72010-01-21 09:55:42 -070029 struct dw_spi dws;
30 struct clk *clk;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070031};
32
Grant Likelyfd4a3192012-12-07 16:57:14 +000033static int dw_spi_mmio_probe(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070034{
35 struct dw_spi_mmio *dwsmmio;
36 struct dw_spi *dws;
Baruch Siach04f421e2013-12-30 20:30:44 +020037 struct resource *mem;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070038 int ret;
Steffen Trumtrar22dae172014-06-13 15:36:18 +020039 int num_cs;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070040
Baruch Siach04f421e2013-12-30 20:30:44 +020041 dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio),
42 GFP_KERNEL);
43 if (!dwsmmio)
44 return -ENOMEM;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070045
46 dws = &dwsmmio->dws;
47
48 /* Get basic io resource and map it */
49 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Baruch Siach04f421e2013-12-30 20:30:44 +020050 dws->regs = devm_ioremap_resource(&pdev->dev, mem);
51 if (IS_ERR(dws->regs)) {
52 dev_err(&pdev->dev, "SPI region map failed\n");
53 return PTR_ERR(dws->regs);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070054 }
55
56 dws->irq = platform_get_irq(pdev, 0);
57 if (dws->irq < 0) {
58 dev_err(&pdev->dev, "no irq resource?\n");
Baruch Siach04f421e2013-12-30 20:30:44 +020059 return dws->irq; /* -ENXIO */
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070060 }
61
Baruch Siach04f421e2013-12-30 20:30:44 +020062 dwsmmio->clk = devm_clk_get(&pdev->dev, NULL);
63 if (IS_ERR(dwsmmio->clk))
64 return PTR_ERR(dwsmmio->clk);
Baruch Siach020fe3f2013-12-30 20:30:45 +020065 ret = clk_prepare_enable(dwsmmio->clk);
Baruch Siach04f421e2013-12-30 20:30:44 +020066 if (ret)
67 return ret;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070068
Baruch Siach2418991e2014-01-26 10:14:32 +020069 dws->bus_num = pdev->id;
Steffen Trumtrar22dae172014-06-13 15:36:18 +020070
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070071 dws->max_freq = clk_get_rate(dwsmmio->clk);
72
Andy Shevchenko98999952015-10-14 23:12:25 +030073 device_property_read_u32(&pdev->dev, "reg-io-width", &dws->reg_io_width);
Michael van der Westhuizenc4fe57f2015-08-18 22:21:53 +020074
Steffen Trumtrar22dae172014-06-13 15:36:18 +020075 num_cs = 4;
76
Andy Shevchenko98999952015-10-14 23:12:25 +030077 device_property_read_u32(&pdev->dev, "num-cs", &num_cs);
Steffen Trumtrar22dae172014-06-13 15:36:18 +020078
79 dws->num_cs = num_cs;
80
Baruch Siachd9c73bb2014-01-31 12:07:47 +020081 if (pdev->dev.of_node) {
82 int i;
83
84 for (i = 0; i < dws->num_cs; i++) {
85 int cs_gpio = of_get_named_gpio(pdev->dev.of_node,
86 "cs-gpios", i);
87
88 if (cs_gpio == -EPROBE_DEFER) {
89 ret = cs_gpio;
90 goto out;
91 }
92
93 if (gpio_is_valid(cs_gpio)) {
94 ret = devm_gpio_request(&pdev->dev, cs_gpio,
95 dev_name(&pdev->dev));
96 if (ret)
97 goto out;
98 }
99 }
100 }
101
Baruch Siach04f421e2013-12-30 20:30:44 +0200102 ret = dw_spi_add_host(&pdev->dev, dws);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700103 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +0200104 goto out;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700105
106 platform_set_drvdata(pdev, dwsmmio);
107 return 0;
108
Baruch Siach04f421e2013-12-30 20:30:44 +0200109out:
Baruch Siach020fe3f2013-12-30 20:30:45 +0200110 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700111 return ret;
112}
113
Grant Likelyfd4a3192012-12-07 16:57:14 +0000114static int dw_spi_mmio_remove(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700115{
116 struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700117
Baruch Siach020fe3f2013-12-30 20:30:45 +0200118 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700119 dw_spi_remove_host(&dwsmmio->dws);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700120
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700121 return 0;
122}
123
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200124static const struct of_device_id dw_spi_mmio_of_match[] = {
125 { .compatible = "snps,dw-apb-ssi", },
126 { /* end of table */}
127};
128MODULE_DEVICE_TABLE(of, dw_spi_mmio_of_match);
129
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700130static struct platform_driver dw_spi_mmio_driver = {
Grant Likely940ab882011-10-05 11:29:49 -0600131 .probe = dw_spi_mmio_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +0000132 .remove = dw_spi_mmio_remove,
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700133 .driver = {
134 .name = DRIVER_NAME,
Steffen Trumtrar22dae172014-06-13 15:36:18 +0200135 .of_match_table = dw_spi_mmio_of_match,
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700136 },
137};
Grant Likely940ab882011-10-05 11:29:49 -0600138module_platform_driver(dw_spi_mmio_driver);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700139
140MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
141MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
142MODULE_LICENSE("GPL v2");