smiapp-core.c 82.9 KB
Newer Older
Sakari Ailus's avatar
Sakari Ailus committed
1
/*
2
 * drivers/media/i2c/smiapp/smiapp-core.c
Sakari Ailus's avatar
Sakari Ailus committed
3 4 5 6
 *
 * Generic driver for SMIA/SMIA++ compliant camera modules
 *
 * Copyright (C) 2010--2012 Nokia Corporation
7
 * Contact: Sakari Ailus <sakari.ailus@iki.fi>
Sakari Ailus's avatar
Sakari Ailus committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * Based on smiapp driver by Vimarsh Zutshi
 * Based on jt8ev1.c by Vimarsh Zutshi
 * Based on smia-sensor.c by Tuukka Toivonen <tuukkat76@gmail.com>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

23
#include <linux/clk.h>
Sakari Ailus's avatar
Sakari Ailus committed
24 25 26
#include <linux/delay.h>
#include <linux/device.h>
#include <linux/gpio.h>
27
#include <linux/gpio/consumer.h>
Sakari Ailus's avatar
Sakari Ailus committed
28 29
#include <linux/module.h>
#include <linux/regulator/consumer.h>
30 31
#include <linux/slab.h>
#include <linux/smiapp.h>
Sakari Ailus's avatar
Sakari Ailus committed
32 33
#include <linux/v4l2-mediabus.h>
#include <media/v4l2-device.h>
34
#include <media/v4l2-of.h>
Sakari Ailus's avatar
Sakari Ailus committed
35 36 37

#include "smiapp.h"

38 39 40
#define SMIAPP_ALIGN_DIM(dim, flags)	\
	((flags) & V4L2_SEL_FLAG_GE	\
	 ? ALIGN((dim), 2)		\
Sakari Ailus's avatar
Sakari Ailus committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	 : (dim) & ~1)

/*
 * smiapp_module_idents - supported camera modules
 */
static const struct smiapp_module_ident smiapp_module_idents[] = {
	SMIAPP_IDENT_L(0x01, 0x022b, -1, "vs6555"),
	SMIAPP_IDENT_L(0x01, 0x022e, -1, "vw6558"),
	SMIAPP_IDENT_L(0x07, 0x7698, -1, "ovm7698"),
	SMIAPP_IDENT_L(0x0b, 0x4242, -1, "smiapp-003"),
	SMIAPP_IDENT_L(0x0c, 0x208a, -1, "tcm8330md"),
	SMIAPP_IDENT_LQ(0x0c, 0x2134, -1, "tcm8500md", &smiapp_tcm8500md_quirk),
	SMIAPP_IDENT_L(0x0c, 0x213e, -1, "et8en2"),
	SMIAPP_IDENT_L(0x0c, 0x2184, -1, "tcm8580md"),
	SMIAPP_IDENT_LQ(0x0c, 0x560f, -1, "jt8ew9", &smiapp_jt8ew9_quirk),
	SMIAPP_IDENT_LQ(0x10, 0x4141, -1, "jt8ev1", &smiapp_jt8ev1_quirk),
	SMIAPP_IDENT_LQ(0x10, 0x4241, -1, "imx125es", &smiapp_imx125es_quirk),
};

/*
 *
 * Dynamic Capability Identification
 *
 */

static int smiapp_read_frame_fmt(struct smiapp_sensor *sensor)
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	u32 fmt_model_type, fmt_model_subtype, ncol_desc, nrow_desc;
	unsigned int i;
71
	int pixel_count = 0;
Sakari Ailus's avatar
Sakari Ailus committed
72
	int line_count = 0;
73
	int rval;
Sakari Ailus's avatar
Sakari Ailus committed
74

75
	rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_TYPE,
Sakari Ailus's avatar
Sakari Ailus committed
76 77 78 79
			   &fmt_model_type);
	if (rval)
		return rval;

80
	rval = smiapp_read(sensor, SMIAPP_REG_U8_FRAME_FORMAT_MODEL_SUBTYPE,
Sakari Ailus's avatar
Sakari Ailus committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
			   &fmt_model_subtype);
	if (rval)
		return rval;

	ncol_desc = (fmt_model_subtype
		     & SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_MASK)
		>> SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NCOLS_SHIFT;
	nrow_desc = fmt_model_subtype
		& SMIAPP_FRAME_FORMAT_MODEL_SUBTYPE_NROWS_MASK;

	dev_dbg(&client->dev, "format_model_type %s\n",
		fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE
		? "2 byte" :
		fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE
		? "4 byte" : "is simply bad");

	for (i = 0; i < ncol_desc + nrow_desc; i++) {
		u32 desc;
		u32 pixelcode;
		u32 pixels;
		char *which;
		char *what;
103
		u32 reg;
Sakari Ailus's avatar
Sakari Ailus committed
104 105

		if (fmt_model_type == SMIAPP_FRAME_FORMAT_MODEL_TYPE_2BYTE) {
106 107
			reg = SMIAPP_REG_U16_FRAME_FORMAT_DESCRIPTOR_2(i);
			rval = smiapp_read(sensor, reg,	&desc);
Sakari Ailus's avatar
Sakari Ailus committed
108 109 110 111 112 113 114 115 116 117
			if (rval)
				return rval;

			pixelcode =
				(desc
				 & SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_MASK)
				>> SMIAPP_FRAME_FORMAT_DESC_2_PIXELCODE_SHIFT;
			pixels = desc & SMIAPP_FRAME_FORMAT_DESC_2_PIXELS_MASK;
		} else if (fmt_model_type
			   == SMIAPP_FRAME_FORMAT_MODEL_TYPE_4BYTE) {
118 119
			reg = SMIAPP_REG_U32_FRAME_FORMAT_DESCRIPTOR_4(i);
			rval = smiapp_read(sensor, reg, &desc);
Sakari Ailus's avatar
Sakari Ailus committed
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
			if (rval)
				return rval;

			pixelcode =
				(desc
				 & SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_MASK)
				>> SMIAPP_FRAME_FORMAT_DESC_4_PIXELCODE_SHIFT;
			pixels = desc & SMIAPP_FRAME_FORMAT_DESC_4_PIXELS_MASK;
		} else {
			dev_dbg(&client->dev,
				"invalid frame format model type %d\n",
				fmt_model_type);
			return -EINVAL;
		}

		if (i < ncol_desc)
			which = "columns";
		else
			which = "rows";

		switch (pixelcode) {
		case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
			what = "embedded";
			break;
		case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DUMMY:
			what = "dummy";
			break;
		case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_BLACK:
			what = "black";
			break;
		case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_DARK:
			what = "dark";
			break;
		case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
			what = "visible";
			break;
		default:
			what = "invalid";
			break;
		}

161 162 163
		dev_dbg(&client->dev,
			"0x%8.8x %s pixels: %d %s (pixelcode %u)\n", reg,
			what, pixels, which, pixelcode);
Sakari Ailus's avatar
Sakari Ailus committed
164

165 166 167 168 169
		if (i < ncol_desc) {
			if (pixelcode ==
			    SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE)
				sensor->visible_pixel_start = pixel_count;
			pixel_count += pixels;
Sakari Ailus's avatar
Sakari Ailus committed
170
			continue;
171
		}
Sakari Ailus's avatar
Sakari Ailus committed
172 173

		/* Handle row descriptors */
174 175 176 177 178 179 180 181 182 183
		switch (pixelcode) {
		case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_EMBEDDED:
			if (sensor->embedded_end)
				break;
			sensor->embedded_start = line_count;
			sensor->embedded_end = line_count + pixels;
			break;
		case SMIAPP_FRAME_FORMAT_DESC_PIXELCODE_VISIBLE:
			sensor->image_start = line_count;
			break;
Sakari Ailus's avatar
Sakari Ailus committed
184 185 186 187
		}
		line_count += pixels;
	}

188 189 190 191 192
	if (sensor->embedded_end > sensor->image_start) {
		dev_dbg(&client->dev,
			"adjusting image start line to %u (was %u)\n",
			sensor->embedded_end, sensor->image_start);
		sensor->image_start = sensor->embedded_end;
Sakari Ailus's avatar
Sakari Ailus committed
193 194 195
	}

	dev_dbg(&client->dev, "embedded data from lines %d to %d\n",
196 197 198
		sensor->embedded_start, sensor->embedded_end);
	dev_dbg(&client->dev, "image data starts at line %d\n",
		sensor->image_start);
Sakari Ailus's avatar
Sakari Ailus committed
199 200 201 202 203 204 205 206 207 208

	return 0;
}

static int smiapp_pll_configure(struct smiapp_sensor *sensor)
{
	struct smiapp_pll *pll = &sensor->pll;
	int rval;

	rval = smiapp_write(
209
		sensor, SMIAPP_REG_U16_VT_PIX_CLK_DIV, pll->vt.pix_clk_div);
Sakari Ailus's avatar
Sakari Ailus committed
210 211 212 213
	if (rval < 0)
		return rval;

	rval = smiapp_write(
214
		sensor, SMIAPP_REG_U16_VT_SYS_CLK_DIV, pll->vt.sys_clk_div);
Sakari Ailus's avatar
Sakari Ailus committed
215 216 217 218
	if (rval < 0)
		return rval;

	rval = smiapp_write(
219
		sensor, SMIAPP_REG_U16_PRE_PLL_CLK_DIV, pll->pre_pll_clk_div);
Sakari Ailus's avatar
Sakari Ailus committed
220 221 222 223
	if (rval < 0)
		return rval;

	rval = smiapp_write(
224
		sensor, SMIAPP_REG_U16_PLL_MULTIPLIER, pll->pll_multiplier);
Sakari Ailus's avatar
Sakari Ailus committed
225 226 227 228 229
	if (rval < 0)
		return rval;

	/* Lane op clock ratio does not apply here. */
	rval = smiapp_write(
230
		sensor, SMIAPP_REG_U32_REQUESTED_LINK_BIT_RATE_MBPS,
231
		DIV_ROUND_UP(pll->op.sys_clk_freq_hz, 1000000 / 256 / 256));
Sakari Ailus's avatar
Sakari Ailus committed
232 233 234 235
	if (rval < 0 || sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
		return rval;

	rval = smiapp_write(
236
		sensor, SMIAPP_REG_U16_OP_PIX_CLK_DIV, pll->op.pix_clk_div);
Sakari Ailus's avatar
Sakari Ailus committed
237 238 239 240
	if (rval < 0)
		return rval;

	return smiapp_write(
241
		sensor, SMIAPP_REG_U16_OP_SYS_CLK_DIV, pll->op.sys_clk_div);
Sakari Ailus's avatar
Sakari Ailus committed
242 243
}

244 245
static int smiapp_pll_try(struct smiapp_sensor *sensor,
			  struct smiapp_pll *pll)
Sakari Ailus's avatar
Sakari Ailus committed
246 247 248 249 250 251 252 253 254 255 256 257
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	struct smiapp_pll_limits lim = {
		.min_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_PRE_PLL_CLK_DIV],
		.max_pre_pll_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_PRE_PLL_CLK_DIV],
		.min_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_IP_FREQ_HZ],
		.max_pll_ip_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_IP_FREQ_HZ],
		.min_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MIN_PLL_MULTIPLIER],
		.max_pll_multiplier = sensor->limits[SMIAPP_LIMIT_MAX_PLL_MULTIPLIER],
		.min_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_PLL_OP_FREQ_HZ],
		.max_pll_op_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_PLL_OP_FREQ_HZ],

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
		.op.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV],
		.op.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV],
		.op.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV],
		.op.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV],
		.op.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_FREQ_HZ],
		.op.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_FREQ_HZ],
		.op.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_FREQ_HZ],
		.op.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_FREQ_HZ],

		.vt.min_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_DIV],
		.vt.max_sys_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_DIV],
		.vt.min_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_DIV],
		.vt.max_pix_clk_div = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_DIV],
		.vt.min_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_SYS_CLK_FREQ_HZ],
		.vt.max_sys_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_SYS_CLK_FREQ_HZ],
		.vt.min_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MIN_VT_PIX_CLK_FREQ_HZ],
		.vt.max_pix_clk_freq_hz = sensor->limits[SMIAPP_LIMIT_MAX_VT_PIX_CLK_FREQ_HZ],
Sakari Ailus's avatar
Sakari Ailus committed
275 276 277 278

		.min_line_length_pck_bin = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN],
		.min_line_length_pck = sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK],
	};
279 280 281 282 283 284

	return smiapp_pll_calculate(&client->dev, &lim, pll);
}

static int smiapp_pll_update(struct smiapp_sensor *sensor)
{
Sakari Ailus's avatar
Sakari Ailus committed
285 286 287 288 289 290 291 292 293 294
	struct smiapp_pll *pll = &sensor->pll;
	int rval;

	pll->binning_horizontal = sensor->binning_horizontal;
	pll->binning_vertical = sensor->binning_vertical;
	pll->link_freq =
		sensor->link_freq->qmenu_int[sensor->link_freq->val];
	pll->scale_m = sensor->scale_m;
	pll->bits_per_pixel = sensor->csi_format->compressed;

295
	rval = smiapp_pll_try(sensor, pll);
Sakari Ailus's avatar
Sakari Ailus committed
296 297 298
	if (rval < 0)
		return rval;

299
	__v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_parray,
300
				 pll->pixel_rate_pixel_array);
301
	__v4l2_ctrl_s_ctrl_int64(sensor->pixel_rate_csi, pll->pixel_rate_csi);
Sakari Ailus's avatar
Sakari Ailus committed
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

	return 0;
}


/*
 *
 * V4L2 Controls handling
 *
 */

static void __smiapp_update_exposure_limits(struct smiapp_sensor *sensor)
{
	struct v4l2_ctrl *ctrl = sensor->exposure;
	int max;

	max = sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
		+ sensor->vblank->val
		- sensor->limits[SMIAPP_LIMIT_COARSE_INTEGRATION_TIME_MAX_MARGIN];

322
	__v4l2_ctrl_modify_range(ctrl, ctrl->minimum, max, ctrl->step, max);
Sakari Ailus's avatar
Sakari Ailus committed
323 324 325 326 327 328 329 330 331 332 333
}

/*
 * Order matters.
 *
 * 1. Bits-per-pixel, descending.
 * 2. Bits-per-pixel compressed, descending.
 * 3. Pixel order, same as in pixel_order_str. Formats for all four pixel
 *    orders must be defined.
 */
static const struct smiapp_csi_data_format smiapp_csi_data_formats[] = {
334 335 336 337 338 339 340 341
	{ MEDIA_BUS_FMT_SGRBG16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_GRBG, },
	{ MEDIA_BUS_FMT_SRGGB16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_RGGB, },
	{ MEDIA_BUS_FMT_SBGGR16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_BGGR, },
	{ MEDIA_BUS_FMT_SGBRG16_1X16, 16, 16, SMIAPP_PIXEL_ORDER_GBRG, },
	{ MEDIA_BUS_FMT_SGRBG14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_GRBG, },
	{ MEDIA_BUS_FMT_SRGGB14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_RGGB, },
	{ MEDIA_BUS_FMT_SBGGR14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_BGGR, },
	{ MEDIA_BUS_FMT_SGBRG14_1X14, 14, 14, SMIAPP_PIXEL_ORDER_GBRG, },
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
	{ MEDIA_BUS_FMT_SGRBG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GRBG, },
	{ MEDIA_BUS_FMT_SRGGB12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_RGGB, },
	{ MEDIA_BUS_FMT_SBGGR12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_BGGR, },
	{ MEDIA_BUS_FMT_SGBRG12_1X12, 12, 12, SMIAPP_PIXEL_ORDER_GBRG, },
	{ MEDIA_BUS_FMT_SGRBG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GRBG, },
	{ MEDIA_BUS_FMT_SRGGB10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_RGGB, },
	{ MEDIA_BUS_FMT_SBGGR10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_BGGR, },
	{ MEDIA_BUS_FMT_SGBRG10_1X10, 10, 10, SMIAPP_PIXEL_ORDER_GBRG, },
	{ MEDIA_BUS_FMT_SGRBG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GRBG, },
	{ MEDIA_BUS_FMT_SRGGB10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_RGGB, },
	{ MEDIA_BUS_FMT_SBGGR10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_BGGR, },
	{ MEDIA_BUS_FMT_SGBRG10_DPCM8_1X8, 10, 8, SMIAPP_PIXEL_ORDER_GBRG, },
	{ MEDIA_BUS_FMT_SGRBG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GRBG, },
	{ MEDIA_BUS_FMT_SRGGB8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_RGGB, },
	{ MEDIA_BUS_FMT_SBGGR8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_BGGR, },
	{ MEDIA_BUS_FMT_SGBRG8_1X8, 8, 8, SMIAPP_PIXEL_ORDER_GBRG, },
Sakari Ailus's avatar
Sakari Ailus committed
358 359
};

