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

[media] soc-camera: add V4L2-async support

Add support for asynchronous subdevice probing, using the v4l2-async API.
The legacy synchronous mode is still supported too, which allows to
gradually update drivers and platforms. The selected approach adds a
notifier for each struct soc_camera_device instance, i.e. for each video
device node, even when there are multiple such instances registered with a
single soc-camera host simultaneously.

Signed-off-by: Guennadi Liakhovetski <g.liakhovetski@gmx.de>
Acked-by: Hans Verkuil <hans.verkuil@cisco.com>
Acked-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Mauro Carvalho Chehab <mchehab@redhat.com>

authored by

Guennadi Liakhovetski and committed by
Mauro Carvalho Chehab
e09da11d 9aea470b

+465 -87
+443 -86
drivers/media/platform/soc_camera/soc_camera.c
··· 21 21 #include <linux/i2c.h> 22 22 #include <linux/init.h> 23 23 #include <linux/list.h> 24 - #include <linux/mutex.h> 25 24 #include <linux/module.h> 25 + #include <linux/mutex.h> 26 26 #include <linux/platform_device.h> 27 + #include <linux/pm_runtime.h> 27 28 #include <linux/regulator/consumer.h> 28 29 #include <linux/slab.h> 29 - #include <linux/pm_runtime.h> 30 30 #include <linux/vmalloc.h> 31 31 32 32 #include <media/soc_camera.h> 33 + #include <media/soc_mediabus.h> 34 + #include <media/v4l2-async.h> 33 35 #include <media/v4l2-clk.h> 34 36 #include <media/v4l2-common.h> 35 37 #include <media/v4l2-ioctl.h> 36 38 #include <media/v4l2-dev.h> 37 39 #include <media/videobuf-core.h> 38 40 #include <media/videobuf2-core.h> 39 - #include <media/soc_mediabus.h> 40 41 41 42 /* Default to VGA resolution */ 42 43 #define DEFAULT_WIDTH 640 ··· 48 47 (icd)->vb_vidq.streaming : \ 49 48 vb2_is_streaming(&(icd)->vb2_vidq)) 50 49 50 + #define MAP_MAX_NUM 32 51 + static DECLARE_BITMAP(device_map, MAP_MAX_NUM); 51 52 static LIST_HEAD(hosts); 52 53 static LIST_HEAD(devices); 53 - static DEFINE_MUTEX(list_lock); /* Protects the list of hosts */ 54 + /* 55 + * Protects lists and bitmaps of hosts and devices. 56 + * Lock nesting: Ok to take ->host_lock under list_lock. 57 + */ 58 + static DEFINE_MUTEX(list_lock); 59 + 60 + struct soc_camera_async_client { 61 + struct v4l2_async_subdev *sensor; 62 + struct v4l2_async_notifier notifier; 63 + struct platform_device *pdev; 64 + struct list_head list; /* needed for clean up */ 65 + }; 66 + 67 + static int soc_camera_video_start(struct soc_camera_device *icd); 68 + static int video_dev_create(struct soc_camera_device *icd); 54 69 55 70 int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd, 56 71 struct v4l2_clk *clk) 57 72 { 58 73 int ret = clk ? v4l2_clk_enable(clk) : 0; 59 74 if (ret < 0) { 60 - dev_err(dev, "Cannot enable clock\n"); 75 + dev_err(dev, "Cannot enable clock: %d\n", ret); 61 76 return ret; 62 77 } 63 78 ret = regulator_bulk_enable(ssdd->num_regulators, 64 79 ssdd->regulators); 65 80 if (ret < 0) { 66 81 dev_err(dev, "Cannot enable regulators\n"); 67 - goto eregenable;; 82 + goto eregenable; 68 83 } 69 84 70 85 if (ssdd->power) { ··· 133 116 return ret; 134 117 } 135 118 EXPORT_SYMBOL(soc_camera_power_off); 119 + 120 + int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd) 121 + { 122 + 123 + return devm_regulator_bulk_get(dev, ssdd->num_regulators, 124 + ssdd->regulators); 125 + } 126 + EXPORT_SYMBOL(soc_camera_power_init); 136 127 137 128 static int __soc_camera_power_on(struct soc_camera_device *icd) 138 129 { ··· 557 532 return -EBUSY; 558 533 559 534 if (!icd->clk) { 535 + mutex_lock(&ici->clk_lock); 560 536 ret = ici->ops->clock_start(ici); 537 + mutex_unlock(&ici->clk_lock); 561 538 if (ret < 0) 562 539 return ret; 563 540 } ··· 575 548 return 0; 576 549 577 550 eadd: 578 - if (!icd->clk) 551 + if (!icd->clk) { 552 + mutex_lock(&ici->clk_lock); 579 553 ici->ops->clock_stop(ici); 554 + mutex_unlock(&ici->clk_lock); 555 + } 580 556 return ret; 581 557 } 582 558 ··· 592 562 593 563 if (ici->ops->remove) 594 564 ici->ops->remove(icd); 595 - if (!icd->clk) 565 + if (!icd->clk) { 566 + mutex_lock(&ici->clk_lock); 596 567 ici->ops->clock_stop(ici); 568 + mutex_unlock(&ici->clk_lock); 569 + } 597 570 ici->icd = NULL; 598 571 } 599 572 ··· 705 672 return 0; 706 673 707 674 /* 708 - * First four errors are entered with the .host_lock held 709 - * and use_count == 1 675 + * All errors are entered with the .host_lock held, first four also 676 + * with use_count == 1 710 677 */ 711 678 einitvb: 712 679 esfmt: ··· 1131 1098 return -ENOIOCTLCMD; 1132 1099 } 1133 1100 1134 - static int soc_camera_probe(struct soc_camera_device *icd); 1101 + static int soc_camera_probe(struct soc_camera_host *ici, 1102 + struct soc_camera_device *icd); 1135 1103 1136 1104 /* So far this function cannot fail */ 1137 1105 static void scan_add_host(struct soc_camera_host *ici) ··· 1141 1107 1142 1108 mutex_lock(&list_lock); 1143 1109 1144 - list_for_each_entry(icd, &devices, list) { 1110 + list_for_each_entry(icd, &devices, list) 1145 1111 if (icd->iface == ici->nr) { 1112 + struct soc_camera_desc *sdesc = to_soc_camera_desc(icd); 1113 + struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc; 1114 + 1115 + /* The camera could have been already on, try to reset */ 1116 + if (ssdd->reset) 1117 + ssdd->reset(icd->pdev); 1118 + 1146 1119 icd->parent = ici->v4l2_dev.dev; 1147 - soc_camera_probe(icd); 1120 + 1121 + /* Ignore errors */ 1122 + soc_camera_probe(ici, icd); 1148 1123 } 1149 - } 1150 1124 1151 1125 mutex_unlock(&list_lock); 1152 1126 } ··· 1167 1125 { 1168 1126 struct soc_camera_device *icd = clk->priv; 1169 1127 struct soc_camera_host *ici; 1128 + int ret; 1170 1129 1171 1130 if (!icd || !icd->parent) 1172 1131 return -ENODEV; ··· 1181 1138 * If a different client is currently being probed, the host will tell 1182 1139 * you to go 1183 1140 */ 1184 - return ici->ops->clock_start(ici); 1141 + mutex_lock(&ici->clk_lock); 1142 + ret = ici->ops->clock_start(ici); 1143 + mutex_unlock(&ici->clk_lock); 1144 + return ret; 1185 1145 } 1186 1146 1187 1147 static void soc_camera_clk_disable(struct v4l2_clk *clk) ··· 1197 1151 1198 1152 ici = to_soc_camera_host(icd->parent); 1199 1153 1154 + mutex_lock(&ici->clk_lock); 1200 1155 ici->ops->clock_stop(ici); 1156 + mutex_unlock(&ici->clk_lock); 1201 1157 1202 1158 module_put(ici->ops->owner); 1203 1159 } ··· 1216 1168 .disable = soc_camera_clk_disable, 1217 1169 }; 1218 1170 1171 + static int soc_camera_dyn_pdev(struct soc_camera_desc *sdesc, 1172 + struct soc_camera_async_client *sasc) 1173 + { 1174 + struct platform_device *pdev; 1175 + int ret, i; 1176 + 1177 + mutex_lock(&list_lock); 1178 + i = find_first_zero_bit(device_map, MAP_MAX_NUM); 1179 + if (i < MAP_MAX_NUM) 1180 + set_bit(i, device_map); 1181 + mutex_unlock(&list_lock); 1182 + if (i >= MAP_MAX_NUM) 1183 + return -ENOMEM; 1184 + 1185 + pdev = platform_device_alloc("soc-camera-pdrv", i); 1186 + if (!pdev) 1187 + return -ENOMEM; 1188 + 1189 + ret = platform_device_add_data(pdev, sdesc, sizeof(*sdesc)); 1190 + if (ret < 0) { 1191 + platform_device_put(pdev); 1192 + return ret; 1193 + } 1194 + 1195 + sasc->pdev = pdev; 1196 + 1197 + return 0; 1198 + } 1199 + 1200 + static struct soc_camera_device *soc_camera_add_pdev(struct soc_camera_async_client *sasc) 1201 + { 1202 + struct platform_device *pdev = sasc->pdev; 1203 + int ret; 1204 + 1205 + ret = platform_device_add(pdev); 1206 + if (ret < 0 || !pdev->dev.driver) 1207 + return NULL; 1208 + 1209 + return platform_get_drvdata(pdev); 1210 + } 1211 + 1212 + /* Locking: called with .host_lock held */ 1213 + static int soc_camera_probe_finish(struct soc_camera_device *icd) 1214 + { 1215 + struct v4l2_subdev *sd = soc_camera_to_subdev(icd); 1216 + struct v4l2_mbus_framefmt mf; 1217 + int ret; 1218 + 1219 + sd->grp_id = soc_camera_grp_id(icd); 1220 + v4l2_set_subdev_hostdata(sd, icd); 1221 + 1222 + ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL); 1223 + if (ret < 0) 1224 + return ret; 1225 + 1226 + ret = soc_camera_add_device(icd); 1227 + if (ret < 0) { 1228 + dev_err(icd->pdev, "Couldn't activate the camera: %d\n", ret); 1229 + return ret; 1230 + } 1231 + 1232 + /* At this point client .probe() should have run already */ 1233 + ret = soc_camera_init_user_formats(icd); 1234 + if (ret < 0) 1235 + goto eusrfmt; 1236 + 1237 + icd->field = V4L2_FIELD_ANY; 1238 + 1239 + ret = soc_camera_video_start(icd); 1240 + if (ret < 0) 1241 + goto evidstart; 1242 + 1243 + /* Try to improve our guess of a reasonable window format */ 1244 + if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) { 1245 + icd->user_width = mf.width; 1246 + icd->user_height = mf.height; 1247 + icd->colorspace = mf.colorspace; 1248 + icd->field = mf.field; 1249 + } 1250 + soc_camera_remove_device(icd); 1251 + 1252 + return 0; 1253 + 1254 + evidstart: 1255 + soc_camera_free_user_formats(icd); 1256 + eusrfmt: 1257 + soc_camera_remove_device(icd); 1258 + 1259 + return ret; 1260 + } 1261 + 1219 1262 #ifdef CONFIG_I2C_BOARDINFO 1220 - static int soc_camera_init_i2c(struct soc_camera_device *icd, 1263 + static int soc_camera_i2c_init(struct soc_camera_device *icd, 1221 1264 struct soc_camera_desc *sdesc) 1222 1265 { 1223 1266 struct i2c_client *client; 1224 - struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1267 + struct soc_camera_host *ici; 1225 1268 struct soc_camera_host_desc *shd = &sdesc->host_desc; 1226 - struct i2c_adapter *adap = i2c_get_adapter(shd->i2c_adapter_id); 1269 + struct i2c_adapter *adap; 1227 1270 struct v4l2_subdev *subdev; 1228 1271 char clk_name[V4L2_SUBDEV_NAME_SIZE]; 1229 1272 int ret; 1230 1273 1274 + /* First find out how we link the main client */ 1275 + if (icd->sasc) { 1276 + /* Async non-OF probing handled by the subdevice list */ 1277 + return -EPROBE_DEFER; 1278 + } 1279 + 1280 + ici = to_soc_camera_host(icd->parent); 1281 + adap = i2c_get_adapter(shd->i2c_adapter_id); 1231 1282 if (!adap) { 1232 1283 dev_err(icd->pdev, "Cannot get I2C adapter #%d. No driver?\n", 1233 1284 shd->i2c_adapter_id); ··· 1359 1212 return 0; 1360 1213 ei2cnd: 1361 1214 v4l2_clk_unregister(icd->clk); 1362 - icd->clk = NULL; 1363 1215 eclkreg: 1216 + icd->clk = NULL; 1364 1217 i2c_put_adapter(adap); 1365 1218 return ret; 1366 1219 } 1367 1220 1368 - static void soc_camera_free_i2c(struct soc_camera_device *icd) 1221 + static void soc_camera_i2c_free(struct soc_camera_device *icd) 1369 1222 { 1370 1223 struct i2c_client *client = 1371 1224 to_i2c_client(to_soc_camera_control(icd)); 1372 - struct i2c_adapter *adap = client->adapter; 1225 + struct i2c_adapter *adap; 1373 1226 1374 1227 icd->control = NULL; 1228 + if (icd->sasc) 1229 + return; 1230 + 1231 + adap = client->adapter; 1375 1232 v4l2_device_unregister_subdev(i2c_get_clientdata(client)); 1376 1233 i2c_unregister_device(client); 1377 1234 i2c_put_adapter(adap); 1378 1235 v4l2_clk_unregister(icd->clk); 1379 1236 icd->clk = NULL; 1380 1237 } 1238 + 1239 + /* 1240 + * V4L2 asynchronous notifier callbacks. They are all called under a v4l2-async 1241 + * internal global mutex, therefore cannot race against other asynchronous 1242 + * events. Until notifier->complete() (soc_camera_async_complete()) is called, 1243 + * the video device node is not registered and no V4L fops can occur. Unloading 1244 + * of the host driver also calls a v4l2-async function, so also there we're 1245 + * protected. 1246 + */ 1247 + static int soc_camera_async_bound(struct v4l2_async_notifier *notifier, 1248 + struct v4l2_subdev *sd, 1249 + struct v4l2_async_subdev *asd) 1250 + { 1251 + struct soc_camera_async_client *sasc = container_of(notifier, 1252 + struct soc_camera_async_client, notifier); 1253 + struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev); 1254 + 1255 + if (asd == sasc->sensor && !WARN_ON(icd->control)) { 1256 + struct i2c_client *client = v4l2_get_subdevdata(sd); 1257 + 1258 + /* 1259 + * Only now we get subdevice-specific information like 1260 + * regulators, flags, callbacks, etc. 1261 + */ 1262 + if (client) { 1263 + struct soc_camera_desc *sdesc = to_soc_camera_desc(icd); 1264 + struct soc_camera_subdev_desc *ssdd = 1265 + soc_camera_i2c_to_desc(client); 1266 + if (ssdd) { 1267 + memcpy(&sdesc->subdev_desc, ssdd, 1268 + sizeof(sdesc->subdev_desc)); 1269 + if (ssdd->reset) 1270 + ssdd->reset(icd->pdev); 1271 + } 1272 + 1273 + icd->control = &client->dev; 1274 + } 1275 + } 1276 + 1277 + return 0; 1278 + } 1279 + 1280 + static void soc_camera_async_unbind(struct v4l2_async_notifier *notifier, 1281 + struct v4l2_subdev *sd, 1282 + struct v4l2_async_subdev *asd) 1283 + { 1284 + struct soc_camera_async_client *sasc = container_of(notifier, 1285 + struct soc_camera_async_client, notifier); 1286 + struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev); 1287 + 1288 + if (icd->clk) { 1289 + v4l2_clk_unregister(icd->clk); 1290 + icd->clk = NULL; 1291 + } 1292 + } 1293 + 1294 + static int soc_camera_async_complete(struct v4l2_async_notifier *notifier) 1295 + { 1296 + struct soc_camera_async_client *sasc = container_of(notifier, 1297 + struct soc_camera_async_client, notifier); 1298 + struct soc_camera_device *icd = platform_get_drvdata(sasc->pdev); 1299 + 1300 + if (to_soc_camera_control(icd)) { 1301 + struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1302 + int ret; 1303 + 1304 + mutex_lock(&list_lock); 1305 + ret = soc_camera_probe(ici, icd); 1306 + mutex_unlock(&list_lock); 1307 + if (ret < 0) 1308 + return ret; 1309 + } 1310 + 1311 + return 0; 1312 + } 1313 + 1314 + static int scan_async_group(struct soc_camera_host *ici, 1315 + struct v4l2_async_subdev **asd, int size) 1316 + { 1317 + struct soc_camera_async_subdev *sasd; 1318 + struct soc_camera_async_client *sasc; 1319 + struct soc_camera_device *icd; 1320 + struct soc_camera_desc sdesc = {.host_desc.bus_id = ici->nr,}; 1321 + char clk_name[V4L2_SUBDEV_NAME_SIZE]; 1322 + int ret, i; 1323 + 1324 + /* First look for a sensor */ 1325 + for (i = 0; i < size; i++) { 1326 + sasd = container_of(asd[i], struct soc_camera_async_subdev, asd); 1327 + if (sasd->role == SOCAM_SUBDEV_DATA_SOURCE) 1328 + break; 1329 + } 1330 + 1331 + if (i == size || asd[i]->bus_type != V4L2_ASYNC_BUS_I2C) { 1332 + /* All useless */ 1333 + dev_err(ici->v4l2_dev.dev, "No I2C data source found!\n"); 1334 + return -ENODEV; 1335 + } 1336 + 1337 + /* Or shall this be managed by the soc-camera device? */ 1338 + sasc = devm_kzalloc(ici->v4l2_dev.dev, sizeof(*sasc), GFP_KERNEL); 1339 + if (!sasc) 1340 + return -ENOMEM; 1341 + 1342 + /* HACK: just need a != NULL */ 1343 + sdesc.host_desc.board_info = ERR_PTR(-ENODATA); 1344 + 1345 + ret = soc_camera_dyn_pdev(&sdesc, sasc); 1346 + if (ret < 0) 1347 + return ret; 1348 + 1349 + sasc->sensor = &sasd->asd; 1350 + 1351 + icd = soc_camera_add_pdev(sasc); 1352 + if (!icd) { 1353 + platform_device_put(sasc->pdev); 1354 + return -ENOMEM; 1355 + } 1356 + 1357 + sasc->notifier.subdev = asd; 1358 + sasc->notifier.num_subdevs = size; 1359 + sasc->notifier.bound = soc_camera_async_bound; 1360 + sasc->notifier.unbind = soc_camera_async_unbind; 1361 + sasc->notifier.complete = soc_camera_async_complete; 1362 + 1363 + icd->sasc = sasc; 1364 + icd->parent = ici->v4l2_dev.dev; 1365 + 1366 + snprintf(clk_name, sizeof(clk_name), "%d-%04x", 1367 + sasd->asd.match.i2c.adapter_id, sasd->asd.match.i2c.address); 1368 + 1369 + icd->clk = v4l2_clk_register(&soc_camera_clk_ops, clk_name, "mclk", icd); 1370 + if (IS_ERR(icd->clk)) { 1371 + ret = PTR_ERR(icd->clk); 1372 + goto eclkreg; 1373 + } 1374 + 1375 + ret = v4l2_async_notifier_register(&ici->v4l2_dev, &sasc->notifier); 1376 + if (!ret) 1377 + return 0; 1378 + 1379 + v4l2_clk_unregister(icd->clk); 1380 + eclkreg: 1381 + icd->clk = NULL; 1382 + platform_device_unregister(sasc->pdev); 1383 + dev_err(ici->v4l2_dev.dev, "group probe failed: %d\n", ret); 1384 + 1385 + return ret; 1386 + } 1387 + 1388 + static void scan_async_host(struct soc_camera_host *ici) 1389 + { 1390 + struct v4l2_async_subdev **asd; 1391 + int j; 1392 + 1393 + for (j = 0, asd = ici->asd; ici->asd_sizes[j]; j++) { 1394 + scan_async_group(ici, asd, ici->asd_sizes[j]); 1395 + asd += ici->asd_sizes[j]; 1396 + } 1397 + } 1381 1398 #else 1382 - #define soc_camera_init_i2c(icd, sdesc) (-ENODEV) 1383 - #define soc_camera_free_i2c(icd) do {} while (0) 1399 + #define soc_camera_i2c_init(icd, sdesc) (-ENODEV) 1400 + #define soc_camera_i2c_free(icd) do {} while (0) 1401 + #define scan_async_host(ici) do {} while (0) 1384 1402 #endif 1385 1403 1386 - static int soc_camera_video_start(struct soc_camera_device *icd); 1387 - static int video_dev_create(struct soc_camera_device *icd); 1388 1404 /* Called during host-driver probe */ 1389 - static int soc_camera_probe(struct soc_camera_device *icd) 1405 + static int soc_camera_probe(struct soc_camera_host *ici, 1406 + struct soc_camera_device *icd) 1390 1407 { 1391 - struct soc_camera_host *ici = to_soc_camera_host(icd->parent); 1392 1408 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd); 1393 1409 struct soc_camera_host_desc *shd = &sdesc->host_desc; 1394 - struct soc_camera_subdev_desc *ssdd = &sdesc->subdev_desc; 1395 1410 struct device *control = NULL; 1396 - struct v4l2_subdev *sd; 1397 - struct v4l2_mbus_framefmt mf; 1398 1411 int ret; 1399 1412 1400 1413 dev_info(icd->pdev, "Probing %s\n", dev_name(icd->pdev)); ··· 1570 1263 if (ret < 0) 1571 1264 return ret; 1572 1265 1573 - /* The camera could have been already on, try to reset */ 1574 - if (ssdd->reset) 1575 - ssdd->reset(icd->pdev); 1576 - 1577 1266 /* Must have icd->vdev before registering the device */ 1578 1267 ret = video_dev_create(icd); 1579 1268 if (ret < 0) ··· 1580 1277 * itself is protected against concurrent open() calls, but we also have 1581 1278 * to protect our data also during client probing. 1582 1279 */ 1583 - mutex_lock(&ici->host_lock); 1584 1280 1585 1281 /* Non-i2c cameras, e.g., soc_camera_platform, have no board_info */ 1586 1282 if (shd->board_info) { 1587 - ret = soc_camera_init_i2c(icd, sdesc); 1588 - if (ret < 0) 1283 + ret = soc_camera_i2c_init(icd, sdesc); 1284 + if (ret < 0 && ret != -EPROBE_DEFER) 1589 1285 goto eadd; 1590 1286 } else if (!shd->add_device || !shd->del_device) { 1591 1287 ret = -EINVAL; 1592 1288 goto eadd; 1593 1289 } else { 1290 + mutex_lock(&ici->clk_lock); 1594 1291 ret = ici->ops->clock_start(ici); 1292 + mutex_unlock(&ici->clk_lock); 1595 1293 if (ret < 0) 1596 1294 goto eadd; 1597 1295 ··· 1616 1312 } 1617 1313 } 1618 1314 1619 - sd = soc_camera_to_subdev(icd); 1620 - sd->grp_id = soc_camera_grp_id(icd); 1621 - v4l2_set_subdev_hostdata(sd, icd); 1622 - 1623 - ret = v4l2_ctrl_add_handler(&icd->ctrl_handler, sd->ctrl_handler, NULL); 1624 - if (ret < 0) 1625 - goto ectrl; 1626 - 1627 - /* At this point client .probe() should have run already */ 1628 - ret = soc_camera_init_user_formats(icd); 1629 - if (ret < 0) 1630 - goto eiufmt; 1631 - 1632 - icd->field = V4L2_FIELD_ANY; 1633 - 1634 - ret = soc_camera_video_start(icd); 1635 - if (ret < 0) 1636 - goto evidstart; 1637 - 1638 - /* Try to improve our guess of a reasonable window format */ 1639 - if (!v4l2_subdev_call(sd, video, g_mbus_fmt, &mf)) { 1640 - icd->user_width = mf.width; 1641 - icd->user_height = mf.height; 1642 - icd->colorspace = mf.colorspace; 1643 - icd->field = mf.field; 1644 - } 1645 - 1646 - if (!shd->board_info) 1647 - ici->ops->clock_stop(ici); 1648 - 1315 + mutex_lock(&ici->host_lock); 1316 + ret = soc_camera_probe_finish(icd); 1649 1317 mutex_unlock(&ici->host_lock); 1318 + if (ret < 0) 1319 + goto efinish; 1650 1320 1651 1321 return 0; 1652 1322 1653 - evidstart: 1654 - soc_camera_free_user_formats(icd); 1655 - eiufmt: 1656 - ectrl: 1323 + efinish: 1657 1324 if (shd->board_info) { 1658 - soc_camera_free_i2c(icd); 1325 + soc_camera_i2c_free(icd); 1659 1326 } else { 1660 1327 shd->del_device(icd); 1661 1328 module_put(control->driver->owner); 1662 1329 enodrv: 1663 1330 eadddev: 1331 + mutex_lock(&ici->clk_lock); 1664 1332 ici->ops->clock_stop(ici); 1333 + mutex_unlock(&ici->clk_lock); 1665 1334 } 1666 1335 eadd: 1667 1336 video_device_release(icd->vdev); 1668 1337 icd->vdev = NULL; 1669 - mutex_unlock(&ici->host_lock); 1338 + if (icd->vdev) { 1339 + video_device_release(icd->vdev); 1340 + icd->vdev = NULL; 1341 + } 1670 1342 evdc: 1671 1343 v4l2_ctrl_handler_free(&icd->ctrl_handler); 1672 1344 return ret; ··· 1650 1370 1651 1371 /* 1652 1372 * This is called on device_unregister, which only means we have to disconnect 1653 - * from the host, but not remove ourselves from the device list 1373 + * from the host, but not remove ourselves from the device list. With 1374 + * asynchronous client probing this can also be called without 1375 + * soc_camera_probe_finish() having run. Careful with clean up. 1654 1376 */ 1655 1377 static int soc_camera_remove(struct soc_camera_device *icd) 1656 1378 { 1657 1379 struct soc_camera_desc *sdesc = to_soc_camera_desc(icd); 1658 1380 struct video_device *vdev = icd->vdev; 1659 - 1660 - BUG_ON(!icd->parent); 1661 1381 1662 1382 v4l2_ctrl_handler_free(&icd->ctrl_handler); 1663 1383 if (vdev) { ··· 1666 1386 } 1667 1387 1668 1388 if (sdesc->host_desc.board_info) { 1669 - soc_camera_free_i2c(icd); 1389 + soc_camera_i2c_free(icd); 1670 1390 } else { 1671 - struct device_driver *drv = to_soc_camera_control(icd)->driver; 1391 + struct device *dev = to_soc_camera_control(icd); 1392 + struct device_driver *drv = dev ? dev->driver : NULL; 1672 1393 if (drv) { 1673 1394 sdesc->host_desc.del_device(icd); 1674 1395 module_put(drv->owner); 1675 1396 } 1676 1397 } 1677 - soc_camera_free_user_formats(icd); 1398 + 1399 + if (icd->num_user_formats) 1400 + soc_camera_free_user_formats(icd); 1401 + 1402 + if (icd->clk) { 1403 + /* For the synchronous case */ 1404 + v4l2_clk_unregister(icd->clk); 1405 + icd->clk = NULL; 1406 + } 1407 + 1408 + if (icd->sasc) 1409 + platform_device_unregister(icd->sasc->pdev); 1678 1410 1679 1411 return 0; 1680 1412 } ··· 1797 1505 mutex_unlock(&list_lock); 1798 1506 1799 1507 mutex_init(&ici->host_lock); 1800 - scan_add_host(ici); 1508 + mutex_init(&ici->clk_lock); 1509 + 1510 + if (ici->asd_sizes) 1511 + /* 1512 + * No OF, host with a list of subdevices. Don't try to mix 1513 + * modes by initialising some groups statically and some 1514 + * dynamically! 1515 + */ 1516 + scan_async_host(ici); 1517 + else 1518 + /* Legacy: static platform devices from board data */ 1519 + scan_add_host(ici); 1801 1520 1802 1521 return 0; 1803 1522 ··· 1821 1518 /* Unregister all clients! */ 1822 1519 void soc_camera_host_unregister(struct soc_camera_host *ici) 1823 1520 { 1824 - struct soc_camera_device *icd; 1521 + struct soc_camera_device *icd, *tmp; 1522 + struct soc_camera_async_client *sasc; 1523 + LIST_HEAD(notifiers); 1524 + 1525 + mutex_lock(&list_lock); 1526 + list_del(&ici->list); 1527 + list_for_each_entry(icd, &devices, list) 1528 + if (icd->iface == ici->nr && icd->sasc) { 1529 + /* as long as we hold the device, sasc won't be freed */ 1530 + get_device(icd->pdev); 1531 + list_add(&icd->sasc->list, &notifiers); 1532 + } 1533 + mutex_unlock(&list_lock); 1534 + 1535 + list_for_each_entry(sasc, &notifiers, list) { 1536 + /* Must call unlocked to avoid AB-BA dead-lock */ 1537 + v4l2_async_notifier_unregister(&sasc->notifier); 1538 + put_device(&sasc->pdev->dev); 1539 + } 1825 1540 1826 1541 mutex_lock(&list_lock); 1827 1542 1828 - list_del(&ici->list); 1829 - list_for_each_entry(icd, &devices, list) 1830 - if (icd->iface == ici->nr && to_soc_camera_control(icd)) 1543 + list_for_each_entry_safe(icd, tmp, &devices, list) 1544 + if (icd->iface == ici->nr) 1831 1545 soc_camera_remove(icd); 1832 1546 1833 1547 mutex_unlock(&list_lock); ··· 1859 1539 struct soc_camera_device *ix; 1860 1540 int num = -1, i; 1861 1541 1542 + mutex_lock(&list_lock); 1862 1543 for (i = 0; i < 256 && num < 0; i++) { 1863 1544 num = i; 1864 1545 /* Check if this index is available on this interface */ ··· 1871 1550 } 1872 1551 } 1873 1552 1874 - if (num < 0) 1553 + if (num < 0) { 1875 1554 /* 1876 1555 * ok, we have 256 cameras on this host... 1877 1556 * man, stay reasonable... 1878 1557 */ 1558 + mutex_unlock(&list_lock); 1879 1559 return -ENOMEM; 1560 + } 1880 1561 1881 1562 icd->devnum = num; 1882 1563 icd->use_count = 0; 1883 1564 icd->host_priv = NULL; 1884 1565 1566 + /* 1567 + * Dynamically allocated devices set the bit earlier, but it doesn't hurt setting 1568 + * it again 1569 + */ 1570 + i = to_platform_device(icd->pdev)->id; 1571 + if (i < 0) 1572 + /* One static (legacy) soc-camera platform device */ 1573 + i = 0; 1574 + if (i >= MAP_MAX_NUM) { 1575 + mutex_unlock(&list_lock); 1576 + return -EBUSY; 1577 + } 1578 + set_bit(i, device_map); 1885 1579 list_add_tail(&icd->list, &devices); 1580 + mutex_unlock(&list_lock); 1886 1581 1887 1582 return 0; 1888 1583 } ··· 1992 1655 if (!icd) 1993 1656 return -ENOMEM; 1994 1657 1658 + /* 1659 + * In the asynchronous case ssdd->num_regulators == 0 yet, so, the below 1660 + * regulator allocation is a dummy. They will be really requested later 1661 + * in soc_camera_async_bind(). Also note, that in that case regulators 1662 + * are attached to the I2C device and not to the camera platform device. 1663 + */ 1995 1664 ret = devm_regulator_bulk_get(&pdev->dev, ssdd->num_regulators, 1996 1665 ssdd->regulators); 1997 1666 if (ret < 0) ··· 2022 1679 static int soc_camera_pdrv_remove(struct platform_device *pdev) 2023 1680 { 2024 1681 struct soc_camera_device *icd = platform_get_drvdata(pdev); 1682 + int i; 2025 1683 2026 1684 if (!icd) 2027 1685 return -EINVAL; 2028 1686 2029 - list_del(&icd->list); 1687 + i = pdev->id; 1688 + if (i < 0) 1689 + i = 0; 1690 + 1691 + /* 1692 + * In synchronous mode with static platform devices this is called in a 1693 + * loop from drivers/base/dd.c::driver_detach(), no parallel execution, 1694 + * no need to lock. In asynchronous case the caller - 1695 + * soc_camera_host_unregister() - already holds the lock 1696 + */ 1697 + if (test_bit(i, device_map)) { 1698 + clear_bit(i, device_map); 1699 + list_del(&icd->list); 1700 + } 2030 1701 2031 1702 return 0; 2032 1703 }
+22 -1
include/media/soc_camera.h
··· 19 19 #include <linux/videodev2.h> 20 20 #include <media/videobuf-core.h> 21 21 #include <media/videobuf2-core.h> 22 + #include <media/v4l2-async.h> 22 23 #include <media/v4l2-ctrls.h> 23 24 #include <media/v4l2-device.h> 24 25 25 26 struct file; 26 27 struct soc_camera_desc; 28 + struct soc_camera_async_client; 27 29 28 30 struct soc_camera_device { 29 31 struct list_head list; /* list of all registered devices */ ··· 52 50 int use_count; 53 51 struct file *streamer; /* stream owner */ 54 52 struct v4l2_clk *clk; 53 + /* Asynchronous subdevice management */ 54 + struct soc_camera_async_client *sasc; 55 + /* video buffer queue */ 55 56 union { 56 57 struct videobuf_queue vb_vidq; 57 58 struct vb2_queue vb2_vidq; ··· 64 59 /* Host supports programmable stride */ 65 60 #define SOCAM_HOST_CAP_STRIDE (1 << 0) 66 61 62 + enum soc_camera_subdev_role { 63 + SOCAM_SUBDEV_DATA_SOURCE = 1, 64 + SOCAM_SUBDEV_DATA_SINK, 65 + SOCAM_SUBDEV_DATA_PROCESSOR, 66 + }; 67 + 68 + struct soc_camera_async_subdev { 69 + struct v4l2_async_subdev asd; 70 + enum soc_camera_subdev_role role; 71 + }; 72 + 67 73 struct soc_camera_host { 68 74 struct v4l2_device v4l2_dev; 69 75 struct list_head list; 70 - struct mutex host_lock; /* Protect pipeline modifications */ 76 + struct mutex host_lock; /* Main synchronisation lock */ 77 + struct mutex clk_lock; /* Protect pipeline modifications */ 71 78 unsigned char nr; /* Host number */ 72 79 u32 capabilities; 73 80 struct soc_camera_device *icd; /* Currently attached client */ 74 81 void *priv; 75 82 const char *drv_name; 76 83 struct soc_camera_host_ops *ops; 84 + struct v4l2_async_subdev **asd; /* Flat array, arranged in groups */ 85 + int *asd_sizes; /* 0-terminated array of asd group sizes */ 77 86 }; 78 87 79 88 struct soc_camera_host_ops { ··· 180 161 }; 181 162 182 163 /* 164 + * Platform data for "soc-camera-pdrv" 183 165 * This MUST be kept binary-identical to struct soc_camera_link below, until 184 166 * it is completely replaced by this one, after which we can split it into its 185 167 * two components. ··· 346 326 unsigned long soc_camera_apply_board_flags(struct soc_camera_subdev_desc *ssdd, 347 327 const struct v4l2_mbus_config *cfg); 348 328 329 + int soc_camera_power_init(struct device *dev, struct soc_camera_subdev_desc *ssdd); 349 330 int soc_camera_power_on(struct device *dev, struct soc_camera_subdev_desc *ssdd, 350 331 struct v4l2_clk *clk); 351 332 int soc_camera_power_off(struct device *dev, struct soc_camera_subdev_desc *ssdd,