2 * Copyright (C) 2015, Samsung Electronics Co., Ltd.
4 * Author: Marek Szyprowski <m.szyprowski@samsung.com>
6 * License terms: GNU General Public License (GPL) version 2
8 * Simple eMMC hardware reset provider
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/init.h>
13 #include <linux/platform_device.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/device.h>
17 #include <linux/err.h>
18 #include <linux/gpio/consumer.h>
19 #include <linux/reboot.h>
21 #include <linux/mmc/host.h>
25 struct mmc_pwrseq_emmc
{
26 struct mmc_pwrseq pwrseq
;
27 struct notifier_block reset_nb
;
28 struct gpio_desc
*reset_gpio
;
31 #define to_pwrseq_emmc(p) container_of(p, struct mmc_pwrseq_emmc, pwrseq)
33 static void __mmc_pwrseq_emmc_reset(struct mmc_pwrseq_emmc
*pwrseq
)
35 gpiod_set_value(pwrseq
->reset_gpio
, 1);
37 gpiod_set_value(pwrseq
->reset_gpio
, 0);
41 static void mmc_pwrseq_emmc_reset(struct mmc_host
*host
)
43 struct mmc_pwrseq_emmc
*pwrseq
= to_pwrseq_emmc(host
->pwrseq
);
45 __mmc_pwrseq_emmc_reset(pwrseq
);
48 static int mmc_pwrseq_emmc_reset_nb(struct notifier_block
*this,
49 unsigned long mode
, void *cmd
)
51 struct mmc_pwrseq_emmc
*pwrseq
= container_of(this,
52 struct mmc_pwrseq_emmc
, reset_nb
);
54 __mmc_pwrseq_emmc_reset(pwrseq
);
58 static const struct mmc_pwrseq_ops mmc_pwrseq_emmc_ops
= {
59 .post_power_on
= mmc_pwrseq_emmc_reset
,
62 static int mmc_pwrseq_emmc_probe(struct platform_device
*pdev
)
64 struct mmc_pwrseq_emmc
*pwrseq
;
65 struct device
*dev
= &pdev
->dev
;
67 pwrseq
= devm_kzalloc(dev
, sizeof(*pwrseq
), GFP_KERNEL
);
71 pwrseq
->reset_gpio
= devm_gpiod_get(dev
, "reset", GPIOD_OUT_LOW
);
72 if (IS_ERR(pwrseq
->reset_gpio
))
73 return PTR_ERR(pwrseq
->reset_gpio
);
76 * register reset handler to ensure emmc reset also from
77 * emergency_reboot(), priority 255 is the highest priority
78 * so it will be executed before any system reboot handler.
80 pwrseq
->reset_nb
.notifier_call
= mmc_pwrseq_emmc_reset_nb
;
81 pwrseq
->reset_nb
.priority
= 255;
82 register_restart_handler(&pwrseq
->reset_nb
);
84 pwrseq
->pwrseq
.ops
= &mmc_pwrseq_emmc_ops
;
85 pwrseq
->pwrseq
.dev
= dev
;
86 pwrseq
->pwrseq
.owner
= THIS_MODULE
;
87 platform_set_drvdata(pdev
, pwrseq
);
89 return mmc_pwrseq_register(&pwrseq
->pwrseq
);
92 static int mmc_pwrseq_emmc_remove(struct platform_device
*pdev
)
94 struct mmc_pwrseq_emmc
*pwrseq
= platform_get_drvdata(pdev
);
96 unregister_restart_handler(&pwrseq
->reset_nb
);
97 mmc_pwrseq_unregister(&pwrseq
->pwrseq
);
102 static const struct of_device_id mmc_pwrseq_emmc_of_match
[] = {
103 { .compatible
= "mmc-pwrseq-emmc",},
107 MODULE_DEVICE_TABLE(of
, mmc_pwrseq_emmc_of_match
);
109 static struct platform_driver mmc_pwrseq_emmc_driver
= {
110 .probe
= mmc_pwrseq_emmc_probe
,
111 .remove
= mmc_pwrseq_emmc_remove
,
113 .name
= "pwrseq_emmc",
114 .of_match_table
= mmc_pwrseq_emmc_of_match
,
118 module_platform_driver(mmc_pwrseq_emmc_driver
);
119 MODULE_LICENSE("GPL v2");