aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/spi
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@linux-foundation.org>2014-12-11 16:06:58 -0500
committerLinus Torvalds <torvalds@linux-foundation.org>2014-12-11 16:06:58 -0500
commit7ef58b32f571bffb7763c6252ad7527562081f34 (patch)
tree6d1493304ec7a47e4d9e3e84dc9f6e53547dff91 /drivers/spi
parent413fd0e3fbf52873f2310eb75bfa6c7b72847277 (diff)
parentc46ca3c8310b61d253a39ff1375ea97912794cd1 (diff)
Merge tag 'devicetree-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/glikely/linux
Pull devicetree changes from Grant Likely: "Lots of activity in the devicetree code for v3.18. Most of it is related to getting all of the overlay support code in place, but there are other important things in there. Highlights: - OF_RECONFIG notifiers for SPI, I2C and Platform devices. Those subsystems can now respond to live changes to the device tree. - CONFIG_OF_OVERLAY method for applying live changes to the device tree - Removal of the of_allnodes list. This used to be used to iterate over all the nodes in the device tree, but it is unnecessary because the same thing can be done by iterating over the list of child pointers. Getting rid of of_allnodes saves some memory and avoids the possibility of of_allnodes being sorted differently from the child lists. - Support for retrieving original DTB blob via sysfs. Needed by kexec. - More unittests - Documentation and minor bug fixes" * tag 'devicetree-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/glikely/linux: (42 commits) of: Delete unnecessary check before calling "of_node_put()" of: Drop ->next pointer from struct device_node spi: Check for spi_of_notifier when CONFIG_OF_DYNAMIC=y of: support passing console options with stdout-path of: add optional options parameter to of_find_node_by_path() of: Add bindings for chosen node, stdout-path of: Remove unneeded and incorrect MODULE_DEVICE_TABLE ARM: dt: fix up PL011 device tree bindings of: base, fix of_property_read_string_helper kernel-doc of: remove select of non-existant OF_DEVICE config symbol spi/of: Add OF notifier handler spi/of: Create new device registration method and accessors i2c/of: Add OF_RECONFIG notifier handler i2c/of: Factor out Devicetree registration code of/overlay: Add overlay unittests of/overlay: Introduce DT overlay support of/reconfig: Add OF_DYNAMIC notifier for platform_bus_type of/reconfig: Always use the same structure for notifiers of/reconfig: Add debug output for OF_RECONFIG notifiers of/reconfig: Add empty stubs for the of_reconfig methods ...
Diffstat (limited to 'drivers/spi')
-rw-r--r--drivers/spi/spi.c306
1 files changed, 202 insertions, 104 deletions
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index da7e6225b8f6..66a70e9bc743 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -1220,6 +1220,121 @@ err_init_queue:
1220/*-------------------------------------------------------------------------*/ 1220/*-------------------------------------------------------------------------*/
1221 1221
1222#if defined(CONFIG_OF) 1222#if defined(CONFIG_OF)
1223static struct spi_device *
1224of_register_spi_device(struct spi_master *master, struct device_node *nc)
1225{
1226 struct spi_device *spi;
1227 int rc;
1228 u32 value;
1229
1230 /* Alloc an spi_device */
1231 spi = spi_alloc_device(master);
1232 if (!spi) {
1233 dev_err(&master->dev, "spi_device alloc error for %s\n",
1234 nc->full_name);
1235 rc = -ENOMEM;
1236 goto err_out;
1237 }
1238
1239 /* Select device driver */
1240 rc = of_modalias_node(nc, spi->modalias,
1241 sizeof(spi->modalias));
1242 if (rc < 0) {
1243 dev_err(&master->dev, "cannot find modalias for %s\n",
1244 nc->full_name);
1245 goto err_out;
1246 }
1247
1248 /* Device address */
1249 rc = of_property_read_u32(nc, "reg", &value);
1250 if (rc) {
1251 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1252 nc->full_name, rc);
1253 goto err_out;
1254 }
1255 spi->chip_select = value;
1256
1257 /* Mode (clock phase/polarity/etc.) */
1258 if (of_find_property(nc, "spi-cpha", NULL))
1259 spi->mode |= SPI_CPHA;
1260 if (of_find_property(nc, "spi-cpol", NULL))
1261 spi->mode |= SPI_CPOL;
1262 if (of_find_property(nc, "spi-cs-high", NULL))
1263 spi->mode |= SPI_CS_HIGH;
1264 if (of_find_property(nc, "spi-3wire", NULL))
1265 spi->mode |= SPI_3WIRE;
1266 if (of_find_property(nc, "spi-lsb-first", NULL))
1267 spi->mode |= SPI_LSB_FIRST;
1268
1269 /* Device DUAL/QUAD mode */
1270 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1271 switch (value) {
1272 case 1:
1273 break;
1274 case 2:
1275 spi->mode |= SPI_TX_DUAL;
1276 break;
1277 case 4:
1278 spi->mode |= SPI_TX_QUAD;
1279 break;
1280 default:
1281 dev_warn(&master->dev,
1282 "spi-tx-bus-width %d not supported\n",
1283 value);
1284 break;
1285 }
1286 }
1287
1288 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1289 switch (value) {
1290 case 1:
1291 break;
1292 case 2:
1293 spi->mode |= SPI_RX_DUAL;
1294 break;
1295 case 4:
1296 spi->mode |= SPI_RX_QUAD;
1297 break;
1298 default:
1299 dev_warn(&master->dev,
1300 "spi-rx-bus-width %d not supported\n",
1301 value);
1302 break;
1303 }
1304 }
1305
1306 /* Device speed */
1307 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1308 if (rc) {
1309 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1310 nc->full_name, rc);
1311 goto err_out;
1312 }
1313 spi->max_speed_hz = value;
1314
1315 /* IRQ */
1316 spi->irq = irq_of_parse_and_map(nc, 0);
1317
1318 /* Store a pointer to the node in the device structure */
1319 of_node_get(nc);
1320 spi->dev.of_node = nc;
1321
1322 /* Register the new device */
1323 request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
1324 rc = spi_add_device(spi);
1325 if (rc) {
1326 dev_err(&master->dev, "spi_device register error %s\n",
1327 nc->full_name);
1328 goto err_out;
1329 }
1330
1331 return spi;
1332
1333err_out:
1334 spi_dev_put(spi);
1335 return ERR_PTR(rc);
1336}
1337
1223/** 1338/**
1224 * of_register_spi_devices() - Register child devices onto the SPI bus 1339 * of_register_spi_devices() - Register child devices onto the SPI bus
1225 * @master: Pointer to spi_master device 1340 * @master: Pointer to spi_master device
@@ -1231,116 +1346,15 @@ static void of_register_spi_devices(struct spi_master *master)
1231{ 1346{
1232 struct spi_device *spi; 1347 struct spi_device *spi;
1233 struct device_node *nc; 1348 struct device_node *nc;
1234 int rc;
1235 u32 value;
1236 1349
1237 if (!master->dev.of_node) 1350 if (!master->dev.of_node)
1238 return; 1351 return;
1239 1352
1240 for_each_available_child_of_node(master->dev.of_node, nc) { 1353 for_each_available_child_of_node(master->dev.of_node, nc) {
1241 /* Alloc an spi_device */ 1354 spi = of_register_spi_device(master, nc);
1242 spi = spi_alloc_device(master); 1355 if (IS_ERR(spi))
1243 if (!spi) { 1356 dev_warn(&master->dev, "Failed to create SPI device for %s\n",
1244 dev_err(&master->dev, "spi_device alloc error for %s\n",
1245 nc->full_name); 1357 nc->full_name);
1246 spi_dev_put(spi);
1247 continue;
1248 }
1249
1250 /* Select device driver */
1251 if (of_modalias_node(nc, spi->modalias,
1252 sizeof(spi->modalias)) < 0) {
1253 dev_err(&master->dev, "cannot find modalias for %s\n",
1254 nc->full_name);
1255 spi_dev_put(spi);
1256 continue;
1257 }
1258
1259 /* Device address */
1260 rc = of_property_read_u32(nc, "reg", &value);
1261 if (rc) {
1262 dev_err(&master->dev, "%s has no valid 'reg' property (%d)\n",
1263 nc->full_name, rc);
1264 spi_dev_put(spi);
1265 continue;
1266 }
1267 spi->chip_select = value;
1268
1269 /* Mode (clock phase/polarity/etc.) */
1270 if (of_find_property(nc, "spi-cpha", NULL))
1271 spi->mode |= SPI_CPHA;
1272 if (of_find_property(nc, "spi-cpol", NULL))
1273 spi->mode |= SPI_CPOL;
1274 if (of_find_property(nc, "spi-cs-high", NULL))
1275 spi->mode |= SPI_CS_HIGH;
1276 if (of_find_property(nc, "spi-3wire", NULL))
1277 spi->mode |= SPI_3WIRE;
1278 if (of_find_property(nc, "spi-lsb-first", NULL))
1279 spi->mode |= SPI_LSB_FIRST;
1280
1281 /* Device DUAL/QUAD mode */
1282 if (!of_property_read_u32(nc, "spi-tx-bus-width", &value)) {
1283 switch (value) {
1284 case 1:
1285 break;
1286 case 2:
1287 spi->mode |= SPI_TX_DUAL;
1288 break;
1289 case 4:
1290 spi->mode |= SPI_TX_QUAD;
1291 break;
1292 default:
1293 dev_warn(&master->dev,
1294 "spi-tx-bus-width %d not supported\n",
1295 value);
1296 break;
1297 }
1298 }
1299
1300 if (!of_property_read_u32(nc, "spi-rx-bus-width", &value)) {
1301 switch (value) {
1302 case 1:
1303 break;
1304 case 2:
1305 spi->mode |= SPI_RX_DUAL;
1306 break;
1307 case 4:
1308 spi->mode |= SPI_RX_QUAD;
1309 break;
1310 default:
1311 dev_warn(&master->dev,
1312 "spi-rx-bus-width %d not supported\n",
1313 value);
1314 break;
1315 }
1316 }
1317
1318 /* Device speed */
1319 rc = of_property_read_u32(nc, "spi-max-frequency", &value);
1320 if (rc) {
1321 dev_err(&master->dev, "%s has no valid 'spi-max-frequency' property (%d)\n",
1322 nc->full_name, rc);
1323 spi_dev_put(spi);
1324 continue;
1325 }
1326 spi->max_speed_hz = value;
1327
1328 /* IRQ */
1329 spi->irq = irq_of_parse_and_map(nc, 0);
1330
1331 /* Store a pointer to the node in the device structure */
1332 of_node_get(nc);
1333 spi->dev.of_node = nc;
1334
1335 /* Register the new device */
1336 request_module("%s%s", SPI_MODULE_PREFIX, spi->modalias);
1337 rc = spi_add_device(spi);
1338 if (rc) {
1339 dev_err(&master->dev, "spi_device register error %s\n",
1340 nc->full_name);
1341 spi_dev_put(spi);
1342 }
1343
1344 } 1358 }
1345} 1359}
1346#else 1360#else
@@ -2303,6 +2317,86 @@ EXPORT_SYMBOL_GPL(spi_write_then_read);
2303 2317
2304/*-------------------------------------------------------------------------*/ 2318/*-------------------------------------------------------------------------*/
2305 2319
2320#if IS_ENABLED(CONFIG_OF_DYNAMIC)
2321static int __spi_of_device_match(struct device *dev, void *data)
2322{
2323 return dev->of_node == data;
2324}
2325
2326/* must call put_device() when done with returned spi_device device */
2327static struct spi_device *of_find_spi_device_by_node(struct device_node *node)
2328{
2329 struct device *dev = bus_find_device(&spi_bus_type, NULL, node,
2330 __spi_of_device_match);
2331 return dev ? to_spi_device(dev) : NULL;
2332}
2333
2334static int __spi_of_master_match(struct device *dev, const void *data)
2335{
2336 return dev->of_node == data;
2337}
2338
2339/* the spi masters are not using spi_bus, so we find it with another way */
2340static struct spi_master *of_find_spi_master_by_node(struct device_node *node)
2341{
2342 struct device *dev;
2343
2344 dev = class_find_device(&spi_master_class, NULL, node,
2345 __spi_of_master_match);
2346 if (!dev)
2347 return NULL;
2348
2349 /* reference got in class_find_device */
2350 return container_of(dev, struct spi_master, dev);
2351}
2352
2353static int of_spi_notify(struct notifier_block *nb, unsigned long action,
2354 void *arg)
2355{
2356 struct of_reconfig_data *rd = arg;
2357 struct spi_master *master;
2358 struct spi_device *spi;
2359
2360 switch (of_reconfig_get_state_change(action, arg)) {
2361 case OF_RECONFIG_CHANGE_ADD:
2362 master = of_find_spi_master_by_node(rd->dn->parent);
2363 if (master == NULL)
2364 return NOTIFY_OK; /* not for us */
2365
2366 spi = of_register_spi_device(master, rd->dn);
2367 put_device(&master->dev);
2368
2369 if (IS_ERR(spi)) {
2370 pr_err("%s: failed to create for '%s'\n",
2371 __func__, rd->dn->full_name);
2372 return notifier_from_errno(PTR_ERR(spi));
2373 }
2374 break;
2375
2376 case OF_RECONFIG_CHANGE_REMOVE:
2377 /* find our device by node */
2378 spi = of_find_spi_device_by_node(rd->dn);
2379 if (spi == NULL)
2380 return NOTIFY_OK; /* no? not meant for us */
2381
2382 /* unregister takes one ref away */
2383 spi_unregister_device(spi);
2384
2385 /* and put the reference of the find */
2386 put_device(&spi->dev);
2387 break;
2388 }
2389
2390 return NOTIFY_OK;
2391}
2392
2393static struct notifier_block spi_of_notifier = {
2394 .notifier_call = of_spi_notify,
2395};
2396#else /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
2397extern struct notifier_block spi_of_notifier;
2398#endif /* IS_ENABLED(CONFIG_OF_DYNAMIC) */
2399
2306static int __init spi_init(void) 2400static int __init spi_init(void)
2307{ 2401{
2308 int status; 2402 int status;
@@ -2320,6 +2414,10 @@ static int __init spi_init(void)
2320 status = class_register(&spi_master_class); 2414 status = class_register(&spi_master_class);
2321 if (status < 0) 2415 if (status < 0)
2322 goto err2; 2416 goto err2;
2417
2418 if (IS_ENABLED(CONFIG_OF_DYNAMIC))
2419 WARN_ON(of_reconfig_notifier_register(&spi_of_notifier));
2420
2323 return 0; 2421 return 0;
2324 2422
2325err2: 2423err2: