diff options
author | Mark Brown <broonie@linaro.org> | 2013-08-31 13:50:52 -0400 |
---|---|---|
committer | Mark Brown <broonie@linaro.org> | 2013-09-16 19:59:36 -0400 |
commit | 666d5b4c742ba666eb68b467d777b7862f362ae5 (patch) | |
tree | efb1a0d621991a55effac8cc4f61f669542308e2 | |
parent | 272b98c6455f00884f0350f775c5342358ebb73f (diff) |
spi: core: Add devm_spi_register_master()
Help simplify the cleanup code for SPI master drivers by providing a
managed master registration function, ensuring that the master is
automatically unregistered whenever the device is unbound.
Signed-off-by: Mark Brown <broonie@linaro.org>
-rw-r--r-- | Documentation/driver-model/devres.txt | 3 | ||||
-rw-r--r-- | drivers/spi/spi.c | 35 | ||||
-rw-r--r-- | include/linux/spi/spi.h | 2 |
3 files changed, 40 insertions, 0 deletions
diff --git a/Documentation/driver-model/devres.txt b/Documentation/driver-model/devres.txt index fcb34a5697ea..84ea8216cc7d 100644 --- a/Documentation/driver-model/devres.txt +++ b/Documentation/driver-model/devres.txt | |||
@@ -302,3 +302,6 @@ PHY | |||
302 | 302 | ||
303 | SLAVE DMA ENGINE | 303 | SLAVE DMA ENGINE |
304 | devm_acpi_dma_controller_register() | 304 | devm_acpi_dma_controller_register() |
305 | |||
306 | SPI | ||
307 | devm_spi_register_master() | ||
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c index 9e039c60c068..a586ceb111fc 100644 --- a/drivers/spi/spi.c +++ b/drivers/spi/spi.c | |||
@@ -1245,6 +1245,41 @@ done: | |||
1245 | } | 1245 | } |
1246 | EXPORT_SYMBOL_GPL(spi_register_master); | 1246 | EXPORT_SYMBOL_GPL(spi_register_master); |
1247 | 1247 | ||
1248 | static void devm_spi_unregister(struct device *dev, void *res) | ||
1249 | { | ||
1250 | spi_unregister_master(*(struct spi_master **)res); | ||
1251 | } | ||
1252 | |||
1253 | /** | ||
1254 | * dev_spi_register_master - register managed SPI master controller | ||
1255 | * @dev: device managing SPI master | ||
1256 | * @master: initialized master, originally from spi_alloc_master() | ||
1257 | * Context: can sleep | ||
1258 | * | ||
1259 | * Register a SPI device as with spi_register_master() which will | ||
1260 | * automatically be unregister | ||
1261 | */ | ||
1262 | int devm_spi_register_master(struct device *dev, struct spi_master *master) | ||
1263 | { | ||
1264 | struct spi_master **ptr; | ||
1265 | int ret; | ||
1266 | |||
1267 | ptr = devres_alloc(devm_spi_unregister, sizeof(*ptr), GFP_KERNEL); | ||
1268 | if (!ptr) | ||
1269 | return -ENOMEM; | ||
1270 | |||
1271 | ret = spi_register_master(master); | ||
1272 | if (ret != 0) { | ||
1273 | *ptr = master; | ||
1274 | devres_add(dev, ptr); | ||
1275 | } else { | ||
1276 | devres_free(ptr); | ||
1277 | } | ||
1278 | |||
1279 | return ret; | ||
1280 | } | ||
1281 | EXPORT_SYMBOL_GPL(devm_spi_register_master); | ||
1282 | |||
1248 | static int __unregister(struct device *dev, void *null) | 1283 | static int __unregister(struct device *dev, void *null) |
1249 | { | 1284 | { |
1250 | spi_unregister_device(to_spi_device(dev)); | 1285 | spi_unregister_device(to_spi_device(dev)); |
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h index 887116dbce2c..4d634d66ba0b 100644 --- a/include/linux/spi/spi.h +++ b/include/linux/spi/spi.h | |||
@@ -434,6 +434,8 @@ extern struct spi_master * | |||
434 | spi_alloc_master(struct device *host, unsigned size); | 434 | spi_alloc_master(struct device *host, unsigned size); |
435 | 435 | ||
436 | extern int spi_register_master(struct spi_master *master); | 436 | extern int spi_register_master(struct spi_master *master); |
437 | extern int devm_spi_register_master(struct device *dev, | ||
438 | struct spi_master *master); | ||
437 | extern void spi_unregister_master(struct spi_master *master); | 439 | extern void spi_unregister_master(struct spi_master *master); |
438 | 440 | ||
439 | extern struct spi_master *spi_busnum_to_master(u16 busnum); | 441 | extern struct spi_master *spi_busnum_to_master(u16 busnum); |