360
static const char *pixel_order_str[] = { "GRBG", "RGGB", "BGGR", "GBRG" };
Sakari Ailus's avatar
Sakari Ailus committed
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 398 399 400 401 402 403 404 405 406 407 408

#define to_csi_format_idx(fmt) (((unsigned long)(fmt)			\
				 - (unsigned long)smiapp_csi_data_formats) \
				/ sizeof(*smiapp_csi_data_formats))

static u32 smiapp_pixel_order(struct smiapp_sensor *sensor)
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	int flip = 0;

	if (sensor->hflip) {
		if (sensor->hflip->val)
			flip |= SMIAPP_IMAGE_ORIENTATION_HFLIP;

		if (sensor->vflip->val)
			flip |= SMIAPP_IMAGE_ORIENTATION_VFLIP;
	}

	flip ^= sensor->hvflip_inv_mask;

	dev_dbg(&client->dev, "flip %d\n", flip);
	return sensor->default_pixel_order ^ flip;
}

static void smiapp_update_mbus_formats(struct smiapp_sensor *sensor)
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	unsigned int csi_format_idx =
		to_csi_format_idx(sensor->csi_format) & ~3;
	unsigned int internal_csi_format_idx =
		to_csi_format_idx(sensor->internal_csi_format) & ~3;
	unsigned int pixel_order = smiapp_pixel_order(sensor);

	sensor->mbus_frame_fmts =
		sensor->default_mbus_frame_fmts << pixel_order;
	sensor->csi_format =
		&smiapp_csi_data_formats[csi_format_idx + pixel_order];
	sensor->internal_csi_format =
		&smiapp_csi_data_formats[internal_csi_format_idx
					 + pixel_order];

	BUG_ON(max(internal_csi_format_idx, csi_format_idx) + pixel_order
	       >= ARRAY_SIZE(smiapp_csi_data_formats));

	dev_dbg(&client->dev, "new pixel order %s\n",
		pixel_order_str[pixel_order]);
}

409 410 411 412 413 414 415 416
static const char * const smiapp_test_patterns[] = {
	"Disabled",
	"Solid Colour",
	"Eight Vertical Colour Bars",
	"Colour Bars With Fade to Grey",
	"Pseudorandom Sequence (PN9)",
};

Sakari Ailus's avatar
Sakari Ailus committed
417 418 419 420 421 422 423 424 425 426 427 428
static int smiapp_set_ctrl(struct v4l2_ctrl *ctrl)
{
	struct smiapp_sensor *sensor =
		container_of(ctrl->handler, struct smiapp_subdev, ctrl_handler)
			->sensor;
	u32 orient = 0;
	int exposure;
	int rval;

	switch (ctrl->id) {
	case V4L2_CID_ANALOGUE_GAIN:
		return smiapp_write(
429
			sensor,
Sakari Ailus's avatar
Sakari Ailus committed
430 431 432 433
			SMIAPP_REG_U16_ANALOGUE_GAIN_CODE_GLOBAL, ctrl->val);

	case V4L2_CID_EXPOSURE:
		return smiapp_write(
434
			sensor,
Sakari Ailus's avatar
Sakari Ailus committed
435 436 437 438 439 440 441 442 443 444 445 446 447 448
			SMIAPP_REG_U16_COARSE_INTEGRATION_TIME, ctrl->val);

	case V4L2_CID_HFLIP:
	case V4L2_CID_VFLIP:
		if (sensor->streaming)
			return -EBUSY;

		if (sensor->hflip->val)
			orient |= SMIAPP_IMAGE_ORIENTATION_HFLIP;

		if (sensor->vflip->val)
			orient |= SMIAPP_IMAGE_ORIENTATION_VFLIP;

		orient ^= sensor->hvflip_inv_mask;
449
		rval = smiapp_write(sensor, SMIAPP_REG_U8_IMAGE_ORIENTATION,
Sakari Ailus's avatar
Sakari Ailus committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463
				    orient);
		if (rval < 0)
			return rval;

		smiapp_update_mbus_formats(sensor);

		return 0;

	case V4L2_CID_VBLANK:
		exposure = sensor->exposure->val;

		__smiapp_update_exposure_limits(sensor);

		if (exposure > sensor->exposure->maximum) {
464 465
			sensor->exposure->val =	sensor->exposure->maximum;
			rval = smiapp_set_ctrl(sensor->exposure);
Sakari Ailus's avatar
Sakari Ailus committed
466 467 468 469 470
			if (rval < 0)
				return rval;
		}

		return smiapp_write(
471
			sensor, SMIAPP_REG_U16_FRAME_LENGTH_LINES,
Sakari Ailus's avatar
Sakari Ailus committed
472 473 474 475 476
			sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
			+ ctrl->val);

	case V4L2_CID_HBLANK:
		return smiapp_write(
477
			sensor, SMIAPP_REG_U16_LINE_LENGTH_PCK,
Sakari Ailus's avatar
Sakari Ailus committed
478 479 480 481 482 483 484 485 486
			sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
			+ ctrl->val);

	case V4L2_CID_LINK_FREQ:
		if (sensor->streaming)
			return -EBUSY;

		return smiapp_pll_update(sensor);

487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
	case V4L2_CID_TEST_PATTERN: {
		unsigned int i;

		for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
			v4l2_ctrl_activate(
				sensor->test_data[i],
				ctrl->val ==
				V4L2_SMIAPP_TEST_PATTERN_MODE_SOLID_COLOUR);

		return smiapp_write(
			sensor, SMIAPP_REG_U16_TEST_PATTERN_MODE, ctrl->val);
	}

	case V4L2_CID_TEST_PATTERN_RED:
		return smiapp_write(
			sensor, SMIAPP_REG_U16_TEST_DATA_RED, ctrl->val);

	case V4L2_CID_TEST_PATTERN_GREENR:
		return smiapp_write(
			sensor, SMIAPP_REG_U16_TEST_DATA_GREENR, ctrl->val);

	case V4L2_CID_TEST_PATTERN_BLUE:
		return smiapp_write(
			sensor, SMIAPP_REG_U16_TEST_DATA_BLUE, ctrl->val);

	case V4L2_CID_TEST_PATTERN_GREENB:
		return smiapp_write(
			sensor, SMIAPP_REG_U16_TEST_DATA_GREENB, ctrl->val);

516 517 518 519
	case V4L2_CID_PIXEL_RATE:
		/* For v4l2_ctrl_s_ctrl_int64() used internally. */
		return 0;

Sakari Ailus's avatar
Sakari Ailus committed
520 521 522 523 524 525 526 527 528 529 530 531 532 533
	default:
		return -EINVAL;
	}
}

static const struct v4l2_ctrl_ops smiapp_ctrl_ops = {
	.s_ctrl = smiapp_set_ctrl,
};

static int smiapp_init_controls(struct smiapp_sensor *sensor)
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	int rval;

534
	rval = v4l2_ctrl_handler_init(&sensor->pixel_array->ctrl_handler, 12);
Sakari Ailus's avatar
Sakari Ailus committed
535 536
	if (rval)
		return rval;
537

Sakari Ailus's avatar
Sakari Ailus committed
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
	sensor->pixel_array->ctrl_handler.lock = &sensor->mutex;

	sensor->analog_gain = v4l2_ctrl_new_std(
		&sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
		V4L2_CID_ANALOGUE_GAIN,
		sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN],
		sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MAX],
		max(sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_STEP], 1U),
		sensor->limits[SMIAPP_LIMIT_ANALOGUE_GAIN_CODE_MIN]);

	/* Exposure limits will be updated soon, use just something here. */
	sensor->exposure = v4l2_ctrl_new_std(
		&sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
		V4L2_CID_EXPOSURE, 0, 0, 1, 0);

	sensor->hflip = v4l2_ctrl_new_std(
		&sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
		V4L2_CID_HFLIP, 0, 1, 1, 0);
	sensor->vflip = v4l2_ctrl_new_std(
		&sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
		V4L2_CID_VFLIP, 0, 1, 1, 0);

	sensor->vblank = v4l2_ctrl_new_std(
		&sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
		V4L2_CID_VBLANK, 0, 1, 1, 0);

	if (sensor->vblank)
		sensor->vblank->flags |= V4L2_CTRL_FLAG_UPDATE;

	sensor->hblank = v4l2_ctrl_new_std(
		&sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
		V4L2_CID_HBLANK, 0, 1, 1, 0);

	if (sensor->hblank)
		sensor->hblank->flags |= V4L2_CTRL_FLAG_UPDATE;

	sensor->pixel_rate_parray = v4l2_ctrl_new_std(
		&sensor->pixel_array->ctrl_handler, &smiapp_ctrl_ops,
576
		V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
Sakari Ailus's avatar
Sakari Ailus committed
577

578 579 580 581 582
	v4l2_ctrl_new_std_menu_items(&sensor->pixel_array->ctrl_handler,
				     &smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN,
				     ARRAY_SIZE(smiapp_test_patterns) - 1,
				     0, 0, smiapp_test_patterns);

Sakari Ailus's avatar
Sakari Ailus committed
583 584 585 586
	if (sensor->pixel_array->ctrl_handler.error) {
		dev_err(&client->dev,
			"pixel array controls initialization failed (%d)\n",
			sensor->pixel_array->ctrl_handler.error);
587
		return sensor->pixel_array->ctrl_handler.error;
Sakari Ailus's avatar
Sakari Ailus committed
588 589 590 591 592 593 594 595 596
	}

	sensor->pixel_array->sd.ctrl_handler =
		&sensor->pixel_array->ctrl_handler;

	v4l2_ctrl_cluster(2, &sensor->hflip);

	rval = v4l2_ctrl_handler_init(&sensor->src->ctrl_handler, 0);
	if (rval)
597 598
		return rval;

Sakari Ailus's avatar
Sakari Ailus committed
599 600 601 602
	sensor->src->ctrl_handler.lock = &sensor->mutex;

	sensor->pixel_rate_csi = v4l2_ctrl_new_std(
		&sensor->src->ctrl_handler, &smiapp_ctrl_ops,
603
		V4L2_CID_PIXEL_RATE, 1, INT_MAX, 1, 1);
Sakari Ailus's avatar
Sakari Ailus committed
604 605 606 607 608

	if (sensor->src->ctrl_handler.error) {
		dev_err(&client->dev,
			"src controls initialization failed (%d)\n",
			sensor->src->ctrl_handler.error);
609
		return sensor->src->ctrl_handler.error;
Sakari Ailus's avatar
Sakari Ailus committed
610 611
	}

612
	sensor->src->sd.ctrl_handler = &sensor->src->ctrl_handler;
Sakari Ailus's avatar
Sakari Ailus committed
613 614 615 616

	return 0;
}

617 618 619 620 621 622 623
/*
 * For controls that require information on available media bus codes
 * and linke frequencies.
 */
static int smiapp_init_late_controls(struct smiapp_sensor *sensor)
{
	unsigned long *valid_link_freqs = &sensor->valid_link_freqs[
624
		sensor->csi_format->compressed - sensor->compressed_min_bpp];
625 626 627 628 629 630 631 632 633 634 635
	unsigned int max, i;

	for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++) {
		int max_value = (1 << sensor->csi_format->width) - 1;

		sensor->test_data[i] = v4l2_ctrl_new_std(
				&sensor->pixel_array->ctrl_handler,
				&smiapp_ctrl_ops, V4L2_CID_TEST_PATTERN_RED + i,
				0, max_value, 1, max_value);
	}

636
	for (max = 0; sensor->hwcfg->op_sys_clock[max + 1]; max++);
637 638 639 640

	sensor->link_freq = v4l2_ctrl_new_int_menu(
		&sensor->src->ctrl_handler, &smiapp_ctrl_ops,
		V4L2_CID_LINK_FREQ, __fls(*valid_link_freqs),
641
		__ffs(*valid_link_freqs), sensor->hwcfg->op_sys_clock);
642 643 644 645

	return sensor->src->ctrl_handler.error;
}

Sakari Ailus's avatar
Sakari Ailus committed
646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663
static void smiapp_free_controls(struct smiapp_sensor *sensor)
{
	unsigned int i;

	for (i = 0; i < sensor->ssds_used; i++)
		v4l2_ctrl_handler_free(&sensor->ssds[i].ctrl_handler);
}

static int smiapp_get_limits(struct smiapp_sensor *sensor, int const *limit,
			     unsigned int n)
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	unsigned int i;
	u32 val;
	int rval;

	for (i = 0; i < n; i++) {
		rval = smiapp_read(
664
			sensor, smiapp_reg_limits[limit[i]].addr, &val);
Sakari Ailus's avatar
Sakari Ailus committed
665 666 667
		if (rval)
			return rval;
		sensor->limits[limit[i]] = val;
668
		dev_dbg(&client->dev, "0x%8.8x \"%s\" = %u, 0x%x\n",
Sakari Ailus's avatar
Sakari Ailus committed
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
			smiapp_reg_limits[limit[i]].addr,
			smiapp_reg_limits[limit[i]].what, val, val);
	}

	return 0;
}

static int smiapp_get_all_limits(struct smiapp_sensor *sensor)
{
	unsigned int i;
	int rval;

	for (i = 0; i < SMIAPP_LIMIT_LAST; i++) {
		rval = smiapp_get_limits(sensor, &i, 1);
		if (rval < 0)
			return rval;
	}

	if (sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] == 0)
		smiapp_replace_limit(sensor, SMIAPP_LIMIT_SCALER_N_MIN, 16);

	return 0;
}

static int smiapp_get_limits_binning(struct smiapp_sensor *sensor)
{
695
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
Sakari Ailus's avatar
Sakari Ailus committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713
	static u32 const limits[] = {
		SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN,
		SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN,
		SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN,
		SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN,
		SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN,
		SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN_BIN,
		SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN_BIN,
	};
	static u32 const limits_replace[] = {
		SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES,
		SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES,
		SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK,
		SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK,
		SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK,
		SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MIN,
		SMIAPP_LIMIT_FINE_INTEGRATION_TIME_MAX_MARGIN,
	};
714 715
	unsigned int i;
	int rval;
Sakari Ailus's avatar
Sakari Ailus committed
716 717 718 719 720 721 722 723 724 725

	if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY] ==
	    SMIAPP_BINNING_CAPABILITY_NO) {
		for (i = 0; i < ARRAY_SIZE(limits); i++)
			sensor->limits[limits[i]] =
				sensor->limits[limits_replace[i]];

		return 0;
	}

726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
	rval = smiapp_get_limits(sensor, limits, ARRAY_SIZE(limits));
	if (rval < 0)
		return rval;

	/*
	 * Sanity check whether the binning limits are valid. If not,
	 * use the non-binning ones.
	 */
	if (sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN]
	    && sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN]
	    && sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN])
		return 0;

	for (i = 0; i < ARRAY_SIZE(limits); i++) {
		dev_dbg(&client->dev,
			"replace limit 0x%8.8x \"%s\" = %d, 0x%x\n",
			smiapp_reg_limits[limits[i]].addr,
			smiapp_reg_limits[limits[i]].what,
			sensor->limits[limits_replace[i]],
			sensor->limits[limits_replace[i]]);
		sensor->limits[limits[i]] =
			sensor->limits[limits_replace[i]];
	}

	return 0;
