camss.c 23.4 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Todor Tomov's avatar
Todor Tomov committed
2 3 4 5 6 7
/*
 * camss.c
 *
 * Qualcomm MSM Camera Subsystem - Core
 *
 * Copyright (c) 2015, The Linux Foundation. All rights reserved.
8
 * Copyright (C) 2015-2018 Linaro Ltd.
Todor Tomov's avatar
Todor Tomov committed
9 10 11 12 13 14 15 16
 */
#include <linux/clk.h>
#include <linux/media-bus-format.h>
#include <linux/media.h>
#include <linux/module.h>
#include <linux/platform_device.h>
#include <linux/of.h>
#include <linux/of_graph.h>
17 18
#include <linux/pm_runtime.h>
#include <linux/pm_domain.h>
Todor Tomov's avatar
Todor Tomov committed
19 20 21 22 23 24 25 26 27 28 29
#include <linux/slab.h>
#include <linux/videodev2.h>

#include <media/media-device.h>
#include <media/v4l2-async.h>
#include <media/v4l2-device.h>
#include <media/v4l2-mc.h>
#include <media/v4l2-fwnode.h>

#include "camss.h"

30 31 32
#define CAMSS_CLOCK_MARGIN_NUMERATOR 105
#define CAMSS_CLOCK_MARGIN_DENOMINATOR 100

33
static const struct resources csiphy_res_8x16[] = {
Todor Tomov's avatar
Todor Tomov committed
34 35 36
	/* CSIPHY0 */
	{
		.regulator = { NULL },
37
		.clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy0_timer" },
38 39 40 41
		.clock_rate = { { 0 },
				{ 0 },
				{ 0 },
				{ 100000000, 200000000 } },
Todor Tomov's avatar
Todor Tomov committed
42 43 44 45 46 47 48
		.reg = { "csiphy0", "csiphy0_clk_mux" },
		.interrupt = { "csiphy0" }
	},

	/* CSIPHY1 */
	{
		.regulator = { NULL },
49
		.clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy1_timer" },
50 51 52 53
		.clock_rate = { { 0 },
				{ 0 },
				{ 0 },
				{ 100000000, 200000000 } },
Todor Tomov's avatar
Todor Tomov committed
54 55 56 57 58
		.reg = { "csiphy1", "csiphy1_clk_mux" },
		.interrupt = { "csiphy1" }
	}
};

59
static const struct resources csid_res_8x16[] = {
Todor Tomov's avatar
Todor Tomov committed
60 61 62
	/* CSID0 */
	{
		.regulator = { "vdda" },
63
		.clock = { "top_ahb", "ispif_ahb", "csi0_ahb", "ahb",
Todor Tomov's avatar
Todor Tomov committed
64
			   "csi0", "csi0_phy", "csi0_pix", "csi0_rdi" },
65 66 67 68 69 70 71 72
		.clock_rate = { { 0 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 100000000, 200000000 },
				{ 0 },
				{ 0 },
				{ 0 } },
Todor Tomov's avatar
Todor Tomov committed
73 74 75 76 77 78 79
		.reg = { "csid0" },
		.interrupt = { "csid0" }
	},

	/* CSID1 */
	{
		.regulator = { "vdda" },
80
		.clock = { "top_ahb", "ispif_ahb", "csi1_ahb", "ahb",
Todor Tomov's avatar
Todor Tomov committed
81
			   "csi1", "csi1_phy", "csi1_pix", "csi1_rdi" },
82 83 84 85 86 87 88 89
		.clock_rate = { { 0 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 100000000, 200000000 },
				{ 0 },
				{ 0 },
				{ 0 } },
Todor Tomov's avatar
Todor Tomov committed
90 91 92 93 94
		.reg = { "csid1" },
		.interrupt = { "csid1" }
	},
};

95
static const struct resources_ispif ispif_res_8x16 = {
Todor Tomov's avatar
Todor Tomov committed
96
	/* ISPIF */
97
	.clock = { "top_ahb", "ahb", "ispif_ahb",
Todor Tomov's avatar
Todor Tomov committed
98 99
		   "csi0", "csi0_pix", "csi0_rdi",
		   "csi1", "csi1_pix", "csi1_rdi" },
100
	.clock_for_reset = { "vfe0", "csi_vfe0" },
Todor Tomov's avatar
Todor Tomov committed
101 102 103 104 105
	.reg = { "ispif", "csi_clk_mux" },
	.interrupt = "ispif"

};

