blob: 9af56cdf1540551d595409efc47533cf45c5e5c5 [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>
Grant Likely568a60e2011-02-28 12:47:12 -070019
Grant Likelyca632f52011-06-06 01:16:30 -060020#include "spi-dw.h"
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070021
22#define DRIVER_NAME "dw_spi_mmio"
23
24struct dw_spi_mmio {
Jean-Hugues Deschenes0a4c1d72010-01-21 09:55:42 -070025 struct dw_spi dws;
26 struct clk *clk;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070027};
28
Grant Likelyfd4a3192012-12-07 16:57:14 +000029static int dw_spi_mmio_probe(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070030{
31 struct dw_spi_mmio *dwsmmio;
32 struct dw_spi *dws;
Baruch Siach04f421e2013-12-30 20:30:44 +020033 struct resource *mem;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070034 int ret;
35
Baruch Siach04f421e2013-12-30 20:30:44 +020036 dwsmmio = devm_kzalloc(&pdev->dev, sizeof(struct dw_spi_mmio),
37 GFP_KERNEL);
38 if (!dwsmmio)
39 return -ENOMEM;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070040
41 dws = &dwsmmio->dws;
42
43 /* Get basic io resource and map it */
44 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
45 if (!mem) {
46 dev_err(&pdev->dev, "no mem resource?\n");
Baruch Siach04f421e2013-12-30 20:30:44 +020047 return -EINVAL;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070048 }
49
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
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070069 dws->bus_num = 0;
70 dws->num_cs = 4;
71 dws->max_freq = clk_get_rate(dwsmmio->clk);
72
Baruch Siach04f421e2013-12-30 20:30:44 +020073 ret = dw_spi_add_host(&pdev->dev, dws);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070074 if (ret)
Baruch Siach04f421e2013-12-30 20:30:44 +020075 goto out;
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070076
77 platform_set_drvdata(pdev, dwsmmio);
78 return 0;
79
Baruch Siach04f421e2013-12-30 20:30:44 +020080out:
Baruch Siach020fe3f2013-12-30 20:30:45 +020081 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070082 return ret;
83}
84
Grant Likelyfd4a3192012-12-07 16:57:14 +000085static int dw_spi_mmio_remove(struct platform_device *pdev)
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070086{
87 struct dw_spi_mmio *dwsmmio = platform_get_drvdata(pdev);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070088
Baruch Siach020fe3f2013-12-30 20:30:45 +020089 clk_disable_unprepare(dwsmmio->clk);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070090 dw_spi_remove_host(&dwsmmio->dws);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070091
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070092 return 0;
93}
94
95static struct platform_driver dw_spi_mmio_driver = {
Grant Likely940ab882011-10-05 11:29:49 -060096 .probe = dw_spi_mmio_probe,
Grant Likelyfd4a3192012-12-07 16:57:14 +000097 .remove = dw_spi_mmio_remove,
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -070098 .driver = {
99 .name = DRIVER_NAME,
100 .owner = THIS_MODULE,
101 },
102};
Grant Likely940ab882011-10-05 11:29:49 -0600103module_platform_driver(dw_spi_mmio_driver);
Jean-Hugues Deschenesf7b6fd62010-01-21 07:46:42 -0700104
105MODULE_AUTHOR("Jean-Hugues Deschenes <jean-hugues.deschenes@octasic.com>");
106MODULE_DESCRIPTION("Memory-mapped I/O interface driver for DW SPI Core");
107MODULE_LICENSE("GPL v2");