Sakari Ailus's avatar
Sakari Ailus committed
751 752 753 754 755
}

static int smiapp_get_mbus_formats(struct smiapp_sensor *sensor)
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
756
	struct smiapp_pll *pll = &sensor->pll;
757
	u8 compressed_max_bpp = 0;
Sakari Ailus's avatar
Sakari Ailus committed
758 759 760 761 762
	unsigned int type, n;
	unsigned int i, pixel_order;
	int rval;

	rval = smiapp_read(
763
		sensor, SMIAPP_REG_U8_DATA_FORMAT_MODEL_TYPE, &type);
Sakari Ailus's avatar
Sakari Ailus committed
764 765 766 767 768
	if (rval)
		return rval;

	dev_dbg(&client->dev, "data_format_model_type %d\n", type);

769
	rval = smiapp_read(sensor, SMIAPP_REG_U8_PIXEL_ORDER,
Sakari Ailus's avatar
Sakari Ailus committed
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 797 798 799
			   &pixel_order);
	if (rval)
		return rval;

	if (pixel_order >= ARRAY_SIZE(pixel_order_str)) {
		dev_dbg(&client->dev, "bad pixel order %d\n", pixel_order);
		return -EINVAL;
	}

	dev_dbg(&client->dev, "pixel order %d (%s)\n", pixel_order,
		pixel_order_str[pixel_order]);

	switch (type) {
	case SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL:
		n = SMIAPP_DATA_FORMAT_MODEL_TYPE_NORMAL_N;
		break;
	case SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED:
		n = SMIAPP_DATA_FORMAT_MODEL_TYPE_EXTENDED_N;
		break;
	default:
		return -EINVAL;
	}

	sensor->default_pixel_order = pixel_order;
	sensor->mbus_frame_fmts = 0;

	for (i = 0; i < n; i++) {
		unsigned int fmt, j;

		rval = smiapp_read(
800
			sensor,
Sakari Ailus's avatar
Sakari Ailus committed
801 802 803 804
			SMIAPP_REG_U16_DATA_FORMAT_DESCRIPTOR(i), &fmt);
		if (rval)
			return rval;

805 806
		dev_dbg(&client->dev, "%u: bpp %u, compressed %u\n",
			i, fmt >> 8, (u8)fmt);
Sakari Ailus's avatar
Sakari Ailus committed
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823

		for (j = 0; j < ARRAY_SIZE(smiapp_csi_data_formats); j++) {
			const struct smiapp_csi_data_format *f =
				&smiapp_csi_data_formats[j];

			if (f->pixel_order != SMIAPP_PIXEL_ORDER_GRBG)
				continue;

			if (f->width != fmt >> 8 || f->compressed != (u8)fmt)
				continue;

			dev_dbg(&client->dev, "jolly good! %d\n", j);

			sensor->default_mbus_frame_fmts |= 1 << j;
		}
	}

824 825 826 827 828
	/* Figure out which BPP values can be used with which formats. */
	pll->binning_horizontal = 1;
	pll->binning_vertical = 1;
	pll->scale_m = sensor->scale_m;

829 830 831 832 833 834 835 836 837 838 839 840 841 842
	for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
		sensor->compressed_min_bpp =
			min(smiapp_csi_data_formats[i].compressed,
			    sensor->compressed_min_bpp);
		compressed_max_bpp =
			max(smiapp_csi_data_formats[i].compressed,
			    compressed_max_bpp);
	}

	sensor->valid_link_freqs = devm_kcalloc(
		&client->dev,
		compressed_max_bpp - sensor->compressed_min_bpp + 1,
		sizeof(*sensor->valid_link_freqs), GFP_KERNEL);

843 844 845 846 847
	for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
		const struct smiapp_csi_data_format *f =
			&smiapp_csi_data_formats[i];
		unsigned long *valid_link_freqs =
			&sensor->valid_link_freqs[
848
				f->compressed - sensor->compressed_min_bpp];
849 850 851 852 853 854 855
		unsigned int j;

		if (!(sensor->default_mbus_frame_fmts & 1 << i))
			continue;

		pll->bits_per_pixel = f->compressed;

856 857
		for (j = 0; sensor->hwcfg->op_sys_clock[j]; j++) {
			pll->link_freq = sensor->hwcfg->op_sys_clock[j];
858 859 860 861 862 863 864 865 866 867

			rval = smiapp_pll_try(sensor, pll);
			dev_dbg(&client->dev, "link freq %u Hz, bpp %u %s\n",
				pll->link_freq, pll->bits_per_pixel,
				rval ? "not ok" : "ok");
			if (rval)
				continue;

			set_bit(j, valid_link_freqs);
		}
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882

		if (!*valid_link_freqs) {
			dev_info(&client->dev,
				 "no valid link frequencies for %u bpp\n",
				 f->compressed);
			sensor->default_mbus_frame_fmts &= ~BIT(i);
			continue;
		}

		if (!sensor->csi_format
		    || f->width > sensor->csi_format->width
		    || (f->width == sensor->csi_format->width
			&& f->compressed > sensor->csi_format->compressed)) {
			sensor->csi_format = f;
			sensor->internal_csi_format = f;
Sakari Ailus's avatar
Sakari Ailus committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
		}
	}

	if (!sensor->csi_format) {
		dev_err(&client->dev, "no supported mbus code found\n");
		return -EINVAL;
	}

	smiapp_update_mbus_formats(sensor);

	return 0;
}

static void smiapp_update_blanking(struct smiapp_sensor *sensor)
{
	struct v4l2_ctrl *vblank = sensor->vblank;
	struct v4l2_ctrl *hblank = sensor->hblank;
900
	int min, max;
Sakari Ailus's avatar
Sakari Ailus committed
901

902 903 904 905 906
	min = max_t(int,
		    sensor->limits[SMIAPP_LIMIT_MIN_FRAME_BLANKING_LINES],
		    sensor->limits[SMIAPP_LIMIT_MIN_FRAME_LENGTH_LINES_BIN] -
		    sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height);
	max = sensor->limits[SMIAPP_LIMIT_MAX_FRAME_LENGTH_LINES_BIN] -
Sakari Ailus's avatar
Sakari Ailus committed
907 908
		sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height;

909 910 911 912 913 914 915
	__v4l2_ctrl_modify_range(vblank, min, max, vblank->step, min);

	min = max_t(int,
		    sensor->limits[SMIAPP_LIMIT_MIN_LINE_LENGTH_PCK_BIN] -
		    sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width,
		    sensor->limits[SMIAPP_LIMIT_MIN_LINE_BLANKING_PCK_BIN]);
	max = sensor->limits[SMIAPP_LIMIT_MAX_LINE_LENGTH_PCK_BIN] -
Sakari Ailus's avatar
Sakari Ailus committed
916 917
		sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width;

918
	__v4l2_ctrl_modify_range(hblank, min, max, hblank->step, min);
Sakari Ailus's avatar
Sakari Ailus committed
919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938

	__smiapp_update_exposure_limits(sensor);
}

static int smiapp_update_mode(struct smiapp_sensor *sensor)
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	unsigned int binning_mode;
	int rval;

	/* Binning has to be set up here; it affects limits */
	if (sensor->binning_horizontal == 1 &&
	    sensor->binning_vertical == 1) {
		binning_mode = 0;
	} else {
		u8 binning_type =
			(sensor->binning_horizontal << 4)
			| sensor->binning_vertical;

		rval = smiapp_write(
939
			sensor, SMIAPP_REG_U8_BINNING_TYPE, binning_type);
Sakari Ailus's avatar
Sakari Ailus committed
940 941 942 943 944
		if (rval < 0)
			return rval;

		binning_mode = 1;
	}
945
	rval = smiapp_write(sensor, SMIAPP_REG_U8_BINNING_MODE, binning_mode);
Sakari Ailus's avatar
Sakari Ailus committed
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
	if (rval < 0)
		return rval;

	/* Get updated limits due to binning */
	rval = smiapp_get_limits_binning(sensor);
	if (rval < 0)
		return rval;

	rval = smiapp_pll_update(sensor);
	if (rval < 0)
		return rval;

	/* Output from pixel array, including blanking */
	smiapp_update_blanking(sensor);

	dev_dbg(&client->dev, "vblank\t\t%d\n", sensor->vblank->val);
	dev_dbg(&client->dev, "hblank\t\t%d\n", sensor->hblank->val);

	dev_dbg(&client->dev, "real timeperframe\t100/%d\n",
965
		sensor->pll.pixel_rate_pixel_array /
Sakari Ailus's avatar
Sakari Ailus committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
		((sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width
		  + sensor->hblank->val) *
		 (sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height
		  + sensor->vblank->val) / 100));

	return 0;
}

/*
 *
 * SMIA++ NVM handling
 *
 */
static int smiapp_read_nvm(struct smiapp_sensor *sensor,
			   unsigned char *nvm)
{
	u32 i, s, p, np, v;
983
	int rval = 0, rval2;
Sakari Ailus's avatar
Sakari Ailus committed
984 985 986 987

	np = sensor->nvm_size / SMIAPP_NVM_PAGE_SIZE;
	for (p = 0; p < np; p++) {
		rval = smiapp_write(
988
			sensor,
Sakari Ailus's avatar
Sakari Ailus committed
989 990 991 992
			SMIAPP_REG_U8_DATA_TRANSFER_IF_1_PAGE_SELECT, p);
		if (rval)
			goto out;

993
		rval = smiapp_write(sensor,
Sakari Ailus's avatar
Sakari Ailus committed
994 995 996 997 998 999 1000 1001
				    SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL,
				    SMIAPP_DATA_TRANSFER_IF_1_CTRL_EN |
				    SMIAPP_DATA_TRANSFER_IF_1_CTRL_RD_EN);
		if (rval)
			goto out;

		for (i = 0; i < 1000; i++) {
			rval = smiapp_read(
1002
				sensor,
Sakari Ailus's avatar
Sakari Ailus committed
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019
				SMIAPP_REG_U8_DATA_TRANSFER_IF_1_STATUS, &s);

			if (rval)
				goto out;

			if (s & SMIAPP_DATA_TRANSFER_IF_1_STATUS_RD_READY)
				break;

			if (--i == 0) {
				rval = -ETIMEDOUT;
				goto out;
			}

		}

		for (i = 0; i < SMIAPP_NVM_PAGE_SIZE; i++) {
			rval = smiapp_read(
1020
				sensor,
Sakari Ailus's avatar
Sakari Ailus committed
1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
				SMIAPP_REG_U8_DATA_TRANSFER_IF_1_DATA_0 + i,
				&v);
			if (rval)
				goto out;

			*nvm++ = v;
		}
	}

out:
1031
	rval2 = smiapp_write(sensor, SMIAPP_REG_U8_DATA_TRANSFER_IF_1_CTRL, 0);
Sakari Ailus's avatar
Sakari Ailus committed
1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048
	if (rval < 0)
		return rval;
	else
		return rval2;
}

/*
 *
 * SMIA++ CCI address control
 *
 */
static int smiapp_change_cci_addr(struct smiapp_sensor *sensor)
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	int rval;
	u32 val;

1049
	client->addr = sensor->hwcfg->i2c_addr_dfl;
Sakari Ailus's avatar
Sakari Ailus committed
1050

1051
	rval = smiapp_write(sensor,
Sakari Ailus's avatar
Sakari Ailus committed
1052
			    SMIAPP_REG_U8_CCI_ADDRESS_CONTROL,
1053
			    sensor->hwcfg->i2c_addr_alt << 1);
Sakari Ailus's avatar
Sakari Ailus committed
1054 1055 1056
	if (rval)
		return rval;

1057
	client->addr = sensor->hwcfg->i2c_addr_alt;
Sakari Ailus's avatar
Sakari Ailus committed
1058 1059

	/* verify addr change went ok */
1060
	rval = smiapp_read(sensor, SMIAPP_REG_U8_CCI_ADDRESS_CONTROL, &val);
Sakari Ailus's avatar
Sakari Ailus committed
1061 1062 1063
	if (rval)
		return rval;

1064
	if (val != sensor->hwcfg->i2c_addr_alt << 1)
Sakari Ailus's avatar
Sakari Ailus committed
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
		return -ENODEV;

	return 0;
}

/*
 *
 * SMIA++ Mode Control
 *
 */
static int smiapp_setup_flash_strobe(struct smiapp_sensor *sensor)
{
	struct smiapp_flash_strobe_parms *strobe_setup;
1078
	unsigned int ext_freq = sensor->hwcfg->ext_clk;
Sakari Ailus's avatar
Sakari Ailus committed
1079 1080 1081 1082 1083
	u32 tmp;
	u32 strobe_adjustment;
	u32 strobe_width_high_rs;
	int rval;

1084
	strobe_setup = sensor->hwcfg->strobe_setup;
Sakari Ailus's avatar
Sakari Ailus committed
1085 1086 1087 1088 1089 1090

	/*
	 * How to calculate registers related to strobe length. Please
	 * do not change, or if you do at least know what you're
	 * doing. :-)
	 *
1091
	 * Sakari Ailus <sakari.ailus@iki.fi> 2010-10-25
Sakari Ailus's avatar
Sakari Ailus committed
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
	 *
	 * flash_strobe_length [us] / 10^6 = (tFlash_strobe_width_ctrl
	 *	/ EXTCLK freq [Hz]) * flash_strobe_adjustment
	 *
	 * tFlash_strobe_width_ctrl E N, [1 - 0xffff]
	 * flash_strobe_adjustment E N, [1 - 0xff]
	 *
	 * The formula above is written as below to keep it on one
	 * line:
	 *
	 * l / 10^6 = w / e * a
	 *
	 * Let's mark w * a by x:
	 *
	 * x = w * a
	 *
	 * Thus, we get:
	 *
	 * x = l * e / 10^6
	 *
	 * The strobe width must be at least as long as requested,
	 * thus rounding upwards is needed.
	 *
	 * x = (l * e + 10^6 - 1) / 10^6
	 * -----------------------------
	 *
	 * Maximum possible accuracy is wanted at all times. Thus keep
	 * a as small as possible.
	 *
	 * Calculate a, assuming maximum w, with rounding upwards:
	 *
	 * a = (x + (2^16 - 1) - 1) / (2^16 - 1)
	 * -------------------------------------
	 *
	 * Thus, we also get w, with that a, with rounding upwards:
	 *
	 * w = (x + a - 1) / a
	 * -------------------
	 *
	 * To get limits:
	 *
	 * x E [1, (2^16 - 1) * (2^8 - 1)]
	 *
	 * Substituting maximum x to the original formula (with rounding),
	 * the maximum l is thus
	 *
	 * (2^16 - 1) * (2^8 - 1) * 10^6 = l * e + 10^6 - 1
	 *
	 * l = (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / e
	 * --------------------------------------------------
	 *
	 * flash_strobe_length must be clamped between 1 and
	 * (10^6 * (2^16 - 1) * (2^8 - 1) - 10^6 + 1) / EXTCLK freq.
	 *
	 * Then,
	 *
	 * flash_strobe_adjustment = ((flash_strobe_length *
	 *	EXTCLK freq + 10^6 - 1) / 10^6 + (2^16 - 1) - 1) / (2^16 - 1)
	 *
	 * tFlash_strobe_width_ctrl = ((flash_strobe_length *
	 *	EXTCLK freq + 10^6 - 1) / 10^6 +
	 *	flash_strobe_adjustment - 1) / flash_strobe_adjustment
	 */
	tmp = div_u64(1000000ULL * ((1 << 16) - 1) * ((1 << 8) - 1) -
		      1000000 + 1, ext_freq);
	strobe_setup->strobe_width_high_us =
		clamp_t(u32, strobe_setup->strobe_width_high_us, 1, tmp);

	tmp = div_u64(((u64)strobe_setup->strobe_width_high_us * (u64)ext_freq +
			1000000 - 1), 1000000ULL);
	strobe_adjustment = (tmp + (1 << 16) - 1 - 1) / ((1 << 16) - 1);
	strobe_width_high_rs = (tmp + strobe_adjustment - 1) /
				strobe_adjustment;

1166
	rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_MODE_RS,
Sakari Ailus's avatar
Sakari Ailus committed
1167 1168 1169 1170
			    strobe_setup->mode);
	if (rval < 0)
		goto out;