106
static const struct resources vfe_res_8x16[] = {
Todor Tomov's avatar
Todor Tomov committed
107
	/* VFE0 */
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
	{
		.regulator = { NULL },
		.clock = { "top_ahb", "vfe0", "csi_vfe0",
			   "vfe_ahb", "vfe_axi", "ahb" },
		.clock_rate = { { 0 },
				{ 50000000, 80000000, 100000000, 160000000,
				  177780000, 200000000, 266670000, 320000000,
				  400000000, 465000000 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 0 } },
		.reg = { "vfe0" },
		.interrupt = { "vfe0" }
	}
};

static const struct resources csiphy_res_8x96[] = {
	/* CSIPHY0 */
	{
		.regulator = { NULL },
		.clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy0_timer" },
		.clock_rate = { { 0 },
				{ 0 },
				{ 0 },
				{ 100000000, 200000000, 266666667 } },
		.reg = { "csiphy0", "csiphy0_clk_mux" },
		.interrupt = { "csiphy0" }
	},

	/* CSIPHY1 */
	{
		.regulator = { NULL },
		.clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy1_timer" },
		.clock_rate = { { 0 },
				{ 0 },
				{ 0 },
				{ 100000000, 200000000, 266666667 } },
		.reg = { "csiphy1", "csiphy1_clk_mux" },
		.interrupt = { "csiphy1" }
	},

	/* CSIPHY2 */
	{
		.regulator = { NULL },
		.clock = { "top_ahb", "ispif_ahb", "ahb", "csiphy2_timer" },
		.clock_rate = { { 0 },
				{ 0 },
				{ 0 },
				{ 100000000, 200000000, 266666667 } },
		.reg = { "csiphy2", "csiphy2_clk_mux" },
		.interrupt = { "csiphy2" }
	}
};

static const struct resources csid_res_8x96[] = {
	/* CSID0 */
	{
		.regulator = { "vdda" },
		.clock = { "top_ahb", "ispif_ahb", "csi0_ahb", "ahb",
			   "csi0", "csi0_phy", "csi0_pix", "csi0_rdi" },
		.clock_rate = { { 0 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 100000000, 200000000, 266666667 },
				{ 0 },
				{ 0 },
				{ 0 } },
		.reg = { "csid0" },
		.interrupt = { "csid0" }
	},

	/* CSID1 */
	{
		.regulator = { "vdda" },
		.clock = { "top_ahb", "ispif_ahb", "csi1_ahb", "ahb",
			   "csi1", "csi1_phy", "csi1_pix", "csi1_rdi" },
		.clock_rate = { { 0 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 100000000, 200000000, 266666667 },
				{ 0 },
				{ 0 },
				{ 0 } },
		.reg = { "csid1" },
		.interrupt = { "csid1" }
	},

	/* CSID2 */
	{
		.regulator = { "vdda" },
		.clock = { "top_ahb", "ispif_ahb", "csi2_ahb", "ahb",
			   "csi2", "csi2_phy", "csi2_pix", "csi2_rdi" },
		.clock_rate = { { 0 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 100000000, 200000000, 266666667 },
				{ 0 },
				{ 0 },
				{ 0 } },
		.reg = { "csid2" },
		.interrupt = { "csid2" }
	},

	/* CSID3 */
	{
		.regulator = { "vdda" },
		.clock = { "top_ahb", "ispif_ahb", "csi3_ahb", "ahb",
			   "csi3", "csi3_phy", "csi3_pix", "csi3_rdi" },
		.clock_rate = { { 0 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 100000000, 200000000, 266666667 },
				{ 0 },
				{ 0 },
				{ 0 } },
		.reg = { "csid3" },
		.interrupt = { "csid3" }
	}
};

static const struct resources_ispif ispif_res_8x96 = {
	/* ISPIF */
	.clock = { "top_ahb", "ahb", "ispif_ahb",
		   "csi0", "csi0_pix", "csi0_rdi",
		   "csi1", "csi1_pix", "csi1_rdi",
		   "csi2", "csi2_pix", "csi2_rdi",
		   "csi3", "csi3_pix", "csi3_rdi" },
	.clock_for_reset = { "vfe0", "csi_vfe0", "vfe1", "csi_vfe1" },
	.reg = { "ispif", "csi_clk_mux" },
	.interrupt = "ispif"
};

static const struct resources vfe_res_8x96[] = {
	/* VFE0 */
	{
		.regulator = { NULL },
		.clock = { "top_ahb", "ahb", "vfe0", "csi_vfe0", "vfe_ahb",
			   "vfe0_ahb", "vfe_axi", "vfe0_stream"},
		.clock_rate = { { 0 },
				{ 0 },
				{ 75000000, 100000000, 300000000,
				  320000000, 480000000, 600000000 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 0 } },
		.reg = { "vfe0" },
		.interrupt = { "vfe0" }
	},

	/* VFE1 */
	{
		.regulator = { NULL },
		.clock = { "top_ahb", "ahb", "vfe1", "csi_vfe1", "vfe_ahb",
			   "vfe1_ahb", "vfe_axi", "vfe1_stream"},
		.clock_rate = { { 0 },
				{ 0 },
				{ 75000000, 100000000, 300000000,
				  320000000, 480000000, 600000000 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 0 },
				{ 0 } },
		.reg = { "vfe1" },
		.interrupt = { "vfe1" }
	}
Todor Tomov's avatar
Todor Tomov committed
284 285
};

