Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

spi: split up spi_new_device() to allow two stage registration.

spi_new_device() allocates and registers an spi device all in one swoop.
If the driver needs to add extra data to the spi_device before it is
registered, then this causes problems. This is needed for OF device
tree support so that the SPI device tree helper can add a pointer to
the device node after the device is allocated, but before the device
is registered. OF aware SPI devices can then retrieve data out of the
device node to populate a platform data structure.

This patch splits the allocation and registration portions of code out
of spi_new_device() and creates two new functions; spi_alloc_device()
and spi_register_device(). spi_new_device() is modified to use the new
functions for allocation and registration. None of the existing users
of spi_new_device() should be affected by this change.

Drivers using the new API can forego the use of spi_board_info
structure to describe the device layout and populate data into the
spi_device structure directly.

This change is in preparation for adding an OF device tree parser to
generate spi_devices based on data in the device tree.

Signed-off-by: Grant Likely <grant.likely@secretlab.ca>
Acked-by: David Brownell <dbrownell@users.sourceforge.net>

+107 -44
+95 -44
drivers/spi/spi.c
··· 178 178 static LIST_HEAD(board_list); 179 179 static DEFINE_MUTEX(board_lock); 180 180 181 + /** 182 + * spi_alloc_device - Allocate a new SPI device 183 + * @master: Controller to which device is connected 184 + * Context: can sleep 185 + * 186 + * Allows a driver to allocate and initialize a spi_device without 187 + * registering it immediately. This allows a driver to directly 188 + * fill the spi_device with device parameters before calling 189 + * spi_add_device() on it. 190 + * 191 + * Caller is responsible to call spi_add_device() on the returned 192 + * spi_device structure to add it to the SPI master. If the caller 193 + * needs to discard the spi_device without adding it, then it should 194 + * call spi_dev_put() on it. 195 + * 196 + * Returns a pointer to the new device, or NULL. 197 + */ 198 + struct spi_device *spi_alloc_device(struct spi_master *master) 199 + { 200 + struct spi_device *spi; 201 + struct device *dev = master->dev.parent; 202 + 203 + if (!spi_master_get(master)) 204 + return NULL; 205 + 206 + spi = kzalloc(sizeof *spi, GFP_KERNEL); 207 + if (!spi) { 208 + dev_err(dev, "cannot alloc spi_device\n"); 209 + spi_master_put(master); 210 + return NULL; 211 + } 212 + 213 + spi->master = master; 214 + spi->dev.parent = dev; 215 + spi->dev.bus = &spi_bus_type; 216 + spi->dev.release = spidev_release; 217 + device_initialize(&spi->dev); 218 + return spi; 219 + } 220 + EXPORT_SYMBOL_GPL(spi_alloc_device); 221 + 222 + /** 223 + * spi_add_device - Add spi_device allocated with spi_alloc_device 224 + * @spi: spi_device to register 225 + * 226 + * Companion function to spi_alloc_device. Devices allocated with 227 + * spi_alloc_device can be added onto the spi bus with this function. 228 + * 229 + * Returns 0 on success; non-zero on failure 230 + */ 231 + int spi_add_device(struct spi_device *spi) 232 + { 233 + struct device *dev = spi->master->dev.parent; 234 + int status; 235 + 236 + /* Chipselects are numbered 0..max; validate. */ 237 + if (spi->chip_select >= spi->master->num_chipselect) { 238 + dev_err(dev, "cs%d >= max %d\n", 239 + spi->chip_select, 240 + spi->master->num_chipselect); 241 + return -EINVAL; 242 + } 243 + 244 + /* Set the bus ID string */ 245 + snprintf(spi->dev.bus_id, sizeof spi->dev.bus_id, 246 + "%s.%u", spi->master->dev.bus_id, 247 + spi->chip_select); 248 + 249 + /* drivers may modify this initial i/o setup */ 250 + status = spi->master->setup(spi); 251 + if (status < 0) { 252 + dev_err(dev, "can't %s %s, status %d\n", 253 + "setup", spi->dev.bus_id, status); 254 + return status; 255 + } 256 + 257 + /* driver core catches callers that misbehave by defining 258 + * devices that already exist. 259 + */ 260 + status = device_add(&spi->dev); 261 + if (status < 0) { 262 + dev_err(dev, "can't %s %s, status %d\n", 263 + "add", spi->dev.bus_id, status); 264 + return status; 265 + } 266 + 267 + dev_dbg(dev, "registered child %s\n", spi->dev.bus_id); 268 + return 0; 269 + } 270 + EXPORT_SYMBOL_GPL(spi_add_device); 181 271 182 272 /** 183 273 * spi_new_device - instantiate one new SPI device ··· 287 197 struct spi_board_info *chip) 288 198 { 289 199 struct spi_device *proxy; 290 - struct device *dev = master->dev.parent; 291 200 int status; 292 201 293 202 /* NOTE: caller did any chip->bus_num checks necessary. ··· 296 207 * suggests syslogged diagnostics are best here (ugh). 297 208 */ 298 209 299 - /* Chipselects are numbered 0..max; validate. */ 300 - if (chip->chip_select >= master->num_chipselect) { 301 - dev_err(dev, "cs%d > max %d\n", 302 - chip->chip_select, 303 - master->num_chipselect); 304 - return NULL; 305 - } 306 - 307 - if (!spi_master_get(master)) 210 + proxy = spi_alloc_device(master); 211 + if (!proxy) 308 212 return NULL; 309 213 310 214 WARN_ON(strlen(chip->modalias) >= sizeof(proxy->modalias)); 311 215 312 - proxy = kzalloc(sizeof *proxy, GFP_KERNEL); 313 - if (!proxy) { 314 - dev_err(dev, "can't alloc dev for cs%d\n", 315 - chip->chip_select); 316 - goto fail; 317 - } 318 - proxy->master = master; 319 216 proxy->chip_select = chip->chip_select; 320 217 proxy->max_speed_hz = chip->max_speed_hz; 321 218 proxy->mode = chip->mode; 322 219 proxy->irq = chip->irq; 323 220 strlcpy(proxy->modalias, chip->modalias, sizeof(proxy->modalias)); 324 - 325 - snprintf(proxy->dev.bus_id, sizeof proxy->dev.bus_id, 326 - "%s.%u", master->dev.bus_id, 327 - chip->chip_select); 328 - proxy->dev.parent = dev; 329 - proxy->dev.bus = &spi_bus_type; 330 221 proxy->dev.platform_data = (void *) chip->platform_data; 331 222 proxy->controller_data = chip->controller_data; 332 223 proxy->controller_state = NULL; 333 - proxy->dev.release = spidev_release; 334 224 335 - /* drivers may modify this initial i/o setup */ 336 - status = master->setup(proxy); 225 + status = spi_add_device(proxy); 337 226 if (status < 0) { 338 - dev_err(dev, "can't %s %s, status %d\n", 339 - "setup", proxy->dev.bus_id, status); 340 - goto fail; 227 + spi_dev_put(proxy); 228 + return NULL; 341 229 } 342 230 343 - /* driver core catches callers that misbehave by defining 344 - * devices that already exist. 345 - */ 346 - status = device_register(&proxy->dev); 347 - if (status < 0) { 348 - dev_err(dev, "can't %s %s, status %d\n", 349 - "add", proxy->dev.bus_id, status); 350 - goto fail; 351 - } 352 - dev_dbg(dev, "registered child %s\n", proxy->dev.bus_id); 353 231 return proxy; 354 - 355 - fail: 356 - spi_master_put(master); 357 - kfree(proxy); 358 - return NULL; 359 232 } 360 233 EXPORT_SYMBOL_GPL(spi_new_device); 361 234
+12
include/linux/spi/spi.h
··· 778 778 * use spi_new_device() to describe each device. You can also call 779 779 * spi_unregister_device() to start making that device vanish, but 780 780 * normally that would be handled by spi_unregister_master(). 781 + * 782 + * You can also use spi_alloc_device() and spi_add_device() to use a two 783 + * stage registration sequence for each spi_device. This gives the caller 784 + * some more control over the spi_device structure before it is registered, 785 + * but requires that caller to initialize fields that would otherwise 786 + * be defined using the board info. 781 787 */ 788 + extern struct spi_device * 789 + spi_alloc_device(struct spi_master *master); 790 + 791 + extern int 792 + spi_add_device(struct spi_device *spi); 793 + 782 794 extern struct spi_device * 783 795 spi_new_device(struct spi_master *, struct spi_board_info *); 784 796