1171
	rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_STROBE_ADJUSTMENT,
Sakari Ailus's avatar
Sakari Ailus committed
1172 1173 1174 1175 1176
			    strobe_adjustment);
	if (rval < 0)
		goto out;

	rval = smiapp_write(
1177
		sensor, SMIAPP_REG_U16_TFLASH_STROBE_WIDTH_HIGH_RS_CTRL,
Sakari Ailus's avatar
Sakari Ailus committed
1178 1179 1180 1181
		strobe_width_high_rs);
	if (rval < 0)
		goto out;

1182
	rval = smiapp_write(sensor, SMIAPP_REG_U16_TFLASH_STROBE_DELAY_RS_CTRL,
Sakari Ailus's avatar
Sakari Ailus committed
1183 1184 1185 1186
			    strobe_setup->strobe_delay);
	if (rval < 0)
		goto out;

1187
	rval = smiapp_write(sensor, SMIAPP_REG_U16_FLASH_STROBE_START_POINT,
Sakari Ailus's avatar
Sakari Ailus committed
1188 1189 1190 1191
			    strobe_setup->stobe_start_point);
	if (rval < 0)
		goto out;

1192
	rval = smiapp_write(sensor, SMIAPP_REG_U8_FLASH_TRIGGER_RS,
Sakari Ailus's avatar
Sakari Ailus committed
1193 1194 1195
			    strobe_setup->trigger);

out:
1196
	sensor->hwcfg->strobe_setup->trigger = 0;
Sakari Ailus's avatar
Sakari Ailus committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217

	return rval;
}

/* -----------------------------------------------------------------------------
 * Power management
 */

static int smiapp_power_on(struct smiapp_sensor *sensor)
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	unsigned int sleep;
	int rval;

	rval = regulator_enable(sensor->vana);
	if (rval) {
		dev_err(&client->dev, "failed to enable vana regulator\n");
		return rval;
	}
	usleep_range(1000, 1000);

1218
	rval = clk_prepare_enable(sensor->ext_clk);
Sakari Ailus's avatar
Sakari Ailus committed
1219
	if (rval < 0) {
1220
		dev_dbg(&client->dev, "failed to enable xclk\n");
Sakari Ailus's avatar
Sakari Ailus committed
1221 1222 1223 1224
		goto out_xclk_fail;
	}
	usleep_range(1000, 1000);

1225
	gpiod_set_value(sensor->xshutdown, 1);
Sakari Ailus's avatar
Sakari Ailus committed
1226

1227
	sleep = SMIAPP_RESET_DELAY(sensor->hwcfg->ext_clk);
Sakari Ailus's avatar
Sakari Ailus committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
	usleep_range(sleep, sleep);

	/*
	 * Failures to respond to the address change command have been noticed.
	 * Those failures seem to be caused by the sensor requiring a longer
	 * boot time than advertised. An additional 10ms delay seems to work
	 * around the issue, but the SMIA++ I2C write retry hack makes the delay
	 * unnecessary. The failures need to be investigated to find a proper
	 * fix, and a delay will likely need to be added here if the I2C write
	 * retry hack is reverted before the root cause of the boot time issue
	 * is found.
	 */

1241
	if (sensor->hwcfg->i2c_addr_alt) {
Sakari Ailus's avatar
Sakari Ailus committed
1242 1243 1244 1245 1246 1247 1248
		rval = smiapp_change_cci_addr(sensor);
		if (rval) {
			dev_err(&client->dev, "cci address change error\n");
			goto out_cci_addr_fail;
		}
	}

1249
	rval = smiapp_write(sensor, SMIAPP_REG_U8_SOFTWARE_RESET,
Sakari Ailus's avatar
Sakari Ailus committed
1250 1251 1252 1253 1254 1255
			    SMIAPP_SOFTWARE_RESET);
	if (rval < 0) {
		dev_err(&client->dev, "software reset failed\n");
		goto out_cci_addr_fail;
	}

1256
	if (sensor->hwcfg->i2c_addr_alt) {
Sakari Ailus's avatar
Sakari Ailus committed
1257 1258 1259 1260 1261 1262 1263
		rval = smiapp_change_cci_addr(sensor);
		if (rval) {
			dev_err(&client->dev, "cci address change error\n");
			goto out_cci_addr_fail;
		}
	}

1264
	rval = smiapp_write(sensor, SMIAPP_REG_U16_COMPRESSION_MODE,
Sakari Ailus's avatar
Sakari Ailus committed
1265 1266 1267 1268 1269 1270 1271
			    SMIAPP_COMPRESSION_MODE_SIMPLE_PREDICTOR);
	if (rval) {
		dev_err(&client->dev, "compression mode set failed\n");
		goto out_cci_addr_fail;
	}

	rval = smiapp_write(
1272
		sensor, SMIAPP_REG_U16_EXTCLK_FREQUENCY_MHZ,
1273
		sensor->hwcfg->ext_clk / (1000000 / (1 << 8)));
Sakari Ailus's avatar
Sakari Ailus committed
1274 1275 1276 1277 1278
	if (rval) {
		dev_err(&client->dev, "extclk frequency set failed\n");
		goto out_cci_addr_fail;
	}

1279
	rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_LANE_MODE,
1280
			    sensor->hwcfg->lanes - 1);
Sakari Ailus's avatar
Sakari Ailus committed
1281 1282 1283 1284 1285
	if (rval) {
		dev_err(&client->dev, "csi lane mode set failed\n");
		goto out_cci_addr_fail;
	}

1286
	rval = smiapp_write(sensor, SMIAPP_REG_U8_FAST_STANDBY_CTRL,
Sakari Ailus's avatar
Sakari Ailus committed
1287 1288 1289 1290 1291 1292
			    SMIAPP_FAST_STANDBY_CTRL_IMMEDIATE);
	if (rval) {
		dev_err(&client->dev, "fast standby set failed\n");
		goto out_cci_addr_fail;
	}

1293
	rval = smiapp_write(sensor, SMIAPP_REG_U8_CSI_SIGNALLING_MODE,
1294
			    sensor->hwcfg->csi_signalling_mode);
Sakari Ailus's avatar
Sakari Ailus committed
1295 1296 1297 1298 1299 1300
	if (rval) {
		dev_err(&client->dev, "csi signalling mode set failed\n");
		goto out_cci_addr_fail;
	}

	/* DPHY control done by sensor based on requested link rate */
1301
	rval = smiapp_write(sensor, SMIAPP_REG_U8_DPHY_CTRL,
Sakari Ailus's avatar
Sakari Ailus committed
1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315
			    SMIAPP_DPHY_CTRL_UI);
	if (rval < 0)
		return rval;

	rval = smiapp_call_quirk(sensor, post_poweron);
	if (rval) {
		dev_err(&client->dev, "post_poweron quirks failed\n");
		goto out_cci_addr_fail;
	}

	/* Are we still initialising...? If yes, return here. */
	if (!sensor->pixel_array)
		return 0;

1316
	rval = v4l2_ctrl_handler_setup(&sensor->pixel_array->ctrl_handler);
Sakari Ailus's avatar
Sakari Ailus committed
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
	if (rval)
		goto out_cci_addr_fail;

	rval = v4l2_ctrl_handler_setup(&sensor->src->ctrl_handler);
	if (rval)
		goto out_cci_addr_fail;

	mutex_lock(&sensor->mutex);
	rval = smiapp_update_mode(sensor);
	mutex_unlock(&sensor->mutex);
	if (rval < 0)
		goto out_cci_addr_fail;

	return 0;

out_cci_addr_fail:
1333
	gpiod_set_value(sensor->xshutdown, 0);
1334
	clk_disable_unprepare(sensor->ext_clk);
Sakari Ailus's avatar
Sakari Ailus committed
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349

out_xclk_fail:
	regulator_disable(sensor->vana);
	return rval;
}

static void smiapp_power_off(struct smiapp_sensor *sensor)
{
	/*
	 * Currently power/clock to lens are enable/disabled separately
	 * but they are essentially the same signals. So if the sensor is
	 * powered off while the lens is powered on the sensor does not
	 * really see a power off and next time the cci address change
	 * will fail. So do a soft reset explicitly here.
	 */
1350
	if (sensor->hwcfg->i2c_addr_alt)
1351
		smiapp_write(sensor,
Sakari Ailus's avatar
Sakari Ailus committed
1352 1353 1354
			     SMIAPP_REG_U8_SOFTWARE_RESET,
			     SMIAPP_SOFTWARE_RESET);

1355
	gpiod_set_value(sensor->xshutdown, 0);
1356
	clk_disable_unprepare(sensor->ext_clk);
Sakari Ailus's avatar
Sakari Ailus committed
1357 1358
	usleep_range(5000, 5000);
	regulator_disable(sensor->vana);
1359
	sensor->streaming = false;
Sakari Ailus's avatar
Sakari Ailus committed
1360 1361 1362 1363 1364 1365 1366 1367 1368
}

static int smiapp_set_power(struct v4l2_subdev *subdev, int on)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	int ret = 0;

	mutex_lock(&sensor->power_mutex);

1369
	if (on && !sensor->power_count) {
Sakari Ailus's avatar
Sakari Ailus committed
1370 1371 1372 1373
		/* Power on and perform initialisation. */
		ret = smiapp_power_on(sensor);
		if (ret < 0)
			goto out;
1374
	} else if (!on && sensor->power_count == 1) {
Sakari Ailus's avatar
Sakari Ailus committed
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397
		smiapp_power_off(sensor);
	}

	/* Update the power count. */
	sensor->power_count += on ? 1 : -1;
	WARN_ON(sensor->power_count < 0);

out:
	mutex_unlock(&sensor->power_mutex);
	return ret;
}

/* -----------------------------------------------------------------------------
 * Video stream management
 */

static int smiapp_start_streaming(struct smiapp_sensor *sensor)
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	int rval;

	mutex_lock(&sensor->mutex);

1398
	rval = smiapp_write(sensor, SMIAPP_REG_U16_CSI_DATA_FORMAT,
Sakari Ailus's avatar
Sakari Ailus committed
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
			    (sensor->csi_format->width << 8) |
			    sensor->csi_format->compressed);
	if (rval)
		goto out;

	rval = smiapp_pll_configure(sensor);
	if (rval)
		goto out;

	/* Analog crop start coordinates */
1409
	rval = smiapp_write(sensor, SMIAPP_REG_U16_X_ADDR_START,
Sakari Ailus's avatar
Sakari Ailus committed
1410 1411 1412 1413
			    sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left);
	if (rval < 0)
		goto out;

1414
	rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_ADDR_START,
Sakari Ailus's avatar
Sakari Ailus committed
1415 1416 1417 1418 1419 1420
			    sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top);
	if (rval < 0)
		goto out;

	/* Analog crop end coordinates */
	rval = smiapp_write(
1421
		sensor, SMIAPP_REG_U16_X_ADDR_END,
Sakari Ailus's avatar
Sakari Ailus committed
1422 1423 1424 1425 1426 1427
		sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].left
		+ sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].width - 1);
	if (rval < 0)
		goto out;

	rval = smiapp_write(
1428
		sensor, SMIAPP_REG_U16_Y_ADDR_END,
Sakari Ailus's avatar
Sakari Ailus committed
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442
		sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].top
		+ sensor->pixel_array->crop[SMIAPP_PA_PAD_SRC].height - 1);
	if (rval < 0)
		goto out;

	/*
	 * Output from pixel array, including blanking, is set using
	 * controls below. No need to set here.
	 */

	/* Digital crop */
	if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
	    == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
		rval = smiapp_write(
1443
			sensor, SMIAPP_REG_U16_DIGITAL_CROP_X_OFFSET,
Sakari Ailus's avatar
Sakari Ailus committed
1444 1445 1446 1447 1448
			sensor->scaler->crop[SMIAPP_PAD_SINK].left);
		if (rval < 0)
			goto out;

		rval = smiapp_write(
1449
			sensor, SMIAPP_REG_U16_DIGITAL_CROP_Y_OFFSET,
Sakari Ailus's avatar
Sakari Ailus committed
1450 1451 1452 1453 1454
			sensor->scaler->crop[SMIAPP_PAD_SINK].top);
		if (rval < 0)
			goto out;

		rval = smiapp_write(
1455
			sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_WIDTH,
Sakari Ailus's avatar
Sakari Ailus committed
1456 1457 1458 1459 1460
			sensor->scaler->crop[SMIAPP_PAD_SINK].width);
		if (rval < 0)
			goto out;

		rval = smiapp_write(
1461
			sensor, SMIAPP_REG_U16_DIGITAL_CROP_IMAGE_HEIGHT,
Sakari Ailus's avatar
Sakari Ailus committed
1462 1463 1464 1465 1466 1467 1468 1469
			sensor->scaler->crop[SMIAPP_PAD_SINK].height);
		if (rval < 0)
			goto out;
	}

	/* Scaling */
	if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
	    != SMIAPP_SCALING_CAPABILITY_NONE) {
1470
		rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALING_MODE,
Sakari Ailus's avatar
Sakari Ailus committed
1471 1472 1473 1474
				    sensor->scaling_mode);
		if (rval < 0)
			goto out;

1475
		rval = smiapp_write(sensor, SMIAPP_REG_U16_SCALE_M,
Sakari Ailus's avatar
Sakari Ailus committed
1476 1477 1478 1479 1480 1481
				    sensor->scale_m);
		if (rval < 0)
			goto out;
	}

	/* Output size from sensor */
1482
	rval = smiapp_write(sensor, SMIAPP_REG_U16_X_OUTPUT_SIZE,
Sakari Ailus's avatar
Sakari Ailus committed
1483 1484 1485
			    sensor->src->crop[SMIAPP_PAD_SRC].width);
	if (rval < 0)
		goto out;
1486
	rval = smiapp_write(sensor, SMIAPP_REG_U16_Y_OUTPUT_SIZE,
Sakari Ailus's avatar
Sakari Ailus committed
1487 1488 1489 1490
			    sensor->src->crop[SMIAPP_PAD_SRC].height);
	if (rval < 0)
		goto out;

1491
	if ((sensor->limits[SMIAPP_LIMIT_FLASH_MODE_CAPABILITY] &
Sakari Ailus's avatar
Sakari Ailus committed
1492 1493
	     (SMIAPP_FLASH_MODE_CAPABILITY_SINGLE_STROBE |
	      SMIAPP_FLASH_MODE_CAPABILITY_MULTIPLE_STROBE)) &&
1494 1495
	    sensor->hwcfg->strobe_setup != NULL &&
	    sensor->hwcfg->strobe_setup->trigger != 0) {
Sakari Ailus's avatar
Sakari Ailus committed
1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506
		rval = smiapp_setup_flash_strobe(sensor);
		if (rval)
			goto out;
	}

	rval = smiapp_call_quirk(sensor, pre_streamon);
	if (rval) {
		dev_err(&client->dev, "pre_streamon quirks failed\n");
		goto out;
	}

1507
	rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
Sakari Ailus's avatar
Sakari Ailus committed
1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521
			    SMIAPP_MODE_SELECT_STREAMING);

out:
	mutex_unlock(&sensor->mutex);

	return rval;
}

static int smiapp_stop_streaming(struct smiapp_sensor *sensor)
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	int rval;

	mutex_lock(&sensor->mutex);