286 287 288 289 290 291 292 293 294 295 296 297 298
/*
 * camss_add_clock_margin - Add margin to clock frequency rate
 * @rate: Clock frequency rate
 *
 * When making calculations with physical clock frequency values
 * some safety margin must be added. Add it.
 */
inline void camss_add_clock_margin(u64 *rate)
{
	*rate *= CAMSS_CLOCK_MARGIN_NUMERATOR;
	*rate = div_u64(*rate, CAMSS_CLOCK_MARGIN_DENOMINATOR);
}

Todor Tomov's avatar
Todor Tomov committed
299 300 301 302 303 304 305 306
/*
 * camss_enable_clocks - Enable multiple clocks
 * @nclocks: Number of clocks in clock array
 * @clock: Clock array
 * @dev: Device
 *
 * Return 0 on success or a negative error code otherwise
 */
307 308
int camss_enable_clocks(int nclocks, struct camss_clock *clock,
			struct device *dev)
Todor Tomov's avatar
Todor Tomov committed
309 310 311 312 313
{
	int ret;
	int i;

	for (i = 0; i < nclocks; i++) {
314
		ret = clk_prepare_enable(clock[i].clk);
Todor Tomov's avatar
Todor Tomov committed
315 316 317 318 319 320 321 322 323 324
		if (ret) {
			dev_err(dev, "clock enable failed: %d\n", ret);
			goto error;
		}
	}

	return 0;

error:
	for (i--; i >= 0; i--)
325
		clk_disable_unprepare(clock[i].clk);
Todor Tomov's avatar
Todor Tomov committed
326 327 328 329 330 331 332 333 334

	return ret;
}

/*
 * camss_disable_clocks - Disable multiple clocks
 * @nclocks: Number of clocks in clock array
 * @clock: Clock array
 */
335
void camss_disable_clocks(int nclocks, struct camss_clock *clock)
Todor Tomov's avatar
Todor Tomov committed
336 337 338 339
{
	int i;

	for (i = nclocks - 1; i >= 0; i--)
340
		clk_disable_unprepare(clock[i].clk);
Todor Tomov's avatar
Todor Tomov committed
341 342 343 344 345 346 347 348
}

/*
 * camss_find_sensor - Find a linked media entity which represents a sensor
 * @entity: Media entity to start searching from
 *
 * Return a pointer to sensor media entity or NULL if not found
 */
349
struct media_entity *camss_find_sensor(struct media_entity *entity)
Todor Tomov's avatar
Todor Tomov committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
{
	struct media_pad *pad;

	while (1) {
		pad = &entity->pads[0];
		if (!(pad->flags & MEDIA_PAD_FL_SINK))
			return NULL;

		pad = media_entity_remote_pad(pad);
		if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
			return NULL;

		entity = pad->entity;

		if (entity->function == MEDIA_ENT_F_CAM_SENSOR)
			return entity;
	}
}

/*
 * camss_get_pixel_clock - Get pixel clock rate from sensor
 * @entity: Media entity in the current pipeline
 * @pixel_clock: Received pixel clock value
 *
 * Return 0 on success or a negative error code otherwise
 */
int camss_get_pixel_clock(struct media_entity *entity, u32 *pixel_clock)
{
	struct media_entity *sensor;
	struct v4l2_subdev *subdev;
	struct v4l2_ctrl *ctrl;

	sensor = camss_find_sensor(entity);
	if (!sensor)
		return -ENODEV;

	subdev = media_entity_to_v4l2_subdev(sensor);

	ctrl = v4l2_ctrl_find(subdev->ctrl_handler, V4L2_CID_PIXEL_RATE);

	if (!ctrl)
		return -EINVAL;

	*pixel_clock = v4l2_ctrl_g_ctrl_int64(ctrl);

	return 0;
}

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
int camss_pm_domain_on(struct camss *camss, int id)
{
	if (camss->version == CAMSS_8x96) {
		camss->genpd_link[id] = device_link_add(camss->dev,
				camss->genpd[id], DL_FLAG_STATELESS |
				DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE);

		if (!camss->genpd_link[id])
			return -EINVAL;
	}

	return 0;
}

void camss_pm_domain_off(struct camss *camss, int id)
{
	if (camss->version == CAMSS_8x96)
		device_link_del(camss->genpd_link[id]);
}

