rcar-core.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for Renesas R-Car VIN
  4. *
  5. * Copyright (C) 2016 Renesas Electronics Corp.
  6. * Copyright (C) 2011-2013 Renesas Solutions Corp.
  7. * Copyright (C) 2013 Cogent Embedded, Inc., <source@cogentembedded.com>
  8. * Copyright (C) 2008 Magnus Damm
  9. *
  10. * Based on the soc-camera rcar_vin driver
  11. */
  12. #include <linux/module.h>
  13. #include <linux/of.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_graph.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/pm_runtime.h>
  18. #include <linux/slab.h>
  19. #include <linux/sys_soc.h>
  20. #include <media/v4l2-async.h>
  21. #include <media/v4l2-fwnode.h>
  22. #include <media/v4l2-mc.h>
  23. #include "rcar-vin.h"
  24. /*
  25. * The companion CSI-2 receiver driver (rcar-csi2) is known
  26. * and we know it has one source pad (pad 0) and four sink
  27. * pads (pad 1-4). So to translate a pad on the remote
  28. * CSI-2 receiver to/from the VIN internal channel number simply
  29. * subtract/add one from the pad/channel number.
  30. */
  31. #define rvin_group_csi_pad_to_channel(pad) ((pad) - 1)
  32. #define rvin_group_csi_channel_to_pad(channel) ((channel) + 1)
  33. /*
  34. * Not all VINs are created equal, master VINs control the
  35. * routing for other VIN's. We can figure out which VIN is
  36. * master by looking at a VINs id.
  37. */
  38. #define rvin_group_id_to_master(vin) ((vin) < 4 ? 0 : 4)
  39. #define v4l2_dev_to_vin(d) container_of(d, struct rvin_dev, v4l2_dev)
  40. /* -----------------------------------------------------------------------------
  41. * Media Controller link notification
  42. */
  43. /* group lock should be held when calling this function. */
  44. static int rvin_group_entity_to_csi_id(struct rvin_group *group,
  45. struct media_entity *entity)
  46. {
  47. struct v4l2_subdev *sd;
  48. unsigned int i;
  49. sd = media_entity_to_v4l2_subdev(entity);
  50. for (i = 0; i < RVIN_CSI_MAX; i++)
  51. if (group->csi[i].subdev == sd)
  52. return i;
  53. return -ENODEV;
  54. }
  55. static unsigned int rvin_group_get_mask(struct rvin_dev *vin,
  56. enum rvin_csi_id csi_id,
  57. unsigned char channel)
  58. {
  59. const struct rvin_group_route *route;
  60. unsigned int mask = 0;
  61. for (route = vin->info->routes; route->mask; route++) {
  62. if (route->vin == vin->id &&
  63. route->csi == csi_id &&
  64. route->channel == channel) {
  65. vin_dbg(vin,
  66. "Adding route: vin: %d csi: %d channel: %d\n",
  67. route->vin, route->csi, route->channel);
  68. mask |= route->mask;
  69. }
  70. }
  71. return mask;
  72. }
  73. /*
  74. * Link setup for the links between a VIN and a CSI-2 receiver is a bit
  75. * complex. The reason for this is that the register controlling routing
  76. * is not present in each VIN instance. There are special VINs which
  77. * control routing for themselves and other VINs. There are not many
  78. * different possible links combinations that can be enabled at the same
  79. * time, therefor all already enabled links which are controlled by a
  80. * master VIN need to be taken into account when making the decision
  81. * if a new link can be enabled or not.
  82. *
  83. * 1. Find out which VIN the link the user tries to enable is connected to.
  84. * 2. Lookup which master VIN controls the links for this VIN.
  85. * 3. Start with a bitmask with all bits set.
  86. * 4. For each previously enabled link from the master VIN bitwise AND its
  87. * route mask (see documentation for mask in struct rvin_group_route)
  88. * with the bitmask.
  89. * 5. Bitwise AND the mask for the link the user tries to enable to the bitmask.
  90. * 6. If the bitmask is not empty at this point the new link can be enabled
  91. * while keeping all previous links enabled. Update the CHSEL value of the
  92. * master VIN and inform the user that the link could be enabled.
  93. *
  94. * Please note that no link can be enabled if any VIN in the group is
  95. * currently open.
  96. */
  97. static int rvin_group_link_notify(struct media_link *link, u32 flags,
  98. unsigned int notification)
  99. {
  100. struct rvin_group *group = container_of(link->graph_obj.mdev,
  101. struct rvin_group, mdev);
  102. unsigned int master_id, channel, mask_new, i;
  103. unsigned int mask = ~0;
  104. struct media_entity *entity;
  105. struct video_device *vdev;
  106. struct media_pad *csi_pad;
  107. struct rvin_dev *vin = NULL;
  108. int csi_id, ret;
  109. ret = v4l2_pipeline_link_notify(link, flags, notification);
  110. if (ret)
  111. return ret;
  112. /* Only care about link enablement for VIN nodes. */
  113. if (!(flags & MEDIA_LNK_FL_ENABLED) ||
  114. !is_media_entity_v4l2_video_device(link->sink->entity))
  115. return 0;
  116. /*
  117. * Don't allow link changes if any entity in the graph is
  118. * streaming, modifying the CHSEL register fields can disrupt
  119. * running streams.
  120. */
  121. media_device_for_each_entity(entity, &group->mdev)
  122. if (entity->stream_count)
  123. return -EBUSY;
  124. mutex_lock(&group->lock);
  125. /* Find the master VIN that controls the routes. */
  126. vdev = media_entity_to_video_device(link->sink->entity);
  127. vin = container_of(vdev, struct rvin_dev, vdev);
  128. master_id = rvin_group_id_to_master(vin->id);
  129. if (WARN_ON(!group->vin[master_id])) {
  130. ret = -ENODEV;
  131. goto out;
  132. }
  133. /* Build a mask for already enabled links. */
  134. for (i = master_id; i < master_id + 4; i++) {
  135. if (!group->vin[i])
  136. continue;
  137. /* Get remote CSI-2, if any. */
  138. csi_pad = media_entity_remote_pad(
  139. &group->vin[i]->vdev.entity.pads[0]);
  140. if (!csi_pad)
  141. continue;
  142. csi_id = rvin_group_entity_to_csi_id(group, csi_pad->entity);
  143. channel = rvin_group_csi_pad_to_channel(csi_pad->index);
  144. mask &= rvin_group_get_mask(group->vin[i], csi_id, channel);
  145. }
  146. /* Add the new link to the existing mask and check if it works. */
  147. csi_id = rvin_group_entity_to_csi_id(group, link->source->entity);
  148. if (csi_id == -ENODEV) {
  149. struct v4l2_subdev *sd;
  150. /*
  151. * Make sure the source entity subdevice is registered as
  152. * a parallel input of one of the enabled VINs if it is not
  153. * one of the CSI-2 subdevices.
  154. *
  155. * No hardware configuration required for parallel inputs,
  156. * we can return here.
  157. */
  158. sd = media_entity_to_v4l2_subdev(link->source->entity);
  159. for (i = 0; i < RCAR_VIN_NUM; i++) {
  160. if (group->vin[i] && group->vin[i]->parallel &&
  161. group->vin[i]->parallel->subdev == sd) {
  162. group->vin[i]->is_csi = false;
  163. ret = 0;
  164. goto out;
  165. }
  166. }
  167. vin_err(vin, "Subdevice %s not registered to any VIN\n",
  168. link->source->entity->name);
  169. ret = -ENODEV;
  170. goto out;
  171. }
  172. channel = rvin_group_csi_pad_to_channel(link->source->index);
  173. mask_new = mask & rvin_group_get_mask(vin, csi_id, channel);
  174. vin_dbg(vin, "Try link change mask: 0x%x new: 0x%x\n", mask, mask_new);
  175. if (!mask_new) {
  176. ret = -EMLINK;
  177. goto out;
  178. }
  179. /* New valid CHSEL found, set the new value. */
  180. ret = rvin_set_channel_routing(group->vin[master_id], __ffs(mask_new));
  181. if (ret)
  182. goto out;
  183. vin->is_csi = true;
  184. out:
  185. mutex_unlock(&group->lock);
  186. return ret;
  187. }
  188. static const struct media_device_ops rvin_media_ops = {
  189. .link_notify = rvin_group_link_notify,
  190. };
  191. /* -----------------------------------------------------------------------------
  192. * Gen3 CSI2 Group Allocator
  193. */
  194. /* FIXME: This should if we find a system that supports more
  195. * than one group for the whole system be replaced with a linked
  196. * list of groups. And eventually all of this should be replaced
  197. * with a global device allocator API.
  198. *
  199. * But for now this works as on all supported systems there will
  200. * be only one group for all instances.
  201. */
  202. static DEFINE_MUTEX(rvin_group_lock);
  203. static struct rvin_group *rvin_group_data;
  204. static void rvin_group_cleanup(struct rvin_group *group)
  205. {
  206. media_device_unregister(&group->mdev);
  207. media_device_cleanup(&group->mdev);
  208. mutex_destroy(&group->lock);
  209. }
  210. static int rvin_group_init(struct rvin_group *group, struct rvin_dev *vin)
  211. {
  212. struct media_device *mdev = &group->mdev;
  213. const struct of_device_id *match;
  214. struct device_node *np;
  215. int ret;
  216. mutex_init(&group->lock);
  217. /* Count number of VINs in the system */
  218. group->count = 0;
  219. for_each_matching_node(np, vin->dev->driver->of_match_table)
  220. if (of_device_is_available(np))
  221. group->count++;
  222. vin_dbg(vin, "found %u enabled VIN's in DT", group->count);
  223. mdev->dev = vin->dev;
  224. mdev->ops = &rvin_media_ops;
  225. match = of_match_node(vin->dev->driver->of_match_table,
  226. vin->dev->of_node);
  227. strscpy(mdev->driver_name, KBUILD_MODNAME, sizeof(mdev->driver_name));
  228. strscpy(mdev->model, match->compatible, sizeof(mdev->model));
  229. snprintf(mdev->bus_info, sizeof(mdev->bus_info), "platform:%s",
  230. dev_name(mdev->dev));
  231. media_device_init(mdev);
  232. ret = media_device_register(&group->mdev);
  233. if (ret)
  234. rvin_group_cleanup(group);
  235. return ret;
  236. }
  237. static void rvin_group_release(struct kref *kref)
  238. {
  239. struct rvin_group *group =
  240. container_of(kref, struct rvin_group, refcount);
  241. mutex_lock(&rvin_group_lock);
  242. rvin_group_data = NULL;
  243. rvin_group_cleanup(group);
  244. kfree(group);
  245. mutex_unlock(&rvin_group_lock);
  246. }
  247. static int rvin_group_get(struct rvin_dev *vin)
  248. {
  249. struct rvin_group *group;
  250. u32 id;
  251. int ret;
  252. /* Make sure VIN id is present and sane */
  253. ret = of_property_read_u32(vin->dev->of_node, "renesas,id", &id);
  254. if (ret) {
  255. vin_err(vin, "%pOF: No renesas,id property found\n",
  256. vin->dev->of_node);
  257. return -EINVAL;
  258. }
  259. if (id >= RCAR_VIN_NUM) {
  260. vin_err(vin, "%pOF: Invalid renesas,id '%u'\n",
  261. vin->dev->of_node, id);
  262. return -EINVAL;
  263. }
  264. /* Join or create a VIN group */
  265. mutex_lock(&rvin_group_lock);
  266. if (rvin_group_data) {
  267. group = rvin_group_data;
  268. kref_get(&group->refcount);
  269. } else {
  270. group = kzalloc(sizeof(*group), GFP_KERNEL);
  271. if (!group) {
  272. ret = -ENOMEM;
  273. goto err_group;
  274. }
  275. ret = rvin_group_init(group, vin);
  276. if (ret) {
  277. kfree(group);
  278. vin_err(vin, "Failed to initialize group\n");
  279. goto err_group;
  280. }
  281. kref_init(&group->refcount);
  282. rvin_group_data = group;
  283. }
  284. mutex_unlock(&rvin_group_lock);
  285. /* Add VIN to group */
  286. mutex_lock(&group->lock);
  287. if (group->vin[id]) {
  288. vin_err(vin, "Duplicate renesas,id property value %u\n", id);
  289. mutex_unlock(&group->lock);
  290. kref_put(&group->refcount, rvin_group_release);
  291. return -EINVAL;
  292. }
  293. group->vin[id] = vin;
  294. vin->id = id;
  295. vin->group = group;
  296. vin->v4l2_dev.mdev = &group->mdev;
  297. mutex_unlock(&group->lock);
  298. return 0;
  299. err_group:
  300. mutex_unlock(&rvin_group_lock);
  301. return ret;
  302. }
  303. static void rvin_group_put(struct rvin_dev *vin)
  304. {
  305. struct rvin_group *group = vin->group;
  306. mutex_lock(&group->lock);
  307. vin->group = NULL;
  308. vin->v4l2_dev.mdev = NULL;
  309. if (WARN_ON(group->vin[vin->id] != vin))
  310. goto out;
  311. group->vin[vin->id] = NULL;
  312. out:
  313. mutex_unlock(&group->lock);
  314. kref_put(&group->refcount, rvin_group_release);
  315. }
  316. /* -----------------------------------------------------------------------------
  317. * Controls
  318. */
  319. static int rvin_s_ctrl(struct v4l2_ctrl *ctrl)
  320. {
  321. struct rvin_dev *vin =
  322. container_of(ctrl->handler, struct rvin_dev, ctrl_handler);
  323. switch (ctrl->id) {
  324. case V4L2_CID_ALPHA_COMPONENT:
  325. rvin_set_alpha(vin, ctrl->val);
  326. break;
  327. }
  328. return 0;
  329. }
  330. static const struct v4l2_ctrl_ops rvin_ctrl_ops = {
  331. .s_ctrl = rvin_s_ctrl,
  332. };
  333. /* -----------------------------------------------------------------------------
  334. * Async notifier
  335. */
  336. static int rvin_find_pad(struct v4l2_subdev *sd, int direction)
  337. {
  338. unsigned int pad;
  339. if (sd->entity.num_pads <= 1)
  340. return 0;
  341. for (pad = 0; pad < sd->entity.num_pads; pad++)
  342. if (sd->entity.pads[pad].flags & direction)
  343. return pad;
  344. return -EINVAL;
  345. }
  346. /* -----------------------------------------------------------------------------
  347. * Parallel async notifier
  348. */
  349. /* The vin lock should be held when calling the subdevice attach and detach */
  350. static int rvin_parallel_subdevice_attach(struct rvin_dev *vin,
  351. struct v4l2_subdev *subdev)
  352. {
  353. struct v4l2_subdev_mbus_code_enum code = {
  354. .which = V4L2_SUBDEV_FORMAT_ACTIVE,
  355. };
  356. int ret;
  357. /* Find source and sink pad of remote subdevice */
  358. ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SOURCE);
  359. if (ret < 0)
  360. return ret;
  361. vin->parallel->source_pad = ret;
  362. ret = rvin_find_pad(subdev, MEDIA_PAD_FL_SINK);
  363. vin->parallel->sink_pad = ret < 0 ? 0 : ret;
  364. if (vin->info->use_mc) {
  365. vin->parallel->subdev = subdev;
  366. return 0;
  367. }
  368. /* Find compatible subdevices mbus format */
  369. vin->mbus_code = 0;
  370. code.index = 0;
  371. code.pad = vin->parallel->source_pad;
  372. while (!vin->mbus_code &&
  373. !v4l2_subdev_call(subdev, pad, enum_mbus_code, NULL, &code)) {
  374. code.index++;
  375. switch (code.code) {
  376. case MEDIA_BUS_FMT_YUYV8_1X16:
  377. case MEDIA_BUS_FMT_UYVY8_1X16:
  378. case MEDIA_BUS_FMT_UYVY8_2X8:
  379. case MEDIA_BUS_FMT_UYVY10_2X10:
  380. case MEDIA_BUS_FMT_RGB888_1X24:
  381. vin->mbus_code = code.code;
  382. vin_dbg(vin, "Found media bus format for %s: %d\n",
  383. subdev->name, vin->mbus_code);
  384. break;
  385. default:
  386. break;
  387. }
  388. }
  389. if (!vin->mbus_code) {
  390. vin_err(vin, "Unsupported media bus format for %s\n",
  391. subdev->name);
  392. return -EINVAL;
  393. }
  394. /* Read tvnorms */
  395. ret = v4l2_subdev_call(subdev, video, g_tvnorms, &vin->vdev.tvnorms);
  396. if (ret < 0 && ret != -ENOIOCTLCMD && ret != -ENODEV)
  397. return ret;
  398. /* Read standard */
  399. vin->std = V4L2_STD_UNKNOWN;
  400. ret = v4l2_subdev_call(subdev, video, g_std, &vin->std);
  401. if (ret < 0 && ret != -ENOIOCTLCMD)
  402. return ret;
  403. /* Add the controls */
  404. ret = v4l2_ctrl_handler_init(&vin->ctrl_handler, 16);
  405. if (ret < 0)
  406. return ret;
  407. v4l2_ctrl_new_std(&vin->ctrl_handler, &rvin_ctrl_ops,
  408. V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
  409. if (vin->ctrl_handler.error) {
  410. ret = vin->ctrl_handler.error;
  411. v4l2_ctrl_handler_free(&vin->ctrl_handler);
  412. return ret;
  413. }
  414. ret = v4l2_ctrl_add_handler(&vin->ctrl_handler, subdev->ctrl_handler,
  415. NULL, true);
  416. if (ret < 0) {
  417. v4l2_ctrl_handler_free(&vin->ctrl_handler);
  418. return ret;
  419. }
  420. vin->vdev.ctrl_handler = &vin->ctrl_handler;
  421. vin->parallel->subdev = subdev;
  422. return 0;
  423. }
  424. static void rvin_parallel_subdevice_detach(struct rvin_dev *vin)
  425. {
  426. rvin_v4l2_unregister(vin);
  427. vin->parallel->subdev = NULL;
  428. if (!vin->info->use_mc) {
  429. v4l2_ctrl_handler_free(&vin->ctrl_handler);
  430. vin->vdev.ctrl_handler = NULL;
  431. }
  432. }
  433. static int rvin_parallel_notify_complete(struct v4l2_async_notifier *notifier)
  434. {
  435. struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
  436. struct media_entity *source;
  437. struct media_entity *sink;
  438. int ret;
  439. ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
  440. if (ret < 0) {
  441. vin_err(vin, "Failed to register subdev nodes\n");
  442. return ret;
  443. }
  444. if (!video_is_registered(&vin->vdev)) {
  445. ret = rvin_v4l2_register(vin);
  446. if (ret < 0)
  447. return ret;
  448. }
  449. if (!vin->info->use_mc)
  450. return 0;
  451. /* If we're running with media-controller, link the subdevs. */
  452. source = &vin->parallel->subdev->entity;
  453. sink = &vin->vdev.entity;
  454. ret = media_create_pad_link(source, vin->parallel->source_pad,
  455. sink, vin->parallel->sink_pad, 0);
  456. if (ret)
  457. vin_err(vin, "Error adding link from %s to %s: %d\n",
  458. source->name, sink->name, ret);
  459. return ret;
  460. }
  461. static void rvin_parallel_notify_unbind(struct v4l2_async_notifier *notifier,
  462. struct v4l2_subdev *subdev,
  463. struct v4l2_async_subdev *asd)
  464. {
  465. struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
  466. vin_dbg(vin, "unbind parallel subdev %s\n", subdev->name);
  467. mutex_lock(&vin->lock);
  468. rvin_parallel_subdevice_detach(vin);
  469. mutex_unlock(&vin->lock);
  470. }
  471. static int rvin_parallel_notify_bound(struct v4l2_async_notifier *notifier,
  472. struct v4l2_subdev *subdev,
  473. struct v4l2_async_subdev *asd)
  474. {
  475. struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
  476. int ret;
  477. mutex_lock(&vin->lock);
  478. ret = rvin_parallel_subdevice_attach(vin, subdev);
  479. mutex_unlock(&vin->lock);
  480. if (ret)
  481. return ret;
  482. v4l2_set_subdev_hostdata(subdev, vin);
  483. vin_dbg(vin, "bound subdev %s source pad: %u sink pad: %u\n",
  484. subdev->name, vin->parallel->source_pad,
  485. vin->parallel->sink_pad);
  486. return 0;
  487. }
  488. static const struct v4l2_async_notifier_operations rvin_parallel_notify_ops = {
  489. .bound = rvin_parallel_notify_bound,
  490. .unbind = rvin_parallel_notify_unbind,
  491. .complete = rvin_parallel_notify_complete,
  492. };
  493. static int rvin_parallel_parse_v4l2(struct device *dev,
  494. struct v4l2_fwnode_endpoint *vep,
  495. struct v4l2_async_subdev *asd)
  496. {
  497. struct rvin_dev *vin = dev_get_drvdata(dev);
  498. struct rvin_parallel_entity *rvpe =
  499. container_of(asd, struct rvin_parallel_entity, asd);
  500. if (vep->base.port || vep->base.id)
  501. return -ENOTCONN;
  502. vin->parallel = rvpe;
  503. vin->parallel->mbus_type = vep->bus_type;
  504. switch (vin->parallel->mbus_type) {
  505. case V4L2_MBUS_PARALLEL:
  506. vin_dbg(vin, "Found PARALLEL media bus\n");
  507. vin->parallel->mbus_flags = vep->bus.parallel.flags;
  508. break;
  509. case V4L2_MBUS_BT656:
  510. vin_dbg(vin, "Found BT656 media bus\n");
  511. vin->parallel->mbus_flags = 0;
  512. break;
  513. default:
  514. vin_err(vin, "Unknown media bus type\n");
  515. return -EINVAL;
  516. }
  517. return 0;
  518. }
  519. static int rvin_parallel_init(struct rvin_dev *vin)
  520. {
  521. int ret;
  522. v4l2_async_notifier_init(&vin->notifier);
  523. ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
  524. vin->dev, &vin->notifier, sizeof(struct rvin_parallel_entity),
  525. 0, rvin_parallel_parse_v4l2);
  526. if (ret)
  527. return ret;
  528. /* If using mc, it's fine not to have any input registered. */
  529. if (!vin->parallel)
  530. return vin->info->use_mc ? 0 : -ENODEV;
  531. vin_dbg(vin, "Found parallel subdevice %pOF\n",
  532. to_of_node(vin->parallel->asd.match.fwnode));
  533. vin->notifier.ops = &rvin_parallel_notify_ops;
  534. ret = v4l2_async_notifier_register(&vin->v4l2_dev, &vin->notifier);
  535. if (ret < 0) {
  536. vin_err(vin, "Notifier registration failed\n");
  537. v4l2_async_notifier_cleanup(&vin->notifier);
  538. return ret;
  539. }
  540. return 0;
  541. }
  542. /* -----------------------------------------------------------------------------
  543. * Group async notifier
  544. */
  545. static int rvin_group_notify_complete(struct v4l2_async_notifier *notifier)
  546. {
  547. struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
  548. const struct rvin_group_route *route;
  549. unsigned int i;
  550. int ret;
  551. ret = v4l2_device_register_subdev_nodes(&vin->v4l2_dev);
  552. if (ret) {
  553. vin_err(vin, "Failed to register subdev nodes\n");
  554. return ret;
  555. }
  556. /* Register all video nodes for the group. */
  557. for (i = 0; i < RCAR_VIN_NUM; i++) {
  558. if (vin->group->vin[i] &&
  559. !video_is_registered(&vin->group->vin[i]->vdev)) {
  560. ret = rvin_v4l2_register(vin->group->vin[i]);
  561. if (ret)
  562. return ret;
  563. }
  564. }
  565. /* Create all media device links between VINs and CSI-2's. */
  566. mutex_lock(&vin->group->lock);
  567. for (route = vin->info->routes; route->mask; route++) {
  568. struct media_pad *source_pad, *sink_pad;
  569. struct media_entity *source, *sink;
  570. unsigned int source_idx;
  571. /* Check that VIN is part of the group. */
  572. if (!vin->group->vin[route->vin])
  573. continue;
  574. /* Check that VIN' master is part of the group. */
  575. if (!vin->group->vin[rvin_group_id_to_master(route->vin)])
  576. continue;
  577. /* Check that CSI-2 is part of the group. */
  578. if (!vin->group->csi[route->csi].subdev)
  579. continue;
  580. source = &vin->group->csi[route->csi].subdev->entity;
  581. source_idx = rvin_group_csi_channel_to_pad(route->channel);
  582. source_pad = &source->pads[source_idx];
  583. sink = &vin->group->vin[route->vin]->vdev.entity;
  584. sink_pad = &sink->pads[0];
  585. /* Skip if link already exists. */
  586. if (media_entity_find_link(source_pad, sink_pad))
  587. continue;
  588. ret = media_create_pad_link(source, source_idx, sink, 0, 0);
  589. if (ret) {
  590. vin_err(vin, "Error adding link from %s to %s\n",
  591. source->name, sink->name);
  592. break;
  593. }
  594. }
  595. mutex_unlock(&vin->group->lock);
  596. return ret;
  597. }
  598. static void rvin_group_notify_unbind(struct v4l2_async_notifier *notifier,
  599. struct v4l2_subdev *subdev,
  600. struct v4l2_async_subdev *asd)
  601. {
  602. struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
  603. unsigned int i;
  604. for (i = 0; i < RCAR_VIN_NUM; i++)
  605. if (vin->group->vin[i])
  606. rvin_v4l2_unregister(vin->group->vin[i]);
  607. mutex_lock(&vin->group->lock);
  608. for (i = 0; i < RVIN_CSI_MAX; i++) {
  609. if (vin->group->csi[i].fwnode != asd->match.fwnode)
  610. continue;
  611. vin->group->csi[i].subdev = NULL;
  612. vin_dbg(vin, "Unbind CSI-2 %s from slot %u\n", subdev->name, i);
  613. break;
  614. }
  615. mutex_unlock(&vin->group->lock);
  616. }
  617. static int rvin_group_notify_bound(struct v4l2_async_notifier *notifier,
  618. struct v4l2_subdev *subdev,
  619. struct v4l2_async_subdev *asd)
  620. {
  621. struct rvin_dev *vin = v4l2_dev_to_vin(notifier->v4l2_dev);
  622. unsigned int i;
  623. mutex_lock(&vin->group->lock);
  624. for (i = 0; i < RVIN_CSI_MAX; i++) {
  625. if (vin->group->csi[i].fwnode != asd->match.fwnode)
  626. continue;
  627. vin->group->csi[i].subdev = subdev;
  628. vin_dbg(vin, "Bound CSI-2 %s to slot %u\n", subdev->name, i);
  629. break;
  630. }
  631. mutex_unlock(&vin->group->lock);
  632. return 0;
  633. }
  634. static const struct v4l2_async_notifier_operations rvin_group_notify_ops = {
  635. .bound = rvin_group_notify_bound,
  636. .unbind = rvin_group_notify_unbind,
  637. .complete = rvin_group_notify_complete,
  638. };
  639. static int rvin_mc_parse_of_endpoint(struct device *dev,
  640. struct v4l2_fwnode_endpoint *vep,
  641. struct v4l2_async_subdev *asd)
  642. {
  643. struct rvin_dev *vin = dev_get_drvdata(dev);
  644. int ret = 0;
  645. if (vep->base.port != 1 || vep->base.id >= RVIN_CSI_MAX)
  646. return -EINVAL;
  647. if (!of_device_is_available(to_of_node(asd->match.fwnode))) {
  648. vin_dbg(vin, "OF device %pOF disabled, ignoring\n",
  649. to_of_node(asd->match.fwnode));
  650. return -ENOTCONN;
  651. }
  652. mutex_lock(&vin->group->lock);
  653. if (vin->group->csi[vep->base.id].fwnode) {
  654. vin_dbg(vin, "OF device %pOF already handled\n",
  655. to_of_node(asd->match.fwnode));
  656. ret = -ENOTCONN;
  657. goto out;
  658. }
  659. vin->group->csi[vep->base.id].fwnode = asd->match.fwnode;
  660. vin_dbg(vin, "Add group OF device %pOF to slot %u\n",
  661. to_of_node(asd->match.fwnode), vep->base.id);
  662. out:
  663. mutex_unlock(&vin->group->lock);
  664. return ret;
  665. }
  666. static int rvin_mc_parse_of_graph(struct rvin_dev *vin)
  667. {
  668. unsigned int count = 0, vin_mask = 0;
  669. unsigned int i;
  670. int ret;
  671. mutex_lock(&vin->group->lock);
  672. /* If not all VIN's are registered don't register the notifier. */
  673. for (i = 0; i < RCAR_VIN_NUM; i++) {
  674. if (vin->group->vin[i]) {
  675. count++;
  676. vin_mask |= BIT(i);
  677. }
  678. }
  679. if (vin->group->count != count) {
  680. mutex_unlock(&vin->group->lock);
  681. return 0;
  682. }
  683. mutex_unlock(&vin->group->lock);
  684. v4l2_async_notifier_init(&vin->group->notifier);
  685. /*
  686. * Have all VIN's look for CSI-2 subdevices. Some subdevices will
  687. * overlap but the parser function can handle it, so each subdevice
  688. * will only be registered once with the group notifier.
  689. */
  690. for (i = 0; i < RCAR_VIN_NUM; i++) {
  691. if (!(vin_mask & BIT(i)))
  692. continue;
  693. ret = v4l2_async_notifier_parse_fwnode_endpoints_by_port(
  694. vin->group->vin[i]->dev, &vin->group->notifier,
  695. sizeof(struct v4l2_async_subdev), 1,
  696. rvin_mc_parse_of_endpoint);
  697. if (ret)
  698. return ret;
  699. }
  700. if (list_empty(&vin->group->notifier.asd_list))
  701. return 0;
  702. vin->group->notifier.ops = &rvin_group_notify_ops;
  703. ret = v4l2_async_notifier_register(&vin->v4l2_dev,
  704. &vin->group->notifier);
  705. if (ret < 0) {
  706. vin_err(vin, "Notifier registration failed\n");
  707. v4l2_async_notifier_cleanup(&vin->group->notifier);
  708. return ret;
  709. }
  710. return 0;
  711. }
  712. static int rvin_mc_init(struct rvin_dev *vin)
  713. {
  714. int ret;
  715. vin->pad.flags = MEDIA_PAD_FL_SINK;
  716. ret = media_entity_pads_init(&vin->vdev.entity, 1, &vin->pad);
  717. if (ret)
  718. return ret;
  719. ret = rvin_group_get(vin);
  720. if (ret)
  721. return ret;
  722. ret = rvin_mc_parse_of_graph(vin);
  723. if (ret)
  724. rvin_group_put(vin);
  725. ret = v4l2_ctrl_handler_init(&vin->ctrl_handler, 1);
  726. if (ret < 0)
  727. return ret;
  728. v4l2_ctrl_new_std(&vin->ctrl_handler, &rvin_ctrl_ops,
  729. V4L2_CID_ALPHA_COMPONENT, 0, 255, 1, 255);
  730. if (vin->ctrl_handler.error) {
  731. ret = vin->ctrl_handler.error;
  732. v4l2_ctrl_handler_free(&vin->ctrl_handler);
  733. return ret;
  734. }
  735. vin->vdev.ctrl_handler = &vin->ctrl_handler;
  736. return ret;
  737. }
  738. /* -----------------------------------------------------------------------------
  739. * Platform Device Driver
  740. */
  741. static const struct rvin_info rcar_info_h1 = {
  742. .model = RCAR_H1,
  743. .use_mc = false,
  744. .max_width = 2048,
  745. .max_height = 2048,
  746. };
  747. static const struct rvin_info rcar_info_m1 = {
  748. .model = RCAR_M1,
  749. .use_mc = false,
  750. .max_width = 2048,
  751. .max_height = 2048,
  752. };
  753. static const struct rvin_info rcar_info_gen2 = {
  754. .model = RCAR_GEN2,
  755. .use_mc = false,
  756. .max_width = 2048,
  757. .max_height = 2048,
  758. };
  759. static const struct rvin_group_route rcar_info_r8a7795_routes[] = {
  760. { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
  761. { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
  762. { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
  763. { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
  764. { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
  765. { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
  766. { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
  767. { .csi = RVIN_CSI20, .channel = 1, .vin = 2, .mask = BIT(0) },
  768. { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
  769. { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
  770. { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
  771. { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
  772. { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
  773. { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) | BIT(2) },
  774. { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
  775. { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
  776. { .csi = RVIN_CSI41, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
  777. { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
  778. { .csi = RVIN_CSI41, .channel = 1, .vin = 4, .mask = BIT(2) },
  779. { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
  780. { .csi = RVIN_CSI41, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
  781. { .csi = RVIN_CSI41, .channel = 0, .vin = 5, .mask = BIT(2) },
  782. { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
  783. { .csi = RVIN_CSI20, .channel = 1, .vin = 6, .mask = BIT(0) },
  784. { .csi = RVIN_CSI41, .channel = 0, .vin = 6, .mask = BIT(1) },
  785. { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
  786. { .csi = RVIN_CSI41, .channel = 2, .vin = 6, .mask = BIT(3) },
  787. { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
  788. { .csi = RVIN_CSI41, .channel = 1, .vin = 7, .mask = BIT(0) },
  789. { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) | BIT(2) },
  790. { .csi = RVIN_CSI41, .channel = 3, .vin = 7, .mask = BIT(3) },
  791. { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
  792. { /* Sentinel */ }
  793. };
  794. static const struct rvin_info rcar_info_r8a7795 = {
  795. .model = RCAR_GEN3,
  796. .use_mc = true,
  797. .max_width = 4096,
  798. .max_height = 4096,
  799. .routes = rcar_info_r8a7795_routes,
  800. };
  801. static const struct rvin_group_route rcar_info_r8a7795es1_routes[] = {
  802. { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
  803. { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
  804. { .csi = RVIN_CSI21, .channel = 0, .vin = 0, .mask = BIT(2) | BIT(5) },
  805. { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
  806. { .csi = RVIN_CSI21, .channel = 0, .vin = 1, .mask = BIT(1) },
  807. { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
  808. { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
  809. { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
  810. { .csi = RVIN_CSI21, .channel = 1, .vin = 1, .mask = BIT(5) },
  811. { .csi = RVIN_CSI21, .channel = 0, .vin = 2, .mask = BIT(0) },
  812. { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
  813. { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
  814. { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
  815. { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
  816. { .csi = RVIN_CSI21, .channel = 2, .vin = 2, .mask = BIT(5) },
  817. { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
  818. { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) },
  819. { .csi = RVIN_CSI21, .channel = 1, .vin = 3, .mask = BIT(2) },
  820. { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
  821. { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
  822. { .csi = RVIN_CSI21, .channel = 3, .vin = 3, .mask = BIT(5) },
  823. { .csi = RVIN_CSI41, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
  824. { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
  825. { .csi = RVIN_CSI21, .channel = 0, .vin = 4, .mask = BIT(2) | BIT(5) },
  826. { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
  827. { .csi = RVIN_CSI21, .channel = 0, .vin = 5, .mask = BIT(1) },
  828. { .csi = RVIN_CSI41, .channel = 0, .vin = 5, .mask = BIT(2) },
  829. { .csi = RVIN_CSI41, .channel = 1, .vin = 5, .mask = BIT(3) },
  830. { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
  831. { .csi = RVIN_CSI21, .channel = 1, .vin = 5, .mask = BIT(5) },
  832. { .csi = RVIN_CSI21, .channel = 0, .vin = 6, .mask = BIT(0) },
  833. { .csi = RVIN_CSI41, .channel = 0, .vin = 6, .mask = BIT(1) },
  834. { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
  835. { .csi = RVIN_CSI41, .channel = 2, .vin = 6, .mask = BIT(3) },
  836. { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
  837. { .csi = RVIN_CSI21, .channel = 2, .vin = 6, .mask = BIT(5) },
  838. { .csi = RVIN_CSI41, .channel = 1, .vin = 7, .mask = BIT(0) },
  839. { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) },
  840. { .csi = RVIN_CSI21, .channel = 1, .vin = 7, .mask = BIT(2) },
  841. { .csi = RVIN_CSI41, .channel = 3, .vin = 7, .mask = BIT(3) },
  842. { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
  843. { .csi = RVIN_CSI21, .channel = 3, .vin = 7, .mask = BIT(5) },
  844. { /* Sentinel */ }
  845. };
  846. static const struct rvin_info rcar_info_r8a7795es1 = {
  847. .model = RCAR_GEN3,
  848. .use_mc = true,
  849. .max_width = 4096,
  850. .max_height = 4096,
  851. .routes = rcar_info_r8a7795es1_routes,
  852. };
  853. static const struct rvin_group_route rcar_info_r8a7796_routes[] = {
  854. { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
  855. { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
  856. { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
  857. { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
  858. { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
  859. { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
  860. { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
  861. { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
  862. { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
  863. { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
  864. { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
  865. { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) },
  866. { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
  867. { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
  868. { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
  869. { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
  870. { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
  871. { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
  872. { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(3) },
  873. { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
  874. { .csi = RVIN_CSI40, .channel = 0, .vin = 6, .mask = BIT(1) },
  875. { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
  876. { .csi = RVIN_CSI40, .channel = 2, .vin = 6, .mask = BIT(3) },
  877. { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
  878. { .csi = RVIN_CSI40, .channel = 1, .vin = 7, .mask = BIT(0) },
  879. { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) },
  880. { .csi = RVIN_CSI40, .channel = 3, .vin = 7, .mask = BIT(3) },
  881. { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
  882. { /* Sentinel */ }
  883. };
  884. static const struct rvin_info rcar_info_r8a7796 = {
  885. .model = RCAR_GEN3,
  886. .use_mc = true,
  887. .max_width = 4096,
  888. .max_height = 4096,
  889. .routes = rcar_info_r8a7796_routes,
  890. };
  891. static const struct rvin_group_route rcar_info_r8a77965_routes[] = {
  892. { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
  893. { .csi = RVIN_CSI20, .channel = 0, .vin = 0, .mask = BIT(1) | BIT(4) },
  894. { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
  895. { .csi = RVIN_CSI20, .channel = 0, .vin = 1, .mask = BIT(0) },
  896. { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
  897. { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
  898. { .csi = RVIN_CSI20, .channel = 1, .vin = 1, .mask = BIT(4) },
  899. { .csi = RVIN_CSI20, .channel = 1, .vin = 2, .mask = BIT(0) },
  900. { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
  901. { .csi = RVIN_CSI20, .channel = 0, .vin = 2, .mask = BIT(2) },
  902. { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
  903. { .csi = RVIN_CSI20, .channel = 2, .vin = 2, .mask = BIT(4) },
  904. { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
  905. { .csi = RVIN_CSI20, .channel = 1, .vin = 3, .mask = BIT(1) | BIT(2) },
  906. { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
  907. { .csi = RVIN_CSI20, .channel = 3, .vin = 3, .mask = BIT(4) },
  908. { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
  909. { .csi = RVIN_CSI20, .channel = 0, .vin = 4, .mask = BIT(1) | BIT(4) },
  910. { .csi = RVIN_CSI40, .channel = 1, .vin = 4, .mask = BIT(2) },
  911. { .csi = RVIN_CSI20, .channel = 0, .vin = 5, .mask = BIT(0) },
  912. { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
  913. { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
  914. { .csi = RVIN_CSI20, .channel = 1, .vin = 5, .mask = BIT(4) },
  915. { .csi = RVIN_CSI20, .channel = 1, .vin = 6, .mask = BIT(0) },
  916. { .csi = RVIN_CSI40, .channel = 0, .vin = 6, .mask = BIT(1) },
  917. { .csi = RVIN_CSI20, .channel = 0, .vin = 6, .mask = BIT(2) },
  918. { .csi = RVIN_CSI40, .channel = 2, .vin = 6, .mask = BIT(3) },
  919. { .csi = RVIN_CSI20, .channel = 2, .vin = 6, .mask = BIT(4) },
  920. { .csi = RVIN_CSI40, .channel = 1, .vin = 7, .mask = BIT(0) },
  921. { .csi = RVIN_CSI20, .channel = 1, .vin = 7, .mask = BIT(1) | BIT(2) },
  922. { .csi = RVIN_CSI40, .channel = 3, .vin = 7, .mask = BIT(3) },
  923. { .csi = RVIN_CSI20, .channel = 3, .vin = 7, .mask = BIT(4) },
  924. { /* Sentinel */ }
  925. };
  926. static const struct rvin_info rcar_info_r8a77965 = {
  927. .model = RCAR_GEN3,
  928. .use_mc = true,
  929. .max_width = 4096,
  930. .max_height = 4096,
  931. .routes = rcar_info_r8a77965_routes,
  932. };
  933. static const struct rvin_group_route rcar_info_r8a77970_routes[] = {
  934. { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
  935. { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
  936. { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(3) },
  937. { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
  938. { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
  939. { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
  940. { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
  941. { /* Sentinel */ }
  942. };
  943. static const struct rvin_info rcar_info_r8a77970 = {
  944. .model = RCAR_GEN3,
  945. .use_mc = true,
  946. .max_width = 4096,
  947. .max_height = 4096,
  948. .routes = rcar_info_r8a77970_routes,
  949. };
  950. static const struct rvin_group_route rcar_info_r8a77980_routes[] = {
  951. { .csi = RVIN_CSI40, .channel = 0, .vin = 0, .mask = BIT(0) | BIT(3) },
  952. { .csi = RVIN_CSI40, .channel = 1, .vin = 0, .mask = BIT(2) },
  953. { .csi = RVIN_CSI40, .channel = 0, .vin = 1, .mask = BIT(2) },
  954. { .csi = RVIN_CSI40, .channel = 1, .vin = 1, .mask = BIT(1) | BIT(3) },
  955. { .csi = RVIN_CSI40, .channel = 0, .vin = 2, .mask = BIT(1) },
  956. { .csi = RVIN_CSI40, .channel = 2, .vin = 2, .mask = BIT(3) },
  957. { .csi = RVIN_CSI40, .channel = 1, .vin = 3, .mask = BIT(0) },
  958. { .csi = RVIN_CSI40, .channel = 3, .vin = 3, .mask = BIT(3) },
  959. { .csi = RVIN_CSI41, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
  960. { .csi = RVIN_CSI41, .channel = 1, .vin = 4, .mask = BIT(2) },
  961. { .csi = RVIN_CSI41, .channel = 0, .vin = 5, .mask = BIT(2) },
  962. { .csi = RVIN_CSI41, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
  963. { .csi = RVIN_CSI41, .channel = 0, .vin = 6, .mask = BIT(1) },
  964. { .csi = RVIN_CSI41, .channel = 2, .vin = 6, .mask = BIT(3) },
  965. { .csi = RVIN_CSI41, .channel = 1, .vin = 7, .mask = BIT(0) },
  966. { .csi = RVIN_CSI41, .channel = 3, .vin = 7, .mask = BIT(3) },
  967. { /* Sentinel */ }
  968. };
  969. static const struct rvin_info rcar_info_r8a77980 = {
  970. .model = RCAR_GEN3,
  971. .use_mc = true,
  972. .max_width = 4096,
  973. .max_height = 4096,
  974. .routes = rcar_info_r8a77980_routes,
  975. };
  976. static const struct rvin_group_route rcar_info_r8a77990_routes[] = {
  977. { .csi = RVIN_CSI40, .channel = 0, .vin = 4, .mask = BIT(0) | BIT(3) },
  978. { .csi = RVIN_CSI40, .channel = 0, .vin = 5, .mask = BIT(2) },
  979. { .csi = RVIN_CSI40, .channel = 1, .vin = 4, .mask = BIT(2) },
  980. { .csi = RVIN_CSI40, .channel = 1, .vin = 5, .mask = BIT(1) | BIT(3) },
  981. { /* Sentinel */ }
  982. };
  983. static const struct rvin_info rcar_info_r8a77990 = {
  984. .model = RCAR_GEN3,
  985. .use_mc = true,
  986. .max_width = 4096,
  987. .max_height = 4096,
  988. .routes = rcar_info_r8a77990_routes,
  989. };
  990. static const struct rvin_group_route rcar_info_r8a77995_routes[] = {
  991. { /* Sentinel */ }
  992. };
  993. static const struct rvin_info rcar_info_r8a77995 = {
  994. .model = RCAR_GEN3,
  995. .use_mc = true,
  996. .max_width = 4096,
  997. .max_height = 4096,
  998. .routes = rcar_info_r8a77995_routes,
  999. };
  1000. static const struct of_device_id rvin_of_id_table[] = {
  1001. {
  1002. .compatible = "renesas,vin-r8a774a1",
  1003. .data = &rcar_info_r8a7796,
  1004. },
  1005. {
  1006. .compatible = "renesas,vin-r8a774c0",
  1007. .data = &rcar_info_r8a77990,
  1008. },
  1009. {
  1010. .compatible = "renesas,vin-r8a7778",
  1011. .data = &rcar_info_m1,
  1012. },
  1013. {
  1014. .compatible = "renesas,vin-r8a7779",
  1015. .data = &rcar_info_h1,
  1016. },
  1017. {
  1018. .compatible = "renesas,vin-r8a7790",
  1019. .data = &rcar_info_gen2,
  1020. },
  1021. {
  1022. .compatible = "renesas,vin-r8a7791",
  1023. .data = &rcar_info_gen2,
  1024. },
  1025. {
  1026. .compatible = "renesas,vin-r8a7793",
  1027. .data = &rcar_info_gen2,
  1028. },
  1029. {
  1030. .compatible = "renesas,vin-r8a7794",
  1031. .data = &rcar_info_gen2,
  1032. },
  1033. {
  1034. .compatible = "renesas,rcar-gen2-vin",
  1035. .data = &rcar_info_gen2,
  1036. },
  1037. {
  1038. .compatible = "renesas,vin-r8a7795",
  1039. .data = &rcar_info_r8a7795,
  1040. },
  1041. {
  1042. .compatible = "renesas,vin-r8a7796",
  1043. .data = &rcar_info_r8a7796,
  1044. },
  1045. {
  1046. .compatible = "renesas,vin-r8a77965",
  1047. .data = &rcar_info_r8a77965,
  1048. },
  1049. {
  1050. .compatible = "renesas,vin-r8a77970",
  1051. .data = &rcar_info_r8a77970,
  1052. },
  1053. {
  1054. .compatible = "renesas,vin-r8a77980",
  1055. .data = &rcar_info_r8a77980,
  1056. },
  1057. {
  1058. .compatible = "renesas,vin-r8a77990",
  1059. .data = &rcar_info_r8a77990,
  1060. },
  1061. {
  1062. .compatible = "renesas,vin-r8a77995",
  1063. .data = &rcar_info_r8a77995,
  1064. },
  1065. { /* Sentinel */ },
  1066. };
  1067. MODULE_DEVICE_TABLE(of, rvin_of_id_table);
  1068. static const struct soc_device_attribute r8a7795es1[] = {
  1069. {
  1070. .soc_id = "r8a7795", .revision = "ES1.*",
  1071. .data = &rcar_info_r8a7795es1,
  1072. },
  1073. { /* Sentinel */ }
  1074. };
  1075. static int rcar_vin_probe(struct platform_device *pdev)
  1076. {
  1077. const struct soc_device_attribute *attr;
  1078. struct rvin_dev *vin;
  1079. struct resource *mem;
  1080. int irq, ret;
  1081. vin = devm_kzalloc(&pdev->dev, sizeof(*vin), GFP_KERNEL);
  1082. if (!vin)
  1083. return -ENOMEM;
  1084. vin->dev = &pdev->dev;
  1085. vin->info = of_device_get_match_data(&pdev->dev);
  1086. vin->alpha = 0xff;
  1087. /*
  1088. * Special care is needed on r8a7795 ES1.x since it
  1089. * uses different routing than r8a7795 ES2.0.
  1090. */
  1091. attr = soc_device_match(r8a7795es1);
  1092. if (attr)
  1093. vin->info = attr->data;
  1094. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1095. if (mem == NULL)
  1096. return -EINVAL;
  1097. vin->base = devm_ioremap_resource(vin->dev, mem);
  1098. if (IS_ERR(vin->base))
  1099. return PTR_ERR(vin->base);
  1100. irq = platform_get_irq(pdev, 0);
  1101. if (irq < 0)
  1102. return irq;
  1103. ret = rvin_dma_register(vin, irq);
  1104. if (ret)
  1105. return ret;
  1106. platform_set_drvdata(pdev, vin);
  1107. if (vin->info->use_mc) {
  1108. ret = rvin_mc_init(vin);
  1109. if (ret)
  1110. goto error_dma_unregister;
  1111. }
  1112. ret = rvin_parallel_init(vin);
  1113. if (ret)
  1114. goto error_group_unregister;
  1115. pm_suspend_ignore_children(&pdev->dev, true);
  1116. pm_runtime_enable(&pdev->dev);
  1117. return 0;
  1118. error_group_unregister:
  1119. v4l2_ctrl_handler_free(&vin->ctrl_handler);
  1120. if (vin->info->use_mc) {
  1121. mutex_lock(&vin->group->lock);
  1122. if (&vin->v4l2_dev == vin->group->notifier.v4l2_dev) {
  1123. v4l2_async_notifier_unregister(&vin->group->notifier);
  1124. v4l2_async_notifier_cleanup(&vin->group->notifier);
  1125. }
  1126. mutex_unlock(&vin->group->lock);
  1127. rvin_group_put(vin);
  1128. }
  1129. error_dma_unregister:
  1130. rvin_dma_unregister(vin);
  1131. return ret;
  1132. }
  1133. static int rcar_vin_remove(struct platform_device *pdev)
  1134. {
  1135. struct rvin_dev *vin = platform_get_drvdata(pdev);
  1136. pm_runtime_disable(&pdev->dev);
  1137. rvin_v4l2_unregister(vin);
  1138. v4l2_async_notifier_unregister(&vin->notifier);
  1139. v4l2_async_notifier_cleanup(&vin->notifier);
  1140. if (vin->info->use_mc) {
  1141. mutex_lock(&vin->group->lock);
  1142. if (&vin->v4l2_dev == vin->group->notifier.v4l2_dev) {
  1143. v4l2_async_notifier_unregister(&vin->group->notifier);
  1144. v4l2_async_notifier_cleanup(&vin->group->notifier);
  1145. }
  1146. mutex_unlock(&vin->group->lock);
  1147. rvin_group_put(vin);
  1148. }
  1149. v4l2_ctrl_handler_free(&vin->ctrl_handler);
  1150. rvin_dma_unregister(vin);
  1151. return 0;
  1152. }
  1153. static struct platform_driver rcar_vin_driver = {
  1154. .driver = {
  1155. .name = "rcar-vin",
  1156. .of_match_table = rvin_of_id_table,
  1157. },
  1158. .probe = rcar_vin_probe,
  1159. .remove = rcar_vin_remove,
  1160. };
  1161. module_platform_driver(rcar_vin_driver);
  1162. MODULE_AUTHOR("Niklas Söderlund <niklas.soderlund@ragnatech.se>");
  1163. MODULE_DESCRIPTION("Renesas R-Car VIN camera host driver");
  1164. MODULE_LICENSE("GPL");