blob: cd164595f08a0cedfc7335a3033fe7a0980ca025 [file] [log] [blame]
Magnus Damma87d5632009-10-02 02:22:09 +00001/*
2 * SuperH Mobile SDHI
3 *
4 * Copyright (C) 2009 Magnus Damm
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * Based on "Compaq ASIC3 support":
11 *
12 * Copyright 2001 Compaq Computer Corporation.
13 * Copyright 2004-2005 Phil Blundell
14 * Copyright 2007-2008 OpenedHand Ltd.
15 *
16 * Authors: Phil Blundell <pb@handhelds.org>,
17 * Samuel Ortiz <sameo@openedhand.com>
18 *
19 */
20
21#include <linux/kernel.h>
22#include <linux/clk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Magnus Damma87d5632009-10-02 02:22:09 +000024#include <linux/platform_device.h>
Magnus Damm3c49e812010-02-22 13:37:09 +090025#include <linux/mmc/host.h>
Magnus Damma87d5632009-10-02 02:22:09 +000026#include <linux/mfd/core.h>
27#include <linux/mfd/tmio.h>
Magnus Dammbe9cd7b2009-11-27 04:31:27 +000028#include <linux/mfd/sh_mobile_sdhi.h>
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +000029#include <linux/sh_dma.h>
Magnus Damma87d5632009-10-02 02:22:09 +000030
31struct sh_mobile_sdhi {
32 struct clk *clk;
33 struct tmio_mmc_data mmc_data;
34 struct mfd_cell cell_mmc;
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +000035 struct sh_dmae_slave param_tx;
36 struct sh_dmae_slave param_rx;
37 struct tmio_mmc_dma dma_priv;
Magnus Damma87d5632009-10-02 02:22:09 +000038};
39
40static struct resource sh_mobile_sdhi_resources[] = {
41 {
42 .start = 0x000,
43 .end = 0x1ff,
44 .flags = IORESOURCE_MEM,
45 },
46 {
47 .start = 0,
48 .end = 0,
49 .flags = IORESOURCE_IRQ,
50 },
51};
52
53static struct mfd_cell sh_mobile_sdhi_cell = {
54 .name = "tmio-mmc",
55 .num_resources = ARRAY_SIZE(sh_mobile_sdhi_resources),
56 .resources = sh_mobile_sdhi_resources,
57};
58
Magnus Dammbe9cd7b2009-11-27 04:31:27 +000059static void sh_mobile_sdhi_set_pwr(struct platform_device *tmio, int state)
60{
61 struct platform_device *pdev = to_platform_device(tmio->dev.parent);
62 struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
63
64 if (p && p->set_pwr)
65 p->set_pwr(pdev, state);
66}
67
Magnus Damma87d5632009-10-02 02:22:09 +000068static int __init sh_mobile_sdhi_probe(struct platform_device *pdev)
69{
70 struct sh_mobile_sdhi *priv;
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +000071 struct tmio_mmc_data *mmc_data;
72 struct sh_mobile_sdhi_info *p = pdev->dev.platform_data;
Magnus Damma87d5632009-10-02 02:22:09 +000073 struct resource *mem;
74 char clk_name[8];
75 int ret, irq;
76
77 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
78 if (!mem)
79 dev_err(&pdev->dev, "missing MEM resource\n");
80
81 irq = platform_get_irq(pdev, 0);
82 if (irq < 0)
83 dev_err(&pdev->dev, "missing IRQ resource\n");
84
85 if (!mem || (irq < 0))
86 return -EINVAL;
87
88 priv = kzalloc(sizeof(struct sh_mobile_sdhi), GFP_KERNEL);
89 if (priv == NULL) {
90 dev_err(&pdev->dev, "kzalloc failed\n");
91 return -ENOMEM;
92 }
93
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +000094 mmc_data = &priv->mmc_data;
95
Magnus Damma87d5632009-10-02 02:22:09 +000096 snprintf(clk_name, sizeof(clk_name), "sdhi%d", pdev->id);
97 priv->clk = clk_get(&pdev->dev, clk_name);
98 if (IS_ERR(priv->clk)) {
99 dev_err(&pdev->dev, "cannot get clock \"%s\"\n", clk_name);
100 ret = PTR_ERR(priv->clk);
101 kfree(priv);
102 return ret;
103 }
104
105 clk_enable(priv->clk);
106
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +0000107 mmc_data->hclk = clk_get_rate(priv->clk);
108 mmc_data->set_pwr = sh_mobile_sdhi_set_pwr;
109 mmc_data->capabilities = MMC_CAP_MMC_HIGHSPEED;
Guennadi Liakhovetskibb0fe532010-05-19 18:37:36 +0000110 if (p) {
Guennadi Liakhovetskif87c20a2010-05-19 18:36:08 +0000111 mmc_data->flags = p->tmio_flags;
Guennadi Liakhovetskibb0fe532010-05-19 18:37:36 +0000112 mmc_data->ocr_mask = p->tmio_ocr_mask;
113 }
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +0000114
115 if (p && p->dma_slave_tx >= 0 && p->dma_slave_rx >= 0) {
116 priv->param_tx.slave_id = p->dma_slave_tx;
117 priv->param_rx.slave_id = p->dma_slave_rx;
118 priv->dma_priv.chan_priv_tx = &priv->param_tx;
119 priv->dma_priv.chan_priv_rx = &priv->param_rx;
120 mmc_data->dma = &priv->dma_priv;
121 }
Magnus Damma87d5632009-10-02 02:22:09 +0000122
123 memcpy(&priv->cell_mmc, &sh_mobile_sdhi_cell, sizeof(priv->cell_mmc));
Guennadi Liakhovetski056676d2010-05-19 18:34:16 +0000124 priv->cell_mmc.driver_data = mmc_data;
Magnus Damma87d5632009-10-02 02:22:09 +0000125 priv->cell_mmc.platform_data = &priv->cell_mmc;
126 priv->cell_mmc.data_size = sizeof(priv->cell_mmc);
127
128 platform_set_drvdata(pdev, priv);
129
130 ret = mfd_add_devices(&pdev->dev, pdev->id,
131 &priv->cell_mmc, 1, mem, irq);
132 if (ret) {
133 clk_disable(priv->clk);
134 clk_put(priv->clk);
135 kfree(priv);
136 }
137
138 return ret;
139}
140
141static int sh_mobile_sdhi_remove(struct platform_device *pdev)
142{
143 struct sh_mobile_sdhi *priv = platform_get_drvdata(pdev);
144
145 mfd_remove_devices(&pdev->dev);
146 clk_disable(priv->clk);
147 clk_put(priv->clk);
148 kfree(priv);
149
150 return 0;
151}
152
153static struct platform_driver sh_mobile_sdhi_driver = {
154 .driver = {
155 .name = "sh_mobile_sdhi",
156 .owner = THIS_MODULE,
157 },
158 .probe = sh_mobile_sdhi_probe,
159 .remove = __devexit_p(sh_mobile_sdhi_remove),
160};
161
162static int __init sh_mobile_sdhi_init(void)
163{
164 return platform_driver_register(&sh_mobile_sdhi_driver);
165}
166
167static void __exit sh_mobile_sdhi_exit(void)
168{
169 platform_driver_unregister(&sh_mobile_sdhi_driver);
170}
171
172module_init(sh_mobile_sdhi_init);
173module_exit(sh_mobile_sdhi_exit);
174
175MODULE_DESCRIPTION("SuperH Mobile SDHI driver");
176MODULE_AUTHOR("Magnus Damm");
177MODULE_LICENSE("GPL v2");