Todor Tomov's avatar
Todor Tomov committed
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
/*
 * camss_of_parse_endpoint_node - Parse port endpoint node
 * @dev: Device
 * @node: Device node to be parsed
 * @csd: Parsed data from port endpoint node
 *
 * Return 0 on success or a negative error code on failure
 */
static int camss_of_parse_endpoint_node(struct device *dev,
					struct device_node *node,
					struct camss_async_subdev *csd)
{
	struct csiphy_lanes_cfg *lncfg = &csd->interface.csi2.lane_cfg;
	struct v4l2_fwnode_bus_mipi_csi2 *mipi_csi2;
	struct v4l2_fwnode_endpoint vep = { { 0 } };
	unsigned int i;

	v4l2_fwnode_endpoint_parse(of_fwnode_handle(node), &vep);

	csd->interface.csiphy_id = vep.base.port;

	mipi_csi2 = &vep.bus.mipi_csi2;
	lncfg->clk.pos = mipi_csi2->clock_lane;
	lncfg->clk.pol = mipi_csi2->lane_polarities[0];
	lncfg->num_data = mipi_csi2->num_data_lanes;

444 445
	lncfg->data = devm_kcalloc(dev,
				   lncfg->num_data, sizeof(*lncfg->data),
Todor Tomov's avatar
Todor Tomov committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
				   GFP_KERNEL);
	if (!lncfg->data)
		return -ENOMEM;

	for (i = 0; i < lncfg->num_data; i++) {
		lncfg->data[i].pos = mipi_csi2->data_lanes[i];
		lncfg->data[i].pol = mipi_csi2->lane_polarities[i + 1];
	}

	return 0;
}

/*
 * camss_of_parse_ports - Parse ports node
 * @dev: Device
 * @notifier: v4l2_device notifier data
 *
 * Return number of "port" nodes found in "ports" node
 */
465
static int camss_of_parse_ports(struct camss *camss)
Todor Tomov's avatar
Todor Tomov committed
466
{
467
	struct device *dev = camss->dev;
Todor Tomov's avatar
Todor Tomov committed
468 469
	struct device_node *node = NULL;
	struct device_node *remote = NULL;
470
	int ret, num_subdevs = 0;
Todor Tomov's avatar
Todor Tomov committed
471

472
	for_each_endpoint_of_node(dev->of_node, node) {
Todor Tomov's avatar
Todor Tomov committed
473
		struct camss_async_subdev *csd;
474
		struct v4l2_async_subdev *asd;
Todor Tomov's avatar
Todor Tomov committed
475 476 477 478 479 480 481

		if (!of_device_is_available(node))
			continue;

		remote = of_graph_get_remote_port_parent(node);
		if (!remote) {
			dev_err(dev, "Cannot get remote parent\n");
482 483
			ret = -EINVAL;
			goto err_cleanup;
Todor Tomov's avatar
Todor Tomov committed
484 485
		}

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
		asd = v4l2_async_notifier_add_fwnode_subdev(
			&camss->notifier, of_fwnode_handle(remote),
			sizeof(*csd));
		if (IS_ERR(asd)) {
			ret = PTR_ERR(asd);
			of_node_put(remote);
			goto err_cleanup;
		}

		csd = container_of(asd, struct camss_async_subdev, asd);

		ret = camss_of_parse_endpoint_node(dev, node, csd);
		if (ret < 0)
			goto err_cleanup;

		num_subdevs++;
Todor Tomov's avatar
Todor Tomov committed
502 503
	}

504 505 506 507 508 509
	return num_subdevs;

err_cleanup:
	v4l2_async_notifier_cleanup(&camss->notifier);
	of_node_put(node);
	return ret;
Todor Tomov's avatar
Todor Tomov committed
510 511 512 513 514 515 516 517 518 519
}

/*
 * camss_init_subdevices - Initialize subdev structures and resources
 * @camss: CAMSS device
 *
 * Return 0 on success or a negative error code on failure
 */
