upload tizen1.0 source
[kernel/linux-2.6.36.git] / drivers / mmc / host / sdhci-s3c.c
1 /* linux/drivers/mmc/host/sdhci-s3c.c
2  *
3  * Copyright 2008 Openmoko Inc.
4  * Copyright 2008 Simtec Electronics
5  *      Ben Dooks <ben@simtec.co.uk>
6  *      http://armlinux.simtec.co.uk/
7  *
8  * SDHCI (HSMMC) support for Samsung SoC
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License version 2 as
12  * published by the Free Software Foundation.
13  */
14
15 #include <linux/delay.h>
16 #include <linux/dma-mapping.h>
17 #include <linux/platform_device.h>
18 #include <linux/slab.h>
19 #include <linux/clk.h>
20 #include <linux/io.h>
21 #include <linux/gpio.h>
22 #include <linux/regulator/consumer.h>
23
24 #include <linux/mmc/host.h>
25
26 #include <plat/sdhci.h>
27 #include <plat/regs-sdhci.h>
28
29 #include "sdhci.h"
30
31 #define MAX_BUS_CLK     (4)
32
33 /**
34  * struct sdhci_s3c - S3C SDHCI instance
35  * @host: The SDHCI host created
36  * @pdev: The platform device we where created from.
37  * @ioarea: The resource created when we claimed the IO area.
38  * @pdata: The platform data for this controller.
39  * @cur_clk: The index of the current bus clock.
40  * @clk_io: The clock for the internal bus interface.
41  * @clk_bus: The clocks that are available for the SD/MMC bus clock.
42  */
43 struct sdhci_s3c {
44         struct sdhci_host       *host;
45         struct platform_device  *pdev;
46         struct resource         *ioarea;
47         struct s3c_sdhci_platdata *pdata;
48         unsigned int            cur_clk;
49         int                     ext_cd_irq;
50         int                     ext_cd_gpio;
51         int                     cd_status;
52         int                     flag;
53
54         struct clk              *clk_io;
55         struct clk              *clk_bus[MAX_BUS_CLK];
56 };
57
58 static inline struct sdhci_s3c *to_s3c(struct sdhci_host *host)
59 {
60         return sdhci_priv(host);
61 }
62
63 /**
64  * get_curclk - convert ctrl2 register to clock source number
65  * @ctrl2: Control2 register value.
66  */
67 static u32 get_curclk(u32 ctrl2)
68 {
69         ctrl2 &= S3C_SDHCI_CTRL2_SELBASECLK_MASK;
70         ctrl2 >>= S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
71
72         return ctrl2;
73 }
74
75 static void sdhci_s3c_check_sclk(struct sdhci_host *host)
76 {
77         struct sdhci_s3c *ourhost = to_s3c(host);
78         u32 tmp = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
79
80         if (get_curclk(tmp) != ourhost->cur_clk) {
81                 dev_dbg(&ourhost->pdev->dev, "restored ctrl2 clock setting\n");
82
83                 tmp &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
84                 tmp |= ourhost->cur_clk << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
85                 writel(tmp, host->ioaddr + 0x80);
86         }
87 }
88
89 /**
90  * sdhci_s3c_get_max_clk - callback to get maximum clock frequency.
91  * @host: The SDHCI host instance.
92  *
93  * Callback to return the maximum clock rate acheivable by the controller.
94 */
95 static unsigned int sdhci_s3c_get_max_clk(struct sdhci_host *host)
96 {
97         struct sdhci_s3c *ourhost = to_s3c(host);
98         struct clk *busclk;
99         unsigned int rate, max;
100         int clk;
101
102         /* note, a reset will reset the clock source */
103
104         sdhci_s3c_check_sclk(host);
105
106         for (max = 0, clk = 0; clk < MAX_BUS_CLK; clk++) {
107                 busclk = ourhost->clk_bus[clk];
108                 if (!busclk)
109                         continue;
110
111                 rate = clk_get_rate(busclk);
112                 if (rate > max)
113                         max = rate;
114         }
115
116         return max;
117 }
118
119 /**
120  * sdhci_s3c_consider_clock - consider one the bus clocks for current setting
121  * @ourhost: Our SDHCI instance.
122  * @src: The source clock index.
123  * @wanted: The clock frequency wanted.
124  */
125 static unsigned int sdhci_s3c_consider_clock(struct sdhci_s3c *ourhost,
126                                              unsigned int src,
127                                              unsigned int wanted)
128 {
129         unsigned long rate;
130         struct clk *clksrc = ourhost->clk_bus[src];
131         int div;
132
133         if (!clksrc)
134                 return UINT_MAX;
135
136         if(ourhost->pdata->clk_type == S3C_SDHCI_CLK_DIV_EXTERNAL) {
137                 rate = clk_round_rate(clksrc,wanted);
138                 return (wanted - rate);
139         }
140
141         rate = clk_get_rate(clksrc);
142
143         for (div = 1; div < 256; div *= 2) {
144                 if ((rate / div) <= wanted)
145                         break;
146         }
147
148         dev_dbg(&ourhost->pdev->dev, "clk %d: rate %ld, want %d, got %ld\n",
149                 src, rate, wanted, rate / div);
150
151         return (wanted - (rate / div));
152 }
153
154 /**
155  * sdhci_s3c_set_clock - callback on clock change
156  * @host: The SDHCI host being changed
157  * @clock: The clock rate being requested.
158  *
159  * When the card's clock is going to be changed, look at the new frequency
160  * and find the best clock source to go with it.
161 */
162 static void sdhci_s3c_set_clock(struct sdhci_host *host, unsigned int clock)
163 {
164         struct sdhci_s3c *ourhost = to_s3c(host);
165         unsigned int best = UINT_MAX;
166         unsigned int delta;
167         int best_src = 0;
168         int src;
169         u32 ctrl;
170
171         /* don't bother if the clock is going off. */
172         if (clock == 0)
173                 return;
174
175         for (src = 0; src < MAX_BUS_CLK; src++) {
176                 delta = sdhci_s3c_consider_clock(ourhost, src, clock);
177                 if (delta < best) {
178                         best = delta;
179                         best_src = src;
180                 }
181         }
182
183         dev_dbg(&ourhost->pdev->dev,
184                 "selected source %d, clock %d, delta %d\n",
185                  best_src, clock, best);
186
187         /* select the new clock source */
188
189         if (ourhost->cur_clk != best_src) {
190                 struct clk *clk = ourhost->clk_bus[best_src];
191
192                 /* turn clock off to card before changing clock source */
193                 writew(0, host->ioaddr + SDHCI_CLOCK_CONTROL);
194
195                 ourhost->cur_clk = best_src;
196                 host->max_clk = clk_get_rate(clk);
197
198                 ctrl = readl(host->ioaddr + S3C_SDHCI_CONTROL2);
199                 ctrl &= ~S3C_SDHCI_CTRL2_SELBASECLK_MASK;
200                 ctrl |= best_src << S3C_SDHCI_CTRL2_SELBASECLK_SHIFT;
201                 writel(ctrl, host->ioaddr + S3C_SDHCI_CONTROL2);
202         }
203
204         /* reconfigure the hardware for new clock rate */
205
206         {
207                 struct mmc_ios ios;
208
209                 ios.clock = clock;
210
211                 if (ourhost->pdata->cfg_card)
212                         (ourhost->pdata->cfg_card)(ourhost->pdev, host->ioaddr,
213                                                    &ios, NULL);
214         }
215 }
216
217 /**
218  * sdhci_s3c_get_min_clock - callback to get minimal supported clock value
219  * @host: The SDHCI host being queried
220  *
221  * To init mmc host properly a minimal clock value is needed. For high system
222  * bus clock's values the standard formula gives values out of allowed range.
223  * The clock still can be set to lower values, if clock source other then
224  * system bus is selected.
225 */
226 static unsigned int sdhci_s3c_get_min_clock(struct sdhci_host *host)
227 {
228         struct sdhci_s3c *ourhost = to_s3c(host);
229         unsigned int delta, min = UINT_MAX;
230         int src;
231
232         for (src = 0; src < MAX_BUS_CLK; src++) {
233                 delta = sdhci_s3c_consider_clock(ourhost, src, 0);
234                 if (delta == UINT_MAX)
235                         continue;
236                 /* delta is a negative value in this case */
237                 if (-delta < min)
238                         min = -delta;
239         }
240         return min;
241 }
242
243 static unsigned int sdhci_cmu_get_max_clock(struct sdhci_host *host)
244 {
245         struct sdhci_s3c *ourhost = to_s3c(host);
246
247         return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], UINT_MAX);
248 }
249
250 static unsigned int sdhci_cmu_get_min_clock(struct sdhci_host *host)
251 {
252         struct sdhci_s3c *ourhost = to_s3c(host);
253
254         return clk_round_rate(ourhost->clk_bus[ourhost->cur_clk], 400000);
255 }
256
257 static void sdhci_cmu_set_clock(struct sdhci_host *host, unsigned int clock)
258 {
259         struct sdhci_s3c *ourhost = to_s3c(host);
260
261         if (clock == 0)
262                 return;
263
264         sdhci_s3c_set_clock(host, clock);
265
266         clk_set_rate(ourhost->clk_bus[ourhost->cur_clk], clock);
267
268         host->clock = clock;
269 }
270
271 static int sdhci_s3c_enable_clk(struct sdhci_host *host)
272 {
273         struct sdhci_s3c *sc = to_s3c(host);
274
275         if (sc->flag != 1) {
276                 clk_disable(sc->clk_io);
277                 clk_disable(sc->clk_bus[sc->cur_clk]);
278         }
279
280         if (sc->host->clk_cnt == 0) {
281                 clk_enable(sc->clk_io);
282                 clk_enable(sc->clk_bus[sc->cur_clk]);
283                 sc->host->clk_cnt++;
284                 sc->flag = 1;
285         }
286
287         return 0;
288 }
289
290 static int sdhci_s3c_disable_clk(struct sdhci_host *host, int lazy)
291 {
292         struct sdhci_s3c *sc = to_s3c(host);
293
294         if (sc->host->clk_cnt) {
295                 clk_disable(sc->clk_io);
296                 clk_disable(sc->clk_bus[sc->cur_clk]);
297                 if(sc->host->clk_cnt)
298                         sc->host->clk_cnt--;
299         }
300
301         return 0;
302 }
303
304 static struct sdhci_ops sdhci_s3c_ops = {
305         .get_max_clock          = sdhci_s3c_get_max_clk,
306         .set_clock              = sdhci_s3c_set_clock,
307         .get_min_clock          = sdhci_s3c_get_min_clock,
308         .enable                 = sdhci_s3c_enable_clk,
309         .disable                = sdhci_s3c_disable_clk,
310 };
311
312 static void sdhci_s3c_notify_change(struct platform_device *dev, int state)
313 {
314         struct sdhci_host *host = platform_get_drvdata(dev);
315         unsigned long flags;
316
317         if (host) {
318                 struct  sdhci_s3c *sc = to_s3c(host);
319                 /*
320                  * FIXME : Why need clk_get & clk_put for clk_io
321                  */
322                 sc->clk_io = clk_get(&dev->dev, "hsmmc");
323
324
325                 if (state && sc->cd_status == 0) {
326                         dev_dbg(&dev->dev, "card inserted.\n");
327
328                         spin_lock_irqsave(&host->lock, flags);
329                         host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
330                         sc->cd_status = 1;
331                         spin_unlock_irqrestore(&host->lock, flags);
332
333                         if (host->vmmc && !regulator_is_enabled(host->vmmc))
334                                 regulator_enable(host->vmmc);
335                 } else if (!state && sc->cd_status == 1) {
336                         dev_dbg(&dev->dev, "card removed.\n");
337
338                         spin_lock_irqsave(&host->lock, flags);
339                         host->quirks &= ~SDHCI_QUIRK_BROKEN_CARD_DETECTION;
340                         sc->cd_status = 0;
341                         spin_unlock_irqrestore(&host->lock, flags);
342
343                         if (host->vmmc && regulator_is_enabled(host->vmmc))
344                                 regulator_disable(host->vmmc);
345                 }
346
347                 spin_lock_irqsave(&host->lock, flags);
348                 tasklet_schedule(&host->card_tasklet);
349                 spin_unlock_irqrestore(&host->lock, flags);
350
351                 clk_put(sc->clk_io);
352
353         }
354 }
355
356 static irqreturn_t sdhci_s3c_gpio_card_detect_thread(int irq, void *dev_id)
357 {
358         struct sdhci_s3c *sc = dev_id;
359         int status = gpio_get_value(sc->ext_cd_gpio);
360         if (sc->pdata->ext_cd_gpio_invert)
361                 status = !status;
362         sdhci_s3c_notify_change(sc->pdev, status);
363         return IRQ_HANDLED;
364 }
365
366 static void sdhci_s3c_setup_card_detect_gpio(struct sdhci_s3c *sc)
367 {
368         struct s3c_sdhci_platdata *pdata = sc->pdata;
369         struct device *dev = &sc->pdev->dev;
370
371         if (gpio_request(pdata->ext_cd_gpio, "SDHCI EXT CD") == 0) {
372                 sc->ext_cd_gpio = pdata->ext_cd_gpio;
373                 sc->ext_cd_irq = gpio_to_irq(pdata->ext_cd_gpio);
374                 if (sc->ext_cd_irq &&
375                     request_threaded_irq(sc->ext_cd_irq, NULL,
376                                          sdhci_s3c_gpio_card_detect_thread,
377                                          IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
378                                          dev_name(dev), sc) == 0) {
379                         int status = gpio_get_value(sc->ext_cd_gpio);
380                         if (pdata->ext_cd_gpio_invert)
381                                 status = !status;
382                         sdhci_s3c_notify_change(sc->pdev, status);
383                         device_init_wakeup(dev, pdata->wakeup);
384                 } else {
385                         dev_warn(dev, "cannot request irq for card detect\n");
386                         sc->ext_cd_irq = 0;
387                 }
388         } else {
389                 dev_err(dev, "cannot request gpio for card detect\n");
390         }
391 }
392
393 static ssize_t card_status(struct device *dev, struct device_attribute *attr,
394                            char *buf)
395 {
396         struct platform_device *pdev = to_platform_device(dev);
397         struct s3c_sdhci_platdata *pdata = (struct s3c_sdhci_platdata *)
398                         pdev->dev.platform_data;
399         int status = 0;
400
401         if(!pdata->ext_cd_gpio)
402                 return sprintf(buf,"-1");
403         status = gpio_get_value(pdata->ext_cd_gpio);
404
405         if (pdata->ext_cd_gpio_invert)
406                 status = !status;
407
408         return sprintf(buf,"%d",!!status);
409 }
410
411 static DEVICE_ATTR(mmc_sd_detect, S_IRUGO, card_status, NULL);
412
413 static int __devinit sdhci_s3c_probe(struct platform_device *pdev)
414 {
415         struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data;
416         struct device *dev = &pdev->dev;
417         struct sdhci_host *host;
418         struct sdhci_s3c *sc;
419         struct resource *res;
420         int ret, irq, ptr, clks, rc;
421
422         if (!pdata) {
423                 dev_err(dev, "no device data specified\n");
424                 return -ENOENT;
425         }
426
427         irq = platform_get_irq(pdev, 0);
428         if (irq < 0) {
429                 dev_err(dev, "no irq specified\n");
430                 return irq;
431         }
432
433         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
434         if (!res) {
435                 dev_err(dev, "no memory specified\n");
436                 return -ENOENT;
437         }
438
439         host = sdhci_alloc_host(dev, sizeof(struct sdhci_s3c));
440         if (IS_ERR(host)) {
441                 dev_err(dev, "sdhci_alloc_host() failed\n");
442                 return PTR_ERR(host);
443         }
444
445         sc = sdhci_priv(host);
446
447         sc->host = host;
448         sc->pdev = pdev;
449         sc->pdata = pdata;
450         sc->ext_cd_gpio = -1; /* invalid gpio number */
451
452         platform_set_drvdata(pdev, host);
453
454         sc->clk_io = clk_get(dev, "hsmmc");
455         if (IS_ERR(sc->clk_io)) {
456                 dev_err(dev, "failed to get io clock\n");
457                 ret = PTR_ERR(sc->clk_io);
458                 goto err_io_clk;
459         }
460
461         /* enable the local io clock and keep it running for the moment. */
462         clk_enable(sc->clk_io);
463         sc->host->clk_cnt = 1;  /* initialize clk_cnt for clk_enable*/
464
465         for (clks = 0, ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
466                 struct clk *clk;
467                 char *name = pdata->clocks[ptr];
468
469                 if (name == NULL)
470                         continue;
471
472                 clk = clk_get(dev, name);
473                 if (IS_ERR(clk)) {
474                         dev_err(dev, "failed to get clock %s\n", name);
475                         continue;
476                 }
477
478                 clks++;
479                 sc->clk_bus[ptr] = clk;
480                 sc->cur_clk = ptr;
481                 clk_enable(clk);
482
483                 dev_info(dev, "clock source %d: %s (%ld Hz)\n",
484                          ptr, name, clk_get_rate(clk));
485         }
486
487         if (clks == 0) {
488                 dev_err(dev, "failed to find any bus clocks\n");
489                 ret = -ENOENT;
490                 goto err_no_busclks;
491         }
492
493         sc->ioarea = request_mem_region(res->start, resource_size(res),
494                                         mmc_hostname(host->mmc));
495         if (!sc->ioarea) {
496                 dev_err(dev, "failed to reserve register area\n");
497                 ret = -ENXIO;
498                 goto err_req_regs;
499         }
500
501         host->ioaddr = ioremap_nocache(res->start, resource_size(res));
502         if (!host->ioaddr) {
503                 dev_err(dev, "failed to map registers\n");
504                 ret = -ENXIO;
505                 goto err_req_regs;
506         }
507
508         /* Ensure we have minimal gpio selected CMD/CLK/Detect */
509         if (pdata->cfg_gpio)
510                 pdata->cfg_gpio(pdev, pdata->max_width);
511
512         host->hw_name = "samsung-hsmmc";
513         host->ops = &sdhci_s3c_ops;
514         host->quirks = 0;
515         host->irq = irq;
516
517         /* Setup quirks for the controller */
518         host->quirks |= SDHCI_QUIRK_NO_ENDATTR_IN_NOPDESC;
519         host->quirks |= SDHCI_QUIRK_NO_HISPD_BIT;
520
521         /* This host treats ADMA descriptors with leght 0000h incorrecyly */
522         host->quirks |= SDHCI_QUIRK_BROKEN_ADMA_ZEROLEN_DESC;
523
524 #ifndef CONFIG_MMC_SDHCI_S3C_DMA
525
526         /* we currently see overruns on errors, so disable the SDMA
527          * support as well. */
528         host->quirks |= SDHCI_QUIRK_BROKEN_DMA;
529
530 #endif /* CONFIG_MMC_SDHCI_S3C_DMA */
531
532         /* It seems we do not get an DATA transfer complete on non-busy
533          * transfers, not sure if this is a problem with this specific
534          * SDHCI block, or a missing configuration that needs to be set. */
535         host->quirks |= SDHCI_QUIRK_NO_BUSY_IRQ;
536
537         /* This host supports the Auto CMD12 */
538         /* Auto CMD12 can be harmful with a defected MMC card which has bad
539          * sectors. May system seems be lockup.
540            host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12; */
541
542         if (pdata->cd_type == S3C_SDHCI_CD_NONE ||
543             pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
544                 host->quirks |= SDHCI_QUIRK_BROKEN_CARD_DETECTION;
545
546         if (pdata->cd_type == S3C_SDHCI_CD_PERMANENT)
547                 host->mmc->caps = MMC_CAP_NONREMOVABLE;
548
549         host->quirks |= (SDHCI_QUIRK_32BIT_DMA_ADDR |
550                          SDHCI_QUIRK_32BIT_DMA_SIZE);
551
552         /* HSMMC on Samsung SoCs uses SDCLK as timeout clock */
553         host->quirks |= SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK;
554
555         if (pdata->clk_type == S3C_SDHCI_CLK_DIV_EXTERNAL) {
556                 sdhci_s3c_ops.set_clock = sdhci_cmu_set_clock;
557                 sdhci_s3c_ops.get_min_clock = sdhci_cmu_get_min_clock;
558                 sdhci_s3c_ops.get_max_clock = sdhci_cmu_get_max_clock;
559         }
560
561         if(pdata->host_caps)
562                 host->mmc->caps |= pdata->host_caps;
563
564         if (pdata->pm_caps)
565                 host->mmc->pm_flags = pdata->pm_caps;
566
567         ret = sdhci_add_host(host);
568         if (ret) {
569                 dev_err(dev, "sdhci_add_host() failed\n");
570                 goto err_add_host;
571         }
572
573         sc->cd_status = 1;
574         if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL ||
575             pdata->cd_type == S3C_SDHCI_CD_GPIO)
576                 sdhci_s3c_notify_change(pdev, 0);
577
578         /* The following two methods of card detection might call
579            sdhci_s3c_notify_change() immediately, so they can be called
580            only after sdhci_add_host(). Setup errors are ignored. */
581         if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_init)
582                 pdata->ext_cd_init(&sdhci_s3c_notify_change);
583
584         if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
585             gpio_is_valid(pdata->ext_cd_gpio)) {
586                 sdhci_s3c_setup_card_detect_gpio(sc);
587                 rc = device_create_file(&(pdev->dev), &dev_attr_mmc_sd_detect);
588                 if (rc)
589                         dev_err(dev, "Can't device_create_file\n");
590                 else
591                         dev_info(dev, "Add a card detection sysfs file\n");
592         }
593
594         return 0;
595
596  err_add_host:
597         release_resource(sc->ioarea);
598         kfree(sc->ioarea);
599
600  err_req_regs:
601         for (ptr = 0; ptr < MAX_BUS_CLK; ptr++) {
602                 if (sc->clk_bus[ptr]) {
603                         clk_disable(sc->clk_bus[ptr]);
604                         clk_put(sc->clk_bus[ptr]);
605                 }
606         }
607
608  err_no_busclks:
609         clk_disable(sc->clk_io);
610         clk_put(sc->clk_io);
611
612  err_io_clk:
613         sdhci_free_host(host);
614
615         return ret;
616 }
617
618 static int __devexit sdhci_s3c_remove(struct platform_device *pdev)
619 {
620         struct s3c_sdhci_platdata *pdata = pdev->dev.platform_data;
621         struct sdhci_host *host =  platform_get_drvdata(pdev);
622         struct sdhci_s3c *sc = sdhci_priv(host);
623         int ptr;
624
625         if (pdata->cd_type == S3C_SDHCI_CD_EXTERNAL && pdata->ext_cd_cleanup)
626                 pdata->ext_cd_cleanup(&sdhci_s3c_notify_change);
627
628         if (sc->ext_cd_irq)
629                 free_irq(sc->ext_cd_irq, sc);
630
631         if (gpio_is_valid(sc->ext_cd_gpio))
632                 gpio_free(sc->ext_cd_gpio);
633
634         if (pdata->cd_type == S3C_SDHCI_CD_GPIO &&
635             gpio_is_valid(pdata->ext_cd_gpio))
636                 device_remove_file(&(pdev->dev), &dev_attr_mmc_sd_detect);
637
638         sdhci_remove_host(host, 1);
639
640         for (ptr = 0; ptr < 3; ptr++) {
641                 if (sc->clk_bus[ptr]) {
642                         clk_disable(sc->clk_bus[ptr]);
643                         clk_put(sc->clk_bus[ptr]);
644                 }
645         }
646         clk_disable(sc->clk_io);
647         clk_put(sc->clk_io);
648
649         iounmap(host->ioaddr);
650         release_resource(sc->ioarea);
651         kfree(sc->ioarea);
652
653         sdhci_free_host(host);
654         platform_set_drvdata(pdev, NULL);
655
656         return 0;
657 }
658
659 #ifdef CONFIG_PM
660
661 static int sdhci_s3c_suspend(struct device *dev)
662 {
663         struct sdhci_host *host = dev_get_drvdata(dev);
664         struct sdhci_s3c *sc = sdhci_priv(host);
665         int ret = 0;
666
667         ret = sdhci_suspend_host(host, PMSG_SUSPEND);
668
669         if (!ret && host->clk_cnt)
670                 sdhci_s3c_disable_clk(host, 0);
671
672         if (!ret && device_may_wakeup(dev))
673                 if (sc->ext_cd_irq)
674                         enable_irq_wake(sc->ext_cd_irq);
675
676         return ret;
677 }
678
679 static int sdhci_s3c_resume(struct device *dev)
680 {
681         struct sdhci_host *host = dev_get_drvdata(dev);
682         struct sdhci_s3c *sc = sdhci_priv(host);
683
684         if (device_may_wakeup(dev))
685                 if (sc->ext_cd_irq)
686                         disable_irq_wake(sc->ext_cd_irq);
687
688         if (!host->clk_cnt)
689                 sdhci_s3c_enable_clk(host);
690
691         return sdhci_resume_host(host);
692 }
693
694 static const struct dev_pm_ops sdhci_s3c_pmops = {
695         .suspend        = sdhci_s3c_suspend,
696         .resume         = sdhci_s3c_resume,
697 };
698
699 #define SDHCI_S3C_PMOPS (&sdhci_s3c_pmops)
700
701 #else
702 #define SDHCI_S3C_PMOPS NULL
703 #endif
704
705 static struct platform_driver sdhci_s3c_driver = {
706         .probe          = sdhci_s3c_probe,
707         .remove         = __devexit_p(sdhci_s3c_remove),
708         .driver         = {
709                 .owner  = THIS_MODULE,
710                 .name   = "s3c-sdhci",
711                 .pm     = SDHCI_S3C_PMOPS,
712         },
713 };
714
715 static int __init sdhci_s3c_init(void)
716 {
717         return platform_driver_register(&sdhci_s3c_driver);
718 }
719
720 static void __exit sdhci_s3c_exit(void)
721 {
722         platform_driver_unregister(&sdhci_s3c_driver);
723 }
724
725 #ifdef CONFIG_FAST_RESUME
726 beforeresume_initcall(sdhci_s3c_init);
727 #else
728 module_init(sdhci_s3c_init);
729 #endif
730 module_exit(sdhci_s3c_exit);
731
732 MODULE_DESCRIPTION("Samsung SDHCI (HSMMC) glue");
733 MODULE_AUTHOR("Ben Dooks, <ben@simtec.co.uk>");
734 MODULE_LICENSE("GPL v2");
735 MODULE_ALIAS("platform:s3c-sdhci");