1522
	rval = smiapp_write(sensor, SMIAPP_REG_U8_MODE_SELECT,
Sakari Ailus's avatar
Sakari Ailus committed
1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548
			    SMIAPP_MODE_SELECT_SOFTWARE_STANDBY);
	if (rval)
		goto out;

	rval = smiapp_call_quirk(sensor, post_streamoff);
	if (rval)
		dev_err(&client->dev, "post_streamoff quirks failed\n");

out:
	mutex_unlock(&sensor->mutex);
	return rval;
}

/* -----------------------------------------------------------------------------
 * V4L2 subdev video operations
 */

static int smiapp_set_stream(struct v4l2_subdev *subdev, int enable)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	int rval;

	if (sensor->streaming == enable)
		return 0;

	if (enable) {
1549
		sensor->streaming = true;
Sakari Ailus's avatar
Sakari Ailus committed
1550 1551
		rval = smiapp_start_streaming(sensor);
		if (rval < 0)
1552
			sensor->streaming = false;
Sakari Ailus's avatar
Sakari Ailus committed
1553 1554
	} else {
		rval = smiapp_stop_streaming(sensor);
1555
		sensor->streaming = false;
Sakari Ailus's avatar
Sakari Ailus committed
1556 1557 1558 1559 1560 1561
	}

	return rval;
}

static int smiapp_enum_mbus_code(struct v4l2_subdev *subdev,
1562
				 struct v4l2_subdev_pad_config *cfg,
Sakari Ailus's avatar
Sakari Ailus committed
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615
				 struct v4l2_subdev_mbus_code_enum *code)
{
	struct i2c_client *client = v4l2_get_subdevdata(subdev);
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	unsigned int i;
	int idx = -1;
	int rval = -EINVAL;

	mutex_lock(&sensor->mutex);

	dev_err(&client->dev, "subdev %s, pad %d, index %d\n",
		subdev->name, code->pad, code->index);

	if (subdev != &sensor->src->sd || code->pad != SMIAPP_PAD_SRC) {
		if (code->index)
			goto out;

		code->code = sensor->internal_csi_format->code;
		rval = 0;
		goto out;
	}

	for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
		if (sensor->mbus_frame_fmts & (1 << i))
			idx++;

		if (idx == code->index) {
			code->code = smiapp_csi_data_formats[i].code;
			dev_err(&client->dev, "found index %d, i %d, code %x\n",
				code->index, i, code->code);
			rval = 0;
			break;
		}
	}

out:
	mutex_unlock(&sensor->mutex);

	return rval;
}

static u32 __smiapp_get_mbus_code(struct v4l2_subdev *subdev,
				  unsigned int pad)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);

	if (subdev == &sensor->src->sd && pad == SMIAPP_PAD_SRC)
		return sensor->csi_format->code;
	else
		return sensor->internal_csi_format->code;
}

static int __smiapp_get_format(struct v4l2_subdev *subdev,
1616
			       struct v4l2_subdev_pad_config *cfg,
Sakari Ailus's avatar
Sakari Ailus committed
1617 1618 1619 1620 1621
			       struct v4l2_subdev_format *fmt)
{
	struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);

	if (fmt->which == V4L2_SUBDEV_FORMAT_TRY) {
1622 1623
		fmt->format = *v4l2_subdev_get_try_format(subdev, cfg,
							  fmt->pad);
Sakari Ailus's avatar
Sakari Ailus committed
1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634
	} else {
		struct v4l2_rect *r;

		if (fmt->pad == ssd->source_pad)
			r = &ssd->crop[ssd->source_pad];
		else
			r = &ssd->sink_fmt;

		fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
		fmt->format.width = r->width;
		fmt->format.height = r->height;
1635
		fmt->format.field = V4L2_FIELD_NONE;
Sakari Ailus's avatar
Sakari Ailus committed
1636 1637 1638 1639 1640 1641
	}

	return 0;
}

static int smiapp_get_format(struct v4l2_subdev *subdev,
1642
			     struct v4l2_subdev_pad_config *cfg,
Sakari Ailus's avatar
Sakari Ailus committed
1643 1644 1645 1646 1647 1648
			     struct v4l2_subdev_format *fmt)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	int rval;

	mutex_lock(&sensor->mutex);
1649
	rval = __smiapp_get_format(subdev, cfg, fmt);
Sakari Ailus's avatar
Sakari Ailus committed
1650 1651 1652 1653 1654 1655
	mutex_unlock(&sensor->mutex);

	return rval;
}

static void smiapp_get_crop_compose(struct v4l2_subdev *subdev,
1656
				    struct v4l2_subdev_pad_config *cfg,
Sakari Ailus's avatar
Sakari Ailus committed
1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671
				    struct v4l2_rect **crops,
				    struct v4l2_rect **comps, int which)
{
	struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
	unsigned int i;

	if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
		if (crops)
			for (i = 0; i < subdev->entity.num_pads; i++)
				crops[i] = &ssd->crop[i];
		if (comps)
			*comps = &ssd->compose;
	} else {
		if (crops) {
			for (i = 0; i < subdev->entity.num_pads; i++) {
1672
				crops[i] = v4l2_subdev_get_try_crop(subdev, cfg, i);
Sakari Ailus's avatar
Sakari Ailus committed
1673 1674 1675 1676
				BUG_ON(!crops[i]);
			}
		}
		if (comps) {
1677
			*comps = v4l2_subdev_get_try_compose(subdev, cfg,
Sakari Ailus's avatar
Sakari Ailus committed
1678 1679 1680 1681 1682 1683 1684 1685
							     SMIAPP_PAD_SINK);
			BUG_ON(!*comps);
		}
	}
}

/* Changes require propagation only on sink pad. */
static void smiapp_propagate(struct v4l2_subdev *subdev,
1686
			     struct v4l2_subdev_pad_config *cfg, int which,
Sakari Ailus's avatar
Sakari Ailus committed
1687 1688 1689 1690 1691 1692
			     int target)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
	struct v4l2_rect *comp, *crops[SMIAPP_PADS];

1693
	smiapp_get_crop_compose(subdev, cfg, crops, &comp, which);
Sakari Ailus's avatar
Sakari Ailus committed
1694 1695

	switch (target) {
1696
	case V4L2_SEL_TGT_CROP:
Sakari Ailus's avatar
Sakari Ailus committed
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711
		comp->width = crops[SMIAPP_PAD_SINK]->width;
		comp->height = crops[SMIAPP_PAD_SINK]->height;
		if (which == V4L2_SUBDEV_FORMAT_ACTIVE) {
			if (ssd == sensor->scaler) {
				sensor->scale_m =
					sensor->limits[
						SMIAPP_LIMIT_SCALER_N_MIN];
				sensor->scaling_mode =
					SMIAPP_SCALING_MODE_NONE;
			} else if (ssd == sensor->binner) {
				sensor->binning_horizontal = 1;
				sensor->binning_vertical = 1;
			}
		}
		/* Fall through */
1712
	case V4L2_SEL_TGT_COMPOSE:
Sakari Ailus's avatar
Sakari Ailus committed
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730
		*crops[SMIAPP_PAD_SRC] = *comp;
		break;
	default:
		BUG();
	}
}

static const struct smiapp_csi_data_format
*smiapp_validate_csi_data_format(struct smiapp_sensor *sensor, u32 code)
{
	unsigned int i;

	for (i = 0; i < ARRAY_SIZE(smiapp_csi_data_formats); i++) {
		if (sensor->mbus_frame_fmts & (1 << i)
		    && smiapp_csi_data_formats[i].code == code)
			return &smiapp_csi_data_formats[i];
	}

1731
	return sensor->csi_format;
Sakari Ailus's avatar
Sakari Ailus committed
1732 1733
}

1734
static int smiapp_set_format_source(struct v4l2_subdev *subdev,
1735
				    struct v4l2_subdev_pad_config *cfg,
1736
				    struct v4l2_subdev_format *fmt)
Sakari Ailus's avatar
Sakari Ailus committed
1737 1738
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
1739 1740
	const struct smiapp_csi_data_format *csi_format,
		*old_csi_format = sensor->csi_format;
1741
	unsigned long *valid_link_freqs;
1742 1743 1744
	u32 code = fmt->format.code;
	unsigned int i;
	int rval;
Sakari Ailus's avatar
Sakari Ailus committed
1745

1746
	rval = __smiapp_get_format(subdev, cfg, fmt);
1747 1748
	if (rval)
		return rval;
Sakari Ailus's avatar
Sakari Ailus committed
1749 1750 1751 1752 1753

	/*
	 * Media bus code is changeable on src subdev's source pad. On
	 * other source pads we just get format here.
	 */
1754 1755
	if (subdev != &sensor->src->sd)
		return 0;
Sakari Ailus's avatar
Sakari Ailus committed
1756

1757
	csi_format = smiapp_validate_csi_data_format(sensor, code);
1758

1759
	fmt->format.code = csi_format->code;
1760

1761 1762
	if (fmt->which != V4L2_SUBDEV_FORMAT_ACTIVE)
		return 0;
Sakari Ailus's avatar
Sakari Ailus committed
1763

1764
	sensor->csi_format = csi_format;
1765

1766
	if (csi_format->width != old_csi_format->width)
1767
		for (i = 0; i < ARRAY_SIZE(sensor->test_data); i++)
1768 1769 1770
			__v4l2_ctrl_modify_range(
				sensor->test_data[i], 0,
				(1 << csi_format->width) - 1, 1, 0);
1771

1772
	if (csi_format->compressed == old_csi_format->compressed)
1773
		return 0;
1774 1775 1776

	valid_link_freqs = 
		&sensor->valid_link_freqs[sensor->csi_format->compressed
1777
					  - sensor->compressed_min_bpp];
1778 1779 1780 1781 1782 1783

	__v4l2_ctrl_modify_range(
		sensor->link_freq, 0,
		__fls(*valid_link_freqs), ~*valid_link_freqs,
		__ffs(*valid_link_freqs));

1784
	return smiapp_pll_update(sensor);
1785 1786 1787
}

static int smiapp_set_format(struct v4l2_subdev *subdev,
1788
			     struct v4l2_subdev_pad_config *cfg,
1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799
			     struct v4l2_subdev_format *fmt)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
	struct v4l2_rect *crops[SMIAPP_PADS];

	mutex_lock(&sensor->mutex);

	if (fmt->pad == ssd->source_pad) {
		int rval;

1800
		rval = smiapp_set_format_source(subdev, cfg, fmt);
1801 1802 1803 1804

		mutex_unlock(&sensor->mutex);

		return rval;
Sakari Ailus's avatar
Sakari Ailus committed
1805 1806 1807 1808 1809 1810
	}

	/* Sink pad. Width and height are changeable here. */
	fmt->format.code = __smiapp_get_mbus_code(subdev, fmt->pad);
	fmt->format.width &= ~1;
	fmt->format.height &= ~1;
1811
	fmt->format.field = V4L2_FIELD_NONE;
Sakari Ailus's avatar
Sakari Ailus committed
1812 1813 1814 1815 1816 1817 1818 1819 1820 1821

	fmt->format.width =
		clamp(fmt->format.width,
		      sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
		      sensor->limits[SMIAPP_LIMIT_MAX_X_OUTPUT_SIZE]);
	fmt->format.height =
		clamp(fmt->format.height,
		      sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
		      sensor->limits[SMIAPP_LIMIT_MAX_Y_OUTPUT_SIZE]);

1822
	smiapp_get_crop_compose(subdev, cfg, crops, NULL, fmt->which);
Sakari Ailus's avatar
Sakari Ailus committed
1823 1824 1825 1826 1827 1828 1829

	crops[ssd->sink_pad]->left = 0;
	crops[ssd->sink_pad]->top = 0;
	crops[ssd->sink_pad]->width = fmt->format.width;
	crops[ssd->sink_pad]->height = fmt->format.height;
	if (fmt->which == V4L2_SUBDEV_FORMAT_ACTIVE)
		ssd->sink_fmt = *crops[ssd->sink_pad];
1830
	smiapp_propagate(subdev, cfg, fmt->which,
1831
			 V4L2_SEL_TGT_CROP);
Sakari Ailus's avatar
Sakari Ailus committed
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855

	mutex_unlock(&sensor->mutex);

	return 0;
}

/*
 * Calculate goodness of scaled image size compared to expected image
 * size and flags provided.
 */
#define SCALING_GOODNESS		100000
#define SCALING_GOODNESS_EXTREME	100000000
static int scaling_goodness(struct v4l2_subdev *subdev, int w, int ask_w,
			    int h, int ask_h, u32 flags)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	struct i2c_client *client = v4l2_get_subdevdata(subdev);
	int val = 0;

	w &= ~1;
	ask_w &= ~1;
	h &= ~1;
	ask_h &= ~1;

1856
	if (flags & V4L2_SEL_FLAG_GE) {
Sakari Ailus's avatar
Sakari Ailus committed
1857 1858 1859 1860 1861 1862
		if (w < ask_w)
			val -= SCALING_GOODNESS;
		if (h < ask_h)
			val -= SCALING_GOODNESS;
	}

1863
	if (flags & V4L2_SEL_FLAG_LE) {
Sakari Ailus's avatar
Sakari Ailus committed
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882
		if (w > ask_w)
			val -= SCALING_GOODNESS;
		if (h > ask_h)
			val -= SCALING_GOODNESS;
	}

	val -= abs(w - ask_w);
	val -= abs(h - ask_h);

	if (w < sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE])
		val -= SCALING_GOODNESS_EXTREME;

	dev_dbg(&client->dev, "w %d ask_w %d h %d ask_h %d goodness %d\n",
		w, ask_h, h, ask_h, val);

	return val;
}

static void smiapp_set_compose_binner(struct v4l2_subdev *subdev,
1883
				      struct v4l2_subdev_pad_config *cfg,
Sakari Ailus's avatar
Sakari Ailus committed
1884 1885 1886 1887 1888 1889 1890
				      struct v4l2_subdev_selection *sel,
				      struct v4l2_rect **crops,
				      struct v4l2_rect *comp)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	unsigned int i;
	unsigned int binh = 1, binv = 1;
1891
	int best = scaling_goodness(
Sakari Ailus's avatar
Sakari Ailus committed
1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930
		subdev,
		crops[SMIAPP_PAD_SINK]->width, sel->r.width,
		crops[SMIAPP_PAD_SINK]->height, sel->r.height, sel->flags);

	for (i = 0; i < sensor->nbinning_subtypes; i++) {
		int this = scaling_goodness(
			subdev,
			crops[SMIAPP_PAD_SINK]->width
			/ sensor->binning_subtypes[i].horizontal,
			sel->r.width,
			crops[SMIAPP_PAD_SINK]->height
			/ sensor->binning_subtypes[i].vertical,
			sel->r.height, sel->flags);

		if (this > best) {
			binh = sensor->binning_subtypes[i].horizontal;
			binv = sensor->binning_subtypes[i].vertical;
			best = this;
		}
	}
	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
		sensor->binning_vertical = binv;
		sensor->binning_horizontal = binh;
	}

	sel->r.width = (crops[SMIAPP_PAD_SINK]->width / binh) & ~1;
	sel->r.height = (crops[SMIAPP_PAD_SINK]->height / binv) & ~1;
}

/*
 * Calculate best scaling ratio and mode for given output resolution.
 *
 * Try all of these: horizontal ratio, vertical ratio and smallest
 * size possible (horizontally).
 *
 * Also try whether horizontal scaler or full scaler gives a better
 * result.
 */
static void smiapp_set_compose_scaler(struct v4l2_subdev *subdev,
1931
				      struct v4l2_subdev_pad_config *cfg,
Sakari Ailus's avatar
Sakari Ailus committed
1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958
				      struct v4l2_subdev_selection *sel,
				      struct v4l2_rect **crops,
				      struct v4l2_rect *comp)
{
	struct i2c_client *client = v4l2_get_subdevdata(subdev);
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	u32 min, max, a, b, max_m;
	u32 scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
	int mode = SMIAPP_SCALING_MODE_HORIZONTAL;
	u32 try[4];
	u32 ntry = 0;
	unsigned int i;
	int best = INT_MIN;

