Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Toshiba TC6387XB support |
| 4 | * Copyright (c) 2005 Ian Molton |
| 5 | * |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 6 | * This file contains TC6387XB base support. |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/platform_device.h> |
Ian Molton | 7acb706 | 2008-10-09 20:06:09 +0200 | [diff] [blame] | 11 | #include <linux/clk.h> |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 12 | #include <linux/err.h> |
| 13 | #include <linux/mfd/core.h> |
| 14 | #include <linux/mfd/tmio.h> |
| 15 | #include <linux/mfd/tc6387xb.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 17 | |
Ian Molton | d2432a6 | 2008-08-04 18:58:18 +0200 | [diff] [blame] | 18 | enum { |
| 19 | TC6387XB_CELL_MMC, |
| 20 | }; |
| 21 | |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 22 | struct tc6387xb { |
| 23 | void __iomem *scr; |
| 24 | struct clk *clk32k; |
| 25 | struct resource rscr; |
| 26 | }; |
| 27 | |
| 28 | static 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 Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 43 | #ifdef CONFIG_PM |
| 44 | static int tc6387xb_suspend(struct platform_device *dev, pm_message_t state) |
| 45 | { |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 46 | struct tc6387xb *tc6387xb = platform_get_drvdata(dev); |
Jingoo Han | 334a41ce | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 47 | struct tc6387xb_platform_data *pdata = dev_get_platdata(&dev->dev); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 48 | |
| 49 | if (pdata && pdata->suspend) |
| 50 | pdata->suspend(dev); |
Dmitry Eremin-Solenikov | 7263bd3 | 2014-11-17 18:07:43 +0300 | [diff] [blame] | 51 | clk_disable_unprepare(tc6387xb->clk32k); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static int tc6387xb_resume(struct platform_device *dev) |
| 57 | { |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 58 | struct tc6387xb *tc6387xb = platform_get_drvdata(dev); |
Jingoo Han | 334a41ce | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 59 | struct tc6387xb_platform_data *pdata = dev_get_platdata(&dev->dev); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 60 | |
Dmitry Eremin-Solenikov | 7263bd3 | 2014-11-17 18:07:43 +0300 | [diff] [blame] | 61 | clk_prepare_enable(tc6387xb->clk32k); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 62 | if (pdata && pdata->resume) |
| 63 | pdata->resume(dev); |
| 64 | |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 65 | tmio_core_mmc_resume(tc6387xb->scr + 0x200, 0, |
| 66 | tc6387xb_mmc_resources[0].start & 0xfffe); |
| 67 | |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 68 | return 0; |
| 69 | } |
| 70 | #else |
| 71 | #define tc6387xb_suspend NULL |
| 72 | #define tc6387xb_resume NULL |
| 73 | #endif |
| 74 | |
| 75 | /*--------------------------------------------------------------------------*/ |
| 76 | |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 77 | static void tc6387xb_mmc_pwr(struct platform_device *mmc, int state) |
| 78 | { |
Kefeng Wang | ed83513 | 2019-05-09 22:23:39 +0800 | [diff] [blame] | 79 | struct tc6387xb *tc6387xb = dev_get_drvdata(mmc->dev.parent); |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 80 | |
| 81 | tmio_core_mmc_pwr(tc6387xb->scr + 0x200, 0, state); |
| 82 | } |
| 83 | |
| 84 | static void tc6387xb_mmc_clk_div(struct platform_device *mmc, int state) |
| 85 | { |
Kefeng Wang | ed83513 | 2019-05-09 22:23:39 +0800 | [diff] [blame] | 86 | struct tc6387xb *tc6387xb = dev_get_drvdata(mmc->dev.parent); |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 87 | |
| 88 | tmio_core_mmc_clk_div(tc6387xb->scr + 0x200, 0, state); |
| 89 | } |
| 90 | |
| 91 | |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 92 | static int tc6387xb_mmc_enable(struct platform_device *mmc) |
| 93 | { |
Kefeng Wang | ed83513 | 2019-05-09 22:23:39 +0800 | [diff] [blame] | 94 | struct tc6387xb *tc6387xb = dev_get_drvdata(mmc->dev.parent); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 95 | |
Dmitry Eremin-Solenikov | 7263bd3 | 2014-11-17 18:07:43 +0300 | [diff] [blame] | 96 | clk_prepare_enable(tc6387xb->clk32k); |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 97 | |
| 98 | tmio_core_mmc_enable(tc6387xb->scr + 0x200, 0, |
| 99 | tc6387xb_mmc_resources[0].start & 0xfffe); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static int tc6387xb_mmc_disable(struct platform_device *mmc) |
| 105 | { |
Kefeng Wang | ed83513 | 2019-05-09 22:23:39 +0800 | [diff] [blame] | 106 | struct tc6387xb *tc6387xb = dev_get_drvdata(mmc->dev.parent); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 107 | |
Dmitry Eremin-Solenikov | 7263bd3 | 2014-11-17 18:07:43 +0300 | [diff] [blame] | 108 | clk_disable_unprepare(tc6387xb->clk32k); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 109 | |
| 110 | return 0; |
| 111 | } |
| 112 | |
Samuel Ortiz | 4d3792e | 2009-06-15 15:43:31 +0200 | [diff] [blame] | 113 | static struct tmio_mmc_data tc6387xb_mmc_data = { |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 114 | .hclk = 24000000, |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 115 | .set_pwr = tc6387xb_mmc_pwr, |
| 116 | .set_clk_div = tc6387xb_mmc_clk_div, |
Philipp Zabel | f0e46cc | 2009-06-04 20:12:31 +0200 | [diff] [blame] | 117 | }; |
| 118 | |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 119 | /*--------------------------------------------------------------------------*/ |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 120 | |
Geert Uytterhoeven | afb580a | 2013-11-18 14:33:03 +0100 | [diff] [blame] | 121 | static const struct mfd_cell tc6387xb_cells[] = { |
Ian Molton | d2432a6 | 2008-08-04 18:58:18 +0200 | [diff] [blame] | 122 | [TC6387XB_CELL_MMC] = { |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 123 | .name = "tmio-mmc", |
| 124 | .enable = tc6387xb_mmc_enable, |
| 125 | .disable = tc6387xb_mmc_disable, |
Samuel Ortiz | ec71974 | 2011-04-06 11:38:14 +0200 | [diff] [blame] | 126 | .platform_data = &tc6387xb_mmc_data, |
| 127 | .pdata_size = sizeof(tc6387xb_mmc_data), |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 128 | .num_resources = ARRAY_SIZE(tc6387xb_mmc_resources), |
| 129 | .resources = tc6387xb_mmc_resources, |
| 130 | }, |
| 131 | }; |
| 132 | |
Bill Pemberton | f791be4 | 2012-11-19 13:23:04 -0500 | [diff] [blame] | 133 | static int tc6387xb_probe(struct platform_device *dev) |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 134 | { |
Jingoo Han | 334a41ce | 2013-07-30 17:10:05 +0900 | [diff] [blame] | 135 | struct tc6387xb_platform_data *pdata = dev_get_platdata(&dev->dev); |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 136 | struct resource *iomem, *rscr; |
Ian Molton | 7acb706 | 2008-10-09 20:06:09 +0200 | [diff] [blame] | 137 | struct clk *clk32k; |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 138 | struct tc6387xb *tc6387xb; |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 139 | int irq, ret; |
| 140 | |
| 141 | iomem = platform_get_resource(dev, IORESOURCE_MEM, 0); |
Lee Jones | d5ce79f | 2014-07-21 15:10:35 +0100 | [diff] [blame] | 142 | if (!iomem) |
Ian Molton | 7acb706 | 2008-10-09 20:06:09 +0200 | [diff] [blame] | 143 | return -EINVAL; |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 144 | |
Lee Jones | d5ce79f | 2014-07-21 15:10:35 +0100 | [diff] [blame] | 145 | tc6387xb = kzalloc(sizeof(*tc6387xb), GFP_KERNEL); |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 146 | if (!tc6387xb) |
| 147 | return -ENOMEM; |
| 148 | |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 149 | ret = platform_get_irq(dev, 0); |
| 150 | if (ret >= 0) |
| 151 | irq = ret; |
| 152 | else |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 153 | goto err_no_irq; |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 154 | |
Ian Molton | 7acb706 | 2008-10-09 20:06:09 +0200 | [diff] [blame] | 155 | clk32k = clk_get(&dev->dev, "CLK_CK32K"); |
| 156 | if (IS_ERR(clk32k)) { |
| 157 | ret = PTR_ERR(clk32k); |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 158 | goto err_no_clk; |
Ian Molton | 7acb706 | 2008-10-09 20:06:09 +0200 | [diff] [blame] | 159 | } |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 160 | |
| 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 Perches | 28f65c11 | 2011-06-09 09:13:32 -0700 | [diff] [blame] | 171 | tc6387xb->scr = ioremap(rscr->start, resource_size(rscr)); |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 172 | if (!tc6387xb->scr) { |
| 173 | ret = -ENOMEM; |
| 174 | goto err_ioremap; |
| 175 | } |
| 176 | |
| 177 | tc6387xb->clk32k = clk32k; |
| 178 | platform_set_drvdata(dev, tc6387xb); |
Ian Molton | 7acb706 | 2008-10-09 20:06:09 +0200 | [diff] [blame] | 179 | |
| 180 | if (pdata && pdata->enable) |
| 181 | pdata->enable(dev); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 182 | |
Lee Jones | d5ce79f | 2014-07-21 15:10:35 +0100 | [diff] [blame] | 183 | dev_info(&dev->dev, "Toshiba tc6387xb initialised\n"); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 184 | |
Samuel Ortiz | 56bf2bd | 2008-08-01 00:16:13 +0200 | [diff] [blame] | 185 | ret = mfd_add_devices(&dev->dev, dev->id, tc6387xb_cells, |
Mark Brown | 0848c94 | 2012-09-11 15:16:36 +0800 | [diff] [blame] | 186 | ARRAY_SIZE(tc6387xb_cells), iomem, irq, NULL); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 187 | |
| 188 | if (!ret) |
| 189 | return 0; |
| 190 | |
Axel Lin | 08b877b | 2010-08-03 13:44:00 +0800 | [diff] [blame] | 191 | iounmap(tc6387xb->scr); |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 192 | err_ioremap: |
| 193 | release_resource(&tc6387xb->rscr); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 194 | err_resource: |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 195 | clk_put(clk32k); |
| 196 | err_no_clk: |
| 197 | err_no_irq: |
| 198 | kfree(tc6387xb); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 199 | return ret; |
| 200 | } |
| 201 | |
Bill Pemberton | 4740f73 | 2012-11-19 13:26:01 -0500 | [diff] [blame] | 202 | static int tc6387xb_remove(struct platform_device *dev) |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 203 | { |
Axel Lin | 08b877b | 2010-08-03 13:44:00 +0800 | [diff] [blame] | 204 | struct tc6387xb *tc6387xb = platform_get_drvdata(dev); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 205 | |
Ian Molton | 7acb706 | 2008-10-09 20:06:09 +0200 | [diff] [blame] | 206 | mfd_remove_devices(&dev->dev); |
Axel Lin | 08b877b | 2010-08-03 13:44:00 +0800 | [diff] [blame] | 207 | iounmap(tc6387xb->scr); |
| 208 | release_resource(&tc6387xb->rscr); |
Dmitry Eremin-Solenikov | 7263bd3 | 2014-11-17 18:07:43 +0300 | [diff] [blame] | 209 | clk_disable_unprepare(tc6387xb->clk32k); |
Axel Lin | 08b877b | 2010-08-03 13:44:00 +0800 | [diff] [blame] | 210 | clk_put(tc6387xb->clk32k); |
Axel Lin | 08b877b | 2010-08-03 13:44:00 +0800 | [diff] [blame] | 211 | kfree(tc6387xb); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | |
| 217 | static struct platform_driver tc6387xb_platform_driver = { |
| 218 | .driver = { |
| 219 | .name = "tc6387xb", |
| 220 | }, |
| 221 | .probe = tc6387xb_probe, |
Bill Pemberton | 8444921 | 2012-11-19 13:20:24 -0500 | [diff] [blame] | 222 | .remove = tc6387xb_remove, |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 223 | .suspend = tc6387xb_suspend, |
| 224 | .resume = tc6387xb_resume, |
| 225 | }; |
| 226 | |
Mark Brown | 65349d6 | 2011-11-23 22:58:34 +0000 | [diff] [blame] | 227 | module_platform_driver(tc6387xb_platform_driver); |
Ian Molton | cbdfb42 | 2008-07-15 15:12:52 +0100 | [diff] [blame] | 228 | |
| 229 | MODULE_DESCRIPTION("Toshiba TC6387XB core driver"); |
| 230 | MODULE_LICENSE("GPL v2"); |
| 231 | MODULE_AUTHOR("Ian Molton"); |
| 232 | MODULE_ALIAS("platform:tc6387xb"); |
Ian Molton | 64e8867 | 2010-01-06 13:51:48 +0100 | [diff] [blame] | 233 | |