static int camss_init_subdevices(struct camss *camss)
{
520 521 522 523
	const struct resources *csiphy_res;
	const struct resources *csid_res;
	const struct resources_ispif *ispif_res;
	const struct resources *vfe_res;
Todor Tomov's avatar
Todor Tomov committed
524 525 526
	unsigned int i;
	int ret;

527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542
	if (camss->version == CAMSS_8x16) {
		csiphy_res = csiphy_res_8x16;
		csid_res = csid_res_8x16;
		ispif_res = &ispif_res_8x16;
		vfe_res = vfe_res_8x16;
	} else if (camss->version == CAMSS_8x96) {
		csiphy_res = csiphy_res_8x96;
		csid_res = csid_res_8x96;
		ispif_res = &ispif_res_8x96;
		vfe_res = vfe_res_8x96;
	} else {
		return -EINVAL;
	}

	for (i = 0; i < camss->csiphy_num; i++) {
		ret = msm_csiphy_subdev_init(camss, &camss->csiphy[i],
Todor Tomov's avatar
Todor Tomov committed
543 544 545 546 547 548 549 550 551
					     &csiphy_res[i], i);
		if (ret < 0) {
			dev_err(camss->dev,
				"Failed to init csiphy%d sub-device: %d\n",
				i, ret);
			return ret;
		}
	}

552 553
	for (i = 0; i < camss->csid_num; i++) {
		ret = msm_csid_subdev_init(camss, &camss->csid[i],
Todor Tomov's avatar
Todor Tomov committed
554 555 556 557 558 559 560 561 562
					   &csid_res[i], i);
		if (ret < 0) {
			dev_err(camss->dev,
				"Failed to init csid%d sub-device: %d\n",
				i, ret);
			return ret;
		}
	}

563
	ret = msm_ispif_subdev_init(&camss->ispif, ispif_res);
Todor Tomov's avatar
Todor Tomov committed
564 565 566 567 568 569
	if (ret < 0) {
		dev_err(camss->dev, "Failed to init ispif sub-device: %d\n",
			ret);
		return ret;
	}

570 571 572 573 574 575 576 577
	for (i = 0; i < camss->vfe_num; i++) {
		ret = msm_vfe_subdev_init(camss, &camss->vfe[i],
					  &vfe_res[i], i);
		if (ret < 0) {
			dev_err(camss->dev,
				"Fail to init vfe%d sub-device: %d\n", i, ret);
			return ret;
		}
Todor Tomov's avatar
Todor Tomov committed
578 579 580 581 582 583 584 585 586 587 588 589 590
	}

	return 0;
}

/*
 * camss_register_entities - Register subdev nodes and create links
 * @camss: CAMSS device
 *
 * Return 0 on success or a negative error code on failure
 */
static int camss_register_entities(struct camss *camss)
{
591
	int i, j, k;
Todor Tomov's avatar
Todor Tomov committed
592 593
	int ret;

594
	for (i = 0; i < camss->csiphy_num; i++) {
Todor Tomov's avatar
Todor Tomov committed
595 596 597 598 599 600 601 602 603 604
		ret = msm_csiphy_register_entity(&camss->csiphy[i],
						 &camss->v4l2_dev);
		if (ret < 0) {
			dev_err(camss->dev,
				"Failed to register csiphy%d entity: %d\n",
				i, ret);
			goto err_reg_csiphy;
		}
	}

605
	for (i = 0; i < camss->csid_num; i++) {
Todor Tomov's avatar
Todor Tomov committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
		ret = msm_csid_register_entity(&camss->csid[i],
					       &camss->v4l2_dev);
		if (ret < 0) {
			dev_err(camss->dev,
				"Failed to register csid%d entity: %d\n",
				i, ret);
			goto err_reg_csid;
		}
	}

	ret = msm_ispif_register_entities(&camss->ispif, &camss->v4l2_dev);
	if (ret < 0) {
		dev_err(camss->dev, "Failed to register ispif entities: %d\n",
			ret);
		goto err_reg_ispif;
	}

623 624 625 626 627 628 629 630 631
	for (i = 0; i < camss->vfe_num; i++) {
		ret = msm_vfe_register_entities(&camss->vfe[i],
						&camss->v4l2_dev);
		if (ret < 0) {
			dev_err(camss->dev,
				"Failed to register vfe%d entities: %d\n",
				i, ret);
			goto err_reg_vfe;
		}
Todor Tomov's avatar
Todor Tomov committed
632 633
	}

634 635
	for (i = 0; i < camss->csiphy_num; i++) {
		for (j = 0; j < camss->csid_num; j++) {
Todor Tomov's avatar
Todor Tomov committed
636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
			ret = media_create_pad_link(
				&camss->csiphy[i].subdev.entity,
				MSM_CSIPHY_PAD_SRC,
				&camss->csid[j].subdev.entity,
				MSM_CSID_PAD_SINK,
				0);
			if (ret < 0) {
				dev_err(camss->dev,
					"Failed to link %s->%s entities: %d\n",
					camss->csiphy[i].subdev.entity.name,
					camss->csid[j].subdev.entity.name,
					ret);
				goto err_link;
			}
		}
	}

653 654
	for (i = 0; i < camss->csid_num; i++) {
		for (j = 0; j < camss->ispif.line_num; j++) {
Todor Tomov's avatar
Todor Tomov committed
655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671
			ret = media_create_pad_link(
				&camss->csid[i].subdev.entity,
				MSM_CSID_PAD_SRC,
				&camss->ispif.line[j].subdev.entity,
				MSM_ISPIF_PAD_SINK,
				0);
			if (ret < 0) {
				dev_err(camss->dev,
					"Failed to link %s->%s entities: %d\n",
					camss->csid[i].subdev.entity.name,
					camss->ispif.line[j].subdev.entity.name,
					ret);
				goto err_link;
			}
		}
	}

672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
	for (i = 0; i < camss->ispif.line_num; i++)
		for (k = 0; k < camss->vfe_num; k++)
			for (j = 0; j < ARRAY_SIZE(camss->vfe[k].line); j++) {
				ret = media_create_pad_link(
					&camss->ispif.line[i].subdev.entity,
					MSM_ISPIF_PAD_SRC,
					&camss->vfe[k].line[j].subdev.entity,
					MSM_VFE_PAD_SINK,
					0);
				if (ret < 0) {
					dev_err(camss->dev,
						"Failed to link %s->%s entities: %d\n",
						camss->ispif.line[i].subdev.entity.name,
						camss->vfe[k].line[j].subdev.entity.name,
						ret);
					goto err_link;
				}
Todor Tomov's avatar
Todor Tomov committed
689 690 691 692 693
			}

	return 0;

err_link:
694
	i = camss->vfe_num;
Todor Tomov's avatar
Todor Tomov committed
695
err_reg_vfe:
696 697 698
	for (i--; i >= 0; i--)
		msm_vfe_unregister_entities(&camss->vfe[i]);

Todor Tomov's avatar
Todor Tomov committed
699 700 701
	msm_ispif_unregister_entities(&camss->ispif);
err_reg_ispif:

702
	i = camss->csid_num;
Todor Tomov's avatar
Todor Tomov committed
703 704 705 706
err_reg_csid:
	for (i--; i >= 0; i--)
		msm_csid_unregister_entity(&camss->csid[i]);

707
	i = camss->csiphy_num;
Todor Tomov's avatar
Todor Tomov committed
708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724
err_reg_csiphy:
	for (i--; i >= 0; i--)
		msm_csiphy_unregister_entity(&camss->csiphy[i]);

	return ret;
}