	sel->r.width = min_t(unsigned int, sel->r.width,
			     crops[SMIAPP_PAD_SINK]->width);
	sel->r.height = min_t(unsigned int, sel->r.height,
			      crops[SMIAPP_PAD_SINK]->height);

	a = crops[SMIAPP_PAD_SINK]->width
		* sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.width;
	b = crops[SMIAPP_PAD_SINK]->height
		* sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN] / sel->r.height;
	max_m = crops[SMIAPP_PAD_SINK]->width
		* sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]
		/ sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE];

1959 1960 1961 1962 1963 1964
	a = clamp(a, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
		  sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
	b = clamp(b, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
		  sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
	max_m = clamp(max_m, sensor->limits[SMIAPP_LIMIT_SCALER_M_MIN],
		      sensor->limits[SMIAPP_LIMIT_SCALER_M_MAX]);
Sakari Ailus's avatar
Sakari Ailus committed
1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046

	dev_dbg(&client->dev, "scaling: a %d b %d max_m %d\n", a, b, max_m);

	min = min(max_m, min(a, b));
	max = min(max_m, max(a, b));

	try[ntry] = min;
	ntry++;
	if (min != max) {
		try[ntry] = max;
		ntry++;
	}
	if (max != max_m) {
		try[ntry] = min + 1;
		ntry++;
		if (min != max) {
			try[ntry] = max + 1;
			ntry++;
		}
	}

	for (i = 0; i < ntry; i++) {
		int this = scaling_goodness(
			subdev,
			crops[SMIAPP_PAD_SINK]->width
			/ try[i]
			* sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
			sel->r.width,
			crops[SMIAPP_PAD_SINK]->height,
			sel->r.height,
			sel->flags);

		dev_dbg(&client->dev, "trying factor %d (%d)\n", try[i], i);

		if (this > best) {
			scale_m = try[i];
			mode = SMIAPP_SCALING_MODE_HORIZONTAL;
			best = this;
		}

		if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
		    == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
			continue;

		this = scaling_goodness(
			subdev, crops[SMIAPP_PAD_SINK]->width
			/ try[i]
			* sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
			sel->r.width,
			crops[SMIAPP_PAD_SINK]->height
			/ try[i]
			* sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN],
			sel->r.height,
			sel->flags);

		if (this > best) {
			scale_m = try[i];
			mode = SMIAPP_SCALING_MODE_BOTH;
			best = this;
		}
	}

	sel->r.width =
		(crops[SMIAPP_PAD_SINK]->width
		 / scale_m
		 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN]) & ~1;
	if (mode == SMIAPP_SCALING_MODE_BOTH)
		sel->r.height =
			(crops[SMIAPP_PAD_SINK]->height
			 / scale_m
			 * sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN])
			& ~1;
	else
		sel->r.height = crops[SMIAPP_PAD_SINK]->height;

	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
		sensor->scale_m = scale_m;
		sensor->scaling_mode = mode;
	}
}
/* We're only called on source pads. This function sets scaling. */
static int smiapp_set_compose(struct v4l2_subdev *subdev,
2047
			      struct v4l2_subdev_pad_config *cfg,
Sakari Ailus's avatar
Sakari Ailus committed
2048 2049 2050 2051 2052 2053
			      struct v4l2_subdev_selection *sel)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
	struct v4l2_rect *comp, *crops[SMIAPP_PADS];

2054
	smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
Sakari Ailus's avatar
Sakari Ailus committed
2055 2056 2057 2058 2059

	sel->r.top = 0;
	sel->r.left = 0;

	if (ssd == sensor->binner)
2060
		smiapp_set_compose_binner(subdev, cfg, sel, crops, comp);
Sakari Ailus's avatar
Sakari Ailus committed
2061
	else
2062
		smiapp_set_compose_scaler(subdev, cfg, sel, crops, comp);
Sakari Ailus's avatar
Sakari Ailus committed
2063 2064

	*comp = sel->r;
2065
	smiapp_propagate(subdev, cfg, sel->which, V4L2_SEL_TGT_COMPOSE);
Sakari Ailus's avatar
Sakari Ailus committed
2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080

	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE)
		return smiapp_update_mode(sensor);

	return 0;
}

static int __smiapp_sel_supported(struct v4l2_subdev *subdev,
				  struct v4l2_subdev_selection *sel)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);

	/* We only implement crop in three places. */
	switch (sel->target) {
2081 2082
	case V4L2_SEL_TGT_CROP:
	case V4L2_SEL_TGT_CROP_BOUNDS:
Sakari Ailus's avatar
Sakari Ailus committed
2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094
		if (ssd == sensor->pixel_array
		    && sel->pad == SMIAPP_PA_PAD_SRC)
			return 0;
		if (ssd == sensor->src
		    && sel->pad == SMIAPP_PAD_SRC)
			return 0;
		if (ssd == sensor->scaler
		    && sel->pad == SMIAPP_PAD_SINK
		    && sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
		    == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP)
			return 0;
		return -EINVAL;
2095 2096 2097 2098 2099
	case V4L2_SEL_TGT_NATIVE_SIZE:
		if (ssd == sensor->pixel_array
		    && sel->pad == SMIAPP_PA_PAD_SRC)
			return 0;
		return -EINVAL;
2100 2101
	case V4L2_SEL_TGT_COMPOSE:
	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sakari Ailus's avatar
Sakari Ailus committed
2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116
		if (sel->pad == ssd->source_pad)
			return -EINVAL;
		if (ssd == sensor->binner)
			return 0;
		if (ssd == sensor->scaler
		    && sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
		    != SMIAPP_SCALING_CAPABILITY_NONE)
			return 0;
		/* Fall through */
	default:
		return -EINVAL;
	}
}

static int smiapp_set_crop(struct v4l2_subdev *subdev,
2117
			   struct v4l2_subdev_pad_config *cfg,
Sakari Ailus's avatar
Sakari Ailus committed
2118 2119 2120 2121 2122 2123 2124
			   struct v4l2_subdev_selection *sel)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
	struct v4l2_rect *src_size, *crops[SMIAPP_PADS];
	struct v4l2_rect _r;

2125
	smiapp_get_crop_compose(subdev, cfg, crops, NULL, sel->which);
Sakari Ailus's avatar
Sakari Ailus committed
2126 2127 2128 2129 2130 2131 2132 2133 2134 2135

	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
		if (sel->pad == ssd->sink_pad)
			src_size = &ssd->sink_fmt;
		else
			src_size = &ssd->compose;
	} else {
		if (sel->pad == ssd->sink_pad) {
			_r.left = 0;
			_r.top = 0;
2136
			_r.width = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
Sakari Ailus's avatar
Sakari Ailus committed
2137
				->width;
2138
			_r.height = v4l2_subdev_get_try_format(subdev, cfg, sel->pad)
Sakari Ailus's avatar
Sakari Ailus committed
2139 2140 2141
				->height;
			src_size = &_r;
		} else {
2142 2143
			src_size = v4l2_subdev_get_try_compose(
				subdev, cfg, ssd->sink_pad);
Sakari Ailus's avatar
Sakari Ailus committed
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154
		}
	}

	if (ssd == sensor->src && sel->pad == SMIAPP_PAD_SRC) {
		sel->r.left = 0;
		sel->r.top = 0;
	}

	sel->r.width = min(sel->r.width, src_size->width);
	sel->r.height = min(sel->r.height, src_size->height);

2155 2156
	sel->r.left = min_t(int, sel->r.left, src_size->width - sel->r.width);
	sel->r.top = min_t(int, sel->r.top, src_size->height - sel->r.height);
Sakari Ailus's avatar
Sakari Ailus committed
2157 2158 2159 2160

	*crops[sel->pad] = sel->r;

	if (ssd != sensor->pixel_array && sel->pad == SMIAPP_PAD_SINK)
2161
		smiapp_propagate(subdev, cfg, sel->which,
2162
				 V4L2_SEL_TGT_CROP);
Sakari Ailus's avatar
Sakari Ailus committed
2163 2164 2165 2166

	return 0;
}

2167 2168 2169 2170 2171 2172 2173 2174 2175
static void smiapp_get_native_size(struct smiapp_subdev *ssd,
				    struct v4l2_rect *r)
{
	r->top = 0;
	r->left = 0;
	r->width = ssd->sensor->limits[SMIAPP_LIMIT_X_ADDR_MAX] + 1;
	r->height = ssd->sensor->limits[SMIAPP_LIMIT_Y_ADDR_MAX] + 1;
}

Sakari Ailus's avatar
Sakari Ailus committed
2176
static int __smiapp_get_selection(struct v4l2_subdev *subdev,
2177
				  struct v4l2_subdev_pad_config *cfg,
Sakari Ailus's avatar
Sakari Ailus committed
2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189
				  struct v4l2_subdev_selection *sel)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	struct smiapp_subdev *ssd = to_smiapp_subdev(subdev);
	struct v4l2_rect *comp, *crops[SMIAPP_PADS];
	struct v4l2_rect sink_fmt;
	int ret;

	ret = __smiapp_sel_supported(subdev, sel);
	if (ret)
		return ret;

2190
	smiapp_get_crop_compose(subdev, cfg, crops, &comp, sel->which);
Sakari Ailus's avatar
Sakari Ailus committed
2191 2192 2193 2194 2195

	if (sel->which == V4L2_SUBDEV_FORMAT_ACTIVE) {
		sink_fmt = ssd->sink_fmt;
	} else {
		struct v4l2_mbus_framefmt *fmt =
2196
			v4l2_subdev_get_try_format(subdev, cfg, ssd->sink_pad);
Sakari Ailus's avatar
Sakari Ailus committed
2197 2198 2199 2200 2201 2202 2203 2204

		sink_fmt.left = 0;
		sink_fmt.top = 0;
		sink_fmt.width = fmt->width;
		sink_fmt.height = fmt->height;
	}

	switch (sel->target) {
2205
	case V4L2_SEL_TGT_CROP_BOUNDS:
2206
	case V4L2_SEL_TGT_NATIVE_SIZE:
2207 2208 2209
		if (ssd == sensor->pixel_array)
			smiapp_get_native_size(ssd, &sel->r);
		else if (sel->pad == ssd->sink_pad)
Sakari Ailus's avatar
Sakari Ailus committed
2210
			sel->r = sink_fmt;
2211
		else
Sakari Ailus's avatar
Sakari Ailus committed
2212 2213
			sel->r = *comp;
		break;
2214 2215
	case V4L2_SEL_TGT_CROP:
	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
Sakari Ailus's avatar
Sakari Ailus committed
2216 2217
		sel->r = *crops[sel->pad];
		break;
2218
	case V4L2_SEL_TGT_COMPOSE:
Sakari Ailus's avatar
Sakari Ailus committed
2219 2220 2221 2222 2223 2224 2225 2226
		sel->r = *comp;
		break;
	}

	return 0;
}

static int smiapp_get_selection(struct v4l2_subdev *subdev,
2227
				struct v4l2_subdev_pad_config *cfg,
Sakari Ailus's avatar
Sakari Ailus committed
2228 2229 2230 2231 2232 2233
				struct v4l2_subdev_selection *sel)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	int rval;

	mutex_lock(&sensor->mutex);
2234
	rval = __smiapp_get_selection(subdev, cfg, sel);
Sakari Ailus's avatar
Sakari Ailus committed
2235 2236 2237 2238 2239
	mutex_unlock(&sensor->mutex);

	return rval;
}
static int smiapp_set_selection(struct v4l2_subdev *subdev,
2240
				struct v4l2_subdev_pad_config *cfg,
Sakari Ailus's avatar
Sakari Ailus committed
2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253
				struct v4l2_subdev_selection *sel)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	int ret;

	ret = __smiapp_sel_supported(subdev, sel);
	if (ret)
		return ret;

	mutex_lock(&sensor->mutex);

	sel->r.left = max(0, sel->r.left & ~1);
	sel->r.top = max(0, sel->r.top & ~1);
2254 2255
	sel->r.width = SMIAPP_ALIGN_DIM(sel->r.width, sel->flags);
	sel->r.height =	SMIAPP_ALIGN_DIM(sel->r.height, sel->flags);
Sakari Ailus's avatar
Sakari Ailus committed
2256 2257 2258 2259 2260 2261 2262 2263 2264

	sel->r.width = max_t(unsigned int,
			     sensor->limits[SMIAPP_LIMIT_MIN_X_OUTPUT_SIZE],
			     sel->r.width);
	sel->r.height = max_t(unsigned int,
			      sensor->limits[SMIAPP_LIMIT_MIN_Y_OUTPUT_SIZE],
			      sel->r.height);

	switch (sel->target) {
2265
	case V4L2_SEL_TGT_CROP:
2266
		ret = smiapp_set_crop(subdev, cfg, sel);
Sakari Ailus's avatar
Sakari Ailus committed
2267
		break;
2268
	case V4L2_SEL_TGT_COMPOSE:
2269
		ret = smiapp_set_compose(subdev, cfg, sel);
Sakari Ailus's avatar
Sakari Ailus committed
2270 2271
		break;
	default:
2272
		ret = -EINVAL;
Sakari Ailus's avatar
Sakari Ailus committed
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286
	}

	mutex_unlock(&sensor->mutex);
	return ret;
}

static int smiapp_get_skip_frames(struct v4l2_subdev *subdev, u32 *frames)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);

	*frames = sensor->frame_skip;
	return 0;
}

2287 2288 2289 2290 2291 2292 2293 2294 2295
static int smiapp_get_skip_top_lines(struct v4l2_subdev *subdev, u32 *lines)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);

	*lines = sensor->image_start;

	return 0;
}

Sakari Ailus's avatar
Sakari Ailus committed
2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313
/* -----------------------------------------------------------------------------
 * sysfs attributes
 */

static ssize_t
smiapp_sysfs_nvm_read(struct device *dev, struct device_attribute *attr,
		      char *buf)
{
	struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
	struct i2c_client *client = v4l2_get_subdevdata(subdev);
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	unsigned int nbytes;

	if (!sensor->dev_init_done)
		return -EBUSY;

	if (!sensor->nvm_size) {
		/* NVM not read yet - read it now */
2314
		sensor->nvm_size = sensor->hwcfg->nvm_size;
Sakari Ailus's avatar
Sakari Ailus committed
2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333
		if (smiapp_set_power(subdev, 1) < 0)
			return -ENODEV;
		if (smiapp_read_nvm(sensor, sensor->nvm)) {
			dev_err(&client->dev, "nvm read failed\n");
			return -ENODEV;
		}
		smiapp_set_power(subdev, 0);
	}
	/*
	 * NVM is still way below a PAGE_SIZE, so we can safely
	 * assume this for now.
	 */
	nbytes = min_t(unsigned int, sensor->nvm_size, PAGE_SIZE);
	memcpy(buf, sensor->nvm, nbytes);

	return nbytes;
}
static DEVICE_ATTR(nvm, S_IRUGO, smiapp_sysfs_nvm_read, NULL);

2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348
static ssize_t
smiapp_sysfs_ident_read(struct device *dev, struct device_attribute *attr,
			char *buf)
{
	struct v4l2_subdev *subdev = i2c_get_clientdata(to_i2c_client(dev));
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	struct smiapp_module_info *minfo = &sensor->minfo;

	return snprintf(buf, PAGE_SIZE, "%2.2x%4.4x%2.2x\n",
			minfo->manufacturer_id, minfo->model_id,
			minfo->revision_number_major) + 1;
}

static DEVICE_ATTR(ident, S_IRUGO, smiapp_sysfs_ident_read, NULL);

Sakari Ailus's avatar
Sakari Ailus committed
2349 2350 2351 2352
/* -----------------------------------------------------------------------------
 * V4L2 subdev core operations
 */

