Skip to content

Commit 99fc513

Browse files
Linus Walleijcjb
authored andcommitted
mmc: Move regulator handling closer to core
After discovering a problem in regulator reference counting I took Mark Brown's advice to move the reference count into the MMC core by making the regulator status a member of struct mmc_host. I took this opportunity to also implement NULL versions of the regulator functions so as to rid the driver code from some ugly #ifdef CONFIG_REGULATOR clauses. Signed-off-by: Linus Walleij <linus.walleij@stericsson.com> Reviewed-by: Mark Brown <broonie@opensource.wolfsonmicro.com> Cc: Liam Girdwood <lrg@slimlogic.co.uk> Cc: Tony Lindgren <tony@atomide.com> Cc: Adrian Hunter <adrian.hunter@nokia.com> Cc: Robert Jarzmik <robert.jarzmik@free.fr> Cc: Sundar Iyer <sundar.iyer@stericsson.com> Cc: Daniel Mack <daniel@caiaq.de> Cc: Pierre Ossman <pierre@ossman.eu> Cc: Matt Fleming <matt@console-pimps.org> Cc: David Brownell <dbrownell@users.sourceforge.net> Cc: Russell King <rmk+kernel@arm.linux.org.uk> Cc: Eric Miao <eric.y.miao@gmail.com> Cc: Cliff Brake <cbrake@bec-systems.com> Cc: Jarkko Lavinen <jarkko.lavinen@nokia.com> Cc: <linux-mmc@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Chris Ball <cjb@laptop.org>
1 parent 4d0b861 commit 99fc513

5 files changed

Lines changed: 101 additions & 37 deletions

File tree

‎drivers/mmc/core/core.c‎

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -772,24 +772,22 @@ EXPORT_SYMBOL(mmc_regulator_get_ocrmask);
772772

773773
/**
774774
* mmc_regulator_set_ocr - set regulator to match host->ios voltage
775-
* @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
775+
* @mmc: the host to regulate
776776
* @supply: regulator to use
777+
* @vdd_bit: zero for power off, else a bit number (host->ios.vdd)
777778
*
778779
* Returns zero on success, else negative errno.
779780
*
780781
* MMC host drivers may use this to enable or disable a regulator using
781782
* a particular supply voltage. This would normally be called from the
782783
* set_ios() method.
783784
*/
784-
int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
785+
int mmc_regulator_set_ocr(struct mmc_host *mmc,
786+
struct regulator *supply,
787+
unsigned short vdd_bit)
785788
{
786789
int result = 0;
787790
int min_uV, max_uV;
788-
int enabled;
789-
790-
enabled = regulator_is_enabled(supply);
791-
if (enabled < 0)
792-
return enabled;
793791

794792
if (vdd_bit) {
795793
int tmp;
@@ -820,17 +818,25 @@ int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit)
820818
else
821819
result = 0;
822820

823-
if (result == 0 && !enabled)
821+
if (result == 0 && !mmc->regulator_enabled) {
824822
result = regulator_enable(supply);
825-
} else if (enabled) {
823+
if (!result)
824+
mmc->regulator_enabled = true;
825+
}
826+
} else if (mmc->regulator_enabled) {
826827
result = regulator_disable(supply);
828+
if (result == 0)
829+
mmc->regulator_enabled = false;
827830
}
828831

832+
if (result)
833+
dev_err(mmc_dev(mmc),
834+
"could not set regulator OCR (%d)\n", result);
829835
return result;
830836
}
831837
EXPORT_SYMBOL(mmc_regulator_set_ocr);
832838

833-
#endif
839+
#endif /* CONFIG_REGULATOR */
834840