/*
 * camss_unregister_entities - Unregister subdev nodes
 * @camss: CAMSS device
 *
 * Return 0 on success or a negative error code on failure
 */
static void camss_unregister_entities(struct camss *camss)
{
	unsigned int i;

725
	for (i = 0; i < camss->csiphy_num; i++)
Todor Tomov's avatar
Todor Tomov committed
726 727
		msm_csiphy_unregister_entity(&camss->csiphy[i]);

728
	for (i = 0; i < camss->csid_num; i++)
Todor Tomov's avatar
Todor Tomov committed
729 730 731
		msm_csid_unregister_entity(&camss->csid[i]);

	msm_ispif_unregister_entities(&camss->ispif);
732 733 734

	for (i = 0; i < camss->vfe_num; i++)
		msm_vfe_unregister_entities(&camss->vfe[i]);
Todor Tomov's avatar
Todor Tomov committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
}

static int camss_subdev_notifier_bound(struct v4l2_async_notifier *async,
				       struct v4l2_subdev *subdev,
				       struct v4l2_async_subdev *asd)
{
	struct camss *camss = container_of(async, struct camss, notifier);
	struct camss_async_subdev *csd =
		container_of(asd, struct camss_async_subdev, asd);
	u8 id = csd->interface.csiphy_id;
	struct csiphy_device *csiphy = &camss->csiphy[id];

	csiphy->cfg.csi2 = &csd->interface.csi2;
	subdev->host_priv = csiphy;

	return 0;
}

static int camss_subdev_notifier_complete(struct v4l2_async_notifier *async)
{
	struct camss *camss = container_of(async, struct camss, notifier);
	struct v4l2_device *v4l2_dev = &camss->v4l2_dev;
	struct v4l2_subdev *sd;
	int ret;

	list_for_each_entry(sd, &v4l2_dev->subdevs, list) {
		if (sd->host_priv) {
			struct media_entity *sensor = &sd->entity;
			struct csiphy_device *csiphy =
					(struct csiphy_device *) sd->host_priv;
			struct media_entity *input = &csiphy->subdev.entity;
			unsigned int i;

			for (i = 0; i < sensor->num_pads; i++) {
				if (sensor->pads[i].flags & MEDIA_PAD_FL_SOURCE)
					break;
			}
			if (i == sensor->num_pads) {
				dev_err(camss->dev,
					"No source pad in external entity\n");
				return -EINVAL;
			}

			ret = media_create_pad_link(sensor, i,
				input, MSM_CSIPHY_PAD_SINK,
				MEDIA_LNK_FL_IMMUTABLE | MEDIA_LNK_FL_ENABLED);
			if (ret < 0) {
				dev_err(camss->dev,
					"Failed to link %s->%s entities: %d\n",
					sensor->name, input->name, ret);
				return ret;
			}
		}
	}

	ret = v4l2_device_register_subdev_nodes(&camss->v4l2_dev);
	if (ret < 0)
		return ret;

	return media_device_register(&camss->media_dev);
}