2353
static int smiapp_identify_module(struct smiapp_sensor *sensor)
Sakari Ailus's avatar
Sakari Ailus committed
2354
{
2355
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
Sakari Ailus's avatar
Sakari Ailus committed
2356 2357 2358 2359 2360 2361 2362
	struct smiapp_module_info *minfo = &sensor->minfo;
	unsigned int i;
	int rval = 0;

	minfo->name = SMIAPP_NAME;

	/* Module info */
2363 2364
	rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MANUFACTURER_ID,
				 &minfo->manufacturer_id);
Sakari Ailus's avatar
Sakari Ailus committed
2365
	if (!rval)
2366 2367
		rval = smiapp_read_8only(sensor, SMIAPP_REG_U16_MODEL_ID,
					 &minfo->model_id);
Sakari Ailus's avatar
Sakari Ailus committed
2368
	if (!rval)
2369 2370 2371
		rval = smiapp_read_8only(sensor,
					 SMIAPP_REG_U8_REVISION_NUMBER_MAJOR,
					 &minfo->revision_number_major);
Sakari Ailus's avatar
Sakari Ailus committed
2372
	if (!rval)
2373 2374 2375
		rval = smiapp_read_8only(sensor,
					 SMIAPP_REG_U8_REVISION_NUMBER_MINOR,
					 &minfo->revision_number_minor);
Sakari Ailus's avatar
Sakari Ailus committed
2376
	if (!rval)
2377 2378 2379
		rval = smiapp_read_8only(sensor,
					 SMIAPP_REG_U8_MODULE_DATE_YEAR,
					 &minfo->module_year);
Sakari Ailus's avatar
Sakari Ailus committed
2380
	if (!rval)
2381 2382 2383
		rval = smiapp_read_8only(sensor,
					 SMIAPP_REG_U8_MODULE_DATE_MONTH,
					 &minfo->module_month);
Sakari Ailus's avatar
Sakari Ailus committed
2384
	if (!rval)
2385 2386
		rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_MODULE_DATE_DAY,
					 &minfo->module_day);
Sakari Ailus's avatar
Sakari Ailus committed
2387 2388 2389

	/* Sensor info */
	if (!rval)
2390 2391 2392
		rval = smiapp_read_8only(sensor,
					 SMIAPP_REG_U8_SENSOR_MANUFACTURER_ID,
					 &minfo->sensor_manufacturer_id);
Sakari Ailus's avatar
Sakari Ailus committed
2393
	if (!rval)
2394 2395 2396
		rval = smiapp_read_8only(sensor,
					 SMIAPP_REG_U16_SENSOR_MODEL_ID,
					 &minfo->sensor_model_id);
Sakari Ailus's avatar
Sakari Ailus committed
2397
	if (!rval)
2398 2399 2400
		rval = smiapp_read_8only(sensor,
					 SMIAPP_REG_U8_SENSOR_REVISION_NUMBER,
					 &minfo->sensor_revision_number);
Sakari Ailus's avatar
Sakari Ailus committed
2401
	if (!rval)
2402 2403 2404
		rval = smiapp_read_8only(sensor,
					 SMIAPP_REG_U8_SENSOR_FIRMWARE_VERSION,
					 &minfo->sensor_firmware_version);
Sakari Ailus's avatar
Sakari Ailus committed
2405 2406 2407

	/* SMIA */
	if (!rval)
2408 2409
		rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIA_VERSION,
					 &minfo->smia_version);
Sakari Ailus's avatar
Sakari Ailus committed
2410
	if (!rval)
2411 2412
		rval = smiapp_read_8only(sensor, SMIAPP_REG_U8_SMIAPP_VERSION,
					 &minfo->smiapp_version);
Sakari Ailus's avatar
Sakari Ailus committed
2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484

	if (rval) {
		dev_err(&client->dev, "sensor detection failed\n");
		return -ENODEV;
	}

	dev_dbg(&client->dev, "module 0x%2.2x-0x%4.4x\n",
		minfo->manufacturer_id, minfo->model_id);

	dev_dbg(&client->dev,
		"module revision 0x%2.2x-0x%2.2x date %2.2d-%2.2d-%2.2d\n",
		minfo->revision_number_major, minfo->revision_number_minor,
		minfo->module_year, minfo->module_month, minfo->module_day);

	dev_dbg(&client->dev, "sensor 0x%2.2x-0x%4.4x\n",
		minfo->sensor_manufacturer_id, minfo->sensor_model_id);

	dev_dbg(&client->dev,
		"sensor revision 0x%2.2x firmware version 0x%2.2x\n",
		minfo->sensor_revision_number, minfo->sensor_firmware_version);

	dev_dbg(&client->dev, "smia version %2.2d smiapp version %2.2d\n",
		minfo->smia_version, minfo->smiapp_version);

	/*
	 * Some modules have bad data in the lvalues below. Hope the
	 * rvalues have better stuff. The lvalues are module
	 * parameters whereas the rvalues are sensor parameters.
	 */
	if (!minfo->manufacturer_id && !minfo->model_id) {
		minfo->manufacturer_id = minfo->sensor_manufacturer_id;
		minfo->model_id = minfo->sensor_model_id;
		minfo->revision_number_major = minfo->sensor_revision_number;
	}

	for (i = 0; i < ARRAY_SIZE(smiapp_module_idents); i++) {
		if (smiapp_module_idents[i].manufacturer_id
		    != minfo->manufacturer_id)
			continue;
		if (smiapp_module_idents[i].model_id != minfo->model_id)
			continue;
		if (smiapp_module_idents[i].flags
		    & SMIAPP_MODULE_IDENT_FLAG_REV_LE) {
			if (smiapp_module_idents[i].revision_number_major
			    < minfo->revision_number_major)
				continue;
		} else {
			if (smiapp_module_idents[i].revision_number_major
			    != minfo->revision_number_major)
				continue;
		}

		minfo->name = smiapp_module_idents[i].name;
		minfo->quirk = smiapp_module_idents[i].quirk;
		break;
	}

	if (i >= ARRAY_SIZE(smiapp_module_idents))
		dev_warn(&client->dev,
			 "no quirks for this module; let's hope it's fully compliant\n");

	dev_dbg(&client->dev, "the sensor is called %s, ident %2.2x%4.4x%2.2x\n",
		minfo->name, minfo->manufacturer_id, minfo->model_id,
		minfo->revision_number_major);

	return 0;
}

static const struct v4l2_subdev_ops smiapp_ops;
static const struct v4l2_subdev_internal_ops smiapp_internal_ops;
static const struct media_entity_operations smiapp_entity_ops;

2485 2486 2487 2488
static int smiapp_register_subdev(struct smiapp_sensor *sensor,
				  struct smiapp_subdev *ssd,
				  struct smiapp_subdev *sink_ssd,
				  u16 source_pad, u16 sink_pad, u32 link_flags)
2489 2490 2491 2492
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);
	int rval;

2493 2494
	if (!sink_ssd)
		return 0;
2495

2496 2497 2498 2499 2500 2501 2502
	rval = media_entity_pads_init(&ssd->sd.entity,
				      ssd->npads, ssd->pads);
	if (rval) {
		dev_err(&client->dev,
			"media_entity_pads_init failed\n");
		return rval;
	}
2503

2504 2505 2506 2507 2508 2509 2510
	rval = v4l2_device_register_subdev(sensor->src->sd.v4l2_dev,
					   &ssd->sd);
	if (rval) {
		dev_err(&client->dev,
			"v4l2_device_register_subdev failed\n");
		return rval;
	}
2511

2512 2513 2514 2515 2516 2517
	rval = media_create_pad_link(&ssd->sd.entity, source_pad,
				     &sink_ssd->sd.entity, sink_pad,
				     link_flags);
	if (rval) {
		dev_err(&client->dev,
			"media_create_pad_link failed\n");
2518
		v4l2_device_unregister_subdev(&ssd->sd);
2519 2520
		return rval;
	}
2521

2522 2523 2524
	return 0;
}

2525 2526 2527 2528 2529 2530 2531 2532 2533
static void smiapp_unregistered(struct v4l2_subdev *subdev)
{
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	unsigned int i;

	for (i = 1; i < sensor->ssds_used; i++)
		v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
}

2534
static int smiapp_registered(struct v4l2_subdev *subdev)
2535
{
2536
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
2537 2538 2539 2540 2541 2542 2543 2544
	int rval;

	if (sensor->scaler) {
		rval = smiapp_register_subdev(
			sensor, sensor->binner, sensor->scaler,
			SMIAPP_PAD_SRC, SMIAPP_PAD_SINK,
			MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
		if (rval < 0)
2545 2546 2547
			return rval;
	}

2548
	rval = smiapp_register_subdev(
2549 2550 2551
		sensor, sensor->pixel_array, sensor->binner,
		SMIAPP_PA_PAD_SRC, SMIAPP_PAD_SINK,
		MEDIA_LNK_FL_ENABLED | MEDIA_LNK_FL_IMMUTABLE);
2552 2553 2554 2555 2556 2557 2558 2559 2560
	if (rval)
		goto out_err;

	return 0;

out_err:
	smiapp_unregistered(subdev);

	return rval;
2561 2562
}

2563
static void smiapp_cleanup(struct smiapp_sensor *sensor)
Sakari Ailus's avatar
Sakari Ailus committed
2564
{
2565 2566 2567 2568
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);

	device_remove_file(&client->dev, &dev_attr_nvm);
	device_remove_file(&client->dev, &dev_attr_ident);
2569 2570

	smiapp_free_controls(sensor);
2571 2572
}

2573
static void smiapp_create_subdev(struct smiapp_sensor *sensor,
2574 2575
				 struct smiapp_subdev *ssd, const char *name,
				 unsigned short num_pads)
2576 2577 2578 2579 2580 2581 2582 2583 2584
{
	struct i2c_client *client = v4l2_get_subdevdata(&sensor->src->sd);

	if (!ssd)
		return;

	if (ssd != sensor->src)
		v4l2_subdev_init(&ssd->sd, &smiapp_ops);

2585
	ssd->sd.flags |= V4L2_SUBDEV_FL_HAS_DEVNODE;
2586 2587
	ssd->sensor = sensor;

2588 2589
	ssd->npads = num_pads;
	ssd->source_pad = num_pads - 1;
2590 2591 2592 2593 2594

	snprintf(ssd->sd.name,
		 sizeof(ssd->sd.name), "%s %s %d-%4.4x", sensor->minfo.name,
		 name, i2c_adapter_id(client->adapter), client->addr);

2595 2596
	smiapp_get_native_size(ssd, &ssd->sink_fmt);

2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612
	ssd->compose.width = ssd->sink_fmt.width;
	ssd->compose.height = ssd->sink_fmt.height;
	ssd->crop[ssd->source_pad] = ssd->compose;
	ssd->pads[ssd->source_pad].flags = MEDIA_PAD_FL_SOURCE;
	if (ssd != sensor->pixel_array) {
		ssd->crop[ssd->sink_pad] = ssd->compose;
		ssd->pads[ssd->sink_pad].flags = MEDIA_PAD_FL_SINK;
	}

	ssd->sd.entity.ops = &smiapp_entity_ops;

	if (ssd == sensor->src)
		return;

	ssd->sd.internal_ops = &smiapp_internal_ops;
	ssd->sd.owner = THIS_MODULE;
2613
	ssd->sd.dev = &client->dev;
2614 2615 2616
	v4l2_set_subdevdata(&ssd->sd, client);
}

Sakari Ailus's avatar
Sakari Ailus committed
2617 2618 2619 2620 2621 2622 2623 2624 2625 2626
static int smiapp_open(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
	struct smiapp_subdev *ssd = to_smiapp_subdev(sd);
	struct smiapp_sensor *sensor = ssd->sensor;
	unsigned int i;

	mutex_lock(&sensor->mutex);

	for (i = 0; i < ssd->npads; i++) {
		struct v4l2_mbus_framefmt *try_fmt =
2627
			v4l2_subdev_get_try_format(sd, fh->pad, i);
2628 2629
		struct v4l2_rect *try_crop =
			v4l2_subdev_get_try_crop(sd, fh->pad, i);
Sakari Ailus's avatar
Sakari Ailus committed
2630 2631
		struct v4l2_rect *try_comp;

2632 2633 2634 2635
		smiapp_get_native_size(ssd, try_crop);

		try_fmt->width = try_crop->width;
		try_fmt->height = try_crop->height;
2636
		try_fmt->code = sensor->internal_csi_format->code;
2637
		try_fmt->field = V4L2_FIELD_NONE;
Sakari Ailus's avatar
Sakari Ailus committed
2638 2639 2640 2641

		if (ssd != sensor->pixel_array)
			continue;

2642
		try_comp = v4l2_subdev_get_try_compose(sd, fh->pad, i);
Sakari Ailus's avatar
Sakari Ailus committed
2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673
		*try_comp = *try_crop;
	}

	mutex_unlock(&sensor->mutex);

	return smiapp_set_power(sd, 1);
}

static int smiapp_close(struct v4l2_subdev *sd, struct v4l2_subdev_fh *fh)
{
	return smiapp_set_power(sd, 0);
}

static const struct v4l2_subdev_video_ops smiapp_video_ops = {
	.s_stream = smiapp_set_stream,
};

static const struct v4l2_subdev_core_ops smiapp_core_ops = {
	.s_power = smiapp_set_power,
};

static const struct v4l2_subdev_pad_ops smiapp_pad_ops = {
	.enum_mbus_code = smiapp_enum_mbus_code,
	.get_fmt = smiapp_get_format,
	.set_fmt = smiapp_set_format,
	.get_selection = smiapp_get_selection,
	.set_selection = smiapp_set_selection,
};

static const struct v4l2_subdev_sensor_ops smiapp_sensor_ops = {
	.g_skip_frames = smiapp_get_skip_frames,
2674
	.g_skip_top_lines = smiapp_get_skip_top_lines,
Sakari Ailus's avatar
Sakari Ailus committed
2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689
};

static const struct v4l2_subdev_ops smiapp_ops = {
	.core = &smiapp_core_ops,
	.video = &smiapp_video_ops,
	.pad = &smiapp_pad_ops,
	.sensor = &smiapp_sensor_ops,
};

static const struct media_entity_operations smiapp_entity_ops = {
	.link_validate = v4l2_subdev_link_validate,
};

static const struct v4l2_subdev_internal_ops smiapp_internal_src_ops = {
	.registered = smiapp_registered,
2690
	.unregistered = smiapp_unregistered,
Sakari Ailus's avatar
Sakari Ailus committed
2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755
	.open = smiapp_open,
	.close = smiapp_close,
};

static const struct v4l2_subdev_internal_ops smiapp_internal_ops = {
	.open = smiapp_open,
	.close = smiapp_close,
};

/* -----------------------------------------------------------------------------
 * I2C Driver
 */

#ifdef CONFIG_PM

static int smiapp_suspend(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	bool streaming;

	if (sensor->power_count == 0)
		return 0;

	if (sensor->streaming)
		smiapp_stop_streaming(sensor);

	streaming = sensor->streaming;

	smiapp_power_off(sensor);

	/* save state for resume */
	sensor->streaming = streaming;

	return 0;
}

static int smiapp_resume(struct device *dev)
{
	struct i2c_client *client = to_i2c_client(dev);
	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	int rval;

	if (sensor->power_count == 0)
		return 0;

	rval = smiapp_power_on(sensor);
	if (rval)
		return rval;

	if (sensor->streaming)
		rval = smiapp_start_streaming(sensor);

	return rval;
}

#else

#define smiapp_suspend	NULL
#define smiapp_resume	NULL

#endif /* CONFIG_PM */

