blob: c66a701ab21c04b271c8651e5e55426aa6f0fb45 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Ian Moltoncbdfb422008-07-15 15:12:52 +01002/*
3 * Toshiba TC6387XB support
4 * Copyright (c) 2005 Ian Molton
5 *
Ian Moltoncbdfb422008-07-15 15:12:52 +01006 * This file contains TC6387XB base support.
Ian Moltoncbdfb422008-07-15 15:12:52 +01007 */
8
9#include <linux/module.h>
10#include <linux/platform_device.h>
Ian Molton7acb7062008-10-09 20:06:09 +020011#include <linux/clk.h>
Ian Moltoncbdfb422008-07-15 15:12:52 +010012#include <linux/err.h>
13#include <linux/mfd/core.h>
14#include <linux/mfd/tmio.h>
15#include <linux/mfd/tc6387xb.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Ian Moltoncbdfb422008-07-15 15:12:52 +010017
Ian Moltond2432a62008-08-04 18:58:18 +020018enum {
19 TC6387XB_CELL_MMC,
20};
21
Ian Molton64e88672010-01-06 13:51:48 +010022struct tc6387xb {
23 void __iomem *scr;
24 struct clk *clk32k;
25 struct resource rscr;
26};
27
28static struct resource tc6387xb_mmc_resources[] = {
29 {
30 .start = 0x800,
31 .end = 0x9ff,
32 .flags = IORESOURCE_MEM,
33 },
34 {
35 .start = 0,
36 .end = 0,
37 .flags = IORESOURCE_IRQ,
38 },
39};
40
41/*--------------------------------------------------------------------------*/
42
Ian Moltoncbdfb422008-07-15 15:12:52 +010043#ifdef CONFIG_PM
44static int tc6387xb_suspend(struct platform_device *dev, pm_message_t state)
45{
Ian Molton64e88672010-01-06 13:51:48 +010046 struct tc6387xb *tc6387xb = platform_get_drvdata(dev);
Jingoo Han334a41ce2013-07-30 17:10:05 +090047 struct tc6387xb_platform_data *pdata = dev_get_platdata(&dev->dev);
Ian Moltoncbdfb422008-07-15 15:12:52 +010048
49 if (pdata && pdata->suspend)
50 pdata->suspend(dev);
Dmitry Eremin-Solenikov7263bd32014-11-17 18:07:43 +030051 clk_disable_unprepare(tc6387xb->clk32k);
Ian Moltoncbdfb422008-07-15 15:12:52 +010052
53 return 0;
54}
55
56static int tc6387xb_resume(struct platform_device *dev)
57{
Ian Molton64e88672010-01-06 13:51:48 +010058 struct tc6387xb *tc6387xb = platform_get_drvdata(dev);
Jingoo Han334a41ce2013-07-30 17:10:05 +090059 struct tc6387xb_platform_data *pdata = dev_get_platdata(&dev->dev);
Ian Moltoncbdfb422008-07-15 15:12:52 +010060
Dmitry Eremin-Solenikov7263bd32014-11-17 18:07:43 +030061 clk_prepare_enable(tc6387xb->clk32k);
Ian Moltoncbdfb422008-07-15 15:12:52 +010062 if (pdata && pdata->resume)
63 pdata->resume(dev);
64
Ian Molton64e88672010-01-06 13:51:48 +010065 tmio_core_mmc_resume(tc6387xb->scr + 0x200, 0,
66 tc6387xb_mmc_resources[0].start & 0xfffe);
67
Ian Moltoncbdfb422008-07-15 15:12:52 +010068 return 0;
69}
70#else
71#define tc6387xb_suspend NULL
72#define tc6387xb_resume NULL
73#endif
74
75/*--------------------------------------------------------------------------*/
76
Ian Molton64e88672010-01-06 13:51:48 +010077static void tc6387xb_mmc_pwr(struct platform_device *mmc, int state)
78{
Kefeng Wanged835132019-05-09 22:23:39 +080079 struct tc6387xb *tc6387xb = dev_get_drvdata(mmc->dev.parent);
Ian Molton64e88672010-01-06 13:51:48 +010080
81 tmio_core_mmc_pwr(tc6387xb->scr + 0x200, 0, state);
82}
83
84static void tc6387xb_mmc_clk_div(struct platform_device *mmc, int state)
85{
Kefeng Wanged835132019-05-09 22:23:39 +080086 struct tc6387xb *tc6387xb = dev_get_drvdata(mmc->dev.parent);
Ian Molton64e88672010-01-06 13:51:48 +010087
88 tmio_core_mmc_clk_div(tc6387xb->scr + 0x200, 0, state);
89}
90
91
Ian Moltoncbdfb422008-07-15 15:12:52 +010092static int tc6387xb_mmc_enable(struct platform_device *mmc)
93{
Kefeng Wanged835132019-05-09 22:23:39 +080094 struct tc6387xb *tc6387xb = dev_get_drvdata(mmc->dev.parent);
Ian Moltoncbdfb422008-07-15 15:12:52 +010095
Dmitry Eremin-Solenikov7263bd32014-11-17 18:07:43 +030096 clk_prepare_enable(tc6387xb->clk32k);
Ian Molton64e88672010-01-06 13:51:48 +010097
98 tmio_core_mmc_enable(tc6387xb->scr + 0x200, 0,
99 tc6387xb_mmc_resources[0].start & 0xfffe);
Ian Moltoncbdfb422008-07-15 15:12:52 +0100100
101 return 0;
102}
103
104static int tc6387xb_mmc_disable(struct platform_device *mmc)
105{
Kefeng Wanged835132019-05-09 22:23:39 +0800106 struct tc6387xb *tc6387xb = dev_get_drvdata(mmc->dev.parent);
Ian Moltoncbdfb422008-07-15 15:12:52 +0100107
Dmitry Eremin-Solenikov7263bd32014-11-17 18:07:43 +0300108 clk_disable_unprepare(tc6387xb->clk32k);
Ian Moltoncbdfb422008-07-15 15:12:52 +0100109
110 return 0;
111}
112
Samuel Ortiz4d3792e2009-06-15 15:43:31 +0200113static struct tmio_mmc_data tc6387xb_mmc_data = {
Philipp Zabelf0e46cc2009-06-04 20:12:31 +0200114 .hclk = 24000000,
Ian Molton64e88672010-01-06 13:51:48 +0100115 .set_pwr = tc6387xb_mmc_pwr,
116 .set_clk_div = tc6387xb_mmc_clk_div,
Philipp Zabelf0e46cc2009-06-04 20:12:31 +0200117};
118
Ian Molton64e88672010-01-06 13:51:48 +0100119/*--------------------------------------------------------------------------*/
Ian Moltoncbdfb422008-07-15 15:12:52 +0100120
Geert Uytterhoevenafb580a2013-11-18 14:33:03 +0100121static const struct mfd_cell tc6387xb_cells[] = {
Ian Moltond2432a62008-08-04 18:58:18 +0200122 [TC6387XB_CELL_MMC] = {
Ian Moltoncbdfb422008-07-15 15:12:52 +0100123 .name = "tmio-mmc",
124 .enable = tc6387xb_mmc_enable,
125 .disable = tc6387xb_mmc_disable,
Samuel Ortizec719742011-04-06 11:38:14 +0200126 .platform_data = &tc6387xb_mmc_data,
127 .pdata_size = sizeof(tc6387xb_mmc_data),
Ian Moltoncbdfb422008-07-15 15:12:52 +0100128 .num_resources = ARRAY_SIZE(tc6387xb_mmc_resources),
129 .resources = tc6387xb_mmc_resources,
130 },
131};
132
Bill Pembertonf791be42012-11-19 13:23:04 -0500133static int tc6387xb_probe(struct platform_device *dev)
Ian Moltoncbdfb422008-07-15 15:12:52 +0100134{
Jingoo Han334a41ce2013-07-30 17:10:05 +0900135 struct tc6387xb_platform_data *pdata = dev_get_platdata(&dev->dev);
Ian Molton64e88672010-01-06 13:51:48 +0100136 struct resource *iomem, *rscr;
Ian Molton7acb7062008-10-09 20:06:09 +0200137 struct clk *clk32k;
Ian Molton64e88672010-01-06 13:51:48 +0100138 struct tc6387xb *tc6387xb;
Ian Moltoncbdfb422008-07-15 15:12:52 +0100139 int irq, ret;
140
141 iomem = platform_get_resource(dev, IORESOURCE_MEM, 0);
Lee Jonesd5ce79f2014-07-21 15:10:35 +0100142 if (!iomem)
Ian Molton7acb7062008-10-09 20:06:09 +0200143 return -EINVAL;
Ian Moltoncbdfb422008-07-15 15:12:52 +0100144
Lee Jonesd5ce79f2014-07-21 15:10:35 +0100145 tc6387xb = kzalloc(sizeof(*tc6387xb), GFP_KERNEL);
Ian Molton64e88672010-01-06 13:51:48 +0100146 if (!tc6387xb)
147 return -ENOMEM;
148
Ian Moltoncbdfb422008-07-15 15:12:52 +0100149 ret = platform_get_irq(dev, 0);
150 if (ret >= 0)
151 irq = ret;
152 else
Ian Molton64e88672010-01-06 13:51:48 +0100153 goto err_no_irq;
Ian Moltoncbdfb422008-07-15 15:12:52 +0100154
Ian Molton7acb7062008-10-09 20:06:09 +0200155 clk32k = clk_get(&dev->dev, "CLK_CK32K");
156 if (IS_ERR(clk32k)) {
157 ret = PTR_ERR(clk32k);
Ian Molton64e88672010-01-06 13:51:48 +0100158 goto err_no_clk;
Ian Molton7acb7062008-10-09 20:06:09 +0200159 }
Ian Molton64e88672010-01-06 13:51:48 +0100160
161 rscr = &tc6387xb->rscr;
162 rscr->name = "tc6387xb-core";
163 rscr->start = iomem->start;
164 rscr->end = iomem->start + 0xff;
165 rscr->flags = IORESOURCE_MEM;
166
167 ret = request_resource(iomem, rscr);
168 if (ret)
169 goto err_resource;
170
Joe Perches28f65c112011-06-09 09:13:32 -0700171 tc6387xb->scr = ioremap(rscr->start, resource_size(rscr));
Ian Molton64e88672010-01-06 13:51:48 +0100172 if (!tc6387xb->scr) {
173 ret = -ENOMEM;
174 goto err_ioremap;
175 }
176
177 tc6387xb->clk32k = clk32k;
178 platform_set_drvdata(dev, tc6387xb);
Ian Molton7acb7062008-10-09 20:06:09 +0200179
180 if (pdata && pdata->enable)
181 pdata->enable(dev);
Ian Moltoncbdfb422008-07-15 15:12:52 +0100182
Lee Jonesd5ce79f2014-07-21 15:10:35 +0100183 dev_info(&dev->dev, "Toshiba tc6387xb initialised\n");
Ian Moltoncbdfb422008-07-15 15:12:52 +0100184
Samuel Ortiz56bf2bd2008-08-01 00:16:13 +0200185 ret = mfd_add_devices(&dev->dev, dev->id, tc6387xb_cells,
Mark Brown0848c942012-09-11 15:16:36 +0800186 ARRAY_SIZE(tc6387xb_cells), iomem, irq, NULL);
Ian Moltoncbdfb422008-07-15 15:12:52 +0100187
188 if (!ret)
189 return 0;
190
Axel Lin08b877b2010-08-03 13:44:00 +0800191 iounmap(tc6387xb->scr);
Ian Molton64e88672010-01-06 13:51:48 +0100192err_ioremap:
193 release_resource(&tc6387xb->rscr);
Ian Moltoncbdfb422008-07-15 15:12:52 +0100194err_resource:
Ian Molton64e88672010-01-06 13:51:48 +0100195 clk_put(clk32k);
196err_no_clk:
197err_no_irq:
198 kfree(tc6387xb);
Ian Moltoncbdfb422008-07-15 15:12:52 +0100199 return ret;
200}
201
Bill Pemberton4740f732012-11-19 13:26:01 -0500202static int tc6387xb_remove(struct platform_device *dev)
Ian Moltoncbdfb422008-07-15 15:12:52 +0100203{
Axel Lin08b877b2010-08-03 13:44:00 +0800204 struct tc6387xb *tc6387xb = platform_get_drvdata(dev);
Ian Moltoncbdfb422008-07-15 15:12:52 +0100205
Ian Molton7acb7062008-10-09 20:06:09 +0200206 mfd_remove_devices(&dev->dev);
Axel Lin08b877b2010-08-03 13:44:00 +0800207 iounmap(tc6387xb->scr);
208 release_resource(&tc6387xb->rscr);
Dmitry Eremin-Solenikov7263bd32014-11-17 18:07:43 +0300209 clk_disable_unprepare(tc6387xb->clk32k);
Axel Lin08b877b2010-08-03 13:44:00 +0800210 clk_put(tc6387xb->clk32k);
Axel Lin08b877b2010-08-03 13:44:00 +0800211 kfree(tc6387xb);
Ian Moltoncbdfb422008-07-15 15:12:52 +0100212
213 return 0;
214}
215
216
217static struct platform_driver tc6387xb_platform_driver = {
218 .driver = {
219 .name = "tc6387xb",
220 },
221 .probe = tc6387xb_probe,
Bill Pemberton84449212012-11-19 13:20:24 -0500222 .remove = tc6387xb_remove,
Ian Moltoncbdfb422008-07-15 15:12:52 +0100223 .suspend = tc6387xb_suspend,
224 .resume = tc6387xb_resume,
225};
226
Mark Brown65349d62011-11-23 22:58:34 +0000227module_platform_driver(tc6387xb_platform_driver);
Ian Moltoncbdfb422008-07-15 15:12:52 +0100228
229MODULE_DESCRIPTION("Toshiba TC6387XB core driver");
230MODULE_LICENSE("GPL v2");
231MODULE_AUTHOR("Ian Molton");
232MODULE_ALIAS("platform:tc6387xb");
Ian Molton64e88672010-01-06 13:51:48 +0100233