797 798 799 800 801
static const struct v4l2_async_notifier_operations camss_subdev_notifier_ops = {
	.bound = camss_subdev_notifier_bound,
	.complete = camss_subdev_notifier_complete,
};

Todor Tomov's avatar
Todor Tomov committed
802 803 804 805 806 807 808 809 810 811 812 813 814 815
static const struct media_device_ops camss_media_ops = {
	.link_notify = v4l2_pipeline_link_notify,
};

/*
 * camss_probe - Probe CAMSS platform device
 * @pdev: Pointer to CAMSS platform device
 *
 * Return 0 on success or a negative error code on failure
 */
static int camss_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct camss *camss;
816
	int num_subdevs, ret;
Todor Tomov's avatar
Todor Tomov committed
817 818 819 820 821 822 823 824 825

	camss = kzalloc(sizeof(*camss), GFP_KERNEL);
	if (!camss)
		return -ENOMEM;

	atomic_set(&camss->ref_count, 0);
	camss->dev = dev;
	platform_set_drvdata(pdev, camss);

826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
	if (of_device_is_compatible(dev->of_node, "qcom,msm8916-camss")) {
		camss->version = CAMSS_8x16;
		camss->csiphy_num = 2;
		camss->csid_num = 2;
		camss->vfe_num = 1;
	} else if (of_device_is_compatible(dev->of_node,
					   "qcom,msm8996-camss")) {
		camss->version = CAMSS_8x96;
		camss->csiphy_num = 3;
		camss->csid_num = 4;
		camss->vfe_num = 2;
	} else {
		return -EINVAL;
	}

841 842
	camss->csiphy = devm_kcalloc(dev, camss->csiphy_num,
				     sizeof(*camss->csiphy), GFP_KERNEL);
843 844 845
	if (!camss->csiphy)
		return -ENOMEM;

846 847
	camss->csid = devm_kcalloc(dev, camss->csid_num, sizeof(*camss->csid),
				   GFP_KERNEL);
848 849 850
	if (!camss->csid)
		return -ENOMEM;

851 852
	camss->vfe = devm_kcalloc(dev, camss->vfe_num, sizeof(*camss->vfe),
				  GFP_KERNEL);
853 854 855
	if (!camss->vfe)
		return -ENOMEM;

856 857 858 859 860
	v4l2_async_notifier_init(&camss->notifier);

	num_subdevs = camss_of_parse_ports(camss);
	if (num_subdevs < 0)
		return num_subdevs;
Todor Tomov's avatar
Todor Tomov committed
861 862 863

	ret = camss_init_subdevices(camss);
	if (ret < 0)
864
		goto err_cleanup;
Todor Tomov's avatar
Todor Tomov committed
865 866 867

	ret = dma_set_mask_and_coherent(dev, 0xffffffff);
	if (ret)
868
		goto err_cleanup;
Todor Tomov's avatar
Todor Tomov committed
869 870

	camss->media_dev.dev = camss->dev;
871
	strscpy(camss->media_dev.model, "Qualcomm Camera Subsystem",
Todor Tomov's avatar
Todor Tomov committed
872 873 874 875 876 877 878 879
		sizeof(camss->media_dev.model));
	camss->media_dev.ops = &camss_media_ops;
	media_device_init(&camss->media_dev);

	camss->v4l2_dev.mdev = &camss->media_dev;
	ret = v4l2_device_register(camss->dev, &camss->v4l2_dev);
	if (ret < 0) {
		dev_err(dev, "Failed to register V4L2 device: %d\n", ret);
880
		goto err_cleanup;
Todor Tomov's avatar
Todor Tomov committed
881 882 883 884 885 886
	}

	ret = camss_register_entities(camss);
	if (ret < 0)
		goto err_register_entities;

887
	if (num_subdevs) {
888
		camss->notifier.ops = &camss_subdev_notifier_ops;
Todor Tomov's avatar
Todor Tomov committed
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913

		ret = v4l2_async_notifier_register(&camss->v4l2_dev,
						   &camss->notifier);
		if (ret) {
			dev_err(dev,
				"Failed to register async subdev nodes: %d\n",
				ret);
			goto err_register_subdevs;
		}
	} else {
		ret = v4l2_device_register_subdev_nodes(&camss->v4l2_dev);
		if (ret < 0) {
			dev_err(dev, "Failed to register subdev nodes: %d\n",
				ret);
			goto err_register_subdevs;
		}

		ret = media_device_register(&camss->media_dev);
		if (ret < 0) {
			dev_err(dev, "Failed to register media device: %d\n",
				ret);
			goto err_register_subdevs;
		}
	}