2756
static struct smiapp_hwconfig *smiapp_get_hwconfig(struct device *dev)
2757
{
2758
	struct smiapp_hwconfig *hwcfg;
2759
	struct v4l2_of_endpoint *bus_cfg;
2760
	struct device_node *ep;
2761
	int i;
2762 2763 2764 2765 2766 2767 2768 2769 2770
	int rval;

	if (!dev->of_node)
		return dev->platform_data;

	ep = of_graph_get_next_endpoint(dev->of_node, NULL);
	if (!ep)
		return NULL;

2771 2772 2773 2774
	bus_cfg = v4l2_of_alloc_parse_endpoint(ep);
	if (IS_ERR(bus_cfg))
		goto out_err;

2775 2776
	hwcfg = devm_kzalloc(dev, sizeof(*hwcfg), GFP_KERNEL);
	if (!hwcfg)
2777 2778
		goto out_err;

2779
	switch (bus_cfg->bus_type) {
2780
	case V4L2_MBUS_CSI2:
2781
		hwcfg->csi_signalling_mode = SMIAPP_CSI_SIGNALLING_MODE_CSI2;
2782 2783 2784 2785 2786 2787
		break;
		/* FIXME: add CCP2 support. */
	default:
		goto out_err;
	}

2788 2789
	hwcfg->lanes = bus_cfg->bus.mipi_csi2.num_data_lanes;
	dev_dbg(dev, "lanes %u\n", hwcfg->lanes);
2790 2791 2792

	/* NVM size is not mandatory */
	of_property_read_u32(dev->of_node, "nokia,nvm-size",
2793
				    &hwcfg->nvm_size);
2794 2795

	rval = of_property_read_u32(dev->of_node, "clock-frequency",
2796
				    &hwcfg->ext_clk);
2797 2798 2799 2800 2801
	if (rval) {
		dev_warn(dev, "can't get clock-frequency\n");
		goto out_err;
	}

2802 2803
	dev_dbg(dev, "nvm %d, clk %d, csi %d\n", hwcfg->nvm_size,
		hwcfg->ext_clk, hwcfg->csi_signalling_mode);
2804

2805 2806
	if (!bus_cfg->nr_of_link_frequencies) {
		dev_warn(dev, "no link frequencies defined\n");
2807 2808 2809
		goto out_err;
	}

2810
	hwcfg->op_sys_clock = devm_kcalloc(
2811
		dev, bus_cfg->nr_of_link_frequencies + 1 /* guardian */,
2812 2813
		sizeof(*hwcfg->op_sys_clock), GFP_KERNEL);
	if (!hwcfg->op_sys_clock)
2814 2815
		goto out_err;

2816
	for (i = 0; i < bus_cfg->nr_of_link_frequencies; i++) {
2817 2818
		hwcfg->op_sys_clock[i] = bus_cfg->link_frequencies[i];
		dev_dbg(dev, "freq %d: %lld\n", i, hwcfg->op_sys_clock[i]);
2819
	}
2820

2821
	v4l2_of_free_endpoint(bus_cfg);
2822
	of_node_put(ep);
2823
	return hwcfg;
2824 2825

out_err:
2826
	v4l2_of_free_endpoint(bus_cfg);
2827 2828 2829 2830
	of_node_put(ep);
	return NULL;
}

Sakari Ailus's avatar
Sakari Ailus committed
2831 2832 2833 2834
static int smiapp_probe(struct i2c_client *client,
			const struct i2c_device_id *devid)
{
	struct smiapp_sensor *sensor;
2835
	struct smiapp_hwconfig *hwcfg = smiapp_get_hwconfig(&client->dev);
2836
	unsigned int i;
2837
	int rval;
Sakari Ailus's avatar
Sakari Ailus committed
2838

2839
	if (hwcfg == NULL)
Sakari Ailus's avatar
Sakari Ailus committed
2840 2841
		return -ENODEV;

2842
	sensor = devm_kzalloc(&client->dev, sizeof(*sensor), GFP_KERNEL);
Sakari Ailus's avatar
Sakari Ailus committed
2843 2844 2845
	if (sensor == NULL)
		return -ENOMEM;

2846
	sensor->hwcfg = hwcfg;
Sakari Ailus's avatar
Sakari Ailus committed
2847 2848 2849 2850 2851 2852
	mutex_init(&sensor->mutex);
	mutex_init(&sensor->power_mutex);
	sensor->src = &sensor->ssds[sensor->ssds_used];

	v4l2_i2c_subdev_init(&sensor->src->sd, client, &smiapp_ops);
	sensor->src->sd.internal_ops = &smiapp_internal_src_ops;
2853

2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866
	sensor->vana = devm_regulator_get(&client->dev, "vana");
	if (IS_ERR(sensor->vana)) {
		dev_err(&client->dev, "could not get regulator for vana\n");
		return PTR_ERR(sensor->vana);
	}

	sensor->ext_clk = devm_clk_get(&client->dev, NULL);
	if (IS_ERR(sensor->ext_clk)) {
		dev_err(&client->dev, "could not get clock (%ld)\n",
			PTR_ERR(sensor->ext_clk));
		return -EPROBE_DEFER;
	}

2867
	rval = clk_set_rate(sensor->ext_clk, sensor->hwcfg->ext_clk);
2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880
	if (rval < 0) {
		dev_err(&client->dev,
			"unable to set clock freq to %u\n",
			sensor->hwcfg->ext_clk);
		return rval;
	}

	sensor->xshutdown = devm_gpiod_get_optional(&client->dev, "xshutdown",
						    GPIOD_OUT_LOW);
	if (IS_ERR(sensor->xshutdown))
		return PTR_ERR(sensor->xshutdown);

	rval = smiapp_power_on(sensor);
2881
	if (rval)
2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895
		return -ENODEV;

	rval = smiapp_identify_module(sensor);
	if (rval) {
		rval = -ENODEV;
		goto out_power_off;
	}

	rval = smiapp_get_all_limits(sensor);
	if (rval) {
		rval = -ENODEV;
		goto out_power_off;
	}

2896 2897 2898 2899 2900 2901
	rval = smiapp_read_frame_fmt(sensor);
	if (rval) {
		rval = -ENODEV;
		goto out_power_off;
	}

2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054
	/*
	 * Handle Sensor Module orientation on the board.
	 *
	 * The application of H-FLIP and V-FLIP on the sensor is modified by
	 * the sensor orientation on the board.
	 *
	 * For SMIAPP_BOARD_SENSOR_ORIENT_180 the default behaviour is to set
	 * both H-FLIP and V-FLIP for normal operation which also implies
	 * that a set/unset operation for user space HFLIP and VFLIP v4l2
	 * controls will need to be internally inverted.
	 *
	 * Rotation also changes the bayer pattern.
	 */
	if (sensor->hwcfg->module_board_orient ==
	    SMIAPP_MODULE_BOARD_ORIENT_180)
		sensor->hvflip_inv_mask = SMIAPP_IMAGE_ORIENTATION_HFLIP |
					  SMIAPP_IMAGE_ORIENTATION_VFLIP;

	rval = smiapp_call_quirk(sensor, limits);
	if (rval) {
		dev_err(&client->dev, "limits quirks failed\n");
		goto out_power_off;
	}

	if (sensor->limits[SMIAPP_LIMIT_BINNING_CAPABILITY]) {
		u32 val;

		rval = smiapp_read(sensor,
				   SMIAPP_REG_U8_BINNING_SUBTYPES, &val);
		if (rval < 0) {
			rval = -ENODEV;
			goto out_power_off;
		}
		sensor->nbinning_subtypes = min_t(u8, val,
						  SMIAPP_BINNING_SUBTYPES);

		for (i = 0; i < sensor->nbinning_subtypes; i++) {
			rval = smiapp_read(
				sensor, SMIAPP_REG_U8_BINNING_TYPE_n(i), &val);
			if (rval < 0) {
				rval = -ENODEV;
				goto out_power_off;
			}
			sensor->binning_subtypes[i] =
				*(struct smiapp_binning_subtype *)&val;

			dev_dbg(&client->dev, "binning %xx%x\n",
				sensor->binning_subtypes[i].horizontal,
				sensor->binning_subtypes[i].vertical);
		}
	}
	sensor->binning_horizontal = 1;
	sensor->binning_vertical = 1;

	if (device_create_file(&client->dev, &dev_attr_ident) != 0) {
		dev_err(&client->dev, "sysfs ident entry creation failed\n");
		rval = -ENOENT;
		goto out_power_off;
	}
	/* SMIA++ NVM initialization - it will be read from the sensor
	 * when it is first requested by userspace.
	 */
	if (sensor->minfo.smiapp_version && sensor->hwcfg->nvm_size) {
		sensor->nvm = devm_kzalloc(&client->dev,
				sensor->hwcfg->nvm_size, GFP_KERNEL);
		if (sensor->nvm == NULL) {
			rval = -ENOMEM;
			goto out_cleanup;
		}

		if (device_create_file(&client->dev, &dev_attr_nvm) != 0) {
			dev_err(&client->dev, "sysfs nvm entry failed\n");
			rval = -EBUSY;
			goto out_cleanup;
		}
	}

	/* We consider this as profile 0 sensor if any of these are zero. */
	if (!sensor->limits[SMIAPP_LIMIT_MIN_OP_SYS_CLK_DIV] ||
	    !sensor->limits[SMIAPP_LIMIT_MAX_OP_SYS_CLK_DIV] ||
	    !sensor->limits[SMIAPP_LIMIT_MIN_OP_PIX_CLK_DIV] ||
	    !sensor->limits[SMIAPP_LIMIT_MAX_OP_PIX_CLK_DIV]) {
		sensor->minfo.smiapp_profile = SMIAPP_PROFILE_0;
	} else if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
		   != SMIAPP_SCALING_CAPABILITY_NONE) {
		if (sensor->limits[SMIAPP_LIMIT_SCALING_CAPABILITY]
		    == SMIAPP_SCALING_CAPABILITY_HORIZONTAL)
			sensor->minfo.smiapp_profile = SMIAPP_PROFILE_1;
		else
			sensor->minfo.smiapp_profile = SMIAPP_PROFILE_2;
		sensor->scaler = &sensor->ssds[sensor->ssds_used];
		sensor->ssds_used++;
	} else if (sensor->limits[SMIAPP_LIMIT_DIGITAL_CROP_CAPABILITY]
		   == SMIAPP_DIGITAL_CROP_CAPABILITY_INPUT_CROP) {
		sensor->scaler = &sensor->ssds[sensor->ssds_used];
		sensor->ssds_used++;
	}
	sensor->binner = &sensor->ssds[sensor->ssds_used];
	sensor->ssds_used++;
	sensor->pixel_array = &sensor->ssds[sensor->ssds_used];
	sensor->ssds_used++;

	sensor->scale_m = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];

	/* prepare PLL configuration input values */
	sensor->pll.bus_type = SMIAPP_PLL_BUS_TYPE_CSI2;
	sensor->pll.csi2.lanes = sensor->hwcfg->lanes;
	sensor->pll.ext_clk_freq_hz = sensor->hwcfg->ext_clk;
	sensor->pll.scale_n = sensor->limits[SMIAPP_LIMIT_SCALER_N_MIN];
	/* Profile 0 sensors have no separate OP clock branch. */
	if (sensor->minfo.smiapp_profile == SMIAPP_PROFILE_0)
		sensor->pll.flags |= SMIAPP_PLL_FLAG_NO_OP_CLOCKS;

	smiapp_create_subdev(sensor, sensor->scaler, "scaler", 2);
	smiapp_create_subdev(sensor, sensor->binner, "binner", 2);
	smiapp_create_subdev(sensor, sensor->pixel_array, "pixel_array", 1);

	dev_dbg(&client->dev, "profile %d\n", sensor->minfo.smiapp_profile);

	sensor->pixel_array->sd.entity.function = MEDIA_ENT_F_CAM_SENSOR;

	rval = smiapp_init_controls(sensor);
	if (rval < 0)
		goto out_cleanup;

	rval = smiapp_call_quirk(sensor, init);
	if (rval)
		goto out_cleanup;

	rval = smiapp_get_mbus_formats(sensor);
	if (rval) {
		rval = -ENODEV;
		goto out_cleanup;
	}

	rval = smiapp_init_late_controls(sensor);
	if (rval) {
		rval = -ENODEV;
		goto out_cleanup;
	}

	mutex_lock(&sensor->mutex);
	rval = smiapp_update_mode(sensor);
	mutex_unlock(&sensor->mutex);
	if (rval) {
		dev_err(&client->dev, "update mode failed\n");
		goto out_cleanup;
	}

	sensor->streaming = false;
	sensor->dev_init_done = true;

	smiapp_power_off(sensor);
3055

3056 3057 3058 3059 3060
	rval = media_entity_pads_init(&sensor->src->sd.entity, 2,
				 sensor->src->pads);
	if (rval < 0)
		goto out_media_entity_cleanup;

3061 3062 3063 3064 3065 3066 3067 3068 3069
	rval = v4l2_async_register_subdev(&sensor->src->sd);
	if (rval < 0)
		goto out_media_entity_cleanup;

	return 0;

out_media_entity_cleanup:
	media_entity_cleanup(&sensor->src->sd.entity);

3070 3071 3072 3073 3074
out_cleanup:
	smiapp_cleanup(sensor);

out_power_off:
	smiapp_power_off(sensor);
3075
	return rval;
Sakari Ailus's avatar
Sakari Ailus committed
3076 3077
}

3078
static int smiapp_remove(struct i2c_client *client)
Sakari Ailus's avatar
Sakari Ailus committed
3079 3080 3081 3082 3083
{
	struct v4l2_subdev *subdev = i2c_get_clientdata(client);
	struct smiapp_sensor *sensor = to_smiapp_sensor(subdev);
	unsigned int i;

3084 3085
	v4l2_async_unregister_subdev(subdev);

Sakari Ailus's avatar
Sakari Ailus committed
3086
	if (sensor->power_count) {
3087
		gpiod_set_value(sensor->xshutdown, 0);
3088
		clk_disable_unprepare(sensor->ext_clk);
Sakari Ailus's avatar
Sakari Ailus committed
3089 3090 3091 3092 3093
		sensor->power_count = 0;
	}

	for (i = 0; i < sensor->ssds_used; i++) {
		v4l2_device_unregister_subdev(&sensor->ssds[i].sd);
3094
		media_entity_cleanup(&sensor->ssds[i].sd.entity);
Sakari Ailus's avatar
Sakari Ailus committed
3095
	}
3096
	smiapp_cleanup(sensor);
Sakari Ailus's avatar
Sakari Ailus committed
3097 3098 3099 3100

	return 0;
}

3101 3102 3103 3104
static const struct of_device_id smiapp_of_table[] = {
	{ .compatible = "nokia,smia" },
	{ },
};
3105
MODULE_DEVICE_TABLE(of, smiapp_of_table);
3106

Sakari Ailus's avatar
Sakari Ailus committed
3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119
static const struct i2c_device_id smiapp_id_table[] = {
	{ SMIAPP_NAME, 0 },
	{ },
};
MODULE_DEVICE_TABLE(i2c, smiapp_id_table);

static const struct dev_pm_ops smiapp_pm_ops = {
	.suspend	= smiapp_suspend,
	.resume		= smiapp_resume,
};

static struct i2c_driver smiapp_i2c_driver = {
	.driver	= {
3120
		.of_match_table = smiapp_of_table,
Sakari Ailus's avatar
Sakari Ailus committed
3121 3122 3123 3124
		.name = SMIAPP_NAME,
		.pm = &smiapp_pm_ops,
	},
	.probe	= smiapp_probe,
3125
	.remove	= smiapp_remove,
Sakari Ailus's avatar
Sakari Ailus committed
3126 3127 3128 3129 3130
	.id_table = smiapp_id_table,
};

module_i2c_driver(smiapp_i2c_driver);

3131
MODULE_AUTHOR("Sakari Ailus <sakari.ailus@iki.fi>");
Sakari Ailus's avatar
Sakari Ailus committed
3132 3133
MODULE_DESCRIPTION("Generic SMIA/SMIA++ camera module driver");
MODULE_LICENSE("GPL");