835841
/*
836842
* Mask off any voltages we don't support and select

‎drivers/mmc/host/mmci.c‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -523,19 +523,27 @@ static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
523523
struct mmci_host *host = mmc_priv(mmc);
524524
u32 pwr = 0;
525525
unsigned long flags;
526+
int ret;
526527

527528
switch (ios->power_mode) {
528529
case MMC_POWER_OFF:
529-
if(host->vcc &&
530-
regulator_is_enabled(host->vcc))
531-
regulator_disable(host->vcc);
530+
if (host->vcc)
531+
ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
532532
break;
533533
case MMC_POWER_UP:
534-
#ifdef CONFIG_REGULATOR
535-
if (host->vcc)
536-
/* This implicitly enables the regulator */
537-
mmc_regulator_set_ocr(host->vcc, ios->vdd);
538-
#endif
534+
if (host->vcc) {
535+
ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
536+
if (ret) {
537+
dev_err(mmc_dev(mmc), "unable to set OCR\n");
538+
/*
539+
* The .set_ios() function in the mmc_host_ops
540+
* struct return void, and failing to set the
541+
* power should be rare so we print an error
542+
* and return here.
543+
*/
544+
return;
545+
}
546+
}
539547
if (host->plat->vdd_handler)
540548
pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
541549
ios->power_mode);
@@ -869,8 +877,8 @@ static int __devexit mmci_remove(struct amba_device *dev)
869877
clk_disable(host->clk);
870878
clk_put(host->clk);
871879

872-
if (regulator_is_enabled(host->vcc))
873-
regulator_disable(host->vcc);
880+
if (host->vcc)
881+
mmc_regulator_set_ocr(mmc, host->vcc, 0);
874882
regulator_put(host->vcc);
875883

876884
mmc_free_host(mmc);

‎drivers/mmc/host/omap_hsmmc.c‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,9 @@ static int omap_hsmmc_1_set_power(struct device *dev, int slot, int power_on,
250250
mmc_slot(host).before_set_reg(dev, slot, power_on, vdd);
251251

252252
if (power_on)
253-
ret = mmc_regulator_set_ocr(host->vcc, vdd);
253+
ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
254254
else
255-
ret = mmc_regulator_set_ocr(host->vcc, 0);
255+
ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
256256

257257
if (mmc_slot(host).after_set_reg)
258258
mmc_slot(host).after_set_reg(dev, slot, power_on, vdd);
@@ -291,18 +291,23 @@ static int omap_hsmmc_23_set_power(struct device *dev, int slot, int power_on,
291291
* chips/cards need an interface voltage rail too.
292292
*/
293293
if (power_on) {
294-
ret = mmc_regulator_set_ocr(host->vcc, vdd);
294+
ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
295295
/* Enable interface voltage rail, if needed */
296296
if (ret == 0 && host->vcc_aux) {
297297
ret = regulator_enable(host->vcc_aux);
298298
if (ret < 0)
299-
ret = mmc_regulator_set_ocr(host->vcc, 0);
299+
ret = mmc_regulator_set_ocr(host->mmc,
300+
host->vcc, 0);
300301
}
301302
} else {
303+
/* Shut down the rail */
302304
if (host->vcc_aux)
303305
ret = regulator_disable(host->vcc_aux);
304-
if (ret == 0)
305-
ret = mmc_regulator_set_ocr(host->vcc, 0);
306+
if (!ret) {
307+
/* Then proceed to shut down the local regulator */
308+
ret = mmc_regulator_set_ocr(host->mmc,
309+
host->vcc, 0);
310+
}
306311
}
307312

308313
if (mmc_slot(host).after_set_reg)
@@ -343,9 +348,9 @@ static int omap_hsmmc_23_set_sleep(struct device *dev, int slot, int sleep,
343348
if (cardsleep) {
344349
/* VCC can be turned off if card is asleep */
345350
if (sleep)
346-
err = mmc_regulator_set_ocr(host->vcc, 0);
351+
err = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
347352
else
348-
err = mmc_regulator_set_ocr(host->vcc, vdd);
353+
err = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
349354
} else
350355
err = regulator_set_mode(host->vcc, mode);
351356
if (err)

‎drivers/mmc/host/pxamci.c‎

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,25 @@ static inline void pxamci_init_ocr(struct pxamci_host *host)
9999
}
100100
}
101101

102-
static inline void pxamci_set_power(struct pxamci_host *host, unsigned int vdd)
102+
static inline int pxamci_set_power(struct pxamci_host *host,
103+
unsigned char power_mode,
104+
unsigned int vdd)
103105
{
104106
int on;
105107

106-
#ifdef CONFIG_REGULATOR
107-
if (host->vcc)
108-
mmc_regulator_set_ocr(host->vcc, vdd);
109-
#endif
108+
if (host->vcc) {
109+
int ret;
110+
111+
if (power_mode == MMC_POWER_UP) {
112+
ret = mmc_regulator_set_ocr(host->mmc, host->vcc, vdd);
113+
if (ret)
114+
return ret;
115+
} else if (power_mode == MMC_POWER_OFF) {
116+
ret = mmc_regulator_set_ocr(host->mmc, host->vcc, 0);
117+
if (ret)
118+
return ret;
119+
}
120+
}
110121
if (!host->vcc && host->pdata &&
111122
gpio_is_valid(host->pdata->gpio_power)) {
112123
on = ((1 << vdd) & host->pdata->ocr_mask);
@@ -115,6 +126,8 @@ static inline void pxamci_set_power(struct pxamci_host *host, unsigned int vdd)
115126
}
116127
if (!host->vcc && host->pdata && host->pdata->setpower)
117128
host->pdata->setpower(mmc_dev(host->mmc), vdd);
129+
130+
return 0;
118131
}
119132

120133
static void pxamci_stop_clock(struct pxamci_host *host)
@@ -490,9 +503,21 @@ static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
490503
}
491504