914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
	if (camss->version == CAMSS_8x96) {
		camss->genpd[PM_DOMAIN_VFE0] = dev_pm_domain_attach_by_id(
						camss->dev, PM_DOMAIN_VFE0);
		if (IS_ERR(camss->genpd[PM_DOMAIN_VFE0]))
			return PTR_ERR(camss->genpd[PM_DOMAIN_VFE0]);

		camss->genpd[PM_DOMAIN_VFE1] = dev_pm_domain_attach_by_id(
						camss->dev, PM_DOMAIN_VFE1);
		if (IS_ERR(camss->genpd[PM_DOMAIN_VFE1])) {
			dev_pm_domain_detach(camss->genpd[PM_DOMAIN_VFE0],
					     true);
			return PTR_ERR(camss->genpd[PM_DOMAIN_VFE1]);
		}
	}

	pm_runtime_enable(dev);

Todor Tomov's avatar
Todor Tomov committed
931 932 933 934 935 936
	return 0;

err_register_subdevs:
	camss_unregister_entities(camss);
err_register_entities:
	v4l2_device_unregister(&camss->v4l2_dev);
937 938
err_cleanup:
	v4l2_async_notifier_cleanup(&camss->notifier);
Todor Tomov's avatar
Todor Tomov committed
939 940 941 942 943 944 945 946 947 948

	return ret;
}

void camss_delete(struct camss *camss)
{
	v4l2_device_unregister(&camss->v4l2_dev);
	media_device_unregister(&camss->media_dev);
	media_device_cleanup(&camss->media_dev);

949 950 951 952 953 954 955
	pm_runtime_disable(camss->dev);

	if (camss->version == CAMSS_8x96) {
		dev_pm_domain_detach(camss->genpd[PM_DOMAIN_VFE0], true);
		dev_pm_domain_detach(camss->genpd[PM_DOMAIN_VFE1], true);
	}

Todor Tomov's avatar
Todor Tomov committed
956 957 958 959 960 961 962 963 964 965 966
	kfree(camss);
}

/*
 * camss_remove - Remove CAMSS platform device
 * @pdev: Pointer to CAMSS platform device
 *
 * Always returns 0.
 */
static int camss_remove(struct platform_device *pdev)
{
967 968
	unsigned int i;

Todor Tomov's avatar
Todor Tomov committed
969 970
	struct camss *camss = platform_get_drvdata(pdev);

971 972
	for (i = 0; i < camss->vfe_num; i++)
		msm_vfe_stop_streaming(&camss->vfe[i]);
Todor Tomov's avatar
Todor Tomov committed
973 974

	v4l2_async_notifier_unregister(&camss->notifier);
975
	v4l2_async_notifier_cleanup(&camss->notifier);
Todor Tomov's avatar
Todor Tomov committed
976 977 978 979 980 981 982 983 984 985
	camss_unregister_entities(camss);

	if (atomic_read(&camss->ref_count) == 0)
		camss_delete(camss);

	return 0;
}

static const struct of_device_id camss_dt_match[] = {
	{ .compatible = "qcom,msm8916-camss" },
986
	{ .compatible = "qcom,msm8996-camss" },
Todor Tomov's avatar
Todor Tomov committed
987 988 989 990 991
	{ }
};

MODULE_DEVICE_TABLE(of, camss_dt_match);

992
static int __maybe_unused camss_runtime_suspend(struct device *dev)
993 994 995 996
{
	return 0;
}

997
static int __maybe_unused camss_runtime_resume(struct device *dev)
998 999 1000 1001 1002 1003 1004 1005 1006 1007
{
	return 0;
}

static const struct dev_pm_ops camss_pm_ops = {
	SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
				pm_runtime_force_resume)
	SET_RUNTIME_PM_OPS(camss_runtime_suspend, camss_runtime_resume, NULL)
};

Todor Tomov's avatar
Todor Tomov committed
1008 1009 1010 1011 1012 1013
static struct platform_driver qcom_camss_driver = {
	.probe = camss_probe,
	.remove = camss_remove,
	.driver = {
		.name = "qcom-camss",
		.of_match_table = camss_dt_match,
1014
		.pm = &camss_pm_ops,
Todor Tomov's avatar
Todor Tomov committed
1015 1016 1017 1018 1019 1020 1021 1022 1023
	},
};

module_platform_driver(qcom_camss_driver);

MODULE_ALIAS("platform:qcom-camss");
MODULE_DESCRIPTION("Qualcomm Camera Subsystem driver");
MODULE_AUTHOR("Todor Tomov <todor.tomov@linaro.org>");
MODULE_LICENSE("GPL v2");