xref: /openbmc/linux/drivers/mmc/core/sdio_bus.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e29a7d73SPierre Ossman /*
3e29a7d73SPierre Ossman  *  linux/drivers/mmc/core/sdio_bus.c
4e29a7d73SPierre Ossman  *
5e29a7d73SPierre Ossman  *  Copyright 2007 Pierre Ossman
6e29a7d73SPierre Ossman  *
7e29a7d73SPierre Ossman  * SDIO function driver model
8e29a7d73SPierre Ossman  */
9e29a7d73SPierre Ossman 
10e29a7d73SPierre Ossman #include <linux/device.h>
11e29a7d73SPierre Ossman #include <linux/err.h>
123ef77af1SPaul Gortmaker #include <linux/export.h>
135a0e3ad6STejun Heo #include <linux/slab.h>
1480fd933cSOhad Ben-Cohen #include <linux/pm_runtime.h>
15f48c767cSUlf Hansson #include <linux/pm_domain.h>
16eed222acSAaron Lu #include <linux/acpi.h>
17f5d8a5feSSergey Shtylyov #include <linux/sysfs.h>
18e29a7d73SPierre Ossman 
19e29a7d73SPierre Ossman #include <linux/mmc/card.h>
20ed919b01SOhad Ben-Cohen #include <linux/mmc/host.h>
21e29a7d73SPierre Ossman #include <linux/mmc/sdio_func.h>
2225185f3fSSascha Hauer #include <linux/of.h>
23e29a7d73SPierre Ossman 
2425185f3fSSascha Hauer #include "core.h"
254facdde1SUlf Hansson #include "card.h"
26b1538bcfSNicolas Pitre #include "sdio_cis.h"
27e29a7d73SPierre Ossman #include "sdio_bus.h"
28e29a7d73SPierre Ossman 
29433b7b12SUlf Hansson #define to_sdio_driver(d)	container_of(d, struct sdio_driver, drv)
30433b7b12SUlf Hansson 
31bcfe66e2SPierre Ossman /* show configuration fields */
32b91ec1dcSPali Rohár #define sdio_config_attr(field, format_string, args...)			\
33bcfe66e2SPierre Ossman static ssize_t								\
34bcfe66e2SPierre Ossman field##_show(struct device *dev, struct device_attribute *attr, char *buf)				\
35bcfe66e2SPierre Ossman {									\
36bcfe66e2SPierre Ossman 	struct sdio_func *func;						\
37bcfe66e2SPierre Ossman 									\
38bcfe66e2SPierre Ossman 	func = dev_to_sdio_func (dev);					\
39f5d8a5feSSergey Shtylyov 	return sysfs_emit(buf, format_string, args);			\
40f24fc57bSGreg Kroah-Hartman }									\
41f24fc57bSGreg Kroah-Hartman static DEVICE_ATTR_RO(field)
42bcfe66e2SPierre Ossman 
43b91ec1dcSPali Rohár sdio_config_attr(class, "0x%02x\n", func->class);
44b91ec1dcSPali Rohár sdio_config_attr(vendor, "0x%04x\n", func->vendor);
45b91ec1dcSPali Rohár sdio_config_attr(device, "0x%04x\n", func->device);
46b698f6abSPali Rohár sdio_config_attr(revision, "%u.%u\n", func->major_rev, func->minor_rev);
47b91ec1dcSPali Rohár sdio_config_attr(modalias, "sdio:c%02Xv%04Xd%04X\n", func->class, func->vendor, func->device);
48bcfe66e2SPierre Ossman 
49b698f6abSPali Rohár #define sdio_info_attr(num)									\
50b698f6abSPali Rohár static ssize_t info##num##_show(struct device *dev, struct device_attribute *attr, char *buf)	\
51b698f6abSPali Rohár {												\
52b698f6abSPali Rohár 	struct sdio_func *func = dev_to_sdio_func(dev);						\
53b698f6abSPali Rohár 												\
54b698f6abSPali Rohár 	if (num > func->num_info)								\
55b698f6abSPali Rohár 		return -ENODATA;								\
56b698f6abSPali Rohár 	if (!func->info[num - 1][0])								\
57b698f6abSPali Rohár 		return 0;									\
58f5d8a5feSSergey Shtylyov 	return sysfs_emit(buf, "%s\n", func->info[num - 1]);					\
59b698f6abSPali Rohár }												\
60b698f6abSPali Rohár static DEVICE_ATTR_RO(info##num)
61b698f6abSPali Rohár 
62b698f6abSPali Rohár sdio_info_attr(1);
63b698f6abSPali Rohár sdio_info_attr(2);
64b698f6abSPali Rohár sdio_info_attr(3);
65b698f6abSPali Rohár sdio_info_attr(4);
66b698f6abSPali Rohár 
67f24fc57bSGreg Kroah-Hartman static struct attribute *sdio_dev_attrs[] = {
68f24fc57bSGreg Kroah-Hartman 	&dev_attr_class.attr,
69f24fc57bSGreg Kroah-Hartman 	&dev_attr_vendor.attr,
70f24fc57bSGreg Kroah-Hartman 	&dev_attr_device.attr,
71b698f6abSPali Rohár 	&dev_attr_revision.attr,
72b698f6abSPali Rohár 	&dev_attr_info1.attr,
73b698f6abSPali Rohár 	&dev_attr_info2.attr,
74b698f6abSPali Rohár 	&dev_attr_info3.attr,
75b698f6abSPali Rohár 	&dev_attr_info4.attr,
76f24fc57bSGreg Kroah-Hartman 	&dev_attr_modalias.attr,
77f24fc57bSGreg Kroah-Hartman 	NULL,
78bcfe66e2SPierre Ossman };
79f24fc57bSGreg Kroah-Hartman ATTRIBUTE_GROUPS(sdio_dev);
80bcfe66e2SPierre Ossman 
sdio_match_one(struct sdio_func * func,const struct sdio_device_id * id)813b38bea0SPierre Ossman static const struct sdio_device_id *sdio_match_one(struct sdio_func *func,
823b38bea0SPierre Ossman 	const struct sdio_device_id *id)
833b38bea0SPierre Ossman {
843b38bea0SPierre Ossman 	if (id->class != (__u8)SDIO_ANY_ID && id->class != func->class)
853b38bea0SPierre Ossman 		return NULL;
863b38bea0SPierre Ossman 	if (id->vendor != (__u16)SDIO_ANY_ID && id->vendor != func->vendor)
873b38bea0SPierre Ossman 		return NULL;
883b38bea0SPierre Ossman 	if (id->device != (__u16)SDIO_ANY_ID && id->device != func->device)
893b38bea0SPierre Ossman 		return NULL;
903b38bea0SPierre Ossman 	return id;
913b38bea0SPierre Ossman }
923b38bea0SPierre Ossman 
sdio_match_device(struct sdio_func * func,struct sdio_driver * sdrv)933b38bea0SPierre Ossman static const struct sdio_device_id *sdio_match_device(struct sdio_func *func,
943b38bea0SPierre Ossman 	struct sdio_driver *sdrv)
953b38bea0SPierre Ossman {
963b38bea0SPierre Ossman 	const struct sdio_device_id *ids;
973b38bea0SPierre Ossman 
983b38bea0SPierre Ossman 	ids = sdrv->id_table;
993b38bea0SPierre Ossman 
1003b38bea0SPierre Ossman 	if (ids) {
1013b38bea0SPierre Ossman 		while (ids->class || ids->vendor || ids->device) {
1023b38bea0SPierre Ossman 			if (sdio_match_one(func, ids))
1033b38bea0SPierre Ossman 				return ids;
1043b38bea0SPierre Ossman 			ids++;
1053b38bea0SPierre Ossman 		}
1063b38bea0SPierre Ossman 	}
1073b38bea0SPierre Ossman 
1083b38bea0SPierre Ossman 	return NULL;
1093b38bea0SPierre Ossman }
1103b38bea0SPierre Ossman 
sdio_bus_match(struct device * dev,struct device_driver * drv)111e29a7d73SPierre Ossman static int sdio_bus_match(struct device *dev, struct device_driver *drv)
112e29a7d73SPierre Ossman {
1133b38bea0SPierre Ossman 	struct sdio_func *func = dev_to_sdio_func(dev);
1143b38bea0SPierre Ossman 	struct sdio_driver *sdrv = to_sdio_driver(drv);
1153b38bea0SPierre Ossman 
1163b38bea0SPierre Ossman 	if (sdio_match_device(func, sdrv))
117e29a7d73SPierre Ossman 		return 1;
1183b38bea0SPierre Ossman 
1193b38bea0SPierre Ossman 	return 0;
120e29a7d73SPierre Ossman }
121e29a7d73SPierre Ossman 
122e29a7d73SPierre Ossman static int
sdio_bus_uevent(const struct device * dev,struct kobj_uevent_env * env)1232a81ada3SGreg Kroah-Hartman sdio_bus_uevent(const struct device *dev, struct kobj_uevent_env *env)
124e29a7d73SPierre Ossman {
1252a81ada3SGreg Kroah-Hartman 	const struct sdio_func *func = dev_to_sdio_func(dev);
126b698f6abSPali Rohár 	unsigned int i;
127d59b66c7SPierre Ossman 
1287ac0326cSAl Viro 	if (add_uevent_var(env,
129d59b66c7SPierre Ossman 			"SDIO_CLASS=%02X", func->class))
130d59b66c7SPierre Ossman 		return -ENOMEM;
131d59b66c7SPierre Ossman 
1327ac0326cSAl Viro 	if (add_uevent_var(env,
133d59b66c7SPierre Ossman 			"SDIO_ID=%04X:%04X", func->vendor, func->device))
134d59b66c7SPierre Ossman 		return -ENOMEM;
135d59b66c7SPierre Ossman 
1367ac0326cSAl Viro 	if (add_uevent_var(env,
137b698f6abSPali Rohár 			"SDIO_REVISION=%u.%u", func->major_rev, func->minor_rev))
138b698f6abSPali Rohár 		return -ENOMEM;
139b698f6abSPali Rohár 
140b698f6abSPali Rohár 	for (i = 0; i < func->num_info; i++) {
141b698f6abSPali Rohár 		if (add_uevent_var(env, "SDIO_INFO%u=%s", i+1, func->info[i]))
142b698f6abSPali Rohár 			return -ENOMEM;
143b698f6abSPali Rohár 	}
144b698f6abSPali Rohár 
145b698f6abSPali Rohár 	if (add_uevent_var(env,
146d59b66c7SPierre Ossman 			"MODALIAS=sdio:c%02Xv%04Xd%04X",
147d59b66c7SPierre Ossman 			func->class, func->vendor, func->device))
148d59b66c7SPierre Ossman 		return -ENOMEM;
149d59b66c7SPierre Ossman 
150e29a7d73SPierre Ossman 	return 0;
151e29a7d73SPierre Ossman }
152e29a7d73SPierre Ossman 
sdio_bus_probe(struct device * dev)153e29a7d73SPierre Ossman static int sdio_bus_probe(struct device *dev)
154e29a7d73SPierre Ossman {
1553b38bea0SPierre Ossman 	struct sdio_driver *drv = to_sdio_driver(dev->driver);
1563b38bea0SPierre Ossman 	struct sdio_func *func = dev_to_sdio_func(dev);
1573b38bea0SPierre Ossman 	const struct sdio_device_id *id;
1589a08f82bSDavid Vrabel 	int ret;
1593b38bea0SPierre Ossman 
1603b38bea0SPierre Ossman 	id = sdio_match_device(func, drv);
1613b38bea0SPierre Ossman 	if (!id)
162e29a7d73SPierre Ossman 		return -ENODEV;
1633b38bea0SPierre Ossman 
1641ef48e3dSUlf Hansson 	ret = dev_pm_domain_attach(dev, false);
165d23fc022SUlf Hansson 	if (ret)
1661ef48e3dSUlf Hansson 		return ret;
1671ef48e3dSUlf Hansson 
1682ac55d5eSUlf Hansson 	atomic_inc(&func->card->sdio_funcs_probed);
1692ac55d5eSUlf Hansson 
17040bba0c1SOhad Ben-Cohen 	/* Unbound SDIO functions are always suspended.
17140bba0c1SOhad Ben-Cohen 	 * During probe, the function is set active and the usage count
17240bba0c1SOhad Ben-Cohen 	 * is incremented.  If the driver supports runtime PM,
17340bba0c1SOhad Ben-Cohen 	 * it should call pm_runtime_put_noidle() in its probe routine and
17440bba0c1SOhad Ben-Cohen 	 * pm_runtime_get_noresume() in its remove routine.
17540bba0c1SOhad Ben-Cohen 	 */
176ed919b01SOhad Ben-Cohen 	if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD) {
17740bba0c1SOhad Ben-Cohen 		ret = pm_runtime_get_sync(dev);
17840bba0c1SOhad Ben-Cohen 		if (ret < 0)
1793bffb800SLi Fei 			goto disable_runtimepm;
180ed919b01SOhad Ben-Cohen 	}
18140bba0c1SOhad Ben-Cohen 
1829a08f82bSDavid Vrabel 	/* Set the default block size so the driver is sure it's something
1839a08f82bSDavid Vrabel 	 * sensible. */
1849a08f82bSDavid Vrabel 	sdio_claim_host(func);
1852ac55d5eSUlf Hansson 	if (mmc_card_removed(func->card))
1862ac55d5eSUlf Hansson 		ret = -ENOMEDIUM;
1872ac55d5eSUlf Hansson 	else
1889a08f82bSDavid Vrabel 		ret = sdio_set_block_size(func, 0);
1899a08f82bSDavid Vrabel 	sdio_release_host(func);
1909a08f82bSDavid Vrabel 	if (ret)
19140bba0c1SOhad Ben-Cohen 		goto disable_runtimepm;
1929a08f82bSDavid Vrabel 
19340bba0c1SOhad Ben-Cohen 	ret = drv->probe(func, id);
19440bba0c1SOhad Ben-Cohen 	if (ret)
19540bba0c1SOhad Ben-Cohen 		goto disable_runtimepm;
19640bba0c1SOhad Ben-Cohen 
19740bba0c1SOhad Ben-Cohen 	return 0;
19840bba0c1SOhad Ben-Cohen 
19940bba0c1SOhad Ben-Cohen disable_runtimepm:
2002ac55d5eSUlf Hansson 	atomic_dec(&func->card->sdio_funcs_probed);
201ed919b01SOhad Ben-Cohen 	if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
20240bba0c1SOhad Ben-Cohen 		pm_runtime_put_noidle(dev);
2031ef48e3dSUlf Hansson 	dev_pm_domain_detach(dev, false);
20440bba0c1SOhad Ben-Cohen 	return ret;
205e29a7d73SPierre Ossman }
206e29a7d73SPierre Ossman 
sdio_bus_remove(struct device * dev)207fc7a6209SUwe Kleine-König static void sdio_bus_remove(struct device *dev)
208e29a7d73SPierre Ossman {
2093b38bea0SPierre Ossman 	struct sdio_driver *drv = to_sdio_driver(dev->driver);
2103b38bea0SPierre Ossman 	struct sdio_func *func = dev_to_sdio_func(dev);
21140bba0c1SOhad Ben-Cohen 
21240bba0c1SOhad Ben-Cohen 	/* Make sure card is powered before invoking ->remove() */
213ecc02441SOhad Ben-Cohen 	if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
214ecc02441SOhad Ben-Cohen 		pm_runtime_get_sync(dev);
2153b38bea0SPierre Ossman 
2163b38bea0SPierre Ossman 	drv->remove(func);
2172ac55d5eSUlf Hansson 	atomic_dec(&func->card->sdio_funcs_probed);
2183b38bea0SPierre Ossman 
219d1496c39SNicolas Pitre 	if (func->irq_handler) {
2206606110dSJoe Perches 		pr_warn("WARNING: driver %s did not remove its interrupt handler!\n",
2216606110dSJoe Perches 			drv->name);
222d1496c39SNicolas Pitre 		sdio_claim_host(func);
223d1496c39SNicolas Pitre 		sdio_release_irq(func);
224d1496c39SNicolas Pitre 		sdio_release_host(func);
225d1496c39SNicolas Pitre 	}
226d1496c39SNicolas Pitre 
22740bba0c1SOhad Ben-Cohen 	/* First, undo the increment made directly above */
228ed919b01SOhad Ben-Cohen 	if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
22940bba0c1SOhad Ben-Cohen 		pm_runtime_put_noidle(dev);
23040bba0c1SOhad Ben-Cohen 
23140bba0c1SOhad Ben-Cohen 	/* Then undo the runtime PM settings in sdio_bus_probe() */
232ed919b01SOhad Ben-Cohen 	if (func->card->host->caps & MMC_CAP_POWER_OFF_CARD)
233297c7f2fSOhad Ben-Cohen 		pm_runtime_put_sync(dev);
23440bba0c1SOhad Ben-Cohen 
2351ef48e3dSUlf Hansson 	dev_pm_domain_detach(dev, false);
236e29a7d73SPierre Ossman }
237e29a7d73SPierre Ossman 
23880fd933cSOhad Ben-Cohen static const struct dev_pm_ops sdio_bus_pm_ops = {
239573185ccSUlf Hansson 	SET_SYSTEM_SLEEP_PM_OPS(pm_generic_suspend, pm_generic_resume)
24080fd933cSOhad Ben-Cohen 	SET_RUNTIME_PM_OPS(
24180fd933cSOhad Ben-Cohen 		pm_generic_runtime_suspend,
24280fd933cSOhad Ben-Cohen 		pm_generic_runtime_resume,
24345f0a85cSRafael J. Wysocki 		NULL
24480fd933cSOhad Ben-Cohen 	)
24580fd933cSOhad Ben-Cohen };
24680fd933cSOhad Ben-Cohen 
247e29a7d73SPierre Ossman static struct bus_type sdio_bus_type = {
248e29a7d73SPierre Ossman 	.name		= "sdio",
249f24fc57bSGreg Kroah-Hartman 	.dev_groups	= sdio_dev_groups,
250e29a7d73SPierre Ossman 	.match		= sdio_bus_match,
251e29a7d73SPierre Ossman 	.uevent		= sdio_bus_uevent,
252e29a7d73SPierre Ossman 	.probe		= sdio_bus_probe,
253e29a7d73SPierre Ossman 	.remove		= sdio_bus_remove,
254d99903caSUlf Hansson 	.pm		= &sdio_bus_pm_ops,
255e29a7d73SPierre Ossman };
256e29a7d73SPierre Ossman 
sdio_register_bus(void)257e29a7d73SPierre Ossman int sdio_register_bus(void)
258e29a7d73SPierre Ossman {
259e29a7d73SPierre Ossman 	return bus_register(&sdio_bus_type);
260e29a7d73SPierre Ossman }
261e29a7d73SPierre Ossman 
sdio_unregister_bus(void)262e29a7d73SPierre Ossman void sdio_unregister_bus(void)
263e29a7d73SPierre Ossman {
264e29a7d73SPierre Ossman 	bus_unregister(&sdio_bus_type);
265e29a7d73SPierre Ossman }
266e29a7d73SPierre Ossman 
267f76c8515SPierre Ossman /**
268f76c8515SPierre Ossman  *	sdio_register_driver - register a function driver
269f76c8515SPierre Ossman  *	@drv: SDIO function driver
270f76c8515SPierre Ossman  */
sdio_register_driver(struct sdio_driver * drv)271f76c8515SPierre Ossman int sdio_register_driver(struct sdio_driver *drv)
272f76c8515SPierre Ossman {
273f76c8515SPierre Ossman 	drv->drv.name = drv->name;
274f76c8515SPierre Ossman 	drv->drv.bus = &sdio_bus_type;
275f76c8515SPierre Ossman 	return driver_register(&drv->drv);
276f76c8515SPierre Ossman }
277f76c8515SPierre Ossman EXPORT_SYMBOL_GPL(sdio_register_driver);
278f76c8515SPierre Ossman 
279f76c8515SPierre Ossman /**
280f76c8515SPierre Ossman  *	sdio_unregister_driver - unregister a function driver
281f76c8515SPierre Ossman  *	@drv: SDIO function driver
282f76c8515SPierre Ossman  */
sdio_unregister_driver(struct sdio_driver * drv)283f76c8515SPierre Ossman void sdio_unregister_driver(struct sdio_driver *drv)
284f76c8515SPierre Ossman {
285f76c8515SPierre Ossman 	drv->drv.bus = &sdio_bus_type;
286f76c8515SPierre Ossman 	driver_unregister(&drv->drv);
287f76c8515SPierre Ossman }
288f76c8515SPierre Ossman EXPORT_SYMBOL_GPL(sdio_unregister_driver);
289f76c8515SPierre Ossman 
sdio_release_func(struct device * dev)290e29a7d73SPierre Ossman static void sdio_release_func(struct device *dev)
291e29a7d73SPierre Ossman {
292e29a7d73SPierre Ossman 	struct sdio_func *func = dev_to_sdio_func(dev);
293e29a7d73SPierre Ossman 
2949972e6b4SMatthew Ma 	if (!(func->card->quirks & MMC_QUIRK_NONSTD_SDIO))
2951a632f8cSPierre Ossman 		sdio_free_func_cis(func);
296b1538bcfSNicolas Pitre 
297*605d9fb9SYang Yingliang 	/*
298*605d9fb9SYang Yingliang 	 * We have now removed the link to the tuples in the
299*605d9fb9SYang Yingliang 	 * card structure, so remove the reference.
300*605d9fb9SYang Yingliang 	 */
301*605d9fb9SYang Yingliang 	put_device(&func->card->dev);
302*605d9fb9SYang Yingliang 
303759bdc7aSPierre Ossman 	kfree(func->info);
3045ef1ecf0SHeiner Kallweit 	kfree(func->tmpbuf);
305e29a7d73SPierre Ossman 	kfree(func);
306e29a7d73SPierre Ossman }
307e29a7d73SPierre Ossman 
308e29a7d73SPierre Ossman /*
309e29a7d73SPierre Ossman  * Allocate and initialise a new SDIO function structure.
310e29a7d73SPierre Ossman  */
sdio_alloc_func(struct mmc_card * card)311e29a7d73SPierre Ossman struct sdio_func *sdio_alloc_func(struct mmc_card *card)
312e29a7d73SPierre Ossman {
313e29a7d73SPierre Ossman 	struct sdio_func *func;
314e29a7d73SPierre Ossman 
3159f2fcf99SMariusz Kozlowski 	func = kzalloc(sizeof(struct sdio_func), GFP_KERNEL);
316e29a7d73SPierre Ossman 	if (!func)
317e29a7d73SPierre Ossman 		return ERR_PTR(-ENOMEM);
318e29a7d73SPierre Ossman 
3195ef1ecf0SHeiner Kallweit 	/*
3205ef1ecf0SHeiner Kallweit 	 * allocate buffer separately to make sure it's properly aligned for
3215ef1ecf0SHeiner Kallweit 	 * DMA usage (incl. 64 bit DMA)
3225ef1ecf0SHeiner Kallweit 	 */
3235ef1ecf0SHeiner Kallweit 	func->tmpbuf = kmalloc(4, GFP_KERNEL);
3245ef1ecf0SHeiner Kallweit 	if (!func->tmpbuf) {
3255ef1ecf0SHeiner Kallweit 		kfree(func);
3265ef1ecf0SHeiner Kallweit 		return ERR_PTR(-ENOMEM);
3275ef1ecf0SHeiner Kallweit 	}
3285ef1ecf0SHeiner Kallweit 
329e29a7d73SPierre Ossman 	func->card = card;
330e29a7d73SPierre Ossman 
331e29a7d73SPierre Ossman 	device_initialize(&func->dev);
332e29a7d73SPierre Ossman 
333*605d9fb9SYang Yingliang 	/*
334*605d9fb9SYang Yingliang 	 * We may link to tuples in the card structure,
335*605d9fb9SYang Yingliang 	 * we need make sure we have a reference to it.
336*605d9fb9SYang Yingliang 	 */
337*605d9fb9SYang Yingliang 	get_device(&func->card->dev);
338*605d9fb9SYang Yingliang 
339e29a7d73SPierre Ossman 	func->dev.parent = &card->dev;
340e29a7d73SPierre Ossman 	func->dev.bus = &sdio_bus_type;
341e29a7d73SPierre Ossman 	func->dev.release = sdio_release_func;
342e29a7d73SPierre Ossman 
343e29a7d73SPierre Ossman 	return func;
344e29a7d73SPierre Ossman }
345e29a7d73SPierre Ossman 
346eed222acSAaron Lu #ifdef CONFIG_ACPI
sdio_acpi_set_handle(struct sdio_func * func)347eed222acSAaron Lu static void sdio_acpi_set_handle(struct sdio_func *func)
348eed222acSAaron Lu {
349eed222acSAaron Lu 	struct mmc_host *host = func->card->host;
35051d34606SDan Carpenter 	u64 addr = ((u64)host->slotno << 16) | func->num;
351eed222acSAaron Lu 
3529c5ad36dSRafael J. Wysocki 	acpi_preset_companion(&func->dev, ACPI_COMPANION(host->parent), addr);
353eed222acSAaron Lu }
354eed222acSAaron Lu #else
sdio_acpi_set_handle(struct sdio_func * func)355eed222acSAaron Lu static inline void sdio_acpi_set_handle(struct sdio_func *func) {}
356eed222acSAaron Lu #endif
357eed222acSAaron Lu 
sdio_set_of_node(struct sdio_func * func)35825185f3fSSascha Hauer static void sdio_set_of_node(struct sdio_func *func)
35925185f3fSSascha Hauer {
36025185f3fSSascha Hauer 	struct mmc_host *host = func->card->host;
36125185f3fSSascha Hauer 
36225185f3fSSascha Hauer 	func->dev.of_node = mmc_of_find_child_device(host, func->num);
36325185f3fSSascha Hauer }
36425185f3fSSascha Hauer 
365e29a7d73SPierre Ossman /*
366e29a7d73SPierre Ossman  * Register a new SDIO function with the driver model.
367e29a7d73SPierre Ossman  */
sdio_add_func(struct sdio_func * func)368e29a7d73SPierre Ossman int sdio_add_func(struct sdio_func *func)
369e29a7d73SPierre Ossman {
370e29a7d73SPierre Ossman 	int ret;
371e29a7d73SPierre Ossman 
372d1b26863SKay Sievers 	dev_set_name(&func->dev, "%s:%d", mmc_card_id(func->card), func->num);
373e29a7d73SPierre Ossman 
37425185f3fSSascha Hauer 	sdio_set_of_node(func);
375eed222acSAaron Lu 	sdio_acpi_set_handle(func);
376ec076cd2SFu, Zhonghui 	device_enable_async_suspend(&func->dev);
377e29a7d73SPierre Ossman 	ret = device_add(&func->dev);
3781ef48e3dSUlf Hansson 	if (ret == 0)
379e29a7d73SPierre Ossman 		sdio_func_set_present(func);
380e29a7d73SPierre Ossman 
381e29a7d73SPierre Ossman 	return ret;
382e29a7d73SPierre Ossman }
383e29a7d73SPierre Ossman 
384e29a7d73SPierre Ossman /*
385e29a7d73SPierre Ossman  * Unregister a SDIO function with the driver model, and
386e29a7d73SPierre Ossman  * (eventually) free it.
3873d10a1baSDaniel Drake  * This function can be called through error paths where sdio_add_func() was
3883d10a1baSDaniel Drake  * never executed (because a failure occurred at an earlier point).
389e29a7d73SPierre Ossman  */
sdio_remove_func(struct sdio_func * func)390e29a7d73SPierre Ossman void sdio_remove_func(struct sdio_func *func)
391e29a7d73SPierre Ossman {
392*605d9fb9SYang Yingliang 	if (sdio_func_present(func))
3933d10a1baSDaniel Drake 		device_del(&func->dev);
394*605d9fb9SYang Yingliang 
39525185f3fSSascha Hauer 	of_node_put(func->dev.of_node);
396e29a7d73SPierre Ossman 	put_device(&func->dev);
397e29a7d73SPierre Ossman }
398e29a7d73SPierre Ossman 
399