492505
if (host->power_mode != ios->power_mode) {
506+
int ret;
507+
493508
host->power_mode = ios->power_mode;
494509

495-
pxamci_set_power(host, ios->vdd);
510+
ret = pxamci_set_power(host, ios->power_mode, ios->vdd);
511+
if (ret) {
512+
dev_err(mmc_dev(mmc), "unable to set power\n");
513+
/*
514+
* The .set_ios() function in the mmc_host_ops
515+
* struct return void, and failing to set the
516+
* power should be rare so we print an error and
517+
* return here.
518+
*/
519+
return;
520+
}
496521

497522
if (ios->power_mode == MMC_POWER_ON)
498523
host->cmdat |= CMDAT_INIT;
@@ -503,8 +528,8 @@ static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
503528
else
504529
host->cmdat &= ~CMDAT_SD_4DAT;
505530

506-
pr_debug("PXAMCI: clkrt = %x cmdat = %x\n",
507-
host->clkrt, host->cmdat);
531+
dev_dbg(mmc_dev(mmc), "PXAMCI: clkrt = %x cmdat = %x\n",
532+
host->clkrt, host->cmdat);
508533
}
509534

510535
static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)

‎include/linux/mmc/host.h‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ struct mmc_host {
212212
struct led_trigger *led; /* activity led */
213213
#endif
214214

215+
#ifdef CONFIG_REGULATOR
216+
bool regulator_enabled; /* regulator state */
217+
#endif
218+
215219
struct dentry *debugfs_root;
216220

217221
unsigned long private[0] ____cacheline_aligned;
@@ -250,8 +254,24 @@ static inline void mmc_signal_sdio_irq(struct mmc_host *host)
250254

251255
struct regulator;
252256

257+
#ifdef CONFIG_REGULATOR
253258
int mmc_regulator_get_ocrmask(struct regulator *supply);
254-
int mmc_regulator_set_ocr(struct regulator *supply, unsigned short vdd_bit);
259+
int mmc_regulator_set_ocr(struct mmc_host *mmc,
260+
struct regulator *supply,
261+
unsigned short vdd_bit);
262+
#else
263+
static inline int mmc_regulator_get_ocrmask(struct regulator *supply)
264+
{
265+
return 0;
266+
}
267+
268+
static inline int mmc_regulator_set_ocr(struct mmc_host *mmc,
269+
struct regulator *supply,
270+
unsigned short vdd_bit)
271+
{
272+
return 0;
273+
}
274+
#endif
255275

256276
int mmc_card_awake(struct mmc_host *host);
257277
int mmc_card_sleep(struct mmc_host *host);

0 commit comments

Comments
 (0)