host.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. /*
  2. * linux/drivers/mmc/core/host.c
  3. *
  4. * Copyright (C) 2003 Russell King, All Rights Reserved.
  5. * Copyright (C) 2007-2008 Pierre Ossman
  6. * Copyright (C) 2010 Linus Walleij
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * MMC host class device management
  13. */
  14. #include <linux/device.h>
  15. #include <linux/err.h>
  16. #include <linux/idr.h>
  17. #include <linux/of.h>
  18. #include <linux/of_gpio.h>
  19. #include <linux/pagemap.h>
  20. #include <linux/export.h>
  21. #include <linux/leds.h>
  22. #include <linux/slab.h>
  23. #include <linux/suspend.h>
  24. #include <linux/mmc/host.h>
  25. #include <linux/mmc/card.h>
  26. #include <linux/mmc/slot-gpio.h>
  27. #include "core.h"
  28. #include "host.h"
  29. #include "slot-gpio.h"
  30. #include "pwrseq.h"
  31. #define cls_dev_to_mmc_host(d) container_of(d, struct mmc_host, class_dev)
  32. static DEFINE_IDR(mmc_host_idr);
  33. static DEFINE_SPINLOCK(mmc_host_lock);
  34. static void mmc_host_classdev_release(struct device *dev)
  35. {
  36. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  37. spin_lock(&mmc_host_lock);
  38. idr_remove(&mmc_host_idr, host->index);
  39. spin_unlock(&mmc_host_lock);
  40. kfree(host);
  41. }
  42. static struct class mmc_host_class = {
  43. .name = "mmc_host",
  44. .dev_release = mmc_host_classdev_release,
  45. };
  46. int mmc_register_host_class(void)
  47. {
  48. return class_register(&mmc_host_class);
  49. }
  50. void mmc_unregister_host_class(void)
  51. {
  52. class_unregister(&mmc_host_class);
  53. }
  54. #ifdef CONFIG_MMC_CLKGATE
  55. static ssize_t clkgate_delay_show(struct device *dev,
  56. struct device_attribute *attr, char *buf)
  57. {
  58. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  59. return snprintf(buf, PAGE_SIZE, "%lu\n", host->clkgate_delay);
  60. }
  61. static ssize_t clkgate_delay_store(struct device *dev,
  62. struct device_attribute *attr, const char *buf, size_t count)
  63. {
  64. struct mmc_host *host = cls_dev_to_mmc_host(dev);
  65. unsigned long flags, value;
  66. if (kstrtoul(buf, 0, &value))
  67. return -EINVAL;
  68. spin_lock_irqsave(&host->clk_lock, flags);
  69. host->clkgate_delay = value;
  70. spin_unlock_irqrestore(&host->clk_lock, flags);
  71. return count;
  72. }
  73. /*
  74. * Enabling clock gating will make the core call out to the host
  75. * once up and once down when it performs a request or card operation
  76. * intermingled in any fashion. The driver will see this through
  77. * set_ios() operations with ios.clock field set to 0 to gate (disable)
  78. * the block clock, and to the old frequency to enable it again.
  79. */
  80. static void mmc_host_clk_gate_delayed(struct mmc_host *host)
  81. {
  82. unsigned long tick_ns;
  83. unsigned long freq = host->ios.clock;
  84. unsigned long flags;
  85. if (!freq) {
  86. pr_debug("%s: frequency set to 0 in disable function, "
  87. "this means the clock is already disabled.\n",
  88. mmc_hostname(host));
  89. return;
  90. }
  91. /*
  92. * New requests may have appeared while we were scheduling,
  93. * then there is no reason to delay the check before
  94. * clk_disable().
  95. */
  96. spin_lock_irqsave(&host->clk_lock, flags);
  97. /*
  98. * Delay n bus cycles (at least 8 from MMC spec) before attempting
  99. * to disable the MCI block clock. The reference count may have
  100. * gone up again after this delay due to rescheduling!
  101. */
  102. if (!host->clk_requests) {
  103. spin_unlock_irqrestore(&host->clk_lock, flags);
  104. tick_ns = DIV_ROUND_UP(1000000000, freq);
  105. ndelay(host->clk_delay * tick_ns);
  106. } else {
  107. /* New users appeared while waiting for this work */
  108. spin_unlock_irqrestore(&host->clk_lock, flags);
  109. return;
  110. }
  111. mutex_lock(&host->clk_gate_mutex);
  112. spin_lock_irqsave(&host->clk_lock, flags);
  113. if (!host->clk_requests) {
  114. spin_unlock_irqrestore(&host->clk_lock, flags);
  115. /* This will set host->ios.clock to 0 */
  116. mmc_gate_clock(host);
  117. spin_lock_irqsave(&host->clk_lock, flags);
  118. pr_debug("%s: gated MCI clock\n", mmc_hostname(host));
  119. }
  120. spin_unlock_irqrestore(&host->clk_lock, flags);
  121. mutex_unlock(&host->clk_gate_mutex);
  122. }
  123. /*
  124. * Internal work. Work to disable the clock at some later point.
  125. */
  126. static void mmc_host_clk_gate_work(struct work_struct *work)
  127. {
  128. struct mmc_host *host = container_of(work, struct mmc_host,
  129. clk_gate_work.work);
  130. mmc_host_clk_gate_delayed(host);
  131. }
  132. /**
  133. * mmc_host_clk_hold - ungate hardware MCI clocks
  134. * @host: host to ungate.
  135. *
  136. * Makes sure the host ios.clock is restored to a non-zero value
  137. * past this call. Increase clock reference count and ungate clock
  138. * if we're the first user.
  139. */
  140. void mmc_host_clk_hold(struct mmc_host *host)
  141. {
  142. unsigned long flags;
  143. /* cancel any clock gating work scheduled by mmc_host_clk_release() */
  144. cancel_delayed_work_sync(&host->clk_gate_work);
  145. mutex_lock(&host->clk_gate_mutex);
  146. spin_lock_irqsave(&host->clk_lock, flags);
  147. if (host->clk_gated) {
  148. spin_unlock_irqrestore(&host->clk_lock, flags);
  149. mmc_ungate_clock(host);
  150. spin_lock_irqsave(&host->clk_lock, flags);
  151. pr_debug("%s: ungated MCI clock\n", mmc_hostname(host));
  152. }
  153. host->clk_requests++;
  154. spin_unlock_irqrestore(&host->clk_lock, flags);
  155. mutex_unlock(&host->clk_gate_mutex);
  156. }
  157. /**
  158. * mmc_host_may_gate_card - check if this card may be gated
  159. * @card: card to check.
  160. */
  161. static bool mmc_host_may_gate_card(struct mmc_card *card)
  162. {
  163. /* If there is no card we may gate it */
  164. if (!card)
  165. return true;
  166. /*
  167. * Don't gate SDIO cards! These need to be clocked at all times
  168. * since they may be independent systems generating interrupts
  169. * and other events. The clock requests counter from the core will
  170. * go down to zero since the core does not need it, but we will not
  171. * gate the clock, because there is somebody out there that may still
  172. * be using it.
  173. */
  174. return !(card->quirks & MMC_QUIRK_BROKEN_CLK_GATING);
  175. }
  176. /**
  177. * mmc_host_clk_release - gate off hardware MCI clocks
  178. * @host: host to gate.
  179. *
  180. * Calls the host driver with ios.clock set to zero as often as possible
  181. * in order to gate off hardware MCI clocks. Decrease clock reference
  182. * count and schedule disabling of clock.
  183. */
  184. void mmc_host_clk_release(struct mmc_host *host)
  185. {
  186. unsigned long flags;
  187. spin_lock_irqsave(&host->clk_lock, flags);
  188. host->clk_requests--;
  189. if (mmc_host_may_gate_card(host->card) &&
  190. !host->clk_requests)
  191. schedule_delayed_work(&host->clk_gate_work,
  192. msecs_to_jiffies(host->clkgate_delay));
  193. spin_unlock_irqrestore(&host->clk_lock, flags);
  194. }
  195. /**
  196. * mmc_host_clk_rate - get current clock frequency setting
  197. * @host: host to get the clock frequency for.
  198. *
  199. * Returns current clock frequency regardless of gating.
  200. */
  201. unsigned int mmc_host_clk_rate(struct mmc_host *host)
  202. {
  203. unsigned long freq;
  204. unsigned long flags;
  205. spin_lock_irqsave(&host->clk_lock, flags);
  206. if (host->clk_gated)
  207. freq = host->clk_old;
  208. else
  209. freq = host->ios.clock;
  210. spin_unlock_irqrestore(&host->clk_lock, flags);
  211. return freq;
  212. }
  213. /**
  214. * mmc_host_clk_init - set up clock gating code
  215. * @host: host with potential clock to control
  216. */
  217. static inline void mmc_host_clk_init(struct mmc_host *host)
  218. {
  219. host->clk_requests = 0;
  220. /* Hold MCI clock for 8 cycles by default */
  221. host->clk_delay = 8;
  222. /*
  223. * Default clock gating delay is 0ms to avoid wasting power.
  224. * This value can be tuned by writing into sysfs entry.
  225. */
  226. host->clkgate_delay = 0;
  227. host->clk_gated = false;
  228. INIT_DELAYED_WORK(&host->clk_gate_work, mmc_host_clk_gate_work);
  229. spin_lock_init(&host->clk_lock);
  230. mutex_init(&host->clk_gate_mutex);
  231. }
  232. /**
  233. * mmc_host_clk_exit - shut down clock gating code
  234. * @host: host with potential clock to control
  235. */
  236. static inline void mmc_host_clk_exit(struct mmc_host *host)
  237. {
  238. /*
  239. * Wait for any outstanding gate and then make sure we're
  240. * ungated before exiting.
  241. */
  242. if (cancel_delayed_work_sync(&host->clk_gate_work))
  243. mmc_host_clk_gate_delayed(host);
  244. if (host->clk_gated)
  245. mmc_host_clk_hold(host);
  246. /* There should be only one user now */
  247. WARN_ON(host->clk_requests > 1);
  248. }
  249. static inline void mmc_host_clk_sysfs_init(struct mmc_host *host)
  250. {
  251. host->clkgate_delay_attr.show = clkgate_delay_show;
  252. host->clkgate_delay_attr.store = clkgate_delay_store;
  253. sysfs_attr_init(&host->clkgate_delay_attr.attr);
  254. host->clkgate_delay_attr.attr.name = "clkgate_delay";
  255. host->clkgate_delay_attr.attr.mode = S_IRUGO | S_IWUSR;
  256. if (device_create_file(&host->class_dev, &host->clkgate_delay_attr))
  257. pr_err("%s: Failed to create clkgate_delay sysfs entry\n",
  258. mmc_hostname(host));
  259. }
  260. #else
  261. static inline void mmc_host_clk_init(struct mmc_host *host)
  262. {
  263. }
  264. static inline void mmc_host_clk_exit(struct mmc_host *host)
  265. {
  266. }
  267. static inline void mmc_host_clk_sysfs_init(struct mmc_host *host)
  268. {
  269. }
  270. #endif
  271. void mmc_retune_enable(struct mmc_host *host)
  272. {
  273. host->can_retune = 1;
  274. if (host->retune_period)
  275. mod_timer(&host->retune_timer,
  276. jiffies + host->retune_period * HZ);
  277. }
  278. void mmc_retune_disable(struct mmc_host *host)
  279. {
  280. host->can_retune = 0;
  281. del_timer_sync(&host->retune_timer);
  282. host->retune_now = 0;
  283. host->need_retune = 0;
  284. }
  285. void mmc_retune_timer_stop(struct mmc_host *host)
  286. {
  287. del_timer_sync(&host->retune_timer);
  288. }
  289. EXPORT_SYMBOL(mmc_retune_timer_stop);
  290. void mmc_retune_hold(struct mmc_host *host)
  291. {
  292. if (!host->hold_retune)
  293. host->retune_now = 1;
  294. host->hold_retune += 1;
  295. }
  296. void mmc_retune_release(struct mmc_host *host)
  297. {
  298. if (host->hold_retune)
  299. host->hold_retune -= 1;
  300. else
  301. WARN_ON(1);
  302. }
  303. int mmc_retune(struct mmc_host *host)
  304. {
  305. bool return_to_hs400 = false;
  306. int err;
  307. if (host->retune_now)
  308. host->retune_now = 0;
  309. else
  310. return 0;
  311. if (!host->need_retune || host->doing_retune || !host->card)
  312. return 0;
  313. host->need_retune = 0;
  314. host->doing_retune = 1;
  315. if (host->ios.timing == MMC_TIMING_MMC_HS400) {
  316. err = mmc_hs400_to_hs200(host->card);
  317. if (err)
  318. goto out;
  319. return_to_hs400 = true;
  320. if (host->ops->prepare_hs400_tuning)
  321. host->ops->prepare_hs400_tuning(host, &host->ios);
  322. }
  323. err = mmc_execute_tuning(host->card);
  324. if (err)
  325. goto out;
  326. if (return_to_hs400)
  327. err = mmc_hs200_to_hs400(host->card);
  328. out:
  329. host->doing_retune = 0;
  330. return err;
  331. }
  332. static void mmc_retune_timer(unsigned long data)
  333. {
  334. struct mmc_host *host = (struct mmc_host *)data;
  335. mmc_retune_needed(host);
  336. }
  337. /**
  338. * mmc_of_parse() - parse host's device-tree node
  339. * @host: host whose node should be parsed.
  340. *
  341. * To keep the rest of the MMC subsystem unaware of whether DT has been
  342. * used to to instantiate and configure this host instance or not, we
  343. * parse the properties and set respective generic mmc-host flags and
  344. * parameters.
  345. */
  346. int mmc_of_parse(struct mmc_host *host)
  347. {
  348. struct device_node *np;
  349. u32 bus_width;
  350. int ret;
  351. bool cd_cap_invert, cd_gpio_invert = false;
  352. bool ro_cap_invert, ro_gpio_invert = false;
  353. if (!host->parent || !host->parent->of_node)
  354. return 0;
  355. np = host->parent->of_node;
  356. /* "bus-width" is translated to MMC_CAP_*_BIT_DATA flags */
  357. if (of_property_read_u32(np, "bus-width", &bus_width) < 0) {
  358. dev_dbg(host->parent,
  359. "\"bus-width\" property is missing, assuming 1 bit.\n");
  360. bus_width = 1;
  361. }
  362. switch (bus_width) {
  363. case 8:
  364. host->caps |= MMC_CAP_8_BIT_DATA;
  365. /* Hosts capable of 8-bit transfers can also do 4 bits */
  366. case 4:
  367. host->caps |= MMC_CAP_4_BIT_DATA;
  368. break;
  369. case 1:
  370. break;
  371. default:
  372. dev_err(host->parent,
  373. "Invalid \"bus-width\" value %u!\n", bus_width);
  374. return -EINVAL;
  375. }
  376. /* f_max is obtained from the optional "max-frequency" property */
  377. of_property_read_u32(np, "max-frequency", &host->f_max);
  378. /*
  379. * Configure CD and WP pins. They are both by default active low to
  380. * match the SDHCI spec. If GPIOs are provided for CD and / or WP, the
  381. * mmc-gpio helpers are used to attach, configure and use them. If
  382. * polarity inversion is specified in DT, one of MMC_CAP2_CD_ACTIVE_HIGH
  383. * and MMC_CAP2_RO_ACTIVE_HIGH capability-2 flags is set. If the
  384. * "broken-cd" property is provided, the MMC_CAP_NEEDS_POLL capability
  385. * is set. If the "non-removable" property is found, the
  386. * MMC_CAP_NONREMOVABLE capability is set and no card-detection
  387. * configuration is performed.
  388. */
  389. /* Parse Card Detection */
  390. if (of_property_read_bool(np, "non-removable")) {
  391. host->caps |= MMC_CAP_NONREMOVABLE;
  392. } else {
  393. cd_cap_invert = of_property_read_bool(np, "cd-inverted");
  394. if (of_property_read_bool(np, "broken-cd"))
  395. host->caps |= MMC_CAP_NEEDS_POLL;
  396. ret = mmc_gpiod_request_cd(host, "cd", 0, true,
  397. 0, &cd_gpio_invert);
  398. if (!ret)
  399. dev_info(host->parent, "Got CD GPIO\n");
  400. else if (ret != -ENOENT && ret != -ENOSYS)
  401. return ret;
  402. /*
  403. * There are two ways to flag that the CD line is inverted:
  404. * through the cd-inverted flag and by the GPIO line itself
  405. * being inverted from the GPIO subsystem. This is a leftover
  406. * from the times when the GPIO subsystem did not make it
  407. * possible to flag a line as inverted.
  408. *
  409. * If the capability on the host AND the GPIO line are
  410. * both inverted, the end result is that the CD line is
  411. * not inverted.
  412. */
  413. if (cd_cap_invert ^ cd_gpio_invert)
  414. host->caps2 |= MMC_CAP2_CD_ACTIVE_HIGH;
  415. }
  416. /* Parse Write Protection */
  417. ro_cap_invert = of_property_read_bool(np, "wp-inverted");
  418. ret = mmc_gpiod_request_ro(host, "wp", 0, false, 0, &ro_gpio_invert);
  419. if (!ret)
  420. dev_info(host->parent, "Got WP GPIO\n");
  421. else if (ret != -ENOENT && ret != -ENOSYS)
  422. return ret;
  423. if (of_property_read_bool(np, "disable-wp"))
  424. host->caps2 |= MMC_CAP2_NO_WRITE_PROTECT;
  425. /* See the comment on CD inversion above */
  426. if (ro_cap_invert ^ ro_gpio_invert)
  427. host->caps2 |= MMC_CAP2_RO_ACTIVE_HIGH;
  428. if (of_property_read_bool(np, "cap-sd-highspeed"))
  429. host->caps |= MMC_CAP_SD_HIGHSPEED;
  430. if (of_property_read_bool(np, "cap-mmc-highspeed"))
  431. host->caps |= MMC_CAP_MMC_HIGHSPEED;
  432. if (of_property_read_bool(np, "sd-uhs-sdr12"))
  433. host->caps |= MMC_CAP_UHS_SDR12;
  434. if (of_property_read_bool(np, "sd-uhs-sdr25"))
  435. host->caps |= MMC_CAP_UHS_SDR25;
  436. if (of_property_read_bool(np, "sd-uhs-sdr50"))
  437. host->caps |= MMC_CAP_UHS_SDR50;
  438. if (of_property_read_bool(np, "sd-uhs-sdr104"))
  439. host->caps |= MMC_CAP_UHS_SDR104;
  440. if (of_property_read_bool(np, "sd-uhs-ddr50"))
  441. host->caps |= MMC_CAP_UHS_DDR50;
  442. if (of_property_read_bool(np, "cap-power-off-card"))
  443. host->caps |= MMC_CAP_POWER_OFF_CARD;
  444. if (of_property_read_bool(np, "cap-sdio-irq"))
  445. host->caps |= MMC_CAP_SDIO_IRQ;
  446. if (of_property_read_bool(np, "full-pwr-cycle"))
  447. host->caps2 |= MMC_CAP2_FULL_PWR_CYCLE;
  448. if (of_property_read_bool(np, "keep-power-in-suspend"))
  449. host->pm_caps |= MMC_PM_KEEP_POWER;
  450. if (of_property_read_bool(np, "enable-sdio-wakeup"))
  451. host->pm_caps |= MMC_PM_WAKE_SDIO_IRQ;
  452. if (of_property_read_bool(np, "mmc-ddr-1_8v"))
  453. host->caps |= MMC_CAP_1_8V_DDR;
  454. if (of_property_read_bool(np, "mmc-ddr-1_2v"))
  455. host->caps |= MMC_CAP_1_2V_DDR;
  456. if (of_property_read_bool(np, "mmc-hs200-1_8v"))
  457. host->caps2 |= MMC_CAP2_HS200_1_8V_SDR;
  458. if (of_property_read_bool(np, "mmc-hs200-1_2v"))
  459. host->caps2 |= MMC_CAP2_HS200_1_2V_SDR;
  460. if (of_property_read_bool(np, "mmc-hs400-1_8v"))
  461. host->caps2 |= MMC_CAP2_HS400_1_8V | MMC_CAP2_HS200_1_8V_SDR;
  462. if (of_property_read_bool(np, "mmc-hs400-1_2v"))
  463. host->caps2 |= MMC_CAP2_HS400_1_2V | MMC_CAP2_HS200_1_2V_SDR;
  464. host->dsr_req = !of_property_read_u32(np, "dsr", &host->dsr);
  465. if (host->dsr_req && (host->dsr & ~0xffff)) {
  466. dev_err(host->parent,
  467. "device tree specified broken value for DSR: 0x%x, ignoring\n",
  468. host->dsr);
  469. host->dsr_req = 0;
  470. }
  471. return mmc_pwrseq_alloc(host);
  472. }
  473. EXPORT_SYMBOL(mmc_of_parse);
  474. /**
  475. * mmc_alloc_host - initialise the per-host structure.
  476. * @extra: sizeof private data structure
  477. * @dev: pointer to host device model structure
  478. *
  479. * Initialise the per-host structure.
  480. */
  481. struct mmc_host *mmc_alloc_host(int extra, struct device *dev)
  482. {
  483. int err;
  484. struct mmc_host *host;
  485. host = kzalloc(sizeof(struct mmc_host) + extra, GFP_KERNEL);
  486. if (!host)
  487. return NULL;
  488. /* scanning will be enabled when we're ready */
  489. host->rescan_disable = 1;
  490. idr_preload(GFP_KERNEL);
  491. spin_lock(&mmc_host_lock);
  492. err = idr_alloc(&mmc_host_idr, host, 0, 0, GFP_NOWAIT);
  493. if (err >= 0)
  494. host->index = err;
  495. spin_unlock(&mmc_host_lock);
  496. idr_preload_end();
  497. if (err < 0) {
  498. kfree(host);
  499. return NULL;
  500. }
  501. dev_set_name(&host->class_dev, "mmc%d", host->index);
  502. host->parent = dev;
  503. host->class_dev.parent = dev;
  504. host->class_dev.class = &mmc_host_class;
  505. device_initialize(&host->class_dev);
  506. if (mmc_gpio_alloc(host)) {
  507. put_device(&host->class_dev);
  508. return NULL;
  509. }
  510. mmc_host_clk_init(host);
  511. spin_lock_init(&host->lock);
  512. init_waitqueue_head(&host->wq);
  513. INIT_DELAYED_WORK(&host->detect, mmc_rescan);
  514. #ifdef CONFIG_PM
  515. host->pm_notify.notifier_call = mmc_pm_notify;
  516. #endif
  517. setup_timer(&host->retune_timer, mmc_retune_timer, (unsigned long)host);
  518. /*
  519. * By default, hosts do not support SGIO or large requests.
  520. * They have to set these according to their abilities.
  521. */
  522. host->max_segs = 1;
  523. host->max_seg_size = PAGE_CACHE_SIZE;
  524. host->max_req_size = PAGE_CACHE_SIZE;
  525. host->max_blk_size = 512;
  526. host->max_blk_count = PAGE_CACHE_SIZE / 512;
  527. return host;
  528. }
  529. EXPORT_SYMBOL(mmc_alloc_host);
  530. /**
  531. * mmc_add_host - initialise host hardware
  532. * @host: mmc host
  533. *
  534. * Register the host with the driver model. The host must be
  535. * prepared to start servicing requests before this function
  536. * completes.
  537. */
  538. int mmc_add_host(struct mmc_host *host)
  539. {
  540. int err;
  541. WARN_ON((host->caps & MMC_CAP_SDIO_IRQ) &&
  542. !host->ops->enable_sdio_irq);
  543. err = device_add(&host->class_dev);
  544. if (err)
  545. return err;
  546. led_trigger_register_simple(dev_name(&host->class_dev), &host->led);
  547. #ifdef CONFIG_DEBUG_FS
  548. mmc_add_host_debugfs(host);
  549. #endif
  550. mmc_host_clk_sysfs_init(host);
  551. mmc_start_host(host);
  552. register_pm_notifier(&host->pm_notify);
  553. return 0;
  554. }
  555. EXPORT_SYMBOL(mmc_add_host);
  556. /**
  557. * mmc_remove_host - remove host hardware
  558. * @host: mmc host
  559. *
  560. * Unregister and remove all cards associated with this host,
  561. * and power down the MMC bus. No new requests will be issued
  562. * after this function has returned.
  563. */
  564. void mmc_remove_host(struct mmc_host *host)
  565. {
  566. unregister_pm_notifier(&host->pm_notify);
  567. mmc_stop_host(host);
  568. #ifdef CONFIG_DEBUG_FS
  569. mmc_remove_host_debugfs(host);
  570. #endif
  571. device_del(&host->class_dev);
  572. led_trigger_unregister_simple(host->led);
  573. mmc_host_clk_exit(host);
  574. }
  575. EXPORT_SYMBOL(mmc_remove_host);
  576. /**
  577. * mmc_free_host - free the host structure
  578. * @host: mmc host
  579. *
  580. * Free the host once all references to it have been dropped.
  581. */
  582. void mmc_free_host(struct mmc_host *host)
  583. {
  584. mmc_pwrseq_free(host);
  585. put_device(&host->class_dev);
  586. }
  587. EXPORT_SYMBOL(mmc_free_host);